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