/****************************************************************************
@File Name: stringop.h
@Author:
@mail:
@Created Time: Tue 04 Apr 2017 05:25:09 PM CST
****************************************************************************/
#ifndef STRING_OP_H
#define STRING_OP_H
#include <string>
namespace stringop
{
using namespace std;
class StringOp
{
public:
/*
* @brief replace oldstr with new str in str
* */
static void replace(string &str,
const string &oldstr,
const string &newstr);
/*
* @brief parse value from <type:filename:sha><...>...<...> to
* @types:type0;type1;...
* @shas:sha0;sha1;...
* @names:name0;name1;...
* */
static void parsevalue(const string &value,
string &types,
string &shas,
string &names);
};
}
#endif
/****************************************************************************
@File Name: stringop.h
@Author:
@mail:
@Created Time: Tue 04 Apr 2017 05:25:09 PM CST
****************************************************************************/
#include "stringop.h"
namespace stringop
{
/*
* @brief replace oldstr with new str in str
* */
void StringOp::replace(string &str,
const string &oldstr,
const string &newstr)
{
string::size_type pos = 0;
string::size_type size = str.size();
string::size_type oldsize = oldstr.size();
string::size_type newsize = newstr.size();
if(oldsize != newsize) return;
while(pos < size)
{
if((pos = str.find(oldstr, pos)) != string::npos)
{
str.replace(pos, oldsize, newstr);
pos += newsize;
}
else break;
}
}
/*
* @brief parse value from <type:filename:sha><...>...<...> to
* @types:type0;type1;...
* @shas:sha0;sha1;...
* @names:name0;name1;...
* */
void StringOp::parsevalue(const string &value,
string &types,
string &shas,
string &names)
{
int startpos; // pointer to '<'
int endpos; // pointer to '>'
int size = value.size();
int pos = 0;
int i;
int mode; // 0 -- type 1 -- sha 2 -- name
types.clear(); // pre treated
shas.clear();
names.clear();
while(pos < size)
{
startpos = value.find("<", pos);
endpos = value.find(">", pos);
if(startpos >= size || endpos >= size || startpos >= endpos) break;
mode = 0;
// transfer <...>
for(i = startpos + 1;i < endpos;i++)
{
switch(mode)
{
case 0: // type
if(':' == value[i])
{
types += ";";
++mode;
}
else types += value[i];
break;
case 1: // sha
if(':' == value[i])
{
shas += ";";
++mode;
}
else shas += value[i];
break;
case 2: // name
if(':' == value[i])
{
names += ";";
++mode;
}
else names += value[i];
break;
}
if(mode >= 3) break;
}
if(';' != names[names.size() - 1]) names += ";";
pos = endpos + 1;
}
// put off the last ';'
types[types.size() - 1] = 0;
shas[shas.size() - 1] = 0;
names[names.size() - 1] = 0;
}
}
/****************************************************************************
@File Name: main.cpp
@Author:
@mail:
@Created Time: Tue 04 Apr 2017 05:32:16 PM CST
****************************************************************************/
#include "stringop.h"
#include <iostream>
using namespace stringop;
int main()
{
string str = "abc99abcabcdfgabchhhJJJJJabc788Acbabckkkk111111111abc";
string str0= "abc";
string str1= "LLL";
StringOp::replace(str, str0, str1);
cout << str << endl;
string value = "<type0:filename0:sha0><type1:filename1:sha1>";
string types;
string shas;
string names;
StringOp::parsevalue(value, types, shas, names);
cout << types << endl;
cout << shas << endl;
cout << names << endl;
return 0;
}
CC=g++
all:
$(CC) -std=c++11 -g -o stringopTest stringop.h stringop.cpp main.cpp
stringop
最新推荐文章于 2022-05-24 22:19:50 发布