29#ifndef _GLIBCXX_FORMAT
30#define _GLIBCXX_FORMAT 1
33#pragma GCC system_header
38#define __glibcxx_want_format
39#define __glibcxx_want_format_ranges
40#define __glibcxx_want_format_uchar
41#define __glibcxx_want_constexpr_exceptions
42#define __glibcxx_want_constexpr_format
45#ifdef __cpp_lib_format
67#ifdef __glibcxx_constexpr_format
68# define _GLIBCXX_CONSTEXPR_FORMAT constexpr
70# define _GLIBCXX_CONSTEXPR_FORMAT
73#pragma GCC diagnostic push
74#pragma GCC diagnostic ignored "-Wpedantic"
75#pragma GCC diagnostic ignored "-Wc++23-extensions"
77namespace std _GLIBCXX_VISIBILITY(default)
79_GLIBCXX_BEGIN_NAMESPACE_VERSION
82 template<
typename _CharT,
typename... _Args>
struct basic_format_string;
92 template<
typename _CharT>
94 _Widen(
const char* __narrow,
const wchar_t* __wide)
96 if constexpr (is_same_v<_CharT, wchar_t>)
101#define _GLIBCXX_WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
102#define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
105 template<
typename _CharT>
106 constexpr size_t __stackbuf_size = 32 *
sizeof(
void*) /
sizeof(_CharT);
109 template<
typename _CharT>
class _Sink;
110 template<
typename _CharT>
class _Fixedbuf_sink;
111 template<
typename _Out,
typename _CharT>
class _Padding_sink;
112 template<
typename _Out,
typename _CharT>
class _Escaping_sink;
115 template<
typename _CharT>
119 template<
typename _CharT>
123 template<
typename _CharT>
125 {
using type = _Drop_iter<_CharT>; };
127 template<
typename _CharT>
128 using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
130 template<
typename _CharT>
131 struct _Dynamic_format_string
133 [[__gnu__::__always_inline__]]
134 _GLIBCXX_CONSTEXPR_FORMAT
135 _Dynamic_format_string(basic_string_view<_CharT> __s) noexcept
138 _Dynamic_format_string(
const _Dynamic_format_string&) =
delete;
139 void operator=(
const _Dynamic_format_string&) =
delete;
142 basic_string_view<_CharT> _M_str;
144 template<
typename,
typename...>
friend struct std::basic_format_string;
150 using format_context = __format::__format_context<char>;
151#ifdef _GLIBCXX_USE_WCHAR_T
152 using wformat_context = __format::__format_context<wchar_t>;
156 template<
typename _Context>
class basic_format_args;
157 using format_args = basic_format_args<format_context>;
158#ifdef _GLIBCXX_USE_WCHAR_T
159 using wformat_args = basic_format_args<wformat_context>;
164 template<
typename _Context>
165 class basic_format_arg;
171 template<
typename _CharT,
typename... _Args>
172 struct basic_format_string
174 template<
typename _Tp>
175 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
177 basic_format_string(
const _Tp& __s)
noexcept;
179 [[__gnu__::__always_inline__]]
180 _GLIBCXX_CONSTEXPR_FORMAT
181 basic_format_string(__format::_Dynamic_format_string<_CharT> __s) noexcept
185 [[__gnu__::__always_inline__]]
186 constexpr basic_string_view<_CharT>
191 basic_string_view<_CharT> _M_str;
194 template<
typename... _Args>
195 using format_string = basic_format_string<char, type_identity_t<_Args>...>;
197#ifdef _GLIBCXX_USE_WCHAR_T
198 template<
typename... _Args>
200 = basic_format_string<wchar_t, type_identity_t<_Args>...>;
203#if __cpp_lib_format >= 202603L
204 [[__gnu__::__always_inline__]]
205 inline _GLIBCXX_CONSTEXPR_FORMAT __format::_Dynamic_format_string<char>
206 dynamic_format(string_view __fmt)
noexcept
209#ifdef _GLIBCXX_USE_WCHAR_T
210 [[__gnu__::__always_inline__]]
211 inline _GLIBCXX_CONSTEXPR_FORMAT __format::_Dynamic_format_string<wchar_t>
212 dynamic_format(wstring_view __fmt)
noexcept
220 template<
typename _Tp,
typename _CharT>
223 formatter() =
delete;
224 formatter(
const formatter&) =
delete;
225 formatter& operator=(
const formatter&) =
delete;
232 _GLIBCXX_CONSTEXPR_FORMAT
233 explicit format_error(
const string& __what)
234 : runtime_error(__what) { }
236 _GLIBCXX_CONSTEXPR_FORMAT
explicit
237 format_error(
const char* __what)
238 : runtime_error(__what) { }
243 inline _GLIBCXX_CONSTEXPR_FORMAT
void
244 __throw_format_error(
const char* __what)
245 { _GLIBCXX_THROW_OR_ABORT(format_error(__what)); }
252 inline _GLIBCXX_CONSTEXPR_FORMAT
void
253 __unmatched_left_brace_in_format_string()
254 { __throw_format_error(
"format error: unmatched '{' in format string"); }
257 inline _GLIBCXX_CONSTEXPR_FORMAT
void
258 __unmatched_right_brace_in_format_string()
259 { __throw_format_error(
"format error: unmatched '}' in format string"); }
262 inline _GLIBCXX_CONSTEXPR_FORMAT
void
263 __conflicting_indexing_in_format_string()
264 { __throw_format_error(
"format error: conflicting indexing style in format string"); }
267 inline _GLIBCXX_CONSTEXPR_FORMAT
void
268 __invalid_arg_id_in_format_string()
269 { __throw_format_error(
"format error: invalid arg-id in format string"); }
272 inline _GLIBCXX_CONSTEXPR_FORMAT
void
273 __failed_to_parse_format_spec()
274 { __throw_format_error(
"format error: failed to parse format-spec"); }
276 template<
typename _CharT>
class _Scanner;
278 enum class _Arg_t :
unsigned char {
279 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
280 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
281 _Arg_i128, _Arg_u128, _Arg_float128,
282 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
285#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
286 _Arg_ibm128 = _Arg_ldbl,
287 _Arg_ieee128 = _Arg_float128,
292 template<
typename _CharT,
typename _Tp>
294 __to_arg_t_enum() noexcept;
300 template<typename _CharT> class basic_format_parse_context;
301 using format_parse_context = basic_format_parse_context<
char>;
302#ifdef _GLIBCXX_USE_WCHAR_T
303 using wformat_parse_context = basic_format_parse_context<wchar_t>;
306 template<
typename _CharT>
309 class _GLIBCXX_NO_SPECIALIZATIONS basic_format_parse_context
312 using char_type = _CharT;
313 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
314 using iterator = const_iterator;
317 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
318 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
321 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
322 void operator=(
const basic_format_parse_context&) =
delete;
324 constexpr const_iterator begin() const noexcept {
return _M_begin; }
325 constexpr const_iterator end() const noexcept {
return _M_end; }
328 advance_to(const_iterator __it)
noexcept
334 if (_M_indexing == _Manual)
335 __format::__conflicting_indexing_in_format_string();
340 if (std::is_constant_evaluated())
341 if (_M_next_arg_id == _M_num_args)
342 __format::__invalid_arg_id_in_format_string();
343 return _M_next_arg_id++;
347 check_arg_id(
size_t __id)
349 if (_M_indexing == _Auto)
350 __format::__conflicting_indexing_in_format_string();
351 _M_indexing = _Manual;
353 if (std::is_constant_evaluated())
354 if (__id >= _M_num_args)
355 __format::__invalid_arg_id_in_format_string();
358#if __cpp_lib_format >= 202305L
359 template<
typename... _Ts>
361 check_dynamic_spec(
size_t __id)
noexcept
363 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
364 "template arguments for check_dynamic_spec<Ts...>(id) "
365 "must be unique and must be one of the allowed types");
366 if constexpr (
sizeof...(_Ts))
368 const __format::_Arg_t __t[] = {
369 __format::__to_arg_t_enum<_CharT, _Ts>()...
371 __check_dynamic_spec(__id, __t);
376 check_dynamic_spec_integral(
size_t __id)
noexcept
379 using enum __format::_Arg_t;
380 const __format::_Arg_t __t[] = { _Arg_i, _Arg_u, _Arg_ll, _Arg_ull };
381 __check_dynamic_spec(__id, __t);
386 check_dynamic_spec_string(
size_t __id)
noexcept
389 using enum __format::_Arg_t;
390 const __format::_Arg_t __t[] = { _Arg_str, _Arg_sv };
391 __check_dynamic_spec(__id, __t);
397 template<
typename _Tp,
typename... _Ts>
398 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
400 template<
typename... _Ts>
401 static consteval bool
402 __valid_types_for_check_dynamic_spec()
406 if constexpr (
sizeof...(_Ts) == 0)
415 = __once<bool, _Ts...>
416 + __once<char_type, _Ts...>
417 + __once<int, _Ts...>
418 + __once<
unsigned int, _Ts...>
419 + __once<
long long int, _Ts...>
420 + __once<
unsigned long long int, _Ts...>
421 + __once<float, _Ts...>
422 + __once<double, _Ts...>
423 + __once<
long double, _Ts...>
424 + __once<
const char_type*, _Ts...>
425 + __once<basic_string_view<char_type>, _Ts...>
426 + __once<
const void*, _Ts...>;
427 return __sum ==
sizeof...(_Ts);
433 __check_dynamic_spec(
size_t __id,
434 span<const __format::_Arg_t> __types)
noexcept
436 if (__id >= _M_num_args)
437 __format::__invalid_arg_id_in_format_string();
443 if (
auto* __args =
static_cast<_Scan_parse_context*
>(
this)->_M_types)
445 for (
auto __t : __types)
446 if (__args[__id] == __t)
449 __invalid_dynamic_spec(
"arg(id) type does not match");
455 static void __invalid_dynamic_spec(
const char*);
460 basic_format_parse_context(basic_string_view<_CharT> __fmt,
461 size_t __num_args) noexcept
462 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
468 enum _Indexing { _Unknown, _Manual, _Auto };
469 _Indexing _M_indexing = _Unknown;
470 size_t _M_next_arg_id = 0;
471 size_t _M_num_args = 0;
474 struct _Scan_parse_context;
475 friend __format::_Scanner<_CharT>;
478 template<
typename _CharT>
479 struct basic_format_parse_context<_CharT>::_Scan_parse_context
480 : basic_format_parse_context<_CharT>
482 using basic_format_parse_context<_CharT>::basic_format_parse_context;
483 const __format::_Arg_t* _M_types =
nullptr;
487 template<
typename _Tp,
template<
typename...>
class _Class>
488 constexpr bool __is_specialization_of =
false;
489 template<
template<
typename...>
class _Class,
typename... _Args>
490 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
495 template<
typename _CharT>
496 constexpr pair<unsigned short, const _CharT*>
497 __parse_integer(
const _CharT* __first,
const _CharT* __last)
499 if (__first == __last)
500 __builtin_unreachable();
502 if constexpr (is_same_v<_CharT, char>)
504 const auto __start = __first;
505 unsigned short __val = 0;
507 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
508 && __first != __start) [[likely]]
509 return {__val, __first};
513 constexpr int __n = 32;
515 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
516 __buf[__i] = __first[__i];
517 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
518 if (__ptr) [[likely]]
519 return {__v, __first + (__ptr - __buf)};
524 template<
typename _CharT>
525 constexpr pair<unsigned short, const _CharT*>
526 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
528 if (__first == __last)
529 __builtin_unreachable();
532 return {0, __first + 1};
534 if (
'1' <= *__first && *__first <=
'9')
536 const unsigned short __id = *__first -
'0';
537 const auto __next = __first + 1;
539 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
540 return {__id, __next};
542 return __format::__parse_integer(__first, __last);
547 enum class _Pres_type :
unsigned char {
551 _Pres_c = 2, _Pres_x, _Pres_X, _Pres_d, _Pres_o, _Pres_b, _Pres_B,
553 _Pres_g = 1, _Pres_G, _Pres_a, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F,
557 using enum _Pres_type;
559 enum class _Sign :
unsigned char {
567 enum _WidthPrec :
unsigned char {
572 using enum _WidthPrec;
574 template<
typename _Context>
575 _GLIBCXX_CONSTEXPR_FORMAT
size_t
576 __int_from_arg(
const basic_format_arg<_Context>& __arg);
578 constexpr bool __is_digit(
char __c)
579 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
581 constexpr bool __is_xdigit(
char __c)
582 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
589 template<
typename _CharT>
590 struct _Spec : _SpecBase
592 unsigned short _M_width;
593 unsigned short _M_prec;
594 char32_t _M_fill =
' ';
598 unsigned _M_localized : 1;
599 unsigned _M_zero_fill : 1;
600 _WidthPrec _M_width_kind : 2;
601 _WidthPrec _M_prec_kind : 2;
602 unsigned _M_debug : 1;
603 _Pres_type _M_type : 4;
604 unsigned _M_reserved : 8;
608 using iterator =
typename basic_string_view<_CharT>::iterator;
610 static constexpr _Align
611 _S_align(_CharT __c)
noexcept
615 case '<':
return _Align_left;
616 case '>':
return _Align_right;
617 case '^':
return _Align_centre;
618 default:
return _Align_default;
624 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
625 {
return _M_parse_fill_and_align(__first, __last,
"{"); }
629 _M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill)
noexcept
631 for (
char __c : __not_fill)
632 if (*__first ==
static_cast<_CharT
>(__c))
635 using namespace __unicode;
636 if constexpr (__literal_encoding_is_unicode<_CharT>())
639 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
642 auto __beg = __uv.begin();
643 char32_t __c = *__beg++;
644 if (__is_scalar_value(__c))
645 if (
auto __next = __beg.base(); __next != __last)
646 if (_Align __align = _S_align(*__next); __align != _Align_default)
654 else if (__last - __first >= 2)
655 if (_Align __align = _S_align(__first[1]); __align != _Align_default)
662 if (_Align __align = _S_align(__first[0]); __align != _Align_default)
671 static constexpr _Sign
672 _S_sign(_CharT __c)
noexcept
676 case '+':
return _Sign_plus;
677 case '-':
return _Sign_minus;
678 case ' ':
return _Sign_space;
679 default:
return _Sign_default;
685 _M_parse_sign(iterator __first, iterator)
noexcept
687 if (_Sign __sign = _S_sign(*__first); __sign != _Sign_default)
697 _M_parse_alternate_form(iterator __first, iterator)
noexcept
709 _M_parse_zero_fill(iterator __first, iterator )
noexcept
720 static constexpr iterator
721 _S_parse_width_or_precision(iterator __first, iterator __last,
722 unsigned short& __val,
bool& __arg_id,
723 basic_format_parse_context<_CharT>& __pc)
725 if (__format::__is_digit(*__first))
727 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
729 __throw_format_error(
"format error: invalid width or precision "
734 else if (*__first ==
'{')
738 if (__first == __last)
739 __format::__unmatched_left_brace_in_format_string();
741 __val = __pc.next_arg_id();
744 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
745 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
746 __format::__invalid_arg_id_in_format_string();
748 __pc.check_arg_id(__v);
751#if __cpp_lib_format >= 202305L
752 __pc.check_dynamic_spec_integral(__val);
761 _M_parse_width(iterator __first, iterator __last,
762 basic_format_parse_context<_CharT>& __pc)
764 bool __arg_id =
false;
766 __throw_format_error(
"format error: width must be non-zero in "
768 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
770 if (__next != __first)
771 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
777 _M_parse_precision(iterator __first, iterator __last,
778 basic_format_parse_context<_CharT>& __pc)
780 if (__first[0] !=
'.')
783 iterator __next = ++__first;
784 bool __arg_id =
false;
785 if (__next != __last)
786 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
788 if (__next == __first)
789 __throw_format_error(
"format error: missing precision after '.' in "
791 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
797 _M_parse_locale(iterator __first, iterator )
noexcept
807 template<
typename _Context>
808 _GLIBCXX_CONSTEXPR_FORMAT
size_t
809 _M_get_width(_Context& __ctx)
const
812 if (_M_width_kind == _WP_value)
814 else if (_M_width_kind == _WP_from_arg)
815 __width = __format::__int_from_arg(__ctx.arg(_M_width));
819 template<
typename _Context>
820 _GLIBCXX_CONSTEXPR_FORMAT
size_t
821 _M_get_precision(_Context& __ctx)
const
824 if (_M_prec_kind == _WP_value)
826 else if (_M_prec_kind == _WP_from_arg)
827 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
832 template<
typename _Int>
833 inline _GLIBCXX_CONSTEXPR_FORMAT
char*
834 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
838 else if (__sign == _Sign_plus)
840 else if (__sign == _Sign_space)
848 template<
typename _Out,
typename _CharT>
849 requires output_iterator<_Out, const _CharT&>
850 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
851 __write(_Out __out, basic_string_view<_CharT> __str)
853 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
859 for (_CharT __c : __str)
866 template<
typename _Out,
typename _CharT>
867 _GLIBCXX_CONSTEXPR_FORMAT _Out
868 __write_padded(_Out __out, basic_string_view<_CharT> __str,
869 _Align __align,
size_t __nfill,
char32_t __fill_char)
871 const size_t __buflen = 0x20;
872 _CharT __padding_chars[__buflen];
873 __padding_chars[0] = _CharT();
874 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
876 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
879 while (__n > __padding.size())
881 __o = __format::__write(
std::move(__o), __padding);
882 __n -= __padding.size();
885 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
888 size_t __l, __r, __max;
889 if (__align == _Align_centre)
892 __r = __l + (__nfill & 1);
895 else if (__align == _Align_right)
908 using namespace __unicode;
909 if constexpr (__literal_encoding_is_unicode<_CharT>())
910 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
913 const char32_t __arr[1]{ __fill_char };
914 _Utf_view<_CharT, span<const char32_t, 1>> __v(__arr);
915 basic_string<_CharT> __padstr(__v.begin(), __v.end());
916 __padding = __padstr;
918 __out = __format::__write(
std::move(__out), __padding);
919 __out = __format::__write(
std::move(__out), __str);
921 __out = __format::__write(
std::move(__out), __padding);
925 if (__max < __buflen)
926 __padding.remove_suffix(__buflen - __max);
930 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
932 __out = __format::__write(
std::move(__out), __str);
940 template<
typename _CharT,
typename _Out>
941 _GLIBCXX_CONSTEXPR_FORMAT _Out
942 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
943 size_t __estimated_width,
944 basic_format_context<_Out, _CharT>& __fc,
945 const _Spec<_CharT>& __spec,
946 _Align __align = _Align_left)
948 size_t __width = __spec._M_get_width(__fc);
950 if (__width <= __estimated_width)
951 return __format::__write(__fc.out(), __str);
953 const size_t __nfill = __width - __estimated_width;
955 if (__spec._M_align != _Align_default)
956 __align = __spec._M_align;
958 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
962 template<
typename _CharT>
963 _GLIBCXX_CONSTEXPR_FORMAT
size_t
964 __truncate(basic_string_view<_CharT>& __s,
size_t __prec)
966 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
968 if (__prec != (
size_t)-1)
969 return __unicode::__truncate(__s, __prec);
971 return __unicode::__field_width(__s);
975 __s = __s.substr(0, __prec);
980 enum class _Term_char :
unsigned char {
985 using enum _Term_char;
987 template<
typename _CharT>
990 using _Str_view = basic_string_view<_CharT>;
994 {
return _GLIBCXX_WIDEN(
"\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
998 {
return _S_all().substr(0, 3); }
1001 _Str_view _S_newline()
1002 {
return _S_all().substr(3, 3); }
1005 _Str_view _S_return()
1006 {
return _S_all().substr(6, 3); }
1009 _Str_view _S_bslash()
1010 {
return _S_all().substr(9, 3); }
1013 _Str_view _S_quote()
1014 {
return _S_all().substr(12, 3); }
1018 {
return _S_all().substr(15, 3); }
1022 {
return _S_all().substr(18, 2); }
1026 {
return _S_all().substr(20, 2); }
1029 _Str_view _S_term(_Term_char __term)
1036 return _S_quote().substr(0, 1);
1038 return _S_apos().substr(0, 1);
1040 __builtin_unreachable();
1044 template<
typename _CharT>
1047 using _Str_view = basic_string_view<_CharT>;
1051 {
return _GLIBCXX_WIDEN(
"[]{}(), : "); }
1054 _Str_view _S_squares()
1055 {
return _S_all().substr(0, 2); }
1058 _Str_view _S_braces()
1059 {
return _S_all().substr(2, 2); }
1062 _Str_view _S_parens()
1063 {
return _S_all().substr(4, 2); }
1066 _Str_view _S_comma()
1067 {
return _S_all().substr(6, 2); }
1070 _Str_view _S_colon()
1071 {
return _S_all().substr(8, 2); }
1074 template<
typename _CharT>
1075 constexpr bool __should_escape_ascii(_CharT __c, _Term_char __term)
1077 using _Esc = _Escapes<_CharT>;
1080 case _Esc::_S_tab()[0]:
1081 case _Esc::_S_newline()[0]:
1082 case _Esc::_S_return()[0]:
1083 case _Esc::_S_bslash()[0]:
1085 case _Esc::_S_quote()[0]:
1086 return __term == _Term_quote;
1087 case _Esc::_S_apos()[0]:
1088 return __term == _Term_apos;
1090 return (__c >= 0 && __c < 0x20) || __c == 0x7f;
1095 constexpr bool __should_escape_unicode(
char32_t __c,
bool __prev_esc)
1097 if (__unicode::__should_escape_category(__c))
1101 return __unicode::__grapheme_cluster_break_property(__c)
1102 == __unicode::_Gcb_property::_Gcb_Extend;
1105 using uint_least32_t = __UINT_LEAST32_TYPE__;
1106 template<
typename _Out,
typename _CharT>
1107 _GLIBCXX_CONSTEXPR_FORMAT _Out
1108 __write_escape_seq(_Out __out, uint_least32_t __val,
1109 basic_string_view<_CharT> __prefix)
1111 constexpr size_t __max = 8;
1113 const string_view __narrow(
1115 std::__to_chars_i<uint_least32_t>(__buf, __buf + __max, __val, 16).ptr);
1117 __out = __format::__write(__out, __prefix);
1118 *__out = _Separators<_CharT>::_S_braces()[0];
1120 if constexpr (is_same_v<char, _CharT>)
1121 __out = __format::__write(__out, __narrow);
1122#ifdef _GLIBCXX_USE_WCHAR_T
1125 wchar_t __wbuf[__max];
1126 const size_t __n = __narrow.size();
1127 std::__to_wstring_numeric(__narrow.data(), __n, __wbuf);
1128 __out = __format::__write(__out, wstring_view(__wbuf, __n));
1131 *__out = _Separators<_CharT>::_S_braces()[1];
1135 template<
typename _Out,
typename _CharT>
1136 _GLIBCXX_CONSTEXPR_FORMAT _Out
1137 __write_escape_seqs(_Out __out, basic_string_view<_CharT> __units)
1139 using _UChar = make_unsigned_t<_CharT>;
1140 for (_CharT __c : __units)
1141 __out = __format::__write_escape_seq(
1142 __out,
static_cast<_UChar
>(__c), _Escapes<_CharT>::_S_x());
1146 template<
typename _Out,
typename _CharT>
1147 _GLIBCXX_CONSTEXPR_FORMAT _Out
1148 __write_escaped_char(_Out __out, _CharT __c)
1150 using _UChar = make_unsigned_t<_CharT>;
1151 using _Esc = _Escapes<_CharT>;
1154 case _Esc::_S_tab()[0]:
1155 return __format::__write(__out, _Esc::_S_tab().substr(1, 2));
1156 case _Esc::_S_newline()[0]:
1157 return __format::__write(__out, _Esc::_S_newline().substr(1, 2));
1158 case _Esc::_S_return()[0]:
1159 return __format::__write(__out, _Esc::_S_return().substr(1, 2));
1160 case _Esc::_S_bslash()[0]:
1161 return __format::__write(__out, _Esc::_S_bslash().substr(1, 2));
1162 case _Esc::_S_quote()[0]:
1163 return __format::__write(__out, _Esc::_S_quote().substr(1, 2));
1164 case _Esc::_S_apos()[0]:
1165 return __format::__write(__out, _Esc::_S_apos().substr(1, 2));
1167 return __format::__write_escape_seq(
1168 __out,
static_cast<_UChar
>(__c), _Esc::_S_u());
1172 template<
typename _CharT,
typename _Out>
1173 _GLIBCXX_CONSTEXPR_FORMAT _Out
1174 __write_escaped_ascii(_Out __out,
1175 basic_string_view<_CharT> __str,
1178 using _Str_view = basic_string_view<_CharT>;
1182 constexpr _Str_view __supported(_GLIBCXX_WIDEN(
1183 "ABCDEFGHIJKLMNOPQRSTUWXYZ"
1184 "abdeefghijklmnopqrstuwzyz"
1185 " !#$%&'()*+-./:;<=>?[]^_{|}~"
1186 "0123456789" "\t\n\r\\\"\'\0"
1188 if (__str.find_first_not_of(__supported) != _Str_view::npos)
1189#if __has_builtin(__builtin_constexpr_diag)
1190 __builtin_constexpr_diag (2,
"",
1191 "for non-Unicode literal encodings, only"
1192 " printable ASCII characters and standard"
1193 " escape sequencess can be escaped in constant"
1200 auto __first = __str.begin();
1201 auto const __last = __str.end();
1202 while (__first != __last)
1204 auto __print = __first;
1206 while (__print != __last
1207 && !__format::__should_escape_ascii(*__print, __term))
1210 if (__print != __first)
1211 __out = __format::__write(__out, _Str_view(__first, __print));
1213 if (__print == __last)
1217 __out = __format::__write_escaped_char(__out, *__first);
1223 template<
typename _CharT,
typename _Out>
1224 _GLIBCXX_CONSTEXPR_FORMAT _Out
1225 __write_escaped_unicode_part(_Out __out, basic_string_view<_CharT>& __str,
1226 bool& __prev_esc, _Term_char __term)
1228 using _Str_view = basic_string_view<_CharT>;
1229 using _Esc = _Escapes<_CharT>;
1231 static constexpr char32_t __replace = U
'\uFFFD';
1232 static constexpr _Str_view __replace_rep = []
1235 if constexpr (is_same_v<char, _CharT>)
1236 return "\xEF\xBF\xBD";
1241 __unicode::_Utf_view<char32_t, _Str_view> __v(
std::move(__str));
1244 auto __first = __v.begin();
1245 auto const __last = __v.end();
1246 while (__first != __last)
1248 bool __esc_ascii =
false;
1249 bool __esc_unicode =
false;
1250 bool __esc_replace =
false;
1251 auto __should_escape = [&](
auto const& __it)
1255 = __format::__should_escape_ascii(*__it.base(), __term);
1256 if (__format::__should_escape_unicode(*__it, __prev_esc))
1257 return __esc_unicode =
true;
1258 if (*__it == __replace)
1260 _Str_view __units(__it.base(), __it._M_units());
1261 return __esc_replace = (__units != __replace_rep);
1266 auto __print = __first;
1267 while (__print != __last && !__should_escape(__print))
1273 if (__print != __first)
1274 __out = __format::__write(__out, _Str_view(__first.base(), __print.base()));
1276 if (__print == __last)
1281 __out = __format::__write_escaped_char(__out, *__first.base());
1282 else if (__esc_unicode)
1283 __out = __format::__write_escape_seq(__out, *__first, _Esc::_S_u());
1285 else if (_Str_view __units(__first.base(), __first._M_units());
1286 __units.end() != __last.base())
1287 __out = __format::__write_escape_seqs(__out, __units);
1301 template<
typename _CharT,
typename _Out>
1302 _GLIBCXX_CONSTEXPR_FORMAT _Out
1303 __write_escaped_unicode(_Out __out, basic_string_view<_CharT> __str,
1306 bool __prev_escape =
true;
1307 __out = __format::__write_escaped_unicode_part(__out, __str,
1308 __prev_escape, __term);
1309 __out = __format::__write_escape_seqs(__out, __str);
1313 template<
typename _CharT,
typename _Out>
1314 _GLIBCXX_CONSTEXPR_FORMAT _Out
1315 __write_escaped(_Out __out, basic_string_view<_CharT> __str, _Term_char __term)
1317 __out = __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1319 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1320 __out = __format::__write_escaped_unicode(__out, __str, __term);
1321 else if constexpr (is_same_v<char, _CharT>
1322 && __unicode::__literal_encoding_is_extended_ascii())
1323 __out = __format::__write_escaped_ascii(__out, __str, __term);
1326 __out = __format::__write_escaped_ascii(__out, __str, __term);
1328 return __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1332 struct _Optional_locale
1334 [[__gnu__::__always_inline__]]
1335 _GLIBCXX_CONSTEXPR_FORMAT
1336 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
1338 _Optional_locale(
const locale& __loc) noexcept
1339 : _M_loc(__loc), _M_hasval(
true)
1342 _GLIBCXX_CONSTEXPR_FORMAT
1343 _Optional_locale(
const _Optional_locale& __l) noexcept
1344 : _M_dummy(), _M_hasval(__l._M_hasval)
1347 std::construct_at(&_M_loc, __l._M_loc);
1350 _GLIBCXX_CONSTEXPR_FORMAT
1352 operator=(
const _Optional_locale& __l)
noexcept
1357 _M_loc = __l._M_loc;
1364 else if (__l._M_hasval)
1366 std::construct_at(&_M_loc, __l._M_loc);
1372 _GLIBCXX_CONSTEXPR_FORMAT
1373 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
1376 operator=(locale&& __loc)
noexcept
1382 std::construct_at(&_M_loc,
std::move(__loc));
1393 std::construct_at(&_M_loc);
1399 _GLIBCXX_CONSTEXPR_FORMAT
bool
1400 has_value() const noexcept {
return _M_hasval; }
1403 char _M_dummy =
'\0';
1406 bool _M_hasval =
false;
1409 template<__
char _CharT>
1410 struct __formatter_str
1412 __formatter_str() =
default;
1415 __formatter_str(_Spec<_CharT> __spec) noexcept
1419 constexpr typename basic_format_parse_context<_CharT>::iterator
1420 parse(basic_format_parse_context<_CharT>& __pc)
1422 auto __first = __pc.begin();
1423 const auto __last = __pc.end();
1424 _Spec<_CharT> __spec{};
1426 auto __finalize = [
this, &__spec] {
1430 auto __finished = [&] {
1431 if (__first == __last || *__first ==
'}')
1442 __first = __spec._M_parse_fill_and_align(__first, __last);
1446 __first = __spec._M_parse_width(__first, __last, __pc);
1450 __first = __spec._M_parse_precision(__first, __last, __pc);
1454 if (*__first ==
's')
1456 __spec._M_type = _Pres_s;
1459#if __glibcxx_format_ranges
1460 else if (*__first ==
'?')
1462 __spec._M_debug =
true;
1470 __format::__failed_to_parse_format_spec();
1473 template<
typename _Out>
1474 _GLIBCXX_CONSTEXPR_FORMAT _Out
1475 format(basic_string_view<_CharT> __s,
1476 basic_format_context<_Out, _CharT>& __fc)
const
1478 if (_M_spec._M_debug)
1479 return _M_format_escaped(__s, __fc);
1481 if (_M_spec._M_width_kind == _WP_none
1482 && _M_spec._M_prec_kind == _WP_none)
1483 return __format::__write(__fc.out(), __s);
1485 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1486 const size_t __width = __format::__truncate(__s, __maxwidth);
1487 return __format::__write_padded_as_spec(__s, __width, __fc, _M_spec);
1490 template<
typename _Out>
1491 _GLIBCXX_CONSTEXPR_FORMAT _Out
1492 _M_format_escaped(basic_string_view<_CharT> __s,
1493 basic_format_context<_Out, _CharT>& __fc)
const
1495 const size_t __padwidth = _M_spec._M_get_width(__fc);
1496 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1497 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1499 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1500 const size_t __width = __truncate(__s, __maxwidth);
1502 if (__padwidth <= __width && _M_spec._M_prec_kind == _WP_none)
1503 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1508 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth, __maxwidth);
1509 __format::__write_escaped(__sink.out(), __s, _Term_quote);
1510 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1513#if __glibcxx_format_ranges
1514 template<ranges::input_range _Rg,
typename _Out>
1515 requires same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _CharT>
1516 _GLIBCXX_CONSTEXPR_FORMAT _Out
1517 _M_format_range(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
1519 using _Range = remove_reference_t<_Rg>;
1520 using _String_view = basic_string_view<_CharT>;
1521 if constexpr (ranges::contiguous_range<_Rg>)
1523 _String_view __str(ranges::data(__rg),
1524 size_t(ranges::distance(__rg)));
1525 return format(__str, __fc);
1527 else if constexpr (!is_const_v<_Range>
1528 && __simply_formattable_range<_Range, _CharT>)
1529 return _M_format_range<const _Range&>(__rg, __fc);
1530 else if constexpr (!is_lvalue_reference_v<_Rg>)
1531 return _M_format_range<_Range&>(__rg, __fc);
1534 auto __handle_debug = [
this, &__rg]<
typename _NOut>(_NOut __nout)
1536 if (!_M_spec._M_debug)
1537 return ranges::copy(__rg,
std::move(__nout)).out;
1539 _Escaping_sink<_NOut, _CharT>
1541 ranges::copy(__rg, __sink.out());
1542 return __sink._M_finish();
1545 const size_t __padwidth = _M_spec._M_get_width(__fc);
1546 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1547 return __handle_debug(__fc.out());
1549 _Padding_sink<_Out, _CharT>
1550 __sink(__fc.out(), __padwidth, _M_spec._M_get_precision(__fc));
1551 __handle_debug(__sink.out());
1552 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1557 set_debug_format() noexcept
1558 { _M_spec._M_debug =
true; }
1562 _Spec<_CharT> _M_spec{};
1567 [[__gnu__::__always_inline__]]
1569 __toupper_numeric(
char __c)
1573 case 'a':
return 'A';
1574 case 'b':
return 'B';
1575 case 'c':
return 'C';
1576 case 'd':
return 'D';
1577 case 'e':
return 'E';
1578 case 'f':
return 'F';
1579 case 'i':
return 'I';
1580 case 'n':
return 'N';
1581 case 'p':
return 'P';
1582 case 'x':
return 'X';
1583 default:
return __c;
1587 template<__
char _CharT>
1588 struct __formatter_int
1592 static constexpr _Pres_type _AsInteger = _Pres_d;
1593 static constexpr _Pres_type _AsBool = _Pres_s;
1594 static constexpr _Pres_type _AsChar = _Pres_c;
1596 __formatter_int() =
default;
1599 __formatter_int(_Spec<_CharT> __spec) noexcept
1602 if (_M_spec._M_type == _Pres_none)
1603 _M_spec._M_type = _Pres_d;
1606 constexpr typename basic_format_parse_context<_CharT>::iterator
1607 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1609 _Spec<_CharT> __spec{};
1610 __spec._M_type = __type;
1612 const auto __last = __pc.end();
1613 auto __first = __pc.begin();
1615 auto __finalize = [
this, &__spec] {
1619 auto __finished = [&] {
1620 if (__first == __last || *__first ==
'}')
1631 __first = __spec._M_parse_fill_and_align(__first, __last);
1635 __first = __spec._M_parse_sign(__first, __last);
1639 __first = __spec._M_parse_alternate_form(__first, __last);
1643 __first = __spec._M_parse_zero_fill(__first, __last);
1647 __first = __spec._M_parse_width(__first, __last, __pc);
1651 __first = __spec._M_parse_locale(__first, __last);
1658 __spec._M_type = _Pres_b;
1662 __spec._M_type = _Pres_B;
1668 if (__type != _AsBool)
1670 __spec._M_type = _Pres_c;
1675 __spec._M_type = _Pres_d;
1679 __spec._M_type = _Pres_o;
1683 __spec._M_type = _Pres_x;
1687 __spec._M_type = _Pres_X;
1691 if (__type == _AsBool)
1693 __spec._M_type = _Pres_s;
1697#if __glibcxx_format_ranges
1699 if (__type == _AsChar)
1701 __spec._M_debug =
true;
1711 __format::__failed_to_parse_format_spec();
1714 template<
typename _Tp>
1715 constexpr typename basic_format_parse_context<_CharT>::iterator
1716 _M_parse(basic_format_parse_context<_CharT>& __pc)
1718 if constexpr (is_same_v<_Tp, bool>)
1720 auto __end = _M_do_parse(__pc, _AsBool);
1721 if (_M_spec._M_type == _Pres_s)
1722 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1723 || _M_spec._M_zero_fill)
1724 __throw_format_error(
"format error: format-spec contains "
1725 "invalid formatting options for "
1729 else if constexpr (__char<_Tp>)
1731 auto __end = _M_do_parse(__pc, _AsChar);
1732 if (_M_spec._M_type == _Pres_c)
1733 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1734 || _M_spec._M_zero_fill
1736 __throw_format_error(
"format error: format-spec contains "
1737 "invalid formatting options for "
1742 return _M_do_parse(__pc, _AsInteger);
1745 template<
typename _Int,
typename _Out>
1746 _GLIBCXX_CONSTEXPR_FORMAT
1747 typename basic_format_context<_Out, _CharT>::iterator
1748 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1750 if (_M_spec._M_type == _Pres_c)
1751 return _M_format_character(_S_to_character(__i), __fc);
1753 constexpr size_t __buf_size =
sizeof(_Int) * __CHAR_BIT__ + 3;
1754 char __buf[__buf_size];
1755 to_chars_result __res{};
1757 string_view __base_prefix;
1758 make_unsigned_t<_Int> __u;
1760 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1764 char* __start = __buf + 3;
1765 char*
const __end = __buf +
sizeof(__buf);
1766 char*
const __start_digits = __start;
1768 switch (_M_spec._M_type)
1772 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1773 __res = to_chars(__start, __end, __u, 2);
1777 return _M_format_character(_S_to_character(__i), __fc);
1784 __res = to_chars(__start, __end, __u, 10);
1788 __base_prefix =
"0";
1789 __res = to_chars(__start, __end, __u, 8);
1793 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1794 __res = to_chars(__start, __end, __u, 16);
1795 if (_M_spec._M_type == _Pres_X)
1796 for (
auto __p = __start; __p != __res.ptr; ++__p)
1797 *__p = __format::__toupper_numeric(*__p);
1801 if (_M_spec._M_alt && __base_prefix.size())
1803 __start -= __base_prefix.size();
1804 ranges::copy(__base_prefix, __start);
1806 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1808 string_view __narrow_str(__start, __res.ptr - __start);
1809 size_t __prefix_len = __start_digits - __start;
1810 if constexpr (is_same_v<char, _CharT>)
1811 return _M_format_int(__narrow_str, __prefix_len, __fc);
1812#ifdef _GLIBCXX_USE_WCHAR_T
1815 _CharT __wbuf[__buf_size];
1816 size_t __n = __narrow_str.size();
1819 std::__to_wstring_numeric(__narrow_str.data(), __n, __wbuf);
1820 return _M_format_int(basic_string_view<_CharT>(__wbuf, __n),
1821 __prefix_len, __fc);
1826 template<
typename _Out>
1827 _GLIBCXX_CONSTEXPR_FORMAT
1828 typename basic_format_context<_Out, _CharT>::iterator
1829 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1831 if (_M_spec._M_type == _Pres_c)
1832 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1833 if (_M_spec._M_type != _Pres_s)
1834 return format(
static_cast<unsigned char>(__i), __fc);
1836 basic_string<_CharT> __s;
1838 if (_M_spec._M_localized) [[unlikely]]
1841 __s = __i ? __np.truename() : __np.falsename();
1842 __est_width = __s.size();
1846 if constexpr (is_same_v<char, _CharT>)
1847 __s = __i ?
"true" :
"false";
1849 __s = __i ? L
"true" : L
"false";
1850 __est_width = __s.size();
1853 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1857 template<
typename _Out>
1858 _GLIBCXX_CONSTEXPR_FORMAT
1859 typename basic_format_context<_Out, _CharT>::iterator
1860 _M_format_character(_CharT __c,
1861 basic_format_context<_Out, _CharT>& __fc)
const
1863 basic_string_view<_CharT> __in(&__c, 1u);
1864 size_t __width = 1u;
1866 if constexpr (
sizeof(_CharT) > 1u &&
1867 __unicode::__literal_encoding_is_unicode<_CharT>())
1868 __width = __unicode::__field_width(__c);
1870 if (!_M_spec._M_debug)
1871 return __format::__write_padded_as_spec(__in, __width,
1875 if (_M_spec._M_get_width(__fc) <= __width)
1876 return __format::__write_escaped(__fc.out(), __in, _Term_apos);
1879 _Fixedbuf_sink<_CharT> __sink(__buf);
1880 __format::__write_escaped(__sink.out(), __in, _Term_apos);
1882 __in = __sink.view();
1883 if (__in[1] == _Escapes<_CharT>::_S_bslash()[0])
1884 __width = __in.size();
1885 return __format::__write_padded_as_spec(__in, __width,
1889 template<
typename _Int>
1890 static _GLIBCXX_CONSTEXPR_FORMAT _CharT
1891 _S_to_character(_Int __i)
1894 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1896 if (_Traits::__min <= __i && __i <= _Traits::__max)
1897 return static_cast<_CharT
>(__i);
1899 else if constexpr (is_signed_v<_Int>)
1901 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1902 return static_cast<_CharT
>(__i);
1904 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1905 return static_cast<_CharT
>(__i);
1906 __throw_format_error(
"format error: integer not representable as "
1910 template<
typename _Out>
1911 _GLIBCXX_CONSTEXPR_FORMAT
1912 typename basic_format_context<_Out, _CharT>::iterator
1913 _M_format_int(basic_string_view<_CharT> __str,
size_t __prefix_len,
1914 basic_format_context<_Out, _CharT>& __fc)
const
1916 size_t __width = _M_spec._M_get_width(__fc);
1917 if (_M_spec._M_localized)
1919 const auto& __l = __fc.locale();
1920 if (__l.name() !=
"C")
1922 auto& __np = use_facet<numpunct<_CharT>>(__l);
1923 string __grp = __np.grouping();
1926 size_t __n = __str.size() - __prefix_len;
1927 auto __p = (_CharT*)__builtin_alloca(2 * __n
1930 auto __s = __str.data();
1931 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1932 __s += __prefix_len;
1933 auto __end = std::__add_grouping(__p + __prefix_len,
1934 __np.thousands_sep(),
1938 __str = {__p, size_t(__end - __p)};
1943 if (__width <= __str.size())
1944 return __format::__write(__fc.out(), __str);
1946 char32_t __fill_char = _M_spec._M_fill;
1947 _Align __align = _M_spec._M_align;
1949 size_t __nfill = __width - __str.size();
1950 auto __out = __fc.out();
1951 if (__align == _Align_default)
1953 __align = _Align_right;
1954 if (_M_spec._M_zero_fill)
1956 __fill_char = _CharT(
'0');
1958 if (__prefix_len != 0)
1960 __out = __format::__write(
std::move(__out),
1961 __str.substr(0, __prefix_len));
1962 __str.remove_prefix(__prefix_len);
1966 __fill_char = _CharT(
' ');
1968 return __format::__write_padded(
std::move(__out), __str,
1969 __align, __nfill, __fill_char);
1972 _Spec<_CharT> _M_spec{};
1975#ifdef __BFLT16_DIG__
1976 using __bflt16_t =
decltype(0.0bf16);
1987#undef _GLIBCXX_FORMAT_F128
1989#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1992 using __flt128_t = __ieee128;
1993# define _GLIBCXX_FORMAT_F128 1
1995#ifdef __LONG_DOUBLE_IEEE128__
1999 to_chars(
char*,
char*, __ibm128)
noexcept
2000 __asm(
"_ZSt8to_charsPcS_e");
2003 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
2004 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
2007 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
2008 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
2009#elif __cplusplus == 202002L
2011 to_chars(
char*,
char*, __ieee128)
noexcept
2012 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
2015 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
2016 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
2019 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
2020 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
2023#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
2026 using __flt128_t =
long double;
2027# define _GLIBCXX_FORMAT_F128 2
2029#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2032 using __flt128_t = _Float128;
2033# define _GLIBCXX_FORMAT_F128 3
2035# if __cplusplus == 202002L
2039 to_chars(
char*,
char*, _Float128)
noexcept
2040# if _GLIBCXX_INLINE_VERSION
2041 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
2043 __asm(
"_ZSt8to_charsPcS_DF128_");
2047 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
2048# if _GLIBCXX_INLINE_VERSION
2049 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
2051 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
2055 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
2056# if _GLIBCXX_INLINE_VERSION
2057 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
2059 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
2064 using std::to_chars;
2067 template<
typename _Tp>
2068 concept __formattable_float
2069 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
2070 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
2072 template<__
char _CharT>
2073 struct __formatter_fp
2075 constexpr typename basic_format_parse_context<_CharT>::iterator
2076 parse(basic_format_parse_context<_CharT>& __pc)
2078 _Spec<_CharT> __spec{};
2079 const auto __last = __pc.end();
2080 auto __first = __pc.begin();
2082 auto __finalize = [
this, &__spec] {
2086 auto __finished = [&] {
2087 if (__first == __last || *__first ==
'}')
2098 __first = __spec._M_parse_fill_and_align(__first, __last);
2102 __first = __spec._M_parse_sign(__first, __last);
2106 __first = __spec._M_parse_alternate_form(__first, __last);
2110 __first = __spec._M_parse_zero_fill(__first, __last);
2114 if (__first[0] !=
'.')
2116 __first = __spec._M_parse_width(__first, __last, __pc);
2121 __first = __spec._M_parse_precision(__first, __last, __pc);
2125 __first = __spec._M_parse_locale(__first, __last);
2132 __spec._M_type = _Pres_a;
2136 __spec._M_type = _Pres_A;
2140 __spec._M_type = _Pres_e;
2144 __spec._M_type = _Pres_E;
2148 __spec._M_type = _Pres_f;
2152 __spec._M_type = _Pres_F;
2156 __spec._M_type = _Pres_g;
2160 __spec._M_type = _Pres_G;
2168 __format::__failed_to_parse_format_spec();
2171 template<
typename _Fp,
typename _Out>
2172 typename basic_format_context<_Out, _CharT>::iterator
2173 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2177 to_chars_result __res{};
2180 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2182 __prec = _M_spec._M_get_precision(__fc);
2185 bool __upper =
false;
2186 bool __trailing_zeros =
false;
2188 size_t __offset = 1;
2190 switch (_M_spec._M_type)
2193 if (__builtin_isfinite(__v))
2199 __fmt = chars_format::hex;
2202 if (__builtin_isfinite(__v))
2207 __fmt = chars_format::hex;
2215 __fmt = chars_format::scientific;
2222 __fmt = chars_format::fixed;
2229 __trailing_zeros =
true;
2231 __fmt = chars_format::general;
2236 __fmt = chars_format::general;
2240 char* __start = __buf + __offset;
2241 char* __end = __buf +
sizeof(__buf);
2244 auto __to_chars = [&](
char* __b,
char* __e) {
2246 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2247 else if (__fmt != chars_format{})
2248 return __format::to_chars(__b, __e, __v, __fmt);
2250 return __format::to_chars(__b, __e, __v);
2254 __res = __to_chars(__start, __end);
2256 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2260 size_t __guess = 7 + __offset + __prec;
2261 if (__fmt == chars_format::fixed)
2263 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2264 || is_same_v<_Fp, long double>)
2269 if constexpr (is_same_v<_Fp, float>)
2270 __builtin_frexpf(__v, &__exp);
2271 else if constexpr (is_same_v<_Fp, double>)
2272 __builtin_frexp(__v, &__exp);
2273 else if constexpr (is_same_v<_Fp, long double>)
2274 __builtin_frexpl(__v, &__exp);
2276 __guess += 1U + __exp * 4004U / 13301U;
2279 __guess += numeric_limits<_Fp>::max_exponent10;
2281 if (__guess <=
sizeof(__buf)) [[unlikely]]
2282 __guess =
sizeof(__buf) * 2;
2291 auto __overwrite = [&__to_chars, &__res, __offset] (
char* __p,
size_t __n)
2293 __res = __to_chars(__p + __offset, __p + __n - __offset);
2294 return __res.ec == errc{} ? __res.ptr - __p : 0;
2299 __start = __dynbuf.
data() + __offset;
2300 __end = __dynbuf.
data() + __dynbuf.
size();
2302 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2308 if (__builtin_signbit(__v))
2309 ranges::copy(string_view(
"-0x"), __start);
2311 ranges::copy(string_view(
"0x"), __start);
2317 for (
char* __p = __start; __p != __res.ptr; ++__p)
2318 *__p = __format::__toupper_numeric(*__p);
2322 if (!__builtin_signbit(__v))
2324 if (_M_spec._M_sign == _Sign_plus)
2326 else if (_M_spec._M_sign == _Sign_space)
2332 string_view __narrow_str(__start, __res.ptr - __start);
2336 if (_M_spec._M_alt && __builtin_isfinite(__v))
2338 string_view __s = __narrow_str;
2342 size_t __d = __s.find(
'.');
2343 if (__d != __s.npos)
2345 __p = __s.find(__expc, __d + 1);
2346 if (__p == __s.npos)
2350 if (__trailing_zeros)
2353 if (__s[__offset] !=
'0')
2355 __sigfigs = __p - __offset - 1;
2360 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2365 __p = __s.find(__expc);
2366 if (__p == __s.npos)
2369 __sigfigs = __d - __offset;
2372 if (__trailing_zeros && __prec != 0)
2377 __z = __prec - __sigfigs;
2380 if (
size_t __extras =
int(__d == __p) + __z)
2382 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2386 __builtin_memmove(__start + __p + __extras,
2390 __start[__p++] =
'.';
2391 __builtin_memset(__start + __p,
'0', __z);
2392 __narrow_str = {__s.data(), __s.size() + __extras};
2396 __dynbuf.
reserve(__s.size() + __extras);
2397 if (__dynbuf.
empty())
2399 __dynbuf = __s.
substr(0, __p);
2403 __dynbuf.
append(__z,
'0');
2404 __dynbuf.
append(__s.substr(__p));
2408 __dynbuf.
insert(__p, __extras,
'0');
2410 __dynbuf[__p] =
'.';
2412 __narrow_str = __dynbuf;
2417 basic_string<_CharT> __wstr;
2418 basic_string_view<_CharT> __str;
2419 if constexpr (is_same_v<_CharT, char>)
2420 __str = __narrow_str;
2421#ifdef _GLIBCXX_USE_WCHAR_T
2426 __wstr = std::__to_wstring_numeric(__narrow_str);
2431 if (_M_spec._M_localized && __builtin_isfinite(__v))
2433 auto __s = _M_localize(__str, __expc, __offset, __fc.locale());
2438 size_t __width = _M_spec._M_get_width(__fc);
2440 if (__width <= __str.size())
2441 return __format::__write(__fc.out(), __str);
2443 char32_t __fill_char = _M_spec._M_fill;
2444 _Align __align = _M_spec._M_align;
2446 size_t __nfill = __width - __str.size();
2447 auto __out = __fc.out();
2448 if (__align == _Align_default)
2450 __align = _Align_right;
2451 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2453 __fill_char = _CharT(
'0');
2456 __out = __format::__write(__out, __str.substr(0, __offset));
2457 __str.remove_prefix(__offset);
2461 __fill_char = _CharT(
' ');
2463 return __format::__write_padded(
std::move(__out), __str,
2464 __align, __nfill, __fill_char);
2468 basic_string<_CharT>
2469 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2470 int __offset,
const locale& __loc)
const
2472 basic_string<_CharT> __lstr;
2474 if (__loc == locale::classic())
2477 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2478 const _CharT __point = __np.decimal_point();
2479 const string __grp = __np.grouping();
2481 _CharT __dot, __exp;
2482 if constexpr (is_same_v<_CharT, char>)
2505 __builtin_unreachable();
2509 if (__grp.empty() && __point == __dot)
2512 size_t __d = __str.find(__dot);
2513 size_t __e = min(__d, __str.find(__exp));
2514 if (__e == __str.npos)
2516 const size_t __r = __str.size() - __e;
2517 auto __overwrite = [&](_CharT* __p, size_t) {
2519 ranges::copy_n(__str.data(), __offset, __p);
2521 auto __end = std::__add_grouping(__p + __offset, __np.thousands_sep(),
2522 __grp.data(), __grp.size(),
2523 __str.data() + __offset,
2524 __str.data() + __e);
2527 if (__d != __str.npos)
2533 const size_t __rlen = __str.size() - __e;
2535 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2538 return (__end - __p);
2540 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2544 _Spec<_CharT> _M_spec{};
2547 template<__format::__
char _CharT>
2548 struct __formatter_ptr
2551 __formatter_ptr() noexcept
2554 _M_spec._M_type = _Pres_x;
2555 _M_spec._M_alt =
true;
2559 __formatter_ptr(_Spec<_CharT> __spec) noexcept
2561 { _M_set_default(); }
2563 constexpr typename basic_format_parse_context<_CharT>::iterator
2564 parse(basic_format_parse_context<_CharT>& __pc)
2566 __format::_Spec<_CharT> __spec{};
2567 const auto __last = __pc.end();
2568 auto __first = __pc.begin();
2570 auto __finalize = [
this, &__spec] {
2575 auto __finished = [&] {
2576 if (__first == __last || *__first ==
'}')
2587 __first = __spec._M_parse_fill_and_align(__first, __last);
2593#if __glibcxx_format >= 202304L
2594 __first = __spec._M_parse_zero_fill(__first, __last);
2599 __first = __spec._M_parse_width(__first, __last, __pc);
2603 if (*__first ==
'p')
2605 __spec._M_type = _Pres_x;
2606 __spec._M_alt =
true;
2609#if __glibcxx_format >= 202304L
2610 else if (*__first ==
'P')
2612 __spec._M_type = _Pres_X;
2613 __spec._M_alt =
true;
2621 __format::__failed_to_parse_format_spec();
2624 template<
typename _Out>
2625 _GLIBCXX_CONSTEXPR_FORMAT
2626 typename basic_format_context<_Out, _CharT>::iterator
2627 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2630 ?
reinterpret_cast<__UINTPTR_TYPE__
>(__v)
2631 : static_cast<__UINTPTR_TYPE__>(0);
2632 return __formatter_int<_CharT>(_M_spec).format(__u, __fc);
2636 [[__gnu__::__always_inline__]]
2640 if (_M_spec._M_type == _Pres_none)
2642 _M_spec._M_type = _Pres_x;
2643 _M_spec._M_alt =
true;
2647 __format::_Spec<_CharT> _M_spec;
2654 template<__format::__
char _CharT>
2655 struct formatter<_CharT, _CharT>
2657 formatter() =
default;
2659 constexpr typename basic_format_parse_context<_CharT>::iterator
2660 parse(basic_format_parse_context<_CharT>& __pc)
2662 return _M_f.template _M_parse<_CharT>(__pc);
2665 template<
typename _Out>
2666 _GLIBCXX_CONSTEXPR_FORMAT
2667 typename basic_format_context<_Out, _CharT>::iterator
2668 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2670 if (_M_f._M_spec._M_type == __format::_Pres_c)
2671 return _M_f._M_format_character(__u, __fc);
2673 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2676#if __glibcxx_format_ranges
2678 set_debug_format() noexcept
2679 { _M_f._M_spec._M_debug =
true; }
2683 __format::__formatter_int<_CharT> _M_f;
2686#if __glibcxx_print >= 202403L
2687 template<__format::__
char _CharT>
2688 constexpr bool enable_nonlocking_formatter_optimization<_CharT> =
true;
2691#ifdef _GLIBCXX_USE_WCHAR_T
2694 struct formatter<char, wchar_t>
2696 formatter() =
default;
2698 constexpr typename basic_format_parse_context<wchar_t>::iterator
2699 parse(basic_format_parse_context<wchar_t>& __pc)
2701 return _M_f._M_parse<
char>(__pc);
2704 template<
typename _Out>
2705 _GLIBCXX_CONSTEXPR_FORMAT
2706 typename basic_format_context<_Out, wchar_t>::iterator
2707 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2709 if (_M_f._M_spec._M_type == __format::_Pres_c)
2710 return _M_f._M_format_character(__u, __fc);
2712 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2715#if __glibcxx_format_ranges
2717 set_debug_format() noexcept
2718 { _M_f._M_spec._M_debug =
true; }
2722 __format::__formatter_int<wchar_t> _M_f;
2729 template<__format::__
char _CharT>
2730 struct formatter<_CharT*, _CharT>
2732 formatter() =
default;
2734 [[__gnu__::__always_inline__]]
2735 constexpr typename basic_format_parse_context<_CharT>::iterator
2736 parse(basic_format_parse_context<_CharT>& __pc)
2737 {
return _M_f.parse(__pc); }
2739 template<
typename _Out>
2740 [[__gnu__::__nonnull__]]
2741 _GLIBCXX_CONSTEXPR_FORMAT
2742 typename basic_format_context<_Out, _CharT>::iterator
2743 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2744 {
return _M_f.format(__u, __fc); }
2746#if __glibcxx_format_ranges
2747 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2751 __format::__formatter_str<_CharT> _M_f;
2754#if __glibcxx_print >= 202403L
2755 template<__format::__
char _CharT>
2756 constexpr bool enable_nonlocking_formatter_optimization<_CharT*> =
true;
2759 template<__format::__
char _CharT>
2760 struct formatter<const _CharT*, _CharT>
2762 formatter() =
default;
2764 [[__gnu__::__always_inline__]]
2765 constexpr typename basic_format_parse_context<_CharT>::iterator
2766 parse(basic_format_parse_context<_CharT>& __pc)
2767 {
return _M_f.parse(__pc); }
2769 template<
typename _Out>
2770 [[__gnu__::__nonnull__]]
2771 _GLIBCXX_CONSTEXPR_FORMAT
2772 typename basic_format_context<_Out, _CharT>::iterator
2773 format(
const _CharT* __u,
2774 basic_format_context<_Out, _CharT>& __fc)
const
2775 {
return _M_f.format(__u, __fc); }
2777#if __glibcxx_format_ranges
2778 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2782 __format::__formatter_str<_CharT> _M_f;
2785#if __glibcxx_print >= 202403L
2786 template<__format::__
char _CharT>
2788 enable_nonlocking_formatter_optimization<const _CharT*> =
true;
2791 template<__format::__
char _CharT,
size_t _Nm>
2792 struct formatter<_CharT[_Nm], _CharT>
2794 formatter() =
default;
2796 [[__gnu__::__always_inline__]]
2797 constexpr typename basic_format_parse_context<_CharT>::iterator
2798 parse(basic_format_parse_context<_CharT>& __pc)
2799 {
return _M_f.parse(__pc); }
2801 template<
typename _Out>
2802 _GLIBCXX_CONSTEXPR_FORMAT
2803 typename basic_format_context<_Out, _CharT>::iterator
2804 format(
const _CharT (&__u)[_Nm],
2805 basic_format_context<_Out, _CharT>& __fc)
const
2806 {
return _M_f.format({__u, _Nm}, __fc); }
2808#if __glibcxx_format_ranges
2809 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2813 __format::__formatter_str<_CharT> _M_f;
2816#if __glibcxx_print >= 202403L
2817 template<__format::__
char _CharT,
size_t _Nm>
2818 constexpr bool enable_nonlocking_formatter_optimization<_CharT[_Nm]> =
true;
2821 template<
typename _Traits,
typename _Alloc>
2822 struct formatter<basic_string<char, _Traits, _Alloc>, char>
2824 formatter() =
default;
2826 [[__gnu__::__always_inline__]]
2827 constexpr typename basic_format_parse_context<char>::iterator
2828 parse(basic_format_parse_context<char>& __pc)
2829 {
return _M_f.parse(__pc); }
2831 template<
typename _Out>
2832 _GLIBCXX_CONSTEXPR_FORMAT
2833 typename basic_format_context<_Out, char>::iterator
2834 format(
const basic_string<char, _Traits, _Alloc>& __u,
2835 basic_format_context<_Out, char>& __fc)
const
2836 {
return _M_f.format(__u, __fc); }
2838#if __glibcxx_format_ranges
2839 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2843 __format::__formatter_str<char> _M_f;
2846#if __glibcxx_print >= 202403L
2847 template<
typename _Tr,
typename _Alloc>
2849 enable_nonlocking_formatter_optimization<basic_string<char, _Tr, _Alloc>>
2853#ifdef _GLIBCXX_USE_WCHAR_T
2854 template<
typename _Traits,
typename _Alloc>
2855 struct formatter<basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2857 formatter() =
default;
2859 [[__gnu__::__always_inline__]]
2860 constexpr typename basic_format_parse_context<wchar_t>::iterator
2861 parse(basic_format_parse_context<wchar_t>& __pc)
2862 {
return _M_f.parse(__pc); }
2864 template<
typename _Out>
2865 _GLIBCXX_CONSTEXPR_FORMAT
2866 typename basic_format_context<_Out, wchar_t>::iterator
2867 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2868 basic_format_context<_Out, wchar_t>& __fc)
const
2869 {
return _M_f.format(__u, __fc); }
2871#if __glibcxx_format_ranges
2872 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2876 __format::__formatter_str<wchar_t> _M_f;
2879#if __glibcxx_print >= 202403L
2880 template<
typename _Tr,
typename _Alloc>
2882 enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Tr, _Alloc>>
2888 template<
typename _Traits>
2889 struct formatter<basic_string_view<char, _Traits>, char>
2891 formatter() =
default;
2893 [[__gnu__::__always_inline__]]
2894 constexpr typename basic_format_parse_context<char>::iterator
2895 parse(basic_format_parse_context<char>& __pc)
2896 {
return _M_f.parse(__pc); }
2898 template<
typename _Out>
2899 _GLIBCXX_CONSTEXPR_FORMAT
2900 typename basic_format_context<_Out, char>::iterator
2901 format(basic_string_view<char, _Traits> __u,
2902 basic_format_context<_Out, char>& __fc)
const
2903 {
return _M_f.format(__u, __fc); }
2905#if __glibcxx_format_ranges
2906 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2910 __format::__formatter_str<char> _M_f;
2913#if __glibcxx_print >= 202403L
2914 template<
typename _Tr>
2916 enable_nonlocking_formatter_optimization<basic_string_view<char, _Tr>>
2920#ifdef _GLIBCXX_USE_WCHAR_T
2921 template<
typename _Traits>
2922 struct formatter<basic_string_view<wchar_t, _Traits>, wchar_t>
2924 formatter() =
default;
2926 [[__gnu__::__always_inline__]]
2927 constexpr typename basic_format_parse_context<wchar_t>::iterator
2928 parse(basic_format_parse_context<wchar_t>& __pc)
2929 {
return _M_f.parse(__pc); }
2931 template<
typename _Out>
2932 _GLIBCXX_CONSTEXPR_FORMAT
2933 typename basic_format_context<_Out, wchar_t>::iterator
2934 format(basic_string_view<wchar_t, _Traits> __u,
2935 basic_format_context<_Out, wchar_t>& __fc)
const
2936 {
return _M_f.format(__u, __fc); }
2938#if __glibcxx_format_ranges
2939 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2943 __format::__formatter_str<wchar_t> _M_f;
2946#if __glibcxx_print >= 202403L
2947 template<
typename _Tr>
2949 enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Tr>>
2960 template<
typename _Tp>
2961 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2963#if defined __SIZEOF_INT128__
2964 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2965 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2969 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2970 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2971#ifdef _GLIBCXX_USE_CHAR8_T
2972 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2974 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2975 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2977 template<
typename _Tp>
2978 concept __formattable_integer = __is_formattable_integer<_Tp>;
2983 template<__format::__formattable_
integer _Tp, __format::__
char _CharT>
2984 struct formatter<_Tp, _CharT>
2986 formatter() =
default;
2988 [[__gnu__::__always_inline__]]
2989 constexpr typename basic_format_parse_context<_CharT>::iterator
2990 parse(basic_format_parse_context<_CharT>& __pc)
2992 return _M_f.template _M_parse<_Tp>(__pc);
2995 template<
typename _Out>
2996 _GLIBCXX_CONSTEXPR_FORMAT
2997 typename basic_format_context<_Out, _CharT>::iterator
2998 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2999 {
return _M_f.format(__u, __fc); }
3002 __format::__formatter_int<_CharT> _M_f;
3005#if __glibcxx_print >= 202403L
3006 template<__format::__formattable_
integer _Tp>
3008 enable_nonlocking_formatter_optimization<_Tp> =
true;
3011#if defined __glibcxx_to_chars
3013 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
3014 struct formatter<_Tp, _CharT>
3016 formatter() =
default;
3018 [[__gnu__::__always_inline__]]
3019 constexpr typename basic_format_parse_context<_CharT>::iterator
3020 parse(basic_format_parse_context<_CharT>& __pc)
3021 {
return _M_f.parse(__pc); }
3023 template<
typename _Out>
3024 typename basic_format_context<_Out, _CharT>::iterator
3025 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
3026 {
return _M_f.format(__u, __fc); }
3029 __format::__formatter_fp<_CharT> _M_f;
3032#if __glibcxx_print >= 202403L
3033 template<__format::__formattable_
float _Tp>
3035 enable_nonlocking_formatter_optimization<_Tp> =
true;
3038#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
3040 template<__format::__
char _CharT>
3041 struct formatter<long double, _CharT>
3043 formatter() =
default;
3045 [[__gnu__::__always_inline__]]
3046 constexpr typename basic_format_parse_context<_CharT>::iterator
3047 parse(basic_format_parse_context<_CharT>& __pc)
3048 {
return _M_f.parse(__pc); }
3050 template<
typename _Out>
3051 typename basic_format_context<_Out, _CharT>::iterator
3052 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
3053 {
return _M_f.format((
double)__u, __fc); }
3056 __format::__formatter_fp<_CharT> _M_f;
3060#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3062 template<__format::__
char _CharT>
3063 struct formatter<_Float32, _CharT>
3065 formatter() =
default;
3067 [[__gnu__::__always_inline__]]
3068 constexpr typename basic_format_parse_context<_CharT>::iterator
3069 parse(basic_format_parse_context<_CharT>& __pc)
3070 {
return _M_f.parse(__pc); }
3072 template<
typename _Out>
3073 typename basic_format_context<_Out, _CharT>::iterator
3074 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
3075 {
return _M_f.format((
float)__u, __fc); }
3078 __format::__formatter_fp<_CharT> _M_f;
3082#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3084 template<__format::__
char _CharT>
3085 struct formatter<_Float64, _CharT>
3087 formatter() =
default;
3089 [[__gnu__::__always_inline__]]
3090 constexpr typename basic_format_parse_context<_CharT>::iterator
3091 parse(basic_format_parse_context<_CharT>& __pc)
3092 {
return _M_f.parse(__pc); }
3094 template<
typename _Out>
3095 typename basic_format_context<_Out, _CharT>::iterator
3096 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
3097 {
return _M_f.format((
double)__u, __fc); }
3100 __format::__formatter_fp<_CharT> _M_f;
3104#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128
3106 template<__format::__
char _CharT>
3107 struct formatter<_Float128, _CharT>
3109 formatter() =
default;
3111 [[__gnu__::__always_inline__]]
3112 constexpr typename basic_format_parse_context<_CharT>::iterator
3113 parse(basic_format_parse_context<_CharT>& __pc)
3114 {
return _M_f.parse(__pc); }
3116 template<
typename _Out>
3117 typename basic_format_context<_Out, _CharT>::iterator
3118 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3119 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3122 __format::__formatter_fp<_CharT> _M_f;
3126#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128 == 2
3129 template<__format::__
char _CharT>
3130 struct formatter<__float128, _CharT>
3132 formatter() =
default;
3134 [[__gnu__::__always_inline__]]
3135 constexpr typename basic_format_parse_context<_CharT>::iterator
3136 parse(basic_format_parse_context<_CharT>& __pc)
3137 {
return _M_f.parse(__pc); }
3139 template<
typename _Out>
3140 typename basic_format_context<_Out, _CharT>::iterator
3141 format(__float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3142 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3145 __format::__formatter_fp<_CharT> _M_f;
3154 template<__format::__
char _CharT>
3155 struct formatter<const void*, _CharT>
3157 formatter() =
default;
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 _GLIBCXX_CONSTEXPR_FORMAT
3165 typename basic_format_context<_Out, _CharT>::iterator
3166 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3167 {
return _M_f.format(__v, __fc); }
3170 __format::__formatter_ptr<_CharT> _M_f;
3173#if __glibcxx_print >= 202403L
3175 inline constexpr bool
3176 enable_nonlocking_formatter_optimization<const void*> =
true;
3179 template<__format::__
char _CharT>
3180 struct formatter<void*, _CharT>
3182 formatter() =
default;
3184 [[__gnu__::__always_inline__]]
3185 constexpr typename basic_format_parse_context<_CharT>::iterator
3186 parse(basic_format_parse_context<_CharT>& __pc)
3187 {
return _M_f.parse(__pc); }
3189 template<
typename _Out>
3190 _GLIBCXX_CONSTEXPR_FORMAT
3191 typename basic_format_context<_Out, _CharT>::iterator
3192 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3193 {
return _M_f.format(__v, __fc); }
3196 __format::__formatter_ptr<_CharT> _M_f;
3199#if __glibcxx_print >= 202403l
3201 inline constexpr bool
3202 enable_nonlocking_formatter_optimization<void*> =
true;
3205 template<__format::__
char _CharT>
3206 struct formatter<nullptr_t, _CharT>
3208 formatter() =
default;
3210 [[__gnu__::__always_inline__]]
3211 constexpr typename basic_format_parse_context<_CharT>::iterator
3212 parse(basic_format_parse_context<_CharT>& __pc)
3213 {
return _M_f.parse(__pc); }
3215 template<
typename _Out>
3216 _GLIBCXX_CONSTEXPR_FORMAT
3217 typename basic_format_context<_Out, _CharT>::iterator
3218 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3219 {
return _M_f.format(
nullptr, __fc); }
3222 __format::__formatter_ptr<_CharT> _M_f;
3226#if __glibcxx_print >= 202403L
3228 inline constexpr bool
3229 enable_nonlocking_formatter_optimization<nullptr_t> =
true;
3232#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3236 struct __formatter_disabled
3238 __formatter_disabled() =
delete;
3239 __formatter_disabled(
const __formatter_disabled&) =
delete;
3240 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3244 struct formatter<char*,
wchar_t>
3245 :
private __formatter_disabled { };
3247 struct formatter<const char*,
wchar_t>
3248 :
private __formatter_disabled { };
3249 template<
size_t _Nm>
3250 struct formatter<char[_Nm], wchar_t>
3251 :
private __formatter_disabled { };
3252 template<
class _Traits,
class _Allocator>
3253 struct formatter<basic_string<char, _Traits, _Allocator>, wchar_t>
3254 :
private __formatter_disabled { };
3255 template<
class _Traits>
3256 struct formatter<basic_string_view<char, _Traits>, wchar_t>
3257 :
private __formatter_disabled { };
3262 template<
typename _Out>
3263 struct format_to_n_result
3266 iter_difference_t<_Out> size;
3269_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3270template<
typename,
typename>
class vector;
3271_GLIBCXX_END_NAMESPACE_CONTAINER
3276 template<
typename _CharT>
3280 using iterator_category = output_iterator_tag;
3281 using value_type = void;
3282 using difference_type = ptrdiff_t;
3283 using pointer = void;
3284 using reference = void;
3286 _Drop_iter() =
default;
3287 _Drop_iter(
const _Drop_iter&) =
default;
3288 _Drop_iter& operator=(
const _Drop_iter&) =
default;
3290 [[__gnu__::__always_inline__]]
3291 constexpr _Drop_iter&
3292 operator=(_CharT __c)
3295 [[__gnu__::__always_inline__]]
3296 constexpr _Drop_iter&
3297 operator=(basic_string_view<_CharT> __s)
3300 [[__gnu__::__always_inline__]]
3301 constexpr _Drop_iter&
3304 [[__gnu__::__always_inline__]]
3305 constexpr _Drop_iter&
3306 operator++() {
return *
this; }
3308 [[__gnu__::__always_inline__]]
3309 constexpr _Drop_iter
3310 operator++(
int) {
return *
this; }
3313 template<
typename _CharT>
3316 _Sink<_CharT>* _M_sink =
nullptr;
3319 using iterator_category = output_iterator_tag;
3320 using value_type = void;
3321 using difference_type = ptrdiff_t;
3322 using pointer = void;
3323 using reference = void;
3325 _Sink_iter() =
default;
3326 _Sink_iter(
const _Sink_iter&) =
default;
3327 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3329 [[__gnu__::__always_inline__]]
3331 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3333 [[__gnu__::__always_inline__]]
3334 constexpr _Sink_iter&
3335 operator=(_CharT __c)
3337 _M_sink->_M_write(__c);
3341 [[__gnu__::__always_inline__]]
3342 constexpr _Sink_iter&
3343 operator=(basic_string_view<_CharT> __s)
3345 _M_sink->_M_write(__s);
3349 [[__gnu__::__always_inline__]]
3350 constexpr _Sink_iter&
3353 [[__gnu__::__always_inline__]]
3354 constexpr _Sink_iter&
3355 operator++() {
return *
this; }
3357 [[__gnu__::__always_inline__]]
3358 constexpr _Sink_iter
3359 operator++(
int) {
return *
this; }
3361 _GLIBCXX_CONSTEXPR_FORMAT
auto
3362 _M_reserve(
size_t __n)
const
3363 {
return _M_sink->_M_reserve(__n); }
3365 _GLIBCXX_CONSTEXPR_FORMAT
bool
3366 _M_discarding()
const
3367 {
return _M_sink->_M_discarding(); }
3373 template<
typename _CharT>
3376 friend class _Sink_iter<_CharT>;
3378 span<_CharT> _M_span;
3379 typename span<_CharT>::iterator _M_next;
3385 virtual void _M_overflow() = 0;
3389 [[__gnu__::__always_inline__]]
3391 _Sink(span<_CharT> __span) noexcept
3392 : _M_span(__span), _M_next(__span.begin())
3396 [[__gnu__::__always_inline__]]
3397 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3398 _M_used() const noexcept
3399 {
return _M_span.first(_M_next - _M_span.begin()); }
3402 [[__gnu__::__always_inline__]]
3403 constexpr span<_CharT>
3404 _M_unused() const noexcept
3405 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3408 [[__gnu__::__always_inline__]]
3410 _M_rewind() noexcept
3411 { _M_next = _M_span.begin(); }
3414 _GLIBCXX_CONSTEXPR_FORMAT
void
3415 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3418 _M_next = __s.begin() + __pos;
3423 _M_write(_CharT __c)
3426 if (_M_next - _M_span.begin() ==
std::ssize(_M_span)) [[unlikely]]
3431 _M_write(basic_string_view<_CharT> __s)
3433 span __to = _M_unused();
3434 while (__to.size() <= __s.size())
3436 __s.copy(__to.data(), __to.size());
3437 _M_next += __to.size();
3438 __s.remove_prefix(__to.size());
3444 __s.copy(__to.data(), __s.size());
3445 _M_next += __s.size();
3454 _GLIBCXX_CONSTEXPR_FORMAT
3455 explicit operator bool() const noexcept {
return _M_sink; }
3457 _GLIBCXX_CONSTEXPR_FORMAT _CharT*
3458 get() const noexcept {
return _M_sink->_M_next.operator->(); }
3460 _GLIBCXX_CONSTEXPR_FORMAT
void
3461 _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3469 _GLIBCXX_CONSTEXPR_FORMAT
virtual _Reservation
3470 _M_reserve(
size_t __n)
3472 if (__n <= _M_unused().size())
3475 if (__n <= _M_span.size())
3478 if (__n <= _M_unused().size())
3486 _GLIBCXX_CONSTEXPR_FORMAT
virtual void
3491 _GLIBCXX_CONSTEXPR_FORMAT
virtual bool
3492 _M_discarding()
const
3496 _Sink(
const _Sink&) =
delete;
3497 _Sink& operator=(
const _Sink&) =
delete;
3499 [[__gnu__::__always_inline__]]
3500 constexpr _Sink_iter<_CharT>
3502 {
return _Sink_iter<_CharT>(*
this); }
3506 template<
typename _CharT>
3507 class _Fixedbuf_sink final :
public _Sink<_CharT>
3509 _GLIBCXX_CONSTEXPR_FORMAT
void
3510 _M_overflow()
override
3512 __glibcxx_assert(
false);
3517 [[__gnu__::__always_inline__]]
3519 _Fixedbuf_sink(span<_CharT> __buf)
3520 : _Sink<_CharT>(__buf)
3523 constexpr basic_string_view<_CharT>
3526 auto __s = this->_M_used();
3527 return basic_string_view<_CharT>(__s.data(), __s.size());
3532 template<
typename _CharT>
3533 class _Buf_sink :
public _Sink<_CharT>
3536 _CharT _M_buf[__stackbuf_size<_CharT>];
3538 [[__gnu__::__always_inline__]]
3540 _Buf_sink() noexcept
3541 : _Sink<_CharT>(_M_buf)
3549 template<
typename _CharT>
3550 class _Streambuf_sink :
public _Buf_sink<_CharT>
3553 basic_streambuf<_CharT>* _M_sbuf;
3554 bool _M_write_failed =
false;
3557 _M_on_stack() const noexcept
3558 {
return this->_M_used().data() == this->_M_buf; }
3564 _M_reserve_put_area(
size_t __n = 0)
3566 if (
auto __p = _M_sbuf->pptr())
3567 if (
auto __e = _M_sbuf->epptr(); __e > __p)
3568 if (
size_t __a(__e - __p); __a >= __n)
3570 this->_M_reset(span<_CharT>(__p, __a));
3583 span<const _CharT> __s = this->_M_used();
3584 if (__s.empty() || _M_write_failed) [[unlikely]]
3590 _M_sbuf->__safe_pbump(__s.size());
3591 else if (_M_sbuf->sputn(__s.data(), __s.size())
3593 _M_write_failed =
true;
3595 __catch(
const __cxxabiv1::__forced_unwind&)
3598 { _M_write_failed =
true; }
3602 _M_overflow()
override
3605 if (!_M_write_failed) [[likely]]
3606 if (_M_reserve_put_area())
3608 this->_M_reset(this->_M_buf);
3612 [[__gnu__::__always_inline__]]
3614 _Streambuf_sink(basic_streambuf<_CharT>* __sbuf) noexcept
3615 : _M_sbuf(__sbuf), _M_write_failed(!__sbuf)
3617 if (!_M_write_failed) [[likely]]
3618 _M_reserve_put_area();
3626 typename _Sink<_CharT>::_Reservation
3627 _M_reserve(
size_t __n)
override
3629 if (_M_write_failed) [[unlikely]]
3632 if (__n <= this->_M_unused().size())
3635 if (!this->_M_used().empty())
3639 if (_M_reserve_put_area(__n))
3643 this->_M_reset(this->_M_buf);
3644 if (__n <= this->_M_unused().size())
3651 _M_discarding()
const override
3652 {
return _M_write_failed; }
3659 _M_failed() const noexcept
3660 {
return _M_write_failed; }
3663 using _GLIBCXX_STD_C::vector;
3667 template<
typename _Seq>
3668 class _Seq_sink :
public _Buf_sink<typename _Seq::value_type>
3670 using _CharT =
typename _Seq::value_type;
3675 _GLIBCXX_CONSTEXPR_FORMAT
void
3676 _M_overflow()
override
3678 auto __s = this->_M_used();
3679 if (__s.empty()) [[unlikely]]
3683 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3685 if constexpr (__is_specialization_of<_Seq, basic_string>)
3686 _M_seq.append(__s.data(), __s.size());
3688 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3694 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3695 _M_reserve(
size_t __n)
override
3704 if constexpr (__is_specialization_of<_Seq, basic_string>
3705 || __is_specialization_of<_Seq, vector>)
3708 if (this->_M_used().size()) [[unlikely]]
3709 _Seq_sink::_M_overflow();
3712 const auto __sz = _M_seq.size();
3713 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3714 _M_seq.__resize_and_overwrite(__sz + __n,
3715 [](
auto,
auto __n2) {
3719 _M_seq.resize(__sz + __n);
3723 this->_M_reset(_M_seq, __sz);
3727 return _Sink<_CharT>::_M_reserve(__n);
3730 _GLIBCXX_CONSTEXPR_FORMAT
void
3731 _M_bump(
size_t __n)
override
3733 if constexpr (__is_specialization_of<_Seq, basic_string>
3734 || __is_specialization_of<_Seq, vector>)
3736 auto __s = this->_M_used();
3737 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3739 _M_seq.resize(__s.size() + __n);
3741 this->_M_reset(this->_M_buf);
3745 _GLIBCXX_CONSTEXPR_FORMAT
void
3746 _M_trim(span<const _CharT> __s)
3747 requires __is_specialization_of<_Seq, basic_string>
3749 _GLIBCXX_DEBUG_ASSERT(__s.data() == this->_M_buf
3750 || __s.data() == _M_seq.data());
3751 if (__s.data() == _M_seq.data())
3752 _M_seq.resize(__s.size());
3754 this->_M_reset(this->_M_buf, __s.size());
3761 [[__gnu__::__always_inline__]]
3762 _GLIBCXX_CONSTEXPR_FORMAT
3763 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3766 _GLIBCXX_CONSTEXPR_FORMAT
3767 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3768 : _M_seq(std::move(__s))
3771 using _Sink<_CharT>::out;
3773 _GLIBCXX_CONSTEXPR_FORMAT _Seq
3776 if (this->_M_used().size() != 0)
3777 _Seq_sink::_M_overflow();
3783 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3786 auto __s = this->_M_used();
3789 if (__s.size() != 0)
3790 _Seq_sink::_M_overflow();
3796 _GLIBCXX_CONSTEXPR_FORMAT basic_string_view<_CharT>
3799 auto __span = _M_span();
3800 return basic_string_view<_CharT>(__span.data(), __span.size());
3804 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3806 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3812 template<
typename _CharT,
typename _OutIter>
3813 class _Iter_sink :
public _Buf_sink<_CharT>
3816 iter_difference_t<_OutIter> _M_max;
3819 size_t _M_count = 0;
3821 _GLIBCXX_CONSTEXPR_FORMAT
void
3822 _M_overflow()
override
3824 auto __s = this->_M_used();
3826 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3827 else if (_M_count <
static_cast<size_t>(_M_max))
3829 auto __max = _M_max - _M_count;
3830 span<_CharT> __first;
3831 if (__max < __s.size())
3832 __first = __s.first(
static_cast<size_t>(__max));
3835 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3838 _M_count += __s.size();
3841 _GLIBCXX_CONSTEXPR_FORMAT
bool
3842 _M_discarding()
const override
3850 [[__gnu__::__always_inline__]]
3851 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3852 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3853 : _M_out(std::move(__out)), _M_max(__max)
3856 using _Sink<_CharT>::out;
3858 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3861 if (this->_M_used().size() != 0)
3862 _Iter_sink::_M_overflow();
3863 iter_difference_t<_OutIter> __count(_M_count);
3870 template<
typename _CharT>
3871 class _Iter_sink<_CharT, ostreambuf_iterator<_CharT>>
3872 :
public _Streambuf_sink<_CharT>
3874 using _Base = _Streambuf_sink<_CharT>;
3875 using _OutIter = ostreambuf_iterator<_CharT>;
3877 static constexpr size_t _S_no_limit = size_t(-1);
3882 const size_t __avail = _M_max - _M_count;
3883 if (this->_M_unused().size() > __avail)
3884 this->_M_reset(this->_M_unused().first(__avail));
3888 size_t _M_max = _S_no_limit;
3889 size_t _M_count = 0;
3892 _M_overflow()
override
3894 const size_t __new = this->_M_used().size();
3895 const size_t __pcount = _M_count;
3898 if (__pcount >= _M_max)
3899 this->_M_reset(this->_M_buf);
3900 else if (
size_t __avail = _M_max - __pcount; __avail > __new)
3902 _Base::_M_overflow();
3908 this->_M_reset(this->_M_buf);
3913 _M_discarding()
const override
3917 [[__gnu__::__always_inline__]]
3919 _Iter_sink(_OutIter __out)
3920 : _Base(__out._M_sbuf)
3923 [[__gnu__::__always_inline__]]
3925 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max)
3926 : _Base(__out._M_sbuf), _M_max(__max < 0 ? _S_no_limit : size_t(__max))
3929 typename _Sink<_CharT>::_Reservation
3930 _M_reserve(
size_t __n)
override
3932 if (_M_count < _M_max)
3933 if (
size_t __avail = _M_max - _M_count; __avail >= __n)
3934 return _Base::_M_reserve(__n);
3938 format_to_n_result<_OutIter>
3941 _M_count += this->_M_used().size();
3942 if (_M_count <= _M_max)
3945 _OutIter __out(this->_M_sbuf);
3946 __out._M_failed = this->_M_write_failed;
3947 return {
std::move(__out), ptrdiff_t(_M_count) };
3958 template<
typename _CharT>
3959 class _Ptr_sink :
public _Sink<_CharT>
3961 static constexpr size_t _S_no_limit = size_t(-1);
3965 size_t _M_count = 0;
3970 _GLIBCXX_CONSTEXPR_FORMAT
void
3971 _M_overflow()
override
3973 if (this->_M_unused().size() != 0)
3976 auto __s = this->_M_used();
3978 if (_M_max != _S_no_limit)
3980 _M_count += __s.size();
3984 this->_M_reset(this->_M_buf);
3990 _M_rebuf(__s.data(), __s.size() + 1024, __s.size());
3994 _GLIBCXX_CONSTEXPR_FORMAT
bool
3995 _M_discarding()
const override
4002 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
4003 _M_reserve(
size_t __n)
final
4005 auto __avail = this->_M_unused();
4006 if (__n > __avail.size())
4008 if (_M_max != _S_no_limit)
4011 auto __s = this->_M_used();
4012 _M_rebuf(__s.data(), __s.size() + __n, __s.size());
4018 template<
typename _IterDifference>
4019 static _GLIBCXX_CONSTEXPR_FORMAT
size_t
4020 _S_trim_max(_IterDifference __max)
4024 if constexpr (!is_integral_v<_IterDifference> ||
sizeof(__max) >
sizeof(size_t))
4026 if (_IterDifference((
size_t)-1) < __max)
4028 return size_t(__max);
4031 [[__gnu__::__always_inline__]]
4032 _GLIBCXX_CONSTEXPR_FORMAT
void
4033 _M_rebuf(_CharT* __ptr,
size_t __total,
size_t __inuse = 0)
4035 std::span<_CharT> __span(__ptr, __total);
4036 this->_M_reset(__span, __inuse);
4040 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4041 _Ptr_sink(_CharT* __ptr,
size_t __n = _S_no_limit) noexcept
4042 : _Sink<_CharT>(_M_buf), _M_max(__n)
4046 else if (__n != _S_no_limit)
4047 _M_rebuf(__ptr, __n);
4048#if __has_builtin(__builtin_dynamic_object_size)
4049 else if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
4050 _M_rebuf(__ptr, __bytes /
sizeof(_CharT));
4055 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
4056 __n = (1024 - __off) /
sizeof(_CharT);
4057 if (__n > 0) [[likely]]
4058 _M_rebuf(__ptr, __n);
4064 template<contiguous_iterator _OutIter>
4065 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4066 _Ptr_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1)
4067 : _Ptr_sink(std::
to_address(__out), _S_trim_max(__n))
4070 template<contiguous_iterator _OutIter>
4071 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
4072 _M_finish(_OutIter __first)
const
4074 auto __s = this->_M_used();
4075 if (__s.data() == _M_buf)
4078 iter_difference_t<_OutIter> __m(_M_max);
4079 iter_difference_t<_OutIter> __count(_M_count + __s.size());
4080 return { __first + __m, __count };
4084 iter_difference_t<_OutIter> __count(__s.size());
4085 return { __first + __count, __count };
4090 template<
typename _CharT,
typename _OutIter>
4091 concept __contiguous_char_iter
4092 = contiguous_iterator<_OutIter>
4093 && same_as<iter_value_t<_OutIter>, _CharT>;
4104 template<
typename _Out,
typename _CharT>
4105 class _Padding_sink :
public _Str_sink<_CharT>
4110 size_t _M_printwidth;
4112 [[__gnu__::__always_inline__]]
4113 _GLIBCXX_CONSTEXPR_FORMAT
bool
4115 {
return _M_printwidth >= _M_maxwidth; }
4117 [[__gnu__::__always_inline__]]
4118 _GLIBCXX_CONSTEXPR_FORMAT
bool
4119 _M_buffering()
const
4121 if (_M_printwidth < _M_padwidth)
4123 if (_M_maxwidth != (
size_t)-1)
4124 return _M_printwidth < _M_maxwidth;
4128 _GLIBCXX_CONSTEXPR_FORMAT
void
4129 _M_sync_discarding()
4131 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4132 if (_M_out._M_discarding())
4133 _M_maxwidth = _M_printwidth;
4136 _GLIBCXX_CONSTEXPR_FORMAT
void
4139 span<_CharT> __new = this->_M_used();
4140 basic_string_view<_CharT> __str(__new.data(), __new.size());
4141 _M_out = __format::__write(
std::move(_M_out), __str);
4142 _M_sync_discarding();
4146 _GLIBCXX_CONSTEXPR_FORMAT
bool
4149 auto __str = this->view();
4151 _M_printwidth = __format::__truncate(__str, _M_maxwidth);
4153 this->_M_trim(__str);
4159 if (_M_printwidth >= _M_padwidth)
4161 _M_out = __format::__write(
std::move(_M_out), __str);
4162 _M_sync_discarding();
4167 _Str_sink<_CharT>::_M_overflow();
4170 this->_M_reset(this->_M_buf);
4174 _GLIBCXX_CONSTEXPR_FORMAT
bool
4175 _M_update(
size_t __new)
4177 _M_printwidth += __new;
4179 if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
4180 return _M_force_update();
4184 _GLIBCXX_CONSTEXPR_FORMAT
void
4185 _M_overflow()
override
4191 else if (!_M_buffering())
4195 else if (_M_update(this->_M_used().size()))
4196 _Str_sink<_CharT>::_M_overflow();
4199 _GLIBCXX_CONSTEXPR_FORMAT
bool
4200 _M_discarding()
const override
4201 {
return _M_ignoring(); }
4203 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
4204 _M_reserve(
size_t __n)
override
4209 else if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4210 if (!_M_buffering())
4213 if (!this->_M_used().empty())
4216 if (
auto __reserved = _M_out._M_reserve(__n))
4219 return _Sink<_CharT>::_M_reserve(__n);
4222 _GLIBCXX_CONSTEXPR_FORMAT
void
4223 _M_bump(
size_t __n)
override
4230 _Sink<_CharT>::_M_bump(__n);
4236 [[__gnu__::__always_inline__]]
4237 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4238 _Padding_sink(_Out __out,
size_t __padwidth,
size_t __maxwidth)
4239 : _M_padwidth(__padwidth), _M_maxwidth(__maxwidth),
4240 _M_out(std::move(__out)), _M_printwidth(0)
4241 { _M_sync_discarding(); }
4243 [[__gnu__::__always_inline__]]
4244 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4245 _Padding_sink(_Out __out,
size_t __padwidth)
4246 : _Padding_sink(std::move(__out), __padwidth, (size_t)-1)
4249 _GLIBCXX_CONSTEXPR_FORMAT _Out
4250 _M_finish(_Align __align,
char32_t __fill_char)
4253 if (
auto __rem = this->_M_used().size())
4257 else if (!_M_buffering())
4263 if (!_M_buffering() || !_M_force_update())
4265 if (_M_printwidth >= _M_padwidth)
4268 const auto __str = this->view();
4269 if (_M_printwidth >= _M_padwidth)
4270 return __format::__write(
std::move(_M_out), __str);
4272 const size_t __nfill = _M_padwidth - _M_printwidth;
4273 return __format::__write_padded(
std::move(_M_out), __str,
4274 __align, __nfill, __fill_char);
4278 template<
typename _Out,
typename _CharT>
4279 class _Escaping_sink :
public _Buf_sink<_CharT>
4281 using _Esc = _Escapes<_CharT>;
4284 _Term_char _M_term : 2;
4285 unsigned _M_prev_escape : 1;
4286 unsigned _M_out_discards : 1;
4288 _GLIBCXX_CONSTEXPR_FORMAT
void
4289 _M_sync_discarding()
4291 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4292 _M_out_discards = _M_out._M_discarding();
4295 _GLIBCXX_CONSTEXPR_FORMAT
void
4298 span<_CharT> __bytes = this->_M_used();
4299 basic_string_view<_CharT> __str(__bytes.data(), __bytes.size());
4302 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4304 bool __prev_escape = _M_prev_escape;
4305 _M_out = __format::__write_escaped_unicode_part(
4306 std::move(_M_out), __str, __prev_escape, _M_term);
4307 _M_prev_escape = __prev_escape;
4309 __rem = __str.size();
4310 if (__rem > 0 && __str.data() != this->_M_buf) [[unlikely]]
4311 ranges::move(__str, this->_M_buf);
4314 _M_out = __format::__write_escaped_ascii(
4317 this->_M_reset(this->_M_buf, __rem);
4318 _M_sync_discarding();
4321 _GLIBCXX_CONSTEXPR_FORMAT
void
4322 _M_overflow()
override
4324 if (_M_out_discards)
4330 _GLIBCXX_CONSTEXPR_FORMAT
bool
4331 _M_discarding()
const override
4332 {
return _M_out_discards; }
4335 [[__gnu__::__always_inline__]]
4336 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4337 _Escaping_sink(_Out __out, _Term_char __term)
4338 : _M_out(std::move(__out)), _M_term(__term),
4339 _M_prev_escape(true), _M_out_discards(false)
4341 _M_out = __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4342 _M_sync_discarding();
4345 _GLIBCXX_CONSTEXPR_FORMAT _Out
4348 if (_M_out_discards)
4351 if (!this->_M_used().empty())
4354 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4355 if (
auto __rem = this->_M_used(); !__rem.empty())
4357 basic_string_view<_CharT> __str(__rem.data(), __rem.size());
4358 _M_out = __format::__write_escape_seqs(
std::move(_M_out), __str);
4361 return __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4365 template<
typename _Context>
4368 using _CharT =
typename _Context::char_type;
4372 using _CharT =
typename _Context::char_type;
4373 using _Func = void(*)(basic_format_parse_context<_CharT>&,
4374 _Context&,
const void*);
4377 template<
typename _Tp>
4378 using __maybe_const_t
4379 = __conditional_t<__formattable_with<const _Tp, _Context>,
4382 template<
typename _Tq>
4383 static _GLIBCXX_CONSTEXPR_FORMAT
void
4384 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
4385 _Context& __format_ctx,
const void* __ptr)
4387 using _Td = remove_const_t<_Tq>;
4388 typename _Context::template formatter_type<_Td> __f;
4389 __parse_ctx.advance_to(__f.parse(__parse_ctx));
4390 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
4391 __format_ctx.advance_to(__f.format(__val, __format_ctx));
4394 template<
typename _Tp>
4395 requires (!is_same_v<remove_cv_t<_Tp>, handle>)
4396 explicit _GLIBCXX_CONSTEXPR_FORMAT
4397 handle(_Tp& __val) noexcept
4398 : _M_ptr(__builtin_addressof(__val))
4399 , _M_func(&_S_format<__maybe_const_t<_Tp>>)
4402 friend class basic_format_arg<_Context>;
4405 handle(
const handle&) =
default;
4406 handle& operator=(
const handle&) =
default;
4408 [[__gnu__::__always_inline__]]
4409 void _GLIBCXX_CONSTEXPR_FORMAT
4410 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
4411 { _M_func(__pc, __fc, this->_M_ptr); }
4426 unsigned long long _M_ull;
4429#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4430 long double _M_ldbl;
4433 __ieee128 _M_ieee128;
4435#ifdef __SIZEOF_FLOAT128__
4436 __float128 _M_float128;
4438 const _CharT* _M_str;
4439 basic_string_view<_CharT> _M_sv;
4442#ifdef __SIZEOF_INT128__
4444 unsigned __int128 _M_u128;
4446#ifdef __BFLT16_DIG__
4460 [[__gnu__::__always_inline__]]
4461 _GLIBCXX_CONSTEXPR_FORMAT
4462 _Arg_value() : _M_none() { }
4465 template<
typename _Tp>
4466 _GLIBCXX_CONSTEXPR_FORMAT
4467 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
4468 { _S_get<_Tp>() = __val; }
4473 template<
typename _Tp,
typename _Self,
typename... _Value>
4474 [[__gnu__::__always_inline__]]
4475 static _GLIBCXX_CONSTEXPR_FORMAT
auto&
4476 _S_access(_Self& __u, _Value... __value)
noexcept
4478 static_assert(
sizeof...(_Value) <= 1);
4479 if constexpr (is_same_v<_Tp, bool>)
4480 return (__u._M_bool = ... = __value);
4481 else if constexpr (is_same_v<_Tp, _CharT>)
4482 return (__u._M_c = ... = __value);
4483 else if constexpr (is_same_v<_Tp, int>)
4484 return (__u._M_i = ... = __value);
4485 else if constexpr (is_same_v<_Tp, unsigned>)
4486 return (__u._M_u = ... = __value);
4487 else if constexpr (is_same_v<_Tp, long long>)
4488 return (__u._M_ll = ... = __value);
4489 else if constexpr (is_same_v<_Tp, unsigned long long>)
4490 return (__u._M_ull = ... = __value);
4491 else if constexpr (is_same_v<_Tp, float>)
4492 return (__u._M_flt = ... = __value);
4493 else if constexpr (is_same_v<_Tp, double>)
4494 return (__u._M_dbl = ... = __value);
4495#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4496 else if constexpr (is_same_v<_Tp, long double>)
4497 return (__u._M_ldbl = ... = __value);
4499 else if constexpr (is_same_v<_Tp, __ibm128>)
4500 return (__u._M_ibm128 = ... = __value);
4501 else if constexpr (is_same_v<_Tp, __ieee128>)
4502 return (__u._M_ieee128 = ... = __value);
4504#ifdef __SIZEOF_FLOAT128__
4505 else if constexpr (is_same_v<_Tp, __float128>)
4506 return (__u._M_float128 = ... = __value);
4508 else if constexpr (is_same_v<_Tp, const _CharT*>)
4509 return (__u._M_str = ... = __value);
4510 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4511 return (__u._M_sv = ... = __value);
4512 else if constexpr (is_same_v<_Tp, const void*>)
4513 return (__u._M_ptr = ... = __value);
4514#ifdef __SIZEOF_INT128__
4515 else if constexpr (is_same_v<_Tp, __int128>)
4516 return (__u._M_i128 = ... = __value);
4517 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4518 return (__u._M_u128 = ... = __value);
4520#ifdef __BFLT16_DIG__
4521 else if constexpr (is_same_v<_Tp, __bflt16_t>)
4522 return (__u._M_bf16 = ... = __value);
4525 else if constexpr (is_same_v<_Tp, _Float16>)
4526 return (__u._M_f16 = ... = __value);
4529 else if constexpr (is_same_v<_Tp, _Float32>)
4530 return (__u._M_f32 = ... = __value);
4533 else if constexpr (is_same_v<_Tp, _Float64>)
4534 return (__u._M_f64 = ... = __value);
4536 else if constexpr (is_same_v<_Tp, handle>)
4537 return __u._M_handle;
4539 __builtin_unreachable();
4542 template<
typename _Tp>
4543 [[__gnu__::__always_inline__]]
4544 _GLIBCXX_CONSTEXPR_FORMAT
auto&
4546 {
return _S_access<_Tp>(*
this); }
4548 template<
typename _Tp>
4549 [[__gnu__::__always_inline__]]
4550 _GLIBCXX_CONSTEXPR_FORMAT
const auto&
4551 _M_get() const noexcept
4552 {
return _S_access<_Tp>(*
this); }
4554 template<
typename _Tp>
4555 [[__gnu__::__always_inline__]]
4556 _GLIBCXX_CONSTEXPR_FORMAT
void
4557 _M_set(_Tp __v)
noexcept
4560 if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4561 std::construct_at(&_M_sv, __v);
4562 else if constexpr (is_same_v<_Tp, handle>)
4563 std::construct_at(&_M_handle, __v);
4567 _S_access<_Tp>(*
this, __v);
4572 template<
typename _Context,
typename... _Args>
4575 template<
typename _Visitor,
typename _Ctx>
4576 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4577 __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4579 template<
typename _Ch,
typename _Tp>
4581 __to_arg_t_enum() noexcept;
4585 template<typename _Context>
4586 class basic_format_arg
4588 using _CharT =
typename _Context::char_type;
4591 using handle = __format::_Arg_value<_Context>::handle;
4593 [[__gnu__::__always_inline__]]
4594 _GLIBCXX_CONSTEXPR_FORMAT
4595 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
4597 [[nodiscard,__gnu__::__always_inline__]]
4598 explicit _GLIBCXX_CONSTEXPR_FORMAT
operator bool() const noexcept
4599 {
return _M_type != __format::_Arg_none; }
4601#if __cpp_lib_format >= 202306L
4602 template<
typename _Visitor>
4603 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4604 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4607 template<
typename _Res,
typename _Visitor>
4608 _GLIBCXX_CONSTEXPR_FORMAT _Res
4609 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4614 template<
typename _Ctx>
4615 friend class basic_format_args;
4617 template<
typename _Ctx,
typename... _Args>
4618 friend class __format::_Arg_store;
4620 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
4622 __format::_Arg_value<_Context> _M_val;
4623 __format::_Arg_t _M_type;
4628 template<
typename _Tp>
4629 static consteval auto
4632 using _Td = remove_const_t<_Tp>;
4633 if constexpr (is_same_v<_Td, bool>)
4634 return type_identity<bool>();
4635 else if constexpr (is_same_v<_Td, _CharT>)
4636 return type_identity<_CharT>();
4637 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
4638 return type_identity<_CharT>();
4639#ifdef __SIZEOF_INT128__
4640 else if constexpr (is_same_v<_Td, __int128>)
4641 return type_identity<__int128>();
4642 else if constexpr (is_same_v<_Td, unsigned __int128>)
4643 return type_identity<unsigned __int128>();
4645 else if constexpr (__is_signed_integer<_Td>::value)
4647 if constexpr (
sizeof(_Td) <=
sizeof(int))
4648 return type_identity<int>();
4649 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
4650 return type_identity<long long>();
4652 else if constexpr (__is_unsigned_integer<_Td>::value)
4654 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
4655 return type_identity<unsigned>();
4656 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
4657 return type_identity<unsigned long long>();
4659 else if constexpr (is_same_v<_Td, float>)
4660 return type_identity<float>();
4661 else if constexpr (is_same_v<_Td, double>)
4662 return type_identity<double>();
4663#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4664 else if constexpr (is_same_v<_Td, long double>)
4665 return type_identity<long double>();
4667 else if constexpr (is_same_v<_Td, __ibm128>)
4668 return type_identity<__ibm128>();
4669 else if constexpr (is_same_v<_Td, __ieee128>)
4670 return type_identity<__ieee128>();
4672#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4673 else if constexpr (is_same_v<_Td, __float128>)
4674 return type_identity<__float128>();
4676#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4677 else if constexpr (is_same_v<_Td, __format::__bflt16_t>)
4678 return type_identity<__format::__bflt16_t>();
4680#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4681 else if constexpr (is_same_v<_Td, _Float16>)
4682 return type_identity<_Float16>();
4684#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4685 else if constexpr (is_same_v<_Td, _Float32>)
4686 return type_identity<_Float32>();
4688#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4689 else if constexpr (is_same_v<_Td, _Float64>)
4690 return type_identity<_Float64>();
4692 else if constexpr (__is_specialization_of<_Td, basic_string_view>
4693 || __is_specialization_of<_Td, basic_string>)
4695 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
4696 return type_identity<basic_string_view<_CharT>>();
4698 return type_identity<handle>();
4700 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
4701 return type_identity<const _CharT*>();
4702 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
4703 return type_identity<const _CharT*>();
4704 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
4705 return type_identity<const void*>();
4706 else if constexpr (is_same_v<_Td, nullptr_t>)
4707 return type_identity<const void*>();
4709 return type_identity<handle>();
4713 template<
typename _Tp>
4714 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
4717 template<
typename _Tp>
4718 static consteval __format::_Arg_t
4721 using namespace __format;
4722 if constexpr (is_same_v<_Tp, bool>)
4724 else if constexpr (is_same_v<_Tp, _CharT>)
4726 else if constexpr (is_same_v<_Tp, int>)
4728 else if constexpr (is_same_v<_Tp, unsigned>)
4730 else if constexpr (is_same_v<_Tp, long long>)
4732 else if constexpr (is_same_v<_Tp, unsigned long long>)
4734 else if constexpr (is_same_v<_Tp, float>)
4736 else if constexpr (is_same_v<_Tp, double>)
4738#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4739 else if constexpr (is_same_v<_Tp, long double>)
4743 else if constexpr (is_same_v<_Tp, __ibm128>)
4745 else if constexpr (is_same_v<_Tp, __ieee128>)
4746 return _Arg_ieee128;
4748#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4749 else if constexpr (is_same_v<_Tp, __float128>)
4750 return _Arg_float128;
4752#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4753 else if constexpr (is_same_v<_Tp, __format::__bflt16_t>)
4756#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4757 else if constexpr (is_same_v<_Tp, _Float16>)
4760#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4761 else if constexpr (is_same_v<_Tp, _Float32>)
4764#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4765 else if constexpr (is_same_v<_Tp, _Float64>)
4768 else if constexpr (is_same_v<_Tp, const _CharT*>)
4770 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4772 else if constexpr (is_same_v<_Tp, const void*>)
4774#ifdef __SIZEOF_INT128__
4775 else if constexpr (is_same_v<_Tp, __int128>)
4777 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4780 else if constexpr (is_same_v<_Tp, handle>)
4784 template<
typename _Tp>
4785 _GLIBCXX_CONSTEXPR_FORMAT
void
4786 _M_set(_Tp __v)
noexcept
4788 _M_type = _S_to_enum<_Tp>();
4792 template<
typename _Tp>
4793 requires __format::__formattable_with<_Tp, _Context>
4794 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4795 basic_format_arg(_Tp& __v)
noexcept
4797 using _Td = _Normalize<_Tp>;
4798 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4799 _M_set(_Td{__v.data(), __v.size()});
4800 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4801 && is_same_v<_CharT, wchar_t>)
4802 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4804 _M_set(
static_cast<_Td
>(__v));
4807 template<
typename _Ctx,
typename... _Argz>
4808 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4809 make_format_args(_Argz&...)
noexcept;
4811 template<
typename _Visitor,
typename _Ctx>
4812 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4813 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4815 template<
typename _Visitor,
typename _Ctx>
4816 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4817 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4819 template<
typename _Ch,
typename _Tp>
4820 friend consteval __format::_Arg_t
4821 __format::__to_arg_t_enum() noexcept;
4823 [[__gnu__::__noinline__]]
4825 _M_handle_unrecognized() const;
4827 template<typename _Visitor>
4828 _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4829 _M_visit(_Visitor&& __vis)
4833 using enum __format::_Arg_t;
4848#if __glibcxx_to_chars
4853#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4856#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4866#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4870#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4874#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4878#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4891#ifdef __SIZEOF_INT128__
4901 handle __h = _M_handle_unrecognized();
4906 template<
typename _Visitor>
4907 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4908 _M_visit_user(_Visitor&& __vis)
4910 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4912 constexpr bool __user_facing = __is_one_of<_Tp,
4913 monostate, bool, _CharT,
4914 int,
unsigned int,
long long int,
unsigned long long int,
4915 float, double,
long double,
4916 const _CharT*, basic_string_view<_CharT>,
4917 const void*, handle>::value;
4918 if constexpr (__user_facing)
4929 template<
typename _Visitor,
typename _Context>
4930 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4931 inline _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4932 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4938 template<
typename _Visitor,
typename _Ctx>
4939 inline _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4940 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4943 struct _WidthPrecVisitor
4945 template<
typename _Tp>
4946 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4947 operator()(_Tp& __arg)
const
4949 if constexpr (is_same_v<_Tp, monostate>)
4950 __format::__invalid_arg_id_in_format_string();
4954 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4958 if constexpr (__is_unsigned_integer<_Tp>::value)
4960 else if constexpr (__is_signed_integer<_Tp>::value)
4964 __throw_format_error(
"format error: argument used for width or "
4965 "precision must be a non-negative integer");
4969#pragma GCC diagnostic push
4970#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4971 template<
typename _Context>
4972 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
4973 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4974 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4977 template<
int _Bits,
size_t _Nm>
4979 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4981 __UINT64_TYPE__ __packed_types = 0;
4982 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4983 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4984 return __packed_types;
4989 template<
typename _Context>
4990 class basic_format_args
4992 static constexpr int _S_packed_type_bits = 5;
4993 static constexpr int _S_packed_type_mask = 0b11111;
4994 static constexpr int _S_max_packed_args = 12;
4996 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4998 template<
typename... _Args>
4999 using _Store = __format::_Arg_store<_Context, _Args...>;
5001 template<
typename _Ctx,
typename... _Args>
5002 friend class __format::_Arg_store;
5004 using uint64_t = __UINT64_TYPE__;
5005 using _Format_arg = basic_format_arg<_Context>;
5006 using _Format_arg_val = __format::_Arg_value<_Context>;
5012 uint64_t _M_packed_size : 4;
5013 uint64_t _M_unpacked_size : 60;
5016 const _Format_arg_val* _M_values;
5017 const _Format_arg* _M_args;
5020 _GLIBCXX_CONSTEXPR_FORMAT
size_t
5021 _M_size() const noexcept
5022 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
5024 _GLIBCXX_CONSTEXPR_FORMAT
typename __format::_Arg_t
5025 _M_type(
size_t __i)
const noexcept
5027 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
5028 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
5031 template<
typename _Ctx,
typename... _Args>
5032 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
5033 make_format_args(_Args&...)
noexcept;
5036 template<
typename... _Args>
5037 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
5039 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
5042 template<
typename... _Args>
5043 _GLIBCXX_CONSTEXPR_FORMAT
5044 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
5046 [[nodiscard,__gnu__::__always_inline__]]
5047 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<_Context>
5048 get(
size_t __i)
const noexcept
5050 basic_format_arg<_Context> __arg;
5051 if (__i < _M_packed_size)
5053 __arg._M_type = _M_type(__i);
5054 __arg._M_val = _M_values[__i];
5056 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
5057 __arg = _M_args[__i];
5064 template<
typename _Context,
typename... _Args>
5065 basic_format_args(__format::_Arg_store<_Context, _Args...>)
5066 -> basic_format_args<_Context>;
5068 template<
typename _Context,
typename... _Args>
5069 _GLIBCXX_CONSTEXPR_FORMAT
auto
5070 make_format_args(_Args&... __fmt_args)
noexcept;
5073 template<
typename _Context,
typename... _Args>
5074 class __format::_Arg_store
5076 friend std::basic_format_args<_Context>;
5078 template<
typename _Ctx,
typename... _Argz>
5079 friend _GLIBCXX_CONSTEXPR_FORMAT
auto std::
5080#if _GLIBCXX_INLINE_VERSION
5083 make_format_args(_Argz&...)
noexcept;
5087 static constexpr bool _S_values_only
5088 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
5091 = __conditional_t<_S_values_only,
5092 __format::_Arg_value<_Context>,
5093 basic_format_arg<_Context>>;
5095 _Element_t _M_args[
sizeof...(_Args)];
5097 template<
typename _Tp>
5098 static _GLIBCXX_CONSTEXPR_FORMAT _Element_t
5099 _S_make_elt(_Tp& __v)
5101 using _Tq = remove_const_t<_Tp>;
5102 using _CharT =
typename _Context::char_type;
5103 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
5104 "std::formatter must be specialized for the type "
5105 "of each format arg");
5106 using __format::__formattable_with;
5107 if constexpr (is_const_v<_Tp>)
5108 if constexpr (!__formattable_with<_Tp, _Context>)
5109 if constexpr (__formattable_with<_Tq, _Context>)
5110 static_assert(__formattable_with<_Tp, _Context>,
5111 "format arg must be non-const because its "
5112 "std::formatter specialization has a "
5113 "non-const reference parameter");
5114 basic_format_arg<_Context> __arg(__v);
5115 if constexpr (_S_values_only)
5116 return __arg._M_val;
5121 template<
typename... _Tp>
5122 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
5123 [[__gnu__::__always_inline__]]
5124 _GLIBCXX_CONSTEXPR_FORMAT
5125 _Arg_store(_Tp&... __a) noexcept
5126 : _M_args{_S_make_elt(__a)...}
5130 template<
typename _Context>
5131 class __format::_Arg_store<_Context>
5134 template<
typename _Context>
5135 template<
typename... _Args>
5136 inline _GLIBCXX_CONSTEXPR_FORMAT
5137 basic_format_args<_Context>::
5138 basic_format_args(
const _Store<_Args...>& __store)
noexcept
5140 if constexpr (
sizeof...(_Args) == 0)
5143 _M_unpacked_size = 0;
5146 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
5149 _M_packed_size =
sizeof...(_Args);
5152 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
5154 _M_values = __store._M_args;
5161 _M_unpacked_size =
sizeof...(_Args);
5163 _M_args = __store._M_args;
5168 template<
typename _Context = format_context,
typename... _Args>
5169 [[nodiscard,__gnu__::__always_inline__]]
5170 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
5171 make_format_args(_Args&... __fmt_args)
noexcept
5173 using _Fmt_arg = basic_format_arg<_Context>;
5174 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
5175 _Normalize<_Args>...>;
5176 return _Store(__fmt_args...);
5179#ifdef _GLIBCXX_USE_WCHAR_T
5181 template<
typename... _Args>
5182 [[nodiscard,__gnu__::__always_inline__]]
5183 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
5184 make_wformat_args(_Args&... __args)
noexcept
5185 {
return std::make_format_args<wformat_context>(__args...); }
5191 template<
typename _Out,
typename _CharT,
typename _Context>
5192 _GLIBCXX_CONSTEXPR_FORMAT _Out
5193 __do_vformat_to(_Out, basic_string_view<_CharT>,
5194 const basic_format_args<_Context>&,
5195 const locale* =
nullptr);
5197 template<
typename _CharT>
struct __formatter_chrono;
5215 template<
typename _Out,
typename _CharT>
5218 class _GLIBCXX_NO_SPECIALIZATIONS basic_format_context
5220 static_assert( output_iterator<_Out, const _CharT&> );
5222 basic_format_args<basic_format_context> _M_args;
5224 __format::_Optional_locale _M_loc;
5226 _GLIBCXX_CONSTEXPR_FORMAT
5227 basic_format_context(basic_format_args<basic_format_context> __args,
5229 : _M_args(__args), _M_out(std::move(__out))
5232 _GLIBCXX_CONSTEXPR_FORMAT
5233 basic_format_context(basic_format_args<basic_format_context> __args,
5234 _Out __out,
const std::locale& __loc)
5235 : _M_args(__args), _M_out(std::move(__out)), _M_loc(__loc)
5241 basic_format_context(
const basic_format_context&) =
delete;
5242 basic_format_context& operator=(
const basic_format_context&) =
delete;
5244 template<
typename _Out2,
typename _CharT2,
typename _Context2>
5245 friend _GLIBCXX_CONSTEXPR_FORMAT _Out2
5246 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
5247 const basic_format_args<_Context2>&,
5250 friend __format::__formatter_chrono<_CharT>;
5253 ~basic_format_context() =
default;
5255 using iterator = _Out;
5256 using char_type = _CharT;
5257 template<
typename _Tp>
5258 using formatter_type = formatter<_Tp, _CharT>;
5261 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<basic_format_context>
5262 arg(
size_t __id)
const noexcept
5263 {
return _M_args.get(__id); }
5266 std::locale locale() {
return _M_loc.value(); }
5269 _GLIBCXX_CONSTEXPR_FORMAT iterator
5272 _GLIBCXX_CONSTEXPR_FORMAT
void
5273 advance_to(iterator __it) { _M_out =
std::move(__it); }
5276#if _GLIBCXX_EXTERN_TEMPLATE
5279 extern template basic_format_arg<format_context>::handle
5280 basic_format_arg<format_context>::_M_handle_unrecognized()
const;
5281# ifdef _GLIBCXX_USE_WCHAR_T
5282 extern template basic_format_arg<wformat_context>::handle
5283 basic_format_arg<wformat_context>::_M_handle_unrecognized()
const;
5286 template<
typename _Context>
5287 typename basic_format_arg<_Context>::handle
5288 basic_format_arg<_Context>::_M_handle_unrecognized()
const
5293 __throw_format_error(
"format error: unrecognized argument type");
5305 template<
typename _CharT>
5308 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
5310 typename basic_format_parse_context<_CharT>::_Scan_parse_context _M_pc;
5313 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5314 : _M_pc(__str, __nargs)
5317 constexpr iterator begin() const noexcept {
return _M_pc.begin(); }
5318 constexpr iterator end() const noexcept {
return _M_pc.end(); }
5323 basic_string_view<_CharT> __fmt = _M_fmt_str();
5325 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5327 _M_pc.advance_to(begin() + 1);
5328 _M_format_arg(_M_pc.next_arg_id());
5332 size_t __lbr = __fmt.find(
'{');
5333 size_t __rbr = __fmt.find(
'}');
5335 while (__fmt.size())
5337 auto __cmp = __lbr <=> __rbr;
5341 _M_pc.advance_to(end());
5346 if (__lbr + 1 == __fmt.size()
5347 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5348 __format::__unmatched_left_brace_in_format_string();
5349 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5350 iterator __last = begin() + __lbr + int(__is_escape);
5351 _M_on_chars(__last);
5352 _M_pc.advance_to(__last + 1);
5353 __fmt = _M_fmt_str();
5356 if (__rbr != __fmt.npos)
5358 __lbr = __fmt.find(
'{');
5362 _M_on_replacement_field();
5363 __fmt = _M_fmt_str();
5364 __lbr = __fmt.find(
'{');
5365 __rbr = __fmt.find(
'}');
5370 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5371 __format::__unmatched_right_brace_in_format_string();
5372 iterator __last = begin() + __rbr;
5373 _M_on_chars(__last);
5374 _M_pc.advance_to(__last + 1);
5375 __fmt = _M_fmt_str();
5376 if (__lbr != __fmt.npos)
5378 __rbr = __fmt.find(
'}');
5383 constexpr basic_string_view<_CharT>
5384 _M_fmt_str() const noexcept
5385 {
return {begin(), end()}; }
5387 constexpr virtual void _M_on_chars(iterator) { }
5389 constexpr void _M_on_replacement_field()
5391 auto __next = begin();
5395 __id = _M_pc.next_arg_id();
5396 else if (*__next ==
':')
5398 __id = _M_pc.next_arg_id();
5399 _M_pc.advance_to(++__next);
5403 auto [__i, __ptr] = __format::__parse_arg_id(begin(), end());
5404 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5405 __format::__invalid_arg_id_in_format_string();
5406 _M_pc.check_arg_id(__id = __i);
5409 _M_pc.advance_to(++__ptr);
5412 _M_pc.advance_to(__ptr);
5414 _M_format_arg(__id);
5415 if (begin() == end() || *begin() !=
'}')
5416 __format::__unmatched_left_brace_in_format_string();
5417 _M_pc.advance_to(begin() + 1);
5420 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5424 template<
typename _Out,
typename _CharT>
5425 class _Formatting_scanner :
public _Scanner<_CharT>
5428 _GLIBCXX_CONSTEXPR_FORMAT
5429 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5430 basic_string_view<_CharT> __str)
5431 : _Scanner<_CharT>(__str), _M_fc(__fc)
5435 basic_format_context<_Out, _CharT>& _M_fc;
5437 using iterator =
typename _Scanner<_CharT>::iterator;
5440 _M_on_chars(iterator __last)
override
5442 basic_string_view<_CharT> __str(this->begin(), __last);
5443 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5447 _M_format_arg(
size_t __id)
override
5449 using _Context = basic_format_context<_Out, _CharT>;
5450 using handle =
typename basic_format_arg<_Context>::handle;
5452 __format::__visit_format_arg([
this](
auto& __arg) {
5454 using _Formatter =
typename _Context::template formatter_type<_Type>;
5455 if constexpr (is_same_v<_Type, monostate>)
5456 __format::__invalid_arg_id_in_format_string();
5457 else if constexpr (is_same_v<_Type, handle>)
5458 __arg.format(this->_M_pc, this->_M_fc);
5459 else if constexpr (is_default_constructible_v<_Formatter>)
5462 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5463 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5466 static_assert(__format::__formattable_with<_Type, _Context>);
5467 }, _M_fc.arg(__id));
5471 template<
typename _CharT,
typename _Tp>
5473 __to_arg_t_enum() noexcept
5475 using _Context = __format::__format_context<_CharT>;
5476 using _Fmt_arg = basic_format_arg<_Context>;
5477 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5478 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5482 template<
typename _CharT,
typename... _Args>
5483 class _Checking_scanner :
public _Scanner<_CharT>
5486 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5487 "std::formatter must be specialized for each type being formatted");
5491 _Checking_scanner(basic_string_view<_CharT> __str)
5492 : _Scanner<_CharT>(__str, sizeof...(_Args))
5494#if __cpp_lib_format >= 202305L
5495 this->_M_pc._M_types = _M_types.data();
5501 _M_format_arg(
size_t __id)
override
5503 if constexpr (
sizeof...(_Args) != 0)
5505 if (__id <
sizeof...(_Args))
5507 _M_parse_format_spec<_Args...>(__id);
5511 __builtin_unreachable();
5514 template<
typename _Tp,
typename... _OtherArgs>
5516 _M_parse_format_spec(
size_t __id)
5520 formatter<_Tp, _CharT> __f;
5521 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5523 else if constexpr (
sizeof...(_OtherArgs) != 0)
5524 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5526 __builtin_unreachable();
5529#if __cpp_lib_format >= 202305L
5530 array<_Arg_t,
sizeof...(_Args)>
5531 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5535 template<typename _CharT, unsigned = __unicode::__literal_encoding_is_unicode<_CharT>()>
5536 _GLIBCXX_CONSTEXPR_FORMAT _Sink_iter<_CharT>
5537 __do_vformat_to(_Sink_iter<_CharT> __out, basic_string_view<_CharT> __fmt,
5538 __format_context<_CharT>& __ctx)
5540 if constexpr (is_same_v<_CharT, char>)
5542 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5544 bool __done =
false;
5545 __format::__visit_format_arg([&](
auto& __arg) {
5546 using _Tp = remove_cvref_t<
decltype(__arg)>;
5547 if constexpr (is_same_v<_Tp, bool>)
5549 size_t __len = 4 + !__arg;
5550 const char* __chars[] = {
"false",
"true" };
5551 if (
auto __res = __out._M_reserve(__len))
5553 ranges::copy_n(__chars[__arg], __len, __res.get());
5554 __res._M_bump(__len);
5558 else if constexpr (is_same_v<_Tp, char>)
5560 if (
auto __res = __out._M_reserve(1))
5562 *__res.get() = __arg;
5567 else if constexpr (is_integral_v<_Tp>)
5569 make_unsigned_t<_Tp> __uval;
5570 const bool __neg = __arg < 0;
5572 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5575 const auto __n = __detail::__to_chars_len(__uval);
5576 if (
auto __res = __out._M_reserve(__n + __neg))
5578 auto __ptr = __res.get();
5580 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5582 __res._M_bump(__n + __neg);
5586 else if constexpr (is_convertible_v<_Tp, string_view>)
5588 string_view __sv = __arg;
5589 if (
auto __res = __out._M_reserve(__sv.size()))
5591 ranges::copy(__sv, __res.get());
5592 __res._M_bump(__sv.size());
5602 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5603 __scanner._M_scan();
5612#if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE
5613 extern template _Sink_iter<char>
5614 __do_vformat_to<char, 1>(_Sink_iter<char>, string_view,
5616# ifdef _GLIBCXX_USE_WCHAR_T
5617 extern template _Sink_iter<wchar_t>
5618 __do_vformat_to<wchar_t, 1>(_Sink_iter<wchar_t>, wstring_view,
5623 template<
typename _Out,
typename _CharT,
typename _Context>
5624 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5625 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5626 const basic_format_args<_Context>& __args,
5627 const locale* __loc)
5629 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5631 auto __ctx = __loc ==
nullptr
5632 ? _Context(__args, __out)
5633 : _Context(__args, __out, *__loc);
5634 return __format::__do_vformat_to(__out, __fmt, __ctx);
5636 else if constexpr (__contiguous_char_iter<_CharT, _Out>)
5638 _Ptr_sink<_CharT> __sink(__out);
5639 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5640 return std::move(__sink)._M_finish(__out).out;
5644 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5645 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5646 return std::move(__sink)._M_finish().out;
5650 template<
typename _Out,
typename _CharT>
5651 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5652 __do_vformat_to_n(_Out __out, iter_difference_t<_Out> __n,
5653 basic_string_view<_CharT> __fmt,
5654 const type_identity_t<
5655 basic_format_args<__format_context<_CharT>>>& __args,
5656 const locale* __loc =
nullptr)
5658 if constexpr (__contiguous_char_iter<_CharT, _Out>)
5660 _Ptr_sink<_CharT> __sink(__out, __n);
5661 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5662 return std::move(__sink)._M_finish(__out);
5666 _Iter_sink<_CharT, _Out> __sink(
std::move(__out), __n);
5667 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5672#pragma GCC diagnostic pop
5677 template<
typename _CharT,
typename... _Args>
5678 template<
typename _Tp>
5679 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5681 basic_format_string<_CharT, _Args...>::
5682 basic_format_string(
const _Tp& __s) noexcept
5685 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5687 __scanner._M_scan();
5692 template<
typename _Out>
requires output_iterator<_Out, const char&>
5693 [[__gnu__::__always_inline__]]
5694 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5695 vformat_to(_Out __out, string_view __fmt, format_args __args)
5696 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5698#ifdef _GLIBCXX_USE_WCHAR_T
5699 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5700 [[__gnu__::__always_inline__]]
5701 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5702 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5703 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5706 template<
typename _Out>
requires output_iterator<_Out, const char&>
5707 [[__gnu__::__always_inline__]]
5709 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5712 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5715#ifdef _GLIBCXX_USE_WCHAR_T
5716 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5717 [[__gnu__::__always_inline__]]
5719 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5720 wformat_args __args)
5722 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5727 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5728 vformat(string_view __fmt, format_args __args)
5730 __format::_Str_sink<char> __buf;
5731 std::vformat_to(__buf.out(), __fmt, __args);
5735#ifdef _GLIBCXX_USE_WCHAR_T
5737 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5738 vformat(wstring_view __fmt, wformat_args __args)
5740 __format::_Str_sink<wchar_t> __buf;
5741 std::vformat_to(__buf.out(), __fmt, __args);
5748 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5750 __format::_Str_sink<char> __buf;
5751 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5755#ifdef _GLIBCXX_USE_WCHAR_T
5758 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5760 __format::_Str_sink<wchar_t> __buf;
5761 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5766 template<
typename... _Args>
5768 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5769 format(format_string<_Args...> __fmt, _Args&&... __args)
5770 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5772#ifdef _GLIBCXX_USE_WCHAR_T
5773 template<
typename... _Args>
5775 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5776 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5777 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5780 template<
typename... _Args>
5783 format(
const locale& __loc, format_string<_Args...> __fmt,
5786 return std::vformat(__loc, __fmt.get(),
5787 std::make_format_args(__args...));
5790#ifdef _GLIBCXX_USE_WCHAR_T
5791 template<
typename... _Args>
5794 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5797 return std::vformat(__loc, __fmt.get(),
5798 std::make_wformat_args(__args...));
5802 template<
typename _Out,
typename... _Args>
5803 requires output_iterator<_Out, const char&>
5804 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5805 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5807 return std::vformat_to(
std::move(__out), __fmt.get(),
5808 std::make_format_args(__args...));
5811#ifdef _GLIBCXX_USE_WCHAR_T
5812 template<
typename _Out,
typename... _Args>
5813 requires output_iterator<_Out, const wchar_t&>
5814 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5815 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5817 return std::vformat_to(
std::move(__out), __fmt.get(),
5818 std::make_wformat_args(__args...));
5822 template<
typename _Out,
typename... _Args>
5823 requires output_iterator<_Out, const char&>
5825 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5828 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5829 std::make_format_args(__args...));
5832#ifdef _GLIBCXX_USE_WCHAR_T
5833 template<
typename _Out,
typename... _Args>
5834 requires output_iterator<_Out, const wchar_t&>
5836 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5839 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5840 std::make_wformat_args(__args...));
5844 template<
typename _Out,
typename... _Args>
5845 requires output_iterator<_Out, const char&>
5846 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5847 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5848 format_string<_Args...> __fmt, _Args&&... __args)
5850 return __format::__do_vformat_to_n(
5852 std::make_format_args(__args...));
5855#ifdef _GLIBCXX_USE_WCHAR_T
5856 template<
typename _Out,
typename... _Args>
5857 requires output_iterator<_Out, const wchar_t&>
5858 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5859 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5860 wformat_string<_Args...> __fmt, _Args&&... __args)
5862 return __format::__do_vformat_to_n(
5864 std::make_wformat_args(__args...));
5868 template<
typename _Out,
typename... _Args>
5869 requires output_iterator<_Out, const char&>
5870 inline format_to_n_result<_Out>
5871 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5872 format_string<_Args...> __fmt, _Args&&... __args)
5874 return __format::__do_vformat_to_n(
5876 std::make_format_args(__args...), &__loc);
5879#ifdef _GLIBCXX_USE_WCHAR_T
5880 template<
typename _Out,
typename... _Args>
5881 requires output_iterator<_Out, const wchar_t&>
5882 inline format_to_n_result<_Out>
5883 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5884 wformat_string<_Args...> __fmt, _Args&&... __args)
5886 return __format::__do_vformat_to_n(
5888 std::make_wformat_args(__args...), &__loc);
5896 template<
typename _CharT>
5897 class _Counting_sink final :
public _Ptr_sink<_CharT>
5900 _GLIBCXX_CONSTEXPR_FORMAT
5901 _Counting_sink() : _Ptr_sink<_CharT>(nullptr, 0) { }
5903 [[__gnu__::__always_inline__]]
5904 _GLIBCXX_CONSTEXPR_FORMAT
size_t
5906 {
return this->_M_count + this->_M_used().size(); }
5909 template<
typename _CharT>
5910 class _Counting_sink :
public _Buf_sink<_CharT>
5912 size_t _M_count = 0;
5915 _M_overflow()
override
5917 if (!std::is_constant_evaluated())
5918 _M_count += this->_M_used().size();
5923 _Counting_sink() =
default;
5925 [[__gnu__::__always_inline__]]
5929 _Counting_sink::_M_overflow();
5937 template<
typename... _Args>
5939 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5940 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5942 __format::_Counting_sink<char> __buf;
5943 std::vformat_to(__buf.out(), __fmt.get(),
5944 std::make_format_args(__args...));
5945 return __buf.count();
5948#ifdef _GLIBCXX_USE_WCHAR_T
5949 template<
typename... _Args>
5951 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5952 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5954 __format::_Counting_sink<wchar_t> __buf;
5955 std::vformat_to(__buf.out(), __fmt.get(),
5956 std::make_wformat_args(__args...));
5957 return __buf.count();
5961 template<
typename... _Args>
5964 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5967 __format::_Counting_sink<char> __buf;
5968 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5969 std::make_format_args(__args...));
5970 return __buf.count();
5973#ifdef _GLIBCXX_USE_WCHAR_T
5974 template<
typename... _Args>
5977 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5980 __format::_Counting_sink<wchar_t> __buf;
5981 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5982 std::make_wformat_args(__args...));
5983 return __buf.count();
5987#if __glibcxx_format_ranges
5989 template<typename _Tp>
5990 consteval range_format
5993 using _Ref = ranges::range_reference_t<_Tp>;
5994 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5995 return range_format::disabled;
5996 else if constexpr (
requires {
typename _Tp::key_type; })
5998 if constexpr (
requires {
typename _Tp::mapped_type; })
6000 using _Up = remove_cvref_t<_Ref>;
6001 if constexpr (__is_pair<_Up>)
6002 return range_format::map;
6003 else if constexpr (__is_specialization_of<_Up, tuple>)
6004 if constexpr (tuple_size_v<_Up> == 2)
6005 return range_format::map;
6007 return range_format::set;
6010 return range_format::sequence;
6015 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
6016 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
6021 template<
typename _CharT,
typename _Out,
typename _Callback>
6022 _GLIBCXX_CONSTEXPR_FORMAT
6023 typename basic_format_context<_Out, _CharT>::iterator
6024 __format_padded(basic_format_context<_Out, _CharT>& __fc,
6025 const _Spec<_CharT>& __spec,
6028 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
6034 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
6036 const size_t __padwidth = __spec._M_get_width(__fc);
6037 if (__padwidth == 0)
6038 return __call(__fc);
6042 _GLIBCXX_CONSTEXPR_FORMAT
6043 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
6044 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
6047 _GLIBCXX_CONSTEXPR_FORMAT
void
6049 { _M_ctx =
nullptr; }
6051 _GLIBCXX_CONSTEXPR_FORMAT
6055 _M_ctx->advance_to(_M_out);
6059 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
6060 _Sink_iter<_CharT> _M_out;
6063 _Restore_out __restore(__fc);
6064 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
6065 __fc.advance_to(__sink.out());
6067 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
6068 __restore._M_disarm();
6073 template<
size_t _Pos,
typename _Tp,
typename _CharT>
6074 struct __indexed_formatter_storage
6081 basic_format_parse_context<_CharT> __pc({});
6082 if (_M_formatter.parse(__pc) != __pc.end())
6083 __format::__failed_to_parse_format_spec();
6086 template<
typename _Out>
6087 _GLIBCXX_CONSTEXPR_FORMAT
void
6088 _M_format(__maybe_const<_Tp, _CharT>& __elem,
6089 basic_format_context<_Out, _CharT>& __fc,
6090 basic_string_view<_CharT> __sep)
const
6092 if constexpr (_Pos != 0)
6093 __fc.advance_to(__format::__write(__fc.out(), __sep));
6094 __fc.advance_to(_M_formatter.format(__elem, __fc));
6097 [[__gnu__::__always_inline__]]
6101 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
6102 _M_formatter.set_debug_format();
6106 formatter<_Tp, _CharT> _M_formatter;
6109 template<
typename _CharT,
typename... _Tps>
6110 class __tuple_formatter
6112 using _String_view = basic_string_view<_CharT>;
6113 using _Seps = __format::_Separators<_CharT>;
6117 set_separator(basic_string_view<_CharT> __sep)
noexcept
6121 set_brackets(basic_string_view<_CharT> __open,
6122 basic_string_view<_CharT> __close)
noexcept
6130 constexpr typename basic_format_parse_context<_CharT>::iterator
6131 parse(basic_format_parse_context<_CharT>& __pc)
6133 auto __first = __pc.begin();
6134 const auto __last = __pc.end();
6135 __format::_Spec<_CharT> __spec{};
6137 auto __finished = [&]
6139 if (__first != __last && *__first !=
'}')
6143 _M_felems._M_parse();
6144 _M_felems.set_debug_format();
6151 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6155 __first = __spec._M_parse_width(__first, __last, __pc);
6159 if (*__first ==
'n')
6162 _M_open = _M_close = _String_view();
6164 else if (*__first ==
'm')
6167 if constexpr (
sizeof...(_Tps) == 2)
6169 _M_sep = _Seps::_S_colon();
6170 _M_open = _M_close = _String_view();
6173 __throw_format_error(
"format error: 'm' specifier requires range"
6174 " of pair or tuple of two elements");
6180 __format::__failed_to_parse_format_spec();
6184 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
6185 _GLIBCXX_CONSTEXPR_FORMAT
6186 typename basic_format_context<_Out, _CharT>::iterator
6187 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
6188 basic_format_context<_Out, _CharT>& __fc)
const
6189 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
6191 template<
typename _Out>
6192 _GLIBCXX_CONSTEXPR_FORMAT
6193 typename basic_format_context<_Out, _CharT>::iterator
6194 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
6195 basic_format_context<_Out, _CharT>& __fc)
const
6197 return __format::__format_padded(
6199 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
6201 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
6202 _M_felems._M_format(__elems..., __nfc, _M_sep);
6203 return __format::__write(__nfc.out(), _M_close);
6208 template<
size_t... _Ids>
6209 struct __formatters_storage
6210 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
6212 template<
size_t _Id,
typename _Up>
6213 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
6218 (_Base<_Ids, _Tps>::_M_parse(), ...);
6221 template<
typename _Out>
6222 _GLIBCXX_CONSTEXPR_FORMAT
void
6223 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
6224 basic_format_context<_Out, _CharT>& __fc,
6225 _String_view __sep)
const
6227 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
6233 (_Base<_Ids, _Tps>::set_debug_format(), ...);
6237 template<
size_t... _Ids>
6238 static _GLIBCXX_CONSTEXPR_FORMAT
auto
6239 _S_create_storage(index_sequence<_Ids...>)
6240 -> __formatters_storage<_Ids...>;
6242 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
6244 _Spec<_CharT> _M_spec{};
6245 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
6246 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
6247 _String_view _M_sep = _Seps::_S_comma();
6248 _Formatters _M_felems;
6251 template<
typename _Tp>
6252 concept __is_map_formattable
6253 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
6259 template<__format::__
char _CharT, formattable<_CharT> _Fp,
6260 formattable<_CharT> _Sp>
6261 struct formatter<
pair<_Fp, _Sp>, _CharT>
6262 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
6263 remove_cvref_t<_Sp>>
6266 using __maybe_const_pair
6267 = __conditional_t<formattable<const _Fp, _CharT>
6268 && formattable<const _Sp, _CharT>,
6269 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
6273 template<
typename _Out>
6274 _GLIBCXX_CONSTEXPR_FORMAT
6275 typename basic_format_context<_Out, _CharT>::iterator
6276 format(__maybe_const_pair& __p,
6277 basic_format_context<_Out, _CharT>& __fc)
const
6278 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
6281#if __glibcxx_print >= 202406L
6284 template<
typename _Fp,
typename _Sp>
6285 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
6286 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
6287 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
6290 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
6291 struct formatter<tuple<_Tps...>, _CharT>
6292 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
6295 using __maybe_const_tuple
6296 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
6297 const tuple<_Tps...>, tuple<_Tps...>>;
6301 template<
typename _Out>
6302 _GLIBCXX_CONSTEXPR_FORMAT
6303 typename basic_format_context<_Out, _CharT>::iterator
6304 format(__maybe_const_tuple& __t,
6305 basic_format_context<_Out, _CharT>& __fc)
const
6306 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
6309#if __glibcxx_print >= 202406L
6312 template<
typename... _Tps>
6313 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
6314 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
6318 template<
typename _Tp, __format::__
char _CharT>
6319 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
6320 class range_formatter
6322 using _String_view = basic_string_view<_CharT>;
6323 using _Seps = __format::_Separators<_CharT>;
6327 set_separator(basic_string_view<_CharT> __sep)
noexcept
6331 set_brackets(basic_string_view<_CharT> __open,
6332 basic_string_view<_CharT> __close)
noexcept
6338 constexpr formatter<_Tp, _CharT>&
6339 underlying() noexcept
6342 constexpr const formatter<_Tp, _CharT>&
6343 underlying() const noexcept
6348 constexpr typename basic_format_parse_context<_CharT>::iterator
6349 parse(basic_format_parse_context<_CharT>& __pc)
6351 auto __first = __pc.begin();
6352 const auto __last = __pc.end();
6353 __format::_Spec<_CharT> __spec{};
6354 bool __no_brace =
false;
6356 auto __finished = [&]
6357 {
return __first == __last || *__first ==
'}'; };
6359 auto __finalize = [&]
6365 auto __parse_val = [&](_String_view __nfs = _String_view())
6369 basic_format_parse_context<_CharT> __npc(__nfs);
6370 if (_M_fval.parse(__npc) != __npc.end())
6371 __format::__failed_to_parse_format_spec();
6372 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6373 _M_fval.set_debug_format();
6374 return __finalize();
6378 return __parse_val();
6380 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6382 return __parse_val();
6384 __first = __spec._M_parse_width(__first, __last, __pc);
6386 return __parse_val();
6388 if (*__first ==
'?')
6391 __spec._M_debug =
true;
6392 if (__finished() || *__first !=
's')
6393 __throw_format_error(
"format error: '?' is allowed only in"
6394 " combination with 's'");
6397 if (*__first ==
's')
6400 if constexpr (same_as<_Tp, _CharT>)
6402 __spec._M_type = __format::_Pres_s;
6404 return __finalize();
6405 __throw_format_error(
"format error: element format specifier"
6406 " cannot be provided when 's' specifier is used");
6409 __throw_format_error(
"format error: 's' specifier requires"
6410 " range of character types");
6414 return __parse_val();
6416 if (*__first ==
'n')
6419 _M_open = _M_close = _String_view();
6424 return __parse_val();
6426 if (*__first ==
'm')
6428 _String_view __m(__first, 1);
6430 if constexpr (__format::__is_map_formattable<_Tp>)
6432 _M_sep = _Seps::_S_comma();
6435 _M_open = _Seps::_S_braces().substr(0, 1);
6436 _M_close = _Seps::_S_braces().substr(1, 1);
6439 return __parse_val(__m);
6440 __throw_format_error(
"format error: element format specifier"
6441 " cannot be provided when 'm' specifier is used");
6444 __throw_format_error(
"format error: 'm' specifier requires"
6445 " range of pairs or tuples of two elements");
6449 return __parse_val();
6451 if (*__first ==
':')
6453 __pc.advance_to(++__first);
6454 __first = _M_fval.parse(__pc);
6458 return __finalize();
6460 __format::__failed_to_parse_format_spec();
6465 template<ranges::input_range _Rg,
typename _Out>
6466 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6467 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6468 _GLIBCXX_CONSTEXPR_FORMAT
6469 typename basic_format_context<_Out, _CharT>::iterator
6470 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6472 using _Range = remove_reference_t<_Rg>;
6473 if constexpr (ranges::contiguous_range<_Rg>)
6475 const span<__format::__maybe_const<_Tp, _CharT>>
6476 __spn(ranges::data(__rg),
size_t(ranges::distance(__rg)));
6477 return _M_format(__spn, __fc);
6479 else if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6480 return _M_format<const _Range>(__rg, __fc);
6482 return _M_format(__rg, __fc);
6486 template<ranges::input_range _Rg,
typename _Out>
6487 _GLIBCXX_CONSTEXPR_FORMAT
6488 typename basic_format_context<_Out, _CharT>::iterator
6489 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6491 if constexpr (same_as<_Tp, _CharT>)
6492 if (_M_spec._M_type == __format::_Pres_s)
6494 __format::__formatter_str __fstr(_M_spec);
6495 return __fstr._M_format_range(__rg, __fc);
6497 return __format::__format_padded(
6499 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6500 {
return _M_format_elems(__rg, __nfc); });
6504 template<ranges::input_range _Rg,
typename _Out>
6505 _GLIBCXX_CONSTEXPR_FORMAT
6506 typename basic_format_context<_Out, _CharT>::iterator
6507 _M_format_elems(_Rg& __rg,
6508 basic_format_context<_Out, _CharT>& __fc)
const
6510 auto __out = __format::__write(__fc.out(), _M_open);
6512 auto __first = ranges::begin(__rg);
6513 auto const __last = ranges::end(__rg);
6514 if (__first == __last)
6515 return __format::__write(__out, _M_close);
6517 __fc.advance_to(__out);
6518 __out = _M_fval.format(*__first, __fc);
6519 for (++__first; __first != __last; ++__first)
6521 __out = __format::__write(__out, _M_sep);
6522 __fc.advance_to(__out);
6523 __out = _M_fval.format(*__first, __fc);
6526 return __format::__write(__out, _M_close);
6529 __format::_Spec<_CharT> _M_spec{};
6530 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6531 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6532 _String_view _M_sep = _Seps::_S_comma();
6533 formatter<_Tp, _CharT> _M_fval;
6541 template<ranges::input_range _Rg, __format::__
char _CharT>
6542 requires (format_kind<_Rg> != range_format::disabled)
6543 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6544 struct formatter<_Rg, _CharT>
6547 static const bool _S_range_format_is_string =
6548 (format_kind<_Rg> == range_format::string)
6549 || (format_kind<_Rg> == range_format::debug_string);
6550 using _Vt = remove_cvref_t<
6551 ranges::range_reference_t<
6552 __format::__maybe_const_range<_Rg, _CharT>>>;
6554 static consteval bool _S_is_correct()
6556 if constexpr (_S_range_format_is_string)
6557 static_assert(same_as<_Vt, _CharT>);
6561 static_assert(_S_is_correct());
6564 constexpr formatter() noexcept
6566 using _Seps = __format::_Separators<_CharT>;
6567 if constexpr (format_kind<_Rg> == range_format::map)
6569 static_assert(__format::__is_map_formattable<_Vt>);
6570 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6571 _Seps::_S_braces().substr(1, 1));
6572 _M_under.underlying().set_brackets({}, {});
6573 _M_under.underlying().set_separator(_Seps::_S_colon());
6575 else if constexpr (format_kind<_Rg> == range_format::set)
6576 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6577 _Seps::_S_braces().substr(1, 1));
6581 set_separator(basic_string_view<_CharT> __sep)
noexcept
6582 requires (format_kind<_Rg> == range_format::sequence)
6583 { _M_under.set_separator(__sep); }
6586 set_brackets(basic_string_view<_CharT> __open,
6587 basic_string_view<_CharT> __close)
noexcept
6588 requires (format_kind<_Rg> == range_format::sequence)
6589 { _M_under.set_brackets(__open, __close); }
6593 constexpr typename basic_format_parse_context<_CharT>::iterator
6594 parse(basic_format_parse_context<_CharT>& __pc)
6596 auto __res = _M_under.parse(__pc);
6597 if constexpr (format_kind<_Rg> == range_format::debug_string)
6598 _M_under.set_debug_format();
6604 template<
typename _Out>
6605 _GLIBCXX_CONSTEXPR_FORMAT
6606 typename basic_format_context<_Out, _CharT>::iterator
6607 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6608 basic_format_context<_Out, _CharT>& __fc)
const
6610 if constexpr (_S_range_format_is_string)
6611 return _M_under._M_format_range(__rg, __fc);
6613 return _M_under.format(__rg, __fc);
6617 using _Formatter_under
6618 = __conditional_t<_S_range_format_is_string,
6619 __format::__formatter_str<_CharT>,
6620 range_formatter<_Vt, _CharT>>;
6621 _Formatter_under _M_under;
6624#if __glibcxx_print >= 202406L
6625 template<ranges::input_range _Rg>
6626 requires (format_kind<_Rg> != range_format::disabled)
6627 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6631#undef _GLIBCXX_WIDEN
6632#undef _GLIBCXX_CONSTEXPR_FORMAT
6634_GLIBCXX_END_NAMESPACE_VERSION
6637#pragma GCC diagnostic pop
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
_Tp arg(const complex< _Tp > &)
Return phase angle of z.
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
const _Facet & use_facet(const locale &__loc)
Return a facet.
basic_string< char > string
A string of char.
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
chars_format
floating-point format for primitive numerical conversion
constexpr auto ssize(const _Container &__cont) noexcept(noexcept(__cont.size())) -> common_type_t< ptrdiff_t, make_signed_t< decltype(__cont.size())> >
Return the size of a container, as a signed integer.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
constexpr size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
constexpr void reserve(size_type __res_arg)
Attempt to preallocate enough memory for specified number of characters.
constexpr const _CharT * data() const noexcept
Return const pointer to contents.
constexpr basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
constexpr void __resize_and_overwrite(size_type __n, _Operation __op)
Non-standard version of resize_and_overwrite for C++11 and above.
constexpr basic_string & append(const basic_string &__str)
Append a string to this string.
constexpr iterator insert(const_iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
constexpr size_type capacity() const noexcept
constexpr bool empty() const noexcept
One of two subclasses of exception.