libstdc++: charconv.h Source File

libstdc++

charconv.h
Go to the documentation of this file.
1// Numeric conversions (to_string, to_chars) -*- C++ -*-
2
3// Copyright (C) 2017-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
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}
28 */
29
30#ifndef _GLIBCXX_CHARCONV_H
31#define _GLIBCXX_CHARCONV_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#if __cplusplus >= 201103L
38
39#include <type_traits>
40#include <ext/numeric_traits.h>
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
45namespace __detail
46{
47#if __cpp_variable_templates
48 // This accepts 128-bit integers even in strict mode.
49 template<typename _Tp>
50 constexpr bool __integer_to_chars_is_unsigned
51 = ! __gnu_cxx::__int_traits<_Tp>::__is_signed;
52#endif
53
54 // Generic implementation for arbitrary bases.
55 template<typename _Tp>
56 _GLIBCXX14_CONSTEXPR unsigned
57 __to_chars_len(_Tp __value, int __base = 10) noexcept
58 {
59#if __cpp_variable_templates
60 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
61#endif
62
63 unsigned __n = 1;
64 const unsigned __b2 = __base * __base;
65 const unsigned __b3 = __b2 * __base;
66 const unsigned long __b4 = __b3 * __base;
67 for (;;)
68 {
69 if (__value < (unsigned)__base) return __n;
70 if (__value < __b2) return __n + 1;
71 if (__value < __b3) return __n + 2;
72 if (__value < __b4) return __n + 3;
73 __value /= __b4;
74 __n += 4;
75 }
76 }
77
78 // Write an unsigned integer value to the range [first,first+len).
79 // The caller is required to provide a buffer of exactly the right size
80 // (which can be determined by the __to_chars_len function).
81 template<typename _Tp>
82 _GLIBCXX23_CONSTEXPR void
83 __to_chars_10_impl(char* __first, unsigned __len, _Tp __val) noexcept
84 {
85#if __cpp_variable_templates
86 static_assert(__integer_to_chars_is_unsigned<_Tp>, "implementation bug");
87#endif
88
89 constexpr char __digits[201] =
90 "0001020304050607080910111213141516171819"
91 "2021222324252627282930313233343536373839"
92 "4041424344454647484950515253545556575859"
93 "6061626364656667686970717273747576777879"
94 "8081828384858687888990919293949596979899";
95 unsigned __pos = __len - 1;
96 while (__val >= 100)
97 {
98 auto const __num = (__val % 100) * 2;
99 __val /= 100;
100 __first[__pos] = __digits[__num + 1];
101 __first[__pos - 1] = __digits[__num];
102 __pos -= 2;
103 }
104 if (__val >= 10)
105 {
106 auto const __num = __val * 2;
107 __first[1] = __digits[__num + 1];
108 __first[0] = __digits[__num];
109 }
110 else
111 __first[0] = '0' + __val;
112 }
113
114} // namespace __detail
115_GLIBCXX_END_NAMESPACE_VERSION
116} // namespace std
117#endif // C++11
118#endif // _GLIBCXX_CHARCONV_H
type_traits
std
ISO C++ entities toplevel namespace is std.
std::__detail
Implementation details not part of the namespace std interface.
__gnu_debug::__base
constexpr _Iterator __base(_Iterator __it)
Definition helper_functions.h:339
numeric_traits.h