MyGUI 3.5.3
MyGUI_StringUtility.cpp
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#include "MyGUI_Precompiled.h"
9#include "MyGUI_Types.h"
10
11namespace MyGUI::utility
12{
13
14 template MYGUI_EXPORT std::string toString<int>(int);
15 template MYGUI_EXPORT std::string toString<size_t>(size_t);
16 template MYGUI_EXPORT std::string toString<float>(float);
17
18 template MYGUI_EXPORT int parseValue<int>(std::string_view);
19 template MYGUI_EXPORT size_t parseValue<size_t>(std::string_view);
20 template MYGUI_EXPORT float parseValue<float>(std::string_view);
28
29 void trim(std::string& _str, bool _left, bool _right)
30 {
31 if (_right)
32 _str.erase(_str.find_last_not_of(" \t\r") + 1);
33 if (_left)
34 _str.erase(0, _str.find_first_not_of(" \t\r"));
35 }
36
37 const std::string& toString(const std::string& _value)
38 {
39 return _value;
40 }
41
42 int parseInt(std::string_view _value)
43 {
44 return parseValue<int>(_value);
45 }
46
47 unsigned int parseUInt(std::string_view _value)
48 {
49 return parseValue<unsigned int>(_value);
50 }
51
52 size_t parseSizeT(std::string_view _value)
53 {
54 return parseValue<size_t>(_value);
55 }
56
57 float parseFloat(std::string_view _value)
58 {
59 return parseValue<float>(_value);
60 }
61
62 double parseDouble(std::string_view _value)
63 {
64 return parseValue<double>(_value);
65 }
66
67 bool parseBool(std::string_view _value)
68 {
69 return parseValue<bool>(_value);
70 }
71
72 namespace templates
73 {
74
75 template<typename ReturnType, typename InputType = ReturnType>
76 std::vector<ReturnType> split(const InputType& _source, const InputType& _delims)
77 {
78 std::vector<ReturnType> result;
79
80 std::string_view source{_source};
81 std::string_view delims{_delims};
82
83 size_t start = source.find_first_not_of(delims);
84
85 while (start != std::string_view::npos)
86 {
87 const auto end = source.find_first_of(delims, start);
88
89 if (end == std::string_view::npos)
90 {
91 result.emplace_back(source.substr(start));
92 break;
93 }
94
95 result.emplace_back(source.substr(start, end - start));
96 start = source.find_first_not_of(delims, end + 1);
97 }
98
99 return result;
100 }
101
102 } // namespace templates
103
104 std::vector<std::string> split(std::string_view _source, std::string_view _delims)
105 {
106 return templates::split<std::string>(_source, _delims);
107 }
108
109 bool startWith(std::string_view _source, std::string_view _value)
110 {
111#if __cplusplus >= 202002L
112 return _source.starts_with(_value);
113#else
114 size_t count = _value.size();
115 if (_source.size() < count)
116 return false;
117 for (size_t index = 0; index < count; ++index)
118 {
119 if (_source[index] != _value[index])
120 return false;
121 }
122 return true;
123#endif
124 }
125
126 bool endWith(std::string_view _source, std::string_view _value)
127 {
128#if __cplusplus >= 202002L
129 return _source.ends_with(_value);
130#else
131 size_t count = _value.size();
132 if (_source.size() < count)
133 return false;
134 size_t offset = _source.size() - count;
135 for (size_t index = 0; index < count; ++index)
136 {
137 if (_source[index + offset] != _value[index])
138 return false;
139 }
140 return true;
141#endif
142 }
143
144} // namespace MyGUI
#define MYGUI_EXPORT
std::vector< ReturnType > split(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)
T parseValue(std::string_view value)
size_t parseSizeT(std::string_view _value)
__attribute__((visibility("default"))) void trim(std std::string toString(T _value)
void trim(std::string &_str, bool _left, bool _right)
bool startWith(std::string_view _source, std::string_view _value)
std::vector< std::string > split(std::string_view _source, std::string_view _delims)
int parseInt(std::string_view _value)
double parseDouble(std::string_view _value)