1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___LOCALE_DIR_NUM_H
10#define _LIBCPP___LOCALE_DIR_NUM_H
11
12#include <__algorithm/copy.h>
13#include <__algorithm/find.h>
14#include <__algorithm/reverse.h>
15#include <__algorithm/simd_utils.h>
16#include <__charconv/to_chars_integral.h>
17#include <__charconv/traits.h>
18#include <__config>
19#include <__iterator/istreambuf_iterator.h>
20#include <__iterator/ostreambuf_iterator.h>
21#include <__locale_dir/check_grouping.h>
22#include <__locale_dir/ctype.h>
23#include <__locale_dir/locale.h>
24#include <__locale_dir/pad_and_output.h>
25#include <__locale_dir/scan_keyword.h>
26#include <__memory/unique_ptr.h>
27#include <__system_error/errc.h>
28#include <__type_traits/is_signed.h>
29#include <__utility/checked_int.h>
30#include <cerrno>
31#include <ios>
32#include <streambuf>
33
34#if _LIBCPP_HAS_LOCALIZATION
35
36# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
37# pragma GCC system_header
38# endif
39
40// TODO: Properly qualify calls now that the locale base API defines functions instead of macros
41// NOLINTBEGIN(libcpp-robust-against-adl)
42
43_LIBCPP_PUSH_MACROS
44# include <__undef_macros>
45
46_LIBCPP_BEGIN_NAMESPACE_STD
47_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
48
49// template <class charT> class numpunct
50
51template <class _CharT>
52class numpunct;
53
54template <>
55class _LIBCPP_EXPORTED_FROM_ABI numpunct<char> : public locale::facet {
56public:
57 typedef char char_type;
58 typedef basic_string<char_type> string_type;
59
60 explicit numpunct(size_t __refs = 0);
61
62 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
63 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
64 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
65 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type truename() const { return do_truename(); }
66 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type falsename() const { return do_falsename(); }
67
68 static locale::id id;
69
70protected:
71 ~numpunct() override;
72 virtual char_type do_decimal_point() const;
73 virtual char_type do_thousands_sep() const;
74 virtual string do_grouping() const;
75 virtual string_type do_truename() const;
76 virtual string_type do_falsename() const;
77
78 char_type __decimal_point_;
79 char_type __thousands_sep_;
80 string __grouping_;
81};
82
83# if _LIBCPP_HAS_WIDE_CHARACTERS
84template <>
85class _LIBCPP_EXPORTED_FROM_ABI numpunct<wchar_t> : public locale::facet {
86public:
87 typedef wchar_t char_type;
88 typedef basic_string<char_type> string_type;
89
90 explicit numpunct(size_t __refs = 0);
91
92 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type decimal_point() const { return do_decimal_point(); }
93 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI char_type thousands_sep() const { return do_thousands_sep(); }
94 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string grouping() const { return do_grouping(); }
95 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type truename() const { return do_truename(); }
96 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI string_type falsename() const { return do_falsename(); }
97
98 static locale::id id;
99
100protected:
101 ~numpunct() override;
102 virtual char_type do_decimal_point() const;
103 virtual char_type do_thousands_sep() const;
104 virtual string do_grouping() const;
105 virtual string_type do_truename() const;
106 virtual string_type do_falsename() const;
107
108 char_type __decimal_point_;
109 char_type __thousands_sep_;
110 string __grouping_;
111};
112# endif // _LIBCPP_HAS_WIDE_CHARACTERS
113
114// template <class charT> class numpunct_byname
115
116template <class _CharT>
117class numpunct_byname;
118
119template <>
120class _LIBCPP_EXPORTED_FROM_ABI numpunct_byname<char> : public numpunct<char> {
121public:
122 typedef char char_type;
123 typedef basic_string<char_type> string_type;
124
125 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
126 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
127
128protected:
129 ~numpunct_byname() override;
130};
131
132# if _LIBCPP_HAS_WIDE_CHARACTERS
133template <>
134class _LIBCPP_EXPORTED_FROM_ABI numpunct_byname<wchar_t> : public numpunct<wchar_t> {
135public:
136 typedef wchar_t char_type;
137 typedef basic_string<char_type> string_type;
138
139 explicit numpunct_byname(const char* __nm, size_t __refs = 0);
140 explicit numpunct_byname(const string& __nm, size_t __refs = 0);
141
142protected:
143 ~numpunct_byname() override;
144};
145# endif // _LIBCPP_HAS_WIDE_CHARACTERS
146
147struct _LIBCPP_EXPORTED_FROM_ABI __num_get_base {
148 static const int __num_get_buf_sz = 40;
149
150 static int __get_base(ios_base&);
151 static const char __src[33]; // "0123456789abcdefABCDEFxX+-pPiInN"
152 // count of leading characters in __src used for parsing integers ("012..X+-")
153 static inline const size_t __int_chr_cnt = 26;
154 // count of leading characters in __src used for parsing floating-point values ("012..-pP")
155 static inline const size_t __fp_chr_cnt = 28;
156};
157
158template <class _CharT>
159struct __num_get : protected __num_get_base {
160 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point, _CharT& __thousands_sep);
161
162 static int __stage2_float_loop(
163 _CharT __ct,
164 bool& __in_units,
165 char& __exp,
166 char* __a,
167 char*& __a_end,
168 _CharT __decimal_point,
169 _CharT __thousands_sep,
170 const string& __grouping,
171 unsigned* __g,
172 unsigned*& __g_end,
173 unsigned& __dc,
174 _CharT* __atoms);
175
176 [[__deprecated__("This exists only for ABI compatibility")]] static string
177 __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
178
179 [[__deprecated__("This exists only for ABI compatibility")]] static int __stage2_int_loop(
180 _CharT __ct,
181 int __base,
182 char* __a,
183 char*& __a_end,
184 unsigned& __dc,
185 _CharT __thousands_sep,
186 const string& __grouping,
187 unsigned* __g,
188 unsigned*& __g_end,
189 _CharT* __atoms);
190
191 _LIBCPP_HIDE_FROM_ABI static ptrdiff_t __atoms_offset(const _CharT* __atoms, _CharT __val) {
192 // TODO: Remove the manual vectorization once https://llvm.org/PR168551 is resolved
193# if _LIBCPP_HAS_ALGORITHM_VECTOR_UTILS
194 if constexpr (is_same<_CharT, char>::value) {
195 // TODO(LLVM 24): This can be removed, since -Wpsabi doesn't warn on [[gnu::always_inline]] functions anymore.
196 _LIBCPP_DIAGNOSTIC_PUSH
197 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wpsabi")
198 using __vec = __simd_vector<char, 32>;
199 __vec __cmp = std::__partial_load<__vec, __int_chr_cnt>(__atoms);
200 auto __res = __vec(__val) == __cmp;
201 if (std::__none_of(vec: __res))
202 return __int_chr_cnt;
203 return std::min(a: __int_chr_cnt, b: std::__find_first_set(vec: __res));
204 _LIBCPP_DIAGNOSTIC_POP
205 }
206# endif
207 return std::find(__atoms, __atoms + __int_chr_cnt, __val) - __atoms;
208 }
209
210 _LIBCPP_HIDE_FROM_ABI const _CharT* __do_widen(ios_base& __iob, _CharT* __atoms) const {
211 return __do_widen_p(__iob, __atoms);
212 }
213
214private:
215 template <typename _Tp>
216 _LIBCPP_HIDE_FROM_ABI const _Tp* __do_widen_p(ios_base& __iob, _Tp* __atoms) const {
217 locale __loc = __iob.getloc();
218 use_facet<ctype<_Tp> >(__loc).widen(__src, __src + __int_chr_cnt, __atoms);
219 return __atoms;
220 }
221
222 _LIBCPP_HIDE_FROM_ABI const char* __do_widen_p(ios_base& __iob, char* __atoms) const {
223 (void)__iob;
224 (void)__atoms;
225 return __src;
226 }
227};
228
229template <class _CharT>
230string __num_get<_CharT>::__stage2_float_prep(
231 ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point, _CharT& __thousands_sep) {
232 locale __loc = __iob.getloc();
233 std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + __fp_chr_cnt, __atoms);
234 const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
235 __decimal_point = __np.decimal_point();
236 __thousands_sep = __np.thousands_sep();
237 return __np.grouping();
238}
239
240template <class _CharT>
241int __num_get<_CharT>::__stage2_float_loop(
242 _CharT __ct,
243 bool& __in_units,
244 char& __exp,
245 char* __a,
246 char*& __a_end,
247 _CharT __decimal_point,
248 _CharT __thousands_sep,
249 const string& __grouping,
250 unsigned* __g,
251 unsigned*& __g_end,
252 unsigned& __dc,
253 _CharT* __atoms) {
254 if (__ct == __decimal_point) {
255 if (!__in_units)
256 return -1;
257 __in_units = false;
258 *__a_end++ = '.';
259 if (__grouping.size() != 0 && __g_end - __g < __num_get_buf_sz)
260 *__g_end++ = __dc;
261 return 0;
262 }
263 if (__ct == __thousands_sep && __grouping.size() != 0) {
264 if (!__in_units)
265 return -1;
266 if (__g_end - __g < __num_get_buf_sz) {
267 *__g_end++ = __dc;
268 __dc = 0;
269 }
270 return 0;
271 }
272 ptrdiff_t __f = std::find(__atoms, __atoms + __num_get_base::__fp_chr_cnt, __ct) - __atoms;
273 if (__f >= static_cast<ptrdiff_t>(__num_get_base::__fp_chr_cnt))
274 return -1;
275 char __x = __src[__f];
276 if (__x == '-' || __x == '+') {
277 if (__a_end == __a || (std::toupper(c: __a_end[-1]) == std::toupper(c: __exp))) {
278 *__a_end++ = __x;
279 return 0;
280 }
281 return -1;
282 }
283 if (__x == 'x' || __x == 'X')
284 __exp = 'P';
285 else if (std::toupper(c: __x) == __exp) {
286 __exp = std::tolower(c: __exp);
287 if (__in_units) {
288 __in_units = false;
289 if (__grouping.size() != 0 && __g_end - __g < __num_get_buf_sz)
290 *__g_end++ = __dc;
291 }
292 }
293 *__a_end++ = __x;
294 if (__f >= 22)
295 return 0;
296 ++__dc;
297 return 0;
298}
299
300extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>;
301# if _LIBCPP_HAS_WIDE_CHARACTERS
302extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>;
303# endif
304
305template <class _Tp>
306_LIBCPP_HIDE_FROM_ABI _Tp __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err) {
307 if (__a != __a_end) {
308 __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
309 errno = 0;
310 char* __p2;
311 _Tp __ld = __locale::__str_to_float_c_locale<_Tp>(__a, &__p2);
312 __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
313 if (__current_errno == 0)
314 errno = __save_errno;
315 if (__p2 != __a_end) {
316 __err = ios_base::failbit;
317 return 0;
318 } else if (__current_errno == ERANGE)
319 __err = ios_base::failbit;
320 return __ld;
321 }
322 __err = ios_base::failbit;
323 return 0;
324}
325
326template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
327class num_get : public locale::facet, private __num_get<_CharT> {
328public:
329 typedef _CharT char_type;
330 typedef _InputIterator iter_type;
331
332 _LIBCPP_HIDE_FROM_ABI explicit num_get(size_t __refs = 0) : locale::facet(__refs) {}
333
334 _LIBCPP_HIDE_FROM_ABI iter_type
335 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const {
336 return do_get(__b, __e, __iob, __err, __v);
337 }
338
339 _LIBCPP_HIDE_FROM_ABI iter_type
340 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long& __v) const {
341 return do_get(__b, __e, __iob, __err, __v);
342 }
343
344 _LIBCPP_HIDE_FROM_ABI iter_type
345 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long long& __v) const {
346 return do_get(__b, __e, __iob, __err, __v);
347 }
348
349 _LIBCPP_HIDE_FROM_ABI iter_type
350 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned short& __v) const {
351 return do_get(__b, __e, __iob, __err, __v);
352 }
353
354 _LIBCPP_HIDE_FROM_ABI iter_type
355 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned int& __v) const {
356 return do_get(__b, __e, __iob, __err, __v);
357 }
358
359 _LIBCPP_HIDE_FROM_ABI iter_type
360 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long& __v) const {
361 return do_get(__b, __e, __iob, __err, __v);
362 }
363
364 _LIBCPP_HIDE_FROM_ABI iter_type
365 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long long& __v) const {
366 return do_get(__b, __e, __iob, __err, __v);
367 }
368
369 _LIBCPP_HIDE_FROM_ABI iter_type
370 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, float& __v) const {
371 return do_get(__b, __e, __iob, __err, __v);
372 }
373
374 _LIBCPP_HIDE_FROM_ABI iter_type
375 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, double& __v) const {
376 return do_get(__b, __e, __iob, __err, __v);
377 }
378
379 _LIBCPP_HIDE_FROM_ABI iter_type
380 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
381 return do_get(__b, __e, __iob, __err, __v);
382 }
383
384 _LIBCPP_HIDE_FROM_ABI iter_type
385 get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const {
386 return do_get(__b, __e, __iob, __err, __v);
387 }
388
389 static locale::id id;
390
391protected:
392 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_get() override {}
393
394 template <class _Fp>
395 _LIBCPP_HIDE_FROM_ABI iter_type
396 __do_get_floating_point(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, _Fp& __v) const {
397 // Stage 1, nothing to do
398 // Stage 2
399 char_type __atoms[__num_get_base::__fp_chr_cnt];
400 char_type __decimal_point;
401 char_type __thousands_sep;
402 string __grouping = this->__stage2_float_prep(__iob, __atoms, __decimal_point, __thousands_sep);
403 string __buf;
404 __buf.resize(n: __buf.capacity());
405 char* __a = &__buf[0];
406 char* __a_end = __a;
407 unsigned __g[__num_get_base::__num_get_buf_sz];
408 unsigned* __g_end = __g;
409 unsigned __dc = 0;
410 bool __in_units = true;
411 char __exp = 'E';
412 bool __is_leading_parsed = false;
413 for (; __b != __e; ++__b) {
414 if (__a_end == __a + __buf.size()) {
415 size_t __tmp = __buf.size();
416 __buf.resize(n: 2 * __buf.size());
417 __buf.resize(n: __buf.capacity());
418 __a = &__buf[0];
419 __a_end = __a + __tmp;
420 }
421 if (this->__stage2_float_loop(
422 *__b,
423 __in_units,
424 __exp,
425 __a,
426 __a_end,
427 __decimal_point,
428 __thousands_sep,
429 __grouping,
430 __g,
431 __g_end,
432 __dc,
433 __atoms))
434 break;
435
436 // the leading character excluding the sign must be a decimal digit
437 if (!__is_leading_parsed) {
438 if (__a_end - __a >= 1 && __a[0] != '-' && __a[0] != '+') {
439 if (('0' <= __a[0] && __a[0] <= '9') || __a[0] == '.')
440 __is_leading_parsed = true;
441 else
442 break;
443 } else if (__a_end - __a >= 2 && (__a[0] == '-' || __a[0] == '+')) {
444 if (('0' <= __a[1] && __a[1] <= '9') || __a[1] == '.')
445 __is_leading_parsed = true;
446 else
447 break;
448 }
449 }
450 }
451 if (__grouping.size() != 0 && __in_units && __g_end - __g < __num_get_base::__num_get_buf_sz)
452 *__g_end++ = __dc;
453 // Stage 3
454 __v = std::__num_get_float<_Fp>(__a, __a_end, __err);
455 // Digit grouping checked
456 __check_grouping(__grouping, __g, __g_end, __err);
457 // EOF checked
458 if (__b == __e)
459 __err |= ios_base::eofbit;
460 return __b;
461 }
462
463 template <class _MaybeSigned>
464 iter_type __do_get_integral(
465 iter_type __first, iter_type __last, ios_base& __iob, ios_base::iostate& __err, _MaybeSigned& __v) const {
466 using _Unsigned = __make_unsigned_t<_MaybeSigned>;
467
468 // Stage 1
469 unsigned char __base = this->__get_base(__iob);
470
471 // Stages 2 & 3
472 // These are combined into a single step where we parse the characters and calculate the value in one go instead of
473 // storing the relevant characters first (in an allocated buffer) and parse the characters after we extracted them.
474 // This makes the whole process significantly faster, since we avoid potential allocations and copies.
475
476 const auto& __numpunct = use_facet<numpunct<_CharT> >(__iob.getloc());
477 char_type __thousands_sep = __numpunct.thousands_sep();
478 string __grouping = __numpunct.grouping();
479
480 char_type __atoms_buffer[__num_get_base::__int_chr_cnt];
481 const char_type* __atoms = this->__do_widen(__iob, __atoms_buffer);
482 unsigned __g[__num_get_base::__num_get_buf_sz];
483 unsigned* __g_end = __g;
484 unsigned __dc = 0;
485
486 if (__first == __last) {
487 __err |= ios_base::eofbit | ios_base::failbit;
488 __v = 0;
489 return __first;
490 }
491
492 while (!__grouping.empty() && *__first == __thousands_sep) {
493 ++__first;
494 if (__g_end - __g < this->__num_get_buf_sz)
495 *__g_end++ = 0;
496 }
497
498 bool __negate = false;
499 // __c == '+' || __c == '-'
500 if (auto __c = *__first; __c == __atoms[24] || __c == __atoms[25]) {
501 __negate = __c == __atoms[25];
502 ++__first;
503 }
504
505 if (__first == __last) {
506 __err |= ios_base::eofbit | ios_base::failbit;
507 __v = 0;
508 return __first;
509 }
510
511 bool __parsed_num = false;
512
513 // If we don't have a pre-set base, figure it out and swallow any prefix
514 if (__base == 0) {
515 auto __c = *__first;
516 // __c == '0'
517 if (__c == __atoms[0]) {
518 ++__first;
519 if (__first == __last) {
520 __err |= ios_base::eofbit;
521 __v = 0;
522 return __first;
523 }
524 // __c2 == 'x' || __c2 == 'X'
525 if (auto __c2 = *__first; __c2 == __atoms[22] || __c2 == __atoms[23]) {
526 __base = 16;
527 ++__first;
528 } else {
529 __base = 8;
530 __parsed_num = true; // We only swallowed '0', so we've started to parse a number
531 }
532 } else {
533 __base = 10;
534 }
535
536 // If the base has been specified explicitly, try to swallow the appropriate prefix. We only need to do something
537 // special for hex, since decimal has no prefix and octal's prefix is '0', which doesn't change the value that
538 // we'll parse if we don't swallow it.
539 } else if (__base == 16) {
540 // Try to swallow '0x'
541
542 // *__first == '0'
543 if (*__first == __atoms[0]) {
544 ++__first;
545 if (__first == __last) {
546 __err |= ios_base::eofbit;
547 __v = 0;
548 return __first;
549 }
550 // __c == 'x' || __c == 'X'
551 if (auto __c = *__first; __c == __atoms[22] || __c == __atoms[23])
552 ++__first;
553 else
554 __parsed_num = true; // We only swallowed '0', so we've started to parse a number
555 }
556 }
557
558 // Calculate the actual number
559 __checked_int<_Unsigned> __val(0);
560 for (; __first != __last; ++__first) {
561 auto __c = *__first;
562 if (!__grouping.empty() && __c == __thousands_sep) {
563 if (__g_end - __g < this->__num_get_buf_sz) {
564 *__g_end++ = __dc;
565 __dc = 0;
566 }
567 continue;
568 }
569 auto __offset = this->__atoms_offset(__atoms, __c);
570 if (__offset >= 22) // Not a valid integer character
571 break;
572
573 if (__base == 16 && __offset >= 16)
574 __offset -= 6;
575 if (__offset >= __base)
576 break;
577 __val = (__val * __base) + static_cast<unsigned short>(__offset);
578 __parsed_num = true;
579 ++__dc;
580 }
581
582 if (!__parsed_num) {
583 __err |= ios_base::failbit;
584 __v = 0;
585 } else if (__val.__overflowed()) {
586 __err |= ios_base::failbit;
587 __v = is_signed<_MaybeSigned>::value && __negate
588 ? numeric_limits<_MaybeSigned>::min()
589 : numeric_limits<_MaybeSigned>::max();
590 } else if (auto __value = __val.__value(); !__negate) {
591 if (__value > static_cast<_Unsigned>(numeric_limits<_MaybeSigned>::max())) {
592 __err |= ios_base::failbit;
593 __v = numeric_limits<_MaybeSigned>::max();
594 } else {
595 __v = __value;
596 }
597 } else if (is_signed<_MaybeSigned>::value) {
598 if (__value > static_cast<_Unsigned>(numeric_limits<_MaybeSigned>::max()) + 1) {
599 __err |= ios_base::failbit;
600 __v = numeric_limits<_MaybeSigned>::min();
601 } else if (__value == static_cast<_Unsigned>(numeric_limits<_MaybeSigned>::max()) + 1) {
602 __v = numeric_limits<_MaybeSigned>::min();
603 } else {
604 __v = -__value;
605 }
606 } else {
607 __v = -__value;
608 }
609
610 if (__grouping.size() != 0 && __g_end - __g < __num_get_base::__num_get_buf_sz)
611 *__g_end++ = __dc;
612
613 // Digit grouping checked
614 __check_grouping(__grouping, __g, __g_end, __err);
615 // EOF checked
616 if (__first == __last)
617 __err |= ios_base::eofbit;
618 return __first;
619 }
620
621 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const;
622
623 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long& __v) const {
624 return this->__do_get_integral(__b, __e, __iob, __err, __v);
625 }
626
627 virtual iter_type
628 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long long& __v) const {
629 return this->__do_get_integral(__b, __e, __iob, __err, __v);
630 }
631
632 virtual iter_type
633 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned short& __v) const {
634 return this->__do_get_integral(__b, __e, __iob, __err, __v);
635 }
636
637 virtual iter_type
638 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned int& __v) const {
639 return this->__do_get_integral(__b, __e, __iob, __err, __v);
640 }
641
642 virtual iter_type
643 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long& __v) const {
644 return this->__do_get_integral(__b, __e, __iob, __err, __v);
645 }
646
647 virtual iter_type
648 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, unsigned long long& __v) const {
649 return this->__do_get_integral(__b, __e, __iob, __err, __v);
650 }
651
652 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, float& __v) const {
653 return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
654 }
655
656 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, double& __v) const {
657 return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
658 }
659
660 virtual iter_type
661 do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, long double& __v) const {
662 return this->__do_get_floating_point(__b, __e, __iob, __err, __v);
663 }
664
665 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const;
666};
667
668template <class _CharT, class _InputIterator>
669locale::id num_get<_CharT, _InputIterator>::id;
670
671template <class _CharT, class _InputIterator>
672_InputIterator num_get<_CharT, _InputIterator>::do_get(
673 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, bool& __v) const {
674 if ((__iob.flags() & ios_base::boolalpha) == 0) {
675 long __lv = -1;
676 __b = do_get(__b, __e, __iob, __err, __lv);
677 switch (__lv) {
678 case 0:
679 __v = false;
680 break;
681 case 1:
682 __v = true;
683 break;
684 default:
685 __v = true;
686 __err = ios_base::failbit;
687 break;
688 }
689 return __b;
690 }
691 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__iob.getloc());
692 const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__iob.getloc());
693 typedef typename numpunct<_CharT>::string_type string_type;
694 const string_type __names[2] = {__np.truename(), __np.falsename()};
695 const string_type* __i = std::__scan_keyword(__b, __e, __names, __names + 2, __ct, __err);
696 __v = __i == __names;
697 return __b;
698}
699
700template <class _CharT, class _InputIterator>
701_InputIterator num_get<_CharT, _InputIterator>::do_get(
702 iter_type __b, iter_type __e, ios_base& __iob, ios_base::iostate& __err, void*& __v) const {
703 auto __flags = __iob.flags();
704 __iob.flags(fmtfl: (__flags & ~ios_base::basefield & ~ios_base::uppercase) | ios_base::hex);
705 uintptr_t __ptr;
706 auto __res = __do_get_integral(__b, __e, __iob, __err, __ptr);
707 __iob.flags(fmtfl: __flags);
708 __v = reinterpret_cast<void*>(__ptr);
709 return __res;
710}
711
712extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>;
713# if _LIBCPP_HAS_WIDE_CHARACTERS
714extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>;
715# endif
716
717struct _LIBCPP_EXPORTED_FROM_ABI __num_put_base {
718protected:
719 static void __format_int(char* __fmt, const char* __len, bool __signd, ios_base::fmtflags __flags);
720 static bool __format_float(char* __fmt, const char* __len, ios_base::fmtflags __flags);
721 static char* __identify_padding(char* __nb, char* __ne, const ios_base& __iob);
722};
723
724template <class _CharT>
725struct __num_put : protected __num_put_base {
726 static void __widen_and_group_int(
727 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc);
728 static void __widen_and_group_float(
729 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc);
730};
731
732template <class _CharT>
733void __num_put<_CharT>::__widen_and_group_int(
734 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc) {
735 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__loc);
736 const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
737 string __grouping = __npt.grouping();
738 if (__grouping.empty()) {
739 __ct.widen(__nb, __ne, __ob);
740 __oe = __ob + (__ne - __nb);
741 } else {
742 __oe = __ob;
743 char* __nf = __nb;
744 if (*__nf == '-' || *__nf == '+')
745 *__oe++ = __ct.widen(*__nf++);
746 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' || __nf[1] == 'X')) {
747 *__oe++ = __ct.widen(*__nf++);
748 *__oe++ = __ct.widen(*__nf++);
749 }
750 std::reverse(first: __nf, last: __ne);
751 _CharT __thousands_sep = __npt.thousands_sep();
752 unsigned __dc = 0;
753 unsigned __dg = 0;
754 for (char* __p = __nf; __p < __ne; ++__p) {
755 if (static_cast<unsigned>(__grouping[__dg]) > 0 && __dc == static_cast<unsigned>(__grouping[__dg])) {
756 *__oe++ = __thousands_sep;
757 __dc = 0;
758 if (__dg < __grouping.size() - 1)
759 ++__dg;
760 }
761 *__oe++ = __ct.widen(*__p);
762 ++__dc;
763 }
764 std::reverse(__ob + (__nf - __nb), __oe);
765 }
766 if (__np == __ne)
767 __op = __oe;
768 else
769 __op = __ob + (__np - __nb);
770}
771
772_LIBCPP_HIDE_FROM_ABI inline bool __isdigit(char __c) { return __c >= '0' && __c <= '9'; }
773
774_LIBCPP_HIDE_FROM_ABI inline bool __isxdigit(char __c) {
775 auto __lower = __c | 0x20;
776 return std::__isdigit(__c) || (__lower >= 'a' && __lower <= 'f');
777}
778
779template <class _CharT>
780void __num_put<_CharT>::__widen_and_group_float(
781 char* __nb, char* __np, char* __ne, _CharT* __ob, _CharT*& __op, _CharT*& __oe, const locale& __loc) {
782 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__loc);
783 const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
784 string __grouping = __npt.grouping();
785 __oe = __ob;
786 char* __nf = __nb;
787 if (*__nf == '-' || *__nf == '+')
788 *__oe++ = __ct.widen(*__nf++);
789 char* __ns;
790 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' || __nf[1] == 'X')) {
791 *__oe++ = __ct.widen(*__nf++);
792 *__oe++ = __ct.widen(*__nf++);
793 for (__ns = __nf; __ns < __ne; ++__ns)
794 if (!std::__isxdigit(c: *__ns))
795 break;
796 } else {
797 for (__ns = __nf; __ns < __ne; ++__ns)
798 if (!std::__isdigit(c: *__ns))
799 break;
800 }
801 if (__grouping.empty()) {
802 __ct.widen(__nf, __ns, __oe);
803 __oe += __ns - __nf;
804 } else {
805 std::reverse(first: __nf, last: __ns);
806 _CharT __thousands_sep = __npt.thousands_sep();
807 unsigned __dc = 0;
808 unsigned __dg = 0;
809 for (char* __p = __nf; __p < __ns; ++__p) {
810 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg])) {
811 *__oe++ = __thousands_sep;
812 __dc = 0;
813 if (__dg < __grouping.size() - 1)
814 ++__dg;
815 }
816 *__oe++ = __ct.widen(*__p);
817 ++__dc;
818 }
819 std::reverse(__ob + (__nf - __nb), __oe);
820 }
821 for (__nf = __ns; __nf < __ne; ++__nf) {
822 if (*__nf == '.') {
823 *__oe++ = __npt.decimal_point();
824 ++__nf;
825 break;
826 } else
827 *__oe++ = __ct.widen(*__nf);
828 }
829 __ct.widen(__nf, __ne, __oe);
830 __oe += __ne - __nf;
831 if (__np == __ne)
832 __op = __oe;
833 else
834 __op = __ob + (__np - __nb);
835}
836
837extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>;
838# if _LIBCPP_HAS_WIDE_CHARACTERS
839extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>;
840# endif
841
842template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
843class num_put : public locale::facet, private __num_put<_CharT> {
844public:
845 typedef _CharT char_type;
846 typedef _OutputIterator iter_type;
847
848 _LIBCPP_HIDE_FROM_ABI explicit num_put(size_t __refs = 0) : locale::facet(__refs) {}
849
850 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {
851 return do_put(__s, __iob, __fl, __v);
852 }
853
854 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const {
855 return do_put(__s, __iob, __fl, __v);
856 }
857
858 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const {
859 return do_put(__s, __iob, __fl, __v);
860 }
861
862 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long __v) const {
863 return do_put(__s, __iob, __fl, __v);
864 }
865
866 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long __v) const {
867 return do_put(__s, __iob, __fl, __v);
868 }
869
870 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const {
871 return do_put(__s, __iob, __fl, __v);
872 }
873
874 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const {
875 return do_put(__s, __iob, __fl, __v);
876 }
877
878 _LIBCPP_HIDE_FROM_ABI iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const {
879 return do_put(__s, __iob, __fl, __v);
880 }
881
882 static locale::id id;
883
884protected:
885 _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_put() override {}
886
887 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const;
888 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const;
889 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const;
890 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long) const;
891 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long) const;
892 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const;
893 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const;
894 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const;
895
896 template <class _Integral>
897 _LIBCPP_HIDE_FROM_ABI inline _OutputIterator
898 __do_put_integral(iter_type __s, ios_base& __iob, char_type __fl, _Integral __v) const;
899
900 template <class _Float>
901 _LIBCPP_HIDE_FROM_ABI inline _OutputIterator
902 __do_put_floating_point(iter_type __s, ios_base& __iob, char_type __fl, _Float __v, char const* __len) const;
903};
904
905template <class _CharT, class _OutputIterator>
906locale::id num_put<_CharT, _OutputIterator>::id;
907
908template <class _CharT, class _OutputIterator>
909_OutputIterator
910num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {
911 if ((__iob.flags() & ios_base::boolalpha) == 0)
912 return do_put(__s, __iob, __fl, (unsigned long)__v);
913 const numpunct<char_type>& __np = std::use_facet<numpunct<char_type> >(__iob.getloc());
914 typedef typename numpunct<char_type>::string_type string_type;
915 string_type __nm = __v ? __np.truename() : __np.falsename();
916 return std::copy(__nm.begin(), __nm.end(), __s);
917}
918
919template <class _CharT, class _OutputIterator>
920template <class _Integral>
921_LIBCPP_HIDE_FROM_ABI inline _OutputIterator num_put<_CharT, _OutputIterator>::__do_put_integral(
922 iter_type __s, ios_base& __iob, char_type __fl, _Integral __v) const {
923 // Stage 1 - Get number in narrow char
924
925 // Worst case is octal, with showbase enabled. Note that octal is always
926 // printed as an unsigned value.
927 using _Unsigned = typename make_unsigned<_Integral>::type;
928 _LIBCPP_CONSTEXPR const unsigned __buffer_size =
929 (numeric_limits<_Unsigned>::digits / 3) // 1 char per 3 bits
930 + ((numeric_limits<_Unsigned>::digits % 3) != 0) // round up
931 + 2; // base prefix + terminating null character
932
933 char __char_buffer[__buffer_size];
934 char* __buffer_ptr = __char_buffer;
935
936 auto __flags = __iob.flags();
937
938 auto __basefield = (__flags & ios_base::basefield);
939
940 // Extract base
941 int __base = 10;
942 if (__basefield == ios_base::oct)
943 __base = 8;
944 else if (__basefield == ios_base::hex)
945 __base = 16;
946
947 // Print '-' and make the argument unsigned
948 auto __uval = std::__to_unsigned_like(__v);
949 if (__basefield != ios_base::oct && __basefield != ios_base::hex && __v < 0) {
950 *__buffer_ptr++ = '-';
951 __uval = std::__complement(__uval);
952 }
953
954 // Maybe add '+' prefix
955 if (std::is_signed<_Integral>::value && (__flags & ios_base::showpos) && __basefield != ios_base::oct &&
956 __basefield != ios_base::hex && __v >= 0)
957 *__buffer_ptr++ = '+';
958
959 // Add base prefix
960 if (__v != 0 && __flags & ios_base::showbase) {
961 if (__basefield == ios_base::oct) {
962 *__buffer_ptr++ = '0';
963 } else if (__basefield == ios_base::hex) {
964 *__buffer_ptr++ = '0';
965 *__buffer_ptr++ = (__flags & ios_base::uppercase ? 'X' : 'x');
966 }
967 }
968
969 auto __res = std::__to_chars_integral(__buffer_ptr, __char_buffer + __buffer_size, __uval, __base);
970 _LIBCPP_ASSERT_INTERNAL(__res.__ec == std::errc(0), "to_chars: invalid maximum buffer size computed?");
971
972 // Make letters uppercase
973 if (__flags & ios_base::hex && __flags & ios_base::uppercase) {
974 for (; __buffer_ptr != __res.__ptr; ++__buffer_ptr)
975 *__buffer_ptr = std::__hex_to_upper(c: *__buffer_ptr);
976 }
977
978 char* __np = this->__identify_padding(__char_buffer, __res.__ptr, __iob);
979 // Stage 2 - Widen __nar while adding thousands separators
980 char_type __o[2 * (__buffer_size - 1) - 1];
981 char_type* __op; // pad here
982 char_type* __oe; // end of output
983 this->__widen_and_group_int(__char_buffer, __np, __res.__ptr, __o, __op, __oe, __iob.getloc());
984 // [__o, __oe) contains thousands_sep'd wide number
985 // Stage 3 & 4
986 return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
987}
988
989template <class _CharT, class _OutputIterator>
990_OutputIterator
991num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long __v) const {
992 return this->__do_put_integral(__s, __iob, __fl, __v);
993}
994
995template <class _CharT, class _OutputIterator>
996_OutputIterator
997num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long long __v) const {
998 return this->__do_put_integral(__s, __iob, __fl, __v);
999}
1000
1001template <class _CharT, class _OutputIterator>
1002_OutputIterator
1003num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long __v) const {
1004 return this->__do_put_integral(__s, __iob, __fl, __v);
1005}
1006
1007template <class _CharT, class _OutputIterator>
1008_OutputIterator
1009num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, unsigned long long __v) const {
1010 return this->__do_put_integral(__s, __iob, __fl, __v);
1011}
1012
1013template <class _CharT, class _OutputIterator>
1014template <class _Float>
1015_LIBCPP_HIDE_FROM_ABI inline _OutputIterator num_put<_CharT, _OutputIterator>::__do_put_floating_point(
1016 iter_type __s, ios_base& __iob, char_type __fl, _Float __v, char const* __len) const {
1017 // Stage 1 - Get number in narrow char
1018 char __fmt[8] = {'%', 0};
1019 bool __specify_precision = this->__format_float(__fmt + 1, __len, __iob.flags());
1020 const unsigned __nbuf = 30;
1021 char __nar[__nbuf];
1022 char* __nb = __nar;
1023 int __nc;
1024 _LIBCPP_DIAGNOSTIC_PUSH
1025 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1026 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1027 if (__specify_precision)
1028 __nc = __locale::__snprintf(s: __nb, n: __nbuf, loc: __locale::__get_c_locale(), format: __fmt, (int)__iob.precision(), __v);
1029 else
1030 __nc = __locale::__snprintf(s: __nb, n: __nbuf, loc: __locale::__get_c_locale(), format: __fmt, __v);
1031 unique_ptr<char, void (*)(void*)> __nbh(nullptr, free);
1032 if (__nc > static_cast<int>(__nbuf - 1)) {
1033 if (__specify_precision)
1034 __nc = __locale::__asprintf(s: &__nb, loc: __locale::__get_c_locale(), format: __fmt, (int)__iob.precision(), __v);
1035 else
1036 __nc = __locale::__asprintf(s: &__nb, loc: __locale::__get_c_locale(), format: __fmt, __v);
1037 if (__nc == -1)
1038 std::__throw_bad_alloc();
1039 __nbh.reset(p: __nb);
1040 }
1041 _LIBCPP_DIAGNOSTIC_POP
1042 char* __ne = __nb + __nc;
1043 char* __np = this->__identify_padding(__nb, __ne, __iob);
1044 // Stage 2 - Widen __nar while adding thousands separators
1045 char_type __o[2 * (__nbuf - 1) - 1];
1046 char_type* __ob = __o;
1047 unique_ptr<char_type, void (*)(void*)> __obh(0, free);
1048 if (__nb != __nar) {
1049 __ob = (char_type*)malloc(size: 2 * static_cast<size_t>(__nc) * sizeof(char_type));
1050 if (__ob == 0)
1051 std::__throw_bad_alloc();
1052 __obh.reset(__ob);
1053 }
1054 char_type* __op; // pad here
1055 char_type* __oe; // end of output
1056 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1057 // [__o, __oe) contains thousands_sep'd wide number
1058 // Stage 3 & 4
1059 __s = std::__pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1060 return __s;
1061}
1062
1063template <class _CharT, class _OutputIterator>
1064_OutputIterator
1065num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, double __v) const {
1066 return this->__do_put_floating_point(__s, __iob, __fl, __v, "");
1067}
1068
1069template <class _CharT, class _OutputIterator>
1070_OutputIterator
1071num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, long double __v) const {
1072 return this->__do_put_floating_point(__s, __iob, __fl, __v, "L");
1073}
1074
1075template <class _CharT, class _OutputIterator>
1076_OutputIterator
1077num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, const void* __v) const {
1078 auto __flags = __iob.flags();
1079 __iob.flags(fmtfl: (__flags & ~ios_base::basefield & ~ios_base::uppercase) | ios_base::hex | ios_base::showbase);
1080 auto __res = __do_put_integral(__s, __iob, __fl, reinterpret_cast<uintptr_t>(__v));
1081 __iob.flags(fmtfl: __flags);
1082 return __res;
1083}
1084
1085extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>;
1086# if _LIBCPP_HAS_WIDE_CHARACTERS
1087extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>;
1088# endif
1089
1090_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
1091_LIBCPP_END_NAMESPACE_STD
1092
1093_LIBCPP_POP_MACROS
1094
1095// NOLINTEND(libcpp-robust-against-adl)
1096
1097#endif // _LIBCPP_HAS_LOCALIZATION
1098
1099#endif // _LIBCPP___LOCALE_DIR_NUM_H
1100