xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
parse.h
Go to the documentation of this file.
1 #pragma once
5 #include <string>
7 #define __XTD_CORE_INTERNAL__
8 #include "internal/__parse.h"
9 #undef __XTD_CORE_INTERNAL__
11 #include "number_styles.h"
12 
14 namespace xtd {
21  template<typename value_t>
22  inline value_t parse(const std::string& str) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
23 
30  template<typename value_t>
31  inline value_t parse(const std::string& str, const std::string& fmt) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
32 
39  template<>
40  inline std::string parse<std::string>(const std::string& str) {return str;}
41 
48  template<typename value_t>
49  inline value_t parse(const std::string& str, number_styles) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
50 
57  template<>
58  inline int8_t parse<int8_t>(const std::string& str, number_styles styles) {return __parse_number<int8_t>(str, styles);}
59 
66  template<>
67  inline char parse<char>(const std::string& str, number_styles styles) {return __parse_number<char>(str, styles);}
68 
75  template<>
76  inline unsigned char parse<unsigned char>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned char>(str, styles);}
77 
84  template<>
85  inline short parse<short>(const std::string& str, number_styles styles) {return __parse_number<short>(str, styles);}
86 
93  template<>
94  inline unsigned short parse<unsigned short>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned short>(str, styles);}
95 
102  template<>
103  inline int parse<int>(const std::string& str, number_styles styles) {return __parse_number<int>(str, styles);}
104 
111  template<>
112  inline unsigned int parse<unsigned int>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned int>(str, styles);}
113 
120  template<>
121  inline long parse<long>(const std::string& str, number_styles styles) {return __parse_number<long>(str, styles);}
122 
129  template<>
130  inline unsigned long parse<unsigned long>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned long>(str, styles);}
131 
138  template<>
139  inline long long parse<long long>(const std::string& str, number_styles styles) {return __parse_number<long long>(str, styles);}
140 
147  template<>
148  inline unsigned long long parse<unsigned long long>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned long long>(str, styles);}
149 
156  template<>
157  inline float parse<float>(const std::string& str, number_styles styles) {return __parse_floating_point_number<float>(str, styles);}
158 
165  template<>
166  inline double parse<double>(const std::string& str, number_styles styles) {return __parse_floating_point_number<double>(str, styles);}
167 
174  template<>
175  inline long double parse<long double>(const std::string& str, number_styles styles) {return __parse_floating_point_number<long double>(str, styles);}
176 
183  template<>
184  inline int8_t parse<int8_t>(const std::string& str) {return parse<int8_t>(str, number_styles::integer);}
185 
192  template<>
193  inline char parse<char>(const std::string& str) {return parse<char>(str, number_styles::integer);}
194 
201  template<>
202  inline unsigned char parse<unsigned char>(const std::string& str) {return parse<unsigned char>(str, number_styles::integer);}
203 
210  template<>
211  inline short parse<short>(const std::string& str) {return parse<short>(str, number_styles::integer);}
212 
219  template<>
220  inline unsigned short parse<unsigned short>(const std::string& str) {return parse<unsigned short>(str, number_styles::integer);}
221 
228  template<>
229  inline int parse<int>(const std::string& str) {return parse<int>(str, number_styles::integer);}
230 
237  template<>
238  inline unsigned int parse<unsigned int>(const std::string& str) {return parse<unsigned int>(str, number_styles::integer);}
239 
246  template<>
247  inline long parse<long>(const std::string& str) {return parse<long>(str, number_styles::integer);}
248 
255  template<>
256  inline unsigned long parse<unsigned long>(const std::string& str) {return parse<unsigned long>(str, number_styles::integer);}
257 
264  template<>
265  inline long long parse<long long>(const std::string& str) {return parse<long long>(str, number_styles::integer);}
266 
273  template<>
274  inline unsigned long long parse<unsigned long long>(const std::string& str) {return parse<unsigned long long>(str, number_styles::integer);}
275 
282  template<>
283  inline float parse<float>(const std::string& str) {return parse<float>(str, number_styles::fixed_point);}
284 
291  template<>
292  inline double parse<double>(const std::string& str) {return parse<double>(str, number_styles::fixed_point);}
293 
300  template<>
301  inline long double parse<long double>(const std::string& str) {return parse<long double>(str, number_styles::fixed_point);}
302 
309  template<>
310  inline bool parse<bool>(const std::string& str) {
311  std::string lower_str = str;
312  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))
313  lower_str.erase(0, 1);
314  for (auto& c : lower_str)
315  c = static_cast<char>(std::tolower(c));
316  if (lower_str != "true" && lower_str != "1" && lower_str != "false" && lower_str != "0") __throw_parse_format_exception("Invalid string format");
317  return lower_str == "true" || lower_str == "1";
318  }
319 
326  template<typename value_t>
327  inline value_t parse(const std::wstring& str) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
328 
335  template<typename value_t>
336  inline value_t parse(const std::u16string& str) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
337 
344  template<typename value_t>
345  inline value_t parse(const std::u32string& str) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
346 
353  template<typename value_t, typename char_t>
354  inline bool try_parse(const std::basic_string<char_t>& str, value_t& value) {
355  try {
356  value = parse<value_t>(str);
357  return true;
358  } catch(...) {
359  return false;
360  }
361  }
362 
369  template<typename value_t, typename char_t>
370  inline bool try_parse(const char_t* str, value_t& value) {
371  return try_parse(std::basic_string<char_t>(str), value);
372  }
373 }
374 
double parse< double >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:166
number_styles
Determines the styles permitted in numeric string arguments that are passed to the xtd::parse and xtd...
Definition: number_styles.h:15
int8_t parse< int8_t >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:58
char parse< char >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:67
bool parse< bool >(const std::string &str)
Convert a type into a string.
Definition: parse.h:310
unsigned long parse< unsigned long >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:130
long long parse< long long >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:139
long double parse< long double >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:175
bool try_parse(const std::basic_string< char_t > &str, value_t &value)
Convert a type into a string.
Definition: parse.h:354
int parse< int >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:103
long parse< long >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:121
unsigned char parse< unsigned char >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:76
float parse< float >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:157
short parse< short >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:85
unsigned int parse< unsigned int >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:112
unsigned short parse< unsigned short >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:94
unsigned long long parse< unsigned long long >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:148
value_t parse(const std::string &str)
Convert a type into a string.
Definition: parse.h:22
@ 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: system_report.h:17
Contains xtd::number_styles enum class.