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