Home | History | Annotate | Line # | Download | only in std
      1      1.1  mrg // Primitive numeric conversions (to_chars and from_chars) -*- C++ -*-
      2      1.1  mrg 
      3  1.1.1.4  mrg // Copyright (C) 2017-2022 Free Software Foundation, Inc.
      4      1.1  mrg //
      5      1.1  mrg // This file is part of the GNU ISO C++ Library.  This library is free
      6      1.1  mrg // software; you can redistribute it and/or modify it under the
      7      1.1  mrg // terms of the GNU General Public License as published by the
      8      1.1  mrg // Free Software Foundation; either version 3, or (at your option)
      9      1.1  mrg // any later version.
     10      1.1  mrg 
     11      1.1  mrg // This library is distributed in the hope that it will be useful,
     12      1.1  mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13      1.1  mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14      1.1  mrg // GNU General Public License for more details.
     15      1.1  mrg 
     16      1.1  mrg // Under Section 7 of GPL version 3, you are granted additional
     17      1.1  mrg // permissions described in the GCC Runtime Library Exception, version
     18      1.1  mrg // 3.1, as published by the Free Software Foundation.
     19      1.1  mrg 
     20      1.1  mrg // You should have received a copy of the GNU General Public License and
     21      1.1  mrg // a copy of the GCC Runtime Library Exception along with this program;
     22      1.1  mrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23      1.1  mrg // <http://www.gnu.org/licenses/>.
     24      1.1  mrg 
     25      1.1  mrg /** @file include/charconv
     26      1.1  mrg  *  This is a Standard C++ Library header.
     27      1.1  mrg  */
     28      1.1  mrg 
     29      1.1  mrg #ifndef _GLIBCXX_CHARCONV
     30      1.1  mrg #define _GLIBCXX_CHARCONV 1
     31      1.1  mrg 
     32      1.1  mrg #pragma GCC system_header
     33      1.1  mrg 
     34  1.1.1.3  mrg // As an extension we support <charconv> in C++14, but this header should not
     35  1.1.1.3  mrg // be included by any other library headers in C++14 mode. This ensures that
     36  1.1.1.3  mrg // the names defined in this header are not added to namespace std unless a
     37  1.1.1.3  mrg // user explicitly includes <charconv> in C++14 code.
     38      1.1  mrg #if __cplusplus >= 201402L
     39      1.1  mrg 
     40      1.1  mrg #include <type_traits>
     41  1.1.1.3  mrg #include <bit>			// for __bit_width
     42  1.1.1.3  mrg #include <bits/charconv.h>	// for __to_chars_len, __to_chars_10_impl
     43      1.1  mrg #include <bits/error_constants.h> // for std::errc
     44  1.1.1.3  mrg #include <ext/numeric_traits.h>
     45  1.1.1.3  mrg 
     46  1.1.1.4  mrg #if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \
     47  1.1.1.4  mrg     && __SIZE_WIDTH__ >= 32
     48  1.1.1.4  mrg # define __cpp_lib_to_chars 201611L
     49  1.1.1.4  mrg #endif
     50      1.1  mrg 
     51      1.1  mrg namespace std _GLIBCXX_VISIBILITY(default)
     52      1.1  mrg {
     53      1.1  mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
     54      1.1  mrg 
     55      1.1  mrg   /// Result type of std::to_chars
     56      1.1  mrg   struct to_chars_result
     57      1.1  mrg   {
     58      1.1  mrg     char* ptr;
     59      1.1  mrg     errc ec;
     60  1.1.1.3  mrg 
     61  1.1.1.3  mrg #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
     62  1.1.1.3  mrg     friend bool
     63  1.1.1.3  mrg     operator==(const to_chars_result&, const to_chars_result&) = default;
     64  1.1.1.3  mrg #endif
     65      1.1  mrg   };
     66      1.1  mrg 
     67      1.1  mrg   /// Result type of std::from_chars
     68      1.1  mrg   struct from_chars_result
     69      1.1  mrg   {
     70      1.1  mrg     const char* ptr;
     71      1.1  mrg     errc ec;
     72  1.1.1.3  mrg 
     73  1.1.1.3  mrg #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
     74  1.1.1.3  mrg     friend bool
     75  1.1.1.3  mrg     operator==(const from_chars_result&, const from_chars_result&) = default;
     76  1.1.1.3  mrg #endif
     77      1.1  mrg   };
     78      1.1  mrg 
     79      1.1  mrg namespace __detail
     80      1.1  mrg {
     81      1.1  mrg   template<typename _Tp>
     82      1.1  mrg     using __integer_to_chars_result_type
     83  1.1.1.3  mrg       = enable_if_t<__or_<__is_signed_integer<_Tp>,
     84  1.1.1.3  mrg 			  __is_unsigned_integer<_Tp>,
     85  1.1.1.3  mrg 			  is_same<char, remove_cv_t<_Tp>>>::value,
     86  1.1.1.3  mrg 		    to_chars_result>;
     87  1.1.1.3  mrg 
     88  1.1.1.3  mrg   // Pick an unsigned type of suitable size. This is used to reduce the
     89  1.1.1.3  mrg   // number of specializations of __to_chars_len, __to_chars etc. that
     90  1.1.1.3  mrg   // get instantiated. For example, to_chars<char> and to_chars<short>
     91  1.1.1.3  mrg   // and to_chars<unsigned> will all use the same code, and so will
     92  1.1.1.3  mrg   // to_chars<long> when sizeof(int) == sizeof(long).
     93      1.1  mrg   template<typename _Tp>
     94  1.1.1.3  mrg     struct __to_chars_unsigned_type : __make_unsigned_selector_base
     95  1.1.1.3  mrg     {
     96  1.1.1.3  mrg       using _UInts = _List<unsigned int, unsigned long, unsigned long long
     97  1.1.1.4  mrg #if __SIZEOF_INT128__ > __SIZEOF_LONG_LONG__
     98  1.1.1.3  mrg 	, unsigned __int128
     99      1.1  mrg #endif
    100  1.1.1.3  mrg 	>;
    101  1.1.1.3  mrg       using type = typename __select<sizeof(_Tp), _UInts>::__type;
    102  1.1.1.3  mrg     };
    103      1.1  mrg 
    104      1.1  mrg   template<typename _Tp>
    105  1.1.1.3  mrg     using __unsigned_least_t = typename __to_chars_unsigned_type<_Tp>::type;
    106      1.1  mrg 
    107  1.1.1.3  mrg   // Generic implementation for arbitrary bases.
    108  1.1.1.3  mrg   // Defined in <bits/charconv.h>.
    109      1.1  mrg   template<typename _Tp>
    110      1.1  mrg     constexpr unsigned
    111  1.1.1.3  mrg     __to_chars_len(_Tp __value, int __base /* = 10 */) noexcept;
    112      1.1  mrg 
    113      1.1  mrg   template<typename _Tp>
    114      1.1  mrg     constexpr unsigned
    115  1.1.1.3  mrg     __to_chars_len_2(_Tp __value) noexcept
    116  1.1.1.3  mrg     { return std::__bit_width(__value); }
    117      1.1  mrg 
    118      1.1  mrg   // Generic implementation for arbitrary bases.
    119      1.1  mrg   template<typename _Tp>
    120      1.1  mrg     to_chars_result
    121      1.1  mrg     __to_chars(char* __first, char* __last, _Tp __val, int __base) noexcept
    122      1.1  mrg     {
    123      1.1  mrg       static_assert(is_integral<_Tp>::value, "implementation bug");
    124      1.1  mrg       static_assert(is_unsigned<_Tp>::value, "implementation bug");
    125      1.1  mrg 
    126      1.1  mrg       to_chars_result __res;
    127      1.1  mrg 
    128      1.1  mrg       const unsigned __len = __to_chars_len(__val, __base);
    129      1.1  mrg 
    130      1.1  mrg       if (__builtin_expect((__last - __first) < __len, 0))
    131      1.1  mrg 	{
    132      1.1  mrg 	  __res.ptr = __last;
    133      1.1  mrg 	  __res.ec = errc::value_too_large;
    134      1.1  mrg 	  return __res;
    135      1.1  mrg 	}
    136      1.1  mrg 
    137      1.1  mrg       unsigned __pos = __len - 1;
    138      1.1  mrg 
    139  1.1.1.3  mrg       static constexpr char __digits[] = {
    140  1.1.1.3  mrg 	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    141  1.1.1.3  mrg 	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
    142  1.1.1.3  mrg 	'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
    143  1.1.1.3  mrg 	'u', 'v', 'w', 'x', 'y', 'z'
    144  1.1.1.3  mrg       };
    145      1.1  mrg 
    146  1.1.1.3  mrg       while (__val >= (unsigned)__base)
    147      1.1  mrg 	{
    148      1.1  mrg 	  auto const __quo = __val / __base;
    149      1.1  mrg 	  auto const __rem = __val % __base;
    150      1.1  mrg 	  __first[__pos--] = __digits[__rem];
    151      1.1  mrg 	  __val = __quo;
    152      1.1  mrg 	}
    153      1.1  mrg       *__first = __digits[__val];
    154      1.1  mrg 
    155      1.1  mrg       __res.ptr = __first + __len;
    156      1.1  mrg       __res.ec = {};
    157      1.1  mrg       return __res;
    158      1.1  mrg     }
    159      1.1  mrg 
    160      1.1  mrg   template<typename _Tp>
    161      1.1  mrg     __integer_to_chars_result_type<_Tp>
    162      1.1  mrg     __to_chars_16(char* __first, char* __last, _Tp __val) noexcept
    163      1.1  mrg     {
    164      1.1  mrg       static_assert(is_integral<_Tp>::value, "implementation bug");
    165      1.1  mrg       static_assert(is_unsigned<_Tp>::value, "implementation bug");
    166      1.1  mrg 
    167      1.1  mrg       to_chars_result __res;
    168      1.1  mrg 
    169  1.1.1.3  mrg       const unsigned __len = (__to_chars_len_2(__val) + 3) / 4;
    170      1.1  mrg 
    171      1.1  mrg       if (__builtin_expect((__last - __first) < __len, 0))
    172      1.1  mrg 	{
    173      1.1  mrg 	  __res.ptr = __last;
    174      1.1  mrg 	  __res.ec = errc::value_too_large;
    175      1.1  mrg 	  return __res;
    176      1.1  mrg 	}
    177      1.1  mrg 
    178  1.1.1.3  mrg       static constexpr char __digits[] = {
    179  1.1.1.3  mrg 	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    180  1.1.1.3  mrg 	'a', 'b', 'c', 'd', 'e', 'f'
    181  1.1.1.3  mrg       };
    182      1.1  mrg       unsigned __pos = __len - 1;
    183      1.1  mrg       while (__val >= 0x100)
    184      1.1  mrg 	{
    185  1.1.1.3  mrg 	  auto __num = __val & 0xF;
    186  1.1.1.3  mrg 	  __val >>= 4;
    187  1.1.1.3  mrg 	  __first[__pos] = __digits[__num];
    188  1.1.1.3  mrg 	  __num = __val & 0xF;
    189  1.1.1.3  mrg 	  __val >>= 4;
    190      1.1  mrg 	  __first[__pos - 1] = __digits[__num];
    191      1.1  mrg 	  __pos -= 2;
    192      1.1  mrg 	}
    193      1.1  mrg       if (__val >= 0x10)
    194      1.1  mrg 	{
    195  1.1.1.3  mrg 	  const auto __num = __val & 0xF;
    196  1.1.1.3  mrg 	  __val >>= 4;
    197  1.1.1.3  mrg 	  __first[1] = __digits[__num];
    198  1.1.1.3  mrg 	  __first[0] = __digits[__val];
    199      1.1  mrg 	}
    200      1.1  mrg       else
    201  1.1.1.3  mrg 	__first[0] = __digits[__val];
    202      1.1  mrg       __res.ptr = __first + __len;
    203      1.1  mrg       __res.ec = {};
    204      1.1  mrg       return __res;
    205      1.1  mrg     }
    206      1.1  mrg 
    207      1.1  mrg   template<typename _Tp>
    208  1.1.1.3  mrg     inline __integer_to_chars_result_type<_Tp>
    209      1.1  mrg     __to_chars_10(char* __first, char* __last, _Tp __val) noexcept
    210      1.1  mrg     {
    211      1.1  mrg       static_assert(is_integral<_Tp>::value, "implementation bug");
    212      1.1  mrg       static_assert(is_unsigned<_Tp>::value, "implementation bug");
    213      1.1  mrg 
    214      1.1  mrg       to_chars_result __res;
    215      1.1  mrg 
    216      1.1  mrg       const unsigned __len = __to_chars_len(__val, 10);
    217      1.1  mrg 
    218      1.1  mrg       if (__builtin_expect((__last - __first) < __len, 0))
    219      1.1  mrg 	{
    220      1.1  mrg 	  __res.ptr = __last;
    221      1.1  mrg 	  __res.ec = errc::value_too_large;
    222      1.1  mrg 	  return __res;
    223      1.1  mrg 	}
    224      1.1  mrg 
    225  1.1.1.3  mrg       __detail::__to_chars_10_impl(__first, __len, __val);
    226      1.1  mrg       __res.ptr = __first + __len;
    227      1.1  mrg       __res.ec = {};
    228      1.1  mrg       return __res;
    229      1.1  mrg     }
    230      1.1  mrg 
    231      1.1  mrg   template<typename _Tp>
    232      1.1  mrg     __integer_to_chars_result_type<_Tp>
    233      1.1  mrg     __to_chars_8(char* __first, char* __last, _Tp __val) noexcept
    234      1.1  mrg     {
    235      1.1  mrg       static_assert(is_integral<_Tp>::value, "implementation bug");
    236      1.1  mrg       static_assert(is_unsigned<_Tp>::value, "implementation bug");
    237      1.1  mrg 
    238      1.1  mrg       to_chars_result __res;
    239  1.1.1.4  mrg       unsigned __len = 0;
    240      1.1  mrg 
    241  1.1.1.3  mrg       if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Tp>::__digits <= 16)
    242  1.1.1.3  mrg 	{
    243  1.1.1.3  mrg 	  __len = __val > 077777u ? 6u
    244  1.1.1.3  mrg 	    : __val > 07777u ? 5u
    245  1.1.1.3  mrg 	    : __val > 0777u ? 4u
    246  1.1.1.3  mrg 	    : __val > 077u ? 3u
    247  1.1.1.3  mrg 	    : __val > 07u ? 2u
    248  1.1.1.3  mrg 	    : 1u;
    249  1.1.1.3  mrg 	}
    250  1.1.1.3  mrg       else
    251  1.1.1.3  mrg 	__len = (__to_chars_len_2(__val) + 2) / 3;
    252      1.1  mrg 
    253      1.1  mrg       if (__builtin_expect((__last - __first) < __len, 0))
    254      1.1  mrg 	{
    255      1.1  mrg 	  __res.ptr = __last;
    256      1.1  mrg 	  __res.ec = errc::value_too_large;
    257      1.1  mrg 	  return __res;
    258      1.1  mrg 	}
    259      1.1  mrg 
    260      1.1  mrg       unsigned __pos = __len - 1;
    261      1.1  mrg       while (__val >= 0100)
    262      1.1  mrg 	{
    263  1.1.1.3  mrg 	  auto __num = __val & 7;
    264  1.1.1.3  mrg 	  __val >>= 3;
    265  1.1.1.3  mrg 	  __first[__pos] = '0' + __num;
    266  1.1.1.3  mrg 	  __num = __val & 7;
    267  1.1.1.3  mrg 	  __val >>= 3;
    268  1.1.1.3  mrg 	  __first[__pos - 1] = '0' + __num;
    269      1.1  mrg 	  __pos -= 2;
    270      1.1  mrg 	}
    271      1.1  mrg       if (__val >= 010)
    272      1.1  mrg 	{
    273  1.1.1.3  mrg 	  auto const __num = __val & 7;
    274  1.1.1.3  mrg 	  __val >>= 3;
    275  1.1.1.3  mrg 	  __first[1] = '0' + __num;
    276  1.1.1.3  mrg 	  __first[0] = '0' + __val;
    277      1.1  mrg 	}
    278      1.1  mrg       else
    279  1.1.1.3  mrg 	__first[0] = '0' + __val;
    280      1.1  mrg       __res.ptr = __first + __len;
    281      1.1  mrg       __res.ec = {};
    282      1.1  mrg       return __res;
    283      1.1  mrg     }
    284      1.1  mrg 
    285      1.1  mrg   template<typename _Tp>
    286      1.1  mrg     __integer_to_chars_result_type<_Tp>
    287      1.1  mrg     __to_chars_2(char* __first, char* __last, _Tp __val) noexcept
    288      1.1  mrg     {
    289      1.1  mrg       static_assert(is_integral<_Tp>::value, "implementation bug");
    290      1.1  mrg       static_assert(is_unsigned<_Tp>::value, "implementation bug");
    291      1.1  mrg 
    292      1.1  mrg       to_chars_result __res;
    293      1.1  mrg 
    294      1.1  mrg       const unsigned __len = __to_chars_len_2(__val);
    295      1.1  mrg 
    296      1.1  mrg       if (__builtin_expect((__last - __first) < __len, 0))
    297      1.1  mrg 	{
    298      1.1  mrg 	  __res.ptr = __last;
    299      1.1  mrg 	  __res.ec = errc::value_too_large;
    300      1.1  mrg 	  return __res;
    301      1.1  mrg 	}
    302      1.1  mrg 
    303      1.1  mrg       unsigned __pos = __len - 1;
    304      1.1  mrg 
    305      1.1  mrg       while (__pos)
    306      1.1  mrg 	{
    307      1.1  mrg 	  __first[__pos--] = '0' + (__val & 1);
    308      1.1  mrg 	  __val >>= 1;
    309      1.1  mrg 	}
    310  1.1.1.3  mrg       // First digit is always '1' because __to_chars_len_2 skips
    311  1.1.1.3  mrg       // leading zero bits and std::to_chars handles zero values
    312  1.1.1.3  mrg       // directly.
    313  1.1.1.3  mrg       __first[0] = '1';
    314      1.1  mrg 
    315      1.1  mrg       __res.ptr = __first + __len;
    316      1.1  mrg       __res.ec = {};
    317      1.1  mrg       return __res;
    318      1.1  mrg     }
    319      1.1  mrg 
    320      1.1  mrg } // namespace __detail
    321      1.1  mrg 
    322      1.1  mrg   template<typename _Tp>
    323      1.1  mrg     __detail::__integer_to_chars_result_type<_Tp>
    324  1.1.1.3  mrg     __to_chars_i(char* __first, char* __last, _Tp __value, int __base = 10)
    325      1.1  mrg     {
    326      1.1  mrg       __glibcxx_assert(2 <= __base && __base <= 36);
    327      1.1  mrg 
    328      1.1  mrg       using _Up = __detail::__unsigned_least_t<_Tp>;
    329      1.1  mrg       _Up __unsigned_val = __value;
    330      1.1  mrg 
    331  1.1.1.3  mrg       if (__first == __last) [[__unlikely__]]
    332  1.1.1.3  mrg 	return { __last, errc::value_too_large };
    333  1.1.1.3  mrg 
    334  1.1.1.3  mrg       if (__value == 0)
    335      1.1  mrg 	{
    336      1.1  mrg 	  *__first = '0';
    337      1.1  mrg 	  return { __first + 1, errc{} };
    338      1.1  mrg 	}
    339  1.1.1.3  mrg       else if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
    340      1.1  mrg 	if (__value < 0)
    341      1.1  mrg 	  {
    342  1.1.1.3  mrg 	    *__first++ = '-';
    343      1.1  mrg 	    __unsigned_val = _Up(~__value) + _Up(1);
    344      1.1  mrg 	  }
    345      1.1  mrg 
    346      1.1  mrg       switch (__base)
    347      1.1  mrg       {
    348      1.1  mrg       case 16:
    349      1.1  mrg 	return __detail::__to_chars_16(__first, __last, __unsigned_val);
    350      1.1  mrg       case 10:
    351      1.1  mrg 	return __detail::__to_chars_10(__first, __last, __unsigned_val);
    352      1.1  mrg       case 8:
    353      1.1  mrg 	return __detail::__to_chars_8(__first, __last, __unsigned_val);
    354      1.1  mrg       case 2:
    355      1.1  mrg 	return __detail::__to_chars_2(__first, __last, __unsigned_val);
    356      1.1  mrg       default:
    357      1.1  mrg 	return __detail::__to_chars(__first, __last, __unsigned_val, __base);
    358      1.1  mrg       }
    359      1.1  mrg     }
    360      1.1  mrg 
    361  1.1.1.3  mrg #define _GLIBCXX_TO_CHARS(T) \
    362  1.1.1.3  mrg   inline to_chars_result \
    363  1.1.1.3  mrg   to_chars(char* __first, char* __last, T __value, int __base = 10) \
    364  1.1.1.3  mrg   { return std::__to_chars_i<T>(__first, __last, __value, __base); }
    365  1.1.1.3  mrg _GLIBCXX_TO_CHARS(char)
    366  1.1.1.3  mrg _GLIBCXX_TO_CHARS(signed char)
    367  1.1.1.3  mrg _GLIBCXX_TO_CHARS(unsigned char)
    368  1.1.1.3  mrg _GLIBCXX_TO_CHARS(signed short)
    369  1.1.1.3  mrg _GLIBCXX_TO_CHARS(unsigned short)
    370  1.1.1.3  mrg _GLIBCXX_TO_CHARS(signed int)
    371  1.1.1.3  mrg _GLIBCXX_TO_CHARS(unsigned int)
    372  1.1.1.3  mrg _GLIBCXX_TO_CHARS(signed long)
    373  1.1.1.3  mrg _GLIBCXX_TO_CHARS(unsigned long)
    374  1.1.1.3  mrg _GLIBCXX_TO_CHARS(signed long long)
    375  1.1.1.3  mrg _GLIBCXX_TO_CHARS(unsigned long long)
    376  1.1.1.3  mrg #if defined(__GLIBCXX_TYPE_INT_N_0)
    377  1.1.1.3  mrg _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_0)
    378  1.1.1.3  mrg _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_0)
    379  1.1.1.3  mrg #endif
    380  1.1.1.3  mrg #if defined(__GLIBCXX_TYPE_INT_N_1)
    381  1.1.1.3  mrg _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_1)
    382  1.1.1.3  mrg _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_1)
    383  1.1.1.3  mrg #endif
    384  1.1.1.3  mrg #if defined(__GLIBCXX_TYPE_INT_N_2)
    385  1.1.1.3  mrg _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_2)
    386  1.1.1.3  mrg _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_2)
    387  1.1.1.3  mrg #endif
    388  1.1.1.3  mrg #if defined(__GLIBCXX_TYPE_INT_N_3)
    389  1.1.1.3  mrg _GLIBCXX_TO_CHARS(signed __GLIBCXX_TYPE_INT_N_3)
    390  1.1.1.3  mrg _GLIBCXX_TO_CHARS(unsigned __GLIBCXX_TYPE_INT_N_3)
    391  1.1.1.3  mrg #endif
    392  1.1.1.3  mrg #undef _GLIBCXX_TO_CHARS
    393  1.1.1.3  mrg 
    394  1.1.1.3  mrg   // _GLIBCXX_RESOLVE_LIB_DEFECTS
    395  1.1.1.3  mrg   // 3266. to_chars(bool) should be deleted
    396  1.1.1.3  mrg   to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
    397  1.1.1.3  mrg 
    398      1.1  mrg namespace __detail
    399      1.1  mrg {
    400      1.1  mrg   template<typename _Tp>
    401      1.1  mrg     bool
    402      1.1  mrg     __raise_and_add(_Tp& __val, int __base, unsigned char __c)
    403      1.1  mrg     {
    404      1.1  mrg       if (__builtin_mul_overflow(__val, __base, &__val)
    405      1.1  mrg 	  || __builtin_add_overflow(__val, __c, &__val))
    406      1.1  mrg 	return false;
    407      1.1  mrg       return true;
    408      1.1  mrg     }
    409      1.1  mrg 
    410  1.1.1.4  mrg   // Construct and return a lookup table that maps 0-9, A-Z and a-z to their
    411  1.1.1.4  mrg   // corresponding base-36 value and maps all other characters to 127.
    412  1.1.1.4  mrg   constexpr auto
    413  1.1.1.4  mrg   __from_chars_alnum_to_val_table()
    414  1.1.1.4  mrg   {
    415  1.1.1.4  mrg     constexpr unsigned char __lower_letters[27] = "abcdefghijklmnopqrstuvwxyz";
    416  1.1.1.4  mrg     constexpr unsigned char __upper_letters[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    417  1.1.1.4  mrg     struct { unsigned char __data[1u << __CHAR_BIT__] = {}; } __table;
    418  1.1.1.4  mrg     for (auto& __entry : __table.__data)
    419  1.1.1.4  mrg       __entry = 127;
    420  1.1.1.4  mrg     for (int __i = 0; __i < 10; ++__i)
    421  1.1.1.4  mrg       __table.__data['0' + __i] = __i;
    422  1.1.1.4  mrg     for (int __i = 0; __i < 26; ++__i)
    423  1.1.1.4  mrg       {
    424  1.1.1.4  mrg 	__table.__data[__lower_letters[__i]] = 10 + __i;
    425  1.1.1.4  mrg 	__table.__data[__upper_letters[__i]] = 10 + __i;
    426  1.1.1.4  mrg       }
    427  1.1.1.4  mrg     return __table;
    428  1.1.1.4  mrg   }
    429  1.1.1.4  mrg 
    430  1.1.1.4  mrg   // If _DecOnly is true: if the character is a decimal digit, then
    431  1.1.1.4  mrg   // return its corresponding base-10 value, otherwise return a value >= 127.
    432  1.1.1.4  mrg   // If _DecOnly is false: if the character is an alphanumeric digit, then
    433  1.1.1.4  mrg   // return its corresponding base-36 value, otherwise return a value >= 127.
    434  1.1.1.4  mrg   template<bool _DecOnly = false>
    435  1.1.1.4  mrg     unsigned char
    436  1.1.1.4  mrg     __from_chars_alnum_to_val(unsigned char __c)
    437  1.1.1.4  mrg     {
    438  1.1.1.4  mrg       if _GLIBCXX17_CONSTEXPR (_DecOnly)
    439  1.1.1.4  mrg 	return static_cast<unsigned char>(__c - '0');
    440  1.1.1.4  mrg       else
    441  1.1.1.4  mrg 	{
    442  1.1.1.4  mrg 	  // This initializer is deliberately made dependent in order to work
    443  1.1.1.4  mrg 	  // around modules bug PR105322.
    444  1.1.1.4  mrg 	  static constexpr auto __table = (_DecOnly, __from_chars_alnum_to_val_table());
    445  1.1.1.4  mrg 	  return __table.__data[__c];
    446  1.1.1.4  mrg 	}
    447  1.1.1.4  mrg     }
    448  1.1.1.4  mrg 
    449  1.1.1.4  mrg   /// std::from_chars implementation for integers in a power-of-two base.
    450  1.1.1.4  mrg   /// If _DecOnly is true, then we may assume __base is at most 8.
    451  1.1.1.4  mrg   template<bool _DecOnly, typename _Tp>
    452      1.1  mrg     bool
    453  1.1.1.4  mrg     __from_chars_pow2_base(const char*& __first, const char* __last, _Tp& __val,
    454  1.1.1.4  mrg 			   int __base)
    455      1.1  mrg     {
    456      1.1  mrg       static_assert(is_integral<_Tp>::value, "implementation bug");
    457      1.1  mrg       static_assert(is_unsigned<_Tp>::value, "implementation bug");
    458      1.1  mrg 
    459  1.1.1.4  mrg       // __glibcxx_assert((__base & (__base - 1)) == 0);
    460  1.1.1.4  mrg       // __glibcxx_assert(_DecOnly ? __base <= 8 : __base <= 32);
    461  1.1.1.4  mrg       const int __log2_base = __countr_zero(unsigned(__base & 0x3f));
    462  1.1.1.4  mrg 
    463      1.1  mrg       const ptrdiff_t __len = __last - __first;
    464  1.1.1.3  mrg       ptrdiff_t __i = 0;
    465  1.1.1.3  mrg       while (__i < __len && __first[__i] == '0')
    466  1.1.1.3  mrg 	++__i;
    467  1.1.1.3  mrg       const ptrdiff_t __leading_zeroes = __i;
    468  1.1.1.4  mrg       if (__i >= __len) [[__unlikely__]]
    469  1.1.1.4  mrg 	{
    470  1.1.1.4  mrg 	  __first += __i;
    471  1.1.1.4  mrg 	  return true;
    472  1.1.1.4  mrg 	}
    473  1.1.1.3  mrg 
    474  1.1.1.4  mrg       // Remember the leading significant digit value if necessary.
    475  1.1.1.4  mrg       unsigned char __leading_c = 0;
    476  1.1.1.4  mrg       if (__base != 2)
    477      1.1  mrg 	{
    478  1.1.1.4  mrg 	  __leading_c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
    479  1.1.1.4  mrg 	  // __glibcxx_assert(__leading_c != 0);
    480  1.1.1.4  mrg 	  if (__leading_c >= __base) [[__unlikely__]]
    481  1.1.1.4  mrg 	    {
    482  1.1.1.4  mrg 	      __first += __i;
    483  1.1.1.4  mrg 	      return true;
    484  1.1.1.4  mrg 	    }
    485  1.1.1.4  mrg 	  __val = __leading_c;
    486  1.1.1.4  mrg 	  ++__i;
    487  1.1.1.4  mrg 	}
    488  1.1.1.4  mrg 
    489  1.1.1.4  mrg       for (; __i < __len; ++__i)
    490  1.1.1.4  mrg 	{
    491  1.1.1.4  mrg 	  const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(__first[__i]);
    492  1.1.1.4  mrg 	  if (__c >= __base)
    493      1.1  mrg 	    break;
    494  1.1.1.4  mrg 	  __val = (__val << __log2_base) | __c;
    495      1.1  mrg 	}
    496      1.1  mrg       __first += __i;
    497  1.1.1.4  mrg       auto __significant_bits = (__i - __leading_zeroes) * __log2_base;
    498  1.1.1.4  mrg       if (__base != 2)
    499  1.1.1.4  mrg 	// Compensate for a leading significant digit that didn't use all
    500  1.1.1.4  mrg 	// of its available bits.
    501  1.1.1.4  mrg 	__significant_bits -= __log2_base - __bit_width(__leading_c);
    502  1.1.1.4  mrg       // __glibcxx_assert(__significant_bits >= 0);
    503  1.1.1.4  mrg       return __significant_bits <= __gnu_cxx::__int_traits<_Tp>::__digits;
    504      1.1  mrg     }
    505      1.1  mrg 
    506  1.1.1.4  mrg   /// std::from_chars implementation for integers in any base.
    507  1.1.1.4  mrg   /// If _DecOnly is true, then we may assume __base is at most 10.
    508  1.1.1.4  mrg   template<bool _DecOnly, typename _Tp>
    509      1.1  mrg     bool
    510  1.1.1.4  mrg     __from_chars_alnum(const char*& __first, const char* __last, _Tp& __val,
    511      1.1  mrg 		       int __base)
    512      1.1  mrg     {
    513  1.1.1.4  mrg       // __glibcxx_assert(_DecOnly ? __base <= 10 : __base <= 36);
    514      1.1  mrg 
    515  1.1.1.4  mrg       const int __bits_per_digit = __bit_width(unsigned(__base & 0x3f));
    516  1.1.1.4  mrg       int __unused_bits_lower_bound = __gnu_cxx::__int_traits<_Tp>::__digits;
    517  1.1.1.4  mrg       for (; __first != __last; ++__first)
    518      1.1  mrg 	{
    519  1.1.1.4  mrg 	  const unsigned char __c = __from_chars_alnum_to_val<_DecOnly>(*__first);
    520  1.1.1.4  mrg 	  if (__c >= __base)
    521      1.1  mrg 	    return true;
    522      1.1  mrg 
    523  1.1.1.4  mrg 	  __unused_bits_lower_bound -= __bits_per_digit;
    524  1.1.1.4  mrg 	  if (__unused_bits_lower_bound >= 0) [[__likely__]]
    525  1.1.1.4  mrg 	    // We're definitely not going to overflow.
    526  1.1.1.4  mrg 	    __val = __val * __base + __c;
    527  1.1.1.4  mrg 	  else if (!__raise_and_add(__val, __base, __c)) [[__unlikely__]]
    528      1.1  mrg 	    {
    529  1.1.1.4  mrg 	      while (++__first != __last
    530  1.1.1.4  mrg 		     && __from_chars_alnum_to_val<_DecOnly>(*__first) < __base)
    531  1.1.1.4  mrg 		;
    532  1.1.1.4  mrg 	      return false;
    533      1.1  mrg 	    }
    534      1.1  mrg 	}
    535  1.1.1.4  mrg       return true;
    536      1.1  mrg     }
    537      1.1  mrg 
    538      1.1  mrg   template<typename _Tp>
    539      1.1  mrg     using __integer_from_chars_result_type
    540  1.1.1.3  mrg       = enable_if_t<__or_<__is_signed_integer<_Tp>,
    541  1.1.1.3  mrg 			  __is_unsigned_integer<_Tp>,
    542  1.1.1.3  mrg 			  is_same<char, remove_cv_t<_Tp>>>::value,
    543  1.1.1.3  mrg 		    from_chars_result>;
    544      1.1  mrg 
    545      1.1  mrg } // namespace __detail
    546      1.1  mrg 
    547      1.1  mrg   /// std::from_chars for integral types.
    548      1.1  mrg   template<typename _Tp>
    549      1.1  mrg     __detail::__integer_from_chars_result_type<_Tp>
    550      1.1  mrg     from_chars(const char* __first, const char* __last, _Tp& __value,
    551      1.1  mrg 	       int __base = 10)
    552      1.1  mrg     {
    553      1.1  mrg       __glibcxx_assert(2 <= __base && __base <= 36);
    554      1.1  mrg 
    555      1.1  mrg       from_chars_result __res{__first, {}};
    556      1.1  mrg 
    557      1.1  mrg       int __sign = 1;
    558      1.1  mrg       if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
    559      1.1  mrg 	if (__first != __last && *__first == '-')
    560      1.1  mrg 	  {
    561      1.1  mrg 	    __sign = -1;
    562      1.1  mrg 	    ++__first;
    563      1.1  mrg 	  }
    564      1.1  mrg 
    565      1.1  mrg       using _Up = __detail::__unsigned_least_t<_Tp>;
    566      1.1  mrg       _Up __val = 0;
    567      1.1  mrg 
    568      1.1  mrg       const auto __start = __first;
    569      1.1  mrg       bool __valid;
    570  1.1.1.4  mrg       if ((__base & (__base - 1)) == 0)
    571  1.1.1.4  mrg 	{
    572  1.1.1.4  mrg 	  if (__base <= 8)
    573  1.1.1.4  mrg 	    __valid = __detail::__from_chars_pow2_base<true>(__first, __last, __val, __base);
    574  1.1.1.4  mrg 	  else
    575  1.1.1.4  mrg 	    __valid = __detail::__from_chars_pow2_base<false>(__first, __last, __val, __base);
    576  1.1.1.4  mrg 	}
    577      1.1  mrg       else if (__base <= 10)
    578  1.1.1.4  mrg 	__valid = __detail::__from_chars_alnum<true>(__first, __last, __val, __base);
    579      1.1  mrg       else
    580  1.1.1.4  mrg 	__valid = __detail::__from_chars_alnum<false>(__first, __last, __val, __base);
    581      1.1  mrg 
    582      1.1  mrg       if (__builtin_expect(__first == __start, 0))
    583      1.1  mrg 	__res.ec = errc::invalid_argument;
    584      1.1  mrg       else
    585      1.1  mrg 	{
    586      1.1  mrg 	  __res.ptr = __first;
    587      1.1  mrg 	  if (!__valid)
    588      1.1  mrg 	    __res.ec = errc::result_out_of_range;
    589      1.1  mrg 	  else
    590      1.1  mrg 	    {
    591      1.1  mrg 	      if _GLIBCXX17_CONSTEXPR (std::is_signed<_Tp>::value)
    592      1.1  mrg 		{
    593      1.1  mrg 		  _Tp __tmp;
    594      1.1  mrg 		  if (__builtin_mul_overflow(__val, __sign, &__tmp))
    595      1.1  mrg 		    __res.ec = errc::result_out_of_range;
    596      1.1  mrg 		  else
    597      1.1  mrg 		    __value = __tmp;
    598      1.1  mrg 		}
    599      1.1  mrg 	      else
    600      1.1  mrg 		{
    601  1.1.1.3  mrg 		  if _GLIBCXX17_CONSTEXPR (__gnu_cxx::__int_traits<_Up>::__max
    602  1.1.1.3  mrg 		      > __gnu_cxx::__int_traits<_Tp>::__max)
    603      1.1  mrg 		    {
    604  1.1.1.3  mrg 		      if (__val > __gnu_cxx::__int_traits<_Tp>::__max)
    605      1.1  mrg 			__res.ec = errc::result_out_of_range;
    606      1.1  mrg 		      else
    607      1.1  mrg 			__value = __val;
    608      1.1  mrg 		    }
    609      1.1  mrg 		  else
    610      1.1  mrg 		    __value = __val;
    611      1.1  mrg 		}
    612      1.1  mrg 	    }
    613      1.1  mrg 	}
    614      1.1  mrg       return __res;
    615      1.1  mrg     }
    616      1.1  mrg 
    617  1.1.1.3  mrg   /// floating-point format for primitive numerical conversion
    618  1.1.1.3  mrg   enum class chars_format
    619  1.1.1.3  mrg   {
    620  1.1.1.3  mrg     scientific = 1, fixed = 2, hex = 4, general = fixed | scientific
    621  1.1.1.3  mrg   };
    622  1.1.1.3  mrg 
    623  1.1.1.3  mrg   constexpr chars_format
    624  1.1.1.3  mrg   operator|(chars_format __lhs, chars_format __rhs) noexcept
    625  1.1.1.3  mrg   { return (chars_format)((unsigned)__lhs | (unsigned)__rhs); }
    626  1.1.1.3  mrg 
    627  1.1.1.3  mrg   constexpr chars_format
    628  1.1.1.3  mrg   operator&(chars_format __lhs, chars_format __rhs) noexcept
    629  1.1.1.3  mrg   { return (chars_format)((unsigned)__lhs & (unsigned)__rhs); }
    630  1.1.1.3  mrg 
    631  1.1.1.3  mrg   constexpr chars_format
    632  1.1.1.3  mrg   operator^(chars_format __lhs, chars_format __rhs) noexcept
    633  1.1.1.3  mrg   { return (chars_format)((unsigned)__lhs ^ (unsigned)__rhs); }
    634  1.1.1.3  mrg 
    635  1.1.1.3  mrg   constexpr chars_format
    636  1.1.1.3  mrg   operator~(chars_format __fmt) noexcept
    637  1.1.1.3  mrg   { return (chars_format)~(unsigned)__fmt; }
    638  1.1.1.3  mrg 
    639  1.1.1.3  mrg   constexpr chars_format&
    640  1.1.1.3  mrg   operator|=(chars_format& __lhs, chars_format __rhs) noexcept
    641  1.1.1.3  mrg   { return __lhs = __lhs | __rhs; }
    642  1.1.1.3  mrg 
    643  1.1.1.3  mrg   constexpr chars_format&
    644  1.1.1.3  mrg   operator&=(chars_format& __lhs, chars_format __rhs) noexcept
    645  1.1.1.3  mrg   { return __lhs = __lhs & __rhs; }
    646  1.1.1.3  mrg 
    647  1.1.1.3  mrg   constexpr chars_format&
    648  1.1.1.3  mrg   operator^=(chars_format& __lhs, chars_format __rhs) noexcept
    649  1.1.1.3  mrg   { return __lhs = __lhs ^ __rhs; }
    650  1.1.1.3  mrg 
    651  1.1.1.4  mrg #if defined __cpp_lib_to_chars || _GLIBCXX_HAVE_USELOCALE
    652  1.1.1.4  mrg   from_chars_result
    653  1.1.1.4  mrg   from_chars(const char* __first, const char* __last, float& __value,
    654  1.1.1.4  mrg 	     chars_format __fmt = chars_format::general) noexcept;
    655  1.1.1.4  mrg 
    656  1.1.1.4  mrg   from_chars_result
    657  1.1.1.4  mrg   from_chars(const char* __first, const char* __last, double& __value,
    658  1.1.1.4  mrg 	     chars_format __fmt = chars_format::general) noexcept;
    659  1.1.1.4  mrg 
    660  1.1.1.4  mrg   from_chars_result
    661  1.1.1.4  mrg   from_chars(const char* __first, const char* __last, long double& __value,
    662  1.1.1.4  mrg 	     chars_format __fmt = chars_format::general) noexcept;
    663  1.1.1.4  mrg #endif
    664  1.1.1.4  mrg 
    665  1.1.1.4  mrg #if defined __cpp_lib_to_chars
    666  1.1.1.4  mrg   // Floating-point std::to_chars
    667  1.1.1.4  mrg 
    668  1.1.1.4  mrg   // Overloads for float.
    669  1.1.1.4  mrg   to_chars_result to_chars(char* __first, char* __last, float __value) noexcept;
    670  1.1.1.4  mrg   to_chars_result to_chars(char* __first, char* __last, float __value,
    671  1.1.1.4  mrg 			   chars_format __fmt) noexcept;
    672  1.1.1.4  mrg   to_chars_result to_chars(char* __first, char* __last, float __value,
    673  1.1.1.4  mrg 			   chars_format __fmt, int __precision) noexcept;
    674  1.1.1.4  mrg 
    675  1.1.1.4  mrg   // Overloads for double.
    676  1.1.1.4  mrg   to_chars_result to_chars(char* __first, char* __last, double __value) noexcept;
    677  1.1.1.4  mrg   to_chars_result to_chars(char* __first, char* __last, double __value,
    678  1.1.1.4  mrg 			   chars_format __fmt) noexcept;
    679  1.1.1.4  mrg   to_chars_result to_chars(char* __first, char* __last, double __value,
    680  1.1.1.4  mrg 			   chars_format __fmt, int __precision) noexcept;
    681  1.1.1.4  mrg 
    682  1.1.1.4  mrg   // Overloads for long double.
    683  1.1.1.4  mrg   to_chars_result to_chars(char* __first, char* __last, long double __value)
    684  1.1.1.4  mrg     noexcept;
    685  1.1.1.4  mrg   to_chars_result to_chars(char* __first, char* __last, long double __value,
    686  1.1.1.4  mrg 			   chars_format __fmt) noexcept;
    687  1.1.1.4  mrg   to_chars_result to_chars(char* __first, char* __last, long double __value,
    688  1.1.1.4  mrg 			   chars_format __fmt, int __precision) noexcept;
    689  1.1.1.4  mrg #endif
    690  1.1.1.4  mrg 
    691      1.1  mrg _GLIBCXX_END_NAMESPACE_VERSION
    692      1.1  mrg } // namespace std
    693      1.1  mrg #endif // C++14
    694      1.1  mrg #endif // _GLIBCXX_CHARCONV
    695