C++ Utils
0.1
|
00001 00037 #ifndef UTILS_STRINGUTILS_H 00038 #define UTILS_STRINGUTILS_H 00039 00040 #include <algorithm> 00041 #include <functional> 00042 #include <cctype> 00043 #include <cstring> 00044 #include <locale> 00045 #include <sstream> 00046 #include <string> 00047 #include <vector> 00048 00052 namespace utils 00053 { 00054 00058 class StringUtils 00059 { 00060 public: 00061 00067 static bool replace(std::string& str, const std::string& from, const std::string& to) { 00068 size_t start_pos = str.find(from); 00069 if(start_pos == std::string::npos) 00070 return false; 00071 str.replace(start_pos, from.length(), to); 00072 return true; 00073 } 00074 00078 static bool replaceLast(std::string& str, const std::string& from, const std::string& to) { 00079 size_t start_pos = str.rfind(from); 00080 if (start_pos == std::string::npos) 00081 return false; 00082 str.replace(start_pos, from.length(), to); 00083 return true; 00084 } 00085 00086 static bool startsWith(const std::string &str, const std::string &prefix) 00087 { 00088 return str.size() >= prefix.size() && 00089 str.compare(0, prefix.size(), prefix) == 0; 00090 } 00091 00097 static bool endsWith(const std::string &str, const std::string &suffix) 00098 { 00099 return str.size() >= suffix.size() && 00100 str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; 00101 } 00102 00107 template<typename T> 00108 static std::string toString(T value) 00109 { 00110 std::ostringstream ss; 00111 ss << value; 00112 return ss.str(); 00113 } 00114 00120 template<typename T> 00121 static T parse(const std::string &str) 00122 { 00123 T result; 00124 std::istringstream(str) >> result; 00125 00126 return result; 00127 } 00128 00135 template<typename T> 00136 static T parse(const std::string &str, bool adavanced) 00137 { 00138 // By default the advanced mode is disabled for all datatypes 00139 return parse<T>(str); 00140 } 00141 00142 template<typename T> inline 00143 static std::vector<T> parseArray(const std::string &str) 00144 { 00145 std::vector<T> elems; 00146 std::istringstream f(str); 00147 std::string s; 00148 while (std::getline(f, s, ':')) 00149 elems.push_back(parse<T>(s)); 00150 00151 return elems; 00152 } 00153 00157 static void toUpper(char* s) 00158 { 00159 for (size_t i = 0; s[i]; i++) 00160 s[i] = toupper(s[i]); 00161 } 00162 00166 static void toUpper(std::string &str) 00167 { 00168 for (size_t i = 0; str[i]; i++) 00169 str[i] = toupper(str[i]); 00170 } 00171 00175 static void toLower(char* s) 00176 { 00177 for (size_t i = 0; s[i]; i++) 00178 s[i] = tolower(s[i]); 00179 } 00180 00184 static void toLower(std::string &str) 00185 { 00186 for (size_t i = 0; str[i]; i++) 00187 str[i] = tolower(str[i]); 00188 } 00189 00195 static std::string <rim(std::string &s) { 00196 s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace)))); 00197 return s; 00198 } 00199 00205 static std::string &rtrim(std::string &s) { 00206 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end()); 00207 return s; 00208 } 00209 00215 static std::string &trim(std::string &s) { 00216 return ltrim(rtrim(s)); 00217 } 00218 00224 template<typename T> 00225 static std::string join(const std::vector<T> &v, const std::string &token) { 00226 std::ostringstream result; 00227 for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++){ 00228 if (i != v.begin()) 00229 result << token; 00230 result << *i; 00231 } 00232 00233 return result.str(); 00234 } 00235 00241 static std::vector<std::string> split(const std::string &str, char delim) 00242 { 00243 std::vector<std::string> elems; 00244 std::stringstream ss(str); 00245 00246 std::string item; 00247 while (getline(ss, item, delim)) 00248 elems.push_back(item); 00249 00250 return elems; 00251 } 00252 }; 00253 00254 template<> inline 00255 std::string StringUtils::parse(const std::string &str) 00256 { 00257 return str; 00258 } 00259 00260 template<> inline 00261 const char* StringUtils::parse(const std::string &str) 00262 { 00263 return str.c_str(); 00264 } 00265 00266 template<> inline 00267 bool StringUtils::parse(const std::string &str, bool advanced) 00268 { 00269 std::string s = str; 00270 toLower(s); 00271 00272 if (s == "on" 00273 || s == "yes") 00274 return true; 00275 00276 return parse<bool>(str); 00277 } 00278 00279 } 00280 00281 #endif /* UTILS_STRINGUTILS_H */