29#ifndef _GLIBCXX_FORMAT
30#define _GLIBCXX_FORMAT 1
33#pragma GCC system_header
38#define __glibcxx_want_format
39#define __glibcxx_want_format_ranges
40#define __glibcxx_want_format_uchar
41#define __glibcxx_want_constexpr_exceptions
42#define __glibcxx_want_constexpr_format
45#ifdef __cpp_lib_format
67#ifdef __glibcxx_constexpr_format
68# define _GLIBCXX_CONSTEXPR_FORMAT constexpr
70# define _GLIBCXX_CONSTEXPR_FORMAT
73#pragma GCC diagnostic push
74#pragma GCC diagnostic ignored "-Wpedantic"
75#pragma GCC diagnostic ignored "-Wc++23-extensions"
77namespace std _GLIBCXX_VISIBILITY(default)
79_GLIBCXX_BEGIN_NAMESPACE_VERSION
82 template<
typename _CharT,
typename... _Args>
struct basic_format_string;
92 template<
typename _CharT>
94 _Widen(
const char* __narrow,
const wchar_t* __wide)
96 if constexpr (is_same_v<_CharT, wchar_t>)
101#define _GLIBCXX_WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
102#define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
105 template<
typename _CharT>
106 constexpr size_t __stackbuf_size = 32 *
sizeof(
void*) /
sizeof(_CharT);
109 template<
typename _CharT>
class _Sink;
110 template<
typename _CharT>
class _Fixedbuf_sink;
111 template<
typename _Out,
typename _CharT>
class _Padding_sink;
112 template<
typename _Out,
typename _CharT>
class _Escaping_sink;
115 template<
typename _CharT>
119 template<
typename _CharT>
123 template<
typename _CharT>
125 {
using type = _Drop_iter<_CharT>; };
127 template<
typename _CharT>
128 using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
130 template<
typename _CharT>
131 struct _Dynamic_format_string
133 [[__gnu__::__always_inline__]]
134 _GLIBCXX_CONSTEXPR_FORMAT
135 _Dynamic_format_string(basic_string_view<_CharT> __s) noexcept
138 _Dynamic_format_string(
const _Dynamic_format_string&) =
delete;
139 void operator=(
const _Dynamic_format_string&) =
delete;
142 basic_string_view<_CharT> _M_str;
144 template<
typename,
typename...>
friend struct std::basic_format_string;
150 using format_context = __format::__format_context<char>;
151#ifdef _GLIBCXX_USE_WCHAR_T
152 using wformat_context = __format::__format_context<wchar_t>;
156 template<
typename _Context>
class basic_format_args;
157 using format_args = basic_format_args<format_context>;
158#ifdef _GLIBCXX_USE_WCHAR_T
159 using wformat_args = basic_format_args<wformat_context>;
164 template<
typename _Context>
165 class basic_format_arg;
171 template<
typename _CharT,
typename... _Args>
172 struct basic_format_string
174 template<
typename _Tp>
175 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
177 basic_format_string(
const _Tp& __s)
noexcept;
179 [[__gnu__::__always_inline__]]
180 _GLIBCXX_CONSTEXPR_FORMAT
181 basic_format_string(__format::_Dynamic_format_string<_CharT> __s) noexcept
185 [[__gnu__::__always_inline__]]
186 constexpr basic_string_view<_CharT>
191 basic_string_view<_CharT> _M_str;
194 template<
typename... _Args>
195 using format_string = basic_format_string<char, type_identity_t<_Args>...>;
197#ifdef _GLIBCXX_USE_WCHAR_T
198 template<
typename... _Args>
200 = basic_format_string<wchar_t, type_identity_t<_Args>...>;
203#if __cpp_lib_format >= 202603L
204 [[__gnu__::__always_inline__]]
205 inline _GLIBCXX_CONSTEXPR_FORMAT __format::_Dynamic_format_string<char>
206 dynamic_format(string_view __fmt)
noexcept
209#ifdef _GLIBCXX_USE_WCHAR_T
210 [[__gnu__::__always_inline__]]
211 inline _GLIBCXX_CONSTEXPR_FORMAT __format::_Dynamic_format_string<wchar_t>
212 dynamic_format(wstring_view __fmt)
noexcept
220 template<
typename _Tp,
typename _CharT>
223 formatter() =
delete;
224 formatter(
const formatter&) =
delete;
225 formatter& operator=(
const formatter&) =
delete;
232 _GLIBCXX_CONSTEXPR_FORMAT
233 explicit format_error(
const string& __what)
234 : runtime_error(__what) { }
236 _GLIBCXX_CONSTEXPR_FORMAT
explicit
237 format_error(
const char* __what)
238 : runtime_error(__what) { }
243 inline _GLIBCXX_CONSTEXPR_FORMAT
void
244 __throw_format_error(
const char* __what)
245 { _GLIBCXX_THROW_OR_ABORT(format_error(__what)); }
252 inline _GLIBCXX_CONSTEXPR_FORMAT
void
253 __unmatched_left_brace_in_format_string()
254 { __throw_format_error(
"format error: unmatched '{' in format string"); }
257 inline _GLIBCXX_CONSTEXPR_FORMAT
void
258 __unmatched_right_brace_in_format_string()
259 { __throw_format_error(
"format error: unmatched '}' in format string"); }
262 inline _GLIBCXX_CONSTEXPR_FORMAT
void
263 __conflicting_indexing_in_format_string()
264 { __throw_format_error(
"format error: conflicting indexing style in format string"); }
267 inline _GLIBCXX_CONSTEXPR_FORMAT
void
268 __invalid_arg_id_in_format_string()
269 { __throw_format_error(
"format error: invalid arg-id in format string"); }
272 inline _GLIBCXX_CONSTEXPR_FORMAT
void
273 __failed_to_parse_format_spec()
274 { __throw_format_error(
"format error: failed to parse format-spec"); }
276 template<
typename _CharT>
class _Scanner;
282 template<
typename _CharT>
class basic_format_parse_context;
283 using format_parse_context = basic_format_parse_context<char>;
284#ifdef _GLIBCXX_USE_WCHAR_T
285 using wformat_parse_context = basic_format_parse_context<wchar_t>;
288 template<
typename _CharT>
289 class basic_format_parse_context
292 using char_type = _CharT;
293 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
294 using iterator = const_iterator;
297 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
298 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
301 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
302 void operator=(
const basic_format_parse_context&) =
delete;
304 constexpr const_iterator begin() const noexcept {
return _M_begin; }
305 constexpr const_iterator end() const noexcept {
return _M_end; }
308 advance_to(const_iterator __it)
noexcept
314 if (_M_indexing == _Manual)
315 __format::__conflicting_indexing_in_format_string();
320 if (std::is_constant_evaluated())
321 if (_M_next_arg_id == _M_num_args)
322 __format::__invalid_arg_id_in_format_string();
323 return _M_next_arg_id++;
327 check_arg_id(
size_t __id)
329 if (_M_indexing == _Auto)
330 __format::__conflicting_indexing_in_format_string();
331 _M_indexing = _Manual;
333 if (std::is_constant_evaluated())
334 if (__id >= _M_num_args)
335 __format::__invalid_arg_id_in_format_string();
338#if __cpp_lib_format >= 202305L
339 template<
typename... _Ts>
341 check_dynamic_spec(
size_t __id)
noexcept
343 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
344 "template arguments for check_dynamic_spec<Ts...>(id) "
345 "must be unique and must be one of the allowed types");
347 __check_dynamic_spec<_Ts...>(__id);
352 check_dynamic_spec_integral(
size_t __id)
noexcept
355 __check_dynamic_spec<int, unsigned,
long long,
356 unsigned long long>(__id);
361 check_dynamic_spec_string(
size_t __id)
noexcept
364 __check_dynamic_spec<const _CharT*, basic_string_view<_CharT>>(__id);
370 template<
typename _Tp,
typename... _Ts>
371 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
373 template<
typename... _Ts>
375 __valid_types_for_check_dynamic_spec()
379 if constexpr (
sizeof...(_Ts) == 0)
388 = __once<bool, _Ts...>
389 + __once<char_type, _Ts...>
390 + __once<int, _Ts...>
391 + __once<
unsigned int, _Ts...>
392 + __once<
long long int, _Ts...>
393 + __once<
unsigned long long int, _Ts...>
394 + __once<float, _Ts...>
395 + __once<double, _Ts...>
396 + __once<
long double, _Ts...>
397 + __once<
const char_type*, _Ts...>
398 + __once<basic_string_view<char_type>, _Ts...>
399 + __once<
const void*, _Ts...>;
400 return __sum ==
sizeof...(_Ts);
404 template<
typename... _Ts>
406 __check_dynamic_spec(
size_t __id)
noexcept;
409 static void __invalid_dynamic_spec(
const char*);
411 friend __format::_Scanner<_CharT>;
416 basic_format_parse_context(basic_string_view<_CharT> __fmt,
417 size_t __num_args) noexcept
418 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
424 enum _Indexing { _Unknown, _Manual, _Auto };
425 _Indexing _M_indexing = _Unknown;
426 size_t _M_next_arg_id = 0;
427 size_t _M_num_args = 0;
431 template<
typename _Tp,
template<
typename...>
class _Class>
432 constexpr bool __is_specialization_of =
false;
433 template<
template<
typename...>
class _Class,
typename... _Args>
434 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
439 template<
typename _CharT>
440 constexpr pair<unsigned short, const _CharT*>
441 __parse_integer(
const _CharT* __first,
const _CharT* __last)
443 if (__first == __last)
444 __builtin_unreachable();
446 if constexpr (is_same_v<_CharT, char>)
448 const auto __start = __first;
449 unsigned short __val = 0;
451 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
452 && __first != __start) [[likely]]
453 return {__val, __first};
457 constexpr int __n = 32;
459 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
460 __buf[__i] = __first[__i];
461 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
462 if (__ptr) [[likely]]
463 return {__v, __first + (__ptr - __buf)};
468 template<
typename _CharT>
469 constexpr pair<unsigned short, const _CharT*>
470 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
472 if (__first == __last)
473 __builtin_unreachable();
476 return {0, __first + 1};
478 if (
'1' <= *__first && *__first <=
'9')
480 const unsigned short __id = *__first -
'0';
481 const auto __next = __first + 1;
483 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
484 return {__id, __next};
486 return __format::__parse_integer(__first, __last);
491 enum class _Pres_type :
unsigned char {
495 _Pres_c = 2, _Pres_x, _Pres_X, _Pres_d, _Pres_o, _Pres_b, _Pres_B,
497 _Pres_g = 1, _Pres_G, _Pres_a, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F,
501 using enum _Pres_type;
503 enum class _Sign :
unsigned char {
511 enum _WidthPrec :
unsigned char {
516 using enum _WidthPrec;
518 template<
typename _Context>
519 _GLIBCXX_CONSTEXPR_FORMAT
size_t
520 __int_from_arg(
const basic_format_arg<_Context>& __arg);
522 constexpr bool __is_digit(
char __c)
523 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
525 constexpr bool __is_xdigit(
char __c)
526 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
533 template<
typename _CharT>
534 struct _Spec : _SpecBase
536 unsigned short _M_width;
537 unsigned short _M_prec;
538 char32_t _M_fill =
' ';
542 unsigned _M_localized : 1;
543 unsigned _M_zero_fill : 1;
544 _WidthPrec _M_width_kind : 2;
545 _WidthPrec _M_prec_kind : 2;
546 unsigned _M_debug : 1;
547 _Pres_type _M_type : 4;
548 unsigned _M_reserved : 8;
552 using iterator =
typename basic_string_view<_CharT>::iterator;
554 static constexpr _Align
555 _S_align(_CharT __c)
noexcept
559 case '<':
return _Align_left;
560 case '>':
return _Align_right;
561 case '^':
return _Align_centre;
562 default:
return _Align_default;
568 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
569 {
return _M_parse_fill_and_align(__first, __last,
"{"); }
573 _M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill)
noexcept
575 for (
char __c : __not_fill)
576 if (*__first ==
static_cast<_CharT
>(__c))
579 using namespace __unicode;
580 if constexpr (__literal_encoding_is_unicode<_CharT>())
583 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
586 auto __beg = __uv.begin();
587 char32_t __c = *__beg++;
588 if (__is_scalar_value(__c))
589 if (
auto __next = __beg.base(); __next != __last)
590 if (_Align __align = _S_align(*__next); __align != _Align_default)
598 else if (__last - __first >= 2)
599 if (_Align __align = _S_align(__first[1]); __align != _Align_default)
606 if (_Align __align = _S_align(__first[0]); __align != _Align_default)
615 static constexpr _Sign
616 _S_sign(_CharT __c)
noexcept
620 case '+':
return _Sign_plus;
621 case '-':
return _Sign_minus;
622 case ' ':
return _Sign_space;
623 default:
return _Sign_default;
629 _M_parse_sign(iterator __first, iterator)
noexcept
631 if (_Sign __sign = _S_sign(*__first); __sign != _Sign_default)
641 _M_parse_alternate_form(iterator __first, iterator)
noexcept
653 _M_parse_zero_fill(iterator __first, iterator )
noexcept
664 static constexpr iterator
665 _S_parse_width_or_precision(iterator __first, iterator __last,
666 unsigned short& __val,
bool& __arg_id,
667 basic_format_parse_context<_CharT>& __pc)
669 if (__format::__is_digit(*__first))
671 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
673 __throw_format_error(
"format error: invalid width or precision "
678 else if (*__first ==
'{')
682 if (__first == __last)
683 __format::__unmatched_left_brace_in_format_string();
685 __val = __pc.next_arg_id();
688 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
689 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
690 __format::__invalid_arg_id_in_format_string();
692 __pc.check_arg_id(__v);
695#if __cpp_lib_format >= 202305L
696 __pc.check_dynamic_spec_integral(__val);
705 _M_parse_width(iterator __first, iterator __last,
706 basic_format_parse_context<_CharT>& __pc)
708 bool __arg_id =
false;
710 __throw_format_error(
"format error: width must be non-zero in "
712 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
714 if (__next != __first)
715 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
721 _M_parse_precision(iterator __first, iterator __last,
722 basic_format_parse_context<_CharT>& __pc)
724 if (__first[0] !=
'.')
727 iterator __next = ++__first;
728 bool __arg_id =
false;
729 if (__next != __last)
730 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
732 if (__next == __first)
733 __throw_format_error(
"format error: missing precision after '.' in "
735 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
741 _M_parse_locale(iterator __first, iterator )
noexcept
751 template<
typename _Context>
752 _GLIBCXX_CONSTEXPR_FORMAT
size_t
753 _M_get_width(_Context& __ctx)
const
756 if (_M_width_kind == _WP_value)
758 else if (_M_width_kind == _WP_from_arg)
759 __width = __format::__int_from_arg(__ctx.arg(_M_width));
763 template<
typename _Context>
764 _GLIBCXX_CONSTEXPR_FORMAT
size_t
765 _M_get_precision(_Context& __ctx)
const
768 if (_M_prec_kind == _WP_value)
770 else if (_M_prec_kind == _WP_from_arg)
771 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
776 template<
typename _Int>
777 inline _GLIBCXX_CONSTEXPR_FORMAT
char*
778 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
782 else if (__sign == _Sign_plus)
784 else if (__sign == _Sign_space)
792 template<
typename _Out,
typename _CharT>
793 requires output_iterator<_Out, const _CharT&>
794 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
795 __write(_Out __out, basic_string_view<_CharT> __str)
797 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
803 for (_CharT __c : __str)
810 template<
typename _Out,
typename _CharT>
811 _GLIBCXX_CONSTEXPR_FORMAT _Out
812 __write_padded(_Out __out, basic_string_view<_CharT> __str,
813 _Align __align,
size_t __nfill,
char32_t __fill_char)
815 const size_t __buflen = 0x20;
816 _CharT __padding_chars[__buflen];
817 __padding_chars[0] = _CharT();
818 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
820 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
823 while (__n > __padding.size())
825 __o = __format::__write(
std::move(__o), __padding);
826 __n -= __padding.size();
829 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
832 size_t __l, __r, __max;
833 if (__align == _Align_centre)
836 __r = __l + (__nfill & 1);
839 else if (__align == _Align_right)
852 using namespace __unicode;
853 if constexpr (__literal_encoding_is_unicode<_CharT>())
854 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
857 const char32_t __arr[1]{ __fill_char };
858 _Utf_view<_CharT, span<const char32_t, 1>> __v(__arr);
859 basic_string<_CharT> __padstr(__v.begin(), __v.end());
860 __padding = __padstr;
862 __out = __format::__write(
std::move(__out), __padding);
863 __out = __format::__write(
std::move(__out), __str);
865 __out = __format::__write(
std::move(__out), __padding);
869 if (__max < __buflen)
870 __padding.remove_suffix(__buflen - __max);
874 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
876 __out = __format::__write(
std::move(__out), __str);
884 template<
typename _CharT,
typename _Out>
885 _GLIBCXX_CONSTEXPR_FORMAT _Out
886 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
887 size_t __estimated_width,
888 basic_format_context<_Out, _CharT>& __fc,
889 const _Spec<_CharT>& __spec,
890 _Align __align = _Align_left)
892 size_t __width = __spec._M_get_width(__fc);
894 if (__width <= __estimated_width)
895 return __format::__write(__fc.out(), __str);
897 const size_t __nfill = __width - __estimated_width;
899 if (__spec._M_align != _Align_default)
900 __align = __spec._M_align;
902 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
906 template<
typename _CharT>
907 _GLIBCXX_CONSTEXPR_FORMAT
size_t
908 __truncate(basic_string_view<_CharT>& __s,
size_t __prec)
910 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
912 if (__prec != (
size_t)-1)
913 return __unicode::__truncate(__s, __prec);
915 return __unicode::__field_width(__s);
919 __s = __s.substr(0, __prec);
924 enum class _Term_char :
unsigned char {
929 using enum _Term_char;
931 template<
typename _CharT>
934 using _Str_view = basic_string_view<_CharT>;
938 {
return _GLIBCXX_WIDEN(
"\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
942 {
return _S_all().substr(0, 3); }
945 _Str_view _S_newline()
946 {
return _S_all().substr(3, 3); }
949 _Str_view _S_return()
950 {
return _S_all().substr(6, 3); }
953 _Str_view _S_bslash()
954 {
return _S_all().substr(9, 3); }
958 {
return _S_all().substr(12, 3); }
962 {
return _S_all().substr(15, 3); }
966 {
return _S_all().substr(18, 2); }
970 {
return _S_all().substr(20, 2); }
973 _Str_view _S_term(_Term_char __term)
980 return _S_quote().substr(0, 1);
982 return _S_apos().substr(0, 1);
984 __builtin_unreachable();
988 template<
typename _CharT>
991 using _Str_view = basic_string_view<_CharT>;
995 {
return _GLIBCXX_WIDEN(
"[]{}(), : "); }
998 _Str_view _S_squares()
999 {
return _S_all().substr(0, 2); }
1002 _Str_view _S_braces()
1003 {
return _S_all().substr(2, 2); }
1006 _Str_view _S_parens()
1007 {
return _S_all().substr(4, 2); }
1010 _Str_view _S_comma()
1011 {
return _S_all().substr(6, 2); }
1014 _Str_view _S_colon()
1015 {
return _S_all().substr(8, 2); }
1018 template<
typename _CharT>
1019 constexpr bool __should_escape_ascii(_CharT __c, _Term_char __term)
1021 using _Esc = _Escapes<_CharT>;
1024 case _Esc::_S_tab()[0]:
1025 case _Esc::_S_newline()[0]:
1026 case _Esc::_S_return()[0]:
1027 case _Esc::_S_bslash()[0]:
1029 case _Esc::_S_quote()[0]:
1030 return __term == _Term_quote;
1031 case _Esc::_S_apos()[0]:
1032 return __term == _Term_apos;
1034 return (__c >= 0 && __c < 0x20) || __c == 0x7f;
1039 constexpr bool __should_escape_unicode(
char32_t __c,
bool __prev_esc)
1041 if (__unicode::__should_escape_category(__c))
1045 return __unicode::__grapheme_cluster_break_property(__c)
1046 == __unicode::_Gcb_property::_Gcb_Extend;
1049 using uint_least32_t = __UINT_LEAST32_TYPE__;
1050 template<
typename _Out,
typename _CharT>
1051 _GLIBCXX_CONSTEXPR_FORMAT _Out
1052 __write_escape_seq(_Out __out, uint_least32_t __val,
1053 basic_string_view<_CharT> __prefix)
1055 using _Str_view = basic_string_view<_CharT>;
1056 constexpr size_t __max = 8;
1058 const string_view __narrow(
1060 std::__to_chars_i<uint_least32_t>(__buf, __buf + __max, __val, 16).ptr);
1062 __out = __format::__write(__out, __prefix);
1063 *__out = _Separators<_CharT>::_S_braces()[0];
1065 if constexpr (is_same_v<char, _CharT>)
1066 __out = __format::__write(__out, __narrow);
1067#ifdef _GLIBCXX_USE_WCHAR_T
1070 _CharT __wbuf[__max];
1071 const size_t __n = __narrow.size();
1072 std::__to_wstring_numeric(__narrow.data(), __n, __wbuf);
1073 __out = __format::__write(__out, _Str_view(__wbuf, __n));
1076 *__out = _Separators<_CharT>::_S_braces()[1];
1080 template<
typename _Out,
typename _CharT>
1081 _GLIBCXX_CONSTEXPR_FORMAT _Out
1082 __write_escape_seqs(_Out __out, basic_string_view<_CharT> __units)
1084 using _UChar = make_unsigned_t<_CharT>;
1085 for (_CharT __c : __units)
1086 __out = __format::__write_escape_seq(
1087 __out,
static_cast<_UChar
>(__c), _Escapes<_CharT>::_S_x());
1091 template<
typename _Out,
typename _CharT>
1092 _GLIBCXX_CONSTEXPR_FORMAT _Out
1093 __write_escaped_char(_Out __out, _CharT __c)
1095 using _UChar = make_unsigned_t<_CharT>;
1096 using _Esc = _Escapes<_CharT>;
1099 case _Esc::_S_tab()[0]:
1100 return __format::__write(__out, _Esc::_S_tab().substr(1, 2));
1101 case _Esc::_S_newline()[0]:
1102 return __format::__write(__out, _Esc::_S_newline().substr(1, 2));
1103 case _Esc::_S_return()[0]:
1104 return __format::__write(__out, _Esc::_S_return().substr(1, 2));
1105 case _Esc::_S_bslash()[0]:
1106 return __format::__write(__out, _Esc::_S_bslash().substr(1, 2));
1107 case _Esc::_S_quote()[0]:
1108 return __format::__write(__out, _Esc::_S_quote().substr(1, 2));
1109 case _Esc::_S_apos()[0]:
1110 return __format::__write(__out, _Esc::_S_apos().substr(1, 2));
1112 return __format::__write_escape_seq(
1113 __out,
static_cast<_UChar
>(__c), _Esc::_S_u());
1117 template<
typename _CharT,
typename _Out>
1118 _GLIBCXX_CONSTEXPR_FORMAT _Out
1119 __write_escaped_ascii(_Out __out,
1120 basic_string_view<_CharT> __str,
1123 using _Str_view = basic_string_view<_CharT>;
1127 constexpr _Str_view __supported(_GLIBCXX_WIDEN(
1128 "ABCDEFGHIJKLMNOPQRSTUWXYZ"
1129 "abdeefghijklmnopqrstuwzyz"
1130 " !#$%&'()*+-./:;<=>?[]^_{|}~"
1131 "0123456789" "\t\n\r\\\"\'\0"
1133 if (__str.find_first_not_of(__supported) != _Str_view::npos)
1134#if __has_builtin(__builtin_constexpr_diag)
1135 __builtin_constexpr_diag (2,
"",
1136 "for non-Unicode literal encodings, only"
1137 " printable ASCII characters and standard"
1138 " escape sequencess can be escaped in constant"
1145 auto __first = __str.begin();
1146 auto const __last = __str.end();
1147 while (__first != __last)
1149 auto __print = __first;
1151 while (__print != __last
1152 && !__format::__should_escape_ascii(*__print, __term))
1155 if (__print != __first)
1156 __out = __format::__write(__out, _Str_view(__first, __print));
1158 if (__print == __last)
1162 __out = __format::__write_escaped_char(__out, *__first);
1168 template<
typename _CharT,
typename _Out>
1169 _GLIBCXX_CONSTEXPR_FORMAT _Out
1170 __write_escaped_unicode_part(_Out __out, basic_string_view<_CharT>& __str,
1171 bool& __prev_esc, _Term_char __term)
1173 using _Str_view = basic_string_view<_CharT>;
1174 using _Esc = _Escapes<_CharT>;
1176 static constexpr char32_t __replace = U
'\uFFFD';
1177 static constexpr _Str_view __replace_rep = []
1180 if constexpr (is_same_v<char, _CharT>)
1181 return "\xEF\xBF\xBD";
1186 __unicode::_Utf_view<char32_t, _Str_view> __v(
std::move(__str));
1189 auto __first = __v.begin();
1190 auto const __last = __v.end();
1191 while (__first != __last)
1193 bool __esc_ascii =
false;
1194 bool __esc_unicode =
false;
1195 bool __esc_replace =
false;
1196 auto __should_escape = [&](
auto const& __it)
1200 = __format::__should_escape_ascii(*__it.base(), __term);
1201 if (__format::__should_escape_unicode(*__it, __prev_esc))
1202 return __esc_unicode =
true;
1203 if (*__it == __replace)
1205 _Str_view __units(__it.base(), __it._M_units());
1206 return __esc_replace = (__units != __replace_rep);
1211 auto __print = __first;
1212 while (__print != __last && !__should_escape(__print))
1218 if (__print != __first)
1219 __out = __format::__write(__out, _Str_view(__first.base(), __print.base()));
1221 if (__print == __last)
1226 __out = __format::__write_escaped_char(__out, *__first.base());
1227 else if (__esc_unicode)
1228 __out = __format::__write_escape_seq(__out, *__first, _Esc::_S_u());
1230 else if (_Str_view __units(__first.base(), __first._M_units());
1231 __units.end() != __last.base())
1232 __out = __format::__write_escape_seqs(__out, __units);
1246 template<
typename _CharT,
typename _Out>
1247 _GLIBCXX_CONSTEXPR_FORMAT _Out
1248 __write_escaped_unicode(_Out __out, basic_string_view<_CharT> __str,
1251 bool __prev_escape =
true;
1252 __out = __format::__write_escaped_unicode_part(__out, __str,
1253 __prev_escape, __term);
1254 __out = __format::__write_escape_seqs(__out, __str);
1258 template<
typename _CharT,
typename _Out>
1259 _GLIBCXX_CONSTEXPR_FORMAT _Out
1260 __write_escaped(_Out __out, basic_string_view<_CharT> __str, _Term_char __term)
1262 __out = __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1264 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1265 __out = __format::__write_escaped_unicode(__out, __str, __term);
1266 else if constexpr (is_same_v<char, _CharT>
1267 && __unicode::__literal_encoding_is_extended_ascii())
1268 __out = __format::__write_escaped_ascii(__out, __str, __term);
1271 __out = __format::__write_escaped_ascii(__out, __str, __term);
1273 return __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1277 struct _Optional_locale
1279 [[__gnu__::__always_inline__]]
1280 _GLIBCXX_CONSTEXPR_FORMAT
1281 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
1283 _Optional_locale(
const locale& __loc) noexcept
1284 : _M_loc(__loc), _M_hasval(
true)
1287 _GLIBCXX_CONSTEXPR_FORMAT
1288 _Optional_locale(
const _Optional_locale& __l) noexcept
1289 : _M_dummy(), _M_hasval(__l._M_hasval)
1292 std::construct_at(&_M_loc, __l._M_loc);
1295 _GLIBCXX_CONSTEXPR_FORMAT
1297 operator=(
const _Optional_locale& __l)
noexcept
1302 _M_loc = __l._M_loc;
1309 else if (__l._M_hasval)
1311 std::construct_at(&_M_loc, __l._M_loc);
1317 _GLIBCXX_CONSTEXPR_FORMAT
1318 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
1321 operator=(locale&& __loc)
noexcept
1327 std::construct_at(&_M_loc,
std::move(__loc));
1338 std::construct_at(&_M_loc);
1344 _GLIBCXX_CONSTEXPR_FORMAT
bool
1345 has_value() const noexcept {
return _M_hasval; }
1348 char _M_dummy =
'\0';
1351 bool _M_hasval =
false;
1354 template<__
char _CharT>
1355 struct __formatter_str
1357 __formatter_str() =
default;
1360 __formatter_str(_Spec<_CharT> __spec) noexcept
1364 constexpr typename basic_format_parse_context<_CharT>::iterator
1365 parse(basic_format_parse_context<_CharT>& __pc)
1367 auto __first = __pc.begin();
1368 const auto __last = __pc.end();
1369 _Spec<_CharT> __spec{};
1371 auto __finalize = [
this, &__spec] {
1375 auto __finished = [&] {
1376 if (__first == __last || *__first ==
'}')
1387 __first = __spec._M_parse_fill_and_align(__first, __last);
1391 __first = __spec._M_parse_width(__first, __last, __pc);
1395 __first = __spec._M_parse_precision(__first, __last, __pc);
1399 if (*__first ==
's')
1401 __spec._M_type = _Pres_s;
1404#if __glibcxx_format_ranges
1405 else if (*__first ==
'?')
1407 __spec._M_debug =
true;
1415 __format::__failed_to_parse_format_spec();
1418 template<
typename _Out>
1419 _GLIBCXX_CONSTEXPR_FORMAT _Out
1420 format(basic_string_view<_CharT> __s,
1421 basic_format_context<_Out, _CharT>& __fc)
const
1423 if (_M_spec._M_debug)
1424 return _M_format_escaped(__s, __fc);
1426 if (_M_spec._M_width_kind == _WP_none
1427 && _M_spec._M_prec_kind == _WP_none)
1428 return __format::__write(__fc.out(), __s);
1430 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1431 const size_t __width = __format::__truncate(__s, __maxwidth);
1432 return __format::__write_padded_as_spec(__s, __width, __fc, _M_spec);
1435 template<
typename _Out>
1436 _GLIBCXX_CONSTEXPR_FORMAT _Out
1437 _M_format_escaped(basic_string_view<_CharT> __s,
1438 basic_format_context<_Out, _CharT>& __fc)
const
1440 const size_t __padwidth = _M_spec._M_get_width(__fc);
1441 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1442 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1444 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1445 const size_t __width = __truncate(__s, __maxwidth);
1447 if (__padwidth <= __width && _M_spec._M_prec_kind == _WP_none)
1448 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1453 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth, __maxwidth);
1454 __format::__write_escaped(__sink.out(), __s, _Term_quote);
1455 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1458#if __glibcxx_format_ranges
1459 template<ranges::input_range _Rg,
typename _Out>
1460 requires same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _CharT>
1461 _GLIBCXX_CONSTEXPR_FORMAT _Out
1462 _M_format_range(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
1464 using _Range = remove_reference_t<_Rg>;
1465 using _String_view = basic_string_view<_CharT>;
1466 if constexpr (ranges::contiguous_range<_Rg>)
1468 _String_view __str(ranges::data(__rg),
1469 size_t(ranges::distance(__rg)));
1470 return format(__str, __fc);
1472 else if constexpr (!is_const_v<_Range>
1473 && __simply_formattable_range<_Range, _CharT>)
1474 return _M_format_range<const _Range&>(__rg, __fc);
1475 else if constexpr (!is_lvalue_reference_v<_Rg>)
1476 return _M_format_range<_Range&>(__rg, __fc);
1479 auto __handle_debug = [
this, &__rg]<
typename _NOut>(_NOut __nout)
1481 if (!_M_spec._M_debug)
1482 return ranges::copy(__rg,
std::move(__nout)).out;
1484 _Escaping_sink<_NOut, _CharT>
1486 ranges::copy(__rg, __sink.out());
1487 return __sink._M_finish();
1490 const size_t __padwidth = _M_spec._M_get_width(__fc);
1491 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1492 return __handle_debug(__fc.out());
1494 _Padding_sink<_Out, _CharT>
1495 __sink(__fc.out(), __padwidth, _M_spec._M_get_precision(__fc));
1496 __handle_debug(__sink.out());
1497 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1502 set_debug_format() noexcept
1503 { _M_spec._M_debug =
true; }
1507 _Spec<_CharT> _M_spec{};
1512 [[__gnu__::__always_inline__]]
1514 __toupper_numeric(
char __c)
1518 case 'a':
return 'A';
1519 case 'b':
return 'B';
1520 case 'c':
return 'C';
1521 case 'd':
return 'D';
1522 case 'e':
return 'E';
1523 case 'f':
return 'F';
1524 case 'i':
return 'I';
1525 case 'n':
return 'N';
1526 case 'p':
return 'P';
1527 case 'x':
return 'X';
1528 default:
return __c;
1532 template<__
char _CharT>
1533 struct __formatter_int
1537 static constexpr _Pres_type _AsInteger = _Pres_d;
1538 static constexpr _Pres_type _AsBool = _Pres_s;
1539 static constexpr _Pres_type _AsChar = _Pres_c;
1541 __formatter_int() =
default;
1544 __formatter_int(_Spec<_CharT> __spec) noexcept
1547 if (_M_spec._M_type == _Pres_none)
1548 _M_spec._M_type = _Pres_d;
1551 constexpr typename basic_format_parse_context<_CharT>::iterator
1552 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1554 _Spec<_CharT> __spec{};
1555 __spec._M_type = __type;
1557 const auto __last = __pc.end();
1558 auto __first = __pc.begin();
1560 auto __finalize = [
this, &__spec] {
1564 auto __finished = [&] {
1565 if (__first == __last || *__first ==
'}')
1576 __first = __spec._M_parse_fill_and_align(__first, __last);
1580 __first = __spec._M_parse_sign(__first, __last);
1584 __first = __spec._M_parse_alternate_form(__first, __last);
1588 __first = __spec._M_parse_zero_fill(__first, __last);
1592 __first = __spec._M_parse_width(__first, __last, __pc);
1596 __first = __spec._M_parse_locale(__first, __last);
1603 __spec._M_type = _Pres_b;
1607 __spec._M_type = _Pres_B;
1613 if (__type != _AsBool)
1615 __spec._M_type = _Pres_c;
1620 __spec._M_type = _Pres_d;
1624 __spec._M_type = _Pres_o;
1628 __spec._M_type = _Pres_x;
1632 __spec._M_type = _Pres_X;
1636 if (__type == _AsBool)
1638 __spec._M_type = _Pres_s;
1642#if __glibcxx_format_ranges
1644 if (__type == _AsChar)
1646 __spec._M_debug =
true;
1656 __format::__failed_to_parse_format_spec();
1659 template<
typename _Tp>
1660 constexpr typename basic_format_parse_context<_CharT>::iterator
1661 _M_parse(basic_format_parse_context<_CharT>& __pc)
1663 if constexpr (is_same_v<_Tp, bool>)
1665 auto __end = _M_do_parse(__pc, _AsBool);
1666 if (_M_spec._M_type == _Pres_s)
1667 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1668 || _M_spec._M_zero_fill)
1669 __throw_format_error(
"format error: format-spec contains "
1670 "invalid formatting options for "
1674 else if constexpr (__char<_Tp>)
1676 auto __end = _M_do_parse(__pc, _AsChar);
1677 if (_M_spec._M_type == _Pres_c)
1678 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1679 || _M_spec._M_zero_fill
1681 __throw_format_error(
"format error: format-spec contains "
1682 "invalid formatting options for "
1687 return _M_do_parse(__pc, _AsInteger);
1690 template<
typename _Int,
typename _Out>
1691 _GLIBCXX_CONSTEXPR_FORMAT
1692 typename basic_format_context<_Out, _CharT>::iterator
1693 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1695 if (_M_spec._M_type == _Pres_c)
1696 return _M_format_character(_S_to_character(__i), __fc);
1698 constexpr size_t __buf_size =
sizeof(_Int) * __CHAR_BIT__ + 3;
1699 char __buf[__buf_size];
1700 to_chars_result __res{};
1702 string_view __base_prefix;
1703 make_unsigned_t<_Int> __u;
1705 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1709 char* __start = __buf + 3;
1710 char*
const __end = __buf +
sizeof(__buf);
1711 char*
const __start_digits = __start;
1713 switch (_M_spec._M_type)
1717 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1718 __res = to_chars(__start, __end, __u, 2);
1722 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);
1746 if (_M_spec._M_alt && __base_prefix.size())
1748 __start -= __base_prefix.size();
1749 ranges::copy(__base_prefix, __start);
1751 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1753 string_view __narrow_str(__start, __res.ptr - __start);
1754 size_t __prefix_len = __start_digits - __start;
1755 if constexpr (is_same_v<char, _CharT>)
1756 return _M_format_int(__narrow_str, __prefix_len, __fc);
1757#ifdef _GLIBCXX_USE_WCHAR_T
1760 _CharT __wbuf[__buf_size];
1761 size_t __n = __narrow_str.size();
1764 std::__to_wstring_numeric(__narrow_str.data(), __n, __wbuf);
1765 return _M_format_int(basic_string_view<_CharT>(__wbuf, __n),
1766 __prefix_len, __fc);
1771 template<
typename _Out>
1772 _GLIBCXX_CONSTEXPR_FORMAT
1773 typename basic_format_context<_Out, _CharT>::iterator
1774 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1776 if (_M_spec._M_type == _Pres_c)
1777 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1778 if (_M_spec._M_type != _Pres_s)
1779 return format(
static_cast<unsigned char>(__i), __fc);
1781 basic_string<_CharT> __s;
1783 if (_M_spec._M_localized) [[unlikely]]
1786 __s = __i ? __np.truename() : __np.falsename();
1787 __est_width = __s.size();
1791 if constexpr (is_same_v<char, _CharT>)
1792 __s = __i ?
"true" :
"false";
1794 __s = __i ? L
"true" : L
"false";
1795 __est_width = __s.size();
1798 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1802 template<
typename _Out>
1803 _GLIBCXX_CONSTEXPR_FORMAT
1804 typename basic_format_context<_Out, _CharT>::iterator
1805 _M_format_character(_CharT __c,
1806 basic_format_context<_Out, _CharT>& __fc)
const
1808 basic_string_view<_CharT> __in(&__c, 1u);
1809 size_t __width = 1u;
1811 if constexpr (
sizeof(_CharT) > 1u &&
1812 __unicode::__literal_encoding_is_unicode<_CharT>())
1813 __width = __unicode::__field_width(__c);
1815 if (!_M_spec._M_debug)
1816 return __format::__write_padded_as_spec(__in, __width,
1820 if (_M_spec._M_get_width(__fc) <= __width)
1821 return __format::__write_escaped(__fc.out(), __in, _Term_apos);
1824 _Fixedbuf_sink<_CharT> __sink(__buf);
1825 __format::__write_escaped(__sink.out(), __in, _Term_apos);
1827 __in = __sink.view();
1828 if (__in[1] == _Escapes<_CharT>::_S_bslash()[0])
1829 __width = __in.size();
1830 return __format::__write_padded_as_spec(__in, __width,
1834 template<
typename _Int>
1835 static _GLIBCXX_CONSTEXPR_FORMAT _CharT
1836 _S_to_character(_Int __i)
1839 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1841 if (_Traits::__min <= __i && __i <= _Traits::__max)
1842 return static_cast<_CharT
>(__i);
1844 else if constexpr (is_signed_v<_Int>)
1846 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1847 return static_cast<_CharT
>(__i);
1849 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1850 return static_cast<_CharT
>(__i);
1851 __throw_format_error(
"format error: integer not representable as "
1855 template<
typename _Out>
1856 _GLIBCXX_CONSTEXPR_FORMAT
1857 typename basic_format_context<_Out, _CharT>::iterator
1858 _M_format_int(basic_string_view<_CharT> __str,
size_t __prefix_len,
1859 basic_format_context<_Out, _CharT>& __fc)
const
1861 size_t __width = _M_spec._M_get_width(__fc);
1862 if (_M_spec._M_localized)
1864 const auto& __l = __fc.locale();
1865 if (__l.name() !=
"C")
1867 auto& __np = use_facet<numpunct<_CharT>>(__l);
1868 string __grp = __np.grouping();
1871 size_t __n = __str.size() - __prefix_len;
1872 auto __p = (_CharT*)__builtin_alloca(2 * __n
1875 auto __s = __str.data();
1876 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1877 __s += __prefix_len;
1878 auto __end = std::__add_grouping(__p + __prefix_len,
1879 __np.thousands_sep(),
1883 __str = {__p, size_t(__end - __p)};
1888 if (__width <= __str.size())
1889 return __format::__write(__fc.out(), __str);
1891 char32_t __fill_char = _M_spec._M_fill;
1892 _Align __align = _M_spec._M_align;
1894 size_t __nfill = __width - __str.size();
1895 auto __out = __fc.out();
1896 if (__align == _Align_default)
1898 __align = _Align_right;
1899 if (_M_spec._M_zero_fill)
1901 __fill_char = _CharT(
'0');
1903 if (__prefix_len != 0)
1905 __out = __format::__write(
std::move(__out),
1906 __str.substr(0, __prefix_len));
1907 __str.remove_prefix(__prefix_len);
1911 __fill_char = _CharT(
' ');
1913 return __format::__write_padded(
std::move(__out), __str,
1914 __align, __nfill, __fill_char);
1917 _Spec<_CharT> _M_spec{};
1920#ifdef __BFLT16_DIG__
1921 using __bflt16_t =
decltype(0.0bf16);
1932#undef _GLIBCXX_FORMAT_F128
1934#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1937 using __flt128_t = __ieee128;
1938# define _GLIBCXX_FORMAT_F128 1
1940#ifdef __LONG_DOUBLE_IEEE128__
1944 to_chars(
char*,
char*, __ibm128)
noexcept
1945 __asm(
"_ZSt8to_charsPcS_e");
1948 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1949 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1952 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1953 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1954#elif __cplusplus == 202002L
1956 to_chars(
char*,
char*, __ieee128)
noexcept
1957 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1960 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1961 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1964 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1965 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1968#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1971 using __flt128_t =
long double;
1972# define _GLIBCXX_FORMAT_F128 2
1974#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1977 using __flt128_t = _Float128;
1978# define _GLIBCXX_FORMAT_F128 3
1980# if __cplusplus == 202002L
1984 to_chars(
char*,
char*, _Float128)
noexcept
1985# if _GLIBCXX_INLINE_VERSION
1986 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1988 __asm(
"_ZSt8to_charsPcS_DF128_");
1992 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1993# if _GLIBCXX_INLINE_VERSION
1994 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1996 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
2000 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
2001# if _GLIBCXX_INLINE_VERSION
2002 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
2004 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
2009 using std::to_chars;
2012 template<
typename _Tp>
2013 concept __formattable_float
2014 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
2015 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
2017 template<__
char _CharT>
2018 struct __formatter_fp
2020 constexpr typename basic_format_parse_context<_CharT>::iterator
2021 parse(basic_format_parse_context<_CharT>& __pc)
2023 _Spec<_CharT> __spec{};
2024 const auto __last = __pc.end();
2025 auto __first = __pc.begin();
2027 auto __finalize = [
this, &__spec] {
2031 auto __finished = [&] {
2032 if (__first == __last || *__first ==
'}')
2043 __first = __spec._M_parse_fill_and_align(__first, __last);
2047 __first = __spec._M_parse_sign(__first, __last);
2051 __first = __spec._M_parse_alternate_form(__first, __last);
2055 __first = __spec._M_parse_zero_fill(__first, __last);
2059 if (__first[0] !=
'.')
2061 __first = __spec._M_parse_width(__first, __last, __pc);
2066 __first = __spec._M_parse_precision(__first, __last, __pc);
2070 __first = __spec._M_parse_locale(__first, __last);
2077 __spec._M_type = _Pres_a;
2081 __spec._M_type = _Pres_A;
2085 __spec._M_type = _Pres_e;
2089 __spec._M_type = _Pres_E;
2093 __spec._M_type = _Pres_f;
2097 __spec._M_type = _Pres_F;
2101 __spec._M_type = _Pres_g;
2105 __spec._M_type = _Pres_G;
2113 __format::__failed_to_parse_format_spec();
2116 template<
typename _Fp,
typename _Out>
2117 typename basic_format_context<_Out, _CharT>::iterator
2118 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2122 to_chars_result __res{};
2125 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2127 __prec = _M_spec._M_get_precision(__fc);
2130 bool __upper =
false;
2131 bool __trailing_zeros =
false;
2133 size_t __offset = 1;
2135 switch (_M_spec._M_type)
2138 if (__builtin_isfinite(__v))
2144 __fmt = chars_format::hex;
2147 if (__builtin_isfinite(__v))
2152 __fmt = chars_format::hex;
2160 __fmt = chars_format::scientific;
2167 __fmt = chars_format::fixed;
2174 __trailing_zeros =
true;
2176 __fmt = chars_format::general;
2181 __fmt = chars_format::general;
2185 char* __start = __buf + __offset;
2186 char* __end = __buf +
sizeof(__buf);
2189 auto __to_chars = [&](
char* __b,
char* __e) {
2191 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2192 else if (__fmt != chars_format{})
2193 return __format::to_chars(__b, __e, __v, __fmt);
2195 return __format::to_chars(__b, __e, __v);
2199 __res = __to_chars(__start, __end);
2201 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2205 size_t __guess = 7 + __offset + __prec;
2206 if (__fmt == chars_format::fixed)
2208 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2209 || is_same_v<_Fp, long double>)
2214 if constexpr (is_same_v<_Fp, float>)
2215 __builtin_frexpf(__v, &__exp);
2216 else if constexpr (is_same_v<_Fp, double>)
2217 __builtin_frexp(__v, &__exp);
2218 else if constexpr (is_same_v<_Fp, long double>)
2219 __builtin_frexpl(__v, &__exp);
2221 __guess += 1U + __exp * 4004U / 13301U;
2224 __guess += numeric_limits<_Fp>::max_exponent10;
2226 if (__guess <=
sizeof(__buf)) [[unlikely]]
2227 __guess =
sizeof(__buf) * 2;
2236 auto __overwrite = [&__to_chars, &__res, __offset] (
char* __p,
size_t __n)
2238 __res = __to_chars(__p + __offset, __p + __n - __offset);
2239 return __res.ec == errc{} ? __res.ptr - __p : 0;
2244 __start = __dynbuf.
data() + __offset;
2245 __end = __dynbuf.
data() + __dynbuf.
size();
2247 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2253 if (__builtin_signbit(__v))
2254 ranges::copy(string_view(
"-0x"), __start);
2256 ranges::copy(string_view(
"0x"), __start);
2262 for (
char* __p = __start; __p != __res.ptr; ++__p)
2263 *__p = __format::__toupper_numeric(*__p);
2267 if (!__builtin_signbit(__v))
2269 if (_M_spec._M_sign == _Sign_plus)
2271 else if (_M_spec._M_sign == _Sign_space)
2277 string_view __narrow_str(__start, __res.ptr - __start);
2281 if (_M_spec._M_alt && __builtin_isfinite(__v))
2283 string_view __s = __narrow_str;
2287 size_t __d = __s.find(
'.');
2288 if (__d != __s.npos)
2290 __p = __s.find(__expc, __d + 1);
2291 if (__p == __s.npos)
2295 if (__trailing_zeros)
2298 if (__s[__offset] !=
'0')
2300 __sigfigs = __p - __offset - 1;
2305 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2310 __p = __s.find(__expc);
2311 if (__p == __s.npos)
2314 __sigfigs = __d - __offset;
2317 if (__trailing_zeros && __prec != 0)
2322 __z = __prec - __sigfigs;
2325 if (
size_t __extras =
int(__d == __p) + __z)
2327 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2331 __builtin_memmove(__start + __p + __extras,
2335 __start[__p++] =
'.';
2336 __builtin_memset(__start + __p,
'0', __z);
2337 __narrow_str = {__s.data(), __s.size() + __extras};
2341 __dynbuf.
reserve(__s.size() + __extras);
2342 if (__dynbuf.
empty())
2344 __dynbuf = __s.
substr(0, __p);
2348 __dynbuf.
append(__z,
'0');
2349 __dynbuf.
append(__s.substr(__p));
2353 __dynbuf.
insert(__p, __extras,
'0');
2355 __dynbuf[__p] =
'.';
2357 __narrow_str = __dynbuf;
2362 basic_string<_CharT> __wstr;
2363 basic_string_view<_CharT> __str;
2364 if constexpr (is_same_v<_CharT, char>)
2365 __str = __narrow_str;
2366#ifdef _GLIBCXX_USE_WCHAR_T
2371 __wstr = std::__to_wstring_numeric(__narrow_str);
2376 if (_M_spec._M_localized && __builtin_isfinite(__v))
2378 auto __s = _M_localize(__str, __expc, __offset, __fc.locale());
2383 size_t __width = _M_spec._M_get_width(__fc);
2385 if (__width <= __str.size())
2386 return __format::__write(__fc.out(), __str);
2388 char32_t __fill_char = _M_spec._M_fill;
2389 _Align __align = _M_spec._M_align;
2391 size_t __nfill = __width - __str.size();
2392 auto __out = __fc.out();
2393 if (__align == _Align_default)
2395 __align = _Align_right;
2396 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2398 __fill_char = _CharT(
'0');
2401 __out = __format::__write(__out, __str.substr(0, __offset));
2402 __str.remove_prefix(__offset);
2406 __fill_char = _CharT(
' ');
2408 return __format::__write_padded(
std::move(__out), __str,
2409 __align, __nfill, __fill_char);
2413 basic_string<_CharT>
2414 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2415 int __offset,
const locale& __loc)
const
2417 basic_string<_CharT> __lstr;
2419 if (__loc == locale::classic())
2422 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2423 const _CharT __point = __np.decimal_point();
2424 const string __grp = __np.grouping();
2426 _CharT __dot, __exp;
2427 if constexpr (is_same_v<_CharT, char>)
2450 __builtin_unreachable();
2454 if (__grp.empty() && __point == __dot)
2457 size_t __d = __str.find(__dot);
2458 size_t __e = min(__d, __str.find(__exp));
2459 if (__e == __str.npos)
2461 const size_t __r = __str.size() - __e;
2462 auto __overwrite = [&](_CharT* __p, size_t) {
2464 ranges::copy_n(__str.data(), __offset, __p);
2466 auto __end = std::__add_grouping(__p + __offset, __np.thousands_sep(),
2467 __grp.data(), __grp.size(),
2468 __str.data() + __offset,
2469 __str.data() + __e);
2472 if (__d != __str.npos)
2478 const size_t __rlen = __str.size() - __e;
2480 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2483 return (__end - __p);
2485 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2489 _Spec<_CharT> _M_spec{};
2492 template<__format::__
char _CharT>
2493 struct __formatter_ptr
2496 __formatter_ptr() noexcept
2499 _M_spec._M_type = _Pres_x;
2500 _M_spec._M_alt =
true;
2504 __formatter_ptr(_Spec<_CharT> __spec) noexcept
2506 { _M_set_default(); }
2508 constexpr typename basic_format_parse_context<_CharT>::iterator
2509 parse(basic_format_parse_context<_CharT>& __pc)
2511 __format::_Spec<_CharT> __spec{};
2512 const auto __last = __pc.end();
2513 auto __first = __pc.begin();
2515 auto __finalize = [
this, &__spec] {
2520 auto __finished = [&] {
2521 if (__first == __last || *__first ==
'}')
2532 __first = __spec._M_parse_fill_and_align(__first, __last);
2538#if __glibcxx_format >= 202304L
2539 __first = __spec._M_parse_zero_fill(__first, __last);
2544 __first = __spec._M_parse_width(__first, __last, __pc);
2548 if (*__first ==
'p')
2550 __spec._M_type = _Pres_x;
2551 __spec._M_alt =
true;
2554#if __glibcxx_format >= 202304L
2555 else if (*__first ==
'P')
2557 __spec._M_type = _Pres_X;
2558 __spec._M_alt =
true;
2566 __format::__failed_to_parse_format_spec();
2569 template<
typename _Out>
2570 _GLIBCXX_CONSTEXPR_FORMAT
2571 typename basic_format_context<_Out, _CharT>::iterator
2572 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2575 ?
reinterpret_cast<__UINTPTR_TYPE__
>(__v)
2576 : static_cast<__UINTPTR_TYPE__>(0);
2577 return __formatter_int<_CharT>(_M_spec).format(__u, __fc);
2581 [[__gnu__::__always_inline__]]
2585 if (_M_spec._M_type == _Pres_none)
2587 _M_spec._M_type = _Pres_x;
2588 _M_spec._M_alt =
true;
2592 __format::_Spec<_CharT> _M_spec;
2599 template<__format::__
char _CharT>
2600 struct formatter<_CharT, _CharT>
2602 formatter() =
default;
2604 constexpr typename basic_format_parse_context<_CharT>::iterator
2605 parse(basic_format_parse_context<_CharT>& __pc)
2607 return _M_f.template _M_parse<_CharT>(__pc);
2610 template<
typename _Out>
2611 _GLIBCXX_CONSTEXPR_FORMAT
2612 typename basic_format_context<_Out, _CharT>::iterator
2613 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2615 if (_M_f._M_spec._M_type == __format::_Pres_c)
2616 return _M_f._M_format_character(__u, __fc);
2618 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2621#if __glibcxx_format_ranges
2623 set_debug_format() noexcept
2624 { _M_f._M_spec._M_debug =
true; }
2628 __format::__formatter_int<_CharT> _M_f;
2631#if __glibcxx_print >= 202403L
2632 template<__format::__
char _CharT>
2633 constexpr bool enable_nonlocking_formatter_optimization<_CharT> =
true;
2636#ifdef _GLIBCXX_USE_WCHAR_T
2639 struct formatter<char, wchar_t>
2641 formatter() =
default;
2643 constexpr typename basic_format_parse_context<wchar_t>::iterator
2644 parse(basic_format_parse_context<wchar_t>& __pc)
2646 return _M_f._M_parse<
char>(__pc);
2649 template<
typename _Out>
2650 _GLIBCXX_CONSTEXPR_FORMAT
2651 typename basic_format_context<_Out, wchar_t>::iterator
2652 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2654 if (_M_f._M_spec._M_type == __format::_Pres_c)
2655 return _M_f._M_format_character(__u, __fc);
2657 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2660#if __glibcxx_format_ranges
2662 set_debug_format() noexcept
2663 { _M_f._M_spec._M_debug =
true; }
2667 __format::__formatter_int<wchar_t> _M_f;
2674 template<__format::__
char _CharT>
2675 struct formatter<_CharT*, _CharT>
2677 formatter() =
default;
2679 [[__gnu__::__always_inline__]]
2680 constexpr typename basic_format_parse_context<_CharT>::iterator
2681 parse(basic_format_parse_context<_CharT>& __pc)
2682 {
return _M_f.parse(__pc); }
2684 template<
typename _Out>
2685 [[__gnu__::__nonnull__]]
2686 _GLIBCXX_CONSTEXPR_FORMAT
2687 typename basic_format_context<_Out, _CharT>::iterator
2688 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2689 {
return _M_f.format(__u, __fc); }
2691#if __glibcxx_format_ranges
2692 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2696 __format::__formatter_str<_CharT> _M_f;
2699#if __glibcxx_print >= 202403L
2700 template<__format::__
char _CharT>
2701 constexpr bool enable_nonlocking_formatter_optimization<_CharT*> =
true;
2704 template<__format::__
char _CharT>
2705 struct formatter<const _CharT*, _CharT>
2707 formatter() =
default;
2709 [[__gnu__::__always_inline__]]
2710 constexpr typename basic_format_parse_context<_CharT>::iterator
2711 parse(basic_format_parse_context<_CharT>& __pc)
2712 {
return _M_f.parse(__pc); }
2714 template<
typename _Out>
2715 [[__gnu__::__nonnull__]]
2716 _GLIBCXX_CONSTEXPR_FORMAT
2717 typename basic_format_context<_Out, _CharT>::iterator
2718 format(
const _CharT* __u,
2719 basic_format_context<_Out, _CharT>& __fc)
const
2720 {
return _M_f.format(__u, __fc); }
2722#if __glibcxx_format_ranges
2723 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2727 __format::__formatter_str<_CharT> _M_f;
2730#if __glibcxx_print >= 202403L
2731 template<__format::__
char _CharT>
2733 enable_nonlocking_formatter_optimization<const _CharT*> =
true;
2736 template<__format::__
char _CharT,
size_t _Nm>
2737 struct formatter<_CharT[_Nm], _CharT>
2739 formatter() =
default;
2741 [[__gnu__::__always_inline__]]
2742 constexpr typename basic_format_parse_context<_CharT>::iterator
2743 parse(basic_format_parse_context<_CharT>& __pc)
2744 {
return _M_f.parse(__pc); }
2746 template<
typename _Out>
2747 _GLIBCXX_CONSTEXPR_FORMAT
2748 typename basic_format_context<_Out, _CharT>::iterator
2749 format(
const _CharT (&__u)[_Nm],
2750 basic_format_context<_Out, _CharT>& __fc)
const
2751 {
return _M_f.format({__u, _Nm}, __fc); }
2753#if __glibcxx_format_ranges
2754 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2758 __format::__formatter_str<_CharT> _M_f;
2761#if __glibcxx_print >= 202403L
2762 template<__format::__
char _CharT,
size_t _Nm>
2763 constexpr bool enable_nonlocking_formatter_optimization<_CharT[_Nm]> =
true;
2766 template<
typename _Traits,
typename _Alloc>
2767 struct formatter<
basic_string<char, _Traits, _Alloc>, char>
2769 formatter() =
default;
2771 [[__gnu__::__always_inline__]]
2772 constexpr typename basic_format_parse_context<char>::iterator
2773 parse(basic_format_parse_context<char>& __pc)
2774 {
return _M_f.parse(__pc); }
2776 template<
typename _Out>
2777 _GLIBCXX_CONSTEXPR_FORMAT
2778 typename basic_format_context<_Out, char>::iterator
2779 format(
const basic_string<char, _Traits, _Alloc>& __u,
2780 basic_format_context<_Out, char>& __fc)
const
2781 {
return _M_f.format(__u, __fc); }
2783#if __glibcxx_format_ranges
2784 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2788 __format::__formatter_str<char> _M_f;
2791#if __glibcxx_print >= 202403L
2792 template<
typename _Tr,
typename _Alloc>
2794 enable_nonlocking_formatter_optimization<basic_string<char, _Tr, _Alloc>>
2798#ifdef _GLIBCXX_USE_WCHAR_T
2799 template<
typename _Traits,
typename _Alloc>
2800 struct formatter<
basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2802 formatter() =
default;
2804 [[__gnu__::__always_inline__]]
2805 constexpr typename basic_format_parse_context<wchar_t>::iterator
2806 parse(basic_format_parse_context<wchar_t>& __pc)
2807 {
return _M_f.parse(__pc); }
2809 template<
typename _Out>
2810 _GLIBCXX_CONSTEXPR_FORMAT
2811 typename basic_format_context<_Out, wchar_t>::iterator
2812 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2813 basic_format_context<_Out, wchar_t>& __fc)
const
2814 {
return _M_f.format(__u, __fc); }
2816#if __glibcxx_format_ranges
2817 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2821 __format::__formatter_str<wchar_t> _M_f;
2824#if __glibcxx_print >= 202403L
2825 template<
typename _Tr,
typename _Alloc>
2827 enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Tr, _Alloc>>
2833 template<
typename _Traits>
2836 formatter() =
default;
2838 [[__gnu__::__always_inline__]]
2839 constexpr typename basic_format_parse_context<char>::iterator
2840 parse(basic_format_parse_context<char>& __pc)
2841 {
return _M_f.parse(__pc); }
2843 template<
typename _Out>
2844 _GLIBCXX_CONSTEXPR_FORMAT
2845 typename basic_format_context<_Out, char>::iterator
2846 format(basic_string_view<char, _Traits> __u,
2847 basic_format_context<_Out, char>& __fc)
const
2848 {
return _M_f.format(__u, __fc); }
2850#if __glibcxx_format_ranges
2851 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2855 __format::__formatter_str<char> _M_f;
2858#if __glibcxx_print >= 202403L
2859 template<
typename _Tr>
2861 enable_nonlocking_formatter_optimization<basic_string_view<char, _Tr>>
2865#ifdef _GLIBCXX_USE_WCHAR_T
2866 template<
typename _Traits>
2869 formatter() =
default;
2871 [[__gnu__::__always_inline__]]
2872 constexpr typename basic_format_parse_context<wchar_t>::iterator
2873 parse(basic_format_parse_context<wchar_t>& __pc)
2874 {
return _M_f.parse(__pc); }
2876 template<
typename _Out>
2877 _GLIBCXX_CONSTEXPR_FORMAT
2878 typename basic_format_context<_Out, wchar_t>::iterator
2879 format(basic_string_view<wchar_t, _Traits> __u,
2880 basic_format_context<_Out, wchar_t>& __fc)
const
2881 {
return _M_f.format(__u, __fc); }
2883#if __glibcxx_format_ranges
2884 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2888 __format::__formatter_str<wchar_t> _M_f;
2891#if __glibcxx_print >= 202403L
2892 template<
typename _Tr>
2894 enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Tr>>
2905 template<
typename _Tp>
2906 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2908#if defined __SIZEOF_INT128__
2909 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2910 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2914 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2915 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2916#ifdef _GLIBCXX_USE_CHAR8_T
2917 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2919 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2920 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2922 template<
typename _Tp>
2923 concept __formattable_integer = __is_formattable_integer<_Tp>;
2928 template<__format::__formattable_
integer _Tp, __format::__
char _CharT>
2929 struct formatter<_Tp, _CharT>
2931 formatter() =
default;
2933 [[__gnu__::__always_inline__]]
2934 constexpr typename basic_format_parse_context<_CharT>::iterator
2935 parse(basic_format_parse_context<_CharT>& __pc)
2937 return _M_f.template _M_parse<_Tp>(__pc);
2940 template<
typename _Out>
2941 _GLIBCXX_CONSTEXPR_FORMAT
2942 typename basic_format_context<_Out, _CharT>::iterator
2943 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2944 {
return _M_f.format(__u, __fc); }
2947 __format::__formatter_int<_CharT> _M_f;
2950#if __glibcxx_print >= 202403L
2951 template<__format::__formattable_
integer _Tp>
2953 enable_nonlocking_formatter_optimization<_Tp> =
true;
2956#if defined __glibcxx_to_chars
2958 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2959 struct formatter<_Tp, _CharT>
2961 formatter() =
default;
2963 [[__gnu__::__always_inline__]]
2964 constexpr typename basic_format_parse_context<_CharT>::iterator
2965 parse(basic_format_parse_context<_CharT>& __pc)
2966 {
return _M_f.parse(__pc); }
2968 template<
typename _Out>
2969 typename basic_format_context<_Out, _CharT>::iterator
2970 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2971 {
return _M_f.format(__u, __fc); }
2974 __format::__formatter_fp<_CharT> _M_f;
2977#if __glibcxx_print >= 202403L
2978 template<__format::__formattable_
float _Tp>
2980 enable_nonlocking_formatter_optimization<_Tp> =
true;
2983#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2985 template<__format::__
char _CharT>
2986 struct formatter<long double, _CharT>
2988 formatter() =
default;
2990 [[__gnu__::__always_inline__]]
2991 constexpr typename basic_format_parse_context<_CharT>::iterator
2992 parse(basic_format_parse_context<_CharT>& __pc)
2993 {
return _M_f.parse(__pc); }
2995 template<
typename _Out>
2996 typename basic_format_context<_Out, _CharT>::iterator
2997 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2998 {
return _M_f.format((
double)__u, __fc); }
3001 __format::__formatter_fp<_CharT> _M_f;
3005#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3007 template<__format::__
char _CharT>
3008 struct formatter<_Float16, _CharT>
3010 formatter() =
default;
3012 [[__gnu__::__always_inline__]]
3013 constexpr typename basic_format_parse_context<_CharT>::iterator
3014 parse(basic_format_parse_context<_CharT>& __pc)
3015 {
return _M_f.parse(__pc); }
3017 template<
typename _Out>
3018 typename basic_format_context<_Out, _CharT>::iterator
3019 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
3020 {
return _M_f.format((
float)__u, __fc); }
3023 __format::__formatter_fp<_CharT> _M_f;
3027#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3029 template<__format::__
char _CharT>
3030 struct formatter<_Float32, _CharT>
3032 formatter() =
default;
3034 [[__gnu__::__always_inline__]]
3035 constexpr typename basic_format_parse_context<_CharT>::iterator
3036 parse(basic_format_parse_context<_CharT>& __pc)
3037 {
return _M_f.parse(__pc); }
3039 template<
typename _Out>
3040 typename basic_format_context<_Out, _CharT>::iterator
3041 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
3042 {
return _M_f.format((
float)__u, __fc); }
3045 __format::__formatter_fp<_CharT> _M_f;
3049#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3051 template<__format::__
char _CharT>
3052 struct formatter<_Float64, _CharT>
3054 formatter() =
default;
3056 [[__gnu__::__always_inline__]]
3057 constexpr typename basic_format_parse_context<_CharT>::iterator
3058 parse(basic_format_parse_context<_CharT>& __pc)
3059 {
return _M_f.parse(__pc); }
3061 template<
typename _Out>
3062 typename basic_format_context<_Out, _CharT>::iterator
3063 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
3064 {
return _M_f.format((
double)__u, __fc); }
3067 __format::__formatter_fp<_CharT> _M_f;
3071#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128
3073 template<__format::__
char _CharT>
3074 struct formatter<_Float128, _CharT>
3076 formatter() =
default;
3078 [[__gnu__::__always_inline__]]
3079 constexpr typename basic_format_parse_context<_CharT>::iterator
3080 parse(basic_format_parse_context<_CharT>& __pc)
3081 {
return _M_f.parse(__pc); }
3083 template<
typename _Out>
3084 typename basic_format_context<_Out, _CharT>::iterator
3085 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3086 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3089 __format::__formatter_fp<_CharT> _M_f;
3093#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128 == 2
3096 template<__format::__
char _CharT>
3097 struct formatter<__float128, _CharT>
3099 formatter() =
default;
3101 [[__gnu__::__always_inline__]]
3102 constexpr typename basic_format_parse_context<_CharT>::iterator
3103 parse(basic_format_parse_context<_CharT>& __pc)
3104 {
return _M_f.parse(__pc); }
3106 template<
typename _Out>
3107 typename basic_format_context<_Out, _CharT>::iterator
3108 format(__float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3109 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3112 __format::__formatter_fp<_CharT> _M_f;
3116#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3118 template<__format::__
char _CharT>
3119 struct formatter<__format::__bflt16_t, _CharT>
3121 formatter() =
default;
3123 [[__gnu__::__always_inline__]]
3124 constexpr typename basic_format_parse_context<_CharT>::iterator
3125 parse(basic_format_parse_context<_CharT>& __pc)
3126 {
return _M_f.parse(__pc); }
3128 template<
typename _Out>
3129 typename basic_format_context<_Out, _CharT>::iterator
3130 format(__gnu_cxx::__bfloat16_t __u,
3131 basic_format_context<_Out, _CharT>& __fc)
const
3132 {
return _M_f.format((
float)__u, __fc); }
3135 __format::__formatter_fp<_CharT> _M_f;
3143 template<__format::__
char _CharT>
3144 struct formatter<const void*, _CharT>
3146 formatter() =
default;
3148 constexpr typename basic_format_parse_context<_CharT>::iterator
3149 parse(basic_format_parse_context<_CharT>& __pc)
3150 {
return _M_f.parse(__pc); }
3152 template<
typename _Out>
3153 _GLIBCXX_CONSTEXPR_FORMAT
3154 typename basic_format_context<_Out, _CharT>::iterator
3155 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3156 {
return _M_f.format(__v, __fc); }
3159 __format::__formatter_ptr<_CharT> _M_f;
3162#if __glibcxx_print >= 202403L
3164 inline constexpr bool
3165 enable_nonlocking_formatter_optimization<const void*> =
true;
3168 template<__format::__
char _CharT>
3169 struct formatter<void*, _CharT>
3171 formatter() =
default;
3173 [[__gnu__::__always_inline__]]
3174 constexpr typename basic_format_parse_context<_CharT>::iterator
3175 parse(basic_format_parse_context<_CharT>& __pc)
3176 {
return _M_f.parse(__pc); }
3178 template<
typename _Out>
3179 _GLIBCXX_CONSTEXPR_FORMAT
3180 typename basic_format_context<_Out, _CharT>::iterator
3181 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3182 {
return _M_f.format(__v, __fc); }
3185 __format::__formatter_ptr<_CharT> _M_f;
3188#if __glibcxx_print >= 202403l
3190 inline constexpr bool
3191 enable_nonlocking_formatter_optimization<void*> =
true;
3194 template<__format::__
char _CharT>
3195 struct formatter<nullptr_t, _CharT>
3197 formatter() =
default;
3199 [[__gnu__::__always_inline__]]
3200 constexpr typename basic_format_parse_context<_CharT>::iterator
3201 parse(basic_format_parse_context<_CharT>& __pc)
3202 {
return _M_f.parse(__pc); }
3204 template<
typename _Out>
3205 _GLIBCXX_CONSTEXPR_FORMAT
3206 typename basic_format_context<_Out, _CharT>::iterator
3207 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3208 {
return _M_f.format(
nullptr, __fc); }
3211 __format::__formatter_ptr<_CharT> _M_f;
3215#if __glibcxx_print >= 202403L
3217 inline constexpr bool
3218 enable_nonlocking_formatter_optimization<nullptr_t> =
true;
3221#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3225 struct __formatter_disabled
3227 __formatter_disabled() =
delete;
3228 __formatter_disabled(
const __formatter_disabled&) =
delete;
3229 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3233 struct formatter<char*,
wchar_t>
3234 :
private __formatter_disabled { };
3236 struct formatter<const char*,
wchar_t>
3237 :
private __formatter_disabled { };
3238 template<
size_t _Nm>
3239 struct formatter<char[_Nm], wchar_t>
3240 :
private __formatter_disabled { };
3241 template<
class _Traits,
class _Allocator>
3242 struct formatter<
basic_string<char, _Traits, _Allocator>, wchar_t>
3243 :
private __formatter_disabled { };
3244 template<
class _Traits>
3246 :
private __formatter_disabled { };
3251 template<
typename _Out>
3252 struct format_to_n_result
3255 iter_difference_t<_Out> size;
3258_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3259template<
typename,
typename>
class vector;
3260_GLIBCXX_END_NAMESPACE_CONTAINER
3265 template<
typename _CharT>
3269 using iterator_category = output_iterator_tag;
3270 using value_type = void;
3271 using difference_type = ptrdiff_t;
3272 using pointer = void;
3273 using reference = void;
3275 _Drop_iter() =
default;
3276 _Drop_iter(
const _Drop_iter&) =
default;
3277 _Drop_iter& operator=(
const _Drop_iter&) =
default;
3279 [[__gnu__::__always_inline__]]
3280 constexpr _Drop_iter&
3281 operator=(_CharT __c)
3284 [[__gnu__::__always_inline__]]
3285 constexpr _Drop_iter&
3286 operator=(basic_string_view<_CharT> __s)
3289 [[__gnu__::__always_inline__]]
3290 constexpr _Drop_iter&
3293 [[__gnu__::__always_inline__]]
3294 constexpr _Drop_iter&
3295 operator++() {
return *
this; }
3297 [[__gnu__::__always_inline__]]
3298 constexpr _Drop_iter
3299 operator++(
int) {
return *
this; }
3302 template<
typename _CharT>
3305 _Sink<_CharT>* _M_sink =
nullptr;
3308 using iterator_category = output_iterator_tag;
3309 using value_type = void;
3310 using difference_type = ptrdiff_t;
3311 using pointer = void;
3312 using reference = void;
3314 _Sink_iter() =
default;
3315 _Sink_iter(
const _Sink_iter&) =
default;
3316 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3318 [[__gnu__::__always_inline__]]
3320 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3322 [[__gnu__::__always_inline__]]
3323 constexpr _Sink_iter&
3324 operator=(_CharT __c)
3326 _M_sink->_M_write(__c);
3330 [[__gnu__::__always_inline__]]
3331 constexpr _Sink_iter&
3332 operator=(basic_string_view<_CharT> __s)
3334 _M_sink->_M_write(__s);
3338 [[__gnu__::__always_inline__]]
3339 constexpr _Sink_iter&
3342 [[__gnu__::__always_inline__]]
3343 constexpr _Sink_iter&
3344 operator++() {
return *
this; }
3346 [[__gnu__::__always_inline__]]
3347 constexpr _Sink_iter
3348 operator++(
int) {
return *
this; }
3350 _GLIBCXX_CONSTEXPR_FORMAT
auto
3351 _M_reserve(
size_t __n)
const
3352 {
return _M_sink->_M_reserve(__n); }
3354 _GLIBCXX_CONSTEXPR_FORMAT
bool
3355 _M_discarding()
const
3356 {
return _M_sink->_M_discarding(); }
3362 template<
typename _CharT>
3365 friend class _Sink_iter<_CharT>;
3367 span<_CharT> _M_span;
3368 typename span<_CharT>::iterator _M_next;
3374 virtual void _M_overflow() = 0;
3378 [[__gnu__::__always_inline__]]
3380 _Sink(span<_CharT> __span) noexcept
3381 : _M_span(__span), _M_next(__span.begin())
3385 [[__gnu__::__always_inline__]]
3386 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3387 _M_used() const noexcept
3388 {
return _M_span.first(_M_next - _M_span.begin()); }
3391 [[__gnu__::__always_inline__]]
3392 constexpr span<_CharT>
3393 _M_unused() const noexcept
3394 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3397 [[__gnu__::__always_inline__]]
3399 _M_rewind() noexcept
3400 { _M_next = _M_span.begin(); }
3403 _GLIBCXX_CONSTEXPR_FORMAT
void
3404 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3407 _M_next = __s.begin() + __pos;
3412 _M_write(_CharT __c)
3415 if (_M_next - _M_span.begin() ==
std::ssize(_M_span)) [[unlikely]]
3420 _M_write(basic_string_view<_CharT> __s)
3422 span __to = _M_unused();
3423 while (__to.size() <= __s.size())
3425 __s.copy(__to.data(), __to.size());
3426 _M_next += __to.size();
3427 __s.remove_prefix(__to.size());
3433 __s.copy(__to.data(), __s.size());
3434 _M_next += __s.size();
3443 _GLIBCXX_CONSTEXPR_FORMAT
3444 explicit operator bool() const noexcept {
return _M_sink; }
3446 _GLIBCXX_CONSTEXPR_FORMAT _CharT*
3447 get() const noexcept {
return _M_sink->_M_next.operator->(); }
3449 _GLIBCXX_CONSTEXPR_FORMAT
void
3450 _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3458 _GLIBCXX_CONSTEXPR_FORMAT
virtual _Reservation
3459 _M_reserve(
size_t __n)
3461 if (__n <= _M_unused().size())
3464 if (__n <= _M_span.size())
3467 if (__n <= _M_unused().size())
3475 _GLIBCXX_CONSTEXPR_FORMAT
virtual void
3480 _GLIBCXX_CONSTEXPR_FORMAT
virtual bool
3481 _M_discarding()
const
3485 _Sink(
const _Sink&) =
delete;
3486 _Sink& operator=(
const _Sink&) =
delete;
3488 [[__gnu__::__always_inline__]]
3489 constexpr _Sink_iter<_CharT>
3491 {
return _Sink_iter<_CharT>(*
this); }
3495 template<
typename _CharT>
3496 class _Fixedbuf_sink final :
public _Sink<_CharT>
3498 _GLIBCXX_CONSTEXPR_FORMAT
void
3499 _M_overflow()
override
3501 __glibcxx_assert(
false);
3506 [[__gnu__::__always_inline__]]
3508 _Fixedbuf_sink(span<_CharT> __buf)
3509 : _Sink<_CharT>(__buf)
3512 constexpr basic_string_view<_CharT>
3515 auto __s = this->_M_used();
3516 return basic_string_view<_CharT>(__s.data(), __s.size());
3521 template<
typename _CharT>
3522 class _Buf_sink :
public _Sink<_CharT>
3525 _CharT _M_buf[__stackbuf_size<_CharT>];
3527 [[__gnu__::__always_inline__]]
3529 _Buf_sink() noexcept
3530 : _Sink<_CharT>(_M_buf)
3534 using _GLIBCXX_STD_C::vector;
3538 template<
typename _Seq>
3539 class _Seq_sink :
public _Buf_sink<typename _Seq::value_type>
3541 using _CharT =
typename _Seq::value_type;
3546 _GLIBCXX_CONSTEXPR_FORMAT
void
3547 _M_overflow()
override
3549 auto __s = this->_M_used();
3550 if (__s.empty()) [[unlikely]]
3554 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3556 if constexpr (__is_specialization_of<_Seq, basic_string>)
3557 _M_seq.append(__s.data(), __s.size());
3559 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3565 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3566 _M_reserve(
size_t __n)
override
3575 if constexpr (__is_specialization_of<_Seq, basic_string>
3576 || __is_specialization_of<_Seq, vector>)
3579 if (this->_M_used().size()) [[unlikely]]
3580 _Seq_sink::_M_overflow();
3583 const auto __sz = _M_seq.size();
3584 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3585 _M_seq.__resize_and_overwrite(__sz + __n,
3586 [](
auto,
auto __n2) {
3590 _M_seq.resize(__sz + __n);
3594 this->_M_reset(_M_seq, __sz);
3598 return _Sink<_CharT>::_M_reserve(__n);
3601 _GLIBCXX_CONSTEXPR_FORMAT
void
3602 _M_bump(
size_t __n)
override
3604 if constexpr (__is_specialization_of<_Seq, basic_string>
3605 || __is_specialization_of<_Seq, vector>)
3607 auto __s = this->_M_used();
3608 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3610 _M_seq.resize(__s.size() + __n);
3612 this->_M_reset(this->_M_buf);
3616 _GLIBCXX_CONSTEXPR_FORMAT
void
3617 _M_trim(span<const _CharT> __s)
3618 requires __is_specialization_of<_Seq, basic_string>
3620 _GLIBCXX_DEBUG_ASSERT(__s.data() == this->_M_buf
3621 || __s.data() == _M_seq.data());
3622 if (__s.data() == _M_seq.data())
3623 _M_seq.resize(__s.size());
3625 this->_M_reset(this->_M_buf, __s.size());
3632 [[__gnu__::__always_inline__]]
3633 _GLIBCXX_CONSTEXPR_FORMAT
3634 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3637 _GLIBCXX_CONSTEXPR_FORMAT
3638 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3639 : _M_seq(std::move(__s))
3642 using _Sink<_CharT>::out;
3644 _GLIBCXX_CONSTEXPR_FORMAT _Seq
3647 if (this->_M_used().size() != 0)
3648 _Seq_sink::_M_overflow();
3654 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3657 auto __s = this->_M_used();
3660 if (__s.size() != 0)
3661 _Seq_sink::_M_overflow();
3667 _GLIBCXX_CONSTEXPR_FORMAT basic_string_view<_CharT>
3670 auto __span = _M_span();
3671 return basic_string_view<_CharT>(__span.data(), __span.size());
3675 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3677 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3683 template<
typename _CharT,
typename _OutIter>
3684 class _Iter_sink :
public _Buf_sink<_CharT>
3687 iter_difference_t<_OutIter> _M_max;
3690 size_t _M_count = 0;
3692 _GLIBCXX_CONSTEXPR_FORMAT
void
3693 _M_overflow()
override
3695 auto __s = this->_M_used();
3697 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3698 else if (_M_count <
static_cast<size_t>(_M_max))
3700 auto __max = _M_max - _M_count;
3701 span<_CharT> __first;
3702 if (__max < __s.size())
3703 __first = __s.first(
static_cast<size_t>(__max));
3706 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3709 _M_count += __s.size();
3712 _GLIBCXX_CONSTEXPR_FORMAT
bool
3713 _M_discarding()
const override
3721 [[__gnu__::__always_inline__]]
3722 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3723 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3724 : _M_out(std::move(__out)), _M_max(__max)
3727 using _Sink<_CharT>::out;
3729 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3732 if (this->_M_used().size() != 0)
3733 _Iter_sink::_M_overflow();
3734 iter_difference_t<_OutIter> __count(_M_count);
3746 template<
typename _CharT>
3747 class _Ptr_sink :
public _Sink<_CharT>
3749 static constexpr size_t _S_no_limit = size_t(-1);
3753 size_t _M_count = 0;
3758 _GLIBCXX_CONSTEXPR_FORMAT
void
3759 _M_overflow()
override
3761 if (this->_M_unused().size() != 0)
3764 auto __s = this->_M_used();
3766 if (_M_max != _S_no_limit)
3768 _M_count += __s.size();
3772 this->_M_reset(this->_M_buf);
3778 _M_rebuf(__s.data(), __s.size() + 1024, __s.size());
3782 _GLIBCXX_CONSTEXPR_FORMAT
bool
3783 _M_discarding()
const override
3790 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3791 _M_reserve(
size_t __n)
final
3793 auto __avail = this->_M_unused();
3794 if (__n > __avail.size())
3796 if (_M_max != _S_no_limit)
3799 auto __s = this->_M_used();
3800 _M_rebuf(__s.data(), __s.size() + __n, __s.size());
3806 template<
typename _IterDifference>
3807 static _GLIBCXX_CONSTEXPR_FORMAT
size_t
3808 _S_trim_max(_IterDifference __max)
3812 if constexpr (!is_integral_v<_IterDifference> ||
sizeof(__max) >
sizeof(size_t))
3814 if (_IterDifference((
size_t)-1) < __max)
3816 return size_t(__max);
3819 [[__gnu__::__always_inline__]]
3820 _GLIBCXX_CONSTEXPR_FORMAT
void
3821 _M_rebuf(_CharT* __ptr,
size_t __total,
size_t __inuse = 0)
3823 std::span<_CharT> __span(__ptr, __total);
3824 this->_M_reset(__span, __inuse);
3828 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3829 _Ptr_sink(_CharT* __ptr,
size_t __n = _S_no_limit) noexcept
3830 : _Sink<_CharT>(_M_buf), _M_max(__n)
3834 else if (__n != _S_no_limit)
3835 _M_rebuf(__ptr, __n);
3836#if __has_builtin(__builtin_dynamic_object_size)
3837 else if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3838 _M_rebuf(__ptr, __bytes /
sizeof(_CharT));
3843 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3844 __n = (1024 - __off) /
sizeof(_CharT);
3845 if (__n > 0) [[likely]]
3846 _M_rebuf(__ptr, __n);
3852 template<contiguous_iterator _OutIter>
3853 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3854 _Ptr_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1)
3855 : _Ptr_sink(std::
to_address(__out), _S_trim_max(__n))
3858 template<contiguous_iterator _OutIter>
3859 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3860 _M_finish(_OutIter __first)
const
3862 auto __s = this->_M_used();
3863 if (__s.data() == _M_buf)
3866 iter_difference_t<_OutIter> __m(_M_max);
3867 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3868 return { __first + __m, __count };
3872 iter_difference_t<_OutIter> __count(__s.size());
3873 return { __first + __count, __count };
3878 template<
typename _CharT,
typename _OutIter>
3879 concept __contiguous_char_iter
3880 = contiguous_iterator<_OutIter>
3881 && same_as<iter_value_t<_OutIter>, _CharT>;
3892 template<
typename _Out,
typename _CharT>
3893 class _Padding_sink :
public _Str_sink<_CharT>
3898 size_t _M_printwidth;
3900 [[__gnu__::__always_inline__]]
3901 _GLIBCXX_CONSTEXPR_FORMAT
bool
3903 {
return _M_printwidth >= _M_maxwidth; }
3905 [[__gnu__::__always_inline__]]
3906 _GLIBCXX_CONSTEXPR_FORMAT
bool
3907 _M_buffering()
const
3909 if (_M_printwidth < _M_padwidth)
3911 if (_M_maxwidth != (
size_t)-1)
3912 return _M_printwidth < _M_maxwidth;
3916 _GLIBCXX_CONSTEXPR_FORMAT
void
3917 _M_sync_discarding()
3919 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3920 if (_M_out._M_discarding())
3921 _M_maxwidth = _M_printwidth;
3924 _GLIBCXX_CONSTEXPR_FORMAT
void
3927 span<_CharT> __new = this->_M_used();
3928 basic_string_view<_CharT> __str(__new.data(), __new.size());
3929 _M_out = __format::__write(
std::move(_M_out), __str);
3930 _M_sync_discarding();
3934 _GLIBCXX_CONSTEXPR_FORMAT
bool
3937 auto __str = this->view();
3939 _M_printwidth = __format::__truncate(__str, _M_maxwidth);
3941 this->_M_trim(__str);
3947 if (_M_printwidth >= _M_padwidth)
3949 _M_out = __format::__write(
std::move(_M_out), __str);
3950 _M_sync_discarding();
3955 _Str_sink<_CharT>::_M_overflow();
3958 this->_M_reset(this->_M_buf);
3962 _GLIBCXX_CONSTEXPR_FORMAT
bool
3963 _M_update(
size_t __new)
3965 _M_printwidth += __new;
3967 if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
3968 return _M_force_update();
3972 _GLIBCXX_CONSTEXPR_FORMAT
void
3973 _M_overflow()
override
3979 else if (!_M_buffering())
3983 else if (_M_update(this->_M_used().size()))
3984 _Str_sink<_CharT>::_M_overflow();
3987 _GLIBCXX_CONSTEXPR_FORMAT
bool
3988 _M_discarding()
const override
3989 {
return _M_ignoring(); }
3991 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3992 _M_reserve(
size_t __n)
override
3997 else if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3998 if (!_M_buffering())
4001 if (!this->_M_used().empty())
4004 if (
auto __reserved = _M_out._M_reserve(__n))
4007 return _Sink<_CharT>::_M_reserve(__n);
4010 _GLIBCXX_CONSTEXPR_FORMAT
void
4011 _M_bump(
size_t __n)
override
4018 _Sink<_CharT>::_M_bump(__n);
4024 [[__gnu__::__always_inline__]]
4025 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4026 _Padding_sink(_Out __out,
size_t __padwidth,
size_t __maxwidth)
4027 : _M_padwidth(__padwidth), _M_maxwidth(__maxwidth),
4028 _M_out(std::move(__out)), _M_printwidth(0)
4029 { _M_sync_discarding(); }
4031 [[__gnu__::__always_inline__]]
4032 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4033 _Padding_sink(_Out __out,
size_t __padwidth)
4034 : _Padding_sink(std::move(__out), __padwidth, (size_t)-1)
4037 _GLIBCXX_CONSTEXPR_FORMAT _Out
4038 _M_finish(_Align __align,
char32_t __fill_char)
4041 if (
auto __rem = this->_M_used().size())
4045 else if (!_M_buffering())
4051 if (!_M_buffering() || !_M_force_update())
4053 if (_M_printwidth >= _M_padwidth)
4056 const auto __str = this->view();
4057 if (_M_printwidth >= _M_padwidth)
4058 return __format::__write(
std::move(_M_out), __str);
4060 const size_t __nfill = _M_padwidth - _M_printwidth;
4061 return __format::__write_padded(
std::move(_M_out), __str,
4062 __align, __nfill, __fill_char);
4066 template<
typename _Out,
typename _CharT>
4067 class _Escaping_sink :
public _Buf_sink<_CharT>
4069 using _Esc = _Escapes<_CharT>;
4072 _Term_char _M_term : 2;
4073 unsigned _M_prev_escape : 1;
4074 unsigned _M_out_discards : 1;
4076 _GLIBCXX_CONSTEXPR_FORMAT
void
4077 _M_sync_discarding()
4079 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4080 _M_out_discards = _M_out._M_discarding();
4083 _GLIBCXX_CONSTEXPR_FORMAT
void
4086 span<_CharT> __bytes = this->_M_used();
4087 basic_string_view<_CharT> __str(__bytes.data(), __bytes.size());
4090 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4092 bool __prev_escape = _M_prev_escape;
4093 _M_out = __format::__write_escaped_unicode_part(
4094 std::move(_M_out), __str, __prev_escape, _M_term);
4095 _M_prev_escape = __prev_escape;
4097 __rem = __str.size();
4098 if (__rem > 0 && __str.data() != this->_M_buf) [[unlikely]]
4099 ranges::move(__str, this->_M_buf);
4102 _M_out = __format::__write_escaped_ascii(
4105 this->_M_reset(this->_M_buf, __rem);
4106 _M_sync_discarding();
4109 _GLIBCXX_CONSTEXPR_FORMAT
void
4110 _M_overflow()
override
4112 if (_M_out_discards)
4118 _GLIBCXX_CONSTEXPR_FORMAT
bool
4119 _M_discarding()
const override
4120 {
return _M_out_discards; }
4123 [[__gnu__::__always_inline__]]
4124 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4125 _Escaping_sink(_Out __out, _Term_char __term)
4126 : _M_out(std::move(__out)), _M_term(__term),
4127 _M_prev_escape(true), _M_out_discards(false)
4129 _M_out = __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4130 _M_sync_discarding();
4133 _GLIBCXX_CONSTEXPR_FORMAT _Out
4136 if (_M_out_discards)
4139 if (!this->_M_used().empty())
4142 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4143 if (
auto __rem = this->_M_used(); !__rem.empty())
4145 basic_string_view<_CharT> __str(__rem.data(), __rem.size());
4146 _M_out = __format::__write_escape_seqs(
std::move(_M_out), __str);
4149 return __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4153 enum class _Arg_t :
unsigned char {
4154 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
4155 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
4156 _Arg_i128, _Arg_u128, _Arg_float128,
4157 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
4160#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4161 _Arg_ibm128 = _Arg_ldbl,
4162 _Arg_ieee128 = _Arg_float128,
4167 template<
typename _Context>
4170 using _CharT =
typename _Context::char_type;
4174 using _CharT =
typename _Context::char_type;
4175 using _Func = void(*)(basic_format_parse_context<_CharT>&,
4176 _Context&,
const void*);
4179 template<
typename _Tp>
4180 using __maybe_const_t
4181 = __conditional_t<__formattable_with<const _Tp, _Context>,
4184 template<
typename _Tq>
4185 static _GLIBCXX_CONSTEXPR_FORMAT
void
4186 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
4187 _Context& __format_ctx,
const void* __ptr)
4189 using _Td = remove_const_t<_Tq>;
4190 typename _Context::template formatter_type<_Td> __f;
4191 __parse_ctx.advance_to(__f.parse(__parse_ctx));
4192 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
4193 __format_ctx.advance_to(__f.format(__val, __format_ctx));
4196 template<
typename _Tp>
4197 requires (!is_same_v<remove_cv_t<_Tp>, handle>)
4198 explicit _GLIBCXX_CONSTEXPR_FORMAT
4199 handle(_Tp& __val) noexcept
4200 : _M_ptr(__builtin_addressof(__val))
4201 , _M_func(&_S_format<__maybe_const_t<_Tp>>)
4204 friend class basic_format_arg<_Context>;
4207 handle(
const handle&) =
default;
4208 handle& operator=(
const handle&) =
default;
4210 [[__gnu__::__always_inline__]]
4211 void _GLIBCXX_CONSTEXPR_FORMAT
4212 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
4213 { _M_func(__pc, __fc, this->_M_ptr); }
4228 unsigned long long _M_ull;
4231#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4232 long double _M_ldbl;
4235 __ieee128 _M_ieee128;
4237#ifdef __SIZEOF_FLOAT128__
4238 __float128 _M_float128;
4240 const _CharT* _M_str;
4241 basic_string_view<_CharT> _M_sv;
4244#ifdef __SIZEOF_INT128__
4246 unsigned __int128 _M_u128;
4248#ifdef __BFLT16_DIG__
4262 [[__gnu__::__always_inline__]]
4263 _GLIBCXX_CONSTEXPR_FORMAT
4264 _Arg_value() : _M_none() { }
4267 template<
typename _Tp>
4268 _GLIBCXX_CONSTEXPR_FORMAT
4269 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
4270 { _S_get<_Tp>() = __val; }
4275 template<
typename _Tp,
typename _Self,
typename... _Value>
4276 [[__gnu__::__always_inline__]]
4277 static _GLIBCXX_CONSTEXPR_FORMAT
auto&
4278 _S_access(_Self& __u, _Value... __value)
noexcept
4280 static_assert(
sizeof...(_Value) <= 1);
4281 if constexpr (is_same_v<_Tp, bool>)
4282 return (__u._M_bool = ... = __value);
4283 else if constexpr (is_same_v<_Tp, _CharT>)
4284 return (__u._M_c = ... = __value);
4285 else if constexpr (is_same_v<_Tp, int>)
4286 return (__u._M_i = ... = __value);
4287 else if constexpr (is_same_v<_Tp, unsigned>)
4288 return (__u._M_u = ... = __value);
4289 else if constexpr (is_same_v<_Tp, long long>)
4290 return (__u._M_ll = ... = __value);
4291 else if constexpr (is_same_v<_Tp, unsigned long long>)
4292 return (__u._M_ull = ... = __value);
4293 else if constexpr (is_same_v<_Tp, float>)
4294 return (__u._M_flt = ... = __value);
4295 else if constexpr (is_same_v<_Tp, double>)
4296 return (__u._M_dbl = ... = __value);
4297#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4298 else if constexpr (is_same_v<_Tp, long double>)
4299 return (__u._M_ldbl = ... = __value);
4301 else if constexpr (is_same_v<_Tp, __ibm128>)
4302 return (__u._M_ibm128 = ... = __value);
4303 else if constexpr (is_same_v<_Tp, __ieee128>)
4304 return (__u._M_ieee128 = ... = __value);
4306#ifdef __SIZEOF_FLOAT128__
4307 else if constexpr (is_same_v<_Tp, __float128>)
4308 return (__u._M_float128 = ... = __value);
4310 else if constexpr (is_same_v<_Tp, const _CharT*>)
4311 return (__u._M_str = ... = __value);
4312 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4313 return (__u._M_sv = ... = __value);
4314 else if constexpr (is_same_v<_Tp, const void*>)
4315 return (__u._M_ptr = ... = __value);
4316#ifdef __SIZEOF_INT128__
4317 else if constexpr (is_same_v<_Tp, __int128>)
4318 return (__u._M_i128 = ... = __value);
4319 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4320 return (__u._M_u128 = ... = __value);
4322#ifdef __BFLT16_DIG__
4323 else if constexpr (is_same_v<_Tp, __bflt16_t>)
4324 return (__u._M_bf16 = ... = __value);
4327 else if constexpr (is_same_v<_Tp, _Float16>)
4328 return (__u._M_f16 = ... = __value);
4331 else if constexpr (is_same_v<_Tp, _Float32>)
4332 return (__u._M_f32 = ... = __value);
4335 else if constexpr (is_same_v<_Tp, _Float64>)
4336 return (__u._M_f64 = ... = __value);
4338 else if constexpr (is_same_v<_Tp, handle>)
4339 return __u._M_handle;
4341 __builtin_unreachable();
4344 template<
typename _Tp>
4345 [[__gnu__::__always_inline__]]
4346 _GLIBCXX_CONSTEXPR_FORMAT
auto&
4348 {
return _S_access<_Tp>(*
this); }
4350 template<
typename _Tp>
4351 [[__gnu__::__always_inline__]]
4352 _GLIBCXX_CONSTEXPR_FORMAT
const auto&
4353 _M_get() const noexcept
4354 {
return _S_access<_Tp>(*
this); }
4356 template<
typename _Tp>
4357 [[__gnu__::__always_inline__]]
4358 _GLIBCXX_CONSTEXPR_FORMAT
void
4359 _M_set(_Tp __v)
noexcept
4362 if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4363 std::construct_at(&_M_sv, __v);
4364 else if constexpr (is_same_v<_Tp, handle>)
4365 std::construct_at(&_M_handle, __v);
4369 _S_access<_Tp>(*
this, __v);
4374 template<
typename _Context,
typename... _Args>
4377 template<
typename _Visitor,
typename _Ctx>
4378 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4379 __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4381 template<
typename _Ch,
typename _Tp>
4383 __to_arg_t_enum() noexcept;
4387 template<typename _Context>
4388 class basic_format_arg
4390 using _CharT =
typename _Context::char_type;
4393 using handle = __format::_Arg_value<_Context>::handle;
4395 [[__gnu__::__always_inline__]]
4396 _GLIBCXX_CONSTEXPR_FORMAT
4397 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
4399 [[nodiscard,__gnu__::__always_inline__]]
4400 explicit _GLIBCXX_CONSTEXPR_FORMAT
operator bool() const noexcept
4401 {
return _M_type != __format::_Arg_none; }
4403#if __cpp_lib_format >= 202306L
4404 template<
typename _Visitor>
4405 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4406 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4409 template<
typename _Res,
typename _Visitor>
4410 _GLIBCXX_CONSTEXPR_FORMAT _Res
4411 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4416 template<
typename _Ctx>
4417 friend class basic_format_args;
4419 template<
typename _Ctx,
typename... _Args>
4420 friend class __format::_Arg_store;
4422 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
4424 __format::_Arg_value<_Context> _M_val;
4425 __format::_Arg_t _M_type;
4430 template<
typename _Tp>
4431 static consteval auto
4434 using _Td = remove_const_t<_Tp>;
4435 if constexpr (is_same_v<_Td, bool>)
4436 return type_identity<bool>();
4437 else if constexpr (is_same_v<_Td, _CharT>)
4438 return type_identity<_CharT>();
4439 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
4440 return type_identity<_CharT>();
4441#ifdef __SIZEOF_INT128__
4442 else if constexpr (is_same_v<_Td, __int128>)
4443 return type_identity<__int128>();
4444 else if constexpr (is_same_v<_Td, unsigned __int128>)
4445 return type_identity<unsigned __int128>();
4447 else if constexpr (__is_signed_integer<_Td>::value)
4449 if constexpr (
sizeof(_Td) <=
sizeof(int))
4450 return type_identity<int>();
4451 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
4452 return type_identity<long long>();
4454 else if constexpr (__is_unsigned_integer<_Td>::value)
4456 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
4457 return type_identity<unsigned>();
4458 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
4459 return type_identity<unsigned long long>();
4461 else if constexpr (is_same_v<_Td, float>)
4462 return type_identity<float>();
4463 else if constexpr (is_same_v<_Td, double>)
4464 return type_identity<double>();
4465#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4466 else if constexpr (is_same_v<_Td, long double>)
4467 return type_identity<long double>();
4469 else if constexpr (is_same_v<_Td, __ibm128>)
4470 return type_identity<__ibm128>();
4471 else if constexpr (is_same_v<_Td, __ieee128>)
4472 return type_identity<__ieee128>();
4474#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4475 else if constexpr (is_same_v<_Td, __float128>)
4476 return type_identity<__float128>();
4478#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4479 else if constexpr (is_same_v<_Td, __format::__bflt16_t>)
4480 return type_identity<__format::__bflt16_t>();
4482#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4483 else if constexpr (is_same_v<_Td, _Float16>)
4484 return type_identity<_Float16>();
4486#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4487 else if constexpr (is_same_v<_Td, _Float32>)
4488 return type_identity<_Float32>();
4490#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4491 else if constexpr (is_same_v<_Td, _Float64>)
4492 return type_identity<_Float64>();
4494 else if constexpr (__is_specialization_of<_Td, basic_string_view>
4495 || __is_specialization_of<_Td, basic_string>)
4497 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
4498 return type_identity<basic_string_view<_CharT>>();
4500 return type_identity<handle>();
4502 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
4503 return type_identity<const _CharT*>();
4504 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
4505 return type_identity<const _CharT*>();
4506 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
4507 return type_identity<const void*>();
4508 else if constexpr (is_same_v<_Td, nullptr_t>)
4509 return type_identity<const void*>();
4511 return type_identity<handle>();
4515 template<
typename _Tp>
4516 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
4519 template<
typename _Tp>
4520 static consteval __format::_Arg_t
4523 using namespace __format;
4524 if constexpr (is_same_v<_Tp, bool>)
4526 else if constexpr (is_same_v<_Tp, _CharT>)
4528 else if constexpr (is_same_v<_Tp, int>)
4530 else if constexpr (is_same_v<_Tp, unsigned>)
4532 else if constexpr (is_same_v<_Tp, long long>)
4534 else if constexpr (is_same_v<_Tp, unsigned long long>)
4536 else if constexpr (is_same_v<_Tp, float>)
4538 else if constexpr (is_same_v<_Tp, double>)
4540#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4541 else if constexpr (is_same_v<_Tp, long double>)
4545 else if constexpr (is_same_v<_Tp, __ibm128>)
4547 else if constexpr (is_same_v<_Tp, __ieee128>)
4548 return _Arg_ieee128;
4550#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4551 else if constexpr (is_same_v<_Tp, __float128>)
4552 return _Arg_float128;
4554#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4555 else if constexpr (is_same_v<_Tp, __format::__bflt16_t>)
4558#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4559 else if constexpr (is_same_v<_Tp, _Float16>)
4562#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4563 else if constexpr (is_same_v<_Tp, _Float32>)
4566#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4567 else if constexpr (is_same_v<_Tp, _Float64>)
4570 else if constexpr (is_same_v<_Tp, const _CharT*>)
4572 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4574 else if constexpr (is_same_v<_Tp, const void*>)
4576#ifdef __SIZEOF_INT128__
4577 else if constexpr (is_same_v<_Tp, __int128>)
4579 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4582 else if constexpr (is_same_v<_Tp, handle>)
4586 template<
typename _Tp>
4587 _GLIBCXX_CONSTEXPR_FORMAT
void
4588 _M_set(_Tp __v)
noexcept
4590 _M_type = _S_to_enum<_Tp>();
4594 template<
typename _Tp>
4595 requires __format::__formattable_with<_Tp, _Context>
4596 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4597 basic_format_arg(_Tp& __v)
noexcept
4599 using _Td = _Normalize<_Tp>;
4600 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4601 _M_set(_Td{__v.data(), __v.size()});
4602 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4603 && is_same_v<_CharT, wchar_t>)
4604 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4606 _M_set(
static_cast<_Td
>(__v));
4609 template<
typename _Ctx,
typename... _Argz>
4610 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4611 make_format_args(_Argz&...)
noexcept;
4613 template<
typename _Visitor,
typename _Ctx>
4614 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4615 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4617 template<
typename _Visitor,
typename _Ctx>
4618 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4619 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4621 template<
typename _Ch,
typename _Tp>
4622 friend consteval __format::_Arg_t
4623 __format::__to_arg_t_enum() noexcept;
4625 [[__gnu__::__noinline__]]
4627 _M_handle_unrecognized() const;
4629 template<typename _Visitor>
4630 _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4631 _M_visit(_Visitor&& __vis)
4635 using enum __format::_Arg_t;
4650#if __glibcxx_to_chars
4655#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4658#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4668#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4672#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4676#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4680#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4693#ifdef __SIZEOF_INT128__
4703 handle __h = _M_handle_unrecognized();
4708 template<
typename _Visitor>
4709 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4710 _M_visit_user(_Visitor&& __vis)
4712 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4714 constexpr bool __user_facing = __is_one_of<_Tp,
4715 monostate, bool, _CharT,
4716 int,
unsigned int,
long long int,
unsigned long long int,
4717 float, double,
long double,
4718 const _CharT*, basic_string_view<_CharT>,
4719 const void*, handle>::value;
4720 if constexpr (__user_facing)
4731 template<
typename _Visitor,
typename _Context>
4732 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4733 inline _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4734 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4740 template<
typename _Visitor,
typename _Ctx>
4741 inline _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4742 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4745 struct _WidthPrecVisitor
4747 template<
typename _Tp>
4748 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4749 operator()(_Tp& __arg)
const
4751 if constexpr (is_same_v<_Tp, monostate>)
4752 __format::__invalid_arg_id_in_format_string();
4756 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4760 if constexpr (__is_unsigned_integer<_Tp>::value)
4762 else if constexpr (__is_signed_integer<_Tp>::value)
4766 __throw_format_error(
"format error: argument used for width or "
4767 "precision must be a non-negative integer");
4771#pragma GCC diagnostic push
4772#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4773 template<
typename _Context>
4774 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
4775 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4776 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4779 template<
int _Bits,
size_t _Nm>
4781 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4783 __UINT64_TYPE__ __packed_types = 0;
4784 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4785 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4786 return __packed_types;
4791 template<
typename _Context>
4792 class basic_format_args
4794 static constexpr int _S_packed_type_bits = 5;
4795 static constexpr int _S_packed_type_mask = 0b11111;
4796 static constexpr int _S_max_packed_args = 12;
4798 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4800 template<
typename... _Args>
4801 using _Store = __format::_Arg_store<_Context, _Args...>;
4803 template<
typename _Ctx,
typename... _Args>
4804 friend class __format::_Arg_store;
4806 using uint64_t = __UINT64_TYPE__;
4807 using _Format_arg = basic_format_arg<_Context>;
4808 using _Format_arg_val = __format::_Arg_value<_Context>;
4814 uint64_t _M_packed_size : 4;
4815 uint64_t _M_unpacked_size : 60;
4818 const _Format_arg_val* _M_values;
4819 const _Format_arg* _M_args;
4822 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4823 _M_size() const noexcept
4824 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4826 _GLIBCXX_CONSTEXPR_FORMAT
typename __format::_Arg_t
4827 _M_type(
size_t __i)
const noexcept
4829 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4830 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4833 template<
typename _Ctx,
typename... _Args>
4834 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4835 make_format_args(_Args&...)
noexcept;
4838 template<
typename... _Args>
4839 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4841 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4844 template<
typename... _Args>
4845 _GLIBCXX_CONSTEXPR_FORMAT
4846 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4848 [[nodiscard,__gnu__::__always_inline__]]
4849 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<_Context>
4850 get(
size_t __i)
const noexcept
4852 basic_format_arg<_Context> __arg;
4853 if (__i < _M_packed_size)
4855 __arg._M_type = _M_type(__i);
4856 __arg._M_val = _M_values[__i];
4858 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4859 __arg = _M_args[__i];
4866 template<
typename _Context,
typename... _Args>
4867 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4868 -> basic_format_args<_Context>;
4870 template<
typename _Context,
typename... _Args>
4871 _GLIBCXX_CONSTEXPR_FORMAT
auto
4872 make_format_args(_Args&... __fmt_args)
noexcept;
4875 template<
typename _Context,
typename... _Args>
4876 class __format::_Arg_store
4878 friend std::basic_format_args<_Context>;
4880 template<
typename _Ctx,
typename... _Argz>
4881 friend _GLIBCXX_CONSTEXPR_FORMAT
auto std::
4882#if _GLIBCXX_INLINE_VERSION
4885 make_format_args(_Argz&...)
noexcept;
4889 static constexpr bool _S_values_only
4890 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4893 = __conditional_t<_S_values_only,
4894 __format::_Arg_value<_Context>,
4895 basic_format_arg<_Context>>;
4897 _Element_t _M_args[
sizeof...(_Args)];
4899 template<
typename _Tp>
4900 static _GLIBCXX_CONSTEXPR_FORMAT _Element_t
4901 _S_make_elt(_Tp& __v)
4903 using _Tq = remove_const_t<_Tp>;
4904 using _CharT =
typename _Context::char_type;
4905 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4906 "std::formatter must be specialized for the type "
4907 "of each format arg");
4908 using __format::__formattable_with;
4909 if constexpr (is_const_v<_Tp>)
4910 if constexpr (!__formattable_with<_Tp, _Context>)
4911 if constexpr (__formattable_with<_Tq, _Context>)
4912 static_assert(__formattable_with<_Tp, _Context>,
4913 "format arg must be non-const because its "
4914 "std::formatter specialization has a "
4915 "non-const reference parameter");
4916 basic_format_arg<_Context> __arg(__v);
4917 if constexpr (_S_values_only)
4918 return __arg._M_val;
4923 template<
typename... _Tp>
4924 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4925 [[__gnu__::__always_inline__]]
4926 _GLIBCXX_CONSTEXPR_FORMAT
4927 _Arg_store(_Tp&... __a) noexcept
4928 : _M_args{_S_make_elt(__a)...}
4932 template<
typename _Context>
4933 class __format::_Arg_store<_Context>
4936 template<
typename _Context>
4937 template<
typename... _Args>
4938 inline _GLIBCXX_CONSTEXPR_FORMAT
4939 basic_format_args<_Context>::
4940 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4942 if constexpr (
sizeof...(_Args) == 0)
4945 _M_unpacked_size = 0;
4948 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4951 _M_packed_size =
sizeof...(_Args);
4954 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4956 _M_values = __store._M_args;
4963 _M_unpacked_size =
sizeof...(_Args);
4965 _M_args = __store._M_args;
4970 template<
typename _Context = format_context,
typename... _Args>
4971 [[nodiscard,__gnu__::__always_inline__]]
4972 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4973 make_format_args(_Args&... __fmt_args)
noexcept
4975 using _Fmt_arg = basic_format_arg<_Context>;
4976 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4977 _Normalize<_Args>...>;
4978 return _Store(__fmt_args...);
4981#ifdef _GLIBCXX_USE_WCHAR_T
4983 template<
typename... _Args>
4984 [[nodiscard,__gnu__::__always_inline__]]
4985 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4986 make_wformat_args(_Args&... __args)
noexcept
4987 {
return std::make_format_args<wformat_context>(__args...); }
4993 template<
typename _Out,
typename _CharT,
typename _Context>
4994 _GLIBCXX_CONSTEXPR_FORMAT _Out
4995 __do_vformat_to(_Out, basic_string_view<_CharT>,
4996 const basic_format_args<_Context>&,
4997 const locale* =
nullptr);
4999 template<
typename _CharT>
struct __formatter_chrono;
5017 template<
typename _Out,
typename _CharT>
5018 class basic_format_context
5020 static_assert( output_iterator<_Out, const _CharT&> );
5022 basic_format_args<basic_format_context> _M_args;
5024 __format::_Optional_locale _M_loc;
5026 _GLIBCXX_CONSTEXPR_FORMAT
5027 basic_format_context(basic_format_args<basic_format_context> __args,
5029 : _M_args(__args), _M_out(std::move(__out))
5032 _GLIBCXX_CONSTEXPR_FORMAT
5033 basic_format_context(basic_format_args<basic_format_context> __args,
5034 _Out __out,
const std::locale& __loc)
5035 : _M_args(__args), _M_out(std::move(__out)), _M_loc(__loc)
5041 basic_format_context(
const basic_format_context&) =
delete;
5042 basic_format_context& operator=(
const basic_format_context&) =
delete;
5044 template<
typename _Out2,
typename _CharT2,
typename _Context2>
5045 friend _GLIBCXX_CONSTEXPR_FORMAT _Out2
5046 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
5047 const basic_format_args<_Context2>&,
5050 friend __format::__formatter_chrono<_CharT>;
5053 ~basic_format_context() =
default;
5055 using iterator = _Out;
5056 using char_type = _CharT;
5057 template<
typename _Tp>
5058 using formatter_type = formatter<_Tp, _CharT>;
5061 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<basic_format_context>
5062 arg(
size_t __id)
const noexcept
5063 {
return _M_args.get(__id); }
5066 std::locale locale() {
return _M_loc.value(); }
5069 _GLIBCXX_CONSTEXPR_FORMAT iterator
5072 _GLIBCXX_CONSTEXPR_FORMAT
void
5073 advance_to(iterator __it) { _M_out =
std::move(__it); }
5076#if _GLIBCXX_EXTERN_TEMPLATE
5079 extern template basic_format_arg<format_context>::handle
5080 basic_format_arg<format_context>::_M_handle_unrecognized()
const;
5081# ifdef _GLIBCXX_USE_WCHAR_T
5082 extern template basic_format_arg<wformat_context>::handle
5083 basic_format_arg<wformat_context>::_M_handle_unrecognized()
const;
5086 template<
typename _Context>
5087 typename basic_format_arg<_Context>::handle
5088 basic_format_arg<_Context>::_M_handle_unrecognized()
const
5093 __throw_format_error(
"format error: unrecognized argument type");
5105 template<
typename _CharT>
5108 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
5110 struct _Parse_context : basic_format_parse_context<_CharT>
5112 using basic_format_parse_context<_CharT>::basic_format_parse_context;
5113 const _Arg_t* _M_types =
nullptr;
5117 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5118 : _M_pc(__str, __nargs)
5121 constexpr iterator begin() const noexcept {
return _M_pc.begin(); }
5122 constexpr iterator end() const noexcept {
return _M_pc.end(); }
5127 basic_string_view<_CharT> __fmt = _M_fmt_str();
5129 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5131 _M_pc.advance_to(begin() + 1);
5132 _M_format_arg(_M_pc.next_arg_id());
5136 size_t __lbr = __fmt.find(
'{');
5137 size_t __rbr = __fmt.find(
'}');
5139 while (__fmt.size())
5141 auto __cmp = __lbr <=> __rbr;
5145 _M_pc.advance_to(end());
5150 if (__lbr + 1 == __fmt.size()
5151 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5152 __format::__unmatched_left_brace_in_format_string();
5153 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5154 iterator __last = begin() + __lbr + int(__is_escape);
5155 _M_on_chars(__last);
5156 _M_pc.advance_to(__last + 1);
5157 __fmt = _M_fmt_str();
5160 if (__rbr != __fmt.npos)
5162 __lbr = __fmt.find(
'{');
5166 _M_on_replacement_field();
5167 __fmt = _M_fmt_str();
5168 __lbr = __fmt.find(
'{');
5169 __rbr = __fmt.find(
'}');
5174 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5175 __format::__unmatched_right_brace_in_format_string();
5176 iterator __last = begin() + __rbr;
5177 _M_on_chars(__last);
5178 _M_pc.advance_to(__last + 1);
5179 __fmt = _M_fmt_str();
5180 if (__lbr != __fmt.npos)
5182 __rbr = __fmt.find(
'}');
5187 constexpr basic_string_view<_CharT>
5188 _M_fmt_str() const noexcept
5189 {
return {begin(), end()}; }
5191 constexpr virtual void _M_on_chars(iterator) { }
5193 constexpr void _M_on_replacement_field()
5195 auto __next = begin();
5199 __id = _M_pc.next_arg_id();
5200 else if (*__next ==
':')
5202 __id = _M_pc.next_arg_id();
5203 _M_pc.advance_to(++__next);
5207 auto [__i, __ptr] = __format::__parse_arg_id(begin(), end());
5208 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5209 __format::__invalid_arg_id_in_format_string();
5210 _M_pc.check_arg_id(__id = __i);
5213 _M_pc.advance_to(++__ptr);
5216 _M_pc.advance_to(__ptr);
5218 _M_format_arg(__id);
5219 if (begin() == end() || *begin() !=
'}')
5220 __format::__unmatched_left_brace_in_format_string();
5221 _M_pc.advance_to(begin() + 1);
5224 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5228 template<
typename _Out,
typename _CharT>
5229 class _Formatting_scanner :
public _Scanner<_CharT>
5232 _GLIBCXX_CONSTEXPR_FORMAT
5233 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5234 basic_string_view<_CharT> __str)
5235 : _Scanner<_CharT>(__str), _M_fc(__fc)
5239 basic_format_context<_Out, _CharT>& _M_fc;
5241 using iterator =
typename _Scanner<_CharT>::iterator;
5244 _M_on_chars(iterator __last)
override
5246 basic_string_view<_CharT> __str(this->begin(), __last);
5247 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5251 _M_format_arg(
size_t __id)
override
5253 using _Context = basic_format_context<_Out, _CharT>;
5254 using handle =
typename basic_format_arg<_Context>::handle;
5256 __format::__visit_format_arg([
this](
auto& __arg) {
5258 using _Formatter =
typename _Context::template formatter_type<_Type>;
5259 if constexpr (is_same_v<_Type, monostate>)
5260 __format::__invalid_arg_id_in_format_string();
5261 else if constexpr (is_same_v<_Type, handle>)
5262 __arg.format(this->_M_pc, this->_M_fc);
5263 else if constexpr (is_default_constructible_v<_Formatter>)
5266 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5267 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5270 static_assert(__format::__formattable_with<_Type, _Context>);
5271 }, _M_fc.arg(__id));
5275 template<
typename _CharT,
typename _Tp>
5277 __to_arg_t_enum() noexcept
5279 using _Context = __format::__format_context<_CharT>;
5280 using _Fmt_arg = basic_format_arg<_Context>;
5281 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5282 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5286 template<
typename _CharT,
typename... _Args>
5287 class _Checking_scanner :
public _Scanner<_CharT>
5290 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5291 "std::formatter must be specialized for each type being formatted");
5295 _Checking_scanner(basic_string_view<_CharT> __str)
5296 : _Scanner<_CharT>(__str, sizeof...(_Args))
5298#if __cpp_lib_format >= 202305L
5299 this->_M_pc._M_types = _M_types.data();
5305 _M_format_arg(
size_t __id)
override
5307 if constexpr (
sizeof...(_Args) != 0)
5309 if (__id <
sizeof...(_Args))
5311 _M_parse_format_spec<_Args...>(__id);
5315 __builtin_unreachable();
5318 template<
typename _Tp,
typename... _OtherArgs>
5320 _M_parse_format_spec(
size_t __id)
5324 formatter<_Tp, _CharT> __f;
5325 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5327 else if constexpr (
sizeof...(_OtherArgs) != 0)
5328 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5330 __builtin_unreachable();
5333#if __cpp_lib_format >= 202305L
5334 array<_Arg_t,
sizeof...(_Args)>
5335 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5339 template<typename _CharT, unsigned = __unicode::__literal_encoding_is_unicode<_CharT>()>
5340 _GLIBCXX_CONSTEXPR_FORMAT _Sink_iter<_CharT>
5341 __do_vformat_to(_Sink_iter<_CharT> __out, basic_string_view<_CharT> __fmt,
5342 __format_context<_CharT>& __ctx)
5344 if constexpr (is_same_v<_CharT, char>)
5346 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5348 bool __done =
false;
5349 __format::__visit_format_arg([&](
auto& __arg) {
5350 using _Tp = remove_cvref_t<
decltype(__arg)>;
5351 if constexpr (is_same_v<_Tp, bool>)
5353 size_t __len = 4 + !__arg;
5354 const char* __chars[] = {
"false",
"true" };
5355 if (
auto __res = __out._M_reserve(__len))
5357 ranges::copy_n(__chars[__arg], __len, __res.get());
5358 __res._M_bump(__len);
5362 else if constexpr (is_same_v<_Tp, char>)
5364 if (
auto __res = __out._M_reserve(1))
5366 *__res.get() = __arg;
5371 else if constexpr (is_integral_v<_Tp>)
5373 make_unsigned_t<_Tp> __uval;
5374 const bool __neg = __arg < 0;
5376 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5379 const auto __n = __detail::__to_chars_len(__uval);
5380 if (
auto __res = __out._M_reserve(__n + __neg))
5382 auto __ptr = __res.get();
5384 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5386 __res._M_bump(__n + __neg);
5390 else if constexpr (is_convertible_v<_Tp, string_view>)
5392 string_view __sv = __arg;
5393 if (
auto __res = __out._M_reserve(__sv.size()))
5395 ranges::copy(__sv, __res.get());
5396 __res._M_bump(__sv.size());
5406 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5407 __scanner._M_scan();
5416#if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE
5417 extern template _Sink_iter<char>
5418 __do_vformat_to<char, 1>(_Sink_iter<char>, string_view,
5420# ifdef _GLIBCXX_USE_WCHAR_T
5421 extern template _Sink_iter<wchar_t>
5422 __do_vformat_to<wchar_t, 1>(_Sink_iter<wchar_t>, wstring_view,
5427 template<
typename _Out,
typename _CharT,
typename _Context>
5428 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5429 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5430 const basic_format_args<_Context>& __args,
5431 const locale* __loc)
5433 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5435 auto __ctx = __loc ==
nullptr
5436 ? _Context(__args, __out)
5437 : _Context(__args, __out, *__loc);
5438 return __format::__do_vformat_to(__out, __fmt, __ctx);
5440 else if constexpr (__contiguous_char_iter<_CharT, _Out>)
5442 _Ptr_sink<_CharT> __sink(__out);
5443 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5444 return std::move(__sink)._M_finish(__out).out;
5448 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5449 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5450 return std::move(__sink)._M_finish().out;
5454 template<
typename _Out,
typename _CharT>
5455 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5456 __do_vformat_to_n(_Out __out, iter_difference_t<_Out> __n,
5457 basic_string_view<_CharT> __fmt,
5458 const type_identity_t<
5459 basic_format_args<__format_context<_CharT>>>& __args,
5460 const locale* __loc =
nullptr)
5462 if constexpr (__contiguous_char_iter<_CharT, _Out>)
5464 _Ptr_sink<_CharT> __sink(__out, __n);
5465 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5466 return std::move(__sink)._M_finish(__out);
5470 _Iter_sink<_CharT, _Out> __sink(
std::move(__out), __n);
5471 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5476#pragma GCC diagnostic pop
5481#if __cpp_lib_format >= 202305L
5484 template<
typename _CharT>
5485 template<
typename... _Ts>
5487 basic_format_parse_context<_CharT>::
5488 __check_dynamic_spec(
size_t __id)
noexcept
5490 if (__id >= _M_num_args)
5491 __format::__invalid_arg_id_in_format_string();
5492 if constexpr (
sizeof...(_Ts) != 0)
5494 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
5495 auto* __args =
static_cast<_Parse_ctx*
>(
this)->_M_types;
5500 auto __arg = __args[__id];
5501 __format::_Arg_t __types[] = {
5502 __format::__to_arg_t_enum<_CharT, _Ts>()...
5504 for (
auto __t : __types)
5508 __invalid_dynamic_spec(
"arg(id) type does not match");
5513 template<
typename _CharT,
typename... _Args>
5514 template<
typename _Tp>
5515 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5517 basic_format_string<_CharT, _Args...>::
5518 basic_format_string(
const _Tp& __s) noexcept
5521 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5523 __scanner._M_scan();
5528 template<
typename _Out>
requires output_iterator<_Out, const char&>
5529 [[__gnu__::__always_inline__]]
5530 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5531 vformat_to(_Out __out, string_view __fmt, format_args __args)
5532 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5534#ifdef _GLIBCXX_USE_WCHAR_T
5535 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5536 [[__gnu__::__always_inline__]]
5537 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5538 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5539 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5542 template<
typename _Out>
requires output_iterator<_Out, const char&>
5543 [[__gnu__::__always_inline__]]
5545 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5548 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5551#ifdef _GLIBCXX_USE_WCHAR_T
5552 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5553 [[__gnu__::__always_inline__]]
5555 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5556 wformat_args __args)
5558 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5563 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5564 vformat(string_view __fmt, format_args __args)
5566 __format::_Str_sink<char> __buf;
5567 std::vformat_to(__buf.out(), __fmt, __args);
5571#ifdef _GLIBCXX_USE_WCHAR_T
5573 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5574 vformat(wstring_view __fmt, wformat_args __args)
5576 __format::_Str_sink<wchar_t> __buf;
5577 std::vformat_to(__buf.out(), __fmt, __args);
5584 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5586 __format::_Str_sink<char> __buf;
5587 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5591#ifdef _GLIBCXX_USE_WCHAR_T
5594 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5596 __format::_Str_sink<wchar_t> __buf;
5597 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5602 template<
typename... _Args>
5604 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5605 format(format_string<_Args...> __fmt, _Args&&... __args)
5606 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5608#ifdef _GLIBCXX_USE_WCHAR_T
5609 template<
typename... _Args>
5611 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5612 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5613 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5616 template<
typename... _Args>
5619 format(
const locale& __loc, format_string<_Args...> __fmt,
5622 return std::vformat(__loc, __fmt.get(),
5623 std::make_format_args(__args...));
5626#ifdef _GLIBCXX_USE_WCHAR_T
5627 template<
typename... _Args>
5630 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5633 return std::vformat(__loc, __fmt.get(),
5634 std::make_wformat_args(__args...));
5638 template<
typename _Out,
typename... _Args>
5639 requires output_iterator<_Out, const char&>
5640 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5641 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5643 return std::vformat_to(
std::move(__out), __fmt.get(),
5644 std::make_format_args(__args...));
5647#ifdef _GLIBCXX_USE_WCHAR_T
5648 template<
typename _Out,
typename... _Args>
5649 requires output_iterator<_Out, const wchar_t&>
5650 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5651 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5653 return std::vformat_to(
std::move(__out), __fmt.get(),
5654 std::make_wformat_args(__args...));
5658 template<
typename _Out,
typename... _Args>
5659 requires output_iterator<_Out, const char&>
5661 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5664 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5665 std::make_format_args(__args...));
5668#ifdef _GLIBCXX_USE_WCHAR_T
5669 template<
typename _Out,
typename... _Args>
5670 requires output_iterator<_Out, const wchar_t&>
5672 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5675 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5676 std::make_wformat_args(__args...));
5680 template<
typename _Out,
typename... _Args>
5681 requires output_iterator<_Out, const char&>
5682 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5683 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5684 format_string<_Args...> __fmt, _Args&&... __args)
5686 return __format::__do_vformat_to_n(
5688 std::make_format_args(__args...));
5691#ifdef _GLIBCXX_USE_WCHAR_T
5692 template<
typename _Out,
typename... _Args>
5693 requires output_iterator<_Out, const wchar_t&>
5694 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5695 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5696 wformat_string<_Args...> __fmt, _Args&&... __args)
5698 return __format::__do_vformat_to_n(
5700 std::make_wformat_args(__args...));
5704 template<
typename _Out,
typename... _Args>
5705 requires output_iterator<_Out, const char&>
5706 inline format_to_n_result<_Out>
5707 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5708 format_string<_Args...> __fmt, _Args&&... __args)
5710 return __format::__do_vformat_to_n(
5712 std::make_format_args(__args...), &__loc);
5715#ifdef _GLIBCXX_USE_WCHAR_T
5716 template<
typename _Out,
typename... _Args>
5717 requires output_iterator<_Out, const wchar_t&>
5718 inline format_to_n_result<_Out>
5719 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5720 wformat_string<_Args...> __fmt, _Args&&... __args)
5722 return __format::__do_vformat_to_n(
5724 std::make_wformat_args(__args...), &__loc);
5732 template<
typename _CharT>
5733 class _Counting_sink final :
public _Ptr_sink<_CharT>
5736 _GLIBCXX_CONSTEXPR_FORMAT
5737 _Counting_sink() : _Ptr_sink<_CharT>(nullptr, 0) { }
5739 [[__gnu__::__always_inline__]]
5740 _GLIBCXX_CONSTEXPR_FORMAT
size_t
5742 {
return this->_M_count + this->_M_used().size(); }
5745 template<
typename _CharT>
5746 class _Counting_sink :
public _Buf_sink<_CharT>
5748 size_t _M_count = 0;
5751 _M_overflow()
override
5753 if (!std::is_constant_evaluated())
5754 _M_count += this->_M_used().size();
5759 _Counting_sink() =
default;
5761 [[__gnu__::__always_inline__]]
5765 _Counting_sink::_M_overflow();
5773 template<
typename... _Args>
5775 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5776 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5778 __format::_Counting_sink<char> __buf;
5779 std::vformat_to(__buf.out(), __fmt.get(),
5780 std::make_format_args(__args...));
5781 return __buf.count();
5784#ifdef _GLIBCXX_USE_WCHAR_T
5785 template<
typename... _Args>
5787 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5788 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5790 __format::_Counting_sink<wchar_t> __buf;
5791 std::vformat_to(__buf.out(), __fmt.get(),
5792 std::make_wformat_args(__args...));
5793 return __buf.count();
5797 template<
typename... _Args>
5800 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5803 __format::_Counting_sink<char> __buf;
5804 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5805 std::make_format_args(__args...));
5806 return __buf.count();
5809#ifdef _GLIBCXX_USE_WCHAR_T
5810 template<
typename... _Args>
5813 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5816 __format::_Counting_sink<wchar_t> __buf;
5817 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5818 std::make_wformat_args(__args...));
5819 return __buf.count();
5823#if __glibcxx_format_ranges
5825 template<typename _Tp>
5826 consteval range_format
5829 using _Ref = ranges::range_reference_t<_Tp>;
5830 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5831 return range_format::disabled;
5832 else if constexpr (
requires {
typename _Tp::key_type; })
5834 if constexpr (
requires {
typename _Tp::mapped_type; })
5836 using _Up = remove_cvref_t<_Ref>;
5837 if constexpr (__is_pair<_Up>)
5838 return range_format::map;
5839 else if constexpr (__is_specialization_of<_Up, tuple>)
5840 if constexpr (tuple_size_v<_Up> == 2)
5841 return range_format::map;
5843 return range_format::set;
5846 return range_format::sequence;
5851 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5852 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5857 template<
typename _CharT,
typename _Out,
typename _Callback>
5858 _GLIBCXX_CONSTEXPR_FORMAT
5859 typename basic_format_context<_Out, _CharT>::iterator
5860 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5861 const _Spec<_CharT>& __spec,
5864 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
5870 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
5872 const size_t __padwidth = __spec._M_get_width(__fc);
5873 if (__padwidth == 0)
5874 return __call(__fc);
5878 _GLIBCXX_CONSTEXPR_FORMAT
5879 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5880 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5883 _GLIBCXX_CONSTEXPR_FORMAT
void
5885 { _M_ctx =
nullptr; }
5887 _GLIBCXX_CONSTEXPR_FORMAT
5891 _M_ctx->advance_to(_M_out);
5895 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5896 _Sink_iter<_CharT> _M_out;
5899 _Restore_out __restore(__fc);
5900 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
5901 __fc.advance_to(__sink.out());
5903 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
5904 __restore._M_disarm();
5909 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5910 struct __indexed_formatter_storage
5917 basic_format_parse_context<_CharT> __pc({});
5918 if (_M_formatter.parse(__pc) != __pc.end())
5919 __format::__failed_to_parse_format_spec();
5922 template<
typename _Out>
5923 _GLIBCXX_CONSTEXPR_FORMAT
void
5924 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5925 basic_format_context<_Out, _CharT>& __fc,
5926 basic_string_view<_CharT> __sep)
const
5928 if constexpr (_Pos != 0)
5929 __fc.advance_to(__format::__write(__fc.out(), __sep));
5930 __fc.advance_to(_M_formatter.format(__elem, __fc));
5933 [[__gnu__::__always_inline__]]
5937 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5938 _M_formatter.set_debug_format();
5942 formatter<_Tp, _CharT> _M_formatter;
5945 template<
typename _CharT,
typename... _Tps>
5946 class __tuple_formatter
5948 using _String_view = basic_string_view<_CharT>;
5949 using _Seps = __format::_Separators<_CharT>;
5953 set_separator(basic_string_view<_CharT> __sep)
noexcept
5957 set_brackets(basic_string_view<_CharT> __open,
5958 basic_string_view<_CharT> __close)
noexcept
5966 constexpr typename basic_format_parse_context<_CharT>::iterator
5967 parse(basic_format_parse_context<_CharT>& __pc)
5969 auto __first = __pc.begin();
5970 const auto __last = __pc.end();
5971 __format::_Spec<_CharT> __spec{};
5973 auto __finished = [&]
5975 if (__first != __last && *__first !=
'}')
5979 _M_felems._M_parse();
5980 _M_felems.set_debug_format();
5987 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5991 __first = __spec._M_parse_width(__first, __last, __pc);
5995 if (*__first ==
'n')
5998 _M_open = _M_close = _String_view();
6000 else if (*__first ==
'm')
6003 if constexpr (
sizeof...(_Tps) == 2)
6005 _M_sep = _Seps::_S_colon();
6006 _M_open = _M_close = _String_view();
6009 __throw_format_error(
"format error: 'm' specifier requires range"
6010 " of pair or tuple of two elements");
6016 __format::__failed_to_parse_format_spec();
6020 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
6021 _GLIBCXX_CONSTEXPR_FORMAT
6022 typename basic_format_context<_Out, _CharT>::iterator
6023 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
6024 basic_format_context<_Out, _CharT>& __fc)
const
6025 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
6027 template<
typename _Out>
6028 _GLIBCXX_CONSTEXPR_FORMAT
6029 typename basic_format_context<_Out, _CharT>::iterator
6030 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
6031 basic_format_context<_Out, _CharT>& __fc)
const
6033 return __format::__format_padded(
6035 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
6037 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
6038 _M_felems._M_format(__elems..., __nfc, _M_sep);
6039 return __format::__write(__nfc.out(), _M_close);
6044 template<
size_t... _Ids>
6045 struct __formatters_storage
6046 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
6048 template<
size_t _Id,
typename _Up>
6049 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
6054 (_Base<_Ids, _Tps>::_M_parse(), ...);
6057 template<
typename _Out>
6058 _GLIBCXX_CONSTEXPR_FORMAT
void
6059 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
6060 basic_format_context<_Out, _CharT>& __fc,
6061 _String_view __sep)
const
6063 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
6069 (_Base<_Ids, _Tps>::set_debug_format(), ...);
6073 template<
size_t... _Ids>
6074 static _GLIBCXX_CONSTEXPR_FORMAT
auto
6075 _S_create_storage(index_sequence<_Ids...>)
6076 -> __formatters_storage<_Ids...>;
6078 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
6080 _Spec<_CharT> _M_spec{};
6081 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
6082 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
6083 _String_view _M_sep = _Seps::_S_comma();
6084 _Formatters _M_felems;
6087 template<
typename _Tp>
6088 concept __is_map_formattable
6089 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
6095 template<__format::__
char _CharT, formattable<_CharT> _Fp,
6096 formattable<_CharT> _Sp>
6097 struct formatter<
pair<_Fp, _Sp>, _CharT>
6098 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
6099 remove_cvref_t<_Sp>>
6102 using __maybe_const_pair
6103 = __conditional_t<formattable<const _Fp, _CharT>
6104 && formattable<const _Sp, _CharT>,
6105 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
6109 template<
typename _Out>
6110 _GLIBCXX_CONSTEXPR_FORMAT
6111 typename basic_format_context<_Out, _CharT>::iterator
6112 format(__maybe_const_pair& __p,
6113 basic_format_context<_Out, _CharT>& __fc)
const
6114 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
6117#if __glibcxx_print >= 202406L
6120 template<
typename _Fp,
typename _Sp>
6121 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
6122 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
6123 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
6126 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
6127 struct formatter<tuple<_Tps...>, _CharT>
6128 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
6131 using __maybe_const_tuple
6132 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
6133 const tuple<_Tps...>, tuple<_Tps...>>;
6137 template<
typename _Out>
6138 _GLIBCXX_CONSTEXPR_FORMAT
6139 typename basic_format_context<_Out, _CharT>::iterator
6140 format(__maybe_const_tuple& __t,
6141 basic_format_context<_Out, _CharT>& __fc)
const
6142 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
6145#if __glibcxx_print >= 202406L
6148 template<
typename... _Tps>
6149 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
6150 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
6154 template<
typename _Tp, __format::__
char _CharT>
6155 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
6156 class range_formatter
6158 using _String_view = basic_string_view<_CharT>;
6159 using _Seps = __format::_Separators<_CharT>;
6163 set_separator(basic_string_view<_CharT> __sep)
noexcept
6167 set_brackets(basic_string_view<_CharT> __open,
6168 basic_string_view<_CharT> __close)
noexcept
6174 constexpr formatter<_Tp, _CharT>&
6175 underlying() noexcept
6178 constexpr const formatter<_Tp, _CharT>&
6179 underlying() const noexcept
6184 constexpr typename basic_format_parse_context<_CharT>::iterator
6185 parse(basic_format_parse_context<_CharT>& __pc)
6187 auto __first = __pc.begin();
6188 const auto __last = __pc.end();
6189 __format::_Spec<_CharT> __spec{};
6190 bool __no_brace =
false;
6192 auto __finished = [&]
6193 {
return __first == __last || *__first ==
'}'; };
6195 auto __finalize = [&]
6201 auto __parse_val = [&](_String_view __nfs = _String_view())
6205 basic_format_parse_context<_CharT> __npc(__nfs);
6206 if (_M_fval.parse(__npc) != __npc.end())
6207 __format::__failed_to_parse_format_spec();
6208 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6209 _M_fval.set_debug_format();
6210 return __finalize();
6214 return __parse_val();
6216 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6218 return __parse_val();
6220 __first = __spec._M_parse_width(__first, __last, __pc);
6222 return __parse_val();
6224 if (*__first ==
'?')
6227 __spec._M_debug =
true;
6228 if (__finished() || *__first !=
's')
6229 __throw_format_error(
"format error: '?' is allowed only in"
6230 " combination with 's'");
6233 if (*__first ==
's')
6236 if constexpr (same_as<_Tp, _CharT>)
6238 __spec._M_type = __format::_Pres_s;
6240 return __finalize();
6241 __throw_format_error(
"format error: element format specifier"
6242 " cannot be provided when 's' specifier is used");
6245 __throw_format_error(
"format error: 's' specifier requires"
6246 " range of character types");
6250 return __parse_val();
6252 if (*__first ==
'n')
6255 _M_open = _M_close = _String_view();
6260 return __parse_val();
6262 if (*__first ==
'm')
6264 _String_view __m(__first, 1);
6266 if constexpr (__format::__is_map_formattable<_Tp>)
6268 _M_sep = _Seps::_S_comma();
6271 _M_open = _Seps::_S_braces().substr(0, 1);
6272 _M_close = _Seps::_S_braces().substr(1, 1);
6275 return __parse_val(__m);
6276 __throw_format_error(
"format error: element format specifier"
6277 " cannot be provided when 'm' specifier is used");
6280 __throw_format_error(
"format error: 'm' specifier requires"
6281 " range of pairs or tuples of two elements");
6285 return __parse_val();
6287 if (*__first ==
':')
6289 __pc.advance_to(++__first);
6290 __first = _M_fval.parse(__pc);
6294 return __finalize();
6296 __format::__failed_to_parse_format_spec();
6301 template<ranges::input_range _Rg,
typename _Out>
6302 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6303 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6304 _GLIBCXX_CONSTEXPR_FORMAT
6305 typename basic_format_context<_Out, _CharT>::iterator
6306 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6308 using _Range = remove_reference_t<_Rg>;
6309 if constexpr (ranges::contiguous_range<_Rg>)
6311 const span<__format::__maybe_const<_Tp, _CharT>>
6312 __spn(ranges::data(__rg),
size_t(ranges::distance(__rg)));
6313 return _M_format(__spn, __fc);
6315 else if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6316 return _M_format<const _Range>(__rg, __fc);
6318 return _M_format(__rg, __fc);
6322 template<ranges::input_range _Rg,
typename _Out>
6323 _GLIBCXX_CONSTEXPR_FORMAT
6324 typename basic_format_context<_Out, _CharT>::iterator
6325 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6327 if constexpr (same_as<_Tp, _CharT>)
6328 if (_M_spec._M_type == __format::_Pres_s)
6330 __format::__formatter_str __fstr(_M_spec);
6331 return __fstr._M_format_range(__rg, __fc);
6333 return __format::__format_padded(
6335 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6336 {
return _M_format_elems(__rg, __nfc); });
6340 template<ranges::input_range _Rg,
typename _Out>
6341 _GLIBCXX_CONSTEXPR_FORMAT
6342 typename basic_format_context<_Out, _CharT>::iterator
6343 _M_format_elems(_Rg& __rg,
6344 basic_format_context<_Out, _CharT>& __fc)
const
6346 auto __out = __format::__write(__fc.out(), _M_open);
6348 auto __first = ranges::begin(__rg);
6349 auto const __last = ranges::end(__rg);
6350 if (__first == __last)
6351 return __format::__write(__out, _M_close);
6353 __fc.advance_to(__out);
6354 __out = _M_fval.format(*__first, __fc);
6355 for (++__first; __first != __last; ++__first)
6357 __out = __format::__write(__out, _M_sep);
6358 __fc.advance_to(__out);
6359 __out = _M_fval.format(*__first, __fc);
6362 return __format::__write(__out, _M_close);
6365 __format::_Spec<_CharT> _M_spec{};
6366 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6367 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6368 _String_view _M_sep = _Seps::_S_comma();
6369 formatter<_Tp, _CharT> _M_fval;
6377 template<ranges::input_range _Rg, __format::__
char _CharT>
6378 requires (format_kind<_Rg> != range_format::disabled)
6379 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6380 struct formatter<_Rg, _CharT>
6383 static const bool _S_range_format_is_string =
6384 (format_kind<_Rg> == range_format::string)
6385 || (format_kind<_Rg> == range_format::debug_string);
6386 using _Vt = remove_cvref_t<
6387 ranges::range_reference_t<
6388 __format::__maybe_const_range<_Rg, _CharT>>>;
6390 static consteval bool _S_is_correct()
6392 if constexpr (_S_range_format_is_string)
6393 static_assert(same_as<_Vt, _CharT>);
6397 static_assert(_S_is_correct());
6400 constexpr formatter() noexcept
6402 using _Seps = __format::_Separators<_CharT>;
6403 if constexpr (format_kind<_Rg> == range_format::map)
6405 static_assert(__format::__is_map_formattable<_Vt>);
6406 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6407 _Seps::_S_braces().substr(1, 1));
6408 _M_under.underlying().set_brackets({}, {});
6409 _M_under.underlying().set_separator(_Seps::_S_colon());
6411 else if constexpr (format_kind<_Rg> == range_format::set)
6412 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6413 _Seps::_S_braces().substr(1, 1));
6417 set_separator(basic_string_view<_CharT> __sep)
noexcept
6418 requires (format_kind<_Rg> == range_format::sequence)
6419 { _M_under.set_separator(__sep); }
6422 set_brackets(basic_string_view<_CharT> __open,
6423 basic_string_view<_CharT> __close)
noexcept
6424 requires (format_kind<_Rg> == range_format::sequence)
6425 { _M_under.set_brackets(__open, __close); }
6429 constexpr typename basic_format_parse_context<_CharT>::iterator
6430 parse(basic_format_parse_context<_CharT>& __pc)
6432 auto __res = _M_under.parse(__pc);
6433 if constexpr (format_kind<_Rg> == range_format::debug_string)
6434 _M_under.set_debug_format();
6440 template<
typename _Out>
6441 _GLIBCXX_CONSTEXPR_FORMAT
6442 typename basic_format_context<_Out, _CharT>::iterator
6443 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6444 basic_format_context<_Out, _CharT>& __fc)
const
6446 if constexpr (_S_range_format_is_string)
6447 return _M_under._M_format_range(__rg, __fc);
6449 return _M_under.format(__rg, __fc);
6453 using _Formatter_under
6454 = __conditional_t<_S_range_format_is_string,
6455 __format::__formatter_str<_CharT>,
6456 range_formatter<_Vt, _CharT>>;
6457 _Formatter_under _M_under;
6460#if __glibcxx_print >= 202406L
6461 template<ranges::input_range _Rg>
6462 requires (format_kind<_Rg> != range_format::disabled)
6463 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6467#undef _GLIBCXX_WIDEN
6468#undef _GLIBCXX_CONSTEXPR_FORMAT
6470_GLIBCXX_END_NAMESPACE_VERSION
6473#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.