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,
499 _Pres_p = _Pres_x, _Pres_P = _Pres_X,
502 using enum _Pres_type;
504 enum class _Sign :
unsigned char {
512 enum _WidthPrec :
unsigned char {
517 using enum _WidthPrec;
519 template<
typename _Context>
520 _GLIBCXX_CONSTEXPR_FORMAT
size_t
521 __int_from_arg(
const basic_format_arg<_Context>& __arg);
523 constexpr bool __is_digit(
char __c)
524 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
526 constexpr bool __is_xdigit(
char __c)
527 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
534 template<
typename _CharT>
535 struct _Spec : _SpecBase
537 unsigned short _M_width;
538 unsigned short _M_prec;
539 char32_t _M_fill =
' ';
543 unsigned _M_localized : 1;
544 unsigned _M_zero_fill : 1;
545 _WidthPrec _M_width_kind : 2;
546 _WidthPrec _M_prec_kind : 2;
547 unsigned _M_debug : 1;
548 _Pres_type _M_type : 4;
549 unsigned _M_reserved : 8;
553 using iterator =
typename basic_string_view<_CharT>::iterator;
555 static constexpr _Align
556 _S_align(_CharT __c)
noexcept
560 case '<':
return _Align_left;
561 case '>':
return _Align_right;
562 case '^':
return _Align_centre;
563 default:
return _Align_default;
569 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
570 {
return _M_parse_fill_and_align(__first, __last,
"{"); }
574 _M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill)
noexcept
576 for (
char __c : __not_fill)
577 if (*__first ==
static_cast<_CharT
>(__c))
580 using namespace __unicode;
581 if constexpr (__literal_encoding_is_unicode<_CharT>())
584 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
587 auto __beg = __uv.begin();
588 char32_t __c = *__beg++;
589 if (__is_scalar_value(__c))
590 if (
auto __next = __beg.base(); __next != __last)
591 if (_Align __align = _S_align(*__next); __align != _Align_default)
599 else if (__last - __first >= 2)
600 if (_Align __align = _S_align(__first[1]); __align != _Align_default)
607 if (_Align __align = _S_align(__first[0]); __align != _Align_default)
616 static constexpr _Sign
617 _S_sign(_CharT __c)
noexcept
621 case '+':
return _Sign_plus;
622 case '-':
return _Sign_minus;
623 case ' ':
return _Sign_space;
624 default:
return _Sign_default;
630 _M_parse_sign(iterator __first, iterator)
noexcept
632 if (_Sign __sign = _S_sign(*__first); __sign != _Sign_default)
642 _M_parse_alternate_form(iterator __first, iterator)
noexcept
654 _M_parse_zero_fill(iterator __first, iterator )
noexcept
665 static constexpr iterator
666 _S_parse_width_or_precision(iterator __first, iterator __last,
667 unsigned short& __val,
bool& __arg_id,
668 basic_format_parse_context<_CharT>& __pc)
670 if (__format::__is_digit(*__first))
672 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
674 __throw_format_error(
"format error: invalid width or precision "
679 else if (*__first ==
'{')
683 if (__first == __last)
684 __format::__unmatched_left_brace_in_format_string();
686 __val = __pc.next_arg_id();
689 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
690 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
691 __format::__invalid_arg_id_in_format_string();
693 __pc.check_arg_id(__v);
696#if __cpp_lib_format >= 202305L
697 __pc.check_dynamic_spec_integral(__val);
706 _M_parse_width(iterator __first, iterator __last,
707 basic_format_parse_context<_CharT>& __pc)
709 bool __arg_id =
false;
711 __throw_format_error(
"format error: width must be non-zero in "
713 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
715 if (__next != __first)
716 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
722 _M_parse_precision(iterator __first, iterator __last,
723 basic_format_parse_context<_CharT>& __pc)
725 if (__first[0] !=
'.')
728 iterator __next = ++__first;
729 bool __arg_id =
false;
730 if (__next != __last)
731 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
733 if (__next == __first)
734 __throw_format_error(
"format error: missing precision after '.' in "
736 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
742 _M_parse_locale(iterator __first, iterator )
noexcept
752 template<
typename _Context>
753 _GLIBCXX_CONSTEXPR_FORMAT
size_t
754 _M_get_width(_Context& __ctx)
const
757 if (_M_width_kind == _WP_value)
759 else if (_M_width_kind == _WP_from_arg)
760 __width = __format::__int_from_arg(__ctx.arg(_M_width));
764 template<
typename _Context>
765 _GLIBCXX_CONSTEXPR_FORMAT
size_t
766 _M_get_precision(_Context& __ctx)
const
769 if (_M_prec_kind == _WP_value)
771 else if (_M_prec_kind == _WP_from_arg)
772 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
777 template<
typename _Int>
778 inline _GLIBCXX_CONSTEXPR_FORMAT
char*
779 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
783 else if (__sign == _Sign_plus)
785 else if (__sign == _Sign_space)
793 template<
typename _Out,
typename _CharT>
794 requires output_iterator<_Out, const _CharT&>
795 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
796 __write(_Out __out, basic_string_view<_CharT> __str)
798 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
804 for (_CharT __c : __str)
811 template<
typename _Out,
typename _CharT>
812 _GLIBCXX_CONSTEXPR_FORMAT _Out
813 __write_padded(_Out __out, basic_string_view<_CharT> __str,
814 _Align __align,
size_t __nfill,
char32_t __fill_char)
816 const size_t __buflen = 0x20;
817 _CharT __padding_chars[__buflen];
818 __padding_chars[0] = _CharT();
819 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
821 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
824 while (__n > __padding.size())
826 __o = __format::__write(
std::move(__o), __padding);
827 __n -= __padding.size();
830 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
833 size_t __l, __r, __max;
834 if (__align == _Align_centre)
837 __r = __l + (__nfill & 1);
840 else if (__align == _Align_right)
853 using namespace __unicode;
854 if constexpr (__literal_encoding_is_unicode<_CharT>())
855 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
858 const char32_t __arr[1]{ __fill_char };
859 _Utf_view<_CharT, span<const char32_t, 1>> __v(__arr);
860 basic_string<_CharT> __padstr(__v.begin(), __v.end());
861 __padding = __padstr;
863 __out = __format::__write(
std::move(__out), __padding);
864 __out = __format::__write(
std::move(__out), __str);
866 __out = __format::__write(
std::move(__out), __padding);
870 if (__max < __buflen)
871 __padding.remove_suffix(__buflen - __max);
875 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
877 __out = __format::__write(
std::move(__out), __str);
885 template<
typename _CharT,
typename _Out>
886 _GLIBCXX_CONSTEXPR_FORMAT _Out
887 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
888 size_t __estimated_width,
889 basic_format_context<_Out, _CharT>& __fc,
890 const _Spec<_CharT>& __spec,
891 _Align __align = _Align_left)
893 size_t __width = __spec._M_get_width(__fc);
895 if (__width <= __estimated_width)
896 return __format::__write(__fc.out(), __str);
898 const size_t __nfill = __width - __estimated_width;
900 if (__spec._M_align != _Align_default)
901 __align = __spec._M_align;
903 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
907 template<
typename _CharT>
908 _GLIBCXX_CONSTEXPR_FORMAT
size_t
909 __truncate(basic_string_view<_CharT>& __s,
size_t __prec)
911 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
913 if (__prec != (
size_t)-1)
914 return __unicode::__truncate(__s, __prec);
916 return __unicode::__field_width(__s);
920 __s = __s.substr(0, __prec);
925 enum class _Term_char :
unsigned char {
930 using enum _Term_char;
932 template<
typename _CharT>
935 using _Str_view = basic_string_view<_CharT>;
939 {
return _GLIBCXX_WIDEN(
"\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
943 {
return _S_all().substr(0, 3); }
946 _Str_view _S_newline()
947 {
return _S_all().substr(3, 3); }
950 _Str_view _S_return()
951 {
return _S_all().substr(6, 3); }
954 _Str_view _S_bslash()
955 {
return _S_all().substr(9, 3); }
959 {
return _S_all().substr(12, 3); }
963 {
return _S_all().substr(15, 3); }
967 {
return _S_all().substr(18, 2); }
971 {
return _S_all().substr(20, 2); }
974 _Str_view _S_term(_Term_char __term)
981 return _S_quote().substr(0, 1);
983 return _S_apos().substr(0, 1);
985 __builtin_unreachable();
989 template<
typename _CharT>
992 using _Str_view = basic_string_view<_CharT>;
996 {
return _GLIBCXX_WIDEN(
"[]{}(), : "); }
999 _Str_view _S_squares()
1000 {
return _S_all().substr(0, 2); }
1003 _Str_view _S_braces()
1004 {
return _S_all().substr(2, 2); }
1007 _Str_view _S_parens()
1008 {
return _S_all().substr(4, 2); }
1011 _Str_view _S_comma()
1012 {
return _S_all().substr(6, 2); }
1015 _Str_view _S_colon()
1016 {
return _S_all().substr(8, 2); }
1019 template<
typename _CharT>
1020 constexpr bool __should_escape_ascii(_CharT __c, _Term_char __term)
1022 using _Esc = _Escapes<_CharT>;
1025 case _Esc::_S_tab()[0]:
1026 case _Esc::_S_newline()[0]:
1027 case _Esc::_S_return()[0]:
1028 case _Esc::_S_bslash()[0]:
1030 case _Esc::_S_quote()[0]:
1031 return __term == _Term_quote;
1032 case _Esc::_S_apos()[0]:
1033 return __term == _Term_apos;
1035 return (__c >= 0 && __c < 0x20) || __c == 0x7f;
1040 constexpr bool __should_escape_unicode(
char32_t __c,
bool __prev_esc)
1042 if (__unicode::__should_escape_category(__c))
1046 return __unicode::__grapheme_cluster_break_property(__c)
1047 == __unicode::_Gcb_property::_Gcb_Extend;
1050 using uint_least32_t = __UINT_LEAST32_TYPE__;
1051 template<
typename _Out,
typename _CharT>
1052 _GLIBCXX_CONSTEXPR_FORMAT _Out
1053 __write_escape_seq(_Out __out, uint_least32_t __val,
1054 basic_string_view<_CharT> __prefix)
1056 using _Str_view = basic_string_view<_CharT>;
1057 constexpr size_t __max = 8;
1059 const string_view __narrow(
1061 std::__to_chars_i<uint_least32_t>(__buf, __buf + __max, __val, 16).ptr);
1063 __out = __format::__write(__out, __prefix);
1064 *__out = _Separators<_CharT>::_S_braces()[0];
1066 if constexpr (is_same_v<char, _CharT>)
1067 __out = __format::__write(__out, __narrow);
1068#ifdef _GLIBCXX_USE_WCHAR_T
1071 _CharT __wbuf[__max];
1072 const size_t __n = __narrow.size();
1073 std::__to_wstring_numeric(__narrow.data(), __n, __wbuf);
1074 __out = __format::__write(__out, _Str_view(__wbuf, __n));
1077 *__out = _Separators<_CharT>::_S_braces()[1];
1081 template<
typename _Out,
typename _CharT>
1082 _GLIBCXX_CONSTEXPR_FORMAT _Out
1083 __write_escape_seqs(_Out __out, basic_string_view<_CharT> __units)
1085 using _UChar = make_unsigned_t<_CharT>;
1086 for (_CharT __c : __units)
1087 __out = __format::__write_escape_seq(
1088 __out,
static_cast<_UChar
>(__c), _Escapes<_CharT>::_S_x());
1092 template<
typename _Out,
typename _CharT>
1093 _GLIBCXX_CONSTEXPR_FORMAT _Out
1094 __write_escaped_char(_Out __out, _CharT __c)
1096 using _UChar = make_unsigned_t<_CharT>;
1097 using _Esc = _Escapes<_CharT>;
1100 case _Esc::_S_tab()[0]:
1101 return __format::__write(__out, _Esc::_S_tab().substr(1, 2));
1102 case _Esc::_S_newline()[0]:
1103 return __format::__write(__out, _Esc::_S_newline().substr(1, 2));
1104 case _Esc::_S_return()[0]:
1105 return __format::__write(__out, _Esc::_S_return().substr(1, 2));
1106 case _Esc::_S_bslash()[0]:
1107 return __format::__write(__out, _Esc::_S_bslash().substr(1, 2));
1108 case _Esc::_S_quote()[0]:
1109 return __format::__write(__out, _Esc::_S_quote().substr(1, 2));
1110 case _Esc::_S_apos()[0]:
1111 return __format::__write(__out, _Esc::_S_apos().substr(1, 2));
1113 return __format::__write_escape_seq(
1114 __out,
static_cast<_UChar
>(__c), _Esc::_S_u());
1118 template<
typename _CharT,
typename _Out>
1119 _GLIBCXX_CONSTEXPR_FORMAT _Out
1120 __write_escaped_ascii(_Out __out,
1121 basic_string_view<_CharT> __str,
1124 using _Str_view = basic_string_view<_CharT>;
1128 constexpr _Str_view __supported(_GLIBCXX_WIDEN(
1129 "ABCDEFGHIJKLMNOPQRSTUWXYZ"
1130 "abdeefghijklmnopqrstuwzyz"
1131 " !#$%&'()*+-./:;<=>?[]^_{|}~"
1132 "0123456789" "\t\n\r\\\"\'\0"
1134 if (__str.find_first_not_of(__supported) != _Str_view::npos)
1135#if __has_builtin(__builtin_constexpr_diag)
1136 __builtin_constexpr_diag (2,
"",
1137 "for non-Unicode literal encodings, only"
1138 " printable ASCII characters and standard"
1139 " escape sequencess can be escaped in constant"
1146 auto __first = __str.begin();
1147 auto const __last = __str.end();
1148 while (__first != __last)
1150 auto __print = __first;
1152 while (__print != __last
1153 && !__format::__should_escape_ascii(*__print, __term))
1156 if (__print != __first)
1157 __out = __format::__write(__out, _Str_view(__first, __print));
1159 if (__print == __last)
1163 __out = __format::__write_escaped_char(__out, *__first);
1169 template<
typename _CharT,
typename _Out>
1170 _GLIBCXX_CONSTEXPR_FORMAT _Out
1171 __write_escaped_unicode_part(_Out __out, basic_string_view<_CharT>& __str,
1172 bool& __prev_esc, _Term_char __term)
1174 using _Str_view = basic_string_view<_CharT>;
1175 using _Esc = _Escapes<_CharT>;
1177 static constexpr char32_t __replace = U
'\uFFFD';
1178 static constexpr _Str_view __replace_rep = []
1181 if constexpr (is_same_v<char, _CharT>)
1182 return "\xEF\xBF\xBD";
1187 __unicode::_Utf_view<char32_t, _Str_view> __v(
std::move(__str));
1190 auto __first = __v.begin();
1191 auto const __last = __v.end();
1192 while (__first != __last)
1194 bool __esc_ascii =
false;
1195 bool __esc_unicode =
false;
1196 bool __esc_replace =
false;
1197 auto __should_escape = [&](
auto const& __it)
1201 = __format::__should_escape_ascii(*__it.base(), __term);
1202 if (__format::__should_escape_unicode(*__it, __prev_esc))
1203 return __esc_unicode =
true;
1204 if (*__it == __replace)
1206 _Str_view __units(__it.base(), __it._M_units());
1207 return __esc_replace = (__units != __replace_rep);
1212 auto __print = __first;
1213 while (__print != __last && !__should_escape(__print))
1219 if (__print != __first)
1220 __out = __format::__write(__out, _Str_view(__first.base(), __print.base()));
1222 if (__print == __last)
1227 __out = __format::__write_escaped_char(__out, *__first.base());
1228 else if (__esc_unicode)
1229 __out = __format::__write_escape_seq(__out, *__first, _Esc::_S_u());
1231 else if (_Str_view __units(__first.base(), __first._M_units());
1232 __units.end() != __last.base())
1233 __out = __format::__write_escape_seqs(__out, __units);
1247 template<
typename _CharT,
typename _Out>
1248 _GLIBCXX_CONSTEXPR_FORMAT _Out
1249 __write_escaped_unicode(_Out __out, basic_string_view<_CharT> __str,
1252 bool __prev_escape =
true;
1253 __out = __format::__write_escaped_unicode_part(__out, __str,
1254 __prev_escape, __term);
1255 __out = __format::__write_escape_seqs(__out, __str);
1259 template<
typename _CharT,
typename _Out>
1260 _GLIBCXX_CONSTEXPR_FORMAT _Out
1261 __write_escaped(_Out __out, basic_string_view<_CharT> __str, _Term_char __term)
1263 __out = __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1265 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1266 __out = __format::__write_escaped_unicode(__out, __str, __term);
1267 else if constexpr (is_same_v<char, _CharT>
1268 && __unicode::__literal_encoding_is_extended_ascii())
1269 __out = __format::__write_escaped_ascii(__out, __str, __term);
1272 __out = __format::__write_escaped_ascii(__out, __str, __term);
1274 return __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1278 struct _Optional_locale
1280 [[__gnu__::__always_inline__]]
1281 _GLIBCXX_CONSTEXPR_FORMAT
1282 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
1284 _Optional_locale(
const locale& __loc) noexcept
1285 : _M_loc(__loc), _M_hasval(
true)
1288 _GLIBCXX_CONSTEXPR_FORMAT
1289 _Optional_locale(
const _Optional_locale& __l) noexcept
1290 : _M_dummy(), _M_hasval(__l._M_hasval)
1293 std::construct_at(&_M_loc, __l._M_loc);
1296 _GLIBCXX_CONSTEXPR_FORMAT
1298 operator=(
const _Optional_locale& __l)
noexcept
1303 _M_loc = __l._M_loc;
1310 else if (__l._M_hasval)
1312 std::construct_at(&_M_loc, __l._M_loc);
1318 _GLIBCXX_CONSTEXPR_FORMAT
1319 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
1322 operator=(locale&& __loc)
noexcept
1328 std::construct_at(&_M_loc,
std::move(__loc));
1339 std::construct_at(&_M_loc);
1345 _GLIBCXX_CONSTEXPR_FORMAT
bool
1346 has_value() const noexcept {
return _M_hasval; }
1349 char _M_dummy =
'\0';
1352 bool _M_hasval =
false;
1355 template<__
char _CharT>
1356 struct __formatter_str
1358 __formatter_str() =
default;
1361 __formatter_str(_Spec<_CharT> __spec) noexcept
1365 constexpr typename basic_format_parse_context<_CharT>::iterator
1366 parse(basic_format_parse_context<_CharT>& __pc)
1368 auto __first = __pc.begin();
1369 const auto __last = __pc.end();
1370 _Spec<_CharT> __spec{};
1372 auto __finalize = [
this, &__spec] {
1376 auto __finished = [&] {
1377 if (__first == __last || *__first ==
'}')
1388 __first = __spec._M_parse_fill_and_align(__first, __last);
1392 __first = __spec._M_parse_width(__first, __last, __pc);
1396 __first = __spec._M_parse_precision(__first, __last, __pc);
1400 if (*__first ==
's')
1402 __spec._M_type = _Pres_s;
1405#if __glibcxx_format_ranges
1406 else if (*__first ==
'?')
1408 __spec._M_debug =
true;
1416 __format::__failed_to_parse_format_spec();
1419 template<
typename _Out>
1420 _GLIBCXX_CONSTEXPR_FORMAT _Out
1421 format(basic_string_view<_CharT> __s,
1422 basic_format_context<_Out, _CharT>& __fc)
const
1424 if (_M_spec._M_debug)
1425 return _M_format_escaped(__s, __fc);
1427 if (_M_spec._M_width_kind == _WP_none
1428 && _M_spec._M_prec_kind == _WP_none)
1429 return __format::__write(__fc.out(), __s);
1431 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1432 const size_t __width = __format::__truncate(__s, __maxwidth);
1433 return __format::__write_padded_as_spec(__s, __width, __fc, _M_spec);
1436 template<
typename _Out>
1437 _GLIBCXX_CONSTEXPR_FORMAT _Out
1438 _M_format_escaped(basic_string_view<_CharT> __s,
1439 basic_format_context<_Out, _CharT>& __fc)
const
1441 const size_t __padwidth = _M_spec._M_get_width(__fc);
1442 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1443 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1445 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1446 const size_t __width = __truncate(__s, __maxwidth);
1448 if (__padwidth <= __width && _M_spec._M_prec_kind == _WP_none)
1449 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1454 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth, __maxwidth);
1455 __format::__write_escaped(__sink.out(), __s, _Term_quote);
1456 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1459#if __glibcxx_format_ranges
1460 template<ranges::input_range _Rg,
typename _Out>
1461 requires same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _CharT>
1462 _GLIBCXX_CONSTEXPR_FORMAT _Out
1463 _M_format_range(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
1465 using _Range = remove_reference_t<_Rg>;
1466 using _String_view = basic_string_view<_CharT>;
1467 if constexpr (ranges::contiguous_range<_Rg>)
1469 _String_view __str(ranges::data(__rg),
1470 size_t(ranges::distance(__rg)));
1471 return format(__str, __fc);
1473 else if constexpr (!is_const_v<_Range>
1474 && __simply_formattable_range<_Range, _CharT>)
1475 return _M_format_range<const _Range&>(__rg, __fc);
1476 else if constexpr (!is_lvalue_reference_v<_Rg>)
1477 return _M_format_range<_Range&>(__rg, __fc);
1480 auto __handle_debug = [
this, &__rg]<
typename _NOut>(_NOut __nout)
1482 if (!_M_spec._M_debug)
1483 return ranges::copy(__rg,
std::move(__nout)).out;
1485 _Escaping_sink<_NOut, _CharT>
1487 ranges::copy(__rg, __sink.out());
1488 return __sink._M_finish();
1491 const size_t __padwidth = _M_spec._M_get_width(__fc);
1492 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1493 return __handle_debug(__fc.out());
1495 _Padding_sink<_Out, _CharT>
1496 __sink(__fc.out(), __padwidth, _M_spec._M_get_precision(__fc));
1497 __handle_debug(__sink.out());
1498 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1503 set_debug_format() noexcept
1504 { _M_spec._M_debug =
true; }
1508 _Spec<_CharT> _M_spec{};
1513 [[__gnu__::__always_inline__]]
1515 __toupper_numeric(
char __c)
1519 case 'a':
return 'A';
1520 case 'b':
return 'B';
1521 case 'c':
return 'C';
1522 case 'd':
return 'D';
1523 case 'e':
return 'E';
1524 case 'f':
return 'F';
1525 case 'i':
return 'I';
1526 case 'n':
return 'N';
1527 case 'p':
return 'P';
1528 case 'x':
return 'X';
1529 default:
return __c;
1533 template<__
char _CharT>
1534 struct __formatter_int
1538 static constexpr _Pres_type _AsInteger = _Pres_d;
1539 static constexpr _Pres_type _AsBool = _Pres_s;
1540 static constexpr _Pres_type _AsChar = _Pres_c;
1542 __formatter_int() =
default;
1545 __formatter_int(_Spec<_CharT> __spec) noexcept
1548 if (_M_spec._M_type == _Pres_none)
1549 _M_spec._M_type = _Pres_d;
1552 constexpr typename basic_format_parse_context<_CharT>::iterator
1553 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1555 _Spec<_CharT> __spec{};
1556 __spec._M_type = __type;
1558 const auto __last = __pc.end();
1559 auto __first = __pc.begin();
1561 auto __finalize = [
this, &__spec] {
1565 auto __finished = [&] {
1566 if (__first == __last || *__first ==
'}')
1577 __first = __spec._M_parse_fill_and_align(__first, __last);
1581 __first = __spec._M_parse_sign(__first, __last);
1585 __first = __spec._M_parse_alternate_form(__first, __last);
1589 __first = __spec._M_parse_zero_fill(__first, __last);
1593 __first = __spec._M_parse_width(__first, __last, __pc);
1597 __first = __spec._M_parse_locale(__first, __last);
1604 __spec._M_type = _Pres_b;
1608 __spec._M_type = _Pres_B;
1614 if (__type != _AsBool)
1616 __spec._M_type = _Pres_c;
1621 __spec._M_type = _Pres_d;
1625 __spec._M_type = _Pres_o;
1629 __spec._M_type = _Pres_x;
1633 __spec._M_type = _Pres_X;
1637 if (__type == _AsBool)
1639 __spec._M_type = _Pres_s;
1643#if __glibcxx_format_ranges
1645 if (__type == _AsChar)
1647 __spec._M_debug =
true;
1657 __format::__failed_to_parse_format_spec();
1660 template<
typename _Tp>
1661 constexpr typename basic_format_parse_context<_CharT>::iterator
1662 _M_parse(basic_format_parse_context<_CharT>& __pc)
1664 if constexpr (is_same_v<_Tp, bool>)
1666 auto __end = _M_do_parse(__pc, _AsBool);
1667 if (_M_spec._M_type == _Pres_s)
1668 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1669 || _M_spec._M_zero_fill)
1670 __throw_format_error(
"format error: format-spec contains "
1671 "invalid formatting options for "
1675 else if constexpr (__char<_Tp>)
1677 auto __end = _M_do_parse(__pc, _AsChar);
1678 if (_M_spec._M_type == _Pres_c)
1679 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1680 || _M_spec._M_zero_fill
1682 __throw_format_error(
"format error: format-spec contains "
1683 "invalid formatting options for "
1688 return _M_do_parse(__pc, _AsInteger);
1691 template<
typename _Int,
typename _Out>
1692 _GLIBCXX_CONSTEXPR_FORMAT
1693 typename basic_format_context<_Out, _CharT>::iterator
1694 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1696 if (_M_spec._M_type == _Pres_c)
1697 return _M_format_character(_S_to_character(__i), __fc);
1699 constexpr size_t __buf_size =
sizeof(_Int) * __CHAR_BIT__ + 3;
1700 char __buf[__buf_size];
1701 to_chars_result __res{};
1703 string_view __base_prefix;
1704 make_unsigned_t<_Int> __u;
1706 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1710 char* __start = __buf + 3;
1711 char*
const __end = __buf +
sizeof(__buf);
1712 char*
const __start_digits = __start;
1714 switch (_M_spec._M_type)
1718 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1719 __res = to_chars(__start, __end, __u, 2);
1723 return _M_format_character(_S_to_character(__i), __fc);
1730 __res = to_chars(__start, __end, __u, 10);
1734 __base_prefix =
"0";
1735 __res = to_chars(__start, __end, __u, 8);
1739 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1740 __res = to_chars(__start, __end, __u, 16);
1741 if (_M_spec._M_type == _Pres_X)
1742 for (
auto __p = __start; __p != __res.ptr; ++__p)
1743 *__p = __format::__toupper_numeric(*__p);
1747 if (_M_spec._M_alt && __base_prefix.size())
1749 __start -= __base_prefix.size();
1750 ranges::copy(__base_prefix, __start);
1752 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1755 string_view __narrow_str(__start, __res.ptr - __start);
1756 size_t __prefix_len = __start_digits - __start;
1757 if constexpr (is_same_v<char, _CharT>)
1758 return _M_format_int(__narrow_str, __prefix_len, __fc);
1759#ifdef _GLIBCXX_USE_WCHAR_T
1762 _CharT __wbuf[__buf_size];
1763 size_t __n = __narrow_str.size();
1766 std::__to_wstring_numeric(__narrow_str.data(), __n, __wbuf);
1767 return _M_format_int(basic_string_view<_CharT>(__wbuf, __n),
1768 __prefix_len, __fc);
1773 template<
typename _Out>
1774 _GLIBCXX_CONSTEXPR_FORMAT
1775 typename basic_format_context<_Out, _CharT>::iterator
1776 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1778 if (_M_spec._M_type == _Pres_c)
1779 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1780 if (_M_spec._M_type != _Pres_s)
1781 return format(
static_cast<unsigned char>(__i), __fc);
1783 basic_string<_CharT> __s;
1785 if (_M_spec._M_localized) [[unlikely]]
1788 __s = __i ? __np.truename() : __np.falsename();
1789 __est_width = __s.size();
1793 if constexpr (is_same_v<char, _CharT>)
1794 __s = __i ?
"true" :
"false";
1796 __s = __i ? L
"true" : L
"false";
1797 __est_width = __s.size();
1800 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1804 template<
typename _Out>
1805 _GLIBCXX_CONSTEXPR_FORMAT
1806 typename basic_format_context<_Out, _CharT>::iterator
1807 _M_format_character(_CharT __c,
1808 basic_format_context<_Out, _CharT>& __fc)
const
1810 basic_string_view<_CharT> __in(&__c, 1u);
1811 size_t __width = 1u;
1813 if constexpr (
sizeof(_CharT) > 1u &&
1814 __unicode::__literal_encoding_is_unicode<_CharT>())
1815 __width = __unicode::__field_width(__c);
1817 if (!_M_spec._M_debug)
1818 return __format::__write_padded_as_spec(__in, __width,
1822 if (_M_spec._M_get_width(__fc) <= __width)
1823 return __format::__write_escaped(__fc.out(), __in, _Term_apos);
1826 _Fixedbuf_sink<_CharT> __sink(__buf);
1827 __format::__write_escaped(__sink.out(), __in, _Term_apos);
1829 __in = __sink.view();
1830 if (__in[1] == _Escapes<_CharT>::_S_bslash()[0])
1831 __width = __in.size();
1832 return __format::__write_padded_as_spec(__in, __width,
1836 template<
typename _Int>
1837 static _GLIBCXX_CONSTEXPR_FORMAT _CharT
1838 _S_to_character(_Int __i)
1841 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1843 if (_Traits::__min <= __i && __i <= _Traits::__max)
1844 return static_cast<_CharT
>(__i);
1846 else if constexpr (is_signed_v<_Int>)
1848 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1849 return static_cast<_CharT
>(__i);
1851 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1852 return static_cast<_CharT
>(__i);
1853 __throw_format_error(
"format error: integer not representable as "
1857 template<
typename _Out>
1858 _GLIBCXX_CONSTEXPR_FORMAT
1859 typename basic_format_context<_Out, _CharT>::iterator
1860 _M_format_int(basic_string_view<_CharT> __str,
size_t __prefix_len,
1861 basic_format_context<_Out, _CharT>& __fc)
const
1863 size_t __width = _M_spec._M_get_width(__fc);
1864 if (_M_spec._M_localized)
1866 const auto& __l = __fc.locale();
1867 if (__l.name() !=
"C")
1869 auto& __np = use_facet<numpunct<_CharT>>(__l);
1870 string __grp = __np.grouping();
1873 size_t __n = __str.size() - __prefix_len;
1874 auto __p = (_CharT*)__builtin_alloca(2 * __n
1877 auto __s = __str.data();
1878 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1879 __s += __prefix_len;
1880 auto __end = std::__add_grouping(__p + __prefix_len,
1881 __np.thousands_sep(),
1885 __str = {__p, size_t(__end - __p)};
1890 if (__width <= __str.size())
1891 return __format::__write(__fc.out(), __str);
1893 char32_t __fill_char = _M_spec._M_fill;
1894 _Align __align = _M_spec._M_align;
1896 size_t __nfill = __width - __str.size();
1897 auto __out = __fc.out();
1898 if (__align == _Align_default)
1900 __align = _Align_right;
1901 if (_M_spec._M_zero_fill)
1903 __fill_char = _CharT(
'0');
1905 if (__prefix_len != 0)
1907 __out = __format::__write(
std::move(__out),
1908 __str.substr(0, __prefix_len));
1909 __str.remove_prefix(__prefix_len);
1913 __fill_char = _CharT(
' ');
1915 return __format::__write_padded(
std::move(__out), __str,
1916 __align, __nfill, __fill_char);
1919 _Spec<_CharT> _M_spec{};
1922#ifdef __BFLT16_DIG__
1923 using __bflt16_t =
decltype(0.0bf16);
1934#undef _GLIBCXX_FORMAT_F128
1936#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1939 using __flt128_t = __ieee128;
1940# define _GLIBCXX_FORMAT_F128 1
1942#ifdef __LONG_DOUBLE_IEEE128__
1946 to_chars(
char*,
char*, __ibm128)
noexcept
1947 __asm(
"_ZSt8to_charsPcS_e");
1950 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1951 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1954 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1955 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1956#elif __cplusplus == 202002L
1958 to_chars(
char*,
char*, __ieee128)
noexcept
1959 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1962 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1963 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1966 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1967 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1970#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1973 using __flt128_t =
long double;
1974# define _GLIBCXX_FORMAT_F128 2
1976#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1979 using __flt128_t = _Float128;
1980# define _GLIBCXX_FORMAT_F128 3
1982# if __cplusplus == 202002L
1986 to_chars(
char*,
char*, _Float128)
noexcept
1987# if _GLIBCXX_INLINE_VERSION
1988 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1990 __asm(
"_ZSt8to_charsPcS_DF128_");
1994 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1995# if _GLIBCXX_INLINE_VERSION
1996 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1998 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
2002 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
2003# if _GLIBCXX_INLINE_VERSION
2004 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
2006 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
2011 using std::to_chars;
2014 template<
typename _Tp>
2015 concept __formattable_float
2016 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
2017 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
2019 template<__
char _CharT>
2020 struct __formatter_fp
2022 constexpr typename basic_format_parse_context<_CharT>::iterator
2023 parse(basic_format_parse_context<_CharT>& __pc)
2025 _Spec<_CharT> __spec{};
2026 const auto __last = __pc.end();
2027 auto __first = __pc.begin();
2029 auto __finalize = [
this, &__spec] {
2033 auto __finished = [&] {
2034 if (__first == __last || *__first ==
'}')
2045 __first = __spec._M_parse_fill_and_align(__first, __last);
2049 __first = __spec._M_parse_sign(__first, __last);
2053 __first = __spec._M_parse_alternate_form(__first, __last);
2057 __first = __spec._M_parse_zero_fill(__first, __last);
2061 if (__first[0] !=
'.')
2063 __first = __spec._M_parse_width(__first, __last, __pc);
2068 __first = __spec._M_parse_precision(__first, __last, __pc);
2072 __first = __spec._M_parse_locale(__first, __last);
2079 __spec._M_type = _Pres_a;
2083 __spec._M_type = _Pres_A;
2087 __spec._M_type = _Pres_e;
2091 __spec._M_type = _Pres_E;
2095 __spec._M_type = _Pres_f;
2099 __spec._M_type = _Pres_F;
2103 __spec._M_type = _Pres_g;
2107 __spec._M_type = _Pres_G;
2115 __format::__failed_to_parse_format_spec();
2118 template<
typename _Fp,
typename _Out>
2119 typename basic_format_context<_Out, _CharT>::iterator
2120 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2124 to_chars_result __res{};
2127 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2129 __prec = _M_spec._M_get_precision(__fc);
2131 char* __start = __buf + 1;
2132 char* __end = __buf +
sizeof(__buf);
2135 bool __upper =
false;
2136 bool __trailing_zeros =
false;
2139 switch (_M_spec._M_type)
2146 if (_M_spec._M_type != _Pres_A)
2148 __fmt = chars_format::hex;
2156 __fmt = chars_format::scientific;
2163 __fmt = chars_format::fixed;
2170 __trailing_zeros =
true;
2172 __fmt = chars_format::general;
2177 __fmt = chars_format::general;
2182 auto __to_chars = [&](
char* __b,
char* __e) {
2184 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2185 else if (__fmt != chars_format{})
2186 return __format::to_chars(__b, __e, __v, __fmt);
2188 return __format::to_chars(__b, __e, __v);
2192 __res = __to_chars(__start, __end);
2194 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2198 size_t __guess = 8 + __prec;
2199 if (__fmt == chars_format::fixed)
2201 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2202 || is_same_v<_Fp, long double>)
2207 if constexpr (is_same_v<_Fp, float>)
2208 __builtin_frexpf(__v, &__exp);
2209 else if constexpr (is_same_v<_Fp, double>)
2210 __builtin_frexp(__v, &__exp);
2211 else if constexpr (is_same_v<_Fp, long double>)
2212 __builtin_frexpl(__v, &__exp);
2214 __guess += 1U + __exp * 4004U / 13301U;
2217 __guess += numeric_limits<_Fp>::max_exponent10;
2219 if (__guess <=
sizeof(__buf)) [[unlikely]]
2220 __guess =
sizeof(__buf) * 2;
2229 auto __overwrite = [&__to_chars, &__res] (
char* __p,
size_t __n)
2231 __res = __to_chars(__p + 1, __p + __n - 1);
2232 return __res.ec == errc{} ? __res.ptr - __p : 0;
2237 __start = __dynbuf.
data() + 1;
2238 __end = __dynbuf.
data() + __dynbuf.
size();
2240 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2246 for (
char* __p = __start; __p != __res.ptr; ++__p)
2247 *__p = __format::__toupper_numeric(*__p);
2250 bool __have_sign =
true;
2252 if (!__builtin_signbit(__v))
2254 if (_M_spec._M_sign == _Sign_plus)
2256 else if (_M_spec._M_sign == _Sign_space)
2259 __have_sign =
false;
2262 string_view __narrow_str(__start, __res.ptr - __start);
2266 if (_M_spec._M_alt && __builtin_isfinite(__v))
2268 string_view __s = __narrow_str;
2272 size_t __d = __s.find(
'.');
2273 if (__d != __s.npos)
2275 __p = __s.find(__expc, __d + 1);
2276 if (__p == __s.npos)
2280 if (__trailing_zeros)
2283 if (__s[__have_sign] !=
'0')
2285 __sigfigs = __p - __have_sign - 1;
2290 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2295 __p = __s.find(__expc);
2296 if (__p == __s.npos)
2299 __sigfigs = __d - __have_sign;
2302 if (__trailing_zeros && __prec != 0)
2307 __z = __prec - __sigfigs;
2310 if (
size_t __extras =
int(__d == __p) + __z)
2312 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2316 __builtin_memmove(__start + __p + __extras,
2320 __start[__p++] =
'.';
2321 __builtin_memset(__start + __p,
'0', __z);
2322 __narrow_str = {__s.data(), __s.size() + __extras};
2326 __dynbuf.
reserve(__s.size() + __extras);
2327 if (__dynbuf.
empty())
2329 __dynbuf = __s.
substr(0, __p);
2333 __dynbuf.
append(__z,
'0');
2334 __dynbuf.
append(__s.substr(__p));
2338 __dynbuf.
insert(__p, __extras,
'0');
2340 __dynbuf[__p] =
'.';
2342 __narrow_str = __dynbuf;
2347 basic_string<_CharT> __wstr;
2348 basic_string_view<_CharT> __str;
2349 if constexpr (is_same_v<_CharT, char>)
2350 __str = __narrow_str;
2351#ifdef _GLIBCXX_USE_WCHAR_T
2356 __wstr = std::__to_wstring_numeric(__narrow_str);
2361 if (_M_spec._M_localized && __builtin_isfinite(__v))
2363 auto __s = _M_localize(__str, __expc, __fc.locale());
2368 size_t __width = _M_spec._M_get_width(__fc);
2370 if (__width <= __str.size())
2371 return __format::__write(__fc.out(), __str);
2373 char32_t __fill_char = _M_spec._M_fill;
2374 _Align __align = _M_spec._M_align;
2376 size_t __nfill = __width - __str.size();
2377 auto __out = __fc.out();
2378 if (__align == _Align_default)
2380 __align = _Align_right;
2381 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2383 __fill_char = _CharT(
'0');
2385 if (!__format::__is_xdigit(__narrow_str[0]))
2387 *__out++ = __str[0];
2388 __str.remove_prefix(1);
2392 __fill_char = _CharT(
' ');
2394 return __format::__write_padded(
std::move(__out), __str,
2395 __align, __nfill, __fill_char);
2399 basic_string<_CharT>
2400 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2401 const locale& __loc)
const
2403 basic_string<_CharT> __lstr;
2405 if (__loc == locale::classic())
2408 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2409 const _CharT __point = __np.decimal_point();
2410 const string __grp = __np.grouping();
2412 _CharT __dot, __exp;
2413 if constexpr (is_same_v<_CharT, char>)
2436 __builtin_unreachable();
2440 if (__grp.empty() && __point == __dot)
2443 size_t __d = __str.find(__dot);
2444 size_t __e = min(__d, __str.find(__exp));
2445 if (__e == __str.npos)
2447 const size_t __r = __str.size() - __e;
2448 auto __overwrite = [&](_CharT* __p, size_t) {
2451 if (
auto __c = __str.front(); __c ==
'-' || __c ==
'+' || __c ==
' ')
2456 auto __end = std::__add_grouping(__p + __off, __np.thousands_sep(),
2457 __grp.data(), __grp.size(),
2458 __str.data() + __off,
2459 __str.data() + __e);
2462 if (__d != __str.npos)
2468 const size_t __rlen = __str.size() - __e;
2470 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2473 return (__end - __p);
2475 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2479 _Spec<_CharT> _M_spec{};
2482 template<__format::__
char _CharT>
2483 struct __formatter_ptr
2486 __formatter_ptr() noexcept
2489 _M_spec._M_type = _Pres_p;
2490 _M_spec._M_alt =
true;
2494 __formatter_ptr(_Spec<_CharT> __spec) noexcept
2496 { _M_set_default(_Pres_p); }
2498 constexpr typename basic_format_parse_context<_CharT>::iterator
2499 parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type = _Pres_p)
2501 __format::_Spec<_CharT> __spec{};
2502 const auto __last = __pc.end();
2503 auto __first = __pc.begin();
2505 auto __finalize = [
this, &__spec, __type] {
2507 _M_set_default(__type);
2510 auto __finished = [&] {
2511 if (__first == __last || *__first ==
'}')
2522 __first = __spec._M_parse_fill_and_align(__first, __last);
2528#if __glibcxx_format >= 202304L
2529 __first = __spec._M_parse_zero_fill(__first, __last);
2534 __first = __spec._M_parse_width(__first, __last, __pc);
2538 if (*__first ==
'p')
2540 __spec._M_type = _Pres_p;
2541 __spec._M_alt = !__spec._M_alt;
2544#if __glibcxx_format >= 202304L
2545 else if (*__first ==
'P')
2547 __spec._M_type = _Pres_P;
2548 __spec._M_alt = !__spec._M_alt;
2556 __format::__failed_to_parse_format_spec();
2559 template<
typename _Out>
2560 _GLIBCXX_CONSTEXPR_FORMAT
2561 typename basic_format_context<_Out, _CharT>::iterator
2562 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2565 ?
reinterpret_cast<__UINTPTR_TYPE__
>(__v)
2566 : static_cast<__UINTPTR_TYPE__>(0);
2567 return __formatter_int<_CharT>(_M_spec).format(__u, __fc);
2571 [[__gnu__::__always_inline__]]
2573 _M_set_default(_Pres_type __type)
2575 if (_M_spec._M_type == _Pres_none && __type != _Pres_none)
2577 _M_spec._M_type = __type;
2578 _M_spec._M_alt = !_M_spec._M_alt;
2582 __format::_Spec<_CharT> _M_spec;
2589 template<__format::__
char _CharT>
2590 struct formatter<_CharT, _CharT>
2592 formatter() =
default;
2594 constexpr typename basic_format_parse_context<_CharT>::iterator
2595 parse(basic_format_parse_context<_CharT>& __pc)
2597 return _M_f.template _M_parse<_CharT>(__pc);
2600 template<
typename _Out>
2601 _GLIBCXX_CONSTEXPR_FORMAT
2602 typename basic_format_context<_Out, _CharT>::iterator
2603 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2605 if (_M_f._M_spec._M_type == __format::_Pres_c)
2606 return _M_f._M_format_character(__u, __fc);
2608 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2611#if __glibcxx_format_ranges
2613 set_debug_format() noexcept
2614 { _M_f._M_spec._M_debug =
true; }
2618 __format::__formatter_int<_CharT> _M_f;
2621#if __glibcxx_print >= 202403L
2622 template<__format::__
char _CharT>
2623 constexpr bool enable_nonlocking_formatter_optimization<_CharT> =
true;
2626#ifdef _GLIBCXX_USE_WCHAR_T
2629 struct formatter<char, wchar_t>
2631 formatter() =
default;
2633 constexpr typename basic_format_parse_context<wchar_t>::iterator
2634 parse(basic_format_parse_context<wchar_t>& __pc)
2636 return _M_f._M_parse<
char>(__pc);
2639 template<
typename _Out>
2640 _GLIBCXX_CONSTEXPR_FORMAT
2641 typename basic_format_context<_Out, wchar_t>::iterator
2642 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2644 if (_M_f._M_spec._M_type == __format::_Pres_c)
2645 return _M_f._M_format_character(__u, __fc);
2647 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2650#if __glibcxx_format_ranges
2652 set_debug_format() noexcept
2653 { _M_f._M_spec._M_debug =
true; }
2657 __format::__formatter_int<wchar_t> _M_f;
2664 template<__format::__
char _CharT>
2665 struct formatter<_CharT*, _CharT>
2667 formatter() =
default;
2669 [[__gnu__::__always_inline__]]
2670 constexpr typename basic_format_parse_context<_CharT>::iterator
2671 parse(basic_format_parse_context<_CharT>& __pc)
2672 {
return _M_f.parse(__pc); }
2674 template<
typename _Out>
2675 [[__gnu__::__nonnull__]]
2676 _GLIBCXX_CONSTEXPR_FORMAT
2677 typename basic_format_context<_Out, _CharT>::iterator
2678 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2679 {
return _M_f.format(__u, __fc); }
2681#if __glibcxx_format_ranges
2682 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2686 __format::__formatter_str<_CharT> _M_f;
2689#if __glibcxx_print >= 202403L
2690 template<__format::__
char _CharT>
2691 constexpr bool enable_nonlocking_formatter_optimization<_CharT*> =
true;
2694 template<__format::__
char _CharT>
2695 struct formatter<const _CharT*, _CharT>
2697 formatter() =
default;
2699 [[__gnu__::__always_inline__]]
2700 constexpr typename basic_format_parse_context<_CharT>::iterator
2701 parse(basic_format_parse_context<_CharT>& __pc)
2702 {
return _M_f.parse(__pc); }
2704 template<
typename _Out>
2705 [[__gnu__::__nonnull__]]
2706 _GLIBCXX_CONSTEXPR_FORMAT
2707 typename basic_format_context<_Out, _CharT>::iterator
2708 format(
const _CharT* __u,
2709 basic_format_context<_Out, _CharT>& __fc)
const
2710 {
return _M_f.format(__u, __fc); }
2712#if __glibcxx_format_ranges
2713 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2717 __format::__formatter_str<_CharT> _M_f;
2720#if __glibcxx_print >= 202403L
2721 template<__format::__
char _CharT>
2723 enable_nonlocking_formatter_optimization<const _CharT*> =
true;
2726 template<__format::__
char _CharT,
size_t _Nm>
2727 struct formatter<_CharT[_Nm], _CharT>
2729 formatter() =
default;
2731 [[__gnu__::__always_inline__]]
2732 constexpr typename basic_format_parse_context<_CharT>::iterator
2733 parse(basic_format_parse_context<_CharT>& __pc)
2734 {
return _M_f.parse(__pc); }
2736 template<
typename _Out>
2737 _GLIBCXX_CONSTEXPR_FORMAT
2738 typename basic_format_context<_Out, _CharT>::iterator
2739 format(
const _CharT (&__u)[_Nm],
2740 basic_format_context<_Out, _CharT>& __fc)
const
2741 {
return _M_f.format({__u, _Nm}, __fc); }
2743#if __glibcxx_format_ranges
2744 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2748 __format::__formatter_str<_CharT> _M_f;
2751#if __glibcxx_print >= 202403L
2752 template<__format::__
char _CharT,
size_t _Nm>
2753 constexpr bool enable_nonlocking_formatter_optimization<_CharT[_Nm]> =
true;
2756 template<
typename _Traits,
typename _Alloc>
2757 struct formatter<
basic_string<char, _Traits, _Alloc>, char>
2759 formatter() =
default;
2761 [[__gnu__::__always_inline__]]
2762 constexpr typename basic_format_parse_context<char>::iterator
2763 parse(basic_format_parse_context<char>& __pc)
2764 {
return _M_f.parse(__pc); }
2766 template<
typename _Out>
2767 _GLIBCXX_CONSTEXPR_FORMAT
2768 typename basic_format_context<_Out, char>::iterator
2769 format(
const basic_string<char, _Traits, _Alloc>& __u,
2770 basic_format_context<_Out, char>& __fc)
const
2771 {
return _M_f.format(__u, __fc); }
2773#if __glibcxx_format_ranges
2774 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2778 __format::__formatter_str<char> _M_f;
2781#if __glibcxx_print >= 202403L
2782 template<
typename _Tr,
typename _Alloc>
2784 enable_nonlocking_formatter_optimization<basic_string<char, _Tr, _Alloc>>
2788#ifdef _GLIBCXX_USE_WCHAR_T
2789 template<
typename _Traits,
typename _Alloc>
2790 struct formatter<
basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2792 formatter() =
default;
2794 [[__gnu__::__always_inline__]]
2795 constexpr typename basic_format_parse_context<wchar_t>::iterator
2796 parse(basic_format_parse_context<wchar_t>& __pc)
2797 {
return _M_f.parse(__pc); }
2799 template<
typename _Out>
2800 _GLIBCXX_CONSTEXPR_FORMAT
2801 typename basic_format_context<_Out, wchar_t>::iterator
2802 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2803 basic_format_context<_Out, wchar_t>& __fc)
const
2804 {
return _M_f.format(__u, __fc); }
2806#if __glibcxx_format_ranges
2807 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2811 __format::__formatter_str<wchar_t> _M_f;
2814#if __glibcxx_print >= 202403L
2815 template<
typename _Tr,
typename _Alloc>
2817 enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Tr, _Alloc>>
2823 template<
typename _Traits>
2826 formatter() =
default;
2828 [[__gnu__::__always_inline__]]
2829 constexpr typename basic_format_parse_context<char>::iterator
2830 parse(basic_format_parse_context<char>& __pc)
2831 {
return _M_f.parse(__pc); }
2833 template<
typename _Out>
2834 _GLIBCXX_CONSTEXPR_FORMAT
2835 typename basic_format_context<_Out, char>::iterator
2836 format(basic_string_view<char, _Traits> __u,
2837 basic_format_context<_Out, char>& __fc)
const
2838 {
return _M_f.format(__u, __fc); }
2840#if __glibcxx_format_ranges
2841 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2845 __format::__formatter_str<char> _M_f;
2848#if __glibcxx_print >= 202403L
2849 template<
typename _Tr>
2851 enable_nonlocking_formatter_optimization<basic_string_view<char, _Tr>>
2855#ifdef _GLIBCXX_USE_WCHAR_T
2856 template<
typename _Traits>
2859 formatter() =
default;
2861 [[__gnu__::__always_inline__]]
2862 constexpr typename basic_format_parse_context<wchar_t>::iterator
2863 parse(basic_format_parse_context<wchar_t>& __pc)
2864 {
return _M_f.parse(__pc); }
2866 template<
typename _Out>
2867 _GLIBCXX_CONSTEXPR_FORMAT
2868 typename basic_format_context<_Out, wchar_t>::iterator
2869 format(basic_string_view<wchar_t, _Traits> __u,
2870 basic_format_context<_Out, wchar_t>& __fc)
const
2871 {
return _M_f.format(__u, __fc); }
2873#if __glibcxx_format_ranges
2874 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2878 __format::__formatter_str<wchar_t> _M_f;
2881#if __glibcxx_print >= 202403L
2882 template<
typename _Tr>
2884 enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Tr>>
2895 template<
typename _Tp>
2896 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2898#if defined __SIZEOF_INT128__
2899 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2900 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2904 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2905 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2906#ifdef _GLIBCXX_USE_CHAR8_T
2907 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2909 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2910 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2912 template<
typename _Tp>
2913 concept __formattable_integer = __is_formattable_integer<_Tp>;
2918 template<__format::__formattable_
integer _Tp, __format::__
char _CharT>
2919 struct formatter<_Tp, _CharT>
2921 formatter() =
default;
2923 [[__gnu__::__always_inline__]]
2924 constexpr typename basic_format_parse_context<_CharT>::iterator
2925 parse(basic_format_parse_context<_CharT>& __pc)
2927 return _M_f.template _M_parse<_Tp>(__pc);
2930 template<
typename _Out>
2931 _GLIBCXX_CONSTEXPR_FORMAT
2932 typename basic_format_context<_Out, _CharT>::iterator
2933 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2934 {
return _M_f.format(__u, __fc); }
2937 __format::__formatter_int<_CharT> _M_f;
2940#if __glibcxx_print >= 202403L
2941 template<__format::__formattable_
integer _Tp>
2943 enable_nonlocking_formatter_optimization<_Tp> =
true;
2946#if defined __glibcxx_to_chars
2948 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2949 struct formatter<_Tp, _CharT>
2951 formatter() =
default;
2953 [[__gnu__::__always_inline__]]
2954 constexpr typename basic_format_parse_context<_CharT>::iterator
2955 parse(basic_format_parse_context<_CharT>& __pc)
2956 {
return _M_f.parse(__pc); }
2958 template<
typename _Out>
2959 typename basic_format_context<_Out, _CharT>::iterator
2960 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2961 {
return _M_f.format(__u, __fc); }
2964 __format::__formatter_fp<_CharT> _M_f;
2967#if __glibcxx_print >= 202403L
2968 template<__format::__formattable_
float _Tp>
2970 enable_nonlocking_formatter_optimization<_Tp> =
true;
2973#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2975 template<__format::__
char _CharT>
2976 struct formatter<long double, _CharT>
2978 formatter() =
default;
2980 [[__gnu__::__always_inline__]]
2981 constexpr typename basic_format_parse_context<_CharT>::iterator
2982 parse(basic_format_parse_context<_CharT>& __pc)
2983 {
return _M_f.parse(__pc); }
2985 template<
typename _Out>
2986 typename basic_format_context<_Out, _CharT>::iterator
2987 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2988 {
return _M_f.format((
double)__u, __fc); }
2991 __format::__formatter_fp<_CharT> _M_f;
2995#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2997 template<__format::__
char _CharT>
2998 struct formatter<_Float16, _CharT>
3000 formatter() =
default;
3002 [[__gnu__::__always_inline__]]
3003 constexpr typename basic_format_parse_context<_CharT>::iterator
3004 parse(basic_format_parse_context<_CharT>& __pc)
3005 {
return _M_f.parse(__pc); }
3007 template<
typename _Out>
3008 typename basic_format_context<_Out, _CharT>::iterator
3009 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
3010 {
return _M_f.format((
float)__u, __fc); }
3013 __format::__formatter_fp<_CharT> _M_f;
3017#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3019 template<__format::__
char _CharT>
3020 struct formatter<_Float32, _CharT>
3022 formatter() =
default;
3024 [[__gnu__::__always_inline__]]
3025 constexpr typename basic_format_parse_context<_CharT>::iterator
3026 parse(basic_format_parse_context<_CharT>& __pc)
3027 {
return _M_f.parse(__pc); }
3029 template<
typename _Out>
3030 typename basic_format_context<_Out, _CharT>::iterator
3031 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
3032 {
return _M_f.format((
float)__u, __fc); }
3035 __format::__formatter_fp<_CharT> _M_f;
3039#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3041 template<__format::__
char _CharT>
3042 struct formatter<_Float64, _CharT>
3044 formatter() =
default;
3046 [[__gnu__::__always_inline__]]
3047 constexpr typename basic_format_parse_context<_CharT>::iterator
3048 parse(basic_format_parse_context<_CharT>& __pc)
3049 {
return _M_f.parse(__pc); }
3051 template<
typename _Out>
3052 typename basic_format_context<_Out, _CharT>::iterator
3053 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
3054 {
return _M_f.format((
double)__u, __fc); }
3057 __format::__formatter_fp<_CharT> _M_f;
3061#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128
3063 template<__format::__
char _CharT>
3064 struct formatter<_Float128, _CharT>
3066 formatter() =
default;
3068 [[__gnu__::__always_inline__]]
3069 constexpr typename basic_format_parse_context<_CharT>::iterator
3070 parse(basic_format_parse_context<_CharT>& __pc)
3071 {
return _M_f.parse(__pc); }
3073 template<
typename _Out>
3074 typename basic_format_context<_Out, _CharT>::iterator
3075 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3076 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3079 __format::__formatter_fp<_CharT> _M_f;
3083#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128 == 2
3086 template<__format::__
char _CharT>
3087 struct formatter<__float128, _CharT>
3089 formatter() =
default;
3091 [[__gnu__::__always_inline__]]
3092 constexpr typename basic_format_parse_context<_CharT>::iterator
3093 parse(basic_format_parse_context<_CharT>& __pc)
3094 {
return _M_f.parse(__pc); }
3096 template<
typename _Out>
3097 typename basic_format_context<_Out, _CharT>::iterator
3098 format(__float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3099 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3102 __format::__formatter_fp<_CharT> _M_f;
3106#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3108 template<__format::__
char _CharT>
3109 struct formatter<__format::__bflt16_t, _CharT>
3111 formatter() =
default;
3113 [[__gnu__::__always_inline__]]
3114 constexpr typename basic_format_parse_context<_CharT>::iterator
3115 parse(basic_format_parse_context<_CharT>& __pc)
3116 {
return _M_f.parse(__pc); }
3118 template<
typename _Out>
3119 typename basic_format_context<_Out, _CharT>::iterator
3120 format(__gnu_cxx::__bfloat16_t __u,
3121 basic_format_context<_Out, _CharT>& __fc)
const
3122 {
return _M_f.format((
float)__u, __fc); }
3125 __format::__formatter_fp<_CharT> _M_f;
3133 template<__format::__
char _CharT>
3134 struct formatter<const void*, _CharT>
3136 formatter() =
default;
3138 constexpr typename basic_format_parse_context<_CharT>::iterator
3139 parse(basic_format_parse_context<_CharT>& __pc)
3140 {
return _M_f.parse(__pc); }
3142 template<
typename _Out>
3143 _GLIBCXX_CONSTEXPR_FORMAT
3144 typename basic_format_context<_Out, _CharT>::iterator
3145 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3146 {
return _M_f.format(__v, __fc); }
3149 __format::__formatter_ptr<_CharT> _M_f;
3152#if __glibcxx_print >= 202403L
3154 inline constexpr bool
3155 enable_nonlocking_formatter_optimization<const void*> =
true;
3158 template<__format::__
char _CharT>
3159 struct formatter<void*, _CharT>
3161 formatter() =
default;
3163 [[__gnu__::__always_inline__]]
3164 constexpr typename basic_format_parse_context<_CharT>::iterator
3165 parse(basic_format_parse_context<_CharT>& __pc)
3166 {
return _M_f.parse(__pc); }
3168 template<
typename _Out>
3169 _GLIBCXX_CONSTEXPR_FORMAT
3170 typename basic_format_context<_Out, _CharT>::iterator
3171 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3172 {
return _M_f.format(__v, __fc); }
3175 __format::__formatter_ptr<_CharT> _M_f;
3178#if __glibcxx_print >= 202403l
3180 inline constexpr bool
3181 enable_nonlocking_formatter_optimization<void*> =
true;
3184 template<__format::__
char _CharT>
3185 struct formatter<nullptr_t, _CharT>
3187 formatter() =
default;
3189 [[__gnu__::__always_inline__]]
3190 constexpr typename basic_format_parse_context<_CharT>::iterator
3191 parse(basic_format_parse_context<_CharT>& __pc)
3192 {
return _M_f.parse(__pc); }
3194 template<
typename _Out>
3195 _GLIBCXX_CONSTEXPR_FORMAT
3196 typename basic_format_context<_Out, _CharT>::iterator
3197 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3198 {
return _M_f.format(
nullptr, __fc); }
3201 __format::__formatter_ptr<_CharT> _M_f;
3205#if __glibcxx_print >= 202403L
3207 inline constexpr bool
3208 enable_nonlocking_formatter_optimization<nullptr_t> =
true;
3211#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3215 struct __formatter_disabled
3217 __formatter_disabled() =
delete;
3218 __formatter_disabled(
const __formatter_disabled&) =
delete;
3219 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3223 struct formatter<char*,
wchar_t>
3224 :
private __formatter_disabled { };
3226 struct formatter<const char*,
wchar_t>
3227 :
private __formatter_disabled { };
3228 template<
size_t _Nm>
3229 struct formatter<char[_Nm], wchar_t>
3230 :
private __formatter_disabled { };
3231 template<
class _Traits,
class _Allocator>
3232 struct formatter<
basic_string<char, _Traits, _Allocator>, wchar_t>
3233 :
private __formatter_disabled { };
3234 template<
class _Traits>
3236 :
private __formatter_disabled { };
3241 template<
typename _Out>
3242 struct format_to_n_result
3245 iter_difference_t<_Out> size;
3248_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3249template<
typename,
typename>
class vector;
3250_GLIBCXX_END_NAMESPACE_CONTAINER
3255 template<
typename _CharT>
3259 using iterator_category = output_iterator_tag;
3260 using value_type = void;
3261 using difference_type = ptrdiff_t;
3262 using pointer = void;
3263 using reference = void;
3265 _Drop_iter() =
default;
3266 _Drop_iter(
const _Drop_iter&) =
default;
3267 _Drop_iter& operator=(
const _Drop_iter&) =
default;
3269 [[__gnu__::__always_inline__]]
3270 constexpr _Drop_iter&
3271 operator=(_CharT __c)
3274 [[__gnu__::__always_inline__]]
3275 constexpr _Drop_iter&
3276 operator=(basic_string_view<_CharT> __s)
3279 [[__gnu__::__always_inline__]]
3280 constexpr _Drop_iter&
3283 [[__gnu__::__always_inline__]]
3284 constexpr _Drop_iter&
3285 operator++() {
return *
this; }
3287 [[__gnu__::__always_inline__]]
3288 constexpr _Drop_iter
3289 operator++(
int) {
return *
this; }
3292 template<
typename _CharT>
3295 _Sink<_CharT>* _M_sink =
nullptr;
3298 using iterator_category = output_iterator_tag;
3299 using value_type = void;
3300 using difference_type = ptrdiff_t;
3301 using pointer = void;
3302 using reference = void;
3304 _Sink_iter() =
default;
3305 _Sink_iter(
const _Sink_iter&) =
default;
3306 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3308 [[__gnu__::__always_inline__]]
3310 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3312 [[__gnu__::__always_inline__]]
3313 constexpr _Sink_iter&
3314 operator=(_CharT __c)
3316 _M_sink->_M_write(__c);
3320 [[__gnu__::__always_inline__]]
3321 constexpr _Sink_iter&
3322 operator=(basic_string_view<_CharT> __s)
3324 _M_sink->_M_write(__s);
3328 [[__gnu__::__always_inline__]]
3329 constexpr _Sink_iter&
3332 [[__gnu__::__always_inline__]]
3333 constexpr _Sink_iter&
3334 operator++() {
return *
this; }
3336 [[__gnu__::__always_inline__]]
3337 constexpr _Sink_iter
3338 operator++(
int) {
return *
this; }
3340 _GLIBCXX_CONSTEXPR_FORMAT
auto
3341 _M_reserve(
size_t __n)
const
3342 {
return _M_sink->_M_reserve(__n); }
3344 _GLIBCXX_CONSTEXPR_FORMAT
bool
3345 _M_discarding()
const
3346 {
return _M_sink->_M_discarding(); }
3352 template<
typename _CharT>
3355 friend class _Sink_iter<_CharT>;
3357 span<_CharT> _M_span;
3358 typename span<_CharT>::iterator _M_next;
3364 virtual void _M_overflow() = 0;
3368 [[__gnu__::__always_inline__]]
3370 _Sink(span<_CharT> __span) noexcept
3371 : _M_span(__span), _M_next(__span.begin())
3375 [[__gnu__::__always_inline__]]
3376 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3377 _M_used() const noexcept
3378 {
return _M_span.first(_M_next - _M_span.begin()); }
3381 [[__gnu__::__always_inline__]]
3382 constexpr span<_CharT>
3383 _M_unused() const noexcept
3384 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3387 [[__gnu__::__always_inline__]]
3389 _M_rewind() noexcept
3390 { _M_next = _M_span.begin(); }
3393 _GLIBCXX_CONSTEXPR_FORMAT
void
3394 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3397 _M_next = __s.begin() + __pos;
3402 _M_write(_CharT __c)
3405 if (_M_next - _M_span.begin() ==
std::ssize(_M_span)) [[unlikely]]
3410 _M_write(basic_string_view<_CharT> __s)
3412 span __to = _M_unused();
3413 while (__to.size() <= __s.size())
3415 __s.copy(__to.data(), __to.size());
3416 _M_next += __to.size();
3417 __s.remove_prefix(__to.size());
3423 __s.copy(__to.data(), __s.size());
3424 _M_next += __s.size();
3433 _GLIBCXX_CONSTEXPR_FORMAT
3434 explicit operator bool() const noexcept {
return _M_sink; }
3436 _GLIBCXX_CONSTEXPR_FORMAT _CharT*
3437 get() const noexcept {
return _M_sink->_M_next.operator->(); }
3439 _GLIBCXX_CONSTEXPR_FORMAT
void
3440 _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3448 _GLIBCXX_CONSTEXPR_FORMAT
virtual _Reservation
3449 _M_reserve(
size_t __n)
3451 if (__n <= _M_unused().size())
3454 if (__n <= _M_span.size())
3457 if (__n <= _M_unused().size())
3465 _GLIBCXX_CONSTEXPR_FORMAT
virtual void
3470 _GLIBCXX_CONSTEXPR_FORMAT
virtual bool
3471 _M_discarding()
const
3475 _Sink(
const _Sink&) =
delete;
3476 _Sink& operator=(
const _Sink&) =
delete;
3478 [[__gnu__::__always_inline__]]
3479 constexpr _Sink_iter<_CharT>
3481 {
return _Sink_iter<_CharT>(*
this); }
3485 template<
typename _CharT>
3486 class _Fixedbuf_sink final :
public _Sink<_CharT>
3488 _GLIBCXX_CONSTEXPR_FORMAT
void
3489 _M_overflow()
override
3491 __glibcxx_assert(
false);
3496 [[__gnu__::__always_inline__]]
3498 _Fixedbuf_sink(span<_CharT> __buf)
3499 : _Sink<_CharT>(__buf)
3502 constexpr basic_string_view<_CharT>
3505 auto __s = this->_M_used();
3506 return basic_string_view<_CharT>(__s.data(), __s.size());
3511 template<
typename _CharT>
3512 class _Buf_sink :
public _Sink<_CharT>
3515 _CharT _M_buf[__stackbuf_size<_CharT>];
3517 [[__gnu__::__always_inline__]]
3519 _Buf_sink() noexcept
3520 : _Sink<_CharT>(_M_buf)
3524 using _GLIBCXX_STD_C::vector;
3528 template<
typename _Seq>
3529 class _Seq_sink :
public _Buf_sink<typename _Seq::value_type>
3531 using _CharT =
typename _Seq::value_type;
3536 _GLIBCXX_CONSTEXPR_FORMAT
void
3537 _M_overflow()
override
3539 auto __s = this->_M_used();
3540 if (__s.empty()) [[unlikely]]
3544 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3546 if constexpr (__is_specialization_of<_Seq, basic_string>)
3547 _M_seq.append(__s.data(), __s.size());
3549 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3555 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3556 _M_reserve(
size_t __n)
override
3565 if constexpr (__is_specialization_of<_Seq, basic_string>
3566 || __is_specialization_of<_Seq, vector>)
3569 if (this->_M_used().size()) [[unlikely]]
3570 _Seq_sink::_M_overflow();
3573 const auto __sz = _M_seq.size();
3574 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3575 _M_seq.__resize_and_overwrite(__sz + __n,
3576 [](
auto,
auto __n2) {
3580 _M_seq.resize(__sz + __n);
3584 this->_M_reset(_M_seq, __sz);
3588 return _Sink<_CharT>::_M_reserve(__n);
3591 _GLIBCXX_CONSTEXPR_FORMAT
void
3592 _M_bump(
size_t __n)
override
3594 if constexpr (__is_specialization_of<_Seq, basic_string>
3595 || __is_specialization_of<_Seq, vector>)
3597 auto __s = this->_M_used();
3598 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3600 _M_seq.resize(__s.size() + __n);
3602 this->_M_reset(this->_M_buf);
3606 _GLIBCXX_CONSTEXPR_FORMAT
void
3607 _M_trim(span<const _CharT> __s)
3608 requires __is_specialization_of<_Seq, basic_string>
3610 _GLIBCXX_DEBUG_ASSERT(__s.data() == this->_M_buf
3611 || __s.data() == _M_seq.data());
3612 if (__s.data() == _M_seq.data())
3613 _M_seq.resize(__s.size());
3615 this->_M_reset(this->_M_buf, __s.size());
3622 [[__gnu__::__always_inline__]]
3623 _GLIBCXX_CONSTEXPR_FORMAT
3624 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3627 _GLIBCXX_CONSTEXPR_FORMAT
3628 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3629 : _M_seq(std::move(__s))
3632 using _Sink<_CharT>::out;
3634 _GLIBCXX_CONSTEXPR_FORMAT _Seq
3637 if (this->_M_used().size() != 0)
3638 _Seq_sink::_M_overflow();
3644 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3647 auto __s = this->_M_used();
3650 if (__s.size() != 0)
3651 _Seq_sink::_M_overflow();
3657 _GLIBCXX_CONSTEXPR_FORMAT basic_string_view<_CharT>
3660 auto __span = _M_span();
3661 return basic_string_view<_CharT>(__span.data(), __span.size());
3665 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3667 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3673 template<
typename _CharT,
typename _OutIter>
3674 class _Iter_sink :
public _Buf_sink<_CharT>
3677 iter_difference_t<_OutIter> _M_max;
3680 size_t _M_count = 0;
3682 _GLIBCXX_CONSTEXPR_FORMAT
void
3683 _M_overflow()
override
3685 auto __s = this->_M_used();
3687 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3688 else if (_M_count <
static_cast<size_t>(_M_max))
3690 auto __max = _M_max - _M_count;
3691 span<_CharT> __first;
3692 if (__max < __s.size())
3693 __first = __s.first(
static_cast<size_t>(__max));
3696 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3699 _M_count += __s.size();
3702 _GLIBCXX_CONSTEXPR_FORMAT
bool
3703 _M_discarding()
const override
3711 [[__gnu__::__always_inline__]]
3712 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3713 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3714 : _M_out(std::move(__out)), _M_max(__max)
3717 using _Sink<_CharT>::out;
3719 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3722 if (this->_M_used().size() != 0)
3723 _Iter_sink::_M_overflow();
3724 iter_difference_t<_OutIter> __count(_M_count);
3736 template<
typename _CharT>
3737 class _Ptr_sink :
public _Sink<_CharT>
3739 static constexpr size_t _S_no_limit = size_t(-1);
3743 size_t _M_count = 0;
3748 _GLIBCXX_CONSTEXPR_FORMAT
void
3749 _M_overflow()
override
3751 if (this->_M_unused().size() != 0)
3754 auto __s = this->_M_used();
3756 if (_M_max != _S_no_limit)
3758 _M_count += __s.size();
3762 this->_M_reset(this->_M_buf);
3768 _M_rebuf(__s.data(), __s.size() + 1024, __s.size());
3772 _GLIBCXX_CONSTEXPR_FORMAT
bool
3773 _M_discarding()
const override
3780 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3781 _M_reserve(
size_t __n)
final
3783 auto __avail = this->_M_unused();
3784 if (__n > __avail.size())
3786 if (_M_max != _S_no_limit)
3789 auto __s = this->_M_used();
3790 _M_rebuf(__s.data(), __s.size() + __n, __s.size());
3796 template<
typename _IterDifference>
3797 static _GLIBCXX_CONSTEXPR_FORMAT
size_t
3798 _S_trim_max(_IterDifference __max)
3802 if constexpr (!is_integral_v<_IterDifference> ||
sizeof(__max) >
sizeof(size_t))
3804 if (_IterDifference((
size_t)-1) < __max)
3806 return size_t(__max);
3809 [[__gnu__::__always_inline__]]
3810 _GLIBCXX_CONSTEXPR_FORMAT
void
3811 _M_rebuf(_CharT* __ptr,
size_t __total,
size_t __inuse = 0)
3813 std::span<_CharT> __span(__ptr, __total);
3814 this->_M_reset(__span, __inuse);
3818 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3819 _Ptr_sink(_CharT* __ptr,
size_t __n = _S_no_limit) noexcept
3820 : _Sink<_CharT>(_M_buf), _M_max(__n)
3824 else if (__n != _S_no_limit)
3825 _M_rebuf(__ptr, __n);
3826#if __has_builtin(__builtin_dynamic_object_size)
3827 else if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3828 _M_rebuf(__ptr, __bytes /
sizeof(_CharT));
3833 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3834 __n = (1024 - __off) /
sizeof(_CharT);
3835 if (__n > 0) [[likely]]
3836 _M_rebuf(__ptr, __n);
3842 template<contiguous_iterator _OutIter>
3843 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3844 _Ptr_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1)
3845 : _Ptr_sink(std::
to_address(__out), _S_trim_max(__n))
3848 template<contiguous_iterator _OutIter>
3849 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3850 _M_finish(_OutIter __first)
const
3852 auto __s = this->_M_used();
3853 if (__s.data() == _M_buf)
3856 iter_difference_t<_OutIter> __m(_M_max);
3857 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3858 return { __first + __m, __count };
3862 iter_difference_t<_OutIter> __count(__s.size());
3863 return { __first + __count, __count };
3868 template<
typename _CharT,
typename _OutIter>
3869 concept __contiguous_char_iter
3870 = contiguous_iterator<_OutIter>
3871 && same_as<iter_value_t<_OutIter>, _CharT>;
3882 template<
typename _Out,
typename _CharT>
3883 class _Padding_sink :
public _Str_sink<_CharT>
3888 size_t _M_printwidth;
3890 [[__gnu__::__always_inline__]]
3891 _GLIBCXX_CONSTEXPR_FORMAT
bool
3893 {
return _M_printwidth >= _M_maxwidth; }
3895 [[__gnu__::__always_inline__]]
3896 _GLIBCXX_CONSTEXPR_FORMAT
bool
3897 _M_buffering()
const
3899 if (_M_printwidth < _M_padwidth)
3901 if (_M_maxwidth != (
size_t)-1)
3902 return _M_printwidth < _M_maxwidth;
3906 _GLIBCXX_CONSTEXPR_FORMAT
void
3907 _M_sync_discarding()
3909 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3910 if (_M_out._M_discarding())
3911 _M_maxwidth = _M_printwidth;
3914 _GLIBCXX_CONSTEXPR_FORMAT
void
3917 span<_CharT> __new = this->_M_used();
3918 basic_string_view<_CharT> __str(__new.data(), __new.size());
3919 _M_out = __format::__write(
std::move(_M_out), __str);
3920 _M_sync_discarding();
3924 _GLIBCXX_CONSTEXPR_FORMAT
bool
3927 auto __str = this->view();
3929 _M_printwidth = __format::__truncate(__str, _M_maxwidth);
3931 this->_M_trim(__str);
3937 if (_M_printwidth >= _M_padwidth)
3939 _M_out = __format::__write(
std::move(_M_out), __str);
3940 _M_sync_discarding();
3945 _Str_sink<_CharT>::_M_overflow();
3948 this->_M_reset(this->_M_buf);
3952 _GLIBCXX_CONSTEXPR_FORMAT
bool
3953 _M_update(
size_t __new)
3955 _M_printwidth += __new;
3957 if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
3958 return _M_force_update();
3962 _GLIBCXX_CONSTEXPR_FORMAT
void
3963 _M_overflow()
override
3969 else if (!_M_buffering())
3973 else if (_M_update(this->_M_used().size()))
3974 _Str_sink<_CharT>::_M_overflow();
3977 _GLIBCXX_CONSTEXPR_FORMAT
bool
3978 _M_discarding()
const override
3979 {
return _M_ignoring(); }
3981 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3982 _M_reserve(
size_t __n)
override
3987 else if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3988 if (!_M_buffering())
3991 if (!this->_M_used().empty())
3994 if (
auto __reserved = _M_out._M_reserve(__n))
3997 return _Sink<_CharT>::_M_reserve(__n);
4000 _GLIBCXX_CONSTEXPR_FORMAT
void
4001 _M_bump(
size_t __n)
override
4008 _Sink<_CharT>::_M_bump(__n);
4014 [[__gnu__::__always_inline__]]
4015 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4016 _Padding_sink(_Out __out,
size_t __padwidth,
size_t __maxwidth)
4017 : _M_padwidth(__padwidth), _M_maxwidth(__maxwidth),
4018 _M_out(std::move(__out)), _M_printwidth(0)
4019 { _M_sync_discarding(); }
4021 [[__gnu__::__always_inline__]]
4022 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4023 _Padding_sink(_Out __out,
size_t __padwidth)
4024 : _Padding_sink(std::move(__out), __padwidth, (size_t)-1)
4027 _GLIBCXX_CONSTEXPR_FORMAT _Out
4028 _M_finish(_Align __align,
char32_t __fill_char)
4031 if (
auto __rem = this->_M_used().size())
4035 else if (!_M_buffering())
4041 if (!_M_buffering() || !_M_force_update())
4043 if (_M_printwidth >= _M_padwidth)
4046 const auto __str = this->view();
4047 if (_M_printwidth >= _M_padwidth)
4048 return __format::__write(
std::move(_M_out), __str);
4050 const size_t __nfill = _M_padwidth - _M_printwidth;
4051 return __format::__write_padded(
std::move(_M_out), __str,
4052 __align, __nfill, __fill_char);
4056 template<
typename _Out,
typename _CharT>
4057 class _Escaping_sink :
public _Buf_sink<_CharT>
4059 using _Esc = _Escapes<_CharT>;
4062 _Term_char _M_term : 2;
4063 unsigned _M_prev_escape : 1;
4064 unsigned _M_out_discards : 1;
4066 _GLIBCXX_CONSTEXPR_FORMAT
void
4067 _M_sync_discarding()
4069 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4070 _M_out_discards = _M_out._M_discarding();
4073 _GLIBCXX_CONSTEXPR_FORMAT
void
4076 span<_CharT> __bytes = this->_M_used();
4077 basic_string_view<_CharT> __str(__bytes.data(), __bytes.size());
4080 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4082 bool __prev_escape = _M_prev_escape;
4083 _M_out = __format::__write_escaped_unicode_part(
4084 std::move(_M_out), __str, __prev_escape, _M_term);
4085 _M_prev_escape = __prev_escape;
4087 __rem = __str.size();
4088 if (__rem > 0 && __str.data() != this->_M_buf) [[unlikely]]
4089 ranges::move(__str, this->_M_buf);
4092 _M_out = __format::__write_escaped_ascii(
4095 this->_M_reset(this->_M_buf, __rem);
4096 _M_sync_discarding();
4099 _GLIBCXX_CONSTEXPR_FORMAT
void
4100 _M_overflow()
override
4102 if (_M_out_discards)
4108 _GLIBCXX_CONSTEXPR_FORMAT
bool
4109 _M_discarding()
const override
4110 {
return _M_out_discards; }
4113 [[__gnu__::__always_inline__]]
4114 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4115 _Escaping_sink(_Out __out, _Term_char __term)
4116 : _M_out(std::move(__out)), _M_term(__term),
4117 _M_prev_escape(true), _M_out_discards(false)
4119 _M_out = __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4120 _M_sync_discarding();
4123 _GLIBCXX_CONSTEXPR_FORMAT _Out
4126 if (_M_out_discards)
4129 if (!this->_M_used().empty())
4132 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4133 if (
auto __rem = this->_M_used(); !__rem.empty())
4135 basic_string_view<_CharT> __str(__rem.data(), __rem.size());
4136 _M_out = __format::__write_escape_seqs(
std::move(_M_out), __str);
4139 return __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4143 enum class _Arg_t :
unsigned char {
4144 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
4145 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
4146 _Arg_i128, _Arg_u128, _Arg_float128,
4147 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
4150#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4151 _Arg_ibm128 = _Arg_ldbl,
4152 _Arg_ieee128 = _Arg_float128,
4157 template<
typename _Context>
4160 using _CharT =
typename _Context::char_type;
4164 using _CharT =
typename _Context::char_type;
4165 using _Func = void(*)(basic_format_parse_context<_CharT>&,
4166 _Context&,
const void*);
4169 template<
typename _Tp>
4170 using __maybe_const_t
4171 = __conditional_t<__formattable_with<const _Tp, _Context>,
4174 template<
typename _Tq>
4175 static _GLIBCXX_CONSTEXPR_FORMAT
void
4176 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
4177 _Context& __format_ctx,
const void* __ptr)
4179 using _Td = remove_const_t<_Tq>;
4180 typename _Context::template formatter_type<_Td> __f;
4181 __parse_ctx.advance_to(__f.parse(__parse_ctx));
4182 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
4183 __format_ctx.advance_to(__f.format(__val, __format_ctx));
4186 template<
typename _Tp>
4187 requires (!is_same_v<remove_cv_t<_Tp>, handle>)
4188 explicit _GLIBCXX_CONSTEXPR_FORMAT
4189 handle(_Tp& __val) noexcept
4190 : _M_ptr(__builtin_addressof(__val))
4191 , _M_func(&_S_format<__maybe_const_t<_Tp>>)
4194 friend class basic_format_arg<_Context>;
4197 handle(
const handle&) =
default;
4198 handle& operator=(
const handle&) =
default;
4200 [[__gnu__::__always_inline__]]
4201 void _GLIBCXX_CONSTEXPR_FORMAT
4202 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
4203 { _M_func(__pc, __fc, this->_M_ptr); }
4218 unsigned long long _M_ull;
4221#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4222 long double _M_ldbl;
4225 __ieee128 _M_ieee128;
4227#ifdef __SIZEOF_FLOAT128__
4228 __float128 _M_float128;
4230 const _CharT* _M_str;
4231 basic_string_view<_CharT> _M_sv;
4234#ifdef __SIZEOF_INT128__
4236 unsigned __int128 _M_u128;
4238#ifdef __BFLT16_DIG__
4252 [[__gnu__::__always_inline__]]
4253 _GLIBCXX_CONSTEXPR_FORMAT
4254 _Arg_value() : _M_none() { }
4257 template<
typename _Tp>
4258 _GLIBCXX_CONSTEXPR_FORMAT
4259 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
4260 { _S_get<_Tp>() = __val; }
4265 template<
typename _Tp,
typename _Self,
typename... _Value>
4266 [[__gnu__::__always_inline__]]
4267 static _GLIBCXX_CONSTEXPR_FORMAT
auto&
4268 _S_access(_Self& __u, _Value... __value)
noexcept
4270 static_assert(
sizeof...(_Value) <= 1);
4271 if constexpr (is_same_v<_Tp, bool>)
4272 return (__u._M_bool = ... = __value);
4273 else if constexpr (is_same_v<_Tp, _CharT>)
4274 return (__u._M_c = ... = __value);
4275 else if constexpr (is_same_v<_Tp, int>)
4276 return (__u._M_i = ... = __value);
4277 else if constexpr (is_same_v<_Tp, unsigned>)
4278 return (__u._M_u = ... = __value);
4279 else if constexpr (is_same_v<_Tp, long long>)
4280 return (__u._M_ll = ... = __value);
4281 else if constexpr (is_same_v<_Tp, unsigned long long>)
4282 return (__u._M_ull = ... = __value);
4283 else if constexpr (is_same_v<_Tp, float>)
4284 return (__u._M_flt = ... = __value);
4285 else if constexpr (is_same_v<_Tp, double>)
4286 return (__u._M_dbl = ... = __value);
4287#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4288 else if constexpr (is_same_v<_Tp, long double>)
4289 return (__u._M_ldbl = ... = __value);
4291 else if constexpr (is_same_v<_Tp, __ibm128>)
4292 return (__u._M_ibm128 = ... = __value);
4293 else if constexpr (is_same_v<_Tp, __ieee128>)
4294 return (__u._M_ieee128 = ... = __value);
4296#ifdef __SIZEOF_FLOAT128__
4297 else if constexpr (is_same_v<_Tp, __float128>)
4298 return (__u._M_float128 = ... = __value);
4300 else if constexpr (is_same_v<_Tp, const _CharT*>)
4301 return (__u._M_str = ... = __value);
4302 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4303 return (__u._M_sv = ... = __value);
4304 else if constexpr (is_same_v<_Tp, const void*>)
4305 return (__u._M_ptr = ... = __value);
4306#ifdef __SIZEOF_INT128__
4307 else if constexpr (is_same_v<_Tp, __int128>)
4308 return (__u._M_i128 = ... = __value);
4309 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4310 return (__u._M_u128 = ... = __value);
4312#ifdef __BFLT16_DIG__
4313 else if constexpr (is_same_v<_Tp, __bflt16_t>)
4314 return (__u._M_bf16 = ... = __value);
4317 else if constexpr (is_same_v<_Tp, _Float16>)
4318 return (__u._M_f16 = ... = __value);
4321 else if constexpr (is_same_v<_Tp, _Float32>)
4322 return (__u._M_f32 = ... = __value);
4325 else if constexpr (is_same_v<_Tp, _Float64>)
4326 return (__u._M_f64 = ... = __value);
4328 else if constexpr (is_same_v<_Tp, handle>)
4329 return __u._M_handle;
4331 __builtin_unreachable();
4334 template<
typename _Tp>
4335 [[__gnu__::__always_inline__]]
4336 _GLIBCXX_CONSTEXPR_FORMAT
auto&
4338 {
return _S_access<_Tp>(*
this); }
4340 template<
typename _Tp>
4341 [[__gnu__::__always_inline__]]
4342 _GLIBCXX_CONSTEXPR_FORMAT
const auto&
4343 _M_get() const noexcept
4344 {
return _S_access<_Tp>(*
this); }
4346 template<
typename _Tp>
4347 [[__gnu__::__always_inline__]]
4348 _GLIBCXX_CONSTEXPR_FORMAT
void
4349 _M_set(_Tp __v)
noexcept
4352 if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4353 std::construct_at(&_M_sv, __v);
4354 else if constexpr (is_same_v<_Tp, handle>)
4355 std::construct_at(&_M_handle, __v);
4359 _S_access<_Tp>(*
this, __v);
4364 template<
typename _Context,
typename... _Args>
4367 template<
typename _Visitor,
typename _Ctx>
4368 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4369 __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4371 template<
typename _Ch,
typename _Tp>
4373 __to_arg_t_enum() noexcept;
4377 template<typename _Context>
4378 class basic_format_arg
4380 using _CharT =
typename _Context::char_type;
4383 using handle = __format::_Arg_value<_Context>::handle;
4385 [[__gnu__::__always_inline__]]
4386 _GLIBCXX_CONSTEXPR_FORMAT
4387 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
4389 [[nodiscard,__gnu__::__always_inline__]]
4390 explicit _GLIBCXX_CONSTEXPR_FORMAT
operator bool() const noexcept
4391 {
return _M_type != __format::_Arg_none; }
4393#if __cpp_lib_format >= 202306L
4394 template<
typename _Visitor>
4395 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4396 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4399 template<
typename _Res,
typename _Visitor>
4400 _GLIBCXX_CONSTEXPR_FORMAT _Res
4401 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4406 template<
typename _Ctx>
4407 friend class basic_format_args;
4409 template<
typename _Ctx,
typename... _Args>
4410 friend class __format::_Arg_store;
4412 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
4414 __format::_Arg_value<_Context> _M_val;
4415 __format::_Arg_t _M_type;
4420 template<
typename _Tp>
4421 static consteval auto
4424 using _Td = remove_const_t<_Tp>;
4425 if constexpr (is_same_v<_Td, bool>)
4426 return type_identity<bool>();
4427 else if constexpr (is_same_v<_Td, _CharT>)
4428 return type_identity<_CharT>();
4429 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
4430 return type_identity<_CharT>();
4431#ifdef __SIZEOF_INT128__
4432 else if constexpr (is_same_v<_Td, __int128>)
4433 return type_identity<__int128>();
4434 else if constexpr (is_same_v<_Td, unsigned __int128>)
4435 return type_identity<unsigned __int128>();
4437 else if constexpr (__is_signed_integer<_Td>::value)
4439 if constexpr (
sizeof(_Td) <=
sizeof(int))
4440 return type_identity<int>();
4441 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
4442 return type_identity<long long>();
4444 else if constexpr (__is_unsigned_integer<_Td>::value)
4446 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
4447 return type_identity<unsigned>();
4448 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
4449 return type_identity<unsigned long long>();
4451 else if constexpr (is_same_v<_Td, float>)
4452 return type_identity<float>();
4453 else if constexpr (is_same_v<_Td, double>)
4454 return type_identity<double>();
4455#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4456 else if constexpr (is_same_v<_Td, long double>)
4457 return type_identity<long double>();
4459 else if constexpr (is_same_v<_Td, __ibm128>)
4460 return type_identity<__ibm128>();
4461 else if constexpr (is_same_v<_Td, __ieee128>)
4462 return type_identity<__ieee128>();
4464#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4465 else if constexpr (is_same_v<_Td, __float128>)
4466 return type_identity<__float128>();
4468#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4469 else if constexpr (is_same_v<_Td, __format::__bflt16_t>)
4470 return type_identity<__format::__bflt16_t>();
4472#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4473 else if constexpr (is_same_v<_Td, _Float16>)
4474 return type_identity<_Float16>();
4476#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4477 else if constexpr (is_same_v<_Td, _Float32>)
4478 return type_identity<_Float32>();
4480#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4481 else if constexpr (is_same_v<_Td, _Float64>)
4482 return type_identity<_Float64>();
4484 else if constexpr (__is_specialization_of<_Td, basic_string_view>
4485 || __is_specialization_of<_Td, basic_string>)
4487 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
4488 return type_identity<basic_string_view<_CharT>>();
4490 return type_identity<handle>();
4492 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
4493 return type_identity<const _CharT*>();
4494 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
4495 return type_identity<const _CharT*>();
4496 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
4497 return type_identity<const void*>();
4498 else if constexpr (is_same_v<_Td, nullptr_t>)
4499 return type_identity<const void*>();
4501 return type_identity<handle>();
4505 template<
typename _Tp>
4506 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
4509 template<
typename _Tp>
4510 static consteval __format::_Arg_t
4513 using namespace __format;
4514 if constexpr (is_same_v<_Tp, bool>)
4516 else if constexpr (is_same_v<_Tp, _CharT>)
4518 else if constexpr (is_same_v<_Tp, int>)
4520 else if constexpr (is_same_v<_Tp, unsigned>)
4522 else if constexpr (is_same_v<_Tp, long long>)
4524 else if constexpr (is_same_v<_Tp, unsigned long long>)
4526 else if constexpr (is_same_v<_Tp, float>)
4528 else if constexpr (is_same_v<_Tp, double>)
4530#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4531 else if constexpr (is_same_v<_Tp, long double>)
4535 else if constexpr (is_same_v<_Tp, __ibm128>)
4537 else if constexpr (is_same_v<_Tp, __ieee128>)
4538 return _Arg_ieee128;
4540#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4541 else if constexpr (is_same_v<_Tp, __float128>)
4542 return _Arg_float128;
4544#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4545 else if constexpr (is_same_v<_Tp, __format::__bflt16_t>)
4548#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4549 else if constexpr (is_same_v<_Tp, _Float16>)
4552#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4553 else if constexpr (is_same_v<_Tp, _Float32>)
4556#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4557 else if constexpr (is_same_v<_Tp, _Float64>)
4560 else if constexpr (is_same_v<_Tp, const _CharT*>)
4562 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4564 else if constexpr (is_same_v<_Tp, const void*>)
4566#ifdef __SIZEOF_INT128__
4567 else if constexpr (is_same_v<_Tp, __int128>)
4569 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4572 else if constexpr (is_same_v<_Tp, handle>)
4576 template<
typename _Tp>
4577 _GLIBCXX_CONSTEXPR_FORMAT
void
4578 _M_set(_Tp __v)
noexcept
4580 _M_type = _S_to_enum<_Tp>();
4584 template<
typename _Tp>
4585 requires __format::__formattable_with<_Tp, _Context>
4586 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4587 basic_format_arg(_Tp& __v)
noexcept
4589 using _Td = _Normalize<_Tp>;
4590 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4591 _M_set(_Td{__v.data(), __v.size()});
4592 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4593 && is_same_v<_CharT, wchar_t>)
4594 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4596 _M_set(
static_cast<_Td
>(__v));
4599 template<
typename _Ctx,
typename... _Argz>
4600 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4601 make_format_args(_Argz&...)
noexcept;
4603 template<
typename _Visitor,
typename _Ctx>
4604 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4605 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4607 template<
typename _Visitor,
typename _Ctx>
4608 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4609 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4611 template<
typename _Ch,
typename _Tp>
4612 friend consteval __format::_Arg_t
4613 __format::__to_arg_t_enum() noexcept;
4615 template<typename _Visitor>
4616 _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4617 _M_visit(_Visitor&& __vis, __format::_Arg_t __type)
4619 using namespace __format;
4636#if __glibcxx_to_chars
4641#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4644#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4654#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4658#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4662#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4666#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4679#ifdef __SIZEOF_INT128__
4686 __builtin_unreachable();
4690 template<
typename _Visitor>
4691 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4692 _M_visit_user(_Visitor&& __vis, __format::_Arg_t __type)
4694 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4696 constexpr bool __user_facing = __is_one_of<_Tp,
4697 monostate, bool, _CharT,
4698 int,
unsigned int,
long long int,
unsigned long long int,
4699 float, double,
long double,
4700 const _CharT*, basic_string_view<_CharT>,
4701 const void*, handle>::value;
4702 if constexpr (__user_facing)
4713 template<
typename _Visitor,
typename _Context>
4714 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4715 inline _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4716 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4724 template<
typename _Visitor,
typename _Ctx>
4725 inline _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4726 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4731 struct _WidthPrecVisitor
4733 template<
typename _Tp>
4734 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4735 operator()(_Tp& __arg)
const
4737 if constexpr (is_same_v<_Tp, monostate>)
4738 __format::__invalid_arg_id_in_format_string();
4742 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4746 if constexpr (__is_unsigned_integer<_Tp>::value)
4748 else if constexpr (__is_signed_integer<_Tp>::value)
4752 __throw_format_error(
"format error: argument used for width or "
4753 "precision must be a non-negative integer");
4757#pragma GCC diagnostic push
4758#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4759 template<
typename _Context>
4760 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
4761 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4762 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4765 template<
int _Bits,
size_t _Nm>
4767 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4769 __UINT64_TYPE__ __packed_types = 0;
4770 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4771 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4772 return __packed_types;
4777 template<
typename _Context>
4778 class basic_format_args
4780 static constexpr int _S_packed_type_bits = 5;
4781 static constexpr int _S_packed_type_mask = 0b11111;
4782 static constexpr int _S_max_packed_args = 12;
4784 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4786 template<
typename... _Args>
4787 using _Store = __format::_Arg_store<_Context, _Args...>;
4789 template<
typename _Ctx,
typename... _Args>
4790 friend class __format::_Arg_store;
4792 using uint64_t = __UINT64_TYPE__;
4793 using _Format_arg = basic_format_arg<_Context>;
4794 using _Format_arg_val = __format::_Arg_value<_Context>;
4800 uint64_t _M_packed_size : 4;
4801 uint64_t _M_unpacked_size : 60;
4804 const _Format_arg_val* _M_values;
4805 const _Format_arg* _M_args;
4808 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4809 _M_size() const noexcept
4810 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4812 _GLIBCXX_CONSTEXPR_FORMAT
typename __format::_Arg_t
4813 _M_type(
size_t __i)
const noexcept
4815 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4816 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4819 template<
typename _Ctx,
typename... _Args>
4820 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4821 make_format_args(_Args&...)
noexcept;
4824 template<
typename... _Args>
4825 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4827 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4830 template<
typename... _Args>
4831 _GLIBCXX_CONSTEXPR_FORMAT
4832 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4834 [[nodiscard,__gnu__::__always_inline__]]
4835 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<_Context>
4836 get(
size_t __i)
const noexcept
4838 basic_format_arg<_Context> __arg;
4839 if (__i < _M_packed_size)
4841 __arg._M_type = _M_type(__i);
4842 __arg._M_val = _M_values[__i];
4844 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4845 __arg = _M_args[__i];
4852 template<
typename _Context,
typename... _Args>
4853 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4854 -> basic_format_args<_Context>;
4856 template<
typename _Context,
typename... _Args>
4857 _GLIBCXX_CONSTEXPR_FORMAT
auto
4858 make_format_args(_Args&... __fmt_args)
noexcept;
4861 template<
typename _Context,
typename... _Args>
4862 class __format::_Arg_store
4864 friend std::basic_format_args<_Context>;
4866 template<
typename _Ctx,
typename... _Argz>
4867 friend _GLIBCXX_CONSTEXPR_FORMAT
auto std::
4868#if _GLIBCXX_INLINE_VERSION
4871 make_format_args(_Argz&...)
noexcept;
4875 static constexpr bool _S_values_only
4876 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4879 = __conditional_t<_S_values_only,
4880 __format::_Arg_value<_Context>,
4881 basic_format_arg<_Context>>;
4883 _Element_t _M_args[
sizeof...(_Args)];
4885 template<
typename _Tp>
4886 static _GLIBCXX_CONSTEXPR_FORMAT _Element_t
4887 _S_make_elt(_Tp& __v)
4889 using _Tq = remove_const_t<_Tp>;
4890 using _CharT =
typename _Context::char_type;
4891 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4892 "std::formatter must be specialized for the type "
4893 "of each format arg");
4894 using __format::__formattable_with;
4895 if constexpr (is_const_v<_Tp>)
4896 if constexpr (!__formattable_with<_Tp, _Context>)
4897 if constexpr (__formattable_with<_Tq, _Context>)
4898 static_assert(__formattable_with<_Tp, _Context>,
4899 "format arg must be non-const because its "
4900 "std::formatter specialization has a "
4901 "non-const reference parameter");
4902 basic_format_arg<_Context> __arg(__v);
4903 if constexpr (_S_values_only)
4904 return __arg._M_val;
4909 template<
typename... _Tp>
4910 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4911 [[__gnu__::__always_inline__]]
4912 _GLIBCXX_CONSTEXPR_FORMAT
4913 _Arg_store(_Tp&... __a) noexcept
4914 : _M_args{_S_make_elt(__a)...}
4918 template<
typename _Context>
4919 class __format::_Arg_store<_Context>
4922 template<
typename _Context>
4923 template<
typename... _Args>
4924 inline _GLIBCXX_CONSTEXPR_FORMAT
4925 basic_format_args<_Context>::
4926 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4928 if constexpr (
sizeof...(_Args) == 0)
4931 _M_unpacked_size = 0;
4934 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4937 _M_packed_size =
sizeof...(_Args);
4940 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4942 _M_values = __store._M_args;
4949 _M_unpacked_size =
sizeof...(_Args);
4951 _M_args = __store._M_args;
4956 template<
typename _Context = format_context,
typename... _Args>
4957 [[nodiscard,__gnu__::__always_inline__]]
4958 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4959 make_format_args(_Args&... __fmt_args)
noexcept
4961 using _Fmt_arg = basic_format_arg<_Context>;
4962 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4963 _Normalize<_Args>...>;
4964 return _Store(__fmt_args...);
4967#ifdef _GLIBCXX_USE_WCHAR_T
4969 template<
typename... _Args>
4970 [[nodiscard,__gnu__::__always_inline__]]
4971 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4972 make_wformat_args(_Args&... __args)
noexcept
4973 {
return std::make_format_args<wformat_context>(__args...); }
4979 template<
typename _Out,
typename _CharT,
typename _Context>
4980 _GLIBCXX_CONSTEXPR_FORMAT _Out
4981 __do_vformat_to(_Out, basic_string_view<_CharT>,
4982 const basic_format_args<_Context>&,
4983 const locale* =
nullptr);
4985 template<
typename _CharT>
struct __formatter_chrono;
5003 template<
typename _Out,
typename _CharT>
5004 class basic_format_context
5006 static_assert( output_iterator<_Out, const _CharT&> );
5008 basic_format_args<basic_format_context> _M_args;
5010 __format::_Optional_locale _M_loc;
5012 _GLIBCXX_CONSTEXPR_FORMAT
5013 basic_format_context(basic_format_args<basic_format_context> __args,
5015 : _M_args(__args), _M_out(std::move(__out))
5018 _GLIBCXX_CONSTEXPR_FORMAT
5019 basic_format_context(basic_format_args<basic_format_context> __args,
5020 _Out __out,
const std::locale& __loc)
5021 : _M_args(__args), _M_out(std::move(__out)), _M_loc(__loc)
5027 basic_format_context(
const basic_format_context&) =
delete;
5028 basic_format_context& operator=(
const basic_format_context&) =
delete;
5030 template<
typename _Out2,
typename _CharT2,
typename _Context2>
5031 friend _GLIBCXX_CONSTEXPR_FORMAT _Out2
5032 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
5033 const basic_format_args<_Context2>&,
5036 friend __format::__formatter_chrono<_CharT>;
5039 ~basic_format_context() =
default;
5041 using iterator = _Out;
5042 using char_type = _CharT;
5043 template<
typename _Tp>
5044 using formatter_type = formatter<_Tp, _CharT>;
5047 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<basic_format_context>
5048 arg(
size_t __id)
const noexcept
5049 {
return _M_args.get(__id); }
5052 std::locale locale() {
return _M_loc.value(); }
5055 _GLIBCXX_CONSTEXPR_FORMAT iterator
5058 _GLIBCXX_CONSTEXPR_FORMAT
void
5059 advance_to(iterator __it) { _M_out =
std::move(__it); }
5071 template<
typename _CharT>
5074 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
5076 struct _Parse_context : basic_format_parse_context<_CharT>
5078 using basic_format_parse_context<_CharT>::basic_format_parse_context;
5079 const _Arg_t* _M_types =
nullptr;
5083 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5084 : _M_pc(__str, __nargs)
5087 constexpr iterator begin() const noexcept {
return _M_pc.begin(); }
5088 constexpr iterator end() const noexcept {
return _M_pc.end(); }
5093 basic_string_view<_CharT> __fmt = _M_fmt_str();
5095 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5097 _M_pc.advance_to(begin() + 1);
5098 _M_format_arg(_M_pc.next_arg_id());
5102 size_t __lbr = __fmt.find(
'{');
5103 size_t __rbr = __fmt.find(
'}');
5105 while (__fmt.size())
5107 auto __cmp = __lbr <=> __rbr;
5111 _M_pc.advance_to(end());
5116 if (__lbr + 1 == __fmt.size()
5117 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5118 __format::__unmatched_left_brace_in_format_string();
5119 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5120 iterator __last = begin() + __lbr + int(__is_escape);
5121 _M_on_chars(__last);
5122 _M_pc.advance_to(__last + 1);
5123 __fmt = _M_fmt_str();
5126 if (__rbr != __fmt.npos)
5128 __lbr = __fmt.find(
'{');
5132 _M_on_replacement_field();
5133 __fmt = _M_fmt_str();
5134 __lbr = __fmt.find(
'{');
5135 __rbr = __fmt.find(
'}');
5140 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5141 __format::__unmatched_right_brace_in_format_string();
5142 iterator __last = begin() + __rbr;
5143 _M_on_chars(__last);
5144 _M_pc.advance_to(__last + 1);
5145 __fmt = _M_fmt_str();
5146 if (__lbr != __fmt.npos)
5148 __rbr = __fmt.find(
'}');
5153 constexpr basic_string_view<_CharT>
5154 _M_fmt_str() const noexcept
5155 {
return {begin(), end()}; }
5157 constexpr virtual void _M_on_chars(iterator) { }
5159 constexpr void _M_on_replacement_field()
5161 auto __next = begin();
5165 __id = _M_pc.next_arg_id();
5166 else if (*__next ==
':')
5168 __id = _M_pc.next_arg_id();
5169 _M_pc.advance_to(++__next);
5173 auto [__i, __ptr] = __format::__parse_arg_id(begin(), end());
5174 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5175 __format::__invalid_arg_id_in_format_string();
5176 _M_pc.check_arg_id(__id = __i);
5179 _M_pc.advance_to(++__ptr);
5182 _M_pc.advance_to(__ptr);
5184 _M_format_arg(__id);
5185 if (begin() == end() || *begin() !=
'}')
5186 __format::__unmatched_left_brace_in_format_string();
5187 _M_pc.advance_to(begin() + 1);
5190 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5194 template<
typename _Out,
typename _CharT>
5195 class _Formatting_scanner :
public _Scanner<_CharT>
5198 _GLIBCXX_CONSTEXPR_FORMAT
5199 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5200 basic_string_view<_CharT> __str)
5201 : _Scanner<_CharT>(__str), _M_fc(__fc)
5205 basic_format_context<_Out, _CharT>& _M_fc;
5207 using iterator =
typename _Scanner<_CharT>::iterator;
5210 _M_on_chars(iterator __last)
override
5212 basic_string_view<_CharT> __str(this->begin(), __last);
5213 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5217 _M_format_arg(
size_t __id)
override
5219 using _Context = basic_format_context<_Out, _CharT>;
5220 using handle =
typename basic_format_arg<_Context>::handle;
5222 __format::__visit_format_arg([
this](
auto& __arg) {
5224 using _Formatter =
typename _Context::template formatter_type<_Type>;
5225 if constexpr (is_same_v<_Type, monostate>)
5226 __format::__invalid_arg_id_in_format_string();
5227 else if constexpr (is_same_v<_Type, handle>)
5228 __arg.format(this->_M_pc, this->_M_fc);
5229 else if constexpr (is_default_constructible_v<_Formatter>)
5232 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5233 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5236 static_assert(__format::__formattable_with<_Type, _Context>);
5237 }, _M_fc.arg(__id));
5241 template<
typename _CharT,
typename _Tp>
5243 __to_arg_t_enum() noexcept
5245 using _Context = __format::__format_context<_CharT>;
5246 using _Fmt_arg = basic_format_arg<_Context>;
5247 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5248 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5252 template<
typename _CharT,
typename... _Args>
5253 class _Checking_scanner :
public _Scanner<_CharT>
5256 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5257 "std::formatter must be specialized for each type being formatted");
5261 _Checking_scanner(basic_string_view<_CharT> __str)
5262 : _Scanner<_CharT>(__str, sizeof...(_Args))
5264#if __cpp_lib_format >= 202305L
5265 this->_M_pc._M_types = _M_types.data();
5271 _M_format_arg(
size_t __id)
override
5273 if constexpr (
sizeof...(_Args) != 0)
5275 if (__id <
sizeof...(_Args))
5277 _M_parse_format_spec<_Args...>(__id);
5281 __builtin_unreachable();
5284 template<
typename _Tp,
typename... _OtherArgs>
5286 _M_parse_format_spec(
size_t __id)
5290 formatter<_Tp, _CharT> __f;
5291 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5293 else if constexpr (
sizeof...(_OtherArgs) != 0)
5294 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5296 __builtin_unreachable();
5299#if __cpp_lib_format >= 202305L
5300 array<_Arg_t,
sizeof...(_Args)>
5301 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5305 template<typename _CharT, unsigned = __unicode::__literal_encoding_is_unicode<_CharT>()>
5306 _GLIBCXX_CONSTEXPR_FORMAT _Sink_iter<_CharT>
5307 __do_vformat_to(_Sink_iter<_CharT> __out, basic_string_view<_CharT> __fmt,
5308 __format_context<_CharT>& __ctx)
5310 if constexpr (is_same_v<_CharT, char>)
5312 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5314 bool __done =
false;
5315 __format::__visit_format_arg([&](
auto& __arg) {
5316 using _Tp = remove_cvref_t<
decltype(__arg)>;
5317 if constexpr (is_same_v<_Tp, bool>)
5319 size_t __len = 4 + !__arg;
5320 const char* __chars[] = {
"false",
"true" };
5321 if (
auto __res = __out._M_reserve(__len))
5323 ranges::copy_n(__chars[__arg], __len, __res.get());
5324 __res._M_bump(__len);
5328 else if constexpr (is_same_v<_Tp, char>)
5330 if (
auto __res = __out._M_reserve(1))
5332 *__res.get() = __arg;
5337 else if constexpr (is_integral_v<_Tp>)
5339 make_unsigned_t<_Tp> __uval;
5340 const bool __neg = __arg < 0;
5342 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5345 const auto __n = __detail::__to_chars_len(__uval);
5346 if (
auto __res = __out._M_reserve(__n + __neg))
5348 auto __ptr = __res.get();
5350 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5352 __res._M_bump(__n + __neg);
5356 else if constexpr (is_convertible_v<_Tp, string_view>)
5358 string_view __sv = __arg;
5359 if (
auto __res = __out._M_reserve(__sv.size()))
5361 ranges::copy(__sv, __res.get());
5362 __res._M_bump(__sv.size());
5372 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5373 __scanner._M_scan();
5382#if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE
5383 extern template _Sink_iter<char>
5384 __do_vformat_to<char, 1>(_Sink_iter<char>, string_view,
5386# ifdef _GLIBCXX_USE_WCHAR_T
5387 extern template _Sink_iter<wchar_t>
5388 __do_vformat_to<wchar_t, 1>(_Sink_iter<wchar_t>, wstring_view,
5393 template<
typename _Out,
typename _CharT,
typename _Context>
5394 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5395 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5396 const basic_format_args<_Context>& __args,
5397 const locale* __loc)
5399 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5401 auto __ctx = __loc ==
nullptr
5402 ? _Context(__args, __out)
5403 : _Context(__args, __out, *__loc);
5404 return __format::__do_vformat_to(__out, __fmt, __ctx);
5406 else if constexpr (__contiguous_char_iter<_CharT, _Out>)
5408 _Ptr_sink<_CharT> __sink(__out);
5409 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5410 return std::move(__sink)._M_finish(__out).out;
5414 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5415 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5416 return std::move(__sink)._M_finish().out;
5420 template<
typename _Out,
typename _CharT>
5421 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5422 __do_vformat_to_n(_Out __out, iter_difference_t<_Out> __n,
5423 basic_string_view<_CharT> __fmt,
5424 const type_identity_t<
5425 basic_format_args<__format_context<_CharT>>>& __args,
5426 const locale* __loc =
nullptr)
5428 if constexpr (__contiguous_char_iter<_CharT, _Out>)
5430 _Ptr_sink<_CharT> __sink(__out, __n);
5431 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5432 return std::move(__sink)._M_finish(__out);
5436 _Iter_sink<_CharT, _Out> __sink(
std::move(__out), __n);
5437 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5442#pragma GCC diagnostic pop
5447#if __cpp_lib_format >= 202305L
5450 template<
typename _CharT>
5451 template<
typename... _Ts>
5453 basic_format_parse_context<_CharT>::
5454 __check_dynamic_spec(
size_t __id)
noexcept
5456 if (__id >= _M_num_args)
5457 __format::__invalid_arg_id_in_format_string();
5458 if constexpr (
sizeof...(_Ts) != 0)
5460 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
5461 auto* __args =
static_cast<_Parse_ctx*
>(
this)->_M_types;
5466 auto __arg = __args[__id];
5467 __format::_Arg_t __types[] = {
5468 __format::__to_arg_t_enum<_CharT, _Ts>()...
5470 for (
auto __t : __types)
5474 __invalid_dynamic_spec(
"arg(id) type does not match");
5479 template<
typename _CharT,
typename... _Args>
5480 template<
typename _Tp>
5481 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5483 basic_format_string<_CharT, _Args...>::
5484 basic_format_string(
const _Tp& __s) noexcept
5487 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5489 __scanner._M_scan();
5494 template<
typename _Out>
requires output_iterator<_Out, const char&>
5495 [[__gnu__::__always_inline__]]
5496 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5497 vformat_to(_Out __out, string_view __fmt, format_args __args)
5498 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5500#ifdef _GLIBCXX_USE_WCHAR_T
5501 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5502 [[__gnu__::__always_inline__]]
5503 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5504 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5505 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5508 template<
typename _Out>
requires output_iterator<_Out, const char&>
5509 [[__gnu__::__always_inline__]]
5511 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5514 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5517#ifdef _GLIBCXX_USE_WCHAR_T
5518 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5519 [[__gnu__::__always_inline__]]
5521 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5522 wformat_args __args)
5524 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5529 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5530 vformat(string_view __fmt, format_args __args)
5532 __format::_Str_sink<char> __buf;
5533 std::vformat_to(__buf.out(), __fmt, __args);
5537#ifdef _GLIBCXX_USE_WCHAR_T
5539 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5540 vformat(wstring_view __fmt, wformat_args __args)
5542 __format::_Str_sink<wchar_t> __buf;
5543 std::vformat_to(__buf.out(), __fmt, __args);
5550 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5552 __format::_Str_sink<char> __buf;
5553 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5557#ifdef _GLIBCXX_USE_WCHAR_T
5560 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5562 __format::_Str_sink<wchar_t> __buf;
5563 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5568 template<
typename... _Args>
5570 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5571 format(format_string<_Args...> __fmt, _Args&&... __args)
5572 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5574#ifdef _GLIBCXX_USE_WCHAR_T
5575 template<
typename... _Args>
5577 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5578 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5579 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5582 template<
typename... _Args>
5585 format(
const locale& __loc, format_string<_Args...> __fmt,
5588 return std::vformat(__loc, __fmt.get(),
5589 std::make_format_args(__args...));
5592#ifdef _GLIBCXX_USE_WCHAR_T
5593 template<
typename... _Args>
5596 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5599 return std::vformat(__loc, __fmt.get(),
5600 std::make_wformat_args(__args...));
5604 template<
typename _Out,
typename... _Args>
5605 requires output_iterator<_Out, const char&>
5606 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5607 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5609 return std::vformat_to(
std::move(__out), __fmt.get(),
5610 std::make_format_args(__args...));
5613#ifdef _GLIBCXX_USE_WCHAR_T
5614 template<
typename _Out,
typename... _Args>
5615 requires output_iterator<_Out, const wchar_t&>
5616 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5617 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5619 return std::vformat_to(
std::move(__out), __fmt.get(),
5620 std::make_wformat_args(__args...));
5624 template<
typename _Out,
typename... _Args>
5625 requires output_iterator<_Out, const char&>
5627 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5630 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5631 std::make_format_args(__args...));
5634#ifdef _GLIBCXX_USE_WCHAR_T
5635 template<
typename _Out,
typename... _Args>
5636 requires output_iterator<_Out, const wchar_t&>
5638 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5641 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5642 std::make_wformat_args(__args...));
5646 template<
typename _Out,
typename... _Args>
5647 requires output_iterator<_Out, const char&>
5648 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5649 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5650 format_string<_Args...> __fmt, _Args&&... __args)
5652 return __format::__do_vformat_to_n(
5654 std::make_format_args(__args...));
5657#ifdef _GLIBCXX_USE_WCHAR_T
5658 template<
typename _Out,
typename... _Args>
5659 requires output_iterator<_Out, const wchar_t&>
5660 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5661 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5662 wformat_string<_Args...> __fmt, _Args&&... __args)
5664 return __format::__do_vformat_to_n(
5666 std::make_wformat_args(__args...));
5670 template<
typename _Out,
typename... _Args>
5671 requires output_iterator<_Out, const char&>
5672 inline format_to_n_result<_Out>
5673 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5674 format_string<_Args...> __fmt, _Args&&... __args)
5676 return __format::__do_vformat_to_n(
5678 std::make_format_args(__args...), &__loc);
5681#ifdef _GLIBCXX_USE_WCHAR_T
5682 template<
typename _Out,
typename... _Args>
5683 requires output_iterator<_Out, const wchar_t&>
5684 inline format_to_n_result<_Out>
5685 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5686 wformat_string<_Args...> __fmt, _Args&&... __args)
5688 return __format::__do_vformat_to_n(
5690 std::make_wformat_args(__args...), &__loc);
5698 template<
typename _CharT>
5699 class _Counting_sink final :
public _Ptr_sink<_CharT>
5702 _GLIBCXX_CONSTEXPR_FORMAT
5703 _Counting_sink() : _Ptr_sink<_CharT>(nullptr, 0) { }
5705 [[__gnu__::__always_inline__]]
5706 _GLIBCXX_CONSTEXPR_FORMAT
size_t
5708 {
return this->_M_count + this->_M_used().size(); }
5711 template<
typename _CharT>
5712 class _Counting_sink :
public _Buf_sink<_CharT>
5714 size_t _M_count = 0;
5717 _M_overflow()
override
5719 if (!std::is_constant_evaluated())
5720 _M_count += this->_M_used().size();
5725 _Counting_sink() =
default;
5727 [[__gnu__::__always_inline__]]
5731 _Counting_sink::_M_overflow();
5739 template<
typename... _Args>
5741 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5742 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5744 __format::_Counting_sink<char> __buf;
5745 std::vformat_to(__buf.out(), __fmt.get(),
5746 std::make_format_args(__args...));
5747 return __buf.count();
5750#ifdef _GLIBCXX_USE_WCHAR_T
5751 template<
typename... _Args>
5753 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5754 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5756 __format::_Counting_sink<wchar_t> __buf;
5757 std::vformat_to(__buf.out(), __fmt.get(),
5758 std::make_wformat_args(__args...));
5759 return __buf.count();
5763 template<
typename... _Args>
5766 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5769 __format::_Counting_sink<char> __buf;
5770 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5771 std::make_format_args(__args...));
5772 return __buf.count();
5775#ifdef _GLIBCXX_USE_WCHAR_T
5776 template<
typename... _Args>
5779 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5782 __format::_Counting_sink<wchar_t> __buf;
5783 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5784 std::make_wformat_args(__args...));
5785 return __buf.count();
5789#if __glibcxx_format_ranges
5791 template<typename _Tp>
5792 consteval range_format
5795 using _Ref = ranges::range_reference_t<_Tp>;
5796 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5797 return range_format::disabled;
5798 else if constexpr (
requires {
typename _Tp::key_type; })
5800 if constexpr (
requires {
typename _Tp::mapped_type; })
5802 using _Up = remove_cvref_t<_Ref>;
5803 if constexpr (__is_pair<_Up>)
5804 return range_format::map;
5805 else if constexpr (__is_specialization_of<_Up, tuple>)
5806 if constexpr (tuple_size_v<_Up> == 2)
5807 return range_format::map;
5809 return range_format::set;
5812 return range_format::sequence;
5817 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5818 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5823 template<
typename _CharT,
typename _Out,
typename _Callback>
5824 _GLIBCXX_CONSTEXPR_FORMAT
5825 typename basic_format_context<_Out, _CharT>::iterator
5826 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5827 const _Spec<_CharT>& __spec,
5830 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
5836 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
5838 const size_t __padwidth = __spec._M_get_width(__fc);
5839 if (__padwidth == 0)
5840 return __call(__fc);
5844 _GLIBCXX_CONSTEXPR_FORMAT
5845 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5846 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5849 _GLIBCXX_CONSTEXPR_FORMAT
void
5851 { _M_ctx =
nullptr; }
5853 _GLIBCXX_CONSTEXPR_FORMAT
5857 _M_ctx->advance_to(_M_out);
5861 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5862 _Sink_iter<_CharT> _M_out;
5865 _Restore_out __restore(__fc);
5866 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
5867 __fc.advance_to(__sink.out());
5869 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
5870 __restore._M_disarm();
5875 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5876 struct __indexed_formatter_storage
5881 basic_format_parse_context<_CharT> __pc({});
5882 if (_M_formatter.parse(__pc) != __pc.end())
5883 __format::__failed_to_parse_format_spec();
5886 template<
typename _Out>
5887 _GLIBCXX_CONSTEXPR_FORMAT
void
5888 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5889 basic_format_context<_Out, _CharT>& __fc,
5890 basic_string_view<_CharT> __sep)
const
5892 if constexpr (_Pos != 0)
5893 __fc.advance_to(__format::__write(__fc.out(), __sep));
5894 __fc.advance_to(_M_formatter.format(__elem, __fc));
5897 [[__gnu__::__always_inline__]]
5901 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5902 _M_formatter.set_debug_format();
5906 formatter<_Tp, _CharT> _M_formatter;
5909 template<
typename _CharT,
typename... _Tps>
5910 class __tuple_formatter
5912 using _String_view = basic_string_view<_CharT>;
5913 using _Seps = __format::_Separators<_CharT>;
5917 set_separator(basic_string_view<_CharT> __sep)
noexcept
5921 set_brackets(basic_string_view<_CharT> __open,
5922 basic_string_view<_CharT> __close)
noexcept
5930 constexpr typename basic_format_parse_context<_CharT>::iterator
5931 parse(basic_format_parse_context<_CharT>& __pc)
5933 auto __first = __pc.begin();
5934 const auto __last = __pc.end();
5935 __format::_Spec<_CharT> __spec{};
5937 auto __finished = [&]
5939 if (__first != __last && *__first !=
'}')
5943 _M_felems._M_parse();
5944 _M_felems.set_debug_format();
5951 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5955 __first = __spec._M_parse_width(__first, __last, __pc);
5959 if (*__first ==
'n')
5962 _M_open = _M_close = _String_view();
5964 else if (*__first ==
'm')
5967 if constexpr (
sizeof...(_Tps) == 2)
5969 _M_sep = _Seps::_S_colon();
5970 _M_open = _M_close = _String_view();
5973 __throw_format_error(
"format error: 'm' specifier requires range"
5974 " of pair or tuple of two elements");
5980 __format::__failed_to_parse_format_spec();
5984 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
5985 _GLIBCXX_CONSTEXPR_FORMAT
5986 typename basic_format_context<_Out, _CharT>::iterator
5987 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
5988 basic_format_context<_Out, _CharT>& __fc)
const
5989 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
5991 template<
typename _Out>
5992 _GLIBCXX_CONSTEXPR_FORMAT
5993 typename basic_format_context<_Out, _CharT>::iterator
5994 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
5995 basic_format_context<_Out, _CharT>& __fc)
const
5997 return __format::__format_padded(
5999 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
6001 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
6002 _M_felems._M_format(__elems..., __nfc, _M_sep);
6003 return __format::__write(__nfc.out(), _M_close);
6008 template<
size_t... _Ids>
6009 struct __formatters_storage
6010 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
6012 template<
size_t _Id,
typename _Up>
6013 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
6018 (_Base<_Ids, _Tps>::_M_parse(), ...);
6021 template<
typename _Out>
6022 _GLIBCXX_CONSTEXPR_FORMAT
void
6023 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
6024 basic_format_context<_Out, _CharT>& __fc,
6025 _String_view __sep)
const
6027 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
6033 (_Base<_Ids, _Tps>::set_debug_format(), ...);
6037 template<
size_t... _Ids>
6038 static _GLIBCXX_CONSTEXPR_FORMAT
auto
6039 _S_create_storage(index_sequence<_Ids...>)
6040 -> __formatters_storage<_Ids...>;
6042 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
6044 _Spec<_CharT> _M_spec{};
6045 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
6046 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
6047 _String_view _M_sep = _Seps::_S_comma();
6048 _Formatters _M_felems;
6051 template<
typename _Tp>
6052 concept __is_map_formattable
6053 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
6059 template<__format::__
char _CharT, formattable<_CharT> _Fp,
6060 formattable<_CharT> _Sp>
6061 struct formatter<
pair<_Fp, _Sp>, _CharT>
6062 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
6063 remove_cvref_t<_Sp>>
6066 using __maybe_const_pair
6067 = __conditional_t<formattable<const _Fp, _CharT>
6068 && formattable<const _Sp, _CharT>,
6069 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
6073 template<
typename _Out>
6074 _GLIBCXX_CONSTEXPR_FORMAT
6075 typename basic_format_context<_Out, _CharT>::iterator
6076 format(__maybe_const_pair& __p,
6077 basic_format_context<_Out, _CharT>& __fc)
const
6078 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
6081#if __glibcxx_print >= 202406L
6084 template<
typename _Fp,
typename _Sp>
6085 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
6086 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
6087 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
6090 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
6091 struct formatter<tuple<_Tps...>, _CharT>
6092 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
6095 using __maybe_const_tuple
6096 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
6097 const tuple<_Tps...>, tuple<_Tps...>>;
6101 template<
typename _Out>
6102 _GLIBCXX_CONSTEXPR_FORMAT
6103 typename basic_format_context<_Out, _CharT>::iterator
6104 format(__maybe_const_tuple& __t,
6105 basic_format_context<_Out, _CharT>& __fc)
const
6106 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
6109#if __glibcxx_print >= 202406L
6112 template<
typename... _Tps>
6113 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
6114 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
6118 template<
typename _Tp, __format::__
char _CharT>
6119 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
6120 class range_formatter
6122 using _String_view = basic_string_view<_CharT>;
6123 using _Seps = __format::_Separators<_CharT>;
6127 set_separator(basic_string_view<_CharT> __sep)
noexcept
6131 set_brackets(basic_string_view<_CharT> __open,
6132 basic_string_view<_CharT> __close)
noexcept
6138 constexpr formatter<_Tp, _CharT>&
6139 underlying() noexcept
6142 constexpr const formatter<_Tp, _CharT>&
6143 underlying() const noexcept
6148 constexpr typename basic_format_parse_context<_CharT>::iterator
6149 parse(basic_format_parse_context<_CharT>& __pc)
6151 auto __first = __pc.begin();
6152 const auto __last = __pc.end();
6153 __format::_Spec<_CharT> __spec{};
6154 bool __no_brace =
false;
6156 auto __finished = [&]
6157 {
return __first == __last || *__first ==
'}'; };
6159 auto __finalize = [&]
6165 auto __parse_val = [&](_String_view __nfs = _String_view())
6167 basic_format_parse_context<_CharT> __npc(__nfs);
6168 if (_M_fval.parse(__npc) != __npc.end())
6169 __format::__failed_to_parse_format_spec();
6170 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6171 _M_fval.set_debug_format();
6172 return __finalize();
6176 return __parse_val();
6178 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6180 return __parse_val();
6182 __first = __spec._M_parse_width(__first, __last, __pc);
6184 return __parse_val();
6186 if (*__first ==
'?')
6189 __spec._M_debug =
true;
6190 if (__finished() || *__first !=
's')
6191 __throw_format_error(
"format error: '?' is allowed only in"
6192 " combination with 's'");
6195 if (*__first ==
's')
6198 if constexpr (same_as<_Tp, _CharT>)
6200 __spec._M_type = __format::_Pres_s;
6202 return __finalize();
6203 __throw_format_error(
"format error: element format specifier"
6204 " cannot be provided when 's' specifier is used");
6207 __throw_format_error(
"format error: 's' specifier requires"
6208 " range of character types");
6212 return __parse_val();
6214 if (*__first ==
'n')
6217 _M_open = _M_close = _String_view();
6222 return __parse_val();
6224 if (*__first ==
'm')
6226 _String_view __m(__first, 1);
6228 if constexpr (__format::__is_map_formattable<_Tp>)
6230 _M_sep = _Seps::_S_comma();
6233 _M_open = _Seps::_S_braces().substr(0, 1);
6234 _M_close = _Seps::_S_braces().substr(1, 1);
6237 return __parse_val(__m);
6238 __throw_format_error(
"format error: element format specifier"
6239 " cannot be provided when 'm' specifier is used");
6242 __throw_format_error(
"format error: 'm' specifier requires"
6243 " range of pairs or tuples of two elements");
6247 return __parse_val();
6249 if (*__first ==
':')
6251 __pc.advance_to(++__first);
6252 __first = _M_fval.parse(__pc);
6256 return __finalize();
6258 __format::__failed_to_parse_format_spec();
6263 template<ranges::input_range _Rg,
typename _Out>
6264 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6265 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6266 _GLIBCXX_CONSTEXPR_FORMAT
6267 typename basic_format_context<_Out, _CharT>::iterator
6268 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6270 using _Range = remove_reference_t<_Rg>;
6271 if constexpr (ranges::contiguous_range<_Rg>)
6273 const span<__format::__maybe_const<_Tp, _CharT>>
6274 __spn(ranges::data(__rg),
size_t(ranges::distance(__rg)));
6275 return _M_format(__spn, __fc);
6277 else if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6278 return _M_format<const _Range>(__rg, __fc);
6280 return _M_format(__rg, __fc);
6284 template<ranges::input_range _Rg,
typename _Out>
6285 _GLIBCXX_CONSTEXPR_FORMAT
6286 typename basic_format_context<_Out, _CharT>::iterator
6287 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6289 if constexpr (same_as<_Tp, _CharT>)
6290 if (_M_spec._M_type == __format::_Pres_s)
6292 __format::__formatter_str __fstr(_M_spec);
6293 return __fstr._M_format_range(__rg, __fc);
6295 return __format::__format_padded(
6297 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6298 {
return _M_format_elems(__rg, __nfc); });
6302 template<ranges::input_range _Rg,
typename _Out>
6303 _GLIBCXX_CONSTEXPR_FORMAT
6304 typename basic_format_context<_Out, _CharT>::iterator
6305 _M_format_elems(_Rg& __rg,
6306 basic_format_context<_Out, _CharT>& __fc)
const
6308 auto __out = __format::__write(__fc.out(), _M_open);
6310 auto __first = ranges::begin(__rg);
6311 auto const __last = ranges::end(__rg);
6312 if (__first == __last)
6313 return __format::__write(__out, _M_close);
6315 __fc.advance_to(__out);
6316 __out = _M_fval.format(*__first, __fc);
6317 for (++__first; __first != __last; ++__first)
6319 __out = __format::__write(__out, _M_sep);
6320 __fc.advance_to(__out);
6321 __out = _M_fval.format(*__first, __fc);
6324 return __format::__write(__out, _M_close);
6327 __format::_Spec<_CharT> _M_spec{};
6328 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6329 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6330 _String_view _M_sep = _Seps::_S_comma();
6331 formatter<_Tp, _CharT> _M_fval;
6339 template<ranges::input_range _Rg, __format::__
char _CharT>
6340 requires (format_kind<_Rg> != range_format::disabled)
6341 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6342 struct formatter<_Rg, _CharT>
6345 static const bool _S_range_format_is_string =
6346 (format_kind<_Rg> == range_format::string)
6347 || (format_kind<_Rg> == range_format::debug_string);
6348 using _Vt = remove_cvref_t<
6349 ranges::range_reference_t<
6350 __format::__maybe_const_range<_Rg, _CharT>>>;
6352 static consteval bool _S_is_correct()
6354 if constexpr (_S_range_format_is_string)
6355 static_assert(same_as<_Vt, _CharT>);
6359 static_assert(_S_is_correct());
6362 constexpr formatter() noexcept
6364 using _Seps = __format::_Separators<_CharT>;
6365 if constexpr (format_kind<_Rg> == range_format::map)
6367 static_assert(__format::__is_map_formattable<_Vt>);
6368 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6369 _Seps::_S_braces().substr(1, 1));
6370 _M_under.underlying().set_brackets({}, {});
6371 _M_under.underlying().set_separator(_Seps::_S_colon());
6373 else if constexpr (format_kind<_Rg> == range_format::set)
6374 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6375 _Seps::_S_braces().substr(1, 1));
6379 set_separator(basic_string_view<_CharT> __sep)
noexcept
6380 requires (format_kind<_Rg> == range_format::sequence)
6381 { _M_under.set_separator(__sep); }
6384 set_brackets(basic_string_view<_CharT> __open,
6385 basic_string_view<_CharT> __close)
noexcept
6386 requires (format_kind<_Rg> == range_format::sequence)
6387 { _M_under.set_brackets(__open, __close); }
6391 constexpr typename basic_format_parse_context<_CharT>::iterator
6392 parse(basic_format_parse_context<_CharT>& __pc)
6394 auto __res = _M_under.parse(__pc);
6395 if constexpr (format_kind<_Rg> == range_format::debug_string)
6396 _M_under.set_debug_format();
6402 template<
typename _Out>
6403 _GLIBCXX_CONSTEXPR_FORMAT
6404 typename basic_format_context<_Out, _CharT>::iterator
6405 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6406 basic_format_context<_Out, _CharT>& __fc)
const
6408 if constexpr (_S_range_format_is_string)
6409 return _M_under._M_format_range(__rg, __fc);
6411 return _M_under.format(__rg, __fc);
6415 using _Formatter_under
6416 = __conditional_t<_S_range_format_is_string,
6417 __format::__formatter_str<_CharT>,
6418 range_formatter<_Vt, _CharT>>;
6419 _Formatter_under _M_under;
6422#if __glibcxx_print >= 202406L
6423 template<ranges::input_range _Rg>
6424 requires (format_kind<_Rg> != range_format::disabled)
6425 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6429#undef _GLIBCXX_WIDEN
6430#undef _GLIBCXX_CONSTEXPR_FORMAT
6432_GLIBCXX_END_NAMESPACE_VERSION
6435#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.