00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef _SWORD_BASE_SRINGTOOLS_
00020 #define _SWORD_BASE_SRINGTOOLS_
00021
00022 #include <vector>
00023
00024 namespace sword {
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00052 SWORDDECL std::string format(float value);
00053
00055 SWORDDECL std::string format(double value);
00056
00058
00070 SWORDDECL std::string format(double value, word precision, char decimalChar = '.', char thousandChar = '\0', bool negativeInParenthesis = false);
00071
00072
00073
00074
00075
00076 SWORDDECL char *format_(char *buffer, word bufferLen, word64 value, char thousandChar = '\0');
00077 SWORDDECL char *format_(char *buffer, word bufferLen, int64 value, char thousandChar = '\0', bool negativeInParenthesis = false);
00078 SWORDDECL char *format_(char *buffer, word bufferLen, double value, word precision, char decimalChar = '.', char thousandChar = '\0', bool negativeInParenthesis = false);
00079
00081 SWORDDECL std::string format(void *value);
00082
00084 SWORDDECL std::string format(char value);
00085
00087 SWORDDECL std::string format(const char *format, ...);
00088
00090 SWORDDECL int8 parseInt8(const std::string &str);
00091
00093 SWORDDECL int16 parseInt16(const std::string &str);
00094
00096 SWORDDECL int32 parseInt32(const std::string &str);
00097
00099 SWORDDECL int parseInt(const std::string &str);
00100
00102 SWORDDECL word8 parseWord8(const std::string &str);
00103
00105 SWORDDECL word16 parseWord16(const std::string &str);
00106
00108 SWORDDECL word32 parseWord32(const std::string &str);
00109
00111 SWORDDECL word parseWord(const std::string &str);
00112
00114 SWORDDECL float parseFloat(const std::string &str);
00115
00117 SWORDDECL double parseDouble(const std::string &str);
00118
00119
00120
00121 template<class charT, class traits, class Allocator>
00122 inline std::basic_string<charT, traits, Allocator> trimLeft(const std::basic_string<charT, traits, Allocator> &str)
00123 {
00124 std::string result = str;
00125 result.erase(0, result.find_first_not_of(" \t\n\r"));
00126 return result;
00127 }
00128
00129 template<class charT, class traits, class Allocator>
00130 inline std::basic_string<charT, traits, Allocator> trimRight(const std::basic_string<charT, traits, Allocator> &str)
00131 {
00132 std::string result = str;
00133 result.erase(result.find_last_not_of(" \t\n\r") + 1);
00134 return result;
00135 }
00136
00137 template<class charT, class traits, class Allocator>
00138 inline std::basic_string<charT, traits, Allocator> trim(const std::basic_string<charT, traits, Allocator> &str)
00139 {
00140 return trimLeft( trimRight( str ) );
00141 }
00142
00143
00144
00145 template<class charT, class traits, class Allocator>
00146 void strlwr(std::basic_string<charT, traits, Allocator> &str)
00147 {
00148
00149 for(typename std::basic_string<charT, traits, Allocator>::iterator i=str.begin();
00150 i!=str.end(); ++i)
00151 {
00152
00153 *i=(char)tolower(*i);
00154 }
00155 }
00156
00157 template<class charT, class traits, class Allocator>
00158 void strupr(std::basic_string<charT, traits, Allocator> &str)
00159 {
00160
00161 for(typename std::basic_string<charT, traits, Allocator>::iterator i=str.begin();
00162 i!=str.end(); ++i)
00163 {
00164
00165 *i=toupper(*i);
00166 }
00167 }
00168
00169
00170
00171 template<class charT, class traits, class Allocator>
00172 std::basic_string<charT, traits, Allocator> strlwr(const std::basic_string<charT, traits, Allocator> &str)
00173 {
00174 std::basic_string<charT, traits, Allocator> target(str);
00175 strlwr(target);
00176 return target;
00177 }
00178
00179 template<class charT, class traits, class Allocator>
00180 std::basic_string<charT, traits, Allocator> strupr(const std::basic_string<charT, traits, Allocator> &str)
00181 {
00182 std::basic_string<charT, traits, Allocator> target(str);
00183 strupr(target);
00184 return target;
00185 }
00186
00187
00188
00189 template<class charT, class traits, class Allocator>
00190 void replace(std::basic_string<charT, traits, Allocator> &str,
00191 const char *before, const char *after)
00192 {
00193
00194 size_t pos=str.find(before);
00195 if (pos!=std::string::npos)
00196 str=str.substr(0,pos)+after+str.substr(pos+traits::length(before));
00197 }
00198
00199 template<class charT, class traits, class Allocator>
00200 void replace(std::basic_string<charT, traits, Allocator> &str,
00201 const std::basic_string<charT, traits, Allocator> &before,
00202 const std::basic_string<charT, traits, Allocator> &after)
00203 {
00204
00205 size_t pos=str.find(before);
00206 if (pos!=std::string::npos)
00207 str=str.substr(0,pos)+after+str.substr(pos+before.size());
00208 }
00209
00210 template<class charT, class traits, class Allocator>
00211 void replacem(std::basic_string<charT, traits, Allocator> &str, char before, char after)
00212 {
00213
00214 for(typename std::basic_string<charT, traits, Allocator>::iterator i=str.begin();
00215 i!=str.end(); ++i)
00216
00217 if (traits::eq(*i,before))
00218
00219 traits::assign(*i,after);
00220 }
00221
00222 template<class charT, class traits, class Allocator>
00223 void replacem(std::basic_string<charT, traits, Allocator> &str,
00224 const char *before, const char *after)
00225 {
00226
00227 size_t pos=str.find(before);
00228 while(pos!=std::string::npos)
00229 {
00230 str = str.substr(0,pos)+after+str.substr(pos+traits::length(before));
00231 pos = str.find(before, pos+traits::length(after));
00232 }
00233 }
00234
00235 template<class charT, class traits, class Allocator>
00236 void replacem(std::basic_string<charT, traits, Allocator> &str,
00237 const std::basic_string<charT, traits, Allocator> &before,
00238 const std::basic_string<charT, traits, Allocator> &after)
00239 {
00240
00241 size_t pos=str.find(before);
00242 while(pos!=std::string::npos)
00243 {
00244 str = str.substr(0,pos)+after+str.substr(pos+before.size());
00245 pos = str.find(before, pos+after.size());
00246 }
00247 }
00248
00249
00250
00261 template<class charT, class traits, class Allocator>
00262 void split(const std::basic_string<charT, traits, Allocator> &str,
00263 std::vector<std::basic_string<charT, traits, Allocator> > &segments,
00264 charT separator)
00265 {
00266 std::basic_string<charT, traits, Allocator> remain=str;
00267 std::basic_string<charT, traits, Allocator> segment;
00268 for(;;)
00269 {
00270 size_t pos=remain.find(separator);
00271 if (pos==std::string::npos) break;
00272
00273 segment=trim(remain.substr(0, pos));
00274 remain =remain.substr(pos+1);
00275 segments.push_back(segment);
00276 }
00277 if (remain.size()) segments.push_back(remain);
00278 }
00279
00289 template<class charT, class traits, class Allocator>
00290 std::vector<std::basic_string<charT, traits, Allocator> >
00291 split(const std::basic_string<charT, traits, Allocator> &str,
00292 charT separator)
00293 {
00294 std::basic_string<charT, traits, Allocator> remain=str;
00295 std::basic_string<charT, traits, Allocator> segment;
00296 std::vector<std::basic_string<charT, traits, Allocator> > segments;
00297 for(;;)
00298 {
00299 size_t pos=remain.find(separator);
00300 if (pos==std::string::npos) break;
00301
00302 segment=trim(remain.substr(0, pos));
00303 remain =remain.substr(pos+1);
00304 segments.push_back(segment);
00305 }
00306 if (remain.size()) segments.push_back(remain);
00307
00308 return segments;
00309 }
00310
00317 template<class charT, class traits, class Allocator>
00318 std::basic_string<charT, traits, Allocator> join(const std::vector<std::basic_string<charT, traits, Allocator> > &segments, charT separator)
00319 {
00320 std::basic_ostringstream<charT, traits, Allocator> oss;
00321 bool first = true;
00322 for(
00323 std::vector<std::basic_string<charT, traits, Allocator> >::const_iterator i=segments.begin();
00324 i!=segments.end();
00325 ++i)
00326 {
00327 if (first)
00328 first = false;
00329 else
00330 oss << separator;
00331 oss << *i;
00332 }
00333 return oss.str();
00334 }
00335
00336 template<class charT, class traits, class Allocator>
00337 void extractNameValue(const std::basic_string<charT, traits, Allocator> &line,
00338 std::basic_string<charT, traits, Allocator> &name,
00339 std::basic_string<charT, traits, Allocator> &value,
00340 char separator = '=')
00341 {
00342 int i=line.find(separator);
00343 name =trim(line.substr(0,i));
00344 value=trim(line.substr(i+1));
00345 }
00346
00347 SWORDDECL int stricmp (const std::string &s1, const std::string &s2);
00348 SWORDDECL int strnicmp(const std::string &s1, const std::string &s2, size_t length);
00349
00350
00351 SWORDDECL void canonize__(const char *src, char *dst);
00352
00353 SWORDDECL std::string readLine(const std::string& blob, size_t& offset);
00354
00355 }
00356
00357 #ifndef HAVE_STRICMP
00358 SWORDDECL int stricmp (const char *s1, const char *s2);
00359 #endif
00360
00361 #ifndef HAVE_STRNICMP
00362 SWORDDECL int strnicmp(const char *s1, const char *s2, size_t length);
00363 #endif
00364
00365 #include "sword.private/base.StringTools.inl"
00366
00367 #endif // _SWORD_BASE_SRINGTOOLS_