xtd 0.2.0
parse.h
Go to the documentation of this file.
1 #pragma once
5 #define __XTD_CORE_INTERNAL__
7 #include "internal/__parse.h"
8 #undef __XTD_CORE_INTERNAL__
9 #include "number_styles.h"
11 #include "types.h"
12 #include <string>
13 
15 namespace xtd {
22  template<typename value_t>
23  inline value_t parse(const std::string& str) {
24  if (std::is_enum<value_t>::value) return __parse_enum<value_t>(str);
25  __throw_parse_format_exception("Parse specialisation not found");
26  return {};
27  }
28 
35  template<typename value_t>
36  inline value_t parse(const std::string& str, const std::locale& locale) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
37 
44  template<typename value_t>
45  inline value_t parse(const std::string& str, const std::string& fmt) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
46 
53  template<>
54  inline std::string parse<std::string>(const std::string& str) {return str;}
55 
62  template<typename value_t>
63  inline value_t parse(const std::string& str, number_styles) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
64 
71  template<typename value_t>
72  inline value_t parse(const std::string& str, number_styles, const std::locale& locale) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
73 
80  template<>
81  inline xtd::sbyte parse<xtd::sbyte>(const std::string& str, number_styles styles) {return __parse_number<xtd::sbyte>(str, styles);}
82 
89  template<>
90  inline char parse<char>(const std::string& str, number_styles styles) {return __parse_number<char>(str, styles);}
91 
98  template<>
99  inline unsigned char parse<unsigned char>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned char>(str, styles);}
100 
107  template<>
108  inline short parse<short>(const std::string& str, number_styles styles) {return __parse_number<short>(str, styles);}
109 
116  template<>
117  inline unsigned short parse<unsigned short>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned short>(str, styles);}
118 
125  template<>
126  inline int parse<int>(const std::string& str, number_styles styles) {return __parse_number<int>(str, styles);}
127 
134  template<>
135  inline unsigned int parse<unsigned int>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned int>(str, styles);}
136 
143  template<>
144  inline long parse<long>(const std::string& str, number_styles styles) {return __parse_number<long>(str, styles);}
145 
152  template<>
153  inline unsigned long parse<unsigned long>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned long>(str, styles);}
154 
161  template<>
162  inline long long parse<long long>(const std::string& str, number_styles styles) {return __parse_number<long long>(str, styles);}
163 
170  template<>
171  inline unsigned long long parse<unsigned long long>(const std::string& str, number_styles styles) {return __parse_unsigned_number<unsigned long long>(str, styles);}
172 
179  template<>
180  inline float parse<float>(const std::string& str, number_styles styles) {return __parse_floating_point_number<float>(str, styles, std::locale());}
181 
188  template<>
189  inline float parse<float>(const std::string& str, number_styles styles, const std::locale& locale) {return __parse_floating_point_number<float>(str, styles, locale);}
190 
197  template<>
198  inline double parse<double>(const std::string& str, number_styles styles) {return __parse_floating_point_number<double>(str, styles, std::locale());}
199 
206  template<>
207  inline double parse<double>(const std::string& str, number_styles styles, const std::locale& locale) {return __parse_floating_point_number<double>(str, styles, locale);}
208 
215  template<>
216  inline long double parse<long double>(const std::string& str, number_styles styles) {return __parse_floating_point_number<long double>(str, styles, std::locale());}
217 
224  template<>
225  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);}
226 
233  template<>
234  inline xtd::sbyte parse<xtd::sbyte>(const std::string& str) {return parse<xtd::sbyte>(str, number_styles::integer);}
235 
242  template<>
243  inline char parse<char>(const std::string& str) {return parse<char>(str, number_styles::integer);}
244 
251  template<>
252  inline unsigned char parse<unsigned char>(const std::string& str) {return parse<unsigned char>(str, number_styles::integer);}
253 
260  template<>
261  inline short parse<short>(const std::string& str) {return parse<short>(str, number_styles::integer);}
262 
269  template<>
270  inline unsigned short parse<unsigned short>(const std::string& str) {return parse<unsigned short>(str, number_styles::integer);}
271 
278  template<>
279  inline int parse<int>(const std::string& str) {return parse<int>(str, number_styles::integer);}
280 
287  template<>
288  inline unsigned int parse<unsigned int>(const std::string& str) {return parse<unsigned int>(str, number_styles::integer);}
289 
296  template<>
297  inline long parse<long>(const std::string& str) {return parse<long>(str, number_styles::integer);}
298 
305  template<>
306  inline unsigned long parse<unsigned long>(const std::string& str) {return parse<unsigned long>(str, number_styles::integer);}
307 
314  template<>
315  inline long long parse<long long>(const std::string& str) {return parse<long long>(str, number_styles::integer);}
316 
323  template<>
324  inline unsigned long long parse<unsigned long long>(const std::string& str) {return parse<unsigned long long>(str, number_styles::integer);}
325 
332  template<>
333  inline float parse<float>(const std::string& str) {return parse<float>(str, number_styles::fixed_point);}
334 
341  template<>
342  inline double parse<double>(const std::string& str) {return parse<double>(str, number_styles::fixed_point);}
343 
350  template<>
351  inline long double parse<long double>(const std::string& str) {return parse<long double>(str, number_styles::fixed_point);}
352 
359  template<>
360  inline bool parse<bool>(const std::string& str) {
361  std::string lower_str = str;
362  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))
363  lower_str.erase(0, 1);
364  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))
365  lower_str.erase(lower_str.size() - 1, 1);
366  for (auto& c : lower_str)
367  c = static_cast<char>(std::tolower(c));
368  if (lower_str != "true" && lower_str != "1" && lower_str != "false" && lower_str != "0") __throw_parse_format_exception("Invalid string format");
369  return lower_str == "true" || lower_str == "1";
370  }
371 
378  template<typename value_t>
379  inline value_t parse(const std::wstring& str) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
380 
387  template<typename value_t>
388  inline value_t parse(const std::u16string& str) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
389 
396  template<typename value_t>
397  inline value_t parse(const std::u32string& str) {__throw_parse_format_exception("Parse specialisation not found"); return {};}
398 
405  template<typename value_t, typename char_t>
406  inline bool try_parse(const std::basic_string<char_t>& str, value_t& value) noexcept {
407  try {
408  value = parse<value_t>(str);
409  return true;
410  } catch (...) {
411  return false;
412  }
413  }
414 
421  template<typename value_t, typename char_t>
422  inline bool try_parse(const std::basic_string<char_t>& str, value_t& value, const std::locale& locale) noexcept {
423  try {
424  value = parse<value_t>(str, locale);
425  return true;
426  } catch (...) {
427  return false;
428  }
429  }
430 
437  template<typename value_t, typename char_t>
438  inline bool try_parse(const char_t* str, value_t& value) noexcept {
439  return try_parse(std::basic_string<char_t>(str), value);
440  }
441 
448  template<typename value_t, typename char_t>
449  inline bool try_parse(const char_t* str, value_t& value, const std::locale& locale) noexcept {
450  return try_parse(std::basic_string<char_t>(str), value, locale);
451  }
452 
459  template<typename value_t, typename char_t>
460  inline bool try_parse(const std::basic_string<char_t>& str, value_t& value, number_styles style) noexcept {
461  try {
462  value = parse<value_t>(str, style);
463  return true;
464  } catch (...) {
465  return false;
466  }
467  }
468 
475  template<typename value_t, typename char_t>
476  inline bool try_parse(const std::basic_string<char_t>& str, value_t& value, number_styles style, const std::locale& locale) noexcept {
477  try {
478  value = parse<value_t>(str, style, locale);
479  return true;
480  } catch (...) {
481  return false;
482  }
483  }
484 
491  template<typename value_t, typename char_t>
492  inline bool try_parse(const char_t* str, value_t& value, number_styles style) noexcept {
493  return try_parse(std::basic_string<char_t>(str), value, style);
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, const std::locale& locale) noexcept {
504  return try_parse(std::basic_string<char_t>(str), value, style, locale);
505  }
506 }
507 
unsigned char parse< unsigned char >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:99
int parse< int >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:126
Contains xtd fundamental types.
int_least8_t sbyte
Represents a 8-bit signed integer.
Definition: types.h:173
value_t parse(const std::string &str)
Convert a string into a type.
Definition: parse.h:23
char parse< char >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:90
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
long long parse< long long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:162
Indicates that the allow_leading_white, allow_trailing_white, and allow_leading_sign styles are used...
long double parse< long double >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:216
double parse< double >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:198
long parse< long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:144
unsigned long long parse< unsigned long long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:171
Indicates that the allow_leading_white, allow_trailing_white, allow_leading_sign, allow_decimal_point...
bool parse< bool >(const std::string &str)
Convert a string into a type.
Definition: parse.h:360
unsigned long parse< unsigned long >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:153
float parse< float >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:180
unsigned int parse< unsigned int >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:135
bool try_parse(const std::basic_string< char_t > &str, value_t &value) noexcept
Convert a string into a type.
Definition: parse.h:406
unsigned short parse< unsigned short >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:117
Contains xtd::number_styles enum class.
number_styles
Determines the styles permitted in numeric string arguments that are passed to the xtd::parse and xtd...
Definition: number_styles.h:16
short parse< short >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition: parse.h:108