7#ifndef MYGUI_STRING_UTILITY_H_
8#define MYGUI_STRING_UTILITY_H_
19 inline void trim(std::string& _str,
bool _left =
true,
bool _right =
true)
21 if (_right) _str.erase(_str.find_last_not_of(
" \t\r") + 1);
22 if (_left) _str.erase(0, _str.find_first_not_of(
" \t\r"));
29 std::ostringstream stream;
34 inline const std::string&
toString (
const std::string& _value)
39 template<
typename T1,
typename T2>
42 std::ostringstream stream;
47 template<
typename T1,
typename T2,
typename T3>
48 inline std::string
toString (T1 p1, T2 p2, T3 p3)
50 std::ostringstream stream;
51 stream << p1 << p2 << p3;
55 template<
typename T1,
typename T2,
typename T3,
typename T4>
56 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4)
58 std::ostringstream stream;
59 stream << p1 << p2 << p3 << p4;
63 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5>
64 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
66 std::ostringstream stream;
67 stream << p1 << p2 << p3 << p4 << p5;
71 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6>
72 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6)
74 std::ostringstream stream;
75 stream << p1 << p2 << p3 << p4 << p5 << p6;
79 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7>
80 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7)
82 std::ostringstream stream;
83 stream << p1 << p2 << p3 << p4 << p5 << p6 << p7;
87 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8>
88 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8)
90 std::ostringstream stream;
91 stream << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8;
95 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8,
typename T9>
96 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8, T9 p9)
98 std::ostringstream stream;
99 stream << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8 << p9;
106 return _value ?
"true" :
"false";
114 std::istringstream stream(_value);
121 int item = stream.get();
124 if (item !=
' ' && item !=
'\t')
136 return _value ==
"True" || _value ==
"true" || _value ==
"1";
143 return (
char)parseValue<short>(_value);
150 return (
unsigned char)parseValue<unsigned short>(_value);
156 return parseValue<short>(_value);
161 return parseValue<unsigned short>(_value);
166 return parseValue<int>(_value);
169 inline unsigned int parseUInt(
const std::string& _value)
171 return parseValue<unsigned int>(_value);
176 return parseValue<size_t>(_value);
181 return parseValue<float>(_value);
186 return parseValue<double>(_value);
191 return parseValue<bool>(_value);
196 return parseValue<char>(_value);
201 return parseValue<unsigned char>(_value);
205 template<
typename T1,
typename T2>
209 std::istringstream stream(_value);
215 int item = stream.get();
218 if (item !=
' ' && item !=
'\t')
226 template<
typename T1,
typename T2>
230 std::istringstream stream(_value);
231 stream >> p1 >> p2 >> p3;
236 int item = stream.get();
239 if (item !=
' ' && item !=
'\t')
244 return T1(p1, p2, p3);
247 template<
typename T1,
typename T2>
251 std::istringstream stream(_value);
252 stream >> p1 >> p2 >> p3 >> p4;
257 int item = stream.get();
260 if (item !=
' ' && item !=
'\t')
265 return T1(p1, p2, p3, p4);
270 template<
typename Type>
271 inline void split(std::vector<Type>& _ret,
const Type& _source,
const Type& _delims)
273 size_t start = _source.find_first_not_of(_delims);
274 while (start != _source.npos)
276 size_t end = _source.find_first_of(_delims, start);
277 if (end != _source.npos)
278 _ret.push_back(_source.substr(start, end - start));
281 _ret.push_back(_source.substr(start));
284 start = _source.find_first_not_of(_delims, end + 1);
289 inline std::vector<std::string>
split(
const std::string& _source,
const std::string& _delims =
"\t\n ")
291 std::vector<std::string> result;
292 templates::split<std::string>(result, _source, _delims);
296 template<
typename T1,
typename T2,
typename T3,
typename T4>
297 inline bool parseComplex(
const std::string& _value, T1& _p1, T2& _p2, T3& _p3, T4& _p4)
299 std::istringstream stream(_value);
301 stream >> _p1 >> _p2 >> _p3 >> _p4;
305 int item = stream.get();
308 if (item !=
' ' && item !=
'\t')
316 template<
typename T1,
typename T2,
typename T3>
317 inline bool parseComplex(
const std::string& _value, T1& _p1, T2& _p2, T3& _p3)
319 std::istringstream stream(_value);
321 stream >> _p1 >> _p2 >> _p3;
325 int item = stream.get();
328 if (item !=
' ' && item !=
'\t')
336 template<
typename T1,
typename T2>
339 std::istringstream stream(_value);
341 stream >> _p1 >> _p2;
345 int item = stream.get();
348 if (item !=
' ' && item !=
'\t')
356 template<
typename T1>
359 std::istringstream stream(_value);
365 int item = stream.get();
368 if (item !=
' ' && item !=
'\t')
379 std::string value(_value);
381 if ((value ==
"True") || (value ==
"true") || (value ==
"1"))
386 else if ((value ==
"False") || (value ==
"false") || (value ==
"0"))
395 inline bool startWith(
const std::string& _source,
const std::string& _value)
397 size_t count = _value.size();
398 if (_source.size() < count)
400 for (
size_t index = 0; index < count; ++ index)
402 if (_source[index] != _value[index])
408 inline bool endWith(
const std::string& _source,
const std::string& _value)
410 size_t count = _value.size();
411 if (_source.size() < count)
413 size_t offset = _source.size() - count;
414 for (
size_t index = 0; index < count; ++ index)
416 if (_source[index + offset] != _value[index])
void split(std::vector< Type > &_ret, const Type &_source, const Type &_delims)
size_t parseSizeT(const std::string &_value)
std::vector< std::string > split(const std::string &_source, const std::string &_delims="\t\n ")
bool parseComplex< bool >(const std::string &_value, bool &_p1)
int parseInt(const std::string &_value)
T1 parseValueEx2(const std::string &_value)
bool parseBool(const std::string &_value)
T parseValue(const std::string &_value)
double parseDouble(const std::string &_value)
short parseShort(const std::string &_value)
unsigned int parseUInt(const std::string &_value)
char parseChar(const std::string &_value)
bool parseComplex(const std::string &_value, T1 &_p1, T2 &_p2, T3 &_p3, T4 &_p4)
unsigned char parseUChar(const std::string &_value)
std::string toString< bool >(bool _value)
T1 parseValueEx4(const std::string &_value)
std::string toString(T p)
bool startWith(const std::string &_source, const std::string &_value)
bool endWith(const std::string &_source, const std::string &_value)
T1 parseValueEx3(const std::string &_value)
void trim(std::string &_str, bool _left=true, bool _right=true)
float parseFloat(const std::string &_value)
unsigned short parseUShort(const std::string &_value)