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
41#define __glibcxx_want_constexpr_exceptions
44#ifdef __cpp_lib_format
66#if !__has_builtin(__builtin_toupper)
70#pragma GCC diagnostic push
71#pragma GCC diagnostic ignored "-Wpedantic"
72#pragma GCC diagnostic ignored "-Wc++23-extensions"
74namespace std _GLIBCXX_VISIBILITY(default)
76_GLIBCXX_BEGIN_NAMESPACE_VERSION
79 template<
typename _CharT,
typename... _Args>
struct basic_format_string;
89 template<
typename _CharT>
91 _Widen(
const char* __narrow,
const wchar_t* __wide)
93 if constexpr (is_same_v<_CharT, wchar_t>)
98#define _GLIBCXX_WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
99#define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
102 template<
typename _CharT>
103 constexpr size_t __stackbuf_size = 32 *
sizeof(
void*) /
sizeof(_CharT);
106 template<
typename _CharT>
class _Sink;
107 template<
typename _CharT>
class _Fixedbuf_sink;
108 template<
typename _Out,
typename _CharT>
class _Padding_sink;
109 template<
typename _Out,
typename _CharT>
class _Escaping_sink;
112 template<
typename _CharT>
116 template<
typename _CharT>
120 template<
typename _CharT>
122 {
using type = _Drop_iter<_CharT>; };
124 template<
typename _CharT>
125 using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
127 template<
typename _CharT>
128 struct _Dynamic_format_string
130 [[__gnu__::__always_inline__]]
131 _Dynamic_format_string(basic_string_view<_CharT> __s) noexcept
134 _Dynamic_format_string(
const _Dynamic_format_string&) =
delete;
135 void operator=(
const _Dynamic_format_string&) =
delete;
138 basic_string_view<_CharT> _M_str;
140 template<
typename,
typename...>
friend struct std::basic_format_string;
146 using format_context = __format::__format_context<char>;
147#ifdef _GLIBCXX_USE_WCHAR_T
148 using wformat_context = __format::__format_context<wchar_t>;
152 template<
typename _Context>
class basic_format_args;
153 using format_args = basic_format_args<format_context>;
154#ifdef _GLIBCXX_USE_WCHAR_T
155 using wformat_args = basic_format_args<wformat_context>;
160 template<
typename _Context>
161 class basic_format_arg;
167 template<
typename _CharT,
typename... _Args>
168 struct basic_format_string
170 template<
typename _Tp>
171 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
173 basic_format_string(
const _Tp& __s);
175 [[__gnu__::__always_inline__]]
176 basic_format_string(__format::_Dynamic_format_string<_CharT> __s) noexcept
180 [[__gnu__::__always_inline__]]
181 constexpr basic_string_view<_CharT>
186 basic_string_view<_CharT> _M_str;
189 template<
typename... _Args>
190 using format_string = basic_format_string<char, type_identity_t<_Args>...>;
192#ifdef _GLIBCXX_USE_WCHAR_T
193 template<
typename... _Args>
195 = basic_format_string<wchar_t, type_identity_t<_Args>...>;
198#if __cpp_lib_format >= 202603L
199 [[__gnu__::__always_inline__]]
200 inline __format::_Dynamic_format_string<char>
201 dynamic_format(string_view __fmt)
noexcept
204#ifdef _GLIBCXX_USE_WCHAR_T
205 [[__gnu__::__always_inline__]]
206 inline __format::_Dynamic_format_string<wchar_t>
207 dynamic_format(wstring_view __fmt)
noexcept
215 template<
typename _Tp,
typename _CharT>
218 formatter() =
delete;
219 formatter(
const formatter&) =
delete;
220 formatter& operator=(
const formatter&) =
delete;
223#if __cpp_lib_constexpr_exceptions >= 202502L
224#define _GLIBCXX_CONSTEXPR_FORMAT_ERROR constexpr
226#define _GLIBCXX_CONSTEXPR_FORMAT_ERROR
233 _GLIBCXX_CONSTEXPR_FORMAT_ERROR
explicit format_error(
const string& __what)
234 : runtime_error(__what) { }
235 _GLIBCXX_CONSTEXPR_FORMAT_ERROR
explicit format_error(
const char* __what)
236 : runtime_error(__what) { }
242 __throw_format_error(
const char* __what)
243 { _GLIBCXX_THROW_OR_ABORT(format_error(__what)); }
245#undef _GLIBCXX_CONSTEXPR_FORMAT_ERROR
253 __unmatched_left_brace_in_format_string()
254 { __throw_format_error(
"format error: unmatched '{' in format string"); }
258 __unmatched_right_brace_in_format_string()
259 { __throw_format_error(
"format error: unmatched '}' in format string"); }
263 __conflicting_indexing_in_format_string()
264 { __throw_format_error(
"format error: conflicting indexing style in format string"); }
268 __invalid_arg_id_in_format_string()
269 { __throw_format_error(
"format error: invalid arg-id in format string"); }
273 __failed_to_parse_format_spec()
274 { __throw_format_error(
"format error: failed to parse format-spec"); }
276 template<
typename _CharT>
class _Scanner;
278 enum class _Arg_t :
unsigned char {
279 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
280 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
281 _Arg_i128, _Arg_u128, _Arg_float128,
282 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
285#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
286 _Arg_ibm128 = _Arg_ldbl,
287 _Arg_ieee128 = _Arg_float128,
292 template<
typename _CharT,
typename _Tp>
294 __to_arg_t_enum() noexcept;
300 template<typename _CharT> class basic_format_parse_context;
301 using format_parse_context = basic_format_parse_context<
char>;
302#ifdef _GLIBCXX_USE_WCHAR_T
303 using wformat_parse_context = basic_format_parse_context<wchar_t>;
306 template<
typename _CharT>
307 class basic_format_parse_context
310 using char_type = _CharT;
311 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
312 using iterator = const_iterator;
315 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
316 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
319 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
320 void operator=(
const basic_format_parse_context&) =
delete;
322 constexpr const_iterator begin() const noexcept {
return _M_begin; }
323 constexpr const_iterator end() const noexcept {
return _M_end; }
326 advance_to(const_iterator __it)
noexcept
332 if (_M_indexing == _Manual)
333 __format::__conflicting_indexing_in_format_string();
338 if (std::is_constant_evaluated())
339 if (_M_next_arg_id == _M_num_args)
340 __format::__invalid_arg_id_in_format_string();
341 return _M_next_arg_id++;
345 check_arg_id(
size_t __id)
347 if (_M_indexing == _Auto)
348 __format::__conflicting_indexing_in_format_string();
349 _M_indexing = _Manual;
351 if (std::is_constant_evaluated())
352 if (__id >= _M_num_args)
353 __format::__invalid_arg_id_in_format_string();
356#if __cpp_lib_format >= 202305L
357 template<
typename... _Ts>
359 check_dynamic_spec(
size_t __id)
noexcept
361 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
362 "template arguments for check_dynamic_spec<Ts...>(id) "
363 "must be unique and must be one of the allowed types");
364 if constexpr (
sizeof...(_Ts))
366 const __format::_Arg_t __t[] = {
367 __format::__to_arg_t_enum<_CharT, _Ts>()...
369 __check_dynamic_spec(__id, __t);
374 check_dynamic_spec_integral(
size_t __id)
noexcept
377 using enum __format::_Arg_t;
378 const __format::_Arg_t __t[] = { _Arg_i, _Arg_u, _Arg_ll, _Arg_ull };
379 __check_dynamic_spec(__id, __t);
384 check_dynamic_spec_string(
size_t __id)
noexcept
387 using enum __format::_Arg_t;
388 const __format::_Arg_t __t[] = { _Arg_str, _Arg_sv };
389 __check_dynamic_spec(__id, __t);
395 template<
typename _Tp,
typename... _Ts>
396 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
398 template<
typename... _Ts>
399 static consteval bool
400 __valid_types_for_check_dynamic_spec()
404 if constexpr (
sizeof...(_Ts) == 0)
413 = __once<bool, _Ts...>
414 + __once<char_type, _Ts...>
415 + __once<int, _Ts...>
416 + __once<
unsigned int, _Ts...>
417 + __once<
long long int, _Ts...>
418 + __once<
unsigned long long int, _Ts...>
419 + __once<float, _Ts...>
420 + __once<double, _Ts...>
421 + __once<
long double, _Ts...>
422 + __once<
const char_type*, _Ts...>
423 + __once<basic_string_view<char_type>, _Ts...>
424 + __once<
const void*, _Ts...>;
425 return __sum ==
sizeof...(_Ts);
431 __check_dynamic_spec(
size_t __id,
432 span<const __format::_Arg_t> __types)
noexcept
434 if (__id >= _M_num_args)
435 __format::__invalid_arg_id_in_format_string();
441 if (
auto* __args =
static_cast<_Scan_parse_context*
>(
this)->_M_types)
443 for (
auto __t : __types)
444 if (__args[__id] == __t)
447 __invalid_dynamic_spec(
"arg(id) type does not match");
453 static void __invalid_dynamic_spec(
const char*);
458 basic_format_parse_context(basic_string_view<_CharT> __fmt,
459 size_t __num_args) noexcept
460 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
466 enum _Indexing { _Unknown, _Manual, _Auto };
467 _Indexing _M_indexing = _Unknown;
468 size_t _M_next_arg_id = 0;
469 size_t _M_num_args = 0;
472 struct _Scan_parse_context;
473 friend __format::_Scanner<_CharT>;
476 template<
typename _CharT>
477 struct basic_format_parse_context<_CharT>::_Scan_parse_context
478 : basic_format_parse_context<_CharT>
480 using basic_format_parse_context<_CharT>::basic_format_parse_context;
481 const __format::_Arg_t* _M_types =
nullptr;
485 template<
typename _Tp,
template<
typename...>
class _Class>
486 constexpr bool __is_specialization_of =
false;
487 template<
template<
typename...>
class _Class,
typename... _Args>
488 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
493 template<
typename _CharT>
494 constexpr pair<unsigned short, const _CharT*>
495 __parse_integer(
const _CharT* __first,
const _CharT* __last)
497 if (__first == __last)
498 __builtin_unreachable();
500 if constexpr (is_same_v<_CharT, char>)
502 const auto __start = __first;
503 unsigned short __val = 0;
505 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
506 && __first != __start) [[likely]]
507 return {__val, __first};
511 constexpr int __n = 32;
513 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
514 __buf[__i] = __first[__i];
515 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
516 if (__ptr) [[likely]]
517 return {__v, __first + (__ptr - __buf)};
522 template<
typename _CharT>
523 constexpr pair<unsigned short, const _CharT*>
524 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
526 if (__first == __last)
527 __builtin_unreachable();
530 return {0, __first + 1};
532 if (
'1' <= *__first && *__first <=
'9')
534 const unsigned short __id = *__first -
'0';
535 const auto __next = __first + 1;
537 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
538 return {__id, __next};
540 return __format::__parse_integer(__first, __last);
545 enum class _Pres_type :
unsigned char {
549 _Pres_c = 2, _Pres_x, _Pres_X, _Pres_d, _Pres_o, _Pres_b, _Pres_B,
551 _Pres_g = 1, _Pres_G, _Pres_a, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F,
555 using enum _Pres_type;
557 enum class _Sign :
unsigned char {
565 enum _WidthPrec :
unsigned char {
570 using enum _WidthPrec;
572 template<
typename _Context>
574 __int_from_arg(
const basic_format_arg<_Context>& __arg);
576 constexpr bool __is_digit(
char __c)
577 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
579 constexpr bool __is_xdigit(
char __c)
580 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
587 template<
typename _CharT>
588 struct _Spec : _SpecBase
590 unsigned short _M_width;
591 unsigned short _M_prec;
592 char32_t _M_fill =
' ';
596 unsigned _M_localized : 1;
597 unsigned _M_zero_fill : 1;
598 _WidthPrec _M_width_kind : 2;
599 _WidthPrec _M_prec_kind : 2;
600 unsigned _M_debug : 1;
601 _Pres_type _M_type : 4;
602 unsigned _M_reserved : 8;
606 using iterator =
typename basic_string_view<_CharT>::iterator;
608 static constexpr _Align
609 _S_align(_CharT __c)
noexcept
613 case '<':
return _Align_left;
614 case '>':
return _Align_right;
615 case '^':
return _Align_centre;
616 default:
return _Align_default;
622 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
623 {
return _M_parse_fill_and_align(__first, __last,
"{"); }
627 _M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill)
noexcept
629 for (
char __c : __not_fill)
630 if (*__first ==
static_cast<_CharT
>(__c))
633 using namespace __unicode;
634 if constexpr (__literal_encoding_is_unicode<_CharT>())
637 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
640 auto __beg = __uv.begin();
641 char32_t __c = *__beg++;
642 if (__is_scalar_value(__c))
643 if (
auto __next = __beg.base(); __next != __last)
644 if (_Align __align = _S_align(*__next); __align != _Align_default)
652 else if (__last - __first >= 2)
653 if (_Align __align = _S_align(__first[1]); __align != _Align_default)
660 if (_Align __align = _S_align(__first[0]); __align != _Align_default)
669 static constexpr _Sign
670 _S_sign(_CharT __c)
noexcept
674 case '+':
return _Sign_plus;
675 case '-':
return _Sign_minus;
676 case ' ':
return _Sign_space;
677 default:
return _Sign_default;
683 _M_parse_sign(iterator __first, iterator)
noexcept
685 if (_Sign __sign = _S_sign(*__first); __sign != _Sign_default)
695 _M_parse_alternate_form(iterator __first, iterator)
noexcept
707 _M_parse_zero_fill(iterator __first, iterator )
noexcept
718 static constexpr iterator
719 _S_parse_width_or_precision(iterator __first, iterator __last,
720 unsigned short& __val,
bool& __arg_id,
721 basic_format_parse_context<_CharT>& __pc)
723 if (__format::__is_digit(*__first))
725 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
727 __throw_format_error(
"format error: invalid width or precision "
732 else if (*__first ==
'{')
736 if (__first == __last)
737 __format::__unmatched_left_brace_in_format_string();
739 __val = __pc.next_arg_id();
742 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
743 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
744 __format::__invalid_arg_id_in_format_string();
746 __pc.check_arg_id(__v);
749#if __cpp_lib_format >= 202305L
750 __pc.check_dynamic_spec_integral(__val);
759 _M_parse_width(iterator __first, iterator __last,
760 basic_format_parse_context<_CharT>& __pc)
762 bool __arg_id =
false;
764 __throw_format_error(
"format error: width must be non-zero in "
766 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
768 if (__next != __first)
769 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
775 _M_parse_precision(iterator __first, iterator __last,
776 basic_format_parse_context<_CharT>& __pc)
778 if (__first[0] !=
'.')
781 iterator __next = ++__first;
782 bool __arg_id =
false;
783 if (__next != __last)
784 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
786 if (__next == __first)
787 __throw_format_error(
"format error: missing precision after '.' in "
789 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
795 _M_parse_locale(iterator __first, iterator )
noexcept
805 template<
typename _Context>
807 _M_get_width(_Context& __ctx)
const
810 if (_M_width_kind == _WP_value)
812 else if (_M_width_kind == _WP_from_arg)
813 __width = __format::__int_from_arg(__ctx.arg(_M_width));
817 template<
typename _Context>
819 _M_get_precision(_Context& __ctx)
const
822 if (_M_prec_kind == _WP_value)
824 else if (_M_prec_kind == _WP_from_arg)
825 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
830 template<
typename _Int>
832 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
836 else if (__sign == _Sign_plus)
838 else if (__sign == _Sign_space)
846 template<
typename _Out,
typename _CharT>
847 requires output_iterator<_Out, const _CharT&>
849 __write(_Out __out, basic_string_view<_CharT> __str)
851 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
857 for (_CharT __c : __str)
864 template<
typename _Out,
typename _CharT>
866 __write_padded(_Out __out, basic_string_view<_CharT> __str,
867 _Align __align,
size_t __nfill,
char32_t __fill_char)
869 const size_t __buflen = 0x20;
870 _CharT __padding_chars[__buflen];
871 __padding_chars[0] = _CharT();
872 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
874 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
877 while (__n > __padding.size())
879 __o = __format::__write(
std::move(__o), __padding);
880 __n -= __padding.size();
883 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
886 size_t __l, __r, __max;
887 if (__align == _Align_centre)
890 __r = __l + (__nfill & 1);
893 else if (__align == _Align_right)
906 using namespace __unicode;
907 if constexpr (__literal_encoding_is_unicode<_CharT>())
908 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
911 const char32_t __arr[1]{ __fill_char };
912 _Utf_view<_CharT, span<const char32_t, 1>> __v(__arr);
913 basic_string<_CharT> __padstr(__v.begin(), __v.end());
914 __padding = __padstr;
916 __out = __format::__write(
std::move(__out), __padding);
917 __out = __format::__write(
std::move(__out), __str);
919 __out = __format::__write(
std::move(__out), __padding);
923 if (__max < __buflen)
924 __padding.remove_suffix(__buflen - __max);
928 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
930 __out = __format::__write(
std::move(__out), __str);
938 template<
typename _CharT,
typename _Out>
940 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
941 size_t __estimated_width,
942 basic_format_context<_Out, _CharT>& __fc,
943 const _Spec<_CharT>& __spec,
944 _Align __align = _Align_left)
946 size_t __width = __spec._M_get_width(__fc);
948 if (__width <= __estimated_width)
949 return __format::__write(__fc.out(), __str);
951 const size_t __nfill = __width - __estimated_width;
953 if (__spec._M_align != _Align_default)
954 __align = __spec._M_align;
956 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
960 template<
typename _CharT>
962 __truncate(basic_string_view<_CharT>& __s,
size_t __prec)
964 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
966 if (__prec != (
size_t)-1)
967 return __unicode::__truncate(__s, __prec);
969 return __unicode::__field_width(__s);
973 __s = __s.substr(0, __prec);
978 enum class _Term_char :
unsigned char {
983 using enum _Term_char;
985 template<
typename _CharT>
988 using _Str_view = basic_string_view<_CharT>;
992 {
return _GLIBCXX_WIDEN(
"\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
996 {
return _S_all().substr(0, 3); }
999 _Str_view _S_newline()
1000 {
return _S_all().substr(3, 3); }
1003 _Str_view _S_return()
1004 {
return _S_all().substr(6, 3); }
1007 _Str_view _S_bslash()
1008 {
return _S_all().substr(9, 3); }
1011 _Str_view _S_quote()
1012 {
return _S_all().substr(12, 3); }
1016 {
return _S_all().substr(15, 3); }
1020 {
return _S_all().substr(18, 2); }
1024 {
return _S_all().substr(20, 2); }
1027 _Str_view _S_term(_Term_char __term)
1034 return _S_quote().substr(0, 1);
1036 return _S_apos().substr(0, 1);
1038 __builtin_unreachable();
1042 template<
typename _CharT>
1045 using _Str_view = basic_string_view<_CharT>;
1049 {
return _GLIBCXX_WIDEN(
"[]{}(), : "); }
1052 _Str_view _S_squares()
1053 {
return _S_all().substr(0, 2); }
1056 _Str_view _S_braces()
1057 {
return _S_all().substr(2, 2); }
1060 _Str_view _S_parens()
1061 {
return _S_all().substr(4, 2); }
1064 _Str_view _S_comma()
1065 {
return _S_all().substr(6, 2); }
1068 _Str_view _S_colon()
1069 {
return _S_all().substr(8, 2); }
1072 template<
typename _CharT>
1073 constexpr bool __should_escape_ascii(_CharT __c, _Term_char __term)
1075 using _Esc = _Escapes<_CharT>;
1078 case _Esc::_S_tab()[0]:
1079 case _Esc::_S_newline()[0]:
1080 case _Esc::_S_return()[0]:
1081 case _Esc::_S_bslash()[0]:
1083 case _Esc::_S_quote()[0]:
1084 return __term == _Term_quote;
1085 case _Esc::_S_apos()[0]:
1086 return __term == _Term_apos;
1088 return (__c >= 0 && __c < 0x20) || __c == 0x7f;
1093 constexpr bool __should_escape_unicode(
char32_t __c,
bool __prev_esc)
1095 if (__unicode::__should_escape_category(__c))
1099 return __unicode::__grapheme_cluster_break_property(__c)
1100 == __unicode::_Gcb_property::_Gcb_Extend;
1103 using uint_least32_t = __UINT_LEAST32_TYPE__;
1104 template<
typename _Out,
typename _CharT>
1106 __write_escape_seq(_Out __out, uint_least32_t __val,
1107 basic_string_view<_CharT> __prefix)
1109 constexpr size_t __max = 8;
1111 const string_view __narrow(
1113 std::__to_chars_i<uint_least32_t>(__buf, __buf + __max, __val, 16).ptr);
1115 __out = __format::__write(__out, __prefix);
1116 *__out = _Separators<_CharT>::_S_braces()[0];
1118 if constexpr (is_same_v<char, _CharT>)
1119 __out = __format::__write(__out, __narrow);
1120#ifdef _GLIBCXX_USE_WCHAR_T
1123 wchar_t __wbuf[__max];
1124 const size_t __n = __narrow.size();
1125 std::__to_wstring_numeric(__narrow.data(), __n, __wbuf);
1126 __out = __format::__write(__out, wstring_view(__wbuf, __n));
1129 *__out = _Separators<_CharT>::_S_braces()[1];
1133 template<
typename _Out,
typename _CharT>
1135 __write_escape_seqs(_Out __out, basic_string_view<_CharT> __units)
1137 using _UChar = make_unsigned_t<_CharT>;
1138 for (_CharT __c : __units)
1139 __out = __format::__write_escape_seq(
1140 __out,
static_cast<_UChar
>(__c), _Escapes<_CharT>::_S_x());
1144 template<
typename _Out,
typename _CharT>
1146 __write_escaped_char(_Out __out, _CharT __c)
1148 using _UChar = make_unsigned_t<_CharT>;
1149 using _Esc = _Escapes<_CharT>;
1152 case _Esc::_S_tab()[0]:
1153 return __format::__write(__out, _Esc::_S_tab().substr(1, 2));
1154 case _Esc::_S_newline()[0]:
1155 return __format::__write(__out, _Esc::_S_newline().substr(1, 2));
1156 case _Esc::_S_return()[0]:
1157 return __format::__write(__out, _Esc::_S_return().substr(1, 2));
1158 case _Esc::_S_bslash()[0]:
1159 return __format::__write(__out, _Esc::_S_bslash().substr(1, 2));
1160 case _Esc::_S_quote()[0]:
1161 return __format::__write(__out, _Esc::_S_quote().substr(1, 2));
1162 case _Esc::_S_apos()[0]:
1163 return __format::__write(__out, _Esc::_S_apos().substr(1, 2));
1165 return __format::__write_escape_seq(
1166 __out,
static_cast<_UChar
>(__c), _Esc::_S_u());
1170 template<
typename _CharT,
typename _Out>
1172 __write_escaped_ascii(_Out __out,
1173 basic_string_view<_CharT> __str,
1176 using _Str_view = basic_string_view<_CharT>;
1177 auto __first = __str.begin();
1178 auto const __last = __str.end();
1179 while (__first != __last)
1181 auto __print = __first;
1183 while (__print != __last
1184 && !__format::__should_escape_ascii(*__print, __term))
1187 if (__print != __first)
1188 __out = __format::__write(__out, _Str_view(__first, __print));
1190 if (__print == __last)
1194 __out = __format::__write_escaped_char(__out, *__first);
1200 template<
typename _CharT,
typename _Out>
1202 __write_escaped_unicode_part(_Out __out, basic_string_view<_CharT>& __str,
1203 bool& __prev_esc, _Term_char __term)
1205 using _Str_view = basic_string_view<_CharT>;
1206 using _Esc = _Escapes<_CharT>;
1208 static constexpr char32_t __replace = U
'\uFFFD';
1209 static constexpr _Str_view __replace_rep = []
1212 if constexpr (is_same_v<char, _CharT>)
1213 return "\xEF\xBF\xBD";
1218 __unicode::_Utf_view<char32_t, _Str_view> __v(
std::move(__str));
1221 auto __first = __v.begin();
1222 auto const __last = __v.end();
1223 while (__first != __last)
1225 bool __esc_ascii =
false;
1226 bool __esc_unicode =
false;
1227 bool __esc_replace =
false;
1228 auto __should_escape = [&](
auto const& __it)
1232 = __format::__should_escape_ascii(*__it.base(), __term);
1233 if (__format::__should_escape_unicode(*__it, __prev_esc))
1234 return __esc_unicode =
true;
1235 if (*__it == __replace)
1237 _Str_view __units(__it.base(), __it._M_units());
1238 return __esc_replace = (__units != __replace_rep);
1243 auto __print = __first;
1244 while (__print != __last && !__should_escape(__print))
1250 if (__print != __first)
1251 __out = __format::__write(__out, _Str_view(__first.base(), __print.base()));
1253 if (__print == __last)
1258 __out = __format::__write_escaped_char(__out, *__first.base());
1259 else if (__esc_unicode)
1260 __out = __format::__write_escape_seq(__out, *__first, _Esc::_S_u());
1262 else if (_Str_view __units(__first.base(), __first._M_units());
1263 __units.end() != __last.base())
1264 __out = __format::__write_escape_seqs(__out, __units);
1278 template<
typename _CharT,
typename _Out>
1280 __write_escaped_unicode(_Out __out, basic_string_view<_CharT> __str,
1283 bool __prev_escape =
true;
1284 __out = __format::__write_escaped_unicode_part(__out, __str,
1285 __prev_escape, __term);
1286 __out = __format::__write_escape_seqs(__out, __str);
1290 template<
typename _CharT,
typename _Out>
1292 __write_escaped(_Out __out, basic_string_view<_CharT> __str, _Term_char __term)
1294 __out = __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1296 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1297 __out = __format::__write_escaped_unicode(__out, __str, __term);
1298 else if constexpr (is_same_v<char, _CharT>
1299 && __unicode::__literal_encoding_is_extended_ascii())
1300 __out = __format::__write_escaped_ascii(__out, __str, __term);
1303 __out = __format::__write_escaped_ascii(__out, __str, __term);
1305 return __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1309 struct _Optional_locale
1311 [[__gnu__::__always_inline__]]
1312 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
1314 _Optional_locale(
const locale& __loc) noexcept
1315 : _M_loc(__loc), _M_hasval(
true)
1318 _Optional_locale(
const _Optional_locale& __l) noexcept
1319 : _M_dummy(), _M_hasval(__l._M_hasval)
1322 std::construct_at(&_M_loc, __l._M_loc);
1326 operator=(
const _Optional_locale& __l)
noexcept
1331 _M_loc = __l._M_loc;
1338 else if (__l._M_hasval)
1340 std::construct_at(&_M_loc, __l._M_loc);
1346 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
1349 operator=(locale&& __loc)
noexcept
1355 std::construct_at(&_M_loc,
std::move(__loc));
1366 std::construct_at(&_M_loc);
1372 bool has_value() const noexcept {
return _M_hasval; }
1375 char _M_dummy =
'\0';
1378 bool _M_hasval =
false;
1381 template<__
char _CharT>
1382 struct __formatter_str
1384 __formatter_str() =
default;
1387 __formatter_str(_Spec<_CharT> __spec) noexcept
1391 constexpr typename basic_format_parse_context<_CharT>::iterator
1392 parse(basic_format_parse_context<_CharT>& __pc)
1394 auto __first = __pc.begin();
1395 const auto __last = __pc.end();
1396 _Spec<_CharT> __spec{};
1398 auto __finalize = [
this, &__spec] {
1402 auto __finished = [&] {
1403 if (__first == __last || *__first ==
'}')
1414 __first = __spec._M_parse_fill_and_align(__first, __last);
1418 __first = __spec._M_parse_width(__first, __last, __pc);
1422 __first = __spec._M_parse_precision(__first, __last, __pc);
1426 if (*__first ==
's')
1428 __spec._M_type = _Pres_s;
1431#if __glibcxx_format_ranges
1432 else if (*__first ==
'?')
1434 __spec._M_debug =
true;
1442 __format::__failed_to_parse_format_spec();
1445 template<
typename _Out>
1447 format(basic_string_view<_CharT> __s,
1448 basic_format_context<_Out, _CharT>& __fc)
const
1450 if (_M_spec._M_debug)
1451 return _M_format_escaped(__s, __fc);
1453 if (_M_spec._M_width_kind == _WP_none
1454 && _M_spec._M_prec_kind == _WP_none)
1455 return __format::__write(__fc.out(), __s);
1457 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1458 const size_t __width = __format::__truncate(__s, __maxwidth);
1459 return __format::__write_padded_as_spec(__s, __width, __fc, _M_spec);
1462 template<
typename _Out>
1464 _M_format_escaped(basic_string_view<_CharT> __s,
1465 basic_format_context<_Out, _CharT>& __fc)
const
1467 const size_t __padwidth = _M_spec._M_get_width(__fc);
1468 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1469 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1471 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1472 const size_t __width = __truncate(__s, __maxwidth);
1474 if (__padwidth <= __width && _M_spec._M_prec_kind == _WP_none)
1475 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1480 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth, __maxwidth);
1481 __format::__write_escaped(__sink.out(), __s, _Term_quote);
1482 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1485#if __glibcxx_format_ranges
1486 template<ranges::input_range _Rg,
typename _Out>
1487 requires same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _CharT>
1489 _M_format_range(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
1491 using _Range = remove_reference_t<_Rg>;
1492 using _String_view = basic_string_view<_CharT>;
1493 if constexpr (!is_lvalue_reference_v<_Rg>)
1494 return _M_format_range<_Range&>(__rg, __fc);
1495 else if constexpr (!is_const_v<_Range>
1496 && __simply_formattable_range<_Range, _CharT>)
1497 return _M_format_range<const _Range&>(__rg, __fc);
1498 else if constexpr (ranges::contiguous_range<_Rg>)
1500 _String_view __str(ranges::data(__rg),
1501 size_t(ranges::distance(__rg)));
1502 return format(__str, __fc);
1506 auto __handle_debug = [
this, &__rg]<
typename _NOut>(_NOut __nout)
1508 if (!_M_spec._M_debug)
1509 return ranges::copy(__rg,
std::move(__nout)).out;
1511 _Escaping_sink<_NOut, _CharT>
1513 ranges::copy(__rg, __sink.out());
1514 return __sink._M_finish();
1517 const size_t __padwidth = _M_spec._M_get_width(__fc);
1518 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1519 return __handle_debug(__fc.out());
1521 _Padding_sink<_Out, _CharT>
1522 __sink(__fc.out(), __padwidth, _M_spec._M_get_precision(__fc));
1523 __handle_debug(__sink.out());
1524 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1529 set_debug_format() noexcept
1530 { _M_spec._M_debug =
true; }
1534 _Spec<_CharT> _M_spec{};
1537 template<__
char _CharT>
1538 struct __formatter_int
1542 static constexpr _Pres_type _AsInteger = _Pres_d;
1543 static constexpr _Pres_type _AsBool = _Pres_s;
1544 static constexpr _Pres_type _AsChar = _Pres_c;
1546 __formatter_int() =
default;
1549 __formatter_int(_Spec<_CharT> __spec) noexcept
1552 if (_M_spec._M_type == _Pres_none)
1553 _M_spec._M_type = _Pres_d;
1556 constexpr typename basic_format_parse_context<_CharT>::iterator
1557 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1559 _Spec<_CharT> __spec{};
1560 __spec._M_type = __type;
1562 const auto __last = __pc.end();
1563 auto __first = __pc.begin();
1565 auto __finalize = [
this, &__spec] {
1569 auto __finished = [&] {
1570 if (__first == __last || *__first ==
'}')
1581 __first = __spec._M_parse_fill_and_align(__first, __last);
1585 __first = __spec._M_parse_sign(__first, __last);
1589 __first = __spec._M_parse_alternate_form(__first, __last);
1593 __first = __spec._M_parse_zero_fill(__first, __last);
1597 __first = __spec._M_parse_width(__first, __last, __pc);
1601 __first = __spec._M_parse_locale(__first, __last);
1608 __spec._M_type = _Pres_b;
1612 __spec._M_type = _Pres_B;
1618 if (__type != _AsBool)
1620 __spec._M_type = _Pres_c;
1625 __spec._M_type = _Pres_d;
1629 __spec._M_type = _Pres_o;
1633 __spec._M_type = _Pres_x;
1637 __spec._M_type = _Pres_X;
1641 if (__type == _AsBool)
1643 __spec._M_type = _Pres_s;
1647#if __glibcxx_format_ranges
1649 if (__type == _AsChar)
1651 __spec._M_debug =
true;
1661 __format::__failed_to_parse_format_spec();
1664 template<
typename _Tp>
1665 constexpr typename basic_format_parse_context<_CharT>::iterator
1666 _M_parse(basic_format_parse_context<_CharT>& __pc)
1668 if constexpr (is_same_v<_Tp, bool>)
1670 auto __end = _M_do_parse(__pc, _AsBool);
1671 if (_M_spec._M_type == _Pres_s)
1672 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1673 || _M_spec._M_zero_fill)
1674 __throw_format_error(
"format error: format-spec contains "
1675 "invalid formatting options for "
1679 else if constexpr (__char<_Tp>)
1681 auto __end = _M_do_parse(__pc, _AsChar);
1682 if (_M_spec._M_type == _Pres_c)
1683 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1684 || _M_spec._M_zero_fill
1686 __throw_format_error(
"format error: format-spec contains "
1687 "invalid formatting options for "
1692 return _M_do_parse(__pc, _AsInteger);
1695 template<
typename _Int,
typename _Out>
1696 typename basic_format_context<_Out, _CharT>::iterator
1697 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1699 if (_M_spec._M_type == _Pres_c)
1700 return _M_format_character(_S_to_character(__i), __fc);
1702 constexpr size_t __buf_size =
sizeof(_Int) * __CHAR_BIT__ + 3;
1703 char __buf[__buf_size];
1704 to_chars_result __res{};
1706 string_view __base_prefix;
1707 make_unsigned_t<_Int> __u;
1709 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1713 char* __start = __buf + 3;
1714 char*
const __end = __buf +
sizeof(__buf);
1715 char*
const __start_digits = __start;
1717 switch (_M_spec._M_type)
1721 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1722 __res = to_chars(__start, __end, __u, 2);
1726 return _M_format_character(_S_to_character(__i), __fc);
1733 __res = to_chars(__start, __end, __u, 10);
1737 __base_prefix =
"0";
1738 __res = to_chars(__start, __end, __u, 8);
1742 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1743 __res = to_chars(__start, __end, __u, 16);
1744 if (_M_spec._M_type == _Pres_X)
1745 for (
auto __p = __start; __p != __res.ptr; ++__p)
1746#
if __has_builtin(__builtin_toupper)
1747 *__p = __builtin_toupper(*__p);
1754 if (_M_spec._M_alt && __base_prefix.size())
1756 __start -= __base_prefix.size();
1757 __builtin_memcpy(__start, __base_prefix.data(),
1758 __base_prefix.size());
1760 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1762 string_view __narrow_str(__start, __res.ptr - __start);
1763 size_t __prefix_len = __start_digits - __start;
1764 if constexpr (is_same_v<char, _CharT>)
1765 return _M_format_int(__narrow_str, __prefix_len, __fc);
1766#ifdef _GLIBCXX_USE_WCHAR_T
1769 _CharT __wbuf[__buf_size];
1770 size_t __n = __narrow_str.size();
1773 std::__to_wstring_numeric(__narrow_str.data(), __n, __wbuf);
1774 return _M_format_int(basic_string_view<_CharT>(__wbuf, __n),
1775 __prefix_len, __fc);
1780 template<
typename _Out>
1781 typename basic_format_context<_Out, _CharT>::iterator
1782 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1784 if (_M_spec._M_type == _Pres_c)
1785 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1786 if (_M_spec._M_type != _Pres_s)
1787 return format(
static_cast<unsigned char>(__i), __fc);
1789 basic_string<_CharT> __s;
1791 if (_M_spec._M_localized) [[unlikely]]
1794 __s = __i ? __np.truename() : __np.falsename();
1795 __est_width = __s.size();
1799 if constexpr (is_same_v<char, _CharT>)
1800 __s = __i ?
"true" :
"false";
1802 __s = __i ? L
"true" : L
"false";
1803 __est_width = __s.size();
1806 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1810 template<
typename _Out>
1811 typename basic_format_context<_Out, _CharT>::iterator
1812 _M_format_character(_CharT __c,
1813 basic_format_context<_Out, _CharT>& __fc)
const
1815 basic_string_view<_CharT> __in(&__c, 1u);
1816 size_t __width = 1u;
1818 if constexpr (
sizeof(_CharT) > 1u &&
1819 __unicode::__literal_encoding_is_unicode<_CharT>())
1820 __width = __unicode::__field_width(__c);
1822 if (!_M_spec._M_debug)
1823 return __format::__write_padded_as_spec(__in, __width,
1827 if (_M_spec._M_get_width(__fc) <= __width)
1828 return __format::__write_escaped(__fc.out(), __in, _Term_apos);
1831 _Fixedbuf_sink<_CharT> __sink(__buf);
1832 __format::__write_escaped(__sink.out(), __in, _Term_apos);
1834 __in = __sink.view();
1835 if (__in[1] == _Escapes<_CharT>::_S_bslash()[0])
1836 __width = __in.size();
1837 return __format::__write_padded_as_spec(__in, __width,
1841 template<
typename _Int>
1843 _S_to_character(_Int __i)
1846 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1848 if (_Traits::__min <= __i && __i <= _Traits::__max)
1849 return static_cast<_CharT
>(__i);
1851 else if constexpr (is_signed_v<_Int>)
1853 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1854 return static_cast<_CharT
>(__i);
1856 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1857 return static_cast<_CharT
>(__i);
1858 __throw_format_error(
"format error: integer not representable as "
1862 template<
typename _Out>
1863 typename basic_format_context<_Out, _CharT>::iterator
1864 _M_format_int(basic_string_view<_CharT> __str,
size_t __prefix_len,
1865 basic_format_context<_Out, _CharT>& __fc)
const
1867 size_t __width = _M_spec._M_get_width(__fc);
1868 if (_M_spec._M_localized)
1870 const auto& __l = __fc.locale();
1871 if (__l.name() !=
"C")
1873 auto& __np = use_facet<numpunct<_CharT>>(__l);
1874 string __grp = __np.grouping();
1877 size_t __n = __str.size() - __prefix_len;
1878 auto __p = (_CharT*)__builtin_alloca(2 * __n
1881 auto __s = __str.data();
1882 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1883 __s += __prefix_len;
1884 auto __end = std::__add_grouping(__p + __prefix_len,
1885 __np.thousands_sep(),
1889 __str = {__p, size_t(__end - __p)};
1894 if (__width <= __str.size())
1895 return __format::__write(__fc.out(), __str);
1897 char32_t __fill_char = _M_spec._M_fill;
1898 _Align __align = _M_spec._M_align;
1900 size_t __nfill = __width - __str.size();
1901 auto __out = __fc.out();
1902 if (__align == _Align_default)
1904 __align = _Align_right;
1905 if (_M_spec._M_zero_fill)
1907 __fill_char = _CharT(
'0');
1909 if (__prefix_len != 0)
1911 __out = __format::__write(
std::move(__out),
1912 __str.substr(0, __prefix_len));
1913 __str.remove_prefix(__prefix_len);
1917 __fill_char = _CharT(
' ');
1919 return __format::__write_padded(
std::move(__out), __str,
1920 __align, __nfill, __fill_char);
1923 _Spec<_CharT> _M_spec{};
1926#ifdef __BFLT16_DIG__
1927 using __bflt16_t =
decltype(0.0bf16);
1938#undef _GLIBCXX_FORMAT_F128
1940#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1943 using __flt128_t = __ieee128;
1944# define _GLIBCXX_FORMAT_F128 1
1946#ifdef __LONG_DOUBLE_IEEE128__
1950 to_chars(
char*,
char*, __ibm128)
noexcept
1951 __asm(
"_ZSt8to_charsPcS_e");
1954 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1955 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1958 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1959 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1960#elif __cplusplus == 202002L
1962 to_chars(
char*,
char*, __ieee128)
noexcept
1963 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1966 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1967 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1970 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1971 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1974#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1977 using __flt128_t =
long double;
1978# define _GLIBCXX_FORMAT_F128 2
1980#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1983 using __flt128_t = _Float128;
1984# define _GLIBCXX_FORMAT_F128 3
1986# if __cplusplus == 202002L
1990 to_chars(
char*,
char*, _Float128)
noexcept
1991# if _GLIBCXX_INLINE_VERSION
1992 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1994 __asm(
"_ZSt8to_charsPcS_DF128_");
1998 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1999# if _GLIBCXX_INLINE_VERSION
2000 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
2002 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
2006 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
2007# if _GLIBCXX_INLINE_VERSION
2008 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
2010 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
2015 using std::to_chars;
2018 template<
typename _Tp>
2019 concept __formattable_float
2020 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
2021 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
2023 template<__
char _CharT>
2024 struct __formatter_fp
2026 constexpr typename basic_format_parse_context<_CharT>::iterator
2027 parse(basic_format_parse_context<_CharT>& __pc)
2029 _Spec<_CharT> __spec{};
2030 const auto __last = __pc.end();
2031 auto __first = __pc.begin();
2033 auto __finalize = [
this, &__spec] {
2037 auto __finished = [&] {
2038 if (__first == __last || *__first ==
'}')
2049 __first = __spec._M_parse_fill_and_align(__first, __last);
2053 __first = __spec._M_parse_sign(__first, __last);
2057 __first = __spec._M_parse_alternate_form(__first, __last);
2061 __first = __spec._M_parse_zero_fill(__first, __last);
2065 if (__first[0] !=
'.')
2067 __first = __spec._M_parse_width(__first, __last, __pc);
2072 __first = __spec._M_parse_precision(__first, __last, __pc);
2076 __first = __spec._M_parse_locale(__first, __last);
2083 __spec._M_type = _Pres_a;
2087 __spec._M_type = _Pres_A;
2091 __spec._M_type = _Pres_e;
2095 __spec._M_type = _Pres_E;
2099 __spec._M_type = _Pres_f;
2103 __spec._M_type = _Pres_F;
2107 __spec._M_type = _Pres_g;
2111 __spec._M_type = _Pres_G;
2119 __format::__failed_to_parse_format_spec();
2122 template<
typename _Fp,
typename _Out>
2123 typename basic_format_context<_Out, _CharT>::iterator
2124 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2128 to_chars_result __res{};
2131 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2133 __prec = _M_spec._M_get_precision(__fc);
2136 bool __upper =
false;
2137 bool __trailing_zeros =
false;
2139 size_t __offset = 1;
2141 switch (_M_spec._M_type)
2144 if (__builtin_isfinite(__v))
2150 __fmt = chars_format::hex;
2153 if (__builtin_isfinite(__v))
2158 __fmt = chars_format::hex;
2166 __fmt = chars_format::scientific;
2173 __fmt = chars_format::fixed;
2180 __trailing_zeros =
true;
2182 __fmt = chars_format::general;
2187 __fmt = chars_format::general;
2191 char* __start = __buf + __offset;
2192 char* __end = __buf +
sizeof(__buf);
2195 auto __to_chars = [&](
char* __b,
char* __e) {
2197 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2198 else if (__fmt != chars_format{})
2199 return __format::to_chars(__b, __e, __v, __fmt);
2201 return __format::to_chars(__b, __e, __v);
2205 __res = __to_chars(__start, __end);
2207 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2211 size_t __guess = 7 + __offset + __prec;
2212 if (__fmt == chars_format::fixed)
2214 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2215 || is_same_v<_Fp, long double>)
2220 if constexpr (is_same_v<_Fp, float>)
2221 __builtin_frexpf(__v, &__exp);
2222 else if constexpr (is_same_v<_Fp, double>)
2223 __builtin_frexp(__v, &__exp);
2224 else if constexpr (is_same_v<_Fp, long double>)
2225 __builtin_frexpl(__v, &__exp);
2227 __guess += 1U + __exp * 4004U / 13301U;
2230 __guess += numeric_limits<_Fp>::max_exponent10;
2232 if (__guess <=
sizeof(__buf)) [[unlikely]]
2233 __guess =
sizeof(__buf) * 2;
2242 auto __overwrite = [&__to_chars, &__res, __offset] (
char* __p,
size_t __n)
2244 __res = __to_chars(__p + __offset, __p + __n - __offset);
2245 return __res.ec == errc{} ? __res.ptr - __p : 0;
2250 __start = __dynbuf.
data() + __offset;
2251 __end = __dynbuf.
data() + __dynbuf.
size();
2253 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2259 if (__builtin_signbit(__v))
2260 ranges::copy(string_view(
"-0x"), __start);
2262 ranges::copy(string_view(
"0x"), __start);
2268 for (
char* __p = __start; __p != __res.ptr; ++__p)
2273 if (!__builtin_signbit(__v))
2275 if (_M_spec._M_sign == _Sign_plus)
2277 else if (_M_spec._M_sign == _Sign_space)
2283 string_view __narrow_str(__start, __res.ptr - __start);
2287 if (_M_spec._M_alt && __builtin_isfinite(__v))
2289 string_view __s = __narrow_str;
2293 size_t __d = __s.find(
'.');
2294 if (__d != __s.npos)
2296 __p = __s.find(__expc, __d + 1);
2297 if (__p == __s.npos)
2301 if (__trailing_zeros)
2304 if (__s[__offset] !=
'0')
2306 __sigfigs = __p - __offset - 1;
2311 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2316 __p = __s.find(__expc);
2317 if (__p == __s.npos)
2320 __sigfigs = __d - __offset;
2323 if (__trailing_zeros && __prec != 0)
2328 __z = __prec - __sigfigs;
2331 if (
size_t __extras =
int(__d == __p) + __z)
2333 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2337 __builtin_memmove(__start + __p + __extras,
2341 __start[__p++] =
'.';
2342 __builtin_memset(__start + __p,
'0', __z);
2343 __narrow_str = {__s.data(), __s.size() + __extras};
2347 __dynbuf.
reserve(__s.size() + __extras);
2348 if (__dynbuf.
empty())
2350 __dynbuf = __s.
substr(0, __p);
2354 __dynbuf.
append(__z,
'0');
2355 __dynbuf.
append(__s.substr(__p));
2359 __dynbuf.
insert(__p, __extras,
'0');
2361 __dynbuf[__p] =
'.';
2363 __narrow_str = __dynbuf;
2368 basic_string<_CharT> __wstr;
2369 basic_string_view<_CharT> __str;
2370 if constexpr (is_same_v<_CharT, char>)
2371 __str = __narrow_str;
2372#ifdef _GLIBCXX_USE_WCHAR_T
2377 __wstr = std::__to_wstring_numeric(__narrow_str);
2382 if (_M_spec._M_localized && __builtin_isfinite(__v))
2384 auto __s = _M_localize(__str, __expc, __offset, __fc.locale());
2389 size_t __width = _M_spec._M_get_width(__fc);
2391 if (__width <= __str.size())
2392 return __format::__write(__fc.out(), __str);
2394 char32_t __fill_char = _M_spec._M_fill;
2395 _Align __align = _M_spec._M_align;
2397 size_t __nfill = __width - __str.size();
2398 auto __out = __fc.out();
2399 if (__align == _Align_default)
2401 __align = _Align_right;
2402 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2404 __fill_char = _CharT(
'0');
2407 __out = __format::__write(__out, __str.substr(0, __offset));
2408 __str.remove_prefix(__offset);
2412 __fill_char = _CharT(
' ');
2414 return __format::__write_padded(
std::move(__out), __str,
2415 __align, __nfill, __fill_char);
2419 basic_string<_CharT>
2420 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2421 int __offset,
const locale& __loc)
const
2423 basic_string<_CharT> __lstr;
2425 if (__loc == locale::classic())
2428 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2429 const _CharT __point = __np.decimal_point();
2430 const string __grp = __np.grouping();
2432 _CharT __dot, __exp;
2433 if constexpr (is_same_v<_CharT, char>)
2456 __builtin_unreachable();
2460 if (__grp.empty() && __point == __dot)
2463 size_t __d = __str.find(__dot);
2464 size_t __e = min(__d, __str.find(__exp));
2465 if (__e == __str.npos)
2467 const size_t __r = __str.size() - __e;
2468 auto __overwrite = [&](_CharT* __p, size_t) {
2470 ranges::copy_n(__str.data(), __offset, __p);
2472 auto __end = std::__add_grouping(__p + __offset, __np.thousands_sep(),
2473 __grp.data(), __grp.size(),
2474 __str.data() + __offset,
2475 __str.data() + __e);
2478 if (__d != __str.npos)
2484 const size_t __rlen = __str.size() - __e;
2486 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2489 return (__end - __p);
2491 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2495 _Spec<_CharT> _M_spec{};
2498 template<__format::__
char _CharT>
2499 struct __formatter_ptr
2502 __formatter_ptr() noexcept
2505 _M_spec._M_type = _Pres_x;
2506 _M_spec._M_alt =
true;
2510 __formatter_ptr(_Spec<_CharT> __spec) noexcept
2512 { _M_set_default(); }
2514 constexpr typename basic_format_parse_context<_CharT>::iterator
2515 parse(basic_format_parse_context<_CharT>& __pc)
2517 __format::_Spec<_CharT> __spec{};
2518 const auto __last = __pc.end();
2519 auto __first = __pc.begin();
2521 auto __finalize = [
this, &__spec] {
2526 auto __finished = [&] {
2527 if (__first == __last || *__first ==
'}')
2538 __first = __spec._M_parse_fill_and_align(__first, __last);
2544#if __glibcxx_format >= 202304L
2545 __first = __spec._M_parse_zero_fill(__first, __last);
2550 __first = __spec._M_parse_width(__first, __last, __pc);
2554 if (*__first ==
'p')
2556 __spec._M_type = _Pres_x;
2557 __spec._M_alt =
true;
2560#if __glibcxx_format >= 202304L
2561 else if (*__first ==
'P')
2563 __spec._M_type = _Pres_X;
2564 __spec._M_alt =
true;
2572 __format::__failed_to_parse_format_spec();
2575 template<
typename _Out>
2576 typename basic_format_context<_Out, _CharT>::iterator
2577 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2579 auto __u =
reinterpret_cast<__UINTPTR_TYPE__
>(__v);
2580 return __formatter_int<_CharT>(_M_spec).format(__u, __fc);
2584 [[__gnu__::__always_inline__]]
2588 if (_M_spec._M_type == _Pres_none)
2590 _M_spec._M_type = _Pres_x;
2591 _M_spec._M_alt =
true;
2595 __format::_Spec<_CharT> _M_spec;
2602 template<__format::__
char _CharT>
2603 struct formatter<_CharT, _CharT>
2605 formatter() =
default;
2607 constexpr typename basic_format_parse_context<_CharT>::iterator
2608 parse(basic_format_parse_context<_CharT>& __pc)
2610 return _M_f.template _M_parse<_CharT>(__pc);
2613 template<
typename _Out>
2614 typename basic_format_context<_Out, _CharT>::iterator
2615 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2617 if (_M_f._M_spec._M_type == __format::_Pres_c)
2618 return _M_f._M_format_character(__u, __fc);
2620 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2623#if __glibcxx_format_ranges
2625 set_debug_format() noexcept
2626 { _M_f._M_spec._M_debug =
true; }
2630 __format::__formatter_int<_CharT> _M_f;
2633#if __glibcxx_print >= 202403L
2634 template<__format::__
char _CharT>
2635 constexpr bool enable_nonlocking_formatter_optimization<_CharT> =
true;
2638#ifdef _GLIBCXX_USE_WCHAR_T
2641 struct formatter<char, wchar_t>
2643 formatter() =
default;
2645 constexpr typename basic_format_parse_context<wchar_t>::iterator
2646 parse(basic_format_parse_context<wchar_t>& __pc)
2648 return _M_f._M_parse<
char>(__pc);
2651 template<
typename _Out>
2652 typename basic_format_context<_Out, wchar_t>::iterator
2653 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2655 if (_M_f._M_spec._M_type == __format::_Pres_c)
2656 return _M_f._M_format_character(__u, __fc);
2658 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2661#if __glibcxx_format_ranges
2663 set_debug_format() noexcept
2664 { _M_f._M_spec._M_debug =
true; }
2668 __format::__formatter_int<wchar_t> _M_f;
2675 template<__format::__
char _CharT>
2676 struct formatter<_CharT*, _CharT>
2678 formatter() =
default;
2680 [[__gnu__::__always_inline__]]
2681 constexpr typename basic_format_parse_context<_CharT>::iterator
2682 parse(basic_format_parse_context<_CharT>& __pc)
2683 {
return _M_f.parse(__pc); }
2685 template<
typename _Out>
2686 [[__gnu__::__nonnull__]]
2687 typename basic_format_context<_Out, _CharT>::iterator
2688 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2689 {
return _M_f.format(__u, __fc); }
2691#if __glibcxx_format_ranges
2692 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2696 __format::__formatter_str<_CharT> _M_f;
2699#if __glibcxx_print >= 202403L
2700 template<__format::__
char _CharT>
2701 constexpr bool enable_nonlocking_formatter_optimization<_CharT*> =
true;
2704 template<__format::__
char _CharT>
2705 struct formatter<const _CharT*, _CharT>
2707 formatter() =
default;
2709 [[__gnu__::__always_inline__]]
2710 constexpr typename basic_format_parse_context<_CharT>::iterator
2711 parse(basic_format_parse_context<_CharT>& __pc)
2712 {
return _M_f.parse(__pc); }
2714 template<
typename _Out>
2715 [[__gnu__::__nonnull__]]
2716 typename basic_format_context<_Out, _CharT>::iterator
2717 format(
const _CharT* __u,
2718 basic_format_context<_Out, _CharT>& __fc)
const
2719 {
return _M_f.format(__u, __fc); }
2721#if __glibcxx_format_ranges
2722 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2726 __format::__formatter_str<_CharT> _M_f;
2729#if __glibcxx_print >= 202403L
2730 template<__format::__
char _CharT>
2732 enable_nonlocking_formatter_optimization<const _CharT*> =
true;
2735 template<__format::__
char _CharT,
size_t _Nm>
2736 struct formatter<_CharT[_Nm], _CharT>
2738 formatter() =
default;
2740 [[__gnu__::__always_inline__]]
2741 constexpr typename basic_format_parse_context<_CharT>::iterator
2742 parse(basic_format_parse_context<_CharT>& __pc)
2743 {
return _M_f.parse(__pc); }
2745 template<
typename _Out>
2746 typename basic_format_context<_Out, _CharT>::iterator
2747 format(
const _CharT (&__u)[_Nm],
2748 basic_format_context<_Out, _CharT>& __fc)
const
2749 {
return _M_f.format({__u, _Nm}, __fc); }
2751#if __glibcxx_format_ranges
2752 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2756 __format::__formatter_str<_CharT> _M_f;
2759#if __glibcxx_print >= 202403L
2760 template<__format::__
char _CharT,
size_t _Nm>
2761 constexpr bool enable_nonlocking_formatter_optimization<_CharT[_Nm]> =
true;
2764 template<
typename _Traits,
typename _Alloc>
2765 struct formatter<basic_string<char, _Traits, _Alloc>, char>
2767 formatter() =
default;
2769 [[__gnu__::__always_inline__]]
2770 constexpr typename basic_format_parse_context<char>::iterator
2771 parse(basic_format_parse_context<char>& __pc)
2772 {
return _M_f.parse(__pc); }
2774 template<
typename _Out>
2775 typename basic_format_context<_Out, char>::iterator
2776 format(
const basic_string<char, _Traits, _Alloc>& __u,
2777 basic_format_context<_Out, char>& __fc)
const
2778 {
return _M_f.format(__u, __fc); }
2780#if __glibcxx_format_ranges
2781 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2785 __format::__formatter_str<char> _M_f;
2788#if __glibcxx_print >= 202403L
2789 template<
typename _Tr,
typename _Alloc>
2791 enable_nonlocking_formatter_optimization<basic_string<char, _Tr, _Alloc>>
2795#ifdef _GLIBCXX_USE_WCHAR_T
2796 template<
typename _Traits,
typename _Alloc>
2797 struct formatter<basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2799 formatter() =
default;
2801 [[__gnu__::__always_inline__]]
2802 constexpr typename basic_format_parse_context<wchar_t>::iterator
2803 parse(basic_format_parse_context<wchar_t>& __pc)
2804 {
return _M_f.parse(__pc); }
2806 template<
typename _Out>
2807 typename basic_format_context<_Out, wchar_t>::iterator
2808 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2809 basic_format_context<_Out, wchar_t>& __fc)
const
2810 {
return _M_f.format(__u, __fc); }
2812#if __glibcxx_format_ranges
2813 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2817 __format::__formatter_str<wchar_t> _M_f;
2820#if __glibcxx_print >= 202403L
2821 template<
typename _Tr,
typename _Alloc>
2823 enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Tr, _Alloc>>
2829 template<
typename _Traits>
2830 struct formatter<basic_string_view<char, _Traits>, char>
2832 formatter() =
default;
2834 [[__gnu__::__always_inline__]]
2835 constexpr typename basic_format_parse_context<char>::iterator
2836 parse(basic_format_parse_context<char>& __pc)
2837 {
return _M_f.parse(__pc); }
2839 template<
typename _Out>
2840 typename basic_format_context<_Out, char>::iterator
2841 format(basic_string_view<char, _Traits> __u,
2842 basic_format_context<_Out, char>& __fc)
const
2843 {
return _M_f.format(__u, __fc); }
2845#if __glibcxx_format_ranges
2846 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2850 __format::__formatter_str<char> _M_f;
2853#if __glibcxx_print >= 202403L
2854 template<
typename _Tr>
2856 enable_nonlocking_formatter_optimization<basic_string_view<char, _Tr>>
2860#ifdef _GLIBCXX_USE_WCHAR_T
2861 template<
typename _Traits>
2862 struct formatter<basic_string_view<wchar_t, _Traits>, wchar_t>
2864 formatter() =
default;
2866 [[__gnu__::__always_inline__]]
2867 constexpr typename basic_format_parse_context<wchar_t>::iterator
2868 parse(basic_format_parse_context<wchar_t>& __pc)
2869 {
return _M_f.parse(__pc); }
2871 template<
typename _Out>
2872 typename basic_format_context<_Out, wchar_t>::iterator
2873 format(basic_string_view<wchar_t, _Traits> __u,
2874 basic_format_context<_Out, wchar_t>& __fc)
const
2875 {
return _M_f.format(__u, __fc); }
2877#if __glibcxx_format_ranges
2878 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2882 __format::__formatter_str<wchar_t> _M_f;
2885#if __glibcxx_print >= 202403L
2886 template<
typename _Tr>
2888 enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Tr>>
2899 template<
typename _Tp>
2900 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2902#if defined __SIZEOF_INT128__
2903 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2904 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2908 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2909 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2910#ifdef _GLIBCXX_USE_CHAR8_T
2911 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2913 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2914 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2916 template<
typename _Tp>
2917 concept __formattable_integer = __is_formattable_integer<_Tp>;
2922 template<__format::__formattable_
integer _Tp, __format::__
char _CharT>
2923 struct formatter<_Tp, _CharT>
2925 formatter() =
default;
2927 [[__gnu__::__always_inline__]]
2928 constexpr typename basic_format_parse_context<_CharT>::iterator
2929 parse(basic_format_parse_context<_CharT>& __pc)
2931 return _M_f.template _M_parse<_Tp>(__pc);
2934 template<
typename _Out>
2935 typename basic_format_context<_Out, _CharT>::iterator
2936 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2937 {
return _M_f.format(__u, __fc); }
2940 __format::__formatter_int<_CharT> _M_f;
2943#if __glibcxx_print >= 202403L
2944 template<__format::__formattable_
integer _Tp>
2946 enable_nonlocking_formatter_optimization<_Tp> =
true;
2949#if defined __glibcxx_to_chars
2951 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2952 struct formatter<_Tp, _CharT>
2954 formatter() =
default;
2956 [[__gnu__::__always_inline__]]
2957 constexpr typename basic_format_parse_context<_CharT>::iterator
2958 parse(basic_format_parse_context<_CharT>& __pc)
2959 {
return _M_f.parse(__pc); }
2961 template<
typename _Out>
2962 typename basic_format_context<_Out, _CharT>::iterator
2963 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2964 {
return _M_f.format(__u, __fc); }
2967 __format::__formatter_fp<_CharT> _M_f;
2970#if __glibcxx_print >= 202403L
2971 template<__format::__formattable_
float _Tp>
2973 enable_nonlocking_formatter_optimization<_Tp> =
true;
2976#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2978 template<__format::__
char _CharT>
2979 struct formatter<long double, _CharT>
2981 formatter() =
default;
2983 [[__gnu__::__always_inline__]]
2984 constexpr typename basic_format_parse_context<_CharT>::iterator
2985 parse(basic_format_parse_context<_CharT>& __pc)
2986 {
return _M_f.parse(__pc); }
2988 template<
typename _Out>
2989 typename basic_format_context<_Out, _CharT>::iterator
2990 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2991 {
return _M_f.format((
double)__u, __fc); }
2994 __format::__formatter_fp<_CharT> _M_f;
2998#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3000 template<__format::__
char _CharT>
3001 struct formatter<_Float32, _CharT>
3003 formatter() =
default;
3005 [[__gnu__::__always_inline__]]
3006 constexpr typename basic_format_parse_context<_CharT>::iterator
3007 parse(basic_format_parse_context<_CharT>& __pc)
3008 {
return _M_f.parse(__pc); }
3010 template<
typename _Out>
3011 typename basic_format_context<_Out, _CharT>::iterator
3012 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
3013 {
return _M_f.format((
float)__u, __fc); }
3016 __format::__formatter_fp<_CharT> _M_f;
3020#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3022 template<__format::__
char _CharT>
3023 struct formatter<_Float64, _CharT>
3025 formatter() =
default;
3027 [[__gnu__::__always_inline__]]
3028 constexpr typename basic_format_parse_context<_CharT>::iterator
3029 parse(basic_format_parse_context<_CharT>& __pc)
3030 {
return _M_f.parse(__pc); }
3032 template<
typename _Out>
3033 typename basic_format_context<_Out, _CharT>::iterator
3034 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
3035 {
return _M_f.format((
double)__u, __fc); }
3038 __format::__formatter_fp<_CharT> _M_f;
3042#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128
3044 template<__format::__
char _CharT>
3045 struct formatter<_Float128, _CharT>
3047 formatter() =
default;
3049 [[__gnu__::__always_inline__]]
3050 constexpr typename basic_format_parse_context<_CharT>::iterator
3051 parse(basic_format_parse_context<_CharT>& __pc)
3052 {
return _M_f.parse(__pc); }
3054 template<
typename _Out>
3055 typename basic_format_context<_Out, _CharT>::iterator
3056 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3057 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3060 __format::__formatter_fp<_CharT> _M_f;
3064#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128 == 2
3067 template<__format::__
char _CharT>
3068 struct formatter<__float128, _CharT>
3070 formatter() =
default;
3072 [[__gnu__::__always_inline__]]
3073 constexpr typename basic_format_parse_context<_CharT>::iterator
3074 parse(basic_format_parse_context<_CharT>& __pc)
3075 {
return _M_f.parse(__pc); }
3077 template<
typename _Out>
3078 typename basic_format_context<_Out, _CharT>::iterator
3079 format(__float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3080 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3083 __format::__formatter_fp<_CharT> _M_f;
3092 template<__format::__
char _CharT>
3093 struct formatter<const void*, _CharT>
3095 formatter() =
default;
3097 constexpr typename basic_format_parse_context<_CharT>::iterator
3098 parse(basic_format_parse_context<_CharT>& __pc)
3099 {
return _M_f.parse(__pc); }
3101 template<
typename _Out>
3102 typename basic_format_context<_Out, _CharT>::iterator
3103 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3104 {
return _M_f.format(__v, __fc); }
3107 __format::__formatter_ptr<_CharT> _M_f;
3110#if __glibcxx_print >= 202403L
3112 inline constexpr bool
3113 enable_nonlocking_formatter_optimization<const void*> =
true;
3116 template<__format::__
char _CharT>
3117 struct formatter<void*, _CharT>
3119 formatter() =
default;
3121 [[__gnu__::__always_inline__]]
3122 constexpr typename basic_format_parse_context<_CharT>::iterator
3123 parse(basic_format_parse_context<_CharT>& __pc)
3124 {
return _M_f.parse(__pc); }
3126 template<
typename _Out>
3127 typename basic_format_context<_Out, _CharT>::iterator
3128 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3129 {
return _M_f.format(__v, __fc); }
3132 __format::__formatter_ptr<_CharT> _M_f;
3135#if __glibcxx_print >= 202403l
3137 inline constexpr bool
3138 enable_nonlocking_formatter_optimization<void*> =
true;
3141 template<__format::__
char _CharT>
3142 struct formatter<nullptr_t, _CharT>
3144 formatter() =
default;
3146 [[__gnu__::__always_inline__]]
3147 constexpr typename basic_format_parse_context<_CharT>::iterator
3148 parse(basic_format_parse_context<_CharT>& __pc)
3149 {
return _M_f.parse(__pc); }
3151 template<
typename _Out>
3152 typename basic_format_context<_Out, _CharT>::iterator
3153 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3154 {
return _M_f.format(
nullptr, __fc); }
3157 __format::__formatter_ptr<_CharT> _M_f;
3161#if __glibcxx_print >= 202403L
3163 inline constexpr bool
3164 enable_nonlocking_formatter_optimization<nullptr_t> =
true;
3167#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3171 struct __formatter_disabled
3173 __formatter_disabled() =
delete;
3174 __formatter_disabled(
const __formatter_disabled&) =
delete;
3175 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3179 struct formatter<char*,
wchar_t>
3180 :
private __formatter_disabled { };
3182 struct formatter<const char*,
wchar_t>
3183 :
private __formatter_disabled { };
3184 template<
size_t _Nm>
3185 struct formatter<char[_Nm], wchar_t>
3186 :
private __formatter_disabled { };
3187 template<
class _Traits,
class _Allocator>
3188 struct formatter<basic_string<char, _Traits, _Allocator>, wchar_t>
3189 :
private __formatter_disabled { };
3190 template<
class _Traits>
3191 struct formatter<basic_string_view<char, _Traits>, wchar_t>
3192 :
private __formatter_disabled { };
3197 template<
typename _Out>
3198 struct format_to_n_result
3201 iter_difference_t<_Out> size;
3204_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3205template<
typename,
typename>
class vector;
3206_GLIBCXX_END_NAMESPACE_CONTAINER
3211 template<
typename _CharT>
3215 using iterator_category = output_iterator_tag;
3216 using value_type = void;
3217 using difference_type = ptrdiff_t;
3218 using pointer = void;
3219 using reference = void;
3221 _Drop_iter() =
default;
3222 _Drop_iter(
const _Drop_iter&) =
default;
3223 _Drop_iter& operator=(
const _Drop_iter&) =
default;
3225 [[__gnu__::__always_inline__]]
3226 constexpr _Drop_iter&
3227 operator=(_CharT __c)
3230 [[__gnu__::__always_inline__]]
3231 constexpr _Drop_iter&
3232 operator=(basic_string_view<_CharT> __s)
3235 [[__gnu__::__always_inline__]]
3236 constexpr _Drop_iter&
3239 [[__gnu__::__always_inline__]]
3240 constexpr _Drop_iter&
3241 operator++() {
return *
this; }
3243 [[__gnu__::__always_inline__]]
3244 constexpr _Drop_iter
3245 operator++(
int) {
return *
this; }
3248 template<
typename _CharT>
3251 _Sink<_CharT>* _M_sink =
nullptr;
3254 using iterator_category = output_iterator_tag;
3255 using value_type = void;
3256 using difference_type = ptrdiff_t;
3257 using pointer = void;
3258 using reference = void;
3260 _Sink_iter() =
default;
3261 _Sink_iter(
const _Sink_iter&) =
default;
3262 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3264 [[__gnu__::__always_inline__]]
3266 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3268 [[__gnu__::__always_inline__]]
3269 constexpr _Sink_iter&
3270 operator=(_CharT __c)
3272 _M_sink->_M_write(__c);
3276 [[__gnu__::__always_inline__]]
3277 constexpr _Sink_iter&
3278 operator=(basic_string_view<_CharT> __s)
3280 _M_sink->_M_write(__s);
3284 [[__gnu__::__always_inline__]]
3285 constexpr _Sink_iter&
3288 [[__gnu__::__always_inline__]]
3289 constexpr _Sink_iter&
3290 operator++() {
return *
this; }
3292 [[__gnu__::__always_inline__]]
3293 constexpr _Sink_iter
3294 operator++(
int) {
return *
this; }
3297 _M_reserve(
size_t __n)
const
3298 {
return _M_sink->_M_reserve(__n); }
3301 _M_discarding()
const
3302 {
return _M_sink->_M_discarding(); }
3308 template<
typename _CharT>
3311 friend class _Sink_iter<_CharT>;
3313 span<_CharT> _M_span;
3314 typename span<_CharT>::iterator _M_next;
3320 virtual void _M_overflow() = 0;
3324 [[__gnu__::__always_inline__]]
3326 _Sink(span<_CharT> __span) noexcept
3327 : _M_span(__span), _M_next(__span.begin())
3331 [[__gnu__::__always_inline__]]
3333 _M_used() const noexcept
3334 {
return _M_span.first(_M_next - _M_span.begin()); }
3337 [[__gnu__::__always_inline__]]
3338 constexpr span<_CharT>
3339 _M_unused() const noexcept
3340 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3343 [[__gnu__::__always_inline__]]
3345 _M_rewind() noexcept
3346 { _M_next = _M_span.begin(); }
3350 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3353 _M_next = __s.begin() + __pos;
3358 _M_write(_CharT __c)
3361 if (_M_next - _M_span.begin() == std::ssize(_M_span)) [[unlikely]]
3366 _M_write(basic_string_view<_CharT> __s)
3368 span __to = _M_unused();
3369 while (__to.size() <= __s.size())
3371 __s.copy(__to.data(), __to.size());
3372 _M_next += __to.size();
3373 __s.remove_prefix(__to.size());
3379 __s.copy(__to.data(), __s.size());
3380 _M_next += __s.size();
3389 explicit operator bool() const noexcept {
return _M_sink; }
3391 _CharT* get() const noexcept {
return _M_sink->_M_next.operator->(); }
3393 void _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3401 virtual _Reservation
3402 _M_reserve(
size_t __n)
3404 if (__n <= _M_unused().size())
3407 if (__n <= _M_span.size())
3410 if (__n <= _M_unused().size())
3424 _M_discarding()
const
3428 _Sink(
const _Sink&) =
delete;
3429 _Sink& operator=(
const _Sink&) =
delete;
3431 [[__gnu__::__always_inline__]]
3432 constexpr _Sink_iter<_CharT>
3434 {
return _Sink_iter<_CharT>(*
this); }
3438 template<
typename _CharT>
3439 class _Fixedbuf_sink final :
public _Sink<_CharT>
3442 _M_overflow()
override
3444 __glibcxx_assert(
false);
3449 [[__gnu__::__always_inline__]]
3451 _Fixedbuf_sink(span<_CharT> __buf)
3452 : _Sink<_CharT>(__buf)
3455 constexpr basic_string_view<_CharT>
3458 auto __s = this->_M_used();
3459 return basic_string_view<_CharT>(__s.data(), __s.size());
3464 template<
typename _CharT>
3465 class _Buf_sink :
public _Sink<_CharT>
3468 _CharT _M_buf[__stackbuf_size<_CharT>];
3470 [[__gnu__::__always_inline__]]
3472 _Buf_sink() noexcept
3473 : _Sink<_CharT>(_M_buf)
3477 using _GLIBCXX_STD_C::vector;
3481 template<
typename _Seq>
3482 class _Seq_sink :
public _Buf_sink<typename _Seq::value_type>
3484 using _CharT =
typename _Seq::value_type;
3490 _M_overflow()
override
3492 auto __s = this->_M_used();
3493 if (__s.empty()) [[unlikely]]
3497 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3499 if constexpr (__is_specialization_of<_Seq, basic_string>)
3500 _M_seq.append(__s.data(), __s.size());
3502 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3508 typename _Sink<_CharT>::_Reservation
3509 _M_reserve(
size_t __n)
override
3518 if constexpr (__is_specialization_of<_Seq, basic_string>
3519 || __is_specialization_of<_Seq, vector>)
3522 if (this->_M_used().size()) [[unlikely]]
3523 _Seq_sink::_M_overflow();
3526 const auto __sz = _M_seq.size();
3527 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3528 _M_seq.__resize_and_overwrite(__sz + __n,
3529 [](
auto,
auto __n2) {
3533 _M_seq.resize(__sz + __n);
3537 this->_M_reset(_M_seq, __sz);
3541 return _Sink<_CharT>::_M_reserve(__n);
3545 _M_bump(
size_t __n)
override
3547 if constexpr (__is_specialization_of<_Seq, basic_string>
3548 || __is_specialization_of<_Seq, vector>)
3550 auto __s = this->_M_used();
3551 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3553 _M_seq.resize(__s.size() + __n);
3555 this->_M_reset(this->_M_buf);
3559 void _M_trim(span<const _CharT> __s)
3560 requires __is_specialization_of<_Seq, basic_string>
3562 _GLIBCXX_DEBUG_ASSERT(__s.data() == this->_M_buf
3563 || __s.data() == _M_seq.data());
3564 if (__s.data() == _M_seq.data())
3565 _M_seq.resize(__s.size());
3567 this->_M_reset(this->_M_buf, __s.size());
3574 [[__gnu__::__always_inline__]]
3575 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3578 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3579 : _M_seq(std::move(__s))
3582 using _Sink<_CharT>::out;
3587 if (this->_M_used().size() != 0)
3588 _Seq_sink::_M_overflow();
3597 auto __s = this->_M_used();
3600 if (__s.size() != 0)
3601 _Seq_sink::_M_overflow();
3607 basic_string_view<_CharT>
3610 auto __span = _M_span();
3611 return basic_string_view<_CharT>(__span.data(), __span.size());
3615 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3617 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3623 template<
typename _CharT,
typename _OutIter>
3624 class _Iter_sink :
public _Buf_sink<_CharT>
3627 iter_difference_t<_OutIter> _M_max;
3630 size_t _M_count = 0;
3633 _M_overflow()
override
3635 auto __s = this->_M_used();
3637 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3638 else if (_M_count <
static_cast<size_t>(_M_max))
3640 auto __max = _M_max - _M_count;
3641 span<_CharT> __first;
3642 if (__max < __s.size())
3643 __first = __s.first(
static_cast<size_t>(__max));
3646 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3649 _M_count += __s.size();
3653 _M_discarding()
const override
3661 [[__gnu__::__always_inline__]]
3663 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3664 : _M_out(std::move(__out)), _M_max(__max)
3667 using _Sink<_CharT>::out;
3669 format_to_n_result<_OutIter>
3672 if (this->_M_used().size() != 0)
3673 _Iter_sink::_M_overflow();
3674 iter_difference_t<_OutIter> __count(_M_count);
3686 template<
typename _CharT>
3687 class _Ptr_sink :
public _Sink<_CharT>
3689 static constexpr size_t _S_no_limit = size_t(-1);
3693 size_t _M_count = 0;
3699 _M_overflow()
override
3701 if (this->_M_unused().size() != 0)
3704 auto __s = this->_M_used();
3706 if (_M_max != _S_no_limit)
3708 _M_count += __s.size();
3712 this->_M_reset(this->_M_buf);
3718 _M_rebuf(__s.data(), __s.size() + 1024, __s.size());
3723 _M_discarding()
const override
3730 typename _Sink<_CharT>::_Reservation
3731 _M_reserve(
size_t __n)
final
3733 auto __avail = this->_M_unused();
3734 if (__n > __avail.size())
3736 if (_M_max != _S_no_limit)
3739 auto __s = this->_M_used();
3740 _M_rebuf(__s.data(), __s.size() + __n, __s.size());
3746 template<
typename _IterDifference>
3748 _S_trim_max(_IterDifference __max)
3752 if constexpr (!is_integral_v<_IterDifference> ||
sizeof(__max) >
sizeof(size_t))
3754 if (_IterDifference((
size_t)-1) < __max)
3756 return size_t(__max);
3759 [[__gnu__::__always_inline__]]
3761 _M_rebuf(_CharT* __ptr,
size_t __total,
size_t __inuse = 0)
3763 std::span<_CharT> __span(__ptr, __total);
3764 this->_M_reset(__span, __inuse);
3769 _Ptr_sink(_CharT* __ptr,
size_t __n = _S_no_limit) noexcept
3770 : _Sink<_CharT>(_M_buf), _M_max(__n)
3774 else if (__n != _S_no_limit)
3775 _M_rebuf(__ptr, __n);
3776#if __has_builtin(__builtin_dynamic_object_size)
3777 else if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3778 _M_rebuf(__ptr, __bytes /
sizeof(_CharT));
3783 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3784 __n = (1024 - __off) /
sizeof(_CharT);
3785 if (__n > 0) [[likely]]
3786 _M_rebuf(__ptr, __n);
3792 template<contiguous_iterator _OutIter>
3794 _Ptr_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1)
3795 : _Ptr_sink(std::
to_address(__out), _S_trim_max(__n))
3798 template<contiguous_iterator _OutIter>
3799 format_to_n_result<_OutIter>
3800 _M_finish(_OutIter __first)
const
3802 auto __s = this->_M_used();
3803 if (__s.data() == _M_buf)
3806 iter_difference_t<_OutIter> __m(_M_max);
3807 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3808 return { __first + __m, __count };
3812 iter_difference_t<_OutIter> __count(__s.size());
3813 return { __first + __count, __count };
3818 template<
typename _CharT,
typename _OutIter>
3819 concept __contiguous_char_iter
3820 = contiguous_iterator<_OutIter>
3821 && same_as<iter_value_t<_OutIter>, _CharT>;
3832 template<
typename _Out,
typename _CharT>
3833 class _Padding_sink :
public _Str_sink<_CharT>
3838 size_t _M_printwidth;
3840 [[__gnu__::__always_inline__]]
3843 {
return _M_printwidth >= _M_maxwidth; }
3845 [[__gnu__::__always_inline__]]
3847 _M_buffering()
const
3849 if (_M_printwidth < _M_padwidth)
3851 if (_M_maxwidth != (
size_t)-1)
3852 return _M_printwidth < _M_maxwidth;
3857 _M_sync_discarding()
3859 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3860 if (_M_out._M_discarding())
3861 _M_maxwidth = _M_printwidth;
3867 span<_CharT> __new = this->_M_used();
3868 basic_string_view<_CharT> __str(__new.data(), __new.size());
3869 _M_out = __format::__write(
std::move(_M_out), __str);
3870 _M_sync_discarding();
3877 auto __str = this->view();
3879 _M_printwidth = __format::__truncate(__str, _M_maxwidth);
3881 this->_M_trim(__str);
3887 if (_M_printwidth >= _M_padwidth)
3889 _M_out = __format::__write(
std::move(_M_out), __str);
3890 _M_sync_discarding();
3895 _Str_sink<_CharT>::_M_overflow();
3898 this->_M_reset(this->_M_buf);
3903 _M_update(
size_t __new)
3905 _M_printwidth += __new;
3907 if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
3908 return _M_force_update();
3913 _M_overflow()
override
3919 else if (!_M_buffering())
3923 else if (_M_update(this->_M_used().size()))
3924 _Str_sink<_CharT>::_M_overflow();
3928 _M_discarding()
const override
3929 {
return _M_ignoring(); }
3931 typename _Sink<_CharT>::_Reservation
3932 _M_reserve(
size_t __n)
override
3937 else if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3938 if (!_M_buffering())
3941 if (!this->_M_used().empty())
3944 if (
auto __reserved = _M_out._M_reserve(__n))
3947 return _Sink<_CharT>::_M_reserve(__n);
3951 _M_bump(
size_t __n)
override
3958 _Sink<_CharT>::_M_bump(__n);
3964 [[__gnu__::__always_inline__]]
3966 _Padding_sink(_Out __out,
size_t __padwidth,
size_t __maxwidth)
3967 : _M_padwidth(__padwidth), _M_maxwidth(__maxwidth),
3968 _M_out(std::move(__out)), _M_printwidth(0)
3969 { _M_sync_discarding(); }
3971 [[__gnu__::__always_inline__]]
3973 _Padding_sink(_Out __out,
size_t __padwidth)
3974 : _Padding_sink(std::move(__out), __padwidth, (size_t)-1)
3978 _M_finish(_Align __align,
char32_t __fill_char)
3981 if (
auto __rem = this->_M_used().size())
3985 else if (!_M_buffering())
3991 if (!_M_buffering() || !_M_force_update())
3993 if (_M_printwidth >= _M_padwidth)
3996 const auto __str = this->view();
3997 if (_M_printwidth >= _M_padwidth)
3998 return __format::__write(
std::move(_M_out), __str);
4000 const size_t __nfill = _M_padwidth - _M_printwidth;
4001 return __format::__write_padded(
std::move(_M_out), __str,
4002 __align, __nfill, __fill_char);
4006 template<
typename _Out,
typename _CharT>
4007 class _Escaping_sink :
public _Buf_sink<_CharT>
4009 using _Esc = _Escapes<_CharT>;
4012 _Term_char _M_term : 2;
4013 unsigned _M_prev_escape : 1;
4014 unsigned _M_out_discards : 1;
4017 _M_sync_discarding()
4019 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4020 _M_out_discards = _M_out._M_discarding();
4026 span<_CharT> __bytes = this->_M_used();
4027 basic_string_view<_CharT> __str(__bytes.data(), __bytes.size());
4030 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4032 bool __prev_escape = _M_prev_escape;
4033 _M_out = __format::__write_escaped_unicode_part(
4034 std::move(_M_out), __str, __prev_escape, _M_term);
4035 _M_prev_escape = __prev_escape;
4037 __rem = __str.size();
4038 if (__rem > 0 && __str.data() != this->_M_buf) [[unlikely]]
4039 ranges::move(__str, this->_M_buf);
4042 _M_out = __format::__write_escaped_ascii(
4045 this->_M_reset(this->_M_buf, __rem);
4046 _M_sync_discarding();
4050 _M_overflow()
override
4052 if (_M_out_discards)
4059 _M_discarding()
const override
4060 {
return _M_out_discards; }
4063 [[__gnu__::__always_inline__]]
4065 _Escaping_sink(_Out __out, _Term_char __term)
4066 : _M_out(std::move(__out)), _M_term(__term),
4067 _M_prev_escape(true), _M_out_discards(false)
4069 _M_out = __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4070 _M_sync_discarding();
4076 if (_M_out_discards)
4079 if (!this->_M_used().empty())
4082 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4083 if (
auto __rem = this->_M_used(); !__rem.empty())
4085 basic_string_view<_CharT> __str(__rem.data(), __rem.size());
4086 _M_out = __format::__write_escape_seqs(
std::move(_M_out), __str);
4089 return __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4093 template<
typename _Context>
4096 using _CharT =
typename _Context::char_type;
4100 using _CharT =
typename _Context::char_type;
4101 using _Func = void(*)(basic_format_parse_context<_CharT>&,
4102 _Context&,
const void*);
4105 template<
typename _Tp>
4106 using __maybe_const_t
4107 = __conditional_t<__formattable_with<const _Tp, _Context>,
4110 template<
typename _Tq>
4112 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
4113 _Context& __format_ctx,
const void* __ptr)
4115 using _Td = remove_const_t<_Tq>;
4116 typename _Context::template formatter_type<_Td> __f;
4117 __parse_ctx.advance_to(__f.parse(__parse_ctx));
4118 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
4119 __format_ctx.advance_to(__f.format(__val, __format_ctx));
4122 template<
typename _Tp>
4123 requires (!is_same_v<remove_cv_t<_Tp>, handle>)
4125 handle(_Tp& __val) noexcept
4126 : _M_ptr(__builtin_addressof(__val))
4127 , _M_func(&_S_format<__maybe_const_t<_Tp>>)
4130 friend class basic_format_arg<_Context>;
4133 handle(
const handle&) =
default;
4134 handle& operator=(
const handle&) =
default;
4136 [[__gnu__::__always_inline__]]
4138 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
4139 { _M_func(__pc, __fc, this->_M_ptr); }
4154 unsigned long long _M_ull;
4157#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4158 long double _M_ldbl;
4161 __ieee128 _M_ieee128;
4163#ifdef __SIZEOF_FLOAT128__
4164 __float128 _M_float128;
4166 const _CharT* _M_str;
4167 basic_string_view<_CharT> _M_sv;
4170#ifdef __SIZEOF_INT128__
4172 unsigned __int128 _M_u128;
4174#ifdef __BFLT16_DIG__
4188 [[__gnu__::__always_inline__]]
4189 _Arg_value() : _M_none() { }
4192 template<
typename _Tp>
4193 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
4194 { _S_get<_Tp>() = __val; }
4199 template<
typename _Tp,
typename _Self,
typename... _Value>
4200 [[__gnu__::__always_inline__]]
4202 _S_access(_Self& __u, _Value... __value)
noexcept
4204 static_assert(
sizeof...(_Value) <= 1);
4205 if constexpr (is_same_v<_Tp, bool>)
4206 return (__u._M_bool = ... = __value);
4207 else if constexpr (is_same_v<_Tp, _CharT>)
4208 return (__u._M_c = ... = __value);
4209 else if constexpr (is_same_v<_Tp, int>)
4210 return (__u._M_i = ... = __value);
4211 else if constexpr (is_same_v<_Tp, unsigned>)
4212 return (__u._M_u = ... = __value);
4213 else if constexpr (is_same_v<_Tp, long long>)
4214 return (__u._M_ll = ... = __value);
4215 else if constexpr (is_same_v<_Tp, unsigned long long>)
4216 return (__u._M_ull = ... = __value);
4217 else if constexpr (is_same_v<_Tp, float>)
4218 return (__u._M_flt = ... = __value);
4219 else if constexpr (is_same_v<_Tp, double>)
4220 return (__u._M_dbl = ... = __value);
4221#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4222 else if constexpr (is_same_v<_Tp, long double>)
4223 return (__u._M_ldbl = ... = __value);
4225 else if constexpr (is_same_v<_Tp, __ibm128>)
4226 return (__u._M_ibm128 = ... = __value);
4227 else if constexpr (is_same_v<_Tp, __ieee128>)
4228 return (__u._M_ieee128 = ... = __value);
4230#ifdef __SIZEOF_FLOAT128__
4231 else if constexpr (is_same_v<_Tp, __float128>)
4232 return (__u._M_float128 = ... = __value);
4234 else if constexpr (is_same_v<_Tp, const _CharT*>)
4235 return (__u._M_str = ... = __value);
4236 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4237 return (__u._M_sv = ... = __value);
4238 else if constexpr (is_same_v<_Tp, const void*>)
4239 return (__u._M_ptr = ... = __value);
4240#ifdef __SIZEOF_INT128__
4241 else if constexpr (is_same_v<_Tp, __int128>)
4242 return (__u._M_i128 = ... = __value);
4243 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4244 return (__u._M_u128 = ... = __value);
4246#ifdef __BFLT16_DIG__
4247 else if constexpr (is_same_v<_Tp, __bflt16_t>)
4248 return (__u._M_bf16 = ... = __value);
4251 else if constexpr (is_same_v<_Tp, _Float16>)
4252 return (__u._M_f16 = ... = __value);
4255 else if constexpr (is_same_v<_Tp, _Float32>)
4256 return (__u._M_f32 = ... = __value);
4259 else if constexpr (is_same_v<_Tp, _Float64>)
4260 return (__u._M_f64 = ... = __value);
4262 else if constexpr (is_same_v<_Tp, handle>)
4263 return __u._M_handle;
4267 template<
typename _Tp>
4268 [[__gnu__::__always_inline__]]
4271 {
return _S_access<_Tp>(*
this); }
4273 template<
typename _Tp>
4274 [[__gnu__::__always_inline__]]
4276 _M_get() const noexcept
4277 {
return _S_access<_Tp>(*
this); }
4279 template<
typename _Tp>
4280 [[__gnu__::__always_inline__]]
4282 _M_set(_Tp __v)
noexcept
4285 if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4286 std::construct_at(&_M_sv, __v);
4287 else if constexpr (is_same_v<_Tp, handle>)
4288 std::construct_at(&_M_handle, __v);
4292 _S_access<_Tp>(*
this, __v);
4297 template<
typename _Context,
typename... _Args>
4300 template<
typename _Visitor,
typename _Ctx>
4301 decltype(
auto) __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4303 template<
typename _Ch,
typename _Tp>
4305 __to_arg_t_enum() noexcept;
4309 template<typename _Context>
4310 class basic_format_arg
4312 using _CharT =
typename _Context::char_type;
4315 using handle = __format::_Arg_value<_Context>::handle;
4317 [[__gnu__::__always_inline__]]
4318 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
4320 [[nodiscard,__gnu__::__always_inline__]]
4321 explicit operator bool() const noexcept
4322 {
return _M_type != __format::_Arg_none; }
4324#if __cpp_lib_format >= 202306L
4325 template<
typename _Visitor>
4327 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4330 template<
typename _Res,
typename _Visitor>
4332 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4337 template<
typename _Ctx>
4338 friend class basic_format_args;
4340 template<
typename _Ctx,
typename... _Args>
4341 friend class __format::_Arg_store;
4343 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
4345 __format::_Arg_value<_Context> _M_val;
4346 __format::_Arg_t _M_type;
4351 template<
typename _Tp>
4352 static consteval auto
4355 using _Td = remove_const_t<_Tp>;
4356 if constexpr (is_same_v<_Td, bool>)
4357 return type_identity<bool>();
4358 else if constexpr (is_same_v<_Td, _CharT>)
4359 return type_identity<_CharT>();
4360 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
4361 return type_identity<_CharT>();
4362#ifdef __SIZEOF_INT128__
4363 else if constexpr (is_same_v<_Td, __int128>)
4364 return type_identity<__int128>();
4365 else if constexpr (is_same_v<_Td, unsigned __int128>)
4366 return type_identity<unsigned __int128>();
4368 else if constexpr (__is_signed_integer<_Td>::value)
4370 if constexpr (
sizeof(_Td) <=
sizeof(int))
4371 return type_identity<int>();
4372 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
4373 return type_identity<long long>();
4375 else if constexpr (__is_unsigned_integer<_Td>::value)
4377 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
4378 return type_identity<unsigned>();
4379 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
4380 return type_identity<unsigned long long>();
4382 else if constexpr (is_same_v<_Td, float>)
4383 return type_identity<float>();
4384 else if constexpr (is_same_v<_Td, double>)
4385 return type_identity<double>();
4386#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4387 else if constexpr (is_same_v<_Td, long double>)
4388 return type_identity<long double>();
4390 else if constexpr (is_same_v<_Td, __ibm128>)
4391 return type_identity<__ibm128>();
4392 else if constexpr (is_same_v<_Td, __ieee128>)
4393 return type_identity<__ieee128>();
4395#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4396 else if constexpr (is_same_v<_Td, __float128>)
4397 return type_identity<__float128>();
4399#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4400 else if constexpr (is_same_v<_Td, __format::__bflt16_t>)
4401 return type_identity<__format::__bflt16_t>();
4403#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4404 else if constexpr (is_same_v<_Td, _Float16>)
4405 return type_identity<_Float16>();
4407#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4408 else if constexpr (is_same_v<_Td, _Float32>)
4409 return type_identity<_Float32>();
4411#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4412 else if constexpr (is_same_v<_Td, _Float64>)
4413 return type_identity<_Float64>();
4415 else if constexpr (__is_specialization_of<_Td, basic_string_view>
4416 || __is_specialization_of<_Td, basic_string>)
4418 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
4419 return type_identity<basic_string_view<_CharT>>();
4421 return type_identity<handle>();
4423 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
4424 return type_identity<const _CharT*>();
4425 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
4426 return type_identity<const _CharT*>();
4427 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
4428 return type_identity<const void*>();
4429 else if constexpr (is_same_v<_Td, nullptr_t>)
4430 return type_identity<const void*>();
4432 return type_identity<handle>();
4436 template<
typename _Tp>
4437 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
4440 template<
typename _Tp>
4441 static consteval __format::_Arg_t
4444 using namespace __format;
4445 if constexpr (is_same_v<_Tp, bool>)
4447 else if constexpr (is_same_v<_Tp, _CharT>)
4449 else if constexpr (is_same_v<_Tp, int>)
4451 else if constexpr (is_same_v<_Tp, unsigned>)
4453 else if constexpr (is_same_v<_Tp, long long>)
4455 else if constexpr (is_same_v<_Tp, unsigned long long>)
4457 else if constexpr (is_same_v<_Tp, float>)
4459 else if constexpr (is_same_v<_Tp, double>)
4461#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4462 else if constexpr (is_same_v<_Tp, long double>)
4466 else if constexpr (is_same_v<_Tp, __ibm128>)
4468 else if constexpr (is_same_v<_Tp, __ieee128>)
4469 return _Arg_ieee128;
4471#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4472 else if constexpr (is_same_v<_Tp, __float128>)
4473 return _Arg_float128;
4475#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4476 else if constexpr (is_same_v<_Tp, __format::__bflt16_t>)
4479#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4480 else if constexpr (is_same_v<_Tp, _Float16>)
4483#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4484 else if constexpr (is_same_v<_Tp, _Float32>)
4487#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4488 else if constexpr (is_same_v<_Tp, _Float64>)
4491 else if constexpr (is_same_v<_Tp, const _CharT*>)
4493 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4495 else if constexpr (is_same_v<_Tp, const void*>)
4497#ifdef __SIZEOF_INT128__
4498 else if constexpr (is_same_v<_Tp, __int128>)
4500 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4503 else if constexpr (is_same_v<_Tp, handle>)
4507 template<
typename _Tp>
4509 _M_set(_Tp __v)
noexcept
4511 _M_type = _S_to_enum<_Tp>();
4515 template<
typename _Tp>
4516 requires __format::__formattable_with<_Tp, _Context>
4518 basic_format_arg(_Tp& __v)
noexcept
4520 using _Td = _Normalize<_Tp>;
4521 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4522 _M_set(_Td{__v.data(), __v.size()});
4523 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4524 && is_same_v<_CharT, wchar_t>)
4525 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4527 _M_set(
static_cast<_Td
>(__v));
4530 template<
typename _Ctx,
typename... _Argz>
4532 make_format_args(_Argz&...)
noexcept;
4534 template<
typename _Visitor,
typename _Ctx>
4535 friend decltype(
auto)
4536 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4538 template<
typename _Visitor,
typename _Ctx>
4539 friend decltype(
auto)
4540 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4542 template<
typename _Ch,
typename _Tp>
4543 friend consteval __format::_Arg_t
4544 __format::__to_arg_t_enum() noexcept;
4546 [[__gnu__::__noinline__]]
4548 _M_handle_unrecognized() const;
4550 template<typename _Visitor>
4552 _M_visit(_Visitor&& __vis)
4556 using enum __format::_Arg_t;
4571#if __glibcxx_to_chars
4576#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4579#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4589#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4593#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4597#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4601#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4614#ifdef __SIZEOF_INT128__
4624 handle __h = _M_handle_unrecognized();
4629 template<
typename _Visitor>
4631 _M_visit_user(_Visitor&& __vis)
4633 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4635 constexpr bool __user_facing = __is_one_of<_Tp,
4636 monostate, bool, _CharT,
4637 int,
unsigned int,
long long int,
unsigned long long int,
4638 float, double,
long double,
4639 const _CharT*, basic_string_view<_CharT>,
4640 const void*, handle>::value;
4641 if constexpr (__user_facing)
4652 template<
typename _Visitor,
typename _Context>
4653 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4654 inline decltype(auto)
4655 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4661 template<
typename _Visitor,
typename _Ctx>
4662 inline decltype(
auto)
4663 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4666 struct _WidthPrecVisitor
4668 template<
typename _Tp>
4670 operator()(_Tp& __arg)
const
4672 if constexpr (is_same_v<_Tp, monostate>)
4673 __format::__invalid_arg_id_in_format_string();
4677 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4681 if constexpr (__is_unsigned_integer<_Tp>::value)
4683 else if constexpr (__is_signed_integer<_Tp>::value)
4687 __throw_format_error(
"format error: argument used for width or "
4688 "precision must be a non-negative integer");
4692#pragma GCC diagnostic push
4693#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4694 template<
typename _Context>
4696 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4697 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4700 template<
int _Bits,
size_t _Nm>
4702 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4704 __UINT64_TYPE__ __packed_types = 0;
4705 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4706 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4707 return __packed_types;
4712 template<
typename _Context>
4713 class basic_format_args
4715 static constexpr int _S_packed_type_bits = 5;
4716 static constexpr int _S_packed_type_mask = 0b11111;
4717 static constexpr int _S_max_packed_args = 12;
4719 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4721 template<
typename... _Args>
4722 using _Store = __format::_Arg_store<_Context, _Args...>;
4724 template<
typename _Ctx,
typename... _Args>
4725 friend class __format::_Arg_store;
4727 using uint64_t = __UINT64_TYPE__;
4728 using _Format_arg = basic_format_arg<_Context>;
4729 using _Format_arg_val = __format::_Arg_value<_Context>;
4735 uint64_t _M_packed_size : 4;
4736 uint64_t _M_unpacked_size : 60;
4739 const _Format_arg_val* _M_values;
4740 const _Format_arg* _M_args;
4744 _M_size() const noexcept
4745 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4747 typename __format::_Arg_t
4748 _M_type(
size_t __i)
const noexcept
4750 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4751 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4754 template<
typename _Ctx,
typename... _Args>
4756 make_format_args(_Args&...)
noexcept;
4759 template<
typename... _Args>
4760 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4762 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4765 template<
typename... _Args>
4766 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4768 [[nodiscard,__gnu__::__always_inline__]]
4769 basic_format_arg<_Context>
4770 get(
size_t __i)
const noexcept
4772 basic_format_arg<_Context> __arg;
4773 if (__i < _M_packed_size)
4775 __arg._M_type = _M_type(__i);
4776 __arg._M_val = _M_values[__i];
4778 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4779 __arg = _M_args[__i];
4786 template<
typename _Context,
typename... _Args>
4787 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4788 -> basic_format_args<_Context>;
4790 template<
typename _Context,
typename... _Args>
4792 make_format_args(_Args&... __fmt_args)
noexcept;
4795 template<
typename _Context,
typename... _Args>
4796 class __format::_Arg_store
4798 friend std::basic_format_args<_Context>;
4800 template<
typename _Ctx,
typename... _Argz>
4802#if _GLIBCXX_INLINE_VERSION
4805 make_format_args(_Argz&...)
noexcept;
4809 static constexpr bool _S_values_only
4810 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4813 = __conditional_t<_S_values_only,
4814 __format::_Arg_value<_Context>,
4815 basic_format_arg<_Context>>;
4817 _Element_t _M_args[
sizeof...(_Args)];
4819 template<
typename _Tp>
4821 _S_make_elt(_Tp& __v)
4823 using _Tq = remove_const_t<_Tp>;
4824 using _CharT =
typename _Context::char_type;
4825 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4826 "std::formatter must be specialized for the type "
4827 "of each format arg");
4828 using __format::__formattable_with;
4829 if constexpr (is_const_v<_Tp>)
4830 if constexpr (!__formattable_with<_Tp, _Context>)
4831 if constexpr (__formattable_with<_Tq, _Context>)
4832 static_assert(__formattable_with<_Tp, _Context>,
4833 "format arg must be non-const because its "
4834 "std::formatter specialization has a "
4835 "non-const reference parameter");
4836 basic_format_arg<_Context> __arg(__v);
4837 if constexpr (_S_values_only)
4838 return __arg._M_val;
4843 template<
typename... _Tp>
4844 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4845 [[__gnu__::__always_inline__]]
4846 _Arg_store(_Tp&... __a) noexcept
4847 : _M_args{_S_make_elt(__a)...}
4851 template<
typename _Context>
4852 class __format::_Arg_store<_Context>
4855 template<
typename _Context>
4856 template<
typename... _Args>
4858 basic_format_args<_Context>::
4859 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4861 if constexpr (
sizeof...(_Args) == 0)
4864 _M_unpacked_size = 0;
4867 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4870 _M_packed_size =
sizeof...(_Args);
4873 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4875 _M_values = __store._M_args;
4882 _M_unpacked_size =
sizeof...(_Args);
4884 _M_args = __store._M_args;
4889 template<
typename _Context = format_context,
typename... _Args>
4890 [[nodiscard,__gnu__::__always_inline__]]
4892 make_format_args(_Args&... __fmt_args)
noexcept
4894 using _Fmt_arg = basic_format_arg<_Context>;
4895 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4896 _Normalize<_Args>...>;
4897 return _Store(__fmt_args...);
4900#ifdef _GLIBCXX_USE_WCHAR_T
4902 template<
typename... _Args>
4903 [[nodiscard,__gnu__::__always_inline__]]
4905 make_wformat_args(_Args&... __args)
noexcept
4906 {
return std::make_format_args<wformat_context>(__args...); }
4912 template<
typename _Out,
typename _CharT,
typename _Context>
4914 __do_vformat_to(_Out, basic_string_view<_CharT>,
4915 const basic_format_args<_Context>&,
4916 const locale* =
nullptr);
4918 template<
typename _CharT>
struct __formatter_chrono;
4936 template<
typename _Out,
typename _CharT>
4937 class basic_format_context
4939 static_assert( output_iterator<_Out, const _CharT&> );
4941 basic_format_args<basic_format_context> _M_args;
4943 __format::_Optional_locale _M_loc;
4945 basic_format_context(basic_format_args<basic_format_context> __args,
4947 : _M_args(__args), _M_out(std::move(__out))
4950 basic_format_context(basic_format_args<basic_format_context> __args,
4951 _Out __out,
const std::locale& __loc)
4952 : _M_args(__args), _M_out(std::move(__out)), _M_loc(__loc)
4958 basic_format_context(
const basic_format_context&) =
delete;
4959 basic_format_context& operator=(
const basic_format_context&) =
delete;
4961 template<
typename _Out2,
typename _CharT2,
typename _Context2>
4963 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
4964 const basic_format_args<_Context2>&,
4967 friend __format::__formatter_chrono<_CharT>;
4970 ~basic_format_context() =
default;
4972 using iterator = _Out;
4973 using char_type = _CharT;
4974 template<
typename _Tp>
4975 using formatter_type = formatter<_Tp, _CharT>;
4978 basic_format_arg<basic_format_context>
4979 arg(
size_t __id)
const noexcept
4980 {
return _M_args.get(__id); }
4983 std::locale locale() {
return _M_loc.value(); }
4986 iterator out() {
return std::move(_M_out); }
4988 void advance_to(iterator __it) { _M_out =
std::move(__it); }
4991#if _GLIBCXX_EXTERN_TEMPLATE
4994 extern template basic_format_arg<format_context>::handle
4995 basic_format_arg<format_context>::_M_handle_unrecognized()
const;
4996# ifdef _GLIBCXX_USE_WCHAR_T
4997 extern template basic_format_arg<wformat_context>::handle
4998 basic_format_arg<wformat_context>::_M_handle_unrecognized()
const;
5001 template<
typename _Context>
5002 typename basic_format_arg<_Context>::handle
5003 basic_format_arg<_Context>::_M_handle_unrecognized()
const
5008 __throw_format_error(
"format error: unrecognized argument type");
5020 template<
typename _CharT>
5023 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
5025 typename basic_format_parse_context<_CharT>::_Scan_parse_context _M_pc;
5028 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5029 : _M_pc(__str, __nargs)
5032 constexpr iterator begin() const noexcept {
return _M_pc.begin(); }
5033 constexpr iterator end() const noexcept {
return _M_pc.end(); }
5038 basic_string_view<_CharT> __fmt = _M_fmt_str();
5040 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5042 _M_pc.advance_to(begin() + 1);
5043 _M_format_arg(_M_pc.next_arg_id());
5047 size_t __lbr = __fmt.find(
'{');
5048 size_t __rbr = __fmt.find(
'}');
5050 while (__fmt.size())
5052 auto __cmp = __lbr <=> __rbr;
5056 _M_pc.advance_to(end());
5061 if (__lbr + 1 == __fmt.size()
5062 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5063 __format::__unmatched_left_brace_in_format_string();
5064 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5065 iterator __last = begin() + __lbr + int(__is_escape);
5066 _M_on_chars(__last);
5067 _M_pc.advance_to(__last + 1);
5068 __fmt = _M_fmt_str();
5071 if (__rbr != __fmt.npos)
5073 __lbr = __fmt.find(
'{');
5077 _M_on_replacement_field();
5078 __fmt = _M_fmt_str();
5079 __lbr = __fmt.find(
'{');
5080 __rbr = __fmt.find(
'}');
5085 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5086 __format::__unmatched_right_brace_in_format_string();
5087 iterator __last = begin() + __rbr;
5088 _M_on_chars(__last);
5089 _M_pc.advance_to(__last + 1);
5090 __fmt = _M_fmt_str();
5091 if (__lbr != __fmt.npos)
5093 __rbr = __fmt.find(
'}');
5098 constexpr basic_string_view<_CharT>
5099 _M_fmt_str() const noexcept
5100 {
return {begin(), end()}; }
5102 constexpr virtual void _M_on_chars(iterator) { }
5104 constexpr void _M_on_replacement_field()
5106 auto __next = begin();
5110 __id = _M_pc.next_arg_id();
5111 else if (*__next ==
':')
5113 __id = _M_pc.next_arg_id();
5114 _M_pc.advance_to(++__next);
5118 auto [__i, __ptr] = __format::__parse_arg_id(begin(), end());
5119 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5120 __format::__invalid_arg_id_in_format_string();
5121 _M_pc.check_arg_id(__id = __i);
5124 _M_pc.advance_to(++__ptr);
5127 _M_pc.advance_to(__ptr);
5129 _M_format_arg(__id);
5130 if (begin() == end() || *begin() !=
'}')
5131 __format::__unmatched_left_brace_in_format_string();
5132 _M_pc.advance_to(begin() + 1);
5135 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5139 template<
typename _Out,
typename _CharT>
5140 class _Formatting_scanner :
public _Scanner<_CharT>
5143 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5144 basic_string_view<_CharT> __str)
5145 : _Scanner<_CharT>(__str), _M_fc(__fc)
5149 basic_format_context<_Out, _CharT>& _M_fc;
5151 using iterator =
typename _Scanner<_CharT>::iterator;
5154 _M_on_chars(iterator __last)
override
5156 basic_string_view<_CharT> __str(this->begin(), __last);
5157 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5161 _M_format_arg(
size_t __id)
override
5163 using _Context = basic_format_context<_Out, _CharT>;
5164 using handle =
typename basic_format_arg<_Context>::handle;
5166 __format::__visit_format_arg([
this](
auto& __arg) {
5168 using _Formatter =
typename _Context::template formatter_type<_Type>;
5169 if constexpr (is_same_v<_Type, monostate>)
5170 __format::__invalid_arg_id_in_format_string();
5171 else if constexpr (is_same_v<_Type, handle>)
5172 __arg.format(this->_M_pc, this->_M_fc);
5173 else if constexpr (is_default_constructible_v<_Formatter>)
5176 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5177 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5180 static_assert(__format::__formattable_with<_Type, _Context>);
5181 }, _M_fc.arg(__id));
5185 template<
typename _CharT,
typename _Tp>
5187 __to_arg_t_enum() noexcept
5189 using _Context = __format::__format_context<_CharT>;
5190 using _Fmt_arg = basic_format_arg<_Context>;
5191 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5192 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5196 template<
typename _CharT,
typename... _Args>
5197 class _Checking_scanner :
public _Scanner<_CharT>
5200 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5201 "std::formatter must be specialized for each type being formatted");
5205 _Checking_scanner(basic_string_view<_CharT> __str)
5206 : _Scanner<_CharT>(__str, sizeof...(_Args))
5208#if __cpp_lib_format >= 202305L
5209 this->_M_pc._M_types = _M_types.data();
5215 _M_format_arg(
size_t __id)
override
5217 if constexpr (
sizeof...(_Args) != 0)
5219 if (__id <
sizeof...(_Args))
5221 _M_parse_format_spec<_Args...>(__id);
5225 __builtin_unreachable();
5228 template<
typename _Tp,
typename... _OtherArgs>
5230 _M_parse_format_spec(
size_t __id)
5234 formatter<_Tp, _CharT> __f;
5235 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5237 else if constexpr (
sizeof...(_OtherArgs) != 0)
5238 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5240 __builtin_unreachable();
5243#if __cpp_lib_format >= 202305L
5244 array<_Arg_t,
sizeof...(_Args)>
5245 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5249 template<typename _CharT, unsigned = __unicode::__literal_encoding_is_unicode<_CharT>()>
5251 __do_vformat_to(_Sink_iter<_CharT> __out, basic_string_view<_CharT> __fmt,
5252 __format_context<_CharT>& __ctx)
5254 if constexpr (is_same_v<_CharT, char>)
5256 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5258 bool __done =
false;
5259 __format::__visit_format_arg([&](
auto& __arg) {
5260 using _Tp = remove_cvref_t<
decltype(__arg)>;
5261 if constexpr (is_same_v<_Tp, bool>)
5263 size_t __len = 4 + !__arg;
5264 const char* __chars[] = {
"false",
"true" };
5265 if (
auto __res = __out._M_reserve(__len))
5267 __builtin_memcpy(__res.get(), __chars[__arg], __len);
5268 __res._M_bump(__len);
5272 else if constexpr (is_same_v<_Tp, char>)
5274 if (
auto __res = __out._M_reserve(1))
5276 *__res.get() = __arg;
5281 else if constexpr (is_integral_v<_Tp>)
5283 make_unsigned_t<_Tp> __uval;
5284 const bool __neg = __arg < 0;
5286 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5289 const auto __n = __detail::__to_chars_len(__uval);
5290 if (
auto __res = __out._M_reserve(__n + __neg))
5292 auto __ptr = __res.get();
5294 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5296 __res._M_bump(__n + __neg);
5300 else if constexpr (is_convertible_v<_Tp, string_view>)
5302 string_view __sv = __arg;
5303 if (
auto __res = __out._M_reserve(__sv.size()))
5305 __builtin_memcpy(__res.get(), __sv.data(), __sv.size());
5306 __res._M_bump(__sv.size());
5316 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5317 __scanner._M_scan();
5326#if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE
5327 extern template _Sink_iter<char>
5328 __do_vformat_to<char, 1>(_Sink_iter<char>, string_view,
5330# ifdef _GLIBCXX_USE_WCHAR_T
5331 extern template _Sink_iter<wchar_t>
5332 __do_vformat_to<wchar_t, 1>(_Sink_iter<wchar_t>, wstring_view,
5337 template<
typename _Out,
typename _CharT,
typename _Context>
5339 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5340 const basic_format_args<_Context>& __args,
5341 const locale* __loc)
5343 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5345 auto __ctx = __loc ==
nullptr
5346 ? _Context(__args, __out)
5347 : _Context(__args, __out, *__loc);
5348 return __format::__do_vformat_to(__out, __fmt, __ctx);
5350 else if constexpr (__contiguous_char_iter<_CharT, _Out>)
5352 _Ptr_sink<_CharT> __sink(__out);
5353 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5354 return std::move(__sink)._M_finish(__out).out;
5358 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5359 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5360 return std::move(__sink)._M_finish().out;
5364 template<
typename _Out,
typename _CharT>
5365 inline format_to_n_result<_Out>
5366 __do_vformat_to_n(_Out __out, iter_difference_t<_Out> __n,
5367 basic_string_view<_CharT> __fmt,
5368 const type_identity_t<
5369 basic_format_args<__format_context<_CharT>>>& __args,
5370 const locale* __loc =
nullptr)
5372 if constexpr (__contiguous_char_iter<_CharT, _Out>)
5374 _Ptr_sink<_CharT> __sink(__out, __n);
5375 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5376 return std::move(__sink)._M_finish(__out);
5380 _Iter_sink<_CharT, _Out> __sink(
std::move(__out), __n);
5381 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5386#pragma GCC diagnostic pop
5391 template<
typename _CharT,
typename... _Args>
5392 template<
typename _Tp>
5393 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5395 basic_format_string<_CharT, _Args...>::
5396 basic_format_string(
const _Tp& __s)
5399 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5401 __scanner._M_scan();
5406 template<
typename _Out>
requires output_iterator<_Out, const char&>
5407 [[__gnu__::__always_inline__]]
5409 vformat_to(_Out __out, string_view __fmt, format_args __args)
5410 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5412#ifdef _GLIBCXX_USE_WCHAR_T
5413 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5414 [[__gnu__::__always_inline__]]
5416 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5417 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5420 template<
typename _Out>
requires output_iterator<_Out, const char&>
5421 [[__gnu__::__always_inline__]]
5423 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5426 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5429#ifdef _GLIBCXX_USE_WCHAR_T
5430 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5431 [[__gnu__::__always_inline__]]
5433 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5434 wformat_args __args)
5436 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5442 vformat(string_view __fmt, format_args __args)
5444 __format::_Str_sink<char> __buf;
5445 std::vformat_to(__buf.out(), __fmt, __args);
5449#ifdef _GLIBCXX_USE_WCHAR_T
5452 vformat(wstring_view __fmt, wformat_args __args)
5454 __format::_Str_sink<wchar_t> __buf;
5455 std::vformat_to(__buf.out(), __fmt, __args);
5462 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5464 __format::_Str_sink<char> __buf;
5465 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5469#ifdef _GLIBCXX_USE_WCHAR_T
5472 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5474 __format::_Str_sink<wchar_t> __buf;
5475 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5480 template<
typename... _Args>
5483 format(format_string<_Args...> __fmt, _Args&&... __args)
5484 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5486#ifdef _GLIBCXX_USE_WCHAR_T
5487 template<
typename... _Args>
5490 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5491 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5494 template<
typename... _Args>
5497 format(
const locale& __loc, format_string<_Args...> __fmt,
5500 return std::vformat(__loc, __fmt.get(),
5501 std::make_format_args(__args...));
5504#ifdef _GLIBCXX_USE_WCHAR_T
5505 template<
typename... _Args>
5508 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5511 return std::vformat(__loc, __fmt.get(),
5512 std::make_wformat_args(__args...));
5516 template<
typename _Out,
typename... _Args>
5517 requires output_iterator<_Out, const char&>
5519 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5521 return std::vformat_to(
std::move(__out), __fmt.get(),
5522 std::make_format_args(__args...));
5525#ifdef _GLIBCXX_USE_WCHAR_T
5526 template<
typename _Out,
typename... _Args>
5527 requires output_iterator<_Out, const wchar_t&>
5529 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5531 return std::vformat_to(
std::move(__out), __fmt.get(),
5532 std::make_wformat_args(__args...));
5536 template<
typename _Out,
typename... _Args>
5537 requires output_iterator<_Out, const char&>
5539 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5542 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5543 std::make_format_args(__args...));
5546#ifdef _GLIBCXX_USE_WCHAR_T
5547 template<
typename _Out,
typename... _Args>
5548 requires output_iterator<_Out, const wchar_t&>
5550 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5553 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5554 std::make_wformat_args(__args...));
5558 template<
typename _Out,
typename... _Args>
5559 requires output_iterator<_Out, const char&>
5560 inline format_to_n_result<_Out>
5561 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5562 format_string<_Args...> __fmt, _Args&&... __args)
5564 return __format::__do_vformat_to_n(
5566 std::make_format_args(__args...));
5569#ifdef _GLIBCXX_USE_WCHAR_T
5570 template<
typename _Out,
typename... _Args>
5571 requires output_iterator<_Out, const wchar_t&>
5572 inline format_to_n_result<_Out>
5573 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5574 wformat_string<_Args...> __fmt, _Args&&... __args)
5576 return __format::__do_vformat_to_n(
5578 std::make_wformat_args(__args...));
5582 template<
typename _Out,
typename... _Args>
5583 requires output_iterator<_Out, const char&>
5584 inline format_to_n_result<_Out>
5585 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5586 format_string<_Args...> __fmt, _Args&&... __args)
5588 return __format::__do_vformat_to_n(
5590 std::make_format_args(__args...), &__loc);
5593#ifdef _GLIBCXX_USE_WCHAR_T
5594 template<
typename _Out,
typename... _Args>
5595 requires output_iterator<_Out, const wchar_t&>
5596 inline format_to_n_result<_Out>
5597 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5598 wformat_string<_Args...> __fmt, _Args&&... __args)
5600 return __format::__do_vformat_to_n(
5602 std::make_wformat_args(__args...), &__loc);
5610 template<
typename _CharT>
5611 class _Counting_sink final :
public _Ptr_sink<_CharT>
5614 _Counting_sink() : _Ptr_sink<_CharT>(nullptr, 0) { }
5616 [[__gnu__::__always_inline__]]
5619 {
return this->_M_count + this->_M_used().size(); }
5622 template<
typename _CharT>
5623 class _Counting_sink :
public _Buf_sink<_CharT>
5625 size_t _M_count = 0;
5628 _M_overflow()
override
5630 if (!std::is_constant_evaluated())
5631 _M_count += this->_M_used().size();
5636 _Counting_sink() =
default;
5638 [[__gnu__::__always_inline__]]
5642 _Counting_sink::_M_overflow();
5650 template<
typename... _Args>
5653 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5655 __format::_Counting_sink<char> __buf;
5656 std::vformat_to(__buf.out(), __fmt.get(),
5657 std::make_format_args(__args...));
5658 return __buf.count();
5661#ifdef _GLIBCXX_USE_WCHAR_T
5662 template<
typename... _Args>
5665 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5667 __format::_Counting_sink<wchar_t> __buf;
5668 std::vformat_to(__buf.out(), __fmt.get(),
5669 std::make_wformat_args(__args...));
5670 return __buf.count();
5674 template<
typename... _Args>
5677 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5680 __format::_Counting_sink<char> __buf;
5681 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5682 std::make_format_args(__args...));
5683 return __buf.count();
5686#ifdef _GLIBCXX_USE_WCHAR_T
5687 template<
typename... _Args>
5690 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5693 __format::_Counting_sink<wchar_t> __buf;
5694 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5695 std::make_wformat_args(__args...));
5696 return __buf.count();
5700#if __glibcxx_format_ranges
5702 template<typename _Tp>
5703 consteval range_format
5706 using _Ref = ranges::range_reference_t<_Tp>;
5707 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5708 return range_format::disabled;
5709 else if constexpr (
requires {
typename _Tp::key_type; })
5711 if constexpr (
requires {
typename _Tp::mapped_type; })
5713 using _Up = remove_cvref_t<_Ref>;
5714 if constexpr (__is_pair<_Up>)
5715 return range_format::map;
5716 else if constexpr (__is_specialization_of<_Up, tuple>)
5717 if constexpr (tuple_size_v<_Up> == 2)
5718 return range_format::map;
5720 return range_format::set;
5723 return range_format::sequence;
5728 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5729 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5734 template<
typename _CharT,
typename _Out,
typename _Callback>
5735 typename basic_format_context<_Out, _CharT>::iterator
5736 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5737 const _Spec<_CharT>& __spec,
5740 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
5746 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
5748 const size_t __padwidth = __spec._M_get_width(__fc);
5749 if (__padwidth == 0)
5750 return __call(__fc);
5754 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5755 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5760 { _M_ctx =
nullptr; }
5765 _M_ctx->advance_to(_M_out);
5769 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5770 _Sink_iter<_CharT> _M_out;
5773 _Restore_out __restore(__fc);
5774 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
5775 __fc.advance_to(__sink.out());
5777 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
5778 __restore._M_disarm();
5783 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5784 struct __indexed_formatter_storage
5789 basic_format_parse_context<_CharT> __pc({});
5790 if (_M_formatter.parse(__pc) != __pc.end())
5791 __format::__failed_to_parse_format_spec();
5794 template<
typename _Out>
5796 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5797 basic_format_context<_Out, _CharT>& __fc,
5798 basic_string_view<_CharT> __sep)
const
5800 if constexpr (_Pos != 0)
5801 __fc.advance_to(__format::__write(__fc.out(), __sep));
5802 __fc.advance_to(_M_formatter.format(__elem, __fc));
5805 [[__gnu__::__always_inline__]]
5809 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5810 _M_formatter.set_debug_format();
5814 formatter<_Tp, _CharT> _M_formatter;
5817 template<
typename _CharT,
typename... _Tps>
5818 class __tuple_formatter
5820 using _String_view = basic_string_view<_CharT>;
5821 using _Seps = __format::_Separators<_CharT>;
5825 set_separator(basic_string_view<_CharT> __sep)
noexcept
5829 set_brackets(basic_string_view<_CharT> __open,
5830 basic_string_view<_CharT> __close)
noexcept
5838 constexpr typename basic_format_parse_context<_CharT>::iterator
5839 parse(basic_format_parse_context<_CharT>& __pc)
5841 auto __first = __pc.begin();
5842 const auto __last = __pc.end();
5843 __format::_Spec<_CharT> __spec{};
5845 auto __finished = [&]
5847 if (__first != __last && *__first !=
'}')
5851 _M_felems._M_parse();
5852 _M_felems.set_debug_format();
5859 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5863 __first = __spec._M_parse_width(__first, __last, __pc);
5867 if (*__first ==
'n')
5870 _M_open = _M_close = _String_view();
5872 else if (*__first ==
'm')
5875 if constexpr (
sizeof...(_Tps) == 2)
5877 _M_sep = _Seps::_S_colon();
5878 _M_open = _M_close = _String_view();
5881 __throw_format_error(
"format error: 'm' specifier requires range"
5882 " of pair or tuple of two elements");
5888 __format::__failed_to_parse_format_spec();
5892 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
5893 typename basic_format_context<_Out, _CharT>::iterator
5894 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
5895 basic_format_context<_Out, _CharT>& __fc)
const
5896 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
5898 template<
typename _Out>
5899 typename basic_format_context<_Out, _CharT>::iterator
5900 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
5901 basic_format_context<_Out, _CharT>& __fc)
const
5903 return __format::__format_padded(
5905 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
5907 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
5908 _M_felems._M_format(__elems..., __nfc, _M_sep);
5909 return __format::__write(__nfc.out(), _M_close);
5914 template<
size_t... _Ids>
5915 struct __formatters_storage
5916 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
5918 template<
size_t _Id,
typename _Up>
5919 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
5924 (_Base<_Ids, _Tps>::_M_parse(), ...);
5927 template<
typename _Out>
5929 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
5930 basic_format_context<_Out, _CharT>& __fc,
5931 _String_view __sep)
const
5933 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
5939 (_Base<_Ids, _Tps>::set_debug_format(), ...);
5943 template<
size_t... _Ids>
5945 _S_create_storage(index_sequence<_Ids...>)
5946 -> __formatters_storage<_Ids...>;
5948 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
5950 _Spec<_CharT> _M_spec{};
5951 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
5952 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
5953 _String_view _M_sep = _Seps::_S_comma();
5954 _Formatters _M_felems;
5957 template<
typename _Tp>
5958 concept __is_map_formattable
5959 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
5965 template<__format::__
char _CharT, formattable<_CharT> _Fp,
5966 formattable<_CharT> _Sp>
5967 struct formatter<
pair<_Fp, _Sp>, _CharT>
5968 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
5969 remove_cvref_t<_Sp>>
5972 using __maybe_const_pair
5973 = __conditional_t<formattable<const _Fp, _CharT>
5974 && formattable<const _Sp, _CharT>,
5975 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
5979 template<
typename _Out>
5980 typename basic_format_context<_Out, _CharT>::iterator
5981 format(__maybe_const_pair& __p,
5982 basic_format_context<_Out, _CharT>& __fc)
const
5983 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
5986#if __glibcxx_print >= 202406L
5989 template<
typename _Fp,
typename _Sp>
5990 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
5991 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
5992 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
5995 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
5996 struct formatter<tuple<_Tps...>, _CharT>
5997 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
6000 using __maybe_const_tuple
6001 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
6002 const tuple<_Tps...>, tuple<_Tps...>>;
6006 template<
typename _Out>
6007 typename basic_format_context<_Out, _CharT>::iterator
6008 format(__maybe_const_tuple& __t,
6009 basic_format_context<_Out, _CharT>& __fc)
const
6010 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
6013#if __glibcxx_print >= 202406L
6016 template<
typename... _Tps>
6017 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
6018 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
6022 template<
typename _Tp, __format::__
char _CharT>
6023 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
6024 class range_formatter
6026 using _String_view = basic_string_view<_CharT>;
6027 using _Seps = __format::_Separators<_CharT>;
6031 set_separator(basic_string_view<_CharT> __sep)
noexcept
6035 set_brackets(basic_string_view<_CharT> __open,
6036 basic_string_view<_CharT> __close)
noexcept
6042 constexpr formatter<_Tp, _CharT>&
6043 underlying() noexcept
6046 constexpr const formatter<_Tp, _CharT>&
6047 underlying() const noexcept
6052 constexpr typename basic_format_parse_context<_CharT>::iterator
6053 parse(basic_format_parse_context<_CharT>& __pc)
6055 auto __first = __pc.begin();
6056 const auto __last = __pc.end();
6057 __format::_Spec<_CharT> __spec{};
6058 bool __no_brace =
false;
6060 auto __finished = [&]
6061 {
return __first == __last || *__first ==
'}'; };
6063 auto __finalize = [&]
6069 auto __parse_val = [&](_String_view __nfs = _String_view())
6071 basic_format_parse_context<_CharT> __npc(__nfs);
6072 if (_M_fval.parse(__npc) != __npc.end())
6073 __format::__failed_to_parse_format_spec();
6074 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6075 _M_fval.set_debug_format();
6076 return __finalize();
6080 return __parse_val();
6082 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6084 return __parse_val();
6086 __first = __spec._M_parse_width(__first, __last, __pc);
6088 return __parse_val();
6090 if (*__first ==
'?')
6093 __spec._M_debug =
true;
6094 if (__finished() || *__first !=
's')
6095 __throw_format_error(
"format error: '?' is allowed only in"
6096 " combination with 's'");
6099 if (*__first ==
's')
6102 if constexpr (same_as<_Tp, _CharT>)
6104 __spec._M_type = __format::_Pres_s;
6106 return __finalize();
6107 __throw_format_error(
"format error: element format specifier"
6108 " cannot be provided when 's' specifier is used");
6111 __throw_format_error(
"format error: 's' specifier requires"
6112 " range of character types");
6116 return __parse_val();
6118 if (*__first ==
'n')
6121 _M_open = _M_close = _String_view();
6126 return __parse_val();
6128 if (*__first ==
'm')
6130 _String_view __m(__first, 1);
6132 if constexpr (__format::__is_map_formattable<_Tp>)
6134 _M_sep = _Seps::_S_comma();
6137 _M_open = _Seps::_S_braces().substr(0, 1);
6138 _M_close = _Seps::_S_braces().substr(1, 1);
6141 return __parse_val(__m);
6142 __throw_format_error(
"format error: element format specifier"
6143 " cannot be provided when 'm' specifier is used");
6146 __throw_format_error(
"format error: 'm' specifier requires"
6147 " range of pairs or tuples of two elements");
6151 return __parse_val();
6153 if (*__first ==
':')
6155 __pc.advance_to(++__first);
6156 __first = _M_fval.parse(__pc);
6160 return __finalize();
6162 __format::__failed_to_parse_format_spec();
6167 template<ranges::input_range _Rg,
typename _Out>
6168 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6169 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6170 typename basic_format_context<_Out, _CharT>::iterator
6171 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6173 using _Range = remove_reference_t<_Rg>;
6174 if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6175 return _M_format<const _Range>(__rg, __fc);
6177 return _M_format(__rg, __fc);
6181 template<ranges::input_range _Rg,
typename _Out>
6182 typename basic_format_context<_Out, _CharT>::iterator
6183 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6185 if constexpr (same_as<_Tp, _CharT>)
6186 if (_M_spec._M_type == __format::_Pres_s)
6188 __format::__formatter_str __fstr(_M_spec);
6189 return __fstr._M_format_range(__rg, __fc);
6191 return __format::__format_padded(
6193 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6194 {
return _M_format_elems(__rg, __nfc); });
6198 template<ranges::input_range _Rg,
typename _Out>
6199 typename basic_format_context<_Out, _CharT>::iterator
6200 _M_format_elems(_Rg& __rg,
6201 basic_format_context<_Out, _CharT>& __fc)
const
6203 auto __out = __format::__write(__fc.out(), _M_open);
6205 auto __first = ranges::begin(__rg);
6206 auto const __last = ranges::end(__rg);
6207 if (__first == __last)
6208 return __format::__write(__out, _M_close);
6210 __fc.advance_to(__out);
6211 __out = _M_fval.format(*__first, __fc);
6212 for (++__first; __first != __last; ++__first)
6214 __out = __format::__write(__out, _M_sep);
6215 __fc.advance_to(__out);
6216 __out = _M_fval.format(*__first, __fc);
6219 return __format::__write(__out, _M_close);
6222 __format::_Spec<_CharT> _M_spec{};
6223 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6224 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6225 _String_view _M_sep = _Seps::_S_comma();
6226 formatter<_Tp, _CharT> _M_fval;
6234 template<ranges::input_range _Rg, __format::__
char _CharT>
6235 requires (format_kind<_Rg> != range_format::disabled)
6236 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6237 struct formatter<_Rg, _CharT>
6240 static const bool _S_range_format_is_string =
6241 (format_kind<_Rg> == range_format::string)
6242 || (format_kind<_Rg> == range_format::debug_string);
6243 using _Vt = remove_cvref_t<
6244 ranges::range_reference_t<
6245 __format::__maybe_const_range<_Rg, _CharT>>>;
6247 static consteval bool _S_is_correct()
6249 if constexpr (_S_range_format_is_string)
6250 static_assert(same_as<_Vt, _CharT>);
6254 static_assert(_S_is_correct());
6257 constexpr formatter() noexcept
6259 using _Seps = __format::_Separators<_CharT>;
6260 if constexpr (format_kind<_Rg> == range_format::map)
6262 static_assert(__format::__is_map_formattable<_Vt>);
6263 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6264 _Seps::_S_braces().substr(1, 1));
6265 _M_under.underlying().set_brackets({}, {});
6266 _M_under.underlying().set_separator(_Seps::_S_colon());
6268 else if constexpr (format_kind<_Rg> == range_format::set)
6269 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6270 _Seps::_S_braces().substr(1, 1));
6274 set_separator(basic_string_view<_CharT> __sep)
noexcept
6275 requires (format_kind<_Rg> == range_format::sequence)
6276 { _M_under.set_separator(__sep); }
6279 set_brackets(basic_string_view<_CharT> __open,
6280 basic_string_view<_CharT> __close)
noexcept
6281 requires (format_kind<_Rg> == range_format::sequence)
6282 { _M_under.set_brackets(__open, __close); }
6286 constexpr typename basic_format_parse_context<_CharT>::iterator
6287 parse(basic_format_parse_context<_CharT>& __pc)
6289 auto __res = _M_under.parse(__pc);
6290 if constexpr (format_kind<_Rg> == range_format::debug_string)
6291 _M_under.set_debug_format();
6297 template<
typename _Out>
6298 typename basic_format_context<_Out, _CharT>::iterator
6299 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6300 basic_format_context<_Out, _CharT>& __fc)
const
6302 if constexpr (_S_range_format_is_string)
6303 return _M_under._M_format_range(__rg, __fc);
6305 return _M_under.format(__rg, __fc);
6309 using _Formatter_under
6310 = __conditional_t<_S_range_format_is_string,
6311 __format::__formatter_str<_CharT>,
6312 range_formatter<_Vt, _CharT>>;
6313 _Formatter_under _M_under;
6316#if __glibcxx_print >= 202406L
6317 template<ranges::input_range _Rg>
6318 requires (format_kind<_Rg> != range_format::disabled)
6319 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6323#undef _GLIBCXX_WIDEN
6325_GLIBCXX_END_NAMESPACE_VERSION
6328#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.
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
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.
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).
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
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
One of two subclasses of exception.