MVault 1.0.0
Simple c++ database
Loading...
Searching...
No Matches
FromString.h
1#pragma once
2
3#include <string>
4#include <cstdint>
5
6namespace mvlt
7{
30 template <class T>
31 inline bool FromString(const std::string& stringToCopyDataFrom, T& data) noexcept { return false; }
32
43 template <>
44 inline bool FromString(const std::string& stringToCopyDataFrom, std::string& data) noexcept
45 {
47 return true;
48 }
49
64 template <>
65 inline bool FromString(const std::string& stringToCopyDataFrom, bool& data) noexcept
66 {
67 if (stringToCopyDataFrom == "true")
68 {
69 data = true;
70 return true;
71 }
72 if (stringToCopyDataFrom == "false")
73 {
74 data = false;
75 return true;
76 }
77 return false;
78 }
79
92 template <class T>
93 inline bool FromStringDoubleAndFloat(const std::string& stringToCopyDataFrom, T& data) noexcept
94 {
95 if (stringToCopyDataFrom.length() == 0) return false;
96
97 int integer = 0, fractional = 0;
98
99 std::size_t i = 0;
100 bool isNegative = false;
101
102 if (stringToCopyDataFrom[0] == '-' && stringToCopyDataFrom.length() > 1)
103 {
104 isNegative = true;
105 i = 1;
106 }
107
108 for (; i < stringToCopyDataFrom.length(); ++i)
109 {
110 if (stringToCopyDataFrom[i] == '.')
111 {
112 ++i;
113 break;
114 }
115
116 if (stringToCopyDataFrom[i] < '0' || stringToCopyDataFrom[i] > '9') return false;
117 integer *= 10;
118 integer += static_cast<int>(stringToCopyDataFrom[i] - '0');
119 }
120
121 T coeff = 1;
122 for (; i < stringToCopyDataFrom.length(); ++i)
123 {
124 if (stringToCopyDataFrom[i] < '0' || stringToCopyDataFrom[i] > '9') return false;
125 coeff /= 10;
126 fractional *= 10;
127 fractional += static_cast<int>(stringToCopyDataFrom[i] - '0');
128 }
129
130 data = static_cast<T>(integer) + static_cast<T>(fractional) * coeff;
131
132 if (isNegative) data *= -1;
133
134 return true;
135 }
136
147 template <>
148 inline bool FromString(const std::string& stringToCopyDataFrom, float& data) noexcept
149 {
151 }
152
163 template <>
164 inline bool FromString(const std::string& stringToCopyDataFrom, double& data) noexcept
165 {
167 }
168
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 // codechecker_intentional [all] its not error because negative numbers handled by if
206 if (isNegative) data = res * -1;
207 else data = res;
208
209 return true;
210 }
211
222 template <>
223 inline bool FromString(const std::string& stringToCopyDataFrom, std::int16_t& data) noexcept
224 {
226 }
227
238 template <>
239 inline bool FromString(const std::string& stringToCopyDataFrom, std::int32_t& data) noexcept
240 {
242 }
243
254 template <>
255 inline bool FromString(const std::string& stringToCopyDataFrom, std::int64_t& data) noexcept
256 {
258 }
259
274 template <class T>
275 inline bool FromStringUnsignedInt(const std::string& stringToCopyDataFrom, T& data) noexcept
276 {
277 if (stringToCopyDataFrom.length() == 0) return false;
278
279 T res = 0;
280
281 for (std::size_t i = 0; i < stringToCopyDataFrom.length(); ++i)
282 {
283 if (stringToCopyDataFrom[i] < '0' || stringToCopyDataFrom[i] > '9') return false;
284 res *= 10;
285 res += static_cast<int>(stringToCopyDataFrom[i] - '0');
286 }
287
288 data = res;
289 return true;
290 }
291
302 template <>
303 inline bool FromString(const std::string& stringToCopyDataFrom, std::uint16_t& data) noexcept
304 {
306 }
307
318 template <>
319 inline bool FromString(const std::string& stringToCopyDataFrom, std::uint32_t& data) noexcept
320 {
322 }
323
334 template <>
335 inline bool FromString(const std::string& stringToCopyDataFrom, std::uint64_t& data) noexcept
336 {
338 }
339
341}
Iterator class for all library maps.
Definition Map.h:18
bool FromStringUnsignedInt(const std::string &stringToCopyDataFrom, T &data) noexcept
FromStringSignedInt template for working with all unsigned signed integers.
Definition FromString.h:275
bool FromStringDoubleAndFloat(const std::string &stringToCopyDataFrom, T &data) noexcept
FromStringDoubleAndFloat template for working with float and double.
Definition FromString.h:93
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:31