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;
282 template<
typename _CharT>
class basic_format_parse_context;
283 using format_parse_context = basic_format_parse_context<char>;
284#ifdef _GLIBCXX_USE_WCHAR_T
285 using wformat_parse_context = basic_format_parse_context<wchar_t>;
288 template<
typename _CharT>
289 class basic_format_parse_context
292 using char_type = _CharT;
293 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
294 using iterator = const_iterator;
297 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
298 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
301 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
302 void operator=(
const basic_format_parse_context&) =
delete;
304 constexpr const_iterator begin() const noexcept {
return _M_begin; }
305 constexpr const_iterator end() const noexcept {
return _M_end; }
308 advance_to(const_iterator __it)
noexcept
314 if (_M_indexing == _Manual)
315 __format::__conflicting_indexing_in_format_string();
320 if (std::is_constant_evaluated())
321 if (_M_next_arg_id == _M_num_args)
322 __format::__invalid_arg_id_in_format_string();
323 return _M_next_arg_id++;
327 check_arg_id(
size_t __id)
329 if (_M_indexing == _Auto)
330 __format::__conflicting_indexing_in_format_string();
331 _M_indexing = _Manual;
333 if (std::is_constant_evaluated())
334 if (__id >= _M_num_args)
335 __format::__invalid_arg_id_in_format_string();
338#if __cpp_lib_format >= 202305L
339 template<
typename... _Ts>
341 check_dynamic_spec(
size_t __id)
noexcept
343 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
344 "template arguments for check_dynamic_spec<Ts...>(id) "
345 "must be unique and must be one of the allowed types");
347 __check_dynamic_spec<_Ts...>(__id);
352 check_dynamic_spec_integral(
size_t __id)
noexcept
355 __check_dynamic_spec<int, unsigned,
long long,
356 unsigned long long>(__id);
361 check_dynamic_spec_string(
size_t __id)
noexcept
364 __check_dynamic_spec<const _CharT*, basic_string_view<_CharT>>(__id);
370 template<
typename _Tp,
typename... _Ts>
371 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
373 template<
typename... _Ts>
375 __valid_types_for_check_dynamic_spec()
379 if constexpr (
sizeof...(_Ts) == 0)
388 = __once<bool, _Ts...>
389 + __once<char_type, _Ts...>
390 + __once<int, _Ts...>
391 + __once<
unsigned int, _Ts...>
392 + __once<
long long int, _Ts...>
393 + __once<
unsigned long long int, _Ts...>
394 + __once<float, _Ts...>
395 + __once<double, _Ts...>
396 + __once<
long double, _Ts...>
397 + __once<
const char_type*, _Ts...>
398 + __once<basic_string_view<char_type>, _Ts...>
399 + __once<
const void*, _Ts...>;
400 return __sum ==
sizeof...(_Ts);
404 template<
typename... _Ts>
406 __check_dynamic_spec(
size_t __id)
noexcept;
409 static void __invalid_dynamic_spec(
const char*);
411 friend __format::_Scanner<_CharT>;
416 basic_format_parse_context(basic_string_view<_CharT> __fmt,
417 size_t __num_args) noexcept
418 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
424 enum _Indexing { _Unknown, _Manual, _Auto };
425 _Indexing _M_indexing = _Unknown;
426 size_t _M_next_arg_id = 0;
427 size_t _M_num_args = 0;
431 template<
typename _Tp,
template<
typename...>
class _Class>
432 constexpr bool __is_specialization_of =
false;
433 template<
template<
typename...>
class _Class,
typename... _Args>
434 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
439 template<
typename _CharT>
440 constexpr pair<unsigned short, const _CharT*>
441 __parse_integer(
const _CharT* __first,
const _CharT* __last)
443 if (__first == __last)
444 __builtin_unreachable();
446 if constexpr (is_same_v<_CharT, char>)
448 const auto __start = __first;
449 unsigned short __val = 0;
451 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
452 && __first != __start) [[likely]]
453 return {__val, __first};
457 constexpr int __n = 32;
459 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
460 __buf[__i] = __first[__i];
461 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
462 if (__ptr) [[likely]]
463 return {__v, __first + (__ptr - __buf)};
468 template<
typename _CharT>
469 constexpr pair<unsigned short, const _CharT*>
470 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
472 if (__first == __last)
473 __builtin_unreachable();
476 return {0, __first + 1};
478 if (
'1' <= *__first && *__first <=
'9')
480 const unsigned short __id = *__first -
'0';
481 const auto __next = __first + 1;
483 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
484 return {__id, __next};
486 return __format::__parse_integer(__first, __last);
491 enum class _Pres_type :
unsigned char {
495 _Pres_c = 2, _Pres_x, _Pres_X, _Pres_d, _Pres_o, _Pres_b, _Pres_B,
497 _Pres_g = 1, _Pres_G, _Pres_a, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F,
501 using enum _Pres_type;
503 enum class _Sign :
unsigned char {
511 enum _WidthPrec :
unsigned char {
516 using enum _WidthPrec;
518 template<
typename _Context>
519 _GLIBCXX_CONSTEXPR_FORMAT
size_t
520 __int_from_arg(
const basic_format_arg<_Context>& __arg);
522 constexpr bool __is_digit(
char __c)
523 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
525 constexpr bool __is_xdigit(
char __c)
526 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
533 template<
typename _CharT>
534 struct _Spec : _SpecBase
536 unsigned short _M_width;
537 unsigned short _M_prec;
538 char32_t _M_fill =
' ';
542 unsigned _M_localized : 1;
543 unsigned _M_zero_fill : 1;
544 _WidthPrec _M_width_kind : 2;
545 _WidthPrec _M_prec_kind : 2;
546 unsigned _M_debug : 1;
547 _Pres_type _M_type : 4;
548 unsigned _M_reserved : 8;
552 using iterator =
typename basic_string_view<_CharT>::iterator;
554 static constexpr _Align
555 _S_align(_CharT __c)
noexcept
559 case '<':
return _Align_left;
560 case '>':
return _Align_right;
561 case '^':
return _Align_centre;
562 default:
return _Align_default;
568 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
569 {
return _M_parse_fill_and_align(__first, __last,
"{"); }
573 _M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill)
noexcept
575 for (
char __c : __not_fill)
576 if (*__first ==
static_cast<_CharT
>(__c))
579 using namespace __unicode;
580 if constexpr (__literal_encoding_is_unicode<_CharT>())
583 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
586 auto __beg = __uv.begin();
587 char32_t __c = *__beg++;
588 if (__is_scalar_value(__c))
589 if (
auto __next = __beg.base(); __next != __last)
590 if (_Align __align = _S_align(*__next); __align != _Align_default)
598 else if (__last - __first >= 2)
599 if (_Align __align = _S_align(__first[1]); __align != _Align_default)
606 if (_Align __align = _S_align(__first[0]); __align != _Align_default)
615 static constexpr _Sign
616 _S_sign(_CharT __c)
noexcept
620 case '+':
return _Sign_plus;
621 case '-':
return _Sign_minus;
622 case ' ':
return _Sign_space;
623 default:
return _Sign_default;
629 _M_parse_sign(iterator __first, iterator)
noexcept
631 if (_Sign __sign = _S_sign(*__first); __sign != _Sign_default)
641 _M_parse_alternate_form(iterator __first, iterator)
noexcept
653 _M_parse_zero_fill(iterator __first, iterator )
noexcept
664 static constexpr iterator
665 _S_parse_width_or_precision(iterator __first, iterator __last,
666 unsigned short& __val,
bool& __arg_id,
667 basic_format_parse_context<_CharT>& __pc)
669 if (__format::__is_digit(*__first))
671 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
673 __throw_format_error(
"format error: invalid width or precision "
678 else if (*__first ==
'{')
682 if (__first == __last)
683 __format::__unmatched_left_brace_in_format_string();
685 __val = __pc.next_arg_id();
688 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
689 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
690 __format::__invalid_arg_id_in_format_string();
692 __pc.check_arg_id(__v);
695#if __cpp_lib_format >= 202305L
696 __pc.check_dynamic_spec_integral(__val);
705 _M_parse_width(iterator __first, iterator __last,
706 basic_format_parse_context<_CharT>& __pc)
708 bool __arg_id =
false;
710 __throw_format_error(
"format error: width must be non-zero in "
712 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
714 if (__next != __first)
715 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
721 _M_parse_precision(iterator __first, iterator __last,
722 basic_format_parse_context<_CharT>& __pc)
724 if (__first[0] !=
'.')
727 iterator __next = ++__first;
728 bool __arg_id =
false;
729 if (__next != __last)
730 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
732 if (__next == __first)
733 __throw_format_error(
"format error: missing precision after '.' in "
735 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
741 _M_parse_locale(iterator __first, iterator )
noexcept
751 template<
typename _Context>
752 _GLIBCXX_CONSTEXPR_FORMAT
size_t
753 _M_get_width(_Context& __ctx)
const
756 if (_M_width_kind == _WP_value)
758 else if (_M_width_kind == _WP_from_arg)
759 __width = __format::__int_from_arg(__ctx.arg(_M_width));
763 template<
typename _Context>
764 _GLIBCXX_CONSTEXPR_FORMAT
size_t
765 _M_get_precision(_Context& __ctx)
const
768 if (_M_prec_kind == _WP_value)
770 else if (_M_prec_kind == _WP_from_arg)
771 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
776 template<
typename _Int>
777 inline _GLIBCXX_CONSTEXPR_FORMAT
char*
778 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
782 else if (__sign == _Sign_plus)
784 else if (__sign == _Sign_space)
792 template<
typename _Out,
typename _CharT>
793 requires output_iterator<_Out, const _CharT&>
794 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
795 __write(_Out __out, basic_string_view<_CharT> __str)
797 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
803 for (_CharT __c : __str)
810 template<
typename _Out,
typename _CharT>
811 _GLIBCXX_CONSTEXPR_FORMAT _Out
812 __write_padded(_Out __out, basic_string_view<_CharT> __str,
813 _Align __align,
size_t __nfill,
char32_t __fill_char)
815 const size_t __buflen = 0x20;
816 _CharT __padding_chars[__buflen];
817 __padding_chars[0] = _CharT();
818 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
820 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
823 while (__n > __padding.size())
825 __o = __format::__write(
std::move(__o), __padding);
826 __n -= __padding.size();
829 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
832 size_t __l, __r, __max;
833 if (__align == _Align_centre)
836 __r = __l + (__nfill & 1);
839 else if (__align == _Align_right)
852 using namespace __unicode;
853 if constexpr (__literal_encoding_is_unicode<_CharT>())
854 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
857 const char32_t __arr[1]{ __fill_char };
858 _Utf_view<_CharT, span<const char32_t, 1>> __v(__arr);
859 basic_string<_CharT> __padstr(__v.begin(), __v.end());
860 __padding = __padstr;
862 __out = __format::__write(
std::move(__out), __padding);
863 __out = __format::__write(
std::move(__out), __str);
865 __out = __format::__write(
std::move(__out), __padding);
869 if (__max < __buflen)
870 __padding.remove_suffix(__buflen - __max);
874 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
876 __out = __format::__write(
std::move(__out), __str);
884 template<
typename _CharT,
typename _Out>
885 _GLIBCXX_CONSTEXPR_FORMAT _Out
886 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
887 size_t __estimated_width,
888 basic_format_context<_Out, _CharT>& __fc,
889 const _Spec<_CharT>& __spec,
890 _Align __align = _Align_left)
892 size_t __width = __spec._M_get_width(__fc);
894 if (__width <= __estimated_width)
895 return __format::__write(__fc.out(), __str);
897 const size_t __nfill = __width - __estimated_width;
899 if (__spec._M_align != _Align_default)
900 __align = __spec._M_align;
902 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
906 template<
typename _CharT>
907 _GLIBCXX_CONSTEXPR_FORMAT
size_t
908 __truncate(basic_string_view<_CharT>& __s,
size_t __prec)
910 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
912 if (__prec != (
size_t)-1)
913 return __unicode::__truncate(__s, __prec);
915 return __unicode::__field_width(__s);
919 __s = __s.substr(0, __prec);
924 enum class _Term_char :
unsigned char {
929 using enum _Term_char;
931 template<
typename _CharT>
934 using _Str_view = basic_string_view<_CharT>;
938 {
return _GLIBCXX_WIDEN(
"\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
942 {
return _S_all().substr(0, 3); }
945 _Str_view _S_newline()
946 {
return _S_all().substr(3, 3); }
949 _Str_view _S_return()
950 {
return _S_all().substr(6, 3); }
953 _Str_view _S_bslash()
954 {
return _S_all().substr(9, 3); }
958 {
return _S_all().substr(12, 3); }
962 {
return _S_all().substr(15, 3); }
966 {
return _S_all().substr(18, 2); }
970 {
return _S_all().substr(20, 2); }
973 _Str_view _S_term(_Term_char __term)
980 return _S_quote().substr(0, 1);
982 return _S_apos().substr(0, 1);
984 __builtin_unreachable();
988 template<
typename _CharT>
991 using _Str_view = basic_string_view<_CharT>;
995 {
return _GLIBCXX_WIDEN(
"[]{}(), : "); }
998 _Str_view _S_squares()
999 {
return _S_all().substr(0, 2); }
1002 _Str_view _S_braces()
1003 {
return _S_all().substr(2, 2); }
1006 _Str_view _S_parens()
1007 {
return _S_all().substr(4, 2); }
1010 _Str_view _S_comma()
1011 {
return _S_all().substr(6, 2); }
1014 _Str_view _S_colon()
1015 {
return _S_all().substr(8, 2); }
1018 template<
typename _CharT>
1019 constexpr bool __should_escape_ascii(_CharT __c, _Term_char __term)
1021 using _Esc = _Escapes<_CharT>;
1024 case _Esc::_S_tab()[0]:
1025 case _Esc::_S_newline()[0]:
1026 case _Esc::_S_return()[0]:
1027 case _Esc::_S_bslash()[0]:
1029 case _Esc::_S_quote()[0]:
1030 return __term == _Term_quote;
1031 case _Esc::_S_apos()[0]:
1032 return __term == _Term_apos;
1034 return (__c >= 0 && __c < 0x20) || __c == 0x7f;
1039 constexpr bool __should_escape_unicode(
char32_t __c,
bool __prev_esc)
1041 if (__unicode::__should_escape_category(__c))
1045 return __unicode::__grapheme_cluster_break_property(__c)
1046 == __unicode::_Gcb_property::_Gcb_Extend;
1049 using uint_least32_t = __UINT_LEAST32_TYPE__;
1050 template<
typename _Out,
typename _CharT>
1051 _GLIBCXX_CONSTEXPR_FORMAT _Out
1052 __write_escape_seq(_Out __out, uint_least32_t __val,
1053 basic_string_view<_CharT> __prefix)
1055 constexpr size_t __max = 8;
1057 const string_view __narrow(
1059 std::__to_chars_i<uint_least32_t>(__buf, __buf + __max, __val, 16).ptr);
1061 __out = __format::__write(__out, __prefix);
1062 *__out = _Separators<_CharT>::_S_braces()[0];
1064 if constexpr (is_same_v<char, _CharT>)
1065 __out = __format::__write(__out, __narrow);
1066#ifdef _GLIBCXX_USE_WCHAR_T
1069 wchar_t __wbuf[__max];
1070 const size_t __n = __narrow.size();
1071 std::__to_wstring_numeric(__narrow.data(), __n, __wbuf);
1072 __out = __format::__write(__out, wstring_view(__wbuf, __n));
1075 *__out = _Separators<_CharT>::_S_braces()[1];
1079 template<
typename _Out,
typename _CharT>
1080 _GLIBCXX_CONSTEXPR_FORMAT _Out
1081 __write_escape_seqs(_Out __out, basic_string_view<_CharT> __units)
1083 using _UChar = make_unsigned_t<_CharT>;
1084 for (_CharT __c : __units)
1085 __out = __format::__write_escape_seq(
1086 __out,
static_cast<_UChar
>(__c), _Escapes<_CharT>::_S_x());
1090 template<
typename _Out,
typename _CharT>
1091 _GLIBCXX_CONSTEXPR_FORMAT _Out
1092 __write_escaped_char(_Out __out, _CharT __c)
1094 using _UChar = make_unsigned_t<_CharT>;
1095 using _Esc = _Escapes<_CharT>;
1098 case _Esc::_S_tab()[0]:
1099 return __format::__write(__out, _Esc::_S_tab().substr(1, 2));
1100 case _Esc::_S_newline()[0]:
1101 return __format::__write(__out, _Esc::_S_newline().substr(1, 2));
1102 case _Esc::_S_return()[0]:
1103 return __format::__write(__out, _Esc::_S_return().substr(1, 2));
1104 case _Esc::_S_bslash()[0]:
1105 return __format::__write(__out, _Esc::_S_bslash().substr(1, 2));
1106 case _Esc::_S_quote()[0]:
1107 return __format::__write(__out, _Esc::_S_quote().substr(1, 2));
1108 case _Esc::_S_apos()[0]:
1109 return __format::__write(__out, _Esc::_S_apos().substr(1, 2));
1111 return __format::__write_escape_seq(
1112 __out,
static_cast<_UChar
>(__c), _Esc::_S_u());
1116 template<
typename _CharT,
typename _Out>
1117 _GLIBCXX_CONSTEXPR_FORMAT _Out
1118 __write_escaped_ascii(_Out __out,
1119 basic_string_view<_CharT> __str,
1122 using _Str_view = basic_string_view<_CharT>;
1126 constexpr _Str_view __supported(_GLIBCXX_WIDEN(
1127 "ABCDEFGHIJKLMNOPQRSTUWXYZ"
1128 "abdeefghijklmnopqrstuwzyz"
1129 " !#$%&'()*+-./:;<=>?[]^_{|}~"
1130 "0123456789" "\t\n\r\\\"\'\0"
1132 if (__str.find_first_not_of(__supported) != _Str_view::npos)
1133#if __has_builtin(__builtin_constexpr_diag)
1134 __builtin_constexpr_diag (2,
"",
1135 "for non-Unicode literal encodings, only"
1136 " printable ASCII characters and standard"
1137 " escape sequencess can be escaped in constant"
1144 auto __first = __str.begin();
1145 auto const __last = __str.end();
1146 while (__first != __last)
1148 auto __print = __first;
1150 while (__print != __last
1151 && !__format::__should_escape_ascii(*__print, __term))
1154 if (__print != __first)
1155 __out = __format::__write(__out, _Str_view(__first, __print));
1157 if (__print == __last)
1161 __out = __format::__write_escaped_char(__out, *__first);
1167 template<
typename _CharT,
typename _Out>
1168 _GLIBCXX_CONSTEXPR_FORMAT _Out
1169 __write_escaped_unicode_part(_Out __out, basic_string_view<_CharT>& __str,
1170 bool& __prev_esc, _Term_char __term)
1172 using _Str_view = basic_string_view<_CharT>;
1173 using _Esc = _Escapes<_CharT>;
1175 static constexpr char32_t __replace = U
'\uFFFD';
1176 static constexpr _Str_view __replace_rep = []
1179 if constexpr (is_same_v<char, _CharT>)
1180 return "\xEF\xBF\xBD";
1185 __unicode::_Utf_view<char32_t, _Str_view> __v(
std::move(__str));
1188 auto __first = __v.begin();
1189 auto const __last = __v.end();
1190 while (__first != __last)
1192 bool __esc_ascii =
false;
1193 bool __esc_unicode =
false;
1194 bool __esc_replace =
false;
1195 auto __should_escape = [&](
auto const& __it)
1199 = __format::__should_escape_ascii(*__it.base(), __term);
1200 if (__format::__should_escape_unicode(*__it, __prev_esc))
1201 return __esc_unicode =
true;
1202 if (*__it == __replace)
1204 _Str_view __units(__it.base(), __it._M_units());
1205 return __esc_replace = (__units != __replace_rep);
1210 auto __print = __first;
1211 while (__print != __last && !__should_escape(__print))
1217 if (__print != __first)
1218 __out = __format::__write(__out, _Str_view(__first.base(), __print.base()));
1220 if (__print == __last)
1225 __out = __format::__write_escaped_char(__out, *__first.base());
1226 else if (__esc_unicode)
1227 __out = __format::__write_escape_seq(__out, *__first, _Esc::_S_u());
1229 else if (_Str_view __units(__first.base(), __first._M_units());
1230 __units.end() != __last.base())
1231 __out = __format::__write_escape_seqs(__out, __units);
1245 template<
typename _CharT,
typename _Out>
1246 _GLIBCXX_CONSTEXPR_FORMAT _Out
1247 __write_escaped_unicode(_Out __out, basic_string_view<_CharT> __str,
1250 bool __prev_escape =
true;
1251 __out = __format::__write_escaped_unicode_part(__out, __str,
1252 __prev_escape, __term);
1253 __out = __format::__write_escape_seqs(__out, __str);
1257 template<
typename _CharT,
typename _Out>
1258 _GLIBCXX_CONSTEXPR_FORMAT _Out
1259 __write_escaped(_Out __out, basic_string_view<_CharT> __str, _Term_char __term)
1261 __out = __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1263 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1264 __out = __format::__write_escaped_unicode(__out, __str, __term);
1265 else if constexpr (is_same_v<char, _CharT>
1266 && __unicode::__literal_encoding_is_extended_ascii())
1267 __out = __format::__write_escaped_ascii(__out, __str, __term);
1270 __out = __format::__write_escaped_ascii(__out, __str, __term);
1272 return __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1276 struct _Optional_locale
1278 [[__gnu__::__always_inline__]]
1279 _GLIBCXX_CONSTEXPR_FORMAT
1280 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
1282 _Optional_locale(
const locale& __loc) noexcept
1283 : _M_loc(__loc), _M_hasval(
true)
1286 _GLIBCXX_CONSTEXPR_FORMAT
1287 _Optional_locale(
const _Optional_locale& __l) noexcept
1288 : _M_dummy(), _M_hasval(__l._M_hasval)
1291 std::construct_at(&_M_loc, __l._M_loc);
1294 _GLIBCXX_CONSTEXPR_FORMAT
1296 operator=(
const _Optional_locale& __l)
noexcept
1301 _M_loc = __l._M_loc;
1308 else if (__l._M_hasval)
1310 std::construct_at(&_M_loc, __l._M_loc);
1316 _GLIBCXX_CONSTEXPR_FORMAT
1317 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
1320 operator=(locale&& __loc)
noexcept
1326 std::construct_at(&_M_loc,
std::move(__loc));
1337 std::construct_at(&_M_loc);
1343 _GLIBCXX_CONSTEXPR_FORMAT
bool
1344 has_value() const noexcept {
return _M_hasval; }
1347 char _M_dummy =
'\0';
1350 bool _M_hasval =
false;
1353 template<__
char _CharT>
1354 struct __formatter_str
1356 __formatter_str() =
default;
1359 __formatter_str(_Spec<_CharT> __spec) noexcept
1363 constexpr typename basic_format_parse_context<_CharT>::iterator
1364 parse(basic_format_parse_context<_CharT>& __pc)
1366 auto __first = __pc.begin();
1367 const auto __last = __pc.end();
1368 _Spec<_CharT> __spec{};
1370 auto __finalize = [
this, &__spec] {
1374 auto __finished = [&] {
1375 if (__first == __last || *__first ==
'}')
1386 __first = __spec._M_parse_fill_and_align(__first, __last);
1390 __first = __spec._M_parse_width(__first, __last, __pc);
1394 __first = __spec._M_parse_precision(__first, __last, __pc);
1398 if (*__first ==
's')
1400 __spec._M_type = _Pres_s;
1403#if __glibcxx_format_ranges
1404 else if (*__first ==
'?')
1406 __spec._M_debug =
true;
1414 __format::__failed_to_parse_format_spec();
1417 template<
typename _Out>
1418 _GLIBCXX_CONSTEXPR_FORMAT _Out
1419 format(basic_string_view<_CharT> __s,
1420 basic_format_context<_Out, _CharT>& __fc)
const
1422 if (_M_spec._M_debug)
1423 return _M_format_escaped(__s, __fc);
1425 if (_M_spec._M_width_kind == _WP_none
1426 && _M_spec._M_prec_kind == _WP_none)
1427 return __format::__write(__fc.out(), __s);
1429 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1430 const size_t __width = __format::__truncate(__s, __maxwidth);
1431 return __format::__write_padded_as_spec(__s, __width, __fc, _M_spec);
1434 template<
typename _Out>
1435 _GLIBCXX_CONSTEXPR_FORMAT _Out
1436 _M_format_escaped(basic_string_view<_CharT> __s,
1437 basic_format_context<_Out, _CharT>& __fc)
const
1439 const size_t __padwidth = _M_spec._M_get_width(__fc);
1440 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1441 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1443 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1444 const size_t __width = __truncate(__s, __maxwidth);
1446 if (__padwidth <= __width && _M_spec._M_prec_kind == _WP_none)
1447 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1452 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth, __maxwidth);
1453 __format::__write_escaped(__sink.out(), __s, _Term_quote);
1454 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1457#if __glibcxx_format_ranges
1458 template<ranges::input_range _Rg,
typename _Out>
1459 requires same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _CharT>
1460 _GLIBCXX_CONSTEXPR_FORMAT _Out
1461 _M_format_range(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
1463 using _Range = remove_reference_t<_Rg>;
1464 using _String_view = basic_string_view<_CharT>;
1465 if constexpr (ranges::contiguous_range<_Rg>)
1467 _String_view __str(ranges::data(__rg),
1468 size_t(ranges::distance(__rg)));
1469 return format(__str, __fc);
1471 else if constexpr (!is_const_v<_Range>
1472 && __simply_formattable_range<_Range, _CharT>)
1473 return _M_format_range<const _Range&>(__rg, __fc);
1474 else if constexpr (!is_lvalue_reference_v<_Rg>)
1475 return _M_format_range<_Range&>(__rg, __fc);
1478 auto __handle_debug = [
this, &__rg]<
typename _NOut>(_NOut __nout)
1480 if (!_M_spec._M_debug)
1481 return ranges::copy(__rg,
std::move(__nout)).out;
1483 _Escaping_sink<_NOut, _CharT>
1485 ranges::copy(__rg, __sink.out());
1486 return __sink._M_finish();
1489 const size_t __padwidth = _M_spec._M_get_width(__fc);
1490 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1491 return __handle_debug(__fc.out());
1493 _Padding_sink<_Out, _CharT>
1494 __sink(__fc.out(), __padwidth, _M_spec._M_get_precision(__fc));
1495 __handle_debug(__sink.out());
1496 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1501 set_debug_format() noexcept
1502 { _M_spec._M_debug =
true; }
1506 _Spec<_CharT> _M_spec{};
1511 [[__gnu__::__always_inline__]]
1513 __toupper_numeric(
char __c)
1517 case 'a':
return 'A';
1518 case 'b':
return 'B';
1519 case 'c':
return 'C';
1520 case 'd':
return 'D';
1521 case 'e':
return 'E';
1522 case 'f':
return 'F';
1523 case 'i':
return 'I';
1524 case 'n':
return 'N';
1525 case 'p':
return 'P';
1526 case 'x':
return 'X';
1527 default:
return __c;
1531 template<__
char _CharT>
1532 struct __formatter_int
1536 static constexpr _Pres_type _AsInteger = _Pres_d;
1537 static constexpr _Pres_type _AsBool = _Pres_s;
1538 static constexpr _Pres_type _AsChar = _Pres_c;
1540 __formatter_int() =
default;
1543 __formatter_int(_Spec<_CharT> __spec) noexcept
1546 if (_M_spec._M_type == _Pres_none)
1547 _M_spec._M_type = _Pres_d;
1550 constexpr typename basic_format_parse_context<_CharT>::iterator
1551 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1553 _Spec<_CharT> __spec{};
1554 __spec._M_type = __type;
1556 const auto __last = __pc.end();
1557 auto __first = __pc.begin();
1559 auto __finalize = [
this, &__spec] {
1563 auto __finished = [&] {
1564 if (__first == __last || *__first ==
'}')
1575 __first = __spec._M_parse_fill_and_align(__first, __last);
1579 __first = __spec._M_parse_sign(__first, __last);
1583 __first = __spec._M_parse_alternate_form(__first, __last);
1587 __first = __spec._M_parse_zero_fill(__first, __last);
1591 __first = __spec._M_parse_width(__first, __last, __pc);
1595 __first = __spec._M_parse_locale(__first, __last);
1602 __spec._M_type = _Pres_b;
1606 __spec._M_type = _Pres_B;
1612 if (__type != _AsBool)
1614 __spec._M_type = _Pres_c;
1619 __spec._M_type = _Pres_d;
1623 __spec._M_type = _Pres_o;
1627 __spec._M_type = _Pres_x;
1631 __spec._M_type = _Pres_X;
1635 if (__type == _AsBool)
1637 __spec._M_type = _Pres_s;
1641#if __glibcxx_format_ranges
1643 if (__type == _AsChar)
1645 __spec._M_debug =
true;
1655 __format::__failed_to_parse_format_spec();
1658 template<
typename _Tp>
1659 constexpr typename basic_format_parse_context<_CharT>::iterator
1660 _M_parse(basic_format_parse_context<_CharT>& __pc)
1662 if constexpr (is_same_v<_Tp, bool>)
1664 auto __end = _M_do_parse(__pc, _AsBool);
1665 if (_M_spec._M_type == _Pres_s)
1666 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1667 || _M_spec._M_zero_fill)
1668 __throw_format_error(
"format error: format-spec contains "
1669 "invalid formatting options for "
1673 else if constexpr (__char<_Tp>)
1675 auto __end = _M_do_parse(__pc, _AsChar);
1676 if (_M_spec._M_type == _Pres_c)
1677 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1678 || _M_spec._M_zero_fill
1680 __throw_format_error(
"format error: format-spec contains "
1681 "invalid formatting options for "
1686 return _M_do_parse(__pc, _AsInteger);
1689 template<
typename _Int,
typename _Out>
1690 _GLIBCXX_CONSTEXPR_FORMAT
1691 typename basic_format_context<_Out, _CharT>::iterator
1692 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1694 if (_M_spec._M_type == _Pres_c)
1695 return _M_format_character(_S_to_character(__i), __fc);
1697 constexpr size_t __buf_size =
sizeof(_Int) * __CHAR_BIT__ + 3;
1698 char __buf[__buf_size];
1699 to_chars_result __res{};
1701 string_view __base_prefix;
1702 make_unsigned_t<_Int> __u;
1704 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1708 char* __start = __buf + 3;
1709 char*
const __end = __buf +
sizeof(__buf);
1710 char*
const __start_digits = __start;
1712 switch (_M_spec._M_type)
1716 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1717 __res = to_chars(__start, __end, __u, 2);
1721 return _M_format_character(_S_to_character(__i), __fc);
1728 __res = to_chars(__start, __end, __u, 10);
1732 __base_prefix =
"0";
1733 __res = to_chars(__start, __end, __u, 8);
1737 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1738 __res = to_chars(__start, __end, __u, 16);
1739 if (_M_spec._M_type == _Pres_X)
1740 for (
auto __p = __start; __p != __res.ptr; ++__p)
1741 *__p = __format::__toupper_numeric(*__p);
1745 if (_M_spec._M_alt && __base_prefix.size())
1747 __start -= __base_prefix.size();
1748 ranges::copy(__base_prefix, __start);
1750 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1752 string_view __narrow_str(__start, __res.ptr - __start);
1753 size_t __prefix_len = __start_digits - __start;
1754 if constexpr (is_same_v<char, _CharT>)
1755 return _M_format_int(__narrow_str, __prefix_len, __fc);
1756#ifdef _GLIBCXX_USE_WCHAR_T
1759 _CharT __wbuf[__buf_size];
1760 size_t __n = __narrow_str.size();
1763 std::__to_wstring_numeric(__narrow_str.data(), __n, __wbuf);
1764 return _M_format_int(basic_string_view<_CharT>(__wbuf, __n),
1765 __prefix_len, __fc);
1770 template<
typename _Out>
1771 _GLIBCXX_CONSTEXPR_FORMAT
1772 typename basic_format_context<_Out, _CharT>::iterator
1773 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1775 if (_M_spec._M_type == _Pres_c)
1776 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1777 if (_M_spec._M_type != _Pres_s)
1778 return format(
static_cast<unsigned char>(__i), __fc);
1780 basic_string<_CharT> __s;
1782 if (_M_spec._M_localized) [[unlikely]]
1785 __s = __i ? __np.truename() : __np.falsename();
1786 __est_width = __s.size();
1790 if constexpr (is_same_v<char, _CharT>)
1791 __s = __i ?
"true" :
"false";
1793 __s = __i ? L
"true" : L
"false";
1794 __est_width = __s.size();
1797 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1801 template<
typename _Out>
1802 _GLIBCXX_CONSTEXPR_FORMAT
1803 typename basic_format_context<_Out, _CharT>::iterator
1804 _M_format_character(_CharT __c,
1805 basic_format_context<_Out, _CharT>& __fc)
const
1807 basic_string_view<_CharT> __in(&__c, 1u);
1808 size_t __width = 1u;
1810 if constexpr (
sizeof(_CharT) > 1u &&
1811 __unicode::__literal_encoding_is_unicode<_CharT>())
1812 __width = __unicode::__field_width(__c);
1814 if (!_M_spec._M_debug)
1815 return __format::__write_padded_as_spec(__in, __width,
1819 if (_M_spec._M_get_width(__fc) <= __width)
1820 return __format::__write_escaped(__fc.out(), __in, _Term_apos);
1823 _Fixedbuf_sink<_CharT> __sink(__buf);
1824 __format::__write_escaped(__sink.out(), __in, _Term_apos);
1826 __in = __sink.view();
1827 if (__in[1] == _Escapes<_CharT>::_S_bslash()[0])
1828 __width = __in.size();
1829 return __format::__write_padded_as_spec(__in, __width,
1833 template<
typename _Int>
1834 static _GLIBCXX_CONSTEXPR_FORMAT _CharT
1835 _S_to_character(_Int __i)
1838 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1840 if (_Traits::__min <= __i && __i <= _Traits::__max)
1841 return static_cast<_CharT
>(__i);
1843 else if constexpr (is_signed_v<_Int>)
1845 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1846 return static_cast<_CharT
>(__i);
1848 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1849 return static_cast<_CharT
>(__i);
1850 __throw_format_error(
"format error: integer not representable as "
1854 template<
typename _Out>
1855 _GLIBCXX_CONSTEXPR_FORMAT
1856 typename basic_format_context<_Out, _CharT>::iterator
1857 _M_format_int(basic_string_view<_CharT> __str,
size_t __prefix_len,
1858 basic_format_context<_Out, _CharT>& __fc)
const
1860 size_t __width = _M_spec._M_get_width(__fc);
1861 if (_M_spec._M_localized)
1863 const auto& __l = __fc.locale();
1864 if (__l.name() !=
"C")
1866 auto& __np = use_facet<numpunct<_CharT>>(__l);
1867 string __grp = __np.grouping();
1870 size_t __n = __str.size() - __prefix_len;
1871 auto __p = (_CharT*)__builtin_alloca(2 * __n
1874 auto __s = __str.data();
1875 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1876 __s += __prefix_len;
1877 auto __end = std::__add_grouping(__p + __prefix_len,
1878 __np.thousands_sep(),
1882 __str = {__p, size_t(__end - __p)};
1887 if (__width <= __str.size())
1888 return __format::__write(__fc.out(), __str);
1890 char32_t __fill_char = _M_spec._M_fill;
1891 _Align __align = _M_spec._M_align;
1893 size_t __nfill = __width - __str.size();
1894 auto __out = __fc.out();
1895 if (__align == _Align_default)
1897 __align = _Align_right;
1898 if (_M_spec._M_zero_fill)
1900 __fill_char = _CharT(
'0');
1902 if (__prefix_len != 0)
1904 __out = __format::__write(
std::move(__out),
1905 __str.substr(0, __prefix_len));
1906 __str.remove_prefix(__prefix_len);
1910 __fill_char = _CharT(
' ');
1912 return __format::__write_padded(
std::move(__out), __str,
1913 __align, __nfill, __fill_char);
1916 _Spec<_CharT> _M_spec{};
1919#ifdef __BFLT16_DIG__
1920 using __bflt16_t =
decltype(0.0bf16);
1931#undef _GLIBCXX_FORMAT_F128
1933#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1936 using __flt128_t = __ieee128;
1937# define _GLIBCXX_FORMAT_F128 1
1939#ifdef __LONG_DOUBLE_IEEE128__
1943 to_chars(
char*,
char*, __ibm128)
noexcept
1944 __asm(
"_ZSt8to_charsPcS_e");
1947 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1948 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1951 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1952 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1953#elif __cplusplus == 202002L
1955 to_chars(
char*,
char*, __ieee128)
noexcept
1956 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1959 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1960 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1963 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1964 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1967#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1970 using __flt128_t =
long double;
1971# define _GLIBCXX_FORMAT_F128 2
1973#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1976 using __flt128_t = _Float128;
1977# define _GLIBCXX_FORMAT_F128 3
1979# if __cplusplus == 202002L
1983 to_chars(
char*,
char*, _Float128)
noexcept
1984# if _GLIBCXX_INLINE_VERSION
1985 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1987 __asm(
"_ZSt8to_charsPcS_DF128_");
1991 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1992# if _GLIBCXX_INLINE_VERSION
1993 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1995 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
1999 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
2000# if _GLIBCXX_INLINE_VERSION
2001 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
2003 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
2008 using std::to_chars;
2011 template<
typename _Tp>
2012 concept __formattable_float
2013 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
2014 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
2016 template<__
char _CharT>
2017 struct __formatter_fp
2019 constexpr typename basic_format_parse_context<_CharT>::iterator
2020 parse(basic_format_parse_context<_CharT>& __pc)
2022 _Spec<_CharT> __spec{};
2023 const auto __last = __pc.end();
2024 auto __first = __pc.begin();
2026 auto __finalize = [
this, &__spec] {
2030 auto __finished = [&] {
2031 if (__first == __last || *__first ==
'}')
2042 __first = __spec._M_parse_fill_and_align(__first, __last);
2046 __first = __spec._M_parse_sign(__first, __last);
2050 __first = __spec._M_parse_alternate_form(__first, __last);
2054 __first = __spec._M_parse_zero_fill(__first, __last);
2058 if (__first[0] !=
'.')
2060 __first = __spec._M_parse_width(__first, __last, __pc);
2065 __first = __spec._M_parse_precision(__first, __last, __pc);
2069 __first = __spec._M_parse_locale(__first, __last);
2076 __spec._M_type = _Pres_a;
2080 __spec._M_type = _Pres_A;
2084 __spec._M_type = _Pres_e;
2088 __spec._M_type = _Pres_E;
2092 __spec._M_type = _Pres_f;
2096 __spec._M_type = _Pres_F;
2100 __spec._M_type = _Pres_g;
2104 __spec._M_type = _Pres_G;
2112 __format::__failed_to_parse_format_spec();
2115 template<
typename _Fp,
typename _Out>
2116 typename basic_format_context<_Out, _CharT>::iterator
2117 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2121 to_chars_result __res{};
2124 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2126 __prec = _M_spec._M_get_precision(__fc);
2129 bool __upper =
false;
2130 bool __trailing_zeros =
false;
2132 size_t __offset = 1;
2134 switch (_M_spec._M_type)
2137 if (__builtin_isfinite(__v))
2143 __fmt = chars_format::hex;
2146 if (__builtin_isfinite(__v))
2151 __fmt = chars_format::hex;
2159 __fmt = chars_format::scientific;
2166 __fmt = chars_format::fixed;
2173 __trailing_zeros =
true;
2175 __fmt = chars_format::general;
2180 __fmt = chars_format::general;
2184 char* __start = __buf + __offset;
2185 char* __end = __buf +
sizeof(__buf);
2188 auto __to_chars = [&](
char* __b,
char* __e) {
2190 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2191 else if (__fmt != chars_format{})
2192 return __format::to_chars(__b, __e, __v, __fmt);
2194 return __format::to_chars(__b, __e, __v);
2198 __res = __to_chars(__start, __end);
2200 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2204 size_t __guess = 7 + __offset + __prec;
2205 if (__fmt == chars_format::fixed)
2207 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2208 || is_same_v<_Fp, long double>)
2213 if constexpr (is_same_v<_Fp, float>)
2214 __builtin_frexpf(__v, &__exp);
2215 else if constexpr (is_same_v<_Fp, double>)
2216 __builtin_frexp(__v, &__exp);
2217 else if constexpr (is_same_v<_Fp, long double>)
2218 __builtin_frexpl(__v, &__exp);
2220 __guess += 1U + __exp * 4004U / 13301U;
2223 __guess += numeric_limits<_Fp>::max_exponent10;
2225 if (__guess <=
sizeof(__buf)) [[unlikely]]
2226 __guess =
sizeof(__buf) * 2;
2235 auto __overwrite = [&__to_chars, &__res, __offset] (
char* __p,
size_t __n)
2237 __res = __to_chars(__p + __offset, __p + __n - __offset);
2238 return __res.ec == errc{} ? __res.ptr - __p : 0;
2243 __start = __dynbuf.
data() + __offset;
2244 __end = __dynbuf.
data() + __dynbuf.
size();
2246 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2252 if (__builtin_signbit(__v))
2253 ranges::copy(string_view(
"-0x"), __start);
2255 ranges::copy(string_view(
"0x"), __start);
2261 for (
char* __p = __start; __p != __res.ptr; ++__p)
2262 *__p = __format::__toupper_numeric(*__p);
2266 if (!__builtin_signbit(__v))
2268 if (_M_spec._M_sign == _Sign_plus)
2270 else if (_M_spec._M_sign == _Sign_space)
2276 string_view __narrow_str(__start, __res.ptr - __start);
2280 if (_M_spec._M_alt && __builtin_isfinite(__v))
2282 string_view __s = __narrow_str;
2286 size_t __d = __s.find(
'.');
2287 if (__d != __s.npos)
2289 __p = __s.find(__expc, __d + 1);
2290 if (__p == __s.npos)
2294 if (__trailing_zeros)
2297 if (__s[__offset] !=
'0')
2299 __sigfigs = __p - __offset - 1;
2304 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2309 __p = __s.find(__expc);
2310 if (__p == __s.npos)
2313 __sigfigs = __d - __offset;
2316 if (__trailing_zeros && __prec != 0)
2321 __z = __prec - __sigfigs;
2324 if (
size_t __extras =
int(__d == __p) + __z)
2326 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2330 __builtin_memmove(__start + __p + __extras,
2334 __start[__p++] =
'.';
2335 __builtin_memset(__start + __p,
'0', __z);
2336 __narrow_str = {__s.data(), __s.size() + __extras};
2340 __dynbuf.
reserve(__s.size() + __extras);
2341 if (__dynbuf.
empty())
2343 __dynbuf = __s.
substr(0, __p);
2347 __dynbuf.
append(__z,
'0');
2348 __dynbuf.
append(__s.substr(__p));
2352 __dynbuf.
insert(__p, __extras,
'0');
2354 __dynbuf[__p] =
'.';
2356 __narrow_str = __dynbuf;
2361 basic_string<_CharT> __wstr;
2362 basic_string_view<_CharT> __str;
2363 if constexpr (is_same_v<_CharT, char>)
2364 __str = __narrow_str;
2365#ifdef _GLIBCXX_USE_WCHAR_T
2370 __wstr = std::__to_wstring_numeric(__narrow_str);
2375 if (_M_spec._M_localized && __builtin_isfinite(__v))
2377 auto __s = _M_localize(__str, __expc, __offset, __fc.locale());
2382 size_t __width = _M_spec._M_get_width(__fc);
2384 if (__width <= __str.size())
2385 return __format::__write(__fc.out(), __str);
2387 char32_t __fill_char = _M_spec._M_fill;
2388 _Align __align = _M_spec._M_align;
2390 size_t __nfill = __width - __str.size();
2391 auto __out = __fc.out();
2392 if (__align == _Align_default)
2394 __align = _Align_right;
2395 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2397 __fill_char = _CharT(
'0');
2400 __out = __format::__write(__out, __str.substr(0, __offset));
2401 __str.remove_prefix(__offset);
2405 __fill_char = _CharT(
' ');
2407 return __format::__write_padded(
std::move(__out), __str,
2408 __align, __nfill, __fill_char);
2412 basic_string<_CharT>
2413 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2414 int __offset,
const locale& __loc)
const
2416 basic_string<_CharT> __lstr;
2418 if (__loc == locale::classic())
2421 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2422 const _CharT __point = __np.decimal_point();
2423 const string __grp = __np.grouping();
2425 _CharT __dot, __exp;
2426 if constexpr (is_same_v<_CharT, char>)
2449 __builtin_unreachable();
2453 if (__grp.empty() && __point == __dot)
2456 size_t __d = __str.find(__dot);
2457 size_t __e = min(__d, __str.find(__exp));
2458 if (__e == __str.npos)
2460 const size_t __r = __str.size() - __e;
2461 auto __overwrite = [&](_CharT* __p, size_t) {
2463 ranges::copy_n(__str.data(), __offset, __p);
2465 auto __end = std::__add_grouping(__p + __offset, __np.thousands_sep(),
2466 __grp.data(), __grp.size(),
2467 __str.data() + __offset,
2468 __str.data() + __e);
2471 if (__d != __str.npos)
2477 const size_t __rlen = __str.size() - __e;
2479 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2482 return (__end - __p);
2484 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2488 _Spec<_CharT> _M_spec{};
2491 template<__format::__
char _CharT>
2492 struct __formatter_ptr
2495 __formatter_ptr() noexcept
2498 _M_spec._M_type = _Pres_x;
2499 _M_spec._M_alt =
true;
2503 __formatter_ptr(_Spec<_CharT> __spec) noexcept
2505 { _M_set_default(); }
2507 constexpr typename basic_format_parse_context<_CharT>::iterator
2508 parse(basic_format_parse_context<_CharT>& __pc)
2510 __format::_Spec<_CharT> __spec{};
2511 const auto __last = __pc.end();
2512 auto __first = __pc.begin();
2514 auto __finalize = [
this, &__spec] {
2519 auto __finished = [&] {
2520 if (__first == __last || *__first ==
'}')
2531 __first = __spec._M_parse_fill_and_align(__first, __last);
2537#if __glibcxx_format >= 202304L
2538 __first = __spec._M_parse_zero_fill(__first, __last);
2543 __first = __spec._M_parse_width(__first, __last, __pc);
2547 if (*__first ==
'p')
2549 __spec._M_type = _Pres_x;
2550 __spec._M_alt =
true;
2553#if __glibcxx_format >= 202304L
2554 else if (*__first ==
'P')
2556 __spec._M_type = _Pres_X;
2557 __spec._M_alt =
true;
2565 __format::__failed_to_parse_format_spec();
2568 template<
typename _Out>
2569 _GLIBCXX_CONSTEXPR_FORMAT
2570 typename basic_format_context<_Out, _CharT>::iterator
2571 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2574 ?
reinterpret_cast<__UINTPTR_TYPE__
>(__v)
2575 : static_cast<__UINTPTR_TYPE__>(0);
2576 return __formatter_int<_CharT>(_M_spec).format(__u, __fc);
2580 [[__gnu__::__always_inline__]]
2584 if (_M_spec._M_type == _Pres_none)
2586 _M_spec._M_type = _Pres_x;
2587 _M_spec._M_alt =
true;
2591 __format::_Spec<_CharT> _M_spec;
2598 template<__format::__
char _CharT>
2599 struct formatter<_CharT, _CharT>
2601 formatter() =
default;
2603 constexpr typename basic_format_parse_context<_CharT>::iterator
2604 parse(basic_format_parse_context<_CharT>& __pc)
2606 return _M_f.template _M_parse<_CharT>(__pc);
2609 template<
typename _Out>
2610 _GLIBCXX_CONSTEXPR_FORMAT
2611 typename basic_format_context<_Out, _CharT>::iterator
2612 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2614 if (_M_f._M_spec._M_type == __format::_Pres_c)
2615 return _M_f._M_format_character(__u, __fc);
2617 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2620#if __glibcxx_format_ranges
2622 set_debug_format() noexcept
2623 { _M_f._M_spec._M_debug =
true; }
2627 __format::__formatter_int<_CharT> _M_f;
2630#if __glibcxx_print >= 202403L
2631 template<__format::__
char _CharT>
2632 constexpr bool enable_nonlocking_formatter_optimization<_CharT> =
true;
2635#ifdef _GLIBCXX_USE_WCHAR_T
2638 struct formatter<char, wchar_t>
2640 formatter() =
default;
2642 constexpr typename basic_format_parse_context<wchar_t>::iterator
2643 parse(basic_format_parse_context<wchar_t>& __pc)
2645 return _M_f._M_parse<
char>(__pc);
2648 template<
typename _Out>
2649 _GLIBCXX_CONSTEXPR_FORMAT
2650 typename basic_format_context<_Out, wchar_t>::iterator
2651 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2653 if (_M_f._M_spec._M_type == __format::_Pres_c)
2654 return _M_f._M_format_character(__u, __fc);
2656 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2659#if __glibcxx_format_ranges
2661 set_debug_format() noexcept
2662 { _M_f._M_spec._M_debug =
true; }
2666 __format::__formatter_int<wchar_t> _M_f;
2673 template<__format::__
char _CharT>
2674 struct formatter<_CharT*, _CharT>
2676 formatter() =
default;
2678 [[__gnu__::__always_inline__]]
2679 constexpr typename basic_format_parse_context<_CharT>::iterator
2680 parse(basic_format_parse_context<_CharT>& __pc)
2681 {
return _M_f.parse(__pc); }
2683 template<
typename _Out>
2684 [[__gnu__::__nonnull__]]
2685 _GLIBCXX_CONSTEXPR_FORMAT
2686 typename basic_format_context<_Out, _CharT>::iterator
2687 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2688 {
return _M_f.format(__u, __fc); }
2690#if __glibcxx_format_ranges
2691 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2695 __format::__formatter_str<_CharT> _M_f;
2698#if __glibcxx_print >= 202403L
2699 template<__format::__
char _CharT>
2700 constexpr bool enable_nonlocking_formatter_optimization<_CharT*> =
true;
2703 template<__format::__
char _CharT>
2704 struct formatter<const _CharT*, _CharT>
2706 formatter() =
default;
2708 [[__gnu__::__always_inline__]]
2709 constexpr typename basic_format_parse_context<_CharT>::iterator
2710 parse(basic_format_parse_context<_CharT>& __pc)
2711 {
return _M_f.parse(__pc); }
2713 template<
typename _Out>
2714 [[__gnu__::__nonnull__]]
2715 _GLIBCXX_CONSTEXPR_FORMAT
2716 typename basic_format_context<_Out, _CharT>::iterator
2717 format(
const _CharT* __u,
2718 basic_format_context<_Out, _CharT>& __fc)
const
2719 {
return _M_f.format(__u, __fc); }
2721#if __glibcxx_format_ranges
2722 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2726 __format::__formatter_str<_CharT> _M_f;
2729#if __glibcxx_print >= 202403L
2730 template<__format::__
char _CharT>
2732 enable_nonlocking_formatter_optimization<const _CharT*> =
true;
2735 template<__format::__
char _CharT,
size_t _Nm>
2736 struct formatter<_CharT[_Nm], _CharT>
2738 formatter() =
default;
2740 [[__gnu__::__always_inline__]]
2741 constexpr typename basic_format_parse_context<_CharT>::iterator
2742 parse(basic_format_parse_context<_CharT>& __pc)
2743 {
return _M_f.parse(__pc); }
2745 template<
typename _Out>
2746 _GLIBCXX_CONSTEXPR_FORMAT
2747 typename basic_format_context<_Out, _CharT>::iterator
2748 format(
const _CharT (&__u)[_Nm],
2749 basic_format_context<_Out, _CharT>& __fc)
const
2750 {
return _M_f.format({__u, _Nm}, __fc); }
2752#if __glibcxx_format_ranges
2753 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2757 __format::__formatter_str<_CharT> _M_f;
2760#if __glibcxx_print >= 202403L
2761 template<__format::__
char _CharT,
size_t _Nm>
2762 constexpr bool enable_nonlocking_formatter_optimization<_CharT[_Nm]> =
true;
2765 template<
typename _Traits,
typename _Alloc>
2766 struct formatter<
basic_string<char, _Traits, _Alloc>, char>
2768 formatter() =
default;
2770 [[__gnu__::__always_inline__]]
2771 constexpr typename basic_format_parse_context<char>::iterator
2772 parse(basic_format_parse_context<char>& __pc)
2773 {
return _M_f.parse(__pc); }
2775 template<
typename _Out>
2776 _GLIBCXX_CONSTEXPR_FORMAT
2777 typename basic_format_context<_Out, char>::iterator
2778 format(
const basic_string<char, _Traits, _Alloc>& __u,
2779 basic_format_context<_Out, char>& __fc)
const
2780 {
return _M_f.format(__u, __fc); }
2782#if __glibcxx_format_ranges
2783 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2787 __format::__formatter_str<char> _M_f;
2790#if __glibcxx_print >= 202403L
2791 template<
typename _Tr,
typename _Alloc>
2793 enable_nonlocking_formatter_optimization<basic_string<char, _Tr, _Alloc>>
2797#ifdef _GLIBCXX_USE_WCHAR_T
2798 template<
typename _Traits,
typename _Alloc>
2799 struct formatter<
basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2801 formatter() =
default;
2803 [[__gnu__::__always_inline__]]
2804 constexpr typename basic_format_parse_context<wchar_t>::iterator
2805 parse(basic_format_parse_context<wchar_t>& __pc)
2806 {
return _M_f.parse(__pc); }
2808 template<
typename _Out>
2809 _GLIBCXX_CONSTEXPR_FORMAT
2810 typename basic_format_context<_Out, wchar_t>::iterator
2811 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2812 basic_format_context<_Out, wchar_t>& __fc)
const
2813 {
return _M_f.format(__u, __fc); }
2815#if __glibcxx_format_ranges
2816 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2820 __format::__formatter_str<wchar_t> _M_f;
2823#if __glibcxx_print >= 202403L
2824 template<
typename _Tr,
typename _Alloc>
2826 enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Tr, _Alloc>>
2832 template<
typename _Traits>
2835 formatter() =
default;
2837 [[__gnu__::__always_inline__]]
2838 constexpr typename basic_format_parse_context<char>::iterator
2839 parse(basic_format_parse_context<char>& __pc)
2840 {
return _M_f.parse(__pc); }
2842 template<
typename _Out>
2843 _GLIBCXX_CONSTEXPR_FORMAT
2844 typename basic_format_context<_Out, char>::iterator
2845 format(basic_string_view<char, _Traits> __u,
2846 basic_format_context<_Out, char>& __fc)
const
2847 {
return _M_f.format(__u, __fc); }
2849#if __glibcxx_format_ranges
2850 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2854 __format::__formatter_str<char> _M_f;
2857#if __glibcxx_print >= 202403L
2858 template<
typename _Tr>
2860 enable_nonlocking_formatter_optimization<basic_string_view<char, _Tr>>
2864#ifdef _GLIBCXX_USE_WCHAR_T
2865 template<
typename _Traits>
2868 formatter() =
default;
2870 [[__gnu__::__always_inline__]]
2871 constexpr typename basic_format_parse_context<wchar_t>::iterator
2872 parse(basic_format_parse_context<wchar_t>& __pc)
2873 {
return _M_f.parse(__pc); }
2875 template<
typename _Out>
2876 _GLIBCXX_CONSTEXPR_FORMAT
2877 typename basic_format_context<_Out, wchar_t>::iterator
2878 format(basic_string_view<wchar_t, _Traits> __u,
2879 basic_format_context<_Out, wchar_t>& __fc)
const
2880 {
return _M_f.format(__u, __fc); }
2882#if __glibcxx_format_ranges
2883 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2887 __format::__formatter_str<wchar_t> _M_f;
2890#if __glibcxx_print >= 202403L
2891 template<
typename _Tr>
2893 enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Tr>>
2904 template<
typename _Tp>
2905 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2907#if defined __SIZEOF_INT128__
2908 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2909 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2913 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2914 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2915#ifdef _GLIBCXX_USE_CHAR8_T
2916 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2918 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2919 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2921 template<
typename _Tp>
2922 concept __formattable_integer = __is_formattable_integer<_Tp>;
2927 template<__format::__formattable_
integer _Tp, __format::__
char _CharT>
2928 struct formatter<_Tp, _CharT>
2930 formatter() =
default;
2932 [[__gnu__::__always_inline__]]
2933 constexpr typename basic_format_parse_context<_CharT>::iterator
2934 parse(basic_format_parse_context<_CharT>& __pc)
2936 return _M_f.template _M_parse<_Tp>(__pc);
2939 template<
typename _Out>
2940 _GLIBCXX_CONSTEXPR_FORMAT
2941 typename basic_format_context<_Out, _CharT>::iterator
2942 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2943 {
return _M_f.format(__u, __fc); }
2946 __format::__formatter_int<_CharT> _M_f;
2949#if __glibcxx_print >= 202403L
2950 template<__format::__formattable_
integer _Tp>
2952 enable_nonlocking_formatter_optimization<_Tp> =
true;
2955#if defined __glibcxx_to_chars
2957 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2958 struct formatter<_Tp, _CharT>
2960 formatter() =
default;
2962 [[__gnu__::__always_inline__]]
2963 constexpr typename basic_format_parse_context<_CharT>::iterator
2964 parse(basic_format_parse_context<_CharT>& __pc)
2965 {
return _M_f.parse(__pc); }
2967 template<
typename _Out>
2968 typename basic_format_context<_Out, _CharT>::iterator
2969 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2970 {
return _M_f.format(__u, __fc); }
2973 __format::__formatter_fp<_CharT> _M_f;
2976#if __glibcxx_print >= 202403L
2977 template<__format::__formattable_
float _Tp>
2979 enable_nonlocking_formatter_optimization<_Tp> =
true;
2982#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2984 template<__format::__
char _CharT>
2985 struct formatter<long double, _CharT>
2987 formatter() =
default;
2989 [[__gnu__::__always_inline__]]
2990 constexpr typename basic_format_parse_context<_CharT>::iterator
2991 parse(basic_format_parse_context<_CharT>& __pc)
2992 {
return _M_f.parse(__pc); }
2994 template<
typename _Out>
2995 typename basic_format_context<_Out, _CharT>::iterator
2996 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2997 {
return _M_f.format((
double)__u, __fc); }
3000 __format::__formatter_fp<_CharT> _M_f;
3004#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3006 template<__format::__
char _CharT>
3007 struct formatter<_Float16, _CharT>
3009 formatter() =
default;
3011 [[__gnu__::__always_inline__]]
3012 constexpr typename basic_format_parse_context<_CharT>::iterator
3013 parse(basic_format_parse_context<_CharT>& __pc)
3014 {
return _M_f.parse(__pc); }
3016 template<
typename _Out>
3017 typename basic_format_context<_Out, _CharT>::iterator
3018 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
3019 {
return _M_f.format((
float)__u, __fc); }
3022 __format::__formatter_fp<_CharT> _M_f;
3026#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3028 template<__format::__
char _CharT>
3029 struct formatter<_Float32, _CharT>
3031 formatter() =
default;
3033 [[__gnu__::__always_inline__]]
3034 constexpr typename basic_format_parse_context<_CharT>::iterator
3035 parse(basic_format_parse_context<_CharT>& __pc)
3036 {
return _M_f.parse(__pc); }
3038 template<
typename _Out>
3039 typename basic_format_context<_Out, _CharT>::iterator
3040 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
3041 {
return _M_f.format((
float)__u, __fc); }
3044 __format::__formatter_fp<_CharT> _M_f;
3048#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3050 template<__format::__
char _CharT>
3051 struct formatter<_Float64, _CharT>
3053 formatter() =
default;
3055 [[__gnu__::__always_inline__]]
3056 constexpr typename basic_format_parse_context<_CharT>::iterator
3057 parse(basic_format_parse_context<_CharT>& __pc)
3058 {
return _M_f.parse(__pc); }
3060 template<
typename _Out>
3061 typename basic_format_context<_Out, _CharT>::iterator
3062 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
3063 {
return _M_f.format((
double)__u, __fc); }
3066 __format::__formatter_fp<_CharT> _M_f;
3070#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128
3072 template<__format::__
char _CharT>
3073 struct formatter<_Float128, _CharT>
3075 formatter() =
default;
3077 [[__gnu__::__always_inline__]]
3078 constexpr typename basic_format_parse_context<_CharT>::iterator
3079 parse(basic_format_parse_context<_CharT>& __pc)
3080 {
return _M_f.parse(__pc); }
3082 template<
typename _Out>
3083 typename basic_format_context<_Out, _CharT>::iterator
3084 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3085 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3088 __format::__formatter_fp<_CharT> _M_f;
3092#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128 == 2
3095 template<__format::__
char _CharT>
3096 struct formatter<__float128, _CharT>
3098 formatter() =
default;
3100 [[__gnu__::__always_inline__]]
3101 constexpr typename basic_format_parse_context<_CharT>::iterator
3102 parse(basic_format_parse_context<_CharT>& __pc)
3103 {
return _M_f.parse(__pc); }
3105 template<
typename _Out>
3106 typename basic_format_context<_Out, _CharT>::iterator
3107 format(__float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3108 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3111 __format::__formatter_fp<_CharT> _M_f;
3115#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3117 template<__format::__
char _CharT>
3118 struct formatter<__format::__bflt16_t, _CharT>
3120 formatter() =
default;
3122 [[__gnu__::__always_inline__]]
3123 constexpr typename basic_format_parse_context<_CharT>::iterator
3124 parse(basic_format_parse_context<_CharT>& __pc)
3125 {
return _M_f.parse(__pc); }
3127 template<
typename _Out>
3128 typename basic_format_context<_Out, _CharT>::iterator
3129 format(__gnu_cxx::__bfloat16_t __u,
3130 basic_format_context<_Out, _CharT>& __fc)
const
3131 {
return _M_f.format((
float)__u, __fc); }
3134 __format::__formatter_fp<_CharT> _M_f;
3142 template<__format::__
char _CharT>
3143 struct formatter<const void*, _CharT>
3145 formatter() =
default;
3147 constexpr typename basic_format_parse_context<_CharT>::iterator
3148 parse(basic_format_parse_context<_CharT>& __pc)
3149 {
return _M_f.parse(__pc); }
3151 template<
typename _Out>
3152 _GLIBCXX_CONSTEXPR_FORMAT
3153 typename basic_format_context<_Out, _CharT>::iterator
3154 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3155 {
return _M_f.format(__v, __fc); }
3158 __format::__formatter_ptr<_CharT> _M_f;
3161#if __glibcxx_print >= 202403L
3163 inline constexpr bool
3164 enable_nonlocking_formatter_optimization<const void*> =
true;
3167 template<__format::__
char _CharT>
3168 struct formatter<void*, _CharT>
3170 formatter() =
default;
3172 [[__gnu__::__always_inline__]]
3173 constexpr typename basic_format_parse_context<_CharT>::iterator
3174 parse(basic_format_parse_context<_CharT>& __pc)
3175 {
return _M_f.parse(__pc); }
3177 template<
typename _Out>
3178 _GLIBCXX_CONSTEXPR_FORMAT
3179 typename basic_format_context<_Out, _CharT>::iterator
3180 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3181 {
return _M_f.format(__v, __fc); }
3184 __format::__formatter_ptr<_CharT> _M_f;
3187#if __glibcxx_print >= 202403l
3189 inline constexpr bool
3190 enable_nonlocking_formatter_optimization<void*> =
true;
3193 template<__format::__
char _CharT>
3194 struct formatter<nullptr_t, _CharT>
3196 formatter() =
default;
3198 [[__gnu__::__always_inline__]]
3199 constexpr typename basic_format_parse_context<_CharT>::iterator
3200 parse(basic_format_parse_context<_CharT>& __pc)
3201 {
return _M_f.parse(__pc); }
3203 template<
typename _Out>
3204 _GLIBCXX_CONSTEXPR_FORMAT
3205 typename basic_format_context<_Out, _CharT>::iterator
3206 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3207 {
return _M_f.format(
nullptr, __fc); }
3210 __format::__formatter_ptr<_CharT> _M_f;
3214#if __glibcxx_print >= 202403L
3216 inline constexpr bool
3217 enable_nonlocking_formatter_optimization<nullptr_t> =
true;
3220#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3224 struct __formatter_disabled
3226 __formatter_disabled() =
delete;
3227 __formatter_disabled(
const __formatter_disabled&) =
delete;
3228 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3232 struct formatter<char*,
wchar_t>
3233 :
private __formatter_disabled { };
3235 struct formatter<const char*,
wchar_t>
3236 :
private __formatter_disabled { };
3237 template<
size_t _Nm>
3238 struct formatter<char[_Nm], wchar_t>
3239 :
private __formatter_disabled { };
3240 template<
class _Traits,
class _Allocator>
3241 struct formatter<
basic_string<char, _Traits, _Allocator>, wchar_t>
3242 :
private __formatter_disabled { };
3243 template<
class _Traits>
3245 :
private __formatter_disabled { };
3250 template<
typename _Out>
3251 struct format_to_n_result
3254 iter_difference_t<_Out> size;
3257_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3258template<
typename,
typename>
class vector;
3259_GLIBCXX_END_NAMESPACE_CONTAINER
3264 template<
typename _CharT>
3268 using iterator_category = output_iterator_tag;
3269 using value_type = void;
3270 using difference_type = ptrdiff_t;
3271 using pointer = void;
3272 using reference = void;
3274 _Drop_iter() =
default;
3275 _Drop_iter(
const _Drop_iter&) =
default;
3276 _Drop_iter& operator=(
const _Drop_iter&) =
default;
3278 [[__gnu__::__always_inline__]]
3279 constexpr _Drop_iter&
3280 operator=(_CharT __c)
3283 [[__gnu__::__always_inline__]]
3284 constexpr _Drop_iter&
3285 operator=(basic_string_view<_CharT> __s)
3288 [[__gnu__::__always_inline__]]
3289 constexpr _Drop_iter&
3292 [[__gnu__::__always_inline__]]
3293 constexpr _Drop_iter&
3294 operator++() {
return *
this; }
3296 [[__gnu__::__always_inline__]]
3297 constexpr _Drop_iter
3298 operator++(
int) {
return *
this; }
3301 template<
typename _CharT>
3304 _Sink<_CharT>* _M_sink =
nullptr;
3307 using iterator_category = output_iterator_tag;
3308 using value_type = void;
3309 using difference_type = ptrdiff_t;
3310 using pointer = void;
3311 using reference = void;
3313 _Sink_iter() =
default;
3314 _Sink_iter(
const _Sink_iter&) =
default;
3315 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3317 [[__gnu__::__always_inline__]]
3319 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3321 [[__gnu__::__always_inline__]]
3322 constexpr _Sink_iter&
3323 operator=(_CharT __c)
3325 _M_sink->_M_write(__c);
3329 [[__gnu__::__always_inline__]]
3330 constexpr _Sink_iter&
3331 operator=(basic_string_view<_CharT> __s)
3333 _M_sink->_M_write(__s);
3337 [[__gnu__::__always_inline__]]
3338 constexpr _Sink_iter&
3341 [[__gnu__::__always_inline__]]
3342 constexpr _Sink_iter&
3343 operator++() {
return *
this; }
3345 [[__gnu__::__always_inline__]]
3346 constexpr _Sink_iter
3347 operator++(
int) {
return *
this; }
3349 _GLIBCXX_CONSTEXPR_FORMAT
auto
3350 _M_reserve(
size_t __n)
const
3351 {
return _M_sink->_M_reserve(__n); }
3353 _GLIBCXX_CONSTEXPR_FORMAT
bool
3354 _M_discarding()
const
3355 {
return _M_sink->_M_discarding(); }
3361 template<
typename _CharT>
3364 friend class _Sink_iter<_CharT>;
3366 span<_CharT> _M_span;
3367 typename span<_CharT>::iterator _M_next;
3373 virtual void _M_overflow() = 0;
3377 [[__gnu__::__always_inline__]]
3379 _Sink(span<_CharT> __span) noexcept
3380 : _M_span(__span), _M_next(__span.begin())
3384 [[__gnu__::__always_inline__]]
3385 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3386 _M_used() const noexcept
3387 {
return _M_span.first(_M_next - _M_span.begin()); }
3390 [[__gnu__::__always_inline__]]
3391 constexpr span<_CharT>
3392 _M_unused() const noexcept
3393 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3396 [[__gnu__::__always_inline__]]
3398 _M_rewind() noexcept
3399 { _M_next = _M_span.begin(); }
3402 _GLIBCXX_CONSTEXPR_FORMAT
void
3403 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3406 _M_next = __s.begin() + __pos;
3411 _M_write(_CharT __c)
3414 if (_M_next - _M_span.begin() ==
std::ssize(_M_span)) [[unlikely]]
3419 _M_write(basic_string_view<_CharT> __s)
3421 span __to = _M_unused();
3422 while (__to.size() <= __s.size())
3424 __s.copy(__to.data(), __to.size());
3425 _M_next += __to.size();
3426 __s.remove_prefix(__to.size());
3432 __s.copy(__to.data(), __s.size());
3433 _M_next += __s.size();
3442 _GLIBCXX_CONSTEXPR_FORMAT
3443 explicit operator bool() const noexcept {
return _M_sink; }
3445 _GLIBCXX_CONSTEXPR_FORMAT _CharT*
3446 get() const noexcept {
return _M_sink->_M_next.operator->(); }
3448 _GLIBCXX_CONSTEXPR_FORMAT
void
3449 _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3457 _GLIBCXX_CONSTEXPR_FORMAT
virtual _Reservation
3458 _M_reserve(
size_t __n)
3460 if (__n <= _M_unused().size())
3463 if (__n <= _M_span.size())
3466 if (__n <= _M_unused().size())
3474 _GLIBCXX_CONSTEXPR_FORMAT
virtual void
3479 _GLIBCXX_CONSTEXPR_FORMAT
virtual bool
3480 _M_discarding()
const
3484 _Sink(
const _Sink&) =
delete;
3485 _Sink& operator=(
const _Sink&) =
delete;
3487 [[__gnu__::__always_inline__]]
3488 constexpr _Sink_iter<_CharT>
3490 {
return _Sink_iter<_CharT>(*
this); }
3494 template<
typename _CharT>
3495 class _Fixedbuf_sink final :
public _Sink<_CharT>
3497 _GLIBCXX_CONSTEXPR_FORMAT
void
3498 _M_overflow()
override
3500 __glibcxx_assert(
false);
3505 [[__gnu__::__always_inline__]]
3507 _Fixedbuf_sink(span<_CharT> __buf)
3508 : _Sink<_CharT>(__buf)
3511 constexpr basic_string_view<_CharT>
3514 auto __s = this->_M_used();
3515 return basic_string_view<_CharT>(__s.data(), __s.size());
3520 template<
typename _CharT>
3521 class _Buf_sink :
public _Sink<_CharT>
3524 _CharT _M_buf[__stackbuf_size<_CharT>];
3526 [[__gnu__::__always_inline__]]
3528 _Buf_sink() noexcept
3529 : _Sink<_CharT>(_M_buf)
3533 using _GLIBCXX_STD_C::vector;
3537 template<
typename _Seq>
3538 class _Seq_sink :
public _Buf_sink<typename _Seq::value_type>
3540 using _CharT =
typename _Seq::value_type;
3545 _GLIBCXX_CONSTEXPR_FORMAT
void
3546 _M_overflow()
override
3548 auto __s = this->_M_used();
3549 if (__s.empty()) [[unlikely]]
3553 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3555 if constexpr (__is_specialization_of<_Seq, basic_string>)
3556 _M_seq.append(__s.data(), __s.size());
3558 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3564 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3565 _M_reserve(
size_t __n)
override
3574 if constexpr (__is_specialization_of<_Seq, basic_string>
3575 || __is_specialization_of<_Seq, vector>)
3578 if (this->_M_used().size()) [[unlikely]]
3579 _Seq_sink::_M_overflow();
3582 const auto __sz = _M_seq.size();
3583 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3584 _M_seq.__resize_and_overwrite(__sz + __n,
3585 [](
auto,
auto __n2) {
3589 _M_seq.resize(__sz + __n);
3593 this->_M_reset(_M_seq, __sz);
3597 return _Sink<_CharT>::_M_reserve(__n);
3600 _GLIBCXX_CONSTEXPR_FORMAT
void
3601 _M_bump(
size_t __n)
override
3603 if constexpr (__is_specialization_of<_Seq, basic_string>
3604 || __is_specialization_of<_Seq, vector>)
3606 auto __s = this->_M_used();
3607 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3609 _M_seq.resize(__s.size() + __n);
3611 this->_M_reset(this->_M_buf);
3615 _GLIBCXX_CONSTEXPR_FORMAT
void
3616 _M_trim(span<const _CharT> __s)
3617 requires __is_specialization_of<_Seq, basic_string>
3619 _GLIBCXX_DEBUG_ASSERT(__s.data() == this->_M_buf
3620 || __s.data() == _M_seq.data());
3621 if (__s.data() == _M_seq.data())
3622 _M_seq.resize(__s.size());
3624 this->_M_reset(this->_M_buf, __s.size());
3631 [[__gnu__::__always_inline__]]
3632 _GLIBCXX_CONSTEXPR_FORMAT
3633 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3636 _GLIBCXX_CONSTEXPR_FORMAT
3637 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3638 : _M_seq(std::move(__s))
3641 using _Sink<_CharT>::out;
3643 _GLIBCXX_CONSTEXPR_FORMAT _Seq
3646 if (this->_M_used().size() != 0)
3647 _Seq_sink::_M_overflow();
3653 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3656 auto __s = this->_M_used();
3659 if (__s.size() != 0)
3660 _Seq_sink::_M_overflow();
3666 _GLIBCXX_CONSTEXPR_FORMAT basic_string_view<_CharT>
3669 auto __span = _M_span();
3670 return basic_string_view<_CharT>(__span.data(), __span.size());
3674 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3676 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3682 template<
typename _CharT,
typename _OutIter>
3683 class _Iter_sink :
public _Buf_sink<_CharT>
3686 iter_difference_t<_OutIter> _M_max;
3689 size_t _M_count = 0;
3691 _GLIBCXX_CONSTEXPR_FORMAT
void
3692 _M_overflow()
override
3694 auto __s = this->_M_used();
3696 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3697 else if (_M_count <
static_cast<size_t>(_M_max))
3699 auto __max = _M_max - _M_count;
3700 span<_CharT> __first;
3701 if (__max < __s.size())
3702 __first = __s.first(
static_cast<size_t>(__max));
3705 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3708 _M_count += __s.size();
3711 _GLIBCXX_CONSTEXPR_FORMAT
bool
3712 _M_discarding()
const override
3720 [[__gnu__::__always_inline__]]
3721 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3722 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3723 : _M_out(std::move(__out)), _M_max(__max)
3726 using _Sink<_CharT>::out;
3728 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3731 if (this->_M_used().size() != 0)
3732 _Iter_sink::_M_overflow();
3733 iter_difference_t<_OutIter> __count(_M_count);
3745 template<
typename _CharT>
3746 class _Ptr_sink :
public _Sink<_CharT>
3748 static constexpr size_t _S_no_limit = size_t(-1);
3752 size_t _M_count = 0;
3757 _GLIBCXX_CONSTEXPR_FORMAT
void
3758 _M_overflow()
override
3760 if (this->_M_unused().size() != 0)
3763 auto __s = this->_M_used();
3765 if (_M_max != _S_no_limit)
3767 _M_count += __s.size();
3771 this->_M_reset(this->_M_buf);
3777 _M_rebuf(__s.data(), __s.size() + 1024, __s.size());
3781 _GLIBCXX_CONSTEXPR_FORMAT
bool
3782 _M_discarding()
const override
3789 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3790 _M_reserve(
size_t __n)
final
3792 auto __avail = this->_M_unused();
3793 if (__n > __avail.size())
3795 if (_M_max != _S_no_limit)
3798 auto __s = this->_M_used();
3799 _M_rebuf(__s.data(), __s.size() + __n, __s.size());
3805 template<
typename _IterDifference>
3806 static _GLIBCXX_CONSTEXPR_FORMAT
size_t
3807 _S_trim_max(_IterDifference __max)
3811 if constexpr (!is_integral_v<_IterDifference> ||
sizeof(__max) >
sizeof(size_t))
3813 if (_IterDifference((
size_t)-1) < __max)
3815 return size_t(__max);
3818 [[__gnu__::__always_inline__]]
3819 _GLIBCXX_CONSTEXPR_FORMAT
void
3820 _M_rebuf(_CharT* __ptr,
size_t __total,
size_t __inuse = 0)
3822 std::span<_CharT> __span(__ptr, __total);
3823 this->_M_reset(__span, __inuse);
3827 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3828 _Ptr_sink(_CharT* __ptr,
size_t __n = _S_no_limit) noexcept
3829 : _Sink<_CharT>(_M_buf), _M_max(__n)
3833 else if (__n != _S_no_limit)
3834 _M_rebuf(__ptr, __n);
3835#if __has_builtin(__builtin_dynamic_object_size)
3836 else if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3837 _M_rebuf(__ptr, __bytes /
sizeof(_CharT));
3842 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3843 __n = (1024 - __off) /
sizeof(_CharT);
3844 if (__n > 0) [[likely]]
3845 _M_rebuf(__ptr, __n);
3851 template<contiguous_iterator _OutIter>
3852 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3853 _Ptr_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1)
3854 : _Ptr_sink(std::
to_address(__out), _S_trim_max(__n))
3857 template<contiguous_iterator _OutIter>
3858 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3859 _M_finish(_OutIter __first)
const
3861 auto __s = this->_M_used();
3862 if (__s.data() == _M_buf)
3865 iter_difference_t<_OutIter> __m(_M_max);
3866 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3867 return { __first + __m, __count };
3871 iter_difference_t<_OutIter> __count(__s.size());
3872 return { __first + __count, __count };
3877 template<
typename _CharT,
typename _OutIter>
3878 concept __contiguous_char_iter
3879 = contiguous_iterator<_OutIter>
3880 && same_as<iter_value_t<_OutIter>, _CharT>;
3891 template<
typename _Out,
typename _CharT>
3892 class _Padding_sink :
public _Str_sink<_CharT>
3897 size_t _M_printwidth;
3899 [[__gnu__::__always_inline__]]
3900 _GLIBCXX_CONSTEXPR_FORMAT
bool
3902 {
return _M_printwidth >= _M_maxwidth; }
3904 [[__gnu__::__always_inline__]]
3905 _GLIBCXX_CONSTEXPR_FORMAT
bool
3906 _M_buffering()
const
3908 if (_M_printwidth < _M_padwidth)
3910 if (_M_maxwidth != (
size_t)-1)
3911 return _M_printwidth < _M_maxwidth;
3915 _GLIBCXX_CONSTEXPR_FORMAT
void
3916 _M_sync_discarding()
3918 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3919 if (_M_out._M_discarding())
3920 _M_maxwidth = _M_printwidth;
3923 _GLIBCXX_CONSTEXPR_FORMAT
void
3926 span<_CharT> __new = this->_M_used();
3927 basic_string_view<_CharT> __str(__new.data(), __new.size());
3928 _M_out = __format::__write(
std::move(_M_out), __str);
3929 _M_sync_discarding();
3933 _GLIBCXX_CONSTEXPR_FORMAT
bool
3936 auto __str = this->view();
3938 _M_printwidth = __format::__truncate(__str, _M_maxwidth);
3940 this->_M_trim(__str);
3946 if (_M_printwidth >= _M_padwidth)
3948 _M_out = __format::__write(
std::move(_M_out), __str);
3949 _M_sync_discarding();
3954 _Str_sink<_CharT>::_M_overflow();
3957 this->_M_reset(this->_M_buf);
3961 _GLIBCXX_CONSTEXPR_FORMAT
bool
3962 _M_update(
size_t __new)
3964 _M_printwidth += __new;
3966 if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
3967 return _M_force_update();
3971 _GLIBCXX_CONSTEXPR_FORMAT
void
3972 _M_overflow()
override
3978 else if (!_M_buffering())
3982 else if (_M_update(this->_M_used().size()))
3983 _Str_sink<_CharT>::_M_overflow();
3986 _GLIBCXX_CONSTEXPR_FORMAT
bool
3987 _M_discarding()
const override
3988 {
return _M_ignoring(); }
3990 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3991 _M_reserve(
size_t __n)
override
3996 else if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3997 if (!_M_buffering())
4000 if (!this->_M_used().empty())
4003 if (
auto __reserved = _M_out._M_reserve(__n))
4006 return _Sink<_CharT>::_M_reserve(__n);
4009 _GLIBCXX_CONSTEXPR_FORMAT
void
4010 _M_bump(
size_t __n)
override
4017 _Sink<_CharT>::_M_bump(__n);
4023 [[__gnu__::__always_inline__]]
4024 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4025 _Padding_sink(_Out __out,
size_t __padwidth,
size_t __maxwidth)
4026 : _M_padwidth(__padwidth), _M_maxwidth(__maxwidth),
4027 _M_out(std::move(__out)), _M_printwidth(0)
4028 { _M_sync_discarding(); }
4030 [[__gnu__::__always_inline__]]
4031 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4032 _Padding_sink(_Out __out,
size_t __padwidth)
4033 : _Padding_sink(std::move(__out), __padwidth, (size_t)-1)
4036 _GLIBCXX_CONSTEXPR_FORMAT _Out
4037 _M_finish(_Align __align,
char32_t __fill_char)
4040 if (
auto __rem = this->_M_used().size())
4044 else if (!_M_buffering())
4050 if (!_M_buffering() || !_M_force_update())
4052 if (_M_printwidth >= _M_padwidth)
4055 const auto __str = this->view();
4056 if (_M_printwidth >= _M_padwidth)
4057 return __format::__write(
std::move(_M_out), __str);
4059 const size_t __nfill = _M_padwidth - _M_printwidth;
4060 return __format::__write_padded(
std::move(_M_out), __str,
4061 __align, __nfill, __fill_char);
4065 template<
typename _Out,
typename _CharT>
4066 class _Escaping_sink :
public _Buf_sink<_CharT>
4068 using _Esc = _Escapes<_CharT>;
4071 _Term_char _M_term : 2;
4072 unsigned _M_prev_escape : 1;
4073 unsigned _M_out_discards : 1;
4075 _GLIBCXX_CONSTEXPR_FORMAT
void
4076 _M_sync_discarding()
4078 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4079 _M_out_discards = _M_out._M_discarding();
4082 _GLIBCXX_CONSTEXPR_FORMAT
void
4085 span<_CharT> __bytes = this->_M_used();
4086 basic_string_view<_CharT> __str(__bytes.data(), __bytes.size());
4089 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4091 bool __prev_escape = _M_prev_escape;
4092 _M_out = __format::__write_escaped_unicode_part(
4093 std::move(_M_out), __str, __prev_escape, _M_term);
4094 _M_prev_escape = __prev_escape;
4096 __rem = __str.size();
4097 if (__rem > 0 && __str.data() != this->_M_buf) [[unlikely]]
4098 ranges::move(__str, this->_M_buf);
4101 _M_out = __format::__write_escaped_ascii(
4104 this->_M_reset(this->_M_buf, __rem);
4105 _M_sync_discarding();
4108 _GLIBCXX_CONSTEXPR_FORMAT
void
4109 _M_overflow()
override
4111 if (_M_out_discards)
4117 _GLIBCXX_CONSTEXPR_FORMAT
bool
4118 _M_discarding()
const override
4119 {
return _M_out_discards; }
4122 [[__gnu__::__always_inline__]]
4123 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4124 _Escaping_sink(_Out __out, _Term_char __term)
4125 : _M_out(std::move(__out)), _M_term(__term),
4126 _M_prev_escape(true), _M_out_discards(false)
4128 _M_out = __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4129 _M_sync_discarding();
4132 _GLIBCXX_CONSTEXPR_FORMAT _Out
4135 if (_M_out_discards)
4138 if (!this->_M_used().empty())
4141 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4142 if (
auto __rem = this->_M_used(); !__rem.empty())
4144 basic_string_view<_CharT> __str(__rem.data(), __rem.size());
4145 _M_out = __format::__write_escape_seqs(
std::move(_M_out), __str);
4148 return __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4152 enum class _Arg_t :
unsigned char {
4153 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
4154 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
4155 _Arg_i128, _Arg_u128, _Arg_float128,
4156 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
4159#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4160 _Arg_ibm128 = _Arg_ldbl,
4161 _Arg_ieee128 = _Arg_float128,
4166 template<
typename _Context>
4169 using _CharT =
typename _Context::char_type;
4173 using _CharT =
typename _Context::char_type;
4174 using _Func = void(*)(basic_format_parse_context<_CharT>&,
4175 _Context&,
const void*);
4178 template<
typename _Tp>
4179 using __maybe_const_t
4180 = __conditional_t<__formattable_with<const _Tp, _Context>,
4183 template<
typename _Tq>
4184 static _GLIBCXX_CONSTEXPR_FORMAT
void
4185 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
4186 _Context& __format_ctx,
const void* __ptr)
4188 using _Td = remove_const_t<_Tq>;
4189 typename _Context::template formatter_type<_Td> __f;
4190 __parse_ctx.advance_to(__f.parse(__parse_ctx));
4191 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
4192 __format_ctx.advance_to(__f.format(__val, __format_ctx));
4195 template<
typename _Tp>
4196 requires (!is_same_v<remove_cv_t<_Tp>, handle>)
4197 explicit _GLIBCXX_CONSTEXPR_FORMAT
4198 handle(_Tp& __val) noexcept
4199 : _M_ptr(__builtin_addressof(__val))
4200 , _M_func(&_S_format<__maybe_const_t<_Tp>>)
4203 friend class basic_format_arg<_Context>;
4206 handle(
const handle&) =
default;
4207 handle& operator=(
const handle&) =
default;
4209 [[__gnu__::__always_inline__]]
4210 void _GLIBCXX_CONSTEXPR_FORMAT
4211 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
4212 { _M_func(__pc, __fc, this->_M_ptr); }
4227 unsigned long long _M_ull;
4230#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4231 long double _M_ldbl;
4234 __ieee128 _M_ieee128;
4236#ifdef __SIZEOF_FLOAT128__
4237 __float128 _M_float128;
4239 const _CharT* _M_str;
4240 basic_string_view<_CharT> _M_sv;
4243#ifdef __SIZEOF_INT128__
4245 unsigned __int128 _M_u128;
4247#ifdef __BFLT16_DIG__
4261 [[__gnu__::__always_inline__]]
4262 _GLIBCXX_CONSTEXPR_FORMAT
4263 _Arg_value() : _M_none() { }
4266 template<
typename _Tp>
4267 _GLIBCXX_CONSTEXPR_FORMAT
4268 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
4269 { _S_get<_Tp>() = __val; }
4274 template<
typename _Tp,
typename _Self,
typename... _Value>
4275 [[__gnu__::__always_inline__]]
4276 static _GLIBCXX_CONSTEXPR_FORMAT
auto&
4277 _S_access(_Self& __u, _Value... __value)
noexcept
4279 static_assert(
sizeof...(_Value) <= 1);
4280 if constexpr (is_same_v<_Tp, bool>)
4281 return (__u._M_bool = ... = __value);
4282 else if constexpr (is_same_v<_Tp, _CharT>)
4283 return (__u._M_c = ... = __value);
4284 else if constexpr (is_same_v<_Tp, int>)
4285 return (__u._M_i = ... = __value);
4286 else if constexpr (is_same_v<_Tp, unsigned>)
4287 return (__u._M_u = ... = __value);
4288 else if constexpr (is_same_v<_Tp, long long>)
4289 return (__u._M_ll = ... = __value);
4290 else if constexpr (is_same_v<_Tp, unsigned long long>)
4291 return (__u._M_ull = ... = __value);
4292 else if constexpr (is_same_v<_Tp, float>)
4293 return (__u._M_flt = ... = __value);
4294 else if constexpr (is_same_v<_Tp, double>)
4295 return (__u._M_dbl = ... = __value);
4296#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4297 else if constexpr (is_same_v<_Tp, long double>)
4298 return (__u._M_ldbl = ... = __value);
4300 else if constexpr (is_same_v<_Tp, __ibm128>)
4301 return (__u._M_ibm128 = ... = __value);
4302 else if constexpr (is_same_v<_Tp, __ieee128>)
4303 return (__u._M_ieee128 = ... = __value);
4305#ifdef __SIZEOF_FLOAT128__
4306 else if constexpr (is_same_v<_Tp, __float128>)
4307 return (__u._M_float128 = ... = __value);
4309 else if constexpr (is_same_v<_Tp, const _CharT*>)
4310 return (__u._M_str = ... = __value);
4311 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4312 return (__u._M_sv = ... = __value);
4313 else if constexpr (is_same_v<_Tp, const void*>)
4314 return (__u._M_ptr = ... = __value);
4315#ifdef __SIZEOF_INT128__
4316 else if constexpr (is_same_v<_Tp, __int128>)
4317 return (__u._M_i128 = ... = __value);
4318 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4319 return (__u._M_u128 = ... = __value);
4321#ifdef __BFLT16_DIG__
4322 else if constexpr (is_same_v<_Tp, __bflt16_t>)
4323 return (__u._M_bf16 = ... = __value);
4326 else if constexpr (is_same_v<_Tp, _Float16>)
4327 return (__u._M_f16 = ... = __value);
4330 else if constexpr (is_same_v<_Tp, _Float32>)
4331 return (__u._M_f32 = ... = __value);
4334 else if constexpr (is_same_v<_Tp, _Float64>)
4335 return (__u._M_f64 = ... = __value);
4337 else if constexpr (is_same_v<_Tp, handle>)
4338 return __u._M_handle;
4340 __builtin_unreachable();
4343 template<
typename _Tp>
4344 [[__gnu__::__always_inline__]]
4345 _GLIBCXX_CONSTEXPR_FORMAT
auto&
4347 {
return _S_access<_Tp>(*
this); }
4349 template<
typename _Tp>
4350 [[__gnu__::__always_inline__]]
4351 _GLIBCXX_CONSTEXPR_FORMAT
const auto&
4352 _M_get() const noexcept
4353 {
return _S_access<_Tp>(*
this); }
4355 template<
typename _Tp>
4356 [[__gnu__::__always_inline__]]
4357 _GLIBCXX_CONSTEXPR_FORMAT
void
4358 _M_set(_Tp __v)
noexcept
4361 if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4362 std::construct_at(&_M_sv, __v);
4363 else if constexpr (is_same_v<_Tp, handle>)
4364 std::construct_at(&_M_handle, __v);
4368 _S_access<_Tp>(*
this, __v);
4373 template<
typename _Context,
typename... _Args>
4376 template<
typename _Visitor,
typename _Ctx>
4377 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4378 __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4380 template<
typename _Ch,
typename _Tp>
4382 __to_arg_t_enum() noexcept;
4386 template<typename _Context>
4387 class basic_format_arg
4389 using _CharT =
typename _Context::char_type;
4392 using handle = __format::_Arg_value<_Context>::handle;
4394 [[__gnu__::__always_inline__]]
4395 _GLIBCXX_CONSTEXPR_FORMAT
4396 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
4398 [[nodiscard,__gnu__::__always_inline__]]
4399 explicit _GLIBCXX_CONSTEXPR_FORMAT
operator bool() const noexcept
4400 {
return _M_type != __format::_Arg_none; }
4402#if __cpp_lib_format >= 202306L
4403 template<
typename _Visitor>
4404 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4405 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4408 template<
typename _Res,
typename _Visitor>
4409 _GLIBCXX_CONSTEXPR_FORMAT _Res
4410 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4415 template<
typename _Ctx>
4416 friend class basic_format_args;
4418 template<
typename _Ctx,
typename... _Args>
4419 friend class __format::_Arg_store;
4421 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
4423 __format::_Arg_value<_Context> _M_val;
4424 __format::_Arg_t _M_type;
4429 template<
typename _Tp>
4430 static consteval auto
4433 using _Td = remove_const_t<_Tp>;
4434 if constexpr (is_same_v<_Td, bool>)
4435 return type_identity<bool>();
4436 else if constexpr (is_same_v<_Td, _CharT>)
4437 return type_identity<_CharT>();
4438 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
4439 return type_identity<_CharT>();
4440#ifdef __SIZEOF_INT128__
4441 else if constexpr (is_same_v<_Td, __int128>)
4442 return type_identity<__int128>();
4443 else if constexpr (is_same_v<_Td, unsigned __int128>)
4444 return type_identity<unsigned __int128>();
4446 else if constexpr (__is_signed_integer<_Td>::value)
4448 if constexpr (
sizeof(_Td) <=
sizeof(int))
4449 return type_identity<int>();
4450 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
4451 return type_identity<long long>();
4453 else if constexpr (__is_unsigned_integer<_Td>::value)
4455 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
4456 return type_identity<unsigned>();
4457 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
4458 return type_identity<unsigned long long>();
4460 else if constexpr (is_same_v<_Td, float>)
4461 return type_identity<float>();
4462 else if constexpr (is_same_v<_Td, double>)
4463 return type_identity<double>();
4464#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4465 else if constexpr (is_same_v<_Td, long double>)
4466 return type_identity<long double>();
4468 else if constexpr (is_same_v<_Td, __ibm128>)
4469 return type_identity<__ibm128>();
4470 else if constexpr (is_same_v<_Td, __ieee128>)
4471 return type_identity<__ieee128>();
4473#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4474 else if constexpr (is_same_v<_Td, __float128>)
4475 return type_identity<__float128>();
4477#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4478 else if constexpr (is_same_v<_Td, __format::__bflt16_t>)
4479 return type_identity<__format::__bflt16_t>();
4481#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4482 else if constexpr (is_same_v<_Td, _Float16>)
4483 return type_identity<_Float16>();
4485#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4486 else if constexpr (is_same_v<_Td, _Float32>)
4487 return type_identity<_Float32>();
4489#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4490 else if constexpr (is_same_v<_Td, _Float64>)
4491 return type_identity<_Float64>();
4493 else if constexpr (__is_specialization_of<_Td, basic_string_view>
4494 || __is_specialization_of<_Td, basic_string>)
4496 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
4497 return type_identity<basic_string_view<_CharT>>();
4499 return type_identity<handle>();
4501 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
4502 return type_identity<const _CharT*>();
4503 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
4504 return type_identity<const _CharT*>();
4505 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
4506 return type_identity<const void*>();
4507 else if constexpr (is_same_v<_Td, nullptr_t>)
4508 return type_identity<const void*>();
4510 return type_identity<handle>();
4514 template<
typename _Tp>
4515 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
4518 template<
typename _Tp>
4519 static consteval __format::_Arg_t
4522 using namespace __format;
4523 if constexpr (is_same_v<_Tp, bool>)
4525 else if constexpr (is_same_v<_Tp, _CharT>)
4527 else if constexpr (is_same_v<_Tp, int>)
4529 else if constexpr (is_same_v<_Tp, unsigned>)
4531 else if constexpr (is_same_v<_Tp, long long>)
4533 else if constexpr (is_same_v<_Tp, unsigned long long>)
4535 else if constexpr (is_same_v<_Tp, float>)
4537 else if constexpr (is_same_v<_Tp, double>)
4539#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4540 else if constexpr (is_same_v<_Tp, long double>)
4544 else if constexpr (is_same_v<_Tp, __ibm128>)
4546 else if constexpr (is_same_v<_Tp, __ieee128>)
4547 return _Arg_ieee128;
4549#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4550 else if constexpr (is_same_v<_Tp, __float128>)
4551 return _Arg_float128;
4553#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4554 else if constexpr (is_same_v<_Tp, __format::__bflt16_t>)
4557#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4558 else if constexpr (is_same_v<_Tp, _Float16>)
4561#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4562 else if constexpr (is_same_v<_Tp, _Float32>)
4565#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4566 else if constexpr (is_same_v<_Tp, _Float64>)
4569 else if constexpr (is_same_v<_Tp, const _CharT*>)
4571 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4573 else if constexpr (is_same_v<_Tp, const void*>)
4575#ifdef __SIZEOF_INT128__
4576 else if constexpr (is_same_v<_Tp, __int128>)
4578 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4581 else if constexpr (is_same_v<_Tp, handle>)
4585 template<
typename _Tp>
4586 _GLIBCXX_CONSTEXPR_FORMAT
void
4587 _M_set(_Tp __v)
noexcept
4589 _M_type = _S_to_enum<_Tp>();
4593 template<
typename _Tp>
4594 requires __format::__formattable_with<_Tp, _Context>
4595 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4596 basic_format_arg(_Tp& __v)
noexcept
4598 using _Td = _Normalize<_Tp>;
4599 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4600 _M_set(_Td{__v.data(), __v.size()});
4601 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4602 && is_same_v<_CharT, wchar_t>)
4603 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4605 _M_set(
static_cast<_Td
>(__v));
4608 template<
typename _Ctx,
typename... _Argz>
4609 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4610 make_format_args(_Argz&...)
noexcept;
4612 template<
typename _Visitor,
typename _Ctx>
4613 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4614 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4616 template<
typename _Visitor,
typename _Ctx>
4617 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4618 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4620 template<
typename _Ch,
typename _Tp>
4621 friend consteval __format::_Arg_t
4622 __format::__to_arg_t_enum() noexcept;
4624 [[__gnu__::__noinline__]]
4626 _M_handle_unrecognized() const;
4628 template<typename _Visitor>
4629 _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4630 _M_visit(_Visitor&& __vis)
4634 using enum __format::_Arg_t;
4649#if __glibcxx_to_chars
4654#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4657#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4667#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4671#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4675#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4679#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4692#ifdef __SIZEOF_INT128__
4702 handle __h = _M_handle_unrecognized();
4707 template<
typename _Visitor>
4708 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4709 _M_visit_user(_Visitor&& __vis)
4711 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4713 constexpr bool __user_facing = __is_one_of<_Tp,
4714 monostate, bool, _CharT,
4715 int,
unsigned int,
long long int,
unsigned long long int,
4716 float, double,
long double,
4717 const _CharT*, basic_string_view<_CharT>,
4718 const void*, handle>::value;
4719 if constexpr (__user_facing)
4730 template<
typename _Visitor,
typename _Context>
4731 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4732 inline _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4733 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4739 template<
typename _Visitor,
typename _Ctx>
4740 inline _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4741 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4744 struct _WidthPrecVisitor
4746 template<
typename _Tp>
4747 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4748 operator()(_Tp& __arg)
const
4750 if constexpr (is_same_v<_Tp, monostate>)
4751 __format::__invalid_arg_id_in_format_string();
4755 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4759 if constexpr (__is_unsigned_integer<_Tp>::value)
4761 else if constexpr (__is_signed_integer<_Tp>::value)
4765 __throw_format_error(
"format error: argument used for width or "
4766 "precision must be a non-negative integer");
4770#pragma GCC diagnostic push
4771#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4772 template<
typename _Context>
4773 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
4774 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4775 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4778 template<
int _Bits,
size_t _Nm>
4780 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4782 __UINT64_TYPE__ __packed_types = 0;
4783 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4784 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4785 return __packed_types;
4790 template<
typename _Context>
4791 class basic_format_args
4793 static constexpr int _S_packed_type_bits = 5;
4794 static constexpr int _S_packed_type_mask = 0b11111;
4795 static constexpr int _S_max_packed_args = 12;
4797 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4799 template<
typename... _Args>
4800 using _Store = __format::_Arg_store<_Context, _Args...>;
4802 template<
typename _Ctx,
typename... _Args>
4803 friend class __format::_Arg_store;
4805 using uint64_t = __UINT64_TYPE__;
4806 using _Format_arg = basic_format_arg<_Context>;
4807 using _Format_arg_val = __format::_Arg_value<_Context>;
4813 uint64_t _M_packed_size : 4;
4814 uint64_t _M_unpacked_size : 60;
4817 const _Format_arg_val* _M_values;
4818 const _Format_arg* _M_args;
4821 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4822 _M_size() const noexcept
4823 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4825 _GLIBCXX_CONSTEXPR_FORMAT
typename __format::_Arg_t
4826 _M_type(
size_t __i)
const noexcept
4828 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4829 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4832 template<
typename _Ctx,
typename... _Args>
4833 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4834 make_format_args(_Args&...)
noexcept;
4837 template<
typename... _Args>
4838 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4840 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4843 template<
typename... _Args>
4844 _GLIBCXX_CONSTEXPR_FORMAT
4845 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4847 [[nodiscard,__gnu__::__always_inline__]]
4848 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<_Context>
4849 get(
size_t __i)
const noexcept
4851 basic_format_arg<_Context> __arg;
4852 if (__i < _M_packed_size)
4854 __arg._M_type = _M_type(__i);
4855 __arg._M_val = _M_values[__i];
4857 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4858 __arg = _M_args[__i];
4865 template<
typename _Context,
typename... _Args>
4866 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4867 -> basic_format_args<_Context>;
4869 template<
typename _Context,
typename... _Args>
4870 _GLIBCXX_CONSTEXPR_FORMAT
auto
4871 make_format_args(_Args&... __fmt_args)
noexcept;
4874 template<
typename _Context,
typename... _Args>
4875 class __format::_Arg_store
4877 friend std::basic_format_args<_Context>;
4879 template<
typename _Ctx,
typename... _Argz>
4880 friend _GLIBCXX_CONSTEXPR_FORMAT
auto std::
4881#if _GLIBCXX_INLINE_VERSION
4884 make_format_args(_Argz&...)
noexcept;
4888 static constexpr bool _S_values_only
4889 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4892 = __conditional_t<_S_values_only,
4893 __format::_Arg_value<_Context>,
4894 basic_format_arg<_Context>>;
4896 _Element_t _M_args[
sizeof...(_Args)];
4898 template<
typename _Tp>
4899 static _GLIBCXX_CONSTEXPR_FORMAT _Element_t
4900 _S_make_elt(_Tp& __v)
4902 using _Tq = remove_const_t<_Tp>;
4903 using _CharT =
typename _Context::char_type;
4904 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4905 "std::formatter must be specialized for the type "
4906 "of each format arg");
4907 using __format::__formattable_with;
4908 if constexpr (is_const_v<_Tp>)
4909 if constexpr (!__formattable_with<_Tp, _Context>)
4910 if constexpr (__formattable_with<_Tq, _Context>)
4911 static_assert(__formattable_with<_Tp, _Context>,
4912 "format arg must be non-const because its "
4913 "std::formatter specialization has a "
4914 "non-const reference parameter");
4915 basic_format_arg<_Context> __arg(__v);
4916 if constexpr (_S_values_only)
4917 return __arg._M_val;
4922 template<
typename... _Tp>
4923 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4924 [[__gnu__::__always_inline__]]
4925 _GLIBCXX_CONSTEXPR_FORMAT
4926 _Arg_store(_Tp&... __a) noexcept
4927 : _M_args{_S_make_elt(__a)...}
4931 template<
typename _Context>
4932 class __format::_Arg_store<_Context>
4935 template<
typename _Context>
4936 template<
typename... _Args>
4937 inline _GLIBCXX_CONSTEXPR_FORMAT
4938 basic_format_args<_Context>::
4939 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4941 if constexpr (
sizeof...(_Args) == 0)
4944 _M_unpacked_size = 0;
4947 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4950 _M_packed_size =
sizeof...(_Args);
4953 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4955 _M_values = __store._M_args;
4962 _M_unpacked_size =
sizeof...(_Args);
4964 _M_args = __store._M_args;
4969 template<
typename _Context = format_context,
typename... _Args>
4970 [[nodiscard,__gnu__::__always_inline__]]
4971 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4972 make_format_args(_Args&... __fmt_args)
noexcept
4974 using _Fmt_arg = basic_format_arg<_Context>;
4975 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4976 _Normalize<_Args>...>;
4977 return _Store(__fmt_args...);
4980#ifdef _GLIBCXX_USE_WCHAR_T
4982 template<
typename... _Args>
4983 [[nodiscard,__gnu__::__always_inline__]]
4984 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4985 make_wformat_args(_Args&... __args)
noexcept
4986 {
return std::make_format_args<wformat_context>(__args...); }
4992 template<
typename _Out,
typename _CharT,
typename _Context>
4993 _GLIBCXX_CONSTEXPR_FORMAT _Out
4994 __do_vformat_to(_Out, basic_string_view<_CharT>,
4995 const basic_format_args<_Context>&,
4996 const locale* =
nullptr);
4998 template<
typename _CharT>
struct __formatter_chrono;
5016 template<
typename _Out,
typename _CharT>
5017 class 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 struct _Parse_context : basic_format_parse_context<_CharT>
5111 using basic_format_parse_context<_CharT>::basic_format_parse_context;
5112 const _Arg_t* _M_types =
nullptr;
5116 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5117 : _M_pc(__str, __nargs)
5120 constexpr iterator begin() const noexcept {
return _M_pc.begin(); }
5121 constexpr iterator end() const noexcept {
return _M_pc.end(); }
5126 basic_string_view<_CharT> __fmt = _M_fmt_str();
5128 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5130 _M_pc.advance_to(begin() + 1);
5131 _M_format_arg(_M_pc.next_arg_id());
5135 size_t __lbr = __fmt.find(
'{');
5136 size_t __rbr = __fmt.find(
'}');
5138 while (__fmt.size())
5140 auto __cmp = __lbr <=> __rbr;
5144 _M_pc.advance_to(end());
5149 if (__lbr + 1 == __fmt.size()
5150 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5151 __format::__unmatched_left_brace_in_format_string();
5152 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5153 iterator __last = begin() + __lbr + int(__is_escape);
5154 _M_on_chars(__last);
5155 _M_pc.advance_to(__last + 1);
5156 __fmt = _M_fmt_str();
5159 if (__rbr != __fmt.npos)
5161 __lbr = __fmt.find(
'{');
5165 _M_on_replacement_field();
5166 __fmt = _M_fmt_str();
5167 __lbr = __fmt.find(
'{');
5168 __rbr = __fmt.find(
'}');
5173 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5174 __format::__unmatched_right_brace_in_format_string();
5175 iterator __last = begin() + __rbr;
5176 _M_on_chars(__last);
5177 _M_pc.advance_to(__last + 1);
5178 __fmt = _M_fmt_str();
5179 if (__lbr != __fmt.npos)
5181 __rbr = __fmt.find(
'}');
5186 constexpr basic_string_view<_CharT>
5187 _M_fmt_str() const noexcept
5188 {
return {begin(), end()}; }
5190 constexpr virtual void _M_on_chars(iterator) { }
5192 constexpr void _M_on_replacement_field()
5194 auto __next = begin();
5198 __id = _M_pc.next_arg_id();
5199 else if (*__next ==
':')
5201 __id = _M_pc.next_arg_id();
5202 _M_pc.advance_to(++__next);
5206 auto [__i, __ptr] = __format::__parse_arg_id(begin(), end());
5207 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5208 __format::__invalid_arg_id_in_format_string();
5209 _M_pc.check_arg_id(__id = __i);
5212 _M_pc.advance_to(++__ptr);
5215 _M_pc.advance_to(__ptr);
5217 _M_format_arg(__id);
5218 if (begin() == end() || *begin() !=
'}')
5219 __format::__unmatched_left_brace_in_format_string();
5220 _M_pc.advance_to(begin() + 1);
5223 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5227 template<
typename _Out,
typename _CharT>
5228 class _Formatting_scanner :
public _Scanner<_CharT>
5231 _GLIBCXX_CONSTEXPR_FORMAT
5232 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5233 basic_string_view<_CharT> __str)
5234 : _Scanner<_CharT>(__str), _M_fc(__fc)
5238 basic_format_context<_Out, _CharT>& _M_fc;
5240 using iterator =
typename _Scanner<_CharT>::iterator;
5243 _M_on_chars(iterator __last)
override
5245 basic_string_view<_CharT> __str(this->begin(), __last);
5246 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5250 _M_format_arg(
size_t __id)
override
5252 using _Context = basic_format_context<_Out, _CharT>;
5253 using handle =
typename basic_format_arg<_Context>::handle;
5255 __format::__visit_format_arg([
this](
auto& __arg) {
5257 using _Formatter =
typename _Context::template formatter_type<_Type>;
5258 if constexpr (is_same_v<_Type, monostate>)
5259 __format::__invalid_arg_id_in_format_string();
5260 else if constexpr (is_same_v<_Type, handle>)
5261 __arg.format(this->_M_pc, this->_M_fc);
5262 else if constexpr (is_default_constructible_v<_Formatter>)
5265 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5266 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5269 static_assert(__format::__formattable_with<_Type, _Context>);
5270 }, _M_fc.arg(__id));
5274 template<
typename _CharT,
typename _Tp>
5276 __to_arg_t_enum() noexcept
5278 using _Context = __format::__format_context<_CharT>;
5279 using _Fmt_arg = basic_format_arg<_Context>;
5280 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5281 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5285 template<
typename _CharT,
typename... _Args>
5286 class _Checking_scanner :
public _Scanner<_CharT>
5289 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5290 "std::formatter must be specialized for each type being formatted");
5294 _Checking_scanner(basic_string_view<_CharT> __str)
5295 : _Scanner<_CharT>(__str, sizeof...(_Args))
5297#if __cpp_lib_format >= 202305L
5298 this->_M_pc._M_types = _M_types.data();
5304 _M_format_arg(
size_t __id)
override
5306 if constexpr (
sizeof...(_Args) != 0)
5308 if (__id <
sizeof...(_Args))
5310 _M_parse_format_spec<_Args...>(__id);
5314 __builtin_unreachable();
5317 template<
typename _Tp,
typename... _OtherArgs>
5319 _M_parse_format_spec(
size_t __id)
5323 formatter<_Tp, _CharT> __f;
5324 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5326 else if constexpr (
sizeof...(_OtherArgs) != 0)
5327 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5329 __builtin_unreachable();
5332#if __cpp_lib_format >= 202305L
5333 array<_Arg_t,
sizeof...(_Args)>
5334 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5338 template<typename _CharT, unsigned = __unicode::__literal_encoding_is_unicode<_CharT>()>
5339 _GLIBCXX_CONSTEXPR_FORMAT _Sink_iter<_CharT>
5340 __do_vformat_to(_Sink_iter<_CharT> __out, basic_string_view<_CharT> __fmt,
5341 __format_context<_CharT>& __ctx)
5343 if constexpr (is_same_v<_CharT, char>)
5345 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5347 bool __done =
false;
5348 __format::__visit_format_arg([&](
auto& __arg) {
5349 using _Tp = remove_cvref_t<
decltype(__arg)>;
5350 if constexpr (is_same_v<_Tp, bool>)
5352 size_t __len = 4 + !__arg;
5353 const char* __chars[] = {
"false",
"true" };
5354 if (
auto __res = __out._M_reserve(__len))
5356 ranges::copy_n(__chars[__arg], __len, __res.get());
5357 __res._M_bump(__len);
5361 else if constexpr (is_same_v<_Tp, char>)
5363 if (
auto __res = __out._M_reserve(1))
5365 *__res.get() = __arg;
5370 else if constexpr (is_integral_v<_Tp>)
5372 make_unsigned_t<_Tp> __uval;
5373 const bool __neg = __arg < 0;
5375 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5378 const auto __n = __detail::__to_chars_len(__uval);
5379 if (
auto __res = __out._M_reserve(__n + __neg))
5381 auto __ptr = __res.get();
5383 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5385 __res._M_bump(__n + __neg);
5389 else if constexpr (is_convertible_v<_Tp, string_view>)
5391 string_view __sv = __arg;
5392 if (
auto __res = __out._M_reserve(__sv.size()))
5394 ranges::copy(__sv, __res.get());
5395 __res._M_bump(__sv.size());
5405 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5406 __scanner._M_scan();
5415#if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE
5416 extern template _Sink_iter<char>
5417 __do_vformat_to<char, 1>(_Sink_iter<char>, string_view,
5419# ifdef _GLIBCXX_USE_WCHAR_T
5420 extern template _Sink_iter<wchar_t>
5421 __do_vformat_to<wchar_t, 1>(_Sink_iter<wchar_t>, wstring_view,
5426 template<
typename _Out,
typename _CharT,
typename _Context>
5427 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5428 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5429 const basic_format_args<_Context>& __args,
5430 const locale* __loc)
5432 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5434 auto __ctx = __loc ==
nullptr
5435 ? _Context(__args, __out)
5436 : _Context(__args, __out, *__loc);
5437 return __format::__do_vformat_to(__out, __fmt, __ctx);
5439 else if constexpr (__contiguous_char_iter<_CharT, _Out>)
5441 _Ptr_sink<_CharT> __sink(__out);
5442 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5443 return std::move(__sink)._M_finish(__out).out;
5447 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5448 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5449 return std::move(__sink)._M_finish().out;
5453 template<
typename _Out,
typename _CharT>
5454 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5455 __do_vformat_to_n(_Out __out, iter_difference_t<_Out> __n,
5456 basic_string_view<_CharT> __fmt,
5457 const type_identity_t<
5458 basic_format_args<__format_context<_CharT>>>& __args,
5459 const locale* __loc =
nullptr)
5461 if constexpr (__contiguous_char_iter<_CharT, _Out>)
5463 _Ptr_sink<_CharT> __sink(__out, __n);
5464 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5465 return std::move(__sink)._M_finish(__out);
5469 _Iter_sink<_CharT, _Out> __sink(
std::move(__out), __n);
5470 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5475#pragma GCC diagnostic pop
5480#if __cpp_lib_format >= 202305L
5483 template<
typename _CharT>
5484 template<
typename... _Ts>
5486 basic_format_parse_context<_CharT>::
5487 __check_dynamic_spec(
size_t __id)
noexcept
5489 if (__id >= _M_num_args)
5490 __format::__invalid_arg_id_in_format_string();
5491 if constexpr (
sizeof...(_Ts) != 0)
5493 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
5494 auto* __args =
static_cast<_Parse_ctx*
>(
this)->_M_types;
5499 auto __arg = __args[__id];
5500 __format::_Arg_t __types[] = {
5501 __format::__to_arg_t_enum<_CharT, _Ts>()...
5503 for (
auto __t : __types)
5507 __invalid_dynamic_spec(
"arg(id) type does not match");
5512 template<
typename _CharT,
typename... _Args>
5513 template<
typename _Tp>
5514 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5516 basic_format_string<_CharT, _Args...>::
5517 basic_format_string(
const _Tp& __s) noexcept
5520 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5522 __scanner._M_scan();
5527 template<
typename _Out>
requires output_iterator<_Out, const char&>
5528 [[__gnu__::__always_inline__]]
5529 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5530 vformat_to(_Out __out, string_view __fmt, format_args __args)
5531 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5533#ifdef _GLIBCXX_USE_WCHAR_T
5534 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5535 [[__gnu__::__always_inline__]]
5536 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5537 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5538 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5541 template<
typename _Out>
requires output_iterator<_Out, const char&>
5542 [[__gnu__::__always_inline__]]
5544 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5547 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5550#ifdef _GLIBCXX_USE_WCHAR_T
5551 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5552 [[__gnu__::__always_inline__]]
5554 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5555 wformat_args __args)
5557 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5562 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5563 vformat(string_view __fmt, format_args __args)
5565 __format::_Str_sink<char> __buf;
5566 std::vformat_to(__buf.out(), __fmt, __args);
5570#ifdef _GLIBCXX_USE_WCHAR_T
5572 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5573 vformat(wstring_view __fmt, wformat_args __args)
5575 __format::_Str_sink<wchar_t> __buf;
5576 std::vformat_to(__buf.out(), __fmt, __args);
5583 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5585 __format::_Str_sink<char> __buf;
5586 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5590#ifdef _GLIBCXX_USE_WCHAR_T
5593 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5595 __format::_Str_sink<wchar_t> __buf;
5596 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5601 template<
typename... _Args>
5603 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5604 format(format_string<_Args...> __fmt, _Args&&... __args)
5605 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5607#ifdef _GLIBCXX_USE_WCHAR_T
5608 template<
typename... _Args>
5610 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5611 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5612 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5615 template<
typename... _Args>
5618 format(
const locale& __loc, format_string<_Args...> __fmt,
5621 return std::vformat(__loc, __fmt.get(),
5622 std::make_format_args(__args...));
5625#ifdef _GLIBCXX_USE_WCHAR_T
5626 template<
typename... _Args>
5629 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5632 return std::vformat(__loc, __fmt.get(),
5633 std::make_wformat_args(__args...));
5637 template<
typename _Out,
typename... _Args>
5638 requires output_iterator<_Out, const char&>
5639 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5640 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5642 return std::vformat_to(
std::move(__out), __fmt.get(),
5643 std::make_format_args(__args...));
5646#ifdef _GLIBCXX_USE_WCHAR_T
5647 template<
typename _Out,
typename... _Args>
5648 requires output_iterator<_Out, const wchar_t&>
5649 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5650 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5652 return std::vformat_to(
std::move(__out), __fmt.get(),
5653 std::make_wformat_args(__args...));
5657 template<
typename _Out,
typename... _Args>
5658 requires output_iterator<_Out, const char&>
5660 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5663 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5664 std::make_format_args(__args...));
5667#ifdef _GLIBCXX_USE_WCHAR_T
5668 template<
typename _Out,
typename... _Args>
5669 requires output_iterator<_Out, const wchar_t&>
5671 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5674 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5675 std::make_wformat_args(__args...));
5679 template<
typename _Out,
typename... _Args>
5680 requires output_iterator<_Out, const char&>
5681 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5682 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5683 format_string<_Args...> __fmt, _Args&&... __args)
5685 return __format::__do_vformat_to_n(
5687 std::make_format_args(__args...));
5690#ifdef _GLIBCXX_USE_WCHAR_T
5691 template<
typename _Out,
typename... _Args>
5692 requires output_iterator<_Out, const wchar_t&>
5693 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5694 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5695 wformat_string<_Args...> __fmt, _Args&&... __args)
5697 return __format::__do_vformat_to_n(
5699 std::make_wformat_args(__args...));
5703 template<
typename _Out,
typename... _Args>
5704 requires output_iterator<_Out, const char&>
5705 inline format_to_n_result<_Out>
5706 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5707 format_string<_Args...> __fmt, _Args&&... __args)
5709 return __format::__do_vformat_to_n(
5711 std::make_format_args(__args...), &__loc);
5714#ifdef _GLIBCXX_USE_WCHAR_T
5715 template<
typename _Out,
typename... _Args>
5716 requires output_iterator<_Out, const wchar_t&>
5717 inline format_to_n_result<_Out>
5718 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5719 wformat_string<_Args...> __fmt, _Args&&... __args)
5721 return __format::__do_vformat_to_n(
5723 std::make_wformat_args(__args...), &__loc);
5731 template<
typename _CharT>
5732 class _Counting_sink final :
public _Ptr_sink<_CharT>
5735 _GLIBCXX_CONSTEXPR_FORMAT
5736 _Counting_sink() : _Ptr_sink<_CharT>(nullptr, 0) { }
5738 [[__gnu__::__always_inline__]]
5739 _GLIBCXX_CONSTEXPR_FORMAT
size_t
5741 {
return this->_M_count + this->_M_used().size(); }
5744 template<
typename _CharT>
5745 class _Counting_sink :
public _Buf_sink<_CharT>
5747 size_t _M_count = 0;
5750 _M_overflow()
override
5752 if (!std::is_constant_evaluated())
5753 _M_count += this->_M_used().size();
5758 _Counting_sink() =
default;
5760 [[__gnu__::__always_inline__]]
5764 _Counting_sink::_M_overflow();
5772 template<
typename... _Args>
5774 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5775 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5777 __format::_Counting_sink<char> __buf;
5778 std::vformat_to(__buf.out(), __fmt.get(),
5779 std::make_format_args(__args...));
5780 return __buf.count();
5783#ifdef _GLIBCXX_USE_WCHAR_T
5784 template<
typename... _Args>
5786 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5787 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5789 __format::_Counting_sink<wchar_t> __buf;
5790 std::vformat_to(__buf.out(), __fmt.get(),
5791 std::make_wformat_args(__args...));
5792 return __buf.count();
5796 template<
typename... _Args>
5799 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5802 __format::_Counting_sink<char> __buf;
5803 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5804 std::make_format_args(__args...));
5805 return __buf.count();
5808#ifdef _GLIBCXX_USE_WCHAR_T
5809 template<
typename... _Args>
5812 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5815 __format::_Counting_sink<wchar_t> __buf;
5816 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5817 std::make_wformat_args(__args...));
5818 return __buf.count();
5822#if __glibcxx_format_ranges
5824 template<typename _Tp>
5825 consteval range_format
5828 using _Ref = ranges::range_reference_t<_Tp>;
5829 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5830 return range_format::disabled;
5831 else if constexpr (
requires {
typename _Tp::key_type; })
5833 if constexpr (
requires {
typename _Tp::mapped_type; })
5835 using _Up = remove_cvref_t<_Ref>;
5836 if constexpr (__is_pair<_Up>)
5837 return range_format::map;
5838 else if constexpr (__is_specialization_of<_Up, tuple>)
5839 if constexpr (tuple_size_v<_Up> == 2)
5840 return range_format::map;
5842 return range_format::set;
5845 return range_format::sequence;
5850 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5851 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5856 template<
typename _CharT,
typename _Out,
typename _Callback>
5857 _GLIBCXX_CONSTEXPR_FORMAT
5858 typename basic_format_context<_Out, _CharT>::iterator
5859 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5860 const _Spec<_CharT>& __spec,
5863 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
5869 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
5871 const size_t __padwidth = __spec._M_get_width(__fc);
5872 if (__padwidth == 0)
5873 return __call(__fc);
5877 _GLIBCXX_CONSTEXPR_FORMAT
5878 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5879 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5882 _GLIBCXX_CONSTEXPR_FORMAT
void
5884 { _M_ctx =
nullptr; }
5886 _GLIBCXX_CONSTEXPR_FORMAT
5890 _M_ctx->advance_to(_M_out);
5894 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5895 _Sink_iter<_CharT> _M_out;
5898 _Restore_out __restore(__fc);
5899 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
5900 __fc.advance_to(__sink.out());
5902 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
5903 __restore._M_disarm();
5908 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5909 struct __indexed_formatter_storage
5916 basic_format_parse_context<_CharT> __pc({});
5917 if (_M_formatter.parse(__pc) != __pc.end())
5918 __format::__failed_to_parse_format_spec();
5921 template<
typename _Out>
5922 _GLIBCXX_CONSTEXPR_FORMAT
void
5923 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5924 basic_format_context<_Out, _CharT>& __fc,
5925 basic_string_view<_CharT> __sep)
const
5927 if constexpr (_Pos != 0)
5928 __fc.advance_to(__format::__write(__fc.out(), __sep));
5929 __fc.advance_to(_M_formatter.format(__elem, __fc));
5932 [[__gnu__::__always_inline__]]
5936 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5937 _M_formatter.set_debug_format();
5941 formatter<_Tp, _CharT> _M_formatter;
5944 template<
typename _CharT,
typename... _Tps>
5945 class __tuple_formatter
5947 using _String_view = basic_string_view<_CharT>;
5948 using _Seps = __format::_Separators<_CharT>;
5952 set_separator(basic_string_view<_CharT> __sep)
noexcept
5956 set_brackets(basic_string_view<_CharT> __open,
5957 basic_string_view<_CharT> __close)
noexcept
5965 constexpr typename basic_format_parse_context<_CharT>::iterator
5966 parse(basic_format_parse_context<_CharT>& __pc)
5968 auto __first = __pc.begin();
5969 const auto __last = __pc.end();
5970 __format::_Spec<_CharT> __spec{};
5972 auto __finished = [&]
5974 if (__first != __last && *__first !=
'}')
5978 _M_felems._M_parse();
5979 _M_felems.set_debug_format();
5986 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5990 __first = __spec._M_parse_width(__first, __last, __pc);
5994 if (*__first ==
'n')
5997 _M_open = _M_close = _String_view();
5999 else if (*__first ==
'm')
6002 if constexpr (
sizeof...(_Tps) == 2)
6004 _M_sep = _Seps::_S_colon();
6005 _M_open = _M_close = _String_view();
6008 __throw_format_error(
"format error: 'm' specifier requires range"
6009 " of pair or tuple of two elements");
6015 __format::__failed_to_parse_format_spec();
6019 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
6020 _GLIBCXX_CONSTEXPR_FORMAT
6021 typename basic_format_context<_Out, _CharT>::iterator
6022 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
6023 basic_format_context<_Out, _CharT>& __fc)
const
6024 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
6026 template<
typename _Out>
6027 _GLIBCXX_CONSTEXPR_FORMAT
6028 typename basic_format_context<_Out, _CharT>::iterator
6029 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
6030 basic_format_context<_Out, _CharT>& __fc)
const
6032 return __format::__format_padded(
6034 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
6036 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
6037 _M_felems._M_format(__elems..., __nfc, _M_sep);
6038 return __format::__write(__nfc.out(), _M_close);
6043 template<
size_t... _Ids>
6044 struct __formatters_storage
6045 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
6047 template<
size_t _Id,
typename _Up>
6048 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
6053 (_Base<_Ids, _Tps>::_M_parse(), ...);
6056 template<
typename _Out>
6057 _GLIBCXX_CONSTEXPR_FORMAT
void
6058 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
6059 basic_format_context<_Out, _CharT>& __fc,
6060 _String_view __sep)
const
6062 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
6068 (_Base<_Ids, _Tps>::set_debug_format(), ...);
6072 template<
size_t... _Ids>
6073 static _GLIBCXX_CONSTEXPR_FORMAT
auto
6074 _S_create_storage(index_sequence<_Ids...>)
6075 -> __formatters_storage<_Ids...>;
6077 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
6079 _Spec<_CharT> _M_spec{};
6080 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
6081 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
6082 _String_view _M_sep = _Seps::_S_comma();
6083 _Formatters _M_felems;
6086 template<
typename _Tp>
6087 concept __is_map_formattable
6088 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
6094 template<__format::__
char _CharT, formattable<_CharT> _Fp,
6095 formattable<_CharT> _Sp>
6096 struct formatter<
pair<_Fp, _Sp>, _CharT>
6097 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
6098 remove_cvref_t<_Sp>>
6101 using __maybe_const_pair
6102 = __conditional_t<formattable<const _Fp, _CharT>
6103 && formattable<const _Sp, _CharT>,
6104 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
6108 template<
typename _Out>
6109 _GLIBCXX_CONSTEXPR_FORMAT
6110 typename basic_format_context<_Out, _CharT>::iterator
6111 format(__maybe_const_pair& __p,
6112 basic_format_context<_Out, _CharT>& __fc)
const
6113 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
6116#if __glibcxx_print >= 202406L
6119 template<
typename _Fp,
typename _Sp>
6120 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
6121 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
6122 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
6125 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
6126 struct formatter<tuple<_Tps...>, _CharT>
6127 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
6130 using __maybe_const_tuple
6131 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
6132 const tuple<_Tps...>, tuple<_Tps...>>;
6136 template<
typename _Out>
6137 _GLIBCXX_CONSTEXPR_FORMAT
6138 typename basic_format_context<_Out, _CharT>::iterator
6139 format(__maybe_const_tuple& __t,
6140 basic_format_context<_Out, _CharT>& __fc)
const
6141 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
6144#if __glibcxx_print >= 202406L
6147 template<
typename... _Tps>
6148 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
6149 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
6153 template<
typename _Tp, __format::__
char _CharT>
6154 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
6155 class range_formatter
6157 using _String_view = basic_string_view<_CharT>;
6158 using _Seps = __format::_Separators<_CharT>;
6162 set_separator(basic_string_view<_CharT> __sep)
noexcept
6166 set_brackets(basic_string_view<_CharT> __open,
6167 basic_string_view<_CharT> __close)
noexcept
6173 constexpr formatter<_Tp, _CharT>&
6174 underlying() noexcept
6177 constexpr const formatter<_Tp, _CharT>&
6178 underlying() const noexcept
6183 constexpr typename basic_format_parse_context<_CharT>::iterator
6184 parse(basic_format_parse_context<_CharT>& __pc)
6186 auto __first = __pc.begin();
6187 const auto __last = __pc.end();
6188 __format::_Spec<_CharT> __spec{};
6189 bool __no_brace =
false;
6191 auto __finished = [&]
6192 {
return __first == __last || *__first ==
'}'; };
6194 auto __finalize = [&]
6200 auto __parse_val = [&](_String_view __nfs = _String_view())
6204 basic_format_parse_context<_CharT> __npc(__nfs);
6205 if (_M_fval.parse(__npc) != __npc.end())
6206 __format::__failed_to_parse_format_spec();
6207 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6208 _M_fval.set_debug_format();
6209 return __finalize();
6213 return __parse_val();
6215 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6217 return __parse_val();
6219 __first = __spec._M_parse_width(__first, __last, __pc);
6221 return __parse_val();
6223 if (*__first ==
'?')
6226 __spec._M_debug =
true;
6227 if (__finished() || *__first !=
's')
6228 __throw_format_error(
"format error: '?' is allowed only in"
6229 " combination with 's'");
6232 if (*__first ==
's')
6235 if constexpr (same_as<_Tp, _CharT>)
6237 __spec._M_type = __format::_Pres_s;
6239 return __finalize();
6240 __throw_format_error(
"format error: element format specifier"
6241 " cannot be provided when 's' specifier is used");
6244 __throw_format_error(
"format error: 's' specifier requires"
6245 " range of character types");
6249 return __parse_val();
6251 if (*__first ==
'n')
6254 _M_open = _M_close = _String_view();
6259 return __parse_val();
6261 if (*__first ==
'm')
6263 _String_view __m(__first, 1);
6265 if constexpr (__format::__is_map_formattable<_Tp>)
6267 _M_sep = _Seps::_S_comma();
6270 _M_open = _Seps::_S_braces().substr(0, 1);
6271 _M_close = _Seps::_S_braces().substr(1, 1);
6274 return __parse_val(__m);
6275 __throw_format_error(
"format error: element format specifier"
6276 " cannot be provided when 'm' specifier is used");
6279 __throw_format_error(
"format error: 'm' specifier requires"
6280 " range of pairs or tuples of two elements");
6284 return __parse_val();
6286 if (*__first ==
':')
6288 __pc.advance_to(++__first);
6289 __first = _M_fval.parse(__pc);
6293 return __finalize();
6295 __format::__failed_to_parse_format_spec();
6300 template<ranges::input_range _Rg,
typename _Out>
6301 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6302 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6303 _GLIBCXX_CONSTEXPR_FORMAT
6304 typename basic_format_context<_Out, _CharT>::iterator
6305 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6307 using _Range = remove_reference_t<_Rg>;
6308 if constexpr (ranges::contiguous_range<_Rg>)
6310 const span<__format::__maybe_const<_Tp, _CharT>>
6311 __spn(ranges::data(__rg),
size_t(ranges::distance(__rg)));
6312 return _M_format(__spn, __fc);
6314 else if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6315 return _M_format<const _Range>(__rg, __fc);
6317 return _M_format(__rg, __fc);
6321 template<ranges::input_range _Rg,
typename _Out>
6322 _GLIBCXX_CONSTEXPR_FORMAT
6323 typename basic_format_context<_Out, _CharT>::iterator
6324 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6326 if constexpr (same_as<_Tp, _CharT>)
6327 if (_M_spec._M_type == __format::_Pres_s)
6329 __format::__formatter_str __fstr(_M_spec);
6330 return __fstr._M_format_range(__rg, __fc);
6332 return __format::__format_padded(
6334 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6335 {
return _M_format_elems(__rg, __nfc); });
6339 template<ranges::input_range _Rg,
typename _Out>
6340 _GLIBCXX_CONSTEXPR_FORMAT
6341 typename basic_format_context<_Out, _CharT>::iterator
6342 _M_format_elems(_Rg& __rg,
6343 basic_format_context<_Out, _CharT>& __fc)
const
6345 auto __out = __format::__write(__fc.out(), _M_open);
6347 auto __first = ranges::begin(__rg);
6348 auto const __last = ranges::end(__rg);
6349 if (__first == __last)
6350 return __format::__write(__out, _M_close);
6352 __fc.advance_to(__out);
6353 __out = _M_fval.format(*__first, __fc);
6354 for (++__first; __first != __last; ++__first)
6356 __out = __format::__write(__out, _M_sep);
6357 __fc.advance_to(__out);
6358 __out = _M_fval.format(*__first, __fc);
6361 return __format::__write(__out, _M_close);
6364 __format::_Spec<_CharT> _M_spec{};
6365 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6366 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6367 _String_view _M_sep = _Seps::_S_comma();
6368 formatter<_Tp, _CharT> _M_fval;
6376 template<ranges::input_range _Rg, __format::__
char _CharT>
6377 requires (format_kind<_Rg> != range_format::disabled)
6378 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6379 struct formatter<_Rg, _CharT>
6382 static const bool _S_range_format_is_string =
6383 (format_kind<_Rg> == range_format::string)
6384 || (format_kind<_Rg> == range_format::debug_string);
6385 using _Vt = remove_cvref_t<
6386 ranges::range_reference_t<
6387 __format::__maybe_const_range<_Rg, _CharT>>>;
6389 static consteval bool _S_is_correct()
6391 if constexpr (_S_range_format_is_string)
6392 static_assert(same_as<_Vt, _CharT>);
6396 static_assert(_S_is_correct());
6399 constexpr formatter() noexcept
6401 using _Seps = __format::_Separators<_CharT>;
6402 if constexpr (format_kind<_Rg> == range_format::map)
6404 static_assert(__format::__is_map_formattable<_Vt>);
6405 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6406 _Seps::_S_braces().substr(1, 1));
6407 _M_under.underlying().set_brackets({}, {});
6408 _M_under.underlying().set_separator(_Seps::_S_colon());
6410 else if constexpr (format_kind<_Rg> == range_format::set)
6411 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6412 _Seps::_S_braces().substr(1, 1));
6416 set_separator(basic_string_view<_CharT> __sep)
noexcept
6417 requires (format_kind<_Rg> == range_format::sequence)
6418 { _M_under.set_separator(__sep); }
6421 set_brackets(basic_string_view<_CharT> __open,
6422 basic_string_view<_CharT> __close)
noexcept
6423 requires (format_kind<_Rg> == range_format::sequence)
6424 { _M_under.set_brackets(__open, __close); }
6428 constexpr typename basic_format_parse_context<_CharT>::iterator
6429 parse(basic_format_parse_context<_CharT>& __pc)
6431 auto __res = _M_under.parse(__pc);
6432 if constexpr (format_kind<_Rg> == range_format::debug_string)
6433 _M_under.set_debug_format();
6439 template<
typename _Out>
6440 _GLIBCXX_CONSTEXPR_FORMAT
6441 typename basic_format_context<_Out, _CharT>::iterator
6442 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6443 basic_format_context<_Out, _CharT>& __fc)
const
6445 if constexpr (_S_range_format_is_string)
6446 return _M_under._M_format_range(__rg, __fc);
6448 return _M_under.format(__rg, __fc);
6452 using _Formatter_under
6453 = __conditional_t<_S_range_format_is_string,
6454 __format::__formatter_str<_CharT>,
6455 range_formatter<_Vt, _CharT>>;
6456 _Formatter_under _M_under;
6459#if __glibcxx_print >= 202406L
6460 template<ranges::input_range _Rg>
6461 requires (format_kind<_Rg> != range_format::disabled)
6462 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6466#undef _GLIBCXX_WIDEN
6467#undef _GLIBCXX_CONSTEXPR_FORMAT
6469_GLIBCXX_END_NAMESPACE_VERSION
6472#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>.
A non-owning reference to a string.
Managing sequences of characters and character-like objects.
constexpr size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
constexpr void reserve(size_type __res_arg)
Attempt to preallocate enough memory for specified number of characters.
constexpr const _CharT * data() const noexcept
Return const pointer to contents.
constexpr basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
constexpr void __resize_and_overwrite(size_type __n, _Operation __op)
Non-standard version of resize_and_overwrite for C++11 and above.
constexpr basic_string & append(const basic_string &__str)
Append a string to this string.
constexpr iterator insert(const_iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
constexpr size_type capacity() const noexcept
constexpr bool empty() const noexcept
One of two subclasses of exception.
A standard container which offers fixed time access to individual elements in any order.