00001 #pragma once
00002
00003 template<class T>
00004 class StringUtilsImpl
00005 {
00006 public:
00007
00008 static int replace(std::basic_string<T>& s, const std::basic_string<T>& from, const std::basic_string<T>& to)
00009 {
00010 int ii = 0;
00011 int result = 0;
00012
00013 if (s.length() != 0 && from.length() != 0)
00014 {
00015 while ((ii = s.find(from, ii)) != s.npos)
00016 {
00017 s.replace(ii, from.length(), to);
00018 ii += (abs((int)(to.length() - from.length())) + 1);
00019 result ++;
00020 }
00021 }
00022 return result;
00023 }
00024
00025 static void tokenize(const std::basic_string<T>& ss, std::vector<std::basic_string<T>>& tokens, const std::basic_string<T>& delim)
00026 {
00027 tokens.clear();
00028 int begin = 0, end = 0;
00029 std::basic_string<T> temp;
00030
00031 temp = ss;
00032
00033 while ( (end = temp.find(delim)) != std::basic_string<T>::npos )
00034 {
00035 tokens.push_back( temp.substr(0, end) );
00036 begin = end + delim.length();
00037 temp = temp.substr(begin, temp.length() - begin);
00038 }
00039
00040 tokens.push_back(temp.substr(0, temp.length()));
00041 }
00042
00043 static std::basic_string<T> join(const std::vector<std::basic_string<T>>& tokens, const std::basic_string<T>& delim)
00044 {
00045 std::basic_string<T> ss;
00046
00047 for (unsigned int i = 0; i < tokens.size(); i++)
00048 {
00049 if (i > 0) ss.append(delim);
00050 ss.append(tokens[i]);
00051 }
00052
00053 return ss;
00054 }
00055
00056 static std::basic_string<T> join(const std::list<std::basic_string<T>>& tokens, const std::basic_string<T>& delim)
00057 {
00058 std::basic_string<T> ss;
00059
00060 int i = 0;
00061 for each (const std::basic_string<T>& token in tokens)
00062 {
00063 if (i > 0) ss.append(delim);
00064 ss.append(token);
00065 i++;
00066 }
00067
00068 return ss;
00069 }
00070
00071 static void tokenizeWithSkip(const std::basic_string<T>& ss, std::vector<std::basic_string<T>>& tokens, const std::basic_string<T>& delims)
00072 {
00073
00074 std::basic_string<T>::size_type lastpos = ss.find_first_not_of(delims, 0);
00075
00076 std::basic_string<T>::size_type pos = ss.find_first_of(delims, lastpos);
00077
00078 while (std::string::npos != pos || std::string::npos != lastpos)
00079 {
00080
00081 tokens.push_back(ss.substr(lastpos, pos - lastpos));
00082
00083 lastpos = ss.find_first_not_of(delims, pos);
00084
00085 pos = ss.find_first_of(delims, lastpos);
00086 }
00087 }
00088
00089
00090 static void tokenizeOnChar(const std::basic_string<T>& ss, std::vector<std::basic_string<T>>& tokens, const std::basic_string<T>& delims)
00091 {
00092 tokens.clear();
00093 int begin = 0, end = 0;
00094
00095 for( unsigned int i = 0; i < ss.length(); i++)
00096 {
00097 if ( delims.find(ss.substr(i, 1), 0) == std::basic_string<T>::npos )
00098 {
00099 end++;
00100 }
00101 else
00102 {
00103 tokens.push_back(ss.substr(begin, (end - begin)));
00104 begin = ++end;
00105 }
00106 }
00107 tokens.push_back(ss.substr(begin, (end - begin)));
00108 }
00109
00110 static void ltrim(std::basic_string<T>& ss, const std::basic_string<T>& whitespaces)
00111 {
00112 ss.erase(0, ss.find_first_not_of(whitespaces));
00113 }
00114
00115 static void rtrim(std::basic_string<T>& ss, const std::basic_string<T>& whitespaces)
00116 {
00117 ss.erase(ss.find_last_not_of(whitespaces) + 1);
00118 }
00119
00120 static void lrtrim(std::basic_string<T>& ss, const std::basic_string<T>& whitespaces)
00121 {
00122 ltrim(ss, whitespaces);
00123 rtrim(ss, whitespaces);
00124 }
00125
00126 static void lrtrimcrlf(std::basic_string<T>& ss, const std::basic_string<T>& whitespaces)
00127 {
00128 ltrim(ss, whitespaces);
00129 rtrim(ss, whitespaces);
00130 }
00131
00132 static void uppercase(std::basic_string<T>& ss)
00133 {
00134 std::transform(ss.begin(), ss.end(), ss.begin(), (int(*)(int)) toupper);
00135 }
00136
00137 static void lowercase(std::basic_string<T>& ss)
00138 {
00139 std::transform(ss.begin(), ss.end(), ss.begin(), (int(*)(int)) tolower);
00140 }
00141
00142 static bool comparei(const std::basic_string<T>& ss1, const std::basic_string<T>& ss2)
00143 {
00144 std::basic_string<T> s1, s2;
00145 s1 = ss1; s2 = ss2;
00146 lowercase(s1);
00147 lowercase(s2);
00148 return (s1 == s2);
00149 }
00150
00151 static bool isNumber(const std::basic_string<T>& ss)
00152 {
00153 unsigned int i = 0;
00154
00155 if( ss.length() > 1 && ss[0] == '-') i = 1;
00156 if( ss.length() == i ) return false;
00157
00158 for( ; i < ss.length(); i++ )
00159 {
00160 if(! isdigit(ss[i]))
00161 {
00162 return false;
00163 }
00164 }
00165
00166 return true;
00167 }
00168
00169 static bool startsWith(const std::basic_string<T>& what, const std::basic_string<T>& with)
00170 {
00171 if (what.length() < with.length())
00172 return false;
00173 if (what.substr(0, with.length()) == with)
00174 return true;
00175 return false;
00176 }
00177
00178 static bool endsWith(const std::basic_string<T>& what, const std::basic_string<T>& with)
00179 {
00180 if (what.length() < with.length())
00181 return false;
00182 if (what.substr(what.length() - with.length(), what.length()) == with)
00183 return true;
00184 return false;
00185 }
00186 };