MVault 0.0.1
Loading...
Searching...
No Matches
FromString.h
1#pragma once
2
3#include <string>
4
5namespace mvlt
6{
29 template <class T>
30 inline bool FromString(const std::string& stringToCopyDataFrom, T& data) noexcept { return false; }
31
42 template <>
43 inline bool FromString(const std::string& stringToCopyDataFrom, std::string& data) noexcept
44 {
45 data = stringToCopyDataFrom;
46 return true;
47 }
48
63 template <>
64 inline bool FromString(const std::string& stringToCopyDataFrom, bool& data) noexcept
65 {
66 if (stringToCopyDataFrom == "true")
67 {
68 data = true;
69 return true;
70 }
71 if (stringToCopyDataFrom == "false")
72 {
73 data = false;
74 return true;
75 }
76 return false;
77 }
78
91 template <class T>
92 inline bool FromStringDoubleAndFloat(const std::string& stringToCopyDataFrom, T& data) noexcept
93 {
94 if (stringToCopyDataFrom.length() == 0) return false;
95
96 int integer = 0, fractional = 0;
97
98 std::size_t i = 0;
99 bool isNegative = false;
100
101 if (stringToCopyDataFrom[0] == '-' && stringToCopyDataFrom.length() > 1)
102 {
103 isNegative = true;
104 i = 1;
105 }
106
107 for (; i < stringToCopyDataFrom.length(); ++i)
108 {
109 if (stringToCopyDataFrom[i] == '.')
110 {
111 ++i;
112 break;
113 }
114
115 if (stringToCopyDataFrom[i] < '0' || stringToCopyDataFrom[i] > '9') return false;
116 integer *= 10;
117 integer += static_cast<int>(stringToCopyDataFrom[i] - '0');
118 }
119
120 T coeff = 1;
121 for (; i < stringToCopyDataFrom.length(); ++i)
122 {
123 if (stringToCopyDataFrom[i] < '0' || stringToCopyDataFrom[i] > '9') return false;
124 coeff /= 10;
125 fractional *= 10;
126 fractional += static_cast<int>(stringToCopyDataFrom[i] - '0');
127 }
128
129 data = static_cast<T>(integer) + static_cast<T>(fractional) * coeff;
130
131 if (isNegative) data *= -1;
132
133 return true;
134 }
135
146 template <>
147 inline bool FromString(const std::string& stringToCopyDataFrom, float& data) noexcept
148 {
149 return FromStringDoubleAndFloat(stringToCopyDataFrom, data);
150 }
151
162 template <>
163 inline bool FromString(const std::string& stringToCopyDataFrom, double& data) noexcept
164 {
165 return FromStringDoubleAndFloat(stringToCopyDataFrom, data);
166 }
167
182 template <class T>
183 inline bool FromStringSignedInt(const std::string& stringToCopyDataFrom, T& data) noexcept
184 {
185 if (stringToCopyDataFrom.length() == 0) return false;
186
187 T res = 0;
188
189 std::size_t i = 0;
190 bool isNegative = false;
191
192 if (stringToCopyDataFrom[0] == '-' && stringToCopyDataFrom.length() > 1)
193 {
194 isNegative = true;
195 i = 1;
196 }
197
198 for (; i < stringToCopyDataFrom.length(); ++i)
199 {
200 if (stringToCopyDataFrom[i] < '0' || stringToCopyDataFrom[i] > '9') return false;
201 res *= 10;
202 res += static_cast<int>(stringToCopyDataFrom[i] - '0');
203 }
204
205 if (isNegative) data = res * -1;
206 else data = res;
207
208 return true;
209 }
210
221 template <>
222 inline bool FromString(const std::string& stringToCopyDataFrom, short& data) noexcept
223 {
224 return FromStringSignedInt(stringToCopyDataFrom, data);
225 }
226
237 template <>
238 inline bool FromString(const std::string& stringToCopyDataFrom, int& data) noexcept
239 {
240 return FromStringSignedInt(stringToCopyDataFrom, data);
241 }
242
253 template <>
254 inline bool FromString(const std::string& stringToCopyDataFrom, long& data) noexcept
255 {
256 return FromStringSignedInt(stringToCopyDataFrom, data);
257 }
258
269 template <>
270 inline bool FromString(const std::string& stringToCopyDataFrom, long long& data) noexcept
271 {
272 return FromStringSignedInt(stringToCopyDataFrom, data);
273 }
274
289 template <class T>
290 inline bool FromStringUnsignedInt(const std::string& stringToCopyDataFrom, T& data) noexcept
291 {
292 if (stringToCopyDataFrom.length() == 0) return false;
293
294 T res = 0;
295
296 for (std::size_t i = 0; i < stringToCopyDataFrom.length(); ++i)
297 {
298 if (stringToCopyDataFrom[i] < '0' || stringToCopyDataFrom[i] > '9') return false;
299 res *= 10;
300 res += static_cast<int>(stringToCopyDataFrom[i] - '0');
301 }
302
303 data = res;
304 return true;
305 }
306
317 template <>
318 inline bool FromString(const std::string& stringToCopyDataFrom, unsigned short& data) noexcept
319 {
320 return FromStringSignedInt(stringToCopyDataFrom, data);
321 }
322
333 template <>
334 inline bool FromString(const std::string& stringToCopyDataFrom, unsigned int& data) noexcept
335 {
336 return FromStringSignedInt(stringToCopyDataFrom, data);
337 }
338
349 template <>
350 inline bool FromString(const std::string& stringToCopyDataFrom, unsigned long& data) noexcept
351 {
352 return FromStringSignedInt(stringToCopyDataFrom, data);
353 }
354
365 template <>
366 inline bool FromString(const std::string& stringToCopyDataFrom, unsigned long long& data) noexcept
367 {
368 return FromStringSignedInt(stringToCopyDataFrom, data);
369 }
370
372}
bool FromStringUnsignedInt(const std::string &stringToCopyDataFrom, T &data) noexcept
FromStringSignedInt template for working with all unsigned signed integers.
Definition FromString.h:290
bool FromStringDoubleAndFloat(const std::string &stringToCopyDataFrom, T &data) noexcept
FromStringDoubleAndFloat template for working with float and double.
Definition FromString.h:92
bool FromStringSignedInt(const std::string &stringToCopyDataFrom, T &data) noexcept
FromStringSignedInt template for working with all signed integers.
Definition FromString.h:183
bool FromString(const std::string &stringToCopyDataFrom, T &data) noexcept
A template method for providing an interface converting string to a any type.
Definition FromString.h:30