29#ifndef _GLIBCXX_FORMAT
30#define _GLIBCXX_FORMAT 1
33#pragma GCC system_header
38#define __glibcxx_want_format
39#define __glibcxx_want_format_ranges
40#define __glibcxx_want_format_uchar
43#ifdef __cpp_lib_format
65#if !__has_builtin(__builtin_toupper)
69#pragma GCC diagnostic push
70#pragma GCC diagnostic ignored "-Wpedantic"
71#pragma GCC diagnostic ignored "-Wc++23-extensions"
73namespace std _GLIBCXX_VISIBILITY(default)
75_GLIBCXX_BEGIN_NAMESPACE_VERSION
78 template<
typename _CharT,
typename... _Args>
struct basic_format_string;
88 template<
typename _CharT>
90 _Widen(
const char* __narrow,
const wchar_t* __wide)
92 if constexpr (is_same_v<_CharT, wchar_t>)
97#define _GLIBCXX_WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
98#define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
101 template<
typename _CharT>
102 constexpr size_t __stackbuf_size = 32 *
sizeof(
void*) /
sizeof(_CharT);
105 template<
typename _CharT>
class _Sink;
106 template<
typename _CharT>
class _Fixedbuf_sink;
107 template<
typename _Out,
typename _CharT>
class _Padding_sink;
108 template<
typename _Out,
typename _CharT>
class _Escaping_sink;
111 template<
typename _CharT>
115 template<
typename _CharT>
119 template<
typename _CharT>
121 {
using type = _Drop_iter<_CharT>; };
123 template<
typename _CharT>
124 using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
126 template<
typename _CharT>
127 struct _Runtime_format_string
129 [[__gnu__::__always_inline__]]
130 _Runtime_format_string(basic_string_view<_CharT> __s) noexcept
133 _Runtime_format_string(
const _Runtime_format_string&) =
delete;
134 void operator=(
const _Runtime_format_string&) =
delete;
137 basic_string_view<_CharT> _M_str;
139 template<
typename,
typename...>
friend struct std::basic_format_string;
145 using format_context = __format::__format_context<char>;
146#ifdef _GLIBCXX_USE_WCHAR_T
147 using wformat_context = __format::__format_context<wchar_t>;
151 template<
typename _Context>
class basic_format_args;
152 using format_args = basic_format_args<format_context>;
153#ifdef _GLIBCXX_USE_WCHAR_T
154 using wformat_args = basic_format_args<wformat_context>;
159 template<
typename _Context>
160 class basic_format_arg;
166 template<
typename _CharT,
typename... _Args>
167 struct basic_format_string
169 template<
typename _Tp>
170 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
172 basic_format_string(
const _Tp& __s);
174 [[__gnu__::__always_inline__]]
175 basic_format_string(__format::_Runtime_format_string<_CharT> __s) noexcept
179 [[__gnu__::__always_inline__]]
180 constexpr basic_string_view<_CharT>
185 basic_string_view<_CharT> _M_str;
188 template<
typename... _Args>
189 using format_string = basic_format_string<char, type_identity_t<_Args>...>;
191#ifdef _GLIBCXX_USE_WCHAR_T
192 template<
typename... _Args>
194 = basic_format_string<wchar_t, type_identity_t<_Args>...>;
197#if __cpp_lib_format >= 202311L
198 [[__gnu__::__always_inline__]]
199 inline __format::_Runtime_format_string<char>
200 runtime_format(string_view __fmt)
noexcept
203#ifdef _GLIBCXX_USE_WCHAR_T
204 [[__gnu__::__always_inline__]]
205 inline __format::_Runtime_format_string<wchar_t>
206 runtime_format(wstring_view __fmt)
noexcept
214 template<
typename _Tp,
typename _CharT>
217 formatter() =
delete;
218 formatter(
const formatter&) =
delete;
219 formatter& operator=(
const formatter&) =
delete;
226 explicit format_error(
const string& __what) : runtime_error(__what) { }
227 explicit format_error(
const char* __what) : runtime_error(__what) { }
233 __throw_format_error(
const char* __what)
234 { _GLIBCXX_THROW_OR_ABORT(format_error(__what)); }
242 __unmatched_left_brace_in_format_string()
243 { __throw_format_error(
"format error: unmatched '{' in format string"); }
247 __unmatched_right_brace_in_format_string()
248 { __throw_format_error(
"format error: unmatched '}' in format string"); }
252 __conflicting_indexing_in_format_string()
253 { __throw_format_error(
"format error: conflicting indexing style in format string"); }
257 __invalid_arg_id_in_format_string()
258 { __throw_format_error(
"format error: invalid arg-id in format string"); }
262 __failed_to_parse_format_spec()
263 { __throw_format_error(
"format error: failed to parse format-spec"); }
265 template<
typename _CharT>
class _Scanner;
271 template<
typename _CharT>
class basic_format_parse_context;
272 using format_parse_context = basic_format_parse_context<char>;
273#ifdef _GLIBCXX_USE_WCHAR_T
274 using wformat_parse_context = basic_format_parse_context<wchar_t>;
277 template<
typename _CharT>
278 class basic_format_parse_context
281 using char_type = _CharT;
282 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
283 using iterator = const_iterator;
286 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
287 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
290 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
291 void operator=(
const basic_format_parse_context&) =
delete;
293 constexpr const_iterator
begin() const noexcept {
return _M_begin; }
294 constexpr const_iterator
end() const noexcept {
return _M_end; }
297 advance_to(const_iterator __it)
noexcept
303 if (_M_indexing == _Manual)
304 __format::__conflicting_indexing_in_format_string();
309 if (std::is_constant_evaluated())
310 if (_M_next_arg_id == _M_num_args)
311 __format::__invalid_arg_id_in_format_string();
312 return _M_next_arg_id++;
316 check_arg_id(
size_t __id)
318 if (_M_indexing == _Auto)
319 __format::__conflicting_indexing_in_format_string();
320 _M_indexing = _Manual;
322 if (std::is_constant_evaluated())
323 if (__id >= _M_num_args)
324 __format::__invalid_arg_id_in_format_string();
327#if __cpp_lib_format >= 202305L
328 template<
typename... _Ts>
330 check_dynamic_spec(
size_t __id)
noexcept
332 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
333 "template arguments for check_dynamic_spec<Ts...>(id) "
334 "must be unique and must be one of the allowed types");
336 __check_dynamic_spec<_Ts...>(__id);
341 check_dynamic_spec_integral(
size_t __id)
noexcept
344 __check_dynamic_spec<int, unsigned,
long long,
345 unsigned long long>(__id);
350 check_dynamic_spec_string(
size_t __id)
noexcept
353 __check_dynamic_spec<const _CharT*, basic_string_view<_CharT>>(__id);
359 template<
typename _Tp,
typename... _Ts>
360 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
362 template<
typename... _Ts>
364 __valid_types_for_check_dynamic_spec()
368 if constexpr (
sizeof...(_Ts) == 0)
377 = __once<bool, _Ts...>
378 + __once<char_type, _Ts...>
379 + __once<int, _Ts...>
380 + __once<
unsigned int, _Ts...>
381 + __once<
long long int, _Ts...>
382 + __once<
unsigned long long int, _Ts...>
383 + __once<float, _Ts...>
384 + __once<double, _Ts...>
385 + __once<
long double, _Ts...>
386 + __once<
const char_type*, _Ts...>
387 + __once<basic_string_view<char_type>, _Ts...>
388 + __once<
const void*, _Ts...>;
389 return __sum ==
sizeof...(_Ts);
393 template<
typename... _Ts>
395 __check_dynamic_spec(
size_t __id)
noexcept;
398 static void __invalid_dynamic_spec(
const char*);
400 friend __format::_Scanner<_CharT>;
405 basic_format_parse_context(basic_string_view<_CharT> __fmt,
406 size_t __num_args) noexcept
407 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
413 enum _Indexing { _Unknown, _Manual, _Auto };
414 _Indexing _M_indexing = _Unknown;
415 size_t _M_next_arg_id = 0;
416 size_t _M_num_args = 0;
420 template<
typename _Tp,
template<
typename...>
class _Class>
421 constexpr bool __is_specialization_of =
false;
422 template<
template<
typename...>
class _Class,
typename... _Args>
423 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
428 template<
typename _CharT>
429 constexpr pair<unsigned short, const _CharT*>
430 __parse_integer(
const _CharT* __first,
const _CharT* __last)
432 if (__first == __last)
433 __builtin_unreachable();
435 if constexpr (is_same_v<_CharT, char>)
437 const auto __start = __first;
438 unsigned short __val = 0;
440 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
441 && __first != __start) [[likely]]
442 return {__val, __first};
446 constexpr int __n = 32;
448 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
449 __buf[__i] = __first[__i];
450 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
451 if (__ptr) [[likely]]
452 return {__v, __first + (__ptr - __buf)};
457 template<
typename _CharT>
458 constexpr pair<unsigned short, const _CharT*>
459 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
461 if (__first == __last)
462 __builtin_unreachable();
465 return {0, __first + 1};
467 if (
'1' <= *__first && *__first <=
'9')
469 const unsigned short __id = *__first -
'0';
470 const auto __next = __first + 1;
472 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
473 return {__id, __next};
475 return __format::__parse_integer(__first, __last);
480 enum class _Pres_type :
unsigned char {
484 _Pres_c = 2, _Pres_x, _Pres_X, _Pres_d, _Pres_o, _Pres_b, _Pres_B,
486 _Pres_g = 1, _Pres_G, _Pres_a, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F,
488 _Pres_p = _Pres_x, _Pres_P = _Pres_X,
491 using enum _Pres_type;
493 enum class _Sign :
unsigned char {
501 enum _WidthPrec :
unsigned char {
506 using enum _WidthPrec;
508 template<
typename _Context>
510 __int_from_arg(
const basic_format_arg<_Context>& __arg);
512 constexpr bool __is_digit(
char __c)
513 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
515 constexpr bool __is_xdigit(
char __c)
516 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
523 template<
typename _CharT>
524 struct _Spec : _SpecBase
526 unsigned short _M_width;
527 unsigned short _M_prec;
528 char32_t _M_fill =
' ';
532 unsigned _M_localized : 1;
533 unsigned _M_zero_fill : 1;
534 _WidthPrec _M_width_kind : 2;
535 _WidthPrec _M_prec_kind : 2;
536 unsigned _M_debug : 1;
537 _Pres_type _M_type : 4;
538 unsigned _M_reserved : 8;
542 using iterator =
typename basic_string_view<_CharT>::iterator;
544 static constexpr _Align
545 _S_align(_CharT __c)
noexcept
549 case '<':
return _Align_left;
550 case '>':
return _Align_right;
551 case '^':
return _Align_centre;
552 default:
return _Align_default;
558 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
559 {
return _M_parse_fill_and_align(__first, __last,
"{"); }
563 _M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill)
noexcept
565 for (
char __c : __not_fill)
566 if (*__first ==
static_cast<_CharT
>(__c))
569 using namespace __unicode;
570 if constexpr (__literal_encoding_is_unicode<_CharT>())
573 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
576 auto __beg = __uv.begin();
577 char32_t __c = *__beg++;
578 if (__is_scalar_value(__c))
579 if (
auto __next = __beg.base(); __next != __last)
580 if (_Align __align = _S_align(*__next); __align != _Align_default)
588 else if (__last - __first >= 2)
589 if (_Align __align = _S_align(__first[1]); __align != _Align_default)
596 if (_Align __align = _S_align(__first[0]); __align != _Align_default)
605 static constexpr _Sign
606 _S_sign(_CharT __c)
noexcept
610 case '+':
return _Sign_plus;
611 case '-':
return _Sign_minus;
612 case ' ':
return _Sign_space;
613 default:
return _Sign_default;
619 _M_parse_sign(iterator __first, iterator)
noexcept
621 if (_Sign __sign = _S_sign(*__first); __sign != _Sign_default)
631 _M_parse_alternate_form(iterator __first, iterator)
noexcept
643 _M_parse_zero_fill(iterator __first, iterator )
noexcept
654 static constexpr iterator
655 _S_parse_width_or_precision(iterator __first, iterator __last,
656 unsigned short& __val,
bool& __arg_id,
657 basic_format_parse_context<_CharT>& __pc)
659 if (__format::__is_digit(*__first))
661 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
663 __throw_format_error(
"format error: invalid width or precision "
668 else if (*__first ==
'{')
672 if (__first == __last)
673 __format::__unmatched_left_brace_in_format_string();
675 __val = __pc.next_arg_id();
678 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
679 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
680 __format::__invalid_arg_id_in_format_string();
682 __pc.check_arg_id(__v);
685#if __cpp_lib_format >= 202305L
686 __pc.check_dynamic_spec_integral(__val);
695 _M_parse_width(iterator __first, iterator __last,
696 basic_format_parse_context<_CharT>& __pc)
698 bool __arg_id =
false;
700 __throw_format_error(
"format error: width must be non-zero in "
702 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
704 if (__next != __first)
705 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
711 _M_parse_precision(iterator __first, iterator __last,
712 basic_format_parse_context<_CharT>& __pc)
714 if (__first[0] !=
'.')
717 iterator __next = ++__first;
718 bool __arg_id =
false;
719 if (__next != __last)
720 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
722 if (__next == __first)
723 __throw_format_error(
"format error: missing precision after '.' in "
725 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
731 _M_parse_locale(iterator __first, iterator )
noexcept
741 template<
typename _Context>
743 _M_get_width(_Context& __ctx)
const
746 if (_M_width_kind == _WP_value)
748 else if (_M_width_kind == _WP_from_arg)
749 __width = __format::__int_from_arg(__ctx.arg(_M_width));
753 template<
typename _Context>
755 _M_get_precision(_Context& __ctx)
const
758 if (_M_prec_kind == _WP_value)
760 else if (_M_prec_kind == _WP_from_arg)
761 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
766 template<
typename _Int>
768 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
772 else if (__sign == _Sign_plus)
774 else if (__sign == _Sign_space)
782 template<
typename _Out,
typename _CharT>
783 requires output_iterator<_Out, const _CharT&>
785 __write(_Out __out, basic_string_view<_CharT> __str)
787 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
793 for (_CharT __c : __str)
800 template<
typename _Out,
typename _CharT>
802 __write_padded(_Out __out, basic_string_view<_CharT> __str,
803 _Align __align,
size_t __nfill,
char32_t __fill_char)
805 const size_t __buflen = 0x20;
806 _CharT __padding_chars[__buflen];
807 __padding_chars[0] = _CharT();
808 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
810 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
813 while (__n > __padding.size())
815 __o = __format::__write(
std::move(__o), __padding);
816 __n -= __padding.size();
819 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
822 size_t __l, __r, __max;
823 if (__align == _Align_centre)
826 __r = __l + (__nfill & 1);
829 else if (__align == _Align_right)
842 using namespace __unicode;
843 if constexpr (__literal_encoding_is_unicode<_CharT>())
844 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
847 const char32_t __arr[1]{ __fill_char };
848 _Utf_view<_CharT,
const char32_t(&)[1]> __v(__arr);
849 basic_string<_CharT> __padstr(__v.begin(), __v.end());
850 __padding = __padstr;
852 __out = __format::__write(
std::move(__out), __padding);
853 __out = __format::__write(
std::move(__out), __str);
855 __out = __format::__write(
std::move(__out), __padding);
859 if (__max < __buflen)
860 __padding.remove_suffix(__buflen - __max);
864 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
866 __out = __format::__write(
std::move(__out), __str);
874 template<
typename _CharT,
typename _Out>
876 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
877 size_t __estimated_width,
878 basic_format_context<_Out, _CharT>& __fc,
879 const _Spec<_CharT>& __spec,
880 _Align __align = _Align_left)
882 size_t __width = __spec._M_get_width(__fc);
884 if (__width <= __estimated_width)
885 return __format::__write(__fc.out(), __str);
887 const size_t __nfill = __width - __estimated_width;
889 if (__spec._M_align != _Align_default)
890 __align = __spec._M_align;
892 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
896 template<
typename _CharT>
898 __truncate(basic_string_view<_CharT>& __s,
size_t __prec)
900 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
902 if (__prec != (
size_t)-1)
903 return __unicode::__truncate(__s, __prec);
905 return __unicode::__field_width(__s);
909 __s = __s.substr(0, __prec);
914 enum class _Term_char :
unsigned char {
919 using enum _Term_char;
921 template<
typename _CharT>
924 using _Str_view = basic_string_view<_CharT>;
928 {
return _GLIBCXX_WIDEN(
"\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
932 {
return _S_all().substr(0, 3); }
935 _Str_view _S_newline()
936 {
return _S_all().substr(3, 3); }
939 _Str_view _S_return()
940 {
return _S_all().substr(6, 3); }
943 _Str_view _S_bslash()
944 {
return _S_all().substr(9, 3); }
948 {
return _S_all().substr(12, 3); }
952 {
return _S_all().substr(15, 3); }
956 {
return _S_all().substr(18, 2); }
960 {
return _S_all().substr(20, 2); }
963 _Str_view _S_term(_Term_char __term)
970 return _S_quote().substr(0, 1);
972 return _S_apos().substr(0, 1);
974 __builtin_unreachable();
978 template<
typename _CharT>
981 using _Str_view = basic_string_view<_CharT>;
985 {
return _GLIBCXX_WIDEN(
"[]{}(), : "); }
988 _Str_view _S_squares()
989 {
return _S_all().substr(0, 2); }
992 _Str_view _S_braces()
993 {
return _S_all().substr(2, 2); }
996 _Str_view _S_parens()
997 {
return _S_all().substr(4, 2); }
1000 _Str_view _S_comma()
1001 {
return _S_all().substr(6, 2); }
1004 _Str_view _S_colon()
1005 {
return _S_all().substr(8, 2); }
1008 template<
typename _CharT>
1009 constexpr bool __should_escape_ascii(_CharT __c, _Term_char __term)
1011 using _Esc = _Escapes<_CharT>;
1014 case _Esc::_S_tab()[0]:
1015 case _Esc::_S_newline()[0]:
1016 case _Esc::_S_return()[0]:
1017 case _Esc::_S_bslash()[0]:
1019 case _Esc::_S_quote()[0]:
1020 return __term == _Term_quote;
1021 case _Esc::_S_apos()[0]:
1022 return __term == _Term_apos;
1024 return (__c >= 0 && __c < 0x20) || __c == 0x7f;
1029 constexpr bool __should_escape_unicode(
char32_t __c,
bool __prev_esc)
1031 if (__unicode::__should_escape_category(__c))
1035 return __unicode::__grapheme_cluster_break_property(__c)
1036 == __unicode::_Gcb_property::_Gcb_Extend;
1039 using uint_least32_t = __UINT_LEAST32_TYPE__;
1040 template<
typename _Out,
typename _CharT>
1042 __write_escape_seq(_Out __out, uint_least32_t __val,
1043 basic_string_view<_CharT> __prefix)
1045 using _Str_view = basic_string_view<_CharT>;
1046 constexpr size_t __max = 8;
1048 const string_view __narrow(
1050 std::__to_chars_i<uint_least32_t>(__buf, __buf + __max, __val, 16).ptr);
1052 __out = __format::__write(__out, __prefix);
1053 *__out = _Separators<_CharT>::_S_braces()[0];
1055 if constexpr (is_same_v<char, _CharT>)
1056 __out = __format::__write(__out, __narrow);
1057#ifdef _GLIBCXX_USE_WCHAR_T
1060 _CharT __wbuf[__max];
1061 const size_t __n = __narrow.size();
1062 std::__to_wstring_numeric(__narrow.data(), __n, __wbuf);
1063 __out = __format::__write(__out, _Str_view(__wbuf, __n));
1066 *__out = _Separators<_CharT>::_S_braces()[1];
1070 template<
typename _Out,
typename _CharT>
1072 __write_escape_seqs(_Out __out, basic_string_view<_CharT> __units)
1074 using _UChar = make_unsigned_t<_CharT>;
1075 for (_CharT __c : __units)
1076 __out = __format::__write_escape_seq(
1077 __out,
static_cast<_UChar
>(__c), _Escapes<_CharT>::_S_x());
1081 template<
typename _Out,
typename _CharT>
1083 __write_escaped_char(_Out __out, _CharT __c)
1085 using _UChar = make_unsigned_t<_CharT>;
1086 using _Esc = _Escapes<_CharT>;
1089 case _Esc::_S_tab()[0]:
1090 return __format::__write(__out, _Esc::_S_tab().substr(1, 2));
1091 case _Esc::_S_newline()[0]:
1092 return __format::__write(__out, _Esc::_S_newline().substr(1, 2));
1093 case _Esc::_S_return()[0]:
1094 return __format::__write(__out, _Esc::_S_return().substr(1, 2));
1095 case _Esc::_S_bslash()[0]:
1096 return __format::__write(__out, _Esc::_S_bslash().substr(1, 2));
1097 case _Esc::_S_quote()[0]:
1098 return __format::__write(__out, _Esc::_S_quote().substr(1, 2));
1099 case _Esc::_S_apos()[0]:
1100 return __format::__write(__out, _Esc::_S_apos().substr(1, 2));
1102 return __format::__write_escape_seq(
1103 __out,
static_cast<_UChar
>(__c), _Esc::_S_u());
1107 template<
typename _CharT,
typename _Out>
1109 __write_escaped_ascii(_Out __out,
1110 basic_string_view<_CharT> __str,
1113 using _Str_view = basic_string_view<_CharT>;
1114 auto __first = __str.begin();
1115 auto const __last = __str.end();
1116 while (__first != __last)
1118 auto __print = __first;
1120 while (__print != __last
1121 && !__format::__should_escape_ascii(*__print, __term))
1124 if (__print != __first)
1125 __out = __format::__write(__out, _Str_view(__first, __print));
1127 if (__print == __last)
1131 __out = __format::__write_escaped_char(__out, *__first);
1137 template<
typename _CharT,
typename _Out>
1139 __write_escaped_unicode_part(_Out __out, basic_string_view<_CharT>& __str,
1140 bool& __prev_esc, _Term_char __term)
1142 using _Str_view = basic_string_view<_CharT>;
1143 using _Esc = _Escapes<_CharT>;
1145 static constexpr char32_t __replace = U
'\uFFFD';
1146 static constexpr _Str_view __replace_rep = []
1149 if constexpr (is_same_v<char, _CharT>)
1150 return "\xEF\xBF\xBD";
1155 __unicode::_Utf_view<char32_t, _Str_view> __v(
std::move(__str));
1158 auto __first = __v.begin();
1159 auto const __last = __v.end();
1160 while (__first != __last)
1162 bool __esc_ascii =
false;
1163 bool __esc_unicode =
false;
1164 bool __esc_replace =
false;
1165 auto __should_escape = [&](
auto const& __it)
1169 = __format::__should_escape_ascii(*__it.base(), __term);
1170 if (__format::__should_escape_unicode(*__it, __prev_esc))
1171 return __esc_unicode =
true;
1172 if (*__it == __replace)
1174 _Str_view __units(__it.base(), __it._M_units());
1175 return __esc_replace = (__units != __replace_rep);
1180 auto __print = __first;
1181 while (__print != __last && !__should_escape(__print))
1187 if (__print != __first)
1188 __out = __format::__write(__out, _Str_view(__first.base(), __print.base()));
1190 if (__print == __last)
1195 __out = __format::__write_escaped_char(__out, *__first.base());
1196 else if (__esc_unicode)
1197 __out = __format::__write_escape_seq(__out, *__first, _Esc::_S_u());
1199 else if (_Str_view __units(__first.base(), __first._M_units());
1200 __units.end() != __last.base())
1201 __out = __format::__write_escape_seqs(__out, __units);
1215 template<
typename _CharT,
typename _Out>
1217 __write_escaped_unicode(_Out __out, basic_string_view<_CharT> __str,
1220 bool __prev_escape =
true;
1221 __out = __format::__write_escaped_unicode_part(__out, __str,
1222 __prev_escape, __term);
1223 __out = __format::__write_escape_seqs(__out, __str);
1227 template<
typename _CharT,
typename _Out>
1229 __write_escaped(_Out __out, basic_string_view<_CharT> __str, _Term_char __term)
1231 __out = __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1233 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1234 __out = __format::__write_escaped_unicode(__out, __str, __term);
1235 else if constexpr (is_same_v<char, _CharT>
1236 && __unicode::__literal_encoding_is_extended_ascii())
1237 __out = __format::__write_escaped_ascii(__out, __str, __term);
1240 __out = __format::__write_escaped_ascii(__out, __str, __term);
1242 return __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1246 struct _Optional_locale
1248 [[__gnu__::__always_inline__]]
1249 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
1251 _Optional_locale(
const locale& __loc) noexcept
1252 : _M_loc(__loc), _M_hasval(
true)
1255 _Optional_locale(
const _Optional_locale& __l) noexcept
1256 : _M_dummy(), _M_hasval(__l._M_hasval)
1259 std::construct_at(&_M_loc, __l._M_loc);
1263 operator=(
const _Optional_locale& __l)
noexcept
1268 _M_loc = __l._M_loc;
1275 else if (__l._M_hasval)
1277 std::construct_at(&_M_loc, __l._M_loc);
1283 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
1286 operator=(locale&& __loc)
noexcept
1292 std::construct_at(&_M_loc,
std::move(__loc));
1303 std::construct_at(&_M_loc);
1309 bool has_value() const noexcept {
return _M_hasval; }
1312 char _M_dummy =
'\0';
1315 bool _M_hasval =
false;
1318 template<__
char _CharT>
1319 struct __formatter_str
1321 __formatter_str() =
default;
1324 __formatter_str(_Spec<_CharT> __spec) noexcept
1328 constexpr typename basic_format_parse_context<_CharT>::iterator
1329 parse(basic_format_parse_context<_CharT>& __pc)
1331 auto __first = __pc.begin();
1332 const auto __last = __pc.end();
1333 _Spec<_CharT> __spec{};
1335 auto __finalize = [
this, &__spec] {
1339 auto __finished = [&] {
1340 if (__first == __last || *__first ==
'}')
1351 __first = __spec._M_parse_fill_and_align(__first, __last);
1355 __first = __spec._M_parse_width(__first, __last, __pc);
1359 __first = __spec._M_parse_precision(__first, __last, __pc);
1363 if (*__first ==
's')
1365 __spec._M_type = _Pres_s;
1368#if __glibcxx_format_ranges
1369 else if (*__first ==
'?')
1371 __spec._M_debug =
true;
1379 __format::__failed_to_parse_format_spec();
1382 template<
typename _Out>
1384 format(basic_string_view<_CharT> __s,
1385 basic_format_context<_Out, _CharT>& __fc)
const
1387 if (_M_spec._M_debug)
1388 return _M_format_escaped(__s, __fc);
1390 if (_M_spec._M_width_kind == _WP_none
1391 && _M_spec._M_prec_kind == _WP_none)
1392 return __format::__write(__fc.out(), __s);
1394 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1395 const size_t __width = __format::__truncate(__s, __maxwidth);
1396 return __format::__write_padded_as_spec(__s, __width, __fc, _M_spec);
1399 template<
typename _Out>
1401 _M_format_escaped(basic_string_view<_CharT> __s,
1402 basic_format_context<_Out, _CharT>& __fc)
const
1404 const size_t __padwidth = _M_spec._M_get_width(__fc);
1405 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1406 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1408 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1409 const size_t __width = __truncate(__s, __maxwidth);
1411 if (__padwidth <= __width && _M_spec._M_prec_kind == _WP_none)
1412 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1417 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth, __maxwidth);
1418 __format::__write_escaped(__sink.out(), __s, _Term_quote);
1419 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1422#if __glibcxx_format_ranges
1423 template<ranges::input_range _Rg,
typename _Out>
1424 requires same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _CharT>
1426 _M_format_range(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
1428 using _Range = remove_reference_t<_Rg>;
1429 using _String_view = basic_string_view<_CharT>;
1430 if constexpr (!is_lvalue_reference_v<_Rg>)
1431 return _M_format_range<_Range&>(__rg, __fc);
1432 else if constexpr (!is_const_v<_Range>
1433 && __simply_formattable_range<_Range, _CharT>)
1434 return _M_format_range<const _Range&>(__rg, __fc);
1435 else if constexpr (ranges::contiguous_range<_Rg>)
1437 _String_view __str(ranges::data(__rg),
1438 size_t(ranges::distance(__rg)));
1439 return format(__str, __fc);
1443 auto __handle_debug = [
this, &__rg]<
typename _NOut>(_NOut __nout)
1445 if (!_M_spec._M_debug)
1446 return ranges::copy(__rg,
std::move(__nout)).out;
1448 _Escaping_sink<_NOut, _CharT>
1450 ranges::copy(__rg, __sink.out());
1451 return __sink._M_finish();
1454 const size_t __padwidth = _M_spec._M_get_width(__fc);
1455 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1456 return __handle_debug(__fc.out());
1458 _Padding_sink<_Out, _CharT>
1459 __sink(__fc.out(), __padwidth, _M_spec._M_get_precision(__fc));
1460 __handle_debug(__sink.out());
1461 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1466 set_debug_format() noexcept
1467 { _M_spec._M_debug =
true; }
1471 _Spec<_CharT> _M_spec{};
1474 template<__
char _CharT>
1475 struct __formatter_int
1479 static constexpr _Pres_type _AsInteger = _Pres_d;
1480 static constexpr _Pres_type _AsBool = _Pres_s;
1481 static constexpr _Pres_type _AsChar = _Pres_c;
1483 __formatter_int() =
default;
1486 __formatter_int(_Spec<_CharT> __spec) noexcept
1489 if (_M_spec._M_type == _Pres_none)
1490 _M_spec._M_type = _Pres_d;
1493 constexpr typename basic_format_parse_context<_CharT>::iterator
1494 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1496 _Spec<_CharT> __spec{};
1497 __spec._M_type = __type;
1499 const auto __last = __pc.end();
1500 auto __first = __pc.begin();
1502 auto __finalize = [
this, &__spec] {
1506 auto __finished = [&] {
1507 if (__first == __last || *__first ==
'}')
1518 __first = __spec._M_parse_fill_and_align(__first, __last);
1522 __first = __spec._M_parse_sign(__first, __last);
1526 __first = __spec._M_parse_alternate_form(__first, __last);
1530 __first = __spec._M_parse_zero_fill(__first, __last);
1534 __first = __spec._M_parse_width(__first, __last, __pc);
1538 __first = __spec._M_parse_locale(__first, __last);
1545 __spec._M_type = _Pres_b;
1549 __spec._M_type = _Pres_B;
1555 if (__type != _AsBool)
1557 __spec._M_type = _Pres_c;
1562 __spec._M_type = _Pres_d;
1566 __spec._M_type = _Pres_o;
1570 __spec._M_type = _Pres_x;
1574 __spec._M_type = _Pres_X;
1578 if (__type == _AsBool)
1580 __spec._M_type = _Pres_s;
1584#if __glibcxx_format_ranges
1586 if (__type == _AsChar)
1588 __spec._M_debug =
true;
1598 __format::__failed_to_parse_format_spec();
1601 template<
typename _Tp>
1602 constexpr typename basic_format_parse_context<_CharT>::iterator
1603 _M_parse(basic_format_parse_context<_CharT>& __pc)
1605 if constexpr (is_same_v<_Tp, bool>)
1607 auto __end = _M_do_parse(__pc, _AsBool);
1608 if (_M_spec._M_type == _Pres_s)
1609 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1610 || _M_spec._M_zero_fill)
1611 __throw_format_error(
"format error: format-spec contains "
1612 "invalid formatting options for "
1616 else if constexpr (__char<_Tp>)
1618 auto __end = _M_do_parse(__pc, _AsChar);
1619 if (_M_spec._M_type == _Pres_c)
1620 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1621 || _M_spec._M_zero_fill
1623 __throw_format_error(
"format error: format-spec contains "
1624 "invalid formatting options for "
1629 return _M_do_parse(__pc, _AsInteger);
1632 template<
typename _Int,
typename _Out>
1633 typename basic_format_context<_Out, _CharT>::iterator
1634 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1636 if (_M_spec._M_type == _Pres_c)
1637 return _M_format_character(_S_to_character(__i), __fc);
1639 char __buf[
sizeof(_Int) * __CHAR_BIT__ + 3];
1640 to_chars_result __res{};
1642 string_view __base_prefix;
1643 make_unsigned_t<_Int> __u;
1645 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1649 char* __start = __buf + 3;
1650 char*
const __end = __buf +
sizeof(__buf);
1651 char*
const __start_digits = __start;
1653 switch (_M_spec._M_type)
1657 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1658 __res = to_chars(__start, __end, __u, 2);
1662 return _M_format_character(_S_to_character(__i), __fc);
1668 __res = to_chars(__start, __end, __u, 10);
1672 __base_prefix =
"0";
1673 __res = to_chars(__start, __end, __u, 8);
1677 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1678 __res = to_chars(__start, __end, __u, 16);
1679 if (_M_spec._M_type == _Pres_X)
1680 for (
auto __p = __start; __p != __res.ptr; ++__p)
1681#
if __has_builtin(__builtin_toupper)
1682 *__p = __builtin_toupper(*__p);
1688 __builtin_unreachable();
1691 if (_M_spec._M_alt && __base_prefix.size())
1693 __start -= __base_prefix.size();
1694 __builtin_memcpy(__start, __base_prefix.data(),
1695 __base_prefix.size());
1697 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1699 return _M_format_int(string_view(__start, __res.ptr - __start),
1700 __start_digits - __start, __fc);
1703 template<
typename _Out>
1704 typename basic_format_context<_Out, _CharT>::iterator
1705 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1707 if (_M_spec._M_type == _Pres_c)
1708 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1709 if (_M_spec._M_type != _Pres_s)
1710 return format(
static_cast<unsigned char>(__i), __fc);
1712 basic_string<_CharT> __s;
1714 if (_M_spec._M_localized) [[unlikely]]
1717 __s = __i ? __np.truename() : __np.falsename();
1718 __est_width = __s.size();
1722 if constexpr (is_same_v<char, _CharT>)
1723 __s = __i ?
"true" :
"false";
1725 __s = __i ? L
"true" : L
"false";
1726 __est_width = __s.size();
1729 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1733 template<
typename _Out>
1734 typename basic_format_context<_Out, _CharT>::iterator
1735 _M_format_character(_CharT __c,
1736 basic_format_context<_Out, _CharT>& __fc)
const
1738 basic_string_view<_CharT> __in(&__c, 1u);
1739 size_t __width = 1u;
1741 if constexpr (
sizeof(_CharT) > 1u &&
1742 __unicode::__literal_encoding_is_unicode<_CharT>())
1743 __width = __unicode::__field_width(__c);
1745 if (!_M_spec._M_debug)
1746 return __format::__write_padded_as_spec(__in, __width,
1750 if (_M_spec._M_get_width(__fc) <= __width)
1751 return __format::__write_escaped(__fc.out(), __in, _Term_apos);
1754 _Fixedbuf_sink<_CharT> __sink(__buf);
1755 __format::__write_escaped(__sink.out(), __in, _Term_apos);
1757 __in = __sink.view();
1758 if (__in[1] == _Escapes<_CharT>::_S_bslash()[0])
1759 __width = __in.size();
1760 return __format::__write_padded_as_spec(__in, __width,
1764 template<
typename _Int>
1766 _S_to_character(_Int __i)
1769 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1771 if (_Traits::__min <= __i && __i <= _Traits::__max)
1772 return static_cast<_CharT
>(__i);
1774 else if constexpr (is_signed_v<_Int>)
1776 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1777 return static_cast<_CharT
>(__i);
1779 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1780 return static_cast<_CharT
>(__i);
1781 __throw_format_error(
"format error: integer not representable as "
1785 template<
typename _Out>
1786 typename basic_format_context<_Out, _CharT>::iterator
1787 _M_format_int(string_view __narrow_str,
size_t __prefix_len,
1788 basic_format_context<_Out, _CharT>& __fc)
const
1790 size_t __width = _M_spec._M_get_width(__fc);
1792 basic_string_view<_CharT> __str;
1793 if constexpr (is_same_v<char, _CharT>)
1794 __str = __narrow_str;
1795#ifdef _GLIBCXX_USE_WCHAR_T
1798 size_t __n = __narrow_str.size();
1799 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
1800 std::__to_wstring_numeric(__narrow_str.data(), __n, __p);
1805 if (_M_spec._M_localized)
1807 const auto& __l = __fc.locale();
1808 if (__l.name() !=
"C")
1810 auto& __np = use_facet<numpunct<_CharT>>(__l);
1811 string __grp = __np.grouping();
1814 size_t __n = __str.size() - __prefix_len;
1815 auto __p = (_CharT*)__builtin_alloca(2 * __n
1818 auto __s = __str.data();
1819 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1820 __s += __prefix_len;
1821 auto __end = std::__add_grouping(__p + __prefix_len,
1822 __np.thousands_sep(),
1826 __str = {__p, size_t(__end - __p)};
1831 if (__width <= __str.size())
1832 return __format::__write(__fc.out(), __str);
1834 char32_t __fill_char = _M_spec._M_fill;
1835 _Align __align = _M_spec._M_align;
1837 size_t __nfill = __width - __str.size();
1838 auto __out = __fc.out();
1839 if (__align == _Align_default)
1841 __align = _Align_right;
1842 if (_M_spec._M_zero_fill)
1844 __fill_char = _CharT(
'0');
1846 if (__prefix_len != 0)
1848 __out = __format::__write(
std::move(__out),
1849 __str.substr(0, __prefix_len));
1850 __str.remove_prefix(__prefix_len);
1854 __fill_char = _CharT(
' ');
1856 return __format::__write_padded(
std::move(__out), __str,
1857 __align, __nfill, __fill_char);
1860 _Spec<_CharT> _M_spec{};
1863#ifdef __BFLT16_DIG__
1864 using __bflt16_t =
decltype(0.0bf16);
1875#undef _GLIBCXX_FORMAT_F128
1877#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1880 using __flt128_t = __ieee128;
1881# define _GLIBCXX_FORMAT_F128 1
1883#ifdef __LONG_DOUBLE_IEEE128__
1887 to_chars(
char*,
char*, __ibm128)
noexcept
1888 __asm(
"_ZSt8to_charsPcS_e");
1891 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1892 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1895 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1896 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1897#elif __cplusplus == 202002L
1899 to_chars(
char*,
char*, __ieee128)
noexcept
1900 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1903 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1904 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1907 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1908 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1911#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1914 using __flt128_t =
long double;
1915# define _GLIBCXX_FORMAT_F128 2
1917#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1920 using __flt128_t = _Float128;
1921# define _GLIBCXX_FORMAT_F128 3
1923# if __cplusplus == 202002L
1927 to_chars(
char*,
char*, _Float128)
noexcept
1928# if _GLIBCXX_INLINE_VERSION
1929 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1931 __asm(
"_ZSt8to_charsPcS_DF128_");
1935 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1936# if _GLIBCXX_INLINE_VERSION
1937 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1939 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
1943 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
1944# if _GLIBCXX_INLINE_VERSION
1945 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
1947 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
1952 using std::to_chars;
1955 template<
typename _Tp>
1956 concept __formattable_float
1957 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
1958 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
1960 template<__
char _CharT>
1961 struct __formatter_fp
1963 constexpr typename basic_format_parse_context<_CharT>::iterator
1964 parse(basic_format_parse_context<_CharT>& __pc)
1966 _Spec<_CharT> __spec{};
1967 const auto __last = __pc.end();
1968 auto __first = __pc.begin();
1970 auto __finalize = [
this, &__spec] {
1974 auto __finished = [&] {
1975 if (__first == __last || *__first ==
'}')
1986 __first = __spec._M_parse_fill_and_align(__first, __last);
1990 __first = __spec._M_parse_sign(__first, __last);
1994 __first = __spec._M_parse_alternate_form(__first, __last);
1998 __first = __spec._M_parse_zero_fill(__first, __last);
2002 if (__first[0] !=
'.')
2004 __first = __spec._M_parse_width(__first, __last, __pc);
2009 __first = __spec._M_parse_precision(__first, __last, __pc);
2013 __first = __spec._M_parse_locale(__first, __last);
2020 __spec._M_type = _Pres_a;
2024 __spec._M_type = _Pres_A;
2028 __spec._M_type = _Pres_e;
2032 __spec._M_type = _Pres_E;
2036 __spec._M_type = _Pres_f;
2040 __spec._M_type = _Pres_F;
2044 __spec._M_type = _Pres_g;
2048 __spec._M_type = _Pres_G;
2056 __format::__failed_to_parse_format_spec();
2059 template<
typename _Fp,
typename _Out>
2060 typename basic_format_context<_Out, _CharT>::iterator
2061 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2065 to_chars_result __res{};
2068 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2070 __prec = _M_spec._M_get_precision(__fc);
2072 char* __start = __buf + 1;
2073 char* __end = __buf +
sizeof(__buf);
2076 bool __upper =
false;
2077 bool __trailing_zeros =
false;
2080 switch (_M_spec._M_type)
2087 if (_M_spec._M_type != _Pres_A)
2089 __fmt = chars_format::hex;
2097 __fmt = chars_format::scientific;
2104 __fmt = chars_format::fixed;
2111 __trailing_zeros =
true;
2113 __fmt = chars_format::general;
2117 __fmt = chars_format::general;
2120 __builtin_unreachable();
2124 auto __to_chars = [&](
char* __b,
char* __e) {
2126 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2127 else if (__fmt != chars_format{})
2128 return __format::to_chars(__b, __e, __v, __fmt);
2130 return __format::to_chars(__b, __e, __v);
2134 __res = __to_chars(__start, __end);
2136 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2140 size_t __guess = 8 + __prec;
2141 if (__fmt == chars_format::fixed)
2143 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2144 || is_same_v<_Fp, long double>)
2149 if constexpr (is_same_v<_Fp, float>)
2150 __builtin_frexpf(__v, &__exp);
2151 else if constexpr (is_same_v<_Fp, double>)
2152 __builtin_frexp(__v, &__exp);
2153 else if constexpr (is_same_v<_Fp, long double>)
2154 __builtin_frexpl(__v, &__exp);
2156 __guess += 1U + __exp * 4004U / 13301U;
2159 __guess += numeric_limits<_Fp>::max_exponent10;
2161 if (__guess <=
sizeof(__buf)) [[unlikely]]
2162 __guess =
sizeof(__buf) * 2;
2171 auto __overwrite = [&__to_chars, &__res] (
char* __p,
size_t __n)
2173 __res = __to_chars(__p + 1, __p + __n - 1);
2174 return __res.ec == errc{} ? __res.ptr - __p : 0;
2179 __start = __dynbuf.
data() + 1;
2180 __end = __dynbuf.
data() + __dynbuf.
size();
2182 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2188 for (
char* __p = __start; __p != __res.ptr; ++__p)
2192 bool __have_sign =
true;
2194 if (!__builtin_signbit(__v))
2196 if (_M_spec._M_sign == _Sign_plus)
2198 else if (_M_spec._M_sign == _Sign_space)
2201 __have_sign =
false;
2204 string_view __narrow_str(__start, __res.ptr - __start);
2208 if (_M_spec._M_alt && __builtin_isfinite(__v))
2210 string_view __s = __narrow_str;
2214 size_t __d = __s.find(
'.');
2215 if (__d != __s.npos)
2217 __p = __s.find(__expc, __d + 1);
2218 if (__p == __s.npos)
2222 if (__trailing_zeros)
2225 if (__s[__have_sign] !=
'0')
2227 __sigfigs = __p - __have_sign - 1;
2232 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2237 __p = __s.find(__expc);
2238 if (__p == __s.npos)
2241 __sigfigs = __d - __have_sign;
2244 if (__trailing_zeros && __prec != 0)
2249 __z = __prec - __sigfigs;
2252 if (
size_t __extras =
int(__d == __p) + __z)
2254 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2258 __builtin_memmove(__start + __p + __extras,
2262 __start[__p++] =
'.';
2263 __builtin_memset(__start + __p,
'0', __z);
2264 __narrow_str = {__s.data(), __s.size() + __extras};
2268 __dynbuf.
reserve(__s.size() + __extras);
2269 if (__dynbuf.
empty())
2271 __dynbuf = __s.
substr(0, __p);
2275 __dynbuf.
append(__z,
'0');
2276 __dynbuf.
append(__s.substr(__p));
2280 __dynbuf.
insert(__p, __extras,
'0');
2282 __dynbuf[__p] =
'.';
2284 __narrow_str = __dynbuf;
2289 basic_string<_CharT> __wstr;
2290 basic_string_view<_CharT> __str;
2291 if constexpr (is_same_v<_CharT, char>)
2292 __str = __narrow_str;
2293#ifdef _GLIBCXX_USE_WCHAR_T
2296 __wstr = std::__to_wstring_numeric(__narrow_str);
2301 if (_M_spec._M_localized && __builtin_isfinite(__v))
2303 auto __s = _M_localize(__str, __expc, __fc.locale());
2308 size_t __width = _M_spec._M_get_width(__fc);
2310 if (__width <= __str.size())
2311 return __format::__write(__fc.out(), __str);
2313 char32_t __fill_char = _M_spec._M_fill;
2314 _Align __align = _M_spec._M_align;
2316 size_t __nfill = __width - __str.size();
2317 auto __out = __fc.out();
2318 if (__align == _Align_default)
2320 __align = _Align_right;
2321 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2323 __fill_char = _CharT(
'0');
2325 if (!__format::__is_xdigit(__narrow_str[0]))
2327 *__out++ = __str[0];
2328 __str.remove_prefix(1);
2332 __fill_char = _CharT(
' ');
2334 return __format::__write_padded(
std::move(__out), __str,
2335 __align, __nfill, __fill_char);
2339 basic_string<_CharT>
2340 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2341 const locale& __loc)
const
2343 basic_string<_CharT> __lstr;
2345 if (__loc == locale::classic())
2348 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2349 const _CharT __point = __np.decimal_point();
2350 const string __grp = __np.grouping();
2352 _CharT __dot, __exp;
2353 if constexpr (is_same_v<_CharT, char>)
2376 __builtin_unreachable();
2380 if (__grp.empty() && __point == __dot)
2383 size_t __d = __str.find(__dot);
2384 size_t __e = min(__d, __str.find(__exp));
2385 if (__e == __str.npos)
2387 const size_t __r = __str.size() - __e;
2388 auto __overwrite = [&](_CharT* __p, size_t) {
2391 if (
auto __c = __str.front(); __c ==
'-' || __c ==
'+' || __c ==
' ')
2396 auto __end = std::__add_grouping(__p + __off, __np.thousands_sep(),
2397 __grp.data(), __grp.size(),
2398 __str.data() + __off,
2399 __str.data() + __e);
2402 if (__d != __str.npos)
2408 const size_t __rlen = __str.size() - __e;
2410 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2413 return (__end - __p);
2415 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2419 _Spec<_CharT> _M_spec{};
2422 template<__format::__
char _CharT>
2423 struct __formatter_ptr
2425 __formatter_ptr() =
default;
2428 __formatter_ptr(_Spec<_CharT> __spec) noexcept
2430 { _M_set_default(_Pres_p); }
2432 constexpr typename basic_format_parse_context<_CharT>::iterator
2433 parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type = _Pres_p)
2435 __format::_Spec<_CharT> __spec{};
2436 const auto __last = __pc.end();
2437 auto __first = __pc.begin();
2439 auto __finalize = [
this, &__spec, __type] {
2441 _M_set_default(__type);
2444 auto __finished = [&] {
2445 if (__first == __last || *__first ==
'}')
2456 __first = __spec._M_parse_fill_and_align(__first, __last);
2462#if __glibcxx_format >= 202304L
2463 __first = __spec._M_parse_zero_fill(__first, __last);
2468 __first = __spec._M_parse_width(__first, __last, __pc);
2472 if (*__first ==
'p')
2474 __spec._M_type = _Pres_p;
2475 _M_spec._M_alt = !_M_spec._M_alt;
2478#if __glibcxx_format >= 202304L
2479 else if (*__first ==
'P')
2481 __spec._M_type = _Pres_P;
2482 _M_spec._M_alt = !_M_spec._M_alt;
2490 __format::__failed_to_parse_format_spec();
2493 template<
typename _Out>
2494 typename basic_format_context<_Out, _CharT>::iterator
2495 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2497 auto __u =
reinterpret_cast<__UINTPTR_TYPE__
>(__v);
2498 char __buf[2 +
sizeof(__v) * 2];
2499 auto [__ptr, __ec] = std::to_chars(__buf + 2,
std::end(__buf),
2501 int __n = __ptr - __buf;
2504#if __glibcxx_format >= 202304L
2505 if (_M_spec._M_type == __format::_Pres_P)
2508 for (
auto __p = __buf + 2; __p != __ptr; ++__p)
2509#
if __has_builtin(__builtin_toupper)
2510 *__p = __builtin_toupper(*__p);
2517 basic_string_view<_CharT> __str;
2518 if constexpr (is_same_v<_CharT, char>)
2519 __str = string_view(__buf, __n);
2520#ifdef _GLIBCXX_USE_WCHAR_T
2523 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
2524 std::__to_wstring_numeric(__buf, __n, __p);
2525 __str = wstring_view(__p, __n);
2529#if __glibcxx_format >= 202304L
2530 if (_M_spec._M_zero_fill)
2532 size_t __width = _M_spec._M_get_width(__fc);
2533 if (__width <= __str.size())
2534 return __format::__write(__fc.out(), __str);
2536 auto __out = __fc.out();
2538 __out = __format::__write(
std::move(__out), __str.substr(0, 2));
2539 __str.remove_prefix(2);
2540 size_t __nfill = __width - __n;
2541 return __format::__write_padded(
std::move(__out), __str,
2542 __format::_Align_right,
2543 __nfill, _CharT(
'0'));
2547 return __format::__write_padded_as_spec(__str, __n, __fc, _M_spec,
2548 __format::_Align_right);
2552 [[__gnu__::__always_inline__]]
2554 _M_set_default(_Pres_type __type)
2556 if (_M_spec._M_type == _Pres_none && __type != _Pres_none)
2558 _M_spec._M_type = __type;
2559 _M_spec._M_alt = !_M_spec._M_alt;
2563 __format::_Spec<_CharT> _M_spec{};
2570 template<__format::__
char _CharT>
2571 struct formatter<_CharT, _CharT>
2573 formatter() =
default;
2575 constexpr typename basic_format_parse_context<_CharT>::iterator
2576 parse(basic_format_parse_context<_CharT>& __pc)
2578 return _M_f.template _M_parse<_CharT>(__pc);
2581 template<
typename _Out>
2582 typename basic_format_context<_Out, _CharT>::iterator
2583 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2585 if (_M_f._M_spec._M_type == __format::_Pres_c)
2586 return _M_f._M_format_character(__u, __fc);
2588 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2591#if __glibcxx_format_ranges
2593 set_debug_format() noexcept
2594 { _M_f._M_spec._M_debug =
true; }
2598 __format::__formatter_int<_CharT> _M_f;
2601#if __glibcxx_print >= 202403L
2602 template<__format::__
char _CharT>
2603 constexpr bool enable_nonlocking_formatter_optimization<_CharT> =
true;
2606#ifdef _GLIBCXX_USE_WCHAR_T
2609 struct formatter<char, wchar_t>
2611 formatter() =
default;
2613 constexpr typename basic_format_parse_context<wchar_t>::iterator
2614 parse(basic_format_parse_context<wchar_t>& __pc)
2616 return _M_f._M_parse<
char>(__pc);
2619 template<
typename _Out>
2620 typename basic_format_context<_Out, wchar_t>::iterator
2621 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2623 if (_M_f._M_spec._M_type == __format::_Pres_c)
2624 return _M_f._M_format_character(__u, __fc);
2626 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2629#if __glibcxx_format_ranges
2631 set_debug_format() noexcept
2632 { _M_f._M_spec._M_debug =
true; }
2636 __format::__formatter_int<wchar_t> _M_f;
2643 template<__format::__
char _CharT>
2644 struct formatter<_CharT*, _CharT>
2646 formatter() =
default;
2648 [[__gnu__::__always_inline__]]
2649 constexpr typename basic_format_parse_context<_CharT>::iterator
2650 parse(basic_format_parse_context<_CharT>& __pc)
2651 {
return _M_f.parse(__pc); }
2653 template<
typename _Out>
2654 [[__gnu__::__nonnull__]]
2655 typename basic_format_context<_Out, _CharT>::iterator
2656 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2657 {
return _M_f.format(__u, __fc); }
2659#if __glibcxx_format_ranges
2660 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2664 __format::__formatter_str<_CharT> _M_f;
2667#if __glibcxx_print >= 202403L
2668 template<__format::__
char _CharT>
2669 constexpr bool enable_nonlocking_formatter_optimization<_CharT*> =
true;
2672 template<__format::__
char _CharT>
2673 struct formatter<const _CharT*, _CharT>
2675 formatter() =
default;
2677 [[__gnu__::__always_inline__]]
2678 constexpr typename basic_format_parse_context<_CharT>::iterator
2679 parse(basic_format_parse_context<_CharT>& __pc)
2680 {
return _M_f.parse(__pc); }
2682 template<
typename _Out>
2683 [[__gnu__::__nonnull__]]
2684 typename basic_format_context<_Out, _CharT>::iterator
2685 format(
const _CharT* __u,
2686 basic_format_context<_Out, _CharT>& __fc)
const
2687 {
return _M_f.format(__u, __fc); }
2689#if __glibcxx_format_ranges
2690 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2694 __format::__formatter_str<_CharT> _M_f;
2697#if __glibcxx_print >= 202403L
2698 template<__format::__
char _CharT>
2700 enable_nonlocking_formatter_optimization<const _CharT*> =
true;
2703 template<__format::__
char _CharT,
size_t _Nm>
2704 struct formatter<_CharT[_Nm], _CharT>
2706 formatter() =
default;
2708 [[__gnu__::__always_inline__]]
2709 constexpr typename basic_format_parse_context<_CharT>::iterator
2710 parse(basic_format_parse_context<_CharT>& __pc)
2711 {
return _M_f.parse(__pc); }
2713 template<
typename _Out>
2714 typename basic_format_context<_Out, _CharT>::iterator
2715 format(
const _CharT (&__u)[_Nm],
2716 basic_format_context<_Out, _CharT>& __fc)
const
2717 {
return _M_f.format({__u, _Nm}, __fc); }
2719#if __glibcxx_format_ranges
2720 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2724 __format::__formatter_str<_CharT> _M_f;
2727#if __glibcxx_print >= 202403L
2728 template<__format::__
char _CharT,
size_t _Nm>
2729 constexpr bool enable_nonlocking_formatter_optimization<_CharT[_Nm]> =
true;
2732 template<
typename _Traits,
typename _Alloc>
2733 struct formatter<
basic_string<char, _Traits, _Alloc>, char>
2735 formatter() =
default;
2737 [[__gnu__::__always_inline__]]
2738 constexpr typename basic_format_parse_context<char>::iterator
2739 parse(basic_format_parse_context<char>& __pc)
2740 {
return _M_f.parse(__pc); }
2742 template<
typename _Out>
2743 typename basic_format_context<_Out, char>::iterator
2744 format(
const basic_string<char, _Traits, _Alloc>& __u,
2745 basic_format_context<_Out, char>& __fc)
const
2746 {
return _M_f.format(__u, __fc); }
2748#if __glibcxx_format_ranges
2749 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2753 __format::__formatter_str<char> _M_f;
2756#if __glibcxx_print >= 202403L
2757 template<
typename _Tr,
typename _Alloc>
2759 enable_nonlocking_formatter_optimization<basic_string<char, _Tr, _Alloc>>
2763#ifdef _GLIBCXX_USE_WCHAR_T
2764 template<
typename _Traits,
typename _Alloc>
2765 struct formatter<
basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2767 formatter() =
default;
2769 [[__gnu__::__always_inline__]]
2770 constexpr typename basic_format_parse_context<wchar_t>::iterator
2771 parse(basic_format_parse_context<wchar_t>& __pc)
2772 {
return _M_f.parse(__pc); }
2774 template<
typename _Out>
2775 typename basic_format_context<_Out, wchar_t>::iterator
2776 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2777 basic_format_context<_Out, wchar_t>& __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<wchar_t> _M_f;
2788#if __glibcxx_print >= 202403L
2789 template<
typename _Tr,
typename _Alloc>
2791 enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Tr, _Alloc>>
2797 template<
typename _Traits>
2800 formatter() =
default;
2802 [[__gnu__::__always_inline__]]
2803 constexpr typename basic_format_parse_context<char>::iterator
2804 parse(basic_format_parse_context<char>& __pc)
2805 {
return _M_f.parse(__pc); }
2807 template<
typename _Out>
2808 typename basic_format_context<_Out, char>::iterator
2809 format(basic_string_view<char, _Traits> __u,
2810 basic_format_context<_Out, char>& __fc)
const
2811 {
return _M_f.format(__u, __fc); }
2813#if __glibcxx_format_ranges
2814 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2818 __format::__formatter_str<char> _M_f;
2821#if __glibcxx_print >= 202403L
2822 template<
typename _Tr>
2824 enable_nonlocking_formatter_optimization<basic_string_view<char, _Tr>>
2828#ifdef _GLIBCXX_USE_WCHAR_T
2829 template<
typename _Traits>
2832 formatter() =
default;
2834 [[__gnu__::__always_inline__]]
2835 constexpr typename basic_format_parse_context<wchar_t>::iterator
2836 parse(basic_format_parse_context<wchar_t>& __pc)
2837 {
return _M_f.parse(__pc); }
2839 template<
typename _Out>
2840 typename basic_format_context<_Out, wchar_t>::iterator
2841 format(basic_string_view<wchar_t, _Traits> __u,
2842 basic_format_context<_Out, wchar_t>& __fc)
const
2843 {
return _M_f.format(__u, __fc); }
2845#if __glibcxx_format_ranges
2846 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2850 __format::__formatter_str<wchar_t> _M_f;
2853#if __glibcxx_print >= 202403L
2854 template<
typename _Tr>
2856 enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Tr>>
2867 template<
typename _Tp>
2868 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2870#if defined __SIZEOF_INT128__
2871 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2872 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2876 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2877 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2878#ifdef _GLIBCXX_USE_CHAR8_T
2879 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2881 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2882 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2884 template<
typename _Tp>
2885 concept __formattable_integer = __is_formattable_integer<_Tp>;
2890 template<__format::__formattable_
integer _Tp, __format::__
char _CharT>
2891 struct formatter<_Tp, _CharT>
2893 formatter() =
default;
2895 [[__gnu__::__always_inline__]]
2896 constexpr typename basic_format_parse_context<_CharT>::iterator
2897 parse(basic_format_parse_context<_CharT>& __pc)
2899 return _M_f.template _M_parse<_Tp>(__pc);
2902 template<
typename _Out>
2903 typename basic_format_context<_Out, _CharT>::iterator
2904 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2905 {
return _M_f.format(__u, __fc); }
2908 __format::__formatter_int<_CharT> _M_f;
2911#if __glibcxx_print >= 202403L
2912 template<__format::__formattable_
integer _Tp>
2914 enable_nonlocking_formatter_optimization<_Tp> =
true;
2917#if defined __glibcxx_to_chars
2919 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2920 struct formatter<_Tp, _CharT>
2922 formatter() =
default;
2924 [[__gnu__::__always_inline__]]
2925 constexpr typename basic_format_parse_context<_CharT>::iterator
2926 parse(basic_format_parse_context<_CharT>& __pc)
2927 {
return _M_f.parse(__pc); }
2929 template<
typename _Out>
2930 typename basic_format_context<_Out, _CharT>::iterator
2931 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2932 {
return _M_f.format(__u, __fc); }
2935 __format::__formatter_fp<_CharT> _M_f;
2938#if __glibcxx_print >= 202403L
2939 template<__format::__formattable_
float _Tp>
2941 enable_nonlocking_formatter_optimization<_Tp> =
true;
2944#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2946 template<__format::__
char _CharT>
2947 struct formatter<long double, _CharT>
2949 formatter() =
default;
2951 [[__gnu__::__always_inline__]]
2952 constexpr typename basic_format_parse_context<_CharT>::iterator
2953 parse(basic_format_parse_context<_CharT>& __pc)
2954 {
return _M_f.parse(__pc); }
2956 template<
typename _Out>
2957 typename basic_format_context<_Out, _CharT>::iterator
2958 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2959 {
return _M_f.format((
double)__u, __fc); }
2962 __format::__formatter_fp<_CharT> _M_f;
2966#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2968 template<__format::__
char _CharT>
2969 struct formatter<_Float16, _CharT>
2971 formatter() =
default;
2973 [[__gnu__::__always_inline__]]
2974 constexpr typename basic_format_parse_context<_CharT>::iterator
2975 parse(basic_format_parse_context<_CharT>& __pc)
2976 {
return _M_f.parse(__pc); }
2978 template<
typename _Out>
2979 typename basic_format_context<_Out, _CharT>::iterator
2980 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
2981 {
return _M_f.format((
float)__u, __fc); }
2984 __format::__formatter_fp<_CharT> _M_f;
2988#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2990 template<__format::__
char _CharT>
2991 struct formatter<_Float32, _CharT>
2993 formatter() =
default;
2995 [[__gnu__::__always_inline__]]
2996 constexpr typename basic_format_parse_context<_CharT>::iterator
2997 parse(basic_format_parse_context<_CharT>& __pc)
2998 {
return _M_f.parse(__pc); }
3000 template<
typename _Out>
3001 typename basic_format_context<_Out, _CharT>::iterator
3002 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
3003 {
return _M_f.format((
float)__u, __fc); }
3006 __format::__formatter_fp<_CharT> _M_f;
3010#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3012 template<__format::__
char _CharT>
3013 struct formatter<_Float64, _CharT>
3015 formatter() =
default;
3017 [[__gnu__::__always_inline__]]
3018 constexpr typename basic_format_parse_context<_CharT>::iterator
3019 parse(basic_format_parse_context<_CharT>& __pc)
3020 {
return _M_f.parse(__pc); }
3022 template<
typename _Out>
3023 typename basic_format_context<_Out, _CharT>::iterator
3024 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
3025 {
return _M_f.format((
double)__u, __fc); }
3028 __format::__formatter_fp<_CharT> _M_f;
3032#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128
3034 template<__format::__
char _CharT>
3035 struct formatter<_Float128, _CharT>
3037 formatter() =
default;
3039 [[__gnu__::__always_inline__]]
3040 constexpr typename basic_format_parse_context<_CharT>::iterator
3041 parse(basic_format_parse_context<_CharT>& __pc)
3042 {
return _M_f.parse(__pc); }
3044 template<
typename _Out>
3045 typename basic_format_context<_Out, _CharT>::iterator
3046 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3047 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3050 __format::__formatter_fp<_CharT> _M_f;
3054#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128 == 2
3057 template<__format::__
char _CharT>
3058 struct formatter<__float128, _CharT>
3060 formatter() =
default;
3062 [[__gnu__::__always_inline__]]
3063 constexpr typename basic_format_parse_context<_CharT>::iterator
3064 parse(basic_format_parse_context<_CharT>& __pc)
3065 {
return _M_f.parse(__pc); }
3067 template<
typename _Out>
3068 typename basic_format_context<_Out, _CharT>::iterator
3069 format(__float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3070 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3073 __format::__formatter_fp<_CharT> _M_f;
3077#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3079 template<__format::__
char _CharT>
3080 struct formatter<__format::__bflt16_t, _CharT>
3082 formatter() =
default;
3084 [[__gnu__::__always_inline__]]
3085 constexpr typename basic_format_parse_context<_CharT>::iterator
3086 parse(basic_format_parse_context<_CharT>& __pc)
3087 {
return _M_f.parse(__pc); }
3089 template<
typename _Out>
3090 typename basic_format_context<_Out, _CharT>::iterator
3091 format(__gnu_cxx::__bfloat16_t __u,
3092 basic_format_context<_Out, _CharT>& __fc)
const
3093 {
return _M_f.format((
float)__u, __fc); }
3096 __format::__formatter_fp<_CharT> _M_f;
3104 template<__format::__
char _CharT>
3105 struct formatter<const void*, _CharT>
3107 formatter() =
default;
3109 constexpr typename basic_format_parse_context<_CharT>::iterator
3110 parse(basic_format_parse_context<_CharT>& __pc)
3111 {
return _M_f.parse(__pc); }
3113 template<
typename _Out>
3114 typename basic_format_context<_Out, _CharT>::iterator
3115 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3116 {
return _M_f.format(__v, __fc); }
3119 __format::__formatter_ptr<_CharT> _M_f;
3122#if __glibcxx_print >= 202403L
3124 inline constexpr bool
3125 enable_nonlocking_formatter_optimization<const void*> =
true;
3128 template<__format::__
char _CharT>
3129 struct formatter<void*, _CharT>
3131 formatter() =
default;
3133 [[__gnu__::__always_inline__]]
3134 constexpr typename basic_format_parse_context<_CharT>::iterator
3135 parse(basic_format_parse_context<_CharT>& __pc)
3136 {
return _M_f.parse(__pc); }
3138 template<
typename _Out>
3139 typename basic_format_context<_Out, _CharT>::iterator
3140 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3141 {
return _M_f.format(__v, __fc); }
3144 __format::__formatter_ptr<_CharT> _M_f;
3147#if __glibcxx_print >= 202403l
3149 inline constexpr bool
3150 enable_nonlocking_formatter_optimization<void*> =
true;
3153 template<__format::__
char _CharT>
3154 struct formatter<nullptr_t, _CharT>
3156 formatter() =
default;
3158 [[__gnu__::__always_inline__]]
3159 constexpr typename basic_format_parse_context<_CharT>::iterator
3160 parse(basic_format_parse_context<_CharT>& __pc)
3161 {
return _M_f.parse(__pc); }
3163 template<
typename _Out>
3164 typename basic_format_context<_Out, _CharT>::iterator
3165 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3166 {
return _M_f.format(
nullptr, __fc); }
3169 __format::__formatter_ptr<_CharT> _M_f;
3173#if __glibcxx_print >= 202403L
3175 inline constexpr bool
3176 enable_nonlocking_formatter_optimization<nullptr_t> =
true;
3179#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3183 struct __formatter_disabled
3185 __formatter_disabled() =
delete;
3186 __formatter_disabled(
const __formatter_disabled&) =
delete;
3187 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3191 struct formatter<char*,
wchar_t>
3192 :
private __formatter_disabled { };
3194 struct formatter<const char*,
wchar_t>
3195 :
private __formatter_disabled { };
3196 template<
size_t _Nm>
3197 struct formatter<char[_Nm], wchar_t>
3198 :
private __formatter_disabled { };
3199 template<
class _Traits,
class _Allocator>
3200 struct formatter<
basic_string<char, _Traits, _Allocator>, wchar_t>
3201 :
private __formatter_disabled { };
3202 template<
class _Traits>
3204 :
private __formatter_disabled { };
3209 template<
typename _Out>
3210 struct format_to_n_result
3213 iter_difference_t<_Out>
size;
3216_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3217template<
typename,
typename>
class vector;
3218_GLIBCXX_END_NAMESPACE_CONTAINER
3223 template<
typename _CharT>
3227 using iterator_category = output_iterator_tag;
3228 using value_type = void;
3229 using difference_type = ptrdiff_t;
3230 using pointer = void;
3231 using reference = void;
3233 _Drop_iter() =
default;
3234 _Drop_iter(
const _Drop_iter&) =
default;
3235 _Drop_iter& operator=(
const _Drop_iter&) =
default;
3237 [[__gnu__::__always_inline__]]
3238 constexpr _Drop_iter&
3239 operator=(_CharT __c)
3242 [[__gnu__::__always_inline__]]
3243 constexpr _Drop_iter&
3244 operator=(basic_string_view<_CharT> __s)
3247 [[__gnu__::__always_inline__]]
3248 constexpr _Drop_iter&
3251 [[__gnu__::__always_inline__]]
3252 constexpr _Drop_iter&
3253 operator++() {
return *
this; }
3255 [[__gnu__::__always_inline__]]
3256 constexpr _Drop_iter
3257 operator++(
int) {
return *
this; }
3260 template<
typename _CharT>
3263 _Sink<_CharT>* _M_sink =
nullptr;
3266 using iterator_category = output_iterator_tag;
3267 using value_type = void;
3268 using difference_type = ptrdiff_t;
3269 using pointer = void;
3270 using reference = void;
3272 _Sink_iter() =
default;
3273 _Sink_iter(
const _Sink_iter&) =
default;
3274 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3276 [[__gnu__::__always_inline__]]
3278 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3280 [[__gnu__::__always_inline__]]
3281 constexpr _Sink_iter&
3282 operator=(_CharT __c)
3284 _M_sink->_M_write(__c);
3288 [[__gnu__::__always_inline__]]
3289 constexpr _Sink_iter&
3290 operator=(basic_string_view<_CharT> __s)
3292 _M_sink->_M_write(__s);
3296 [[__gnu__::__always_inline__]]
3297 constexpr _Sink_iter&
3300 [[__gnu__::__always_inline__]]
3301 constexpr _Sink_iter&
3302 operator++() {
return *
this; }
3304 [[__gnu__::__always_inline__]]
3305 constexpr _Sink_iter
3306 operator++(
int) {
return *
this; }
3309 _M_reserve(
size_t __n)
const
3310 {
return _M_sink->_M_reserve(__n); }
3313 _M_discarding()
const
3314 {
return _M_sink->_M_discarding(); }
3320 template<
typename _CharT>
3323 friend class _Sink_iter<_CharT>;
3325 span<_CharT> _M_span;
3326 typename span<_CharT>::iterator _M_next;
3332 virtual void _M_overflow() = 0;
3336 [[__gnu__::__always_inline__]]
3338 _Sink(span<_CharT> __span) noexcept
3339 : _M_span(__span), _M_next(__span.begin())
3343 [[__gnu__::__always_inline__]]
3345 _M_used() const noexcept
3346 {
return _M_span.first(_M_next - _M_span.begin()); }
3349 [[__gnu__::__always_inline__]]
3350 constexpr span<_CharT>
3351 _M_unused() const noexcept
3352 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3355 [[__gnu__::__always_inline__]]
3357 _M_rewind() noexcept
3358 { _M_next = _M_span.begin(); }
3362 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3365 _M_next = __s.begin() + __pos;
3370 _M_write(_CharT __c)
3373 if (_M_next - _M_span.begin() == std::ssize(_M_span)) [[unlikely]]
3378 _M_write(basic_string_view<_CharT> __s)
3380 span __to = _M_unused();
3381 while (__to.size() <= __s.size())
3383 __s.copy(__to.data(), __to.size());
3384 _M_next += __to.size();
3385 __s.remove_prefix(__to.size());
3391 __s.copy(__to.data(), __s.size());
3392 _M_next += __s.size();
3401 explicit operator bool() const noexcept {
return _M_sink; }
3403 _CharT* get() const noexcept {
return _M_sink->_M_next.operator->(); }
3405 void _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3413 virtual _Reservation
3414 _M_reserve(
size_t __n)
3416 if (__n <= _M_unused().
size())
3419 if (__n <= _M_span.size())
3422 if (__n <= _M_unused().
size())
3436 _M_discarding()
const
3440 _Sink(
const _Sink&) =
delete;
3441 _Sink& operator=(
const _Sink&) =
delete;
3443 [[__gnu__::__always_inline__]]
3444 constexpr _Sink_iter<_CharT>
3446 {
return _Sink_iter<_CharT>(*
this); }
3450 template<
typename _CharT>
3451 class _Fixedbuf_sink final :
public _Sink<_CharT>
3454 _M_overflow()
override
3456 __glibcxx_assert(
false);
3461 [[__gnu__::__always_inline__]]
3463 _Fixedbuf_sink(span<_CharT> __buf)
3464 : _Sink<_CharT>(__buf)
3467 constexpr basic_string_view<_CharT>
3470 auto __s = this->_M_used();
3471 return basic_string_view<_CharT>(__s.data(), __s.size());
3476 template<
typename _CharT>
3477 class _Buf_sink :
public _Sink<_CharT>
3480 _CharT _M_buf[__stackbuf_size<_CharT>];
3482 [[__gnu__::__always_inline__]]
3484 _Buf_sink() noexcept
3485 : _Sink<_CharT>(_M_buf)
3489 using _GLIBCXX_STD_C::vector;
3493 template<
typename _Seq>
3494 class _Seq_sink :
public _Buf_sink<typename _Seq::value_type>
3496 using _CharT =
typename _Seq::value_type;
3502 _M_overflow()
override
3504 auto __s = this->_M_used();
3505 if (__s.empty()) [[unlikely]]
3509 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3511 if constexpr (__is_specialization_of<_Seq, basic_string>)
3512 _M_seq.append(__s.data(), __s.size());
3514 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3520 typename _Sink<_CharT>::_Reservation
3521 _M_reserve(
size_t __n)
override
3530 if constexpr (__is_specialization_of<_Seq, basic_string>
3531 || __is_specialization_of<_Seq, vector>)
3534 if (this->_M_used().
size()) [[unlikely]]
3535 _Seq_sink::_M_overflow();
3538 const auto __sz = _M_seq.size();
3539 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3540 _M_seq.__resize_and_overwrite(__sz + __n,
3541 [](
auto,
auto __n2) {
3545 _M_seq.resize(__sz + __n);
3549 this->_M_reset(_M_seq, __sz);
3553 return _Sink<_CharT>::_M_reserve(__n);
3557 _M_bump(
size_t __n)
override
3559 if constexpr (__is_specialization_of<_Seq, basic_string>
3560 || __is_specialization_of<_Seq, vector>)
3562 auto __s = this->_M_used();
3563 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3565 _M_seq.resize(__s.size() + __n);
3567 this->_M_reset(this->_M_buf);
3571 void _M_trim(span<const _CharT> __s)
3572 requires __is_specialization_of<_Seq, basic_string>
3574 _GLIBCXX_DEBUG_ASSERT(__s.data() == this->_M_buf
3575 || __s.data() == _M_seq.data());
3576 if (__s.data() == _M_seq.data())
3577 _M_seq.resize(__s.size());
3579 this->_M_reset(this->_M_buf, __s.size());
3586 [[__gnu__::__always_inline__]]
3587 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3590 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3591 : _M_seq(std::
move(__s))
3594 using _Sink<_CharT>::out;
3599 if (this->_M_used().
size() != 0)
3600 _Seq_sink::_M_overflow();
3609 auto __s = this->_M_used();
3612 if (__s.size() != 0)
3613 _Seq_sink::_M_overflow();
3619 basic_string_view<_CharT>
3622 auto __span = _M_span();
3623 return basic_string_view<_CharT>(__span.data(), __span.size());
3627 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3629 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3635 template<
typename _CharT,
typename _OutIter>
3636 class _Iter_sink :
public _Buf_sink<_CharT>
3639 iter_difference_t<_OutIter> _M_max;
3642 size_t _M_count = 0;
3645 _M_overflow()
override
3647 auto __s = this->_M_used();
3649 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3650 else if (_M_count <
static_cast<size_t>(_M_max))
3652 auto __max = _M_max - _M_count;
3653 span<_CharT> __first;
3654 if (__max < __s.size())
3655 __first = __s.first(
static_cast<size_t>(__max));
3658 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3661 _M_count += __s.size();
3665 _M_discarding()
const override
3673 [[__gnu__::__always_inline__]]
3675 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3676 : _M_out(std::
move(__out)), _M_max(__max)
3679 using _Sink<_CharT>::out;
3681 format_to_n_result<_OutIter>
3684 if (this->_M_used().
size() != 0)
3685 _Iter_sink::_M_overflow();
3686 iter_difference_t<_OutIter> __count(_M_count);
3698 template<
typename _CharT, contiguous_iterator _OutIter>
3699 requires same_as<iter_value_t<_OutIter>, _CharT>
3700 class _Iter_sink<_CharT, _OutIter> :
public _Sink<_CharT>
3703 iter_difference_t<_OutIter> _M_max = -1;
3705 size_t _M_count = 0;
3711 _M_overflow()
override
3713 if (this->_M_unused().
size() != 0)
3716 auto __s = this->_M_used();
3720 _M_count += __s.size();
3724 this->_M_reset(this->_M_buf);
3730 this->_M_reset({__s.data(), __s.size() + 1024}, __s.size());
3735 _M_discarding()
const override
3742 typename _Sink<_CharT>::_Reservation
3743 _M_reserve(
size_t __n)
final
3745 auto __avail = this->_M_unused();
3746 if (__n > __avail.size())
3751 auto __s = this->_M_used();
3752 this->_M_reset({__s.data(), __s.size() + __n}, __s.size());
3759 _S_make_span(_CharT* __ptr, iter_difference_t<_OutIter> __n,
3760 span<_CharT> __buf)
noexcept
3767 if constexpr (!is_integral_v<iter_difference_t<_OutIter>>
3768 ||
sizeof(__n) >
sizeof(size_t))
3771 auto __m = iter_difference_t<_OutIter>((
size_t)-1);
3775 return {__ptr, (size_t)__n};
3778#if __has_builtin(__builtin_dynamic_object_size)
3779 if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3780 return {__ptr, __bytes /
sizeof(_CharT)};
3783 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3784 __n = (1024 - __off) /
sizeof(_CharT);
3785 if (__n > 0) [[likely]]
3786 return {__ptr,
static_cast<size_t>(__n)};
3793 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1) noexcept
3794 : _Sink<_CharT>(_S_make_span(std::to_address(__out), __n, _M_buf)),
3795 _M_first(__out), _M_max(__n)
3798 format_to_n_result<_OutIter>
3801 auto __s = this->_M_used();
3802 if (__s.data() == _M_buf)
3805 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3806 return { _M_first + _M_max, __count };
3810 iter_difference_t<_OutIter> __count(__s.size());
3811 return { _M_first + __count, __count };
3825 template<
typename _Out,
typename _CharT>
3826 class _Padding_sink :
public _Str_sink<_CharT>
3831 size_t _M_printwidth;
3833 [[__gnu__::__always_inline__]]
3836 {
return _M_printwidth >= _M_maxwidth; }
3838 [[__gnu__::__always_inline__]]
3840 _M_buffering()
const
3842 if (_M_printwidth < _M_padwidth)
3844 if (_M_maxwidth != (
size_t)-1)
3845 return _M_printwidth < _M_maxwidth;
3850 _M_sync_discarding()
3852 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3853 if (_M_out._M_discarding())
3854 _M_maxwidth = _M_printwidth;
3860 span<_CharT> __new = this->_M_used();
3861 basic_string_view<_CharT> __str(__new.data(), __new.size());
3862 _M_out = __format::__write(
std::move(_M_out), __str);
3863 _M_sync_discarding();
3870 auto __str = this->view();
3872 _M_printwidth = __format::__truncate(__str, _M_maxwidth);
3874 this->_M_trim(__str);
3880 if (_M_printwidth >= _M_padwidth)
3882 _M_out = __format::__write(
std::move(_M_out), __str);
3883 _M_sync_discarding();
3888 _Str_sink<_CharT>::_M_overflow();
3891 this->_M_reset(this->_M_buf);
3896 _M_update(
size_t __new)
3898 _M_printwidth += __new;
3900 if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
3901 return _M_force_update();
3906 _M_overflow()
override
3912 else if (!_M_buffering())
3916 else if (_M_update(this->_M_used().
size()))
3917 _Str_sink<_CharT>::_M_overflow();
3921 _M_discarding()
const override
3922 {
return _M_ignoring(); }
3924 typename _Sink<_CharT>::_Reservation
3925 _M_reserve(
size_t __n)
override
3930 else if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3931 if (!_M_buffering())
3934 if (!this->_M_used().
empty())
3937 if (
auto __reserved = _M_out._M_reserve(__n))
3940 return _Sink<_CharT>::_M_reserve(__n);
3944 _M_bump(
size_t __n)
override
3951 _Sink<_CharT>::_M_bump(__n);
3957 [[__gnu__::__always_inline__]]
3959 _Padding_sink(_Out __out,
size_t __padwidth,
size_t __maxwidth)
3960 : _M_padwidth(__padwidth), _M_maxwidth(__maxwidth),
3961 _M_out(std::
move(__out)), _M_printwidth(0)
3962 { _M_sync_discarding(); }
3964 [[__gnu__::__always_inline__]]
3966 _Padding_sink(_Out __out,
size_t __padwidth)
3967 : _Padding_sink(std::
move(__out), __padwidth, (size_t)-1)
3971 _M_finish(_Align __align,
char32_t __fill_char)
3974 if (
auto __rem = this->_M_used().
size())
3978 else if (!_M_buffering())
3984 if (!_M_buffering() || !_M_force_update())
3986 if (_M_printwidth >= _M_padwidth)
3989 const auto __str = this->view();
3990 if (_M_printwidth >= _M_padwidth)
3991 return __format::__write(
std::move(_M_out), __str);
3993 const size_t __nfill = _M_padwidth - _M_printwidth;
3994 return __format::__write_padded(
std::move(_M_out), __str,
3995 __align, __nfill, __fill_char);
3999 template<
typename _Out,
typename _CharT>
4000 class _Escaping_sink :
public _Buf_sink<_CharT>
4002 using _Esc = _Escapes<_CharT>;
4005 _Term_char _M_term : 2;
4006 unsigned _M_prev_escape : 1;
4007 unsigned _M_out_discards : 1;
4010 _M_sync_discarding()
4012 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4013 _M_out_discards = _M_out._M_discarding();
4019 span<_CharT> __bytes = this->_M_used();
4020 basic_string_view<_CharT> __str(__bytes.data(), __bytes.size());
4023 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4025 bool __prev_escape = _M_prev_escape;
4026 _M_out = __format::__write_escaped_unicode_part(
4027 std::move(_M_out), __str, __prev_escape, _M_term);
4028 _M_prev_escape = __prev_escape;
4030 __rem = __str.size();
4031 if (__rem > 0 && __str.data() != this->_M_buf) [[unlikely]]
4032 ranges::move(__str, this->_M_buf);
4035 _M_out = __format::__write_escaped_ascii(
4038 this->_M_reset(this->_M_buf, __rem);
4039 _M_sync_discarding();
4043 _M_overflow()
override
4045 if (_M_out_discards)
4052 _M_discarding()
const override
4053 {
return _M_out_discards; }
4056 [[__gnu__::__always_inline__]]
4058 _Escaping_sink(_Out __out, _Term_char __term)
4059 : _M_out(std::
move(__out)), _M_term(__term),
4060 _M_prev_escape(true), _M_out_discards(false)
4062 _M_out = __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4063 _M_sync_discarding();
4069 if (_M_out_discards)
4072 if (!this->_M_used().
empty())
4075 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4076 if (
auto __rem = this->_M_used(); !__rem.empty())
4078 basic_string_view<_CharT> __str(__rem.data(), __rem.size());
4079 _M_out = __format::__write_escape_seqs(
std::move(_M_out), __str);
4082 return __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4086 enum class _Arg_t :
unsigned char {
4087 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
4088 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
4089 _Arg_i128, _Arg_u128, _Arg_float128,
4090 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
4093#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4094 _Arg_ibm128 = _Arg_ldbl,
4095 _Arg_ieee128 = _Arg_float128,
4100 template<
typename _Context>
4103 using _CharT =
typename _Context::char_type;
4119 unsigned long long _M_ull;
4122#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4123 long double _M_ldbl;
4126 __ieee128 _M_ieee128;
4128#ifdef __SIZEOF_FLOAT128__
4129 __float128 _M_float128;
4131 const _CharT* _M_str;
4132 basic_string_view<_CharT> _M_sv;
4134 _HandleBase _M_handle;
4135#ifdef __SIZEOF_INT128__
4137 unsigned __int128 _M_u128;
4139#ifdef __BFLT16_DIG__
4153 [[__gnu__::__always_inline__]]
4154 _Arg_value() : _M_none() { }
4157 template<
typename _Tp>
4158 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
4159 { _S_get<_Tp>() = __val; }
4162 template<
typename _Tp,
typename _Self>
4163 [[__gnu__::__always_inline__]]
4165 _S_get(_Self& __u)
noexcept
4167 if constexpr (is_same_v<_Tp, bool>)
4169 else if constexpr (is_same_v<_Tp, _CharT>)
4171 else if constexpr (is_same_v<_Tp, int>)
4173 else if constexpr (is_same_v<_Tp, unsigned>)
4175 else if constexpr (is_same_v<_Tp, long long>)
4177 else if constexpr (is_same_v<_Tp, unsigned long long>)
4179 else if constexpr (is_same_v<_Tp, float>)
4181 else if constexpr (is_same_v<_Tp, double>)
4183#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4184 else if constexpr (is_same_v<_Tp, long double>)
4187 else if constexpr (is_same_v<_Tp, __ibm128>)
4188 return __u._M_ibm128;
4189 else if constexpr (is_same_v<_Tp, __ieee128>)
4190 return __u._M_ieee128;
4192#ifdef __SIZEOF_FLOAT128__
4193 else if constexpr (is_same_v<_Tp, __float128>)
4194 return __u._M_float128;
4196 else if constexpr (is_same_v<_Tp, const _CharT*>)
4198 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4200 else if constexpr (is_same_v<_Tp, const void*>)
4202#ifdef __SIZEOF_INT128__
4203 else if constexpr (is_same_v<_Tp, __int128>)
4205 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4208#ifdef __BFLT16_DIG__
4209 else if constexpr (is_same_v<_Tp, __bflt16_t>)
4213 else if constexpr (is_same_v<_Tp, _Float16>)
4217 else if constexpr (is_same_v<_Tp, _Float32>)
4221 else if constexpr (is_same_v<_Tp, _Float64>)
4224 else if constexpr (derived_from<_Tp, _HandleBase>)
4225 return static_cast<_Tp&
>(__u._M_handle);
4229 template<
typename _Tp>
4230 [[__gnu__::__always_inline__]]
4233 {
return _S_get<_Tp>(*
this); }
4235 template<
typename _Tp>
4236 [[__gnu__::__always_inline__]]
4238 _M_get() const noexcept
4239 {
return _S_get<_Tp>(*
this); }
4241 template<
typename _Tp>
4242 [[__gnu__::__always_inline__]]
4244 _M_set(_Tp __v)
noexcept
4246 if constexpr (derived_from<_Tp, _HandleBase>)
4247 std::construct_at(&_M_handle, __v);
4249 _S_get<_Tp>(*
this) = __v;
4254 template<
typename _Context,
typename... _Args>
4257 template<
typename _Visitor,
typename _Ctx>
4258 decltype(
auto) __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4260 template<
typename _Ch,
typename _Tp>
4262 __to_arg_t_enum() noexcept;
4266 template<typename _Context>
4267 class basic_format_arg
4269 using _CharT =
typename _Context::char_type;
4271 template<
typename _Tp>
4272 static constexpr bool __formattable
4273 = __format::__formattable_with<_Tp, _Context>;
4276 class handle :
public __format::_Arg_value<_Context>::_HandleBase
4278 using _Base =
typename __format::_Arg_value<_Context>::_HandleBase;
4281 template<
typename _Tp>
4282 using __maybe_const_t
4283 = __conditional_t<__formattable<const _Tp>,
const _Tp, _Tp>;
4285 template<
typename _Tq>
4287 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
4288 _Context& __format_ctx,
const void* __ptr)
4290 using _Td = remove_const_t<_Tq>;
4291 typename _Context::template formatter_type<_Td> __f;
4292 __parse_ctx.advance_to(__f.parse(__parse_ctx));
4293 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
4294 __format_ctx.advance_to(__f.format(__val, __format_ctx));
4297 template<
typename _Tp>
4299 handle(_Tp& __val)
noexcept
4301 this->_M_ptr = __builtin_addressof(__val);
4302 auto __func = _S_format<__maybe_const_t<_Tp>>;
4303 this->_M_func =
reinterpret_cast<void(*)()
>(__func);
4306 friend class basic_format_arg<_Context>;
4309 handle(
const handle&) =
default;
4310 handle& operator=(
const handle&) =
default;
4312 [[__gnu__::__always_inline__]]
4314 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
4316 using _Func = void(*)(basic_format_parse_context<_CharT>&,
4317 _Context&,
const void*);
4318 auto __f =
reinterpret_cast<_Func
>(this->_M_func);
4319 __f(__pc, __fc, this->_M_ptr);
4323 [[__gnu__::__always_inline__]]
4324 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
4326 [[nodiscard,__gnu__::__always_inline__]]
4327 explicit operator bool() const noexcept
4328 {
return _M_type != __format::_Arg_none; }
4330#if __cpp_lib_format >= 202306L
4331 template<
typename _Visitor>
4333 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4336 template<
typename _Res,
typename _Visitor>
4338 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4343 template<
typename _Ctx>
4344 friend class basic_format_args;
4346 template<
typename _Ctx,
typename... _Args>
4347 friend class __format::_Arg_store;
4349 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
4351 __format::_Arg_value<_Context> _M_val;
4352 __format::_Arg_t _M_type;
4357 template<
typename _Tp>
4358 static consteval auto
4361 using _Td = remove_const_t<_Tp>;
4362 if constexpr (is_same_v<_Td, bool>)
4363 return type_identity<bool>();
4364 else if constexpr (is_same_v<_Td, _CharT>)
4365 return type_identity<_CharT>();
4366 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
4367 return type_identity<_CharT>();
4368#ifdef __SIZEOF_INT128__
4369 else if constexpr (is_same_v<_Td, __int128>)
4370 return type_identity<__int128>();
4371 else if constexpr (is_same_v<_Td, unsigned __int128>)
4372 return type_identity<unsigned __int128>();
4374 else if constexpr (__is_signed_integer<_Td>::value)
4376 if constexpr (
sizeof(_Td) <=
sizeof(int))
4377 return type_identity<int>();
4378 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
4379 return type_identity<long long>();
4381 else if constexpr (__is_unsigned_integer<_Td>::value)
4383 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
4384 return type_identity<unsigned>();
4385 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
4386 return type_identity<unsigned long long>();
4388 else if constexpr (is_same_v<_Td, float>)
4389 return type_identity<float>();
4390 else if constexpr (is_same_v<_Td, double>)
4391 return type_identity<double>();
4392#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4393 else if constexpr (is_same_v<_Td, long double>)
4394 return type_identity<long double>();
4396 else if constexpr (is_same_v<_Td, __ibm128>)
4397 return type_identity<__ibm128>();
4398 else if constexpr (is_same_v<_Td, __ieee128>)
4399 return type_identity<__ieee128>();
4401#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4402 else if constexpr (is_same_v<_Td, __float128>)
4403 return type_identity<__float128>();
4405#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4406 else if constexpr (is_same_v<_Td, __format::__bflt16_t>)
4407 return type_identity<__format::__bflt16_t>();
4409#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4410 else if constexpr (is_same_v<_Td, _Float16>)
4411 return type_identity<_Float16>();
4413#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4414 else if constexpr (is_same_v<_Td, _Float32>)
4415 return type_identity<_Float32>();
4417#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4418 else if constexpr (is_same_v<_Td, _Float64>)
4419 return type_identity<_Float64>();
4421 else if constexpr (__is_specialization_of<_Td, basic_string_view>
4422 || __is_specialization_of<_Td, basic_string>)
4424 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
4425 return type_identity<basic_string_view<_CharT>>();
4427 return type_identity<handle>();
4429 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
4430 return type_identity<const _CharT*>();
4431 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
4432 return type_identity<const _CharT*>();
4433 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
4434 return type_identity<const void*>();
4435 else if constexpr (is_same_v<_Td, nullptr_t>)
4436 return type_identity<const void*>();
4438 return type_identity<handle>();
4442 template<
typename _Tp>
4443 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
4446 template<
typename _Tp>
4447 static consteval __format::_Arg_t
4450 using namespace __format;
4451 if constexpr (is_same_v<_Tp, bool>)
4453 else if constexpr (is_same_v<_Tp, _CharT>)
4455 else if constexpr (is_same_v<_Tp, int>)
4457 else if constexpr (is_same_v<_Tp, unsigned>)
4459 else if constexpr (is_same_v<_Tp, long long>)
4461 else if constexpr (is_same_v<_Tp, unsigned long long>)
4463 else if constexpr (is_same_v<_Tp, float>)
4465 else if constexpr (is_same_v<_Tp, double>)
4467#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4468 else if constexpr (is_same_v<_Tp, long double>)
4472 else if constexpr (is_same_v<_Tp, __ibm128>)
4474 else if constexpr (is_same_v<_Tp, __ieee128>)
4475 return _Arg_ieee128;
4477#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4478 else if constexpr (is_same_v<_Tp, __float128>)
4479 return _Arg_float128;
4481#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4482 else if constexpr (is_same_v<_Tp, __format::__bflt16_t>)
4485#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4486 else if constexpr (is_same_v<_Tp, _Float16>)
4489#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4490 else if constexpr (is_same_v<_Tp, _Float32>)
4493#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4494 else if constexpr (is_same_v<_Tp, _Float64>)
4497 else if constexpr (is_same_v<_Tp, const _CharT*>)
4499 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4501 else if constexpr (is_same_v<_Tp, const void*>)
4503#ifdef __SIZEOF_INT128__
4504 else if constexpr (is_same_v<_Tp, __int128>)
4506 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4509 else if constexpr (is_same_v<_Tp, handle>)
4513 template<
typename _Tp>
4515 _M_set(_Tp __v)
noexcept
4517 _M_type = _S_to_enum<_Tp>();
4521 template<
typename _Tp>
4522 requires __format::__formattable_with<_Tp, _Context>
4524 basic_format_arg(_Tp& __v)
noexcept
4526 using _Td = _Normalize<_Tp>;
4527 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4528 _M_set(_Td{__v.data(), __v.size()});
4529 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4530 && is_same_v<_CharT, wchar_t>)
4531 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4533 _M_set(
static_cast<_Td
>(__v));
4536 template<
typename _Ctx,
typename... _Argz>
4538 make_format_args(_Argz&...)
noexcept;
4540 template<
typename _Visitor,
typename _Ctx>
4541 friend decltype(
auto)
4542 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4544 template<
typename _Visitor,
typename _Ctx>
4545 friend decltype(
auto)
4546 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4548 template<
typename _Ch,
typename _Tp>
4549 friend consteval __format::_Arg_t
4550 __format::__to_arg_t_enum() noexcept;
4552 template<typename _Visitor>
4554 _M_visit(_Visitor&& __vis, __format::_Arg_t __type)
4556 using namespace __format;
4573#if __glibcxx_to_chars
4578#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4581#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4591#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4595#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4599#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4603#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4616 auto& __h =
static_cast<handle&
>(_M_val._M_handle);
4619#ifdef __SIZEOF_INT128__
4626 __builtin_unreachable();
4630 template<
typename _Visitor>
4632 _M_visit_user(_Visitor&& __vis, __format::_Arg_t __type)
4634 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4636 constexpr bool __user_facing = __is_one_of<_Tp,
4637 monostate, bool, _CharT,
4638 int,
unsigned int,
long long int,
unsigned long long int,
4639 float, double,
long double,
4640 const _CharT*, basic_string_view<_CharT>,
4641 const void*, handle>::value;
4642 if constexpr (__user_facing)
4653 template<
typename _Visitor,
typename _Context>
4654 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4655 inline decltype(auto)
4656 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4664 template<
typename _Visitor,
typename _Ctx>
4665 inline decltype(
auto)
4666 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4671 struct _WidthPrecVisitor
4673 template<
typename _Tp>
4675 operator()(_Tp& __arg)
const
4677 if constexpr (is_same_v<_Tp, monostate>)
4678 __format::__invalid_arg_id_in_format_string();
4682 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4686 if constexpr (__is_unsigned_integer<_Tp>::value)
4688 else if constexpr (__is_signed_integer<_Tp>::value)
4692 __throw_format_error(
"format error: argument used for width or "
4693 "precision must be a non-negative integer");
4697#pragma GCC diagnostic push
4698#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4699 template<
typename _Context>
4701 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4702 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4705 template<
int _Bits,
size_t _Nm>
4707 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4709 __UINT64_TYPE__ __packed_types = 0;
4710 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4711 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4712 return __packed_types;
4717 template<
typename _Context>
4718 class basic_format_args
4720 static constexpr int _S_packed_type_bits = 5;
4721 static constexpr int _S_packed_type_mask = 0b11111;
4722 static constexpr int _S_max_packed_args = 12;
4724 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4726 template<
typename... _Args>
4727 using _Store = __format::_Arg_store<_Context, _Args...>;
4729 template<
typename _Ctx,
typename... _Args>
4730 friend class __format::_Arg_store;
4732 using uint64_t = __UINT64_TYPE__;
4733 using _Format_arg = basic_format_arg<_Context>;
4734 using _Format_arg_val = __format::_Arg_value<_Context>;
4740 uint64_t _M_packed_size : 4;
4741 uint64_t _M_unpacked_size : 60;
4744 const _Format_arg_val* _M_values;
4745 const _Format_arg* _M_args;
4749 _M_size() const noexcept
4750 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4752 typename __format::_Arg_t
4753 _M_type(
size_t __i)
const noexcept
4755 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4756 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4759 template<
typename _Ctx,
typename... _Args>
4761 make_format_args(_Args&...)
noexcept;
4764 template<
typename... _Args>
4765 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4767 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4770 template<
typename... _Args>
4771 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4773 [[nodiscard,__gnu__::__always_inline__]]
4774 basic_format_arg<_Context>
4775 get(
size_t __i)
const noexcept
4777 basic_format_arg<_Context> __arg;
4778 if (__i < _M_packed_size)
4780 __arg._M_type = _M_type(__i);
4781 __arg._M_val = _M_values[__i];
4783 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4784 __arg = _M_args[__i];
4791 template<
typename _Context,
typename... _Args>
4792 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4793 -> basic_format_args<_Context>;
4795 template<
typename _Context,
typename... _Args>
4797 make_format_args(_Args&... __fmt_args)
noexcept;
4800 template<
typename _Context,
typename... _Args>
4801 class __format::_Arg_store
4803 friend std::basic_format_args<_Context>;
4805 template<
typename _Ctx,
typename... _Argz>
4807#if _GLIBCXX_INLINE_VERSION
4810 make_format_args(_Argz&...)
noexcept;
4814 static constexpr bool _S_values_only
4815 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4818 = __conditional_t<_S_values_only,
4819 __format::_Arg_value<_Context>,
4820 basic_format_arg<_Context>>;
4822 _Element_t _M_args[
sizeof...(_Args)];
4824 template<
typename _Tp>
4826 _S_make_elt(_Tp& __v)
4828 using _Tq = remove_const_t<_Tp>;
4829 using _CharT =
typename _Context::char_type;
4830 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4831 "std::formatter must be specialized for the type "
4832 "of each format arg");
4833 using __format::__formattable_with;
4834 if constexpr (is_const_v<_Tp>)
4835 if constexpr (!__formattable_with<_Tp, _Context>)
4836 if constexpr (__formattable_with<_Tq, _Context>)
4837 static_assert(__formattable_with<_Tp, _Context>,
4838 "format arg must be non-const because its "
4839 "std::formatter specialization has a "
4840 "non-const reference parameter");
4841 basic_format_arg<_Context> __arg(__v);
4842 if constexpr (_S_values_only)
4843 return __arg._M_val;
4848 template<
typename... _Tp>
4849 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4850 [[__gnu__::__always_inline__]]
4851 _Arg_store(_Tp&... __a) noexcept
4852 : _M_args{_S_make_elt(__a)...}
4856 template<
typename _Context>
4857 class __format::_Arg_store<_Context>
4860 template<
typename _Context>
4861 template<
typename... _Args>
4863 basic_format_args<_Context>::
4864 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4866 if constexpr (
sizeof...(_Args) == 0)
4869 _M_unpacked_size = 0;
4872 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4875 _M_packed_size =
sizeof...(_Args);
4878 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4880 _M_values = __store._M_args;
4887 _M_unpacked_size =
sizeof...(_Args);
4889 _M_args = __store._M_args;
4894 template<
typename _Context = format_context,
typename... _Args>
4895 [[nodiscard,__gnu__::__always_inline__]]
4897 make_format_args(_Args&... __fmt_args)
noexcept
4899 using _Fmt_arg = basic_format_arg<_Context>;
4900 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4901 _Normalize<_Args>...>;
4902 return _Store(__fmt_args...);
4905#ifdef _GLIBCXX_USE_WCHAR_T
4907 template<
typename... _Args>
4908 [[nodiscard,__gnu__::__always_inline__]]
4910 make_wformat_args(_Args&... __args)
noexcept
4911 {
return std::make_format_args<wformat_context>(__args...); }
4917 template<
typename _Out,
typename _CharT,
typename _Context>
4919 __do_vformat_to(_Out, basic_string_view<_CharT>,
4920 const basic_format_args<_Context>&,
4921 const locale* =
nullptr);
4923 template<
typename _CharT>
struct __formatter_chrono;
4941 template<
typename _Out,
typename _CharT>
4942 class basic_format_context
4944 static_assert( output_iterator<_Out, const _CharT&> );
4946 basic_format_args<basic_format_context> _M_args;
4948 __format::_Optional_locale _M_loc;
4950 basic_format_context(basic_format_args<basic_format_context> __args,
4952 : _M_args(__args), _M_out(std::
move(__out))
4955 basic_format_context(basic_format_args<basic_format_context> __args,
4956 _Out __out,
const std::locale& __loc)
4957 : _M_args(__args), _M_out(std::
move(__out)), _M_loc(__loc)
4963 basic_format_context(
const basic_format_context&) =
delete;
4964 basic_format_context& operator=(
const basic_format_context&) =
delete;
4966 template<
typename _Out2,
typename _CharT2,
typename _Context2>
4968 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
4969 const basic_format_args<_Context2>&,
4972 friend __format::__formatter_chrono<_CharT>;
4975 ~basic_format_context() =
default;
4977 using iterator = _Out;
4978 using char_type = _CharT;
4979 template<
typename _Tp>
4980 using formatter_type = formatter<_Tp, _CharT>;
4983 basic_format_arg<basic_format_context>
4984 arg(
size_t __id)
const noexcept
4985 {
return _M_args.get(__id); }
4988 std::locale locale() {
return _M_loc.value(); }
4991 iterator out() {
return std::move(_M_out); }
4993 void advance_to(iterator __it) { _M_out =
std::move(__it); }
5005 template<
typename _CharT>
5008 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
5010 struct _Parse_context : basic_format_parse_context<_CharT>
5012 using basic_format_parse_context<_CharT>::basic_format_parse_context;
5013 const _Arg_t* _M_types =
nullptr;
5017 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5018 : _M_pc(__str, __nargs)
5021 constexpr iterator
begin() const noexcept {
return _M_pc.begin(); }
5022 constexpr iterator
end() const noexcept {
return _M_pc.end(); }
5027 basic_string_view<_CharT> __fmt = _M_fmt_str();
5029 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5031 _M_pc.advance_to(
begin() + 1);
5032 _M_format_arg(_M_pc.next_arg_id());
5036 size_t __lbr = __fmt.find(
'{');
5037 size_t __rbr = __fmt.find(
'}');
5039 while (__fmt.size())
5041 auto __cmp = __lbr <=> __rbr;
5045 _M_pc.advance_to(
end());
5050 if (__lbr + 1 == __fmt.size()
5051 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5052 __format::__unmatched_left_brace_in_format_string();
5053 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5054 iterator __last =
begin() + __lbr + int(__is_escape);
5055 _M_on_chars(__last);
5056 _M_pc.advance_to(__last + 1);
5057 __fmt = _M_fmt_str();
5060 if (__rbr != __fmt.npos)
5062 __lbr = __fmt.find(
'{');
5066 _M_on_replacement_field();
5067 __fmt = _M_fmt_str();
5068 __lbr = __fmt.find(
'{');
5069 __rbr = __fmt.find(
'}');
5074 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5075 __format::__unmatched_right_brace_in_format_string();
5076 iterator __last =
begin() + __rbr;
5077 _M_on_chars(__last);
5078 _M_pc.advance_to(__last + 1);
5079 __fmt = _M_fmt_str();
5080 if (__lbr != __fmt.npos)
5082 __rbr = __fmt.find(
'}');
5087 constexpr basic_string_view<_CharT>
5088 _M_fmt_str() const noexcept
5091 constexpr virtual void _M_on_chars(iterator) { }
5093 constexpr void _M_on_replacement_field()
5095 auto __next =
begin();
5099 __id = _M_pc.next_arg_id();
5100 else if (*__next ==
':')
5102 __id = _M_pc.next_arg_id();
5103 _M_pc.advance_to(++__next);
5107 auto [__i, __ptr] = __format::__parse_arg_id(
begin(),
end());
5108 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5109 __format::__invalid_arg_id_in_format_string();
5110 _M_pc.check_arg_id(__id = __i);
5113 _M_pc.advance_to(++__ptr);
5116 _M_pc.advance_to(__ptr);
5118 _M_format_arg(__id);
5120 __format::__unmatched_left_brace_in_format_string();
5121 _M_pc.advance_to(
begin() + 1);
5124 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5128 template<
typename _Out,
typename _CharT>
5129 class _Formatting_scanner :
public _Scanner<_CharT>
5132 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5133 basic_string_view<_CharT> __str)
5134 : _Scanner<_CharT>(__str), _M_fc(__fc)
5138 basic_format_context<_Out, _CharT>& _M_fc;
5140 using iterator =
typename _Scanner<_CharT>::iterator;
5143 _M_on_chars(iterator __last)
override
5145 basic_string_view<_CharT> __str(this->
begin(), __last);
5146 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5150 _M_format_arg(
size_t __id)
override
5152 using _Context = basic_format_context<_Out, _CharT>;
5153 using handle =
typename basic_format_arg<_Context>::handle;
5155 __format::__visit_format_arg([
this](
auto& __arg) {
5157 using _Formatter =
typename _Context::template formatter_type<_Type>;
5158 if constexpr (is_same_v<_Type, monostate>)
5159 __format::__invalid_arg_id_in_format_string();
5160 else if constexpr (is_same_v<_Type, handle>)
5161 __arg.format(this->_M_pc, this->_M_fc);
5162 else if constexpr (is_default_constructible_v<_Formatter>)
5165 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5166 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5169 static_assert(__format::__formattable_with<_Type, _Context>);
5170 }, _M_fc.arg(__id));
5174 template<
typename _CharT,
typename _Tp>
5176 __to_arg_t_enum() noexcept
5178 using _Context = __format::__format_context<_CharT>;
5179 using _Fmt_arg = basic_format_arg<_Context>;
5180 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5181 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5185 template<
typename _CharT,
typename... _Args>
5186 class _Checking_scanner :
public _Scanner<_CharT>
5189 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5190 "std::formatter must be specialized for each type being formatted");
5194 _Checking_scanner(basic_string_view<_CharT> __str)
5195 : _Scanner<_CharT>(__str, sizeof...(_Args))
5197#if __cpp_lib_format >= 202305L
5198 this->_M_pc._M_types = _M_types.data();
5204 _M_format_arg(
size_t __id)
override
5206 if constexpr (
sizeof...(_Args) != 0)
5208 if (__id <
sizeof...(_Args))
5210 _M_parse_format_spec<_Args...>(__id);
5214 __builtin_unreachable();
5217 template<
typename _Tp,
typename... _OtherArgs>
5219 _M_parse_format_spec(
size_t __id)
5223 formatter<_Tp, _CharT> __f;
5224 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5226 else if constexpr (
sizeof...(_OtherArgs) != 0)
5227 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5229 __builtin_unreachable();
5232#if __cpp_lib_format >= 202305L
5233 array<_Arg_t,
sizeof...(_Args)>
5234 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5238 template<
typename _Out,
typename _CharT,
typename _Context>
5240 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5241 const basic_format_args<_Context>& __args,
5242 const locale* __loc)
5244 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5245 _Sink_iter<_CharT> __sink_out;
5247 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5250 __sink_out = __sink.out();
5252 if constexpr (is_same_v<_CharT, char>)
5254 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5256 bool __done =
false;
5257 __format::__visit_format_arg([&](
auto& __arg) {
5258 using _Tp = remove_cvref_t<
decltype(__arg)>;
5259 if constexpr (is_same_v<_Tp, bool>)
5261 size_t __len = 4 + !__arg;
5262 const char* __chars[] = {
"false",
"true" };
5263 if (
auto __res = __sink_out._M_reserve(__len))
5265 __builtin_memcpy(__res.get(), __chars[__arg], __len);
5266 __res._M_bump(__len);
5270 else if constexpr (is_same_v<_Tp, char>)
5272 if (
auto __res = __sink_out._M_reserve(1))
5274 *__res.get() = __arg;
5279 else if constexpr (is_integral_v<_Tp>)
5281 make_unsigned_t<_Tp> __uval;
5282 const bool __neg = __arg < 0;
5284 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5287 const auto __n = __detail::__to_chars_len(__uval);
5288 if (
auto __res = __sink_out._M_reserve(__n + __neg))
5290 auto __ptr = __res.get();
5292 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5294 __res._M_bump(__n + __neg);
5298 else if constexpr (is_convertible_v<_Tp, string_view>)
5300 string_view __sv = __arg;
5301 if (
auto __res = __sink_out._M_reserve(__sv.size()))
5303 __builtin_memcpy(__res.get(), __sv.data(), __sv.size());
5304 __res._M_bump(__sv.size());
5312 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5315 return std::move(__sink)._M_finish().out;
5319 auto __ctx = __loc ==
nullptr
5320 ? _Context(__args, __sink_out)
5321 : _Context(__args, __sink_out, *__loc);
5322 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5323 __scanner._M_scan();
5325 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5328 return std::move(__sink)._M_finish().out;
5330#pragma GCC diagnostic pop
5335#if __cpp_lib_format >= 202305L
5338 template<
typename _CharT>
5339 template<
typename... _Ts>
5341 basic_format_parse_context<_CharT>::
5342 __check_dynamic_spec(
size_t __id)
noexcept
5344 if (__id >= _M_num_args)
5345 __format::__invalid_arg_id_in_format_string();
5346 if constexpr (
sizeof...(_Ts) != 0)
5348 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
5349 auto __arg =
static_cast<_Parse_ctx*
>(
this)->_M_types[__id];
5350 __format::_Arg_t __types[] = {
5351 __format::__to_arg_t_enum<_CharT, _Ts>()...
5353 for (
auto __t : __types)
5357 __invalid_dynamic_spec(
"arg(id) type does not match");
5362 template<
typename _CharT,
typename... _Args>
5363 template<
typename _Tp>
5364 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5366 basic_format_string<_CharT, _Args...>::
5367 basic_format_string(
const _Tp& __s)
5370 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5372 __scanner._M_scan();
5377 template<
typename _Out>
requires output_iterator<_Out, const char&>
5378 [[__gnu__::__always_inline__]]
5380 vformat_to(_Out __out, string_view __fmt, format_args __args)
5381 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5383#ifdef _GLIBCXX_USE_WCHAR_T
5384 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5385 [[__gnu__::__always_inline__]]
5387 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5388 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5391 template<
typename _Out>
requires output_iterator<_Out, const char&>
5392 [[__gnu__::__always_inline__]]
5394 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5397 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5400#ifdef _GLIBCXX_USE_WCHAR_T
5401 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5402 [[__gnu__::__always_inline__]]
5404 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5405 wformat_args __args)
5407 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5413 vformat(string_view __fmt, format_args __args)
5415 __format::_Str_sink<char> __buf;
5416 std::vformat_to(__buf.out(), __fmt, __args);
5420#ifdef _GLIBCXX_USE_WCHAR_T
5423 vformat(wstring_view __fmt, wformat_args __args)
5425 __format::_Str_sink<wchar_t> __buf;
5426 std::vformat_to(__buf.out(), __fmt, __args);
5433 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5435 __format::_Str_sink<char> __buf;
5436 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5440#ifdef _GLIBCXX_USE_WCHAR_T
5443 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5445 __format::_Str_sink<wchar_t> __buf;
5446 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5451 template<
typename... _Args>
5454 format(format_string<_Args...> __fmt, _Args&&... __args)
5455 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5457#ifdef _GLIBCXX_USE_WCHAR_T
5458 template<
typename... _Args>
5461 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5462 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5465 template<
typename... _Args>
5468 format(
const locale& __loc, format_string<_Args...> __fmt,
5471 return std::vformat(__loc, __fmt.get(),
5472 std::make_format_args(__args...));
5475#ifdef _GLIBCXX_USE_WCHAR_T
5476 template<
typename... _Args>
5479 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5482 return std::vformat(__loc, __fmt.get(),
5483 std::make_wformat_args(__args...));
5487 template<
typename _Out,
typename... _Args>
5488 requires output_iterator<_Out, const char&>
5490 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5492 return std::vformat_to(
std::move(__out), __fmt.get(),
5493 std::make_format_args(__args...));
5496#ifdef _GLIBCXX_USE_WCHAR_T
5497 template<
typename _Out,
typename... _Args>
5498 requires output_iterator<_Out, const wchar_t&>
5500 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5502 return std::vformat_to(
std::move(__out), __fmt.get(),
5503 std::make_wformat_args(__args...));
5507 template<
typename _Out,
typename... _Args>
5508 requires output_iterator<_Out, const char&>
5510 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5513 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5514 std::make_format_args(__args...));
5517#ifdef _GLIBCXX_USE_WCHAR_T
5518 template<
typename _Out,
typename... _Args>
5519 requires output_iterator<_Out, const wchar_t&>
5521 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5524 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5525 std::make_wformat_args(__args...));
5529 template<
typename _Out,
typename... _Args>
5530 requires output_iterator<_Out, const char&>
5531 inline format_to_n_result<_Out>
5532 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5533 format_string<_Args...> __fmt, _Args&&... __args)
5535 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
5536 std::vformat_to(__sink.out(), __fmt.get(),
5537 std::make_format_args(__args...));
5541#ifdef _GLIBCXX_USE_WCHAR_T
5542 template<
typename _Out,
typename... _Args>
5543 requires output_iterator<_Out, const wchar_t&>
5544 inline format_to_n_result<_Out>
5545 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5546 wformat_string<_Args...> __fmt, _Args&&... __args)
5548 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
5549 std::vformat_to(__sink.out(), __fmt.get(),
5550 std::make_wformat_args(__args...));
5555 template<
typename _Out,
typename... _Args>
5556 requires output_iterator<_Out, const char&>
5557 inline format_to_n_result<_Out>
5558 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5559 format_string<_Args...> __fmt, _Args&&... __args)
5561 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
5562 std::vformat_to(__sink.out(), __loc, __fmt.get(),
5563 std::make_format_args(__args...));
5567#ifdef _GLIBCXX_USE_WCHAR_T
5568 template<
typename _Out,
typename... _Args>
5569 requires output_iterator<_Out, const wchar_t&>
5570 inline format_to_n_result<_Out>
5571 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5572 wformat_string<_Args...> __fmt, _Args&&... __args)
5574 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
5575 std::vformat_to(__sink.out(), __loc, __fmt.get(),
5576 std::make_wformat_args(__args...));
5585 template<
typename _CharT>
5586 class _Counting_sink final :
public _Iter_sink<_CharT, _CharT*>
5589 _Counting_sink() : _Iter_sink<_CharT, _CharT*>(nullptr, 0) { }
5591 [[__gnu__::__always_inline__]]
5594 {
return this->_M_count + this->_M_used().size(); }
5597 template<
typename _CharT>
5598 class _Counting_sink :
public _Buf_sink<_CharT>
5600 size_t _M_count = 0;
5603 _M_overflow()
override
5605 if (!std::is_constant_evaluated())
5606 _M_count += this->_M_used().size();
5611 _Counting_sink() =
default;
5613 [[__gnu__::__always_inline__]]
5617 _Counting_sink::_M_overflow();
5625 template<
typename... _Args>
5628 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5630 __format::_Counting_sink<char> __buf;
5631 std::vformat_to(__buf.out(), __fmt.get(),
5632 std::make_format_args(__args...));
5633 return __buf.count();
5636#ifdef _GLIBCXX_USE_WCHAR_T
5637 template<
typename... _Args>
5640 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5642 __format::_Counting_sink<wchar_t> __buf;
5643 std::vformat_to(__buf.out(), __fmt.get(),
5644 std::make_wformat_args(__args...));
5645 return __buf.count();
5649 template<
typename... _Args>
5652 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5655 __format::_Counting_sink<char> __buf;
5656 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5657 std::make_format_args(__args...));
5658 return __buf.count();
5661#ifdef _GLIBCXX_USE_WCHAR_T
5662 template<
typename... _Args>
5665 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5668 __format::_Counting_sink<wchar_t> __buf;
5669 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5670 std::make_wformat_args(__args...));
5671 return __buf.count();
5675#if __glibcxx_format_ranges
5677 template<typename _Tp>
5678 consteval range_format
5681 using _Ref = ranges::range_reference_t<_Tp>;
5682 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5683 return range_format::disabled;
5684 else if constexpr (
requires {
typename _Tp::key_type; })
5686 if constexpr (
requires {
typename _Tp::mapped_type; })
5688 using _Up = remove_cvref_t<_Ref>;
5689 if constexpr (__is_pair<_Up>)
5690 return range_format::map;
5691 else if constexpr (__is_specialization_of<_Up, tuple>)
5692 if constexpr (tuple_size_v<_Up> == 2)
5693 return range_format::map;
5695 return range_format::set;
5698 return range_format::sequence;
5703 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5704 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5709 template<
typename _CharT,
typename _Out,
typename _Callback>
5710 typename basic_format_context<_Out, _CharT>::iterator
5711 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5712 const _Spec<_CharT>& __spec,
5715 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
5721 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
5723 const size_t __padwidth = __spec._M_get_width(__fc);
5724 if (__padwidth == 0)
5725 return __call(__fc);
5729 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5730 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5735 { _M_ctx =
nullptr; }
5740 _M_ctx->advance_to(_M_out);
5744 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5745 _Sink_iter<_CharT> _M_out;
5748 _Restore_out __restore(__fc);
5749 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
5750 __fc.advance_to(__sink.out());
5752 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
5753 __restore._M_disarm();
5758 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5759 struct __indexed_formatter_storage
5764 basic_format_parse_context<_CharT> __pc({});
5765 if (_M_formatter.parse(__pc) != __pc.end())
5766 __format::__failed_to_parse_format_spec();
5769 template<
typename _Out>
5771 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5772 basic_format_context<_Out, _CharT>& __fc,
5773 basic_string_view<_CharT> __sep)
const
5775 if constexpr (_Pos != 0)
5776 __fc.advance_to(__format::__write(__fc.out(), __sep));
5777 __fc.advance_to(_M_formatter.format(__elem, __fc));
5780 [[__gnu__::__always_inline__]]
5784 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5785 _M_formatter.set_debug_format();
5789 formatter<_Tp, _CharT> _M_formatter;
5792 template<
typename _CharT,
typename... _Tps>
5793 class __tuple_formatter
5795 using _String_view = basic_string_view<_CharT>;
5796 using _Seps = __format::_Separators<_CharT>;
5800 set_separator(basic_string_view<_CharT> __sep)
noexcept
5804 set_brackets(basic_string_view<_CharT> __open,
5805 basic_string_view<_CharT> __close)
noexcept
5813 constexpr typename basic_format_parse_context<_CharT>::iterator
5814 parse(basic_format_parse_context<_CharT>& __pc)
5816 auto __first = __pc.begin();
5817 const auto __last = __pc.end();
5818 __format::_Spec<_CharT> __spec{};
5820 auto __finished = [&]
5822 if (__first != __last && *__first !=
'}')
5826 _M_felems._M_parse();
5827 _M_felems.set_debug_format();
5834 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5838 __first = __spec._M_parse_width(__first, __last, __pc);
5842 if (*__first ==
'n')
5845 _M_open = _M_close = _String_view();
5847 else if (*__first ==
'm')
5850 if constexpr (
sizeof...(_Tps) == 2)
5852 _M_sep = _Seps::_S_colon();
5853 _M_open = _M_close = _String_view();
5856 __throw_format_error(
"format error: 'm' specifier requires range"
5857 " of pair or tuple of two elements");
5863 __format::__failed_to_parse_format_spec();
5867 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
5868 typename basic_format_context<_Out, _CharT>::iterator
5869 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
5870 basic_format_context<_Out, _CharT>& __fc)
const
5871 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
5873 template<
typename _Out>
5874 typename basic_format_context<_Out, _CharT>::iterator
5875 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
5876 basic_format_context<_Out, _CharT>& __fc)
const
5878 return __format::__format_padded(
5880 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
5882 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
5883 _M_felems._M_format(__elems..., __nfc, _M_sep);
5884 return __format::__write(__nfc.out(), _M_close);
5889 template<
size_t... _Ids>
5890 struct __formatters_storage
5891 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
5893 template<
size_t _Id,
typename _Up>
5894 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
5899 (_Base<_Ids, _Tps>::_M_parse(), ...);
5902 template<
typename _Out>
5904 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
5905 basic_format_context<_Out, _CharT>& __fc,
5906 _String_view __sep)
const
5908 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
5914 (_Base<_Ids, _Tps>::set_debug_format(), ...);
5918 template<
size_t... _Ids>
5920 _S_create_storage(index_sequence<_Ids...>)
5921 -> __formatters_storage<_Ids...>;
5923 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
5925 _Spec<_CharT> _M_spec{};
5926 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
5927 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
5928 _String_view _M_sep = _Seps::_S_comma();
5929 _Formatters _M_felems;
5932 template<
typename _Tp>
5933 concept __is_map_formattable
5934 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
5940 template<__format::__
char _CharT, formattable<_CharT> _Fp,
5941 formattable<_CharT> _Sp>
5942 struct formatter<
pair<_Fp, _Sp>, _CharT>
5943 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
5944 remove_cvref_t<_Sp>>
5947 using __maybe_const_pair
5948 = __conditional_t<formattable<const _Fp, _CharT>
5949 && formattable<const _Sp, _CharT>,
5950 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
5954 template<
typename _Out>
5955 typename basic_format_context<_Out, _CharT>::iterator
5956 format(__maybe_const_pair& __p,
5957 basic_format_context<_Out, _CharT>& __fc)
const
5958 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
5961#if __glibcxx_print >= 202406L
5964 template<
typename _Fp,
typename _Sp>
5965 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
5966 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
5967 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
5970 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
5971 struct formatter<tuple<_Tps...>, _CharT>
5972 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
5975 using __maybe_const_tuple
5976 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
5977 const tuple<_Tps...>, tuple<_Tps...>>;
5981 template<
typename _Out>
5982 typename basic_format_context<_Out, _CharT>::iterator
5983 format(__maybe_const_tuple& __t,
5984 basic_format_context<_Out, _CharT>& __fc)
const
5985 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
5988#if __glibcxx_print >= 202406L
5991 template<
typename... _Tps>
5992 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
5993 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
5997 template<
typename _Tp, __format::__
char _CharT>
5998 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
5999 class range_formatter
6001 using _String_view = basic_string_view<_CharT>;
6002 using _Seps = __format::_Separators<_CharT>;
6006 set_separator(basic_string_view<_CharT> __sep)
noexcept
6010 set_brackets(basic_string_view<_CharT> __open,
6011 basic_string_view<_CharT> __close)
noexcept
6017 constexpr formatter<_Tp, _CharT>&
6018 underlying() noexcept
6021 constexpr const formatter<_Tp, _CharT>&
6022 underlying() const noexcept
6027 constexpr typename basic_format_parse_context<_CharT>::iterator
6028 parse(basic_format_parse_context<_CharT>& __pc)
6030 auto __first = __pc.begin();
6031 const auto __last = __pc.end();
6032 __format::_Spec<_CharT> __spec{};
6033 bool __no_brace =
false;
6035 auto __finished = [&]
6036 {
return __first == __last || *__first ==
'}'; };
6038 auto __finalize = [&]
6044 auto __parse_val = [&](_String_view __nfs = _String_view())
6046 basic_format_parse_context<_CharT> __npc(__nfs);
6047 if (_M_fval.parse(__npc) != __npc.end())
6048 __format::__failed_to_parse_format_spec();
6049 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6050 _M_fval.set_debug_format();
6051 return __finalize();
6055 return __parse_val();
6057 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6059 return __parse_val();
6061 __first = __spec._M_parse_width(__first, __last, __pc);
6063 return __parse_val();
6065 if (*__first ==
'?')
6068 __spec._M_debug =
true;
6069 if (__finished() || *__first !=
's')
6070 __throw_format_error(
"format error: '?' is allowed only in"
6071 " combination with 's'");
6074 if (*__first ==
's')
6077 if constexpr (same_as<_Tp, _CharT>)
6079 __spec._M_type = __format::_Pres_s;
6081 return __finalize();
6082 __throw_format_error(
"format error: element format specifier"
6083 " cannot be provided when 's' specifier is used");
6086 __throw_format_error(
"format error: 's' specifier requires"
6087 " range of character types");
6091 return __parse_val();
6093 if (*__first ==
'n')
6096 _M_open = _M_close = _String_view();
6101 return __parse_val();
6103 if (*__first ==
'm')
6105 _String_view __m(__first, 1);
6107 if constexpr (__format::__is_map_formattable<_Tp>)
6109 _M_sep = _Seps::_S_comma();
6112 _M_open = _Seps::_S_braces().substr(0, 1);
6113 _M_close = _Seps::_S_braces().substr(1, 1);
6116 return __parse_val(__m);
6117 __throw_format_error(
"format error: element format specifier"
6118 " cannot be provided when 'm' specifier is used");
6121 __throw_format_error(
"format error: 'm' specifier requires"
6122 " range of pairs or tuples of two elements");
6126 return __parse_val();
6128 if (*__first ==
':')
6130 __pc.advance_to(++__first);
6131 __first = _M_fval.parse(__pc);
6135 return __finalize();
6137 __format::__failed_to_parse_format_spec();
6142 template<ranges::input_range _Rg,
typename _Out>
6143 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6144 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6145 typename basic_format_context<_Out, _CharT>::iterator
6146 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6148 using _Range = remove_reference_t<_Rg>;
6149 if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6150 return _M_format<const _Range>(__rg, __fc);
6152 return _M_format(__rg, __fc);
6156 template<ranges::input_range _Rg,
typename _Out>
6157 typename basic_format_context<_Out, _CharT>::iterator
6158 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6160 if constexpr (same_as<_Tp, _CharT>)
6161 if (_M_spec._M_type == __format::_Pres_s)
6163 __format::__formatter_str __fstr(_M_spec);
6164 return __fstr._M_format_range(__rg, __fc);
6166 return __format::__format_padded(
6168 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6169 {
return _M_format_elems(__rg, __nfc); });
6173 template<ranges::input_range _Rg,
typename _Out>
6174 typename basic_format_context<_Out, _CharT>::iterator
6175 _M_format_elems(_Rg& __rg,
6176 basic_format_context<_Out, _CharT>& __fc)
const
6178 auto __out = __format::__write(__fc.out(), _M_open);
6180 auto __first = ranges::begin(__rg);
6181 auto const __last = ranges::end(__rg);
6182 if (__first == __last)
6183 return __format::__write(__out, _M_close);
6185 __fc.advance_to(__out);
6186 __out = _M_fval.format(*__first, __fc);
6187 for (++__first; __first != __last; ++__first)
6189 __out = __format::__write(__out, _M_sep);
6190 __fc.advance_to(__out);
6191 __out = _M_fval.format(*__first, __fc);
6194 return __format::__write(__out, _M_close);
6197 __format::_Spec<_CharT> _M_spec{};
6198 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6199 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6200 _String_view _M_sep = _Seps::_S_comma();
6201 formatter<_Tp, _CharT> _M_fval;
6209 template<ranges::input_range _Rg, __format::__
char _CharT>
6210 requires (format_kind<_Rg> != range_format::disabled)
6211 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6212 struct formatter<_Rg, _CharT>
6215 static const bool _S_range_format_is_string =
6216 (format_kind<_Rg> == range_format::string)
6217 || (format_kind<_Rg> == range_format::debug_string);
6218 using _Vt = remove_cvref_t<
6219 ranges::range_reference_t<
6220 __format::__maybe_const_range<_Rg, _CharT>>>;
6222 static consteval bool _S_is_correct()
6224 if constexpr (_S_range_format_is_string)
6225 static_assert(same_as<_Vt, _CharT>);
6229 static_assert(_S_is_correct());
6232 constexpr formatter() noexcept
6234 using _Seps = __format::_Separators<_CharT>;
6235 if constexpr (format_kind<_Rg> == range_format::map)
6237 static_assert(__format::__is_map_formattable<_Vt>);
6238 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6239 _Seps::_S_braces().substr(1, 1));
6240 _M_under.underlying().set_brackets({}, {});
6241 _M_under.underlying().set_separator(_Seps::_S_colon());
6243 else if constexpr (format_kind<_Rg> == range_format::set)
6244 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6245 _Seps::_S_braces().substr(1, 1));
6249 set_separator(basic_string_view<_CharT> __sep)
noexcept
6250 requires (format_kind<_Rg> == range_format::sequence)
6251 { _M_under.set_separator(__sep); }
6254 set_brackets(basic_string_view<_CharT> __open,
6255 basic_string_view<_CharT> __close)
noexcept
6256 requires (format_kind<_Rg> == range_format::sequence)
6257 { _M_under.set_brackets(__open, __close); }
6261 constexpr typename basic_format_parse_context<_CharT>::iterator
6262 parse(basic_format_parse_context<_CharT>& __pc)
6264 auto __res = _M_under.parse(__pc);
6265 if constexpr (format_kind<_Rg> == range_format::debug_string)
6266 _M_under.set_debug_format();
6272 template<
typename _Out>
6273 typename basic_format_context<_Out, _CharT>::iterator
6274 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6275 basic_format_context<_Out, _CharT>& __fc)
const
6277 if constexpr (_S_range_format_is_string)
6278 return _M_under._M_format_range(__rg, __fc);
6280 return _M_under.format(__rg, __fc);
6284 using _Formatter_under
6285 = __conditional_t<_S_range_format_is_string,
6286 __format::__formatter_str<_CharT>,
6287 range_formatter<_Vt, _CharT>>;
6288 _Formatter_under _M_under;
6291#if __glibcxx_print >= 202406L
6292 template<ranges::input_range _Rg>
6293 requires (format_kind<_Rg> != range_format::disabled)
6294 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6298#undef _GLIBCXX_WIDEN
6300_GLIBCXX_END_NAMESPACE_VERSION
6303#pragma GCC diagnostic pop
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
_Tp arg(const complex< _Tp > &)
Return phase angle of z.
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
const _Facet & use_facet(const locale &__loc)
Return a facet.
basic_string< char > string
A string of char.
ISO C++ entities toplevel namespace is std.
chars_format
floating-point format for primitive numerical conversion
_CharT toupper(_CharT __c, const locale &__loc)
Convenience interface to ctype.toupper(__c).
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
One of two subclasses of exception.
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
A standard container which offers fixed time access to individual elements in any order.