diff --git a/src/cppcommon/str_functs.cpp b/src/cppcommon/str_functs.cpp index a0075ad..33c9246 100644 --- a/src/cppcommon/str_functs.cpp +++ b/src/cppcommon/str_functs.cpp @@ -6,227 +6,231 @@ namespace CPPCOMMON { - //http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf - string string_format(const string fmt, ...) - { - int size = 100; - std::string str; - va_list ap; - while (1) { - str.resize(size); - va_start(ap, fmt); - int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap); - va_end(ap); - if (n > -1 && n < size) { - str.resize(n); - return str; - } - if (n > -1) - size = n + 1; - else - size *= 2; - } - return str; - } + //http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf + string string_format(const string fmt, ...) + { + int size = 100; + std::string str; + va_list ap; + while (1) { + str.resize(size); + va_start(ap, fmt); + int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap); + va_end(ap); + if (n > -1 && n < size) { + str.resize(n); + return str; + } + if (n > -1) + size = n + 1; + else + size *= 2; + } + return str; + } - string joinStr(const vector& src, const string& connectorStr) - { - string res = ""; - int len = src.size(); - if(0 == len) - { - return ""; - } - for(int i = 0; i < len - 1; i++) - { - res += stripStr(src[i]); - res += connectorStr; - } - res += stripStr(src[len-1]); - return res; - } - vector splitStr(const string& source, const string& pattern) - { - vector res; - splitStr(source, res, pattern); - return res; - } - void splitStr(const string& source, vector& out_vec, const string& pattern) - { - if(source.empty()) - { - return; - } - out_vec.clear(); - string s = source + pattern; - string::size_type pos; - uint length = s.size(); + string joinStr(const vector& src, const string& connectorStr) + { + string res = ""; + int len = src.size(); + if(0 == len) + { + return ""; + } + for(int i = 0; i < len - 1; i++) + { + res += stripStr(src[i]); + res += connectorStr; + } + res += stripStr(src[len-1]); + return res; + } - for(uint i = 0; i < length; i++) - { - pos = s.find(pattern, i); - if(pos < length) - { - string tmp = stripStr(s.substr(i, pos - i)); - if("" != tmp) - { - out_vec.push_back(tmp); - } - i = pos + pattern.size() - 1; - } - } - } + vector splitStr(const string& source, const string& pattern) + { + vector res; + splitStr(source, res, pattern); + return res; + } - string stripStr(const string& str, const string& patternStr) - { - if(str.empty()) - { - return str; - } - string::size_type posL = str.find_first_not_of(patternStr); - if(string::npos == posL) - { - return str; - } - string::size_type posR = str.find_last_not_of(patternStr); - return str.substr(posL, posR - posL + 1); + bool splitStr(const string& source, vector& res, const string& pattern) + { + if(source.empty()) + { + return false; + } + res.clear(); - } - - - //http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring - // trim from start - std::string <rim(std::string &s) - { - s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(std::isspace)))); - return s; - } - - // trim from end - std::string &rtrim(std::string &s) - { - s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(std::isspace))).base(), s.end()); - return s; - } - - // trim from both ends - std::string &trim(std::string &s) - { - return ltrim(rtrim(s)); - } - - bool splitStrMultiPatterns( - const string& strSrc, - vector& outVec, - const vector& patterns - ) - { - char transChar = '#'; - uint transLenThreshold = 10; - string transStr; - transStr += transChar; - while(strSrc.find(transStr) != string::npos) - { - transStr += transChar; - if(transStr.size() > transLenThreshold) - return false; - } - string strSrcMutable = strSrc; - for(uint i = 0; i < patterns.size(); i++) - { - strSrcMutable = replaceStr(strSrcMutable, patterns[i], transStr); - } - splitStr(strSrcMutable, outVec, transStr); - return true; - } - - string upperStr(const string& strIn) - { - string str = strIn; - transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper); - return str; - } - - string lowerStr(const string& strIn) - { - string str = strIn; - transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower); - return str; - } - - string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count) - { - string strRet = strSrc; - size_t pos = 0; - int l_count = 0; - if(-1 == count) - count = strRet.size(); - while((pos = strRet.find(oldStr, pos)) != string::npos) - { - strRet.replace(pos, oldStr.size(), newStr); - if(++l_count >= count) - break; - pos += newStr.size(); - } - return strRet; - } - - unsigned int countStrDistance(const string& A, const string& B) - { - unsigned int lenA = A.size(); - unsigned int lenB = B.size(); - unsigned int len = (lenA < lenB ? lenA : lenB); - unsigned int res = lenA + lenB - 2 * len; - for(size_t i = 0; i < len; i++) - { - if(A[i] != B[i]) - res++; - } - return res; - } - - unsigned int countStrSimilarity(const string& A, const string& B) - { - unsigned int lenA = A.size(); - unsigned int lenB = B.size(); - unsigned int len = (lenA < lenB ? lenA : lenB); - unsigned int res = 0; - for(size_t i = 0; i < len; i++) - { - if(A[i] == B[i]) - res++; - } - return res; - } - - //unicode str to vec - bool uniStrToVec(const string& str, Unicode& vec) - { - vec.clear(); - if(str.empty() || str.size() % 2) + size_t start = source.find_first_not_of(pattern); + size_t end; + if(string::npos == start) { return false; } - for(uint i = 0; i < str.size(); i+=2) + while(string::npos != start) { - vec.push_back(twocharToUint16(str[i], str[i + 1])); + end = source.find_first_of(pattern, start); + if(string::npos == end) + { + 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; + } - return true; - } + string stripStr(const string& str, const string& patternStr) + { + if(str.empty()) + { + return str; + } + string::size_type posL = str.find_first_not_of(patternStr); + if(string::npos == posL) + { + return str; + } + string::size_type posR = str.find_last_not_of(patternStr); + return str.substr(posL, posR - posL + 1); - //unicode vec to str - string uniVecToStr(const Unicode& vec) - { - string res(""); - for(uint i = 0; i < vec.size(); i++) - { - pair pa = uint16ToChar2(vec[i]); - res += pa.first; - res += pa.second; - } - return res; - } + } + + + //http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring + // trim from start + std::string <rim(std::string &s) + { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(std::isspace)))); + return s; + } + + // trim from end + std::string &rtrim(std::string &s) + { + s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(std::isspace))).base(), s.end()); + return s; + } + + // trim from both ends + std::string &trim(std::string &s) + { + return ltrim(rtrim(s)); + } + + bool splitStrMultiPatterns( + const string& strSrc, + vector& outVec, + const vector& patterns + ) + { + char transChar = '#'; + uint transLenThreshold = 10; + string transStr; + transStr += transChar; + while(strSrc.find(transStr) != string::npos) + { + transStr += transChar; + if(transStr.size() > transLenThreshold) + return false; + } + string strSrcMutable = strSrc; + for(uint i = 0; i < patterns.size(); i++) + { + strSrcMutable = replaceStr(strSrcMutable, patterns[i], transStr); + } + splitStr(strSrcMutable, outVec, transStr); + return true; + } + + string upperStr(const string& strIn) + { + string str = strIn; + transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper); + return str; + } + + string lowerStr(const string& strIn) + { + string str = strIn; + transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower); + return str; + } + + string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count) + { + string strRet = strSrc; + size_t pos = 0; + int l_count = 0; + if(-1 == count) + count = strRet.size(); + while((pos = strRet.find(oldStr, pos)) != string::npos) + { + strRet.replace(pos, oldStr.size(), newStr); + if(++l_count >= count) + break; + pos += newStr.size(); + } + return strRet; + } + + unsigned int countStrDistance(const string& A, const string& B) + { + unsigned int lenA = A.size(); + unsigned int lenB = B.size(); + unsigned int len = (lenA < lenB ? lenA : lenB); + unsigned int res = lenA + lenB - 2 * len; + for(size_t i = 0; i < len; i++) + { + if(A[i] != B[i]) + res++; + } + return res; + } + + unsigned int countStrSimilarity(const string& A, const string& B) + { + unsigned int lenA = A.size(); + unsigned int lenB = B.size(); + unsigned int len = (lenA < lenB ? lenA : lenB); + unsigned int res = 0; + for(size_t i = 0; i < len; i++) + { + if(A[i] == B[i]) + res++; + } + return res; + } + + //unicode str to vec + bool uniStrToVec(const string& str, Unicode& vec) + { + vec.clear(); + if(str.empty() || str.size() % 2) + { + return false; + } + for(uint i = 0; i < str.size(); i+=2) + { + vec.push_back(twocharToUint16(str[i], str[i + 1])); + } + + return true; + } + + //unicode vec to str + string uniVecToStr(const Unicode& vec) + { + string res(""); + for(uint i = 0; i < vec.size(); i++) + { + pair pa = uint16ToChar2(vec[i]); + res += pa.first; + res += pa.second; + } + return res; + } } @@ -236,78 +240,20 @@ using namespace CPPCOMMON; using namespace std; int main() { - //string s = " \t\n1 a h \n"; - //cout<<"["< vec; - //splitStr("1 3 4", vec); - //char * a[] = {"3","jaj","ads"}; - //vector pats(a,a+3); - //vec.clear(); - //splitStrMultiPattern("1 #3 jajs5 asdf3ads 4", vec, pats); - //for(int i=0;i tmp; - //tmp.push_back("1"); - ////tmp.push_back("2"); - ////tmp.clear(); - //cout< vec; + splitStr("1 3 4", vec); + for(uint i =0;i < vec.size(); i++) + { + cout< namespace CPPCOMMON { - using namespace std; - string string_format(const string fmt, ...) ; - string joinStr(const vector& source, const string& connector); - vector splitStr(const string& source, const string& pattern = " \t\n"); - void splitStr(const string& source, vector& out_vec, const string& pattern = " \t\n"); - bool splitStrMultiPatterns( - const string& strSrc, - vector& outVec, - const vector& patterns - ); - string upperStr(const string& str); - string lowerStr(const string& str); - string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count = -1); - string stripStr(const string& str, const string& patternstr = " \n\t"); - std::string <rim(std::string &s) ; - std::string &rtrim(std::string &s) ; - std::string &trim(std::string &s) ; - unsigned int countStrDistance(const string& A, const string& B); - unsigned int countStrSimilarity(const string& A, const string& B); + using namespace std; + string string_format(const string fmt, ...) ; + string joinStr(const vector& source, const string& connector); + vector splitStr(const string& source, const string& pattern = " \t\n"); + bool splitStr(const string& source, vector& res, const string& pattern = " \t\n"); + bool splitStrMultiPatterns( + const string& strSrc, + vector& outVec, + const vector& patterns + ); + string upperStr(const string& str); + string lowerStr(const string& str); + string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count = -1); + string stripStr(const string& str, const string& patternstr = " \n\t"); + std::string <rim(std::string &s) ; + std::string &rtrim(std::string &s) ; + std::string &trim(std::string &s) ; + unsigned int countStrDistance(const string& A, const string& B); + unsigned int countStrSimilarity(const string& A, const string& B); - bool uniStrToVec(const string& str, Unicode& vec); - string uniVecToStr(const Unicode& vec); + bool uniStrToVec(const string& str, Unicode& vec); + string uniVecToStr(const Unicode& vec); - inline uint16_t twocharToUint16(char high, char low) - { - return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff)); - } + inline uint16_t twocharToUint16(char high, char low) + { + return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff)); + } - inline pair uint16ToChar2(uint16_t in) - { - pair res; - res.first = (in>>8) & 0x00ff; //high - res.second = (in) & 0x00ff; //low - return res; - } + inline pair uint16ToChar2(uint16_t in) + { + pair res; + res.first = (in>>8) & 0x00ff; //high + res.second = (in) & 0x00ff; //low + return res; + } - inline void printUnicode(const Unicode& unicode) - { - cout< str.length()) - { - return false; - } - return 0 == str.compare(0, prefix.length(), 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); - } - + 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