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)
3545 using _GLIBCXX_STD_C::vector;
3549 template<
typename _Seq>
3550 class _Seq_sink :
public _Buf_sink<typename _Seq::value_type>
3552 using _CharT =
typename _Seq::value_type;
3557 _GLIBCXX_CONSTEXPR_FORMAT
void
3558 _M_overflow()
override
3560 auto __s = this->_M_used();
3561 if (__s.empty()) [[unlikely]]
3565 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3567 if constexpr (__is_specialization_of<_Seq, basic_string>)
3568 _M_seq.append(__s.data(), __s.size());
3570 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3576 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3577 _M_reserve(
size_t __n)
override
3586 if constexpr (__is_specialization_of<_Seq, basic_string>
3587 || __is_specialization_of<_Seq, vector>)
3590 if (this->_M_used().size()) [[unlikely]]
3591 _Seq_sink::_M_overflow();
3594 const auto __sz = _M_seq.size();
3595 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3596 _M_seq.__resize_and_overwrite(__sz + __n,
3597 [](
auto,
auto __n2) {
3601 _M_seq.resize(__sz + __n);
3605 this->_M_reset(_M_seq, __sz);
3609 return _Sink<_CharT>::_M_reserve(__n);
3612 _GLIBCXX_CONSTEXPR_FORMAT
void
3613 _M_bump(
size_t __n)
override
3615 if constexpr (__is_specialization_of<_Seq, basic_string>
3616 || __is_specialization_of<_Seq, vector>)
3618 auto __s = this->_M_used();
3619 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3621 _M_seq.resize(__s.size() + __n);
3623 this->_M_reset(this->_M_buf);
3627 _GLIBCXX_CONSTEXPR_FORMAT
void
3628 _M_trim(span<const _CharT> __s)
3629 requires __is_specialization_of<_Seq, basic_string>
3631 _GLIBCXX_DEBUG_ASSERT(__s.data() == this->_M_buf
3632 || __s.data() == _M_seq.data());
3633 if (__s.data() == _M_seq.data())
3634 _M_seq.resize(__s.size());
3636 this->_M_reset(this->_M_buf, __s.size());
3643 [[__gnu__::__always_inline__]]
3644 _GLIBCXX_CONSTEXPR_FORMAT
3645 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3648 _GLIBCXX_CONSTEXPR_FORMAT
3649 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3650 : _M_seq(std::move(__s))
3653 using _Sink<_CharT>::out;
3655 _GLIBCXX_CONSTEXPR_FORMAT _Seq
3658 if (this->_M_used().size() != 0)
3659 _Seq_sink::_M_overflow();
3665 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3668 auto __s = this->_M_used();
3671 if (__s.size() != 0)
3672 _Seq_sink::_M_overflow();
3678 _GLIBCXX_CONSTEXPR_FORMAT basic_string_view<_CharT>
3681 auto __span = _M_span();
3682 return basic_string_view<_CharT>(__span.data(), __span.size());
3686 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3688 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3694 template<
typename _CharT,
typename _OutIter>
3695 class _Iter_sink :
public _Buf_sink<_CharT>
3698 iter_difference_t<_OutIter> _M_max;
3701 size_t _M_count = 0;
3703 _GLIBCXX_CONSTEXPR_FORMAT
void
3704 _M_overflow()
override
3706 auto __s = this->_M_used();
3708 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3709 else if (_M_count <
static_cast<size_t>(_M_max))
3711 auto __max = _M_max - _M_count;
3712 span<_CharT> __first;
3713 if (__max < __s.size())
3714 __first = __s.first(
static_cast<size_t>(__max));
3717 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3720 _M_count += __s.size();
3723 _GLIBCXX_CONSTEXPR_FORMAT
bool
3724 _M_discarding()
const override
3732 [[__gnu__::__always_inline__]]
3733 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3734 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3735 : _M_out(std::move(__out)), _M_max(__max)
3738 using _Sink<_CharT>::out;
3740 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3743 if (this->_M_used().size() != 0)
3744 _Iter_sink::_M_overflow();
3745 iter_difference_t<_OutIter> __count(_M_count);
3757 template<
typename _CharT>
3758 class _Ptr_sink :
public _Sink<_CharT>
3760 static constexpr size_t _S_no_limit = size_t(-1);
3764 size_t _M_count = 0;
3769 _GLIBCXX_CONSTEXPR_FORMAT
void
3770 _M_overflow()
override
3772 if (this->_M_unused().size() != 0)
3775 auto __s = this->_M_used();
3777 if (_M_max != _S_no_limit)
3779 _M_count += __s.size();
3783 this->_M_reset(this->_M_buf);
3789 _M_rebuf(__s.data(), __s.size() + 1024, __s.size());
3793 _GLIBCXX_CONSTEXPR_FORMAT
bool
3794 _M_discarding()
const override
3801 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3802 _M_reserve(
size_t __n)
final
3804 auto __avail = this->_M_unused();
3805 if (__n > __avail.size())
3807 if (_M_max != _S_no_limit)
3810 auto __s = this->_M_used();
3811 _M_rebuf(__s.data(), __s.size() + __n, __s.size());
3817 template<
typename _IterDifference>
3818 static _GLIBCXX_CONSTEXPR_FORMAT
size_t
3819 _S_trim_max(_IterDifference __max)
3823 if constexpr (!is_integral_v<_IterDifference> ||
sizeof(__max) >
sizeof(size_t))
3825 if (_IterDifference((
size_t)-1) < __max)
3827 return size_t(__max);
3830 [[__gnu__::__always_inline__]]
3831 _GLIBCXX_CONSTEXPR_FORMAT
void
3832 _M_rebuf(_CharT* __ptr,
size_t __total,
size_t __inuse = 0)
3834 std::span<_CharT> __span(__ptr, __total);
3835 this->_M_reset(__span, __inuse);
3839 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3840 _Ptr_sink(_CharT* __ptr,
size_t __n = _S_no_limit) noexcept
3841 : _Sink<_CharT>(_M_buf), _M_max(__n)
3845 else if (__n != _S_no_limit)
3846 _M_rebuf(__ptr, __n);
3847#if __has_builtin(__builtin_dynamic_object_size)
3848 else if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3849 _M_rebuf(__ptr, __bytes /
sizeof(_CharT));
3854 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3855 __n = (1024 - __off) /
sizeof(_CharT);
3856 if (__n > 0) [[likely]]
3857 _M_rebuf(__ptr, __n);
3863 template<contiguous_iterator _OutIter>
3864 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3865 _Ptr_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1)
3866 : _Ptr_sink(std::
to_address(__out), _S_trim_max(__n))
3869 template<contiguous_iterator _OutIter>
3870 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3871 _M_finish(_OutIter __first)
const
3873 auto __s = this->_M_used();
3874 if (__s.data() == _M_buf)
3877 iter_difference_t<_OutIter> __m(_M_max);
3878 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3879 return { __first + __m, __count };
3883 iter_difference_t<_OutIter> __count(__s.size());
3884 return { __first + __count, __count };
3889 template<
typename _CharT,
typename _OutIter>
3890 concept __contiguous_char_iter
3891 = contiguous_iterator<_OutIter>
3892 && same_as<iter_value_t<_OutIter>, _CharT>;
3903 template<
typename _Out,
typename _CharT>
3904 class _Padding_sink :
public _Str_sink<_CharT>
3909 size_t _M_printwidth;
3911 [[__gnu__::__always_inline__]]
3912 _GLIBCXX_CONSTEXPR_FORMAT
bool
3914 {
return _M_printwidth >= _M_maxwidth; }
3916 [[__gnu__::__always_inline__]]
3917 _GLIBCXX_CONSTEXPR_FORMAT
bool
3918 _M_buffering()
const
3920 if (_M_printwidth < _M_padwidth)
3922 if (_M_maxwidth != (
size_t)-1)
3923 return _M_printwidth < _M_maxwidth;
3927 _GLIBCXX_CONSTEXPR_FORMAT
void
3928 _M_sync_discarding()
3930 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3931 if (_M_out._M_discarding())
3932 _M_maxwidth = _M_printwidth;
3935 _GLIBCXX_CONSTEXPR_FORMAT
void
3938 span<_CharT> __new = this->_M_used();
3939 basic_string_view<_CharT> __str(__new.data(), __new.size());
3940 _M_out = __format::__write(
std::move(_M_out), __str);
3941 _M_sync_discarding();
3945 _GLIBCXX_CONSTEXPR_FORMAT
bool
3948 auto __str = this->view();
3950 _M_printwidth = __format::__truncate(__str, _M_maxwidth);
3952 this->_M_trim(__str);
3958 if (_M_printwidth >= _M_padwidth)
3960 _M_out = __format::__write(
std::move(_M_out), __str);
3961 _M_sync_discarding();
3966 _Str_sink<_CharT>::_M_overflow();
3969 this->_M_reset(this->_M_buf);
3973 _GLIBCXX_CONSTEXPR_FORMAT
bool
3974 _M_update(
size_t __new)
3976 _M_printwidth += __new;
3978 if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
3979 return _M_force_update();
3983 _GLIBCXX_CONSTEXPR_FORMAT
void
3984 _M_overflow()
override
3990 else if (!_M_buffering())
3994 else if (_M_update(this->_M_used().size()))
3995 _Str_sink<_CharT>::_M_overflow();
3998 _GLIBCXX_CONSTEXPR_FORMAT
bool
3999 _M_discarding()
const override
4000 {
return _M_ignoring(); }
4002 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
4003 _M_reserve(
size_t __n)
override
4008 else if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4009 if (!_M_buffering())
4012 if (!this->_M_used().empty())
4015 if (
auto __reserved = _M_out._M_reserve(__n))
4018 return _Sink<_CharT>::_M_reserve(__n);
4021 _GLIBCXX_CONSTEXPR_FORMAT
void
4022 _M_bump(
size_t __n)
override
4029 _Sink<_CharT>::_M_bump(__n);
4035 [[__gnu__::__always_inline__]]
4036 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4037 _Padding_sink(_Out __out,
size_t __padwidth,
size_t __maxwidth)
4038 : _M_padwidth(__padwidth), _M_maxwidth(__maxwidth),
4039 _M_out(std::move(__out)), _M_printwidth(0)
4040 { _M_sync_discarding(); }
4042 [[__gnu__::__always_inline__]]
4043 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4044 _Padding_sink(_Out __out,
size_t __padwidth)
4045 : _Padding_sink(std::move(__out), __padwidth, (size_t)-1)
4048 _GLIBCXX_CONSTEXPR_FORMAT _Out
4049 _M_finish(_Align __align,
char32_t __fill_char)
4052 if (
auto __rem = this->_M_used().size())
4056 else if (!_M_buffering())
4062 if (!_M_buffering() || !_M_force_update())
4064 if (_M_printwidth >= _M_padwidth)
4067 const auto __str = this->view();
4068 if (_M_printwidth >= _M_padwidth)
4069 return __format::__write(
std::move(_M_out), __str);
4071 const size_t __nfill = _M_padwidth - _M_printwidth;
4072 return __format::__write_padded(
std::move(_M_out), __str,
4073 __align, __nfill, __fill_char);
4077 template<
typename _Out,
typename _CharT>
4078 class _Escaping_sink :
public _Buf_sink<_CharT>
4080 using _Esc = _Escapes<_CharT>;
4083 _Term_char _M_term : 2;
4084 unsigned _M_prev_escape : 1;
4085 unsigned _M_out_discards : 1;
4087 _GLIBCXX_CONSTEXPR_FORMAT
void
4088 _M_sync_discarding()
4090 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4091 _M_out_discards = _M_out._M_discarding();
4094 _GLIBCXX_CONSTEXPR_FORMAT
void
4097 span<_CharT> __bytes = this->_M_used();
4098 basic_string_view<_CharT> __str(__bytes.data(), __bytes.size());
4101 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4103 bool __prev_escape = _M_prev_escape;
4104 _M_out = __format::__write_escaped_unicode_part(
4105 std::move(_M_out), __str, __prev_escape, _M_term);
4106 _M_prev_escape = __prev_escape;
4108 __rem = __str.size();
4109 if (__rem > 0 && __str.data() != this->_M_buf) [[unlikely]]
4110 ranges::move(__str, this->_M_buf);
4113 _M_out = __format::__write_escaped_ascii(
4116 this->_M_reset(this->_M_buf, __rem);
4117 _M_sync_discarding();
4120 _GLIBCXX_CONSTEXPR_FORMAT
void
4121 _M_overflow()
override
4123 if (_M_out_discards)
4129 _GLIBCXX_CONSTEXPR_FORMAT
bool
4130 _M_discarding()
const override
4131 {
return _M_out_discards; }
4134 [[__gnu__::__always_inline__]]
4135 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4136 _Escaping_sink(_Out __out, _Term_char __term)
4137 : _M_out(std::move(__out)), _M_term(__term),
4138 _M_prev_escape(true), _M_out_discards(false)
4140 _M_out = __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4141 _M_sync_discarding();
4144 _GLIBCXX_CONSTEXPR_FORMAT _Out
4147 if (_M_out_discards)
4150 if (!this->_M_used().empty())
4153 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4154 if (
auto __rem = this->_M_used(); !__rem.empty())
4156 basic_string_view<_CharT> __str(__rem.data(), __rem.size());
4157 _M_out = __format::__write_escape_seqs(
std::move(_M_out), __str);
4160 return __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4164 template<
typename _Context>
4167 using _CharT =
typename _Context::char_type;
4171 using _CharT =
typename _Context::char_type;
4172 using _Func = void(*)(basic_format_parse_context<_CharT>&,
4173 _Context&,
const void*);
4176 template<
typename _Tp>
4177 using __maybe_const_t
4178 = __conditional_t<__formattable_with<const _Tp, _Context>,
4181 template<
typename _Tq>
4182 static _GLIBCXX_CONSTEXPR_FORMAT
void
4183 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
4184 _Context& __format_ctx,
const void* __ptr)
4186 using _Td = remove_const_t<_Tq>;
4187 typename _Context::template formatter_type<_Td> __f;
4188 __parse_ctx.advance_to(__f.parse(__parse_ctx));
4189 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
4190 __format_ctx.advance_to(__f.format(__val, __format_ctx));
4193 template<
typename _Tp>
4194 requires (!is_same_v<remove_cv_t<_Tp>, handle>)
4195 explicit _GLIBCXX_CONSTEXPR_FORMAT
4196 handle(_Tp& __val) noexcept
4197 : _M_ptr(__builtin_addressof(__val))
4198 , _M_func(&_S_format<__maybe_const_t<_Tp>>)
4201 friend class basic_format_arg<_Context>;
4204 handle(
const handle&) =
default;
4205 handle& operator=(
const handle&) =
default;
4207 [[__gnu__::__always_inline__]]
4208 void _GLIBCXX_CONSTEXPR_FORMAT
4209 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
4210 { _M_func(__pc, __fc, this->_M_ptr); }
4225 unsigned long long _M_ull;
4228#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4229 long double _M_ldbl;
4232 __ieee128 _M_ieee128;
4234#ifdef __SIZEOF_FLOAT128__
4235 __float128 _M_float128;
4237 const _CharT* _M_str;
4238 basic_string_view<_CharT> _M_sv;
4241#ifdef __SIZEOF_INT128__
4243 unsigned __int128 _M_u128;
4245#ifdef __BFLT16_DIG__
4259 [[__gnu__::__always_inline__]]
4260 _GLIBCXX_CONSTEXPR_FORMAT
4261 _Arg_value() : _M_none() { }
4264 template<
typename _Tp>
4265 _GLIBCXX_CONSTEXPR_FORMAT
4266 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
4267 { _S_get<_Tp>() = __val; }
4272 template<
typename _Tp,
typename _Self,
typename... _Value>
4273 [[__gnu__::__always_inline__]]
4274 static _GLIBCXX_CONSTEXPR_FORMAT
auto&
4275 _S_access(_Self& __u, _Value... __value)
noexcept
4277 static_assert(
sizeof...(_Value) <= 1);
4278 if constexpr (is_same_v<_Tp, bool>)
4279 return (__u._M_bool = ... = __value);
4280 else if constexpr (is_same_v<_Tp, _CharT>)
4281 return (__u._M_c = ... = __value);
4282 else if constexpr (is_same_v<_Tp, int>)
4283 return (__u._M_i = ... = __value);
4284 else if constexpr (is_same_v<_Tp, unsigned>)
4285 return (__u._M_u = ... = __value);
4286 else if constexpr (is_same_v<_Tp, long long>)
4287 return (__u._M_ll = ... = __value);
4288 else if constexpr (is_same_v<_Tp, unsigned long long>)
4289 return (__u._M_ull = ... = __value);
4290 else if constexpr (is_same_v<_Tp, float>)
4291 return (__u._M_flt = ... = __value);
4292 else if constexpr (is_same_v<_Tp, double>)
4293 return (__u._M_dbl = ... = __value);
4294#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4295 else if constexpr (is_same_v<_Tp, long double>)
4296 return (__u._M_ldbl = ... = __value);
4298 else if constexpr (is_same_v<_Tp, __ibm128>)
4299 return (__u._M_ibm128 = ... = __value);
4300 else if constexpr (is_same_v<_Tp, __ieee128>)
4301 return (__u._M_ieee128 = ... = __value);
4303#ifdef __SIZEOF_FLOAT128__
4304 else if constexpr (is_same_v<_Tp, __float128>)
4305 return (__u._M_float128 = ... = __value);
4307 else if constexpr (is_same_v<_Tp, const _CharT*>)
4308 return (__u._M_str = ... = __value);
4309 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4310 return (__u._M_sv = ... = __value);
4311 else if constexpr (is_same_v<_Tp, const void*>)
4312 return (__u._M_ptr = ... = __value);
4313#ifdef __SIZEOF_INT128__
4314 else if constexpr (is_same_v<_Tp, __int128>)
4315 return (__u._M_i128 = ... = __value);
4316 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4317 return (__u._M_u128 = ... = __value);
4319#ifdef __BFLT16_DIG__
4320 else if constexpr (is_same_v<_Tp, __bflt16_t>)
4321 return (__u._M_bf16 = ... = __value);
4324 else if constexpr (is_same_v<_Tp, _Float16>)
4325 return (__u._M_f16 = ... = __value);
4328 else if constexpr (is_same_v<_Tp, _Float32>)
4329 return (__u._M_f32 = ... = __value);
4332 else if constexpr (is_same_v<_Tp, _Float64>)
4333 return (__u._M_f64 = ... = __value);
4335 else if constexpr (is_same_v<_Tp, handle>)
4336 return __u._M_handle;
4338 __builtin_unreachable();
4341 template<
typename _Tp>
4342 [[__gnu__::__always_inline__]]
4343 _GLIBCXX_CONSTEXPR_FORMAT
auto&
4345 {
return _S_access<_Tp>(*
this); }
4347 template<
typename _Tp>
4348 [[__gnu__::__always_inline__]]
4349 _GLIBCXX_CONSTEXPR_FORMAT
const auto&
4350 _M_get() const noexcept
4351 {
return _S_access<_Tp>(*
this); }
4353 template<
typename _Tp>
4354 [[__gnu__::__always_inline__]]
4355 _GLIBCXX_CONSTEXPR_FORMAT
void
4356 _M_set(_Tp __v)
noexcept
4359 if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4360 std::construct_at(&_M_sv, __v);
4361 else if constexpr (is_same_v<_Tp, handle>)
4362 std::construct_at(&_M_handle, __v);
4366 _S_access<_Tp>(*
this, __v);
4371 template<
typename _Context,
typename... _Args>
4374 template<
typename _Visitor,
typename _Ctx>
4375 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4376 __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4378 template<
typename _Ch,
typename _Tp>
4380 __to_arg_t_enum() noexcept;
4384 template<typename _Context>
4385 class basic_format_arg
4387 using _CharT =
typename _Context::char_type;
4390 using handle = __format::_Arg_value<_Context>::handle;
4392 [[__gnu__::__always_inline__]]
4393 _GLIBCXX_CONSTEXPR_FORMAT
4394 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
4396 [[nodiscard,__gnu__::__always_inline__]]
4397 explicit _GLIBCXX_CONSTEXPR_FORMAT
operator bool() const noexcept
4398 {
return _M_type != __format::_Arg_none; }
4400#if __cpp_lib_format >= 202306L
4401 template<
typename _Visitor>
4402 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4403 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4406 template<
typename _Res,
typename _Visitor>
4407 _GLIBCXX_CONSTEXPR_FORMAT _Res
4408 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4413 template<
typename _Ctx>
4414 friend class basic_format_args;
4416 template<
typename _Ctx,
typename... _Args>
4417 friend class __format::_Arg_store;
4419 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
4421 __format::_Arg_value<_Context> _M_val;
4422 __format::_Arg_t _M_type;
4427 template<
typename _Tp>
4428 static consteval auto
4431 using _Td = remove_const_t<_Tp>;
4432 if constexpr (is_same_v<_Td, bool>)
4433 return type_identity<bool>();
4434 else if constexpr (is_same_v<_Td, _CharT>)
4435 return type_identity<_CharT>();
4436 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
4437 return type_identity<_CharT>();
4438#ifdef __SIZEOF_INT128__
4439 else if constexpr (is_same_v<_Td, __int128>)
4440 return type_identity<__int128>();
4441 else if constexpr (is_same_v<_Td, unsigned __int128>)
4442 return type_identity<unsigned __int128>();
4444 else if constexpr (__is_signed_integer<_Td>::value)
4446 if constexpr (
sizeof(_Td) <=
sizeof(int))
4447 return type_identity<int>();
4448 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
4449 return type_identity<long long>();
4451 else if constexpr (__is_unsigned_integer<_Td>::value)
4453 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
4454 return type_identity<unsigned>();
4455 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
4456 return type_identity<unsigned long long>();
4458 else if constexpr (is_same_v<_Td, float>)
4459 return type_identity<float>();
4460 else if constexpr (is_same_v<_Td, double>)
4461 return type_identity<double>();
4462#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4463 else if constexpr (is_same_v<_Td, long double>)
4464 return type_identity<long double>();
4466 else if constexpr (is_same_v<_Td, __ibm128>)
4467 return type_identity<__ibm128>();
4468 else if constexpr (is_same_v<_Td, __ieee128>)
4469 return type_identity<__ieee128>();
4471#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4472 else if constexpr (is_same_v<_Td, __float128>)
4473 return type_identity<__float128>();
4475#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4476 else if constexpr (is_same_v<_Td, __format::__bflt16_t>)
4477 return type_identity<__format::__bflt16_t>();
4479#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4480 else if constexpr (is_same_v<_Td, _Float16>)
4481 return type_identity<_Float16>();
4483#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4484 else if constexpr (is_same_v<_Td, _Float32>)
4485 return type_identity<_Float32>();
4487#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4488 else if constexpr (is_same_v<_Td, _Float64>)
4489 return type_identity<_Float64>();
4491 else if constexpr (__is_specialization_of<_Td, basic_string_view>
4492 || __is_specialization_of<_Td, basic_string>)
4494 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
4495 return type_identity<basic_string_view<_CharT>>();
4497 return type_identity<handle>();
4499 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
4500 return type_identity<const _CharT*>();
4501 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
4502 return type_identity<const _CharT*>();
4503 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
4504 return type_identity<const void*>();
4505 else if constexpr (is_same_v<_Td, nullptr_t>)
4506 return type_identity<const void*>();
4508 return type_identity<handle>();
4512 template<
typename _Tp>
4513 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
4516 template<
typename _Tp>
4517 static consteval __format::_Arg_t
4520 using namespace __format;
4521 if constexpr (is_same_v<_Tp, bool>)
4523 else if constexpr (is_same_v<_Tp, _CharT>)
4525 else if constexpr (is_same_v<_Tp, int>)
4527 else if constexpr (is_same_v<_Tp, unsigned>)
4529 else if constexpr (is_same_v<_Tp, long long>)
4531 else if constexpr (is_same_v<_Tp, unsigned long long>)
4533 else if constexpr (is_same_v<_Tp, float>)
4535 else if constexpr (is_same_v<_Tp, double>)
4537#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4538 else if constexpr (is_same_v<_Tp, long double>)
4542 else if constexpr (is_same_v<_Tp, __ibm128>)
4544 else if constexpr (is_same_v<_Tp, __ieee128>)
4545 return _Arg_ieee128;
4547#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4548 else if constexpr (is_same_v<_Tp, __float128>)
4549 return _Arg_float128;
4551#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4552 else if constexpr (is_same_v<_Tp, __format::__bflt16_t>)
4555#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4556 else if constexpr (is_same_v<_Tp, _Float16>)
4559#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4560 else if constexpr (is_same_v<_Tp, _Float32>)
4563#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4564 else if constexpr (is_same_v<_Tp, _Float64>)
4567 else if constexpr (is_same_v<_Tp, const _CharT*>)
4569 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4571 else if constexpr (is_same_v<_Tp, const void*>)
4573#ifdef __SIZEOF_INT128__
4574 else if constexpr (is_same_v<_Tp, __int128>)
4576 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4579 else if constexpr (is_same_v<_Tp, handle>)
4583 template<
typename _Tp>
4584 _GLIBCXX_CONSTEXPR_FORMAT
void
4585 _M_set(_Tp __v)
noexcept
4587 _M_type = _S_to_enum<_Tp>();
4591 template<
typename _Tp>
4592 requires __format::__formattable_with<_Tp, _Context>
4593 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4594 basic_format_arg(_Tp& __v)
noexcept
4596 using _Td = _Normalize<_Tp>;
4597 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4598 _M_set(_Td{__v.data(), __v.size()});
4599 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4600 && is_same_v<_CharT, wchar_t>)
4601 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4603 _M_set(
static_cast<_Td
>(__v));
4606 template<
typename _Ctx,
typename... _Argz>
4607 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4608 make_format_args(_Argz&...)
noexcept;
4610 template<
typename _Visitor,
typename _Ctx>
4611 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4612 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4614 template<
typename _Visitor,
typename _Ctx>
4615 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4616 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4618 template<
typename _Ch,
typename _Tp>
4619 friend consteval __format::_Arg_t
4620 __format::__to_arg_t_enum() noexcept;
4622 [[__gnu__::__noinline__]]
4624 _M_handle_unrecognized() const;
4626 template<typename _Visitor>
4627 _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4628 _M_visit(_Visitor&& __vis)
4632 using enum __format::_Arg_t;
4647#if __glibcxx_to_chars
4652#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4655#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4665#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4669#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4673#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4677#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4690#ifdef __SIZEOF_INT128__
4700 handle __h = _M_handle_unrecognized();
4705 template<
typename _Visitor>
4706 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4707 _M_visit_user(_Visitor&& __vis)
4709 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4711 constexpr bool __user_facing = __is_one_of<_Tp,
4712 monostate, bool, _CharT,
4713 int,
unsigned int,
long long int,
unsigned long long int,
4714 float, double,
long double,
4715 const _CharT*, basic_string_view<_CharT>,
4716 const void*, handle>::value;
4717 if constexpr (__user_facing)
4728 template<
typename _Visitor,
typename _Context>
4729 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4730 inline _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4731 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4737 template<
typename _Visitor,
typename _Ctx>
4738 inline _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4739 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4742 struct _WidthPrecVisitor
4744 template<
typename _Tp>
4745 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4746 operator()(_Tp& __arg)
const
4748 if constexpr (is_same_v<_Tp, monostate>)
4749 __format::__invalid_arg_id_in_format_string();
4753 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4757 if constexpr (__is_unsigned_integer<_Tp>::value)
4759 else if constexpr (__is_signed_integer<_Tp>::value)
4763 __throw_format_error(
"format error: argument used for width or "
4764 "precision must be a non-negative integer");
4768#pragma GCC diagnostic push
4769#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4770 template<
typename _Context>
4771 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
4772 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4773 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4776 template<
int _Bits,
size_t _Nm>
4778 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4780 __UINT64_TYPE__ __packed_types = 0;
4781 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4782 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4783 return __packed_types;
4788 template<
typename _Context>
4789 class basic_format_args
4791 static constexpr int _S_packed_type_bits = 5;
4792 static constexpr int _S_packed_type_mask = 0b11111;
4793 static constexpr int _S_max_packed_args = 12;
4795 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4797 template<
typename... _Args>
4798 using _Store = __format::_Arg_store<_Context, _Args...>;
4800 template<
typename _Ctx,
typename... _Args>
4801 friend class __format::_Arg_store;
4803 using uint64_t = __UINT64_TYPE__;
4804 using _Format_arg = basic_format_arg<_Context>;
4805 using _Format_arg_val = __format::_Arg_value<_Context>;
4811 uint64_t _M_packed_size : 4;
4812 uint64_t _M_unpacked_size : 60;
4815 const _Format_arg_val* _M_values;
4816 const _Format_arg* _M_args;
4819 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4820 _M_size() const noexcept
4821 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4823 _GLIBCXX_CONSTEXPR_FORMAT
typename __format::_Arg_t
4824 _M_type(
size_t __i)
const noexcept
4826 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4827 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4830 template<
typename _Ctx,
typename... _Args>
4831 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4832 make_format_args(_Args&...)
noexcept;
4835 template<
typename... _Args>
4836 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4838 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4841 template<
typename... _Args>
4842 _GLIBCXX_CONSTEXPR_FORMAT
4843 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4845 [[nodiscard,__gnu__::__always_inline__]]
4846 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<_Context>
4847 get(
size_t __i)
const noexcept
4849 basic_format_arg<_Context> __arg;
4850 if (__i < _M_packed_size)
4852 __arg._M_type = _M_type(__i);
4853 __arg._M_val = _M_values[__i];
4855 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4856 __arg = _M_args[__i];
4863 template<
typename _Context,
typename... _Args>
4864 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4865 -> basic_format_args<_Context>;
4867 template<
typename _Context,
typename... _Args>
4868 _GLIBCXX_CONSTEXPR_FORMAT
auto
4869 make_format_args(_Args&... __fmt_args)
noexcept;
4872 template<
typename _Context,
typename... _Args>
4873 class __format::_Arg_store
4875 friend std::basic_format_args<_Context>;
4877 template<
typename _Ctx,
typename... _Argz>
4878 friend _GLIBCXX_CONSTEXPR_FORMAT
auto std::
4879#if _GLIBCXX_INLINE_VERSION
4882 make_format_args(_Argz&...)
noexcept;
4886 static constexpr bool _S_values_only
4887 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4890 = __conditional_t<_S_values_only,
4891 __format::_Arg_value<_Context>,
4892 basic_format_arg<_Context>>;
4894 _Element_t _M_args[
sizeof...(_Args)];
4896 template<
typename _Tp>
4897 static _GLIBCXX_CONSTEXPR_FORMAT _Element_t
4898 _S_make_elt(_Tp& __v)
4900 using _Tq = remove_const_t<_Tp>;
4901 using _CharT =
typename _Context::char_type;
4902 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4903 "std::formatter must be specialized for the type "
4904 "of each format arg");
4905 using __format::__formattable_with;
4906 if constexpr (is_const_v<_Tp>)
4907 if constexpr (!__formattable_with<_Tp, _Context>)
4908 if constexpr (__formattable_with<_Tq, _Context>)
4909 static_assert(__formattable_with<_Tp, _Context>,
4910 "format arg must be non-const because its "
4911 "std::formatter specialization has a "
4912 "non-const reference parameter");
4913 basic_format_arg<_Context> __arg(__v);
4914 if constexpr (_S_values_only)
4915 return __arg._M_val;
4920 template<
typename... _Tp>
4921 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4922 [[__gnu__::__always_inline__]]
4923 _GLIBCXX_CONSTEXPR_FORMAT
4924 _Arg_store(_Tp&... __a) noexcept
4925 : _M_args{_S_make_elt(__a)...}
4929 template<
typename _Context>
4930 class __format::_Arg_store<_Context>
4933 template<
typename _Context>
4934 template<
typename... _Args>
4935 inline _GLIBCXX_CONSTEXPR_FORMAT
4936 basic_format_args<_Context>::
4937 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4939 if constexpr (
sizeof...(_Args) == 0)
4942 _M_unpacked_size = 0;
4945 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4948 _M_packed_size =
sizeof...(_Args);
4951 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4953 _M_values = __store._M_args;
4960 _M_unpacked_size =
sizeof...(_Args);
4962 _M_args = __store._M_args;
4967 template<
typename _Context = format_context,
typename... _Args>
4968 [[nodiscard,__gnu__::__always_inline__]]
4969 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4970 make_format_args(_Args&... __fmt_args)
noexcept
4972 using _Fmt_arg = basic_format_arg<_Context>;
4973 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4974 _Normalize<_Args>...>;
4975 return _Store(__fmt_args...);
4978#ifdef _GLIBCXX_USE_WCHAR_T
4980 template<
typename... _Args>
4981 [[nodiscard,__gnu__::__always_inline__]]
4982 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4983 make_wformat_args(_Args&... __args)
noexcept
4984 {
return std::make_format_args<wformat_context>(__args...); }
4990 template<
typename _Out,
typename _CharT,
typename _Context>
4991 _GLIBCXX_CONSTEXPR_FORMAT _Out
4992 __do_vformat_to(_Out, basic_string_view<_CharT>,
4993 const basic_format_args<_Context>&,
4994 const locale* =
nullptr);
4996 template<
typename _CharT>
struct __formatter_chrono;
5014 template<
typename _Out,
typename _CharT>
5017 class _GLIBCXX_NO_SPECIALIZATIONS basic_format_context
5019 static_assert( output_iterator<_Out, const _CharT&> );
5021 basic_format_args<basic_format_context> _M_args;
5023 __format::_Optional_locale _M_loc;
5025 _GLIBCXX_CONSTEXPR_FORMAT
5026 basic_format_context(basic_format_args<basic_format_context> __args,
5028 : _M_args(__args), _M_out(std::move(__out))
5031 _GLIBCXX_CONSTEXPR_FORMAT
5032 basic_format_context(basic_format_args<basic_format_context> __args,
5033 _Out __out,
const std::locale& __loc)
5034 : _M_args(__args), _M_out(std::move(__out)), _M_loc(__loc)
5040 basic_format_context(
const basic_format_context&) =
delete;
5041 basic_format_context& operator=(
const basic_format_context&) =
delete;
5043 template<
typename _Out2,
typename _CharT2,
typename _Context2>
5044 friend _GLIBCXX_CONSTEXPR_FORMAT _Out2
5045 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
5046 const basic_format_args<_Context2>&,
5049 friend __format::__formatter_chrono<_CharT>;
5052 ~basic_format_context() =
default;
5054 using iterator = _Out;
5055 using char_type = _CharT;
5056 template<
typename _Tp>
5057 using formatter_type = formatter<_Tp, _CharT>;
5060 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<basic_format_context>
5061 arg(
size_t __id)
const noexcept
5062 {
return _M_args.get(__id); }
5065 std::locale locale() {
return _M_loc.value(); }
5068 _GLIBCXX_CONSTEXPR_FORMAT iterator
5071 _GLIBCXX_CONSTEXPR_FORMAT
void
5072 advance_to(iterator __it) { _M_out =
std::move(__it); }
5075#if _GLIBCXX_EXTERN_TEMPLATE
5078 extern template basic_format_arg<format_context>::handle
5079 basic_format_arg<format_context>::_M_handle_unrecognized()
const;
5080# ifdef _GLIBCXX_USE_WCHAR_T
5081 extern template basic_format_arg<wformat_context>::handle
5082 basic_format_arg<wformat_context>::_M_handle_unrecognized()
const;
5085 template<
typename _Context>
5086 typename basic_format_arg<_Context>::handle
5087 basic_format_arg<_Context>::_M_handle_unrecognized()
const
5092 __throw_format_error(
"format error: unrecognized argument type");
5104 template<
typename _CharT>
5107 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
5109 typename basic_format_parse_context<_CharT>::_Scan_parse_context _M_pc;
5112 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5113 : _M_pc(__str, __nargs)
5116 constexpr iterator begin() const noexcept {
return _M_pc.begin(); }
5117 constexpr iterator end() const noexcept {
return _M_pc.end(); }
5122 basic_string_view<_CharT> __fmt = _M_fmt_str();
5124 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5126 _M_pc.advance_to(begin() + 1);
5127 _M_format_arg(_M_pc.next_arg_id());
5131 size_t __lbr = __fmt.find(
'{');
5132 size_t __rbr = __fmt.find(
'}');
5134 while (__fmt.size())
5136 auto __cmp = __lbr <=> __rbr;
5140 _M_pc.advance_to(end());
5145 if (__lbr + 1 == __fmt.size()
5146 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5147 __format::__unmatched_left_brace_in_format_string();
5148 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5149 iterator __last = begin() + __lbr + int(__is_escape);
5150 _M_on_chars(__last);
5151 _M_pc.advance_to(__last + 1);
5152 __fmt = _M_fmt_str();
5155 if (__rbr != __fmt.npos)
5157 __lbr = __fmt.find(
'{');
5161 _M_on_replacement_field();
5162 __fmt = _M_fmt_str();
5163 __lbr = __fmt.find(
'{');
5164 __rbr = __fmt.find(
'}');
5169 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5170 __format::__unmatched_right_brace_in_format_string();
5171 iterator __last = begin() + __rbr;
5172 _M_on_chars(__last);
5173 _M_pc.advance_to(__last + 1);
5174 __fmt = _M_fmt_str();
5175 if (__lbr != __fmt.npos)
5177 __rbr = __fmt.find(
'}');
5182 constexpr basic_string_view<_CharT>
5183 _M_fmt_str() const noexcept
5184 {
return {begin(), end()}; }
5186 constexpr virtual void _M_on_chars(iterator) { }
5188 constexpr void _M_on_replacement_field()
5190 auto __next = begin();
5194 __id = _M_pc.next_arg_id();
5195 else if (*__next ==
':')
5197 __id = _M_pc.next_arg_id();
5198 _M_pc.advance_to(++__next);
5202 auto [__i, __ptr] = __format::__parse_arg_id(begin(), end());
5203 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5204 __format::__invalid_arg_id_in_format_string();
5205 _M_pc.check_arg_id(__id = __i);
5208 _M_pc.advance_to(++__ptr);
5211 _M_pc.advance_to(__ptr);
5213 _M_format_arg(__id);
5214 if (begin() == end() || *begin() !=
'}')
5215 __format::__unmatched_left_brace_in_format_string();
5216 _M_pc.advance_to(begin() + 1);
5219 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5223 template<
typename _Out,
typename _CharT>
5224 class _Formatting_scanner :
public _Scanner<_CharT>
5227 _GLIBCXX_CONSTEXPR_FORMAT
5228 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5229 basic_string_view<_CharT> __str)
5230 : _Scanner<_CharT>(__str), _M_fc(__fc)
5234 basic_format_context<_Out, _CharT>& _M_fc;
5236 using iterator =
typename _Scanner<_CharT>::iterator;
5239 _M_on_chars(iterator __last)
override
5241 basic_string_view<_CharT> __str(this->begin(), __last);
5242 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5246 _M_format_arg(
size_t __id)
override
5248 using _Context = basic_format_context<_Out, _CharT>;
5249 using handle =
typename basic_format_arg<_Context>::handle;
5251 __format::__visit_format_arg([
this](
auto& __arg) {
5253 using _Formatter =
typename _Context::template formatter_type<_Type>;
5254 if constexpr (is_same_v<_Type, monostate>)
5255 __format::__invalid_arg_id_in_format_string();
5256 else if constexpr (is_same_v<_Type, handle>)
5257 __arg.format(this->_M_pc, this->_M_fc);
5258 else if constexpr (is_default_constructible_v<_Formatter>)
5261 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5262 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5265 static_assert(__format::__formattable_with<_Type, _Context>);
5266 }, _M_fc.arg(__id));
5270 template<
typename _CharT,
typename _Tp>
5272 __to_arg_t_enum() noexcept
5274 using _Context = __format::__format_context<_CharT>;
5275 using _Fmt_arg = basic_format_arg<_Context>;
5276 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5277 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5281 template<
typename _CharT,
typename... _Args>
5282 class _Checking_scanner :
public _Scanner<_CharT>
5285 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5286 "std::formatter must be specialized for each type being formatted");
5290 _Checking_scanner(basic_string_view<_CharT> __str)
5291 : _Scanner<_CharT>(__str, sizeof...(_Args))
5293#if __cpp_lib_format >= 202305L
5294 this->_M_pc._M_types = _M_types.data();
5300 _M_format_arg(
size_t __id)
override
5302 if constexpr (
sizeof...(_Args) != 0)
5304 if (__id <
sizeof...(_Args))
5306 _M_parse_format_spec<_Args...>(__id);
5310 __builtin_unreachable();
5313 template<
typename _Tp,
typename... _OtherArgs>
5315 _M_parse_format_spec(
size_t __id)
5319 formatter<_Tp, _CharT> __f;
5320 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5322 else if constexpr (
sizeof...(_OtherArgs) != 0)
5323 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5325 __builtin_unreachable();
5328#if __cpp_lib_format >= 202305L
5329 array<_Arg_t,
sizeof...(_Args)>
5330 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5334 template<typename _CharT, unsigned = __unicode::__literal_encoding_is_unicode<_CharT>()>
5335 _GLIBCXX_CONSTEXPR_FORMAT _Sink_iter<_CharT>
5336 __do_vformat_to(_Sink_iter<_CharT> __out, basic_string_view<_CharT> __fmt,
5337 __format_context<_CharT>& __ctx)
5339 if constexpr (is_same_v<_CharT, char>)
5341 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5343 bool __done =
false;
5344 __format::__visit_format_arg([&](
auto& __arg) {
5345 using _Tp = remove_cvref_t<
decltype(__arg)>;
5346 if constexpr (is_same_v<_Tp, bool>)
5348 size_t __len = 4 + !__arg;
5349 const char* __chars[] = {
"false",
"true" };
5350 if (
auto __res = __out._M_reserve(__len))
5352 ranges::copy_n(__chars[__arg], __len, __res.get());
5353 __res._M_bump(__len);
5357 else if constexpr (is_same_v<_Tp, char>)
5359 if (
auto __res = __out._M_reserve(1))
5361 *__res.get() = __arg;
5366 else if constexpr (is_integral_v<_Tp>)
5368 make_unsigned_t<_Tp> __uval;
5369 const bool __neg = __arg < 0;
5371 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5374 const auto __n = __detail::__to_chars_len(__uval);
5375 if (
auto __res = __out._M_reserve(__n + __neg))
5377 auto __ptr = __res.get();
5379 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5381 __res._M_bump(__n + __neg);
5385 else if constexpr (is_convertible_v<_Tp, string_view>)
5387 string_view __sv = __arg;
5388 if (
auto __res = __out._M_reserve(__sv.size()))
5390 ranges::copy(__sv, __res.get());
5391 __res._M_bump(__sv.size());
5401 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5402 __scanner._M_scan();
5411#if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE
5412 extern template _Sink_iter<char>
5413 __do_vformat_to<char, 1>(_Sink_iter<char>, string_view,
5415# ifdef _GLIBCXX_USE_WCHAR_T
5416 extern template _Sink_iter<wchar_t>
5417 __do_vformat_to<wchar_t, 1>(_Sink_iter<wchar_t>, wstring_view,
5422 template<
typename _Out,
typename _CharT,
typename _Context>
5423 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5424 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5425 const basic_format_args<_Context>& __args,
5426 const locale* __loc)
5428 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5430 auto __ctx = __loc ==
nullptr
5431 ? _Context(__args, __out)
5432 : _Context(__args, __out, *__loc);
5433 return __format::__do_vformat_to(__out, __fmt, __ctx);
5435 else if constexpr (__contiguous_char_iter<_CharT, _Out>)
5437 _Ptr_sink<_CharT> __sink(__out);
5438 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5439 return std::move(__sink)._M_finish(__out).out;
5443 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5444 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5445 return std::move(__sink)._M_finish().out;
5449 template<
typename _Out,
typename _CharT>
5450 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5451 __do_vformat_to_n(_Out __out, iter_difference_t<_Out> __n,
5452 basic_string_view<_CharT> __fmt,
5453 const type_identity_t<
5454 basic_format_args<__format_context<_CharT>>>& __args,
5455 const locale* __loc =
nullptr)
5457 if constexpr (__contiguous_char_iter<_CharT, _Out>)
5459 _Ptr_sink<_CharT> __sink(__out, __n);
5460 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5461 return std::move(__sink)._M_finish(__out);
5465 _Iter_sink<_CharT, _Out> __sink(
std::move(__out), __n);
5466 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5471#pragma GCC diagnostic pop
5476 template<
typename _CharT,
typename... _Args>
5477 template<
typename _Tp>
5478 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5480 basic_format_string<_CharT, _Args...>::
5481 basic_format_string(
const _Tp& __s) noexcept
5484 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5486 __scanner._M_scan();
5491 template<
typename _Out>
requires output_iterator<_Out, const char&>
5492 [[__gnu__::__always_inline__]]
5493 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5494 vformat_to(_Out __out, string_view __fmt, format_args __args)
5495 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5497#ifdef _GLIBCXX_USE_WCHAR_T
5498 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5499 [[__gnu__::__always_inline__]]
5500 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5501 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5502 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5505 template<
typename _Out>
requires output_iterator<_Out, const char&>
5506 [[__gnu__::__always_inline__]]
5508 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5511 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5514#ifdef _GLIBCXX_USE_WCHAR_T
5515 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5516 [[__gnu__::__always_inline__]]
5518 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5519 wformat_args __args)
5521 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5526 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5527 vformat(string_view __fmt, format_args __args)
5529 __format::_Str_sink<char> __buf;
5530 std::vformat_to(__buf.out(), __fmt, __args);
5534#ifdef _GLIBCXX_USE_WCHAR_T
5536 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5537 vformat(wstring_view __fmt, wformat_args __args)
5539 __format::_Str_sink<wchar_t> __buf;
5540 std::vformat_to(__buf.out(), __fmt, __args);
5547 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5549 __format::_Str_sink<char> __buf;
5550 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5554#ifdef _GLIBCXX_USE_WCHAR_T
5557 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5559 __format::_Str_sink<wchar_t> __buf;
5560 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5565 template<
typename... _Args>
5567 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5568 format(format_string<_Args...> __fmt, _Args&&... __args)
5569 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5571#ifdef _GLIBCXX_USE_WCHAR_T
5572 template<
typename... _Args>
5574 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5575 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5576 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5579 template<
typename... _Args>
5582 format(
const locale& __loc, format_string<_Args...> __fmt,
5585 return std::vformat(__loc, __fmt.get(),
5586 std::make_format_args(__args...));
5589#ifdef _GLIBCXX_USE_WCHAR_T
5590 template<
typename... _Args>
5593 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5596 return std::vformat(__loc, __fmt.get(),
5597 std::make_wformat_args(__args...));
5601 template<
typename _Out,
typename... _Args>
5602 requires output_iterator<_Out, const char&>
5603 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5604 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5606 return std::vformat_to(
std::move(__out), __fmt.get(),
5607 std::make_format_args(__args...));
5610#ifdef _GLIBCXX_USE_WCHAR_T
5611 template<
typename _Out,
typename... _Args>
5612 requires output_iterator<_Out, const wchar_t&>
5613 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5614 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5616 return std::vformat_to(
std::move(__out), __fmt.get(),
5617 std::make_wformat_args(__args...));
5621 template<
typename _Out,
typename... _Args>
5622 requires output_iterator<_Out, const char&>
5624 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5627 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5628 std::make_format_args(__args...));
5631#ifdef _GLIBCXX_USE_WCHAR_T
5632 template<
typename _Out,
typename... _Args>
5633 requires output_iterator<_Out, const wchar_t&>
5635 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5638 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5639 std::make_wformat_args(__args...));
5643 template<
typename _Out,
typename... _Args>
5644 requires output_iterator<_Out, const char&>
5645 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5646 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5647 format_string<_Args...> __fmt, _Args&&... __args)
5649 return __format::__do_vformat_to_n(
5651 std::make_format_args(__args...));
5654#ifdef _GLIBCXX_USE_WCHAR_T
5655 template<
typename _Out,
typename... _Args>
5656 requires output_iterator<_Out, const wchar_t&>
5657 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5658 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5659 wformat_string<_Args...> __fmt, _Args&&... __args)
5661 return __format::__do_vformat_to_n(
5663 std::make_wformat_args(__args...));
5667 template<
typename _Out,
typename... _Args>
5668 requires output_iterator<_Out, const char&>
5669 inline format_to_n_result<_Out>
5670 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5671 format_string<_Args...> __fmt, _Args&&... __args)
5673 return __format::__do_vformat_to_n(
5675 std::make_format_args(__args...), &__loc);
5678#ifdef _GLIBCXX_USE_WCHAR_T
5679 template<
typename _Out,
typename... _Args>
5680 requires output_iterator<_Out, const wchar_t&>
5681 inline format_to_n_result<_Out>
5682 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5683 wformat_string<_Args...> __fmt, _Args&&... __args)
5685 return __format::__do_vformat_to_n(
5687 std::make_wformat_args(__args...), &__loc);
5695 template<
typename _CharT>
5696 class _Counting_sink final :
public _Ptr_sink<_CharT>
5699 _GLIBCXX_CONSTEXPR_FORMAT
5700 _Counting_sink() : _Ptr_sink<_CharT>(nullptr, 0) { }
5702 [[__gnu__::__always_inline__]]
5703 _GLIBCXX_CONSTEXPR_FORMAT
size_t
5705 {
return this->_M_count + this->_M_used().size(); }
5708 template<
typename _CharT>
5709 class _Counting_sink :
public _Buf_sink<_CharT>
5711 size_t _M_count = 0;
5714 _M_overflow()
override
5716 if (!std::is_constant_evaluated())
5717 _M_count += this->_M_used().size();
5722 _Counting_sink() =
default;
5724 [[__gnu__::__always_inline__]]
5728 _Counting_sink::_M_overflow();
5736 template<
typename... _Args>
5738 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5739 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5741 __format::_Counting_sink<char> __buf;
5742 std::vformat_to(__buf.out(), __fmt.get(),
5743 std::make_format_args(__args...));
5744 return __buf.count();
5747#ifdef _GLIBCXX_USE_WCHAR_T
5748 template<
typename... _Args>
5750 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5751 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5753 __format::_Counting_sink<wchar_t> __buf;
5754 std::vformat_to(__buf.out(), __fmt.get(),
5755 std::make_wformat_args(__args...));
5756 return __buf.count();
5760 template<
typename... _Args>
5763 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5766 __format::_Counting_sink<char> __buf;
5767 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5768 std::make_format_args(__args...));
5769 return __buf.count();
5772#ifdef _GLIBCXX_USE_WCHAR_T
5773 template<
typename... _Args>
5776 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5779 __format::_Counting_sink<wchar_t> __buf;
5780 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5781 std::make_wformat_args(__args...));
5782 return __buf.count();
5786#if __glibcxx_format_ranges
5788 template<typename _Tp>
5789 consteval range_format
5792 using _Ref = ranges::range_reference_t<_Tp>;
5793 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5794 return range_format::disabled;
5795 else if constexpr (
requires {
typename _Tp::key_type; })
5797 if constexpr (
requires {
typename _Tp::mapped_type; })
5799 using _Up = remove_cvref_t<_Ref>;
5800 if constexpr (__is_pair<_Up>)
5801 return range_format::map;
5802 else if constexpr (__is_specialization_of<_Up, tuple>)
5803 if constexpr (tuple_size_v<_Up> == 2)
5804 return range_format::map;
5806 return range_format::set;
5809 return range_format::sequence;
5814 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5815 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5820 template<
typename _CharT,
typename _Out,
typename _Callback>
5821 _GLIBCXX_CONSTEXPR_FORMAT
5822 typename basic_format_context<_Out, _CharT>::iterator
5823 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5824 const _Spec<_CharT>& __spec,
5827 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
5833 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
5835 const size_t __padwidth = __spec._M_get_width(__fc);
5836 if (__padwidth == 0)
5837 return __call(__fc);
5841 _GLIBCXX_CONSTEXPR_FORMAT
5842 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5843 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5846 _GLIBCXX_CONSTEXPR_FORMAT
void
5848 { _M_ctx =
nullptr; }
5850 _GLIBCXX_CONSTEXPR_FORMAT
5854 _M_ctx->advance_to(_M_out);
5858 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5859 _Sink_iter<_CharT> _M_out;
5862 _Restore_out __restore(__fc);
5863 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
5864 __fc.advance_to(__sink.out());
5866 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
5867 __restore._M_disarm();
5872 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5873 struct __indexed_formatter_storage
5880 basic_format_parse_context<_CharT> __pc({});
5881 if (_M_formatter.parse(__pc) != __pc.end())
5882 __format::__failed_to_parse_format_spec();
5885 template<
typename _Out>
5886 _GLIBCXX_CONSTEXPR_FORMAT
void
5887 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5888 basic_format_context<_Out, _CharT>& __fc,
5889 basic_string_view<_CharT> __sep)
const
5891 if constexpr (_Pos != 0)
5892 __fc.advance_to(__format::__write(__fc.out(), __sep));
5893 __fc.advance_to(_M_formatter.format(__elem, __fc));
5896 [[__gnu__::__always_inline__]]
5900 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5901 _M_formatter.set_debug_format();
5905 formatter<_Tp, _CharT> _M_formatter;
5908 template<
typename _CharT,
typename... _Tps>
5909 class __tuple_formatter
5911 using _String_view = basic_string_view<_CharT>;
5912 using _Seps = __format::_Separators<_CharT>;
5916 set_separator(basic_string_view<_CharT> __sep)
noexcept
5920 set_brackets(basic_string_view<_CharT> __open,
5921 basic_string_view<_CharT> __close)
noexcept
5929 constexpr typename basic_format_parse_context<_CharT>::iterator
5930 parse(basic_format_parse_context<_CharT>& __pc)
5932 auto __first = __pc.begin();
5933 const auto __last = __pc.end();
5934 __format::_Spec<_CharT> __spec{};
5936 auto __finished = [&]
5938 if (__first != __last && *__first !=
'}')
5942 _M_felems._M_parse();
5943 _M_felems.set_debug_format();
5950 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5954 __first = __spec._M_parse_width(__first, __last, __pc);
5958 if (*__first ==
'n')
5961 _M_open = _M_close = _String_view();
5963 else if (*__first ==
'm')
5966 if constexpr (
sizeof...(_Tps) == 2)
5968 _M_sep = _Seps::_S_colon();
5969 _M_open = _M_close = _String_view();
5972 __throw_format_error(
"format error: 'm' specifier requires range"
5973 " of pair or tuple of two elements");
5979 __format::__failed_to_parse_format_spec();
5983 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
5984 _GLIBCXX_CONSTEXPR_FORMAT
5985 typename basic_format_context<_Out, _CharT>::iterator
5986 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
5987 basic_format_context<_Out, _CharT>& __fc)
const
5988 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
5990 template<
typename _Out>
5991 _GLIBCXX_CONSTEXPR_FORMAT
5992 typename basic_format_context<_Out, _CharT>::iterator
5993 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
5994 basic_format_context<_Out, _CharT>& __fc)
const
5996 return __format::__format_padded(
5998 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
6000 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
6001 _M_felems._M_format(__elems..., __nfc, _M_sep);
6002 return __format::__write(__nfc.out(), _M_close);
6007 template<
size_t... _Ids>
6008 struct __formatters_storage
6009 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
6011 template<
size_t _Id,
typename _Up>
6012 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
6017 (_Base<_Ids, _Tps>::_M_parse(), ...);
6020 template<
typename _Out>
6021 _GLIBCXX_CONSTEXPR_FORMAT
void
6022 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
6023 basic_format_context<_Out, _CharT>& __fc,
6024 _String_view __sep)
const
6026 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
6032 (_Base<_Ids, _Tps>::set_debug_format(), ...);
6036 template<
size_t... _Ids>
6037 static _GLIBCXX_CONSTEXPR_FORMAT
auto
6038 _S_create_storage(index_sequence<_Ids...>)
6039 -> __formatters_storage<_Ids...>;
6041 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
6043 _Spec<_CharT> _M_spec{};
6044 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
6045 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
6046 _String_view _M_sep = _Seps::_S_comma();
6047 _Formatters _M_felems;
6050 template<
typename _Tp>
6051 concept __is_map_formattable
6052 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
6058 template<__format::__
char _CharT, formattable<_CharT> _Fp,
6059 formattable<_CharT> _Sp>
6060 struct formatter<
pair<_Fp, _Sp>, _CharT>
6061 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
6062 remove_cvref_t<_Sp>>
6065 using __maybe_const_pair
6066 = __conditional_t<formattable<const _Fp, _CharT>
6067 && formattable<const _Sp, _CharT>,
6068 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
6072 template<
typename _Out>
6073 _GLIBCXX_CONSTEXPR_FORMAT
6074 typename basic_format_context<_Out, _CharT>::iterator
6075 format(__maybe_const_pair& __p,
6076 basic_format_context<_Out, _CharT>& __fc)
const
6077 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
6080#if __glibcxx_print >= 202406L
6083 template<
typename _Fp,
typename _Sp>
6084 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
6085 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
6086 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
6089 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
6090 struct formatter<tuple<_Tps...>, _CharT>
6091 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
6094 using __maybe_const_tuple
6095 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
6096 const tuple<_Tps...>, tuple<_Tps...>>;
6100 template<
typename _Out>
6101 _GLIBCXX_CONSTEXPR_FORMAT
6102 typename basic_format_context<_Out, _CharT>::iterator
6103 format(__maybe_const_tuple& __t,
6104 basic_format_context<_Out, _CharT>& __fc)
const
6105 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
6108#if __glibcxx_print >= 202406L
6111 template<
typename... _Tps>
6112 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
6113 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
6117 template<
typename _Tp, __format::__
char _CharT>
6118 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
6119 class range_formatter
6121 using _String_view = basic_string_view<_CharT>;
6122 using _Seps = __format::_Separators<_CharT>;
6126 set_separator(basic_string_view<_CharT> __sep)
noexcept
6130 set_brackets(basic_string_view<_CharT> __open,
6131 basic_string_view<_CharT> __close)
noexcept
6137 constexpr formatter<_Tp, _CharT>&
6138 underlying() noexcept
6141 constexpr const formatter<_Tp, _CharT>&
6142 underlying() const noexcept
6147 constexpr typename basic_format_parse_context<_CharT>::iterator
6148 parse(basic_format_parse_context<_CharT>& __pc)
6150 auto __first = __pc.begin();
6151 const auto __last = __pc.end();
6152 __format::_Spec<_CharT> __spec{};
6153 bool __no_brace =
false;
6155 auto __finished = [&]
6156 {
return __first == __last || *__first ==
'}'; };
6158 auto __finalize = [&]
6164 auto __parse_val = [&](_String_view __nfs = _String_view())
6168 basic_format_parse_context<_CharT> __npc(__nfs);
6169 if (_M_fval.parse(__npc) != __npc.end())
6170 __format::__failed_to_parse_format_spec();
6171 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6172 _M_fval.set_debug_format();
6173 return __finalize();
6177 return __parse_val();
6179 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6181 return __parse_val();
6183 __first = __spec._M_parse_width(__first, __last, __pc);
6185 return __parse_val();
6187 if (*__first ==
'?')
6190 __spec._M_debug =
true;
6191 if (__finished() || *__first !=
's')
6192 __throw_format_error(
"format error: '?' is allowed only in"
6193 " combination with 's'");
6196 if (*__first ==
's')
6199 if constexpr (same_as<_Tp, _CharT>)
6201 __spec._M_type = __format::_Pres_s;
6203 return __finalize();
6204 __throw_format_error(
"format error: element format specifier"
6205 " cannot be provided when 's' specifier is used");
6208 __throw_format_error(
"format error: 's' specifier requires"
6209 " range of character types");
6213 return __parse_val();
6215 if (*__first ==
'n')
6218 _M_open = _M_close = _String_view();
6223 return __parse_val();
6225 if (*__first ==
'm')
6227 _String_view __m(__first, 1);
6229 if constexpr (__format::__is_map_formattable<_Tp>)
6231 _M_sep = _Seps::_S_comma();
6234 _M_open = _Seps::_S_braces().substr(0, 1);
6235 _M_close = _Seps::_S_braces().substr(1, 1);
6238 return __parse_val(__m);
6239 __throw_format_error(
"format error: element format specifier"
6240 " cannot be provided when 'm' specifier is used");
6243 __throw_format_error(
"format error: 'm' specifier requires"
6244 " range of pairs or tuples of two elements");
6248 return __parse_val();
6250 if (*__first ==
':')
6252 __pc.advance_to(++__first);
6253 __first = _M_fval.parse(__pc);
6257 return __finalize();
6259 __format::__failed_to_parse_format_spec();
6264 template<ranges::input_range _Rg,
typename _Out>
6265 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6266 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6267 _GLIBCXX_CONSTEXPR_FORMAT
6268 typename basic_format_context<_Out, _CharT>::iterator
6269 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6271 using _Range = remove_reference_t<_Rg>;
6272 if constexpr (ranges::contiguous_range<_Rg>)
6274 const span<__format::__maybe_const<_Tp, _CharT>>
6275 __spn(ranges::data(__rg),
size_t(ranges::distance(__rg)));
6276 return _M_format(__spn, __fc);
6278 else if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6279 return _M_format<const _Range>(__rg, __fc);
6281 return _M_format(__rg, __fc);
6285 template<ranges::input_range _Rg,
typename _Out>
6286 _GLIBCXX_CONSTEXPR_FORMAT
6287 typename basic_format_context<_Out, _CharT>::iterator
6288 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6290 if constexpr (same_as<_Tp, _CharT>)
6291 if (_M_spec._M_type == __format::_Pres_s)
6293 __format::__formatter_str __fstr(_M_spec);
6294 return __fstr._M_format_range(__rg, __fc);
6296 return __format::__format_padded(
6298 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6299 {
return _M_format_elems(__rg, __nfc); });
6303 template<ranges::input_range _Rg,
typename _Out>
6304 _GLIBCXX_CONSTEXPR_FORMAT
6305 typename basic_format_context<_Out, _CharT>::iterator
6306 _M_format_elems(_Rg& __rg,
6307 basic_format_context<_Out, _CharT>& __fc)
const
6309 auto __out = __format::__write(__fc.out(), _M_open);
6311 auto __first = ranges::begin(__rg);
6312 auto const __last = ranges::end(__rg);
6313 if (__first == __last)
6314 return __format::__write(__out, _M_close);
6316 __fc.advance_to(__out);
6317 __out = _M_fval.format(*__first, __fc);
6318 for (++__first; __first != __last; ++__first)
6320 __out = __format::__write(__out, _M_sep);
6321 __fc.advance_to(__out);
6322 __out = _M_fval.format(*__first, __fc);
6325 return __format::__write(__out, _M_close);
6328 __format::_Spec<_CharT> _M_spec{};
6329 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6330 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6331 _String_view _M_sep = _Seps::_S_comma();
6332 formatter<_Tp, _CharT> _M_fval;
6340 template<ranges::input_range _Rg, __format::__
char _CharT>
6341 requires (format_kind<_Rg> != range_format::disabled)
6342 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6343 struct formatter<_Rg, _CharT>
6346 static const bool _S_range_format_is_string =
6347 (format_kind<_Rg> == range_format::string)
6348 || (format_kind<_Rg> == range_format::debug_string);
6349 using _Vt = remove_cvref_t<
6350 ranges::range_reference_t<
6351 __format::__maybe_const_range<_Rg, _CharT>>>;
6353 static consteval bool _S_is_correct()
6355 if constexpr (_S_range_format_is_string)
6356 static_assert(same_as<_Vt, _CharT>);
6360 static_assert(_S_is_correct());
6363 constexpr formatter() noexcept
6365 using _Seps = __format::_Separators<_CharT>;
6366 if constexpr (format_kind<_Rg> == range_format::map)
6368 static_assert(__format::__is_map_formattable<_Vt>);
6369 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6370 _Seps::_S_braces().substr(1, 1));
6371 _M_under.underlying().set_brackets({}, {});
6372 _M_under.underlying().set_separator(_Seps::_S_colon());
6374 else if constexpr (format_kind<_Rg> == range_format::set)
6375 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6376 _Seps::_S_braces().substr(1, 1));
6380 set_separator(basic_string_view<_CharT> __sep)
noexcept
6381 requires (format_kind<_Rg> == range_format::sequence)
6382 { _M_under.set_separator(__sep); }
6385 set_brackets(basic_string_view<_CharT> __open,
6386 basic_string_view<_CharT> __close)
noexcept
6387 requires (format_kind<_Rg> == range_format::sequence)
6388 { _M_under.set_brackets(__open, __close); }
6392 constexpr typename basic_format_parse_context<_CharT>::iterator
6393 parse(basic_format_parse_context<_CharT>& __pc)
6395 auto __res = _M_under.parse(__pc);
6396 if constexpr (format_kind<_Rg> == range_format::debug_string)
6397 _M_under.set_debug_format();
6403 template<
typename _Out>
6404 _GLIBCXX_CONSTEXPR_FORMAT
6405 typename basic_format_context<_Out, _CharT>::iterator
6406 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6407 basic_format_context<_Out, _CharT>& __fc)
const
6409 if constexpr (_S_range_format_is_string)
6410 return _M_under._M_format_range(__rg, __fc);
6412 return _M_under.format(__rg, __fc);
6416 using _Formatter_under
6417 = __conditional_t<_S_range_format_is_string,
6418 __format::__formatter_str<_CharT>,
6419 range_formatter<_Vt, _CharT>>;
6420 _Formatter_under _M_under;
6423#if __glibcxx_print >= 202406L
6424 template<ranges::input_range _Rg>
6425 requires (format_kind<_Rg> != range_format::disabled)
6426 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6430#undef _GLIBCXX_WIDEN
6431#undef _GLIBCXX_CONSTEXPR_FORMAT
6433_GLIBCXX_END_NAMESPACE_VERSION
6436#pragma GCC diagnostic pop
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
_Tp arg(const complex< _Tp > &)
Return phase angle of z.
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
const _Facet & use_facet(const locale &__loc)
Return a facet.
basic_string< char > string
A string of char.
ISO C++ entities toplevel namespace is std.
chars_format
floating-point format for primitive numerical conversion
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.