29#ifndef _GLIBCXX_FORMAT
30#define _GLIBCXX_FORMAT 1
33#pragma GCC system_header
38#define __glibcxx_want_format
39#define __glibcxx_want_format_ranges
40#define __glibcxx_want_format_uchar
43#ifdef __cpp_lib_format
65#if !__has_builtin(__builtin_toupper)
69#pragma GCC diagnostic push
70#pragma GCC diagnostic ignored "-Wpedantic"
71#pragma GCC diagnostic ignored "-Wc++23-extensions"
73namespace std _GLIBCXX_VISIBILITY(default)
75_GLIBCXX_BEGIN_NAMESPACE_VERSION
78 template<
typename _CharT,
typename... _Args>
struct basic_format_string;
88 template<
typename _CharT>
90 _Widen(
const char* __narrow,
const wchar_t* __wide)
92 if constexpr (is_same_v<_CharT, wchar_t>)
97#define _GLIBCXX_WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
98#define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
101 template<
typename _CharT>
102 constexpr size_t __stackbuf_size = 32 *
sizeof(
void*) /
sizeof(_CharT);
105 template<
typename _CharT>
class _Sink;
106 template<
typename _CharT>
class _Fixedbuf_sink;
107 template<
typename _Seq>
class _Seq_sink;
109 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
111 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
117 template<
typename _CharT>
121 template<
typename _CharT>
123 {
using type = back_insert_iterator<basic_string<_CharT>>; };
125 template<
typename _CharT>
126 using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
128 template<
typename _CharT>
129 struct _Runtime_format_string
131 [[__gnu__::__always_inline__]]
132 _Runtime_format_string(basic_string_view<_CharT> __s) noexcept
135 _Runtime_format_string(
const _Runtime_format_string&) =
delete;
136 void operator=(
const _Runtime_format_string&) =
delete;
139 basic_string_view<_CharT> _M_str;
141 template<
typename,
typename...>
friend struct std::basic_format_string;
147 using format_context = __format::__format_context<char>;
148#ifdef _GLIBCXX_USE_WCHAR_T
149 using wformat_context = __format::__format_context<wchar_t>;
153 template<
typename _Context>
class basic_format_args;
154 using format_args = basic_format_args<format_context>;
155#ifdef _GLIBCXX_USE_WCHAR_T
156 using wformat_args = basic_format_args<wformat_context>;
161 template<
typename _Context>
162 class basic_format_arg;
168 template<
typename _CharT,
typename... _Args>
169 struct basic_format_string
171 template<
typename _Tp>
172 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
174 basic_format_string(
const _Tp& __s);
176 [[__gnu__::__always_inline__]]
177 basic_format_string(__format::_Runtime_format_string<_CharT> __s) noexcept
181 [[__gnu__::__always_inline__]]
182 constexpr basic_string_view<_CharT>
187 basic_string_view<_CharT> _M_str;
190 template<
typename... _Args>
191 using format_string = basic_format_string<char, type_identity_t<_Args>...>;
193#ifdef _GLIBCXX_USE_WCHAR_T
194 template<
typename... _Args>
196 = basic_format_string<wchar_t, type_identity_t<_Args>...>;
199#if __cpp_lib_format >= 202311L
200 [[__gnu__::__always_inline__]]
201 inline __format::_Runtime_format_string<char>
202 runtime_format(string_view __fmt)
noexcept
205#ifdef _GLIBCXX_USE_WCHAR_T
206 [[__gnu__::__always_inline__]]
207 inline __format::_Runtime_format_string<wchar_t>
208 runtime_format(wstring_view __fmt)
noexcept
216 template<
typename _Tp,
typename _CharT>
219 formatter() =
delete;
220 formatter(
const formatter&) =
delete;
221 formatter& operator=(
const formatter&) =
delete;
228 explicit format_error(
const string& __what) : runtime_error(__what) { }
229 explicit format_error(
const char* __what) : runtime_error(__what) { }
235 __throw_format_error(
const char* __what)
236 { _GLIBCXX_THROW_OR_ABORT(format_error(__what)); }
244 __unmatched_left_brace_in_format_string()
245 { __throw_format_error(
"format error: unmatched '{' in format string"); }
249 __unmatched_right_brace_in_format_string()
250 { __throw_format_error(
"format error: unmatched '}' in format string"); }
254 __conflicting_indexing_in_format_string()
255 { __throw_format_error(
"format error: conflicting indexing style in format string"); }
259 __invalid_arg_id_in_format_string()
260 { __throw_format_error(
"format error: invalid arg-id in format string"); }
264 __failed_to_parse_format_spec()
265 { __throw_format_error(
"format error: failed to parse format-spec"); }
267 template<
typename _CharT>
class _Scanner;
269 enum _Arg_t :
unsigned char {
270 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
271 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
272 _Arg_i128, _Arg_u128,
273 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
274#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
276 _Arg_f128 = _Arg_ldbl,
277 _Arg_ibm128 = _Arg_next_value_,
284 template<
typename _CharT,
typename _Tp>
286 __to_arg_t_enum() noexcept;
292 template<typename _CharT> class basic_format_parse_context;
293 using format_parse_context = basic_format_parse_context<
char>;
294#ifdef _GLIBCXX_USE_WCHAR_T
295 using wformat_parse_context = basic_format_parse_context<wchar_t>;
298 template<
typename _CharT>
299 class basic_format_parse_context
302 using char_type = _CharT;
303 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
304 using iterator = const_iterator;
307 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
308 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
311 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
312 void operator=(
const basic_format_parse_context&) =
delete;
314 constexpr const_iterator
begin() const noexcept {
return _M_begin; }
315 constexpr const_iterator
end() const noexcept {
return _M_end; }
318 advance_to(const_iterator __it)
noexcept
324 if (_M_indexing == _Manual)
325 __format::__conflicting_indexing_in_format_string();
330 if (std::is_constant_evaluated())
331 if (_M_next_arg_id == _M_num_args)
332 __format::__invalid_arg_id_in_format_string();
333 return _M_next_arg_id++;
337 check_arg_id(
size_t __id)
339 if (_M_indexing == _Auto)
340 __format::__conflicting_indexing_in_format_string();
341 _M_indexing = _Manual;
343 if (std::is_constant_evaluated())
344 if (__id >= _M_num_args)
345 __format::__invalid_arg_id_in_format_string();
348#if __cpp_lib_format >= 202305L
349 template<
typename... _Ts>
351 check_dynamic_spec(
size_t __id)
noexcept
353 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
354 "template arguments for check_dynamic_spec<Ts...>(id) "
355 "must be unique and must be one of the allowed types");
356 if constexpr (
sizeof...(_Ts))
358 const __format::_Arg_t __t[] = {
359 __format::__to_arg_t_enum<_CharT, _Ts>()...
361 __check_dynamic_spec(__id, __t);
366 check_dynamic_spec_integral(
size_t __id)
noexcept
369 using enum __format::_Arg_t;
370 const __format::_Arg_t __t[] = { _Arg_i, _Arg_u, _Arg_ll, _Arg_ull };
371 __check_dynamic_spec(__id, __t);
376 check_dynamic_spec_string(
size_t __id)
noexcept
379 using enum __format::_Arg_t;
380 const __format::_Arg_t __t[] = { _Arg_str, _Arg_sv };
381 __check_dynamic_spec(__id, __t);
387 template<
typename _Tp,
typename... _Ts>
388 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
390 template<
typename... _Ts>
391 static consteval bool
392 __valid_types_for_check_dynamic_spec()
396 if constexpr (
sizeof...(_Ts) == 0)
405 = __once<bool, _Ts...>
406 + __once<char_type, _Ts...>
407 + __once<int, _Ts...>
408 + __once<
unsigned int, _Ts...>
409 + __once<
long long int, _Ts...>
410 + __once<
unsigned long long int, _Ts...>
411 + __once<float, _Ts...>
412 + __once<double, _Ts...>
413 + __once<
long double, _Ts...>
414 + __once<
const char_type*, _Ts...>
415 + __once<basic_string_view<char_type>, _Ts...>
416 + __once<
const void*, _Ts...>;
417 return __sum ==
sizeof...(_Ts);
423 __check_dynamic_spec(
size_t __id,
424 span<const __format::_Arg_t> __types)
noexcept
426 if (__id >= _M_num_args)
427 __format::__invalid_arg_id_in_format_string();
433 if (
auto* __args =
static_cast<_Scan_parse_context*
>(
this)->_M_types)
435 for (
auto __t : __types)
436 if (__args[__id] == __t)
439 __invalid_dynamic_spec(
"arg(id) type does not match");
445 static void __invalid_dynamic_spec(
const char*);
450 basic_format_parse_context(basic_string_view<_CharT> __fmt,
451 size_t __num_args) noexcept
452 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
458 enum _Indexing { _Unknown, _Manual, _Auto };
459 _Indexing _M_indexing = _Unknown;
460 size_t _M_next_arg_id = 0;
461 size_t _M_num_args = 0;
464 struct _Scan_parse_context;
465 friend __format::_Scanner<_CharT>;
468 template<
typename _CharT>
469 struct basic_format_parse_context<_CharT>::_Scan_parse_context
470 : basic_format_parse_context<_CharT>
472 using basic_format_parse_context<_CharT>::basic_format_parse_context;
473 const __format::_Arg_t* _M_types =
nullptr;
477 template<
typename _Tp,
template<
typename...>
class _Class>
478 constexpr bool __is_specialization_of =
false;
479 template<
template<
typename...>
class _Class,
typename... _Args>
480 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
485 template<
typename _CharT>
486 constexpr pair<unsigned short, const _CharT*>
487 __parse_integer(
const _CharT* __first,
const _CharT* __last)
489 if (__first == __last)
490 __builtin_unreachable();
492 if constexpr (is_same_v<_CharT, char>)
494 const auto __start = __first;
495 unsigned short __val = 0;
497 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
498 && __first != __start) [[likely]]
499 return {__val, __first};
503 constexpr int __n = 32;
505 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
506 __buf[__i] = __first[__i];
507 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
508 if (__ptr) [[likely]]
509 return {__v, __first + (__ptr - __buf)};
514 template<
typename _CharT>
515 constexpr pair<unsigned short, const _CharT*>
516 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
518 if (__first == __last)
519 __builtin_unreachable();
522 return {0, __first + 1};
524 if (
'1' <= *__first && *__first <=
'9')
526 const unsigned short __id = *__first -
'0';
527 const auto __next = __first + 1;
529 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
530 return {__id, __next};
532 return __format::__parse_integer(__first, __last);
540 _Pres_d = 1, _Pres_b, _Pres_B, _Pres_o, _Pres_x, _Pres_X, _Pres_c,
542 _Pres_a = 1, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F, _Pres_g, _Pres_G,
543 _Pres_p = 0, _Pres_P,
545 _Pres_seq = 0, _Pres_str,
569 template<
typename _Context>
571 __int_from_arg(
const basic_format_arg<_Context>& __arg);
573 constexpr bool __is_digit(
char __c)
574 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
576 constexpr bool __is_xdigit(
char __c)
577 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
579 template<
typename _CharT>
585 unsigned _M_localized : 1;
586 unsigned _M_zero_fill : 1;
587 _WidthPrec _M_width_kind : 2;
588 _WidthPrec _M_prec_kind : 2;
589 _Pres_type _M_type : 4;
590 unsigned _M_reserved : 1;
591 unsigned _M_reserved2 : 16;
592 unsigned short _M_width;
593 unsigned short _M_prec;
594 char32_t _M_fill =
' ';
596 using iterator =
typename basic_string_view<_CharT>::iterator;
598 static constexpr _Align
599 _S_align(_CharT __c)
noexcept
603 case '<':
return _Align_left;
604 case '>':
return _Align_right;
605 case '^':
return _Align_centre;
606 default:
return _Align_default;
612 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
613 {
return _M_parse_fill_and_align(__first, __last,
"{"); }
617 _M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill)
noexcept
619 for (
char __c : __not_fill)
620 if (*__first ==
static_cast<_CharT
>(__c))
623 using namespace __unicode;
624 if constexpr (__literal_encoding_is_unicode<_CharT>())
627 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
630 auto __beg = __uv.begin();
631 char32_t __c = *__beg++;
632 if (__is_scalar_value(__c))
633 if (
auto __next = __beg.base(); __next != __last)
634 if (_Align __align = _S_align(*__next))
642 else if (__last - __first >= 2)
643 if (_Align __align = _S_align(__first[1]))
650 if (_Align __align = _S_align(__first[0]))
659 static constexpr _Sign
660 _S_sign(_CharT __c)
noexcept
664 case '+':
return _Sign_plus;
665 case '-':
return _Sign_minus;
666 case ' ':
return _Sign_space;
667 default:
return _Sign_default;
673 _M_parse_sign(iterator __first, iterator)
noexcept
675 if (_Sign __sign = _S_sign(*__first))
685 _M_parse_alternate_form(iterator __first, iterator)
noexcept
697 _M_parse_zero_fill(iterator __first, iterator )
noexcept
708 static constexpr iterator
709 _S_parse_width_or_precision(iterator __first, iterator __last,
710 unsigned short& __val,
bool& __arg_id,
711 basic_format_parse_context<_CharT>& __pc)
713 if (__format::__is_digit(*__first))
715 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
717 __throw_format_error(
"format error: invalid width or precision "
722 else if (*__first ==
'{')
726 if (__first == __last)
727 __format::__unmatched_left_brace_in_format_string();
729 __val = __pc.next_arg_id();
732 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
733 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
734 __format::__invalid_arg_id_in_format_string();
736 __pc.check_arg_id(__v);
739#if __cpp_lib_format >= 202305L
740 __pc.check_dynamic_spec_integral(__val);
749 _M_parse_width(iterator __first, iterator __last,
750 basic_format_parse_context<_CharT>& __pc)
752 bool __arg_id =
false;
754 __throw_format_error(
"format error: width must be non-zero in "
756 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
758 if (__next != __first)
759 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
765 _M_parse_precision(iterator __first, iterator __last,
766 basic_format_parse_context<_CharT>& __pc)
768 if (__first[0] !=
'.')
771 iterator __next = ++__first;
772 bool __arg_id =
false;
773 if (__next != __last)
774 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
776 if (__next == __first)
777 __throw_format_error(
"format error: missing precision after '.' in "
779 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
785 _M_parse_locale(iterator __first, iterator )
noexcept
795 template<
typename _Context>
797 _M_get_width(_Context& __ctx)
const
800 if (_M_width_kind == _WP_value)
802 else if (_M_width_kind == _WP_from_arg)
803 __width = __format::__int_from_arg(__ctx.arg(_M_width));
807 template<
typename _Context>
809 _M_get_precision(_Context& __ctx)
const
812 if (_M_prec_kind == _WP_value)
814 else if (_M_prec_kind == _WP_from_arg)
815 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
820 template<
typename _Int>
822 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
826 else if (__sign == _Sign_plus)
828 else if (__sign == _Sign_space)
836 template<
typename _Out,
typename _CharT>
837 requires output_iterator<_Out, const _CharT&>
839 __write(_Out __out, basic_string_view<_CharT> __str)
841 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
847 for (_CharT __c : __str)
854 template<
typename _Out,
typename _CharT>
856 __write_padded(_Out __out, basic_string_view<_CharT> __str,
857 _Align __align,
size_t __nfill,
char32_t __fill_char)
859 const size_t __buflen = 0x20;
860 _CharT __padding_chars[__buflen];
861 __padding_chars[0] = _CharT();
862 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
864 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
867 while (__n > __padding.size())
869 __o = __format::__write(
std::move(__o), __padding);
870 __n -= __padding.size();
873 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
876 size_t __l, __r, __max;
877 if (__align == _Align_centre)
880 __r = __l + (__nfill & 1);
883 else if (__align == _Align_right)
896 using namespace __unicode;
897 if constexpr (__literal_encoding_is_unicode<_CharT>())
898 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
901 const char32_t __arr[1]{ __fill_char };
902 _Utf_view<_CharT,
const char32_t(&)[1]> __v(__arr);
903 basic_string<_CharT> __padstr(__v.begin(), __v.end());
904 __padding = __padstr;
906 __out = __format::__write(
std::move(__out), __padding);
907 __out = __format::__write(
std::move(__out), __str);
909 __out = __format::__write(
std::move(__out), __padding);
913 if (__max < __buflen)
914 __padding.remove_suffix(__buflen - __max);
918 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
920 __out = __format::__write(
std::move(__out), __str);
928 template<
typename _CharT,
typename _Out>
930 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
931 size_t __estimated_width,
932 basic_format_context<_Out, _CharT>& __fc,
933 const _Spec<_CharT>& __spec,
934 _Align __align = _Align_left)
936 size_t __width = __spec._M_get_width(__fc);
938 if (__width <= __estimated_width)
939 return __format::__write(__fc.out(), __str);
941 const size_t __nfill = __width - __estimated_width;
944 __align = __spec._M_align;
946 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
951 enum class _Term_char :
unsigned char {
956 template<
typename _CharT>
959 using _Str_view = basic_string_view<_CharT>;
963 {
return _GLIBCXX_WIDEN(
"\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
966 _CharT _S_term(_Term_char __term)
967 {
return _S_all()[
static_cast<unsigned char>(__term)]; }
971 {
return _S_all().substr(0, 3); }
974 _Str_view _S_newline()
975 {
return _S_all().substr(3, 3); }
978 _Str_view _S_return()
979 {
return _S_all().substr(6, 3); }
982 _Str_view _S_bslash()
983 {
return _S_all().substr(9, 3); }
987 {
return _S_all().substr(12, 3); }
991 {
return _S_all().substr(15, 3); }
995 {
return _S_all().substr(18, 2); }
999 {
return _S_all().substr(20, 2); }
1002 template<
typename _CharT>
1005 using _Str_view = basic_string_view<_CharT>;
1009 {
return _GLIBCXX_WIDEN(
"[]{}(), : "); }
1012 _Str_view _S_squares()
1013 {
return _S_all().substr(0, 2); }
1016 _Str_view _S_braces()
1017 {
return _S_all().substr(2, 2); }
1020 _Str_view _S_parens()
1021 {
return _S_all().substr(4, 2); }
1024 _Str_view _S_comma()
1025 {
return _S_all().substr(6, 2); }
1028 _Str_view _S_colon()
1029 {
return _S_all().substr(8, 2); }
1032 template<
typename _CharT>
1033 constexpr bool __should_escape_ascii(_CharT __c, _Term_char __term)
1035 using _Esc = _Escapes<_CharT>;
1038 case _Esc::_S_tab()[0]:
1039 case _Esc::_S_newline()[0]:
1040 case _Esc::_S_return()[0]:
1041 case _Esc::_S_bslash()[0]:
1043 case _Esc::_S_quote()[0]:
1044 return __term == _Term_char::_Tc_quote;
1045 case _Esc::_S_apos()[0]:
1046 return __term == _Term_char::_Tc_apos;
1048 return (__c >= 0 && __c < 0x20) || __c == 0x7f;
1053 constexpr bool __should_escape_unicode(
char32_t __c,
bool __prev_esc)
1055 if (__unicode::__should_escape_category(__c))
1059 return __unicode::__grapheme_cluster_break_property(__c)
1060 == __unicode::_Gcb_property::_Gcb_Extend;
1063 using uint_least32_t = __UINT_LEAST32_TYPE__;
1064 template<
typename _Out,
typename _CharT>
1066 __write_escape_seq(_Out __out, uint_least32_t __val,
1067 basic_string_view<_CharT> __prefix)
1069 constexpr size_t __max = 8;
1071 const string_view __narrow(
1073 std::__to_chars_i<uint_least32_t>(__buf, __buf + __max, __val, 16).ptr);
1075 __out = __format::__write(__out, __prefix);
1076 *__out = _Separators<_CharT>::_S_braces()[0];
1078 if constexpr (is_same_v<char, _CharT>)
1079 __out = __format::__write(__out, __narrow);
1080#ifdef _GLIBCXX_USE_WCHAR_T
1083 wchar_t __wbuf[__max];
1084 const size_t __n = __narrow.size();
1085 std::__to_wstring_numeric(__narrow.data(), __n, __wbuf);
1086 __out = __format::__write(__out, wstring_view(__wbuf, __n));
1089 *__out = _Separators<_CharT>::_S_braces()[1];
1093 template<
typename _Out,
typename _CharT>
1095 __write_escaped_char(_Out __out, _CharT __c)
1097 using _UChar = make_unsigned_t<_CharT>;
1098 using _Esc = _Escapes<_CharT>;
1101 case _Esc::_S_tab()[0]:
1102 return __format::__write(__out, _Esc::_S_tab().substr(1, 2));
1103 case _Esc::_S_newline()[0]:
1104 return __format::__write(__out, _Esc::_S_newline().substr(1, 2));
1105 case _Esc::_S_return()[0]:
1106 return __format::__write(__out, _Esc::_S_return().substr(1, 2));
1107 case _Esc::_S_bslash()[0]:
1108 return __format::__write(__out, _Esc::_S_bslash().substr(1, 2));
1109 case _Esc::_S_quote()[0]:
1110 return __format::__write(__out, _Esc::_S_quote().substr(1, 2));
1111 case _Esc::_S_apos()[0]:
1112 return __format::__write(__out, _Esc::_S_apos().substr(1, 2));
1114 return __format::__write_escape_seq(__out,
1115 static_cast<_UChar
>(__c),
1120 template<
typename _CharT,
typename _Out>
1122 __write_escaped_ascii(_Out __out,
1123 basic_string_view<_CharT> __str,
1126 using _Str_view = basic_string_view<_CharT>;
1127 auto __first = __str.begin();
1128 auto const __last = __str.end();
1129 while (__first != __last)
1131 auto __print = __first;
1133 while (__print != __last
1134 && !__format::__should_escape_ascii(*__print, __term))
1137 if (__print != __first)
1138 __out = __format::__write(__out, _Str_view(__first, __print));
1140 if (__print == __last)
1144 __out = __format::__write_escaped_char(__out, *__first);
1150 template<
typename _CharT,
typename _Out>
1152 __write_escaped_unicode(_Out __out,
1153 basic_string_view<_CharT> __str,
1156 using _Str_view = basic_string_view<_CharT>;
1157 using _UChar = make_unsigned_t<_CharT>;
1158 using _Esc = _Escapes<_CharT>;
1160 static constexpr char32_t __replace = U
'\uFFFD';
1161 static constexpr _Str_view __replace_rep = []
1164 if constexpr (is_same_v<char, _CharT>)
1165 return "\xEF\xBF\xBD";
1170 __unicode::_Utf_view<char32_t, _Str_view> __v(
std::move(__str));
1171 auto __first = __v.begin();
1172 auto const __last = __v.end();
1174 bool __prev_esc =
true;
1175 while (__first != __last)
1177 bool __esc_ascii =
false;
1178 bool __esc_unicode =
false;
1179 bool __esc_replace =
false;
1180 auto __should_escape = [&](
auto const& __it)
1184 = __format::__should_escape_ascii(*__it.base(), __term);
1185 if (__format::__should_escape_unicode(*__it, __prev_esc))
1186 return __esc_unicode =
true;
1187 if (*__it == __replace)
1189 _Str_view __units(__it.base(), __it._M_units());
1190 return __esc_replace = (__units != __replace_rep);
1195 auto __print = __first;
1196 while (__print != __last && !__should_escape(__print))
1202 if (__print != __first)
1203 __out = __format::__write(__out, _Str_view(__first.base(), __print.base()));
1205 if (__print == __last)
1210 __out = __format::__write_escaped_char(__out, *__first.base());
1211 else if (__esc_unicode)
1212 __out = __format::__write_escape_seq(__out, *__first, _Esc::_S_u());
1214 for (_CharT __c : _Str_view(__first.base(), __first._M_units()))
1215 __out = __format::__write_escape_seq(__out,
1216 static_cast<_UChar
>(__c),
1225 template<
typename _CharT,
typename _Out>
1227 __write_escaped(_Out __out, basic_string_view<_CharT> __str, _Term_char __term)
1229 *__out = _Escapes<_CharT>::_S_term(__term);
1232 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1233 __out = __format::__write_escaped_unicode(__out, __str, __term);
1234 else if constexpr (is_same_v<char, _CharT>
1235 && __unicode::__literal_encoding_is_extended_ascii())
1236 __out = __format::__write_escaped_ascii(__out, __str, __term);
1239 __out = __format::__write_escaped_ascii(__out, __str, __term);
1241 *__out = _Escapes<_CharT>::_S_term(__term);
1246 struct _Optional_locale
1248 [[__gnu__::__always_inline__]]
1249 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
1251 _Optional_locale(
const locale& __loc) noexcept
1252 : _M_loc(__loc), _M_hasval(
true)
1255 _Optional_locale(
const _Optional_locale& __l) noexcept
1256 : _M_dummy(), _M_hasval(__l._M_hasval)
1259 std::construct_at(&_M_loc, __l._M_loc);
1263 operator=(
const _Optional_locale& __l)
noexcept
1268 _M_loc = __l._M_loc;
1275 else if (__l._M_hasval)
1277 std::construct_at(&_M_loc, __l._M_loc);
1283 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
1286 operator=(locale&& __loc)
noexcept
1292 std::construct_at(&_M_loc,
std::move(__loc));
1303 std::construct_at(&_M_loc);
1309 bool has_value() const noexcept {
return _M_hasval; }
1312 char _M_dummy =
'\0';
1315 bool _M_hasval =
false;
1318 template<__
char _CharT>
1319 struct __formatter_str
1321 __formatter_str() =
default;
1324 __formatter_str(_Spec<_CharT> __spec) noexcept
1328 constexpr typename basic_format_parse_context<_CharT>::iterator
1329 parse(basic_format_parse_context<_CharT>& __pc)
1331 auto __first = __pc.begin();
1332 const auto __last = __pc.end();
1333 _Spec<_CharT> __spec{};
1335 auto __finalize = [
this, &__spec] {
1339 auto __finished = [&] {
1340 if (__first == __last || *__first ==
'}')
1351 __first = __spec._M_parse_fill_and_align(__first, __last);
1355 __first = __spec._M_parse_width(__first, __last, __pc);
1359 __first = __spec._M_parse_precision(__first, __last, __pc);
1363 if (*__first ==
's')
1365#if __glibcxx_format_ranges
1366 else if (*__first ==
'?')
1368 __spec._M_type = _Pres_esc;
1376 __format::__failed_to_parse_format_spec();
1379 template<
typename _Out>
1381 format(basic_string_view<_CharT> __s,
1382 basic_format_context<_Out, _CharT>& __fc)
const
1384 constexpr auto __term = __format::_Term_char::_Tc_quote;
1385 const auto __write_direct = [&]
1387 if (_M_spec._M_type == _Pres_esc)
1388 return __format::__write_escaped(__fc.out(), __s, __term);
1390 return __format::__write(__fc.out(), __s);
1393 if (_M_spec._M_width_kind == _WP_none
1394 && _M_spec._M_prec_kind == _WP_none)
1395 return __write_direct();
1397 const size_t __prec =
1398 _M_spec._M_prec_kind != _WP_none
1399 ? _M_spec._M_get_precision(__fc)
1400 : basic_string_view<_CharT>::npos;
1402 const size_t __estimated_width = _S_trunc(__s, __prec);
1404 if (_M_spec._M_get_width(__fc) <= __estimated_width
1405 && _M_spec._M_prec_kind == _WP_none)
1406 return __write_direct();
1408 if (_M_spec._M_type != _Pres_esc)
1409 return __format::__write_padded_as_spec(__s, __estimated_width,
1412 __format::_Str_sink<_CharT> __sink;
1413 __format::__write_escaped(__sink.out(), __s, __term);
1414 basic_string_view<_CharT> __escaped(__sink.view().data(),
1415 __sink.view().size());
1416 const size_t __escaped_width = _S_trunc(__escaped, __prec);
1420 return __format::__write_padded_as_spec(__escaped, __escaped_width,
1424#if __glibcxx_format_ranges
1425 template<ranges::input_range _Rg,
typename _Out>
1426 requires same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _CharT>
1427 typename basic_format_context<_Out, _CharT>::iterator
1428 _M_format_range(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
1430 using _String = basic_string<_CharT>;
1431 using _String_view = basic_string_view<_CharT>;
1432 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
1434 const size_t __n(ranges::distance(__rg));
1435 if constexpr (ranges::contiguous_range<_Rg>)
1436 return format(_String_view(ranges::data(__rg), __n), __fc);
1437 else if (__n <= __format::__stackbuf_size<_CharT>)
1439 _CharT __buf[__format::__stackbuf_size<_CharT>];
1440 ranges::copy(__rg, __buf);
1441 return format(_String_view(__buf, __n), __fc);
1443 else if constexpr (ranges::sized_range<_Rg>)
1444 return format(_String(from_range, __rg), __fc);
1445 else if constexpr (ranges::random_access_range<_Rg>)
1447 ranges::iterator_t<_Rg> __first = ranges::begin(__rg);
1448 ranges::subrange __sub(__first, __first + __n);
1449 return format(_String(from_range, __sub), __fc);
1454 ranges::subrange __sub(__rg, __n);
1455 return format(_String(from_range, __sub), __fc);
1459 return format(_String(from_range, __rg), __fc);
1463 set_debug_format() noexcept
1464 { _M_spec._M_type = _Pres_esc; }
1469 _S_trunc(basic_string_view<_CharT>& __s,
size_t __prec)
1471 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1473 if (__prec != basic_string_view<_CharT>::npos)
1474 return __unicode::__truncate(__s, __prec);
1476 return __unicode::__field_width(__s);
1480 __s = __s.substr(0, __prec);
1485 _Spec<_CharT> _M_spec{};
1488 template<__
char _CharT>
1489 struct __formatter_int
1493 static constexpr _Pres_type _AsInteger = _Pres_d;
1494 static constexpr _Pres_type _AsBool = _Pres_s;
1495 static constexpr _Pres_type _AsChar = _Pres_c;
1497 constexpr typename basic_format_parse_context<_CharT>::iterator
1498 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1500 _Spec<_CharT> __spec{};
1501 __spec._M_type = __type;
1503 const auto __last = __pc.end();
1504 auto __first = __pc.begin();
1506 auto __finalize = [
this, &__spec] {
1510 auto __finished = [&] {
1511 if (__first == __last || *__first ==
'}')
1522 __first = __spec._M_parse_fill_and_align(__first, __last);
1526 __first = __spec._M_parse_sign(__first, __last);
1530 __first = __spec._M_parse_alternate_form(__first, __last);
1534 __first = __spec._M_parse_zero_fill(__first, __last);
1538 __first = __spec._M_parse_width(__first, __last, __pc);
1542 __first = __spec._M_parse_locale(__first, __last);
1549 __spec._M_type = _Pres_b;
1553 __spec._M_type = _Pres_B;
1559 if (__type != _AsBool)
1561 __spec._M_type = _Pres_c;
1566 __spec._M_type = _Pres_d;
1570 __spec._M_type = _Pres_o;
1574 __spec._M_type = _Pres_x;
1578 __spec._M_type = _Pres_X;
1582 if (__type == _AsBool)
1584 __spec._M_type = _Pres_s;
1588#if __glibcxx_format_ranges
1590 if (__type == _AsChar)
1592 __spec._M_type = _Pres_esc;
1602 __format::__failed_to_parse_format_spec();
1605 template<
typename _Tp>
1606 constexpr typename basic_format_parse_context<_CharT>::iterator
1607 _M_parse(basic_format_parse_context<_CharT>& __pc)
1609 if constexpr (is_same_v<_Tp, bool>)
1611 auto __end = _M_do_parse(__pc, _AsBool);
1612 if (_M_spec._M_type == _Pres_s)
1613 if (_M_spec._M_sign || _M_spec._M_alt || _M_spec._M_zero_fill)
1614 __throw_format_error(
"format error: format-spec contains "
1615 "invalid formatting options for "
1619 else if constexpr (__char<_Tp>)
1621 auto __end = _M_do_parse(__pc, _AsChar);
1622 if (_M_spec._M_type == _Pres_c || _M_spec._M_type == _Pres_esc)
1623 if (_M_spec._M_sign || _M_spec._M_alt || _M_spec._M_zero_fill
1625 __throw_format_error(
"format error: format-spec contains "
1626 "invalid formatting options for "
1631 return _M_do_parse(__pc, _AsInteger);
1634 template<
typename _Int,
typename _Out>
1635 typename basic_format_context<_Out, _CharT>::iterator
1636 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1638 if (_M_spec._M_type == _Pres_c)
1639 return _M_format_character(_S_to_character(__i), __fc);
1641 char __buf[
sizeof(_Int) * __CHAR_BIT__ + 3];
1642 to_chars_result __res{};
1644 string_view __base_prefix;
1645 make_unsigned_t<_Int> __u;
1647 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1651 char* __start = __buf + 3;
1652 char*
const __end = __buf +
sizeof(__buf);
1653 char*
const __start_digits = __start;
1655 switch (_M_spec._M_type)
1659 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1660 __res = to_chars(__start, __end, __u, 2);
1664 return _M_format_character(_S_to_character(__i), __fc);
1670 __res = to_chars(__start, __end, __u, 10);
1674 __base_prefix =
"0";
1675 __res = to_chars(__start, __end, __u, 8);
1679 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1680 __res = to_chars(__start, __end, __u, 16);
1681 if (_M_spec._M_type == _Pres_X)
1682 for (
auto __p = __start; __p != __res.ptr; ++__p)
1683#
if __has_builtin(__builtin_toupper)
1684 *__p = __builtin_toupper(*__p);
1690 __builtin_unreachable();
1693 if (_M_spec._M_alt && __base_prefix.size())
1695 __start -= __base_prefix.size();
1696 __builtin_memcpy(__start, __base_prefix.data(),
1697 __base_prefix.size());
1699 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1701 return _M_format_int(string_view(__start, __res.ptr - __start),
1702 __start_digits - __start, __fc);
1705 template<
typename _Out>
1706 typename basic_format_context<_Out, _CharT>::iterator
1707 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1709 if (_M_spec._M_type == _Pres_c)
1710 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1711 if (_M_spec._M_type != _Pres_s)
1712 return format(
static_cast<unsigned char>(__i), __fc);
1714 basic_string<_CharT> __s;
1716 if (_M_spec._M_localized) [[unlikely]]
1719 __s = __i ? __np.truename() : __np.falsename();
1720 __est_width = __s.size();
1724 if constexpr (is_same_v<char, _CharT>)
1725 __s = __i ?
"true" :
"false";
1727 __s = __i ? L
"true" : L
"false";
1728 __est_width = __s.size();
1731 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1735 [[__gnu__::__always_inline__]]
1737 _S_character_width(_CharT __c)
1740 if constexpr (
sizeof(_CharT) > 1u &&
1741 __unicode::__literal_encoding_is_unicode<_CharT>())
1742 return __unicode::__field_width(__c);
1747 template<
typename _Out>
1748 typename basic_format_context<_Out, _CharT>::iterator
1749 _M_format_character(_CharT __c,
1750 basic_format_context<_Out, _CharT>& __fc)
const
1752 return __format::__write_padded_as_spec({&__c, 1u},
1753 _S_character_width(__c),
1757 template<
typename _Out>
1758 typename basic_format_context<_Out, _CharT>::iterator
1759 _M_format_character_escaped(_CharT __c,
1760 basic_format_context<_Out, _CharT>& __fc)
const
1762 using _Esc = _Escapes<_CharT>;
1763 constexpr auto __term = __format::_Term_char::_Tc_apos;
1764 const basic_string_view<_CharT> __in(&__c, 1u);
1765 if (_M_spec._M_get_width(__fc) <= 3u)
1766 return __format::__write_escaped(__fc.out(), __in, __term);
1769 __format::_Fixedbuf_sink<_CharT> __sink(__buf);
1770 __format::__write_escaped(__sink.out(), __in, __term);
1772 const basic_string_view<_CharT> __escaped = __sink.view();
1773 size_t __estimated_width;
1774 if (__escaped[1] == _Esc::_S_bslash()[0])
1775 __estimated_width = __escaped.size();
1777 __estimated_width = 2 + _S_character_width(__c);
1778 return __format::__write_padded_as_spec(__escaped,
1783 template<
typename _Int>
1785 _S_to_character(_Int __i)
1788 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1790 if (_Traits::__min <= __i && __i <= _Traits::__max)
1791 return static_cast<_CharT
>(__i);
1793 else if constexpr (is_signed_v<_Int>)
1795 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1796 return static_cast<_CharT
>(__i);
1798 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1799 return static_cast<_CharT
>(__i);
1800 __throw_format_error(
"format error: integer not representable as "
1804 template<
typename _Out>
1805 typename basic_format_context<_Out, _CharT>::iterator
1806 _M_format_int(string_view __narrow_str,
size_t __prefix_len,
1807 basic_format_context<_Out, _CharT>& __fc)
const
1809 size_t __width = _M_spec._M_get_width(__fc);
1811 basic_string_view<_CharT> __str;
1812 if constexpr (is_same_v<char, _CharT>)
1813 __str = __narrow_str;
1814#ifdef _GLIBCXX_USE_WCHAR_T
1817 size_t __n = __narrow_str.size();
1818 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
1819 std::__to_wstring_numeric(__narrow_str.data(), __n, __p);
1824 if (_M_spec._M_localized)
1826 const auto& __l = __fc.locale();
1827 if (__l.name() !=
"C")
1829 auto& __np = use_facet<numpunct<_CharT>>(__l);
1830 string __grp = __np.grouping();
1833 size_t __n = __str.size() - __prefix_len;
1834 auto __p = (_CharT*)__builtin_alloca(2 * __n
1837 auto __s = __str.data();
1838 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1839 __s += __prefix_len;
1840 auto __end = std::__add_grouping(__p + __prefix_len,
1841 __np.thousands_sep(),
1845 __str = {__p, size_t(__end - __p)};
1850 if (__width <= __str.size())
1851 return __format::__write(__fc.out(), __str);
1853 char32_t __fill_char = _M_spec._M_fill;
1854 _Align __align = _M_spec._M_align;
1856 size_t __nfill = __width - __str.size();
1857 auto __out = __fc.out();
1858 if (__align == _Align_default)
1860 __align = _Align_right;
1861 if (_M_spec._M_zero_fill)
1863 __fill_char = _CharT(
'0');
1865 if (__prefix_len != 0)
1867 __out = __format::__write(
std::move(__out),
1868 __str.substr(0, __prefix_len));
1869 __str.remove_prefix(__prefix_len);
1873 __fill_char = _CharT(
' ');
1875 return __format::__write_padded(
std::move(__out), __str,
1876 __align, __nfill, __fill_char);
1879#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__
1880 template<
typename _Tp>
1882 =
typename __conditional_t<(
sizeof(_Tp) <=
sizeof(
long long)),
1883 std::make_unsigned<_Tp>,
1884 type_identity<unsigned __int128>>::type;
1887 template<
typename _Int>
1888 static to_chars_result
1889 to_chars(
char* __first,
char* __last, _Int __value,
int __base)
1890 {
return std::__to_chars_i<_Int>(__first, __last, __value, __base); }
1893 _Spec<_CharT> _M_spec{};
1904#undef _GLIBCXX_FORMAT_F128
1906#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1909 using __float128_t = __ieee128;
1910# define _GLIBCXX_FORMAT_F128 1
1912#ifdef __LONG_DOUBLE_IEEE128__
1916 to_chars(
char*,
char*, __ibm128)
noexcept
1917 __asm(
"_ZSt8to_charsPcS_e");
1920 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1921 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1924 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1925 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1926#elif __cplusplus == 202002L
1928 to_chars(
char*,
char*, __ieee128)
noexcept
1929 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1932 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1933 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1936 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1937 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1940#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1943 using __float128_t =
long double;
1944# define _GLIBCXX_FORMAT_F128 1
1946#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1949 using __float128_t = _Float128;
1950# define _GLIBCXX_FORMAT_F128 2
1952# if __cplusplus == 202002L
1956 to_chars(
char*,
char*, _Float128)
noexcept
1957# if _GLIBCXX_INLINE_VERSION
1958 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1960 __asm(
"_ZSt8to_charsPcS_DF128_");
1964 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1965# if _GLIBCXX_INLINE_VERSION
1966 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1968 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
1972 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
1973# if _GLIBCXX_INLINE_VERSION
1974 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
1976 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
1981 using std::to_chars;
1984 template<
typename _Tp>
1985 concept __formattable_float
1986 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
1987 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
1989 template<__
char _CharT>
1990 struct __formatter_fp
1992 constexpr typename basic_format_parse_context<_CharT>::iterator
1993 parse(basic_format_parse_context<_CharT>& __pc)
1995 _Spec<_CharT> __spec{};
1996 const auto __last = __pc.end();
1997 auto __first = __pc.begin();
1999 auto __finalize = [
this, &__spec] {
2003 auto __finished = [&] {
2004 if (__first == __last || *__first ==
'}')
2015 __first = __spec._M_parse_fill_and_align(__first, __last);
2019 __first = __spec._M_parse_sign(__first, __last);
2023 __first = __spec._M_parse_alternate_form(__first, __last);
2027 __first = __spec._M_parse_zero_fill(__first, __last);
2031 if (__first[0] !=
'.')
2033 __first = __spec._M_parse_width(__first, __last, __pc);
2038 __first = __spec._M_parse_precision(__first, __last, __pc);
2042 __first = __spec._M_parse_locale(__first, __last);
2049 __spec._M_type = _Pres_a;
2053 __spec._M_type = _Pres_A;
2057 __spec._M_type = _Pres_e;
2061 __spec._M_type = _Pres_E;
2065 __spec._M_type = _Pres_f;
2069 __spec._M_type = _Pres_F;
2073 __spec._M_type = _Pres_g;
2077 __spec._M_type = _Pres_G;
2085 __format::__failed_to_parse_format_spec();
2088 template<
typename _Fp,
typename _Out>
2089 typename basic_format_context<_Out, _CharT>::iterator
2090 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2094 to_chars_result __res{};
2097 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2099 __prec = _M_spec._M_get_precision(__fc);
2101 char* __start = __buf + 1;
2102 char* __end = __buf +
sizeof(__buf);
2105 bool __upper =
false;
2106 bool __trailing_zeros =
false;
2109 switch (_M_spec._M_type)
2116 if (_M_spec._M_type != _Pres_A)
2118 __fmt = chars_format::hex;
2126 __fmt = chars_format::scientific;
2133 __fmt = chars_format::fixed;
2140 __trailing_zeros =
true;
2142 __fmt = chars_format::general;
2146 __fmt = chars_format::general;
2149 __builtin_unreachable();
2153 auto __to_chars = [&](
char* __b,
char* __e) {
2155 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2156 else if (__fmt != chars_format{})
2157 return __format::to_chars(__b, __e, __v, __fmt);
2159 return __format::to_chars(__b, __e, __v);
2163 __res = __to_chars(__start, __end);
2165 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2169 size_t __guess = 8 + __prec;
2170 if (__fmt == chars_format::fixed)
2172 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2173 || is_same_v<_Fp, long double>)
2178 if constexpr (is_same_v<_Fp, float>)
2179 __builtin_frexpf(__v, &__exp);
2180 else if constexpr (is_same_v<_Fp, double>)
2181 __builtin_frexp(__v, &__exp);
2182 else if constexpr (is_same_v<_Fp, long double>)
2183 __builtin_frexpl(__v, &__exp);
2185 __guess += 1U + __exp * 4004U / 13301U;
2188 __guess += numeric_limits<_Fp>::max_exponent10;
2190 if (__guess <=
sizeof(__buf)) [[unlikely]]
2191 __guess =
sizeof(__buf) * 2;
2200 auto __overwrite = [&__to_chars, &__res] (
char* __p,
size_t __n)
2202 __res = __to_chars(__p + 1, __p + __n - 1);
2203 return __res.ec == errc{} ? __res.ptr - __p : 0;
2208 __start = __dynbuf.
data() + 1;
2209 __end = __dynbuf.
data() + __dynbuf.
size();
2211 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2217 for (
char* __p = __start; __p != __res.ptr; ++__p)
2221 bool __have_sign =
true;
2223 if (!__builtin_signbit(__v))
2225 if (_M_spec._M_sign == _Sign_plus)
2227 else if (_M_spec._M_sign == _Sign_space)
2230 __have_sign =
false;
2233 string_view __narrow_str(__start, __res.ptr - __start);
2237 if (_M_spec._M_alt && __builtin_isfinite(__v))
2239 string_view __s = __narrow_str;
2243 size_t __d = __s.find(
'.');
2244 if (__d != __s.npos)
2246 __p = __s.find(__expc, __d + 1);
2247 if (__p == __s.npos)
2251 if (__trailing_zeros)
2254 if (__s[__have_sign] !=
'0')
2256 __sigfigs = __p - __have_sign - 1;
2261 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2266 __p = __s.find(__expc);
2267 if (__p == __s.npos)
2270 __sigfigs = __d - __have_sign;
2273 if (__trailing_zeros && __prec != 0)
2278 __z = __prec - __sigfigs;
2281 if (
size_t __extras =
int(__d == __p) + __z)
2283 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2287 __builtin_memmove(__start + __p + __extras,
2291 __start[__p++] =
'.';
2292 __builtin_memset(__start + __p,
'0', __z);
2293 __narrow_str = {__s.data(), __s.size() + __extras};
2297 __dynbuf.
reserve(__s.size() + __extras);
2298 if (__dynbuf.
empty())
2300 __dynbuf = __s.
substr(0, __p);
2304 __dynbuf.
append(__z,
'0');
2305 __dynbuf.
append(__s.substr(__p));
2309 __dynbuf.
insert(__p, __extras,
'0');
2311 __dynbuf[__p] =
'.';
2313 __narrow_str = __dynbuf;
2318 basic_string<_CharT> __wstr;
2319 basic_string_view<_CharT> __str;
2320 if constexpr (is_same_v<_CharT, char>)
2321 __str = __narrow_str;
2322#ifdef _GLIBCXX_USE_WCHAR_T
2325 __wstr = std::__to_wstring_numeric(__narrow_str);
2330 if (_M_spec._M_localized && __builtin_isfinite(__v))
2332 auto __s = _M_localize(__str, __expc, __fc.locale());
2337 size_t __width = _M_spec._M_get_width(__fc);
2339 if (__width <= __str.size())
2340 return __format::__write(__fc.out(), __str);
2342 char32_t __fill_char = _M_spec._M_fill;
2343 _Align __align = _M_spec._M_align;
2345 size_t __nfill = __width - __str.size();
2346 auto __out = __fc.out();
2347 if (__align == _Align_default)
2349 __align = _Align_right;
2350 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2352 __fill_char = _CharT(
'0');
2354 if (!__format::__is_xdigit(__narrow_str[0]))
2356 *__out++ = __str[0];
2357 __str.remove_prefix(1);
2361 __fill_char = _CharT(
' ');
2363 return __format::__write_padded(
std::move(__out), __str,
2364 __align, __nfill, __fill_char);
2368 basic_string<_CharT>
2369 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2370 const locale& __loc)
const
2372 basic_string<_CharT> __lstr;
2374 if (__loc == locale::classic())
2377 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2378 const _CharT __point = __np.decimal_point();
2379 const string __grp = __np.grouping();
2381 _CharT __dot, __exp;
2382 if constexpr (is_same_v<_CharT, char>)
2405 __builtin_unreachable();
2409 if (__grp.empty() && __point == __dot)
2412 size_t __d = __str.find(__dot);
2413 size_t __e = min(__d, __str.find(__exp));
2414 if (__e == __str.npos)
2416 const size_t __r = __str.size() - __e;
2417 auto __overwrite = [&](_CharT* __p, size_t) {
2420 if (
auto __c = __str.front(); __c ==
'-' || __c ==
'+' || __c ==
' ')
2425 auto __end = std::__add_grouping(__p + __off, __np.thousands_sep(),
2426 __grp.data(), __grp.size(),
2427 __str.data() + __off,
2428 __str.data() + __e);
2431 if (__d != __str.npos)
2437 const size_t __rlen = __str.size() - __e;
2439 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2442 return (__end - __p);
2444 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2448 _Spec<_CharT> _M_spec{};
2455 template<__format::__
char _CharT>
2456 struct formatter<_CharT, _CharT>
2458 formatter() =
default;
2460 constexpr typename basic_format_parse_context<_CharT>::iterator
2461 parse(basic_format_parse_context<_CharT>& __pc)
2463 return _M_f.template _M_parse<_CharT>(__pc);
2466 template<
typename _Out>
2467 typename basic_format_context<_Out, _CharT>::iterator
2468 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2470 if (_M_f._M_spec._M_type == __format::_Pres_none
2471 || _M_f._M_spec._M_type == __format::_Pres_c)
2472 return _M_f._M_format_character(__u, __fc);
2473 else if (_M_f._M_spec._M_type == __format::_Pres_esc)
2474 return _M_f._M_format_character_escaped(__u, __fc);
2476 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2479#if __glibcxx_format_ranges
2481 set_debug_format() noexcept
2482 { _M_f._M_spec._M_type = __format::_Pres_esc; }
2486 __format::__formatter_int<_CharT> _M_f;
2489#ifdef _GLIBCXX_USE_WCHAR_T
2492 struct formatter<char, wchar_t>
2494 formatter() =
default;
2496 constexpr typename basic_format_parse_context<wchar_t>::iterator
2497 parse(basic_format_parse_context<wchar_t>& __pc)
2499 return _M_f._M_parse<
char>(__pc);
2502 template<
typename _Out>
2503 typename basic_format_context<_Out, wchar_t>::iterator
2504 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2506 if (_M_f._M_spec._M_type == __format::_Pres_none
2507 || _M_f._M_spec._M_type == __format::_Pres_c)
2508 return _M_f._M_format_character(__u, __fc);
2509 else if (_M_f._M_spec._M_type == __format::_Pres_esc)
2510 return _M_f._M_format_character_escaped(__u, __fc);
2512 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2515#if __glibcxx_format_ranges
2517 set_debug_format() noexcept
2518 { _M_f._M_spec._M_type = __format::_Pres_esc; }
2522 __format::__formatter_int<wchar_t> _M_f;
2529 template<__format::__
char _CharT>
2530 struct formatter<_CharT*, _CharT>
2532 formatter() =
default;
2534 [[__gnu__::__always_inline__]]
2535 constexpr typename basic_format_parse_context<_CharT>::iterator
2536 parse(basic_format_parse_context<_CharT>& __pc)
2537 {
return _M_f.parse(__pc); }
2539 template<
typename _Out>
2540 [[__gnu__::__nonnull__]]
2541 typename basic_format_context<_Out, _CharT>::iterator
2542 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2543 {
return _M_f.format(__u, __fc); }
2545#if __glibcxx_format_ranges
2546 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2550 __format::__formatter_str<_CharT> _M_f;
2553 template<__format::__
char _CharT>
2554 struct formatter<const _CharT*, _CharT>
2556 formatter() =
default;
2558 [[__gnu__::__always_inline__]]
2559 constexpr typename basic_format_parse_context<_CharT>::iterator
2560 parse(basic_format_parse_context<_CharT>& __pc)
2561 {
return _M_f.parse(__pc); }
2563 template<
typename _Out>
2564 [[__gnu__::__nonnull__]]
2565 typename basic_format_context<_Out, _CharT>::iterator
2566 format(
const _CharT* __u,
2567 basic_format_context<_Out, _CharT>& __fc)
const
2568 {
return _M_f.format(__u, __fc); }
2570#if __glibcxx_format_ranges
2571 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2575 __format::__formatter_str<_CharT> _M_f;
2578 template<__format::__
char _CharT,
size_t _Nm>
2579 struct formatter<_CharT[_Nm], _CharT>
2581 formatter() =
default;
2583 [[__gnu__::__always_inline__]]
2584 constexpr typename basic_format_parse_context<_CharT>::iterator
2585 parse(basic_format_parse_context<_CharT>& __pc)
2586 {
return _M_f.parse(__pc); }
2588 template<
typename _Out>
2589 typename basic_format_context<_Out, _CharT>::iterator
2590 format(
const _CharT (&__u)[_Nm],
2591 basic_format_context<_Out, _CharT>& __fc)
const
2592 {
return _M_f.format({__u, _Nm}, __fc); }
2594#if __glibcxx_format_ranges
2595 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2599 __format::__formatter_str<_CharT> _M_f;
2602 template<
typename _Traits,
typename _Alloc>
2603 struct formatter<basic_string<char, _Traits, _Alloc>, char>
2605 formatter() =
default;
2607 [[__gnu__::__always_inline__]]
2608 constexpr typename basic_format_parse_context<char>::iterator
2609 parse(basic_format_parse_context<char>& __pc)
2610 {
return _M_f.parse(__pc); }
2612 template<
typename _Out>
2613 typename basic_format_context<_Out, char>::iterator
2614 format(
const basic_string<char, _Traits, _Alloc>& __u,
2615 basic_format_context<_Out, char>& __fc)
const
2616 {
return _M_f.format(__u, __fc); }
2618#if __glibcxx_format_ranges
2619 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2623 __format::__formatter_str<char> _M_f;
2626#ifdef _GLIBCXX_USE_WCHAR_T
2627 template<
typename _Traits,
typename _Alloc>
2628 struct formatter<basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2630 formatter() =
default;
2632 [[__gnu__::__always_inline__]]
2633 constexpr typename basic_format_parse_context<wchar_t>::iterator
2634 parse(basic_format_parse_context<wchar_t>& __pc)
2635 {
return _M_f.parse(__pc); }
2637 template<
typename _Out>
2638 typename basic_format_context<_Out, wchar_t>::iterator
2639 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2640 basic_format_context<_Out, wchar_t>& __fc)
const
2641 {
return _M_f.format(__u, __fc); }
2643#if __glibcxx_format_ranges
2644 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2648 __format::__formatter_str<wchar_t> _M_f;
2652 template<
typename _Traits>
2653 struct formatter<basic_string_view<char, _Traits>, char>
2655 formatter() =
default;
2657 [[__gnu__::__always_inline__]]
2658 constexpr typename basic_format_parse_context<char>::iterator
2659 parse(basic_format_parse_context<char>& __pc)
2660 {
return _M_f.parse(__pc); }
2662 template<
typename _Out>
2663 typename basic_format_context<_Out, char>::iterator
2664 format(basic_string_view<char, _Traits> __u,
2665 basic_format_context<_Out, char>& __fc)
const
2666 {
return _M_f.format(__u, __fc); }
2668#if __glibcxx_format_ranges
2669 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2673 __format::__formatter_str<char> _M_f;
2676#ifdef _GLIBCXX_USE_WCHAR_T
2677 template<
typename _Traits>
2678 struct formatter<basic_string_view<wchar_t, _Traits>, wchar_t>
2680 formatter() =
default;
2682 [[__gnu__::__always_inline__]]
2683 constexpr typename basic_format_parse_context<wchar_t>::iterator
2684 parse(basic_format_parse_context<wchar_t>& __pc)
2685 {
return _M_f.parse(__pc); }
2687 template<
typename _Out>
2688 typename basic_format_context<_Out, wchar_t>::iterator
2689 format(basic_string_view<wchar_t, _Traits> __u,
2690 basic_format_context<_Out, wchar_t>& __fc)
const
2691 {
return _M_f.format(__u, __fc); }
2693#if __glibcxx_format_ranges
2694 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2698 __format::__formatter_str<wchar_t> _M_f;
2708 template<
typename _Tp>
2709 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2711#if defined __SIZEOF_INT128__
2712 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2713 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2717 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2718 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2719#ifdef _GLIBCXX_USE_CHAR8_T
2720 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2722 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2723 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2728 template<
typename _Tp, __format::__
char _CharT>
2729 requires __format::__is_formattable_integer<_Tp>
2730 struct formatter<_Tp, _CharT>
2732 formatter() =
default;
2734 [[__gnu__::__always_inline__]]
2735 constexpr typename basic_format_parse_context<_CharT>::iterator
2736 parse(basic_format_parse_context<_CharT>& __pc)
2738 return _M_f.template _M_parse<_Tp>(__pc);
2741 template<
typename _Out>
2742 typename basic_format_context<_Out, _CharT>::iterator
2743 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2744 {
return _M_f.format(__u, __fc); }
2747 __format::__formatter_int<_CharT> _M_f;
2750#if defined __glibcxx_to_chars
2752 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2753 struct formatter<_Tp, _CharT>
2755 formatter() =
default;
2757 [[__gnu__::__always_inline__]]
2758 constexpr typename basic_format_parse_context<_CharT>::iterator
2759 parse(basic_format_parse_context<_CharT>& __pc)
2760 {
return _M_f.parse(__pc); }
2762 template<
typename _Out>
2763 typename basic_format_context<_Out, _CharT>::iterator
2764 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2765 {
return _M_f.format(__u, __fc); }
2768 __format::__formatter_fp<_CharT> _M_f;
2771#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2773 template<__format::__
char _CharT>
2774 struct formatter<long double, _CharT>
2776 formatter() =
default;
2778 [[__gnu__::__always_inline__]]
2779 constexpr typename basic_format_parse_context<_CharT>::iterator
2780 parse(basic_format_parse_context<_CharT>& __pc)
2781 {
return _M_f.parse(__pc); }
2783 template<
typename _Out>
2784 typename basic_format_context<_Out, _CharT>::iterator
2785 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2786 {
return _M_f.format((
double)__u, __fc); }
2789 __format::__formatter_fp<_CharT> _M_f;
2793#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2795 template<__format::__
char _CharT>
2796 struct formatter<_Float16, _CharT>
2798 formatter() =
default;
2800 [[__gnu__::__always_inline__]]
2801 constexpr typename basic_format_parse_context<_CharT>::iterator
2802 parse(basic_format_parse_context<_CharT>& __pc)
2803 {
return _M_f.parse(__pc); }
2805 template<
typename _Out>
2806 typename basic_format_context<_Out, _CharT>::iterator
2807 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
2808 {
return _M_f.format((
float)__u, __fc); }
2811 __format::__formatter_fp<_CharT> _M_f;
2815#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2817 template<__format::__
char _CharT>
2818 struct formatter<_Float32, _CharT>
2820 formatter() =
default;
2822 [[__gnu__::__always_inline__]]
2823 constexpr typename basic_format_parse_context<_CharT>::iterator
2824 parse(basic_format_parse_context<_CharT>& __pc)
2825 {
return _M_f.parse(__pc); }
2827 template<
typename _Out>
2828 typename basic_format_context<_Out, _CharT>::iterator
2829 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
2830 {
return _M_f.format((
float)__u, __fc); }
2833 __format::__formatter_fp<_CharT> _M_f;
2837#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2839 template<__format::__
char _CharT>
2840 struct formatter<_Float64, _CharT>
2842 formatter() =
default;
2844 [[__gnu__::__always_inline__]]
2845 constexpr typename basic_format_parse_context<_CharT>::iterator
2846 parse(basic_format_parse_context<_CharT>& __pc)
2847 {
return _M_f.parse(__pc); }
2849 template<
typename _Out>
2850 typename basic_format_context<_Out, _CharT>::iterator
2851 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
2852 {
return _M_f.format((
double)__u, __fc); }
2855 __format::__formatter_fp<_CharT> _M_f;
2859#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128 == 1
2861 template<__format::__
char _CharT>
2862 struct formatter<_Float128, _CharT>
2864 formatter() =
default;
2866 [[__gnu__::__always_inline__]]
2867 constexpr typename basic_format_parse_context<_CharT>::iterator
2868 parse(basic_format_parse_context<_CharT>& __pc)
2869 {
return _M_f.parse(__pc); }
2871 template<
typename _Out>
2872 typename basic_format_context<_Out, _CharT>::iterator
2873 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
2874 {
return _M_f.format((__format::__float128_t)__u, __fc); }
2877 __format::__formatter_fp<_CharT> _M_f;
2881#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2883 template<__format::__
char _CharT>
2884 struct formatter<__gnu_cxx::__bfloat16_t, _CharT>
2886 formatter() =
default;
2888 [[__gnu__::__always_inline__]]
2889 constexpr typename basic_format_parse_context<_CharT>::iterator
2890 parse(basic_format_parse_context<_CharT>& __pc)
2891 {
return _M_f.parse(__pc); }
2893 template<
typename _Out>
2894 typename basic_format_context<_Out, _CharT>::iterator
2895 format(__gnu_cxx::__bfloat16_t __u,
2896 basic_format_context<_Out, _CharT>& __fc)
const
2897 {
return _M_f.format((
float)__u, __fc); }
2900 __format::__formatter_fp<_CharT> _M_f;
2908 template<__format::__
char _CharT>
2909 struct formatter<const void*, _CharT>
2911 formatter() =
default;
2913 constexpr typename basic_format_parse_context<_CharT>::iterator
2914 parse(basic_format_parse_context<_CharT>& __pc)
2916 __format::_Spec<_CharT> __spec{};
2917 const auto __last = __pc.end();
2918 auto __first = __pc.begin();
2920 auto __finalize = [
this, &__spec] {
2924 auto __finished = [&] {
2925 if (__first == __last || *__first ==
'}')
2936 __first = __spec._M_parse_fill_and_align(__first, __last);
2942#if __glibcxx_format >= 202304L
2943 __first = __spec._M_parse_zero_fill(__first, __last);
2948 __first = __spec._M_parse_width(__first, __last, __pc);
2950 if (__first != __last)
2952 if (*__first ==
'p')
2954#if __glibcxx_format >= 202304L
2955 else if (*__first ==
'P')
2957 __spec._M_type = __format::_Pres_P;
2966 __format::__failed_to_parse_format_spec();
2969 template<
typename _Out>
2970 typename basic_format_context<_Out, _CharT>::iterator
2971 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2973 auto __u =
reinterpret_cast<__UINTPTR_TYPE__
>(__v);
2974 char __buf[2 +
sizeof(__v) * 2];
2975 auto [__ptr, __ec] = std::to_chars(__buf + 2,
std::end(__buf),
2977 int __n = __ptr - __buf;
2980#if __glibcxx_format >= 202304L
2981 if (_M_spec._M_type == __format::_Pres_P)
2984 for (
auto __p = __buf + 2; __p != __ptr; ++__p)
2985#
if __has_builtin(__builtin_toupper)
2986 *__p = __builtin_toupper(*__p);
2993 basic_string_view<_CharT> __str;
2994 if constexpr (is_same_v<_CharT, char>)
2995 __str = string_view(__buf, __n);
2996#ifdef _GLIBCXX_USE_WCHAR_T
2999 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
3000 std::__to_wstring_numeric(__buf, __n, __p);
3001 __str = wstring_view(__p, __n);
3005#if __glibcxx_format >= 202304L
3006 if (_M_spec._M_zero_fill)
3008 size_t __width = _M_spec._M_get_width(__fc);
3009 if (__width <= __str.size())
3010 return __format::__write(__fc.out(), __str);
3012 auto __out = __fc.out();
3014 __out = __format::__write(
std::move(__out), __str.substr(0, 2));
3015 __str.remove_prefix(2);
3016 size_t __nfill = __width - __n;
3017 return __format::__write_padded(
std::move(__out), __str,
3018 __format::_Align_right,
3019 __nfill, _CharT(
'0'));
3023 return __format::__write_padded_as_spec(__str, __n, __fc, _M_spec,
3024 __format::_Align_right);
3028 __format::_Spec<_CharT> _M_spec{};
3031 template<__format::__
char _CharT>
3032 struct formatter<void*, _CharT>
3034 formatter() =
default;
3036 [[__gnu__::__always_inline__]]
3037 constexpr typename basic_format_parse_context<_CharT>::iterator
3038 parse(basic_format_parse_context<_CharT>& __pc)
3039 {
return _M_f.parse(__pc); }
3041 template<
typename _Out>
3042 typename basic_format_context<_Out, _CharT>::iterator
3043 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3044 {
return _M_f.format(__v, __fc); }
3047 formatter<const void*, _CharT> _M_f;
3050 template<__format::__
char _CharT>
3051 struct formatter<nullptr_t, _CharT>
3053 formatter() =
default;
3055 [[__gnu__::__always_inline__]]
3056 constexpr typename basic_format_parse_context<_CharT>::iterator
3057 parse(basic_format_parse_context<_CharT>& __pc)
3058 {
return _M_f.parse(__pc); }
3060 template<
typename _Out>
3061 typename basic_format_context<_Out, _CharT>::iterator
3062 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3063 {
return _M_f.format(
nullptr, __fc); }
3066 formatter<const void*, _CharT> _M_f;
3070#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3074 struct __formatter_disabled
3076 __formatter_disabled() =
delete;
3077 __formatter_disabled(
const __formatter_disabled&) =
delete;
3078 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3082 struct formatter<char*,
wchar_t>
3083 :
private __formatter_disabled { };
3085 struct formatter<const char*,
wchar_t>
3086 :
private __formatter_disabled { };
3087 template<
size_t _Nm>
3088 struct formatter<char[_Nm], wchar_t>
3089 :
private __formatter_disabled { };
3090 template<
class _Traits,
class _Allocator>
3091 struct formatter<basic_string<char, _Traits, _Allocator>, wchar_t>
3092 :
private __formatter_disabled { };
3093 template<
class _Traits>
3094 struct formatter<basic_string_view<char, _Traits>, wchar_t>
3095 :
private __formatter_disabled { };
3100 template<
typename _Out>
3101 struct format_to_n_result
3104 iter_difference_t<_Out>
size;
3107_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3108template<
typename,
typename>
class vector;
3109_GLIBCXX_END_NAMESPACE_CONTAINER
3114 template<
typename _CharT>
3117 _Sink<_CharT>* _M_sink =
nullptr;
3120 using iterator_category = output_iterator_tag;
3121 using value_type = void;
3122 using difference_type = ptrdiff_t;
3123 using pointer = void;
3124 using reference = void;
3126 _Sink_iter() =
default;
3127 _Sink_iter(
const _Sink_iter&) =
default;
3128 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3130 [[__gnu__::__always_inline__]]
3132 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3134 [[__gnu__::__always_inline__]]
3135 constexpr _Sink_iter&
3136 operator=(_CharT __c)
3138 _M_sink->_M_write(__c);
3142 [[__gnu__::__always_inline__]]
3143 constexpr _Sink_iter&
3144 operator=(basic_string_view<_CharT> __s)
3146 _M_sink->_M_write(__s);
3150 [[__gnu__::__always_inline__]]
3151 constexpr _Sink_iter&
3154 [[__gnu__::__always_inline__]]
3155 constexpr _Sink_iter&
3156 operator++() {
return *
this; }
3158 [[__gnu__::__always_inline__]]
3159 constexpr _Sink_iter
3160 operator++(
int) {
return *
this; }
3163 _M_reserve(
size_t __n)
const
3164 {
return _M_sink->_M_reserve(__n); }
3170 template<
typename _CharT>
3173 friend class _Sink_iter<_CharT>;
3175 span<_CharT> _M_span;
3176 typename span<_CharT>::iterator _M_next;
3182 virtual void _M_overflow() = 0;
3186 [[__gnu__::__always_inline__]]
3188 _Sink(span<_CharT> __span) noexcept
3189 : _M_span(__span), _M_next(__span.begin())
3193 [[__gnu__::__always_inline__]]
3195 _M_used() const noexcept
3196 {
return _M_span.first(_M_next - _M_span.begin()); }
3199 [[__gnu__::__always_inline__]]
3200 constexpr span<_CharT>
3201 _M_unused() const noexcept
3202 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3205 [[__gnu__::__always_inline__]]
3207 _M_rewind() noexcept
3208 { _M_next = _M_span.begin(); }
3212 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3215 _M_next = __s.begin() + __pos;
3220 _M_write(_CharT __c)
3223 if (_M_next - _M_span.begin() == std::ssize(_M_span)) [[unlikely]]
3228 _M_write(basic_string_view<_CharT> __s)
3230 span __to = _M_unused();
3231 while (__to.size() <= __s.size())
3233 __s.copy(__to.data(), __to.size());
3234 _M_next += __to.size();
3235 __s.remove_prefix(__to.size());
3241 __s.copy(__to.data(), __s.size());
3242 _M_next += __s.size();
3251 explicit operator bool() const noexcept {
return _M_sink; }
3253 _CharT* get() const noexcept {
return _M_sink->_M_next.operator->(); }
3255 void _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3263 virtual _Reservation
3264 _M_reserve(
size_t __n)
3266 if (__n <= _M_unused().
size())
3269 if (__n <= _M_span.size())
3272 if (__n <= _M_unused().
size())
3285 _Sink(
const _Sink&) =
delete;
3286 _Sink& operator=(
const _Sink&) =
delete;
3288 [[__gnu__::__always_inline__]]
3289 constexpr _Sink_iter<_CharT>
3291 {
return _Sink_iter<_CharT>(*
this); }
3295 template<
typename _CharT>
3296 class _Fixedbuf_sink final :
public _Sink<_CharT>
3299 _M_overflow()
override
3301 __glibcxx_assert(
false);
3306 [[__gnu__::__always_inline__]]
3308 _Fixedbuf_sink(span<_CharT> __buf)
3309 : _Sink<_CharT>(__buf)
3312 constexpr basic_string_view<_CharT>
3315 auto __s = this->_M_used();
3316 return basic_string_view<_CharT>(__s.data(), __s.size());
3321 template<
typename _CharT>
3322 class _Buf_sink :
public _Sink<_CharT>
3325 _CharT _M_buf[__stackbuf_size<_CharT>];
3327 [[__gnu__::__always_inline__]]
3329 _Buf_sink() noexcept
3330 : _Sink<_CharT>(_M_buf)
3334 using _GLIBCXX_STD_C::vector;
3338 template<
typename _Seq>
3339 class _Seq_sink final :
public _Buf_sink<typename _Seq::value_type>
3341 using _CharT =
typename _Seq::value_type;
3347 _M_overflow()
override
3349 auto __s = this->_M_used();
3350 if (__s.empty()) [[unlikely]]
3354 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3356 if constexpr (__is_specialization_of<_Seq, basic_string>)
3357 _M_seq.append(__s.data(), __s.size());
3359 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3365 typename _Sink<_CharT>::_Reservation
3366 _M_reserve(
size_t __n)
override
3375 if constexpr (__is_specialization_of<_Seq, basic_string>
3376 || __is_specialization_of<_Seq, vector>)
3379 if (this->_M_used().
size()) [[unlikely]]
3380 _Seq_sink::_M_overflow();
3383 const auto __sz = _M_seq.size();
3384 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3385 _M_seq.__resize_and_overwrite(__sz + __n,
3386 [](
auto,
auto __n2) {
3390 _M_seq.resize(__sz + __n);
3394 this->_M_reset(_M_seq, __sz);
3398 return _Sink<_CharT>::_M_reserve(__n);
3402 _M_bump(
size_t __n)
override
3404 if constexpr (__is_specialization_of<_Seq, basic_string>
3405 || __is_specialization_of<_Seq, vector>)
3407 auto __s = this->_M_used();
3408 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3410 _M_seq.resize(__s.size() + __n);
3412 this->_M_reset(this->_M_buf);
3420 [[__gnu__::__always_inline__]]
3421 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3424 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3425 : _M_seq(std::
move(__s))
3428 using _Sink<_CharT>::out;
3433 if (this->_M_used().
size() != 0)
3434 _Seq_sink::_M_overflow();
3443 auto __s = this->_M_used();
3446 if (__s.size() != 0)
3447 _Seq_sink::_M_overflow();
3457 template<
typename _CharT,
typename _OutIter>
3458 class _Iter_sink :
public _Buf_sink<_CharT>
3461 iter_difference_t<_OutIter> _M_max;
3464 size_t _M_count = 0;
3467 _M_overflow()
override
3469 auto __s = this->_M_used();
3471 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3472 else if (_M_count <
static_cast<size_t>(_M_max))
3474 auto __max = _M_max - _M_count;
3475 span<_CharT> __first;
3476 if (__max < __s.size())
3477 __first = __s.first(
static_cast<size_t>(__max));
3480 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3483 _M_count += __s.size();
3487 [[__gnu__::__always_inline__]]
3489 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3490 : _M_out(std::
move(__out)), _M_max(__max)
3493 using _Sink<_CharT>::out;
3495 format_to_n_result<_OutIter>
3498 if (this->_M_used().
size() != 0)
3499 _Iter_sink::_M_overflow();
3500 iter_difference_t<_OutIter> __count(_M_count);
3512 template<
typename _CharT, contiguous_iterator _OutIter>
3513 requires same_as<iter_value_t<_OutIter>, _CharT>
3514 class _Iter_sink<_CharT, _OutIter> :
public _Sink<_CharT>
3517 iter_difference_t<_OutIter> _M_max = -1;
3519 size_t _M_count = 0;
3525 _M_overflow()
override
3527 if (this->_M_unused().
size() != 0)
3530 auto __s = this->_M_used();
3534 _M_count += __s.size();
3538 this->_M_reset(this->_M_buf);
3544 this->_M_reset({__s.data(), __s.size() + 1024}, __s.size());
3548 typename _Sink<_CharT>::_Reservation
3549 _M_reserve(
size_t __n)
final
3551 auto __avail = this->_M_unused();
3552 if (__n > __avail.size())
3557 auto __s = this->_M_used();
3558 this->_M_reset({__s.data(), __s.size() + __n}, __s.size());
3565 _S_make_span(_CharT* __ptr, iter_difference_t<_OutIter> __n,
3566 span<_CharT> __buf)
noexcept
3573 if constexpr (!is_integral_v<iter_difference_t<_OutIter>>
3574 ||
sizeof(__n) >
sizeof(size_t))
3577 auto __m = iter_difference_t<_OutIter>((
size_t)-1);
3581 return {__ptr, (size_t)__n};
3584#if __has_builtin(__builtin_dynamic_object_size)
3585 if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3586 return {__ptr, __bytes /
sizeof(_CharT)};
3589 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3590 __n = (1024 - __off) /
sizeof(_CharT);
3591 if (__n > 0) [[likely]]
3592 return {__ptr,
static_cast<size_t>(__n)};
3599 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1) noexcept
3600 : _Sink<_CharT>(_S_make_span(std::to_address(__out), __n, _M_buf)),
3601 _M_first(__out), _M_max(__n)
3604 format_to_n_result<_OutIter>
3607 auto __s = this->_M_used();
3608 if (__s.data() == _M_buf)
3611 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3612 return { _M_first + _M_max, __count };
3616 iter_difference_t<_OutIter> __count(__s.size());
3617 return { _M_first + __count, __count };
3622 template<
typename _Context>
3625 using _CharT =
typename _Context::char_type;
3641 unsigned long long _M_ull;
3644#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3645 long double _M_ldbl;
3647 const _CharT* _M_str;
3648 basic_string_view<_CharT> _M_sv;
3650 _HandleBase _M_handle;
3651#ifdef __SIZEOF_INT128__
3653 unsigned __int128 _M_u128;
3655#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3658#elif _GLIBCXX_FORMAT_F128 == 2
3659 __float128_t _M_f128;
3663 [[__gnu__::__always_inline__]]
3664 _Arg_value() : _M_none() { }
3667 template<
typename _Tp>
3668 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
3669 { _S_get<_Tp>() = __val; }
3672 template<
typename _Tp,
typename _Self>
3673 [[__gnu__::__always_inline__]]
3675 _S_get(_Self& __u)
noexcept
3677 if constexpr (is_same_v<_Tp, bool>)
3679 else if constexpr (is_same_v<_Tp, _CharT>)
3681 else if constexpr (is_same_v<_Tp, int>)
3683 else if constexpr (is_same_v<_Tp, unsigned>)
3685 else if constexpr (is_same_v<_Tp, long long>)
3687 else if constexpr (is_same_v<_Tp, unsigned long long>)
3689 else if constexpr (is_same_v<_Tp, float>)
3691 else if constexpr (is_same_v<_Tp, double>)
3693#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3694 else if constexpr (is_same_v<_Tp, long double>)
3697 else if constexpr (is_same_v<_Tp, __ieee128>)
3699 else if constexpr (is_same_v<_Tp, __ibm128>)
3700 return __u._M_ibm128;
3702 else if constexpr (is_same_v<_Tp, const _CharT*>)
3704 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
3706 else if constexpr (is_same_v<_Tp, const void*>)
3708#ifdef __SIZEOF_INT128__
3709 else if constexpr (is_same_v<_Tp, __int128>)
3711 else if constexpr (is_same_v<_Tp, unsigned __int128>)
3714#if _GLIBCXX_FORMAT_F128 == 2
3715 else if constexpr (is_same_v<_Tp, __float128_t>)
3718 else if constexpr (derived_from<_Tp, _HandleBase>)
3719 return static_cast<_Tp&
>(__u._M_handle);
3723 template<
typename _Tp>
3724 [[__gnu__::__always_inline__]]
3727 {
return _S_get<_Tp>(*
this); }
3729 template<
typename _Tp>
3730 [[__gnu__::__always_inline__]]
3732 _M_get() const noexcept
3733 {
return _S_get<_Tp>(*
this); }
3735 template<
typename _Tp>
3736 [[__gnu__::__always_inline__]]
3738 _M_set(_Tp __v)
noexcept
3740 if constexpr (derived_from<_Tp, _HandleBase>)
3741 std::construct_at(&_M_handle, __v);
3743 _S_get<_Tp>(*
this) = __v;
3748 template<
typename _Context,
typename... _Args>
3751 template<
typename _Visitor,
typename _Ctx>
3752 decltype(
auto) __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
3754 template<
typename _Ch,
typename _Tp>
3756 __to_arg_t_enum() noexcept;
3760 template<typename _Context>
3761 class basic_format_arg
3763 using _CharT =
typename _Context::char_type;
3765 template<
typename _Tp>
3766 static constexpr bool __formattable
3767 = __format::__formattable_with<_Tp, _Context>;
3770 class handle :
public __format::_Arg_value<_Context>::_HandleBase
3772 using _Base =
typename __format::_Arg_value<_Context>::_HandleBase;
3775 template<
typename _Tp>
3776 using __maybe_const_t
3777 = __conditional_t<__formattable<const _Tp>,
const _Tp, _Tp>;
3779 template<
typename _Tq>
3781 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
3782 _Context& __format_ctx,
const void* __ptr)
3784 using _Td = remove_const_t<_Tq>;
3785 typename _Context::template formatter_type<_Td> __f;
3786 __parse_ctx.advance_to(__f.parse(__parse_ctx));
3787 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
3788 __format_ctx.advance_to(__f.format(__val, __format_ctx));
3791 template<
typename _Tp>
3793 handle(_Tp& __val)
noexcept
3795 this->_M_ptr = __builtin_addressof(__val);
3796 auto __func = _S_format<__maybe_const_t<_Tp>>;
3797 this->_M_func =
reinterpret_cast<void(*)()
>(__func);
3800 friend class basic_format_arg<_Context>;
3803 handle(
const handle&) =
default;
3804 handle& operator=(
const handle&) =
default;
3806 [[__gnu__::__always_inline__]]
3808 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
3810 using _Func = void(*)(basic_format_parse_context<_CharT>&,
3811 _Context&,
const void*);
3812 auto __f =
reinterpret_cast<_Func
>(this->_M_func);
3813 __f(__pc, __fc, this->_M_ptr);
3817 [[__gnu__::__always_inline__]]
3818 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
3820 [[nodiscard,__gnu__::__always_inline__]]
3821 explicit operator bool() const noexcept
3822 {
return _M_type != __format::_Arg_none; }
3824#if __cpp_lib_format >= 202306L
3825 template<
typename _Visitor>
3827 visit(
this basic_format_arg __arg, _Visitor&& __vis)
3830 template<
typename _Res,
typename _Visitor>
3832 visit(
this basic_format_arg __arg, _Visitor&& __vis)
3837 template<
typename _Ctx>
3838 friend class basic_format_args;
3840 template<
typename _Ctx,
typename... _Args>
3841 friend class __format::_Arg_store;
3843 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
3845 __format::_Arg_value<_Context> _M_val;
3846 __format::_Arg_t _M_type;
3851 template<
typename _Tp>
3852 static consteval auto
3855 using _Td = remove_const_t<_Tp>;
3856 if constexpr (is_same_v<_Td, bool>)
3857 return type_identity<bool>();
3858 else if constexpr (is_same_v<_Td, _CharT>)
3859 return type_identity<_CharT>();
3860 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
3861 return type_identity<_CharT>();
3862#ifdef __SIZEOF_INT128__
3863 else if constexpr (is_same_v<_Td, __int128>)
3864 return type_identity<__int128>();
3865 else if constexpr (is_same_v<_Td, unsigned __int128>)
3866 return type_identity<unsigned __int128>();
3868 else if constexpr (__is_signed_integer<_Td>::value)
3870 if constexpr (
sizeof(_Td) <=
sizeof(int))
3871 return type_identity<int>();
3872 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
3873 return type_identity<long long>();
3875 else if constexpr (__is_unsigned_integer<_Td>::value)
3877 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
3878 return type_identity<unsigned>();
3879 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
3880 return type_identity<unsigned long long>();
3882 else if constexpr (is_same_v<_Td, float>)
3883 return type_identity<float>();
3884 else if constexpr (is_same_v<_Td, double>)
3885 return type_identity<double>();
3886#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3887 else if constexpr (is_same_v<_Td, long double>)
3888 return type_identity<long double>();
3890 else if constexpr (is_same_v<_Td, __ibm128>)
3891 return type_identity<__ibm128>();
3892 else if constexpr (is_same_v<_Td, __ieee128>)
3893 return type_identity<__ieee128>();
3896#if defined(__FLT16_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3897 else if constexpr (is_same_v<_Td, _Float16>)
3898 return type_identity<float>();
3901#if defined(__BFLT16_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3902 else if constexpr (is_same_v<_Td,
decltype(0.0bf16)>)
3903 return type_identity<float>();
3906#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3907 else if constexpr (is_same_v<_Td, _Float32>)
3908 return type_identity<float>();
3911#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3912 else if constexpr (is_same_v<_Td, _Float64>)
3913 return type_identity<double>();
3916#if _GLIBCXX_FORMAT_F128
3918 else if constexpr (is_same_v<_Td, _Float128>)
3919 return type_identity<__format::__float128_t>();
3921# if __SIZEOF_FLOAT128__
3922 else if constexpr (is_same_v<_Td, __float128>)
3923 return type_identity<__format::__float128_t>();
3926 else if constexpr (__is_specialization_of<_Td, basic_string_view>
3927 || __is_specialization_of<_Td, basic_string>)
3929 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
3930 return type_identity<basic_string_view<_CharT>>();
3932 return type_identity<handle>();
3934 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
3935 return type_identity<const _CharT*>();
3936 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
3937 return type_identity<const _CharT*>();
3938 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
3939 return type_identity<const void*>();
3940 else if constexpr (is_same_v<_Td, nullptr_t>)
3941 return type_identity<const void*>();
3943 return type_identity<handle>();
3947 template<
typename _Tp>
3948 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
3951 template<
typename _Tp>
3952 static consteval __format::_Arg_t
3955 using namespace __format;
3956 if constexpr (is_same_v<_Tp, bool>)
3958 else if constexpr (is_same_v<_Tp, _CharT>)
3960 else if constexpr (is_same_v<_Tp, int>)
3962 else if constexpr (is_same_v<_Tp, unsigned>)
3964 else if constexpr (is_same_v<_Tp, long long>)
3966 else if constexpr (is_same_v<_Tp, unsigned long long>)
3968 else if constexpr (is_same_v<_Tp, float>)
3970 else if constexpr (is_same_v<_Tp, double>)
3972#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3973 else if constexpr (is_same_v<_Tp, long double>)
3977 else if constexpr (is_same_v<_Tp, __ibm128>)
3979 else if constexpr (is_same_v<_Tp, __ieee128>)
3982 else if constexpr (is_same_v<_Tp, const _CharT*>)
3984 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
3986 else if constexpr (is_same_v<_Tp, const void*>)
3988#ifdef __SIZEOF_INT128__
3989 else if constexpr (is_same_v<_Tp, __int128>)
3991 else if constexpr (is_same_v<_Tp, unsigned __int128>)
3995#if _GLIBCXX_FORMAT_F128 == 2
3996 else if constexpr (is_same_v<_Tp, __format::__float128_t>)
3999 else if constexpr (is_same_v<_Tp, handle>)
4003 template<
typename _Tp>
4005 _M_set(_Tp __v)
noexcept
4007 _M_type = _S_to_enum<_Tp>();
4011 template<
typename _Tp>
4012 requires __format::__formattable_with<_Tp, _Context>
4014 basic_format_arg(_Tp& __v)
noexcept
4016 using _Td = _Normalize<_Tp>;
4017 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4018 _M_set(_Td{__v.data(), __v.size()});
4019 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4020 && is_same_v<_CharT, wchar_t>)
4021 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4023 _M_set(
static_cast<_Td
>(__v));
4026 template<
typename _Ctx,
typename... _Argz>
4028 make_format_args(_Argz&...)
noexcept;
4030 template<
typename _Visitor,
typename _Ctx>
4031 friend decltype(
auto)
4032 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4034 template<
typename _Visitor,
typename _Ctx>
4035 friend decltype(
auto)
4036 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4038 template<
typename _Ch,
typename _Tp>
4039 friend consteval __format::_Arg_t
4040 __format::__to_arg_t_enum() noexcept;
4042 template<typename _Visitor>
4044 _M_visit(_Visitor&& __vis, __format::_Arg_t __type)
4046 using namespace __format;
4063#if __glibcxx_to_chars
4068#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4086 auto& __h =
static_cast<handle&
>(_M_val._M_handle);
4089#ifdef __SIZEOF_INT128__
4096#if _GLIBCXX_FORMAT_F128 == 2
4103 __builtin_unreachable();
4107 template<
typename _Visitor>
4109 _M_visit_user(_Visitor&& __vis, __format::_Arg_t __type)
4111 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4113 constexpr bool __user_facing = __is_one_of<_Tp,
4114 monostate, bool, _CharT,
4115 int,
unsigned int,
long long int,
unsigned long long int,
4116 float, double,
long double,
4117 const _CharT*, basic_string_view<_CharT>,
4118 const void*, handle>::value;
4119 if constexpr (__user_facing)
4130 template<
typename _Visitor,
typename _Context>
4131 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4132 inline decltype(auto)
4133 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4141 template<
typename _Visitor,
typename _Ctx>
4142 inline decltype(
auto)
4143 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4148 struct _WidthPrecVisitor
4150 template<
typename _Tp>
4152 operator()(_Tp& __arg)
const
4154 if constexpr (is_same_v<_Tp, monostate>)
4155 __format::__invalid_arg_id_in_format_string();
4159 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4163 if constexpr (__is_unsigned_integer<_Tp>::value)
4165 else if constexpr (__is_signed_integer<_Tp>::value)
4169 __throw_format_error(
"format error: argument used for width or "
4170 "precision must be a non-negative integer");
4174#pragma GCC diagnostic push
4175#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4176 template<
typename _Context>
4178 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4179 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4182 template<
int _Bits,
size_t _Nm>
4184 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4186 __UINT64_TYPE__ __packed_types = 0;
4187 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4188 __packed_types = (__packed_types << _Bits) | *__i;
4189 return __packed_types;
4194 template<
typename _Context>
4195 class basic_format_args
4197 static constexpr int _S_packed_type_bits = 5;
4198 static constexpr int _S_packed_type_mask = 0b11111;
4199 static constexpr int _S_max_packed_args = 12;
4201 static_assert( __format::_Arg_max_ <= (1 << _S_packed_type_bits) );
4203 template<
typename... _Args>
4204 using _Store = __format::_Arg_store<_Context, _Args...>;
4206 template<
typename _Ctx,
typename... _Args>
4207 friend class __format::_Arg_store;
4209 using uint64_t = __UINT64_TYPE__;
4210 using _Format_arg = basic_format_arg<_Context>;
4211 using _Format_arg_val = __format::_Arg_value<_Context>;
4217 uint64_t _M_packed_size : 4;
4218 uint64_t _M_unpacked_size : 60;
4221 const _Format_arg_val* _M_values;
4222 const _Format_arg* _M_args;
4226 _M_size() const noexcept
4227 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4229 typename __format::_Arg_t
4230 _M_type(
size_t __i)
const noexcept
4232 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4233 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4236 template<
typename _Ctx,
typename... _Args>
4238 make_format_args(_Args&...)
noexcept;
4241 template<
typename... _Args>
4242 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4244 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4247 template<
typename... _Args>
4248 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4250 [[nodiscard,__gnu__::__always_inline__]]
4251 basic_format_arg<_Context>
4252 get(
size_t __i)
const noexcept
4254 basic_format_arg<_Context> __arg;
4255 if (__i < _M_packed_size)
4257 __arg._M_type = _M_type(__i);
4258 __arg._M_val = _M_values[__i];
4260 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4261 __arg = _M_args[__i];
4268 template<
typename _Context,
typename... _Args>
4269 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4270 -> basic_format_args<_Context>;
4272 template<
typename _Context,
typename... _Args>
4274 make_format_args(_Args&... __fmt_args)
noexcept;
4277 template<
typename _Context,
typename... _Args>
4278 class __format::_Arg_store
4280 friend std::basic_format_args<_Context>;
4282 template<
typename _Ctx,
typename... _Argz>
4284#if _GLIBCXX_INLINE_VERSION
4287 make_format_args(_Argz&...)
noexcept;
4291 static constexpr bool _S_values_only
4292 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4295 = __conditional_t<_S_values_only,
4296 __format::_Arg_value<_Context>,
4297 basic_format_arg<_Context>>;
4299 _Element_t _M_args[
sizeof...(_Args)];
4301 template<
typename _Tp>
4303 _S_make_elt(_Tp& __v)
4305 using _Tq = remove_const_t<_Tp>;
4306 using _CharT =
typename _Context::char_type;
4307 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4308 "std::formatter must be specialized for the type "
4309 "of each format arg");
4310 using __format::__formattable_with;
4311 if constexpr (is_const_v<_Tp>)
4312 if constexpr (!__formattable_with<_Tp, _Context>)
4313 if constexpr (__formattable_with<_Tq, _Context>)
4314 static_assert(__formattable_with<_Tp, _Context>,
4315 "format arg must be non-const because its "
4316 "std::formatter specialization has a "
4317 "non-const reference parameter");
4318 basic_format_arg<_Context> __arg(__v);
4319 if constexpr (_S_values_only)
4320 return __arg._M_val;
4325 template<
typename... _Tp>
4326 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4327 [[__gnu__::__always_inline__]]
4328 _Arg_store(_Tp&... __a) noexcept
4329 : _M_args{_S_make_elt(__a)...}
4333 template<
typename _Context>
4334 class __format::_Arg_store<_Context>
4337 template<
typename _Context>
4338 template<
typename... _Args>
4340 basic_format_args<_Context>::
4341 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4343 if constexpr (
sizeof...(_Args) == 0)
4346 _M_unpacked_size = 0;
4349 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4352 _M_packed_size =
sizeof...(_Args);
4355 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4357 _M_values = __store._M_args;
4364 _M_unpacked_size =
sizeof...(_Args);
4366 _M_args = __store._M_args;
4371 template<
typename _Context = format_context,
typename... _Args>
4372 [[nodiscard,__gnu__::__always_inline__]]
4374 make_format_args(_Args&... __fmt_args)
noexcept
4376 using _Fmt_arg = basic_format_arg<_Context>;
4377 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4378 _Normalize<_Args>...>;
4379 return _Store(__fmt_args...);
4382#ifdef _GLIBCXX_USE_WCHAR_T
4384 template<
typename... _Args>
4385 [[nodiscard,__gnu__::__always_inline__]]
4387 make_wformat_args(_Args&... __args)
noexcept
4388 {
return std::make_format_args<wformat_context>(__args...); }
4394 template<
typename _Out,
typename _CharT,
typename _Context>
4396 __do_vformat_to(_Out, basic_string_view<_CharT>,
4397 const basic_format_args<_Context>&,
4398 const locale* =
nullptr);
4400 template<
typename _CharT>
struct __formatter_chrono;
4418 template<
typename _Out,
typename _CharT>
4419 class basic_format_context
4421 static_assert( output_iterator<_Out, const _CharT&> );
4423 basic_format_args<basic_format_context> _M_args;
4425 __format::_Optional_locale _M_loc;
4427 basic_format_context(basic_format_args<basic_format_context> __args,
4429 : _M_args(__args), _M_out(std::
move(__out))
4432 basic_format_context(basic_format_args<basic_format_context> __args,
4433 _Out __out,
const std::locale& __loc)
4434 : _M_args(__args), _M_out(std::
move(__out)), _M_loc(__loc)
4440 basic_format_context(
const basic_format_context&) =
delete;
4441 basic_format_context& operator=(
const basic_format_context&) =
delete;
4443 template<
typename _Out2,
typename _CharT2,
typename _Context2>
4445 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
4446 const basic_format_args<_Context2>&,
4449 friend __format::__formatter_chrono<_CharT>;
4452 ~basic_format_context() =
default;
4454 using iterator = _Out;
4455 using char_type = _CharT;
4456 template<
typename _Tp>
4457 using formatter_type = formatter<_Tp, _CharT>;
4460 basic_format_arg<basic_format_context>
4461 arg(
size_t __id)
const noexcept
4462 {
return _M_args.get(__id); }
4465 std::locale locale() {
return _M_loc.value(); }
4468 iterator out() {
return std::move(_M_out); }
4470 void advance_to(iterator __it) { _M_out =
std::move(__it); }
4482 template<
typename _CharT>
4485 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
4487 typename basic_format_parse_context<_CharT>::_Scan_parse_context _M_pc;
4490 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
4491 : _M_pc(__str, __nargs)
4494 constexpr iterator
begin() const noexcept {
return _M_pc.begin(); }
4495 constexpr iterator
end() const noexcept {
return _M_pc.end(); }
4500 basic_string_view<_CharT> __fmt = _M_fmt_str();
4502 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
4504 _M_pc.advance_to(
begin() + 1);
4505 _M_format_arg(_M_pc.next_arg_id());
4509 size_t __lbr = __fmt.find(
'{');
4510 size_t __rbr = __fmt.find(
'}');
4512 while (__fmt.size())
4514 auto __cmp = __lbr <=> __rbr;
4518 _M_pc.advance_to(
end());
4523 if (__lbr + 1 == __fmt.size()
4524 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
4525 __format::__unmatched_left_brace_in_format_string();
4526 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
4527 iterator __last =
begin() + __lbr + int(__is_escape);
4528 _M_on_chars(__last);
4529 _M_pc.advance_to(__last + 1);
4530 __fmt = _M_fmt_str();
4533 if (__rbr != __fmt.npos)
4535 __lbr = __fmt.find(
'{');
4539 _M_on_replacement_field();
4540 __fmt = _M_fmt_str();
4541 __lbr = __fmt.find(
'{');
4542 __rbr = __fmt.find(
'}');
4547 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
4548 __format::__unmatched_right_brace_in_format_string();
4549 iterator __last =
begin() + __rbr;
4550 _M_on_chars(__last);
4551 _M_pc.advance_to(__last + 1);
4552 __fmt = _M_fmt_str();
4553 if (__lbr != __fmt.npos)
4555 __rbr = __fmt.find(
'}');
4560 constexpr basic_string_view<_CharT>
4561 _M_fmt_str() const noexcept
4564 constexpr virtual void _M_on_chars(iterator) { }
4566 constexpr void _M_on_replacement_field()
4568 auto __next =
begin();
4572 __id = _M_pc.next_arg_id();
4573 else if (*__next ==
':')
4575 __id = _M_pc.next_arg_id();
4576 _M_pc.advance_to(++__next);
4580 auto [__i, __ptr] = __format::__parse_arg_id(
begin(),
end());
4581 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
4582 __format::__invalid_arg_id_in_format_string();
4583 _M_pc.check_arg_id(__id = __i);
4586 _M_pc.advance_to(++__ptr);
4589 _M_pc.advance_to(__ptr);
4591 _M_format_arg(__id);
4593 __format::__unmatched_left_brace_in_format_string();
4594 _M_pc.advance_to(
begin() + 1);
4597 constexpr virtual void _M_format_arg(
size_t __id) = 0;
4601 template<
typename _Out,
typename _CharT>
4602 class _Formatting_scanner :
public _Scanner<_CharT>
4605 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
4606 basic_string_view<_CharT> __str)
4607 : _Scanner<_CharT>(__str), _M_fc(__fc)
4611 basic_format_context<_Out, _CharT>& _M_fc;
4613 using iterator =
typename _Scanner<_CharT>::iterator;
4616 _M_on_chars(iterator __last)
override
4618 basic_string_view<_CharT> __str(this->
begin(), __last);
4619 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
4623 _M_format_arg(
size_t __id)
override
4625 using _Context = basic_format_context<_Out, _CharT>;
4626 using handle =
typename basic_format_arg<_Context>::handle;
4628 __format::__visit_format_arg([
this](
auto& __arg) {
4630 using _Formatter =
typename _Context::template formatter_type<_Type>;
4631 if constexpr (is_same_v<_Type, monostate>)
4632 __format::__invalid_arg_id_in_format_string();
4633 else if constexpr (is_same_v<_Type, handle>)
4634 __arg.format(this->_M_pc, this->_M_fc);
4635 else if constexpr (is_default_constructible_v<_Formatter>)
4638 this->_M_pc.advance_to(__f.parse(this->_M_pc));
4639 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
4642 static_assert(__format::__formattable_with<_Type, _Context>);
4643 }, _M_fc.arg(__id));
4647 template<
typename _CharT,
typename _Tp>
4649 __to_arg_t_enum() noexcept
4651 using _Context = __format::__format_context<_CharT>;
4652 using _Fmt_arg = basic_format_arg<_Context>;
4653 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
4654 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
4658 template<
typename _CharT,
typename... _Args>
4659 class _Checking_scanner :
public _Scanner<_CharT>
4662 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
4663 "std::formatter must be specialized for each type being formatted");
4667 _Checking_scanner(basic_string_view<_CharT> __str)
4668 : _Scanner<_CharT>(__str, sizeof...(_Args))
4670#if __cpp_lib_format >= 202305L
4671 this->_M_pc._M_types = _M_types.data();
4677 _M_format_arg(
size_t __id)
override
4679 if constexpr (
sizeof...(_Args) != 0)
4681 if (__id <
sizeof...(_Args))
4683 _M_parse_format_spec<_Args...>(__id);
4687 __builtin_unreachable();
4690 template<
typename _Tp,
typename... _OtherArgs>
4692 _M_parse_format_spec(
size_t __id)
4696 formatter<_Tp, _CharT> __f;
4697 this->_M_pc.advance_to(__f.parse(this->_M_pc));
4699 else if constexpr (
sizeof...(_OtherArgs) != 0)
4700 _M_parse_format_spec<_OtherArgs...>(__id - 1);
4702 __builtin_unreachable();
4705#if __cpp_lib_format >= 202305L
4706 array<_Arg_t,
sizeof...(_Args)>
4707 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
4711 template<
typename _Out,
typename _CharT,
typename _Context>
4713 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
4714 const basic_format_args<_Context>& __args,
4715 const locale* __loc)
4717 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
4718 _Sink_iter<_CharT> __sink_out;
4720 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4723 __sink_out = __sink.out();
4725 if constexpr (is_same_v<_CharT, char>)
4727 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
4729 bool __done =
false;
4730 __format::__visit_format_arg([&](
auto& __arg) {
4731 using _Tp = remove_cvref_t<
decltype(__arg)>;
4732 if constexpr (is_same_v<_Tp, bool>)
4734 size_t __len = 4 + !__arg;
4735 const char* __chars[] = {
"false",
"true" };
4736 if (
auto __res = __sink_out._M_reserve(__len))
4738 __builtin_memcpy(__res.get(), __chars[__arg], __len);
4739 __res._M_bump(__len);
4743 else if constexpr (is_same_v<_Tp, char>)
4745 if (
auto __res = __sink_out._M_reserve(1))
4747 *__res.get() = __arg;
4752 else if constexpr (is_integral_v<_Tp>)
4754 make_unsigned_t<_Tp> __uval;
4755 const bool __neg = __arg < 0;
4757 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
4760 const auto __n = __detail::__to_chars_len(__uval);
4761 if (
auto __res = __sink_out._M_reserve(__n + __neg))
4763 auto __ptr = __res.get();
4765 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
4767 __res._M_bump(__n + __neg);
4771 else if constexpr (is_convertible_v<_Tp, string_view>)
4773 string_view __sv = __arg;
4774 if (
auto __res = __sink_out._M_reserve(__sv.size()))
4776 __builtin_memcpy(__res.get(), __sv.data(), __sv.size());
4777 __res._M_bump(__sv.size());
4785 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4788 return std::move(__sink)._M_finish().out;
4792 auto __ctx = __loc ==
nullptr
4793 ? _Context(__args, __sink_out)
4794 : _Context(__args, __sink_out, *__loc);
4795 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
4796 __scanner._M_scan();
4798 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4801 return std::move(__sink)._M_finish().out;
4803#pragma GCC diagnostic pop
4808 template<
typename _CharT,
typename... _Args>
4809 template<
typename _Tp>
4810 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
4812 basic_format_string<_CharT, _Args...>::
4813 basic_format_string(
const _Tp& __s)
4816 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
4818 __scanner._M_scan();
4823 template<
typename _Out>
requires output_iterator<_Out, const char&>
4824 [[__gnu__::__always_inline__]]
4826 vformat_to(_Out __out, string_view __fmt, format_args __args)
4827 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
4829#ifdef _GLIBCXX_USE_WCHAR_T
4830 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
4831 [[__gnu__::__always_inline__]]
4833 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
4834 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
4837 template<
typename _Out>
requires output_iterator<_Out, const char&>
4838 [[__gnu__::__always_inline__]]
4840 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
4843 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
4846#ifdef _GLIBCXX_USE_WCHAR_T
4847 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
4848 [[__gnu__::__always_inline__]]
4850 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
4851 wformat_args __args)
4853 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
4859 vformat(string_view __fmt, format_args __args)
4861 __format::_Str_sink<char> __buf;
4862 std::vformat_to(__buf.out(), __fmt, __args);
4866#ifdef _GLIBCXX_USE_WCHAR_T
4869 vformat(wstring_view __fmt, wformat_args __args)
4871 __format::_Str_sink<wchar_t> __buf;
4872 std::vformat_to(__buf.out(), __fmt, __args);
4879 vformat(
const locale& __loc, string_view __fmt, format_args __args)
4881 __format::_Str_sink<char> __buf;
4882 std::vformat_to(__buf.out(), __loc, __fmt, __args);
4886#ifdef _GLIBCXX_USE_WCHAR_T
4889 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
4891 __format::_Str_sink<wchar_t> __buf;
4892 std::vformat_to(__buf.out(), __loc, __fmt, __args);
4897 template<
typename... _Args>
4900 format(format_string<_Args...> __fmt, _Args&&... __args)
4901 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
4903#ifdef _GLIBCXX_USE_WCHAR_T
4904 template<
typename... _Args>
4907 format(wformat_string<_Args...> __fmt, _Args&&... __args)
4908 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
4911 template<
typename... _Args>
4914 format(
const locale& __loc, format_string<_Args...> __fmt,
4917 return std::vformat(__loc, __fmt.get(),
4918 std::make_format_args(__args...));
4921#ifdef _GLIBCXX_USE_WCHAR_T
4922 template<
typename... _Args>
4925 format(
const locale& __loc, wformat_string<_Args...> __fmt,
4928 return std::vformat(__loc, __fmt.get(),
4929 std::make_wformat_args(__args...));
4933 template<
typename _Out,
typename... _Args>
4934 requires output_iterator<_Out, const char&>
4936 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
4938 return std::vformat_to(
std::move(__out), __fmt.get(),
4939 std::make_format_args(__args...));
4942#ifdef _GLIBCXX_USE_WCHAR_T
4943 template<
typename _Out,
typename... _Args>
4944 requires output_iterator<_Out, const wchar_t&>
4946 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
4948 return std::vformat_to(
std::move(__out), __fmt.get(),
4949 std::make_wformat_args(__args...));
4953 template<
typename _Out,
typename... _Args>
4954 requires output_iterator<_Out, const char&>
4956 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
4959 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
4960 std::make_format_args(__args...));
4963#ifdef _GLIBCXX_USE_WCHAR_T
4964 template<
typename _Out,
typename... _Args>
4965 requires output_iterator<_Out, const wchar_t&>
4967 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
4970 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
4971 std::make_wformat_args(__args...));
4975 template<
typename _Out,
typename... _Args>
4976 requires output_iterator<_Out, const char&>
4977 inline format_to_n_result<_Out>
4978 format_to_n(_Out __out, iter_difference_t<_Out> __n,
4979 format_string<_Args...> __fmt, _Args&&... __args)
4981 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
4982 std::vformat_to(__sink.out(), __fmt.get(),
4983 std::make_format_args(__args...));
4987#ifdef _GLIBCXX_USE_WCHAR_T
4988 template<
typename _Out,
typename... _Args>
4989 requires output_iterator<_Out, const wchar_t&>
4990 inline format_to_n_result<_Out>
4991 format_to_n(_Out __out, iter_difference_t<_Out> __n,
4992 wformat_string<_Args...> __fmt, _Args&&... __args)
4994 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
4995 std::vformat_to(__sink.out(), __fmt.get(),
4996 std::make_wformat_args(__args...));
5001 template<
typename _Out,
typename... _Args>
5002 requires output_iterator<_Out, const char&>
5003 inline format_to_n_result<_Out>
5004 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5005 format_string<_Args...> __fmt, _Args&&... __args)
5007 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
5008 std::vformat_to(__sink.out(), __loc, __fmt.get(),
5009 std::make_format_args(__args...));
5013#ifdef _GLIBCXX_USE_WCHAR_T
5014 template<
typename _Out,
typename... _Args>
5015 requires output_iterator<_Out, const wchar_t&>
5016 inline format_to_n_result<_Out>
5017 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5018 wformat_string<_Args...> __fmt, _Args&&... __args)
5020 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
5021 std::vformat_to(__sink.out(), __loc, __fmt.get(),
5022 std::make_wformat_args(__args...));
5031 template<
typename _CharT>
5032 class _Counting_sink final :
public _Iter_sink<_CharT, _CharT*>
5035 _Counting_sink() : _Iter_sink<_CharT, _CharT*>(nullptr, 0) { }
5037 [[__gnu__::__always_inline__]]
5040 {
return this->_M_count + this->_M_used().size(); }
5043 template<
typename _CharT>
5044 class _Counting_sink :
public _Buf_sink<_CharT>
5046 size_t _M_count = 0;
5049 _M_overflow()
override
5051 if (!std::is_constant_evaluated())
5052 _M_count += this->_M_used().size();
5057 _Counting_sink() =
default;
5059 [[__gnu__::__always_inline__]]
5063 _Counting_sink::_M_overflow();
5071 template<
typename... _Args>
5074 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5076 __format::_Counting_sink<char> __buf;
5077 std::vformat_to(__buf.out(), __fmt.get(),
5078 std::make_format_args(__args...));
5079 return __buf.count();
5082#ifdef _GLIBCXX_USE_WCHAR_T
5083 template<
typename... _Args>
5086 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5088 __format::_Counting_sink<wchar_t> __buf;
5089 std::vformat_to(__buf.out(), __fmt.get(),
5090 std::make_wformat_args(__args...));
5091 return __buf.count();
5095 template<
typename... _Args>
5098 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5101 __format::_Counting_sink<char> __buf;
5102 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5103 std::make_format_args(__args...));
5104 return __buf.count();
5107#ifdef _GLIBCXX_USE_WCHAR_T
5108 template<
typename... _Args>
5111 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5114 __format::_Counting_sink<wchar_t> __buf;
5115 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5116 std::make_wformat_args(__args...));
5117 return __buf.count();
5121#if __glibcxx_format_ranges
5124 enum class range_format {
5142 template<
typename _Rg>
5143 constexpr auto format_kind = []{
5144 static_assert(
false,
"cannot use primary template of 'std::format_kind'");
5145 return type_identity<_Rg>{};
5149 template<
typename _Tp>
5150 consteval range_format
5153 using _Ref = ranges::range_reference_t<_Tp>;
5154 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5155 return range_format::disabled;
5156 else if constexpr (
requires {
typename _Tp::key_type; })
5158 if constexpr (
requires {
typename _Tp::mapped_type; })
5160 using _Up = remove_cvref_t<_Ref>;
5161 if constexpr (__is_pair<_Up>)
5162 return range_format::map;
5163 else if constexpr (__is_specialization_of<_Up, tuple>)
5164 if constexpr (tuple_size_v<_Up> == 2)
5165 return range_format::map;
5167 return range_format::set;
5170 return range_format::sequence;
5175 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5176 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5181 template<
typename _CharT,
typename _Out,
typename _Callback>
5182 typename basic_format_context<_Out, _CharT>::iterator
5183 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5184 const _Spec<_CharT>& __spec,
5189 static_assert(is_same_v<_Out, __format::_Sink_iter<_CharT>>);
5191 if (__spec._M_get_width(__fc) == 0)
5192 return __call(__fc);
5196 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5197 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5203 _M_ctx->advance_to(_M_out);
5211 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5212 _Sink_iter<_CharT> _M_out;
5215 _Restore_out __restore(__fc);
5219 _Str_sink<_CharT> __buf;
5220 __fc.advance_to(__buf.out());
5222 __restore._M_trigger();
5224 basic_string_view<_CharT> __str(__buf.view());
5226 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
5227 __width = __unicode::__field_width(__str);
5229 __width = __str.size();
5231 return __format::__write_padded_as_spec(__str, __width, __fc, __spec);
5236 template<
typename _Rg,
typename _CharT>
5237 concept __simply_formattable_range
5238 = __const_formattable_range<_Rg, _CharT>
5239 && same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>,
5240 remove_cvref_t<ranges::range_reference_t<const _Rg>>>;
5242 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5243 struct __indexed_formatter_storage
5248 basic_format_parse_context<_CharT> __pc({});
5249 if (_M_formatter.parse(__pc) != __pc.end())
5250 __format::__failed_to_parse_format_spec();
5253 template<
typename _Out>
5255 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5256 basic_format_context<_Out, _CharT>& __fc,
5257 basic_string_view<_CharT> __sep)
const
5259 if constexpr (_Pos != 0)
5260 __fc.advance_to(__format::__write(__fc.out(), __sep));
5261 __fc.advance_to(_M_formatter.format(__elem, __fc));
5264 [[__gnu__::__always_inline__]]
5268 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5269 _M_formatter.set_debug_format();
5273 formatter<_Tp, _CharT> _M_formatter;
5276 template<
typename _CharT,
typename... _Tps>
5277 class __tuple_formatter
5279 using _String_view = basic_string_view<_CharT>;
5280 using _Seps = __format::_Separators<_CharT>;
5284 set_separator(basic_string_view<_CharT> __sep)
noexcept
5288 set_brackets(basic_string_view<_CharT> __open,
5289 basic_string_view<_CharT> __close)
noexcept
5297 constexpr typename basic_format_parse_context<_CharT>::iterator
5298 parse(basic_format_parse_context<_CharT>& __pc)
5300 auto __first = __pc.begin();
5301 const auto __last = __pc.end();
5302 __format::_Spec<_CharT> __spec{};
5304 auto __finished = [&]
5306 if (__first != __last && *__first !=
'}')
5310 _M_felems._M_parse();
5311 _M_felems.set_debug_format();
5318 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5322 __first = __spec._M_parse_width(__first, __last, __pc);
5326 if (*__first ==
'n')
5329 _M_open = _M_close = _String_view();
5331 else if (*__first ==
'm')
5334 if constexpr (
sizeof...(_Tps) == 2)
5336 _M_sep = _Seps::_S_colon();
5337 _M_open = _M_close = _String_view();
5340 __throw_format_error(
"format error: 'm' specifier requires range"
5341 " of pair or tuple of two elements");
5347 __format::__failed_to_parse_format_spec();
5351 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
5352 typename basic_format_context<_Out, _CharT>::iterator
5353 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
5354 basic_format_context<_Out, _CharT>& __fc)
const
5355 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
5357 template<
typename _Out>
5358 typename basic_format_context<_Out, _CharT>::iterator
5359 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
5360 basic_format_context<_Out, _CharT>& __fc)
const
5362 return __format::__format_padded(
5364 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
5366 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
5367 _M_felems._M_format(__elems..., __nfc, _M_sep);
5368 return __format::__write(__nfc.out(), _M_close);
5373 template<
size_t... _Ids>
5374 struct __formatters_storage
5375 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
5377 template<
size_t _Id,
typename _Up>
5378 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
5383 (_Base<_Ids, _Tps>::_M_parse(), ...);
5386 template<
typename _Out>
5388 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
5389 basic_format_context<_Out, _CharT>& __fc,
5390 _String_view __sep)
const
5392 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
5398 (_Base<_Ids, _Tps>::set_debug_format(), ...);
5402 template<
size_t... _Ids>
5404 _S_create_storage(index_sequence<_Ids...>)
5405 -> __formatters_storage<_Ids...>;
5407 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
5409 _Spec<_CharT> _M_spec{};
5410 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
5411 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
5412 _String_view _M_sep = _Seps::_S_comma();
5413 _Formatters _M_felems;
5416 template<
typename _Tp>
5417 concept __is_map_formattable
5418 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
5424 template<__format::__
char _CharT, formattable<_CharT> _Fp,
5425 formattable<_CharT> _Sp>
5426 struct formatter<
pair<_Fp, _Sp>, _CharT>
5427 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
5428 remove_cvref_t<_Sp>>
5431 using __maybe_const_pair
5432 = __conditional_t<formattable<const _Fp, _CharT>
5433 && formattable<const _Sp, _CharT>,
5434 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
5438 template<
typename _Out>
5439 typename basic_format_context<_Out, _CharT>::iterator
5440 format(__maybe_const_pair& __p,
5441 basic_format_context<_Out, _CharT>& __fc)
const
5442 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
5445 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
5446 struct formatter<tuple<_Tps...>, _CharT>
5447 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
5450 using __maybe_const_tuple
5451 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
5452 const tuple<_Tps...>, tuple<_Tps...>>;
5456 template<
typename _Out>
5457 typename basic_format_context<_Out, _CharT>::iterator
5458 format(__maybe_const_tuple& __t,
5459 basic_format_context<_Out, _CharT>& __fc)
const
5460 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
5464 template<
typename _Tp, __format::__
char _CharT>
5465 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
5466 class range_formatter
5468 using _String_view = basic_string_view<_CharT>;
5469 using _Seps = __format::_Separators<_CharT>;
5473 set_separator(basic_string_view<_CharT> __sep)
noexcept
5477 set_brackets(basic_string_view<_CharT> __open,
5478 basic_string_view<_CharT> __close)
noexcept
5484 constexpr formatter<_Tp, _CharT>&
5485 underlying() noexcept
5488 constexpr const formatter<_Tp, _CharT>&
5489 underlying() const noexcept
5494 constexpr typename basic_format_parse_context<_CharT>::iterator
5495 parse(basic_format_parse_context<_CharT>& __pc)
5497 auto __first = __pc.begin();
5498 const auto __last = __pc.end();
5499 __format::_Spec<_CharT> __spec{};
5500 bool __no_brace =
false;
5502 auto __finished = [&]
5503 {
return __first == __last || *__first ==
'}'; };
5505 auto __finalize = [&]
5511 auto __parse_val = [&](_String_view __nfs = _String_view())
5513 basic_format_parse_context<_CharT> __npc(__nfs);
5514 if (_M_fval.parse(__npc) != __npc.end())
5515 __format::__failed_to_parse_format_spec();
5516 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
5517 _M_fval.set_debug_format();
5518 return __finalize();
5522 return __parse_val();
5524 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5526 return __parse_val();
5528 __first = __spec._M_parse_width(__first, __last, __pc);
5530 return __parse_val();
5532 if (*__first ==
'?')
5535 __spec._M_type = __format::_Pres_esc;
5536 if (__finished() || *__first !=
's')
5537 __throw_format_error(
"format error: '?' is allowed only in"
5538 " combination with 's'");
5541 if (*__first ==
's')
5544 if constexpr (same_as<_Tp, _CharT>)
5546 if (__spec._M_type != __format::_Pres_esc)
5547 __spec._M_type = __format::_Pres_str;
5549 return __finalize();
5550 __throw_format_error(
"format error: element format specifier"
5551 " cannot be provided when 's' specifier is used");
5554 __throw_format_error(
"format error: 's' specifier requires"
5555 " range of character types");
5559 return __parse_val();
5561 if (*__first ==
'n')
5564 _M_open = _M_close = _String_view();
5569 return __parse_val();
5571 if (*__first ==
'm')
5573 _String_view __m(__first, 1);
5575 if constexpr (__format::__is_map_formattable<_Tp>)
5577 _M_sep = _Seps::_S_comma();
5580 _M_open = _Seps::_S_braces().substr(0, 1);
5581 _M_close = _Seps::_S_braces().substr(1, 1);
5584 return __parse_val(__m);
5585 __throw_format_error(
"format error: element format specifier"
5586 " cannot be provided when 'm' specifier is used");
5589 __throw_format_error(
"format error: 'm' specifier requires"
5590 " range of pairs or tuples of two elements");
5594 return __parse_val();
5596 if (*__first ==
':')
5598 __pc.advance_to(++__first);
5599 __first = _M_fval.parse(__pc);
5603 return __finalize();
5605 __format::__failed_to_parse_format_spec();
5610 template<ranges::input_range _Rg,
typename _Out>
5611 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
5612 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
5613 typename basic_format_context<_Out, _CharT>::iterator
5614 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
5616 using _Range = remove_reference_t<_Rg>;
5617 if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
5618 return _M_format<const _Range>(__rg, __fc);
5620 return _M_format(__rg, __fc);
5624 template<ranges::input_range _Rg,
typename _Out>
5625 typename basic_format_context<_Out, _CharT>::iterator
5626 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
5628 if constexpr (same_as<_Tp, _CharT>)
5629 if (_M_spec._M_type == __format::_Pres_str
5630 || _M_spec._M_type == __format::_Pres_esc)
5632 __format::__formatter_str __fstr(_M_spec);
5633 return __fstr._M_format_range(__rg, __fc);
5635 return __format::__format_padded(
5637 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
5638 {
return _M_format_elems(__rg, __nfc); });
5642 template<ranges::input_range _Rg,
typename _Out>
5643 typename basic_format_context<_Out, _CharT>::iterator
5644 _M_format_elems(_Rg& __rg,
5645 basic_format_context<_Out, _CharT>& __fc)
const
5647 auto __out = __format::__write(__fc.out(), _M_open);
5649 auto __first = ranges::begin(__rg);
5650 auto const __last = ranges::end(__rg);
5651 if (__first == __last)
5652 return __format::__write(__out, _M_close);
5654 __fc.advance_to(__out);
5655 __out = _M_fval.format(*__first, __fc);
5656 for (++__first; __first != __last; ++__first)
5658 __out = __format::__write(__out, _M_sep);
5659 __fc.advance_to(__out);
5660 __out = _M_fval.format(*__first, __fc);
5663 return __format::__write(__out, _M_close);
5666 __format::_Spec<_CharT> _M_spec{};
5667 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
5668 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
5669 _String_view _M_sep = _Seps::_S_comma();
5670 formatter<_Tp, _CharT> _M_fval;
5678 template<ranges::input_range _Rg, __format::__
char _CharT>
5679 requires (format_kind<_Rg> != range_format::disabled)
5680 && formattable<ranges::range_reference_t<_Rg>, _CharT>
5681 struct formatter<_Rg, _CharT>
5684 static const bool _S_range_format_is_string =
5685 (format_kind<_Rg> == range_format::string)
5686 || (format_kind<_Rg> == range_format::debug_string);
5687 using _Vt = remove_cvref_t<
5688 ranges::range_reference_t<
5689 __format::__maybe_const_range<_Rg, _CharT>>>;
5691 static consteval bool _S_is_correct()
5693 if constexpr (_S_range_format_is_string)
5694 static_assert(same_as<_Vt, _CharT>);
5698 static_assert(_S_is_correct());
5701 constexpr formatter() noexcept
5703 using _Seps = __format::_Separators<_CharT>;
5704 if constexpr (format_kind<_Rg> == range_format::map)
5706 static_assert(__format::__is_map_formattable<_Vt>);
5707 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
5708 _Seps::_S_braces().substr(1, 1));
5709 _M_under.underlying().set_brackets({}, {});
5710 _M_under.underlying().set_separator(_Seps::_S_colon());
5712 else if constexpr (format_kind<_Rg> == range_format::set)
5713 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
5714 _Seps::_S_braces().substr(1, 1));
5718 set_separator(basic_string_view<_CharT> __sep)
noexcept
5719 requires (format_kind<_Rg> == range_format::sequence)
5720 { _M_under.set_separator(__sep); }
5723 set_brackets(basic_string_view<_CharT> __open,
5724 basic_string_view<_CharT> __close)
noexcept
5725 requires (format_kind<_Rg> == range_format::sequence)
5726 { _M_under.set_brackets(__open, __close); }
5730 constexpr typename basic_format_parse_context<_CharT>::iterator
5731 parse(basic_format_parse_context<_CharT>& __pc)
5733 auto __res = _M_under.parse(__pc);
5734 if constexpr (format_kind<_Rg> == range_format::debug_string)
5735 _M_under.set_debug_format();
5741 template<
typename _Out>
5742 typename basic_format_context<_Out, _CharT>::iterator
5743 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
5744 basic_format_context<_Out, _CharT>& __fc)
const
5746 if constexpr (_S_range_format_is_string)
5747 return _M_under._M_format_range(__rg, __fc);
5749 return _M_under.format(__rg, __fc);
5753 using _Formatter_under
5754 = __conditional_t<_S_range_format_is_string,
5755 __format::__formatter_str<_CharT>,
5756 range_formatter<_Vt, _CharT>>;
5757 _Formatter_under _M_under;
5760#undef _GLIBCXX_WIDEN
5762_GLIBCXX_END_NAMESPACE_VERSION
5765#pragma GCC diagnostic pop
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
_Tp arg(const complex< _Tp > &)
Return phase angle of z.
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
const _Facet & use_facet(const locale &__loc)
Return a facet.
basic_string< char > string
A string of char.
ISO C++ entities toplevel namespace is std.
chars_format
floating-point format for primitive numerical conversion
_CharT toupper(_CharT __c, const locale &__loc)
Convenience interface to ctype.toupper(__c).
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
One of two subclasses of exception.
constexpr size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
constexpr void reserve(size_type __res_arg)
Attempt to preallocate enough memory for specified number of characters.
constexpr const _CharT * data() const noexcept
Return const pointer to contents.
constexpr basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
constexpr void __resize_and_overwrite(size_type __n, _Operation __op)
Non-standard version of resize_and_overwrite for C++11 and above.
constexpr basic_string & append(const basic_string &__str)
Append a string to this string.
constexpr iterator insert(const_iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
constexpr size_type capacity() const noexcept
constexpr bool empty() const noexcept