]>
Commit | Line | Data |
---|---|---|
1c9f675f ESR |
1 | // Components for compile-time parsing of numbers -*- C++ -*- |
2 | ||
a945c346 | 3 | // Copyright (C) 2013-2024 Free Software Foundation, Inc. |
1c9f675f ESR |
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/parse_numbers.h | |
26 | * This is an internal header file, included by other library headers. | |
27 | * Do not attempt to use it directly. @headername{chrono} | |
28 | */ | |
29 | ||
a2f4bd2a ESR |
30 | #ifndef _GLIBCXX_PARSE_NUMBERS_H |
31 | #define _GLIBCXX_PARSE_NUMBERS_H 1 | |
1c9f675f ESR |
32 | |
33 | #pragma GCC system_header | |
34 | ||
7057e645 | 35 | // From n3642.pdf except I added binary literals and digit separator '\''. |
1c9f675f | 36 | |
c03b53da | 37 | #if __cplusplus >= 201402L |
1c9f675f | 38 | |
47cca028 | 39 | #include <type_traits> |
eb04805b | 40 | #include <ext/numeric_traits.h> |
a2f4bd2a | 41 | |
1c9f675f ESR |
42 | namespace std _GLIBCXX_VISIBILITY(default) |
43 | { | |
44 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
45 | ||
a2f4bd2a ESR |
46 | namespace __parse_int |
47 | { | |
1c9f675f ESR |
48 | template<unsigned _Base, char _Dig> |
49 | struct _Digit; | |
50 | ||
51 | template<unsigned _Base> | |
a2f4bd2a | 52 | struct _Digit<_Base, '0'> : integral_constant<unsigned, 0> |
1c9f675f | 53 | { |
a2f4bd2a | 54 | using __valid = true_type; |
1c9f675f ESR |
55 | }; |
56 | ||
57 | template<unsigned _Base> | |
a2f4bd2a | 58 | struct _Digit<_Base, '1'> : integral_constant<unsigned, 1> |
1c9f675f | 59 | { |
a2f4bd2a | 60 | using __valid = true_type; |
1c9f675f ESR |
61 | }; |
62 | ||
a2f4bd2a ESR |
63 | template<unsigned _Base, unsigned _Val> |
64 | struct _Digit_impl : integral_constant<unsigned, _Val> | |
1c9f675f | 65 | { |
a2f4bd2a ESR |
66 | static_assert(_Base > _Val, "invalid digit"); |
67 | using __valid = true_type; | |
1c9f675f ESR |
68 | }; |
69 | ||
70 | template<unsigned _Base> | |
a2f4bd2a ESR |
71 | struct _Digit<_Base, '2'> : _Digit_impl<_Base, 2> |
72 | { }; | |
1c9f675f ESR |
73 | |
74 | template<unsigned _Base> | |
a2f4bd2a ESR |
75 | struct _Digit<_Base, '3'> : _Digit_impl<_Base, 3> |
76 | { }; | |
1c9f675f ESR |
77 | |
78 | template<unsigned _Base> | |
a2f4bd2a ESR |
79 | struct _Digit<_Base, '4'> : _Digit_impl<_Base, 4> |
80 | { }; | |
1c9f675f ESR |
81 | |
82 | template<unsigned _Base> | |
a2f4bd2a ESR |
83 | struct _Digit<_Base, '5'> : _Digit_impl<_Base, 5> |
84 | { }; | |
1c9f675f ESR |
85 | |
86 | template<unsigned _Base> | |
a2f4bd2a ESR |
87 | struct _Digit<_Base, '6'> : _Digit_impl<_Base, 6> |
88 | { }; | |
1c9f675f ESR |
89 | |
90 | template<unsigned _Base> | |
a2f4bd2a ESR |
91 | struct _Digit<_Base, '7'> : _Digit_impl<_Base, 7> |
92 | { }; | |
1c9f675f ESR |
93 | |
94 | template<unsigned _Base> | |
a2f4bd2a ESR |
95 | struct _Digit<_Base, '8'> : _Digit_impl<_Base, 8> |
96 | { }; | |
1c9f675f ESR |
97 | |
98 | template<unsigned _Base> | |
a2f4bd2a ESR |
99 | struct _Digit<_Base, '9'> : _Digit_impl<_Base, 9> |
100 | { }; | |
1c9f675f ESR |
101 | |
102 | template<unsigned _Base> | |
a2f4bd2a ESR |
103 | struct _Digit<_Base, 'a'> : _Digit_impl<_Base, 0xa> |
104 | { }; | |
1c9f675f ESR |
105 | |
106 | template<unsigned _Base> | |
a2f4bd2a ESR |
107 | struct _Digit<_Base, 'A'> : _Digit_impl<_Base, 0xa> |
108 | { }; | |
1c9f675f ESR |
109 | |
110 | template<unsigned _Base> | |
a2f4bd2a ESR |
111 | struct _Digit<_Base, 'b'> : _Digit_impl<_Base, 0xb> |
112 | { }; | |
1c9f675f ESR |
113 | |
114 | template<unsigned _Base> | |
a2f4bd2a ESR |
115 | struct _Digit<_Base, 'B'> : _Digit_impl<_Base, 0xb> |
116 | { }; | |
1c9f675f ESR |
117 | |
118 | template<unsigned _Base> | |
a2f4bd2a ESR |
119 | struct _Digit<_Base, 'c'> : _Digit_impl<_Base, 0xc> |
120 | { }; | |
1c9f675f ESR |
121 | |
122 | template<unsigned _Base> | |
a2f4bd2a ESR |
123 | struct _Digit<_Base, 'C'> : _Digit_impl<_Base, 0xc> |
124 | { }; | |
1c9f675f ESR |
125 | |
126 | template<unsigned _Base> | |
a2f4bd2a ESR |
127 | struct _Digit<_Base, 'd'> : _Digit_impl<_Base, 0xd> |
128 | { }; | |
1c9f675f ESR |
129 | |
130 | template<unsigned _Base> | |
a2f4bd2a ESR |
131 | struct _Digit<_Base, 'D'> : _Digit_impl<_Base, 0xd> |
132 | { }; | |
1c9f675f ESR |
133 | |
134 | template<unsigned _Base> | |
a2f4bd2a ESR |
135 | struct _Digit<_Base, 'e'> : _Digit_impl<_Base, 0xe> |
136 | { }; | |
1c9f675f ESR |
137 | |
138 | template<unsigned _Base> | |
a2f4bd2a ESR |
139 | struct _Digit<_Base, 'E'> : _Digit_impl<_Base, 0xe> |
140 | { }; | |
1c9f675f ESR |
141 | |
142 | template<unsigned _Base> | |
a2f4bd2a ESR |
143 | struct _Digit<_Base, 'f'> : _Digit_impl<_Base, 0xf> |
144 | { }; | |
1c9f675f | 145 | |
1c9f675f | 146 | template<unsigned _Base> |
a2f4bd2a ESR |
147 | struct _Digit<_Base, 'F'> : _Digit_impl<_Base, 0xf> |
148 | { }; | |
1c9f675f | 149 | |
a2f4bd2a | 150 | // Digit separator |
1c9f675f | 151 | template<unsigned _Base> |
a2f4bd2a | 152 | struct _Digit<_Base, '\''> : integral_constant<unsigned, 0> |
1c9f675f | 153 | { |
a2f4bd2a | 154 | using __valid = false_type; |
1c9f675f ESR |
155 | }; |
156 | ||
157 | //------------------------------------------------------------------------------ | |
158 | ||
a2f4bd2a ESR |
159 | template<unsigned long long _Val> |
160 | using __ull_constant = integral_constant<unsigned long long, _Val>; | |
161 | ||
1c9f675f ESR |
162 | template<unsigned _Base, char _Dig, char... _Digs> |
163 | struct _Power_help | |
164 | { | |
a2f4bd2a ESR |
165 | using __next = typename _Power_help<_Base, _Digs...>::type; |
166 | using __valid_digit = typename _Digit<_Base, _Dig>::__valid; | |
167 | using type | |
168 | = __ull_constant<__next::value * (__valid_digit{} ? _Base : 1ULL)>; | |
1c9f675f ESR |
169 | }; |
170 | ||
171 | template<unsigned _Base, char _Dig> | |
172 | struct _Power_help<_Base, _Dig> | |
173 | { | |
a2f4bd2a ESR |
174 | using __valid_digit = typename _Digit<_Base, _Dig>::__valid; |
175 | using type = __ull_constant<__valid_digit::value>; | |
1c9f675f ESR |
176 | }; |
177 | ||
178 | template<unsigned _Base, char... _Digs> | |
a2f4bd2a ESR |
179 | struct _Power : _Power_help<_Base, _Digs...>::type |
180 | { }; | |
1c9f675f ESR |
181 | |
182 | template<unsigned _Base> | |
a2f4bd2a ESR |
183 | struct _Power<_Base> : __ull_constant<0> |
184 | { }; | |
1c9f675f ESR |
185 | |
186 | //------------------------------------------------------------------------------ | |
187 | ||
a2f4bd2a | 188 | template<unsigned _Base, unsigned long long _Pow, char _Dig, char... _Digs> |
1c9f675f ESR |
189 | struct _Number_help |
190 | { | |
a2f4bd2a ESR |
191 | using __digit = _Digit<_Base, _Dig>; |
192 | using __valid_digit = typename __digit::__valid; | |
193 | using __next = _Number_help<_Base, | |
83387bbd | 194 | __valid_digit::value ? _Pow / _Base : _Pow, |
a2f4bd2a ESR |
195 | _Digs...>; |
196 | using type = __ull_constant<_Pow * __digit::value + __next::type::value>; | |
83387bbd JW |
197 | static_assert((type::value / _Pow) == __digit::value, |
198 | "integer literal does not fit in unsigned long long"); | |
1c9f675f ESR |
199 | }; |
200 | ||
2f03003d JW |
201 | // Skip past digit separators: |
202 | template<unsigned _Base, unsigned long long _Pow, char _Dig, char..._Digs> | |
203 | struct _Number_help<_Base, _Pow, '\'', _Dig, _Digs...> | |
204 | : _Number_help<_Base, _Pow, _Dig, _Digs...> | |
205 | { }; | |
206 | ||
207 | // Terminating case for recursion: | |
208 | template<unsigned _Base, char _Dig> | |
209 | struct _Number_help<_Base, 1ULL, _Dig> | |
1c9f675f | 210 | { |
a2f4bd2a | 211 | using type = __ull_constant<_Digit<_Base, _Dig>::value>; |
1c9f675f ESR |
212 | }; |
213 | ||
214 | template<unsigned _Base, char... _Digs> | |
215 | struct _Number | |
a2f4bd2a ESR |
216 | : _Number_help<_Base, _Power<_Base, _Digs...>::value, _Digs...>::type |
217 | { }; | |
1c9f675f ESR |
218 | |
219 | template<unsigned _Base> | |
220 | struct _Number<_Base> | |
a2f4bd2a ESR |
221 | : __ull_constant<0> |
222 | { }; | |
1c9f675f ESR |
223 | |
224 | //------------------------------------------------------------------------------ | |
1c9f675f ESR |
225 | |
226 | template<char... _Digs> | |
227 | struct _Parse_int; | |
228 | ||
229 | template<char... _Digs> | |
230 | struct _Parse_int<'0', 'b', _Digs...> | |
a2f4bd2a ESR |
231 | : _Number<2U, _Digs...>::type |
232 | { }; | |
1c9f675f ESR |
233 | |
234 | template<char... _Digs> | |
235 | struct _Parse_int<'0', 'B', _Digs...> | |
a2f4bd2a ESR |
236 | : _Number<2U, _Digs...>::type |
237 | { }; | |
1c9f675f ESR |
238 | |
239 | template<char... _Digs> | |
240 | struct _Parse_int<'0', 'x', _Digs...> | |
a2f4bd2a ESR |
241 | : _Number<16U, _Digs...>::type |
242 | { }; | |
1c9f675f ESR |
243 | |
244 | template<char... _Digs> | |
245 | struct _Parse_int<'0', 'X', _Digs...> | |
a2f4bd2a ESR |
246 | : _Number<16U, _Digs...>::type |
247 | { }; | |
1c9f675f ESR |
248 | |
249 | template<char... _Digs> | |
250 | struct _Parse_int<'0', _Digs...> | |
a2f4bd2a ESR |
251 | : _Number<8U, _Digs...>::type |
252 | { }; | |
1c9f675f ESR |
253 | |
254 | template<char... _Digs> | |
255 | struct _Parse_int | |
a2f4bd2a ESR |
256 | : _Number<10U, _Digs...>::type |
257 | { }; | |
1c9f675f ESR |
258 | |
259 | } // namespace __parse_int | |
260 | ||
261 | ||
a2f4bd2a ESR |
262 | namespace __select_int |
263 | { | |
1c9f675f ESR |
264 | template<unsigned long long _Val, typename... _Ints> |
265 | struct _Select_int_base; | |
266 | ||
267 | template<unsigned long long _Val, typename _IntType, typename... _Ints> | |
268 | struct _Select_int_base<_Val, _IntType, _Ints...> | |
a09bb4a8 JW |
269 | : __conditional_t<(_Val <= __gnu_cxx::__int_traits<_IntType>::__max), |
270 | integral_constant<_IntType, (_IntType)_Val>, | |
271 | _Select_int_base<_Val, _Ints...>> | |
1c9f675f ESR |
272 | { }; |
273 | ||
274 | template<unsigned long long _Val> | |
a2f4bd2a | 275 | struct _Select_int_base<_Val> |
1c9f675f ESR |
276 | { }; |
277 | ||
278 | template<char... _Digs> | |
a2f4bd2a | 279 | using _Select_int = typename _Select_int_base< |
1c9f675f ESR |
280 | __parse_int::_Parse_int<_Digs...>::value, |
281 | unsigned char, | |
282 | unsigned short, | |
283 | unsigned int, | |
284 | unsigned long, | |
285 | unsigned long long | |
a2f4bd2a | 286 | >::type; |
1c9f675f ESR |
287 | |
288 | } // namespace __select_int | |
289 | ||
290 | _GLIBCXX_END_NAMESPACE_VERSION | |
291 | } // namespace std | |
292 | ||
c03b53da | 293 | #endif // C++14 |
1c9f675f | 294 | |
a2f4bd2a | 295 | #endif // _GLIBCXX_PARSE_NUMBERS_H |