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);
1729 __res = to_chars(__start, __end, __u, 10);
1733 __base_prefix =
"0";
1734 __res = to_chars(__start, __end, __u, 8);
1738 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1739 __res = to_chars(__start, __end, __u, 16);
1740 if (_M_spec._M_type == _Pres_X)
1741 for (
auto __p = __start; __p != __res.ptr; ++__p)
1742 *__p = __format::__toupper_numeric(*__p);
1745 __builtin_unreachable();
1748 if (_M_spec._M_alt && __base_prefix.size())
1750 __start -= __base_prefix.size();
1751 ranges::copy(__base_prefix, __start);
1753 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1756 string_view __narrow_str(__start, __res.ptr - __start);
1757 size_t __prefix_len = __start_digits - __start;
1758 if constexpr (is_same_v<char, _CharT>)
1759 return _M_format_int(__narrow_str, __prefix_len, __fc);
1760#ifdef _GLIBCXX_USE_WCHAR_T
1763 _CharT __wbuf[__buf_size];
1764 size_t __n = __narrow_str.size();
1767 std::__to_wstring_numeric(__narrow_str.data(), __n, __wbuf);
1768 return _M_format_int(basic_string_view<_CharT>(__wbuf, __n),
1769 __prefix_len, __fc);
1774 template<
typename _Out>
1775 _GLIBCXX_CONSTEXPR_FORMAT
1776 typename basic_format_context<_Out, _CharT>::iterator
1777 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1779 if (_M_spec._M_type == _Pres_c)
1780 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1781 if (_M_spec._M_type != _Pres_s)
1782 return format(
static_cast<unsigned char>(__i), __fc);
1784 basic_string<_CharT> __s;
1786 if (_M_spec._M_localized) [[unlikely]]
1789 __s = __i ? __np.truename() : __np.falsename();
1790 __est_width = __s.size();
1794 if constexpr (is_same_v<char, _CharT>)
1795 __s = __i ?
"true" :
"false";
1797 __s = __i ? L
"true" : L
"false";
1798 __est_width = __s.size();
1801 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1805 template<
typename _Out>
1806 _GLIBCXX_CONSTEXPR_FORMAT
1807 typename basic_format_context<_Out, _CharT>::iterator
1808 _M_format_character(_CharT __c,
1809 basic_format_context<_Out, _CharT>& __fc)
const
1811 basic_string_view<_CharT> __in(&__c, 1u);
1812 size_t __width = 1u;
1814 if constexpr (
sizeof(_CharT) > 1u &&
1815 __unicode::__literal_encoding_is_unicode<_CharT>())
1816 __width = __unicode::__field_width(__c);
1818 if (!_M_spec._M_debug)
1819 return __format::__write_padded_as_spec(__in, __width,
1823 if (_M_spec._M_get_width(__fc) <= __width)
1824 return __format::__write_escaped(__fc.out(), __in, _Term_apos);
1827 _Fixedbuf_sink<_CharT> __sink(__buf);
1828 __format::__write_escaped(__sink.out(), __in, _Term_apos);
1830 __in = __sink.view();
1831 if (__in[1] == _Escapes<_CharT>::_S_bslash()[0])
1832 __width = __in.size();
1833 return __format::__write_padded_as_spec(__in, __width,
1837 template<
typename _Int>
1838 static _GLIBCXX_CONSTEXPR_FORMAT _CharT
1839 _S_to_character(_Int __i)
1842 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1844 if (_Traits::__min <= __i && __i <= _Traits::__max)
1845 return static_cast<_CharT
>(__i);
1847 else if constexpr (is_signed_v<_Int>)
1849 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1850 return static_cast<_CharT
>(__i);
1852 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1853 return static_cast<_CharT
>(__i);
1854 __throw_format_error(
"format error: integer not representable as "
1858 template<
typename _Out>
1859 _GLIBCXX_CONSTEXPR_FORMAT
1860 typename basic_format_context<_Out, _CharT>::iterator
1861 _M_format_int(basic_string_view<_CharT> __str,
size_t __prefix_len,
1862 basic_format_context<_Out, _CharT>& __fc)
const
1864 size_t __width = _M_spec._M_get_width(__fc);
1865 if (_M_spec._M_localized)
1867 const auto& __l = __fc.locale();
1868 if (__l.name() !=
"C")
1870 auto& __np = use_facet<numpunct<_CharT>>(__l);
1871 string __grp = __np.grouping();
1874 size_t __n = __str.size() - __prefix_len;
1875 auto __p = (_CharT*)__builtin_alloca(2 * __n
1878 auto __s = __str.data();
1879 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1880 __s += __prefix_len;
1881 auto __end = std::__add_grouping(__p + __prefix_len,
1882 __np.thousands_sep(),
1886 __str = {__p, size_t(__end - __p)};
1891 if (__width <= __str.size())
1892 return __format::__write(__fc.out(), __str);
1894 char32_t __fill_char = _M_spec._M_fill;
1895 _Align __align = _M_spec._M_align;
1897 size_t __nfill = __width - __str.size();
1898 auto __out = __fc.out();
1899 if (__align == _Align_default)
1901 __align = _Align_right;
1902 if (_M_spec._M_zero_fill)
1904 __fill_char = _CharT(
'0');
1906 if (__prefix_len != 0)
1908 __out = __format::__write(
std::move(__out),
1909 __str.substr(0, __prefix_len));
1910 __str.remove_prefix(__prefix_len);
1914 __fill_char = _CharT(
' ');
1916 return __format::__write_padded(
std::move(__out), __str,
1917 __align, __nfill, __fill_char);
1920 _Spec<_CharT> _M_spec{};
1923#ifdef __BFLT16_DIG__
1924 using __bflt16_t =
decltype(0.0bf16);
1935#undef _GLIBCXX_FORMAT_F128
1937#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1940 using __flt128_t = __ieee128;
1941# define _GLIBCXX_FORMAT_F128 1
1943#ifdef __LONG_DOUBLE_IEEE128__
1947 to_chars(
char*,
char*, __ibm128)
noexcept
1948 __asm(
"_ZSt8to_charsPcS_e");
1951 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1952 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1955 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1956 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1957#elif __cplusplus == 202002L
1959 to_chars(
char*,
char*, __ieee128)
noexcept
1960 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1963 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1964 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1967 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1968 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1971#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1974 using __flt128_t =
long double;
1975# define _GLIBCXX_FORMAT_F128 2
1977#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1980 using __flt128_t = _Float128;
1981# define _GLIBCXX_FORMAT_F128 3
1983# if __cplusplus == 202002L
1987 to_chars(
char*,
char*, _Float128)
noexcept
1988# if _GLIBCXX_INLINE_VERSION
1989 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1991 __asm(
"_ZSt8to_charsPcS_DF128_");
1995 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1996# if _GLIBCXX_INLINE_VERSION
1997 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1999 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
2003 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
2004# if _GLIBCXX_INLINE_VERSION
2005 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
2007 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
2012 using std::to_chars;
2015 template<
typename _Tp>
2016 concept __formattable_float
2017 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
2018 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
2020 template<__
char _CharT>
2021 struct __formatter_fp
2023 constexpr typename basic_format_parse_context<_CharT>::iterator
2024 parse(basic_format_parse_context<_CharT>& __pc)
2026 _Spec<_CharT> __spec{};
2027 const auto __last = __pc.end();
2028 auto __first = __pc.begin();
2030 auto __finalize = [
this, &__spec] {
2034 auto __finished = [&] {
2035 if (__first == __last || *__first ==
'}')
2046 __first = __spec._M_parse_fill_and_align(__first, __last);
2050 __first = __spec._M_parse_sign(__first, __last);
2054 __first = __spec._M_parse_alternate_form(__first, __last);
2058 __first = __spec._M_parse_zero_fill(__first, __last);
2062 if (__first[0] !=
'.')
2064 __first = __spec._M_parse_width(__first, __last, __pc);
2069 __first = __spec._M_parse_precision(__first, __last, __pc);
2073 __first = __spec._M_parse_locale(__first, __last);
2080 __spec._M_type = _Pres_a;
2084 __spec._M_type = _Pres_A;
2088 __spec._M_type = _Pres_e;
2092 __spec._M_type = _Pres_E;
2096 __spec._M_type = _Pres_f;
2100 __spec._M_type = _Pres_F;
2104 __spec._M_type = _Pres_g;
2108 __spec._M_type = _Pres_G;
2116 __format::__failed_to_parse_format_spec();
2119 template<
typename _Fp,
typename _Out>
2120 typename basic_format_context<_Out, _CharT>::iterator
2121 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2125 to_chars_result __res{};
2128 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2130 __prec = _M_spec._M_get_precision(__fc);
2132 char* __start = __buf + 1;
2133 char* __end = __buf +
sizeof(__buf);
2136 bool __upper =
false;
2137 bool __trailing_zeros =
false;
2140 switch (_M_spec._M_type)
2147 if (_M_spec._M_type != _Pres_A)
2149 __fmt = chars_format::hex;
2157 __fmt = chars_format::scientific;
2164 __fmt = chars_format::fixed;
2171 __trailing_zeros =
true;
2173 __fmt = chars_format::general;
2177 __fmt = chars_format::general;
2180 __builtin_unreachable();
2184 auto __to_chars = [&](
char* __b,
char* __e) {
2186 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2187 else if (__fmt != chars_format{})
2188 return __format::to_chars(__b, __e, __v, __fmt);
2190 return __format::to_chars(__b, __e, __v);
2194 __res = __to_chars(__start, __end);
2196 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2200 size_t __guess = 8 + __prec;
2201 if (__fmt == chars_format::fixed)
2203 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2204 || is_same_v<_Fp, long double>)
2209 if constexpr (is_same_v<_Fp, float>)
2210 __builtin_frexpf(__v, &__exp);
2211 else if constexpr (is_same_v<_Fp, double>)
2212 __builtin_frexp(__v, &__exp);
2213 else if constexpr (is_same_v<_Fp, long double>)
2214 __builtin_frexpl(__v, &__exp);
2216 __guess += 1U + __exp * 4004U / 13301U;
2219 __guess += numeric_limits<_Fp>::max_exponent10;
2221 if (__guess <=
sizeof(__buf)) [[unlikely]]
2222 __guess =
sizeof(__buf) * 2;
2231 auto __overwrite = [&__to_chars, &__res] (
char* __p,
size_t __n)
2233 __res = __to_chars(__p + 1, __p + __n - 1);
2234 return __res.ec == errc{} ? __res.ptr - __p : 0;
2239 __start = __dynbuf.
data() + 1;
2240 __end = __dynbuf.
data() + __dynbuf.
size();
2242 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2248 for (
char* __p = __start; __p != __res.ptr; ++__p)
2249 *__p = __format::__toupper_numeric(*__p);
2252 bool __have_sign =
true;
2254 if (!__builtin_signbit(__v))
2256 if (_M_spec._M_sign == _Sign_plus)
2258 else if (_M_spec._M_sign == _Sign_space)
2261 __have_sign =
false;
2264 string_view __narrow_str(__start, __res.ptr - __start);
2268 if (_M_spec._M_alt && __builtin_isfinite(__v))
2270 string_view __s = __narrow_str;
2274 size_t __d = __s.find(
'.');
2275 if (__d != __s.npos)
2277 __p = __s.find(__expc, __d + 1);
2278 if (__p == __s.npos)
2282 if (__trailing_zeros)
2285 if (__s[__have_sign] !=
'0')
2287 __sigfigs = __p - __have_sign - 1;
2292 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2297 __p = __s.find(__expc);
2298 if (__p == __s.npos)
2301 __sigfigs = __d - __have_sign;
2304 if (__trailing_zeros && __prec != 0)
2309 __z = __prec - __sigfigs;
2312 if (
size_t __extras =
int(__d == __p) + __z)
2314 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2318 __builtin_memmove(__start + __p + __extras,
2322 __start[__p++] =
'.';
2323 __builtin_memset(__start + __p,
'0', __z);
2324 __narrow_str = {__s.data(), __s.size() + __extras};
2328 __dynbuf.
reserve(__s.size() + __extras);
2329 if (__dynbuf.
empty())
2331 __dynbuf = __s.
substr(0, __p);
2335 __dynbuf.
append(__z,
'0');
2336 __dynbuf.
append(__s.substr(__p));
2340 __dynbuf.
insert(__p, __extras,
'0');
2342 __dynbuf[__p] =
'.';
2344 __narrow_str = __dynbuf;
2349 basic_string<_CharT> __wstr;
2350 basic_string_view<_CharT> __str;
2351 if constexpr (is_same_v<_CharT, char>)
2352 __str = __narrow_str;
2353#ifdef _GLIBCXX_USE_WCHAR_T
2358 __wstr = std::__to_wstring_numeric(__narrow_str);
2363 if (_M_spec._M_localized && __builtin_isfinite(__v))
2365 auto __s = _M_localize(__str, __expc, __fc.locale());
2370 size_t __width = _M_spec._M_get_width(__fc);
2372 if (__width <= __str.size())
2373 return __format::__write(__fc.out(), __str);
2375 char32_t __fill_char = _M_spec._M_fill;
2376 _Align __align = _M_spec._M_align;
2378 size_t __nfill = __width - __str.size();
2379 auto __out = __fc.out();
2380 if (__align == _Align_default)
2382 __align = _Align_right;
2383 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2385 __fill_char = _CharT(
'0');
2387 if (!__format::__is_xdigit(__narrow_str[0]))
2389 *__out++ = __str[0];
2390 __str.remove_prefix(1);
2394 __fill_char = _CharT(
' ');
2396 return __format::__write_padded(
std::move(__out), __str,
2397 __align, __nfill, __fill_char);
2401 basic_string<_CharT>
2402 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2403 const locale& __loc)
const
2405 basic_string<_CharT> __lstr;
2407 if (__loc == locale::classic())
2410 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2411 const _CharT __point = __np.decimal_point();
2412 const string __grp = __np.grouping();
2414 _CharT __dot, __exp;
2415 if constexpr (is_same_v<_CharT, char>)
2438 __builtin_unreachable();
2442 if (__grp.empty() && __point == __dot)
2445 size_t __d = __str.find(__dot);
2446 size_t __e = min(__d, __str.find(__exp));
2447 if (__e == __str.npos)
2449 const size_t __r = __str.size() - __e;
2450 auto __overwrite = [&](_CharT* __p, size_t) {
2453 if (
auto __c = __str.front(); __c ==
'-' || __c ==
'+' || __c ==
' ')
2458 auto __end = std::__add_grouping(__p + __off, __np.thousands_sep(),
2459 __grp.data(), __grp.size(),
2460 __str.data() + __off,
2461 __str.data() + __e);
2464 if (__d != __str.npos)
2470 const size_t __rlen = __str.size() - __e;
2472 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2475 return (__end - __p);
2477 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2481 _Spec<_CharT> _M_spec{};
2484 template<__format::__
char _CharT>
2485 struct __formatter_ptr
2488 __formatter_ptr() noexcept
2491 _M_spec._M_type = _Pres_p;
2492 _M_spec._M_alt =
true;
2496 __formatter_ptr(_Spec<_CharT> __spec) noexcept
2498 { _M_set_default(_Pres_p); }
2500 constexpr typename basic_format_parse_context<_CharT>::iterator
2501 parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type = _Pres_p)
2503 __format::_Spec<_CharT> __spec{};
2504 const auto __last = __pc.end();
2505 auto __first = __pc.begin();
2507 auto __finalize = [
this, &__spec, __type] {
2509 _M_set_default(__type);
2512 auto __finished = [&] {
2513 if (__first == __last || *__first ==
'}')
2524 __first = __spec._M_parse_fill_and_align(__first, __last);
2530#if __glibcxx_format >= 202304L
2531 __first = __spec._M_parse_zero_fill(__first, __last);
2536 __first = __spec._M_parse_width(__first, __last, __pc);
2540 if (*__first ==
'p')
2542 __spec._M_type = _Pres_p;
2543 __spec._M_alt = !__spec._M_alt;
2546#if __glibcxx_format >= 202304L
2547 else if (*__first ==
'P')
2549 __spec._M_type = _Pres_P;
2550 __spec._M_alt = !__spec._M_alt;
2558 __format::__failed_to_parse_format_spec();
2561 template<
typename _Out>
2562 _GLIBCXX_CONSTEXPR_FORMAT
2563 typename basic_format_context<_Out, _CharT>::iterator
2564 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2567 ?
reinterpret_cast<__UINTPTR_TYPE__
>(__v)
2568 : static_cast<__UINTPTR_TYPE__>(0);
2569 return __formatter_int<_CharT>(_M_spec).format(__u, __fc);
2573 [[__gnu__::__always_inline__]]
2575 _M_set_default(_Pres_type __type)
2577 if (_M_spec._M_type == _Pres_none && __type != _Pres_none)
2579 _M_spec._M_type = __type;
2580 _M_spec._M_alt = !_M_spec._M_alt;
2584 __format::_Spec<_CharT> _M_spec;
2591 template<__format::__
char _CharT>
2592 struct formatter<_CharT, _CharT>
2594 formatter() =
default;
2596 constexpr typename basic_format_parse_context<_CharT>::iterator
2597 parse(basic_format_parse_context<_CharT>& __pc)
2599 return _M_f.template _M_parse<_CharT>(__pc);
2602 template<
typename _Out>
2603 _GLIBCXX_CONSTEXPR_FORMAT
2604 typename basic_format_context<_Out, _CharT>::iterator
2605 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2607 if (_M_f._M_spec._M_type == __format::_Pres_c)
2608 return _M_f._M_format_character(__u, __fc);
2610 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2613#if __glibcxx_format_ranges
2615 set_debug_format() noexcept
2616 { _M_f._M_spec._M_debug =
true; }
2620 __format::__formatter_int<_CharT> _M_f;
2623#if __glibcxx_print >= 202403L
2624 template<__format::__
char _CharT>
2625 constexpr bool enable_nonlocking_formatter_optimization<_CharT> =
true;
2628#ifdef _GLIBCXX_USE_WCHAR_T
2631 struct formatter<char, wchar_t>
2633 formatter() =
default;
2635 constexpr typename basic_format_parse_context<wchar_t>::iterator
2636 parse(basic_format_parse_context<wchar_t>& __pc)
2638 return _M_f._M_parse<
char>(__pc);
2641 template<
typename _Out>
2642 _GLIBCXX_CONSTEXPR_FORMAT
2643 typename basic_format_context<_Out, wchar_t>::iterator
2644 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2646 if (_M_f._M_spec._M_type == __format::_Pres_c)
2647 return _M_f._M_format_character(__u, __fc);
2649 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2652#if __glibcxx_format_ranges
2654 set_debug_format() noexcept
2655 { _M_f._M_spec._M_debug =
true; }
2659 __format::__formatter_int<wchar_t> _M_f;
2666 template<__format::__
char _CharT>
2667 struct formatter<_CharT*, _CharT>
2669 formatter() =
default;
2671 [[__gnu__::__always_inline__]]
2672 constexpr typename basic_format_parse_context<_CharT>::iterator
2673 parse(basic_format_parse_context<_CharT>& __pc)
2674 {
return _M_f.parse(__pc); }
2676 template<
typename _Out>
2677 [[__gnu__::__nonnull__]]
2678 _GLIBCXX_CONSTEXPR_FORMAT
2679 typename basic_format_context<_Out, _CharT>::iterator
2680 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2681 {
return _M_f.format(__u, __fc); }
2683#if __glibcxx_format_ranges
2684 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2688 __format::__formatter_str<_CharT> _M_f;
2691#if __glibcxx_print >= 202403L
2692 template<__format::__
char _CharT>
2693 constexpr bool enable_nonlocking_formatter_optimization<_CharT*> =
true;
2696 template<__format::__
char _CharT>
2697 struct formatter<const _CharT*, _CharT>
2699 formatter() =
default;
2701 [[__gnu__::__always_inline__]]
2702 constexpr typename basic_format_parse_context<_CharT>::iterator
2703 parse(basic_format_parse_context<_CharT>& __pc)
2704 {
return _M_f.parse(__pc); }
2706 template<
typename _Out>
2707 [[__gnu__::__nonnull__]]
2708 _GLIBCXX_CONSTEXPR_FORMAT
2709 typename basic_format_context<_Out, _CharT>::iterator
2710 format(
const _CharT* __u,
2711 basic_format_context<_Out, _CharT>& __fc)
const
2712 {
return _M_f.format(__u, __fc); }
2714#if __glibcxx_format_ranges
2715 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2719 __format::__formatter_str<_CharT> _M_f;
2722#if __glibcxx_print >= 202403L
2723 template<__format::__
char _CharT>
2725 enable_nonlocking_formatter_optimization<const _CharT*> =
true;
2728 template<__format::__
char _CharT,
size_t _Nm>
2729 struct formatter<_CharT[_Nm], _CharT>
2731 formatter() =
default;
2733 [[__gnu__::__always_inline__]]
2734 constexpr typename basic_format_parse_context<_CharT>::iterator
2735 parse(basic_format_parse_context<_CharT>& __pc)
2736 {
return _M_f.parse(__pc); }
2738 template<
typename _Out>
2739 _GLIBCXX_CONSTEXPR_FORMAT
2740 typename basic_format_context<_Out, _CharT>::iterator
2741 format(
const _CharT (&__u)[_Nm],
2742 basic_format_context<_Out, _CharT>& __fc)
const
2743 {
return _M_f.format({__u, _Nm}, __fc); }
2745#if __glibcxx_format_ranges
2746 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2750 __format::__formatter_str<_CharT> _M_f;
2753#if __glibcxx_print >= 202403L
2754 template<__format::__
char _CharT,
size_t _Nm>
2755 constexpr bool enable_nonlocking_formatter_optimization<_CharT[_Nm]> =
true;
2758 template<
typename _Traits,
typename _Alloc>
2759 struct formatter<
basic_string<char, _Traits, _Alloc>, char>
2761 formatter() =
default;
2763 [[__gnu__::__always_inline__]]
2764 constexpr typename basic_format_parse_context<char>::iterator
2765 parse(basic_format_parse_context<char>& __pc)
2766 {
return _M_f.parse(__pc); }
2768 template<
typename _Out>
2769 _GLIBCXX_CONSTEXPR_FORMAT
2770 typename basic_format_context<_Out, char>::iterator
2771 format(
const basic_string<char, _Traits, _Alloc>& __u,
2772 basic_format_context<_Out, char>& __fc)
const
2773 {
return _M_f.format(__u, __fc); }
2775#if __glibcxx_format_ranges
2776 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2780 __format::__formatter_str<char> _M_f;
2783#if __glibcxx_print >= 202403L
2784 template<
typename _Tr,
typename _Alloc>
2786 enable_nonlocking_formatter_optimization<basic_string<char, _Tr, _Alloc>>
2790#ifdef _GLIBCXX_USE_WCHAR_T
2791 template<
typename _Traits,
typename _Alloc>
2792 struct formatter<
basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2794 formatter() =
default;
2796 [[__gnu__::__always_inline__]]
2797 constexpr typename basic_format_parse_context<wchar_t>::iterator
2798 parse(basic_format_parse_context<wchar_t>& __pc)
2799 {
return _M_f.parse(__pc); }
2801 template<
typename _Out>
2802 _GLIBCXX_CONSTEXPR_FORMAT
2803 typename basic_format_context<_Out, wchar_t>::iterator
2804 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2805 basic_format_context<_Out, wchar_t>& __fc)
const
2806 {
return _M_f.format(__u, __fc); }
2808#if __glibcxx_format_ranges
2809 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2813 __format::__formatter_str<wchar_t> _M_f;
2816#if __glibcxx_print >= 202403L
2817 template<
typename _Tr,
typename _Alloc>
2819 enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Tr, _Alloc>>
2825 template<
typename _Traits>
2828 formatter() =
default;
2830 [[__gnu__::__always_inline__]]
2831 constexpr typename basic_format_parse_context<char>::iterator
2832 parse(basic_format_parse_context<char>& __pc)
2833 {
return _M_f.parse(__pc); }
2835 template<
typename _Out>
2836 _GLIBCXX_CONSTEXPR_FORMAT
2837 typename basic_format_context<_Out, char>::iterator
2838 format(basic_string_view<char, _Traits> __u,
2839 basic_format_context<_Out, char>& __fc)
const
2840 {
return _M_f.format(__u, __fc); }
2842#if __glibcxx_format_ranges
2843 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2847 __format::__formatter_str<char> _M_f;
2850#if __glibcxx_print >= 202403L
2851 template<
typename _Tr>
2853 enable_nonlocking_formatter_optimization<basic_string_view<char, _Tr>>
2857#ifdef _GLIBCXX_USE_WCHAR_T
2858 template<
typename _Traits>
2861 formatter() =
default;
2863 [[__gnu__::__always_inline__]]
2864 constexpr typename basic_format_parse_context<wchar_t>::iterator
2865 parse(basic_format_parse_context<wchar_t>& __pc)
2866 {
return _M_f.parse(__pc); }
2868 template<
typename _Out>
2869 _GLIBCXX_CONSTEXPR_FORMAT
2870 typename basic_format_context<_Out, wchar_t>::iterator
2871 format(basic_string_view<wchar_t, _Traits> __u,
2872 basic_format_context<_Out, wchar_t>& __fc)
const
2873 {
return _M_f.format(__u, __fc); }
2875#if __glibcxx_format_ranges
2876 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2880 __format::__formatter_str<wchar_t> _M_f;
2883#if __glibcxx_print >= 202403L
2884 template<
typename _Tr>
2886 enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Tr>>
2897 template<
typename _Tp>
2898 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2900#if defined __SIZEOF_INT128__
2901 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2902 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2906 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2907 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2908#ifdef _GLIBCXX_USE_CHAR8_T
2909 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2911 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2912 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2914 template<
typename _Tp>
2915 concept __formattable_integer = __is_formattable_integer<_Tp>;
2920 template<__format::__formattable_
integer _Tp, __format::__
char _CharT>
2921 struct formatter<_Tp, _CharT>
2923 formatter() =
default;
2925 [[__gnu__::__always_inline__]]
2926 constexpr typename basic_format_parse_context<_CharT>::iterator
2927 parse(basic_format_parse_context<_CharT>& __pc)
2929 return _M_f.template _M_parse<_Tp>(__pc);
2932 template<
typename _Out>
2933 _GLIBCXX_CONSTEXPR_FORMAT
2934 typename basic_format_context<_Out, _CharT>::iterator
2935 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2936 {
return _M_f.format(__u, __fc); }
2939 __format::__formatter_int<_CharT> _M_f;
2942#if __glibcxx_print >= 202403L
2943 template<__format::__formattable_
integer _Tp>
2945 enable_nonlocking_formatter_optimization<_Tp> =
true;
2948#if defined __glibcxx_to_chars
2950 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2951 struct formatter<_Tp, _CharT>
2953 formatter() =
default;
2955 [[__gnu__::__always_inline__]]
2956 constexpr typename basic_format_parse_context<_CharT>::iterator
2957 parse(basic_format_parse_context<_CharT>& __pc)
2958 {
return _M_f.parse(__pc); }
2960 template<
typename _Out>
2961 typename basic_format_context<_Out, _CharT>::iterator
2962 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2963 {
return _M_f.format(__u, __fc); }
2966 __format::__formatter_fp<_CharT> _M_f;
2969#if __glibcxx_print >= 202403L
2970 template<__format::__formattable_
float _Tp>
2972 enable_nonlocking_formatter_optimization<_Tp> =
true;
2975#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2977 template<__format::__
char _CharT>
2978 struct formatter<long double, _CharT>
2980 formatter() =
default;
2982 [[__gnu__::__always_inline__]]
2983 constexpr typename basic_format_parse_context<_CharT>::iterator
2984 parse(basic_format_parse_context<_CharT>& __pc)
2985 {
return _M_f.parse(__pc); }
2987 template<
typename _Out>
2988 typename basic_format_context<_Out, _CharT>::iterator
2989 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2990 {
return _M_f.format((
double)__u, __fc); }
2993 __format::__formatter_fp<_CharT> _M_f;
2997#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2999 template<__format::__
char _CharT>
3000 struct formatter<_Float16, _CharT>
3002 formatter() =
default;
3004 [[__gnu__::__always_inline__]]
3005 constexpr typename basic_format_parse_context<_CharT>::iterator
3006 parse(basic_format_parse_context<_CharT>& __pc)
3007 {
return _M_f.parse(__pc); }
3009 template<
typename _Out>
3010 typename basic_format_context<_Out, _CharT>::iterator
3011 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
3012 {
return _M_f.format((
float)__u, __fc); }
3015 __format::__formatter_fp<_CharT> _M_f;
3019#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3021 template<__format::__
char _CharT>
3022 struct formatter<_Float32, _CharT>
3024 formatter() =
default;
3026 [[__gnu__::__always_inline__]]
3027 constexpr typename basic_format_parse_context<_CharT>::iterator
3028 parse(basic_format_parse_context<_CharT>& __pc)
3029 {
return _M_f.parse(__pc); }
3031 template<
typename _Out>
3032 typename basic_format_context<_Out, _CharT>::iterator
3033 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
3034 {
return _M_f.format((
float)__u, __fc); }
3037 __format::__formatter_fp<_CharT> _M_f;
3041#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3043 template<__format::__
char _CharT>
3044 struct formatter<_Float64, _CharT>
3046 formatter() =
default;
3048 [[__gnu__::__always_inline__]]
3049 constexpr typename basic_format_parse_context<_CharT>::iterator
3050 parse(basic_format_parse_context<_CharT>& __pc)
3051 {
return _M_f.parse(__pc); }
3053 template<
typename _Out>
3054 typename basic_format_context<_Out, _CharT>::iterator
3055 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
3056 {
return _M_f.format((
double)__u, __fc); }
3059 __format::__formatter_fp<_CharT> _M_f;
3063#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128
3065 template<__format::__
char _CharT>
3066 struct formatter<_Float128, _CharT>
3068 formatter() =
default;
3070 [[__gnu__::__always_inline__]]
3071 constexpr typename basic_format_parse_context<_CharT>::iterator
3072 parse(basic_format_parse_context<_CharT>& __pc)
3073 {
return _M_f.parse(__pc); }
3075 template<
typename _Out>
3076 typename basic_format_context<_Out, _CharT>::iterator
3077 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3078 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3081 __format::__formatter_fp<_CharT> _M_f;
3085#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128 == 2
3088 template<__format::__
char _CharT>
3089 struct formatter<__float128, _CharT>
3091 formatter() =
default;
3093 [[__gnu__::__always_inline__]]
3094 constexpr typename basic_format_parse_context<_CharT>::iterator
3095 parse(basic_format_parse_context<_CharT>& __pc)
3096 {
return _M_f.parse(__pc); }
3098 template<
typename _Out>
3099 typename basic_format_context<_Out, _CharT>::iterator
3100 format(__float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3101 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3104 __format::__formatter_fp<_CharT> _M_f;
3108#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3110 template<__format::__
char _CharT>
3111 struct formatter<__format::__bflt16_t, _CharT>
3113 formatter() =
default;
3115 [[__gnu__::__always_inline__]]
3116 constexpr typename basic_format_parse_context<_CharT>::iterator
3117 parse(basic_format_parse_context<_CharT>& __pc)
3118 {
return _M_f.parse(__pc); }
3120 template<
typename _Out>
3121 typename basic_format_context<_Out, _CharT>::iterator
3122 format(__gnu_cxx::__bfloat16_t __u,
3123 basic_format_context<_Out, _CharT>& __fc)
const
3124 {
return _M_f.format((
float)__u, __fc); }
3127 __format::__formatter_fp<_CharT> _M_f;
3135 template<__format::__
char _CharT>
3136 struct formatter<const void*, _CharT>
3138 formatter() =
default;
3140 constexpr typename basic_format_parse_context<_CharT>::iterator
3141 parse(basic_format_parse_context<_CharT>& __pc)
3142 {
return _M_f.parse(__pc); }
3144 template<
typename _Out>
3145 _GLIBCXX_CONSTEXPR_FORMAT
3146 typename basic_format_context<_Out, _CharT>::iterator
3147 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3148 {
return _M_f.format(__v, __fc); }
3151 __format::__formatter_ptr<_CharT> _M_f;
3154#if __glibcxx_print >= 202403L
3156 inline constexpr bool
3157 enable_nonlocking_formatter_optimization<const void*> =
true;
3160 template<__format::__
char _CharT>
3161 struct formatter<void*, _CharT>
3163 formatter() =
default;
3165 [[__gnu__::__always_inline__]]
3166 constexpr typename basic_format_parse_context<_CharT>::iterator
3167 parse(basic_format_parse_context<_CharT>& __pc)
3168 {
return _M_f.parse(__pc); }
3170 template<
typename _Out>
3171 _GLIBCXX_CONSTEXPR_FORMAT
3172 typename basic_format_context<_Out, _CharT>::iterator
3173 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3174 {
return _M_f.format(__v, __fc); }
3177 __format::__formatter_ptr<_CharT> _M_f;
3180#if __glibcxx_print >= 202403l
3182 inline constexpr bool
3183 enable_nonlocking_formatter_optimization<void*> =
true;
3186 template<__format::__
char _CharT>
3187 struct formatter<nullptr_t, _CharT>
3189 formatter() =
default;
3191 [[__gnu__::__always_inline__]]
3192 constexpr typename basic_format_parse_context<_CharT>::iterator
3193 parse(basic_format_parse_context<_CharT>& __pc)
3194 {
return _M_f.parse(__pc); }
3196 template<
typename _Out>
3197 _GLIBCXX_CONSTEXPR_FORMAT
3198 typename basic_format_context<_Out, _CharT>::iterator
3199 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3200 {
return _M_f.format(
nullptr, __fc); }
3203 __format::__formatter_ptr<_CharT> _M_f;
3207#if __glibcxx_print >= 202403L
3209 inline constexpr bool
3210 enable_nonlocking_formatter_optimization<nullptr_t> =
true;
3213#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3217 struct __formatter_disabled
3219 __formatter_disabled() =
delete;
3220 __formatter_disabled(
const __formatter_disabled&) =
delete;
3221 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3225 struct formatter<char*,
wchar_t>
3226 :
private __formatter_disabled { };
3228 struct formatter<const char*,
wchar_t>
3229 :
private __formatter_disabled { };
3230 template<
size_t _Nm>
3231 struct formatter<char[_Nm], wchar_t>
3232 :
private __formatter_disabled { };
3233 template<
class _Traits,
class _Allocator>
3234 struct formatter<
basic_string<char, _Traits, _Allocator>, wchar_t>
3235 :
private __formatter_disabled { };
3236 template<
class _Traits>
3238 :
private __formatter_disabled { };
3243 template<
typename _Out>
3244 struct format_to_n_result
3247 iter_difference_t<_Out> size;
3250_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3251template<
typename,
typename>
class vector;
3252_GLIBCXX_END_NAMESPACE_CONTAINER
3257 template<
typename _CharT>
3261 using iterator_category = output_iterator_tag;
3262 using value_type = void;
3263 using difference_type = ptrdiff_t;
3264 using pointer = void;
3265 using reference = void;
3267 _Drop_iter() =
default;
3268 _Drop_iter(
const _Drop_iter&) =
default;
3269 _Drop_iter& operator=(
const _Drop_iter&) =
default;
3271 [[__gnu__::__always_inline__]]
3272 constexpr _Drop_iter&
3273 operator=(_CharT __c)
3276 [[__gnu__::__always_inline__]]
3277 constexpr _Drop_iter&
3278 operator=(basic_string_view<_CharT> __s)
3281 [[__gnu__::__always_inline__]]
3282 constexpr _Drop_iter&
3285 [[__gnu__::__always_inline__]]
3286 constexpr _Drop_iter&
3287 operator++() {
return *
this; }
3289 [[__gnu__::__always_inline__]]
3290 constexpr _Drop_iter
3291 operator++(
int) {
return *
this; }
3294 template<
typename _CharT>
3297 _Sink<_CharT>* _M_sink =
nullptr;
3300 using iterator_category = output_iterator_tag;
3301 using value_type = void;
3302 using difference_type = ptrdiff_t;
3303 using pointer = void;
3304 using reference = void;
3306 _Sink_iter() =
default;
3307 _Sink_iter(
const _Sink_iter&) =
default;
3308 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3310 [[__gnu__::__always_inline__]]
3312 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3314 [[__gnu__::__always_inline__]]
3315 constexpr _Sink_iter&
3316 operator=(_CharT __c)
3318 _M_sink->_M_write(__c);
3322 [[__gnu__::__always_inline__]]
3323 constexpr _Sink_iter&
3324 operator=(basic_string_view<_CharT> __s)
3326 _M_sink->_M_write(__s);
3330 [[__gnu__::__always_inline__]]
3331 constexpr _Sink_iter&
3334 [[__gnu__::__always_inline__]]
3335 constexpr _Sink_iter&
3336 operator++() {
return *
this; }
3338 [[__gnu__::__always_inline__]]
3339 constexpr _Sink_iter
3340 operator++(
int) {
return *
this; }
3342 _GLIBCXX_CONSTEXPR_FORMAT
auto
3343 _M_reserve(
size_t __n)
const
3344 {
return _M_sink->_M_reserve(__n); }
3346 _GLIBCXX_CONSTEXPR_FORMAT
bool
3347 _M_discarding()
const
3348 {
return _M_sink->_M_discarding(); }
3354 template<
typename _CharT>
3357 friend class _Sink_iter<_CharT>;
3359 span<_CharT> _M_span;
3360 typename span<_CharT>::iterator _M_next;
3366 virtual void _M_overflow() = 0;
3370 [[__gnu__::__always_inline__]]
3372 _Sink(span<_CharT> __span) noexcept
3373 : _M_span(__span), _M_next(__span.begin())
3377 [[__gnu__::__always_inline__]]
3378 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3379 _M_used() const noexcept
3380 {
return _M_span.first(_M_next - _M_span.begin()); }
3383 [[__gnu__::__always_inline__]]
3384 constexpr span<_CharT>
3385 _M_unused() const noexcept
3386 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3389 [[__gnu__::__always_inline__]]
3391 _M_rewind() noexcept
3392 { _M_next = _M_span.begin(); }
3395 _GLIBCXX_CONSTEXPR_FORMAT
void
3396 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3399 _M_next = __s.begin() + __pos;
3404 _M_write(_CharT __c)
3407 if (_M_next - _M_span.begin() ==
std::ssize(_M_span)) [[unlikely]]
3412 _M_write(basic_string_view<_CharT> __s)
3414 span __to = _M_unused();
3415 while (__to.size() <= __s.size())
3417 __s.copy(__to.data(), __to.size());
3418 _M_next += __to.size();
3419 __s.remove_prefix(__to.size());
3425 __s.copy(__to.data(), __s.size());
3426 _M_next += __s.size();
3435 _GLIBCXX_CONSTEXPR_FORMAT
3436 explicit operator bool() const noexcept {
return _M_sink; }
3438 _GLIBCXX_CONSTEXPR_FORMAT _CharT*
3439 get() const noexcept {
return _M_sink->_M_next.operator->(); }
3441 _GLIBCXX_CONSTEXPR_FORMAT
void
3442 _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3450 _GLIBCXX_CONSTEXPR_FORMAT
virtual _Reservation
3451 _M_reserve(
size_t __n)
3453 if (__n <= _M_unused().size())
3456 if (__n <= _M_span.size())
3459 if (__n <= _M_unused().size())
3467 _GLIBCXX_CONSTEXPR_FORMAT
virtual void
3472 _GLIBCXX_CONSTEXPR_FORMAT
virtual bool
3473 _M_discarding()
const
3477 _Sink(
const _Sink&) =
delete;
3478 _Sink& operator=(
const _Sink&) =
delete;
3480 [[__gnu__::__always_inline__]]
3481 constexpr _Sink_iter<_CharT>
3483 {
return _Sink_iter<_CharT>(*
this); }
3487 template<
typename _CharT>
3488 class _Fixedbuf_sink final :
public _Sink<_CharT>
3490 _GLIBCXX_CONSTEXPR_FORMAT
void
3491 _M_overflow()
override
3493 __glibcxx_assert(
false);
3498 [[__gnu__::__always_inline__]]
3500 _Fixedbuf_sink(span<_CharT> __buf)
3501 : _Sink<_CharT>(__buf)
3504 constexpr basic_string_view<_CharT>
3507 auto __s = this->_M_used();
3508 return basic_string_view<_CharT>(__s.data(), __s.size());
3513 template<
typename _CharT>
3514 class _Buf_sink :
public _Sink<_CharT>
3517 _CharT _M_buf[__stackbuf_size<_CharT>];
3519 [[__gnu__::__always_inline__]]
3521 _Buf_sink() noexcept
3522 : _Sink<_CharT>(_M_buf)
3526 using _GLIBCXX_STD_C::vector;
3530 template<
typename _Seq>
3531 class _Seq_sink :
public _Buf_sink<typename _Seq::value_type>
3533 using _CharT =
typename _Seq::value_type;
3538 _GLIBCXX_CONSTEXPR_FORMAT
void
3539 _M_overflow()
override
3541 auto __s = this->_M_used();
3542 if (__s.empty()) [[unlikely]]
3546 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3548 if constexpr (__is_specialization_of<_Seq, basic_string>)
3549 _M_seq.append(__s.data(), __s.size());
3551 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3557 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3558 _M_reserve(
size_t __n)
override
3567 if constexpr (__is_specialization_of<_Seq, basic_string>
3568 || __is_specialization_of<_Seq, vector>)
3571 if (this->_M_used().size()) [[unlikely]]
3572 _Seq_sink::_M_overflow();
3575 const auto __sz = _M_seq.size();
3576 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3577 _M_seq.__resize_and_overwrite(__sz + __n,
3578 [](
auto,
auto __n2) {
3582 _M_seq.resize(__sz + __n);
3586 this->_M_reset(_M_seq, __sz);
3590 return _Sink<_CharT>::_M_reserve(__n);
3593 _GLIBCXX_CONSTEXPR_FORMAT
void
3594 _M_bump(
size_t __n)
override
3596 if constexpr (__is_specialization_of<_Seq, basic_string>
3597 || __is_specialization_of<_Seq, vector>)
3599 auto __s = this->_M_used();
3600 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3602 _M_seq.resize(__s.size() + __n);
3604 this->_M_reset(this->_M_buf);
3608 _GLIBCXX_CONSTEXPR_FORMAT
void
3609 _M_trim(span<const _CharT> __s)
3610 requires __is_specialization_of<_Seq, basic_string>
3612 _GLIBCXX_DEBUG_ASSERT(__s.data() == this->_M_buf
3613 || __s.data() == _M_seq.data());
3614 if (__s.data() == _M_seq.data())
3615 _M_seq.resize(__s.size());
3617 this->_M_reset(this->_M_buf, __s.size());
3624 [[__gnu__::__always_inline__]]
3625 _GLIBCXX_CONSTEXPR_FORMAT
3626 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3629 _GLIBCXX_CONSTEXPR_FORMAT
3630 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3631 : _M_seq(std::move(__s))
3634 using _Sink<_CharT>::out;
3636 _GLIBCXX_CONSTEXPR_FORMAT _Seq
3639 if (this->_M_used().size() != 0)
3640 _Seq_sink::_M_overflow();
3646 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3649 auto __s = this->_M_used();
3652 if (__s.size() != 0)
3653 _Seq_sink::_M_overflow();
3659 _GLIBCXX_CONSTEXPR_FORMAT basic_string_view<_CharT>
3662 auto __span = _M_span();
3663 return basic_string_view<_CharT>(__span.data(), __span.size());
3667 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3669 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3675 template<
typename _CharT,
typename _OutIter>
3676 class _Iter_sink :
public _Buf_sink<_CharT>
3679 iter_difference_t<_OutIter> _M_max;
3682 size_t _M_count = 0;
3684 _GLIBCXX_CONSTEXPR_FORMAT
void
3685 _M_overflow()
override
3687 auto __s = this->_M_used();
3689 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3690 else if (_M_count <
static_cast<size_t>(_M_max))
3692 auto __max = _M_max - _M_count;
3693 span<_CharT> __first;
3694 if (__max < __s.size())
3695 __first = __s.first(
static_cast<size_t>(__max));
3698 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3701 _M_count += __s.size();
3704 _GLIBCXX_CONSTEXPR_FORMAT
bool
3705 _M_discarding()
const override
3713 [[__gnu__::__always_inline__]]
3714 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3715 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3716 : _M_out(std::move(__out)), _M_max(__max)
3719 using _Sink<_CharT>::out;
3721 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3724 if (this->_M_used().size() != 0)
3725 _Iter_sink::_M_overflow();
3726 iter_difference_t<_OutIter> __count(_M_count);
3738 template<
typename _CharT>
3739 class _Ptr_sink :
public _Sink<_CharT>
3741 static constexpr size_t _S_no_limit = size_t(-1);
3745 size_t _M_count = 0;
3750 _GLIBCXX_CONSTEXPR_FORMAT
void
3751 _M_overflow()
override
3753 if (this->_M_unused().size() != 0)
3756 auto __s = this->_M_used();
3758 if (_M_max != _S_no_limit)
3760 _M_count += __s.size();
3764 this->_M_reset(this->_M_buf);
3770 _M_rebuf(__s.data(), __s.size() + 1024, __s.size());
3774 _GLIBCXX_CONSTEXPR_FORMAT
bool
3775 _M_discarding()
const override
3782 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3783 _M_reserve(
size_t __n)
final
3785 auto __avail = this->_M_unused();
3786 if (__n > __avail.size())
3788 if (_M_max != _S_no_limit)
3791 auto __s = this->_M_used();
3792 _M_rebuf(__s.data(), __s.size() + __n, __s.size());
3798 template<
typename _IterDifference>
3799 static _GLIBCXX_CONSTEXPR_FORMAT
size_t
3800 _S_trim_max(_IterDifference __max)
3804 if constexpr (!is_integral_v<_IterDifference> ||
sizeof(__max) >
sizeof(size_t))
3806 if (_IterDifference((
size_t)-1) < __max)
3808 return size_t(__max);
3811 [[__gnu__::__always_inline__]]
3812 _GLIBCXX_CONSTEXPR_FORMAT
void
3813 _M_rebuf(_CharT* __ptr,
size_t __total,
size_t __inuse = 0)
3815 std::span<_CharT> __span(__ptr, __total);
3816 this->_M_reset(__span, __inuse);
3820 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3821 _Ptr_sink(_CharT* __ptr,
size_t __n = _S_no_limit) noexcept
3822 : _Sink<_CharT>(_M_buf), _M_max(__n)
3826 else if (__n != _S_no_limit)
3827 _M_rebuf(__ptr, __n);
3828#if __has_builtin(__builtin_dynamic_object_size)
3829 else if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3830 _M_rebuf(__ptr, __bytes /
sizeof(_CharT));
3835 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3836 __n = (1024 - __off) /
sizeof(_CharT);
3837 if (__n > 0) [[likely]]
3838 _M_rebuf(__ptr, __n);
3844 template<contiguous_iterator _OutIter>
3845 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3846 _Ptr_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1)
3847 : _Ptr_sink(std::
to_address(__out), _S_trim_max(__n))
3850 template<contiguous_iterator _OutIter>
3851 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3852 _M_finish(_OutIter __first)
const
3854 auto __s = this->_M_used();
3855 if (__s.data() == _M_buf)
3858 iter_difference_t<_OutIter> __m(_M_max);
3859 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3860 return { __first + __m, __count };
3864 iter_difference_t<_OutIter> __count(__s.size());
3865 return { __first + __count, __count };
3870 template<
typename _CharT,
typename _OutIter>
3871 concept __contiguous_char_iter
3872 = contiguous_iterator<_OutIter>
3873 && same_as<iter_value_t<_OutIter>, _CharT>;
3884 template<
typename _Out,
typename _CharT>
3885 class _Padding_sink :
public _Str_sink<_CharT>
3890 size_t _M_printwidth;
3892 [[__gnu__::__always_inline__]]
3893 _GLIBCXX_CONSTEXPR_FORMAT
bool
3895 {
return _M_printwidth >= _M_maxwidth; }
3897 [[__gnu__::__always_inline__]]
3898 _GLIBCXX_CONSTEXPR_FORMAT
bool
3899 _M_buffering()
const
3901 if (_M_printwidth < _M_padwidth)
3903 if (_M_maxwidth != (
size_t)-1)
3904 return _M_printwidth < _M_maxwidth;
3908 _GLIBCXX_CONSTEXPR_FORMAT
void
3909 _M_sync_discarding()
3911 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3912 if (_M_out._M_discarding())
3913 _M_maxwidth = _M_printwidth;
3916 _GLIBCXX_CONSTEXPR_FORMAT
void
3919 span<_CharT> __new = this->_M_used();
3920 basic_string_view<_CharT> __str(__new.data(), __new.size());
3921 _M_out = __format::__write(
std::move(_M_out), __str);
3922 _M_sync_discarding();
3926 _GLIBCXX_CONSTEXPR_FORMAT
bool
3929 auto __str = this->view();
3931 _M_printwidth = __format::__truncate(__str, _M_maxwidth);
3933 this->_M_trim(__str);
3939 if (_M_printwidth >= _M_padwidth)
3941 _M_out = __format::__write(
std::move(_M_out), __str);
3942 _M_sync_discarding();
3947 _Str_sink<_CharT>::_M_overflow();
3950 this->_M_reset(this->_M_buf);
3954 _GLIBCXX_CONSTEXPR_FORMAT
bool
3955 _M_update(
size_t __new)
3957 _M_printwidth += __new;
3959 if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
3960 return _M_force_update();
3964 _GLIBCXX_CONSTEXPR_FORMAT
void
3965 _M_overflow()
override
3971 else if (!_M_buffering())
3975 else if (_M_update(this->_M_used().size()))
3976 _Str_sink<_CharT>::_M_overflow();
3979 _GLIBCXX_CONSTEXPR_FORMAT
bool
3980 _M_discarding()
const override
3981 {
return _M_ignoring(); }
3983 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3984 _M_reserve(
size_t __n)
override
3989 else if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3990 if (!_M_buffering())
3993 if (!this->_M_used().empty())
3996 if (
auto __reserved = _M_out._M_reserve(__n))
3999 return _Sink<_CharT>::_M_reserve(__n);
4002 _GLIBCXX_CONSTEXPR_FORMAT
void
4003 _M_bump(
size_t __n)
override
4010 _Sink<_CharT>::_M_bump(__n);
4016 [[__gnu__::__always_inline__]]
4017 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4018 _Padding_sink(_Out __out,
size_t __padwidth,
size_t __maxwidth)
4019 : _M_padwidth(__padwidth), _M_maxwidth(__maxwidth),
4020 _M_out(std::move(__out)), _M_printwidth(0)
4021 { _M_sync_discarding(); }
4023 [[__gnu__::__always_inline__]]
4024 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4025 _Padding_sink(_Out __out,
size_t __padwidth)
4026 : _Padding_sink(std::move(__out), __padwidth, (size_t)-1)
4029 _GLIBCXX_CONSTEXPR_FORMAT _Out
4030 _M_finish(_Align __align,
char32_t __fill_char)
4033 if (
auto __rem = this->_M_used().size())
4037 else if (!_M_buffering())
4043 if (!_M_buffering() || !_M_force_update())
4045 if (_M_printwidth >= _M_padwidth)
4048 const auto __str = this->view();
4049 if (_M_printwidth >= _M_padwidth)
4050 return __format::__write(
std::move(_M_out), __str);
4052 const size_t __nfill = _M_padwidth - _M_printwidth;
4053 return __format::__write_padded(
std::move(_M_out), __str,
4054 __align, __nfill, __fill_char);
4058 template<
typename _Out,
typename _CharT>
4059 class _Escaping_sink :
public _Buf_sink<_CharT>
4061 using _Esc = _Escapes<_CharT>;
4064 _Term_char _M_term : 2;
4065 unsigned _M_prev_escape : 1;
4066 unsigned _M_out_discards : 1;
4068 _GLIBCXX_CONSTEXPR_FORMAT
void
4069 _M_sync_discarding()
4071 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4072 _M_out_discards = _M_out._M_discarding();
4075 _GLIBCXX_CONSTEXPR_FORMAT
void
4078 span<_CharT> __bytes = this->_M_used();
4079 basic_string_view<_CharT> __str(__bytes.data(), __bytes.size());
4082 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4084 bool __prev_escape = _M_prev_escape;
4085 _M_out = __format::__write_escaped_unicode_part(
4086 std::move(_M_out), __str, __prev_escape, _M_term);
4087 _M_prev_escape = __prev_escape;
4089 __rem = __str.size();
4090 if (__rem > 0 && __str.data() != this->_M_buf) [[unlikely]]
4091 ranges::move(__str, this->_M_buf);
4094 _M_out = __format::__write_escaped_ascii(
4097 this->_M_reset(this->_M_buf, __rem);
4098 _M_sync_discarding();
4101 _GLIBCXX_CONSTEXPR_FORMAT
void
4102 _M_overflow()
override
4104 if (_M_out_discards)
4110 _GLIBCXX_CONSTEXPR_FORMAT
bool
4111 _M_discarding()
const override
4112 {
return _M_out_discards; }
4115 [[__gnu__::__always_inline__]]
4116 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4117 _Escaping_sink(_Out __out, _Term_char __term)
4118 : _M_out(std::move(__out)), _M_term(__term),
4119 _M_prev_escape(true), _M_out_discards(false)
4121 _M_out = __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4122 _M_sync_discarding();
4125 _GLIBCXX_CONSTEXPR_FORMAT _Out
4128 if (_M_out_discards)
4131 if (!this->_M_used().empty())
4134 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4135 if (
auto __rem = this->_M_used(); !__rem.empty())
4137 basic_string_view<_CharT> __str(__rem.data(), __rem.size());
4138 _M_out = __format::__write_escape_seqs(
std::move(_M_out), __str);
4141 return __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4145 enum class _Arg_t :
unsigned char {
4146 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
4147 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
4148 _Arg_i128, _Arg_u128, _Arg_float128,
4149 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
4152#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4153 _Arg_ibm128 = _Arg_ldbl,
4154 _Arg_ieee128 = _Arg_float128,
4159 template<
typename _Context>
4162 using _CharT =
typename _Context::char_type;
4166 using _CharT =
typename _Context::char_type;
4167 using _Func = void(*)(basic_format_parse_context<_CharT>&,
4168 _Context&,
const void*);
4171 template<
typename _Tp>
4172 using __maybe_const_t
4173 = __conditional_t<__formattable_with<const _Tp, _Context>,
4176 template<
typename _Tq>
4177 static _GLIBCXX_CONSTEXPR_FORMAT
void
4178 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
4179 _Context& __format_ctx,
const void* __ptr)
4181 using _Td = remove_const_t<_Tq>;
4182 typename _Context::template formatter_type<_Td> __f;
4183 __parse_ctx.advance_to(__f.parse(__parse_ctx));
4184 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
4185 __format_ctx.advance_to(__f.format(__val, __format_ctx));
4188 template<
typename _Tp>
4189 requires (!is_same_v<remove_cv_t<_Tp>, handle>)
4190 explicit _GLIBCXX_CONSTEXPR_FORMAT
4191 handle(_Tp& __val) noexcept
4192 : _M_ptr(__builtin_addressof(__val))
4193 , _M_func(&_S_format<__maybe_const_t<_Tp>>)
4196 friend class basic_format_arg<_Context>;
4199 handle(
const handle&) =
default;
4200 handle& operator=(
const handle&) =
default;
4202 [[__gnu__::__always_inline__]]
4203 void _GLIBCXX_CONSTEXPR_FORMAT
4204 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
4205 { _M_func(__pc, __fc, this->_M_ptr); }
4220 unsigned long long _M_ull;
4223#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4224 long double _M_ldbl;
4227 __ieee128 _M_ieee128;
4229#ifdef __SIZEOF_FLOAT128__
4230 __float128 _M_float128;
4232 const _CharT* _M_str;
4233 basic_string_view<_CharT> _M_sv;
4236#ifdef __SIZEOF_INT128__
4238 unsigned __int128 _M_u128;
4240#ifdef __BFLT16_DIG__
4254 [[__gnu__::__always_inline__]]
4255 _GLIBCXX_CONSTEXPR_FORMAT
4256 _Arg_value() : _M_none() { }
4259 template<
typename _Tp>
4260 _GLIBCXX_CONSTEXPR_FORMAT
4261 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
4262 { _S_get<_Tp>() = __val; }
4267 template<
typename _Tp,
typename _Self,
typename... _Value>
4268 [[__gnu__::__always_inline__]]
4269 static _GLIBCXX_CONSTEXPR_FORMAT
auto&
4270 _S_access(_Self& __u, _Value... __value)
noexcept
4272 static_assert(
sizeof...(_Value) <= 1);
4273 if constexpr (is_same_v<_Tp, bool>)
4274 return (__u._M_bool = ... = __value);
4275 else if constexpr (is_same_v<_Tp, _CharT>)
4276 return (__u._M_c = ... = __value);
4277 else if constexpr (is_same_v<_Tp, int>)
4278 return (__u._M_i = ... = __value);
4279 else if constexpr (is_same_v<_Tp, unsigned>)
4280 return (__u._M_u = ... = __value);
4281 else if constexpr (is_same_v<_Tp, long long>)
4282 return (__u._M_ll = ... = __value);
4283 else if constexpr (is_same_v<_Tp, unsigned long long>)
4284 return (__u._M_ull = ... = __value);
4285 else if constexpr (is_same_v<_Tp, float>)
4286 return (__u._M_flt = ... = __value);
4287 else if constexpr (is_same_v<_Tp, double>)
4288 return (__u._M_dbl = ... = __value);
4289#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4290 else if constexpr (is_same_v<_Tp, long double>)
4291 return (__u._M_ldbl = ... = __value);
4293 else if constexpr (is_same_v<_Tp, __ibm128>)
4294 return (__u._M_ibm128 = ... = __value);
4295 else if constexpr (is_same_v<_Tp, __ieee128>)
4296 return (__u._M_ieee128 = ... = __value);
4298#ifdef __SIZEOF_FLOAT128__
4299 else if constexpr (is_same_v<_Tp, __float128>)
4300 return (__u._M_float128 = ... = __value);
4302 else if constexpr (is_same_v<_Tp, const _CharT*>)
4303 return (__u._M_str = ... = __value);
4304 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4305 return (__u._M_sv = ... = __value);
4306 else if constexpr (is_same_v<_Tp, const void*>)
4307 return (__u._M_ptr = ... = __value);
4308#ifdef __SIZEOF_INT128__
4309 else if constexpr (is_same_v<_Tp, __int128>)
4310 return (__u._M_i128 = ... = __value);
4311 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4312 return (__u._M_u128 = ... = __value);
4314#ifdef __BFLT16_DIG__
4315 else if constexpr (is_same_v<_Tp, __bflt16_t>)
4316 return (__u._M_bf16 = ... = __value);
4319 else if constexpr (is_same_v<_Tp, _Float16>)
4320 return (__u._M_f16 = ... = __value);
4323 else if constexpr (is_same_v<_Tp, _Float32>)
4324 return (__u._M_f32 = ... = __value);
4327 else if constexpr (is_same_v<_Tp, _Float64>)
4328 return (__u._M_f64 = ... = __value);
4330 else if constexpr (is_same_v<_Tp, handle>)
4331 return __u._M_handle;
4333 __builtin_unreachable();
4336 template<
typename _Tp>
4337 [[__gnu__::__always_inline__]]
4338 _GLIBCXX_CONSTEXPR_FORMAT
auto&
4340 {
return _S_access<_Tp>(*
this); }
4342 template<
typename _Tp>
4343 [[__gnu__::__always_inline__]]
4344 _GLIBCXX_CONSTEXPR_FORMAT
const auto&
4345 _M_get() const noexcept
4346 {
return _S_access<_Tp>(*
this); }
4348 template<
typename _Tp>
4349 [[__gnu__::__always_inline__]]
4350 _GLIBCXX_CONSTEXPR_FORMAT
void
4351 _M_set(_Tp __v)
noexcept
4354 if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4355 std::construct_at(&_M_sv, __v);
4356 else if constexpr (is_same_v<_Tp, handle>)
4357 std::construct_at(&_M_handle, __v);
4361 _S_access<_Tp>(*
this, __v);
4366 template<
typename _Context,
typename... _Args>
4369 template<
typename _Visitor,
typename _Ctx>
4370 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4371 __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4373 template<
typename _Ch,
typename _Tp>
4375 __to_arg_t_enum() noexcept;
4379 template<typename _Context>
4380 class basic_format_arg
4382 using _CharT =
typename _Context::char_type;
4385 using handle = __format::_Arg_value<_Context>::handle;
4387 [[__gnu__::__always_inline__]]
4388 _GLIBCXX_CONSTEXPR_FORMAT
4389 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
4391 [[nodiscard,__gnu__::__always_inline__]]
4392 explicit _GLIBCXX_CONSTEXPR_FORMAT
operator bool() const noexcept
4393 {
return _M_type != __format::_Arg_none; }
4395#if __cpp_lib_format >= 202306L
4396 template<
typename _Visitor>
4397 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4398 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4401 template<
typename _Res,
typename _Visitor>
4402 _GLIBCXX_CONSTEXPR_FORMAT _Res
4403 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4408 template<
typename _Ctx>
4409 friend class basic_format_args;
4411 template<
typename _Ctx,
typename... _Args>
4412 friend class __format::_Arg_store;
4414 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
4416 __format::_Arg_value<_Context> _M_val;
4417 __format::_Arg_t _M_type;
4422 template<
typename _Tp>
4423 static consteval auto
4426 using _Td = remove_const_t<_Tp>;
4427 if constexpr (is_same_v<_Td, bool>)
4428 return type_identity<bool>();
4429 else if constexpr (is_same_v<_Td, _CharT>)
4430 return type_identity<_CharT>();
4431 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
4432 return type_identity<_CharT>();
4433#ifdef __SIZEOF_INT128__
4434 else if constexpr (is_same_v<_Td, __int128>)
4435 return type_identity<__int128>();
4436 else if constexpr (is_same_v<_Td, unsigned __int128>)
4437 return type_identity<unsigned __int128>();
4439 else if constexpr (__is_signed_integer<_Td>::value)
4441 if constexpr (
sizeof(_Td) <=
sizeof(int))
4442 return type_identity<int>();
4443 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
4444 return type_identity<long long>();
4446 else if constexpr (__is_unsigned_integer<_Td>::value)
4448 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
4449 return type_identity<unsigned>();
4450 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
4451 return type_identity<unsigned long long>();
4453 else if constexpr (is_same_v<_Td, float>)
4454 return type_identity<float>();
4455 else if constexpr (is_same_v<_Td, double>)
4456 return type_identity<double>();
4457#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4458 else if constexpr (is_same_v<_Td, long double>)
4459 return type_identity<long double>();
4461 else if constexpr (is_same_v<_Td, __ibm128>)
4462 return type_identity<__ibm128>();
4463 else if constexpr (is_same_v<_Td, __ieee128>)
4464 return type_identity<__ieee128>();
4466#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4467 else if constexpr (is_same_v<_Td, __float128>)
4468 return type_identity<__float128>();
4470#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4471 else if constexpr (is_same_v<_Td, __format::__bflt16_t>)
4472 return type_identity<__format::__bflt16_t>();
4474#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4475 else if constexpr (is_same_v<_Td, _Float16>)
4476 return type_identity<_Float16>();
4478#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4479 else if constexpr (is_same_v<_Td, _Float32>)
4480 return type_identity<_Float32>();
4482#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4483 else if constexpr (is_same_v<_Td, _Float64>)
4484 return type_identity<_Float64>();
4486 else if constexpr (__is_specialization_of<_Td, basic_string_view>
4487 || __is_specialization_of<_Td, basic_string>)
4489 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
4490 return type_identity<basic_string_view<_CharT>>();
4492 return type_identity<handle>();
4494 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
4495 return type_identity<const _CharT*>();
4496 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
4497 return type_identity<const _CharT*>();
4498 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
4499 return type_identity<const void*>();
4500 else if constexpr (is_same_v<_Td, nullptr_t>)
4501 return type_identity<const void*>();
4503 return type_identity<handle>();
4507 template<
typename _Tp>
4508 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
4511 template<
typename _Tp>
4512 static consteval __format::_Arg_t
4515 using namespace __format;
4516 if constexpr (is_same_v<_Tp, bool>)
4518 else if constexpr (is_same_v<_Tp, _CharT>)
4520 else if constexpr (is_same_v<_Tp, int>)
4522 else if constexpr (is_same_v<_Tp, unsigned>)
4524 else if constexpr (is_same_v<_Tp, long long>)
4526 else if constexpr (is_same_v<_Tp, unsigned long long>)
4528 else if constexpr (is_same_v<_Tp, float>)
4530 else if constexpr (is_same_v<_Tp, double>)
4532#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4533 else if constexpr (is_same_v<_Tp, long double>)
4537 else if constexpr (is_same_v<_Tp, __ibm128>)
4539 else if constexpr (is_same_v<_Tp, __ieee128>)
4540 return _Arg_ieee128;
4542#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4543 else if constexpr (is_same_v<_Tp, __float128>)
4544 return _Arg_float128;
4546#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4547 else if constexpr (is_same_v<_Tp, __format::__bflt16_t>)
4550#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4551 else if constexpr (is_same_v<_Tp, _Float16>)
4554#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4555 else if constexpr (is_same_v<_Tp, _Float32>)
4558#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4559 else if constexpr (is_same_v<_Tp, _Float64>)
4562 else if constexpr (is_same_v<_Tp, const _CharT*>)
4564 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4566 else if constexpr (is_same_v<_Tp, const void*>)
4568#ifdef __SIZEOF_INT128__
4569 else if constexpr (is_same_v<_Tp, __int128>)
4571 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4574 else if constexpr (is_same_v<_Tp, handle>)
4578 template<
typename _Tp>
4579 _GLIBCXX_CONSTEXPR_FORMAT
void
4580 _M_set(_Tp __v)
noexcept
4582 _M_type = _S_to_enum<_Tp>();
4586 template<
typename _Tp>
4587 requires __format::__formattable_with<_Tp, _Context>
4588 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4589 basic_format_arg(_Tp& __v)
noexcept
4591 using _Td = _Normalize<_Tp>;
4592 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4593 _M_set(_Td{__v.data(), __v.size()});
4594 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4595 && is_same_v<_CharT, wchar_t>)
4596 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4598 _M_set(
static_cast<_Td
>(__v));
4601 template<
typename _Ctx,
typename... _Argz>
4602 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4603 make_format_args(_Argz&...)
noexcept;
4605 template<
typename _Visitor,
typename _Ctx>
4606 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4607 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4609 template<
typename _Visitor,
typename _Ctx>
4610 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4611 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4613 template<
typename _Ch,
typename _Tp>
4614 friend consteval __format::_Arg_t
4615 __format::__to_arg_t_enum() noexcept;
4617 template<typename _Visitor>
4618 _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4619 _M_visit(_Visitor&& __vis, __format::_Arg_t __type)
4621 using namespace __format;
4638#if __glibcxx_to_chars
4643#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4646#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4656#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4660#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4664#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4668#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4681#ifdef __SIZEOF_INT128__
4688 __builtin_unreachable();
4692 template<
typename _Visitor>
4693 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4694 _M_visit_user(_Visitor&& __vis, __format::_Arg_t __type)
4696 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4698 constexpr bool __user_facing = __is_one_of<_Tp,
4699 monostate, bool, _CharT,
4700 int,
unsigned int,
long long int,
unsigned long long int,
4701 float, double,
long double,
4702 const _CharT*, basic_string_view<_CharT>,
4703 const void*, handle>::value;
4704 if constexpr (__user_facing)
4715 template<
typename _Visitor,
typename _Context>
4716 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4717 inline _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4718 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4726 template<
typename _Visitor,
typename _Ctx>
4727 inline _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4728 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4733 struct _WidthPrecVisitor
4735 template<
typename _Tp>
4736 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4737 operator()(_Tp& __arg)
const
4739 if constexpr (is_same_v<_Tp, monostate>)
4740 __format::__invalid_arg_id_in_format_string();
4744 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4748 if constexpr (__is_unsigned_integer<_Tp>::value)
4750 else if constexpr (__is_signed_integer<_Tp>::value)
4754 __throw_format_error(
"format error: argument used for width or "
4755 "precision must be a non-negative integer");
4759#pragma GCC diagnostic push
4760#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4761 template<
typename _Context>
4762 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
4763 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4764 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4767 template<
int _Bits,
size_t _Nm>
4769 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4771 __UINT64_TYPE__ __packed_types = 0;
4772 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4773 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4774 return __packed_types;
4779 template<
typename _Context>
4780 class basic_format_args
4782 static constexpr int _S_packed_type_bits = 5;
4783 static constexpr int _S_packed_type_mask = 0b11111;
4784 static constexpr int _S_max_packed_args = 12;
4786 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4788 template<
typename... _Args>
4789 using _Store = __format::_Arg_store<_Context, _Args...>;
4791 template<
typename _Ctx,
typename... _Args>
4792 friend class __format::_Arg_store;
4794 using uint64_t = __UINT64_TYPE__;
4795 using _Format_arg = basic_format_arg<_Context>;
4796 using _Format_arg_val = __format::_Arg_value<_Context>;
4802 uint64_t _M_packed_size : 4;
4803 uint64_t _M_unpacked_size : 60;
4806 const _Format_arg_val* _M_values;
4807 const _Format_arg* _M_args;
4810 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4811 _M_size() const noexcept
4812 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4814 _GLIBCXX_CONSTEXPR_FORMAT
typename __format::_Arg_t
4815 _M_type(
size_t __i)
const noexcept
4817 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4818 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4821 template<
typename _Ctx,
typename... _Args>
4822 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4823 make_format_args(_Args&...)
noexcept;
4826 template<
typename... _Args>
4827 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4829 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4832 template<
typename... _Args>
4833 _GLIBCXX_CONSTEXPR_FORMAT
4834 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4836 [[nodiscard,__gnu__::__always_inline__]]
4837 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<_Context>
4838 get(
size_t __i)
const noexcept
4840 basic_format_arg<_Context> __arg;
4841 if (__i < _M_packed_size)
4843 __arg._M_type = _M_type(__i);
4844 __arg._M_val = _M_values[__i];
4846 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4847 __arg = _M_args[__i];
4854 template<
typename _Context,
typename... _Args>
4855 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4856 -> basic_format_args<_Context>;
4858 template<
typename _Context,
typename... _Args>
4859 _GLIBCXX_CONSTEXPR_FORMAT
auto
4860 make_format_args(_Args&... __fmt_args)
noexcept;
4863 template<
typename _Context,
typename... _Args>
4864 class __format::_Arg_store
4866 friend std::basic_format_args<_Context>;
4868 template<
typename _Ctx,
typename... _Argz>
4869 friend _GLIBCXX_CONSTEXPR_FORMAT
auto std::
4870#if _GLIBCXX_INLINE_VERSION
4873 make_format_args(_Argz&...)
noexcept;
4877 static constexpr bool _S_values_only
4878 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4881 = __conditional_t<_S_values_only,
4882 __format::_Arg_value<_Context>,
4883 basic_format_arg<_Context>>;
4885 _Element_t _M_args[
sizeof...(_Args)];
4887 template<
typename _Tp>
4888 static _GLIBCXX_CONSTEXPR_FORMAT _Element_t
4889 _S_make_elt(_Tp& __v)
4891 using _Tq = remove_const_t<_Tp>;
4892 using _CharT =
typename _Context::char_type;
4893 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4894 "std::formatter must be specialized for the type "
4895 "of each format arg");
4896 using __format::__formattable_with;
4897 if constexpr (is_const_v<_Tp>)
4898 if constexpr (!__formattable_with<_Tp, _Context>)
4899 if constexpr (__formattable_with<_Tq, _Context>)
4900 static_assert(__formattable_with<_Tp, _Context>,
4901 "format arg must be non-const because its "
4902 "std::formatter specialization has a "
4903 "non-const reference parameter");
4904 basic_format_arg<_Context> __arg(__v);
4905 if constexpr (_S_values_only)
4906 return __arg._M_val;
4911 template<
typename... _Tp>
4912 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4913 [[__gnu__::__always_inline__]]
4914 _GLIBCXX_CONSTEXPR_FORMAT
4915 _Arg_store(_Tp&... __a) noexcept
4916 : _M_args{_S_make_elt(__a)...}
4920 template<
typename _Context>
4921 class __format::_Arg_store<_Context>
4924 template<
typename _Context>
4925 template<
typename... _Args>
4926 inline _GLIBCXX_CONSTEXPR_FORMAT
4927 basic_format_args<_Context>::
4928 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4930 if constexpr (
sizeof...(_Args) == 0)
4933 _M_unpacked_size = 0;
4936 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4939 _M_packed_size =
sizeof...(_Args);
4942 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4944 _M_values = __store._M_args;
4951 _M_unpacked_size =
sizeof...(_Args);
4953 _M_args = __store._M_args;
4958 template<
typename _Context = format_context,
typename... _Args>
4959 [[nodiscard,__gnu__::__always_inline__]]
4960 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4961 make_format_args(_Args&... __fmt_args)
noexcept
4963 using _Fmt_arg = basic_format_arg<_Context>;
4964 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4965 _Normalize<_Args>...>;
4966 return _Store(__fmt_args...);
4969#ifdef _GLIBCXX_USE_WCHAR_T
4971 template<
typename... _Args>
4972 [[nodiscard,__gnu__::__always_inline__]]
4973 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4974 make_wformat_args(_Args&... __args)
noexcept
4975 {
return std::make_format_args<wformat_context>(__args...); }
4981 template<
typename _Out,
typename _CharT,
typename _Context>
4982 _GLIBCXX_CONSTEXPR_FORMAT _Out
4983 __do_vformat_to(_Out, basic_string_view<_CharT>,
4984 const basic_format_args<_Context>&,
4985 const locale* =
nullptr);
4987 template<
typename _CharT>
struct __formatter_chrono;
5005 template<
typename _Out,
typename _CharT>
5006 class basic_format_context
5008 static_assert( output_iterator<_Out, const _CharT&> );
5010 basic_format_args<basic_format_context> _M_args;
5012 __format::_Optional_locale _M_loc;
5014 _GLIBCXX_CONSTEXPR_FORMAT
5015 basic_format_context(basic_format_args<basic_format_context> __args,
5017 : _M_args(__args), _M_out(std::move(__out))
5020 _GLIBCXX_CONSTEXPR_FORMAT
5021 basic_format_context(basic_format_args<basic_format_context> __args,
5022 _Out __out,
const std::locale& __loc)
5023 : _M_args(__args), _M_out(std::move(__out)), _M_loc(__loc)
5029 basic_format_context(
const basic_format_context&) =
delete;
5030 basic_format_context& operator=(
const basic_format_context&) =
delete;
5032 template<
typename _Out2,
typename _CharT2,
typename _Context2>
5033 friend _GLIBCXX_CONSTEXPR_FORMAT _Out2
5034 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
5035 const basic_format_args<_Context2>&,
5038 friend __format::__formatter_chrono<_CharT>;
5041 ~basic_format_context() =
default;
5043 using iterator = _Out;
5044 using char_type = _CharT;
5045 template<
typename _Tp>
5046 using formatter_type = formatter<_Tp, _CharT>;
5049 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<basic_format_context>
5050 arg(
size_t __id)
const noexcept
5051 {
return _M_args.get(__id); }
5054 std::locale locale() {
return _M_loc.value(); }
5057 _GLIBCXX_CONSTEXPR_FORMAT iterator
5060 _GLIBCXX_CONSTEXPR_FORMAT
void
5061 advance_to(iterator __it) { _M_out =
std::move(__it); }
5073 template<
typename _CharT>
5076 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
5078 struct _Parse_context : basic_format_parse_context<_CharT>
5080 using basic_format_parse_context<_CharT>::basic_format_parse_context;
5081 const _Arg_t* _M_types =
nullptr;
5085 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5086 : _M_pc(__str, __nargs)
5089 constexpr iterator begin() const noexcept {
return _M_pc.begin(); }
5090 constexpr iterator end() const noexcept {
return _M_pc.end(); }
5095 basic_string_view<_CharT> __fmt = _M_fmt_str();
5097 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5099 _M_pc.advance_to(begin() + 1);
5100 _M_format_arg(_M_pc.next_arg_id());
5104 size_t __lbr = __fmt.find(
'{');
5105 size_t __rbr = __fmt.find(
'}');
5107 while (__fmt.size())
5109 auto __cmp = __lbr <=> __rbr;
5113 _M_pc.advance_to(end());
5118 if (__lbr + 1 == __fmt.size()
5119 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5120 __format::__unmatched_left_brace_in_format_string();
5121 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5122 iterator __last = begin() + __lbr + int(__is_escape);
5123 _M_on_chars(__last);
5124 _M_pc.advance_to(__last + 1);
5125 __fmt = _M_fmt_str();
5128 if (__rbr != __fmt.npos)
5130 __lbr = __fmt.find(
'{');
5134 _M_on_replacement_field();
5135 __fmt = _M_fmt_str();
5136 __lbr = __fmt.find(
'{');
5137 __rbr = __fmt.find(
'}');
5142 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5143 __format::__unmatched_right_brace_in_format_string();
5144 iterator __last = begin() + __rbr;
5145 _M_on_chars(__last);
5146 _M_pc.advance_to(__last + 1);
5147 __fmt = _M_fmt_str();
5148 if (__lbr != __fmt.npos)
5150 __rbr = __fmt.find(
'}');
5155 constexpr basic_string_view<_CharT>
5156 _M_fmt_str() const noexcept
5157 {
return {begin(), end()}; }
5159 constexpr virtual void _M_on_chars(iterator) { }
5161 constexpr void _M_on_replacement_field()
5163 auto __next = begin();
5167 __id = _M_pc.next_arg_id();
5168 else if (*__next ==
':')
5170 __id = _M_pc.next_arg_id();
5171 _M_pc.advance_to(++__next);
5175 auto [__i, __ptr] = __format::__parse_arg_id(begin(), end());
5176 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5177 __format::__invalid_arg_id_in_format_string();
5178 _M_pc.check_arg_id(__id = __i);
5181 _M_pc.advance_to(++__ptr);
5184 _M_pc.advance_to(__ptr);
5186 _M_format_arg(__id);
5187 if (begin() == end() || *begin() !=
'}')
5188 __format::__unmatched_left_brace_in_format_string();
5189 _M_pc.advance_to(begin() + 1);
5192 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5196 template<
typename _Out,
typename _CharT>
5197 class _Formatting_scanner :
public _Scanner<_CharT>
5200 _GLIBCXX_CONSTEXPR_FORMAT
5201 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5202 basic_string_view<_CharT> __str)
5203 : _Scanner<_CharT>(__str), _M_fc(__fc)
5207 basic_format_context<_Out, _CharT>& _M_fc;
5209 using iterator =
typename _Scanner<_CharT>::iterator;
5212 _M_on_chars(iterator __last)
override
5214 basic_string_view<_CharT> __str(this->begin(), __last);
5215 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5219 _M_format_arg(
size_t __id)
override
5221 using _Context = basic_format_context<_Out, _CharT>;
5222 using handle =
typename basic_format_arg<_Context>::handle;
5224 __format::__visit_format_arg([
this](
auto& __arg) {
5226 using _Formatter =
typename _Context::template formatter_type<_Type>;
5227 if constexpr (is_same_v<_Type, monostate>)
5228 __format::__invalid_arg_id_in_format_string();
5229 else if constexpr (is_same_v<_Type, handle>)
5230 __arg.format(this->_M_pc, this->_M_fc);
5231 else if constexpr (is_default_constructible_v<_Formatter>)
5234 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5235 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5238 static_assert(__format::__formattable_with<_Type, _Context>);
5239 }, _M_fc.arg(__id));
5243 template<
typename _CharT,
typename _Tp>
5245 __to_arg_t_enum() noexcept
5247 using _Context = __format::__format_context<_CharT>;
5248 using _Fmt_arg = basic_format_arg<_Context>;
5249 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5250 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5254 template<
typename _CharT,
typename... _Args>
5255 class _Checking_scanner :
public _Scanner<_CharT>
5258 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5259 "std::formatter must be specialized for each type being formatted");
5263 _Checking_scanner(basic_string_view<_CharT> __str)
5264 : _Scanner<_CharT>(__str, sizeof...(_Args))
5266#if __cpp_lib_format >= 202305L
5267 this->_M_pc._M_types = _M_types.data();
5273 _M_format_arg(
size_t __id)
override
5275 if constexpr (
sizeof...(_Args) != 0)
5277 if (__id <
sizeof...(_Args))
5279 _M_parse_format_spec<_Args...>(__id);
5283 __builtin_unreachable();
5286 template<
typename _Tp,
typename... _OtherArgs>
5288 _M_parse_format_spec(
size_t __id)
5292 formatter<_Tp, _CharT> __f;
5293 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5295 else if constexpr (
sizeof...(_OtherArgs) != 0)
5296 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5298 __builtin_unreachable();
5301#if __cpp_lib_format >= 202305L
5302 array<_Arg_t,
sizeof...(_Args)>
5303 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5307 template<typename _CharT, unsigned = __unicode::__literal_encoding_is_unicode<_CharT>()>
5308 _GLIBCXX_CONSTEXPR_FORMAT _Sink_iter<_CharT>
5309 __do_vformat_to(_Sink_iter<_CharT> __out, basic_string_view<_CharT> __fmt,
5310 __format_context<_CharT>& __ctx)
5312 if constexpr (is_same_v<_CharT, char>)
5314 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5316 bool __done =
false;
5317 __format::__visit_format_arg([&](
auto& __arg) {
5318 using _Tp = remove_cvref_t<
decltype(__arg)>;
5319 if constexpr (is_same_v<_Tp, bool>)
5321 size_t __len = 4 + !__arg;
5322 const char* __chars[] = {
"false",
"true" };
5323 if (
auto __res = __out._M_reserve(__len))
5325 ranges::copy_n(__chars[__arg], __len, __res.get());
5326 __res._M_bump(__len);
5330 else if constexpr (is_same_v<_Tp, char>)
5332 if (
auto __res = __out._M_reserve(1))
5334 *__res.get() = __arg;
5339 else if constexpr (is_integral_v<_Tp>)
5341 make_unsigned_t<_Tp> __uval;
5342 const bool __neg = __arg < 0;
5344 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5347 const auto __n = __detail::__to_chars_len(__uval);
5348 if (
auto __res = __out._M_reserve(__n + __neg))
5350 auto __ptr = __res.get();
5352 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5354 __res._M_bump(__n + __neg);
5358 else if constexpr (is_convertible_v<_Tp, string_view>)
5360 string_view __sv = __arg;
5361 if (
auto __res = __out._M_reserve(__sv.size()))
5363 ranges::copy(__sv, __res.get());
5364 __res._M_bump(__sv.size());
5374 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5375 __scanner._M_scan();
5384#if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE > 0
5385 extern template _Sink_iter<char>
5386 __do_vformat_to<char, 1>(_Sink_iter<char>, string_view,
5388# ifdef _GLIBCXX_USE_WCHAR_T
5389 extern template _Sink_iter<wchar_t>
5390 __do_vformat_to<wchar_t, 1>(_Sink_iter<wchar_t>, wstring_view,
5395 template<
typename _Out,
typename _CharT,
typename _Context>
5396 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5397 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5398 const basic_format_args<_Context>& __args,
5399 const locale* __loc)
5401 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5403 auto __ctx = __loc ==
nullptr
5404 ? _Context(__args, __out)
5405 : _Context(__args, __out, *__loc);
5406 return __format::__do_vformat_to(__out, __fmt, __ctx);
5408 else if constexpr (__contiguous_char_iter<_CharT, _Out>)
5410 _Ptr_sink<_CharT> __sink(__out);
5411 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5412 return std::move(__sink)._M_finish(__out).out;
5416 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5417 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5418 return std::move(__sink)._M_finish().out;
5422 template<
typename _Out,
typename _CharT>
5423 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5424 __do_vformat_to_n(_Out __out, iter_difference_t<_Out> __n,
5425 basic_string_view<_CharT> __fmt,
5426 const type_identity_t<
5427 basic_format_args<__format_context<_CharT>>>& __args,
5428 const locale* __loc =
nullptr)
5430 if constexpr (__contiguous_char_iter<_CharT, _Out>)
5432 _Ptr_sink<_CharT> __sink(__out, __n);
5433 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5434 return std::move(__sink)._M_finish(__out);
5438 _Iter_sink<_CharT, _Out> __sink(
std::move(__out), __n);
5439 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5444#pragma GCC diagnostic pop
5449#if __cpp_lib_format >= 202305L
5452 template<
typename _CharT>
5453 template<
typename... _Ts>
5455 basic_format_parse_context<_CharT>::
5456 __check_dynamic_spec(
size_t __id)
noexcept
5458 if (__id >= _M_num_args)
5459 __format::__invalid_arg_id_in_format_string();
5460 if constexpr (
sizeof...(_Ts) != 0)
5462 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
5463 auto* __args =
static_cast<_Parse_ctx*
>(
this)->_M_types;
5468 auto __arg = __args[__id];
5469 __format::_Arg_t __types[] = {
5470 __format::__to_arg_t_enum<_CharT, _Ts>()...
5472 for (
auto __t : __types)
5476 __invalid_dynamic_spec(
"arg(id) type does not match");
5481 template<
typename _CharT,
typename... _Args>
5482 template<
typename _Tp>
5483 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5485 basic_format_string<_CharT, _Args...>::
5486 basic_format_string(
const _Tp& __s) noexcept
5489 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5491 __scanner._M_scan();
5496 template<
typename _Out>
requires output_iterator<_Out, const char&>
5497 [[__gnu__::__always_inline__]]
5498 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5499 vformat_to(_Out __out, string_view __fmt, format_args __args)
5500 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5502#ifdef _GLIBCXX_USE_WCHAR_T
5503 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5504 [[__gnu__::__always_inline__]]
5505 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5506 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5507 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5510 template<
typename _Out>
requires output_iterator<_Out, const char&>
5511 [[__gnu__::__always_inline__]]
5513 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5516 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5519#ifdef _GLIBCXX_USE_WCHAR_T
5520 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5521 [[__gnu__::__always_inline__]]
5523 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5524 wformat_args __args)
5526 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5531 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5532 vformat(string_view __fmt, format_args __args)
5534 __format::_Str_sink<char> __buf;
5535 std::vformat_to(__buf.out(), __fmt, __args);
5539#ifdef _GLIBCXX_USE_WCHAR_T
5541 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5542 vformat(wstring_view __fmt, wformat_args __args)
5544 __format::_Str_sink<wchar_t> __buf;
5545 std::vformat_to(__buf.out(), __fmt, __args);
5552 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5554 __format::_Str_sink<char> __buf;
5555 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5559#ifdef _GLIBCXX_USE_WCHAR_T
5562 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5564 __format::_Str_sink<wchar_t> __buf;
5565 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5570 template<
typename... _Args>
5572 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5573 format(format_string<_Args...> __fmt, _Args&&... __args)
5574 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5576#ifdef _GLIBCXX_USE_WCHAR_T
5577 template<
typename... _Args>
5579 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5580 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5581 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5584 template<
typename... _Args>
5587 format(
const locale& __loc, format_string<_Args...> __fmt,
5590 return std::vformat(__loc, __fmt.get(),
5591 std::make_format_args(__args...));
5594#ifdef _GLIBCXX_USE_WCHAR_T
5595 template<
typename... _Args>
5598 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5601 return std::vformat(__loc, __fmt.get(),
5602 std::make_wformat_args(__args...));
5606 template<
typename _Out,
typename... _Args>
5607 requires output_iterator<_Out, const char&>
5608 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5609 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5611 return std::vformat_to(
std::move(__out), __fmt.get(),
5612 std::make_format_args(__args...));
5615#ifdef _GLIBCXX_USE_WCHAR_T
5616 template<
typename _Out,
typename... _Args>
5617 requires output_iterator<_Out, const wchar_t&>
5618 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5619 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5621 return std::vformat_to(
std::move(__out), __fmt.get(),
5622 std::make_wformat_args(__args...));
5626 template<
typename _Out,
typename... _Args>
5627 requires output_iterator<_Out, const char&>
5629 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5632 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5633 std::make_format_args(__args...));
5636#ifdef _GLIBCXX_USE_WCHAR_T
5637 template<
typename _Out,
typename... _Args>
5638 requires output_iterator<_Out, const wchar_t&>
5640 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5643 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5644 std::make_wformat_args(__args...));
5648 template<
typename _Out,
typename... _Args>
5649 requires output_iterator<_Out, const char&>
5650 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5651 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5652 format_string<_Args...> __fmt, _Args&&... __args)
5654 return __format::__do_vformat_to_n(
5656 std::make_format_args(__args...));
5659#ifdef _GLIBCXX_USE_WCHAR_T
5660 template<
typename _Out,
typename... _Args>
5661 requires output_iterator<_Out, const wchar_t&>
5662 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5663 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5664 wformat_string<_Args...> __fmt, _Args&&... __args)
5666 return __format::__do_vformat_to_n(
5668 std::make_wformat_args(__args...));
5672 template<
typename _Out,
typename... _Args>
5673 requires output_iterator<_Out, const char&>
5674 inline format_to_n_result<_Out>
5675 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5676 format_string<_Args...> __fmt, _Args&&... __args)
5678 return __format::__do_vformat_to_n(
5680 std::make_format_args(__args...), &__loc);
5683#ifdef _GLIBCXX_USE_WCHAR_T
5684 template<
typename _Out,
typename... _Args>
5685 requires output_iterator<_Out, const wchar_t&>
5686 inline format_to_n_result<_Out>
5687 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5688 wformat_string<_Args...> __fmt, _Args&&... __args)
5690 return __format::__do_vformat_to_n(
5692 std::make_wformat_args(__args...), &__loc);
5700 template<
typename _CharT>
5701 class _Counting_sink final :
public _Ptr_sink<_CharT>
5704 _GLIBCXX_CONSTEXPR_FORMAT
5705 _Counting_sink() : _Ptr_sink<_CharT>(nullptr, 0) { }
5707 [[__gnu__::__always_inline__]]
5708 _GLIBCXX_CONSTEXPR_FORMAT
size_t
5710 {
return this->_M_count + this->_M_used().size(); }
5713 template<
typename _CharT>
5714 class _Counting_sink :
public _Buf_sink<_CharT>
5716 size_t _M_count = 0;
5719 _M_overflow()
override
5721 if (!std::is_constant_evaluated())
5722 _M_count += this->_M_used().size();
5727 _Counting_sink() =
default;
5729 [[__gnu__::__always_inline__]]
5733 _Counting_sink::_M_overflow();
5741 template<
typename... _Args>
5743 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5744 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5746 __format::_Counting_sink<char> __buf;
5747 std::vformat_to(__buf.out(), __fmt.get(),
5748 std::make_format_args(__args...));
5749 return __buf.count();
5752#ifdef _GLIBCXX_USE_WCHAR_T
5753 template<
typename... _Args>
5755 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5756 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5758 __format::_Counting_sink<wchar_t> __buf;
5759 std::vformat_to(__buf.out(), __fmt.get(),
5760 std::make_wformat_args(__args...));
5761 return __buf.count();
5765 template<
typename... _Args>
5768 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5771 __format::_Counting_sink<char> __buf;
5772 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5773 std::make_format_args(__args...));
5774 return __buf.count();
5777#ifdef _GLIBCXX_USE_WCHAR_T
5778 template<
typename... _Args>
5781 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5784 __format::_Counting_sink<wchar_t> __buf;
5785 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5786 std::make_wformat_args(__args...));
5787 return __buf.count();
5791#if __glibcxx_format_ranges
5793 template<typename _Tp>
5794 consteval range_format
5797 using _Ref = ranges::range_reference_t<_Tp>;
5798 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5799 return range_format::disabled;
5800 else if constexpr (
requires {
typename _Tp::key_type; })
5802 if constexpr (
requires {
typename _Tp::mapped_type; })
5804 using _Up = remove_cvref_t<_Ref>;
5805 if constexpr (__is_pair<_Up>)
5806 return range_format::map;
5807 else if constexpr (__is_specialization_of<_Up, tuple>)
5808 if constexpr (tuple_size_v<_Up> == 2)
5809 return range_format::map;
5811 return range_format::set;
5814 return range_format::sequence;
5819 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5820 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5825 template<
typename _CharT,
typename _Out,
typename _Callback>
5826 _GLIBCXX_CONSTEXPR_FORMAT
5827 typename basic_format_context<_Out, _CharT>::iterator
5828 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5829 const _Spec<_CharT>& __spec,
5832 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
5838 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
5840 const size_t __padwidth = __spec._M_get_width(__fc);
5841 if (__padwidth == 0)
5842 return __call(__fc);
5846 _GLIBCXX_CONSTEXPR_FORMAT
5847 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5848 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5851 _GLIBCXX_CONSTEXPR_FORMAT
void
5853 { _M_ctx =
nullptr; }
5855 _GLIBCXX_CONSTEXPR_FORMAT
5859 _M_ctx->advance_to(_M_out);
5863 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5864 _Sink_iter<_CharT> _M_out;
5867 _Restore_out __restore(__fc);
5868 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
5869 __fc.advance_to(__sink.out());
5871 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
5872 __restore._M_disarm();
5877 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5878 struct __indexed_formatter_storage
5883 basic_format_parse_context<_CharT> __pc({});
5884 if (_M_formatter.parse(__pc) != __pc.end())
5885 __format::__failed_to_parse_format_spec();
5888 template<
typename _Out>
5889 _GLIBCXX_CONSTEXPR_FORMAT
void
5890 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5891 basic_format_context<_Out, _CharT>& __fc,
5892 basic_string_view<_CharT> __sep)
const
5894 if constexpr (_Pos != 0)
5895 __fc.advance_to(__format::__write(__fc.out(), __sep));
5896 __fc.advance_to(_M_formatter.format(__elem, __fc));
5899 [[__gnu__::__always_inline__]]
5903 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5904 _M_formatter.set_debug_format();
5908 formatter<_Tp, _CharT> _M_formatter;
5911 template<
typename _CharT,
typename... _Tps>
5912 class __tuple_formatter
5914 using _String_view = basic_string_view<_CharT>;
5915 using _Seps = __format::_Separators<_CharT>;
5919 set_separator(basic_string_view<_CharT> __sep)
noexcept
5923 set_brackets(basic_string_view<_CharT> __open,
5924 basic_string_view<_CharT> __close)
noexcept
5932 constexpr typename basic_format_parse_context<_CharT>::iterator
5933 parse(basic_format_parse_context<_CharT>& __pc)
5935 auto __first = __pc.begin();
5936 const auto __last = __pc.end();
5937 __format::_Spec<_CharT> __spec{};
5939 auto __finished = [&]
5941 if (__first != __last && *__first !=
'}')
5945 _M_felems._M_parse();
5946 _M_felems.set_debug_format();
5953 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5957 __first = __spec._M_parse_width(__first, __last, __pc);
5961 if (*__first ==
'n')
5964 _M_open = _M_close = _String_view();
5966 else if (*__first ==
'm')
5969 if constexpr (
sizeof...(_Tps) == 2)
5971 _M_sep = _Seps::_S_colon();
5972 _M_open = _M_close = _String_view();
5975 __throw_format_error(
"format error: 'm' specifier requires range"
5976 " of pair or tuple of two elements");
5982 __format::__failed_to_parse_format_spec();
5986 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
5987 _GLIBCXX_CONSTEXPR_FORMAT
5988 typename basic_format_context<_Out, _CharT>::iterator
5989 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
5990 basic_format_context<_Out, _CharT>& __fc)
const
5991 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
5993 template<
typename _Out>
5994 _GLIBCXX_CONSTEXPR_FORMAT
5995 typename basic_format_context<_Out, _CharT>::iterator
5996 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
5997 basic_format_context<_Out, _CharT>& __fc)
const
5999 return __format::__format_padded(
6001 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
6003 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
6004 _M_felems._M_format(__elems..., __nfc, _M_sep);
6005 return __format::__write(__nfc.out(), _M_close);
6010 template<
size_t... _Ids>
6011 struct __formatters_storage
6012 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
6014 template<
size_t _Id,
typename _Up>
6015 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
6020 (_Base<_Ids, _Tps>::_M_parse(), ...);
6023 template<
typename _Out>
6024 _GLIBCXX_CONSTEXPR_FORMAT
void
6025 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
6026 basic_format_context<_Out, _CharT>& __fc,
6027 _String_view __sep)
const
6029 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
6035 (_Base<_Ids, _Tps>::set_debug_format(), ...);
6039 template<
size_t... _Ids>
6040 static _GLIBCXX_CONSTEXPR_FORMAT
auto
6041 _S_create_storage(index_sequence<_Ids...>)
6042 -> __formatters_storage<_Ids...>;
6044 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
6046 _Spec<_CharT> _M_spec{};
6047 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
6048 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
6049 _String_view _M_sep = _Seps::_S_comma();
6050 _Formatters _M_felems;
6053 template<
typename _Tp>
6054 concept __is_map_formattable
6055 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
6061 template<__format::__
char _CharT, formattable<_CharT> _Fp,
6062 formattable<_CharT> _Sp>
6063 struct formatter<
pair<_Fp, _Sp>, _CharT>
6064 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
6065 remove_cvref_t<_Sp>>
6068 using __maybe_const_pair
6069 = __conditional_t<formattable<const _Fp, _CharT>
6070 && formattable<const _Sp, _CharT>,
6071 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
6075 template<
typename _Out>
6076 _GLIBCXX_CONSTEXPR_FORMAT
6077 typename basic_format_context<_Out, _CharT>::iterator
6078 format(__maybe_const_pair& __p,
6079 basic_format_context<_Out, _CharT>& __fc)
const
6080 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
6083#if __glibcxx_print >= 202406L
6086 template<
typename _Fp,
typename _Sp>
6087 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
6088 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
6089 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
6092 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
6093 struct formatter<tuple<_Tps...>, _CharT>
6094 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
6097 using __maybe_const_tuple
6098 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
6099 const tuple<_Tps...>, tuple<_Tps...>>;
6103 template<
typename _Out>
6104 _GLIBCXX_CONSTEXPR_FORMAT
6105 typename basic_format_context<_Out, _CharT>::iterator
6106 format(__maybe_const_tuple& __t,
6107 basic_format_context<_Out, _CharT>& __fc)
const
6108 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
6111#if __glibcxx_print >= 202406L
6114 template<
typename... _Tps>
6115 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
6116 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
6120 template<
typename _Tp, __format::__
char _CharT>
6121 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
6122 class range_formatter
6124 using _String_view = basic_string_view<_CharT>;
6125 using _Seps = __format::_Separators<_CharT>;
6129 set_separator(basic_string_view<_CharT> __sep)
noexcept
6133 set_brackets(basic_string_view<_CharT> __open,
6134 basic_string_view<_CharT> __close)
noexcept
6140 constexpr formatter<_Tp, _CharT>&
6141 underlying() noexcept
6144 constexpr const formatter<_Tp, _CharT>&
6145 underlying() const noexcept
6150 constexpr typename basic_format_parse_context<_CharT>::iterator
6151 parse(basic_format_parse_context<_CharT>& __pc)
6153 auto __first = __pc.begin();
6154 const auto __last = __pc.end();
6155 __format::_Spec<_CharT> __spec{};
6156 bool __no_brace =
false;
6158 auto __finished = [&]
6159 {
return __first == __last || *__first ==
'}'; };
6161 auto __finalize = [&]
6167 auto __parse_val = [&](_String_view __nfs = _String_view())
6169 basic_format_parse_context<_CharT> __npc(__nfs);
6170 if (_M_fval.parse(__npc) != __npc.end())
6171 __format::__failed_to_parse_format_spec();
6172 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6173 _M_fval.set_debug_format();
6174 return __finalize();
6178 return __parse_val();
6180 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6182 return __parse_val();
6184 __first = __spec._M_parse_width(__first, __last, __pc);
6186 return __parse_val();
6188 if (*__first ==
'?')
6191 __spec._M_debug =
true;
6192 if (__finished() || *__first !=
's')
6193 __throw_format_error(
"format error: '?' is allowed only in"
6194 " combination with 's'");
6197 if (*__first ==
's')
6200 if constexpr (same_as<_Tp, _CharT>)
6202 __spec._M_type = __format::_Pres_s;
6204 return __finalize();
6205 __throw_format_error(
"format error: element format specifier"
6206 " cannot be provided when 's' specifier is used");
6209 __throw_format_error(
"format error: 's' specifier requires"
6210 " range of character types");
6214 return __parse_val();
6216 if (*__first ==
'n')
6219 _M_open = _M_close = _String_view();
6224 return __parse_val();
6226 if (*__first ==
'm')
6228 _String_view __m(__first, 1);
6230 if constexpr (__format::__is_map_formattable<_Tp>)
6232 _M_sep = _Seps::_S_comma();
6235 _M_open = _Seps::_S_braces().substr(0, 1);
6236 _M_close = _Seps::_S_braces().substr(1, 1);
6239 return __parse_val(__m);
6240 __throw_format_error(
"format error: element format specifier"
6241 " cannot be provided when 'm' specifier is used");
6244 __throw_format_error(
"format error: 'm' specifier requires"
6245 " range of pairs or tuples of two elements");
6249 return __parse_val();
6251 if (*__first ==
':')
6253 __pc.advance_to(++__first);
6254 __first = _M_fval.parse(__pc);
6258 return __finalize();
6260 __format::__failed_to_parse_format_spec();
6265 template<ranges::input_range _Rg,
typename _Out>
6266 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6267 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6268 _GLIBCXX_CONSTEXPR_FORMAT
6269 typename basic_format_context<_Out, _CharT>::iterator
6270 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6272 using _Range = remove_reference_t<_Rg>;
6273 if constexpr (ranges::contiguous_range<_Rg>)
6275 const span<__format::__maybe_const<_Tp, _CharT>>
6276 __spn(ranges::data(__rg),
size_t(ranges::distance(__rg)));
6277 return _M_format(__spn, __fc);
6279 else if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6280 return _M_format<const _Range>(__rg, __fc);
6282 return _M_format(__rg, __fc);
6286 template<ranges::input_range _Rg,
typename _Out>
6287 _GLIBCXX_CONSTEXPR_FORMAT
6288 typename basic_format_context<_Out, _CharT>::iterator
6289 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6291 if constexpr (same_as<_Tp, _CharT>)
6292 if (_M_spec._M_type == __format::_Pres_s)
6294 __format::__formatter_str __fstr(_M_spec);
6295 return __fstr._M_format_range(__rg, __fc);
6297 return __format::__format_padded(
6299 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6300 {
return _M_format_elems(__rg, __nfc); });
6304 template<ranges::input_range _Rg,
typename _Out>
6305 _GLIBCXX_CONSTEXPR_FORMAT
6306 typename basic_format_context<_Out, _CharT>::iterator
6307 _M_format_elems(_Rg& __rg,
6308 basic_format_context<_Out, _CharT>& __fc)
const
6310 auto __out = __format::__write(__fc.out(), _M_open);
6312 auto __first = ranges::begin(__rg);
6313 auto const __last = ranges::end(__rg);
6314 if (__first == __last)
6315 return __format::__write(__out, _M_close);
6317 __fc.advance_to(__out);
6318 __out = _M_fval.format(*__first, __fc);
6319 for (++__first; __first != __last; ++__first)
6321 __out = __format::__write(__out, _M_sep);
6322 __fc.advance_to(__out);
6323 __out = _M_fval.format(*__first, __fc);
6326 return __format::__write(__out, _M_close);
6329 __format::_Spec<_CharT> _M_spec{};
6330 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6331 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6332 _String_view _M_sep = _Seps::_S_comma();
6333 formatter<_Tp, _CharT> _M_fval;
6341 template<ranges::input_range _Rg, __format::__
char _CharT>
6342 requires (format_kind<_Rg> != range_format::disabled)
6343 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6344 struct formatter<_Rg, _CharT>
6347 static const bool _S_range_format_is_string =
6348 (format_kind<_Rg> == range_format::string)
6349 || (format_kind<_Rg> == range_format::debug_string);
6350 using _Vt = remove_cvref_t<
6351 ranges::range_reference_t<
6352 __format::__maybe_const_range<_Rg, _CharT>>>;
6354 static consteval bool _S_is_correct()
6356 if constexpr (_S_range_format_is_string)
6357 static_assert(same_as<_Vt, _CharT>);
6361 static_assert(_S_is_correct());
6364 constexpr formatter() noexcept
6366 using _Seps = __format::_Separators<_CharT>;
6367 if constexpr (format_kind<_Rg> == range_format::map)
6369 static_assert(__format::__is_map_formattable<_Vt>);
6370 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6371 _Seps::_S_braces().substr(1, 1));
6372 _M_under.underlying().set_brackets({}, {});
6373 _M_under.underlying().set_separator(_Seps::_S_colon());
6375 else if constexpr (format_kind<_Rg> == range_format::set)
6376 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6377 _Seps::_S_braces().substr(1, 1));
6381 set_separator(basic_string_view<_CharT> __sep)
noexcept
6382 requires (format_kind<_Rg> == range_format::sequence)
6383 { _M_under.set_separator(__sep); }
6386 set_brackets(basic_string_view<_CharT> __open,
6387 basic_string_view<_CharT> __close)
noexcept
6388 requires (format_kind<_Rg> == range_format::sequence)
6389 { _M_under.set_brackets(__open, __close); }
6393 constexpr typename basic_format_parse_context<_CharT>::iterator
6394 parse(basic_format_parse_context<_CharT>& __pc)
6396 auto __res = _M_under.parse(__pc);
6397 if constexpr (format_kind<_Rg> == range_format::debug_string)
6398 _M_under.set_debug_format();
6404 template<
typename _Out>
6405 _GLIBCXX_CONSTEXPR_FORMAT
6406 typename basic_format_context<_Out, _CharT>::iterator
6407 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6408 basic_format_context<_Out, _CharT>& __fc)
const
6410 if constexpr (_S_range_format_is_string)
6411 return _M_under._M_format_range(__rg, __fc);
6413 return _M_under.format(__rg, __fc);
6417 using _Formatter_under
6418 = __conditional_t<_S_range_format_is_string,
6419 __format::__formatter_str<_CharT>,
6420 range_formatter<_Vt, _CharT>>;
6421 _Formatter_under _M_under;
6424#if __glibcxx_print >= 202406L
6425 template<ranges::input_range _Rg>
6426 requires (format_kind<_Rg> != range_format::disabled)
6427 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6431#undef _GLIBCXX_WIDEN
6432#undef _GLIBCXX_CONSTEXPR_FORMAT
6434_GLIBCXX_END_NAMESPACE_VERSION
6437#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.