update cppcommon for endswith in str_funst.h

This commit is contained in:
gwdwyy 2013-08-31 16:14:25 +08:00
parent 8b3a192037
commit 2319fd9307
2 changed files with 29 additions and 15 deletions

View File

@ -228,11 +228,6 @@ namespace CPPCOMMON
return res;
}
bool strStartsWith(const string& str, const string& prefix)
{
return str.substr(0, prefix.size()) == prefix;
}
}
#ifdef TEST_STR_FUNCTS
@ -302,15 +297,17 @@ int main()
// s = utf8ToGbk(s);
// cout<<s<<endl;
//}
//cout<<strStartsWith("--help","--")<<endl;
//cout<<strStartsWith("--help","-")<<endl;
//cout<<strStartsWith("--help","he")<<endl;
//cout<<strStartsWith("help","help")<<endl;
//cout<<strStartsWith("","help")<<endl;
//cout<<strStartsWith("hel","")<<endl;
string s(" helloword heh\t");
string b;
cout<<trim(b)<<"11"<<endl;
cout<<strStartsWith("--help","--")<<endl;
cout<<strStartsWith("--help","-")<<endl;
cout<<strStartsWith("--help","he")<<endl;
cout<<strStartsWith("help","help")<<endl;
cout<<strStartsWith("","help")<<endl;
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

View File

@ -63,7 +63,24 @@ namespace CPPCOMMON
cout<<uniVecToStr(unicode)<<endl;
}
bool strStartsWith(const string& str, const string& prefix);
inline bool strStartsWith(const string& str, const string& prefix)
{
//return str.substr(0, prefix.size()) == prefix;
if(prefix.length() > str.length())
{
return false;
}
return 0 == str.compare(0, prefix.length(), prefix);
}
inline bool strEndsWith(const string& str, const string& suffix)
{
if(suffix.length() > str.length())
{
return false;
}
return 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix);
}
}
#endif