7#ifndef MYGUI_STRING_UTILITY_H_
8#define MYGUI_STRING_UTILITY_H_
17 inline void trim(std::string& _str,
bool _left =
true,
bool _right =
true)
20 _str.erase(_str.find_last_not_of(
" \t\r") + 1);
22 _str.erase(0, _str.find_first_not_of(
" \t\r"));
28 std::ostringstream stream;
33 inline const std::string&
toString(
const std::string& _value)
39 inline std::string
toString(std::string_view _value)
41 return std::string{_value};
44 template<
typename... Args>
47 std::ostringstream stream;
48 ((stream << args), ...);
55 return _value ?
"true" :
"false";
62 std::stringstream stream;
70 int item = stream.get();
73 if (item !=
' ' && item !=
'\t')
84 return _value ==
"True" || _value ==
"true" || _value ==
"1";
134 template<
class ReturnType,
class InputType = ReturnType>
135 inline void split(std::vector<ReturnType>& _ret,
const InputType& _source,
const InputType& _delims)
137 size_t start = _source.find_first_not_of(_delims);
138 while (start != _source.npos)
140 size_t end = _source.find_first_of(_delims, start);
141 if (end != _source.npos)
142 _ret.emplace_back(_source.substr(start, end - start));
145 _ret.emplace_back(_source.substr(start));
148 start = _source.find_first_not_of(_delims, end + 1);
153 inline std::vector<std::string>
split(std::string_view _source, std::string_view _delims =
"\t\n ")
155 std::vector<std::string> result;
160 template<
typename... Args>
163 std::stringstream stream;
166 ((stream >> args), ...);
172 int item = stream.get();
175 if (item !=
' ' && item !=
'\t')
186 std::string value(_value);
188 if ((value ==
"True") || (value ==
"true") || (value ==
"1"))
193 if ((value ==
"False") || (value ==
"false") || (value ==
"0"))
202 inline bool startWith(std::string_view _source, std::string_view _value)
204#if __cplusplus >= 202002L
205 return _source.starts_with(_value);
207 size_t count = _value.size();
208 if (_source.size() < count)
210 for (
size_t index = 0; index < count; ++index)
212 if (_source[index] != _value[index])
219 inline bool endWith(std::string_view _source, std::string_view _value)
221#if __cplusplus >= 202002L
222 return _source.ends_with(_value);
224 size_t count = _value.size();
225 if (_source.size() < count)
227 size_t offset = _source.size() - count;
228 for (
size_t index = 0; index < count; ++index)
230 if (_source[index + offset] != _value[index])
void split(std::vector< ReturnType > &_ret, const InputType &_source, const InputType &_delims)
unsigned int parseUInt(std::string_view _value)
bool endWith(std::string_view _source, std::string_view _value)
bool parseBool(std::string_view _value)
float parseFloat(std::string_view _value)
size_t parseSizeT(std::string_view _value)
std::vector< std::string > split(std::string_view _source, std::string_view _delims="\t\n ")
std::string toString(T _value)
std::string toString< bool >(bool _value)
bool parseComplex< bool >(std::string_view _value, bool &arg)
T parseValue(std::string_view _value)
bool startWith(std::string_view _source, std::string_view _value)
bool parseComplex(std::string_view _value, Args &... args)
int parseInt(std::string_view _value)
double parseDouble(std::string_view _value)
void trim(std::string &_str, bool _left=true, bool _right=true)