22 #ifndef __MYGUI_STRING_UTILITY_H__
23 #define __MYGUI_STRING_UTILITY_H__
34 inline void trim(std::string& _str,
bool _left =
true,
bool _right =
true)
36 if (_right) _str.erase(_str.find_last_not_of(
" \t\r") + 1);
37 if (_left) _str.erase(0, _str.find_first_not_of(
" \t\r"));
44 std::ostringstream stream;
49 inline const std::string&
toString (
const std::string& _value)
54 template<
typename T1,
typename T2>
57 std::ostringstream stream;
62 template<
typename T1,
typename T2,
typename T3>
63 inline std::string
toString (T1 p1, T2 p2, T3 p3)
65 std::ostringstream stream;
66 stream << p1 << p2 << p3;
70 template<
typename T1,
typename T2,
typename T3,
typename T4>
71 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4)
73 std::ostringstream stream;
74 stream << p1 << p2 << p3 << p4;
78 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5>
79 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
81 std::ostringstream stream;
82 stream << p1 << p2 << p3 << p4 << p5;
86 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6>
87 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6)
89 std::ostringstream stream;
90 stream << p1 << p2 << p3 << p4 << p5 << p6;
94 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7>
95 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7)
97 std::ostringstream stream;
98 stream << p1 << p2 << p3 << p4 << p5 << p6 << p7;
102 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8>
103 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8)
105 std::ostringstream stream;
106 stream << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8;
110 template<
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8,
typename T9>
111 inline std::string
toString (T1 p1, T2 p2, T3 p3, T4 p4, T5 p5, T6 p6, T7 p7, T8 p8, T9 p9)
113 std::ostringstream stream;
114 stream << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8 << p9;
121 return _value ?
"true" :
"false";
129 std::istringstream stream(_value);
136 int item = stream.get();
139 if (item !=
' ' && item !=
'\t')
151 if (_value ==
"True" || _value ==
"true" || _value ==
"1")
158 inline char parseValue(
const std::string& _value)
160 return (
char)parseValue<short>(_value);
165 inline unsigned char parseValue(
const std::string& _value)
167 return (
unsigned char)parseValue<unsigned short>(_value);
173 return parseValue<short>(_value);
178 return parseValue<unsigned short>(_value);
183 return parseValue<int>(_value);
186 inline unsigned int parseUInt(
const std::string& _value)
188 return parseValue<unsigned int>(_value);
193 return parseValue<size_t>(_value);
198 return parseValue<float>(_value);
203 return parseValue<double>(_value);
208 return parseValue<bool>(_value);
213 return parseValue<char>(_value);
218 return parseValue<unsigned char>(_value);
222 template<
typename T1,
typename T2>
226 std::istringstream stream(_value);
232 int item = stream.get();
235 if (item !=
' ' && item !=
'\t')
243 template<
typename T1,
typename T2>
247 std::istringstream stream(_value);
248 stream >> p1 >> p2 >> p3;
253 int item = stream.get();
256 if (item !=
' ' && item !=
'\t')
261 return T1(p1, p2, p3);
264 template<
typename T1,
typename T2>
268 std::istringstream stream(_value);
269 stream >> p1 >> p2 >> p3 >> p4;
274 int item = stream.get();
277 if (item !=
' ' && item !=
'\t')
282 return T1(p1, p2, p3, p4);
287 template<
typename Type>
288 inline void split(std::vector<Type>& _ret,
const Type& _source,
const Type& _delims)
290 size_t start = _source.find_first_not_of(_delims);
291 while (start != _source.npos)
293 size_t end = _source.find_first_of(_delims, start);
294 if (end != _source.npos)
295 _ret.push_back(_source.substr(start, end - start));
298 _ret.push_back(_source.substr(start));
301 start = _source.find_first_not_of(_delims, end + 1);
306 inline std::vector<std::string>
split(
const std::string& _source,
const std::string& _delims =
"\t\n ")
308 std::vector<std::string> result;
309 templates::split<std::string>(result, _source, _delims);
313 template<
typename T1,
typename T2,
typename T3,
typename T4>
314 inline bool parseComplex(
const std::string& _value, T1& _p1, T2& _p2, T3& _p3, T4& _p4)
316 std::istringstream stream(_value);
318 stream >> _p1 >> _p2 >> _p3 >> _p4;
322 int item = stream.get();
325 if (item !=
' ' && item !=
'\t')
333 template<
typename T1,
typename T2,
typename T3>
334 inline bool parseComplex(
const std::string& _value, T1& _p1, T2& _p2, T3& _p3)
336 std::istringstream stream(_value);
338 stream >> _p1 >> _p2 >> _p3;
342 int item = stream.get();
345 if (item !=
' ' && item !=
'\t')
353 template<
typename T1,
typename T2>
356 std::istringstream stream(_value);
358 stream >> _p1 >> _p2;
362 int item = stream.get();
365 if (item !=
' ' && item !=
'\t')
373 template<
typename T1>
376 std::istringstream stream(_value);
382 int item = stream.get();
385 if (item !=
' ' && item !=
'\t')
396 std::string value(_value);
398 if ((value ==
"True") || (value ==
"true") || (value ==
"1"))
403 else if ((value ==
"False") || (value ==
"false") || (value ==
"0"))
412 inline bool startWith(
const std::string& _source,
const std::string& _value)
414 size_t count = _value.size();
415 if (_source.size() < count)
417 for (
size_t index = 0; index < count; ++ index)
419 if (_source[index] != _value[index])
425 inline bool endWith(
const std::string& _source,
const std::string& _value)
427 size_t count = _value.size();
428 if (_source.size() < count)
430 size_t offset = _source.size() - count;
431 for (
size_t index = 0; index < count; ++ index)
433 if (_source[index + offset] != _value[index])
443 #endif // __MYGUI_STRING_UTILITY_H__