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 }
424
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 }
439
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
460 template<class value_t>
461 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value) noexcept {
462 try {
463 value = parse<value_t>(str);
464 return true;
465 } catch (...) {
466 return false;
467 }
468 }
469
475 template<class value_t>
476 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value) noexcept {
477 try {
478 value = parse<value_t>(str);
479 return true;
480 } catch (...) {
481 return false;
482 }
483 }
484
491 template<class value_t>
492 inline bool try_parse(const std::basic_string<char>& str, value_t& value, const std::locale& locale) noexcept {
493 try {
494 value = parse<value_t>(str, locale);
495 return true;
496 } catch (...) {
497 return false;
498 }
499 }
500
506 template<class value_t>
507 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value, const std::locale& locale) noexcept {
508 try {
509 value = parse<value_t>(str, locale);
510 return true;
511 } catch (...) {
512 return false;
513 }
514 }
515
521 template<class value_t>
522 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value, const std::locale& locale) noexcept {
523 try {
524 value = parse<value_t>(str, locale);
525 return true;
526 } catch (...) {
527 return false;
528 }
529 }
530
536 template<class value_t>
537 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value, const std::locale& locale) noexcept {
538 try {
539 value = parse<value_t>(str, locale);
540 return true;
541 } catch (...) {
542 return false;
543 }
544 }
545
551 template<class value_t>
552 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value, const std::locale& locale) noexcept {
553 try {
554 value = parse<value_t>(str, locale);
555 return true;
556 } catch (...) {
557 return false;
558 }
559 }
560
567 template<class value_t, class char_t>
568 inline bool try_parse(const char_t* str, value_t& value) noexcept {
569 return try_parse(std::basic_string<char_t>(str), value);
570 }
571
578 template<class value_t, class char_t>
579 inline bool try_parse(const char_t* str, value_t& value, const std::locale& locale) noexcept {
580 return try_parse(std::basic_string<char_t>(str), value, locale);
581 }
582
589 template<class value_t>
590 inline bool try_parse(const std::basic_string<char>& str, value_t& value, number_styles style) noexcept {
591 try {
592 value = parse<value_t>(str, style);
593 return true;
594 } catch (...) {
595 return false;
596 }
597 }
598
604 template<class value_t>
605 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value, number_styles style) noexcept {
606 try {
607 value = parse<value_t>(str, style);
608 return true;
609 } catch (...) {
610 return false;
611 }
612 }
613
619 template<class value_t>
620 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value, number_styles style) noexcept {
621 try {
622 value = parse<value_t>(str, style);
623 return true;
624 } catch (...) {
625 return false;
626 }
627 }
628
634 template<class value_t>
635 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value, number_styles style) noexcept {
636 try {
637 value = parse<value_t>(str, style);
638 return true;
639 } catch (...) {
640 return false;
641 }
642 }
643
649 template<class value_t>
650 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value, number_styles style) noexcept {
651 try {
652 value = parse<value_t>(str, style);
653 return true;
654 } catch (...) {
655 return false;
656 }
657 }
658
665 template<class value_t>
666 inline bool try_parse(const std::basic_string<char>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
667 try {
668 value = parse<value_t>(str, style, locale);
669 return true;
670 } catch (...) {
671 return false;
672 }
673 }
674
680 template<class value_t>
681 inline bool try_parse(const std::basic_string<xtd::char16>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
682 try {
683 value = parse<value_t>(str, style, locale);
684 return true;
685 } catch (...) {
686 return false;
687 }
688 }
689
695 template<class value_t>
696 inline bool try_parse(const std::basic_string<xtd::char32>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
697 try {
698 value = parse<value_t>(str, style, locale);
699 return true;
700 } catch (...) {
701 return false;
702 }
703 }
704
710 template<class value_t>
711 inline bool try_parse(const std::basic_string<xtd::char8>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
712 try {
713 value = parse<value_t>(str, style, locale);
714 return true;
715 } catch (...) {
716 return false;
717 }
718 }
719
725 template<class value_t>
726 inline bool try_parse(const std::basic_string<xtd::wchar>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
727 try {
728 value = parse<value_t>(str, style, locale);
729 return true;
730 } catch (...) {
731 return false;
732 }
733 }
734
741 template<class value_t, class char_t>
742 inline bool try_parse(const char_t* str, value_t& value, number_styles style) noexcept {
743 return try_parse(std::basic_string<char_t>(str), value, style);
744 }
745
752 template<class value_t, class char_t>
753 inline bool try_parse(const char_t* str, value_t& value, number_styles style, const std::locale& locale) noexcept {
754 return try_parse(std::basic_string<char_t>(str), value, style, locale);
755 }
756}
757
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.
Definition exception_case.hpp:49
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....
Definition number_styles.hpp:44
@ fixed_point
Indicates that the allow_leading_white, allow_trailing_white, allow_leading_sign, allow_decimal_point...
Definition number_styles.hpp:48
@ c
The C key.
Definition console_key.hpp:92
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::number_styles enum class.
Contains xtd fundamental types.