MyGUI 3.5.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
14namespace MyGUI::types
15{
16
17 template<typename T>
18 struct TPoint;
19 template<typename T>
20 struct TSize;
21 template<typename T>
22 struct TRect;
23 template<typename T>
24 struct TCoord;
25
26}
27
28namespace MyGUI::utility
29{
30
31 MYGUI_EXPORT void trim(std::string& _str, bool _left = true, bool _right = true);
32
33 template<typename T>
34 inline std::string toString(T _value)
35 {
36 std::ostringstream stream;
37 stream << _value;
38 return stream.str();
39 }
40
41 // Keep numeric formatting in the engine; custom types use the template above.
42 extern template MYGUI_EXPORT std::string toString<int>(int);
43 extern template MYGUI_EXPORT std::string toString<size_t>(size_t);
44 extern template MYGUI_EXPORT std::string toString<float>(float);
45
46 MYGUI_EXPORT const std::string& toString(const std::string& _value);
47
48 template<>
49 inline std::string toString(std::string_view _value)
50 {
51 return std::string{_value};
52 }
53
54 template<typename... Args>
55 inline std::string toString(Args&&... args)
56 {
57 std::ostringstream stream;
58 ((stream << args), ...);
59 return stream.str();
60 }
61
62 template<>
63 inline std::string toString<bool>(bool _value)
64 {
65 return _value ? "true" : "false";
66 }
67
68
69 template<typename T>
70 T parseValue(std::string_view value)
71 {
72 std::stringstream stream(std::string{value});
73
74 T result{};
75 if (stream >> result)
76 {
77 stream >> std::ws;
78 if (!stream.eof())
79 result = T{};
80 }
81
82 return result;
83 }
84
85 // Share stream parsers used by the core with its consumers.
86 extern template MYGUI_EXPORT int parseValue<int>(std::string_view);
87 extern template MYGUI_EXPORT size_t parseValue<size_t>(std::string_view);
88 extern template MYGUI_EXPORT float parseValue<float>(std::string_view);
89 extern template MYGUI_EXPORT types::TPoint<int> parseValue<types::TPoint<int>>(std::string_view);
90 extern template MYGUI_EXPORT types::TPoint<float> parseValue<types::TPoint<float>>(std::string_view);
91 extern template MYGUI_EXPORT types::TSize<int> parseValue<types::TSize<int>>(std::string_view);
92 extern template MYGUI_EXPORT types::TSize<float> parseValue<types::TSize<float>>(std::string_view);
93 extern template MYGUI_EXPORT types::TRect<int> parseValue<types::TRect<int>>(std::string_view);
94 extern template MYGUI_EXPORT types::TCoord<int> parseValue<types::TCoord<int>>(std::string_view);
95 extern template MYGUI_EXPORT types::TCoord<float> parseValue<types::TCoord<float>>(std::string_view);
96
97 // bool specialization
98 template<>
99 inline bool parseValue(std::string_view _value)
100 {
101 return _value == "True" || _value == "true" || _value == "1";
102 }
103
104 // char specialization
105 template<>
106 inline char parseValue(std::string_view _value)
107 {
108 return static_cast<char>(parseValue<short>(_value));
109 }
110
111 // unsigned char specialization
112 template<>
113 inline unsigned char parseValue(std::string_view _value)
114 {
115 return static_cast<unsigned char>(parseValue<unsigned short>(_value));
116 }
117
118
119 MYGUI_EXPORT int parseInt(std::string_view _value);
120 MYGUI_EXPORT unsigned int parseUInt(std::string_view _value);
121 MYGUI_EXPORT size_t parseSizeT(std::string_view _value);
122 MYGUI_EXPORT float parseFloat(std::string_view _value);
123 MYGUI_EXPORT double parseDouble(std::string_view _value);
124 MYGUI_EXPORT bool parseBool(std::string_view _value);
125
126 MYGUI_EXPORT std::vector<std::string> split(std::string_view _source, std::string_view _delims = "\t\n ");
127
128 template<typename... Args>
129 inline bool parseComplex(std::string_view _value, Args&... args)
130 {
131 std::stringstream stream(std::string{_value});
132
133 ((stream >> args), ...);
134
135 if (stream.fail())
136 return false;
137
138 // check if there is more data, return false in this case
139 int item = stream.get();
140 while (item != -1)
141 {
142 if (item != ' ' && item != '\t')
143 return false;
144 item = stream.get();
145 }
146
147 return true;
148 }
149
150 template<>
151 inline bool parseComplex<bool>(std::string_view _value, bool& arg)
152 {
153 std::string value(_value);
154 trim(value);
155 if ((value == "True") || (value == "true") || (value == "1"))
156 {
157 arg = true;
158 return true;
159 }
160 if ((value == "False") || (value == "false") || (value == "0"))
161 {
162 arg = false;
163 return true;
164 }
165
166 return false;
167 }
168
169 MYGUI_EXPORT bool startWith(std::string_view _source, std::string_view _value);
170 MYGUI_EXPORT bool endWith(std::string_view _source, std::string_view _value);
171
172} // namespace MyGUI
173
174#endif // MYGUI_STRING_UTILITY_H_
#define MYGUI_EXPORT
unsigned int parseUInt(std::string_view _value)
template MYGUI_EXPORT int parseValue< int >(std::string_view)
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)
template MYGUI_EXPORT std::string toString< int >(int)
__attribute__((visibility("default"))) void trim(std std::string toString(T _value)
template MYGUI_EXPORT std::string toString< size_t >(size_t)
template MYGUI_EXPORT float parseValue< float >(std::string_view)
void trim(std::string &_str, bool _left, bool _right)
__attribute__((visibility("default"))) int parseInt(std __attribute__((visibility("default"))) unsigned int parseUInt(std __attribute__((visibility("default"))) size_t parseSizeT(std __attribute__((visibility("default"))) float parseFloat(std __attribute__((visibility("default"))) double parseDouble(std __attribute__((visibility("default"))) bool parseBool(std __attribute__((visibility("default"))) std bool parseComplex(std::string_view _value, Args &... args)
template MYGUI_EXPORT size_t parseValue< size_t >(std::string_view)
template MYGUI_EXPORT std::string toString< float >(float)
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)