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>
291 class _GLIBCXX_NO_SPECIALIZATIONS basic_format_parse_context
294 using char_type = _CharT;
295 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
296 using iterator = const_iterator;
299 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
300 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
303 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
304 void operator=(
const basic_format_parse_context&) =
delete;
306 constexpr const_iterator begin() const noexcept {
return _M_begin; }
307 constexpr const_iterator end() const noexcept {
return _M_end; }
310 advance_to(const_iterator __it)
noexcept
316 if (_M_indexing == _Manual)
317 __format::__conflicting_indexing_in_format_string();
322 if (std::is_constant_evaluated())
323 if (_M_next_arg_id == _M_num_args)
324 __format::__invalid_arg_id_in_format_string();
325 return _M_next_arg_id++;
329 check_arg_id(
size_t __id)
331 if (_M_indexing == _Auto)
332 __format::__conflicting_indexing_in_format_string();
333 _M_indexing = _Manual;
335 if (std::is_constant_evaluated())
336 if (__id >= _M_num_args)
337 __format::__invalid_arg_id_in_format_string();
340#if __cpp_lib_format >= 202305L
341 template<
typename... _Ts>
343 check_dynamic_spec(
size_t __id)
noexcept
345 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
346 "template arguments for check_dynamic_spec<Ts...>(id) "
347 "must be unique and must be one of the allowed types");
349 __check_dynamic_spec<_Ts...>(__id);
354 check_dynamic_spec_integral(
size_t __id)
noexcept
357 __check_dynamic_spec<int, unsigned,
long long,
358 unsigned long long>(__id);
363 check_dynamic_spec_string(
size_t __id)
noexcept
366 __check_dynamic_spec<const _CharT*, basic_string_view<_CharT>>(__id);
372 template<
typename _Tp,
typename... _Ts>
373 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
375 template<
typename... _Ts>
377 __valid_types_for_check_dynamic_spec()
381 if constexpr (
sizeof...(_Ts) == 0)
390 = __once<bool, _Ts...>
391 + __once<char_type, _Ts...>
392 + __once<int, _Ts...>
393 + __once<
unsigned int, _Ts...>
394 + __once<
long long int, _Ts...>
395 + __once<
unsigned long long int, _Ts...>
396 + __once<float, _Ts...>
397 + __once<double, _Ts...>
398 + __once<
long double, _Ts...>
399 + __once<
const char_type*, _Ts...>
400 + __once<basic_string_view<char_type>, _Ts...>
401 + __once<
const void*, _Ts...>;
402 return __sum ==
sizeof...(_Ts);
406 template<
typename... _Ts>
408 __check_dynamic_spec(
size_t __id)
noexcept;
411 static void __invalid_dynamic_spec(
const char*);
413 friend __format::_Scanner<_CharT>;
418 basic_format_parse_context(basic_string_view<_CharT> __fmt,
419 size_t __num_args) noexcept
420 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
426 enum _Indexing { _Unknown, _Manual, _Auto };
427 _Indexing _M_indexing = _Unknown;
428 size_t _M_next_arg_id = 0;
429 size_t _M_num_args = 0;
433 template<
typename _Tp,
template<
typename...>
class _Class>
434 constexpr bool __is_specialization_of =
false;
435 template<
template<
typename...>
class _Class,
typename... _Args>
436 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
441 template<
typename _CharT>
442 constexpr pair<unsigned short, const _CharT*>
443 __parse_integer(
const _CharT* __first,
const _CharT* __last)
445 if (__first == __last)
446 __builtin_unreachable();
448 if constexpr (is_same_v<_CharT, char>)
450 const auto __start = __first;
451 unsigned short __val = 0;
453 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
454 && __first != __start) [[likely]]
455 return {__val, __first};
459 constexpr int __n = 32;
461 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
462 __buf[__i] = __first[__i];
463 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
464 if (__ptr) [[likely]]
465 return {__v, __first + (__ptr - __buf)};
470 template<
typename _CharT>
471 constexpr pair<unsigned short, const _CharT*>
472 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
474 if (__first == __last)
475 __builtin_unreachable();
478 return {0, __first + 1};
480 if (
'1' <= *__first && *__first <=
'9')
482 const unsigned short __id = *__first -
'0';
483 const auto __next = __first + 1;
485 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
486 return {__id, __next};
488 return __format::__parse_integer(__first, __last);
493 enum class _Pres_type :
unsigned char {
497 _Pres_c = 2, _Pres_x, _Pres_X, _Pres_d, _Pres_o, _Pres_b, _Pres_B,
499 _Pres_g = 1, _Pres_G, _Pres_a, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F,
503 using enum _Pres_type;
505 enum class _Sign :
unsigned char {
513 enum _WidthPrec :
unsigned char {
518 using enum _WidthPrec;
520 template<
typename _Context>
521 _GLIBCXX_CONSTEXPR_FORMAT
size_t
522 __int_from_arg(
const basic_format_arg<_Context>& __arg);
524 constexpr bool __is_digit(
char __c)
525 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
527 constexpr bool __is_xdigit(
char __c)
528 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
535 template<
typename _CharT>
536 struct _Spec : _SpecBase
538 unsigned short _M_width;
539 unsigned short _M_prec;
540 char32_t _M_fill =
' ';
544 unsigned _M_localized : 1;
545 unsigned _M_zero_fill : 1;
546 _WidthPrec _M_width_kind : 2;
547 _WidthPrec _M_prec_kind : 2;
548 unsigned _M_debug : 1;
549 _Pres_type _M_type : 4;
550 unsigned _M_reserved : 8;
554 using iterator =
typename basic_string_view<_CharT>::iterator;
556 static constexpr _Align
557 _S_align(_CharT __c)
noexcept
561 case '<':
return _Align_left;
562 case '>':
return _Align_right;
563 case '^':
return _Align_centre;
564 default:
return _Align_default;
570 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
571 {
return _M_parse_fill_and_align(__first, __last,
"{"); }
575 _M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill)
noexcept
577 for (
char __c : __not_fill)
578 if (*__first ==
static_cast<_CharT
>(__c))
581 using namespace __unicode;
582 if constexpr (__literal_encoding_is_unicode<_CharT>())
585 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
588 auto __beg = __uv.begin();
589 char32_t __c = *__beg++;
590 if (__is_scalar_value(__c))
591 if (
auto __next = __beg.base(); __next != __last)
592 if (_Align __align = _S_align(*__next); __align != _Align_default)
600 else if (__last - __first >= 2)
601 if (_Align __align = _S_align(__first[1]); __align != _Align_default)
608 if (_Align __align = _S_align(__first[0]); __align != _Align_default)
617 static constexpr _Sign
618 _S_sign(_CharT __c)
noexcept
622 case '+':
return _Sign_plus;
623 case '-':
return _Sign_minus;
624 case ' ':
return _Sign_space;
625 default:
return _Sign_default;
631 _M_parse_sign(iterator __first, iterator)
noexcept
633 if (_Sign __sign = _S_sign(*__first); __sign != _Sign_default)
643 _M_parse_alternate_form(iterator __first, iterator)
noexcept
655 _M_parse_zero_fill(iterator __first, iterator )
noexcept
666 static constexpr iterator
667 _S_parse_width_or_precision(iterator __first, iterator __last,
668 unsigned short& __val,
bool& __arg_id,
669 basic_format_parse_context<_CharT>& __pc)
671 if (__format::__is_digit(*__first))
673 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
675 __throw_format_error(
"format error: invalid width or precision "
680 else if (*__first ==
'{')
684 if (__first == __last)
685 __format::__unmatched_left_brace_in_format_string();
687 __val = __pc.next_arg_id();
690 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
691 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
692 __format::__invalid_arg_id_in_format_string();
694 __pc.check_arg_id(__v);
697#if __cpp_lib_format >= 202305L
698 __pc.check_dynamic_spec_integral(__val);
707 _M_parse_width(iterator __first, iterator __last,
708 basic_format_parse_context<_CharT>& __pc)
710 bool __arg_id =
false;
712 __throw_format_error(
"format error: width must be non-zero in "
714 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
716 if (__next != __first)
717 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
723 _M_parse_precision(iterator __first, iterator __last,
724 basic_format_parse_context<_CharT>& __pc)
726 if (__first[0] !=
'.')
729 iterator __next = ++__first;
730 bool __arg_id =
false;
731 if (__next != __last)
732 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
734 if (__next == __first)
735 __throw_format_error(
"format error: missing precision after '.' in "
737 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
743 _M_parse_locale(iterator __first, iterator )
noexcept
753 template<
typename _Context>
754 _GLIBCXX_CONSTEXPR_FORMAT
size_t
755 _M_get_width(_Context& __ctx)
const
758 if (_M_width_kind == _WP_value)
760 else if (_M_width_kind == _WP_from_arg)
761 __width = __format::__int_from_arg(__ctx.arg(_M_width));
765 template<
typename _Context>
766 _GLIBCXX_CONSTEXPR_FORMAT
size_t
767 _M_get_precision(_Context& __ctx)
const
770 if (_M_prec_kind == _WP_value)
772 else if (_M_prec_kind == _WP_from_arg)
773 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
778 template<
typename _Int>
779 inline _GLIBCXX_CONSTEXPR_FORMAT
char*
780 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
784 else if (__sign == _Sign_plus)
786 else if (__sign == _Sign_space)
794 template<
typename _Out,
typename _CharT>
795 requires output_iterator<_Out, const _CharT&>
796 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
797 __write(_Out __out, basic_string_view<_CharT> __str)
799 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
805 for (_CharT __c : __str)
812 template<
typename _Out,
typename _CharT>
813 _GLIBCXX_CONSTEXPR_FORMAT _Out
814 __write_padded(_Out __out, basic_string_view<_CharT> __str,
815 _Align __align,
size_t __nfill,
char32_t __fill_char)
817 const size_t __buflen = 0x20;
818 _CharT __padding_chars[__buflen];
819 __padding_chars[0] = _CharT();
820 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
822 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
825 while (__n > __padding.size())
827 __o = __format::__write(
std::move(__o), __padding);
828 __n -= __padding.size();
831 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
834 size_t __l, __r, __max;
835 if (__align == _Align_centre)
838 __r = __l + (__nfill & 1);
841 else if (__align == _Align_right)
854 using namespace __unicode;
855 if constexpr (__literal_encoding_is_unicode<_CharT>())
856 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
859 const char32_t __arr[1]{ __fill_char };
860 _Utf_view<_CharT, span<const char32_t, 1>> __v(__arr);
861 basic_string<_CharT> __padstr(__v.begin(), __v.end());
862 __padding = __padstr;
864 __out = __format::__write(
std::move(__out), __padding);
865 __out = __format::__write(
std::move(__out), __str);
867 __out = __format::__write(
std::move(__out), __padding);
871 if (__max < __buflen)
872 __padding.remove_suffix(__buflen - __max);
876 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
878 __out = __format::__write(
std::move(__out), __str);
886 template<
typename _CharT,
typename _Out>
887 _GLIBCXX_CONSTEXPR_FORMAT _Out
888 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
889 size_t __estimated_width,
890 basic_format_context<_Out, _CharT>& __fc,
891 const _Spec<_CharT>& __spec,
892 _Align __align = _Align_left)
894 size_t __width = __spec._M_get_width(__fc);
896 if (__width <= __estimated_width)
897 return __format::__write(__fc.out(), __str);
899 const size_t __nfill = __width - __estimated_width;
901 if (__spec._M_align != _Align_default)
902 __align = __spec._M_align;
904 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
908 template<
typename _CharT>
909 _GLIBCXX_CONSTEXPR_FORMAT
size_t
910 __truncate(basic_string_view<_CharT>& __s,
size_t __prec)
912 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
914 if (__prec != (
size_t)-1)
915 return __unicode::__truncate(__s, __prec);
917 return __unicode::__field_width(__s);
921 __s = __s.substr(0, __prec);
926 enum class _Term_char :
unsigned char {
931 using enum _Term_char;
933 template<
typename _CharT>
936 using _Str_view = basic_string_view<_CharT>;
940 {
return _GLIBCXX_WIDEN(
"\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
944 {
return _S_all().substr(0, 3); }
947 _Str_view _S_newline()
948 {
return _S_all().substr(3, 3); }
951 _Str_view _S_return()
952 {
return _S_all().substr(6, 3); }
955 _Str_view _S_bslash()
956 {
return _S_all().substr(9, 3); }
960 {
return _S_all().substr(12, 3); }
964 {
return _S_all().substr(15, 3); }
968 {
return _S_all().substr(18, 2); }
972 {
return _S_all().substr(20, 2); }
975 _Str_view _S_term(_Term_char __term)
982 return _S_quote().substr(0, 1);
984 return _S_apos().substr(0, 1);
986 __builtin_unreachable();
990 template<
typename _CharT>
993 using _Str_view = basic_string_view<_CharT>;
997 {
return _GLIBCXX_WIDEN(
"[]{}(), : "); }
1000 _Str_view _S_squares()
1001 {
return _S_all().substr(0, 2); }
1004 _Str_view _S_braces()
1005 {
return _S_all().substr(2, 2); }
1008 _Str_view _S_parens()
1009 {
return _S_all().substr(4, 2); }
1012 _Str_view _S_comma()
1013 {
return _S_all().substr(6, 2); }
1016 _Str_view _S_colon()
1017 {
return _S_all().substr(8, 2); }
1020 template<
typename _CharT>
1021 constexpr bool __should_escape_ascii(_CharT __c, _Term_char __term)
1023 using _Esc = _Escapes<_CharT>;
1026 case _Esc::_S_tab()[0]:
1027 case _Esc::_S_newline()[0]:
1028 case _Esc::_S_return()[0]:
1029 case _Esc::_S_bslash()[0]:
1031 case _Esc::_S_quote()[0]:
1032 return __term == _Term_quote;
1033 case _Esc::_S_apos()[0]:
1034 return __term == _Term_apos;
1036 return (__c >= 0 && __c < 0x20) || __c == 0x7f;
1041 constexpr bool __should_escape_unicode(
char32_t __c,
bool __prev_esc)
1043 if (__unicode::__should_escape_category(__c))
1047 return __unicode::__grapheme_cluster_break_property(__c)
1048 == __unicode::_Gcb_property::_Gcb_Extend;
1051 using uint_least32_t = __UINT_LEAST32_TYPE__;
1052 template<
typename _Out,
typename _CharT>
1053 _GLIBCXX_CONSTEXPR_FORMAT _Out
1054 __write_escape_seq(_Out __out, uint_least32_t __val,
1055 basic_string_view<_CharT> __prefix)
1057 constexpr size_t __max = 8;
1059 const string_view __narrow(
1061 std::__to_chars_i<uint_least32_t>(__buf, __buf + __max, __val, 16).ptr);
1063 __out = __format::__write(__out, __prefix);
1064 *__out = _Separators<_CharT>::_S_braces()[0];
1066 if constexpr (is_same_v<char, _CharT>)
1067 __out = __format::__write(__out, __narrow);
1068#ifdef _GLIBCXX_USE_WCHAR_T
1071 wchar_t __wbuf[__max];
1072 const size_t __n = __narrow.size();
1073 std::__to_wstring_numeric(__narrow.data(), __n, __wbuf);
1074 __out = __format::__write(__out, wstring_view(__wbuf, __n));
1077 *__out = _Separators<_CharT>::_S_braces()[1];
1081 template<
typename _Out,
typename _CharT>
1082 _GLIBCXX_CONSTEXPR_FORMAT _Out
1083 __write_escape_seqs(_Out __out, basic_string_view<_CharT> __units)
1085 using _UChar = make_unsigned_t<_CharT>;
1086 for (_CharT __c : __units)
1087 __out = __format::__write_escape_seq(
1088 __out,
static_cast<_UChar
>(__c), _Escapes<_CharT>::_S_x());
1092 template<
typename _Out,
typename _CharT>
1093 _GLIBCXX_CONSTEXPR_FORMAT _Out
1094 __write_escaped_char(_Out __out, _CharT __c)
1096 using _UChar = make_unsigned_t<_CharT>;
1097 using _Esc = _Escapes<_CharT>;
1100 case _Esc::_S_tab()[0]:
1101 return __format::__write(__out, _Esc::_S_tab().substr(1, 2));
1102 case _Esc::_S_newline()[0]:
1103 return __format::__write(__out, _Esc::_S_newline().substr(1, 2));
1104 case _Esc::_S_return()[0]:
1105 return __format::__write(__out, _Esc::_S_return().substr(1, 2));
1106 case _Esc::_S_bslash()[0]:
1107 return __format::__write(__out, _Esc::_S_bslash().substr(1, 2));
1108 case _Esc::_S_quote()[0]:
1109 return __format::__write(__out, _Esc::_S_quote().substr(1, 2));
1110 case _Esc::_S_apos()[0]:
1111 return __format::__write(__out, _Esc::_S_apos().substr(1, 2));
1113 return __format::__write_escape_seq(
1114 __out,
static_cast<_UChar
>(__c), _Esc::_S_u());
1118 template<
typename _CharT,
typename _Out>
1119 _GLIBCXX_CONSTEXPR_FORMAT _Out
1120 __write_escaped_ascii(_Out __out,
1121 basic_string_view<_CharT> __str,
1124 using _Str_view = basic_string_view<_CharT>;
1128 constexpr _Str_view __supported(_GLIBCXX_WIDEN(
1129 "ABCDEFGHIJKLMNOPQRSTUWXYZ"
1130 "abdeefghijklmnopqrstuwzyz"
1131 " !#$%&'()*+-./:;<=>?[]^_{|}~"
1132 "0123456789" "\t\n\r\\\"\'\0"
1134 if (__str.find_first_not_of(__supported) != _Str_view::npos)
1135#if __has_builtin(__builtin_constexpr_diag)
1136 __builtin_constexpr_diag (2,
"",
1137 "for non-Unicode literal encodings, only"
1138 " printable ASCII characters and standard"
1139 " escape sequencess can be escaped in constant"
1146 auto __first = __str.begin();
1147 auto const __last = __str.end();
1148 while (__first != __last)
1150 auto __print = __first;
1152 while (__print != __last
1153 && !__format::__should_escape_ascii(*__print, __term))
1156 if (__print != __first)
1157 __out = __format::__write(__out, _Str_view(__first, __print));
1159 if (__print == __last)
1163 __out = __format::__write_escaped_char(__out, *__first);
1169 template<
typename _CharT,
typename _Out>
1170 _GLIBCXX_CONSTEXPR_FORMAT _Out
1171 __write_escaped_unicode_part(_Out __out, basic_string_view<_CharT>& __str,
1172 bool& __prev_esc, _Term_char __term)
1174 using _Str_view = basic_string_view<_CharT>;
1175 using _Esc = _Escapes<_CharT>;
1177 static constexpr char32_t __replace = U
'\uFFFD';
1178 static constexpr _Str_view __replace_rep = []
1181 if constexpr (is_same_v<char, _CharT>)
1182 return "\xEF\xBF\xBD";
1187 __unicode::_Utf_view<char32_t, _Str_view> __v(
std::move(__str));
1190 auto __first = __v.begin();
1191 auto const __last = __v.end();
1192 while (__first != __last)
1194 bool __esc_ascii =
false;
1195 bool __esc_unicode =
false;
1196 bool __esc_replace =
false;
1197 auto __should_escape = [&](
auto const& __it)
1201 = __format::__should_escape_ascii(*__it.base(), __term);
1202 if (__format::__should_escape_unicode(*__it, __prev_esc))
1203 return __esc_unicode =
true;
1204 if (*__it == __replace)
1206 _Str_view __units(__it.base(), __it._M_units());
1207 return __esc_replace = (__units != __replace_rep);
1212 auto __print = __first;
1213 while (__print != __last && !__should_escape(__print))
1219 if (__print != __first)
1220 __out = __format::__write(__out, _Str_view(__first.base(), __print.base()));
1222 if (__print == __last)
1227 __out = __format::__write_escaped_char(__out, *__first.base());
1228 else if (__esc_unicode)
1229 __out = __format::__write_escape_seq(__out, *__first, _Esc::_S_u());
1231 else if (_Str_view __units(__first.base(), __first._M_units());
1232 __units.end() != __last.base())
1233 __out = __format::__write_escape_seqs(__out, __units);
1247 template<
typename _CharT,
typename _Out>
1248 _GLIBCXX_CONSTEXPR_FORMAT _Out
1249 __write_escaped_unicode(_Out __out, basic_string_view<_CharT> __str,
1252 bool __prev_escape =
true;
1253 __out = __format::__write_escaped_unicode_part(__out, __str,
1254 __prev_escape, __term);
1255 __out = __format::__write_escape_seqs(__out, __str);
1259 template<
typename _CharT,
typename _Out>
1260 _GLIBCXX_CONSTEXPR_FORMAT _Out
1261 __write_escaped(_Out __out, basic_string_view<_CharT> __str, _Term_char __term)
1263 __out = __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1265 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1266 __out = __format::__write_escaped_unicode(__out, __str, __term);
1267 else if constexpr (is_same_v<char, _CharT>
1268 && __unicode::__literal_encoding_is_extended_ascii())
1269 __out = __format::__write_escaped_ascii(__out, __str, __term);
1272 __out = __format::__write_escaped_ascii(__out, __str, __term);
1274 return __format::__write(__out, _Escapes<_CharT>::_S_term(__term));
1278 struct _Optional_locale
1280 [[__gnu__::__always_inline__]]
1281 _GLIBCXX_CONSTEXPR_FORMAT
1282 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
1284 _Optional_locale(
const locale& __loc) noexcept
1285 : _M_loc(__loc), _M_hasval(
true)
1288 _GLIBCXX_CONSTEXPR_FORMAT
1289 _Optional_locale(
const _Optional_locale& __l) noexcept
1290 : _M_dummy(), _M_hasval(__l._M_hasval)
1293 std::construct_at(&_M_loc, __l._M_loc);
1296 _GLIBCXX_CONSTEXPR_FORMAT
1298 operator=(
const _Optional_locale& __l)
noexcept
1303 _M_loc = __l._M_loc;
1310 else if (__l._M_hasval)
1312 std::construct_at(&_M_loc, __l._M_loc);
1318 _GLIBCXX_CONSTEXPR_FORMAT
1319 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
1322 operator=(locale&& __loc)
noexcept
1328 std::construct_at(&_M_loc,
std::move(__loc));
1339 std::construct_at(&_M_loc);
1345 _GLIBCXX_CONSTEXPR_FORMAT
bool
1346 has_value() const noexcept {
return _M_hasval; }
1349 char _M_dummy =
'\0';
1352 bool _M_hasval =
false;
1355 template<__
char _CharT>
1356 struct __formatter_str
1358 __formatter_str() =
default;
1361 __formatter_str(_Spec<_CharT> __spec) noexcept
1365 constexpr typename basic_format_parse_context<_CharT>::iterator
1366 parse(basic_format_parse_context<_CharT>& __pc)
1368 auto __first = __pc.begin();
1369 const auto __last = __pc.end();
1370 _Spec<_CharT> __spec{};
1372 auto __finalize = [
this, &__spec] {
1376 auto __finished = [&] {
1377 if (__first == __last || *__first ==
'}')
1388 __first = __spec._M_parse_fill_and_align(__first, __last);
1392 __first = __spec._M_parse_width(__first, __last, __pc);
1396 __first = __spec._M_parse_precision(__first, __last, __pc);
1400 if (*__first ==
's')
1402 __spec._M_type = _Pres_s;
1405#if __glibcxx_format_ranges
1406 else if (*__first ==
'?')
1408 __spec._M_debug =
true;
1416 __format::__failed_to_parse_format_spec();
1419 template<
typename _Out>
1420 _GLIBCXX_CONSTEXPR_FORMAT _Out
1421 format(basic_string_view<_CharT> __s,
1422 basic_format_context<_Out, _CharT>& __fc)
const
1424 if (_M_spec._M_debug)
1425 return _M_format_escaped(__s, __fc);
1427 if (_M_spec._M_width_kind == _WP_none
1428 && _M_spec._M_prec_kind == _WP_none)
1429 return __format::__write(__fc.out(), __s);
1431 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1432 const size_t __width = __format::__truncate(__s, __maxwidth);
1433 return __format::__write_padded_as_spec(__s, __width, __fc, _M_spec);
1436 template<
typename _Out>
1437 _GLIBCXX_CONSTEXPR_FORMAT _Out
1438 _M_format_escaped(basic_string_view<_CharT> __s,
1439 basic_format_context<_Out, _CharT>& __fc)
const
1441 const size_t __padwidth = _M_spec._M_get_width(__fc);
1442 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1443 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1445 const size_t __maxwidth = _M_spec._M_get_precision(__fc);
1446 const size_t __width = __truncate(__s, __maxwidth);
1448 if (__padwidth <= __width && _M_spec._M_prec_kind == _WP_none)
1449 return __format::__write_escaped(__fc.out(), __s, _Term_quote);
1454 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth, __maxwidth);
1455 __format::__write_escaped(__sink.out(), __s, _Term_quote);
1456 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1459#if __glibcxx_format_ranges
1460 template<ranges::input_range _Rg,
typename _Out>
1461 requires same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _CharT>
1462 _GLIBCXX_CONSTEXPR_FORMAT _Out
1463 _M_format_range(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
1465 using _Range = remove_reference_t<_Rg>;
1466 using _String_view = basic_string_view<_CharT>;
1467 if constexpr (ranges::contiguous_range<_Rg>)
1469 _String_view __str(ranges::data(__rg),
1470 size_t(ranges::distance(__rg)));
1471 return format(__str, __fc);
1473 else if constexpr (!is_const_v<_Range>
1474 && __simply_formattable_range<_Range, _CharT>)
1475 return _M_format_range<const _Range&>(__rg, __fc);
1476 else if constexpr (!is_lvalue_reference_v<_Rg>)
1477 return _M_format_range<_Range&>(__rg, __fc);
1480 auto __handle_debug = [
this, &__rg]<
typename _NOut>(_NOut __nout)
1482 if (!_M_spec._M_debug)
1483 return ranges::copy(__rg,
std::move(__nout)).out;
1485 _Escaping_sink<_NOut, _CharT>
1487 ranges::copy(__rg, __sink.out());
1488 return __sink._M_finish();
1491 const size_t __padwidth = _M_spec._M_get_width(__fc);
1492 if (__padwidth == 0 && _M_spec._M_prec_kind == _WP_none)
1493 return __handle_debug(__fc.out());
1495 _Padding_sink<_Out, _CharT>
1496 __sink(__fc.out(), __padwidth, _M_spec._M_get_precision(__fc));
1497 __handle_debug(__sink.out());
1498 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
1503 set_debug_format() noexcept
1504 { _M_spec._M_debug =
true; }
1508 _Spec<_CharT> _M_spec{};
1513 [[__gnu__::__always_inline__]]
1515 __toupper_numeric(
char __c)
1519 case 'a':
return 'A';
1520 case 'b':
return 'B';
1521 case 'c':
return 'C';
1522 case 'd':
return 'D';
1523 case 'e':
return 'E';
1524 case 'f':
return 'F';
1525 case 'i':
return 'I';
1526 case 'n':
return 'N';
1527 case 'p':
return 'P';
1528 case 'x':
return 'X';
1529 default:
return __c;
1533 template<__
char _CharT>
1534 struct __formatter_int
1538 static constexpr _Pres_type _AsInteger = _Pres_d;
1539 static constexpr _Pres_type _AsBool = _Pres_s;
1540 static constexpr _Pres_type _AsChar = _Pres_c;
1542 __formatter_int() =
default;
1545 __formatter_int(_Spec<_CharT> __spec) noexcept
1548 if (_M_spec._M_type == _Pres_none)
1549 _M_spec._M_type = _Pres_d;
1552 constexpr typename basic_format_parse_context<_CharT>::iterator
1553 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1555 _Spec<_CharT> __spec{};
1556 __spec._M_type = __type;
1558 const auto __last = __pc.end();
1559 auto __first = __pc.begin();
1561 auto __finalize = [
this, &__spec] {
1565 auto __finished = [&] {
1566 if (__first == __last || *__first ==
'}')
1577 __first = __spec._M_parse_fill_and_align(__first, __last);
1581 __first = __spec._M_parse_sign(__first, __last);
1585 __first = __spec._M_parse_alternate_form(__first, __last);
1589 __first = __spec._M_parse_zero_fill(__first, __last);
1593 __first = __spec._M_parse_width(__first, __last, __pc);
1597 __first = __spec._M_parse_locale(__first, __last);
1604 __spec._M_type = _Pres_b;
1608 __spec._M_type = _Pres_B;
1614 if (__type != _AsBool)
1616 __spec._M_type = _Pres_c;
1621 __spec._M_type = _Pres_d;
1625 __spec._M_type = _Pres_o;
1629 __spec._M_type = _Pres_x;
1633 __spec._M_type = _Pres_X;
1637 if (__type == _AsBool)
1639 __spec._M_type = _Pres_s;
1643#if __glibcxx_format_ranges
1645 if (__type == _AsChar)
1647 __spec._M_debug =
true;
1657 __format::__failed_to_parse_format_spec();
1660 template<
typename _Tp>
1661 constexpr typename basic_format_parse_context<_CharT>::iterator
1662 _M_parse(basic_format_parse_context<_CharT>& __pc)
1664 if constexpr (is_same_v<_Tp, bool>)
1666 auto __end = _M_do_parse(__pc, _AsBool);
1667 if (_M_spec._M_type == _Pres_s)
1668 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1669 || _M_spec._M_zero_fill)
1670 __throw_format_error(
"format error: format-spec contains "
1671 "invalid formatting options for "
1675 else if constexpr (__char<_Tp>)
1677 auto __end = _M_do_parse(__pc, _AsChar);
1678 if (_M_spec._M_type == _Pres_c)
1679 if (_M_spec._M_sign != _Sign_default || _M_spec._M_alt
1680 || _M_spec._M_zero_fill
1682 __throw_format_error(
"format error: format-spec contains "
1683 "invalid formatting options for "
1688 return _M_do_parse(__pc, _AsInteger);
1691 template<
typename _Int,
typename _Out>
1692 _GLIBCXX_CONSTEXPR_FORMAT
1693 typename basic_format_context<_Out, _CharT>::iterator
1694 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1696 if (_M_spec._M_type == _Pres_c)
1697 return _M_format_character(_S_to_character(__i), __fc);
1699 constexpr size_t __buf_size =
sizeof(_Int) * __CHAR_BIT__ + 3;
1700 char __buf[__buf_size];
1701 to_chars_result __res{};
1703 string_view __base_prefix;
1704 make_unsigned_t<_Int> __u;
1706 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1710 char* __start = __buf + 3;
1711 char*
const __end = __buf +
sizeof(__buf);
1712 char*
const __start_digits = __start;
1714 switch (_M_spec._M_type)
1718 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1719 __res = to_chars(__start, __end, __u, 2);
1723 return _M_format_character(_S_to_character(__i), __fc);
1730 __res = to_chars(__start, __end, __u, 10);
1734 __base_prefix =
"0";
1735 __res = to_chars(__start, __end, __u, 8);
1739 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1740 __res = to_chars(__start, __end, __u, 16);
1741 if (_M_spec._M_type == _Pres_X)
1742 for (
auto __p = __start; __p != __res.ptr; ++__p)
1743 *__p = __format::__toupper_numeric(*__p);
1747 if (_M_spec._M_alt && __base_prefix.size())
1749 __start -= __base_prefix.size();
1750 ranges::copy(__base_prefix, __start);
1752 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1754 string_view __narrow_str(__start, __res.ptr - __start);
1755 size_t __prefix_len = __start_digits - __start;
1756 if constexpr (is_same_v<char, _CharT>)
1757 return _M_format_int(__narrow_str, __prefix_len, __fc);
1758#ifdef _GLIBCXX_USE_WCHAR_T
1761 _CharT __wbuf[__buf_size];
1762 size_t __n = __narrow_str.size();
1765 std::__to_wstring_numeric(__narrow_str.data(), __n, __wbuf);
1766 return _M_format_int(basic_string_view<_CharT>(__wbuf, __n),
1767 __prefix_len, __fc);
1772 template<
typename _Out>
1773 _GLIBCXX_CONSTEXPR_FORMAT
1774 typename basic_format_context<_Out, _CharT>::iterator
1775 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1777 if (_M_spec._M_type == _Pres_c)
1778 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1779 if (_M_spec._M_type != _Pres_s)
1780 return format(
static_cast<unsigned char>(__i), __fc);
1782 basic_string<_CharT> __s;
1784 if (_M_spec._M_localized) [[unlikely]]
1787 __s = __i ? __np.truename() : __np.falsename();
1788 __est_width = __s.size();
1792 if constexpr (is_same_v<char, _CharT>)
1793 __s = __i ?
"true" :
"false";
1795 __s = __i ? L
"true" : L
"false";
1796 __est_width = __s.size();
1799 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1803 template<
typename _Out>
1804 _GLIBCXX_CONSTEXPR_FORMAT
1805 typename basic_format_context<_Out, _CharT>::iterator
1806 _M_format_character(_CharT __c,
1807 basic_format_context<_Out, _CharT>& __fc)
const
1809 basic_string_view<_CharT> __in(&__c, 1u);
1810 size_t __width = 1u;
1812 if constexpr (
sizeof(_CharT) > 1u &&
1813 __unicode::__literal_encoding_is_unicode<_CharT>())
1814 __width = __unicode::__field_width(__c);
1816 if (!_M_spec._M_debug)
1817 return __format::__write_padded_as_spec(__in, __width,
1821 if (_M_spec._M_get_width(__fc) <= __width)
1822 return __format::__write_escaped(__fc.out(), __in, _Term_apos);
1825 _Fixedbuf_sink<_CharT> __sink(__buf);
1826 __format::__write_escaped(__sink.out(), __in, _Term_apos);
1828 __in = __sink.view();
1829 if (__in[1] == _Escapes<_CharT>::_S_bslash()[0])
1830 __width = __in.size();
1831 return __format::__write_padded_as_spec(__in, __width,
1835 template<
typename _Int>
1836 static _GLIBCXX_CONSTEXPR_FORMAT _CharT
1837 _S_to_character(_Int __i)
1840 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1842 if (_Traits::__min <= __i && __i <= _Traits::__max)
1843 return static_cast<_CharT
>(__i);
1845 else if constexpr (is_signed_v<_Int>)
1847 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1848 return static_cast<_CharT
>(__i);
1850 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1851 return static_cast<_CharT
>(__i);
1852 __throw_format_error(
"format error: integer not representable as "
1856 template<
typename _Out>
1857 _GLIBCXX_CONSTEXPR_FORMAT
1858 typename basic_format_context<_Out, _CharT>::iterator
1859 _M_format_int(basic_string_view<_CharT> __str,
size_t __prefix_len,
1860 basic_format_context<_Out, _CharT>& __fc)
const
1862 size_t __width = _M_spec._M_get_width(__fc);
1863 if (_M_spec._M_localized)
1865 const auto& __l = __fc.locale();
1866 if (__l.name() !=
"C")
1868 auto& __np = use_facet<numpunct<_CharT>>(__l);
1869 string __grp = __np.grouping();
1872 size_t __n = __str.size() - __prefix_len;
1873 auto __p = (_CharT*)__builtin_alloca(2 * __n
1876 auto __s = __str.data();
1877 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1878 __s += __prefix_len;
1879 auto __end = std::__add_grouping(__p + __prefix_len,
1880 __np.thousands_sep(),
1884 __str = {__p, size_t(__end - __p)};
1889 if (__width <= __str.size())
1890 return __format::__write(__fc.out(), __str);
1892 char32_t __fill_char = _M_spec._M_fill;
1893 _Align __align = _M_spec._M_align;
1895 size_t __nfill = __width - __str.size();
1896 auto __out = __fc.out();
1897 if (__align == _Align_default)
1899 __align = _Align_right;
1900 if (_M_spec._M_zero_fill)
1902 __fill_char = _CharT(
'0');
1904 if (__prefix_len != 0)
1906 __out = __format::__write(
std::move(__out),
1907 __str.substr(0, __prefix_len));
1908 __str.remove_prefix(__prefix_len);
1912 __fill_char = _CharT(
' ');
1914 return __format::__write_padded(
std::move(__out), __str,
1915 __align, __nfill, __fill_char);
1918 _Spec<_CharT> _M_spec{};
1921#ifdef __BFLT16_DIG__
1922 using __bflt16_t =
decltype(0.0bf16);
1933#undef _GLIBCXX_FORMAT_F128
1935#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1938 using __flt128_t = __ieee128;
1939# define _GLIBCXX_FORMAT_F128 1
1941#ifdef __LONG_DOUBLE_IEEE128__
1945 to_chars(
char*,
char*, __ibm128)
noexcept
1946 __asm(
"_ZSt8to_charsPcS_e");
1949 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1950 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1953 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1954 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1955#elif __cplusplus == 202002L
1957 to_chars(
char*,
char*, __ieee128)
noexcept
1958 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1961 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1962 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1965 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1966 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1969#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1972 using __flt128_t =
long double;
1973# define _GLIBCXX_FORMAT_F128 2
1975#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1978 using __flt128_t = _Float128;
1979# define _GLIBCXX_FORMAT_F128 3
1981# if __cplusplus == 202002L
1985 to_chars(
char*,
char*, _Float128)
noexcept
1986# if _GLIBCXX_INLINE_VERSION
1987 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1989 __asm(
"_ZSt8to_charsPcS_DF128_");
1993 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1994# if _GLIBCXX_INLINE_VERSION
1995 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1997 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
2001 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
2002# if _GLIBCXX_INLINE_VERSION
2003 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
2005 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
2010 using std::to_chars;
2013 template<
typename _Tp>
2014 concept __formattable_float
2015 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
2016 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
2018 template<__
char _CharT>
2019 struct __formatter_fp
2021 constexpr typename basic_format_parse_context<_CharT>::iterator
2022 parse(basic_format_parse_context<_CharT>& __pc)
2024 _Spec<_CharT> __spec{};
2025 const auto __last = __pc.end();
2026 auto __first = __pc.begin();
2028 auto __finalize = [
this, &__spec] {
2032 auto __finished = [&] {
2033 if (__first == __last || *__first ==
'}')
2044 __first = __spec._M_parse_fill_and_align(__first, __last);
2048 __first = __spec._M_parse_sign(__first, __last);
2052 __first = __spec._M_parse_alternate_form(__first, __last);
2056 __first = __spec._M_parse_zero_fill(__first, __last);
2060 if (__first[0] !=
'.')
2062 __first = __spec._M_parse_width(__first, __last, __pc);
2067 __first = __spec._M_parse_precision(__first, __last, __pc);
2071 __first = __spec._M_parse_locale(__first, __last);
2078 __spec._M_type = _Pres_a;
2082 __spec._M_type = _Pres_A;
2086 __spec._M_type = _Pres_e;
2090 __spec._M_type = _Pres_E;
2094 __spec._M_type = _Pres_f;
2098 __spec._M_type = _Pres_F;
2102 __spec._M_type = _Pres_g;
2106 __spec._M_type = _Pres_G;
2114 __format::__failed_to_parse_format_spec();
2117 template<
typename _Fp,
typename _Out>
2118 typename basic_format_context<_Out, _CharT>::iterator
2119 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2123 to_chars_result __res{};
2126 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2128 __prec = _M_spec._M_get_precision(__fc);
2131 bool __upper =
false;
2132 bool __trailing_zeros =
false;
2134 size_t __offset = 1;
2136 switch (_M_spec._M_type)
2139 if (__builtin_isfinite(__v))
2145 __fmt = chars_format::hex;
2148 if (__builtin_isfinite(__v))
2153 __fmt = chars_format::hex;
2161 __fmt = chars_format::scientific;
2168 __fmt = chars_format::fixed;
2175 __trailing_zeros =
true;
2177 __fmt = chars_format::general;
2182 __fmt = chars_format::general;
2186 char* __start = __buf + __offset;
2187 char* __end = __buf +
sizeof(__buf);
2190 auto __to_chars = [&](
char* __b,
char* __e) {
2192 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2193 else if (__fmt != chars_format{})
2194 return __format::to_chars(__b, __e, __v, __fmt);
2196 return __format::to_chars(__b, __e, __v);
2200 __res = __to_chars(__start, __end);
2202 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2206 size_t __guess = 7 + __offset + __prec;
2207 if (__fmt == chars_format::fixed)
2209 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2210 || is_same_v<_Fp, long double>)
2215 if constexpr (is_same_v<_Fp, float>)
2216 __builtin_frexpf(__v, &__exp);
2217 else if constexpr (is_same_v<_Fp, double>)
2218 __builtin_frexp(__v, &__exp);
2219 else if constexpr (is_same_v<_Fp, long double>)
2220 __builtin_frexpl(__v, &__exp);
2222 __guess += 1U + __exp * 4004U / 13301U;
2225 __guess += numeric_limits<_Fp>::max_exponent10;
2227 if (__guess <=
sizeof(__buf)) [[unlikely]]
2228 __guess =
sizeof(__buf) * 2;
2237 auto __overwrite = [&__to_chars, &__res, __offset] (
char* __p,
size_t __n)
2239 __res = __to_chars(__p + __offset, __p + __n - __offset);
2240 return __res.ec == errc{} ? __res.ptr - __p : 0;
2245 __start = __dynbuf.
data() + __offset;
2246 __end = __dynbuf.
data() + __dynbuf.
size();
2248 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2254 if (__builtin_signbit(__v))
2255 ranges::copy(string_view(
"-0x"), __start);
2257 ranges::copy(string_view(
"0x"), __start);
2263 for (
char* __p = __start; __p != __res.ptr; ++__p)
2264 *__p = __format::__toupper_numeric(*__p);
2268 if (!__builtin_signbit(__v))
2270 if (_M_spec._M_sign == _Sign_plus)
2272 else if (_M_spec._M_sign == _Sign_space)
2278 string_view __narrow_str(__start, __res.ptr - __start);
2282 if (_M_spec._M_alt && __builtin_isfinite(__v))
2284 string_view __s = __narrow_str;
2288 size_t __d = __s.find(
'.');
2289 if (__d != __s.npos)
2291 __p = __s.find(__expc, __d + 1);
2292 if (__p == __s.npos)
2296 if (__trailing_zeros)
2299 if (__s[__offset] !=
'0')
2301 __sigfigs = __p - __offset - 1;
2306 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2311 __p = __s.find(__expc);
2312 if (__p == __s.npos)
2315 __sigfigs = __d - __offset;
2318 if (__trailing_zeros && __prec != 0)
2323 __z = __prec - __sigfigs;
2326 if (
size_t __extras =
int(__d == __p) + __z)
2328 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2332 __builtin_memmove(__start + __p + __extras,
2336 __start[__p++] =
'.';
2337 __builtin_memset(__start + __p,
'0', __z);
2338 __narrow_str = {__s.data(), __s.size() + __extras};
2342 __dynbuf.
reserve(__s.size() + __extras);
2343 if (__dynbuf.
empty())
2345 __dynbuf = __s.
substr(0, __p);
2349 __dynbuf.
append(__z,
'0');
2350 __dynbuf.
append(__s.substr(__p));
2354 __dynbuf.
insert(__p, __extras,
'0');
2356 __dynbuf[__p] =
'.';
2358 __narrow_str = __dynbuf;
2363 basic_string<_CharT> __wstr;
2364 basic_string_view<_CharT> __str;
2365 if constexpr (is_same_v<_CharT, char>)
2366 __str = __narrow_str;
2367#ifdef _GLIBCXX_USE_WCHAR_T
2372 __wstr = std::__to_wstring_numeric(__narrow_str);
2377 if (_M_spec._M_localized && __builtin_isfinite(__v))
2379 auto __s = _M_localize(__str, __expc, __offset, __fc.locale());
2384 size_t __width = _M_spec._M_get_width(__fc);
2386 if (__width <= __str.size())
2387 return __format::__write(__fc.out(), __str);
2389 char32_t __fill_char = _M_spec._M_fill;
2390 _Align __align = _M_spec._M_align;
2392 size_t __nfill = __width - __str.size();
2393 auto __out = __fc.out();
2394 if (__align == _Align_default)
2396 __align = _Align_right;
2397 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2399 __fill_char = _CharT(
'0');
2402 __out = __format::__write(__out, __str.substr(0, __offset));
2403 __str.remove_prefix(__offset);
2407 __fill_char = _CharT(
' ');
2409 return __format::__write_padded(
std::move(__out), __str,
2410 __align, __nfill, __fill_char);
2414 basic_string<_CharT>
2415 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2416 int __offset,
const locale& __loc)
const
2418 basic_string<_CharT> __lstr;
2420 if (__loc == locale::classic())
2423 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2424 const _CharT __point = __np.decimal_point();
2425 const string __grp = __np.grouping();
2427 _CharT __dot, __exp;
2428 if constexpr (is_same_v<_CharT, char>)
2451 __builtin_unreachable();
2455 if (__grp.empty() && __point == __dot)
2458 size_t __d = __str.find(__dot);
2459 size_t __e = min(__d, __str.find(__exp));
2460 if (__e == __str.npos)
2462 const size_t __r = __str.size() - __e;
2463 auto __overwrite = [&](_CharT* __p, size_t) {
2465 ranges::copy_n(__str.data(), __offset, __p);
2467 auto __end = std::__add_grouping(__p + __offset, __np.thousands_sep(),
2468 __grp.data(), __grp.size(),
2469 __str.data() + __offset,
2470 __str.data() + __e);
2473 if (__d != __str.npos)
2479 const size_t __rlen = __str.size() - __e;
2481 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2484 return (__end - __p);
2486 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2490 _Spec<_CharT> _M_spec{};
2493 template<__format::__
char _CharT>
2494 struct __formatter_ptr
2497 __formatter_ptr() noexcept
2500 _M_spec._M_type = _Pres_x;
2501 _M_spec._M_alt =
true;
2505 __formatter_ptr(_Spec<_CharT> __spec) noexcept
2507 { _M_set_default(); }
2509 constexpr typename basic_format_parse_context<_CharT>::iterator
2510 parse(basic_format_parse_context<_CharT>& __pc)
2512 __format::_Spec<_CharT> __spec{};
2513 const auto __last = __pc.end();
2514 auto __first = __pc.begin();
2516 auto __finalize = [
this, &__spec] {
2521 auto __finished = [&] {
2522 if (__first == __last || *__first ==
'}')
2533 __first = __spec._M_parse_fill_and_align(__first, __last);
2539#if __glibcxx_format >= 202304L
2540 __first = __spec._M_parse_zero_fill(__first, __last);
2545 __first = __spec._M_parse_width(__first, __last, __pc);
2549 if (*__first ==
'p')
2551 __spec._M_type = _Pres_x;
2552 __spec._M_alt =
true;
2555#if __glibcxx_format >= 202304L
2556 else if (*__first ==
'P')
2558 __spec._M_type = _Pres_X;
2559 __spec._M_alt =
true;
2567 __format::__failed_to_parse_format_spec();
2570 template<
typename _Out>
2571 _GLIBCXX_CONSTEXPR_FORMAT
2572 typename basic_format_context<_Out, _CharT>::iterator
2573 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2576 ?
reinterpret_cast<__UINTPTR_TYPE__
>(__v)
2577 : static_cast<__UINTPTR_TYPE__>(0);
2578 return __formatter_int<_CharT>(_M_spec).format(__u, __fc);
2582 [[__gnu__::__always_inline__]]
2586 if (_M_spec._M_type == _Pres_none)
2588 _M_spec._M_type = _Pres_x;
2589 _M_spec._M_alt =
true;
2593 __format::_Spec<_CharT> _M_spec;
2600 template<__format::__
char _CharT>
2601 struct formatter<_CharT, _CharT>
2603 formatter() =
default;
2605 constexpr typename basic_format_parse_context<_CharT>::iterator
2606 parse(basic_format_parse_context<_CharT>& __pc)
2608 return _M_f.template _M_parse<_CharT>(__pc);
2611 template<
typename _Out>
2612 _GLIBCXX_CONSTEXPR_FORMAT
2613 typename basic_format_context<_Out, _CharT>::iterator
2614 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2616 if (_M_f._M_spec._M_type == __format::_Pres_c)
2617 return _M_f._M_format_character(__u, __fc);
2619 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2622#if __glibcxx_format_ranges
2624 set_debug_format() noexcept
2625 { _M_f._M_spec._M_debug =
true; }
2629 __format::__formatter_int<_CharT> _M_f;
2632#if __glibcxx_print >= 202403L
2633 template<__format::__
char _CharT>
2634 constexpr bool enable_nonlocking_formatter_optimization<_CharT> =
true;
2637#ifdef _GLIBCXX_USE_WCHAR_T
2640 struct formatter<char, wchar_t>
2642 formatter() =
default;
2644 constexpr typename basic_format_parse_context<wchar_t>::iterator
2645 parse(basic_format_parse_context<wchar_t>& __pc)
2647 return _M_f._M_parse<
char>(__pc);
2650 template<
typename _Out>
2651 _GLIBCXX_CONSTEXPR_FORMAT
2652 typename basic_format_context<_Out, wchar_t>::iterator
2653 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2655 if (_M_f._M_spec._M_type == __format::_Pres_c)
2656 return _M_f._M_format_character(__u, __fc);
2658 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2661#if __glibcxx_format_ranges
2663 set_debug_format() noexcept
2664 { _M_f._M_spec._M_debug =
true; }
2668 __format::__formatter_int<wchar_t> _M_f;
2675 template<__format::__
char _CharT>
2676 struct formatter<_CharT*, _CharT>
2678 formatter() =
default;
2680 [[__gnu__::__always_inline__]]
2681 constexpr typename basic_format_parse_context<_CharT>::iterator
2682 parse(basic_format_parse_context<_CharT>& __pc)
2683 {
return _M_f.parse(__pc); }
2685 template<
typename _Out>
2686 [[__gnu__::__nonnull__]]
2687 _GLIBCXX_CONSTEXPR_FORMAT
2688 typename basic_format_context<_Out, _CharT>::iterator
2689 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2690 {
return _M_f.format(__u, __fc); }
2692#if __glibcxx_format_ranges
2693 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2697 __format::__formatter_str<_CharT> _M_f;
2700#if __glibcxx_print >= 202403L
2701 template<__format::__
char _CharT>
2702 constexpr bool enable_nonlocking_formatter_optimization<_CharT*> =
true;
2705 template<__format::__
char _CharT>
2706 struct formatter<const _CharT*, _CharT>
2708 formatter() =
default;
2710 [[__gnu__::__always_inline__]]
2711 constexpr typename basic_format_parse_context<_CharT>::iterator
2712 parse(basic_format_parse_context<_CharT>& __pc)
2713 {
return _M_f.parse(__pc); }
2715 template<
typename _Out>
2716 [[__gnu__::__nonnull__]]
2717 _GLIBCXX_CONSTEXPR_FORMAT
2718 typename basic_format_context<_Out, _CharT>::iterator
2719 format(
const _CharT* __u,
2720 basic_format_context<_Out, _CharT>& __fc)
const
2721 {
return _M_f.format(__u, __fc); }
2723#if __glibcxx_format_ranges
2724 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2728 __format::__formatter_str<_CharT> _M_f;
2731#if __glibcxx_print >= 202403L
2732 template<__format::__
char _CharT>
2734 enable_nonlocking_formatter_optimization<const _CharT*> =
true;
2737 template<__format::__
char _CharT,
size_t _Nm>
2738 struct formatter<_CharT[_Nm], _CharT>
2740 formatter() =
default;
2742 [[__gnu__::__always_inline__]]
2743 constexpr typename basic_format_parse_context<_CharT>::iterator
2744 parse(basic_format_parse_context<_CharT>& __pc)
2745 {
return _M_f.parse(__pc); }
2747 template<
typename _Out>
2748 _GLIBCXX_CONSTEXPR_FORMAT
2749 typename basic_format_context<_Out, _CharT>::iterator
2750 format(
const _CharT (&__u)[_Nm],
2751 basic_format_context<_Out, _CharT>& __fc)
const
2752 {
return _M_f.format({__u, _Nm}, __fc); }
2754#if __glibcxx_format_ranges
2755 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2759 __format::__formatter_str<_CharT> _M_f;
2762#if __glibcxx_print >= 202403L
2763 template<__format::__
char _CharT,
size_t _Nm>
2764 constexpr bool enable_nonlocking_formatter_optimization<_CharT[_Nm]> =
true;
2767 template<
typename _Traits,
typename _Alloc>
2768 struct formatter<
basic_string<char, _Traits, _Alloc>, char>
2770 formatter() =
default;
2772 [[__gnu__::__always_inline__]]
2773 constexpr typename basic_format_parse_context<char>::iterator
2774 parse(basic_format_parse_context<char>& __pc)
2775 {
return _M_f.parse(__pc); }
2777 template<
typename _Out>
2778 _GLIBCXX_CONSTEXPR_FORMAT
2779 typename basic_format_context<_Out, char>::iterator
2780 format(
const basic_string<char, _Traits, _Alloc>& __u,
2781 basic_format_context<_Out, char>& __fc)
const
2782 {
return _M_f.format(__u, __fc); }
2784#if __glibcxx_format_ranges
2785 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2789 __format::__formatter_str<char> _M_f;
2792#if __glibcxx_print >= 202403L
2793 template<
typename _Tr,
typename _Alloc>
2795 enable_nonlocking_formatter_optimization<basic_string<char, _Tr, _Alloc>>
2799#ifdef _GLIBCXX_USE_WCHAR_T
2800 template<
typename _Traits,
typename _Alloc>
2801 struct formatter<
basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2803 formatter() =
default;
2805 [[__gnu__::__always_inline__]]
2806 constexpr typename basic_format_parse_context<wchar_t>::iterator
2807 parse(basic_format_parse_context<wchar_t>& __pc)
2808 {
return _M_f.parse(__pc); }
2810 template<
typename _Out>
2811 _GLIBCXX_CONSTEXPR_FORMAT
2812 typename basic_format_context<_Out, wchar_t>::iterator
2813 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2814 basic_format_context<_Out, wchar_t>& __fc)
const
2815 {
return _M_f.format(__u, __fc); }
2817#if __glibcxx_format_ranges
2818 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2822 __format::__formatter_str<wchar_t> _M_f;
2825#if __glibcxx_print >= 202403L
2826 template<
typename _Tr,
typename _Alloc>
2828 enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Tr, _Alloc>>
2834 template<
typename _Traits>
2837 formatter() =
default;
2839 [[__gnu__::__always_inline__]]
2840 constexpr typename basic_format_parse_context<char>::iterator
2841 parse(basic_format_parse_context<char>& __pc)
2842 {
return _M_f.parse(__pc); }
2844 template<
typename _Out>
2845 _GLIBCXX_CONSTEXPR_FORMAT
2846 typename basic_format_context<_Out, char>::iterator
2847 format(basic_string_view<char, _Traits> __u,
2848 basic_format_context<_Out, char>& __fc)
const
2849 {
return _M_f.format(__u, __fc); }
2851#if __glibcxx_format_ranges
2852 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2856 __format::__formatter_str<char> _M_f;
2859#if __glibcxx_print >= 202403L
2860 template<
typename _Tr>
2862 enable_nonlocking_formatter_optimization<basic_string_view<char, _Tr>>
2866#ifdef _GLIBCXX_USE_WCHAR_T
2867 template<
typename _Traits>
2870 formatter() =
default;
2872 [[__gnu__::__always_inline__]]
2873 constexpr typename basic_format_parse_context<wchar_t>::iterator
2874 parse(basic_format_parse_context<wchar_t>& __pc)
2875 {
return _M_f.parse(__pc); }
2877 template<
typename _Out>
2878 _GLIBCXX_CONSTEXPR_FORMAT
2879 typename basic_format_context<_Out, wchar_t>::iterator
2880 format(basic_string_view<wchar_t, _Traits> __u,
2881 basic_format_context<_Out, wchar_t>& __fc)
const
2882 {
return _M_f.format(__u, __fc); }
2884#if __glibcxx_format_ranges
2885 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2889 __format::__formatter_str<wchar_t> _M_f;
2892#if __glibcxx_print >= 202403L
2893 template<
typename _Tr>
2895 enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Tr>>
2906 template<
typename _Tp>
2907 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2909#if defined __SIZEOF_INT128__
2910 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2911 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2915 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2916 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2917#ifdef _GLIBCXX_USE_CHAR8_T
2918 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2920 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2921 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2923 template<
typename _Tp>
2924 concept __formattable_integer = __is_formattable_integer<_Tp>;
2929 template<__format::__formattable_
integer _Tp, __format::__
char _CharT>
2930 struct formatter<_Tp, _CharT>
2932 formatter() =
default;
2934 [[__gnu__::__always_inline__]]
2935 constexpr typename basic_format_parse_context<_CharT>::iterator
2936 parse(basic_format_parse_context<_CharT>& __pc)
2938 return _M_f.template _M_parse<_Tp>(__pc);
2941 template<
typename _Out>
2942 _GLIBCXX_CONSTEXPR_FORMAT
2943 typename basic_format_context<_Out, _CharT>::iterator
2944 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2945 {
return _M_f.format(__u, __fc); }
2948 __format::__formatter_int<_CharT> _M_f;
2951#if __glibcxx_print >= 202403L
2952 template<__format::__formattable_
integer _Tp>
2954 enable_nonlocking_formatter_optimization<_Tp> =
true;
2957#if defined __glibcxx_to_chars
2959 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2960 struct formatter<_Tp, _CharT>
2962 formatter() =
default;
2964 [[__gnu__::__always_inline__]]
2965 constexpr typename basic_format_parse_context<_CharT>::iterator
2966 parse(basic_format_parse_context<_CharT>& __pc)
2967 {
return _M_f.parse(__pc); }
2969 template<
typename _Out>
2970 typename basic_format_context<_Out, _CharT>::iterator
2971 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2972 {
return _M_f.format(__u, __fc); }
2975 __format::__formatter_fp<_CharT> _M_f;
2978#if __glibcxx_print >= 202403L
2979 template<__format::__formattable_
float _Tp>
2981 enable_nonlocking_formatter_optimization<_Tp> =
true;
2984#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2986 template<__format::__
char _CharT>
2987 struct formatter<long double, _CharT>
2989 formatter() =
default;
2991 [[__gnu__::__always_inline__]]
2992 constexpr typename basic_format_parse_context<_CharT>::iterator
2993 parse(basic_format_parse_context<_CharT>& __pc)
2994 {
return _M_f.parse(__pc); }
2996 template<
typename _Out>
2997 typename basic_format_context<_Out, _CharT>::iterator
2998 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2999 {
return _M_f.format((
double)__u, __fc); }
3002 __format::__formatter_fp<_CharT> _M_f;
3006#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3008 template<__format::__
char _CharT>
3009 struct formatter<_Float16, _CharT>
3011 formatter() =
default;
3013 [[__gnu__::__always_inline__]]
3014 constexpr typename basic_format_parse_context<_CharT>::iterator
3015 parse(basic_format_parse_context<_CharT>& __pc)
3016 {
return _M_f.parse(__pc); }
3018 template<
typename _Out>
3019 typename basic_format_context<_Out, _CharT>::iterator
3020 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
3021 {
return _M_f.format((
float)__u, __fc); }
3024 __format::__formatter_fp<_CharT> _M_f;
3028#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3030 template<__format::__
char _CharT>
3031 struct formatter<_Float32, _CharT>
3033 formatter() =
default;
3035 [[__gnu__::__always_inline__]]
3036 constexpr typename basic_format_parse_context<_CharT>::iterator
3037 parse(basic_format_parse_context<_CharT>& __pc)
3038 {
return _M_f.parse(__pc); }
3040 template<
typename _Out>
3041 typename basic_format_context<_Out, _CharT>::iterator
3042 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
3043 {
return _M_f.format((
float)__u, __fc); }
3046 __format::__formatter_fp<_CharT> _M_f;
3050#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3052 template<__format::__
char _CharT>
3053 struct formatter<_Float64, _CharT>
3055 formatter() =
default;
3057 [[__gnu__::__always_inline__]]
3058 constexpr typename basic_format_parse_context<_CharT>::iterator
3059 parse(basic_format_parse_context<_CharT>& __pc)
3060 {
return _M_f.parse(__pc); }
3062 template<
typename _Out>
3063 typename basic_format_context<_Out, _CharT>::iterator
3064 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
3065 {
return _M_f.format((
double)__u, __fc); }
3068 __format::__formatter_fp<_CharT> _M_f;
3072#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128
3074 template<__format::__
char _CharT>
3075 struct formatter<_Float128, _CharT>
3077 formatter() =
default;
3079 [[__gnu__::__always_inline__]]
3080 constexpr typename basic_format_parse_context<_CharT>::iterator
3081 parse(basic_format_parse_context<_CharT>& __pc)
3082 {
return _M_f.parse(__pc); }
3084 template<
typename _Out>
3085 typename basic_format_context<_Out, _CharT>::iterator
3086 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3087 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3090 __format::__formatter_fp<_CharT> _M_f;
3094#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128 == 2
3097 template<__format::__
char _CharT>
3098 struct formatter<__float128, _CharT>
3100 formatter() =
default;
3102 [[__gnu__::__always_inline__]]
3103 constexpr typename basic_format_parse_context<_CharT>::iterator
3104 parse(basic_format_parse_context<_CharT>& __pc)
3105 {
return _M_f.parse(__pc); }
3107 template<
typename _Out>
3108 typename basic_format_context<_Out, _CharT>::iterator
3109 format(__float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
3110 {
return _M_f.format((__format::__flt128_t)__u, __fc); }
3113 __format::__formatter_fp<_CharT> _M_f;
3117#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3119 template<__format::__
char _CharT>
3120 struct formatter<__format::__bflt16_t, _CharT>
3122 formatter() =
default;
3124 [[__gnu__::__always_inline__]]
3125 constexpr typename basic_format_parse_context<_CharT>::iterator
3126 parse(basic_format_parse_context<_CharT>& __pc)
3127 {
return _M_f.parse(__pc); }
3129 template<
typename _Out>
3130 typename basic_format_context<_Out, _CharT>::iterator
3131 format(__gnu_cxx::__bfloat16_t __u,
3132 basic_format_context<_Out, _CharT>& __fc)
const
3133 {
return _M_f.format((
float)__u, __fc); }
3136 __format::__formatter_fp<_CharT> _M_f;
3144 template<__format::__
char _CharT>
3145 struct formatter<const void*, _CharT>
3147 formatter() =
default;
3149 constexpr typename basic_format_parse_context<_CharT>::iterator
3150 parse(basic_format_parse_context<_CharT>& __pc)
3151 {
return _M_f.parse(__pc); }
3153 template<
typename _Out>
3154 _GLIBCXX_CONSTEXPR_FORMAT
3155 typename basic_format_context<_Out, _CharT>::iterator
3156 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3157 {
return _M_f.format(__v, __fc); }
3160 __format::__formatter_ptr<_CharT> _M_f;
3163#if __glibcxx_print >= 202403L
3165 inline constexpr bool
3166 enable_nonlocking_formatter_optimization<const void*> =
true;
3169 template<__format::__
char _CharT>
3170 struct formatter<void*, _CharT>
3172 formatter() =
default;
3174 [[__gnu__::__always_inline__]]
3175 constexpr typename basic_format_parse_context<_CharT>::iterator
3176 parse(basic_format_parse_context<_CharT>& __pc)
3177 {
return _M_f.parse(__pc); }
3179 template<
typename _Out>
3180 _GLIBCXX_CONSTEXPR_FORMAT
3181 typename basic_format_context<_Out, _CharT>::iterator
3182 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
3183 {
return _M_f.format(__v, __fc); }
3186 __format::__formatter_ptr<_CharT> _M_f;
3189#if __glibcxx_print >= 202403l
3191 inline constexpr bool
3192 enable_nonlocking_formatter_optimization<void*> =
true;
3195 template<__format::__
char _CharT>
3196 struct formatter<nullptr_t, _CharT>
3198 formatter() =
default;
3200 [[__gnu__::__always_inline__]]
3201 constexpr typename basic_format_parse_context<_CharT>::iterator
3202 parse(basic_format_parse_context<_CharT>& __pc)
3203 {
return _M_f.parse(__pc); }
3205 template<
typename _Out>
3206 _GLIBCXX_CONSTEXPR_FORMAT
3207 typename basic_format_context<_Out, _CharT>::iterator
3208 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3209 {
return _M_f.format(
nullptr, __fc); }
3212 __format::__formatter_ptr<_CharT> _M_f;
3216#if __glibcxx_print >= 202403L
3218 inline constexpr bool
3219 enable_nonlocking_formatter_optimization<nullptr_t> =
true;
3222#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3226 struct __formatter_disabled
3228 __formatter_disabled() =
delete;
3229 __formatter_disabled(
const __formatter_disabled&) =
delete;
3230 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3234 struct formatter<char*,
wchar_t>
3235 :
private __formatter_disabled { };
3237 struct formatter<const char*,
wchar_t>
3238 :
private __formatter_disabled { };
3239 template<
size_t _Nm>
3240 struct formatter<char[_Nm], wchar_t>
3241 :
private __formatter_disabled { };
3242 template<
class _Traits,
class _Allocator>
3243 struct formatter<
basic_string<char, _Traits, _Allocator>, wchar_t>
3244 :
private __formatter_disabled { };
3245 template<
class _Traits>
3247 :
private __formatter_disabled { };
3252 template<
typename _Out>
3253 struct format_to_n_result
3256 iter_difference_t<_Out> size;
3259_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3260template<
typename,
typename>
class vector;
3261_GLIBCXX_END_NAMESPACE_CONTAINER
3266 template<
typename _CharT>
3270 using iterator_category = output_iterator_tag;
3271 using value_type = void;
3272 using difference_type = ptrdiff_t;
3273 using pointer = void;
3274 using reference = void;
3276 _Drop_iter() =
default;
3277 _Drop_iter(
const _Drop_iter&) =
default;
3278 _Drop_iter& operator=(
const _Drop_iter&) =
default;
3280 [[__gnu__::__always_inline__]]
3281 constexpr _Drop_iter&
3282 operator=(_CharT __c)
3285 [[__gnu__::__always_inline__]]
3286 constexpr _Drop_iter&
3287 operator=(basic_string_view<_CharT> __s)
3290 [[__gnu__::__always_inline__]]
3291 constexpr _Drop_iter&
3294 [[__gnu__::__always_inline__]]
3295 constexpr _Drop_iter&
3296 operator++() {
return *
this; }
3298 [[__gnu__::__always_inline__]]
3299 constexpr _Drop_iter
3300 operator++(
int) {
return *
this; }
3303 template<
typename _CharT>
3306 _Sink<_CharT>* _M_sink =
nullptr;
3309 using iterator_category = output_iterator_tag;
3310 using value_type = void;
3311 using difference_type = ptrdiff_t;
3312 using pointer = void;
3313 using reference = void;
3315 _Sink_iter() =
default;
3316 _Sink_iter(
const _Sink_iter&) =
default;
3317 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3319 [[__gnu__::__always_inline__]]
3321 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3323 [[__gnu__::__always_inline__]]
3324 constexpr _Sink_iter&
3325 operator=(_CharT __c)
3327 _M_sink->_M_write(__c);
3331 [[__gnu__::__always_inline__]]
3332 constexpr _Sink_iter&
3333 operator=(basic_string_view<_CharT> __s)
3335 _M_sink->_M_write(__s);
3339 [[__gnu__::__always_inline__]]
3340 constexpr _Sink_iter&
3343 [[__gnu__::__always_inline__]]
3344 constexpr _Sink_iter&
3345 operator++() {
return *
this; }
3347 [[__gnu__::__always_inline__]]
3348 constexpr _Sink_iter
3349 operator++(
int) {
return *
this; }
3351 _GLIBCXX_CONSTEXPR_FORMAT
auto
3352 _M_reserve(
size_t __n)
const
3353 {
return _M_sink->_M_reserve(__n); }
3355 _GLIBCXX_CONSTEXPR_FORMAT
bool
3356 _M_discarding()
const
3357 {
return _M_sink->_M_discarding(); }
3363 template<
typename _CharT>
3366 friend class _Sink_iter<_CharT>;
3368 span<_CharT> _M_span;
3369 typename span<_CharT>::iterator _M_next;
3375 virtual void _M_overflow() = 0;
3379 [[__gnu__::__always_inline__]]
3381 _Sink(span<_CharT> __span) noexcept
3382 : _M_span(__span), _M_next(__span.begin())
3386 [[__gnu__::__always_inline__]]
3387 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3388 _M_used() const noexcept
3389 {
return _M_span.first(_M_next - _M_span.begin()); }
3392 [[__gnu__::__always_inline__]]
3393 constexpr span<_CharT>
3394 _M_unused() const noexcept
3395 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3398 [[__gnu__::__always_inline__]]
3400 _M_rewind() noexcept
3401 { _M_next = _M_span.begin(); }
3404 _GLIBCXX_CONSTEXPR_FORMAT
void
3405 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3408 _M_next = __s.begin() + __pos;
3413 _M_write(_CharT __c)
3416 if (_M_next - _M_span.begin() ==
std::ssize(_M_span)) [[unlikely]]
3421 _M_write(basic_string_view<_CharT> __s)
3423 span __to = _M_unused();
3424 while (__to.size() <= __s.size())
3426 __s.copy(__to.data(), __to.size());
3427 _M_next += __to.size();
3428 __s.remove_prefix(__to.size());
3434 __s.copy(__to.data(), __s.size());
3435 _M_next += __s.size();
3444 _GLIBCXX_CONSTEXPR_FORMAT
3445 explicit operator bool() const noexcept {
return _M_sink; }
3447 _GLIBCXX_CONSTEXPR_FORMAT _CharT*
3448 get() const noexcept {
return _M_sink->_M_next.operator->(); }
3450 _GLIBCXX_CONSTEXPR_FORMAT
void
3451 _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3459 _GLIBCXX_CONSTEXPR_FORMAT
virtual _Reservation
3460 _M_reserve(
size_t __n)
3462 if (__n <= _M_unused().size())
3465 if (__n <= _M_span.size())
3468 if (__n <= _M_unused().size())
3476 _GLIBCXX_CONSTEXPR_FORMAT
virtual void
3481 _GLIBCXX_CONSTEXPR_FORMAT
virtual bool
3482 _M_discarding()
const
3486 _Sink(
const _Sink&) =
delete;
3487 _Sink& operator=(
const _Sink&) =
delete;
3489 [[__gnu__::__always_inline__]]
3490 constexpr _Sink_iter<_CharT>
3492 {
return _Sink_iter<_CharT>(*
this); }
3496 template<
typename _CharT>
3497 class _Fixedbuf_sink final :
public _Sink<_CharT>
3499 _GLIBCXX_CONSTEXPR_FORMAT
void
3500 _M_overflow()
override
3502 __glibcxx_assert(
false);
3507 [[__gnu__::__always_inline__]]
3509 _Fixedbuf_sink(span<_CharT> __buf)
3510 : _Sink<_CharT>(__buf)
3513 constexpr basic_string_view<_CharT>
3516 auto __s = this->_M_used();
3517 return basic_string_view<_CharT>(__s.data(), __s.size());
3522 template<
typename _CharT>
3523 class _Buf_sink :
public _Sink<_CharT>
3526 _CharT _M_buf[__stackbuf_size<_CharT>];
3528 [[__gnu__::__always_inline__]]
3530 _Buf_sink() noexcept
3531 : _Sink<_CharT>(_M_buf)
3535 using _GLIBCXX_STD_C::vector;
3539 template<
typename _Seq>
3540 class _Seq_sink :
public _Buf_sink<typename _Seq::value_type>
3542 using _CharT =
typename _Seq::value_type;
3547 _GLIBCXX_CONSTEXPR_FORMAT
void
3548 _M_overflow()
override
3550 auto __s = this->_M_used();
3551 if (__s.empty()) [[unlikely]]
3555 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3557 if constexpr (__is_specialization_of<_Seq, basic_string>)
3558 _M_seq.append(__s.data(), __s.size());
3560 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3566 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3567 _M_reserve(
size_t __n)
override
3576 if constexpr (__is_specialization_of<_Seq, basic_string>
3577 || __is_specialization_of<_Seq, vector>)
3580 if (this->_M_used().size()) [[unlikely]]
3581 _Seq_sink::_M_overflow();
3584 const auto __sz = _M_seq.size();
3585 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3586 _M_seq.__resize_and_overwrite(__sz + __n,
3587 [](
auto,
auto __n2) {
3591 _M_seq.resize(__sz + __n);
3595 this->_M_reset(_M_seq, __sz);
3599 return _Sink<_CharT>::_M_reserve(__n);
3602 _GLIBCXX_CONSTEXPR_FORMAT
void
3603 _M_bump(
size_t __n)
override
3605 if constexpr (__is_specialization_of<_Seq, basic_string>
3606 || __is_specialization_of<_Seq, vector>)
3608 auto __s = this->_M_used();
3609 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3611 _M_seq.resize(__s.size() + __n);
3613 this->_M_reset(this->_M_buf);
3617 _GLIBCXX_CONSTEXPR_FORMAT
void
3618 _M_trim(span<const _CharT> __s)
3619 requires __is_specialization_of<_Seq, basic_string>
3621 _GLIBCXX_DEBUG_ASSERT(__s.data() == this->_M_buf
3622 || __s.data() == _M_seq.data());
3623 if (__s.data() == _M_seq.data())
3624 _M_seq.resize(__s.size());
3626 this->_M_reset(this->_M_buf, __s.size());
3633 [[__gnu__::__always_inline__]]
3634 _GLIBCXX_CONSTEXPR_FORMAT
3635 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3638 _GLIBCXX_CONSTEXPR_FORMAT
3639 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3640 : _M_seq(std::move(__s))
3643 using _Sink<_CharT>::out;
3645 _GLIBCXX_CONSTEXPR_FORMAT _Seq
3648 if (this->_M_used().size() != 0)
3649 _Seq_sink::_M_overflow();
3655 _GLIBCXX_CONSTEXPR_FORMAT span<_CharT>
3658 auto __s = this->_M_used();
3661 if (__s.size() != 0)
3662 _Seq_sink::_M_overflow();
3668 _GLIBCXX_CONSTEXPR_FORMAT basic_string_view<_CharT>
3671 auto __span = _M_span();
3672 return basic_string_view<_CharT>(__span.data(), __span.size());
3676 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3678 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3684 template<
typename _CharT,
typename _OutIter>
3685 class _Iter_sink :
public _Buf_sink<_CharT>
3688 iter_difference_t<_OutIter> _M_max;
3691 size_t _M_count = 0;
3693 _GLIBCXX_CONSTEXPR_FORMAT
void
3694 _M_overflow()
override
3696 auto __s = this->_M_used();
3698 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3699 else if (_M_count <
static_cast<size_t>(_M_max))
3701 auto __max = _M_max - _M_count;
3702 span<_CharT> __first;
3703 if (__max < __s.size())
3704 __first = __s.first(
static_cast<size_t>(__max));
3707 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3710 _M_count += __s.size();
3713 _GLIBCXX_CONSTEXPR_FORMAT
bool
3714 _M_discarding()
const override
3722 [[__gnu__::__always_inline__]]
3723 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3724 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3725 : _M_out(std::move(__out)), _M_max(__max)
3728 using _Sink<_CharT>::out;
3730 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3733 if (this->_M_used().size() != 0)
3734 _Iter_sink::_M_overflow();
3735 iter_difference_t<_OutIter> __count(_M_count);
3747 template<
typename _CharT>
3748 class _Ptr_sink :
public _Sink<_CharT>
3750 static constexpr size_t _S_no_limit = size_t(-1);
3754 size_t _M_count = 0;
3759 _GLIBCXX_CONSTEXPR_FORMAT
void
3760 _M_overflow()
override
3762 if (this->_M_unused().size() != 0)
3765 auto __s = this->_M_used();
3767 if (_M_max != _S_no_limit)
3769 _M_count += __s.size();
3773 this->_M_reset(this->_M_buf);
3779 _M_rebuf(__s.data(), __s.size() + 1024, __s.size());
3783 _GLIBCXX_CONSTEXPR_FORMAT
bool
3784 _M_discarding()
const override
3791 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3792 _M_reserve(
size_t __n)
final
3794 auto __avail = this->_M_unused();
3795 if (__n > __avail.size())
3797 if (_M_max != _S_no_limit)
3800 auto __s = this->_M_used();
3801 _M_rebuf(__s.data(), __s.size() + __n, __s.size());
3807 template<
typename _IterDifference>
3808 static _GLIBCXX_CONSTEXPR_FORMAT
size_t
3809 _S_trim_max(_IterDifference __max)
3813 if constexpr (!is_integral_v<_IterDifference> ||
sizeof(__max) >
sizeof(size_t))
3815 if (_IterDifference((
size_t)-1) < __max)
3817 return size_t(__max);
3820 [[__gnu__::__always_inline__]]
3821 _GLIBCXX_CONSTEXPR_FORMAT
void
3822 _M_rebuf(_CharT* __ptr,
size_t __total,
size_t __inuse = 0)
3824 std::span<_CharT> __span(__ptr, __total);
3825 this->_M_reset(__span, __inuse);
3829 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3830 _Ptr_sink(_CharT* __ptr,
size_t __n = _S_no_limit) noexcept
3831 : _Sink<_CharT>(_M_buf), _M_max(__n)
3835 else if (__n != _S_no_limit)
3836 _M_rebuf(__ptr, __n);
3837#if __has_builtin(__builtin_dynamic_object_size)
3838 else if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3839 _M_rebuf(__ptr, __bytes /
sizeof(_CharT));
3844 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3845 __n = (1024 - __off) /
sizeof(_CharT);
3846 if (__n > 0) [[likely]]
3847 _M_rebuf(__ptr, __n);
3853 template<contiguous_iterator _OutIter>
3854 _GLIBCXX_CONSTEXPR_FORMAT
explicit
3855 _Ptr_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1)
3856 : _Ptr_sink(std::
to_address(__out), _S_trim_max(__n))
3859 template<contiguous_iterator _OutIter>
3860 _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_OutIter>
3861 _M_finish(_OutIter __first)
const
3863 auto __s = this->_M_used();
3864 if (__s.data() == _M_buf)
3867 iter_difference_t<_OutIter> __m(_M_max);
3868 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3869 return { __first + __m, __count };
3873 iter_difference_t<_OutIter> __count(__s.size());
3874 return { __first + __count, __count };
3879 template<
typename _CharT,
typename _OutIter>
3880 concept __contiguous_char_iter
3881 = contiguous_iterator<_OutIter>
3882 && same_as<iter_value_t<_OutIter>, _CharT>;
3893 template<
typename _Out,
typename _CharT>
3894 class _Padding_sink :
public _Str_sink<_CharT>
3899 size_t _M_printwidth;
3901 [[__gnu__::__always_inline__]]
3902 _GLIBCXX_CONSTEXPR_FORMAT
bool
3904 {
return _M_printwidth >= _M_maxwidth; }
3906 [[__gnu__::__always_inline__]]
3907 _GLIBCXX_CONSTEXPR_FORMAT
bool
3908 _M_buffering()
const
3910 if (_M_printwidth < _M_padwidth)
3912 if (_M_maxwidth != (
size_t)-1)
3913 return _M_printwidth < _M_maxwidth;
3917 _GLIBCXX_CONSTEXPR_FORMAT
void
3918 _M_sync_discarding()
3920 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3921 if (_M_out._M_discarding())
3922 _M_maxwidth = _M_printwidth;
3925 _GLIBCXX_CONSTEXPR_FORMAT
void
3928 span<_CharT> __new = this->_M_used();
3929 basic_string_view<_CharT> __str(__new.data(), __new.size());
3930 _M_out = __format::__write(
std::move(_M_out), __str);
3931 _M_sync_discarding();
3935 _GLIBCXX_CONSTEXPR_FORMAT
bool
3938 auto __str = this->view();
3940 _M_printwidth = __format::__truncate(__str, _M_maxwidth);
3942 this->_M_trim(__str);
3948 if (_M_printwidth >= _M_padwidth)
3950 _M_out = __format::__write(
std::move(_M_out), __str);
3951 _M_sync_discarding();
3956 _Str_sink<_CharT>::_M_overflow();
3959 this->_M_reset(this->_M_buf);
3963 _GLIBCXX_CONSTEXPR_FORMAT
bool
3964 _M_update(
size_t __new)
3966 _M_printwidth += __new;
3968 if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
3969 return _M_force_update();
3973 _GLIBCXX_CONSTEXPR_FORMAT
void
3974 _M_overflow()
override
3980 else if (!_M_buffering())
3984 else if (_M_update(this->_M_used().size()))
3985 _Str_sink<_CharT>::_M_overflow();
3988 _GLIBCXX_CONSTEXPR_FORMAT
bool
3989 _M_discarding()
const override
3990 {
return _M_ignoring(); }
3992 _GLIBCXX_CONSTEXPR_FORMAT
typename _Sink<_CharT>::_Reservation
3993 _M_reserve(
size_t __n)
override
3998 else if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
3999 if (!_M_buffering())
4002 if (!this->_M_used().empty())
4005 if (
auto __reserved = _M_out._M_reserve(__n))
4008 return _Sink<_CharT>::_M_reserve(__n);
4011 _GLIBCXX_CONSTEXPR_FORMAT
void
4012 _M_bump(
size_t __n)
override
4019 _Sink<_CharT>::_M_bump(__n);
4025 [[__gnu__::__always_inline__]]
4026 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4027 _Padding_sink(_Out __out,
size_t __padwidth,
size_t __maxwidth)
4028 : _M_padwidth(__padwidth), _M_maxwidth(__maxwidth),
4029 _M_out(std::move(__out)), _M_printwidth(0)
4030 { _M_sync_discarding(); }
4032 [[__gnu__::__always_inline__]]
4033 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4034 _Padding_sink(_Out __out,
size_t __padwidth)
4035 : _Padding_sink(std::move(__out), __padwidth, (size_t)-1)
4038 _GLIBCXX_CONSTEXPR_FORMAT _Out
4039 _M_finish(_Align __align,
char32_t __fill_char)
4042 if (
auto __rem = this->_M_used().size())
4046 else if (!_M_buffering())
4052 if (!_M_buffering() || !_M_force_update())
4054 if (_M_printwidth >= _M_padwidth)
4057 const auto __str = this->view();
4058 if (_M_printwidth >= _M_padwidth)
4059 return __format::__write(
std::move(_M_out), __str);
4061 const size_t __nfill = _M_padwidth - _M_printwidth;
4062 return __format::__write_padded(
std::move(_M_out), __str,
4063 __align, __nfill, __fill_char);
4067 template<
typename _Out,
typename _CharT>
4068 class _Escaping_sink :
public _Buf_sink<_CharT>
4070 using _Esc = _Escapes<_CharT>;
4073 _Term_char _M_term : 2;
4074 unsigned _M_prev_escape : 1;
4075 unsigned _M_out_discards : 1;
4077 _GLIBCXX_CONSTEXPR_FORMAT
void
4078 _M_sync_discarding()
4080 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4081 _M_out_discards = _M_out._M_discarding();
4084 _GLIBCXX_CONSTEXPR_FORMAT
void
4087 span<_CharT> __bytes = this->_M_used();
4088 basic_string_view<_CharT> __str(__bytes.data(), __bytes.size());
4091 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4093 bool __prev_escape = _M_prev_escape;
4094 _M_out = __format::__write_escaped_unicode_part(
4095 std::move(_M_out), __str, __prev_escape, _M_term);
4096 _M_prev_escape = __prev_escape;
4098 __rem = __str.size();
4099 if (__rem > 0 && __str.data() != this->_M_buf) [[unlikely]]
4100 ranges::move(__str, this->_M_buf);
4103 _M_out = __format::__write_escaped_ascii(
4106 this->_M_reset(this->_M_buf, __rem);
4107 _M_sync_discarding();
4110 _GLIBCXX_CONSTEXPR_FORMAT
void
4111 _M_overflow()
override
4113 if (_M_out_discards)
4119 _GLIBCXX_CONSTEXPR_FORMAT
bool
4120 _M_discarding()
const override
4121 {
return _M_out_discards; }
4124 [[__gnu__::__always_inline__]]
4125 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4126 _Escaping_sink(_Out __out, _Term_char __term)
4127 : _M_out(std::move(__out)), _M_term(__term),
4128 _M_prev_escape(true), _M_out_discards(false)
4130 _M_out = __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4131 _M_sync_discarding();
4134 _GLIBCXX_CONSTEXPR_FORMAT _Out
4137 if (_M_out_discards)
4140 if (!this->_M_used().empty())
4143 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
4144 if (
auto __rem = this->_M_used(); !__rem.empty())
4146 basic_string_view<_CharT> __str(__rem.data(), __rem.size());
4147 _M_out = __format::__write_escape_seqs(
std::move(_M_out), __str);
4150 return __format::__write(
std::move(_M_out), _Esc::_S_term(_M_term));
4154 enum class _Arg_t :
unsigned char {
4155 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
4156 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
4157 _Arg_i128, _Arg_u128, _Arg_float128,
4158 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
4161#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4162 _Arg_ibm128 = _Arg_ldbl,
4163 _Arg_ieee128 = _Arg_float128,
4168 template<
typename _Context>
4171 using _CharT =
typename _Context::char_type;
4175 using _CharT =
typename _Context::char_type;
4176 using _Func = void(*)(basic_format_parse_context<_CharT>&,
4177 _Context&,
const void*);
4180 template<
typename _Tp>
4181 using __maybe_const_t
4182 = __conditional_t<__formattable_with<const _Tp, _Context>,
4185 template<
typename _Tq>
4186 static _GLIBCXX_CONSTEXPR_FORMAT
void
4187 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
4188 _Context& __format_ctx,
const void* __ptr)
4190 using _Td = remove_const_t<_Tq>;
4191 typename _Context::template formatter_type<_Td> __f;
4192 __parse_ctx.advance_to(__f.parse(__parse_ctx));
4193 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
4194 __format_ctx.advance_to(__f.format(__val, __format_ctx));
4197 template<
typename _Tp>
4198 requires (!is_same_v<remove_cv_t<_Tp>, handle>)
4199 explicit _GLIBCXX_CONSTEXPR_FORMAT
4200 handle(_Tp& __val) noexcept
4201 : _M_ptr(__builtin_addressof(__val))
4202 , _M_func(&_S_format<__maybe_const_t<_Tp>>)
4205 friend class basic_format_arg<_Context>;
4208 handle(
const handle&) =
default;
4209 handle& operator=(
const handle&) =
default;
4211 [[__gnu__::__always_inline__]]
4212 void _GLIBCXX_CONSTEXPR_FORMAT
4213 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
4214 { _M_func(__pc, __fc, this->_M_ptr); }
4229 unsigned long long _M_ull;
4232#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4233 long double _M_ldbl;
4236 __ieee128 _M_ieee128;
4238#ifdef __SIZEOF_FLOAT128__
4239 __float128 _M_float128;
4241 const _CharT* _M_str;
4242 basic_string_view<_CharT> _M_sv;
4245#ifdef __SIZEOF_INT128__
4247 unsigned __int128 _M_u128;
4249#ifdef __BFLT16_DIG__
4263 [[__gnu__::__always_inline__]]
4264 _GLIBCXX_CONSTEXPR_FORMAT
4265 _Arg_value() : _M_none() { }
4268 template<
typename _Tp>
4269 _GLIBCXX_CONSTEXPR_FORMAT
4270 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
4271 { _S_get<_Tp>() = __val; }
4276 template<
typename _Tp,
typename _Self,
typename... _Value>
4277 [[__gnu__::__always_inline__]]
4278 static _GLIBCXX_CONSTEXPR_FORMAT
auto&
4279 _S_access(_Self& __u, _Value... __value)
noexcept
4281 static_assert(
sizeof...(_Value) <= 1);
4282 if constexpr (is_same_v<_Tp, bool>)
4283 return (__u._M_bool = ... = __value);
4284 else if constexpr (is_same_v<_Tp, _CharT>)
4285 return (__u._M_c = ... = __value);
4286 else if constexpr (is_same_v<_Tp, int>)
4287 return (__u._M_i = ... = __value);
4288 else if constexpr (is_same_v<_Tp, unsigned>)
4289 return (__u._M_u = ... = __value);
4290 else if constexpr (is_same_v<_Tp, long long>)
4291 return (__u._M_ll = ... = __value);
4292 else if constexpr (is_same_v<_Tp, unsigned long long>)
4293 return (__u._M_ull = ... = __value);
4294 else if constexpr (is_same_v<_Tp, float>)
4295 return (__u._M_flt = ... = __value);
4296 else if constexpr (is_same_v<_Tp, double>)
4297 return (__u._M_dbl = ... = __value);
4298#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4299 else if constexpr (is_same_v<_Tp, long double>)
4300 return (__u._M_ldbl = ... = __value);
4302 else if constexpr (is_same_v<_Tp, __ibm128>)
4303 return (__u._M_ibm128 = ... = __value);
4304 else if constexpr (is_same_v<_Tp, __ieee128>)
4305 return (__u._M_ieee128 = ... = __value);
4307#ifdef __SIZEOF_FLOAT128__
4308 else if constexpr (is_same_v<_Tp, __float128>)
4309 return (__u._M_float128 = ... = __value);
4311 else if constexpr (is_same_v<_Tp, const _CharT*>)
4312 return (__u._M_str = ... = __value);
4313 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4314 return (__u._M_sv = ... = __value);
4315 else if constexpr (is_same_v<_Tp, const void*>)
4316 return (__u._M_ptr = ... = __value);
4317#ifdef __SIZEOF_INT128__
4318 else if constexpr (is_same_v<_Tp, __int128>)
4319 return (__u._M_i128 = ... = __value);
4320 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4321 return (__u._M_u128 = ... = __value);
4323#ifdef __BFLT16_DIG__
4324 else if constexpr (is_same_v<_Tp, __bflt16_t>)
4325 return (__u._M_bf16 = ... = __value);
4328 else if constexpr (is_same_v<_Tp, _Float16>)
4329 return (__u._M_f16 = ... = __value);
4332 else if constexpr (is_same_v<_Tp, _Float32>)
4333 return (__u._M_f32 = ... = __value);
4336 else if constexpr (is_same_v<_Tp, _Float64>)
4337 return (__u._M_f64 = ... = __value);
4339 else if constexpr (is_same_v<_Tp, handle>)
4340 return __u._M_handle;
4342 __builtin_unreachable();
4345 template<
typename _Tp>
4346 [[__gnu__::__always_inline__]]
4347 _GLIBCXX_CONSTEXPR_FORMAT
auto&
4349 {
return _S_access<_Tp>(*
this); }
4351 template<
typename _Tp>
4352 [[__gnu__::__always_inline__]]
4353 _GLIBCXX_CONSTEXPR_FORMAT
const auto&
4354 _M_get() const noexcept
4355 {
return _S_access<_Tp>(*
this); }
4357 template<
typename _Tp>
4358 [[__gnu__::__always_inline__]]
4359 _GLIBCXX_CONSTEXPR_FORMAT
void
4360 _M_set(_Tp __v)
noexcept
4363 if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4364 std::construct_at(&_M_sv, __v);
4365 else if constexpr (is_same_v<_Tp, handle>)
4366 std::construct_at(&_M_handle, __v);
4370 _S_access<_Tp>(*
this, __v);
4375 template<
typename _Context,
typename... _Args>
4378 template<
typename _Visitor,
typename _Ctx>
4379 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4380 __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4382 template<
typename _Ch,
typename _Tp>
4384 __to_arg_t_enum() noexcept;
4388 template<typename _Context>
4389 class basic_format_arg
4391 using _CharT =
typename _Context::char_type;
4394 using handle = __format::_Arg_value<_Context>::handle;
4396 [[__gnu__::__always_inline__]]
4397 _GLIBCXX_CONSTEXPR_FORMAT
4398 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
4400 [[nodiscard,__gnu__::__always_inline__]]
4401 explicit _GLIBCXX_CONSTEXPR_FORMAT
operator bool() const noexcept
4402 {
return _M_type != __format::_Arg_none; }
4404#if __cpp_lib_format >= 202306L
4405 template<
typename _Visitor>
4406 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4407 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4410 template<
typename _Res,
typename _Visitor>
4411 _GLIBCXX_CONSTEXPR_FORMAT _Res
4412 visit(
this basic_format_arg __arg, _Visitor&& __vis)
4417 template<
typename _Ctx>
4418 friend class basic_format_args;
4420 template<
typename _Ctx,
typename... _Args>
4421 friend class __format::_Arg_store;
4423 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
4425 __format::_Arg_value<_Context> _M_val;
4426 __format::_Arg_t _M_type;
4431 template<
typename _Tp>
4432 static consteval auto
4435 using _Td = remove_const_t<_Tp>;
4436 if constexpr (is_same_v<_Td, bool>)
4437 return type_identity<bool>();
4438 else if constexpr (is_same_v<_Td, _CharT>)
4439 return type_identity<_CharT>();
4440 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
4441 return type_identity<_CharT>();
4442#ifdef __SIZEOF_INT128__
4443 else if constexpr (is_same_v<_Td, __int128>)
4444 return type_identity<__int128>();
4445 else if constexpr (is_same_v<_Td, unsigned __int128>)
4446 return type_identity<unsigned __int128>();
4448 else if constexpr (__is_signed_integer<_Td>::value)
4450 if constexpr (
sizeof(_Td) <=
sizeof(int))
4451 return type_identity<int>();
4452 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
4453 return type_identity<long long>();
4455 else if constexpr (__is_unsigned_integer<_Td>::value)
4457 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
4458 return type_identity<unsigned>();
4459 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
4460 return type_identity<unsigned long long>();
4462 else if constexpr (is_same_v<_Td, float>)
4463 return type_identity<float>();
4464 else if constexpr (is_same_v<_Td, double>)
4465 return type_identity<double>();
4466#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4467 else if constexpr (is_same_v<_Td, long double>)
4468 return type_identity<long double>();
4470 else if constexpr (is_same_v<_Td, __ibm128>)
4471 return type_identity<__ibm128>();
4472 else if constexpr (is_same_v<_Td, __ieee128>)
4473 return type_identity<__ieee128>();
4475#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4476 else if constexpr (is_same_v<_Td, __float128>)
4477 return type_identity<__float128>();
4479#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4480 else if constexpr (is_same_v<_Td, __format::__bflt16_t>)
4481 return type_identity<__format::__bflt16_t>();
4483#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4484 else if constexpr (is_same_v<_Td, _Float16>)
4485 return type_identity<_Float16>();
4487#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4488 else if constexpr (is_same_v<_Td, _Float32>)
4489 return type_identity<_Float32>();
4491#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4492 else if constexpr (is_same_v<_Td, _Float64>)
4493 return type_identity<_Float64>();
4495 else if constexpr (__is_specialization_of<_Td, basic_string_view>
4496 || __is_specialization_of<_Td, basic_string>)
4498 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
4499 return type_identity<basic_string_view<_CharT>>();
4501 return type_identity<handle>();
4503 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
4504 return type_identity<const _CharT*>();
4505 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
4506 return type_identity<const _CharT*>();
4507 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
4508 return type_identity<const void*>();
4509 else if constexpr (is_same_v<_Td, nullptr_t>)
4510 return type_identity<const void*>();
4512 return type_identity<handle>();
4516 template<
typename _Tp>
4517 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
4520 template<
typename _Tp>
4521 static consteval __format::_Arg_t
4524 using namespace __format;
4525 if constexpr (is_same_v<_Tp, bool>)
4527 else if constexpr (is_same_v<_Tp, _CharT>)
4529 else if constexpr (is_same_v<_Tp, int>)
4531 else if constexpr (is_same_v<_Tp, unsigned>)
4533 else if constexpr (is_same_v<_Tp, long long>)
4535 else if constexpr (is_same_v<_Tp, unsigned long long>)
4537 else if constexpr (is_same_v<_Tp, float>)
4539 else if constexpr (is_same_v<_Tp, double>)
4541#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4542 else if constexpr (is_same_v<_Tp, long double>)
4546 else if constexpr (is_same_v<_Tp, __ibm128>)
4548 else if constexpr (is_same_v<_Tp, __ieee128>)
4549 return _Arg_ieee128;
4551#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4552 else if constexpr (is_same_v<_Tp, __float128>)
4553 return _Arg_float128;
4555#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4556 else if constexpr (is_same_v<_Tp, __format::__bflt16_t>)
4559#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4560 else if constexpr (is_same_v<_Tp, _Float16>)
4563#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4564 else if constexpr (is_same_v<_Tp, _Float32>)
4567#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4568 else if constexpr (is_same_v<_Tp, _Float64>)
4571 else if constexpr (is_same_v<_Tp, const _CharT*>)
4573 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
4575 else if constexpr (is_same_v<_Tp, const void*>)
4577#ifdef __SIZEOF_INT128__
4578 else if constexpr (is_same_v<_Tp, __int128>)
4580 else if constexpr (is_same_v<_Tp, unsigned __int128>)
4583 else if constexpr (is_same_v<_Tp, handle>)
4587 template<
typename _Tp>
4588 _GLIBCXX_CONSTEXPR_FORMAT
void
4589 _M_set(_Tp __v)
noexcept
4591 _M_type = _S_to_enum<_Tp>();
4595 template<
typename _Tp>
4596 requires __format::__formattable_with<_Tp, _Context>
4597 _GLIBCXX_CONSTEXPR_FORMAT
explicit
4598 basic_format_arg(_Tp& __v)
noexcept
4600 using _Td = _Normalize<_Tp>;
4601 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
4602 _M_set(_Td{__v.data(), __v.size()});
4603 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
4604 && is_same_v<_CharT, wchar_t>)
4605 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
4607 _M_set(
static_cast<_Td
>(__v));
4610 template<
typename _Ctx,
typename... _Argz>
4611 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4612 make_format_args(_Argz&...)
noexcept;
4614 template<
typename _Visitor,
typename _Ctx>
4615 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4616 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
4618 template<
typename _Visitor,
typename _Ctx>
4619 friend _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4620 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
4622 template<
typename _Ch,
typename _Tp>
4623 friend consteval __format::_Arg_t
4624 __format::__to_arg_t_enum() noexcept;
4626 [[__gnu__::__noinline__]]
4628 _M_handle_unrecognized() const;
4630 template<typename _Visitor>
4631 _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4632 _M_visit(_Visitor&& __vis)
4636 using enum __format::_Arg_t;
4651#if __glibcxx_to_chars
4656#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4659#if defined(__SIZEOF_FLOAT128__) && _GLIBCXX_FORMAT_F128
4669#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4673#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4677#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
4681#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
4694#ifdef __SIZEOF_INT128__
4704 handle __h = _M_handle_unrecognized();
4709 template<
typename _Visitor>
4710 _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4711 _M_visit_user(_Visitor&& __vis)
4713 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4715 constexpr bool __user_facing = __is_one_of<_Tp,
4716 monostate, bool, _CharT,
4717 int,
unsigned int,
long long int,
unsigned long long int,
4718 float, double,
long double,
4719 const _CharT*, basic_string_view<_CharT>,
4720 const void*, handle>::value;
4721 if constexpr (__user_facing)
4732 template<
typename _Visitor,
typename _Context>
4733 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4734 inline _GLIBCXX_CONSTEXPR_FORMAT decltype(auto)
4735 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4741 template<
typename _Visitor,
typename _Ctx>
4742 inline _GLIBCXX_CONSTEXPR_FORMAT
decltype(
auto)
4743 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4746 struct _WidthPrecVisitor
4748 template<
typename _Tp>
4749 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4750 operator()(_Tp& __arg)
const
4752 if constexpr (is_same_v<_Tp, monostate>)
4753 __format::__invalid_arg_id_in_format_string();
4757 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4761 if constexpr (__is_unsigned_integer<_Tp>::value)
4763 else if constexpr (__is_signed_integer<_Tp>::value)
4767 __throw_format_error(
"format error: argument used for width or "
4768 "precision must be a non-negative integer");
4772#pragma GCC diagnostic push
4773#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4774 template<
typename _Context>
4775 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
4776 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4777 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4780 template<
int _Bits,
size_t _Nm>
4782 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4784 __UINT64_TYPE__ __packed_types = 0;
4785 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4786 __packed_types = (__packed_types << _Bits) | (
unsigned)*__i;
4787 return __packed_types;
4792 template<
typename _Context>
4793 class basic_format_args
4795 static constexpr int _S_packed_type_bits = 5;
4796 static constexpr int _S_packed_type_mask = 0b11111;
4797 static constexpr int _S_max_packed_args = 12;
4799 static_assert( (unsigned)__format::_Arg_max_ <= (1u << _S_packed_type_bits) );
4801 template<
typename... _Args>
4802 using _Store = __format::_Arg_store<_Context, _Args...>;
4804 template<
typename _Ctx,
typename... _Args>
4805 friend class __format::_Arg_store;
4807 using uint64_t = __UINT64_TYPE__;
4808 using _Format_arg = basic_format_arg<_Context>;
4809 using _Format_arg_val = __format::_Arg_value<_Context>;
4815 uint64_t _M_packed_size : 4;
4816 uint64_t _M_unpacked_size : 60;
4819 const _Format_arg_val* _M_values;
4820 const _Format_arg* _M_args;
4823 _GLIBCXX_CONSTEXPR_FORMAT
size_t
4824 _M_size() const noexcept
4825 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4827 _GLIBCXX_CONSTEXPR_FORMAT
typename __format::_Arg_t
4828 _M_type(
size_t __i)
const noexcept
4830 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4831 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4834 template<
typename _Ctx,
typename... _Args>
4835 friend _GLIBCXX_CONSTEXPR_FORMAT
auto
4836 make_format_args(_Args&...)
noexcept;
4839 template<
typename... _Args>
4840 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4842 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4845 template<
typename... _Args>
4846 _GLIBCXX_CONSTEXPR_FORMAT
4847 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4849 [[nodiscard,__gnu__::__always_inline__]]
4850 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<_Context>
4851 get(
size_t __i)
const noexcept
4853 basic_format_arg<_Context> __arg;
4854 if (__i < _M_packed_size)
4856 __arg._M_type = _M_type(__i);
4857 __arg._M_val = _M_values[__i];
4859 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4860 __arg = _M_args[__i];
4867 template<
typename _Context,
typename... _Args>
4868 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4869 -> basic_format_args<_Context>;
4871 template<
typename _Context,
typename... _Args>
4872 _GLIBCXX_CONSTEXPR_FORMAT
auto
4873 make_format_args(_Args&... __fmt_args)
noexcept;
4876 template<
typename _Context,
typename... _Args>
4877 class __format::_Arg_store
4879 friend std::basic_format_args<_Context>;
4881 template<
typename _Ctx,
typename... _Argz>
4882 friend _GLIBCXX_CONSTEXPR_FORMAT
auto std::
4883#if _GLIBCXX_INLINE_VERSION
4886 make_format_args(_Argz&...)
noexcept;
4890 static constexpr bool _S_values_only
4891 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4894 = __conditional_t<_S_values_only,
4895 __format::_Arg_value<_Context>,
4896 basic_format_arg<_Context>>;
4898 _Element_t _M_args[
sizeof...(_Args)];
4900 template<
typename _Tp>
4901 static _GLIBCXX_CONSTEXPR_FORMAT _Element_t
4902 _S_make_elt(_Tp& __v)
4904 using _Tq = remove_const_t<_Tp>;
4905 using _CharT =
typename _Context::char_type;
4906 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4907 "std::formatter must be specialized for the type "
4908 "of each format arg");
4909 using __format::__formattable_with;
4910 if constexpr (is_const_v<_Tp>)
4911 if constexpr (!__formattable_with<_Tp, _Context>)
4912 if constexpr (__formattable_with<_Tq, _Context>)
4913 static_assert(__formattable_with<_Tp, _Context>,
4914 "format arg must be non-const because its "
4915 "std::formatter specialization has a "
4916 "non-const reference parameter");
4917 basic_format_arg<_Context> __arg(__v);
4918 if constexpr (_S_values_only)
4919 return __arg._M_val;
4924 template<
typename... _Tp>
4925 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4926 [[__gnu__::__always_inline__]]
4927 _GLIBCXX_CONSTEXPR_FORMAT
4928 _Arg_store(_Tp&... __a) noexcept
4929 : _M_args{_S_make_elt(__a)...}
4933 template<
typename _Context>
4934 class __format::_Arg_store<_Context>
4937 template<
typename _Context>
4938 template<
typename... _Args>
4939 inline _GLIBCXX_CONSTEXPR_FORMAT
4940 basic_format_args<_Context>::
4941 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4943 if constexpr (
sizeof...(_Args) == 0)
4946 _M_unpacked_size = 0;
4949 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4952 _M_packed_size =
sizeof...(_Args);
4955 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4957 _M_values = __store._M_args;
4964 _M_unpacked_size =
sizeof...(_Args);
4966 _M_args = __store._M_args;
4971 template<
typename _Context = format_context,
typename... _Args>
4972 [[nodiscard,__gnu__::__always_inline__]]
4973 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4974 make_format_args(_Args&... __fmt_args)
noexcept
4976 using _Fmt_arg = basic_format_arg<_Context>;
4977 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4978 _Normalize<_Args>...>;
4979 return _Store(__fmt_args...);
4982#ifdef _GLIBCXX_USE_WCHAR_T
4984 template<
typename... _Args>
4985 [[nodiscard,__gnu__::__always_inline__]]
4986 inline _GLIBCXX_CONSTEXPR_FORMAT
auto
4987 make_wformat_args(_Args&... __args)
noexcept
4988 {
return std::make_format_args<wformat_context>(__args...); }
4994 template<
typename _Out,
typename _CharT,
typename _Context>
4995 _GLIBCXX_CONSTEXPR_FORMAT _Out
4996 __do_vformat_to(_Out, basic_string_view<_CharT>,
4997 const basic_format_args<_Context>&,
4998 const locale* =
nullptr);
5000 template<
typename _CharT>
struct __formatter_chrono;
5018 template<
typename _Out,
typename _CharT>
5021 class _GLIBCXX_NO_SPECIALIZATIONS basic_format_context
5023 static_assert( output_iterator<_Out, const _CharT&> );
5025 basic_format_args<basic_format_context> _M_args;
5027 __format::_Optional_locale _M_loc;
5029 _GLIBCXX_CONSTEXPR_FORMAT
5030 basic_format_context(basic_format_args<basic_format_context> __args,
5032 : _M_args(__args), _M_out(std::move(__out))
5035 _GLIBCXX_CONSTEXPR_FORMAT
5036 basic_format_context(basic_format_args<basic_format_context> __args,
5037 _Out __out,
const std::locale& __loc)
5038 : _M_args(__args), _M_out(std::move(__out)), _M_loc(__loc)
5044 basic_format_context(
const basic_format_context&) =
delete;
5045 basic_format_context& operator=(
const basic_format_context&) =
delete;
5047 template<
typename _Out2,
typename _CharT2,
typename _Context2>
5048 friend _GLIBCXX_CONSTEXPR_FORMAT _Out2
5049 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
5050 const basic_format_args<_Context2>&,
5053 friend __format::__formatter_chrono<_CharT>;
5056 ~basic_format_context() =
default;
5058 using iterator = _Out;
5059 using char_type = _CharT;
5060 template<
typename _Tp>
5061 using formatter_type = formatter<_Tp, _CharT>;
5064 _GLIBCXX_CONSTEXPR_FORMAT basic_format_arg<basic_format_context>
5065 arg(
size_t __id)
const noexcept
5066 {
return _M_args.get(__id); }
5069 std::locale locale() {
return _M_loc.value(); }
5072 _GLIBCXX_CONSTEXPR_FORMAT iterator
5075 _GLIBCXX_CONSTEXPR_FORMAT
void
5076 advance_to(iterator __it) { _M_out =
std::move(__it); }
5079#if _GLIBCXX_EXTERN_TEMPLATE
5082 extern template basic_format_arg<format_context>::handle
5083 basic_format_arg<format_context>::_M_handle_unrecognized()
const;
5084# ifdef _GLIBCXX_USE_WCHAR_T
5085 extern template basic_format_arg<wformat_context>::handle
5086 basic_format_arg<wformat_context>::_M_handle_unrecognized()
const;
5089 template<
typename _Context>
5090 typename basic_format_arg<_Context>::handle
5091 basic_format_arg<_Context>::_M_handle_unrecognized()
const
5096 __throw_format_error(
"format error: unrecognized argument type");
5108 template<
typename _CharT>
5111 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
5113 struct _Parse_context : basic_format_parse_context<_CharT>
5115 using basic_format_parse_context<_CharT>::basic_format_parse_context;
5116 const _Arg_t* _M_types =
nullptr;
5120 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
5121 : _M_pc(__str, __nargs)
5124 constexpr iterator begin() const noexcept {
return _M_pc.begin(); }
5125 constexpr iterator end() const noexcept {
return _M_pc.end(); }
5130 basic_string_view<_CharT> __fmt = _M_fmt_str();
5132 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5134 _M_pc.advance_to(begin() + 1);
5135 _M_format_arg(_M_pc.next_arg_id());
5139 size_t __lbr = __fmt.find(
'{');
5140 size_t __rbr = __fmt.find(
'}');
5142 while (__fmt.size())
5144 auto __cmp = __lbr <=> __rbr;
5148 _M_pc.advance_to(end());
5153 if (__lbr + 1 == __fmt.size()
5154 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
5155 __format::__unmatched_left_brace_in_format_string();
5156 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
5157 iterator __last = begin() + __lbr + int(__is_escape);
5158 _M_on_chars(__last);
5159 _M_pc.advance_to(__last + 1);
5160 __fmt = _M_fmt_str();
5163 if (__rbr != __fmt.npos)
5165 __lbr = __fmt.find(
'{');
5169 _M_on_replacement_field();
5170 __fmt = _M_fmt_str();
5171 __lbr = __fmt.find(
'{');
5172 __rbr = __fmt.find(
'}');
5177 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
5178 __format::__unmatched_right_brace_in_format_string();
5179 iterator __last = begin() + __rbr;
5180 _M_on_chars(__last);
5181 _M_pc.advance_to(__last + 1);
5182 __fmt = _M_fmt_str();
5183 if (__lbr != __fmt.npos)
5185 __rbr = __fmt.find(
'}');
5190 constexpr basic_string_view<_CharT>
5191 _M_fmt_str() const noexcept
5192 {
return {begin(), end()}; }
5194 constexpr virtual void _M_on_chars(iterator) { }
5196 constexpr void _M_on_replacement_field()
5198 auto __next = begin();
5202 __id = _M_pc.next_arg_id();
5203 else if (*__next ==
':')
5205 __id = _M_pc.next_arg_id();
5206 _M_pc.advance_to(++__next);
5210 auto [__i, __ptr] = __format::__parse_arg_id(begin(), end());
5211 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
5212 __format::__invalid_arg_id_in_format_string();
5213 _M_pc.check_arg_id(__id = __i);
5216 _M_pc.advance_to(++__ptr);
5219 _M_pc.advance_to(__ptr);
5221 _M_format_arg(__id);
5222 if (begin() == end() || *begin() !=
'}')
5223 __format::__unmatched_left_brace_in_format_string();
5224 _M_pc.advance_to(begin() + 1);
5227 constexpr virtual void _M_format_arg(
size_t __id) = 0;
5231 template<
typename _Out,
typename _CharT>
5232 class _Formatting_scanner :
public _Scanner<_CharT>
5235 _GLIBCXX_CONSTEXPR_FORMAT
5236 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
5237 basic_string_view<_CharT> __str)
5238 : _Scanner<_CharT>(__str), _M_fc(__fc)
5242 basic_format_context<_Out, _CharT>& _M_fc;
5244 using iterator =
typename _Scanner<_CharT>::iterator;
5247 _M_on_chars(iterator __last)
override
5249 basic_string_view<_CharT> __str(this->begin(), __last);
5250 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
5254 _M_format_arg(
size_t __id)
override
5256 using _Context = basic_format_context<_Out, _CharT>;
5257 using handle =
typename basic_format_arg<_Context>::handle;
5259 __format::__visit_format_arg([
this](
auto& __arg) {
5261 using _Formatter =
typename _Context::template formatter_type<_Type>;
5262 if constexpr (is_same_v<_Type, monostate>)
5263 __format::__invalid_arg_id_in_format_string();
5264 else if constexpr (is_same_v<_Type, handle>)
5265 __arg.format(this->_M_pc, this->_M_fc);
5266 else if constexpr (is_default_constructible_v<_Formatter>)
5269 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5270 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
5273 static_assert(__format::__formattable_with<_Type, _Context>);
5274 }, _M_fc.arg(__id));
5278 template<
typename _CharT,
typename _Tp>
5280 __to_arg_t_enum() noexcept
5282 using _Context = __format::__format_context<_CharT>;
5283 using _Fmt_arg = basic_format_arg<_Context>;
5284 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
5285 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
5289 template<
typename _CharT,
typename... _Args>
5290 class _Checking_scanner :
public _Scanner<_CharT>
5293 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
5294 "std::formatter must be specialized for each type being formatted");
5298 _Checking_scanner(basic_string_view<_CharT> __str)
5299 : _Scanner<_CharT>(__str, sizeof...(_Args))
5301#if __cpp_lib_format >= 202305L
5302 this->_M_pc._M_types = _M_types.data();
5308 _M_format_arg(
size_t __id)
override
5310 if constexpr (
sizeof...(_Args) != 0)
5312 if (__id <
sizeof...(_Args))
5314 _M_parse_format_spec<_Args...>(__id);
5318 __builtin_unreachable();
5321 template<
typename _Tp,
typename... _OtherArgs>
5323 _M_parse_format_spec(
size_t __id)
5327 formatter<_Tp, _CharT> __f;
5328 this->_M_pc.advance_to(__f.parse(this->_M_pc));
5330 else if constexpr (
sizeof...(_OtherArgs) != 0)
5331 _M_parse_format_spec<_OtherArgs...>(__id - 1);
5333 __builtin_unreachable();
5336#if __cpp_lib_format >= 202305L
5337 array<_Arg_t,
sizeof...(_Args)>
5338 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
5342 template<typename _CharT, unsigned = __unicode::__literal_encoding_is_unicode<_CharT>()>
5343 _GLIBCXX_CONSTEXPR_FORMAT _Sink_iter<_CharT>
5344 __do_vformat_to(_Sink_iter<_CharT> __out, basic_string_view<_CharT> __fmt,
5345 __format_context<_CharT>& __ctx)
5347 if constexpr (is_same_v<_CharT, char>)
5349 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
5351 bool __done =
false;
5352 __format::__visit_format_arg([&](
auto& __arg) {
5353 using _Tp = remove_cvref_t<
decltype(__arg)>;
5354 if constexpr (is_same_v<_Tp, bool>)
5356 size_t __len = 4 + !__arg;
5357 const char* __chars[] = {
"false",
"true" };
5358 if (
auto __res = __out._M_reserve(__len))
5360 ranges::copy_n(__chars[__arg], __len, __res.get());
5361 __res._M_bump(__len);
5365 else if constexpr (is_same_v<_Tp, char>)
5367 if (
auto __res = __out._M_reserve(1))
5369 *__res.get() = __arg;
5374 else if constexpr (is_integral_v<_Tp>)
5376 make_unsigned_t<_Tp> __uval;
5377 const bool __neg = __arg < 0;
5379 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
5382 const auto __n = __detail::__to_chars_len(__uval);
5383 if (
auto __res = __out._M_reserve(__n + __neg))
5385 auto __ptr = __res.get();
5387 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
5389 __res._M_bump(__n + __neg);
5393 else if constexpr (is_convertible_v<_Tp, string_view>)
5395 string_view __sv = __arg;
5396 if (
auto __res = __out._M_reserve(__sv.size()))
5398 ranges::copy(__sv, __res.get());
5399 __res._M_bump(__sv.size());
5409 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
5410 __scanner._M_scan();
5419#if __cplusplus <= 202002L && _GLIBCXX_EXTERN_TEMPLATE
5420 extern template _Sink_iter<char>
5421 __do_vformat_to<char, 1>(_Sink_iter<char>, string_view,
5423# ifdef _GLIBCXX_USE_WCHAR_T
5424 extern template _Sink_iter<wchar_t>
5425 __do_vformat_to<wchar_t, 1>(_Sink_iter<wchar_t>, wstring_view,
5430 template<
typename _Out,
typename _CharT,
typename _Context>
5431 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5432 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
5433 const basic_format_args<_Context>& __args,
5434 const locale* __loc)
5436 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
5438 auto __ctx = __loc ==
nullptr
5439 ? _Context(__args, __out)
5440 : _Context(__args, __out, *__loc);
5441 return __format::__do_vformat_to(__out, __fmt, __ctx);
5443 else if constexpr (__contiguous_char_iter<_CharT, _Out>)
5445 _Ptr_sink<_CharT> __sink(__out);
5446 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5447 return std::move(__sink)._M_finish(__out).out;
5451 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
5452 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5453 return std::move(__sink)._M_finish().out;
5457 template<
typename _Out,
typename _CharT>
5458 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5459 __do_vformat_to_n(_Out __out, iter_difference_t<_Out> __n,
5460 basic_string_view<_CharT> __fmt,
5461 const type_identity_t<
5462 basic_format_args<__format_context<_CharT>>>& __args,
5463 const locale* __loc =
nullptr)
5465 if constexpr (__contiguous_char_iter<_CharT, _Out>)
5467 _Ptr_sink<_CharT> __sink(__out, __n);
5468 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5469 return std::move(__sink)._M_finish(__out);
5473 _Iter_sink<_CharT, _Out> __sink(
std::move(__out), __n);
5474 __format::__do_vformat_to(__sink.out(), __fmt, __args, __loc);
5479#pragma GCC diagnostic pop
5484#if __cpp_lib_format >= 202305L
5487 template<
typename _CharT>
5488 template<
typename... _Ts>
5490 basic_format_parse_context<_CharT>::
5491 __check_dynamic_spec(
size_t __id)
noexcept
5493 if (__id >= _M_num_args)
5494 __format::__invalid_arg_id_in_format_string();
5495 if constexpr (
sizeof...(_Ts) != 0)
5497 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
5498 auto* __args =
static_cast<_Parse_ctx*
>(
this)->_M_types;
5503 auto __arg = __args[__id];
5504 __format::_Arg_t __types[] = {
5505 __format::__to_arg_t_enum<_CharT, _Ts>()...
5507 for (
auto __t : __types)
5511 __invalid_dynamic_spec(
"arg(id) type does not match");
5516 template<
typename _CharT,
typename... _Args>
5517 template<
typename _Tp>
5518 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
5520 basic_format_string<_CharT, _Args...>::
5521 basic_format_string(
const _Tp& __s) noexcept
5524 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
5526 __scanner._M_scan();
5531 template<
typename _Out>
requires output_iterator<_Out, const char&>
5532 [[__gnu__::__always_inline__]]
5533 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5534 vformat_to(_Out __out, string_view __fmt, format_args __args)
5535 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5537#ifdef _GLIBCXX_USE_WCHAR_T
5538 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5539 [[__gnu__::__always_inline__]]
5540 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5541 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
5542 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
5545 template<
typename _Out>
requires output_iterator<_Out, const char&>
5546 [[__gnu__::__always_inline__]]
5548 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
5551 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5554#ifdef _GLIBCXX_USE_WCHAR_T
5555 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
5556 [[__gnu__::__always_inline__]]
5558 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
5559 wformat_args __args)
5561 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
5566 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5567 vformat(string_view __fmt, format_args __args)
5569 __format::_Str_sink<char> __buf;
5570 std::vformat_to(__buf.out(), __fmt, __args);
5574#ifdef _GLIBCXX_USE_WCHAR_T
5576 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5577 vformat(wstring_view __fmt, wformat_args __args)
5579 __format::_Str_sink<wchar_t> __buf;
5580 std::vformat_to(__buf.out(), __fmt, __args);
5587 vformat(
const locale& __loc, string_view __fmt, format_args __args)
5589 __format::_Str_sink<char> __buf;
5590 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5594#ifdef _GLIBCXX_USE_WCHAR_T
5597 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
5599 __format::_Str_sink<wchar_t> __buf;
5600 std::vformat_to(__buf.out(), __loc, __fmt, __args);
5605 template<
typename... _Args>
5607 inline _GLIBCXX_CONSTEXPR_FORMAT
string
5608 format(format_string<_Args...> __fmt, _Args&&... __args)
5609 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
5611#ifdef _GLIBCXX_USE_WCHAR_T
5612 template<
typename... _Args>
5614 inline _GLIBCXX_CONSTEXPR_FORMAT wstring
5615 format(wformat_string<_Args...> __fmt, _Args&&... __args)
5616 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
5619 template<
typename... _Args>
5622 format(
const locale& __loc, format_string<_Args...> __fmt,
5625 return std::vformat(__loc, __fmt.get(),
5626 std::make_format_args(__args...));
5629#ifdef _GLIBCXX_USE_WCHAR_T
5630 template<
typename... _Args>
5633 format(
const locale& __loc, wformat_string<_Args...> __fmt,
5636 return std::vformat(__loc, __fmt.get(),
5637 std::make_wformat_args(__args...));
5641 template<
typename _Out,
typename... _Args>
5642 requires output_iterator<_Out, const char&>
5643 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5644 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
5646 return std::vformat_to(
std::move(__out), __fmt.get(),
5647 std::make_format_args(__args...));
5650#ifdef _GLIBCXX_USE_WCHAR_T
5651 template<
typename _Out,
typename... _Args>
5652 requires output_iterator<_Out, const wchar_t&>
5653 inline _GLIBCXX_CONSTEXPR_FORMAT _Out
5654 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
5656 return std::vformat_to(
std::move(__out), __fmt.get(),
5657 std::make_wformat_args(__args...));
5661 template<
typename _Out,
typename... _Args>
5662 requires output_iterator<_Out, const char&>
5664 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
5667 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5668 std::make_format_args(__args...));
5671#ifdef _GLIBCXX_USE_WCHAR_T
5672 template<
typename _Out,
typename... _Args>
5673 requires output_iterator<_Out, const wchar_t&>
5675 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
5678 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
5679 std::make_wformat_args(__args...));
5683 template<
typename _Out,
typename... _Args>
5684 requires output_iterator<_Out, const char&>
5685 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5686 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5687 format_string<_Args...> __fmt, _Args&&... __args)
5689 return __format::__do_vformat_to_n(
5691 std::make_format_args(__args...));
5694#ifdef _GLIBCXX_USE_WCHAR_T
5695 template<
typename _Out,
typename... _Args>
5696 requires output_iterator<_Out, const wchar_t&>
5697 inline _GLIBCXX_CONSTEXPR_FORMAT format_to_n_result<_Out>
5698 format_to_n(_Out __out, iter_difference_t<_Out> __n,
5699 wformat_string<_Args...> __fmt, _Args&&... __args)
5701 return __format::__do_vformat_to_n(
5703 std::make_wformat_args(__args...));
5707 template<
typename _Out,
typename... _Args>
5708 requires output_iterator<_Out, const char&>
5709 inline format_to_n_result<_Out>
5710 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5711 format_string<_Args...> __fmt, _Args&&... __args)
5713 return __format::__do_vformat_to_n(
5715 std::make_format_args(__args...), &__loc);
5718#ifdef _GLIBCXX_USE_WCHAR_T
5719 template<
typename _Out,
typename... _Args>
5720 requires output_iterator<_Out, const wchar_t&>
5721 inline format_to_n_result<_Out>
5722 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5723 wformat_string<_Args...> __fmt, _Args&&... __args)
5725 return __format::__do_vformat_to_n(
5727 std::make_wformat_args(__args...), &__loc);
5735 template<
typename _CharT>
5736 class _Counting_sink final :
public _Ptr_sink<_CharT>
5739 _GLIBCXX_CONSTEXPR_FORMAT
5740 _Counting_sink() : _Ptr_sink<_CharT>(nullptr, 0) { }
5742 [[__gnu__::__always_inline__]]
5743 _GLIBCXX_CONSTEXPR_FORMAT
size_t
5745 {
return this->_M_count + this->_M_used().size(); }
5748 template<
typename _CharT>
5749 class _Counting_sink :
public _Buf_sink<_CharT>
5751 size_t _M_count = 0;
5754 _M_overflow()
override
5756 if (!std::is_constant_evaluated())
5757 _M_count += this->_M_used().size();
5762 _Counting_sink() =
default;
5764 [[__gnu__::__always_inline__]]
5768 _Counting_sink::_M_overflow();
5776 template<
typename... _Args>
5778 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5779 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5781 __format::_Counting_sink<char> __buf;
5782 std::vformat_to(__buf.out(), __fmt.get(),
5783 std::make_format_args(__args...));
5784 return __buf.count();
5787#ifdef _GLIBCXX_USE_WCHAR_T
5788 template<
typename... _Args>
5790 inline _GLIBCXX_CONSTEXPR_FORMAT
size_t
5791 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5793 __format::_Counting_sink<wchar_t> __buf;
5794 std::vformat_to(__buf.out(), __fmt.get(),
5795 std::make_wformat_args(__args...));
5796 return __buf.count();
5800 template<
typename... _Args>
5803 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5806 __format::_Counting_sink<char> __buf;
5807 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5808 std::make_format_args(__args...));
5809 return __buf.count();
5812#ifdef _GLIBCXX_USE_WCHAR_T
5813 template<
typename... _Args>
5816 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5819 __format::_Counting_sink<wchar_t> __buf;
5820 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5821 std::make_wformat_args(__args...));
5822 return __buf.count();
5826#if __glibcxx_format_ranges
5828 template<typename _Tp>
5829 consteval range_format
5832 using _Ref = ranges::range_reference_t<_Tp>;
5833 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5834 return range_format::disabled;
5835 else if constexpr (
requires {
typename _Tp::key_type; })
5837 if constexpr (
requires {
typename _Tp::mapped_type; })
5839 using _Up = remove_cvref_t<_Ref>;
5840 if constexpr (__is_pair<_Up>)
5841 return range_format::map;
5842 else if constexpr (__is_specialization_of<_Up, tuple>)
5843 if constexpr (tuple_size_v<_Up> == 2)
5844 return range_format::map;
5846 return range_format::set;
5849 return range_format::sequence;
5854 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5855 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5860 template<
typename _CharT,
typename _Out,
typename _Callback>
5861 _GLIBCXX_CONSTEXPR_FORMAT
5862 typename basic_format_context<_Out, _CharT>::iterator
5863 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5864 const _Spec<_CharT>& __spec,
5867 if constexpr (is_same_v<_Out, _Drop_iter<_CharT>>)
5873 static_assert(is_same_v<_Out, _Sink_iter<_CharT>>);
5875 const size_t __padwidth = __spec._M_get_width(__fc);
5876 if (__padwidth == 0)
5877 return __call(__fc);
5881 _GLIBCXX_CONSTEXPR_FORMAT
5882 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5883 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5886 _GLIBCXX_CONSTEXPR_FORMAT
void
5888 { _M_ctx =
nullptr; }
5890 _GLIBCXX_CONSTEXPR_FORMAT
5894 _M_ctx->advance_to(_M_out);
5898 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5899 _Sink_iter<_CharT> _M_out;
5902 _Restore_out __restore(__fc);
5903 _Padding_sink<_Sink_iter<_CharT>, _CharT> __sink(__fc.out(), __padwidth);
5904 __fc.advance_to(__sink.out());
5906 __fc.advance_to(__sink._M_finish(__spec._M_align, __spec._M_fill));
5907 __restore._M_disarm();
5912 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5913 struct __indexed_formatter_storage
5920 basic_format_parse_context<_CharT> __pc({});
5921 if (_M_formatter.parse(__pc) != __pc.end())
5922 __format::__failed_to_parse_format_spec();
5925 template<
typename _Out>
5926 _GLIBCXX_CONSTEXPR_FORMAT
void
5927 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5928 basic_format_context<_Out, _CharT>& __fc,
5929 basic_string_view<_CharT> __sep)
const
5931 if constexpr (_Pos != 0)
5932 __fc.advance_to(__format::__write(__fc.out(), __sep));
5933 __fc.advance_to(_M_formatter.format(__elem, __fc));
5936 [[__gnu__::__always_inline__]]
5940 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5941 _M_formatter.set_debug_format();
5945 formatter<_Tp, _CharT> _M_formatter;
5948 template<
typename _CharT,
typename... _Tps>
5949 class __tuple_formatter
5951 using _String_view = basic_string_view<_CharT>;
5952 using _Seps = __format::_Separators<_CharT>;
5956 set_separator(basic_string_view<_CharT> __sep)
noexcept
5960 set_brackets(basic_string_view<_CharT> __open,
5961 basic_string_view<_CharT> __close)
noexcept
5969 constexpr typename basic_format_parse_context<_CharT>::iterator
5970 parse(basic_format_parse_context<_CharT>& __pc)
5972 auto __first = __pc.begin();
5973 const auto __last = __pc.end();
5974 __format::_Spec<_CharT> __spec{};
5976 auto __finished = [&]
5978 if (__first != __last && *__first !=
'}')
5982 _M_felems._M_parse();
5983 _M_felems.set_debug_format();
5990 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5994 __first = __spec._M_parse_width(__first, __last, __pc);
5998 if (*__first ==
'n')
6001 _M_open = _M_close = _String_view();
6003 else if (*__first ==
'm')
6006 if constexpr (
sizeof...(_Tps) == 2)
6008 _M_sep = _Seps::_S_colon();
6009 _M_open = _M_close = _String_view();
6012 __throw_format_error(
"format error: 'm' specifier requires range"
6013 " of pair or tuple of two elements");
6019 __format::__failed_to_parse_format_spec();
6023 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
6024 _GLIBCXX_CONSTEXPR_FORMAT
6025 typename basic_format_context<_Out, _CharT>::iterator
6026 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
6027 basic_format_context<_Out, _CharT>& __fc)
const
6028 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
6030 template<
typename _Out>
6031 _GLIBCXX_CONSTEXPR_FORMAT
6032 typename basic_format_context<_Out, _CharT>::iterator
6033 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
6034 basic_format_context<_Out, _CharT>& __fc)
const
6036 return __format::__format_padded(
6038 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
6040 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
6041 _M_felems._M_format(__elems..., __nfc, _M_sep);
6042 return __format::__write(__nfc.out(), _M_close);
6047 template<
size_t... _Ids>
6048 struct __formatters_storage
6049 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
6051 template<
size_t _Id,
typename _Up>
6052 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
6057 (_Base<_Ids, _Tps>::_M_parse(), ...);
6060 template<
typename _Out>
6061 _GLIBCXX_CONSTEXPR_FORMAT
void
6062 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
6063 basic_format_context<_Out, _CharT>& __fc,
6064 _String_view __sep)
const
6066 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
6072 (_Base<_Ids, _Tps>::set_debug_format(), ...);
6076 template<
size_t... _Ids>
6077 static _GLIBCXX_CONSTEXPR_FORMAT
auto
6078 _S_create_storage(index_sequence<_Ids...>)
6079 -> __formatters_storage<_Ids...>;
6081 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
6083 _Spec<_CharT> _M_spec{};
6084 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
6085 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
6086 _String_view _M_sep = _Seps::_S_comma();
6087 _Formatters _M_felems;
6090 template<
typename _Tp>
6091 concept __is_map_formattable
6092 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
6098 template<__format::__
char _CharT, formattable<_CharT> _Fp,
6099 formattable<_CharT> _Sp>
6100 struct formatter<
pair<_Fp, _Sp>, _CharT>
6101 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
6102 remove_cvref_t<_Sp>>
6105 using __maybe_const_pair
6106 = __conditional_t<formattable<const _Fp, _CharT>
6107 && formattable<const _Sp, _CharT>,
6108 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
6112 template<
typename _Out>
6113 _GLIBCXX_CONSTEXPR_FORMAT
6114 typename basic_format_context<_Out, _CharT>::iterator
6115 format(__maybe_const_pair& __p,
6116 basic_format_context<_Out, _CharT>& __fc)
const
6117 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
6120#if __glibcxx_print >= 202406L
6123 template<
typename _Fp,
typename _Sp>
6124 constexpr bool enable_nonlocking_formatter_optimization<pair<_Fp, _Sp>>
6125 = enable_nonlocking_formatter_optimization<remove_cvref_t<_Fp>>
6126 && enable_nonlocking_formatter_optimization<remove_cvref_t<_Sp>>;
6129 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
6130 struct formatter<tuple<_Tps...>, _CharT>
6131 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
6134 using __maybe_const_tuple
6135 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
6136 const tuple<_Tps...>, tuple<_Tps...>>;
6140 template<
typename _Out>
6141 _GLIBCXX_CONSTEXPR_FORMAT
6142 typename basic_format_context<_Out, _CharT>::iterator
6143 format(__maybe_const_tuple& __t,
6144 basic_format_context<_Out, _CharT>& __fc)
const
6145 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
6148#if __glibcxx_print >= 202406L
6151 template<
typename... _Tps>
6152 constexpr bool enable_nonlocking_formatter_optimization<tuple<_Tps...>>
6153 = (enable_nonlocking_formatter_optimization<remove_cvref_t<_Tps>> && ...);
6157 template<
typename _Tp, __format::__
char _CharT>
6158 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
6159 class range_formatter
6161 using _String_view = basic_string_view<_CharT>;
6162 using _Seps = __format::_Separators<_CharT>;
6166 set_separator(basic_string_view<_CharT> __sep)
noexcept
6170 set_brackets(basic_string_view<_CharT> __open,
6171 basic_string_view<_CharT> __close)
noexcept
6177 constexpr formatter<_Tp, _CharT>&
6178 underlying() noexcept
6181 constexpr const formatter<_Tp, _CharT>&
6182 underlying() const noexcept
6187 constexpr typename basic_format_parse_context<_CharT>::iterator
6188 parse(basic_format_parse_context<_CharT>& __pc)
6190 auto __first = __pc.begin();
6191 const auto __last = __pc.end();
6192 __format::_Spec<_CharT> __spec{};
6193 bool __no_brace =
false;
6195 auto __finished = [&]
6196 {
return __first == __last || *__first ==
'}'; };
6198 auto __finalize = [&]
6204 auto __parse_val = [&](_String_view __nfs = _String_view())
6208 basic_format_parse_context<_CharT> __npc(__nfs);
6209 if (_M_fval.parse(__npc) != __npc.end())
6210 __format::__failed_to_parse_format_spec();
6211 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
6212 _M_fval.set_debug_format();
6213 return __finalize();
6217 return __parse_val();
6219 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
6221 return __parse_val();
6223 __first = __spec._M_parse_width(__first, __last, __pc);
6225 return __parse_val();
6227 if (*__first ==
'?')
6230 __spec._M_debug =
true;
6231 if (__finished() || *__first !=
's')
6232 __throw_format_error(
"format error: '?' is allowed only in"
6233 " combination with 's'");
6236 if (*__first ==
's')
6239 if constexpr (same_as<_Tp, _CharT>)
6241 __spec._M_type = __format::_Pres_s;
6243 return __finalize();
6244 __throw_format_error(
"format error: element format specifier"
6245 " cannot be provided when 's' specifier is used");
6248 __throw_format_error(
"format error: 's' specifier requires"
6249 " range of character types");
6253 return __parse_val();
6255 if (*__first ==
'n')
6258 _M_open = _M_close = _String_view();
6263 return __parse_val();
6265 if (*__first ==
'm')
6267 _String_view __m(__first, 1);
6269 if constexpr (__format::__is_map_formattable<_Tp>)
6271 _M_sep = _Seps::_S_comma();
6274 _M_open = _Seps::_S_braces().substr(0, 1);
6275 _M_close = _Seps::_S_braces().substr(1, 1);
6278 return __parse_val(__m);
6279 __throw_format_error(
"format error: element format specifier"
6280 " cannot be provided when 'm' specifier is used");
6283 __throw_format_error(
"format error: 'm' specifier requires"
6284 " range of pairs or tuples of two elements");
6288 return __parse_val();
6290 if (*__first ==
':')
6292 __pc.advance_to(++__first);
6293 __first = _M_fval.parse(__pc);
6297 return __finalize();
6299 __format::__failed_to_parse_format_spec();
6304 template<ranges::input_range _Rg,
typename _Out>
6305 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
6306 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
6307 _GLIBCXX_CONSTEXPR_FORMAT
6308 typename basic_format_context<_Out, _CharT>::iterator
6309 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6311 using _Range = remove_reference_t<_Rg>;
6312 if constexpr (ranges::contiguous_range<_Rg>)
6314 const span<__format::__maybe_const<_Tp, _CharT>>
6315 __spn(ranges::data(__rg),
size_t(ranges::distance(__rg)));
6316 return _M_format(__spn, __fc);
6318 else if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
6319 return _M_format<const _Range>(__rg, __fc);
6321 return _M_format(__rg, __fc);
6325 template<ranges::input_range _Rg,
typename _Out>
6326 _GLIBCXX_CONSTEXPR_FORMAT
6327 typename basic_format_context<_Out, _CharT>::iterator
6328 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
6330 if constexpr (same_as<_Tp, _CharT>)
6331 if (_M_spec._M_type == __format::_Pres_s)
6333 __format::__formatter_str __fstr(_M_spec);
6334 return __fstr._M_format_range(__rg, __fc);
6336 return __format::__format_padded(
6338 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
6339 {
return _M_format_elems(__rg, __nfc); });
6343 template<ranges::input_range _Rg,
typename _Out>
6344 _GLIBCXX_CONSTEXPR_FORMAT
6345 typename basic_format_context<_Out, _CharT>::iterator
6346 _M_format_elems(_Rg& __rg,
6347 basic_format_context<_Out, _CharT>& __fc)
const
6349 auto __out = __format::__write(__fc.out(), _M_open);
6351 auto __first = ranges::begin(__rg);
6352 auto const __last = ranges::end(__rg);
6353 if (__first == __last)
6354 return __format::__write(__out, _M_close);
6356 __fc.advance_to(__out);
6357 __out = _M_fval.format(*__first, __fc);
6358 for (++__first; __first != __last; ++__first)
6360 __out = __format::__write(__out, _M_sep);
6361 __fc.advance_to(__out);
6362 __out = _M_fval.format(*__first, __fc);
6365 return __format::__write(__out, _M_close);
6368 __format::_Spec<_CharT> _M_spec{};
6369 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
6370 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
6371 _String_view _M_sep = _Seps::_S_comma();
6372 formatter<_Tp, _CharT> _M_fval;
6380 template<ranges::input_range _Rg, __format::__
char _CharT>
6381 requires (format_kind<_Rg> != range_format::disabled)
6382 && formattable<ranges::range_reference_t<_Rg>, _CharT>
6383 struct formatter<_Rg, _CharT>
6386 static const bool _S_range_format_is_string =
6387 (format_kind<_Rg> == range_format::string)
6388 || (format_kind<_Rg> == range_format::debug_string);
6389 using _Vt = remove_cvref_t<
6390 ranges::range_reference_t<
6391 __format::__maybe_const_range<_Rg, _CharT>>>;
6393 static consteval bool _S_is_correct()
6395 if constexpr (_S_range_format_is_string)
6396 static_assert(same_as<_Vt, _CharT>);
6400 static_assert(_S_is_correct());
6403 constexpr formatter() noexcept
6405 using _Seps = __format::_Separators<_CharT>;
6406 if constexpr (format_kind<_Rg> == range_format::map)
6408 static_assert(__format::__is_map_formattable<_Vt>);
6409 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6410 _Seps::_S_braces().substr(1, 1));
6411 _M_under.underlying().set_brackets({}, {});
6412 _M_under.underlying().set_separator(_Seps::_S_colon());
6414 else if constexpr (format_kind<_Rg> == range_format::set)
6415 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
6416 _Seps::_S_braces().substr(1, 1));
6420 set_separator(basic_string_view<_CharT> __sep)
noexcept
6421 requires (format_kind<_Rg> == range_format::sequence)
6422 { _M_under.set_separator(__sep); }
6425 set_brackets(basic_string_view<_CharT> __open,
6426 basic_string_view<_CharT> __close)
noexcept
6427 requires (format_kind<_Rg> == range_format::sequence)
6428 { _M_under.set_brackets(__open, __close); }
6432 constexpr typename basic_format_parse_context<_CharT>::iterator
6433 parse(basic_format_parse_context<_CharT>& __pc)
6435 auto __res = _M_under.parse(__pc);
6436 if constexpr (format_kind<_Rg> == range_format::debug_string)
6437 _M_under.set_debug_format();
6443 template<
typename _Out>
6444 _GLIBCXX_CONSTEXPR_FORMAT
6445 typename basic_format_context<_Out, _CharT>::iterator
6446 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
6447 basic_format_context<_Out, _CharT>& __fc)
const
6449 if constexpr (_S_range_format_is_string)
6450 return _M_under._M_format_range(__rg, __fc);
6452 return _M_under.format(__rg, __fc);
6456 using _Formatter_under
6457 = __conditional_t<_S_range_format_is_string,
6458 __format::__formatter_str<_CharT>,
6459 range_formatter<_Vt, _CharT>>;
6460 _Formatter_under _M_under;
6463#if __glibcxx_print >= 202406L
6464 template<ranges::input_range _Rg>
6465 requires (format_kind<_Rg> != range_format::disabled)
6466 constexpr bool enable_nonlocking_formatter_optimization<_Rg> =
false;
6470#undef _GLIBCXX_WIDEN
6471#undef _GLIBCXX_CONSTEXPR_FORMAT
6473_GLIBCXX_END_NAMESPACE_VERSION
6476#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.