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 _Runtime_format_string
130 [[__gnu__::__always_inline__]]
131 _Runtime_format_string(basic_string_view<_CharT> __s) noexcept
134 _Runtime_format_string(
const _Runtime_format_string&) =
delete;
135 void operator=(
const _Runtime_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::_Runtime_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 >= 202311L
199 [[__gnu__::__always_inline__]]
200 inline __format::_Runtime_format_string<char>
201 runtime_format(string_view __fmt)
noexcept
204#ifdef _GLIBCXX_USE_WCHAR_T
205 [[__gnu__::__always_inline__]]
206 inline __format::_Runtime_format_string<wchar_t>
207 runtime_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;
282 template<
typename _CharT>
class basic_format_parse_context;
283 using format_parse_context = basic_format_parse_context<char>;
284#ifdef _GLIBCXX_USE_WCHAR_T
285 using wformat_parse_context = basic_format_parse_context<wchar_t>;
288 template<
typename _CharT>
289 class basic_format_parse_context
292 using char_type = _CharT;
293 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
294 using iterator = const_iterator;
297 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
298 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
301 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
302 void operator=(
const basic_format_parse_context&) =
delete;
304 constexpr const_iterator begin() const noexcept {
return _M_begin; }
305 constexpr const_iterator end() const noexcept {
return _M_end; }
308 advance_to(const_iterator __it)
noexcept
314 if (_M_indexing == _Manual)
315 __format::__conflicting_indexing_in_format_string();
320 if (std::is_constant_evaluated())
321 if (_M_next_arg_id == _M_num_args)
322 __format::__invalid_arg_id_in_format_string();
323 return _M_next_arg_id++;
327 check_arg_id(
size_t __id)
329 if (_M_indexing == _Auto)
330 __format::__conflicting_indexing_in_format_string();
331 _M_indexing = _Manual;
333 if (std::is_constant_evaluated())
334 if (__id >= _M_num_args)
335 __format::__invalid_arg_id_in_format_string();
338#if __cpp_lib_format >= 202305L
339 template<
typename... _Ts>
341 check_dynamic_spec(
size_t __id)
noexcept
343 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
344 "template arguments for check_dynamic_spec<Ts...>(id) "
345 "must be unique and must be one of the allowed types");
347 __check_dynamic_spec<_Ts...>(__id);
352 check_dynamic_spec_integral(
size_t __id)
noexcept
355 __check_dynamic_spec<int, unsigned,
long long,
356 unsigned long long>(__id);
361 check_dynamic_spec_string(
size_t __id)
noexcept
364 __check_dynamic_spec<const _CharT*, basic_string_view<_CharT>>(__id);
370 template<
typename _Tp,
typename... _Ts>
371 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
373 template<
typename... _Ts>
375 __valid_types_for_check_dynamic_spec()
379 if constexpr (
sizeof...(_Ts) == 0)
388 = __once<bool, _Ts...>
389 + __once<char_type, _Ts...>
390 + __once<int, _Ts...>
391 + __once<
unsigned int, _Ts...>
392 + __once<
long long int, _Ts...>
393 + __once<
unsigned long long int, _Ts...>
394 + __once<float, _Ts...>
395 + __once<double, _Ts...>
396 + __once<
long double, _Ts...>
397 + __once<
const char_type*, _Ts...>
398 + __once<basic_string_view<char_type>, _Ts...>
399 + __once<
const void*, _Ts...>;
400 return __sum ==
sizeof...(_Ts);
404 template<
typename... _Ts>
406 __check_dynamic_spec(
size_t __id)
noexcept;
409 static void __invalid_dynamic_spec(
const char*);
411 friend __format::_Scanner<_CharT>;
416 basic_format_parse_context(basic_string_view<_CharT> __fmt,
417 size_t __num_args) noexcept
418 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
424 enum _Indexing { _Unknown, _Manual, _Auto };
425 _Indexing _M_indexing = _Unknown;
426 size_t _M_next_arg_id = 0;
427 size_t _M_num_args = 0;
431 template<
typename _Tp,
template<
typename...>
class _Class>
432 constexpr bool __is_specialization_of =
false;
433 template<
template<
typename...>
class _Class,
typename... _Args>
434 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
439 template<
typename _CharT>
440 constexpr pair<unsigned short, const _CharT*>
441 __parse_integer(
const _CharT* __first,
const _CharT* __last)
443 if (__first == __last)
444 __builtin_unreachable();
446 if constexpr (is_same_v<_CharT, char>)
448 const auto __start = __first;
449 unsigned short __val = 0;
451 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
452 && __first != __start) [[likely]]
453 return {__val, __first};
457 constexpr int __n = 32;
459 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
460 __buf[__i] = __first[__i];
461 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
462 if (__ptr) [[likely]]
463 return {__v, __first + (__ptr - __buf)};
468 template<
typename _CharT>
469 constexpr pair<unsigned short, const _CharT*>
470 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
472 if (__first == __last)
473 __builtin_unreachable();
476 return {0, __first + 1};
478 if (
'1' <= *__first && *__first <=
'9')
480 const unsigned short __id = *__first -
'0';
481 const auto __next = __first + 1;
483 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
484 return {__id, __next};
486 return __format::__parse_integer(__first, __last);
491 enum class _Pres_type :
unsigned char {
495 _Pres_c = 2, _Pres_x, _Pres_X, _Pres_d, _Pres_o, _Pres_b, _Pres_B,
497 _Pres_g = 1, _Pres_G, _Pres_a, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F,
499 _Pres_p = _Pres_x, _Pres_P = _Pres_X,
502 using enum _Pres_type;
504 enum class _Sign :
unsigned char {
512 enum _WidthPrec :
unsigned char {
517 using enum _WidthPrec;
519 template<
typename _Context>
521 __int_from_arg(
const basic_format_arg<_Context>& __arg);
523 constexpr bool __is_digit(
char __c)
524 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
526 constexpr bool __is_xdigit(
char __c)
527 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
534 template<
typename _CharT>
535 struct _Spec : _SpecBase
537 unsigned short _M_width;
538 unsigned short _M_prec;
539 char32_t _M_fill =
' ';
543 unsigned _M_localized : 1;
544 unsigned _M_zero_fill : 1;
545 _WidthPrec _M_width_kind : 2;
546 _WidthPrec _M_prec_kind : 2;
547 unsigned _M_debug : 1;
548 _Pres_type _M_type : 4;
549 unsigned _M_reserved : 8;
553 using iterator =
typename basic_string_view<_CharT>::iterator;
555 static constexpr _Align
556 _S_align(_CharT __c)
noexcept
560 case '<':
return _Align_left;
561 case '>':
return _Align_right;
562 case '^':
return _Align_centre;
563 default:
return _Align_default;
569 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
570 {
return _M_parse_fill_and_align(__first, __last,
"{"); }
574 _M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill)
noexcept
576 for (
char __c : __not_fill)
577 if (*__first ==
static_cast<_CharT
>(__c))
580 using namespace __unicode;
581 if constexpr (__literal_encoding_is_unicode<_CharT>())
584 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
587 auto __beg = __uv.begin();
588 char32_t __c = *__beg++;
589 if (__is_scalar_value(__c))
590 if (
auto __next = __beg.base(); __next != __last)
591 if (_Align __align = _S_align(*__next); __align != _Align_default)
599 else if (__last - __first >= 2)
600 if (_Align __align = _S_align(__first[1]); __align != _Align_default)
607 if (_Align __align = _S_align(__first[0]); __align != _Align_default)
616 static constexpr _Sign
617 _S_sign(_CharT __c)
noexcept
621 case '+':
return _Sign_plus;
622 case '-':
return _Sign_minus;
623 case ' ':
return _Sign_space;
624 default:
return _Sign_default;
630 _M_parse_sign(iterator __first, iterator)
noexcept
632 if (_Sign __sign = _S_sign(*__first); __sign != _Sign_default)
642 _M_parse_alternate_form(iterator __first, iterator)
noexcept
654 _M_parse_zero_fill(iterator __first, iterator )
noexcept
665 static constexpr iterator
666 _S_parse_width_or_precision(iterator __first, iterator __last,
667 unsigned short& __val,
bool& __arg_id,
668 basic_format_parse_context<_CharT>& __pc)
670 if (__format::__is_digit(*__first))
672 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
674 __throw_format_error(
"format error: invalid width or precision "
679 else if (*__first ==
'{')
683 if (__first == __last)
684 __format::__unmatched_left_brace_in_format_string();
686 __val = __pc.next_arg_id();
689 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
690 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
691 __format::__invalid_arg_id_in_format_string();
693 __pc.check_arg_id(__v);
696#if __cpp_lib_format >= 202305L
697 __pc.check_dynamic_spec_integral(__val);
706 _M_parse_width(iterator __first, iterator __last,
707 basic_format_parse_context<_CharT>& __pc)
709 bool __arg_id =
false;
711 __throw_format_error(
"format error: width must be non-zero in "
713 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
715 if (__next != __first)
716 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
722 _M_parse_precision(iterator __first, iterator __last,
723 basic_format_parse_context<_CharT>& __pc)
725 if (__first[0] !=
'.')
728 iterator __next = ++__first;
729 bool __arg_id =
false;
730 if (__next != __last)
731 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
733 if (__next == __first)
734 __throw_format_error(
"format error: missing precision after '.' in "
736 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
742 _M_parse_locale(iterator __first, iterator )
noexcept
752 template<
typename _Context>
754 _M_get_width(_Context& __ctx)
const
757 if (_M_width_kind == _WP_value)
759 else if (_M_width_kind == _WP_from_arg)
760 __width = __format::__int_from_arg(__ctx.arg(_M_width));
764 template<
typename _Context>
766 _M_get_precision(_Context& __ctx)
const
769 if (_M_prec_kind == _WP_value)
771 else if (_M_prec_kind == _WP_from_arg)
772 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
777 template<
typename _Int>
779 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
783 else if (__sign == _Sign_plus)
785 else if (__sign == _Sign_space)
793 template<
typename _Out,
typename _CharT>
794 requires output_iterator<_Out, const _CharT&>
796 __write(_Out __out, basic_string_view<_CharT> __str)
798 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
804 for (_CharT __c : __str)
811 template<
typename _Out,
typename _CharT>
813 __write_padded(_Out __out, basic_string_view<_CharT> __str,
814 _Align __align,
size_t __nfill,
char32_t __fill_char)
816 const size_t __buflen = 0x20;
817 _CharT __padding_chars[__buflen];
818 __padding_chars[0] = _CharT();
819 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
821 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
824 while (__n > __padding.size())
826 __o = __format::__write(
std::move(__o), __padding);
827 __n -= __padding.size();
830 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
833 size_t __l, __r, __max;
834 if (__align == _Align_centre)
837 __r = __l + (__nfill & 1);
840 else if (__align == _Align_right)
853 using namespace __unicode;
854 if constexpr (__literal_encoding_is_unicode<_CharT>())
855 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
858 const char32_t __arr[1]{ __fill_char };
859 _Utf_view<_CharT, span<const char32_t, 1>> __v(__arr);
860 basic_string<_CharT> __padstr(__v.begin(), __v.end());
861 __padding = __padstr;
863 __out = __format::__write(
std::move(__out), __padding);
864 __out = __format::__write(
std::move(__out), __str);
866 __out = __format::__write(
std::move(__out), __padding);
870 if (__max < __buflen)
871 __padding.remove_suffix(__buflen - __max);
875 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
877 __out = __format::__write(
std::move(__out), __str);
885 template<
typename _CharT,
typename _Out>
887 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
888 size_t __estimated_width,
889 basic_format_context<_Out, _CharT>& __fc,
890 const _Spec<_CharT>& __spec,
891 _Align __align = _Align_left)
893 size_t __width = __spec._M_get_width(__fc);
895 if (__width <= __estimated_width)
896 return __format::__write(__fc.out(), __str);
898 const size_t __nfill = __width - __estimated_width;
900 if (__spec._M_align != _Align_default)
901 __align = __spec._M_align;
903 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
907 template<
typename _CharT>
909 __truncate(basic_string_view<_CharT>& __s,
size_t __prec)
911 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
913 if (__prec != (
size_t)-1)
914 return __unicode::__truncate(__s, __prec);
916 return __unicode::__field_width(__s);
920 __s = __s.substr(0, __prec);
925 enum class _Term_char :
unsigned char {
930 using enum _Term_char;
932 template<
typename _CharT>
935 using _Str_view = basic_string_view<_CharT>;
939 {
return _GLIBCXX_WIDEN(
"\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
943 {
return _S_all().substr(0, 3); }
946 _Str_view _S_newline()
947 {
return _S_all().substr(3, 3); }
950 _Str_view _S_return()
951 {
return _S_all().substr(6, 3); }
954 _Str_view _S_bslash()
955 {
return _S_all().substr(9, 3); }
959 {
return _S_all().substr(12, 3); }
963 {
return _S_all().substr(15, 3); }
967 {
return _S_all().substr(18, 2); }
971 {
return _S_all().substr(20, 2); }
974 _Str_view _S_term(_Term_char __term)
981 return _S_quote().substr(0, 1);
983 return _S_apos().substr(0, 1);
985 __builtin_unreachable();
989 template<
typename _CharT>
992 using _Str_view = basic_string_view<_CharT>;
996 {
return _GLIBCXX_WIDEN(
"[]{}(), : "); }
999 _Str_view _S_squares()
1000 {
return _S_all().substr(0, 2); }
1003 _Str_view _S_braces()
1004 {
return _S_all().substr(2, 2); }
1007 _Str_view _S_parens()
1008 {
return _S_all().substr(4, 2); }
1011 _Str_view _S_comma()
1012 {
return _S_all().substr(6, 2); }
1015 _Str_view _S_colon()
1016 {
return _S_all().substr(8, 2); }
1019 template<
typename _CharT>
1020 constexpr bool __should_escape_ascii(_CharT __c, _Term_char __term)
1022 using _Esc = _Escapes<_CharT>;
1025 case _Esc::_S_tab()[0]:
1026 case _Esc::_S_newline()[0]:
1027 case _Esc::_S_return()[0]:
1028 case _Esc::_S_bslash()[0]:
1030 case _Esc::_S_quote()[0]:
1031 return __term == _Term_quote;
1032 case _Esc::_S_apos()[0]:
1033 return __term == _Term_apos;
1035 return (__c >= 0 && __c < 0x20) || __c == 0x7f;
1040 constexpr bool __should_escape_unicode(
char32_t __c,
bool __prev_esc)
1042 if (__unicode::__should_escape_category(__c))
1046 return __unicode::__grapheme_cluster_break_property(__c)
1047 == __unicode::_Gcb_property::_Gcb_Extend;
1050 using uint_least32_t = __UINT_LEAST32_TYPE__;
1051 template<
typename _Out,
typename _CharT>
1053 __write_escape_seq(_Out __out, uint_least32_t __val,
1054 basic_string_view<_CharT> __prefix)
1056 using _Str_view = basic_string_view<_CharT>;
1057 constexpr size_t __max = 8;
1059 const string_view __narrow(
1061 std::__to_chars_i<uint_least32_t>(__buf, __buf + __max, __val, 16).ptr);
1063 __out = __format::__write(__out, __prefix);
1064 *__out = _Separators<_CharT>::_S_braces()[0];
1066 if constexpr (is_same_v<char, _CharT>)
1067 __out = __format::__write(__out, __narrow);
1068#ifdef _GLIBCXX_USE_WCHAR_T
1071 _CharT __wbuf[__max];
1072 const size_t __n = __narrow.size();
1073 std::__to_wstring_numeric(__narrow.data(), __n, __wbuf);
1074 __out = __format::__write(__out, _Str_view(__wbuf, __n));
1077 *__out = _Separators<_CharT>::_S_braces()[1];
1081 template<
typename _Out,
typename _CharT>
1083 __write_escape_seqs(_Out __out, basic_string_view<_CharT> __units)
1085 using _UChar = make_unsigned_t<_CharT>;
1086 for (_CharT __c : __units)
1087 __out = __format::__write_escape_seq(
1088 __out,
static_cast<_UChar
>(__c), _Escapes<_CharT>::_S_x());
1092 template<
typename _Out,
typename _CharT>
1094 __write_escaped_char(_Out __out, _CharT __c)
1096 using _UChar = make_unsigned_t<_CharT>;
1097 using _Esc = _Escapes<_CharT>;
1100 case _Esc::_S_tab()[0]:
1101 return __format::__write(__out, _Esc::_S_tab().substr(1, 2));
1102 case _Esc::_S_newline()[0]:
1103 return __format::__write(__out, _Esc::_S_newline().substr(1, 2));
1104 case _Esc::_S_return()[0]:
1105 return __format::__write(__out, _Esc::_S_return().substr(1, 2));
1106 case _Esc::_S_bslash()[0]:
1107 return __format::__write(__out, _Esc::_S_bslash().substr(1, 2));
1108 case _Esc::_S_quote()[0]:
1109 return __format::__write(__out, _Esc::_S_quote().substr(1, 2));
1110 case _Esc::_S_apos()[0]:
1111 return __format::__write(__out, _Esc::_S_apos().substr(1, 2));
1113 return __format::__write_escape_seq(
1114 __out,
static_cast<_UChar
>(__c), _Esc::_S_u());
1118 template<
typename _CharT,
typename _Out>
1120 __write_escaped_ascii(_Out __out,
1121 basic_string_view<_CharT> __str,
1124 using _Str_view = basic_string_view<_CharT>;
1125 auto __first = __str.begin();
1126 auto const __last = __str.end();
1127 while (__first != __last)
1129 auto __print = __first;
1131 while (__print != __last
1132 && !__format::__should_escape_ascii(*__print, __term))
1135 if (__print != __first)
1136 __out = __format::__write(__out, _Str_view(__first, __print));
1138 if (__print == __last)
1142 __out = __format::__write_escaped_char(__out, *__first);
1148 template<
typename _CharT,
typename _Out>
1150 __write_escaped_unicode_part(_Out __out, basic_string_view<_CharT>& __str,
1151 bool& __prev_esc, _Term_char __term)
1153 using _Str_view = basic_string_view<_CharT>;
1154 using _Esc = _Escapes<_CharT>;
1156 static constexpr char32_t __replace = U
'\uFFFD';
1157 static constexpr _Str_view __replace_rep = []
1160 if constexpr (is_same_v<char, _CharT>)
1161 return "\xEF\xBF\xBD";
1166 __unicode::_Utf_view<char32_t, _Str_view> __v(
std::move(__str));
1169 auto __first = __v.begin();
1170 auto const __last = __v.end();
1171 while (__first != __last)
1173 bool __esc_ascii =
false;
1174 bool __esc_unicode =
false;
1175 bool __esc_replace =
false;
1176 auto __should_escape = [&](
auto const& __it)
1180 = __format::__should_escape_ascii(*__it.base(), __term);
1181 if (__format::__should_escape_unicode(*__it, __prev_esc))
1182 return __esc_unicode =
true;
1183 if (*__it == __replace)
1185 _Str_view __units(__it.base(), __it._M_units());
1186 return __esc_replace = (__units != __replace_rep);
1191 auto __print = __first;
1192 while (__print != __last && !__should_escape(__print))
1198 if (__print != __first)
1199 __out = __format::__write(__out, _Str_view(__first.base(), __print.base()));
1201 if (__print == __last)
1206 __out = __format::__write_escaped_char(__out, *__first.base());
1207 else if (__esc_unicode)
1208 __out = __format::__write_escape_seq(__out, *__first, _Esc::_S_u());
1210 else if (_Str_view __units(__first.base(), __first._M_units());
1211 __units.end() != __last.base())
1212 __out = __format::__write_escape_seqs(__out, __units);
1226 template<
typename _CharT,
typename _Out>
1228 __write_escaped_unicode(_Out __out, basic_string_view<_CharT> __str,
1231 bool __prev_escape =
true;
1232 __out = __format::__write_escaped_unicode_part(__out, __str,
1233 __prev_escape, __term);
1234 __out = __format::__write_escape_seqs(__out, __str);
1238 template<
typename _CharT,
typename _Out>
1240 __write_escaped(_Out __out, basic_string_view<_CharT> __str, _Term_char __term)
1242 __out = __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1244 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1245 __out = __format::__write_escaped_unicode(__out, __str, __term);
1246 else if constexpr (is_same_v<char, _CharT>
1247 && __unicode::__literal_encoding_is_extended_ascii())
1248 __out = __format::__write_escaped_ascii(__out, __str, __term);
1251 __out = __format::__write_escaped_ascii(__out, __str, __term);
1253 return __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1257 struct _Optional_locale
1259 [[__gnu__::__always_inline__]]
1260 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
1262 _Optional_locale(
const locale& __loc) noexcept
1263 : _M_loc(__loc), _M_hasval(
true)
1266 _Optional_locale(
const _Optional_locale& __l) noexcept
1267 : _M_dummy(), _M_hasval(__l._M_hasval)
1270 std::construct_at(&_M_loc, __l._M_loc);
1274 operator=(
const _Optional_locale& __l)
noexcept
1279 _M_loc = __l._M_loc;
1286 else if (__l._M_hasval)
1288 std::construct_at(&_M_loc, __l._M_loc);
1294 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
1297 operator=(locale&& __loc)
noexcept
1303 std::construct_at(&_M_loc,
std::move(__loc));
1314 std::construct_at(&_M_loc);
1320 bool has_value() const noexcept {
return _M_hasval; }
1323 char _M_dummy =
'\0';
1326 bool _M_hasval =
false;
1329 template<__
char _CharT>
1330 struct __formatter_str
1332 __formatter_str() =
default;
1335 __formatter_str(_Spec<_CharT> __spec) noexcept
1339 constexpr typename basic_format_parse_context<_CharT>::iterator
1340 parse(basic_format_parse_context<_CharT>& __pc)
1342 auto __first = __pc.begin();
1343 const auto __last = __pc.end();
1344 _Spec<_CharT> __spec{};
1346 auto __finalize = [
this, &__spec] {
1350 auto __finished = [&] {
1351 if (__first == __last || *__first ==
'}')
1362 __first = __spec._M_parse_fill_and_align(__first, __last);
1366 __first = __spec._M_parse_width(__first, __last, __pc);
1370 __first = __spec._M_parse_precision(__first, __last, __pc);
1374 if (*__first ==
's')
1376 __spec._M_type = _Pres_s;
1379#if __glibcxx_format_ranges
1380 else if (*__first ==
'?')
1382 __spec._M_debug =
true;
1390 __format::__failed_to_parse_format_spec();
1393 template<
typename _Out>
1395 format(basic_string_view<_CharT> __s,
1396 basic_format_context<_Out, _CharT>& __fc)
const
1398 if (_M_spec._M_debug)
1399 return _M_format_escaped(__s, __fc);
1401 if (_M_spec._M_width_kind == _WP_none
1402 && _M_spec._M_prec_kind == _WP_none)
1403 return __format::__write(__fc.out(), __s);
1405 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1406 const size_t __width = __format::__truncate(__s, __maxwidth);
1407 return __format::__write_padded_as_spec(__s, __width, __fc, _M_spec);
1410 template<
typename _Out>
1412 _M_format_escaped(basic_string_view<_CharT> __s,
1413 basic_format_context<_Out, _CharT>& __fc)
const
1415 const size_t __padwidth = _M_spec._M_get_width(__fc);
1416 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1417 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1419 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1420 const size_t __width = __truncate(__s, __maxwidth);
1422 if (__padwidth <= __width && _M_spec._M_prec_kind == _WP_none)
1423 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1428 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth, __maxwidth);
1429 __format::__write_escaped(__sink.out(), __s, _Term_quote);
1430 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1433#if __glibcxx_format_ranges
1434 template<ranges::input_range _Rg,
typename _Out>
1435 requires same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _CharT>
1437 _M_format_range(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
1439 using _Range = remove_reference_t<_Rg>;
1440 using _String_view = basic_string_view<_CharT>;
1441 if constexpr (!is_lvalue_reference_v<_Rg>)
1442 return _M_format_range<_Range&>(__rg, __fc);
1443 else if constexpr (!is_const_v<_Range>
1444 && __simply_formattable_range<_Range, _CharT>)
1445 return _M_format_range<const _Range&>(__rg, __fc);
1446 else if constexpr (ranges::contiguous_range<_Rg>)
1448 _String_view __str(ranges::data(__rg),
1449 size_t(ranges::distance(__rg)));
1450 return format(__str, __fc);
1454 auto __handle_debug = [
this, &__rg]<
typename _NOut>(_NOut __nout)
1456 if (!_M_spec._M_debug)
1457 return ranges::copy(__rg,
std::move(__nout)).out;
1459 _Escaping_sink<_NOut, _CharT>
1461 ranges::copy(__rg, __sink.out());
1462 return __sink._M_finish();
1465 const size_t __padwidth = _M_spec._M_get_width(__fc);
1466 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1467 return __handle_debug(__fc.out());
1469 _Padding_sink<_Out, _CharT>
1470 __sink(__fc.out(), __padwidth, _M_spec._M_get_precision(__fc));
1471 __handle_debug(__sink.out());
1472 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1477 set_debug_format() noexcept
1478 { _M_spec._M_debug =
true; }
1482 _Spec<_CharT> _M_spec{};
1485 template<__
char _CharT>
1486 struct __formatter_int
1490 static constexpr _Pres_type _AsInteger = _Pres_d;
1491 static constexpr _Pres_type _AsBool = _Pres_s;
1492 static constexpr _Pres_type _AsChar = _Pres_c;
1494 __formatter_int() =
default;
1497 __formatter_int(_Spec<_CharT> __spec) noexcept
1500 if (_M_spec._M_type == _Pres_none)
1501 _M_spec._M_type = _Pres_d;
1504 constexpr typename basic_format_parse_context<_CharT>::iterator
1505 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1507 _Spec<_CharT> __spec{};
1508 __spec._M_type = __type;
1510 const auto __last = __pc.end();
1511 auto __first = __pc.begin();
1513 auto __finalize = [
this, &__spec] {
1517 auto __finished = [&] {
1518 if (__first == __last || *__first ==
'}')
1529 __first = __spec._M_parse_fill_and_align(__first, __last);
1533 __first = __spec._M_parse_sign(__first, __last);
1537 __first = __spec._M_parse_alternate_form(__first, __last);
1541 __first = __spec._M_parse_zero_fill(__first, __last);
1545 __first = __spec._M_parse_width(__first, __last, __pc);
1549 __first = __spec._M_parse_locale(__first, __last);
1556 __spec._M_type = _Pres_b;
1560 __spec._M_type = _Pres_B;
1566 if (__type != _AsBool)
1568 __spec._M_type = _Pres_c;
1573 __spec._M_type = _Pres_d;
1577 __spec._M_type = _Pres_o;
1581 __spec._M_type = _Pres_x;
1585 __spec._M_type = _Pres_X;
1589 if (__type == _AsBool)
1591 __spec._M_type = _Pres_s;
1595#if __glibcxx_format_ranges
1597 if (__type == _AsChar)
1599 __spec._M_debug =
true;
1609 __format::__failed_to_parse_format_spec();
1612 template<
typename _Tp>
1613 constexpr typename basic_format_parse_context<_CharT>::iterator
1614 _M_parse(basic_format_parse_context<_CharT>& __pc)
1616 if constexpr (is_same_v<_Tp, bool>)
1618 auto __end = _M_do_parse(__pc, _AsBool);
1619 if (_M_spec._M_type == _Pres_s)
1620 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1621 || _M_spec._M_zero_fill)
1622 __throw_format_error(
"format error: format-spec contains "
1623 "invalid formatting options for "
1627 else if constexpr (__char<_Tp>)
1629 auto __end = _M_do_parse(__pc, _AsChar);
1630 if (_M_spec._M_type == _Pres_c)
1631 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1632 || _M_spec._M_zero_fill
1634 __throw_format_error(
"format error: format-spec contains "
1635 "invalid formatting options for "
1640 return _M_do_parse(__pc, _AsInteger);
1643 template<
typename _Int,
typename _Out>
1644 typename basic_format_context<_Out, _CharT>::iterator
1645 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1647 if (_M_spec._M_type == _Pres_c)
1648 return _M_format_character(_S_to_character(__i), __fc);
1650 constexpr size_t __buf_size =
sizeof(_Int) * __CHAR_BIT__ + 3;
1651 char __buf[__buf_size];
1652 to_chars_result __res{};
1654 string_view __base_prefix;
1655 make_unsigned_t<_Int> __u;
1657 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1661 char* __start = __buf + 3;
1662 char*
const __end = __buf +
sizeof(__buf);
1663 char*
const __start_digits = __start;
1665 switch (_M_spec._M_type)
1669 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1670 __res = to_chars(__start, __end, __u, 2);
1674 return _M_format_character(_S_to_character(__i), __fc);
1680 __res = to_chars(__start, __end, __u, 10);
1684 __base_prefix =
"0";
1685 __res = to_chars(__start, __end, __u, 8);
1689 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1690 __res = to_chars(__start, __end, __u, 16);
1691 if (_M_spec._M_type == _Pres_X)
1692 for (
auto __p = __start; __p != __res.ptr; ++__p)
1693#
if __has_builtin(__builtin_toupper)
1694 *__p = __builtin_toupper(*__p);
1700 __builtin_unreachable();
1703 if (_M_spec._M_alt && __base_prefix.size())
1705 __start -= __base_prefix.size();
1706 __builtin_memcpy(__start, __base_prefix.data(),
1707 __base_prefix.size());
1709 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1712 string_view __narrow_str(__start, __res.ptr - __start);
1713 size_t __prefix_len = __start_digits - __start;
1714 if constexpr (is_same_v<char, _CharT>)
1715 return _M_format_int(__narrow_str, __prefix_len, __fc);
1716#ifdef _GLIBCXX_USE_WCHAR_T
1719 _CharT __wbuf[__buf_size];
1720 size_t __n = __narrow_str.size();
1721 std::__to_wstring_numeric(__narrow_str.data(), __n, __wbuf);
1722 return _M_format_int(basic_string_view<_CharT>(__wbuf, __n),
1723 __prefix_len, __fc);
1728 template<
typename _Out>
1729 typename basic_format_context<_Out, _CharT>::iterator
1730 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1732 if (_M_spec._M_type == _Pres_c)
1733 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1734 if (_M_spec._M_type != _Pres_s)
1735 return format(
static_cast<unsigned char>(__i), __fc);
1737 basic_string<_CharT> __s;
1739 if (_M_spec._M_localized) [[unlikely]]
1742 __s = __i ? __np.truename() : __np.falsename();
1743 __est_width = __s.size();
1747 if constexpr (is_same_v<char, _CharT>)
1748 __s = __i ?
"true" :
"false";
1750 __s = __i ? L
"true" : L
"false";
1751 __est_width = __s.size();
1754 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1758 template<
typename _Out>
1759 typename basic_format_context<_Out, _CharT>::iterator
1760 _M_format_character(_CharT __c,
1761 basic_format_context<_Out, _CharT>& __fc)
const
1763 basic_string_view<_CharT> __in(&__c, 1u);
1764 size_t __width = 1u;
1766 if constexpr (
sizeof(_CharT) > 1u &&
1767 __unicode::__literal_encoding_is_unicode<_CharT>())
1768 __width = __unicode::__field_width(__c);
1770 if (!_M_spec._M_debug)
1771 return __format::__write_padded_as_spec(__in, __width,
1775 if (_M_spec._M_get_width(__fc) <= __width)
1776 return __format::__write_escaped(__fc.out(), __in, _Term_apos);
1779 _Fixedbuf_sink<_CharT> __sink(__buf);
1780 __format::__write_escaped(__sink.out(), __in, _Term_apos);
1782 __in = __sink.view();
1783 if (__in[1] == _Escapes<_CharT>::_S_bslash()[0])
1784 __width = __in.size();
1785 return __format::__write_padded_as_spec(__in, __width,
1789 template<
typename _Int>
1791 _S_to_character(_Int __i)
1794 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1796 if (_Traits::__min <= __i && __i <= _Traits::__max)
1797 return static_cast<_CharT
>(__i);
1799 else if constexpr (is_signed_v<_Int>)
1801 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1802 return static_cast<_CharT
>(__i);
1804 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1805 return static_cast<_CharT
>(__i);
1806 __throw_format_error(
"format error: integer not representable as "
1810 template<
typename _Out>
1811 typename basic_format_context<_Out, _CharT>::iterator
1812 _M_format_int(basic_string_view<_CharT> __str,
size_t __prefix_len,
1813 basic_format_context<_Out, _CharT>& __fc)
const
1815 size_t __width = _M_spec._M_get_width(__fc);
1816 if (_M_spec._M_localized)
1818 const auto& __l = __fc.locale();
1819 if (__l.name() !=
"C")
1821 auto& __np = use_facet<numpunct<_CharT>>(__l);
1822 string __grp = __np.grouping();
1825 size_t __n = __str.size() - __prefix_len;
1826 auto __p = (_CharT*)__builtin_alloca(2 * __n
1829 auto __s = __str.data();
1830 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1831 __s += __prefix_len;
1832 auto __end = std::__add_grouping(__p + __prefix_len,
1833 __np.thousands_sep(),
1837 __str = {__p, size_t(__end - __p)};
1842 if (__width <= __str.size())
1843 return __format::__write(__fc.out(), __str);
1845 char32_t __fill_char = _M_spec._M_fill;
1846 _Align __align = _M_spec._M_align;
1848 size_t __nfill = __width - __str.size();
1849 auto __out = __fc.out();
1850 if (__align == _Align_default)
1852 __align = _Align_right;
1853 if (_M_spec._M_zero_fill)
1855 __fill_char = _CharT(
'0');
1857 if (__prefix_len != 0)
1859 __out = __format::__write(
std::move(__out),
1860 __str.substr(0, __prefix_len));
1861 __str.remove_prefix(__prefix_len);
1865 __fill_char = _CharT(
' ');
1867 return __format::__write_padded(
std::move(__out), __str,
1868 __align, __nfill, __fill_char);
1871 _Spec<_CharT> _M_spec{};
1874#ifdef __BFLT16_DIG__
1875 using __bflt16_t =
decltype(0.0bf16);
1886#undef _GLIBCXX_FORMAT_F128
1888#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1891 using __flt128_t = __ieee128;
1892# define _GLIBCXX_FORMAT_F128 1
1894#ifdef __LONG_DOUBLE_IEEE128__
1898 to_chars(
char*,
char*, __ibm128)
noexcept
1899 __asm(
"_ZSt8to_charsPcS_e");
1902 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1903 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1906 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1907 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1908#elif __cplusplus == 202002L
1910 to_chars(
char*,
char*, __ieee128)
noexcept
1911 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1914 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1915 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1918 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1919 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1922#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1925 using __flt128_t =
long double;
1926# define _GLIBCXX_FORMAT_F128 2
1928#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1931 using __flt128_t = _Float128;
1932# define _GLIBCXX_FORMAT_F128 3
1934# if __cplusplus == 202002L
1938 to_chars(
char*,
char*, _Float128)
noexcept
1939# if _GLIBCXX_INLINE_VERSION
1940 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1942 __asm(
"_ZSt8to_charsPcS_DF128_");
1946 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1947# if _GLIBCXX_INLINE_VERSION
1948 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1950 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
1954 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
1955# if _GLIBCXX_INLINE_VERSION
1956 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
1958 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
1963 using std::to_chars;
1966 template<
typename _Tp>
1967 concept __formattable_float
1968 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
1969 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
1971 template<__
char _CharT>
1972 struct __formatter_fp
1974 constexpr typename basic_format_parse_context<_CharT>::iterator
1975 parse(basic_format_parse_context<_CharT>& __pc)
1977 _Spec<_CharT> __spec{};
1978 const auto __last = __pc.end();
1979 auto __first = __pc.begin();
1981 auto __finalize = [
this, &__spec] {
1985 auto __finished = [&] {
1986 if (__first == __last || *__first ==
'}')
1997 __first = __spec._M_parse_fill_and_align(__first, __last);
2001 __first = __spec._M_parse_sign(__first, __last);
2005 __first = __spec._M_parse_alternate_form(__first, __last);
2009 __first = __spec._M_parse_zero_fill(__first, __last);
2013 if (__first[0] !=
'.')
2015 __first = __spec._M_parse_width(__first, __last, __pc);
2020 __first = __spec._M_parse_precision(__first, __last, __pc);
2024 __first = __spec._M_parse_locale(__first, __last);
2031 __spec._M_type = _Pres_a;
2035 __spec._M_type = _Pres_A;
2039 __spec._M_type = _Pres_e;
2043 __spec._M_type = _Pres_E;
2047 __spec._M_type = _Pres_f;
2051 __spec._M_type = _Pres_F;
2055 __spec._M_type = _Pres_g;
2059 __spec._M_type = _Pres_G;
2067 __format::__failed_to_parse_format_spec();
2070 template<
typename _Fp,
typename _Out>
2071 typename basic_format_context<_Out, _CharT>::iterator
2072 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2076 to_chars_result __res{};
2079 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2081 __prec = _M_spec._M_get_precision(__fc);
2083 char* __start = __buf + 1;
2084 char* __end = __buf +
sizeof(__buf);
2087 bool __upper =
false;
2088 bool __trailing_zeros =
false;
2091 switch (_M_spec._M_type)
2098 if (_M_spec._M_type != _Pres_A)
2100 __fmt = chars_format::hex;
2108 __fmt = chars_format::scientific;
2115 __fmt = chars_format::fixed;
2122 __trailing_zeros =
true;
2124 __fmt = chars_format::general;
2128 __fmt = chars_format::general;
2131 __builtin_unreachable();
2135 auto __to_chars = [&](
char* __b,
char* __e) {
2137 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2138 else if (__fmt != chars_format{})
2139 return __format::to_chars(__b, __e, __v, __fmt);
2141 return __format::to_chars(__b, __e, __v);
2145 __res = __to_chars(__start, __end);
2147 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2151 size_t __guess = 8 + __prec;
2152 if (__fmt == chars_format::fixed)
2154 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2155 || is_same_v<_Fp, long double>)
2160 if constexpr (is_same_v<_Fp, float>)
2161 __builtin_frexpf(__v, &__exp);
2162 else if constexpr (is_same_v<_Fp, double>)
2163 __builtin_frexp(__v, &__exp);
2164 else if constexpr (is_same_v<_Fp, long double>)
2165 __builtin_frexpl(__v, &__exp);
2167 __guess += 1U + __exp * 4004U / 13301U;
2170 __guess += numeric_limits<_Fp>::max_exponent10;
2172 if (__guess <=
sizeof(__buf)) [[unlikely]]
2173 __guess =
sizeof(__buf) * 2;
2182 auto __overwrite = [&__to_chars, &__res] (
char* __p,
size_t __n)
2184 __res = __to_chars(__p + 1, __p + __n - 1);
2185 return __res.ec == errc{} ? __res.ptr - __p : 0;
2190 __start = __dynbuf.
data() + 1;
2191 __end = __dynbuf.
data() + __dynbuf.
size();
2193 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2199 for (
char* __p = __start; __p != __res.ptr; ++__p)
2203 bool __have_sign =
true;
2205 if (!__builtin_signbit(__v))
2207 if (_M_spec._M_sign == _Sign_plus)
2209 else if (_M_spec._M_sign == _Sign_space)
2212 __have_sign =
false;
2215 string_view __narrow_str(__start, __res.ptr - __start);
2219 if (_M_spec._M_alt && __builtin_isfinite(__v))
2221 string_view __s = __narrow_str;
2225 size_t __d = __s.find(
'.');
2226 if (__d != __s.npos)
2228 __p = __s.find(__expc, __d + 1);
2229 if (__p == __s.npos)
2233 if (__trailing_zeros)
2236 if (__s[__have_sign] !=
'0')
2238 __sigfigs = __p - __have_sign - 1;
2243 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2248 __p = __s.find(__expc);
2249 if (__p == __s.npos)
2252 __sigfigs = __d - __have_sign;
2255 if (__trailing_zeros && __prec != 0)
2260 __z = __prec - __sigfigs;
2263 if (
size_t __extras =
int(__d == __p) + __z)
2265 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2269 __builtin_memmove(__start + __p + __extras,
2273 __start[__p++] =
'.';
2274 __builtin_memset(__start + __p,
'0', __z);
2275 __narrow_str = {__s.data(), __s.size() + __extras};
2279 __dynbuf.
reserve(__s.size() + __extras);
2280 if (__dynbuf.
empty())
2282 __dynbuf = __s.
substr(0, __p);
2286 __dynbuf.
append(__z,
'0');
2287 __dynbuf.
append(__s.substr(__p));
2291 __dynbuf.
insert(__p, __extras,
'0');
2293 __dynbuf[__p] =
'.';
2295 __narrow_str = __dynbuf;
2300 basic_string<_CharT> __wstr;
2301 basic_string_view<_CharT> __str;
2302 if constexpr (is_same_v<_CharT, char>)
2303 __str = __narrow_str;
2304#ifdef _GLIBCXX_USE_WCHAR_T
2307 __wstr = std::__to_wstring_numeric(__narrow_str);
2312 if (_M_spec._M_localized && __builtin_isfinite(__v))
2314 auto __s = _M_localize(__str, __expc, __fc.locale());
2319 size_t __width = _M_spec._M_get_width(__fc);
2321 if (__width <= __str.size())
2322 return __format::__write(__fc.out(), __str);
2324 char32_t __fill_char = _M_spec._M_fill;
2325 _Align __align = _M_spec._M_align;
2327 size_t __nfill = __width - __str.size();
2328 auto __out = __fc.out();
2329 if (__align == _Align_default)
2331 __align = _Align_right;
2332 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2334 __fill_char = _CharT(
'0');
2336 if (!__format::__is_xdigit(__narrow_str[0]))
2338 *__out++ = __str[0];
2339 __str.remove_prefix(1);
2343 __fill_char = _CharT(
' ');
2345 return __format::__write_padded(
std::move(__out), __str,
2346 __align, __nfill, __fill_char);
2350 basic_string<_CharT>
2351 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2352 const locale& __loc)
const
2354 basic_string<_CharT> __lstr;
2356 if (__loc == locale::classic())
2359 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2360 const _CharT __point = __np.decimal_point();
2361 const string __grp = __np.grouping();
2363 _CharT __dot, __exp;
2364 if constexpr (is_same_v<_CharT, char>)
2387 __builtin_unreachable();
2391 if (__grp.empty() && __point == __dot)
2394 size_t __d = __str.find(__dot);
2395 size_t __e = min(__d, __str.find(__exp));
2396 if (__e == __str.npos)
2398 const size_t __r = __str.size() - __e;
2399 auto __overwrite = [&](_CharT* __p, size_t) {
2402 if (
auto __c = __str.front(); __c ==
'-' || __c ==
'+' || __c ==
' ')
2407 auto __end = std::__add_grouping(__p + __off, __np.thousands_sep(),
2408 __grp.data(), __grp.size(),
2409 __str.data() + __off,
2410 __str.data() + __e);
2413 if (__d != __str.npos)
2419 const size_t __rlen = __str.size() - __e;
2421 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2424 return (__end - __p);
2426 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2430 _Spec<_CharT> _M_spec{};
2433 template<__format::__
char _CharT>
2434 struct __formatter_ptr
2437 __formatter_ptr() noexcept
2440 _M_spec._M_type = _Pres_p;
2441 _M_spec._M_alt =
true;
2445 __formatter_ptr(_Spec<_CharT> __spec) noexcept
2447 { _M_set_default(_Pres_p); }
2449 constexpr typename basic_format_parse_context<_CharT>::iterator
2450 parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type = _Pres_p)
2452 __format::_Spec<_CharT> __spec{};
2453 const auto __last = __pc.end();
2454 auto __first = __pc.begin();
2456 auto __finalize = [
this, &__spec, __type] {
2458 _M_set_default(__type);
2461 auto __finished = [&] {
2462 if (__first == __last || *__first ==
'}')
2473 __first = __spec._M_parse_fill_and_align(__first, __last);
2479#if __glibcxx_format >= 202304L
2480 __first = __spec._M_parse_zero_fill(__first, __last);
2485 __first = __spec._M_parse_width(__first, __last, __pc);
2489 if (*__first ==
'p')
2491 __spec._M_type = _Pres_p;
2492 __spec._M_alt = !__spec._M_alt;
2495#if __glibcxx_format >= 202304L
2496 else if (*__first ==
'P')
2498 __spec._M_type = _Pres_P;
2499 __spec._M_alt = !__spec._M_alt;
2507 __format::__failed_to_parse_format_spec();
2510 template<
typename _Out>
2511 typename basic_format_context<_Out, _CharT>::iterator
2512 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2514 auto __u =
reinterpret_cast<__UINTPTR_TYPE__
>(__v);
2515 return __formatter_int<_CharT>(_M_spec).format(__u, __fc);
2519 [[__gnu__::__always_inline__]]
2521 _M_set_default(_Pres_type __type)
2523 if (_M_spec._M_type == _Pres_none && __type != _Pres_none)
2525 _M_spec._M_type = __type;
2526 _M_spec._M_alt = !_M_spec._M_alt;
2530 __format::_Spec<_CharT> _M_spec;
2537 template<__format::__
char _CharT>
2538 struct formatter<_CharT, _CharT>
2540 formatter() =
default;
2542 constexpr typename basic_format_parse_context<_CharT>::iterator
2543 parse(basic_format_parse_context<_CharT>& __pc)
2545 return _M_f.template _M_parse<_CharT>(__pc);
2548 template<
typename _Out>
2549 typename basic_format_context<_Out, _CharT>::iterator
2550 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2552 if (_M_f._M_spec._M_type == __format::_Pres_c)
2553 return _M_f._M_format_character(__u, __fc);
2555 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2558#if __glibcxx_format_ranges
2560 set_debug_format() noexcept
2561 { _M_f._M_spec._M_debug =
true; }
2565 __format::__formatter_int<_CharT> _M_f;
2568#if __glibcxx_print >= 202403L
2569 template<__format::__
char _CharT>
2570 constexpr bool enable_nonlocking_formatter_optimization<_CharT> =
true;
2573#ifdef _GLIBCXX_USE_WCHAR_T
2576 struct formatter<char, wchar_t>
2578 formatter() =
default;
2580 constexpr typename basic_format_parse_context<wchar_t>::iterator
2581 parse(basic_format_parse_context<wchar_t>& __pc)
2583 return _M_f._M_parse<
char>(__pc);
2586 template<
typename _Out>
2587 typename basic_format_context<_Out, wchar_t>::iterator
2588 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2590 if (_M_f._M_spec._M_type == __format::_Pres_c)
2591 return _M_f._M_format_character(__u, __fc);
2593 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2596#if __glibcxx_format_ranges
2598 set_debug_format() noexcept
2599 { _M_f._M_spec._M_debug =
true; }
2603 __format::__formatter_int<wchar_t> _M_f;
2610 template<__format::__
char _CharT>
2611 struct formatter<_CharT*, _CharT>
2613 formatter() =
default;
2615 [[__gnu__::__always_inline__]]
2616 constexpr typename basic_format_parse_context<_CharT>::iterator
2617 parse(basic_format_parse_context<_CharT>& __pc)
2618 {
return _M_f.parse(__pc); }
2620 template<
typename _Out>
2621 [[__gnu__::__nonnull__]]
2622 typename basic_format_context<_Out, _CharT>::iterator
2623 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2624 {
return _M_f.format(__u, __fc); }
2626#if __glibcxx_format_ranges
2627 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2631 __format::__formatter_str<_CharT> _M_f;
2634#if __glibcxx_print >= 202403L
2635 template<__format::__
char _CharT>
2636 constexpr bool enable_nonlocking_formatter_optimization<_CharT*> =
true;
2639 template<__format::__
char _CharT>
2640 struct formatter<const _CharT*, _CharT>
2642 formatter() =
default;
2644 [[__gnu__::__always_inline__]]
2645 constexpr typename basic_format_parse_context<_CharT>::iterator
2646 parse(basic_format_parse_context<_CharT>& __pc)
2647 {
return _M_f.parse(__pc); }
2649 template<
typename _Out>
2650 [[__gnu__::__nonnull__]]
2651 typename basic_format_context<_Out, _CharT>::iterator
2652 format(
const _CharT* __u,
2653 basic_format_context<_Out, _CharT>& __fc)
const
2654 {
return _M_f.format(__u, __fc); }
2656#if __glibcxx_format_ranges
2657 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2661 __format::__formatter_str<_CharT> _M_f;
2664#if __glibcxx_print >= 202403L
2665 template<__format::__
char _CharT>
2667 enable_nonlocking_formatter_optimization<const _CharT*> =
true;
2670 template<__format::__
char _CharT,
size_t _Nm>
2671 struct formatter<_CharT[_Nm], _CharT>
2673 formatter() =
default;
2675 [[__gnu__::__always_inline__]]
2676 constexpr typename basic_format_parse_context<_CharT>::iterator
2677 parse(basic_format_parse_context<_CharT>& __pc)
2678 {
return _M_f.parse(__pc); }
2680 template<
typename _Out>
2681 typename basic_format_context<_Out, _CharT>::iterator
2682 format(
const _CharT (&__u)[_Nm],
2683 basic_format_context<_Out, _CharT>& __fc)
const
2684 {
return _M_f.format({__u, _Nm}, __fc); }
2686#if __glibcxx_format_ranges
2687 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2691 __format::__formatter_str<_CharT> _M_f;
2694#if __glibcxx_print >= 202403L
2695 template<__format::__
char _CharT,
size_t _Nm>
2696 constexpr bool enable_nonlocking_formatter_optimization<_CharT[_Nm]> =
true;
2699 template<
typename _Traits,
typename _Alloc>
2700 struct formatter<
basic_string<char, _Traits, _Alloc>, char>
2702 formatter() =
default;
2704 [[__gnu__::__always_inline__]]
2705 constexpr typename basic_format_parse_context<char>::iterator
2706 parse(basic_format_parse_context<char>& __pc)
2707 {
return _M_f.parse(__pc); }
2709 template<
typename _Out>
2710 typename basic_format_context<_Out, char>::iterator
2711 format(
const basic_string<char, _Traits, _Alloc>& __u,
2712 basic_format_context<_Out, char>& __fc)
const
2713 {
return _M_f.format(__u, __fc); }
2715#if __glibcxx_format_ranges
2716 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2720 __format::__formatter_str<char> _M_f;
2723#if __glibcxx_print >= 202403L
2724 template<
typename _Tr,
typename _Alloc>
2726 enable_nonlocking_formatter_optimization<basic_string<char, _Tr, _Alloc>>
2730#ifdef _GLIBCXX_USE_WCHAR_T
2731 template<
typename _Traits,
typename _Alloc>
2732 struct formatter<
basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2734 formatter() =
default;
2736 [[__gnu__::__always_inline__]]
2737 constexpr typename basic_format_parse_context<wchar_t>::iterator
2738 parse(basic_format_parse_context<wchar_t>& __pc)
2739 {
return _M_f.parse(__pc); }
2741 template<
typename _Out>
2742 typename basic_format_context<_Out, wchar_t>::iterator
2743 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2744 basic_format_context<_Out, wchar_t>& __fc)
const
2745 {
return _M_f.format(__u, __fc); }
2747#if __glibcxx_format_ranges
2748 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2752 __format::__formatter_str<wchar_t> _M_f;
2755#if __glibcxx_print >= 202403L
2756 template<
typename _Tr,
typename _Alloc>
2758 enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Tr, _Alloc>>
2764 template<
typename _Traits>
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(basic_string_view<char, _Traits> __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>
2791 enable_nonlocking_formatter_optimization<basic_string_view<char, _Tr>>
2795#ifdef _GLIBCXX_USE_WCHAR_T
2796 template<
typename _Traits>
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(basic_string_view<wchar_t, _Traits> __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>
2823 enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Tr>>
2834 template<
typename _Tp>
2835 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2837#if defined __SIZEOF_INT128__
2838 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2839 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2843 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2844 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2845#ifdef _GLIBCXX_USE_CHAR8_T
2846 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2848 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2849 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2851 template<
typename _Tp>
2852 concept __formattable_integer = __is_formattable_integer<_Tp>;
2857 template<__format::__formattable_
integer _Tp, __format::__
char _CharT>
2858 struct formatter<_Tp, _CharT>
2860 formatter() =
default;
2862 [[__gnu__::__always_inline__]]
2863 constexpr typename basic_format_parse_context<_CharT>::iterator
2864 parse(basic_format_parse_context<_CharT>& __pc)
2866 return _M_f.template _M_parse<_Tp>(__pc);
2869 template<
typename _Out>
2870 typename basic_format_context<_Out, _CharT>::iterator
2871 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2872 {
return _M_f.format(__u, __fc); }
2875 __format::__formatter_int<_CharT> _M_f;
2878#if __glibcxx_print >= 202403L
2879 template<__format::__formattable_
integer _Tp>
2881 enable_nonlocking_formatter_optimization<_Tp> =
true;
2884#if defined __glibcxx_to_chars
2886 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2887 struct formatter<_Tp, _CharT>
2889 formatter() =
default;
2891 [[__gnu__::__always_inline__]]
2892 constexpr typename basic_format_parse_context<_CharT>::iterator
2893 parse(basic_format_parse_context<_CharT>& __pc)
2894 {
return _M_f.parse(__pc); }
2896 template<
typename _Out>
2897 typename basic_format_context<_Out, _CharT>::iterator
2898 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2899 {
return _M_f.format(__u, __fc); }
2902 __format::__formatter_fp<_CharT> _M_f;
2905#if __glibcxx_print >= 202403L
2906 template<__format::__formattable_
float _Tp>
2908 enable_nonlocking_formatter_optimization<_Tp> =
true;
2911#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2913 template<__format::__
char _CharT>
2914 struct formatter<long double, _CharT>
2916 formatter() =
default;
2918 [[__gnu__::__always_inline__]]
2919 constexpr typename basic_format_parse_context<_CharT>::iterator
2920 parse(basic_format_parse_context<_CharT>& __pc)
2921 {
return _M_f.parse(__pc); }
2923 template<
typename _Out>
2924 typename basic_format_context<_Out, _CharT>::iterator
2925 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2926 {
return _M_f.format((
double)__u, __fc); }
2929 __format::__formatter_fp<_CharT> _M_f;
2933#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2935 template<__format::__
char _CharT>
2936 struct formatter<_Float16, _CharT>
2938 formatter() =
default;
2940 [[__gnu__::__always_inline__]]
2941 constexpr typename basic_format_parse_context<_CharT>::iterator
2942 parse(basic_format_parse_context<_CharT>& __pc)
2943 {
return _M_f.parse(__pc); }
2945 template<
typename _Out>
2946 typename basic_format_context<_Out, _CharT>::iterator
2947 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
2948 {
return _M_f.format((
float)__u, __fc); }
2951 __format::__formatter_fp<_CharT> _M_f;
2955#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2957 template<__format::__
char _CharT>
2958 struct formatter<_Float32, _CharT>
2960 formatter() =
default;
2962 [[__gnu__::__always_inline__]]
2963 constexpr typename basic_format_parse_context<_CharT>::iterator
2964 parse(basic_format_parse_context<_CharT>& __pc)
2965 {
return _M_f.parse(__pc); }
2967 template<
typename _Out>
2968 typename basic_format_context<_Out, _CharT>::iterator
2969 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
2970 {
return _M_f.format((
float)__u, __fc); }
2973 __format::__formatter_fp<_CharT> _M_f;
2977#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2979 template<__format::__
char _CharT>
2980 struct formatter<_Float64, _CharT>
2982 formatter() =
default;
2984 [[__gnu__::__always_inline__]]
2985 constexpr typename basic_format_parse_context<_CharT>::iterator
2986 parse(basic_format_parse_context<_CharT>& __pc)
2987 {
return _M_f.parse(__pc); }
2989 template<
typename _Out>
2990 typename basic_format_context<_Out, _CharT>::iterator
2991 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
2992 {
return _M_f.format((
double)__u, __fc); }
2995 __format::__formatter_fp<_CharT> _M_f;
2999#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128
3001 template<__format::__
char _CharT>
3002 struct formatter<_Float128, _CharT>
3004 formatter() =
default;
3006 [[__gnu__::__always_inline__]]
3007 constexpr typename basic_format_parse_context<_CharT>::iterator
3008 parse(basic_format_parse_context<_CharT>& __pc)
3009 {
return _M_f.parse(__pc); }
3011 template<
typename _Out>
3012 typename basic_format_context<_Out, _CharT>::iterator
3013 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3014 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3017 __format::__formatter_fp<_CharT> _M_f;
3021#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128 == 2
3024 template<__format::__
char _CharT>
3025 struct formatter<__float128, _CharT>
3027 formatter() =
default;
3029 [[__gnu__::__always_inline__]]
3030 constexpr typename basic_format_parse_context<_CharT>::iterator
3031 parse(basic_format_parse_context<_CharT>& __pc)
3032 {
return _M_f.parse(__pc); }
3034 template<
typename _Out>
3035 typename basic_format_context<_Out, _CharT>::iterator
3036 format(__float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3037 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3040 __format::__formatter_fp<_CharT> _M_f;
3044#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3046 template<__format::__
char _CharT>
3047 struct formatter<__format::__bflt16_t, _CharT>
3049 formatter() =
default;
3051 [[__gnu__::__always_inline__]]
3052 constexpr typename basic_format_parse_context<_CharT>::iterator
3053 parse(basic_format_parse_context<_CharT>& __pc)
3054 {
return _M_f.parse(__pc); }
3056 template<
typename _Out>
3057 typename basic_format_context<_Out, _CharT>::iterator
3058 format(__gnu_cxx::__bfloat16_t __u,
3059 basic_format_context<_Out, _CharT>& __fc)
const
3060 {
return _M_f.format((
float)__u, __fc); }
3063 __format::__formatter_fp<_CharT> _M_f;
3071 template<__format::__
char _CharT>
3072 struct formatter<const void*, _CharT>
3074 formatter() =
default;
3076 constexpr typename basic_format_parse_context<_CharT>::iterator
3077 parse(basic_format_parse_context<_CharT>& __pc)
3078 {
return _M_f.parse(__pc); }
3080 template<
typename _Out>
3081 typename basic_format_context<_Out, _CharT>::iterator
3082 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3083 {
return _M_f.format(__v, __fc); }
3086 __format::__formatter_ptr<_CharT> _M_f;
3089#if __glibcxx_print >= 202403L
3091 inline constexpr bool
3092 enable_nonlocking_formatter_optimization<const void*> =
true;
3095 template<__format::__
char _CharT>
3096 struct formatter<void*, _CharT>
3098 formatter() =
default;
3100 [[__gnu__::__always_inline__]]
3101 constexpr typename basic_format_parse_context<_CharT>::iterator
3102 parse(basic_format_parse_context<_CharT>& __pc)
3103 {
return _M_f.parse(__pc); }
3105 template<
typename _Out>
3106 typename basic_format_context<_Out, _CharT>::iterator
3107 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3108 {
return _M_f.format(__v, __fc); }
3111 __format::__formatter_ptr<_CharT> _M_f;
3114#if __glibcxx_print >= 202403l
3116 inline constexpr bool
3117 enable_nonlocking_formatter_optimization<void*> =
true;
3120 template<__format::__
char _CharT>
3121 struct formatter<nullptr_t, _CharT>
3123 formatter() =
default;
3125 [[__gnu__::__always_inline__]]
3126 constexpr typename basic_format_parse_context<_CharT>::iterator
3127 parse(basic_format_parse_context<_CharT>& __pc)
3128 {
return _M_f.parse(__pc); }
3130 template<
typename _Out>
3131 typename basic_format_context<_Out, _CharT>::iterator
3132 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3133 {
return _M_f.format(
nullptr, __fc); }
3136 __format::__formatter_ptr<_CharT> _M_f;
3140#if __glibcxx_print >= 202403L
3142 inline constexpr bool
3143 enable_nonlocking_formatter_optimization<nullptr_t> =
true;
3146#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3150 struct __formatter_disabled
3152 __formatter_disabled() =
delete;
3153 __formatter_disabled(
const __formatter_disabled&) =
delete;
3154 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3158 struct formatter<char*,
wchar_t>
3159 :
private __formatter_disabled { };
3161 struct formatter<const char*,
wchar_t>
3162 :
private __formatter_disabled { };
3163 template<
size_t _Nm>
3164 struct formatter<char[_Nm], wchar_t>
3165 :
private __formatter_disabled { };
3166 template<
class _Traits,
class _Allocator>
3167 struct formatter<
basic_string<char, _Traits, _Allocator>, wchar_t>
3168 :
private __formatter_disabled { };
3169 template<
class _Traits>
3171 :
private __formatter_disabled { };
3176 template<
typename _Out>
3177 struct format_to_n_result
3180 iter_difference_t<_Out> size;
3183_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3184template<
typename,
typename>
class vector;
3185_GLIBCXX_END_NAMESPACE_CONTAINER
3190 template<
typename _CharT>
3194 using iterator_category = output_iterator_tag;
3195 using value_type = void;
3196 using difference_type = ptrdiff_t;
3197 using pointer = void;
3198 using reference = void;
3200 _Drop_iter() =
default;
3201 _Drop_iter(
const _Drop_iter&) =
default;
3202 _Drop_iter& operator=(
const _Drop_iter&) =
default;
3204 [[__gnu__::__always_inline__]]
3205 constexpr _Drop_iter&
3206 operator=(_CharT __c)
3209 [[__gnu__::__always_inline__]]
3210 constexpr _Drop_iter&
3211 operator=(basic_string_view<_CharT> __s)
3214 [[__gnu__::__always_inline__]]
3215 constexpr _Drop_iter&
3218 [[__gnu__::__always_inline__]]
3219 constexpr _Drop_iter&
3220 operator++() {
return *
this; }
3222 [[__gnu__::__always_inline__]]
3223 constexpr _Drop_iter
3224 operator++(
int) {
return *
this; }
3227 template<
typename _CharT>
3230 _Sink<_CharT>* _M_sink =
nullptr;
3233 using iterator_category = output_iterator_tag;
3234 using value_type = void;
3235 using difference_type = ptrdiff_t;
3236 using pointer = void;
3237 using reference = void;
3239 _Sink_iter() =
default;
3240 _Sink_iter(
const _Sink_iter&) =
default;
3241 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3243 [[__gnu__::__always_inline__]]
3245 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3247 [[__gnu__::__always_inline__]]
3248 constexpr _Sink_iter&
3249 operator=(_CharT __c)
3251 _M_sink->_M_write(__c);
3255 [[__gnu__::__always_inline__]]
3256 constexpr _Sink_iter&
3257 operator=(basic_string_view<_CharT> __s)
3259 _M_sink->_M_write(__s);
3263 [[__gnu__::__always_inline__]]
3264 constexpr _Sink_iter&
3267 [[__gnu__::__always_inline__]]
3268 constexpr _Sink_iter&
3269 operator++() {
return *
this; }
3271 [[__gnu__::__always_inline__]]
3272 constexpr _Sink_iter
3273 operator++(
int) {
return *
this; }
3276 _M_reserve(
size_t __n)
const
3277 {
return _M_sink->_M_reserve(__n); }
3280 _M_discarding()
const
3281 {
return _M_sink->_M_discarding(); }
3287 template<
typename _CharT>
3290 friend class _Sink_iter<_CharT>;
3292 span<_CharT> _M_span;
3293 typename span<_CharT>::iterator _M_next;
3299 virtual void _M_overflow() = 0;
3303 [[__gnu__::__always_inline__]]
3305 _Sink(span<_CharT> __span) noexcept
3306 : _M_span(__span), _M_next(__span.begin())
3310 [[__gnu__::__always_inline__]]
3312 _M_used() const noexcept
3313 {
return _M_span.first(_M_next - _M_span.begin()); }
3316 [[__gnu__::__always_inline__]]
3317 constexpr span<_CharT>
3318 _M_unused() const noexcept
3319 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3322 [[__gnu__::__always_inline__]]
3324 _M_rewind() noexcept
3325 { _M_next = _M_span.begin(); }
3329 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3332 _M_next = __s.begin() + __pos;
3337 _M_write(_CharT __c)
3340 if (_M_next - _M_span.begin() == std::ssize(_M_span)) [[unlikely]]
3345 _M_write(basic_string_view<_CharT> __s)
3347 span __to = _M_unused();
3348 while (__to.size() <= __s.size())
3350 __s.copy(__to.data(), __to.size());
3351 _M_next += __to.size();
3352 __s.remove_prefix(__to.size());
3358 __s.copy(__to.data(), __s.size());
3359 _M_next += __s.size();
3368 explicit operator bool() const noexcept {
return _M_sink; }
3370 _CharT* get() const noexcept {
return _M_sink->_M_next.operator->(); }
3372 void _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3380 virtual _Reservation
3381 _M_reserve(
size_t __n)
3383 if (__n <= _M_unused().size())
3386 if (__n <= _M_span.size())
3389 if (__n <= _M_unused().size())
3403 _M_discarding()
const
3407 _Sink(
const _Sink&) =
delete;
3408 _Sink& operator=(
const _Sink&) =
delete;
3410 [[__gnu__::__always_inline__]]
3411 constexpr _Sink_iter<_CharT>
3413 {
return _Sink_iter<_CharT>(*
this); }
3417 template<
typename _CharT>
3418 class _Fixedbuf_sink final :
public _Sink<_CharT>
3421 _M_overflow()
override
3423 __glibcxx_assert(
false);
3428 [[__gnu__::__always_inline__]]
3430 _Fixedbuf_sink(span<_CharT> __buf)
3431 : _Sink<_CharT>(__buf)
3434 constexpr basic_string_view<_CharT>
3437 auto __s = this->_M_used();
3438 return basic_string_view<_CharT>(__s.data(), __s.size());
3443 template<
typename _CharT>
3444 class _Buf_sink :
public _Sink<_CharT>
3447 _CharT _M_buf[__stackbuf_size<_CharT>];
3449 [[__gnu__::__always_inline__]]
3451 _Buf_sink() noexcept
3452 : _Sink<_CharT>(_M_buf)
3456 using _GLIBCXX_STD_C::vector;
3460 template<
typename _Seq>
3461 class _Seq_sink :
public _Buf_sink<typename _Seq::value_type>
3463 using _CharT =
typename _Seq::value_type;
3469 _M_overflow()
override
3471 auto __s = this->_M_used();
3472 if (__s.empty()) [[unlikely]]
3476 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3478 if constexpr (__is_specialization_of<_Seq, basic_string>)
3479 _M_seq.append(__s.data(), __s.size());
3481 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3487 typename _Sink<_CharT>::_Reservation
3488 _M_reserve(
size_t __n)
override
3497 if constexpr (__is_specialization_of<_Seq, basic_string>
3498 || __is_specialization_of<_Seq, vector>)
3501 if (this->_M_used().size()) [[unlikely]]
3502 _Seq_sink::_M_overflow();
3505 const auto __sz = _M_seq.size();
3506 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3507 _M_seq.__resize_and_overwrite(__sz + __n,
3508 [](
auto,
auto __n2) {
3512 _M_seq.resize(__sz + __n);
3516 this->_M_reset(_M_seq, __sz);
3520 return _Sink<_CharT>::_M_reserve(__n);
3524 _M_bump(
size_t __n)
override
3526 if constexpr (__is_specialization_of<_Seq, basic_string>
3527 || __is_specialization_of<_Seq, vector>)
3529 auto __s = this->_M_used();
3530 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3532 _M_seq.resize(__s.size() + __n);
3534 this->_M_reset(this->_M_buf);
3538 void _M_trim(span<const _CharT> __s)
3539 requires __is_specialization_of<_Seq, basic_string>
3541 _GLIBCXX_DEBUG_ASSERT(__s.data() == this->_M_buf
3542 || __s.data() == _M_seq.data());
3543 if (__s.data() == _M_seq.data())
3544 _M_seq.resize(__s.size());
3546 this->_M_reset(this->_M_buf, __s.size());
3553 [[__gnu__::__always_inline__]]
3554 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3557 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3558 : _M_seq(std::move(__s))
3561 using _Sink<_CharT>::out;
3566 if (this->_M_used().size() != 0)
3567 _Seq_sink::_M_overflow();
3576 auto __s = this->_M_used();
3579 if (__s.size() != 0)
3580 _Seq_sink::_M_overflow();
3586 basic_string_view<_CharT>
3589 auto __span = _M_span();
3590 return basic_string_view<_CharT>(__span.data(), __span.size());
3594 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3596 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3602 template<
typename _CharT,
typename _OutIter>
3603 class _Iter_sink :
public _Buf_sink<_CharT>
3606 iter_difference_t<_OutIter> _M_max;
3609 size_t _M_count = 0;
3612 _M_overflow()
override
3614 auto __s = this->_M_used();
3616 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3617 else if (_M_count <
static_cast<size_t>(_M_max))
3619 auto __max = _M_max - _M_count;
3620 span<_CharT> __first;
3621 if (__max < __s.size())
3622 __first = __s.first(
static_cast<size_t>(__max));
3625 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3628 _M_count += __s.size();
3632 _M_discarding()
const override
3640 [[__gnu__::__always_inline__]]
3642 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3643 : _M_out(std::move(__out)), _M_max(__max)
3646 using _Sink<_CharT>::out;
3648 format_to_n_result<_OutIter>
3651 if (this->_M_used().size() != 0)
3652 _Iter_sink::_M_overflow();
3653 iter_difference_t<_OutIter> __count(_M_count);
3665 template<
typename _CharT>
3666 class _Ptr_sink :
public _Sink<_CharT>
3668 static constexpr size_t _S_no_limit = size_t(-1);
3672 size_t _M_count = 0;
3678 _M_overflow()
override
3680 if (this->_M_unused().size() != 0)
3683 auto __s = this->_M_used();
3685 if (_M_max != _S_no_limit)
3687 _M_count += __s.size();
3691 this->_M_reset(this->_M_buf);
3697 _M_rebuf(__s.data(), __s.size() + 1024, __s.size());
3702 _M_discarding()
const override
3709 typename _Sink<_CharT>::_Reservation
3710 _M_reserve(
size_t __n)
final
3712 auto __avail = this->_M_unused();
3713 if (__n > __avail.size())
3715 if (_M_max != _S_no_limit)
3718 auto __s = this->_M_used();
3719 _M_rebuf(__s.data(), __s.size() + __n, __s.size());
3725 template<
typename _IterDifference>
3727 _S_trim_max(_IterDifference __max)
3731 if constexpr (!is_integral_v<_IterDifference> ||
sizeof(__max) >
sizeof(size_t))
3733 if (_IterDifference((
size_t)-1) < __max)
3735 return size_t(__max);
3738 [[__gnu__::__always_inline__]]
3740 _M_rebuf(_CharT* __ptr,
size_t __total,
size_t __inuse = 0)
3742 std::span<_CharT> __span(__ptr, __total);
3743 this->_M_reset(__span, __inuse);
3748 _Ptr_sink(_CharT* __ptr,
size_t __n = _S_no_limit) noexcept
3749 : _Sink<_CharT>(_M_buf), _M_max(__n)
3753 else if (__n != _S_no_limit)
3754 _M_rebuf(__ptr, __n);
3755#if __has_builtin(__builtin_dynamic_object_size)
3756 else if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3757 _M_rebuf(__ptr, __bytes /
sizeof(_CharT));
3762 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3763 __n = (1024 - __off) /
sizeof(_CharT);
3764 if (__n > 0) [[likely]]
3765 _M_rebuf(__ptr, __n);
3771 template<contiguous_iterator _OutIter>
3773 _Ptr_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1)
3774 : _Ptr_sink(std::
to_address(__out), _S_trim_max(__n))
3777 template<contiguous_iterator _OutIter>
3778 format_to_n_result<_OutIter>
3779 _M_finish(_OutIter __first)
const
3781 auto __s = this->_M_used();
3782 if (__s.data() == _M_buf)
3785 iter_difference_t<_OutIter> __m(_M_max);
3786 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3787 return { __first + __m, __count };
3791 iter_difference_t<_OutIter> __count(__s.size());
3792 return { __first + __count, __count };
3797 template<
typename _CharT,
typename _OutIter>
3798 concept __contiguous_char_iter
3799 = contiguous_iterator<_OutIter>
3800 && same_as<iter_value_t<_OutIter>, _CharT>;
3811 template<
typename _Out,
typename _CharT>
3812 class _Padding_sink :
public _Str_sink<_CharT>
3817 size_t _M_printwidth;
3819 [[__gnu__::__always_inline__]]
3822 {
return _M_printwidth >= _M_maxwidth; }
3824 [[__gnu__::__always_inline__]]
3826 _M_buffering()
const
3828 if (_M_printwidth < _M_padwidth)
3830 if (_M_maxwidth != (
size_t)-1)
3831 return _M_printwidth < _M_maxwidth;
3836 _M_sync_discarding()
3838 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3839 if (_M_out._M_discarding())
3840 _M_maxwidth = _M_printwidth;
3846 span<_CharT> __new = this->_M_used();
3847 basic_string_view<_CharT> __str(__new.data(), __new.size());
3848 _M_out = __format::__write(
std::move(_M_out), __str);
3849 _M_sync_discarding();
3856 auto __str = this->view();
3858 _M_printwidth = __format::__truncate(__str, _M_maxwidth);
3860 this->_M_trim(__str);
3866 if (_M_printwidth >= _M_padwidth)
3868 _M_out = __format::__write(
std::move(_M_out), __str);
3869 _M_sync_discarding();
3874 _Str_sink<_CharT>::_M_overflow();
3877 this->_M_reset(this->_M_buf);
3882 _M_update(
size_t __new)
3884 _M_printwidth += __new;
3886 if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
3887 return _M_force_update();
3892 _M_overflow()
override
3898 else if (!_M_buffering())
3902 else if (_M_update(this->_M_used().size()))
3903 _Str_sink<_CharT>::_M_overflow();
3907 _M_discarding()
const override
3908 {
return _M_ignoring(); }
3910 typename _Sink<_CharT>::_Reservation
3911 _M_reserve(
size_t __n)
override
3916 else if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3917 if (!_M_buffering())
3920 if (!this->_M_used().empty())
3923 if (
auto __reserved = _M_out._M_reserve(__n))
3926 return _Sink<_CharT>::_M_reserve(__n);
3930 _M_bump(
size_t __n)
override
3937 _Sink<_CharT>::_M_bump(__n);
3943 [[__gnu__::__always_inline__]]
3945 _Padding_sink(_Out __out,
size_t __padwidth,
size_t __maxwidth)
3946 : _M_padwidth(__padwidth), _M_maxwidth(__maxwidth),
3947 _M_out(std::move(__out)), _M_printwidth(0)
3948 { _M_sync_discarding(); }
3950 [[__gnu__::__always_inline__]]
3952 _Padding_sink(_Out __out,
size_t __padwidth)
3953 : _Padding_sink(std::move(__out), __padwidth, (size_t)-1)
3957 _M_finish(_Align __align,
char32_t __fill_char)
3960 if (
auto __rem = this->_M_used().size())
3964 else if (!_M_buffering())
3970 if (!_M_buffering() || !_M_force_update())
3972 if (_M_printwidth >= _M_padwidth)
3975 const auto __str = this->view();
3976 if (_M_printwidth >= _M_padwidth)
3977 return __format::__write(
std::move(_M_out), __str);
3979 const size_t __nfill = _M_padwidth - _M_printwidth;
3980 return __format::__write_padded(
std::move(_M_out), __str,
3981 __align, __nfill, __fill_char);
3985 template<
typename _Out,
typename _CharT>
3986 class _Escaping_sink :
public _Buf_sink<_CharT>
3988 using _Esc = _Escapes<_CharT>;
3991 _Term_char _M_term : 2;
3992 unsigned _M_prev_escape : 1;
3993 unsigned _M_out_discards : 1;
3996 _M_sync_discarding()
3998 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3999 _M_out_discards = _M_out._M_discarding();
4005 span<_CharT> __bytes = this->_M_used();
4006 basic_string_view<_CharT> __str(__bytes.data(), __bytes.size());
4009 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4011 bool __prev_escape = _M_prev_escape;
4012 _M_out = __format::__write_escaped_unicode_part(
4013 std::move(_M_out), __str, __prev_escape, _M_term);
4014 _M_prev_escape = __prev_escape;
4016 __rem = __str.size();
4017 if (__rem > 0 && __str.data() != this->_M_buf) [[unlikely]]
4018 ranges::move(__str, this->_M_buf);
4021 _M_out = __format::__write_escaped_ascii(
4024 this->_M_reset(this->_M_buf, __rem);
4025 _M_sync_discarding();
4029 _M_overflow()
override
4031 if (_M_out_discards)
4038 _M_discarding()
const override
4039 {
return _M_out_discards; }
4042 [[__gnu__::__always_inline__]]
4044 _Escaping_sink(_Out __out, _Term_char __term)
4045 : _M_out(std::move(__out)), _M_term(__term),
4046 _M_prev_escape(true), _M_out_discards(false)
4048 _M_out = __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4049 _M_sync_discarding();
4055 if (_M_out_discards)
4058 if (!this->_M_used().empty())
4061 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4062 if (
auto __rem = this->_M_used(); !__rem.empty())
4064 basic_string_view<_CharT> __str(__rem.data(), __rem.size());
4065 _M_out = __format::__write_escape_seqs(
std::move(_M_out), __str);
4068 return __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4072 enum class _Arg_t :
unsigned char {
4073 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
4074 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
4075 _Arg_i128, _Arg_u128, _Arg_float128,
4076 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
4079#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4080 _Arg_ibm128 = _Arg_ldbl,
4081 _Arg_ieee128 = _Arg_float128,
4086 template<
typename _Context>
4089 using _CharT =
typename _Context::char_type;
4093 using _CharT =
typename _Context::char_type;
4094 using _Func = void(*)(basic_format_parse_context<_CharT>&,
4095 _Context&,
const void*);
4098 template<
typename _Tp>
4099 using __maybe_const_t
4100 = __conditional_t<__formattable_with<const _Tp, _Context>,
4103 template<
typename _Tq>
4105 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
4106 _Context& __format_ctx,
const void* __ptr)
4108 using _Td = remove_const_t<_Tq>;
4109 typename _Context::template formatter_type<_Td> __f;
4110 __parse_ctx.advance_to(__f.parse(__parse_ctx));
4111 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
4112 __format_ctx.advance_to(__f.format(__val, __format_ctx));
4115 template<
typename _Tp>
4116 requires (!is_same_v<remove_cv_t<_Tp>, handle>)
4118 handle(_Tp& __val) noexcept
4119 : _M_ptr(__builtin_addressof(__val))
4120 , _M_func(&_S_format<__maybe_const_t<_Tp>>)
4123 friend class basic_format_arg<_Context>;
4126 handle(
const handle&) =
default;
4127 handle& operator=(
const handle&) =
default;
4129 [[__gnu__::__always_inline__]]
4131 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
4132 { _M_func(__pc, __fc, this->_M_ptr); }
4147 unsigned long long _M_ull;
4150#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4151 long double _M_ldbl;
4154 __ieee128 _M_ieee128;
4156#ifdef __SIZEOF_FLOAT128__
4157 __float128 _M_float128;
4159 const _CharT* _M_str;
4160 basic_string_view<_CharT> _M_sv;
4163#ifdef __SIZEOF_INT128__
4165 unsigned __int128 _M_u128;
4167#ifdef __BFLT16_DIG__
4181 [[__gnu__::__always_inline__]]
4182 _Arg_value() : _M_none() { }
4185 template<
typename _Tp>
4186 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
4187 { _S_get<_Tp>() = __val; }
4192 template<
typename _Tp,
typename _Self,
typename... _Value>
4193 [[__gnu__::__always_inline__]]
4195 _S_access(_Self& __u, _Value... __value)
noexcept
4197 static_assert(
sizeof...(_Value) <= 1);
4198 if constexpr (is_same_v<_Tp, bool>)
4199 return (__u._M_bool = ... = __value);
4200 else if constexpr (is_same_v<_Tp, _CharT>)
4201 return (__u._M_c = ... = __value);
4202 else if constexpr (is_same_v<_Tp, int>)
4203 return (__u._M_i = ... = __value);
4204 else if constexpr (is_same_v<_Tp, unsigned>)
4205 return (__u._M_u = ... = __value);
4206 else if constexpr (is_same_v<_Tp, long long>)
4207 return (__u._M_ll = ... = __value);
4208 else if constexpr (is_same_v<_Tp, unsigned long long>)
4209 return (__u._M_ull = ... = __value);
4210 else if constexpr (is_same_v<_Tp, float>)
4211 return (__u._M_flt = ... = __value);
4212 else if constexpr (is_same_v<_Tp, double>)
4213 return (__u._M_dbl = ... = __value);
4214#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4215 else if constexpr (is_same_v<_Tp, long double>)
4216 return (__u._M_ldbl = ... = __value);
4218 else if constexpr (is_same_v<_Tp, __ibm128>)
4219 return (__u._M_ibm128 = ... = __value);
4220 else if constexpr (is_same_v<_Tp, __ieee128>)
4221 return (__u._M_ieee128 = ... = __value);
4223#ifdef __SIZEOF_FLOAT128__
4224 else if constexpr (is_same_v<_Tp, __float128>)
4225 return (__u._M_float128 = ... = __value);
4227 else if constexpr (is_same_v<_Tp, const _CharT*>)
4228 return (__u._M_str = ... = __value);
4229 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4230 return (__u._M_sv = ... = __value);
4231 else if constexpr (is_same_v<_Tp, const void*>)
4232 return (__u._M_ptr = ... = __value);
4233#ifdef __SIZEOF_INT128__
4234 else if constexpr (is_same_v<_Tp, __int128>)
4235 return (__u._M_i128 = ... = __value);
4236 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4237 return (__u._M_u128 = ... = __value);
4239#ifdef __BFLT16_DIG__
4240 else if constexpr (is_same_v<_Tp, __bflt16_t>)
4241 return (__u._M_bf16 = ... = __value);
4244 else if constexpr (is_same_v<_Tp, _Float16>)
4245 return (__u._M_f16 = ... = __value);
4248 else if constexpr (is_same_v<_Tp, _Float32>)
4249 return (__u._M_f32 = ... = __value);
4252 else if constexpr (is_same_v<_Tp, _Float64>)
4253 return (__u._M_f64 = ... = __value);
4255 else if constexpr (is_same_v<_Tp, handle>)
4256 return __u._M_handle;
4260 template<
typename _Tp>
4261 [[__gnu__::__always_inline__]]
4264 {
return _S_access<_Tp>(*
this); }
4266 template<
typename _Tp>
4267 [[__gnu__::__always_inline__]]
4269 _M_get() const noexcept
4270 {
return _S_access<_Tp>(*
this); }
4272 template<
typename _Tp>
4273 [[__gnu__::__always_inline__]]
4275 _M_set(_Tp __v)
noexcept
4278 if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4279 std::construct_at(&_M_sv, __v);
4280 else if constexpr (is_same_v<_Tp, handle>)
4281 std::construct_at(&_M_handle, __v);
4285 _S_access<_Tp>(*
this, __v);
4290 template<
typename _Context,
typename... _Args>
4293 template<
typename _Visitor,
typename _Ctx>
4294 decltype(
auto) __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4296 template<
typename _Ch,
typename _Tp>
4298 __to_arg_t_enum() noexcept;
4302 template<typename _Context>
4303 class basic_format_arg
4305 using _CharT =
typename _Context::char_type;
4308 using handle = __format::_Arg_value<_Context>::handle;
4310 [[__gnu__::__always_inline__]]
4311 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
4313 [[nodiscard,__gnu__::__always_inline__]]
4314 explicit operator bool() const noexcept
4315 {
return _M_type != __format::_Arg_none; }
4317#if __cpp_lib_format >= 202306L
4318 template<
typename _Visitor>
4320 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4323 template<
typename _Res,
typename _Visitor>
4325 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4330 template<
typename _Ctx>
4331 friend class basic_format_args;
4333 template<
typename _Ctx,
typename... _Args>
4334 friend class __format::_Arg_store;
4336 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
4338 __format::_Arg_value<_Context> _M_val;
4339 __format::_Arg_t _M_type;
4344 template<
typename _Tp>
4345 static consteval auto
4348 using _Td = remove_const_t<_Tp>;
4349 if constexpr (is_same_v<_Td, bool>)
4350 return type_identity<bool>();
4351 else if constexpr (is_same_v<_Td, _CharT>)
4352 return type_identity<_CharT>();
4353 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
4354 return type_identity<_CharT>();
4355#ifdef __SIZEOF_INT128__
4356 else if constexpr (is_same_v<_Td, __int128>)
4357 return type_identity<__int128>();
4358 else if constexpr (is_same_v<_Td, unsigned __int128>)
4359 return type_identity<unsigned __int128>();
4361 else if constexpr (__is_signed_integer<_Td>::value)
4363 if constexpr (
sizeof(_Td) <=
sizeof(int))
4364 return type_identity<int>();
4365 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
4366 return type_identity<long long>();
4368 else if constexpr (__is_unsigned_integer<_Td>::value)
4370 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
4371 return type_identity<unsigned>();
4372 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
4373 return type_identity<unsigned long long>();
4375 else if constexpr (is_same_v<_Td, float>)
4376 return type_identity<float>();
4377 else if constexpr (is_same_v<_Td, double>)
4378 return type_identity<double>();
4379#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4380 else if constexpr (is_same_v<_Td, long double>)
4381 return type_identity<long double>();
4383 else if constexpr (is_same_v<_Td, __ibm128>)
4384 return type_identity<__ibm128>();
4385 else if constexpr (is_same_v<_Td, __ieee128>)
4386 return type_identity<__ieee128>();
4388#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4389 else if constexpr (is_same_v<_Td, __float128>)
4390 return type_identity<__float128>();
4392#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4393 else if constexpr (is_same_v<_Td, __format::__bflt16_t>)
4394 return type_identity<__format::__bflt16_t>();
4396#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4397 else if constexpr (is_same_v<_Td, _Float16>)
4398 return type_identity<_Float16>();
4400#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4401 else if constexpr (is_same_v<_Td, _Float32>)
4402 return type_identity<_Float32>();
4404#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4405 else if constexpr (is_same_v<_Td, _Float64>)
4406 return type_identity<_Float64>();
4408 else if constexpr (__is_specialization_of<_Td, basic_string_view>
4409 || __is_specialization_of<_Td, basic_string>)
4411 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
4412 return type_identity<basic_string_view<_CharT>>();
4414 return type_identity<handle>();
4416 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
4417 return type_identity<const _CharT*>();
4418 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
4419 return type_identity<const _CharT*>();
4420 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
4421 return type_identity<const void*>();
4422 else if constexpr (is_same_v<_Td, nullptr_t>)
4423 return type_identity<const void*>();
4425 return type_identity<handle>();
4429 template<
typename _Tp>
4430 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
4433 template<
typename _Tp>
4434 static consteval __format::_Arg_t
4437 using namespace __format;
4438 if constexpr (is_same_v<_Tp, bool>)
4440 else if constexpr (is_same_v<_Tp, _CharT>)
4442 else if constexpr (is_same_v<_Tp, int>)
4444 else if constexpr (is_same_v<_Tp, unsigned>)
4446 else if constexpr (is_same_v<_Tp, long long>)
4448 else if constexpr (is_same_v<_Tp, unsigned long long>)
4450 else if constexpr (is_same_v<_Tp, float>)
4452 else if constexpr (is_same_v<_Tp, double>)
4454#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4455 else if constexpr (is_same_v<_Tp, long double>)
4459 else if constexpr (is_same_v<_Tp, __ibm128>)
4461 else if constexpr (is_same_v<_Tp, __ieee128>)
4462 return _Arg_ieee128;
4464#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4465 else if constexpr (is_same_v<_Tp, __float128>)
4466 return _Arg_float128;
4468#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4469 else if constexpr (is_same_v<_Tp, __format::__bflt16_t>)
4472#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4473 else if constexpr (is_same_v<_Tp, _Float16>)
4476#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4477 else if constexpr (is_same_v<_Tp, _Float32>)
4480#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4481 else if constexpr (is_same_v<_Tp, _Float64>)
4484 else if constexpr (is_same_v<_Tp, const _CharT*>)
4486 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4488 else if constexpr (is_same_v<_Tp, const void*>)
4490#ifdef __SIZEOF_INT128__
4491 else if constexpr (is_same_v<_Tp, __int128>)
4493 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4496 else if constexpr (is_same_v<_Tp, handle>)
4500 template<
typename _Tp>
4502 _M_set(_Tp __v)
noexcept
4504 _M_type = _S_to_enum<_Tp>();
4508 template<
typename _Tp>
4509 requires __format::__formattable_with<_Tp, _Context>
4511 basic_format_arg(_Tp& __v)
noexcept
4513 using _Td = _Normalize<_Tp>;
4514 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4515 _M_set(_Td{__v.data(), __v.size()});
4516 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4517 && is_same_v<_CharT, wchar_t>)
4518 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4520 _M_set(
static_cast<_Td
>(__v));
4523 template<
typename _Ctx,
typename... _Argz>
4525 make_format_args(_Argz&...)
noexcept;
4527 template<
typename _Visitor,
typename _Ctx>
4528 friend decltype(
auto)
4529 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4531 template<
typename _Visitor,
typename _Ctx>
4532 friend decltype(
auto)
4533 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4535 template<
typename _Ch,
typename _Tp>
4536 friend consteval __format::_Arg_t
4537 __format::__to_arg_t_enum() noexcept;
4539 template<typename _Visitor>
4541 _M_visit(_Visitor&& __vis, __format::_Arg_t __type)
4543 using namespace __format;
4560#if __glibcxx_to_chars
4565#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4568#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4578#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4582#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4586#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4590#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4603#ifdef __SIZEOF_INT128__
4610 __builtin_unreachable();
4614 template<
typename _Visitor>
4616 _M_visit_user(_Visitor&& __vis, __format::_Arg_t __type)
4618 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4620 constexpr bool __user_facing = __is_one_of<_Tp,
4621 monostate, bool, _CharT,
4622 int,
unsigned int,
long long int,
unsigned long long int,
4623 float, double,
long double,
4624 const _CharT*, basic_string_view<_CharT>,
4625 const void*, handle>::value;
4626 if constexpr (__user_facing)
4637 template<
typename _Visitor,
typename _Context>
4638 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4639 inline decltype(auto)
4640 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4648 template<
typename _Visitor,
typename _Ctx>
4649 inline decltype(
auto)
4650 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4655 struct _WidthPrecVisitor
4657 template<
typename _Tp>
4659 operator()(_Tp& __arg)
const
4661 if constexpr (is_same_v<_Tp, monostate>)
4662 __format::__invalid_arg_id_in_format_string();
4666 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4670 if constexpr (__is_unsigned_integer<_Tp>::value)
4672 else if constexpr (__is_signed_integer<_Tp>::value)
4676 __throw_format_error(
"format error: argument used for width or "
4677 "precision must be a non-negative integer");
4681#pragma GCC diagnostic push
4682#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4683 template<
typename _Context>
4685 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4686 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4689 template<
int _Bits,
size_t _Nm>
4691 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4693 __UINT64_TYPE__ __packed_types = 0;
4694 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4695 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4696 return __packed_types;
4701 template<
typename _Context>
4702 class basic_format_args
4704 static constexpr int _S_packed_type_bits = 5;
4705 static constexpr int _S_packed_type_mask = 0b11111;
4706 static constexpr int _S_max_packed_args = 12;
4708 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4710 template<
typename... _Args>
4711 using _Store = __format::_Arg_store<_Context, _Args...>;
4713 template<
typename _Ctx,
typename... _Args>
4714 friend class __format::_Arg_store;
4716 using uint64_t = __UINT64_TYPE__;
4717 using _Format_arg = basic_format_arg<_Context>;
4718 using _Format_arg_val = __format::_Arg_value<_Context>;
4724 uint64_t _M_packed_size : 4;
4725 uint64_t _M_unpacked_size : 60;
4728 const _Format_arg_val* _M_values;
4729 const _Format_arg* _M_args;
4733 _M_size() const noexcept
4734 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4736 typename __format::_Arg_t
4737 _M_type(
size_t __i)
const noexcept
4739 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4740 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4743 template<
typename _Ctx,
typename... _Args>
4745 make_format_args(_Args&...)
noexcept;
4748 template<
typename... _Args>
4749 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4751 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4754 template<
typename... _Args>
4755 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4757 [[nodiscard,__gnu__::__always_inline__]]
4758 basic_format_arg<_Context>
4759 get(
size_t __i)
const noexcept
4761 basic_format_arg<_Context> __arg;
4762 if (__i < _M_packed_size)
4764 __arg._M_type = _M_type(__i);
4765 __arg._M_val = _M_values[__i];
4767 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4768 __arg = _M_args[__i];
4775 template<
typename _Context,
typename... _Args>
4776 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4777 -> basic_format_args<_Context>;
4779 template<
typename _Context,
typename... _Args>
4781 make_format_args(_Args&... __fmt_args)
noexcept;
4784 template<
typename _Context,
typename... _Args>
4785 class __format::_Arg_store
4787 friend std::basic_format_args<_Context>;
4789 template<
typename _Ctx,
typename... _Argz>
4791#if _GLIBCXX_INLINE_VERSION
4794 make_format_args(_Argz&...)
noexcept;
4798 static constexpr bool _S_values_only
4799 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4802 = __conditional_t<_S_values_only,
4803 __format::_Arg_value<_Context>,
4804 basic_format_arg<_Context>>;
4806 _Element_t _M_args[
sizeof...(_Args)];
4808 template<
typename _Tp>
4810 _S_make_elt(_Tp& __v)
4812 using _Tq = remove_const_t<_Tp>;
4813 using _CharT =
typename _Context::char_type;
4814 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4815 "std::formatter must be specialized for the type "
4816 "of each format arg");
4817 using __format::__formattable_with;
4818 if constexpr (is_const_v<_Tp>)
4819 if constexpr (!__formattable_with<_Tp, _Context>)
4820 if constexpr (__formattable_with<_Tq, _Context>)
4821 static_assert(__formattable_with<_Tp, _Context>,
4822 "format arg must be non-const because its "
4823 "std::formatter specialization has a "
4824 "non-const reference parameter");
4825 basic_format_arg<_Context> __arg(__v);
4826 if constexpr (_S_values_only)
4827 return __arg._M_val;
4832 template<
typename... _Tp>
4833 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4834 [[__gnu__::__always_inline__]]
4835 _Arg_store(_Tp&... __a) noexcept
4836 : _M_args{_S_make_elt(__a)...}
4840 template<
typename _Context>
4841 class __format::_Arg_store<_Context>
4844 template<
typename _Context>
4845 template<
typename... _Args>
4847 basic_format_args<_Context>::
4848 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4850 if constexpr (
sizeof...(_Args) == 0)
4853 _M_unpacked_size = 0;
4856 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4859 _M_packed_size =
sizeof...(_Args);
4862 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4864 _M_values = __store._M_args;
4871 _M_unpacked_size =
sizeof...(_Args);
4873 _M_args = __store._M_args;
4878 template<
typename _Context = format_context,
typename... _Args>
4879 [[nodiscard,__gnu__::__always_inline__]]
4881 make_format_args(_Args&... __fmt_args)
noexcept
4883 using _Fmt_arg = basic_format_arg<_Context>;
4884 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4885 _Normalize<_Args>...>;
4886 return _Store(__fmt_args...);
4889#ifdef _GLIBCXX_USE_WCHAR_T
4891 template<
typename... _Args>
4892 [[nodiscard,__gnu__::__always_inline__]]
4894 make_wformat_args(_Args&... __args)
noexcept
4895 {
return std::make_format_args<wformat_context>(__args...); }
4901 template<
typename _Out,
typename _CharT,
typename _Context>
4903 __do_vformat_to(_Out, basic_string_view<_CharT>,
4904 const basic_format_args<_Context>&,
4905 const locale* =
nullptr);
4907 template<
typename _CharT>
struct __formatter_chrono;
4925 template<
typename _Out,
typename _CharT>
4926 class basic_format_context
4928 static_assert( output_iterator<_Out, const _CharT&> );
4930 basic_format_args<basic_format_context> _M_args;
4932 __format::_Optional_locale _M_loc;
4934 basic_format_context(basic_format_args<basic_format_context> __args,
4936 : _M_args(__args), _M_out(std::move(__out))
4939 basic_format_context(basic_format_args<basic_format_context> __args,
4940 _Out __out,
const std::locale& __loc)
4941 : _M_args(__args), _M_out(std::move(__out)), _M_loc(__loc)
4947 basic_format_context(
const basic_format_context&) =
delete;
4948 basic_format_context& operator=(
const basic_format_context&) =
delete;
4950 template<
typename _Out2,
typename _CharT2,
typename _Context2>
4952 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
4953 const basic_format_args<_Context2>&,
4956 friend __format::__formatter_chrono<_CharT>;
4959 ~basic_format_context() =
default;
4961 using iterator = _Out;
4962 using char_type = _CharT;
4963 template<
typename _Tp>
4964 using formatter_type = formatter<_Tp, _CharT>;
4967 basic_format_arg<basic_format_context>
4968 arg(
size_t __id)
const noexcept
4969 {
return _M_args.get(__id); }
4972 std::locale locale() {
return _M_loc.value(); }
4975 iterator out() {
return std::move(_M_out); }
4977 void advance_to(iterator __it) { _M_out =
std::move(__it); }
4989 template<
typename _CharT>
4992 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
4994 struct _Parse_context : basic_format_parse_context<_CharT>
4996 using basic_format_parse_context<_CharT>::basic_format_parse_context;
4997 const _Arg_t* _M_types =
nullptr;
5001 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5002 : _M_pc(__str, __nargs)
5005 constexpr iterator begin() const noexcept {
return _M_pc.begin(); }
5006 constexpr iterator end() const noexcept {
return _M_pc.end(); }
5011 basic_string_view<_CharT> __fmt = _M_fmt_str();
5013 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5015 _M_pc.advance_to(begin() + 1);
5016 _M_format_arg(_M_pc.next_arg_id());
5020 size_t __lbr = __fmt.find(
'{');
5021 size_t __rbr = __fmt.find(
'}');
5023 while (__fmt.size())
5025 auto __cmp = __lbr <=> __rbr;
5029 _M_pc.advance_to(end());
5034 if (__lbr + 1 == __fmt.size()
5035 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5036 __format::__unmatched_left_brace_in_format_string();
5037 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5038 iterator __last = begin() + __lbr + int(__is_escape);
5039 _M_on_chars(__last);
5040 _M_pc.advance_to(__last + 1);
5041 __fmt = _M_fmt_str();
5044 if (__rbr != __fmt.npos)
5046 __lbr = __fmt.find(
'{');
5050 _M_on_replacement_field();
5051 __fmt = _M_fmt_str();
5052 __lbr = __fmt.find(
'{');
5053 __rbr = __fmt.find(
'}');
5058 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5059 __format::__unmatched_right_brace_in_format_string();
5060 iterator __last = begin() + __rbr;
5061 _M_on_chars(__last);
5062 _M_pc.advance_to(__last + 1);
5063 __fmt = _M_fmt_str();
5064 if (__lbr != __fmt.npos)
5066 __rbr = __fmt.find(
'}');
5071 constexpr basic_string_view<_CharT>
5072 _M_fmt_str() const noexcept
5073 {
return {begin(), end()}; }
5075 constexpr virtual void _M_on_chars(iterator) { }
5077 constexpr void _M_on_replacement_field()
5079 auto __next = begin();
5083 __id = _M_pc.next_arg_id();
5084 else if (*__next ==
':')
5086 __id = _M_pc.next_arg_id();
5087 _M_pc.advance_to(++__next);
5091 auto [__i, __ptr] = __format::__parse_arg_id(begin(), end());
5092 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5093 __format::__invalid_arg_id_in_format_string();
5094 _M_pc.check_arg_id(__id = __i);
5097 _M_pc.advance_to(++__ptr);
5100 _M_pc.advance_to(__ptr);
5102 _M_format_arg(__id);
5103 if (begin() == end() || *begin() !=
'}')
5104 __format::__unmatched_left_brace_in_format_string();
5105 _M_pc.advance_to(begin() + 1);
5108 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5112 template<
typename _Out,
typename _CharT>
5113 class _Formatting_scanner :
public _Scanner<_CharT>
5116 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5117 basic_string_view<_CharT> __str)
5118 : _Scanner<_CharT>(__str), _M_fc(__fc)
5122 basic_format_context<_Out, _CharT>& _M_fc;
5124 using iterator =
typename _Scanner<_CharT>::iterator;
5127 _M_on_chars(iterator __last)
override
5129 basic_string_view<_CharT> __str(this->begin(), __last);
5130 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5134 _M_format_arg(
size_t __id)
override
5136 using _Context = basic_format_context<_Out, _CharT>;
5137 using handle =
typename basic_format_arg<_Context>::handle;
5139 __format::__visit_format_arg([
this](
auto& __arg) {
5141 using _Formatter =
typename _Context::template formatter_type<_Type>;
5142 if constexpr (is_same_v<_Type, monostate>)
5143 __format::__invalid_arg_id_in_format_string();
5144 else if constexpr (is_same_v<_Type, handle>)
5145 __arg.format(this->_M_pc, this->_M_fc);
5146 else if constexpr (is_default_constructible_v<_Formatter>)
5149 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5150 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5153 static_assert(__format::__formattable_with<_Type, _Context>);
5154 }, _M_fc.arg(__id));
5158 template<
typename _CharT,
typename _Tp>
5160 __to_arg_t_enum() noexcept
5162 using _Context = __format::__format_context<_CharT>;
5163 using _Fmt_arg = basic_format_arg<_Context>;
5164 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5165 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5169 template<
typename _CharT,
typename... _Args>
5170 class _Checking_scanner :
public _Scanner<_CharT>
5173 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5174 "std::formatter must be specialized for each type being formatted");
5178 _Checking_scanner(basic_string_view<_CharT> __str)
5179 : _Scanner<_CharT>(__str, sizeof...(_Args))
5181#if __cpp_lib_format >= 202305L
5182 this->_M_pc._M_types = _M_types.data();
5188 _M_format_arg(
size_t __id)
override
5190 if constexpr (
sizeof...(_Args) != 0)
5192 if (__id <
sizeof...(_Args))
5194 _M_parse_format_spec<_Args...>(__id);
5198 __builtin_unreachable();
5201 template<
typename _Tp,
typename... _OtherArgs>
5203 _M_parse_format_spec(
size_t __id)
5207 formatter<_Tp, _CharT> __f;
5208 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5210 else if constexpr (
sizeof...(_OtherArgs) != 0)
5211 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5213 __builtin_unreachable();
5216#if __cpp_lib_format >= 202305L
5217 array<_Arg_t,
sizeof...(_Args)>
5218 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5222 template<
typename _Out,
typename _CharT,
typename _Context>
5224 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5225 const basic_format_args<_Context>& __args,
5226 const locale* __loc)
5228 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5230 if constexpr (is_same_v<_CharT, char>)
5232 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5234 bool __done =
false;
5235 __format::__visit_format_arg([&](
auto& __arg) {
5236 using _Tp = remove_cvref_t<
decltype(__arg)>;
5237 if constexpr (is_same_v<_Tp, bool>)
5239 size_t __len = 4 + !__arg;
5240 const char* __chars[] = {
"false",
"true" };
5241 if (
auto __res = __out._M_reserve(__len))
5243 __builtin_memcpy(__res.get(), __chars[__arg], __len);
5244 __res._M_bump(__len);
5248 else if constexpr (is_same_v<_Tp, char>)
5250 if (
auto __res = __out._M_reserve(1))
5252 *__res.get() = __arg;
5257 else if constexpr (is_integral_v<_Tp>)
5259 make_unsigned_t<_Tp> __uval;
5260 const bool __neg = __arg < 0;
5262 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5265 const auto __n = __detail::__to_chars_len(__uval);
5266 if (
auto __res = __out._M_reserve(__n + __neg))
5268 auto __ptr = __res.get();
5270 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5272 __res._M_bump(__n + __neg);
5276 else if constexpr (is_convertible_v<_Tp, string_view>)
5278 string_view __sv = __arg;
5279 if (
auto __res = __out._M_reserve(__sv.size()))
5281 __builtin_memcpy(__res.get(), __sv.data(), __sv.size());
5282 __res._M_bump(__sv.size());
5292 auto __ctx = __loc ==
nullptr
5293 ? _Context(__args, __out)
5294 : _Context(__args, __out, *__loc);
5295 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5296 __scanner._M_scan();
5299 else if constexpr (__contiguous_char_iter<_CharT, _Out>)
5301 _Ptr_sink<_CharT> __sink(__out);
5302 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5303 return std::move(__sink)._M_finish(__out).out;
5307 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5308 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5309 return std::move(__sink)._M_finish().out;
5313 template<
typename _Out,
typename _CharT>
5314 format_to_n_result<_Out>
5315 __do_vformat_to_n(_Out __out, iter_difference_t<_Out> __n,
5316 basic_string_view<_CharT> __fmt,
5317 const type_identity_t<
5318 basic_format_args<__format_context<_CharT>>>& __args,
5319 const locale* __loc =
nullptr)
5321 if constexpr (__contiguous_char_iter<_CharT, _Out>)
5323 _Ptr_sink<_CharT> __sink(__out, __n);
5324 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5325 return std::move(__sink)._M_finish(__out);
5329 _Iter_sink<_CharT, _Out> __sink(
std::move(__out), __n);
5330 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5335#pragma GCC diagnostic pop
5340#if __cpp_lib_format >= 202305L
5343 template<
typename _CharT>
5344 template<
typename... _Ts>
5346 basic_format_parse_context<_CharT>::
5347 __check_dynamic_spec(
size_t __id)
noexcept
5349 if (__id >= _M_num_args)
5350 __format::__invalid_arg_id_in_format_string();
5351 if constexpr (
sizeof...(_Ts) != 0)
5353 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
5354 auto __arg =
static_cast<_Parse_ctx*
>(
this)->_M_types[__id];
5355 __format::_Arg_t __types[] = {
5356 __format::__to_arg_t_enum<_CharT, _Ts>()...
5358 for (
auto __t : __types)
5362 __invalid_dynamic_spec(
"arg(id) type does not match");
5367 template<
typename _CharT,
typename... _Args>
5368 template<
typename _Tp>
5369 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5371 basic_format_string<_CharT, _Args...>::
5372 basic_format_string(
const _Tp& __s)
5375 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5377 __scanner._M_scan();
5382 template<
typename _Out>
requires output_iterator<_Out, const char&>
5383 [[__gnu__::__always_inline__]]
5385 vformat_to(_Out __out, string_view __fmt, format_args __args)
5386 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5388#ifdef _GLIBCXX_USE_WCHAR_T
5389 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5390 [[__gnu__::__always_inline__]]
5392 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5393 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5396 template<
typename _Out>
requires output_iterator<_Out, const char&>
5397 [[__gnu__::__always_inline__]]
5399 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5402 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5405#ifdef _GLIBCXX_USE_WCHAR_T
5406 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5407 [[__gnu__::__always_inline__]]
5409 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5410 wformat_args __args)
5412 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5418 vformat(string_view __fmt, format_args __args)
5420 __format::_Str_sink<char> __buf;
5421 std::vformat_to(__buf.out(), __fmt, __args);
5425#ifdef _GLIBCXX_USE_WCHAR_T
5428 vformat(wstring_view __fmt, wformat_args __args)
5430 __format::_Str_sink<wchar_t> __buf;
5431 std::vformat_to(__buf.out(), __fmt, __args);
5438 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5440 __format::_Str_sink<char> __buf;
5441 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5445#ifdef _GLIBCXX_USE_WCHAR_T
5448 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5450 __format::_Str_sink<wchar_t> __buf;
5451 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5456 template<
typename... _Args>
5459 format(format_string<_Args...> __fmt, _Args&&... __args)
5460 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5462#ifdef _GLIBCXX_USE_WCHAR_T
5463 template<
typename... _Args>
5466 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5467 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5470 template<
typename... _Args>
5473 format(
const locale& __loc, format_string<_Args...> __fmt,
5476 return std::vformat(__loc, __fmt.get(),
5477 std::make_format_args(__args...));
5480#ifdef _GLIBCXX_USE_WCHAR_T
5481 template<
typename... _Args>
5484 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5487 return std::vformat(__loc, __fmt.get(),
5488 std::make_wformat_args(__args...));
5492 template<
typename _Out,
typename... _Args>
5493 requires output_iterator<_Out, const char&>
5495 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5497 return std::vformat_to(
std::move(__out), __fmt.get(),
5498 std::make_format_args(__args...));
5501#ifdef _GLIBCXX_USE_WCHAR_T
5502 template<
typename _Out,
typename... _Args>
5503 requires output_iterator<_Out, const wchar_t&>
5505 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5507 return std::vformat_to(
std::move(__out), __fmt.get(),
5508 std::make_wformat_args(__args...));
5512 template<
typename _Out,
typename... _Args>
5513 requires output_iterator<_Out, const char&>
5515 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5518 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5519 std::make_format_args(__args...));
5522#ifdef _GLIBCXX_USE_WCHAR_T
5523 template<
typename _Out,
typename... _Args>
5524 requires output_iterator<_Out, const wchar_t&>
5526 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5529 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5530 std::make_wformat_args(__args...));
5534 template<
typename _Out,
typename... _Args>
5535 requires output_iterator<_Out, const char&>
5536 inline format_to_n_result<_Out>
5537 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5538 format_string<_Args...> __fmt, _Args&&... __args)
5540 return __format::__do_vformat_to_n(
5542 std::make_format_args(__args...));
5545#ifdef _GLIBCXX_USE_WCHAR_T
5546 template<
typename _Out,
typename... _Args>
5547 requires output_iterator<_Out, const wchar_t&>
5548 inline format_to_n_result<_Out>
5549 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5550 wformat_string<_Args...> __fmt, _Args&&... __args)
5552 return __format::__do_vformat_to_n(
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,
const locale& __loc,
5562 format_string<_Args...> __fmt, _Args&&... __args)
5564 return __format::__do_vformat_to_n(
5566 std::make_format_args(__args...), &__loc);
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,
const locale& __loc,
5574 wformat_string<_Args...> __fmt, _Args&&... __args)
5576 return __format::__do_vformat_to_n(
5578 std::make_wformat_args(__args...), &__loc);
5586 template<
typename _CharT>
5587 class _Counting_sink final :
public _Ptr_sink<_CharT>
5590 _Counting_sink() : _Ptr_sink<_CharT>(nullptr, 0) { }
5592 [[__gnu__::__always_inline__]]
5595 {
return this->_M_count + this->_M_used().size(); }
5598 template<
typename _CharT>
5599 class _Counting_sink :
public _Buf_sink<_CharT>
5601 size_t _M_count = 0;
5604 _M_overflow()
override
5606 if (!std::is_constant_evaluated())
5607 _M_count += this->_M_used().size();
5612 _Counting_sink() =
default;
5614 [[__gnu__::__always_inline__]]
5618 _Counting_sink::_M_overflow();
5626 template<
typename... _Args>
5629 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5631 __format::_Counting_sink<char> __buf;
5632 std::vformat_to(__buf.out(), __fmt.get(),
5633 std::make_format_args(__args...));
5634 return __buf.count();
5637#ifdef _GLIBCXX_USE_WCHAR_T
5638 template<
typename... _Args>
5641 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5643 __format::_Counting_sink<wchar_t> __buf;
5644 std::vformat_to(__buf.out(), __fmt.get(),
5645 std::make_wformat_args(__args...));
5646 return __buf.count();
5650 template<
typename... _Args>
5653 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5656 __format::_Counting_sink<char> __buf;
5657 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5658 std::make_format_args(__args...));
5659 return __buf.count();
5662#ifdef _GLIBCXX_USE_WCHAR_T
5663 template<
typename... _Args>
5666 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5669 __format::_Counting_sink<wchar_t> __buf;
5670 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5671 std::make_wformat_args(__args...));
5672 return __buf.count();
5676#if __glibcxx_format_ranges
5678 template<typename _Tp>
5679 consteval range_format
5682 using _Ref = ranges::range_reference_t<_Tp>;
5683 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5684 return range_format::disabled;
5685 else if constexpr (
requires {
typename _Tp::key_type; })
5687 if constexpr (
requires {
typename _Tp::mapped_type; })
5689 using _Up = remove_cvref_t<_Ref>;
5690 if constexpr (__is_pair<_Up>)
5691 return range_format::map;
5692 else if constexpr (__is_specialization_of<_Up, tuple>)
5693 if constexpr (tuple_size_v<_Up> == 2)
5694 return range_format::map;
5696 return range_format::set;
5699 return range_format::sequence;
5704 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5705 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5710 template<
typename _CharT,
typename _Out,
typename _Callback>
5711 typename basic_format_context<_Out, _CharT>::iterator
5712 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5713 const _Spec<_CharT>& __spec,
5716 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
5722 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
5724 const size_t __padwidth = __spec._M_get_width(__fc);
5725 if (__padwidth == 0)
5726 return __call(__fc);
5730 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5731 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5736 { _M_ctx =
nullptr; }
5741 _M_ctx->advance_to(_M_out);
5745 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5746 _Sink_iter<_CharT> _M_out;
5749 _Restore_out __restore(__fc);
5750 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
5751 __fc.advance_to(__sink.out());
5753 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
5754 __restore._M_disarm();
5759 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5760 struct __indexed_formatter_storage
5765 basic_format_parse_context<_CharT> __pc({});
5766 if (_M_formatter.parse(__pc) != __pc.end())
5767 __format::__failed_to_parse_format_spec();
5770 template<
typename _Out>
5772 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5773 basic_format_context<_Out, _CharT>& __fc,
5774 basic_string_view<_CharT> __sep)
const
5776 if constexpr (_Pos != 0)
5777 __fc.advance_to(__format::__write(__fc.out(), __sep));
5778 __fc.advance_to(_M_formatter.format(__elem, __fc));
5781 [[__gnu__::__always_inline__]]
5785 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5786 _M_formatter.set_debug_format();
5790 formatter<_Tp, _CharT> _M_formatter;
5793 template<
typename _CharT,
typename... _Tps>
5794 class __tuple_formatter
5796 using _String_view = basic_string_view<_CharT>;
5797 using _Seps = __format::_Separators<_CharT>;
5801 set_separator(basic_string_view<_CharT> __sep)
noexcept
5805 set_brackets(basic_string_view<_CharT> __open,
5806 basic_string_view<_CharT> __close)
noexcept
5814 constexpr typename basic_format_parse_context<_CharT>::iterator
5815 parse(basic_format_parse_context<_CharT>& __pc)
5817 auto __first = __pc.begin();
5818 const auto __last = __pc.end();
5819 __format::_Spec<_CharT> __spec{};
5821 auto __finished = [&]
5823 if (__first != __last && *__first !=
'}')
5827 _M_felems._M_parse();
5828 _M_felems.set_debug_format();
5835 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5839 __first = __spec._M_parse_width(__first, __last, __pc);
5843 if (*__first ==
'n')
5846 _M_open = _M_close = _String_view();
5848 else if (*__first ==
'm')
5851 if constexpr (
sizeof...(_Tps) == 2)
5853 _M_sep = _Seps::_S_colon();
5854 _M_open = _M_close = _String_view();
5857 __throw_format_error(
"format error: 'm' specifier requires range"
5858 " of pair or tuple of two elements");
5864 __format::__failed_to_parse_format_spec();
5868 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
5869 typename basic_format_context<_Out, _CharT>::iterator
5870 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
5871 basic_format_context<_Out, _CharT>& __fc)
const
5872 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
5874 template<
typename _Out>
5875 typename basic_format_context<_Out, _CharT>::iterator
5876 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
5877 basic_format_context<_Out, _CharT>& __fc)
const
5879 return __format::__format_padded(
5881 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
5883 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
5884 _M_felems._M_format(__elems..., __nfc, _M_sep);
5885 return __format::__write(__nfc.out(), _M_close);
5890 template<
size_t... _Ids>
5891 struct __formatters_storage
5892 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
5894 template<
size_t _Id,
typename _Up>
5895 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
5900 (_Base<_Ids, _Tps>::_M_parse(), ...);
5903 template<
typename _Out>
5905 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
5906 basic_format_context<_Out, _CharT>& __fc,
5907 _String_view __sep)
const
5909 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
5915 (_Base<_Ids, _Tps>::set_debug_format(), ...);
5919 template<
size_t... _Ids>
5921 _S_create_storage(index_sequence<_Ids...>)
5922 -> __formatters_storage<_Ids...>;
5924 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
5926 _Spec<_CharT> _M_spec{};
5927 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
5928 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
5929 _String_view _M_sep = _Seps::_S_comma();
5930 _Formatters _M_felems;
5933 template<
typename _Tp>
5934 concept __is_map_formattable
5935 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
5941 template<__format::__
char _CharT, formattable<_CharT> _Fp,
5942 formattable<_CharT> _Sp>
5943 struct formatter<
pair<_Fp, _Sp>, _CharT>
5944 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
5945 remove_cvref_t<_Sp>>
5948 using __maybe_const_pair
5949 = __conditional_t<formattable<const _Fp, _CharT>
5950 && formattable<const _Sp, _CharT>,
5951 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
5955 template<
typename _Out>
5956 typename basic_format_context<_Out, _CharT>::iterator
5957 format(__maybe_const_pair& __p,
5958 basic_format_context<_Out, _CharT>& __fc)
const
5959 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
5962#if __glibcxx_print >= 202406L
5965 template<
typename _Fp,
typename _Sp>
5966 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
5967 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
5968 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
5971 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
5972 struct formatter<tuple<_Tps...>, _CharT>
5973 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
5976 using __maybe_const_tuple
5977 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
5978 const tuple<_Tps...>, tuple<_Tps...>>;
5982 template<
typename _Out>
5983 typename basic_format_context<_Out, _CharT>::iterator
5984 format(__maybe_const_tuple& __t,
5985 basic_format_context<_Out, _CharT>& __fc)
const
5986 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
5989#if __glibcxx_print >= 202406L
5992 template<
typename... _Tps>
5993 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
5994 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
5998 template<
typename _Tp, __format::__
char _CharT>
5999 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
6000 class range_formatter
6002 using _String_view = basic_string_view<_CharT>;
6003 using _Seps = __format::_Separators<_CharT>;
6007 set_separator(basic_string_view<_CharT> __sep)
noexcept
6011 set_brackets(basic_string_view<_CharT> __open,
6012 basic_string_view<_CharT> __close)
noexcept
6018 constexpr formatter<_Tp, _CharT>&
6019 underlying() noexcept
6022 constexpr const formatter<_Tp, _CharT>&
6023 underlying() const noexcept
6028 constexpr typename basic_format_parse_context<_CharT>::iterator
6029 parse(basic_format_parse_context<_CharT>& __pc)
6031 auto __first = __pc.begin();
6032 const auto __last = __pc.end();
6033 __format::_Spec<_CharT> __spec{};
6034 bool __no_brace =
false;
6036 auto __finished = [&]
6037 {
return __first == __last || *__first ==
'}'; };
6039 auto __finalize = [&]
6045 auto __parse_val = [&](_String_view __nfs = _String_view())
6047 basic_format_parse_context<_CharT> __npc(__nfs);
6048 if (_M_fval.parse(__npc) != __npc.end())
6049 __format::__failed_to_parse_format_spec();
6050 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6051 _M_fval.set_debug_format();
6052 return __finalize();
6056 return __parse_val();
6058 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6060 return __parse_val();
6062 __first = __spec._M_parse_width(__first, __last, __pc);
6064 return __parse_val();
6066 if (*__first ==
'?')
6069 __spec._M_debug =
true;
6070 if (__finished() || *__first !=
's')
6071 __throw_format_error(
"format error: '?' is allowed only in"
6072 " combination with 's'");
6075 if (*__first ==
's')
6078 if constexpr (same_as<_Tp, _CharT>)
6080 __spec._M_type = __format::_Pres_s;
6082 return __finalize();
6083 __throw_format_error(
"format error: element format specifier"
6084 " cannot be provided when 's' specifier is used");
6087 __throw_format_error(
"format error: 's' specifier requires"
6088 " range of character types");
6092 return __parse_val();
6094 if (*__first ==
'n')
6097 _M_open = _M_close = _String_view();
6102 return __parse_val();
6104 if (*__first ==
'm')
6106 _String_view __m(__first, 1);
6108 if constexpr (__format::__is_map_formattable<_Tp>)
6110 _M_sep = _Seps::_S_comma();
6113 _M_open = _Seps::_S_braces().substr(0, 1);
6114 _M_close = _Seps::_S_braces().substr(1, 1);
6117 return __parse_val(__m);
6118 __throw_format_error(
"format error: element format specifier"
6119 " cannot be provided when 'm' specifier is used");
6122 __throw_format_error(
"format error: 'm' specifier requires"
6123 " range of pairs or tuples of two elements");
6127 return __parse_val();
6129 if (*__first ==
':')
6131 __pc.advance_to(++__first);
6132 __first = _M_fval.parse(__pc);
6136 return __finalize();
6138 __format::__failed_to_parse_format_spec();
6143 template<ranges::input_range _Rg,
typename _Out>
6144 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6145 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6146 typename basic_format_context<_Out, _CharT>::iterator
6147 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6149 using _Range = remove_reference_t<_Rg>;
6150 if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6151 return _M_format<const _Range>(__rg, __fc);
6153 return _M_format(__rg, __fc);
6157 template<ranges::input_range _Rg,
typename _Out>
6158 typename basic_format_context<_Out, _CharT>::iterator
6159 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6161 if constexpr (same_as<_Tp, _CharT>)
6162 if (_M_spec._M_type == __format::_Pres_s)
6164 __format::__formatter_str __fstr(_M_spec);
6165 return __fstr._M_format_range(__rg, __fc);
6167 return __format::__format_padded(
6169 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6170 {
return _M_format_elems(__rg, __nfc); });
6174 template<ranges::input_range _Rg,
typename _Out>
6175 typename basic_format_context<_Out, _CharT>::iterator
6176 _M_format_elems(_Rg& __rg,
6177 basic_format_context<_Out, _CharT>& __fc)
const
6179 auto __out = __format::__write(__fc.out(), _M_open);
6181 auto __first = ranges::begin(__rg);
6182 auto const __last = ranges::end(__rg);
6183 if (__first == __last)
6184 return __format::__write(__out, _M_close);
6186 __fc.advance_to(__out);
6187 __out = _M_fval.format(*__first, __fc);
6188 for (++__first; __first != __last; ++__first)
6190 __out = __format::__write(__out, _M_sep);
6191 __fc.advance_to(__out);
6192 __out = _M_fval.format(*__first, __fc);
6195 return __format::__write(__out, _M_close);
6198 __format::_Spec<_CharT> _M_spec{};
6199 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6200 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6201 _String_view _M_sep = _Seps::_S_comma();
6202 formatter<_Tp, _CharT> _M_fval;
6210 template<ranges::input_range _Rg, __format::__
char _CharT>
6211 requires (format_kind<_Rg> != range_format::disabled)
6212 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6213 struct formatter<_Rg, _CharT>
6216 static const bool _S_range_format_is_string =
6217 (format_kind<_Rg> == range_format::string)
6218 || (format_kind<_Rg> == range_format::debug_string);
6219 using _Vt = remove_cvref_t<
6220 ranges::range_reference_t<
6221 __format::__maybe_const_range<_Rg, _CharT>>>;
6223 static consteval bool _S_is_correct()
6225 if constexpr (_S_range_format_is_string)
6226 static_assert(same_as<_Vt, _CharT>);
6230 static_assert(_S_is_correct());
6233 constexpr formatter() noexcept
6235 using _Seps = __format::_Separators<_CharT>;
6236 if constexpr (format_kind<_Rg> == range_format::map)
6238 static_assert(__format::__is_map_formattable<_Vt>);
6239 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6240 _Seps::_S_braces().substr(1, 1));
6241 _M_under.underlying().set_brackets({}, {});
6242 _M_under.underlying().set_separator(_Seps::_S_colon());
6244 else if constexpr (format_kind<_Rg> == range_format::set)
6245 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6246 _Seps::_S_braces().substr(1, 1));
6250 set_separator(basic_string_view<_CharT> __sep)
noexcept
6251 requires (format_kind<_Rg> == range_format::sequence)
6252 { _M_under.set_separator(__sep); }
6255 set_brackets(basic_string_view<_CharT> __open,
6256 basic_string_view<_CharT> __close)
noexcept
6257 requires (format_kind<_Rg> == range_format::sequence)
6258 { _M_under.set_brackets(__open, __close); }
6262 constexpr typename basic_format_parse_context<_CharT>::iterator
6263 parse(basic_format_parse_context<_CharT>& __pc)
6265 auto __res = _M_under.parse(__pc);
6266 if constexpr (format_kind<_Rg> == range_format::debug_string)
6267 _M_under.set_debug_format();
6273 template<
typename _Out>
6274 typename basic_format_context<_Out, _CharT>::iterator
6275 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6276 basic_format_context<_Out, _CharT>& __fc)
const
6278 if constexpr (_S_range_format_is_string)
6279 return _M_under._M_format_range(__rg, __fc);
6281 return _M_under.format(__rg, __fc);
6285 using _Formatter_under
6286 = __conditional_t<_S_range_format_is_string,
6287 __format::__formatter_str<_CharT>,
6288 range_formatter<_Vt, _CharT>>;
6289 _Formatter_under _M_under;
6292#if __glibcxx_print >= 202406L
6293 template<ranges::input_range _Rg>
6294 requires (format_kind<_Rg> != range_format::disabled)
6295 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6299#undef _GLIBCXX_WIDEN
6301_GLIBCXX_END_NAMESPACE_VERSION
6304#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>.
A non-owning reference to a string.
Managing sequences of characters and character-like objects.
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.
A standard container which offers fixed time access to individual elements in any order.