c++/coroutines: correct passing *this to promise type [PR104981]
[official-gcc.git] / libstdc++-v3 / include / bits / charconv.h
blobc9d439b99f273739f4ba4d30afbf8862050bbdcf
1 // Numeric conversions (to_string, to_chars) -*- C++ -*-
3 // Copyright (C) 2017-2024 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.
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
43 namespace __detail
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;
50 #endif
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");
59 #endif
61 unsigned __n = 1;
62 const unsigned __b2 = __base * __base;
63 const unsigned __b3 = __b2 * __base;
64 const unsigned long __b4 = __b3 * __base;
65 for (;;)
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;
71 __value /= __b4;
72 __n += 4;
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");
85 #endif
87 constexpr char __digits[201] =
88 "0001020304050607080910111213141516171819"
89 "2021222324252627282930313233343536373839"
90 "4041424344454647484950515253545556575859"
91 "6061626364656667686970717273747576777879"
92 "8081828384858687888990919293949596979899";
93 unsigned __pos = __len - 1;
94 while (__val >= 100)
96 auto const __num = (__val % 100) * 2;
97 __val /= 100;
98 __first[__pos] = __digits[__num + 1];
99 __first[__pos - 1] = __digits[__num];
100 __pos -= 2;
102 if (__val >= 10)
104 auto const __num = __val * 2;
105 __first[1] = __digits[__num + 1];
106 __first[0] = __digits[__num];
108 else
109 __first[0] = '0' + __val;
112 } // namespace __detail
113 _GLIBCXX_END_NAMESPACE_VERSION
114 } // namespace std
115 #endif // C++11
116 #endif // _GLIBCXX_CHARCONV_H