xtd 0.2.0
Loading...
Searching...
No Matches
parse.hpp
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.hpp"
19#undef __XTD_CORE_INTERNAL__
21#include "number_styles.hpp"
22#include "types.hpp"
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>
417 inline bool try_parse(const std::basic_string<char>& str, value_t& value) noexcept {
418 try {
419 value = parse<value_t>(str);
420 return true;
421 } catch (...) {
422 return false;
423 }
424 }
431 template<typename value_t>
432 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value) noexcept {
433 try {
434 value = parse<value_t>(str);
435 return true;
436 } catch (...) {
437 return false;
438 }
439 }
446 template<typename value_t>
447 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value) noexcept {
448 try {
449 value = parse<value_t>(str);
450 return true;
451 } catch (...) {
452 return false;
453 }
454 }
455#if defined(__xtd__cpp_lib_char8_t)
462 template<typename value_t>
463 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value) noexcept {
464 try {
465 value = parse<value_t>(str);
466 return true;
467 } catch (...) {
468 return false;
469 }
470 }
471#endif
478 template<typename value_t>
479 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value) noexcept {
480 try {
481 value = parse<value_t>(str);
482 return true;
483 } catch (...) {
484 return false;
485 }
486 }
487
494 template<typename value_t>
495 inline bool try_parse(const std::basic_string<char>& str, value_t& value, const std::locale& locale) noexcept {
496 try {
497 value = parse<value_t>(str, locale);
498 return true;
499 } catch (...) {
500 return false;
501 }
502 }
509 template<typename value_t>
510 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value, const std::locale& locale) noexcept {
511 try {
512 value = parse<value_t>(str, locale);
513 return true;
514 } catch (...) {
515 return false;
516 }
517 }
524 template<typename value_t>
525 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value, const std::locale& locale) noexcept {
526 try {
527 value = parse<value_t>(str, locale);
528 return true;
529 } catch (...) {
530 return false;
531 }
532 }
533#if defined(__xtd__cpp_lib_char8_t)
540 template<typename value_t>
541 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value, const std::locale& locale) noexcept {
542 try {
543 value = parse<value_t>(str, locale);
544 return true;
545 } catch (...) {
546 return false;
547 }
548 }
549#endif
556 template<typename value_t>
557 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value, const std::locale& locale) noexcept {
558 try {
559 value = parse<value_t>(str, locale);
560 return true;
561 } catch (...) {
562 return false;
563 }
564 }
565
572 template<typename value_t, typename char_t>
573 inline bool try_parse(const char_t* str, value_t& value) noexcept {
574 return try_parse(std::basic_string<char_t>(str), value);
575 }
576
583 template<typename value_t, typename char_t>
584 inline bool try_parse(const char_t* str, value_t& value, const std::locale& locale) noexcept {
585 return try_parse(std::basic_string<char_t>(str), value, locale);
586 }
587
594 template<typename value_t>
595 inline bool try_parse(const std::basic_string<char>& str, value_t& value, number_styles style) noexcept {
596 try {
597 value = parse<value_t>(str, style);
598 return true;
599 } catch (...) {
600 return false;
601 }
602 }
609 template<typename value_t>
610 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value, number_styles style) noexcept {
611 try {
612 value = parse<value_t>(str, style);
613 return true;
614 } catch (...) {
615 return false;
616 }
617 }
624 template<typename value_t>
625 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value, number_styles style) noexcept {
626 try {
627 value = parse<value_t>(str, style);
628 return true;
629 } catch (...) {
630 return false;
631 }
632 }
633#if defined(__xtd__cpp_lib_char8_t)
640 template<typename value_t>
641 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value, number_styles style) noexcept {
642 try {
643 value = parse<value_t>(str, style);
644 return true;
645 } catch (...) {
646 return false;
647 }
648 }
649#endif
656 template<typename value_t>
657 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value, number_styles style) noexcept {
658 try {
659 value = parse<value_t>(str, style);
660 return true;
661 } catch (...) {
662 return false;
663 }
664 }
665
672 template<typename value_t>
673 inline bool try_parse(const std::basic_string<char>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
674 try {
675 value = parse<value_t>(str, style, locale);
676 return true;
677 } catch (...) {
678 return false;
679 }
680 }
687 template<typename value_t>
688 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
689 try {
690 value = parse<value_t>(str, style, locale);
691 return true;
692 } catch (...) {
693 return false;
694 }
695 }
702 template<typename value_t>
703 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
704 try {
705 value = parse<value_t>(str, style, locale);
706 return true;
707 } catch (...) {
708 return false;
709 }
710 }
711#if defined(__xtd__cpp_lib_char8_t)
718 template<typename value_t>
719 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
720 try {
721 value = parse<value_t>(str, style, locale);
722 return true;
723 } catch (...) {
724 return false;
725 }
726 }
727#endif
734 template<typename value_t>
735 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
736 try {
737 value = parse<value_t>(str, style, locale);
738 return true;
739 } catch (...) {
740 return false;
741 }
742 }
743
750 template<typename value_t, typename char_t>
751 inline bool try_parse(const char_t* str, value_t& value, number_styles style) noexcept {
752 return try_parse(std::basic_string<char_t>(str), value, style);
753 }
754
761 template<typename value_t, typename char_t>
762 inline bool try_parse(const char_t* str, value_t& value, number_styles style, const std::locale& locale) noexcept {
763 return try_parse(std::basic_string<char_t>(str), value, style, locale);
764 }
765}
766
Contains parse methods.
int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.hpp:23
double parse< double >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:209
number_styles
Determines the styles permitted in numeric string arguments that are passed to the xtd::parse and xtd...
Definition number_styles.hpp:16
char parse< char >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:101
bool parse< bool >(const std::string &str)
Convert a string into a type.
Definition parse.hpp:371
xtd::sbyte parse< xtd::sbyte >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:92
unsigned long parse< unsigned long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:164
long long parse< long long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:173
long double parse< long double >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:227
int parse< int >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:137
long parse< long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:155
unsigned char parse< unsigned char >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:110
std::string parse< std::string >(const std::string &str)
Convert a string into a type.
Definition parse.hpp:65
float parse< float >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:191
short parse< short >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:119
unsigned int parse< unsigned int >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:146
unsigned short parse< unsigned short >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:128
unsigned long long parse< unsigned long long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:182
value_t parse(const std::string &str)
Convert a string into a type.
Definition parse.hpp:34
bool try_parse(const std::basic_string< char > &str, value_t &value) noexcept
Convert a string into a type.
Definition parse.hpp:417
@ 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.hpp:10
Contains xtd::number_styles enum class.
Contains xtd fundamental types.