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 template<typename _Visitor>
4626 _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4627 _M_visit(_Visitor&& __vis, __format::_Arg_t __type)
4629 using namespace __format;
4646#if __glibcxx_to_chars
4651#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4654#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4664#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4668#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4672#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4676#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4689#ifdef __SIZEOF_INT128__
4696 __builtin_unreachable();
4700 template<
typename _Visitor>
4701 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4702 _M_visit_user(_Visitor&& __vis, __format::_Arg_t __type)
4704 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4706 constexpr bool __user_facing = __is_one_of<_Tp,
4707 monostate, bool, _CharT,
4708 int,
unsigned int,
long long int,
unsigned long long int,
4709 float, double,
long double,
4710 const _CharT*, basic_string_view<_CharT>,
4711 const void*, handle>::value;
4712 if constexpr (__user_facing)
4723 template<
typename _Visitor,
typename _Context>
4724 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4725 inline _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4726 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4734 template<
typename _Visitor,
typename _Ctx>
4735 inline _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4736 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4741 struct _WidthPrecVisitor
4743 template<
typename _Tp>
4744 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4745 operator()(_Tp& __arg)
const
4747 if constexpr (is_same_v<_Tp, monostate>)
4748 __format::__invalid_arg_id_in_format_string();
4752 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4756 if constexpr (__is_unsigned_integer<_Tp>::value)
4758 else if constexpr (__is_signed_integer<_Tp>::value)
4762 __throw_format_error(
"format error: argument used for width or "
4763 "precision must be a non-negative integer");
4767#pragma GCC diagnostic push
4768#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4769 template<
typename _Context>
4770 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
4771 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4772 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4775 template<
int _Bits,
size_t _Nm>
4777 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4779 __UINT64_TYPE__ __packed_types = 0;
4780 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4781 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4782 return __packed_types;
4787 template<
typename _Context>
4788 class basic_format_args
4790 static constexpr int _S_packed_type_bits = 5;
4791 static constexpr int _S_packed_type_mask = 0b11111;
4792 static constexpr int _S_max_packed_args = 12;
4794 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4796 template<
typename... _Args>
4797 using _Store = __format::_Arg_store<_Context, _Args...>;
4799 template<
typename _Ctx,
typename... _Args>
4800 friend class __format::_Arg_store;
4802 using uint64_t = __UINT64_TYPE__;
4803 using _Format_arg = basic_format_arg<_Context>;
4804 using _Format_arg_val = __format::_Arg_value<_Context>;
4810 uint64_t _M_packed_size : 4;
4811 uint64_t _M_unpacked_size : 60;
4814 const _Format_arg_val* _M_values;
4815 const _Format_arg* _M_args;
4818 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4819 _M_size() const noexcept
4820 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4822 _GLIBCXX_CONSTEXPR_FORMAT
typename __format::_Arg_t
4823 _M_type(
size_t __i)
const noexcept
4825 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4826 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4829 template<
typename _Ctx,
typename... _Args>
4830 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4831 make_format_args(_Args&...)
noexcept;
4834 template<
typename... _Args>
4835 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4837 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4840 template<
typename... _Args>
4841 _GLIBCXX_CONSTEXPR_FORMAT
4842 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4844 [[nodiscard,__gnu__::__always_inline__]]
4845 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<_Context>
4846 get(
size_t __i)
const noexcept
4848 basic_format_arg<_Context> __arg;
4849 if (__i < _M_packed_size)
4851 __arg._M_type = _M_type(__i);
4852 __arg._M_val = _M_values[__i];
4854 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4855 __arg = _M_args[__i];
4862 template<
typename _Context,
typename... _Args>
4863 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4864 -> basic_format_args<_Context>;
4866 template<
typename _Context,
typename... _Args>
4867 _GLIBCXX_CONSTEXPR_FORMAT
auto
4868 make_format_args(_Args&... __fmt_args)
noexcept;
4871 template<
typename _Context,
typename... _Args>
4872 class __format::_Arg_store
4874 friend std::basic_format_args<_Context>;
4876 template<
typename _Ctx,
typename... _Argz>
4877 friend _GLIBCXX_CONSTEXPR_FORMAT
auto std::
4878#if _GLIBCXX_INLINE_VERSION
4881 make_format_args(_Argz&...)
noexcept;
4885 static constexpr bool _S_values_only
4886 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4889 = __conditional_t<_S_values_only,
4890 __format::_Arg_value<_Context>,
4891 basic_format_arg<_Context>>;
4893 _Element_t _M_args[
sizeof...(_Args)];
4895 template<
typename _Tp>
4896 static _GLIBCXX_CONSTEXPR_FORMAT _Element_t
4897 _S_make_elt(_Tp& __v)
4899 using _Tq = remove_const_t<_Tp>;
4900 using _CharT =
typename _Context::char_type;
4901 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4902 "std::formatter must be specialized for the type "
4903 "of each format arg");
4904 using __format::__formattable_with;
4905 if constexpr (is_const_v<_Tp>)
4906 if constexpr (!__formattable_with<_Tp, _Context>)
4907 if constexpr (__formattable_with<_Tq, _Context>)
4908 static_assert(__formattable_with<_Tp, _Context>,
4909 "format arg must be non-const because its "
4910 "std::formatter specialization has a "
4911 "non-const reference parameter");
4912 basic_format_arg<_Context> __arg(__v);
4913 if constexpr (_S_values_only)
4914 return __arg._M_val;
4919 template<
typename... _Tp>
4920 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4921 [[__gnu__::__always_inline__]]
4922 _GLIBCXX_CONSTEXPR_FORMAT
4923 _Arg_store(_Tp&... __a) noexcept
4924 : _M_args{_S_make_elt(__a)...}
4928 template<
typename _Context>
4929 class __format::_Arg_store<_Context>
4932 template<
typename _Context>
4933 template<
typename... _Args>
4934 inline _GLIBCXX_CONSTEXPR_FORMAT
4935 basic_format_args<_Context>::
4936 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4938 if constexpr (
sizeof...(_Args) == 0)
4941 _M_unpacked_size = 0;
4944 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4947 _M_packed_size =
sizeof...(_Args);
4950 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4952 _M_values = __store._M_args;
4959 _M_unpacked_size =
sizeof...(_Args);
4961 _M_args = __store._M_args;
4966 template<
typename _Context = format_context,
typename... _Args>
4967 [[nodiscard,__gnu__::__always_inline__]]
4968 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4969 make_format_args(_Args&... __fmt_args)
noexcept
4971 using _Fmt_arg = basic_format_arg<_Context>;
4972 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4973 _Normalize<_Args>...>;
4974 return _Store(__fmt_args...);
4977#ifdef _GLIBCXX_USE_WCHAR_T
4979 template<
typename... _Args>
4980 [[nodiscard,__gnu__::__always_inline__]]
4981 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4982 make_wformat_args(_Args&... __args)
noexcept
4983 {
return std::make_format_args<wformat_context>(__args...); }
4989 template<
typename _Out,
typename _CharT,
typename _Context>
4990 _GLIBCXX_CONSTEXPR_FORMAT _Out
4991 __do_vformat_to(_Out, basic_string_view<_CharT>,
4992 const basic_format_args<_Context>&,
4993 const locale* =
nullptr);
4995 template<
typename _CharT>
struct __formatter_chrono;
5013 template<
typename _Out,
typename _CharT>
5014 class basic_format_context
5016 static_assert( output_iterator<_Out, const _CharT&> );
5018 basic_format_args<basic_format_context> _M_args;
5020 __format::_Optional_locale _M_loc;
5022 _GLIBCXX_CONSTEXPR_FORMAT
5023 basic_format_context(basic_format_args<basic_format_context> __args,
5025 : _M_args(__args), _M_out(std::move(__out))
5028 _GLIBCXX_CONSTEXPR_FORMAT
5029 basic_format_context(basic_format_args<basic_format_context> __args,
5030 _Out __out,
const std::locale& __loc)
5031 : _M_args(__args), _M_out(std::move(__out)), _M_loc(__loc)
5037 basic_format_context(
const basic_format_context&) =
delete;
5038 basic_format_context& operator=(
const basic_format_context&) =
delete;
5040 template<
typename _Out2,
typename _CharT2,
typename _Context2>
5041 friend _GLIBCXX_CONSTEXPR_FORMAT _Out2
5042 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
5043 const basic_format_args<_Context2>&,
5046 friend __format::__formatter_chrono<_CharT>;
5049 ~basic_format_context() =
default;
5051 using iterator = _Out;
5052 using char_type = _CharT;
5053 template<
typename _Tp>
5054 using formatter_type = formatter<_Tp, _CharT>;
5057 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<basic_format_context>
5058 arg(
size_t __id)
const noexcept
5059 {
return _M_args.get(__id); }
5062 std::locale locale() {
return _M_loc.value(); }
5065 _GLIBCXX_CONSTEXPR_FORMAT iterator
5068 _GLIBCXX_CONSTEXPR_FORMAT
void
5069 advance_to(iterator __it) { _M_out =
std::move(__it); }
5081 template<
typename _CharT>
5084 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
5086 struct _Parse_context : basic_format_parse_context<_CharT>
5088 using basic_format_parse_context<_CharT>::basic_format_parse_context;
5089 const _Arg_t* _M_types =
nullptr;
5093 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5094 : _M_pc(__str, __nargs)
5097 constexpr iterator begin() const noexcept {
return _M_pc.begin(); }
5098 constexpr iterator end() const noexcept {
return _M_pc.end(); }
5103 basic_string_view<_CharT> __fmt = _M_fmt_str();
5105 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5107 _M_pc.advance_to(begin() + 1);
5108 _M_format_arg(_M_pc.next_arg_id());
5112 size_t __lbr = __fmt.find(
'{');
5113 size_t __rbr = __fmt.find(
'}');
5115 while (__fmt.size())
5117 auto __cmp = __lbr <=> __rbr;
5121 _M_pc.advance_to(end());
5126 if (__lbr + 1 == __fmt.size()
5127 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5128 __format::__unmatched_left_brace_in_format_string();
5129 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5130 iterator __last = begin() + __lbr + int(__is_escape);
5131 _M_on_chars(__last);
5132 _M_pc.advance_to(__last + 1);
5133 __fmt = _M_fmt_str();
5136 if (__rbr != __fmt.npos)
5138 __lbr = __fmt.find(
'{');
5142 _M_on_replacement_field();
5143 __fmt = _M_fmt_str();
5144 __lbr = __fmt.find(
'{');
5145 __rbr = __fmt.find(
'}');
5150 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5151 __format::__unmatched_right_brace_in_format_string();
5152 iterator __last = begin() + __rbr;
5153 _M_on_chars(__last);
5154 _M_pc.advance_to(__last + 1);
5155 __fmt = _M_fmt_str();
5156 if (__lbr != __fmt.npos)
5158 __rbr = __fmt.find(
'}');
5163 constexpr basic_string_view<_CharT>
5164 _M_fmt_str() const noexcept
5165 {
return {begin(), end()}; }
5167 constexpr virtual void _M_on_chars(iterator) { }
5169 constexpr void _M_on_replacement_field()
5171 auto __next = begin();
5175 __id = _M_pc.next_arg_id();
5176 else if (*__next ==
':')
5178 __id = _M_pc.next_arg_id();
5179 _M_pc.advance_to(++__next);
5183 auto [__i, __ptr] = __format::__parse_arg_id(begin(), end());
5184 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5185 __format::__invalid_arg_id_in_format_string();
5186 _M_pc.check_arg_id(__id = __i);
5189 _M_pc.advance_to(++__ptr);
5192 _M_pc.advance_to(__ptr);
5194 _M_format_arg(__id);
5195 if (begin() == end() || *begin() !=
'}')
5196 __format::__unmatched_left_brace_in_format_string();
5197 _M_pc.advance_to(begin() + 1);
5200 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5204 template<
typename _Out,
typename _CharT>
5205 class _Formatting_scanner :
public _Scanner<_CharT>
5208 _GLIBCXX_CONSTEXPR_FORMAT
5209 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5210 basic_string_view<_CharT> __str)
5211 : _Scanner<_CharT>(__str), _M_fc(__fc)
5215 basic_format_context<_Out, _CharT>& _M_fc;
5217 using iterator =
typename _Scanner<_CharT>::iterator;
5220 _M_on_chars(iterator __last)
override
5222 basic_string_view<_CharT> __str(this->begin(), __last);
5223 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5227 _M_format_arg(
size_t __id)
override
5229 using _Context = basic_format_context<_Out, _CharT>;
5230 using handle =
typename basic_format_arg<_Context>::handle;
5232 __format::__visit_format_arg([
this](
auto& __arg) {
5234 using _Formatter =
typename _Context::template formatter_type<_Type>;
5235 if constexpr (is_same_v<_Type, monostate>)
5236 __format::__invalid_arg_id_in_format_string();
5237 else if constexpr (is_same_v<_Type, handle>)
5238 __arg.format(this->_M_pc, this->_M_fc);
5239 else if constexpr (is_default_constructible_v<_Formatter>)
5242 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5243 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5246 static_assert(__format::__formattable_with<_Type, _Context>);
5247 }, _M_fc.arg(__id));
5251 template<
typename _CharT,
typename _Tp>
5253 __to_arg_t_enum() noexcept
5255 using _Context = __format::__format_context<_CharT>;
5256 using _Fmt_arg = basic_format_arg<_Context>;
5257 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5258 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5262 template<
typename _CharT,
typename... _Args>
5263 class _Checking_scanner :
public _Scanner<_CharT>
5266 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5267 "std::formatter must be specialized for each type being formatted");
5271 _Checking_scanner(basic_string_view<_CharT> __str)
5272 : _Scanner<_CharT>(__str, sizeof...(_Args))
5274#if __cpp_lib_format >= 202305L
5275 this->_M_pc._M_types = _M_types.data();
5281 _M_format_arg(
size_t __id)
override
5283 if constexpr (
sizeof...(_Args) != 0)
5285 if (__id <
sizeof...(_Args))
5287 _M_parse_format_spec<_Args...>(__id);
5291 __builtin_unreachable();
5294 template<
typename _Tp,
typename... _OtherArgs>
5296 _M_parse_format_spec(
size_t __id)
5300 formatter<_Tp, _CharT> __f;
5301 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5303 else if constexpr (
sizeof...(_OtherArgs) != 0)
5304 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5306 __builtin_unreachable();
5309#if __cpp_lib_format >= 202305L
5310 array<_Arg_t,
sizeof...(_Args)>
5311 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5315 template<typename _CharT, unsigned = __unicode::__literal_encoding_is_unicode<_CharT>()>
5316 _GLIBCXX_CONSTEXPR_FORMAT _Sink_iter<_CharT>
5317 __do_vformat_to(_Sink_iter<_CharT> __out, basic_string_view<_CharT> __fmt,
5318 __format_context<_CharT>& __ctx)
5320 if constexpr (is_same_v<_CharT, char>)
5322 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5324 bool __done =
false;
5325 __format::__visit_format_arg([&](
auto& __arg) {
5326 using _Tp = remove_cvref_t<
decltype(__arg)>;
5327 if constexpr (is_same_v<_Tp, bool>)
5329 size_t __len = 4 + !__arg;
5330 const char* __chars[] = {
"false",
"true" };
5331 if (
auto __res = __out._M_reserve(__len))
5333 ranges::copy_n(__chars[__arg], __len, __res.get());
5334 __res._M_bump(__len);
5338 else if constexpr (is_same_v<_Tp, char>)
5340 if (
auto __res = __out._M_reserve(1))
5342 *__res.get() = __arg;
5347 else if constexpr (is_integral_v<_Tp>)
5349 make_unsigned_t<_Tp> __uval;
5350 const bool __neg = __arg < 0;
5352 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5355 const auto __n = __detail::__to_chars_len(__uval);
5356 if (
auto __res = __out._M_reserve(__n + __neg))
5358 auto __ptr = __res.get();
5360 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5362 __res._M_bump(__n + __neg);
5366 else if constexpr (is_convertible_v<_Tp, string_view>)
5368 string_view __sv = __arg;
5369 if (
auto __res = __out._M_reserve(__sv.size()))
5371 ranges::copy(__sv, __res.get());
5372 __res._M_bump(__sv.size());
5382 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5383 __scanner._M_scan();
5392#if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE
5393 extern template _Sink_iter<char>
5394 __do_vformat_to<char, 1>(_Sink_iter<char>, string_view,
5396# ifdef _GLIBCXX_USE_WCHAR_T
5397 extern template _Sink_iter<wchar_t>
5398 __do_vformat_to<wchar_t, 1>(_Sink_iter<wchar_t>, wstring_view,
5403 template<
typename _Out,
typename _CharT,
typename _Context>
5404 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5405 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5406 const basic_format_args<_Context>& __args,
5407 const locale* __loc)
5409 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5411 auto __ctx = __loc ==
nullptr
5412 ? _Context(__args, __out)
5413 : _Context(__args, __out, *__loc);
5414 return __format::__do_vformat_to(__out, __fmt, __ctx);
5416 else if constexpr (__contiguous_char_iter<_CharT, _Out>)
5418 _Ptr_sink<_CharT> __sink(__out);
5419 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5420 return std::move(__sink)._M_finish(__out).out;
5424 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5425 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5426 return std::move(__sink)._M_finish().out;
5430 template<
typename _Out,
typename _CharT>
5431 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5432 __do_vformat_to_n(_Out __out, iter_difference_t<_Out> __n,
5433 basic_string_view<_CharT> __fmt,
5434 const type_identity_t<
5435 basic_format_args<__format_context<_CharT>>>& __args,
5436 const locale* __loc =
nullptr)
5438 if constexpr (__contiguous_char_iter<_CharT, _Out>)
5440 _Ptr_sink<_CharT> __sink(__out, __n);
5441 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5442 return std::move(__sink)._M_finish(__out);
5446 _Iter_sink<_CharT, _Out> __sink(
std::move(__out), __n);
5447 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5452#pragma GCC diagnostic pop
5457#if __cpp_lib_format >= 202305L
5460 template<
typename _CharT>
5461 template<
typename... _Ts>
5463 basic_format_parse_context<_CharT>::
5464 __check_dynamic_spec(
size_t __id)
noexcept
5466 if (__id >= _M_num_args)
5467 __format::__invalid_arg_id_in_format_string();
5468 if constexpr (
sizeof...(_Ts) != 0)
5470 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
5471 auto* __args =
static_cast<_Parse_ctx*
>(
this)->_M_types;
5476 auto __arg = __args[__id];
5477 __format::_Arg_t __types[] = {
5478 __format::__to_arg_t_enum<_CharT, _Ts>()...
5480 for (
auto __t : __types)
5484 __invalid_dynamic_spec(
"arg(id) type does not match");
5489 template<
typename _CharT,
typename... _Args>
5490 template<
typename _Tp>
5491 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5493 basic_format_string<_CharT, _Args...>::
5494 basic_format_string(
const _Tp& __s) noexcept
5497 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5499 __scanner._M_scan();
5504 template<
typename _Out>
requires output_iterator<_Out, const char&>
5505 [[__gnu__::__always_inline__]]
5506 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5507 vformat_to(_Out __out, string_view __fmt, format_args __args)
5508 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5510#ifdef _GLIBCXX_USE_WCHAR_T
5511 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5512 [[__gnu__::__always_inline__]]
5513 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5514 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5515 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5518 template<
typename _Out>
requires output_iterator<_Out, const char&>
5519 [[__gnu__::__always_inline__]]
5521 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5524 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5527#ifdef _GLIBCXX_USE_WCHAR_T
5528 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5529 [[__gnu__::__always_inline__]]
5531 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5532 wformat_args __args)
5534 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5539 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5540 vformat(string_view __fmt, format_args __args)
5542 __format::_Str_sink<char> __buf;
5543 std::vformat_to(__buf.out(), __fmt, __args);
5547#ifdef _GLIBCXX_USE_WCHAR_T
5549 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5550 vformat(wstring_view __fmt, wformat_args __args)
5552 __format::_Str_sink<wchar_t> __buf;
5553 std::vformat_to(__buf.out(), __fmt, __args);
5560 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5562 __format::_Str_sink<char> __buf;
5563 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5567#ifdef _GLIBCXX_USE_WCHAR_T
5570 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5572 __format::_Str_sink<wchar_t> __buf;
5573 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5578 template<
typename... _Args>
5580 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5581 format(format_string<_Args...> __fmt, _Args&&... __args)
5582 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5584#ifdef _GLIBCXX_USE_WCHAR_T
5585 template<
typename... _Args>
5587 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5588 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5589 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5592 template<
typename... _Args>
5595 format(
const locale& __loc, format_string<_Args...> __fmt,
5598 return std::vformat(__loc, __fmt.get(),
5599 std::make_format_args(__args...));
5602#ifdef _GLIBCXX_USE_WCHAR_T
5603 template<
typename... _Args>
5606 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5609 return std::vformat(__loc, __fmt.get(),
5610 std::make_wformat_args(__args...));
5614 template<
typename _Out,
typename... _Args>
5615 requires output_iterator<_Out, const char&>
5616 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5617 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5619 return std::vformat_to(
std::move(__out), __fmt.get(),
5620 std::make_format_args(__args...));
5623#ifdef _GLIBCXX_USE_WCHAR_T
5624 template<
typename _Out,
typename... _Args>
5625 requires output_iterator<_Out, const wchar_t&>
5626 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5627 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5629 return std::vformat_to(
std::move(__out), __fmt.get(),
5630 std::make_wformat_args(__args...));
5634 template<
typename _Out,
typename... _Args>
5635 requires output_iterator<_Out, const char&>
5637 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5640 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5641 std::make_format_args(__args...));
5644#ifdef _GLIBCXX_USE_WCHAR_T
5645 template<
typename _Out,
typename... _Args>
5646 requires output_iterator<_Out, const wchar_t&>
5648 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5651 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5652 std::make_wformat_args(__args...));
5656 template<
typename _Out,
typename... _Args>
5657 requires output_iterator<_Out, const char&>
5658 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5659 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5660 format_string<_Args...> __fmt, _Args&&... __args)
5662 return __format::__do_vformat_to_n(
5664 std::make_format_args(__args...));
5667#ifdef _GLIBCXX_USE_WCHAR_T
5668 template<
typename _Out,
typename... _Args>
5669 requires output_iterator<_Out, const wchar_t&>
5670 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5671 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5672 wformat_string<_Args...> __fmt, _Args&&... __args)
5674 return __format::__do_vformat_to_n(
5676 std::make_wformat_args(__args...));
5680 template<
typename _Out,
typename... _Args>
5681 requires output_iterator<_Out, const char&>
5682 inline format_to_n_result<_Out>
5683 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5684 format_string<_Args...> __fmt, _Args&&... __args)
5686 return __format::__do_vformat_to_n(
5688 std::make_format_args(__args...), &__loc);
5691#ifdef _GLIBCXX_USE_WCHAR_T
5692 template<
typename _Out,
typename... _Args>
5693 requires output_iterator<_Out, const wchar_t&>
5694 inline format_to_n_result<_Out>
5695 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5696 wformat_string<_Args...> __fmt, _Args&&... __args)
5698 return __format::__do_vformat_to_n(
5700 std::make_wformat_args(__args...), &__loc);
5708 template<
typename _CharT>
5709 class _Counting_sink final :
public _Ptr_sink<_CharT>
5712 _GLIBCXX_CONSTEXPR_FORMAT
5713 _Counting_sink() : _Ptr_sink<_CharT>(nullptr, 0) { }
5715 [[__gnu__::__always_inline__]]
5716 _GLIBCXX_CONSTEXPR_FORMAT
size_t
5718 {
return this->_M_count + this->_M_used().size(); }
5721 template<
typename _CharT>
5722 class _Counting_sink :
public _Buf_sink<_CharT>
5724 size_t _M_count = 0;
5727 _M_overflow()
override
5729 if (!std::is_constant_evaluated())
5730 _M_count += this->_M_used().size();
5735 _Counting_sink() =
default;
5737 [[__gnu__::__always_inline__]]
5741 _Counting_sink::_M_overflow();
5749 template<
typename... _Args>
5751 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5752 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5754 __format::_Counting_sink<char> __buf;
5755 std::vformat_to(__buf.out(), __fmt.get(),
5756 std::make_format_args(__args...));
5757 return __buf.count();
5760#ifdef _GLIBCXX_USE_WCHAR_T
5761 template<
typename... _Args>
5763 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5764 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5766 __format::_Counting_sink<wchar_t> __buf;
5767 std::vformat_to(__buf.out(), __fmt.get(),
5768 std::make_wformat_args(__args...));
5769 return __buf.count();
5773 template<
typename... _Args>
5776 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5779 __format::_Counting_sink<char> __buf;
5780 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5781 std::make_format_args(__args...));
5782 return __buf.count();
5785#ifdef _GLIBCXX_USE_WCHAR_T
5786 template<
typename... _Args>
5789 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5792 __format::_Counting_sink<wchar_t> __buf;
5793 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5794 std::make_wformat_args(__args...));
5795 return __buf.count();
5799#if __glibcxx_format_ranges
5801 template<typename _Tp>
5802 consteval range_format
5805 using _Ref = ranges::range_reference_t<_Tp>;
5806 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5807 return range_format::disabled;
5808 else if constexpr (
requires {
typename _Tp::key_type; })
5810 if constexpr (
requires {
typename _Tp::mapped_type; })
5812 using _Up = remove_cvref_t<_Ref>;
5813 if constexpr (__is_pair<_Up>)
5814 return range_format::map;
5815 else if constexpr (__is_specialization_of<_Up, tuple>)
5816 if constexpr (tuple_size_v<_Up> == 2)
5817 return range_format::map;
5819 return range_format::set;
5822 return range_format::sequence;
5827 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5828 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5833 template<
typename _CharT,
typename _Out,
typename _Callback>
5834 _GLIBCXX_CONSTEXPR_FORMAT
5835 typename basic_format_context<_Out, _CharT>::iterator
5836 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5837 const _Spec<_CharT>& __spec,
5840 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
5846 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
5848 const size_t __padwidth = __spec._M_get_width(__fc);
5849 if (__padwidth == 0)
5850 return __call(__fc);
5854 _GLIBCXX_CONSTEXPR_FORMAT
5855 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5856 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5859 _GLIBCXX_CONSTEXPR_FORMAT
void
5861 { _M_ctx =
nullptr; }
5863 _GLIBCXX_CONSTEXPR_FORMAT
5867 _M_ctx->advance_to(_M_out);
5871 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5872 _Sink_iter<_CharT> _M_out;
5875 _Restore_out __restore(__fc);
5876 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
5877 __fc.advance_to(__sink.out());
5879 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
5880 __restore._M_disarm();
5885 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5886 struct __indexed_formatter_storage
5891 basic_format_parse_context<_CharT> __pc({});
5892 if (_M_formatter.parse(__pc) != __pc.end())
5893 __format::__failed_to_parse_format_spec();
5896 template<
typename _Out>
5897 _GLIBCXX_CONSTEXPR_FORMAT
void
5898 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5899 basic_format_context<_Out, _CharT>& __fc,
5900 basic_string_view<_CharT> __sep)
const
5902 if constexpr (_Pos != 0)
5903 __fc.advance_to(__format::__write(__fc.out(), __sep));
5904 __fc.advance_to(_M_formatter.format(__elem, __fc));
5907 [[__gnu__::__always_inline__]]
5911 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5912 _M_formatter.set_debug_format();
5916 formatter<_Tp, _CharT> _M_formatter;
5919 template<
typename _CharT,
typename... _Tps>
5920 class __tuple_formatter
5922 using _String_view = basic_string_view<_CharT>;
5923 using _Seps = __format::_Separators<_CharT>;
5927 set_separator(basic_string_view<_CharT> __sep)
noexcept
5931 set_brackets(basic_string_view<_CharT> __open,
5932 basic_string_view<_CharT> __close)
noexcept
5940 constexpr typename basic_format_parse_context<_CharT>::iterator
5941 parse(basic_format_parse_context<_CharT>& __pc)
5943 auto __first = __pc.begin();
5944 const auto __last = __pc.end();
5945 __format::_Spec<_CharT> __spec{};
5947 auto __finished = [&]
5949 if (__first != __last && *__first !=
'}')
5953 _M_felems._M_parse();
5954 _M_felems.set_debug_format();
5961 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5965 __first = __spec._M_parse_width(__first, __last, __pc);
5969 if (*__first ==
'n')
5972 _M_open = _M_close = _String_view();
5974 else if (*__first ==
'm')
5977 if constexpr (
sizeof...(_Tps) == 2)
5979 _M_sep = _Seps::_S_colon();
5980 _M_open = _M_close = _String_view();
5983 __throw_format_error(
"format error: 'm' specifier requires range"
5984 " of pair or tuple of two elements");
5990 __format::__failed_to_parse_format_spec();
5994 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
5995 _GLIBCXX_CONSTEXPR_FORMAT
5996 typename basic_format_context<_Out, _CharT>::iterator
5997 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
5998 basic_format_context<_Out, _CharT>& __fc)
const
5999 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
6001 template<
typename _Out>
6002 _GLIBCXX_CONSTEXPR_FORMAT
6003 typename basic_format_context<_Out, _CharT>::iterator
6004 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
6005 basic_format_context<_Out, _CharT>& __fc)
const
6007 return __format::__format_padded(
6009 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
6011 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
6012 _M_felems._M_format(__elems..., __nfc, _M_sep);
6013 return __format::__write(__nfc.out(), _M_close);
6018 template<
size_t... _Ids>
6019 struct __formatters_storage
6020 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
6022 template<
size_t _Id,
typename _Up>
6023 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
6028 (_Base<_Ids, _Tps>::_M_parse(), ...);
6031 template<
typename _Out>
6032 _GLIBCXX_CONSTEXPR_FORMAT
void
6033 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
6034 basic_format_context<_Out, _CharT>& __fc,
6035 _String_view __sep)
const
6037 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
6043 (_Base<_Ids, _Tps>::set_debug_format(), ...);
6047 template<
size_t... _Ids>
6048 static _GLIBCXX_CONSTEXPR_FORMAT
auto
6049 _S_create_storage(index_sequence<_Ids...>)
6050 -> __formatters_storage<_Ids...>;
6052 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
6054 _Spec<_CharT> _M_spec{};
6055 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
6056 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
6057 _String_view _M_sep = _Seps::_S_comma();
6058 _Formatters _M_felems;
6061 template<
typename _Tp>
6062 concept __is_map_formattable
6063 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
6069 template<__format::__
char _CharT, formattable<_CharT> _Fp,
6070 formattable<_CharT> _Sp>
6071 struct formatter<
pair<_Fp, _Sp>, _CharT>
6072 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
6073 remove_cvref_t<_Sp>>
6076 using __maybe_const_pair
6077 = __conditional_t<formattable<const _Fp, _CharT>
6078 && formattable<const _Sp, _CharT>,
6079 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
6083 template<
typename _Out>
6084 _GLIBCXX_CONSTEXPR_FORMAT
6085 typename basic_format_context<_Out, _CharT>::iterator
6086 format(__maybe_const_pair& __p,
6087 basic_format_context<_Out, _CharT>& __fc)
const
6088 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
6091#if __glibcxx_print >= 202406L
6094 template<
typename _Fp,
typename _Sp>
6095 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
6096 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
6097 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
6100 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
6101 struct formatter<tuple<_Tps...>, _CharT>
6102 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
6105 using __maybe_const_tuple
6106 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
6107 const tuple<_Tps...>, tuple<_Tps...>>;
6111 template<
typename _Out>
6112 _GLIBCXX_CONSTEXPR_FORMAT
6113 typename basic_format_context<_Out, _CharT>::iterator
6114 format(__maybe_const_tuple& __t,
6115 basic_format_context<_Out, _CharT>& __fc)
const
6116 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
6119#if __glibcxx_print >= 202406L
6122 template<
typename... _Tps>
6123 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
6124 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
6128 template<
typename _Tp, __format::__
char _CharT>
6129 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
6130 class range_formatter
6132 using _String_view = basic_string_view<_CharT>;
6133 using _Seps = __format::_Separators<_CharT>;
6137 set_separator(basic_string_view<_CharT> __sep)
noexcept
6141 set_brackets(basic_string_view<_CharT> __open,
6142 basic_string_view<_CharT> __close)
noexcept
6148 constexpr formatter<_Tp, _CharT>&
6149 underlying() noexcept
6152 constexpr const formatter<_Tp, _CharT>&
6153 underlying() const noexcept
6158 constexpr typename basic_format_parse_context<_CharT>::iterator
6159 parse(basic_format_parse_context<_CharT>& __pc)
6161 auto __first = __pc.begin();
6162 const auto __last = __pc.end();
6163 __format::_Spec<_CharT> __spec{};
6164 bool __no_brace =
false;
6166 auto __finished = [&]
6167 {
return __first == __last || *__first ==
'}'; };
6169 auto __finalize = [&]
6175 auto __parse_val = [&](_String_view __nfs = _String_view())
6177 basic_format_parse_context<_CharT> __npc(__nfs);
6178 if (_M_fval.parse(__npc) != __npc.end())
6179 __format::__failed_to_parse_format_spec();
6180 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6181 _M_fval.set_debug_format();
6182 return __finalize();
6186 return __parse_val();
6188 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6190 return __parse_val();
6192 __first = __spec._M_parse_width(__first, __last, __pc);
6194 return __parse_val();
6196 if (*__first ==
'?')
6199 __spec._M_debug =
true;
6200 if (__finished() || *__first !=
's')
6201 __throw_format_error(
"format error: '?' is allowed only in"
6202 " combination with 's'");
6205 if (*__first ==
's')
6208 if constexpr (same_as<_Tp, _CharT>)
6210 __spec._M_type = __format::_Pres_s;
6212 return __finalize();
6213 __throw_format_error(
"format error: element format specifier"
6214 " cannot be provided when 's' specifier is used");
6217 __throw_format_error(
"format error: 's' specifier requires"
6218 " range of character types");
6222 return __parse_val();
6224 if (*__first ==
'n')
6227 _M_open = _M_close = _String_view();
6232 return __parse_val();
6234 if (*__first ==
'm')
6236 _String_view __m(__first, 1);
6238 if constexpr (__format::__is_map_formattable<_Tp>)
6240 _M_sep = _Seps::_S_comma();
6243 _M_open = _Seps::_S_braces().substr(0, 1);
6244 _M_close = _Seps::_S_braces().substr(1, 1);
6247 return __parse_val(__m);
6248 __throw_format_error(
"format error: element format specifier"
6249 " cannot be provided when 'm' specifier is used");
6252 __throw_format_error(
"format error: 'm' specifier requires"
6253 " range of pairs or tuples of two elements");
6257 return __parse_val();
6259 if (*__first ==
':')
6261 __pc.advance_to(++__first);
6262 __first = _M_fval.parse(__pc);
6266 return __finalize();
6268 __format::__failed_to_parse_format_spec();
6273 template<ranges::input_range _Rg,
typename _Out>
6274 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6275 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6276 _GLIBCXX_CONSTEXPR_FORMAT
6277 typename basic_format_context<_Out, _CharT>::iterator
6278 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6280 using _Range = remove_reference_t<_Rg>;
6281 if constexpr (ranges::contiguous_range<_Rg>)
6283 const span<__format::__maybe_const<_Tp, _CharT>>
6284 __spn(ranges::data(__rg),
size_t(ranges::distance(__rg)));
6285 return _M_format(__spn, __fc);
6287 else if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6288 return _M_format<const _Range>(__rg, __fc);
6290 return _M_format(__rg, __fc);
6294 template<ranges::input_range _Rg,
typename _Out>
6295 _GLIBCXX_CONSTEXPR_FORMAT
6296 typename basic_format_context<_Out, _CharT>::iterator
6297 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6299 if constexpr (same_as<_Tp, _CharT>)
6300 if (_M_spec._M_type == __format::_Pres_s)
6302 __format::__formatter_str __fstr(_M_spec);
6303 return __fstr._M_format_range(__rg, __fc);
6305 return __format::__format_padded(
6307 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6308 {
return _M_format_elems(__rg, __nfc); });
6312 template<ranges::input_range _Rg,
typename _Out>
6313 _GLIBCXX_CONSTEXPR_FORMAT
6314 typename basic_format_context<_Out, _CharT>::iterator
6315 _M_format_elems(_Rg& __rg,
6316 basic_format_context<_Out, _CharT>& __fc)
const
6318 auto __out = __format::__write(__fc.out(), _M_open);
6320 auto __first = ranges::begin(__rg);
6321 auto const __last = ranges::end(__rg);
6322 if (__first == __last)
6323 return __format::__write(__out, _M_close);
6325 __fc.advance_to(__out);
6326 __out = _M_fval.format(*__first, __fc);
6327 for (++__first; __first != __last; ++__first)
6329 __out = __format::__write(__out, _M_sep);
6330 __fc.advance_to(__out);
6331 __out = _M_fval.format(*__first, __fc);
6334 return __format::__write(__out, _M_close);
6337 __format::_Spec<_CharT> _M_spec{};
6338 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6339 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6340 _String_view _M_sep = _Seps::_S_comma();
6341 formatter<_Tp, _CharT> _M_fval;
6349 template<ranges::input_range _Rg, __format::__
char _CharT>
6350 requires (format_kind<_Rg> != range_format::disabled)
6351 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6352 struct formatter<_Rg, _CharT>
6355 static const bool _S_range_format_is_string =
6356 (format_kind<_Rg> == range_format::string)
6357 || (format_kind<_Rg> == range_format::debug_string);
6358 using _Vt = remove_cvref_t<
6359 ranges::range_reference_t<
6360 __format::__maybe_const_range<_Rg, _CharT>>>;
6362 static consteval bool _S_is_correct()
6364 if constexpr (_S_range_format_is_string)
6365 static_assert(same_as<_Vt, _CharT>);
6369 static_assert(_S_is_correct());
6372 constexpr formatter() noexcept
6374 using _Seps = __format::_Separators<_CharT>;
6375 if constexpr (format_kind<_Rg> == range_format::map)
6377 static_assert(__format::__is_map_formattable<_Vt>);
6378 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6379 _Seps::_S_braces().substr(1, 1));
6380 _M_under.underlying().set_brackets({}, {});
6381 _M_under.underlying().set_separator(_Seps::_S_colon());
6383 else if constexpr (format_kind<_Rg> == range_format::set)
6384 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6385 _Seps::_S_braces().substr(1, 1));
6389 set_separator(basic_string_view<_CharT> __sep)
noexcept
6390 requires (format_kind<_Rg> == range_format::sequence)
6391 { _M_under.set_separator(__sep); }
6394 set_brackets(basic_string_view<_CharT> __open,
6395 basic_string_view<_CharT> __close)
noexcept
6396 requires (format_kind<_Rg> == range_format::sequence)
6397 { _M_under.set_brackets(__open, __close); }
6401 constexpr typename basic_format_parse_context<_CharT>::iterator
6402 parse(basic_format_parse_context<_CharT>& __pc)
6404 auto __res = _M_under.parse(__pc);
6405 if constexpr (format_kind<_Rg> == range_format::debug_string)
6406 _M_under.set_debug_format();
6412 template<
typename _Out>
6413 _GLIBCXX_CONSTEXPR_FORMAT
6414 typename basic_format_context<_Out, _CharT>::iterator
6415 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6416 basic_format_context<_Out, _CharT>& __fc)
const
6418 if constexpr (_S_range_format_is_string)
6419 return _M_under._M_format_range(__rg, __fc);
6421 return _M_under.format(__rg, __fc);
6425 using _Formatter_under
6426 = __conditional_t<_S_range_format_is_string,
6427 __format::__formatter_str<_CharT>,
6428 range_formatter<_Vt, _CharT>>;
6429 _Formatter_under _M_under;
6432#if __glibcxx_print >= 202406L
6433 template<ranges::input_range _Rg>
6434 requires (format_kind<_Rg> != range_format::disabled)
6435 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6439#undef _GLIBCXX_WIDEN
6440#undef _GLIBCXX_CONSTEXPR_FORMAT
6442_GLIBCXX_END_NAMESPACE_VERSION
6445#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.