1*4c3eb207Smrg // Numeric conversions (to_string, to_chars) -*- C++ -*-
2*4c3eb207Smrg
3*4c3eb207Smrg // Copyright (C) 2017-2020 Free Software Foundation, Inc.
4*4c3eb207Smrg //
5*4c3eb207Smrg // This file is part of the GNU ISO C++ Library. This library is free
6*4c3eb207Smrg // software; you can redistribute it and/or modify it under the
7*4c3eb207Smrg // terms of the GNU General Public License as published by the
8*4c3eb207Smrg // Free Software Foundation; either version 3, or (at your option)
9*4c3eb207Smrg // any later version.
10*4c3eb207Smrg
11*4c3eb207Smrg // This library is distributed in the hope that it will be useful,
12*4c3eb207Smrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*4c3eb207Smrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*4c3eb207Smrg // GNU General Public License for more details.
15*4c3eb207Smrg
16*4c3eb207Smrg // Under Section 7 of GPL version 3, you are granted additional
17*4c3eb207Smrg // permissions described in the GCC Runtime Library Exception, version
18*4c3eb207Smrg // 3.1, as published by the Free Software Foundation.
19*4c3eb207Smrg
20*4c3eb207Smrg // You should have received a copy of the GNU General Public License and
21*4c3eb207Smrg // a copy of the GCC Runtime Library Exception along with this program;
22*4c3eb207Smrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*4c3eb207Smrg // <http://www.gnu.org/licenses/>.
24*4c3eb207Smrg
25*4c3eb207Smrg /** @file bits/charconv.h
26*4c3eb207Smrg * This is an internal header file, included by other library headers.
27*4c3eb207Smrg * Do not attempt to use it directly. @headername{charconv}
28*4c3eb207Smrg */
29*4c3eb207Smrg
30*4c3eb207Smrg #ifndef _GLIBCXX_CHARCONV_H
31*4c3eb207Smrg #define _GLIBCXX_CHARCONV_H 1
32*4c3eb207Smrg
33*4c3eb207Smrg #pragma GCC system_header
34*4c3eb207Smrg
35*4c3eb207Smrg #if __cplusplus >= 201103L
36*4c3eb207Smrg
37*4c3eb207Smrg #include <type_traits>
38*4c3eb207Smrg
_GLIBCXX_VISIBILITY(default)39*4c3eb207Smrg namespace std _GLIBCXX_VISIBILITY(default)
40*4c3eb207Smrg {
41*4c3eb207Smrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
42*4c3eb207Smrg namespace __detail
43*4c3eb207Smrg {
44*4c3eb207Smrg // Generic implementation for arbitrary bases.
45*4c3eb207Smrg template<typename _Tp>
46*4c3eb207Smrg _GLIBCXX14_CONSTEXPR unsigned
47*4c3eb207Smrg __to_chars_len(_Tp __value, int __base = 10) noexcept
48*4c3eb207Smrg {
49*4c3eb207Smrg static_assert(is_integral<_Tp>::value, "implementation bug");
50*4c3eb207Smrg static_assert(is_unsigned<_Tp>::value, "implementation bug");
51*4c3eb207Smrg
52*4c3eb207Smrg unsigned __n = 1;
53*4c3eb207Smrg const unsigned __b2 = __base * __base;
54*4c3eb207Smrg const unsigned __b3 = __b2 * __base;
55*4c3eb207Smrg const unsigned long __b4 = __b3 * __base;
56*4c3eb207Smrg for (;;)
57*4c3eb207Smrg {
58*4c3eb207Smrg if (__value < (unsigned)__base) return __n;
59*4c3eb207Smrg if (__value < __b2) return __n + 1;
60*4c3eb207Smrg if (__value < __b3) return __n + 2;
61*4c3eb207Smrg if (__value < __b4) return __n + 3;
62*4c3eb207Smrg __value /= __b4;
63*4c3eb207Smrg __n += 4;
64*4c3eb207Smrg }
65*4c3eb207Smrg }
66*4c3eb207Smrg
67*4c3eb207Smrg // Write an unsigned integer value to the range [first,first+len).
68*4c3eb207Smrg // The caller is required to provide a buffer of exactly the right size
69*4c3eb207Smrg // (which can be determined by the __to_chars_len function).
70*4c3eb207Smrg template<typename _Tp>
71*4c3eb207Smrg void
72*4c3eb207Smrg __to_chars_10_impl(char* __first, unsigned __len, _Tp __val) noexcept
73*4c3eb207Smrg {
74*4c3eb207Smrg static_assert(is_integral<_Tp>::value, "implementation bug");
75*4c3eb207Smrg static_assert(is_unsigned<_Tp>::value, "implementation bug");
76*4c3eb207Smrg
77*4c3eb207Smrg static constexpr char __digits[201] =
78*4c3eb207Smrg "0001020304050607080910111213141516171819"
79*4c3eb207Smrg "2021222324252627282930313233343536373839"
80*4c3eb207Smrg "4041424344454647484950515253545556575859"
81*4c3eb207Smrg "6061626364656667686970717273747576777879"
82*4c3eb207Smrg "8081828384858687888990919293949596979899";
83*4c3eb207Smrg unsigned __pos = __len - 1;
84*4c3eb207Smrg while (__val >= 100)
85*4c3eb207Smrg {
86*4c3eb207Smrg auto const __num = (__val % 100) * 2;
87*4c3eb207Smrg __val /= 100;
88*4c3eb207Smrg __first[__pos] = __digits[__num + 1];
89*4c3eb207Smrg __first[__pos - 1] = __digits[__num];
90*4c3eb207Smrg __pos -= 2;
91*4c3eb207Smrg }
92*4c3eb207Smrg if (__val >= 10)
93*4c3eb207Smrg {
94*4c3eb207Smrg auto const __num = __val * 2;
95*4c3eb207Smrg __first[1] = __digits[__num + 1];
96*4c3eb207Smrg __first[0] = __digits[__num];
97*4c3eb207Smrg }
98*4c3eb207Smrg else
99*4c3eb207Smrg __first[0] = '0' + __val;
100*4c3eb207Smrg }
101*4c3eb207Smrg
102*4c3eb207Smrg } // namespace __detail
103*4c3eb207Smrg _GLIBCXX_END_NAMESPACE_VERSION
104*4c3eb207Smrg } // namespace std
105*4c3eb207Smrg #endif // C++11
106*4c3eb207Smrg #endif // _GLIBCXX_CHARCONV_H
107