libstdc++
charconv.h
Go to the documentation of this file.
1 // Numeric conversions (to_string, to_chars) -*- C++ -*-
2 
3 // Copyright (C) 2017-2022 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 #pragma GCC system_header
34 
35 #if __cplusplus >= 201103L
36 
37 #include <type_traits>
38 
39 namespace std _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 namespace __detail
43 {
44  // Generic implementation for arbitrary bases.
45  template<typename _Tp>
46  _GLIBCXX14_CONSTEXPR unsigned
47  __to_chars_len(_Tp __value, int __base = 10) noexcept
48  {
49  static_assert(is_integral<_Tp>::value, "implementation bug");
50  static_assert(is_unsigned<_Tp>::value, "implementation bug");
51 
52  unsigned __n = 1;
53  const unsigned __b2 = __base * __base;
54  const unsigned __b3 = __b2 * __base;
55  const unsigned long __b4 = __b3 * __base;
56  for (;;)
57  {
58  if (__value < (unsigned)__base) return __n;
59  if (__value < __b2) return __n + 1;
60  if (__value < __b3) return __n + 2;
61  if (__value < __b4) return __n + 3;
62  __value /= __b4;
63  __n += 4;
64  }
65  }
66 
67  // Write an unsigned integer value to the range [first,first+len).
68  // The caller is required to provide a buffer of exactly the right size
69  // (which can be determined by the __to_chars_len function).
70  template<typename _Tp>
71  void
72  __to_chars_10_impl(char* __first, unsigned __len, _Tp __val) noexcept
73  {
74  static_assert(is_integral<_Tp>::value, "implementation bug");
75  static_assert(is_unsigned<_Tp>::value, "implementation bug");
76 
77  static constexpr char __digits[201] =
78  "0001020304050607080910111213141516171819"
79  "2021222324252627282930313233343536373839"
80  "4041424344454647484950515253545556575859"
81  "6061626364656667686970717273747576777879"
82  "8081828384858687888990919293949596979899";
83  unsigned __pos = __len - 1;
84  while (__val >= 100)
85  {
86  auto const __num = (__val % 100) * 2;
87  __val /= 100;
88  __first[__pos] = __digits[__num + 1];
89  __first[__pos - 1] = __digits[__num];
90  __pos -= 2;
91  }
92  if (__val >= 10)
93  {
94  auto const __num = __val * 2;
95  __first[1] = __digits[__num + 1];
96  __first[0] = __digits[__num];
97  }
98  else
99  __first[0] = '0' + __val;
100  }
101 
102 } // namespace __detail
103 _GLIBCXX_END_NAMESPACE_VERSION
104 } // namespace std
105 #endif // C++11
106 #endif // _GLIBCXX_CHARCONV_H
ISO C++ entities toplevel namespace is std.
constexpr _Iterator __base(_Iterator __it)