update cppcommon for splitStr

This commit is contained in:
gwdwyy 2013-09-04 18:03:24 +08:00
parent 2319fd9307
commit a8374bd2b7
2 changed files with 283 additions and 337 deletions

View File

@ -45,36 +45,40 @@ namespace CPPCOMMON
res += stripStr(src[len-1]); res += stripStr(src[len-1]);
return res; return res;
} }
vector<string> splitStr(const string& source, const string& pattern) vector<string> splitStr(const string& source, const string& pattern)
{ {
vector<string> res; vector<string> res;
splitStr(source, res, pattern); splitStr(source, res, pattern);
return res; return res;
} }
void splitStr(const string& source, vector<string>& out_vec, const string& pattern)
bool splitStr(const string& source, vector<string>& res, const string& pattern)
{ {
if(source.empty()) if(source.empty())
{ {
return; return false;
} }
out_vec.clear(); res.clear();
string s = source + pattern;
string::size_type pos;
uint length = s.size();
for(uint i = 0; i < length; i++) size_t start = source.find_first_not_of(pattern);
size_t end;
if(string::npos == start)
{ {
pos = s.find(pattern, i); return false;
if(pos < length) }
while(string::npos != start)
{ {
string tmp = stripStr(s.substr(i, pos - i)); end = source.find_first_of(pattern, start);
if("" != tmp) if(string::npos == end)
{ {
out_vec.push_back(tmp); res.push_back(source.substr(start));
} return true;
i = pos + pattern.size() - 1;
} }
res.push_back(source.substr(start, end - start));
start = source.find_first_not_of(pattern, end);
} }
return true;
} }
string stripStr(const string& str, const string& patternStr) string stripStr(const string& str, const string& patternStr)
@ -236,67 +240,12 @@ using namespace CPPCOMMON;
using namespace std; using namespace std;
int main() int main()
{ {
//string s = " \t\n1 a h \n"; vector<string> vec;
//cout<<"["<<stripStr(s)<<"]"<<endl; splitStr("1 3 4", vec);
//cout<<countStrDistance("Aheheh","heheh1212")<<endl; for(uint i =0;i < vec.size(); i++)
//cout<<joinStr(splitStr(s), ",")<<endl; {
//vector<string> vec; cout<<vec[i]<<endl;
//splitStr("1 3 4", vec); }
//char * a[] = {"3","jaj","ads"};
//vector<string> pats(a,a+3);
//vec.clear();
//splitStrMultiPattern("1 #3 jajs5 asdf3ads 4", vec, pats);
//for(int i=0;i<vec.size();i++)
//{
// cout<<vec[i]<<endl;
//}
//string s = "1111aaafasfa,asdj.sadhashfhaha";
//upperStr(s);
//cout<<s<<endl;
//
//s = "ab1ba2ab3";
//cout<<replaceStr(s,"ab","###")<<endl;
//ifstream ifile("testdata/dict.txt");
//string line;
//while(getline(ifile, line))
//{
// uint16_t strbuf[1024];
// size_t unilen = utf8ToUnicode(line.c_str(), line.size(), strbuf);
// for(int i = 0; i < unilen; i++)
// {
// // printf("%x\n", strbuf[i]);
// }
// char utf8str[512]={0};
// unicodeToUtf8(strbuf, unilen, utf8str);
// //cout<<strlen(utf8str);
// cout<<utf8str<<endl;
//}
//cout<<string_format("hehe%s11asd%dasf","[here]",2);
//ifstream ifile("testdata/dict.gbk");
//string line;
//Unicode unicode;
//while(getline(ifile, line))
//{
// cout<<line<<endl;
// utf8ToUnicode(line, unicode);
// printUnicode(unicode);
// cout<<unicodeToUtf8(unicode)<<endl;;
//}
//vector<string> tmp;
//tmp.push_back("1");
////tmp.push_back("2");
////tmp.clear();
//cout<<joinStr(tmp, ",")<<endl;
//ifstream ifile("testdata/dict.gbk");
//string line;
//while(getline(ifile, line))
//{
// cout<<line<<endl;
// string s = gbkToUtf8(line);
// s = utf8ToGbk(s);
// cout<<s<<endl;
//}
cout<<strStartsWith("--help","--")<<endl; cout<<strStartsWith("--help","--")<<endl;
cout<<strStartsWith("--help","-")<<endl; cout<<strStartsWith("--help","-")<<endl;
cout<<strStartsWith("--help","he")<<endl; cout<<strStartsWith("--help","he")<<endl;
@ -305,9 +254,6 @@ int main()
cout<<strStartsWith("hel","")<<endl; cout<<strStartsWith("hel","")<<endl;
cout<<strEndsWith("hel","")<<endl; cout<<strEndsWith("hel","")<<endl;
cout<<strEndsWith("hel","el")<<endl; cout<<strEndsWith("hel","el")<<endl;
//string s(" helloword heh\t");
//string b;
//cout<<trim(b)<<"11"<<endl;
return 0; return 0;
} }
#endif #endif

View File

@ -25,7 +25,7 @@ namespace CPPCOMMON
string string_format(const string fmt, ...) ; string string_format(const string fmt, ...) ;
string joinStr(const vector<string>& source, const string& connector); string joinStr(const vector<string>& source, const string& connector);
vector<string> splitStr(const string& source, const string& pattern = " \t\n"); vector<string> splitStr(const string& source, const string& pattern = " \t\n");
void splitStr(const string& source, vector<string>& out_vec, const string& pattern = " \t\n"); bool splitStr(const string& source, vector<string>& res, const string& pattern = " \t\n");
bool splitStrMultiPatterns( bool splitStrMultiPatterns(
const string& strSrc, const string& strSrc,
vector<string>& outVec, vector<string>& outVec,