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