xtd 0.2.0
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<class 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);
37 }
38
45 template<class value_t>
46 inline value_t parse(const std::string& str, const std::locale& locale) {xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::format, "Parse specialisation not found"); return {};}
47
54 template<class value_t>
55 inline value_t parse(const std::string& str, const std::string& fmt) {xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::format, "Parse specialisation not found"); return {};}
56
63 template<>
64 inline std::string parse<std::string>(const std::string& str) {return str;}
65
72 template<class value_t>
73 inline value_t parse(const std::string& str, number_styles) {xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::format, "Parse specialisation not found"); return {};}
74
81 template<class value_t>
82 inline value_t parse(const std::string& str, number_styles, const std::locale& locale) {xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::format, "Parse specialisation not found"); return {};}
83
90 template<>
91 inline xtd::sbyte parse<xtd::sbyte>(const std::string& str, number_styles styles) {return __parse_number<xtd::sbyte>(str, styles);}
92
99 template<>
100 inline char parse<char>(const std::string& str, number_styles styles) {return __parse_number<char>(str, styles);}
101
108 template<>
109 inline unsigned char parse<unsigned char>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned char>(str, styles);}
110
117 template<>
118 inline short parse<short>(const std::string& str, number_styles styles) {return __parse_number<short>(str, styles);}
119
126 template<>
127 inline unsigned short parse<unsigned short>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned short>(str, styles);}
128
135 template<>
136 inline int parse<int>(const std::string& str, number_styles styles) {return __parse_number<int>(str, styles);}
137
144 template<>
145 inline unsigned int parse<unsigned int>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned int>(str, styles);}
146
153 template<>
154 inline long parse<long>(const std::string& str, number_styles styles) {return __parse_number<long>(str, styles);}
155
162 template<>
163 inline unsigned long parse<unsigned long>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned long>(str, styles);}
164
171 template<>
172 inline long long parse<long long>(const std::string& str, number_styles styles) {return __parse_number<long long>(str, styles);}
173
180 template<>
181 inline unsigned long long parse<unsigned long long>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned long long>(str, styles);}
182
189 template<>
190 inline float parse<float>(const std::string& str, number_styles styles) {return __parse_floating_point_number<float>(str, styles, std::locale());}
191
198 template<>
199 inline float parse<float>(const std::string& str, number_styles styles, const std::locale& locale) {return __parse_floating_point_number<float>(str, styles, locale);}
200
207 template<>
208 inline double parse<double>(const std::string& str, number_styles styles) {return __parse_floating_point_number<double>(str, styles, std::locale());}
209
216 template<>
217 inline double parse<double>(const std::string& str, number_styles styles, const std::locale& locale) {return __parse_floating_point_number<double>(str, styles, locale);}
218
225 template<>
226 inline long double parse<long double>(const std::string& str, number_styles styles) {return __parse_floating_point_number<long double>(str, styles, std::locale());}
227
234 template<>
235 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);}
236
243 template<>
244 inline xtd::sbyte parse<xtd::sbyte>(const std::string& str) {return parse<xtd::sbyte>(str, number_styles::integer);}
245
252 template<>
253 inline char parse<char>(const std::string& str) {return parse<char>(str, number_styles::integer);}
254
261 template<>
262 inline unsigned char parse<unsigned char>(const std::string& str) {return parse<unsigned char>(str, number_styles::integer);}
263
270 template<>
271 inline short parse<short>(const std::string& str) {return parse<short>(str, number_styles::integer);}
272
279 template<>
280 inline unsigned short parse<unsigned short>(const std::string& str) {return parse<unsigned short>(str, number_styles::integer);}
281
288 template<>
289 inline int parse<int>(const std::string& str) {return parse<int>(str, number_styles::integer);}
290
297 template<>
298 inline unsigned int parse<unsigned int>(const std::string& str) {return parse<unsigned int>(str, number_styles::integer);}
299
306 template<>
307 inline long parse<long>(const std::string& str) {return parse<long>(str, number_styles::integer);}
308
315 template<>
316 inline unsigned long parse<unsigned long>(const std::string& str) {return parse<unsigned long>(str, number_styles::integer);}
317
324 template<>
325 inline long long parse<long long>(const std::string& str) {return parse<long long>(str, number_styles::integer);}
326
333 template<>
334 inline unsigned long long parse<unsigned long long>(const std::string& str) {return parse<unsigned long long>(str, number_styles::integer);}
335
342 template<>
343 inline float parse<float>(const std::string& str) {return parse<float>(str, number_styles::fixed_point);}
344
351 template<>
352 inline double parse<double>(const std::string& str) {return parse<double>(str, number_styles::fixed_point);}
353
360 template<>
361 inline long double parse<long double>(const std::string& str) {return parse<long double>(str, number_styles::fixed_point);}
362
369 template<>
370 inline bool parse<bool>(const std::string& str) {
371 std::string lower_str = str;
372 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))
373 lower_str.erase(0, 1);
374 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))
375 lower_str.erase(lower_str.size() - 1, 1);
376 for (auto& c : lower_str)
377 c = static_cast<char>(std::tolower(c));
378 if (lower_str != "true" && lower_str != "1" && lower_str != "false" && lower_str != "0") xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::format, "Invalid string format");
379 return lower_str == "true" || lower_str == "1";
380 }
381
388 template<class value_t>
389 inline value_t parse(const std::wstring& str) {xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::format, "Parse specialisation not found"); return {};}
390
397 template<class value_t>
398 inline value_t parse(const std::u16string& str) {xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::format, "Parse specialisation not found"); return {};}
399
406 template<class value_t>
407 inline value_t parse(const std::u32string& str) {xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::format, "Parse specialisation not found"); return {};}
408
415 template<class value_t>
416 inline bool try_parse(const std::basic_string<char>& str, value_t& value) noexcept {
417 try {
418 value = parse<value_t>(str);
419 return true;
420 } catch (...) {
421 return false;
422 }
423 }
430 template<class value_t>
431 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value) noexcept {
432 try {
433 value = parse<value_t>(str);
434 return true;
435 } catch (...) {
436 return false;
437 }
438 }
445 template<class value_t>
446 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value) noexcept {
447 try {
448 value = parse<value_t>(str);
449 return true;
450 } catch (...) {
451 return false;
452 }
453 }
454#if defined(__xtd__cpp_lib_char8_t)
461 template<class value_t>
462 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value) noexcept {
463 try {
464 value = parse<value_t>(str);
465 return true;
466 } catch (...) {
467 return false;
468 }
469 }
470#endif
477 template<class value_t>
478 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value) noexcept {
479 try {
480 value = parse<value_t>(str);
481 return true;
482 } catch (...) {
483 return false;
484 }
485 }
486
493 template<class value_t>
494 inline bool try_parse(const std::basic_string<char>& str, value_t& value, const std::locale& locale) noexcept {
495 try {
496 value = parse<value_t>(str, locale);
497 return true;
498 } catch (...) {
499 return false;
500 }
501 }
508 template<class value_t>
509 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value, const std::locale& locale) noexcept {
510 try {
511 value = parse<value_t>(str, locale);
512 return true;
513 } catch (...) {
514 return false;
515 }
516 }
523 template<class value_t>
524 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value, const std::locale& locale) noexcept {
525 try {
526 value = parse<value_t>(str, locale);
527 return true;
528 } catch (...) {
529 return false;
530 }
531 }
532#if defined(__xtd__cpp_lib_char8_t)
539 template<class value_t>
540 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value, const std::locale& locale) noexcept {
541 try {
542 value = parse<value_t>(str, locale);
543 return true;
544 } catch (...) {
545 return false;
546 }
547 }
548#endif
555 template<class value_t>
556 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value, const std::locale& locale) noexcept {
557 try {
558 value = parse<value_t>(str, locale);
559 return true;
560 } catch (...) {
561 return false;
562 }
563 }
564
571 template<class value_t, class char_t>
572 inline bool try_parse(const char_t* str, value_t& value) noexcept {
573 return try_parse(std::basic_string<char_t>(str), value);
574 }
575
582 template<class value_t, class char_t>
583 inline bool try_parse(const char_t* str, value_t& value, const std::locale& locale) noexcept {
584 return try_parse(std::basic_string<char_t>(str), value, locale);
585 }
586
593 template<class value_t>
594 inline bool try_parse(const std::basic_string<char>& str, value_t& value, number_styles style) noexcept {
595 try {
596 value = parse<value_t>(str, style);
597 return true;
598 } catch (...) {
599 return false;
600 }
601 }
608 template<class value_t>
609 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value, number_styles style) noexcept {
610 try {
611 value = parse<value_t>(str, style);
612 return true;
613 } catch (...) {
614 return false;
615 }
616 }
623 template<class value_t>
624 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value, number_styles style) noexcept {
625 try {
626 value = parse<value_t>(str, style);
627 return true;
628 } catch (...) {
629 return false;
630 }
631 }
632#if defined(__xtd__cpp_lib_char8_t)
639 template<class value_t>
640 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value, number_styles style) noexcept {
641 try {
642 value = parse<value_t>(str, style);
643 return true;
644 } catch (...) {
645 return false;
646 }
647 }
648#endif
655 template<class value_t>
656 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value, number_styles style) noexcept {
657 try {
658 value = parse<value_t>(str, style);
659 return true;
660 } catch (...) {
661 return false;
662 }
663 }
664
671 template<class value_t>
672 inline bool try_parse(const std::basic_string<char>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
673 try {
674 value = parse<value_t>(str, style, locale);
675 return true;
676 } catch (...) {
677 return false;
678 }
679 }
686 template<class value_t>
687 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
688 try {
689 value = parse<value_t>(str, style, locale);
690 return true;
691 } catch (...) {
692 return false;
693 }
694 }
701 template<class value_t>
702 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
703 try {
704 value = parse<value_t>(str, style, locale);
705 return true;
706 } catch (...) {
707 return false;
708 }
709 }
710#if defined(__xtd__cpp_lib_char8_t)
717 template<class value_t>
718 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
719 try {
720 value = parse<value_t>(str, style, locale);
721 return true;
722 } catch (...) {
723 return false;
724 }
725 }
726#endif
733 template<class value_t>
734 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
735 try {
736 value = parse<value_t>(str, style, locale);
737 return true;
738 } catch (...) {
739 return false;
740 }
741 }
742
749 template<class value_t, class char_t>
750 inline bool try_parse(const char_t* str, value_t& value, number_styles style) noexcept {
751 return try_parse(std::basic_string<char_t>(str), value, style);
752 }
753
760 template<class value_t, class char_t>
761 inline bool try_parse(const char_t* str, value_t& value, number_styles style, const std::locale& locale) noexcept {
762 return try_parse(std::basic_string<char_t>(str), value, style, locale);
763 }
764}
765
Contains parse methods.
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
@ format
The format is not valid.
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:208
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:100
bool parse< bool >(const std::string &str)
Convert a string into a type.
Definition parse.hpp:370
xtd::sbyte parse< xtd::sbyte >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:91
unsigned long parse< unsigned long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:163
long long parse< long long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:172
long double parse< long double >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:226
int parse< int >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:136
long parse< long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:154
unsigned char parse< unsigned char >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:109
std::string parse< std::string >(const std::string &str)
Convert a string into a type.
Definition parse.hpp:64
float parse< float >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:190
short parse< short >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:118
unsigned int parse< unsigned int >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:145
unsigned short parse< unsigned short >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:127
unsigned long long parse< unsigned long long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:181
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:416
@ 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.