00001 #ifndef Impala_Basis_String_h
00002 #define Impala_Basis_String_h
00003
00004 #include <string>
00005 #include <algorithm>
00006 #include <iostream>
00007 #include <sstream>
00008 #include <iomanip>
00009 #include <cctype>
00010 #include <cstring>
00011 #include "Basis/Std.h"
00012
00013 namespace Impala
00014 {
00015
00016
00017 typedef std::string String;
00018 typedef const std::string& CString;
00019
00020 inline String
00021 MakeString(const int i)
00022 {
00023 std::ostringstream oss;
00024 oss << i;
00025 return oss.str();
00026 }
00027
00028 inline String
00029 MakeString(const int i, const int width, const char fill = ' ')
00030 {
00031 std::ostringstream oss;
00032 oss << std::setw(width) << std::setfill(fill) << i;
00033 return oss.str();
00034 }
00035
00036 inline String
00037 MakeString(const long i)
00038 {
00039 std::ostringstream oss;
00040 oss << i;
00041 return oss.str();
00042 }
00043
00044 inline String
00045 MakeString(const long i, const int width, const char fill = ' ')
00046 {
00047 std::ostringstream oss;
00048 oss << std::setw(width) << std::setfill(fill) << i;
00049 return oss.str();
00050 }
00051
00052 inline String
00053 MakeString(const unsigned long ul)
00054 {
00055 std::ostringstream oss;
00056 oss << ul;
00057 return oss.str();
00058 }
00059
00060 inline String
00061 MakeString(const unsigned long ul, const int width, const char fill = ' ')
00062 {
00063 std::ostringstream oss;
00064 oss << std::setw(width) << std::setfill(fill) << ul;
00065 return oss.str();
00066 }
00067
00068 inline String
00069 MakeString(const long long i)
00070 {
00071 std::ostringstream oss;
00072 oss << i;
00073 return oss.str();
00074 }
00075
00076 inline String
00077 MakeString(const long long i, int width, char fill = ' ')
00078 {
00079 std::ostringstream oss;
00080 oss << std::setw(width) << std::setfill(fill) << i;
00081 return oss.str();
00082 }
00083
00084 inline String
00085 MakeString(const unsigned long long ull)
00086 {
00087 std::ostringstream oss;
00088 oss << ull;
00089 return oss.str();
00090 }
00091
00092 inline String
00093 MakeString(const unsigned long long ull, int width, char fill = ' ')
00094 {
00095 std::ostringstream oss;
00096 oss << std::setw(width) << std::setfill(fill) << ull;
00097 return oss.str();
00098 }
00099
00100 inline String
00101 MakeString(const double d)
00102 {
00103 std::ostringstream oss;
00104 oss << d;
00105 return oss.str();
00106 }
00107
00108 inline String
00109 MakeString(const double d, const int width, const char fill = ' ')
00110 {
00111 std::ostringstream oss;
00112 oss << std::setw(width) << std::setfill(fill) << d;
00113 return oss.str();
00114 }
00115
00116 inline String
00117 MakeString(CString s)
00118 {
00119 return s;
00120 }
00121
00122 inline String
00123 MakeString(CString src, const int width, const char fill = ' ')
00124 {
00125 if (src.length() >= width)
00126 return src.substr(0, width);
00127 String res = src;
00128 for (int i=src.length() ; i<width ; i++)
00129 res.push_back(fill);
00130 return res;
00131 }
00132
00134 inline int
00135 atoi(CString s)
00136 {
00137 std::istringstream iss(s);
00138 int i = 0;
00139 iss >> i;
00140 return i;
00141
00142 }
00143
00145 inline long
00146 atol(CString s)
00147 {
00148 std::istringstream iss(s);
00149 long i = 0;
00150 iss >> i;
00151 return i;
00152 }
00153
00155 inline long
00156 atoul(CString s)
00157 {
00158 std::istringstream iss(s);
00159 long i = 0;
00160 iss >> i;
00161 return i;
00162 }
00163
00165 inline long long
00166 atoll(CString s)
00167 {
00168 std::istringstream iss(s);
00169 long long i = 0;
00170 iss >> i;
00171 return i;
00172 }
00173
00175 inline unsigned long long
00176 atoull(CString s)
00177 {
00178 std::istringstream iss(s);
00179 unsigned long long i = 0;
00180 iss >> i;
00181 return i;
00182 }
00183
00185 inline double
00186 atof(CString s)
00187 {
00188 std::istringstream iss(s);
00189 double d = 0.0;
00190 iss >> d;
00191 return d;
00192 }
00193
00195 inline String
00196 StringReplace(String src, CString subFrom,
00197 CString subTo)
00198 {
00199 String::size_type start = src.find(subFrom);
00200 if (start == String::npos)
00201 return src;
00202 return src.replace(start, subFrom.size(), subTo);
00203 }
00204
00206 inline String
00207 StringReplaceAll(String src, CString subFrom, CString subTo,
00208 const bool recursive = true, String::size_type startPos = 0)
00209 {
00210 String::size_type subFromSize = subFrom.size();
00211 String::size_type subToSize = subTo.size();
00212 String::size_type start = src.find(subFrom, startPos);
00213 while (start != String::npos)
00214 {
00215 src = src.replace(start, subFromSize, subTo);
00216 start = src.find(subFrom, recursive ? startPos : start + subToSize);
00217 }
00218 return src;
00219 }
00220
00221 inline String
00222 StringHead(CString src, const char toChar, const bool includeChar)
00223 {
00224 String::size_type p = src.find(toChar, 0);
00225 if (p == String::npos)
00226 return "";
00227 if (includeChar)
00228 p++;
00229 return src.substr(0, p);
00230 }
00231
00232 inline String
00233 StringTail(String src, char toChar, bool includeChar)
00234 {
00235 String::size_type p = src.find(toChar, 0);
00236 if (p == String::npos)
00237 return "";
00238 if (!includeChar)
00239 p++;
00240 return src.substr(p, src.size() - p);
00241 }
00242
00243 inline bool
00244 StringStartsWith(CString src, CString sub)
00245 {
00246 return src.compare(0, sub.size(), sub) == 0;
00247 }
00248
00249 inline bool
00250 StringEndsWith(String src, const String& sub)
00251 {
00252 if (sub.size() > src.size())
00253 return false;
00254 return src.compare(src.size() - sub.size(), sub.size(), sub) == 0;
00255 }
00256
00257 inline void
00258 StringToLower(String& s)
00259 {
00260
00261
00262 std::transform(s.begin(), s.end(), s.begin(), (int(*)(int))tolower);
00263
00264 }
00265
00266 inline void
00267 StringToUpper(String& s)
00268 {
00269 std::transform(s.begin(), s.end(), s.begin(), (int(*)(int))toupper);
00270 }
00271
00272 inline bool
00273 StringToBool(CString s)
00274 {
00275 return ((s == "0") || (s == "false") || (s == "no")) ? false : true;
00276 }
00277
00278 inline String
00279 StringSplit(String& s, char delim = ' ')
00280 {
00281 String result;
00282 String::size_type pos = s.find(delim);
00283 if(pos == String::npos)
00284 {
00285 result = s;
00286 s = "";
00287 return result;
00288 }
00289 result = s.substr(0, pos);
00290 s = s.substr(pos+1, s.size());
00291 return result;
00292 }
00293
00295 inline String
00296 StringResolveEnv(String src)
00297 {
00298 String::size_type start = src.find("${");
00299 while (start != String::npos)
00300 {
00301 String::size_type end = src.find("}", start+2);
00302 String env = src.substr(start+2, end - (start+2));
00303 char* val = getenv(env.c_str());
00304 if (val)
00305 src = src.replace(start, env.size()+3, String(val));
00306 else
00307 src = src.replace(start, env.size()+3, env);
00308 start = src.find("${");
00309 }
00310 return src;
00311 }
00312
00313 }
00314
00315 #endif