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 if (_value ==
"True" || _value ==
"true" || _value ==
"1")
145 return (
char)parseValue<short>(_value);
152 return (
unsigned char)parseValue<unsigned short>(_value);
158 return parseValue<short>(_value);
163 return parseValue<unsigned short>(_value);
168 return parseValue<int>(_value);
171 inline unsigned int parseUInt(
const std::string& _value)
173 return parseValue<unsigned int>(_value);
178 return parseValue<size_t>(_value);
183 return parseValue<float>(_value);
188 return parseValue<double>(_value);
193 return parseValue<bool>(_value);
198 return parseValue<char>(_value);
203 return parseValue<unsigned char>(_value);
207 template<
typename T1,
typename T2>
211 std::istringstream stream(_value);
217 int item = stream.get();
220 if (item !=
' ' && item !=
'\t')
228 template<
typename T1,
typename T2>
232 std::istringstream stream(_value);
233 stream >> p1 >> p2 >> p3;
238 int item = stream.get();
241 if (item !=
' ' && item !=
'\t')
246 return T1(p1, p2, p3);
249 template<
typename T1,
typename T2>
253 std::istringstream stream(_value);
254 stream >> p1 >> p2 >> p3 >> p4;
259 int item = stream.get();
262 if (item !=
' ' && item !=
'\t')
267 return T1(p1, p2, p3, p4);
272 template<
typename Type>
273 inline void split(std::vector<Type>& _ret,
const Type& _source,
const Type& _delims)
275 size_t start = _source.find_first_not_of(_delims);
276 while (start != _source.npos)
278 size_t end = _source.find_first_of(_delims, start);
279 if (end != _source.npos)
280 _ret.push_back(_source.substr(start, end - start));
283 _ret.push_back(_source.substr(start));
286 start = _source.find_first_not_of(_delims, end + 1);
291 inline std::vector<std::string>
split(
const std::string& _source,
const std::string& _delims =
"\t\n ")
293 std::vector<std::string> result;
294 templates::split<std::string>(result, _source, _delims);
298 template<
typename T1,
typename T2,
typename T3,
typename T4>
299 inline bool parseComplex(
const std::string& _value, T1& _p1, T2& _p2, T3& _p3, T4& _p4)
301 std::istringstream stream(_value);
303 stream >> _p1 >> _p2 >> _p3 >> _p4;
307 int item = stream.get();
310 if (item !=
' ' && item !=
'\t')
318 template<
typename T1,
typename T2,
typename T3>
319 inline bool parseComplex(
const std::string& _value, T1& _p1, T2& _p2, T3& _p3)
321 std::istringstream stream(_value);
323 stream >> _p1 >> _p2 >> _p3;
327 int item = stream.get();
330 if (item !=
' ' && item !=
'\t')
338 template<
typename T1,
typename T2>
341 std::istringstream stream(_value);
343 stream >> _p1 >> _p2;
347 int item = stream.get();
350 if (item !=
' ' && item !=
'\t')
358 template<
typename T1>
361 std::istringstream stream(_value);
367 int item = stream.get();
370 if (item !=
' ' && item !=
'\t')
381 std::string value(_value);
383 if ((value ==
"True") || (value ==
"true") || (value ==
"1"))
388 else if ((value ==
"False") || (value ==
"false") || (value ==
"0"))
397 inline bool startWith(
const std::string& _source,
const std::string& _value)
399 size_t count = _value.size();
400 if (_source.size() < count)
402 for (
size_t index = 0; index < count; ++ index)
404 if (_source[index] != _value[index])
410 inline bool endWith(
const std::string& _source,
const std::string& _value)
412 size_t count = _value.size();
413 if (_source.size() < count)
415 size_t offset = _source.size() - count;
416 for (
size_t index = 0; index < count; ++ index)
418 if (_source[index + offset] != _value[index])
428 #endif // __MYGUI_STRING_UTILITY_H__