MyGUI 3.2.2

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

MyGUI_StringUtility.h
Go to the documentation of this file.
1 /*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7 #ifndef MYGUI_STRING_UTILITY_H_
8 #define MYGUI_STRING_UTILITY_H_
9
10 #include "MyGUI_Prerequest.h"
11 #include <vector>
12 #include <sstream>
13
14 namespace MyGUI
15 {
16 namespace utility
17 {
18
19 inline void trim(std::string& _str, bool _left = true, bool _right = true)
20 {
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"));
23 }
24
25 // конвертирование в строку
26 template<typename T>
27 inline std::string toString (T p)
28 {
29 std::ostringstream stream;
30 stream << p;
31 return stream.str();
32 }
33
34 inline const std::string& toString (const std::string& _value)
35 {
36 return _value;
37 }
38
39 template<typename T1, typename T2>
40 inline std::string toString (T1 p1, T2 p2)
41 {
42 std::ostringstream stream;
43 stream << p1 << p2;
44 return stream.str();
45 }
46
47 template<typename T1, typename T2, typename T3>
48 inline std::string toString (T1 p1, T2 p2, T3 p3)
49 {
50 std::ostringstream stream;
51 stream << p1 << p2 << p3;
52 return stream.str();
53 }
54
55 template<typename T1, typename T2, typename T3, typename T4>
56 inline std::string toString (T1 p1, T2 p2, T3 p3, T4 p4)
57 {
58 std::ostringstream stream;
59 stream << p1 << p2 << p3 << p4;
60 return stream.str();
61 }
62
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)
65 {
66 std::ostringstream stream;
67 stream << p1 << p2 << p3 << p4 << p5;
68 return stream.str();
69 }
70
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)
73 {
74 std::ostringstream stream;
75 stream << p1 << p2 << p3 << p4 << p5 << p6;
76 return stream.str();
77 }
78
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)
81 {
82 std::ostringstream stream;
83 stream << p1 << p2 << p3 << p4 << p5 << p6 << p7;
84 return stream.str();
85 }
86
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)
89 {
90 std::ostringstream stream;
91 stream << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8;
92 return stream.str();
93 }
94
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)
97 {
98 std::ostringstream stream;
99 stream << p1 << p2 << p3 << p4 << p5 << p6 << p7 << p8 << p9;
100 return stream.str();
101 }
102
103 template<>
104 inline std::string toString<bool> (bool _value)
105 {
106 return _value ? "true" : "false";
107 }
108
109
110 // утилиты для парсинга
111 template<typename T>
112 inline T parseValue( const std::string& _value )
113 {
114 std::istringstream stream(_value);
115 T result;
116 stream >> result;
117 if (stream.fail())
118 return T();
119 else
120 {
121 int item = stream.get();
122 while (item != -1)
123 {
124 if (item != ' ' && item != '\t')
125 return T();
126 item = stream.get();
127 }
128 }
129 return result;
130 }
131
132 // отдельная имплементация под bool
133 template<>
134 inline bool parseValue(const std::string& _value)
135 {
136 if (_value == "True" || _value == "true" || _value == "1")
137 return true;
138 return false;
139 }
140
141 // отдельная имплементация под char
142 template<>
143 inline char parseValue(const std::string& _value)
144 {
145 return (char)parseValue<short>(_value);
146 }
147
148 // отдельная имплементация под unsigned char
149 template<>
150 inline unsigned char parseValue(const std::string& _value)
151 {
152 return (unsigned char)parseValue<unsigned short>(_value);
153 }
154
155
156 inline short parseShort(const std::string& _value)
157 {
158 return parseValue<short>(_value);
159 }
160
161 inline unsigned short parseUShort(const std::string& _value)
162 {
163 return parseValue<unsigned short>(_value);
164 }
165
166 inline int parseInt(const std::string& _value)
167 {
168 return parseValue<int>(_value);
169 }
170
171 inline unsigned int parseUInt(const std::string& _value)
172 {
173 return parseValue<unsigned int>(_value);
174 }
175
176 inline size_t parseSizeT(const std::string& _value)
177 {
178 return parseValue<size_t>(_value);
179 }
180
181 inline float parseFloat(const std::string& _value)
182 {
183 return parseValue<float>(_value);
184 }
185
186 inline double parseDouble(const std::string& _value)
187 {
188 return parseValue<double>(_value);
189 }
190
191 inline bool parseBool(const std::string& _value)
192 {
193 return parseValue<bool>(_value);
194 }
195
196 inline char parseChar(const std::string& _value)
197 {
198 return parseValue<char>(_value);
199 }
200
201 inline unsigned char parseUChar(const std::string& _value)
202 {
203 return parseValue<unsigned char>(_value);
204 }
205
206 // для парсинга сложных типов, состоящих из простых
207 template<typename T1, typename T2>
208 inline T1 parseValueEx2(const std::string& _value)
209 {
210 T2 p1, p2;
211 std::istringstream stream(_value);
212 stream >> p1 >> p2;
213 if (stream.fail())
214 return T1();
215 else
216 {
217 int item = stream.get();
218 while (item != -1)
219 {
220 if (item != ' ' && item != '\t')
221 return T1();
222 item = stream.get();
223 }
224 }
225 return T1(p1, p2);
226 }
227
228 template<typename T1, typename T2>
229 inline T1 parseValueEx3(const std::string& _value)
230 {
231 T2 p1, p2, p3;
232 std::istringstream stream(_value);
233 stream >> p1 >> p2 >> p3;
234 if (stream.fail())
235 return T1();
236 else
237 {
238 int item = stream.get();
239 while (item != -1)
240 {
241 if (item != ' ' && item != '\t')
242 return T1();
243 item = stream.get();
244 }
245 }
246 return T1(p1, p2, p3);
247 }
248
249 template<typename T1, typename T2>
250 inline T1 parseValueEx4(const std::string& _value)
251 {
252 T2 p1, p2, p3, p4;
253 std::istringstream stream(_value);
254 stream >> p1 >> p2 >> p3 >> p4;
255 if (stream.fail())
256 return T1();
257 else
258 {
259 int item = stream.get();
260 while (item != -1)
261 {
262 if (item != ' ' && item != '\t')
263 return T1();
264 item = stream.get();
265 }
266 }
267 return T1(p1, p2, p3, p4);
268 }
269
270 namespace templates
271 {
272 template<typename Type>
273 inline void split(std::vector<Type>& _ret, const Type& _source, const Type& _delims)
274 {
275 size_t start = _source.find_first_not_of(_delims);
276 while (start != _source.npos)
277 {
278 size_t end = _source.find_first_of(_delims, start);
279 if (end != _source.npos)
280 _ret.push_back(_source.substr(start, end - start));
281 else
282 {
283 _ret.push_back(_source.substr(start));
284 break;
285 }
286 start = _source.find_first_not_of(_delims, end + 1);
287 }
288 }
289 } // namespace templates
290
291 inline std::vector<std::string> split(const std::string& _source, const std::string& _delims = "\t\n ")
292 {
293 std::vector<std::string> result;
294 templates::split<std::string>(result, _source, _delims);
295 return result;
296 }
297
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)
300 {
301 std::istringstream stream(_value);
302
303 stream >> _p1 >> _p2 >> _p3 >> _p4;
304
305 if (stream.fail())
306 return false;
307 int item = stream.get();
308 while (item != -1)
309 {
310 if (item != ' ' && item != '\t')
311 return false;
312 item = stream.get();
313 }
314
315 return true;
316 }
317
318 template<typename T1, typename T2, typename T3>
319 inline bool parseComplex(const std::string& _value, T1& _p1, T2& _p2, T3& _p3)
320 {
321 std::istringstream stream(_value);
322
323 stream >> _p1 >> _p2 >> _p3;
324
325 if (stream.fail())
326 return false;
327 int item = stream.get();
328 while (item != -1)
329 {
330 if (item != ' ' && item != '\t')
331 return false;
332 item = stream.get();
333 }
334
335 return true;
336 }
337
338 template<typename T1, typename T2>
339 inline bool parseComplex(const std::string& _value, T1& _p1, T2& _p2)
340 {
341 std::istringstream stream(_value);
342
343 stream >> _p1 >> _p2;
344
345 if (stream.fail())
346 return false;
347 int item = stream.get();
348 while (item != -1)
349 {
350 if (item != ' ' && item != '\t')
351 return false;
352 item = stream.get();
353 }
354
355 return true;
356 }
357
358 template<typename T1>
359 inline bool parseComplex(const std::string& _value, T1& _p1)
360 {
361 std::istringstream stream(_value);
362
363 stream >> _p1;
364
365 if (stream.fail())
366 return false;
367 int item = stream.get();
368 while (item != -1)
369 {
370 if (item != ' ' && item != '\t')
371 return false;
372 item = stream.get();
373 }
374
375 return true;
376 }
377
378 template<>
379 inline bool parseComplex<bool>(const std::string& _value, bool& _p1)
380 {
381 std::string value(_value);
382 trim(value);
383 if ((value == "True") || (value == "true") || (value == "1"))
384 {
385 _p1 = true;
386 return true;
387 }
388 else if ((value == "False") || (value == "false") || (value == "0"))
389 {
390 _p1 = false;
391 return true;
392 }
393
394 return false;
395 }
396
397 inline bool startWith(const std::string& _source, const std::string& _value)
398 {
399 size_t count = _value.size();
400 if (_source.size() < count)
401 return false;
402 for (size_t index = 0; index < count; ++ index)
403 {
404 if (_source[index] != _value[index])
405 return false;
406 }
407 return true;
408 }
409
410 inline bool endWith(const std::string& _source, const std::string& _value)
411 {
412 size_t count = _value.size();
413 if (_source.size() < count)
414 return false;
415 size_t offset = _source.size() - count;
416 for (size_t index = 0; index < count; ++ index)
417 {
418 if (_source[index + offset] != _value[index])
419 return false;
420 }
421 return true;
422 }
423
424 } // namespace utility
425
426 } // namespace MyGUI
427
428 #endif // MYGUI_STRING_UTILITY_H_
MyGUI::utility::parseUChar
unsigned char parseUChar(const std::string &_value)
Definition: MyGUI_StringUtility.h:201
MyGUI::utility::parseComplex< bool >
bool parseComplex< bool >(const std::string &_value, bool &_p1)
Definition: MyGUI_StringUtility.h:379
MyGUI::utility::parseChar
char parseChar(const std::string &_value)
Definition: MyGUI_StringUtility.h:196
MyGUI::utility::startWith
bool startWith(const std::string &_source, const std::string &_value)
Definition: MyGUI_StringUtility.h:397
MyGUI::utility::parseUShort
unsigned short parseUShort(const std::string &_value)
Definition: MyGUI_StringUtility.h:161
MyGUI::utility::templates::split
void split(std::vector< Type > &_ret, const Type &_source, const Type &_delims)
Definition: MyGUI_StringUtility.h:273
MyGUI_Prerequest.h
MyGUI::utility::parseInt
int parseInt(const std::string &_value)
Definition: MyGUI_StringUtility.h:166
MyGUI::utility::parseValueEx2
T1 parseValueEx2(const std::string &_value)
Definition: MyGUI_StringUtility.h:208
MyGUI::utility::parseSizeT
size_t parseSizeT(const std::string &_value)
Definition: MyGUI_StringUtility.h:176
MyGUI::utility::parseUInt
unsigned int parseUInt(const std::string &_value)
Definition: MyGUI_StringUtility.h:171
MyGUI::utility::toString< bool >
std::string toString< bool >(bool _value)
Definition: MyGUI_StringUtility.h:104
MyGUI::utility::parseComplex
bool parseComplex(const std::string &_value, T1 &_p1, T2 &_p2, T3 &_p3, T4 &_p4)
Definition: MyGUI_StringUtility.h:299
MyGUI::utility::trim
void trim(std::string &_str, bool _left=true, bool _right=true)
Definition: MyGUI_StringUtility.h:19
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::utility::split
std::vector< std::string > split(const std::string &_source, const std::string &_delims="\t\n ")
Definition: MyGUI_StringUtility.h:291
MyGUI::utility::parseValue
T parseValue(const std::string &_value)
Definition: MyGUI_StringUtility.h:112
MyGUI::utility::parseDouble
double parseDouble(const std::string &_value)
Definition: MyGUI_StringUtility.h:186
MyGUI::utility::parseValueEx3
T1 parseValueEx3(const std::string &_value)
Definition: MyGUI_StringUtility.h:229
MyGUI::utility::toString
std::string toString(T p)
Definition: MyGUI_StringUtility.h:27
MyGUI::utility::parseValueEx4
T1 parseValueEx4(const std::string &_value)
Definition: MyGUI_StringUtility.h:250
MyGUI::utility::parseFloat
float parseFloat(const std::string &_value)
Definition: MyGUI_StringUtility.h:181
MyGUI::utility::parseBool
bool parseBool(const std::string &_value)
Definition: MyGUI_StringUtility.h:191
MyGUI::utility::parseShort
short parseShort(const std::string &_value)
Definition: MyGUI_StringUtility.h:156
MyGUI::utility::endWith
bool endWith(const std::string &_source, const std::string &_value)
Definition: MyGUI_StringUtility.h:410