MyGUI: MyGUI_StringUtility.h Source File

MyGUI 3.4.3

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::utility
15{
16
17 inline void trim(std::string& _str, bool _left = true, bool _right = true)
18 {
19 if (_right)
20 _str.erase(_str.find_last_not_of(" \t\r") + 1);
21 if (_left)
22 _str.erase(0, _str.find_first_not_of(" \t\r"));
23 }
24
25 template<typename T>
26 inline std::string toString(T _value)
27 {
28 std::ostringstream stream;
29 stream << _value;
30 return stream.str();
31 }
32
33 inline const std::string& toString(const std::string& _value)
34 {
35 return _value;
36 }
37
38 template<>
39 inline std::string toString(std::string_view _value)
40 {
41 return std::string{_value};
42 }
43
44 template<typename... Args>
45 inline std::string toString(Args&&... args)
46 {
47 std::ostringstream stream;
48 ((stream << args), ...);
49 return stream.str();
50 }
51
52 template<>
53 inline std::string toString<bool>(bool _value)
54 {
55 return _value ? "true" : "false";
56 }
57
58
59 template<typename T>
60 inline T parseValue(std::string_view _value)
61 {
62 std::stringstream stream;
63 stream << _value;
64 T result;
65 stream >> result;
66 if (stream.fail())
67 return {};
68
69 // check if there is more data, return {} in this case
70 int item = stream.get();
71 while (item != -1)
72 {
73 if (item != ' ' && item != '\t')
74 return {};
75 item = stream.get();
76 }
77 return result;
78 }
79
80 // bool specialization
81 template<>
82 inline bool parseValue(std::string_view _value)
83 {
84 return _value == "True" || _value == "true" || _value == "1";
85 }
86
87 // char specialization
88 template<>
89 inline char parseValue(std::string_view _value)
90 {
91 return static_cast<char>(parseValue<short>(_value));
92 }
93
94 // unsigned char specialization
95 template<>
96 inline unsigned char parseValue(std::string_view _value)
97 {
98 return static_cast<unsigned char>(parseValue<unsigned short>(_value));
99 }
100
101
102 inline int parseInt(std::string_view _value)
103 {
104 return parseValue<int>(_value);
105 }
106
107 inline unsigned int parseUInt(std::string_view _value)
108 {
109 return parseValue<unsigned int>(_value);
110 }
111
112 inline size_t parseSizeT(std::string_view _value)
113 {
114 return parseValue<size_t>(_value);
115 }
116
117 inline float parseFloat(std::string_view _value)
118 {
119 return parseValue<float>(_value);
120 }
121
122 inline double parseDouble(std::string_view _value)
123 {
124 return parseValue<double>(_value);
125 }
126
127 inline bool parseBool(std::string_view _value)
128 {
129 return parseValue<bool>(_value);
130 }
131
132 namespace templates
133 {
134 template<class ReturnType, class InputType = ReturnType>
135 inline void split(std::vector<ReturnType>& _ret, const InputType& _source, const InputType& _delims)
136 {
137 size_t start = _source.find_first_not_of(_delims);
138 while (start != _source.npos)
139 {
140 size_t end = _source.find_first_of(_delims, start);
141 if (end != _source.npos)
142 _ret.emplace_back(_source.substr(start, end - start));
143 else
144 {
145 _ret.emplace_back(_source.substr(start));
146 break;
147 }
148 start = _source.find_first_not_of(_delims, end + 1);
149 }
150 }
151 } // namespace templates
152
153 inline std::vector<std::string> split(std::string_view _source, std::string_view _delims = "\t\n ")
154 {
155 std::vector<std::string> result;
156 templates::split<std::string, std::string_view>(result, _source, _delims);
157 return result;
158 }
159
160 template<typename... Args>
161 inline bool parseComplex(std::string_view _value, Args&... args)
162 {
163 std::stringstream stream;
164 stream << _value;
165
166 ((stream >> args), ...);
167
168 if (stream.fail())
169 return false;
170
171 // check if there is more data, return false in this case
172 int item = stream.get();
173 while (item != -1)
174 {
175 if (item != ' ' && item != '\t')
176 return false;
177 item = stream.get();
178 }
179
180 return true;
181 }
182
183 template<>
184 inline bool parseComplex<bool>(std::string_view _value, bool& arg)
185 {
186 std::string value(_value);
187 trim(value);
188 if ((value == "True") || (value == "true") || (value == "1"))
189 {
190 arg = true;
191 return true;
192 }
193 if ((value == "False") || (value == "false") || (value == "0"))
194 {
195 arg = false;
196 return true;
197 }
198
199 return false;
200 }
201
202 inline bool startWith(std::string_view _source, std::string_view _value)
203 {
204#if __cplusplus >= 202002L
205 return _source.starts_with(_value);
206#else
207 size_t count = _value.size();
208 if (_source.size() < count)
209 return false;
210 for (size_t index = 0; index < count; ++index)
211 {
212 if (_source[index] != _value[index])
213 return false;
214 }
215 return true;
216#endif
217 }
218
219 inline bool endWith(std::string_view _source, std::string_view _value)
220 {
221#if __cplusplus >= 202002L
222 return _source.ends_with(_value);
223#else
224 size_t count = _value.size();
225 if (_source.size() < count)
226 return false;
227 size_t offset = _source.size() - count;
228 for (size_t index = 0; index < count; ++index)
229 {
230 if (_source[index + offset] != _value[index])
231 return false;
232 }
233 return true;
234#endif
235 }
236
237} // namespace MyGUI
238
239#endif // MYGUI_STRING_UTILITY_H_
MyGUI_Prerequest.h
MyGUI::utility::templates
Definition MyGUI_StringUtility.h:133
MyGUI::utility::templates::split
void split(std::vector< ReturnType > &_ret, const InputType &_source, const InputType &_delims)
Definition MyGUI_StringUtility.h:135
MyGUI::utility
Definition MyGUI_StringUtility.h:15
MyGUI::utility::parseUInt
unsigned int parseUInt(std::string_view _value)
Definition MyGUI_StringUtility.h:107
MyGUI::utility::endWith
bool endWith(std::string_view _source, std::string_view _value)
Definition MyGUI_StringUtility.h:219
MyGUI::utility::parseBool
bool parseBool(std::string_view _value)
Definition MyGUI_StringUtility.h:127
MyGUI::utility::parseFloat
float parseFloat(std::string_view _value)
Definition MyGUI_StringUtility.h:117
MyGUI::utility::parseSizeT
size_t parseSizeT(std::string_view _value)
Definition MyGUI_StringUtility.h:112
MyGUI::utility::split
std::vector< std::string > split(std::string_view _source, std::string_view _delims="\t\n ")
Definition MyGUI_StringUtility.h:153
MyGUI::utility::toString
std::string toString(T _value)
Definition MyGUI_StringUtility.h:26
MyGUI::utility::toString< bool >
std::string toString< bool >(bool _value)
Definition MyGUI_StringUtility.h:53
MyGUI::utility::parseComplex< bool >
bool parseComplex< bool >(std::string_view _value, bool &arg)
Definition MyGUI_StringUtility.h:184
MyGUI::utility::parseValue
T parseValue(std::string_view _value)
Definition MyGUI_StringUtility.h:60
MyGUI::utility::startWith
bool startWith(std::string_view _source, std::string_view _value)
Definition MyGUI_StringUtility.h:202
MyGUI::utility::parseComplex
bool parseComplex(std::string_view _value, Args &... args)
Definition MyGUI_StringUtility.h:161
MyGUI::utility::parseInt
int parseInt(std::string_view _value)
Definition MyGUI_StringUtility.h:102
MyGUI::utility::parseDouble
double parseDouble(std::string_view _value)
Definition MyGUI_StringUtility.h:122
MyGUI::utility::trim
void trim(std::string &_str, bool _left=true, bool _right=true)
Definition MyGUI_StringUtility.h:17