from_chars.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2022 Peter Dimov
  2. // Copyright 2023 Matt Borland
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. #ifndef BOOST_CHARCONV_FROM_CHARS_HPP_INCLUDED
  6. #define BOOST_CHARCONV_FROM_CHARS_HPP_INCLUDED
  7. #include <boost/charconv/detail/config.hpp>
  8. #include <boost/charconv/detail/from_chars_result.hpp>
  9. #include <boost/charconv/detail/from_chars_integer_impl.hpp>
  10. #include <boost/charconv/detail/bit_layouts.hpp>
  11. #include <boost/charconv/config.hpp>
  12. #include <boost/charconv/chars_format.hpp>
  13. #include <boost/core/detail/string_view.hpp>
  14. #include <system_error>
  15. namespace boost { namespace charconv {
  16. // integer overloads
  17. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, bool& value, int base = 10) noexcept = delete;
  18. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, char& value, int base = 10) noexcept
  19. {
  20. return detail::from_chars(first, last, value, base);
  21. }
  22. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, signed char& value, int base = 10) noexcept
  23. {
  24. return detail::from_chars(first, last, value, base);
  25. }
  26. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, unsigned char& value, int base = 10) noexcept
  27. {
  28. return detail::from_chars(first, last, value, base);
  29. }
  30. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, short& value, int base = 10) noexcept
  31. {
  32. return detail::from_chars(first, last, value, base);
  33. }
  34. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, unsigned short& value, int base = 10) noexcept
  35. {
  36. return detail::from_chars(first, last, value, base);
  37. }
  38. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, int& value, int base = 10) noexcept
  39. {
  40. return detail::from_chars(first, last, value, base);
  41. }
  42. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, unsigned int& value, int base = 10) noexcept
  43. {
  44. return detail::from_chars(first, last, value, base);
  45. }
  46. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, long& value, int base = 10) noexcept
  47. {
  48. return detail::from_chars(first, last, value, base);
  49. }
  50. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, unsigned long& value, int base = 10) noexcept
  51. {
  52. return detail::from_chars(first, last, value, base);
  53. }
  54. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, long long& value, int base = 10) noexcept
  55. {
  56. return detail::from_chars(first, last, value, base);
  57. }
  58. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, unsigned long long& value, int base = 10) noexcept
  59. {
  60. return detail::from_chars(first, last, value, base);
  61. }
  62. #ifdef BOOST_CHARCONV_HAS_INT128
  63. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, boost::int128_type& value, int base = 10) noexcept
  64. {
  65. return detail::from_chars128(first, last, value, base);
  66. }
  67. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(const char* first, const char* last, boost::uint128_type& value, int base = 10) noexcept
  68. {
  69. return detail::from_chars128(first, last, value, base);
  70. }
  71. #endif
  72. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, bool& value, int base = 10) noexcept = delete;
  73. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, char& value, int base = 10) noexcept
  74. {
  75. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  76. }
  77. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, signed char& value, int base = 10) noexcept
  78. {
  79. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  80. }
  81. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, unsigned char& value, int base = 10) noexcept
  82. {
  83. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  84. }
  85. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, short& value, int base = 10) noexcept
  86. {
  87. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  88. }
  89. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, unsigned short& value, int base = 10) noexcept
  90. {
  91. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  92. }
  93. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, int& value, int base = 10) noexcept
  94. {
  95. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  96. }
  97. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, unsigned int& value, int base = 10) noexcept
  98. {
  99. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  100. }
  101. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, long& value, int base = 10) noexcept
  102. {
  103. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  104. }
  105. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, unsigned long& value, int base = 10) noexcept
  106. {
  107. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  108. }
  109. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, long long& value, int base = 10) noexcept
  110. {
  111. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  112. }
  113. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, unsigned long long& value, int base = 10) noexcept
  114. {
  115. return detail::from_chars(sv.data(), sv.data() + sv.size(), value, base);
  116. }
  117. #ifdef BOOST_CHARCONV_HAS_INT128
  118. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, boost::int128_type& value, int base = 10) noexcept
  119. {
  120. return detail::from_chars128(sv.data(), sv.data() + sv.size(), value, base);
  121. }
  122. BOOST_CHARCONV_GCC5_CONSTEXPR from_chars_result from_chars(boost::core::string_view sv, boost::uint128_type& value, int base = 10) noexcept
  123. {
  124. return detail::from_chars128(sv.data(), sv.data() + sv.size(), value, base);
  125. }
  126. #endif
  127. //----------------------------------------------------------------------------------------------------------------------
  128. // Floating Point
  129. //----------------------------------------------------------------------------------------------------------------------
  130. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(const char* first, const char* last, float& value, chars_format fmt = chars_format::general) noexcept;
  131. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(const char* first, const char* last, double& value, chars_format fmt = chars_format::general) noexcept;
  132. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(const char* first, const char* last, long double& value, chars_format fmt = chars_format::general) noexcept;
  133. #ifdef BOOST_CHARCONV_HAS_FLOAT128
  134. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(const char* first, const char* last, __float128& value, chars_format fmt = chars_format::general) noexcept;
  135. #endif
  136. // <stdfloat> types
  137. #ifdef BOOST_CHARCONV_HAS_FLOAT16
  138. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(const char* first, const char* last, std::float16_t& value, chars_format fmt = chars_format::general) noexcept;
  139. #endif
  140. #ifdef BOOST_CHARCONV_HAS_FLOAT32
  141. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(const char* first, const char* last, std::float32_t& value, chars_format fmt = chars_format::general) noexcept;
  142. #endif
  143. #ifdef BOOST_CHARCONV_HAS_FLOAT64
  144. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(const char* first, const char* last, std::float64_t& value, chars_format fmt = chars_format::general) noexcept;
  145. #endif
  146. #if defined(BOOST_CHARCONV_HAS_STDFLOAT128) && defined(BOOST_CHARCONV_HAS_FLOAT128)
  147. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(const char* first, const char* last, std::float128_t& value, chars_format fmt = chars_format::general) noexcept;
  148. #endif
  149. #ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
  150. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(const char* first, const char* last, std::bfloat16_t& value, chars_format fmt = chars_format::general) noexcept;
  151. #endif
  152. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(boost::core::string_view sv, float& value, chars_format fmt = chars_format::general) noexcept;
  153. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(boost::core::string_view sv, double& value, chars_format fmt = chars_format::general) noexcept;
  154. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(boost::core::string_view sv, long double& value, chars_format fmt = chars_format::general) noexcept;
  155. #ifdef BOOST_CHARCONV_HAS_FLOAT128
  156. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(boost::core::string_view sv, __float128& value, chars_format fmt = chars_format::general) noexcept;
  157. #endif
  158. // <stdfloat> types
  159. #ifdef BOOST_CHARCONV_HAS_FLOAT16
  160. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(boost::core::string_view sv, std::float16_t& value, chars_format fmt = chars_format::general) noexcept;
  161. #endif
  162. #ifdef BOOST_CHARCONV_HAS_FLOAT32
  163. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(boost::core::string_view sv, std::float32_t& value, chars_format fmt = chars_format::general) noexcept;
  164. #endif
  165. #ifdef BOOST_CHARCONV_HAS_FLOAT64
  166. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(boost::core::string_view sv, std::float64_t& value, chars_format fmt = chars_format::general) noexcept;
  167. #endif
  168. #if defined(BOOST_CHARCONV_HAS_STDFLOAT128) && defined(BOOST_CHARCONV_HAS_FLOAT128)
  169. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(boost::core::string_view sv, std::float128_t& value, chars_format fmt = chars_format::general) noexcept;
  170. #endif
  171. #ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
  172. BOOST_CHARCONV_DECL from_chars_result from_chars_erange(boost::core::string_view sv, std::bfloat16_t& value, chars_format fmt = chars_format::general) noexcept;
  173. #endif
  174. // The following adhere to the standard library definition with std::errc::result_out_of_range
  175. // Returns value unmodified
  176. // See: https://github.com/cppalliance/charconv/issues/110
  177. BOOST_CHARCONV_DECL from_chars_result from_chars(const char* first, const char* last, float& value, chars_format fmt = chars_format::general) noexcept;
  178. BOOST_CHARCONV_DECL from_chars_result from_chars(const char* first, const char* last, double& value, chars_format fmt = chars_format::general) noexcept;
  179. BOOST_CHARCONV_DECL from_chars_result from_chars(const char* first, const char* last, long double& value, chars_format fmt = chars_format::general) noexcept;
  180. #ifdef BOOST_CHARCONV_HAS_FLOAT128
  181. BOOST_CHARCONV_DECL from_chars_result from_chars(const char* first, const char* last, __float128& value, chars_format fmt = chars_format::general) noexcept;
  182. #endif
  183. #ifdef BOOST_CHARCONV_HAS_FLOAT16
  184. BOOST_CHARCONV_DECL from_chars_result from_chars(const char* first, const char* last, std::float16_t& value, chars_format fmt = chars_format::general) noexcept;
  185. #endif
  186. #ifdef BOOST_CHARCONV_HAS_FLOAT32
  187. BOOST_CHARCONV_DECL from_chars_result from_chars(const char* first, const char* last, std::float32_t& value, chars_format fmt = chars_format::general) noexcept;
  188. #endif
  189. #ifdef BOOST_CHARCONV_HAS_FLOAT64
  190. BOOST_CHARCONV_DECL from_chars_result from_chars(const char* first, const char* last, std::float64_t& value, chars_format fmt = chars_format::general) noexcept;
  191. #endif
  192. #if defined(BOOST_CHARCONV_HAS_STDFLOAT128) && defined(BOOST_CHARCONV_HAS_FLOAT128)
  193. BOOST_CHARCONV_DECL from_chars_result from_chars(const char* first, const char* last, std::float128_t& value, chars_format fmt = chars_format::general) noexcept;
  194. #endif
  195. #ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
  196. BOOST_CHARCONV_DECL from_chars_result from_chars(const char* first, const char* last, std::bfloat16_t& value, chars_format fmt = chars_format::general) noexcept;
  197. #endif
  198. BOOST_CHARCONV_DECL from_chars_result from_chars(boost::core::string_view sv, float& value, chars_format fmt = chars_format::general) noexcept;
  199. BOOST_CHARCONV_DECL from_chars_result from_chars(boost::core::string_view sv, double& value, chars_format fmt = chars_format::general) noexcept;
  200. BOOST_CHARCONV_DECL from_chars_result from_chars(boost::core::string_view sv, long double& value, chars_format fmt = chars_format::general) noexcept;
  201. #ifdef BOOST_CHARCONV_HAS_FLOAT128
  202. BOOST_CHARCONV_DECL from_chars_result from_chars(boost::core::string_view sv, __float128& value, chars_format fmt = chars_format::general) noexcept;
  203. #endif
  204. #ifdef BOOST_CHARCONV_HAS_FLOAT16
  205. BOOST_CHARCONV_DECL from_chars_result from_chars(boost::core::string_view sv, std::float16_t& value, chars_format fmt = chars_format::general) noexcept;
  206. #endif
  207. #ifdef BOOST_CHARCONV_HAS_FLOAT32
  208. BOOST_CHARCONV_DECL from_chars_result from_chars(boost::core::string_view sv, std::float32_t& value, chars_format fmt = chars_format::general) noexcept;
  209. #endif
  210. #ifdef BOOST_CHARCONV_HAS_FLOAT64
  211. BOOST_CHARCONV_DECL from_chars_result from_chars(boost::core::string_view sv, std::float64_t& value, chars_format fmt = chars_format::general) noexcept;
  212. #endif
  213. #if defined(BOOST_CHARCONV_HAS_STDFLOAT128) && defined(BOOST_CHARCONV_HAS_FLOAT128)
  214. BOOST_CHARCONV_DECL from_chars_result from_chars(boost::core::string_view sv, std::float128_t& value, chars_format fmt = chars_format::general) noexcept;
  215. #endif
  216. #ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
  217. BOOST_CHARCONV_DECL from_chars_result from_chars(boost::core::string_view sv, std::bfloat16_t& value, chars_format fmt = chars_format::general) noexcept;
  218. #endif
  219. } // namespace charconv
  220. } // namespace boost
  221. #endif // #ifndef BOOST_CHARCONV_FROM_CHARS_HPP_INCLUDED