Source/Gorgon/String.h

branch
4.x-dev
changeset 1380
8ea1cd4f856e
parent 1339
308d6cabe504
child 1483
4dbbdae925b4
equal deleted inserted replaced
1379:0c9e6ca3674b 1380:8ea1cd4f856e
189 189
190 template <> 190 template <>
191 inline float To<float>(const std::string &value) { 191 inline float To<float>(const std::string &value) {
192 if(value=="") return 0; 192 if(value=="") return 0;
193 193
194 return (float)std::stof(value); 194 return (float)std::strtof(value.c_str(), nullptr);
195 } 195 }
196 196
197 template <> 197 template <>
198 inline double To<double>(const std::string &value) { 198 inline double To<double>(const std::string &value) {
199 if(value=="") return 0; 199 if(value=="") return 0;
200 200
201 return std::stod(value); 201 return std::strtod(value.c_str(), nullptr);
202 } 202 }
203 203
204 template <> 204 template <>
205 inline long double To<long double>(const std::string &value) { 205 inline long double To<long double>(const std::string &value) {
206 if(value=="") return 0; 206 if(value=="") return 0;
207 207
208 return std::stold(value); 208 return std::strtold(value.c_str(), nullptr);
209 } 209 }
210 210
211 template <> 211 template <>
212 inline bool To<bool>(const std::string &value) { 212 inline bool To<bool>(const std::string &value) {
213 if(value=="false" || value=="no" || value=="" || To<int>(value)==0) 213 if(value=="false" || value=="no" || value=="" || To<int>(value)==0)
216 return true; 216 return true;
217 } 217 }
218 218
219 template <> 219 template <>
220 inline char To<char>(const char *value) { 220 inline char To<char>(const char *value) {
221 char *n; 221 return (char)std::strtol(value, nullptr, 10);
222 return (char)std::strtol(value, &n, 10);
223 } 222 }
224 223
225 template <> 224 template <>
226 inline unsigned char To<unsigned char>(const char *value) { 225 inline unsigned char To<unsigned char>(const char *value) {
227 char *n; 226 return (unsigned char)std::strtol(value, nullptr, 10);
228 return (unsigned char)std::strtol(value, &n, 10);
229 } 227 }
230 228
231 template <> 229 template <>
232 inline short To<short>(const char *value) { 230 inline short To<short>(const char *value) {
233 char *n; 231 return (short)std::strtol(value, nullptr, 10);
234 return (short)std::strtol(value, &n, 10);
235 } 232 }
236 233
237 template <> 234 template <>
238 inline unsigned short To<unsigned short>(const char *value) { 235 inline unsigned short To<unsigned short>(const char *value) {
239 char *n; 236 return (unsigned short)std::strtol(value, nullptr, 10);
240 return (unsigned short)std::strtol(value, &n, 10);
241 } 237 }
242 238
243 template <> 239 template <>
244 inline int To<int>(const char *value) { 240 inline int To<int>(const char *value) {
245 char *n; 241 return (int)std::strtol(value, nullptr, 10);
246 return (int)std::strtol(value, &n, 10);
247 } 242 }
248 243
249 template <> 244 template <>
250 inline unsigned To<unsigned>(const char *value) { 245 inline unsigned To<unsigned>(const char *value) {
251 char *n; 246 return (unsigned)std::strtol(value, nullptr, 10);
252 return (unsigned)std::strtol(value, &n, 10);
253 } 247 }
254 248
255 template <> 249 template <>
256 inline long To<long>(const char *value) { 250 inline long To<long>(const char *value) {
257 char *n; 251 return (long)std::strtol(value, nullptr, 10);
258 return (long)std::strtol(value, &n, 10);
259 } 252 }
260 253
261 template <> 254 template <>
262 inline unsigned long To<unsigned long>(const char *value) { 255 inline unsigned long To<unsigned long>(const char *value) {
263 char *n; 256 return (unsigned long)std::strtol(value, nullptr, 10);
264 return (unsigned long)std::strtol(value, &n, 10);
265 } 257 }
266 258
267 template <> 259 template <>
268 inline long long To<long long>(const char *value) { 260 inline long long To<long long>(const char *value) {
269 char *n; 261 return (long long)std::strtol(value, nullptr, 10);
270 return (long long)std::strtol(value, &n, 10);
271 } 262 }
272 263
273 template <> 264 template <>
274 inline unsigned long long To<unsigned long long>(const char *value) { 265 inline unsigned long long To<unsigned long long>(const char *value) {
275 char *n; 266 return (unsigned long long)std::strtol(value, nullptr, 10);
276 return (unsigned long long)std::strtol(value, &n, 10);
277 } 267 }
278 268
279 template <> 269 template <>
280 inline float To<float>(const char *value) { 270 inline float To<float>(const char *value) {
281 return (float)std::atof(value); 271 return (float)std::atof(value);
295 inline bool To<bool>(const char *value) { 285 inline bool To<bool>(const char *value) {
296 return To<bool>(std::string(value)); 286 return To<bool>(std::string(value));
297 } 287 }
298 288
299 /// Converts a hexadecimal number stored in the string to a given 289 /// Converts a hexadecimal number stored in the string to a given
300 /// integer type. 290 /// integer type. If given string is not a valid number this function
291 /// throws
301 template <class T_> 292 template <class T_>
302 T_ HexTo(const std::string &value); 293 T_ HexTo(const std::string &value);
303 294
304 template <> 295 template <>
305 inline int HexTo<int>(const std::string &value) { 296 inline int HexTo<int>(const std::string &value) {

mercurial