from_chars_integral.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___CHARCONV_FROM_CHARS_INTEGRAL_H
  10. #define _LIBCPP___CHARCONV_FROM_CHARS_INTEGRAL_H
  11. #include <__algorithm/copy_n.h>
  12. #include <__charconv/from_chars_result.h>
  13. #include <__charconv/traits.h>
  14. #include <__config>
  15. #include <__memory/addressof.h>
  16. #include <__system_error/errc.h>
  17. #include <__type_traits/enable_if.h>
  18. #include <__type_traits/integral_constant.h>
  19. #include <__type_traits/is_integral.h>
  20. #include <__type_traits/is_unsigned.h>
  21. #include <__type_traits/make_unsigned.h>
  22. #include <limits>
  23. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  24. # pragma GCC system_header
  25. #endif
  26. _LIBCPP_PUSH_MACROS
  27. #include <__undef_macros>
  28. _LIBCPP_BEGIN_NAMESPACE_STD
  29. #if _LIBCPP_STD_VER >= 17
  30. from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete;
  31. template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
  32. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  33. __sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) {
  34. using __tl = numeric_limits<_Tp>;
  35. decltype(std::__to_unsigned_like(__value)) __x;
  36. bool __neg = (__first != __last && *__first == '-');
  37. auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
  38. switch (__r.ec) {
  39. case errc::invalid_argument:
  40. return {__first, __r.ec};
  41. case errc::result_out_of_range:
  42. return __r;
  43. default:
  44. break;
  45. }
  46. if (__neg) {
  47. if (__x <= std::__complement(std::__to_unsigned_like(__tl::min()))) {
  48. __x = std::__complement(__x);
  49. std::copy_n(std::addressof(__x), 1, std::addressof(__value));
  50. return __r;
  51. }
  52. } else {
  53. if (__x <= std::__to_unsigned_like(__tl::max())) {
  54. __value = __x;
  55. return __r;
  56. }
  57. }
  58. return {__r.ptr, errc::result_out_of_range};
  59. }
  60. template <typename _Tp>
  61. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool __in_pattern(_Tp __c) {
  62. return '0' <= __c && __c <= '9';
  63. }
  64. struct _LIBCPP_HIDDEN __in_pattern_result {
  65. bool __ok;
  66. int __val;
  67. explicit _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; }
  68. };
  69. template <typename _Tp>
  70. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI __in_pattern_result __in_pattern(_Tp __c, int __base) {
  71. if (__base <= 10)
  72. return {'0' <= __c && __c < '0' + __base, __c - '0'};
  73. else if (std::__in_pattern(__c))
  74. return {true, __c - '0'};
  75. else if ('a' <= __c && __c < 'a' + __base - 10)
  76. return {true, __c - 'a' + 10};
  77. else
  78. return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10};
  79. }
  80. template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
  81. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  82. __subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) {
  83. auto __find_non_zero = [](_It __firstit, _It __lastit) {
  84. for (; __firstit != __lastit; ++__firstit)
  85. if (*__firstit != '0')
  86. break;
  87. return __firstit;
  88. };
  89. auto __p = __find_non_zero(__first, __last);
  90. if (__p == __last || !std::__in_pattern(*__p, __args...)) {
  91. if (__p == __first)
  92. return {__first, errc::invalid_argument};
  93. else {
  94. __value = 0;
  95. return {__p, {}};
  96. }
  97. }
  98. auto __r = __f(__p, __last, __value, __args...);
  99. if (__r.ec == errc::result_out_of_range) {
  100. for (; __r.ptr != __last; ++__r.ptr) {
  101. if (!std::__in_pattern(*__r.ptr, __args...))
  102. break;
  103. }
  104. }
  105. return __r;
  106. }
  107. template <typename _Tp, __enable_if_t<is_unsigned<_Tp>::value, int> = 0>
  108. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  109. __from_chars_atoi(const char* __first, const char* __last, _Tp& __value) {
  110. using __tx = __itoa::__traits<_Tp>;
  111. using __output_type = typename __tx::type;
  112. return std::__subject_seq_combinator(
  113. __first, __last, __value, [](const char* __f, const char* __l, _Tp& __val) -> from_chars_result {
  114. __output_type __a, __b;
  115. auto __p = __tx::__read(__f, __l, __a, __b);
  116. if (__p == __l || !std::__in_pattern(*__p)) {
  117. __output_type __m = numeric_limits<_Tp>::max();
  118. if (__m >= __a && __m - __a >= __b) {
  119. __val = __a + __b;
  120. return {__p, {}};
  121. }
  122. }
  123. return {__p, errc::result_out_of_range};
  124. });
  125. }
  126. template <typename _Tp, __enable_if_t<is_signed<_Tp>::value, int> = 0>
  127. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  128. __from_chars_atoi(const char* __first, const char* __last, _Tp& __value) {
  129. using __t = decltype(std::__to_unsigned_like(__value));
  130. return std::__sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
  131. }
  132. /*
  133. // Code used to generate __from_chars_log2f_lut.
  134. #include <cmath>
  135. #include <format>
  136. #include <iostream>
  137. int main() {
  138. for (int i = 2; i <= 36; ++i)
  139. std::cout << std::format("{},\n", log2f(i));
  140. }
  141. */
  142. /// log2f table for bases [2, 36].
  143. inline constexpr float __from_chars_log2f_lut[35] = {
  144. 1, 1.5849625, 2, 2.321928, 2.5849626, 2.807355, 3, 3.169925, 3.321928,
  145. 3.4594316, 3.5849626, 3.7004397, 3.807355, 3.9068906, 4, 4.087463, 4.169925, 4.2479277,
  146. 4.321928, 4.3923173, 4.4594316, 4.523562, 4.5849624, 4.643856, 4.70044, 4.7548876, 4.807355,
  147. 4.857981, 4.9068904, 4.9541965, 5, 5.044394, 5.087463, 5.129283, 5.169925};
  148. template <typename _Tp, __enable_if_t<is_unsigned<_Tp>::value, int> = 0>
  149. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  150. __from_chars_integral(const char* __first, const char* __last, _Tp& __value, int __base) {
  151. if (__base == 10)
  152. return std::__from_chars_atoi(__first, __last, __value);
  153. return std::__subject_seq_combinator(
  154. __first,
  155. __last,
  156. __value,
  157. [](const char* __p, const char* __lastp, _Tp& __val, int __b) -> from_chars_result {
  158. using __tl = numeric_limits<_Tp>;
  159. // __base is always between 2 and 36 inclusive.
  160. auto __digits = __tl::digits / __from_chars_log2f_lut[__b - 2];
  161. _Tp __x = __in_pattern(*__p++, __b).__val, __y = 0;
  162. for (int __i = 1; __p != __lastp; ++__i, ++__p) {
  163. if (auto __c = __in_pattern(*__p, __b)) {
  164. if (__i < __digits - 1)
  165. __x = __x * __b + __c.__val;
  166. else {
  167. if (!__itoa::__mul_overflowed(__x, __b, __x))
  168. ++__p;
  169. __y = __c.__val;
  170. break;
  171. }
  172. } else
  173. break;
  174. }
  175. if (__p == __lastp || !__in_pattern(*__p, __b)) {
  176. if (__tl::max() - __x >= __y) {
  177. __val = __x + __y;
  178. return {__p, {}};
  179. }
  180. }
  181. return {__p, errc::result_out_of_range};
  182. },
  183. __base);
  184. }
  185. template <typename _Tp, __enable_if_t<is_signed<_Tp>::value, int> = 0>
  186. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  187. __from_chars_integral(const char* __first, const char* __last, _Tp& __value, int __base) {
  188. using __t = decltype(std::__to_unsigned_like(__value));
  189. return std::__sign_combinator(__first, __last, __value, __from_chars_integral<__t>, __base);
  190. }
  191. template <typename _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
  192. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  193. from_chars(const char* __first, const char* __last, _Tp& __value) {
  194. return std::__from_chars_atoi(__first, __last, __value);
  195. }
  196. template <typename _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
  197. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result
  198. from_chars(const char* __first, const char* __last, _Tp& __value, int __base) {
  199. _LIBCPP_ASSERT_UNCATEGORIZED(2 <= __base && __base <= 36, "base not in [2, 36]");
  200. return std::__from_chars_integral(__first, __last, __value, __base);
  201. }
  202. #endif // _LIBCPP_STD_VER >= 17
  203. _LIBCPP_END_NAMESPACE_STD
  204. _LIBCPP_POP_MACROS
  205. #endif // _LIBCPP___CHARCONV_FROM_CHARS_INTEGRAL_H