xtd 0.2.0
Loading...
Searching...
No Matches
parse.h
Go to the documentation of this file.
1
4#pragma once
5
7// Workaround : Like Windows.h (with NOMINMAX defined), some includes define max as a macro and this causes compilation errors.
8#if defined(_MSC_VER) && defined(max)
9# if __cplusplus < 202302L
10# pragma message("The macro `max` is defined. If you include the `Windows.h` file, please define the 'NOMINMAX' constant before including `Windows.h`. xtd will undef the `max` macro.")
11# else
12# warning "The macro `max` is defined. If you include the `Windows.h` file, please define the 'NOMINMAX' constant before including `Windows.h`. xtd will undef the `max` macro."
13# endif
14# undef max
15#endif
16
17#define __XTD_CORE_INTERNAL__
18#include "internal/__parse.h"
19#undef __XTD_CORE_INTERNAL__
21#include "number_styles.h"
22#include "types.h"
23#include <string>
24
26namespace xtd {
33 template<typename value_t>
34 inline value_t parse(const std::string& str) {
35 if (std::is_enum<value_t>::value) return __parse_enum<value_t>(str);
36 __throw_parse_format_exception("Parse specialisation not found", __FILE__, __LINE__, __func__);
37 return {};
38 }
39
46 template<typename value_t>
47 inline value_t parse(const std::string& str, const std::locale& locale) {__throw_parse_format_exception("Parse specialisation not found", __FILE__, __LINE__, __func__); return {};}
48
55 template<typename value_t>
56 inline value_t parse(const std::string& str, const std::string& fmt) {__throw_parse_format_exception("Parse specialisation not found", __FILE__, __LINE__, __func__); return {};}
57
64 template<>
65 inline std::string parse<std::string>(const std::string& str) {return str;}
66
73 template<typename value_t>
74 inline value_t parse(const std::string& str, number_styles) {__throw_parse_format_exception("Parse specialisation not found", __FILE__, __LINE__, __func__); return {};}
75
82 template<typename value_t>
83 inline value_t parse(const std::string& str, number_styles, const std::locale& locale) {__throw_parse_format_exception("Parse specialisation not found", __FILE__, __LINE__, __func__); return {};}
84
91 template<>
92 inline xtd::sbyte parse<xtd::sbyte>(const std::string& str, number_styles styles) {return __parse_number<xtd::sbyte>(str, styles);}
93
100 template<>
101 inline char parse<char>(const std::string& str, number_styles styles) {return __parse_number<char>(str, styles);}
102
109 template<>
110 inline unsigned char parse<unsigned char>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned char>(str, styles);}
111
118 template<>
119 inline short parse<short>(const std::string& str, number_styles styles) {return __parse_number<short>(str, styles);}
120
127 template<>
128 inline unsigned short parse<unsigned short>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned short>(str, styles);}
129
136 template<>
137 inline int parse<int>(const std::string& str, number_styles styles) {return __parse_number<int>(str, styles);}
138
145 template<>
146 inline unsigned int parse<unsigned int>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned int>(str, styles);}
147
154 template<>
155 inline long parse<long>(const std::string& str, number_styles styles) {return __parse_number<long>(str, styles);}
156
163 template<>
164 inline unsigned long parse<unsigned long>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned long>(str, styles);}
165
172 template<>
173 inline long long parse<long long>(const std::string& str, number_styles styles) {return __parse_number<long long>(str, styles);}
174
181 template<>
182 inline unsigned long long parse<unsigned long long>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned long long>(str, styles);}
183
190 template<>
191 inline float parse<float>(const std::string& str, number_styles styles) {return __parse_floating_point_number<float>(str, styles, std::locale());}
192
199 template<>
200 inline float parse<float>(const std::string& str, number_styles styles, const std::locale& locale) {return __parse_floating_point_number<float>(str, styles, locale);}
201
208 template<>
209 inline double parse<double>(const std::string& str, number_styles styles) {return __parse_floating_point_number<double>(str, styles, std::locale());}
210
217 template<>
218 inline double parse<double>(const std::string& str, number_styles styles, const std::locale& locale) {return __parse_floating_point_number<double>(str, styles, locale);}
219
226 template<>
227 inline long double parse<long double>(const std::string& str, number_styles styles) {return __parse_floating_point_number<long double>(str, styles, std::locale());}
228
235 template<>
236 inline long double parse<long double>(const std::string& str, number_styles styles, const std::locale& locale) {return __parse_floating_point_number<long double>(str, styles, locale);}
237
244 template<>
245 inline xtd::sbyte parse<xtd::sbyte>(const std::string& str) {return parse<xtd::sbyte>(str, number_styles::integer);}
246
253 template<>
254 inline char parse<char>(const std::string& str) {return parse<char>(str, number_styles::integer);}
255
262 template<>
263 inline unsigned char parse<unsigned char>(const std::string& str) {return parse<unsigned char>(str, number_styles::integer);}
264
271 template<>
272 inline short parse<short>(const std::string& str) {return parse<short>(str, number_styles::integer);}
273
280 template<>
281 inline unsigned short parse<unsigned short>(const std::string& str) {return parse<unsigned short>(str, number_styles::integer);}
282
289 template<>
290 inline int parse<int>(const std::string& str) {return parse<int>(str, number_styles::integer);}
291
298 template<>
299 inline unsigned int parse<unsigned int>(const std::string& str) {return parse<unsigned int>(str, number_styles::integer);}
300
307 template<>
308 inline long parse<long>(const std::string& str) {return parse<long>(str, number_styles::integer);}
309
316 template<>
317 inline unsigned long parse<unsigned long>(const std::string& str) {return parse<unsigned long>(str, number_styles::integer);}
318
325 template<>
326 inline long long parse<long long>(const std::string& str) {return parse<long long>(str, number_styles::integer);}
327
334 template<>
335 inline unsigned long long parse<unsigned long long>(const std::string& str) {return parse<unsigned long long>(str, number_styles::integer);}
336
343 template<>
344 inline float parse<float>(const std::string& str) {return parse<float>(str, number_styles::fixed_point);}
345
352 template<>
353 inline double parse<double>(const std::string& str) {return parse<double>(str, number_styles::fixed_point);}
354
361 template<>
362 inline long double parse<long double>(const std::string& str) {return parse<long double>(str, number_styles::fixed_point);}
363
370 template<>
371 inline bool parse<bool>(const std::string& str) {
372 std::string lower_str = str;
373 while (lower_str.size() > 0 && (lower_str[0] == 9 || lower_str[0] == 10 || lower_str[0] == 11 || lower_str[0] == 12 || lower_str[0] == 13 || lower_str[0] == 32))
374 lower_str.erase(0, 1);
375 while (lower_str.size() > 0 && (lower_str[lower_str.size() - 1] == 9 || lower_str[lower_str.size() - 1] == 10 || lower_str[lower_str.size() - 1] == 11 || lower_str[lower_str.size() - 1] == 12 || lower_str[lower_str.size() - 1] == 13 || lower_str[lower_str.size() - 1] == 32))
376 lower_str.erase(lower_str.size() - 1, 1);
377 for (auto& c : lower_str)
378 c = static_cast<char>(std::tolower(c));
379 if (lower_str != "true" && lower_str != "1" && lower_str != "false" && lower_str != "0") __throw_parse_format_exception("Invalid string format", __FILE__, __LINE__, __func__);
380 return lower_str == "true" || lower_str == "1";
381 }
382
389 template<typename value_t>
390 inline value_t parse(const std::wstring& str) {__throw_parse_format_exception("Parse specialisation not found", __FILE__, __LINE__, __func__); return {};}
391
398 template<typename value_t>
399 inline value_t parse(const std::u16string& str) {__throw_parse_format_exception("Parse specialisation not found", __FILE__, __LINE__, __func__); return {};}
400
407 template<typename value_t>
408 inline value_t parse(const std::u32string& str) {__throw_parse_format_exception("Parse specialisation not found", __FILE__, __LINE__, __func__); return {};}
409
416 template<typename value_t, typename char_t>
417 inline bool try_parse(const std::basic_string<char_t>& str, value_t& value) noexcept {
418 try {
419 value = parse<value_t>(str);
420 return true;
421 } catch (...) {
422 return false;
423 }
424 }
425
432 template<typename value_t, typename char_t>
433 inline bool try_parse(const std::basic_string<char_t>& str, value_t& value, const std::locale& locale) noexcept {
434 try {
435 value = parse<value_t>(str, locale);
436 return true;
437 } catch (...) {
438 return false;
439 }
440 }
441
448 template<typename value_t, typename char_t>
449 inline bool try_parse(const char_t* str, value_t& value) noexcept {
450 return try_parse(std::basic_string<char_t>(str), value);
451 }
452
459 template<typename value_t, typename char_t>
460 inline bool try_parse(const char_t* str, value_t& value, const std::locale& locale) noexcept {
461 return try_parse(std::basic_string<char_t>(str), value, locale);
462 }
463
470 template<typename value_t, typename char_t>
471 inline bool try_parse(const std::basic_string<char_t>& str, value_t& value, number_styles style) noexcept {
472 try {
473 value = parse<value_t>(str, style);
474 return true;
475 } catch (...) {
476 return false;
477 }
478 }
479
486 template<typename value_t, typename char_t>
487 inline bool try_parse(const std::basic_string<char_t>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
488 try {
489 value = parse<value_t>(str, style, locale);
490 return true;
491 } catch (...) {
492 return false;
493 }
494 }
495
502 template<typename value_t, typename char_t>
503 inline bool try_parse(const char_t* str, value_t& value, number_styles style) noexcept {
504 return try_parse(std::basic_string<char_t>(str), value, style);
505 }
506
513 template<typename value_t, typename char_t>
514 inline bool try_parse(const char_t* str, value_t& value, number_styles style, const std::locale& locale) noexcept {
515 return try_parse(std::basic_string<char_t>(str), value, style, locale);
516 }
517}
518
int_least8_t sbyte
Represents a 8-bit signed integer.
Definition types.h:175
double parse< double >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:209
number_styles
Determines the styles permitted in numeric string arguments that are passed to the xtd::parse and xtd...
Definition number_styles.h:16
char parse< char >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:101
bool parse< bool >(const std::string &str)
Convert a string into a type.
Definition parse.h:371
xtd::sbyte parse< xtd::sbyte >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:92
unsigned long parse< unsigned long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:164
long long parse< long long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:173
long double parse< long double >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:227
int parse< int >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:137
long parse< long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:155
unsigned char parse< unsigned char >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:110
std::string parse< std::string >(const std::string &str)
Convert a string into a type.
Definition parse.h:65
float parse< float >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:191
short parse< short >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:119
unsigned int parse< unsigned int >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:146
unsigned short parse< unsigned short >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:128
bool try_parse(const std::basic_string< char_t > &str, value_t &value) noexcept
Convert a string into a type.
Definition parse.h:417
unsigned long long parse< unsigned long long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:182
value_t parse(const std::string &str)
Convert a string into a type.
Definition parse.h:34
@ integer
Indicates that the allow_leading_white, allow_trailing_white, and allow_leading_sign styles are used....
@ fixed_point
Indicates that the allow_leading_white, allow_trailing_white, allow_leading_sign, allow_decimal_point...
@ c
The C key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Contains xtd::number_styles enum class.
Contains xtd fundamental types.