mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
update cppcommon for splitStr
This commit is contained in:
parent
2319fd9307
commit
a8374bd2b7
@ -45,36 +45,40 @@ namespace CPPCOMMON
|
||||
res += stripStr(src[len-1]);
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<string> splitStr(const string& source, const string& pattern)
|
||||
{
|
||||
vector<string> res;
|
||||
splitStr(source, res, pattern);
|
||||
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())
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
out_vec.clear();
|
||||
string s = source + pattern;
|
||||
string::size_type pos;
|
||||
uint length = s.size();
|
||||
res.clear();
|
||||
|
||||
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);
|
||||
if(pos < length)
|
||||
return false;
|
||||
}
|
||||
while(string::npos != start)
|
||||
{
|
||||
string tmp = stripStr(s.substr(i, pos - i));
|
||||
if("" != tmp)
|
||||
end = source.find_first_of(pattern, start);
|
||||
if(string::npos == end)
|
||||
{
|
||||
out_vec.push_back(tmp);
|
||||
}
|
||||
i = pos + pattern.size() - 1;
|
||||
res.push_back(source.substr(start));
|
||||
return true;
|
||||
}
|
||||
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)
|
||||
@ -236,67 +240,12 @@ using namespace CPPCOMMON;
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
//string s = " \t\n1 a h \n";
|
||||
//cout<<"["<<stripStr(s)<<"]"<<endl;
|
||||
//cout<<countStrDistance("Aheheh","heheh1212")<<endl;
|
||||
//cout<<joinStr(splitStr(s), ",")<<endl;
|
||||
//vector<string> vec;
|
||||
//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;
|
||||
//}
|
||||
vector<string> vec;
|
||||
splitStr("1 3 4", vec);
|
||||
for(uint i =0;i < vec.size(); i++)
|
||||
{
|
||||
cout<<vec[i]<<endl;
|
||||
}
|
||||
cout<<strStartsWith("--help","--")<<endl;
|
||||
cout<<strStartsWith("--help","-")<<endl;
|
||||
cout<<strStartsWith("--help","he")<<endl;
|
||||
@ -305,9 +254,6 @@ int main()
|
||||
cout<<strStartsWith("hel","")<<endl;
|
||||
cout<<strEndsWith("hel","")<<endl;
|
||||
cout<<strEndsWith("hel","el")<<endl;
|
||||
//string s(" helloword heh\t");
|
||||
//string b;
|
||||
//cout<<trim(b)<<"11"<<endl;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
@ -25,7 +25,7 @@ namespace CPPCOMMON
|
||||
string string_format(const string fmt, ...) ;
|
||||
string joinStr(const vector<string>& source, const string& connector);
|
||||
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(
|
||||
const string& strSrc,
|
||||
vector<string>& outVec,
|
||||
|
Loading…
x
Reference in New Issue
Block a user