MyGUI 3.2.0

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

- File List
- Globals

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