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
43#ifdef __cpp_lib_format
64#if !__has_builtin(__builtin_toupper)
68#pragma GCC diagnostic push
69#pragma GCC diagnostic ignored "-Wpedantic"
70#pragma GCC diagnostic ignored "-Wc++23-extensions"
72namespace std _GLIBCXX_VISIBILITY(default)
74_GLIBCXX_BEGIN_NAMESPACE_VERSION
77 template<
typename _Out,
typename _CharT>
class basic_format_context;
80 template<
typename _CharT,
typename... _Args>
struct basic_format_string;
86 template<
typename _CharT>
class _Sink;
88 template<
typename _CharT>
91 template<
typename _CharT>
92 using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
94 template<
typename _CharT>
95 struct _Runtime_format_string
97 [[__gnu__::__always_inline__]]
98 _Runtime_format_string(basic_string_view<_CharT> __s) noexcept
101 _Runtime_format_string(
const _Runtime_format_string&) =
delete;
102 void operator=(
const _Runtime_format_string&) =
delete;
105 basic_string_view<_CharT> _M_str;
107 template<
typename,
typename...>
friend struct std::basic_format_string;
112 using format_context = __format::__format_context<char>;
113#ifdef _GLIBCXX_USE_WCHAR_T
114 using wformat_context = __format::__format_context<wchar_t>;
118 template<
typename _Context>
class basic_format_args;
119 using format_args = basic_format_args<format_context>;
120#ifdef _GLIBCXX_USE_WCHAR_T
121 using wformat_args = basic_format_args<wformat_context>;
126 template<
typename _Context>
127 class basic_format_arg;
133 template<
typename _CharT,
typename... _Args>
134 struct basic_format_string
136 template<
typename _Tp>
137 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
139 basic_format_string(
const _Tp& __s);
141 [[__gnu__::__always_inline__]]
142 basic_format_string(__format::_Runtime_format_string<_CharT> __s) noexcept
146 [[__gnu__::__always_inline__]]
147 constexpr basic_string_view<_CharT>
152 basic_string_view<_CharT> _M_str;
155 template<
typename... _Args>
156 using format_string = basic_format_string<char, type_identity_t<_Args>...>;
158#ifdef _GLIBCXX_USE_WCHAR_T
159 template<
typename... _Args>
161 = basic_format_string<wchar_t, type_identity_t<_Args>...>;
164#if __cpp_lib_format >= 202311L
165 [[__gnu__::__always_inline__]]
166 inline __format::_Runtime_format_string<char>
167 runtime_format(string_view __fmt)
noexcept
170#ifdef _GLIBCXX_USE_WCHAR_T
171 [[__gnu__::__always_inline__]]
172 inline __format::_Runtime_format_string<wchar_t>
173 runtime_format(wstring_view __fmt)
noexcept
181 template<
typename _Tp,
typename _CharT =
char>
184 formatter() =
delete;
185 formatter(
const formatter&) =
delete;
186 formatter& operator=(
const formatter&) =
delete;
190 class format_error :
public runtime_error
193 explicit format_error(
const string& __what) : runtime_error(__what) { }
194 explicit format_error(
const char* __what) : runtime_error(__what) { }
200 __throw_format_error(
const char* __what)
201 { _GLIBCXX_THROW_OR_ABORT(format_error(__what)); }
209 __unmatched_left_brace_in_format_string()
210 { __throw_format_error(
"format error: unmatched '{' in format string"); }
214 __unmatched_right_brace_in_format_string()
215 { __throw_format_error(
"format error: unmatched '}' in format string"); }
219 __conflicting_indexing_in_format_string()
220 { __throw_format_error(
"format error: conflicting indexing style in format string"); }
224 __invalid_arg_id_in_format_string()
225 { __throw_format_error(
"format error: invalid arg-id in format string"); }
229 __failed_to_parse_format_spec()
230 { __throw_format_error(
"format error: failed to parse format-spec"); }
232 template<
typename _CharT>
class _Scanner;
238 template<
typename _CharT>
class basic_format_parse_context;
239 using format_parse_context = basic_format_parse_context<char>;
240#ifdef _GLIBCXX_USE_WCHAR_T
241 using wformat_parse_context = basic_format_parse_context<wchar_t>;
244 template<
typename _CharT>
245 class basic_format_parse_context
248 using char_type = _CharT;
249 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
250 using iterator = const_iterator;
253 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
254 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
257 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
258 void operator=(
const basic_format_parse_context&) =
delete;
260 constexpr const_iterator
begin() const noexcept {
return _M_begin; }
261 constexpr const_iterator
end() const noexcept {
return _M_end; }
264 advance_to(const_iterator __it)
noexcept
270 if (_M_indexing == _Manual)
271 __format::__conflicting_indexing_in_format_string();
276 if (std::is_constant_evaluated())
277 if (_M_next_arg_id == _M_num_args)
278 __format::__invalid_arg_id_in_format_string();
279 return _M_next_arg_id++;
283 check_arg_id(
size_t __id)
285 if (_M_indexing == _Auto)
286 __format::__conflicting_indexing_in_format_string();
287 _M_indexing = _Manual;
289 if (std::is_constant_evaluated())
290 if (__id >= _M_num_args)
291 __format::__invalid_arg_id_in_format_string();
294#if __cpp_lib_format >= 202305L
295 template<
typename... _Ts>
297 check_dynamic_spec(
size_t __id)
noexcept
299 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
300 "template arguments for check_dynamic_spec<Ts...>(id) "
301 "must be unique and must be one of the allowed types");
303 __check_dynamic_spec<_Ts...>(__id);
308 check_dynamic_spec_integral(
size_t __id)
noexcept
311 __check_dynamic_spec<int, unsigned,
long long,
312 unsigned long long>(__id);
317 check_dynamic_spec_string(
size_t __id)
noexcept
320 __check_dynamic_spec<const _CharT*, basic_string_view<_CharT>>(__id);
326 template<
typename _Tp,
typename... _Ts>
327 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
329 template<
typename... _Ts>
331 __valid_types_for_check_dynamic_spec()
335 if constexpr (
sizeof...(_Ts) == 0)
344 = __once<bool, _Ts...>
345 + __once<char_type, _Ts...>
346 + __once<int, _Ts...>
347 + __once<
unsigned int, _Ts...>
348 + __once<
long long int, _Ts...>
349 + __once<
unsigned long long int, _Ts...>
350 + __once<float, _Ts...>
351 + __once<double, _Ts...>
352 + __once<
long double, _Ts...>
353 + __once<
const char_type*, _Ts...>
354 + __once<basic_string_view<char_type>, _Ts...>
355 + __once<
const void*, _Ts...>;
356 return __sum ==
sizeof...(_Ts);
360 template<
typename... _Ts>
362 __check_dynamic_spec(
size_t __id)
noexcept;
365 static void __invalid_dynamic_spec(
const char*);
367 friend __format::_Scanner<_CharT>;
372 basic_format_parse_context(basic_string_view<_CharT> __fmt,
373 size_t __num_args) noexcept
374 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
380 enum _Indexing { _Unknown, _Manual, _Auto };
381 _Indexing _M_indexing = _Unknown;
382 size_t _M_next_arg_id = 0;
383 size_t _M_num_args = 0;
387 template<
typename _Tp,
template<
typename...>
class _Class>
388 constexpr bool __is_specialization_of =
false;
389 template<
template<
typename...>
class _Class,
typename... _Args>
390 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
395 template<
typename _CharT>
396 constexpr pair<unsigned short, const _CharT*>
397 __parse_integer(
const _CharT* __first,
const _CharT* __last)
399 if (__first == __last)
400 __builtin_unreachable();
402 if constexpr (is_same_v<_CharT, char>)
404 const auto __start = __first;
405 unsigned short __val = 0;
407 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
408 && __first != __start) [[likely]]
409 return {__val, __first};
413 constexpr int __n = 32;
415 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
416 __buf[__i] = __first[__i];
417 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
418 if (__ptr) [[likely]]
419 return {__v, __first + (__ptr - __buf)};
424 template<
typename _CharT>
425 constexpr pair<unsigned short, const _CharT*>
426 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
428 if (__first == __last)
429 __builtin_unreachable();
432 return {0, __first + 1};
434 if (
'1' <= *__first && *__first <=
'9')
436 const unsigned short __id = *__first -
'0';
437 const auto __next = __first + 1;
439 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
440 return {__id, __next};
442 return __format::__parse_integer(__first, __last);
450 _Pres_d = 1, _Pres_b, _Pres_B, _Pres_o, _Pres_x, _Pres_X, _Pres_c,
452 _Pres_a = 1, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F, _Pres_g, _Pres_G,
453 _Pres_p = 0, _Pres_P,
478 template<
typename _Context>
480 __int_from_arg(
const basic_format_arg<_Context>& __arg);
482 constexpr bool __is_digit(
char __c)
483 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
485 constexpr bool __is_xdigit(
char __c)
486 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
488 template<
typename _CharT>
494 unsigned _M_localized : 1;
495 unsigned _M_zero_fill : 1;
496 _WidthPrec _M_width_kind : 2;
497 _WidthPrec _M_prec_kind : 2;
498 _Pres_type _M_type : 4;
499 unsigned _M_reserved : 1;
500 unsigned _M_reserved2 : 16;
501 unsigned short _M_width;
502 unsigned short _M_prec;
503 char32_t _M_fill =
' ';
505 using iterator =
typename basic_string_view<_CharT>::iterator;
507 static constexpr _Align
508 _S_align(_CharT __c)
noexcept
512 case '<':
return _Align_left;
513 case '>':
return _Align_right;
514 case '^':
return _Align_centre;
515 default:
return _Align_default;
521 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
525 using namespace __unicode;
526 if constexpr (__literal_encoding_is_unicode<_CharT>())
529 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
532 auto __beg = __uv.begin();
533 char32_t __c = *__beg++;
534 if (__is_scalar_value(__c))
535 if (
auto __next = __beg.base(); __next != __last)
536 if (_Align __align = _S_align(*__next))
544 else if (__last - __first >= 2)
545 if (_Align __align = _S_align(__first[1]))
552 if (_Align __align = _S_align(__first[0]))
562 static constexpr _Sign
563 _S_sign(_CharT __c)
noexcept
567 case '+':
return _Sign_plus;
568 case '-':
return _Sign_minus;
569 case ' ':
return _Sign_space;
570 default:
return _Sign_default;
576 _M_parse_sign(iterator __first, iterator)
noexcept
578 if (_Sign __sign = _S_sign(*__first))
588 _M_parse_alternate_form(iterator __first, iterator)
noexcept
600 _M_parse_zero_fill(iterator __first, iterator )
noexcept
611 static constexpr iterator
612 _S_parse_width_or_precision(iterator __first, iterator __last,
613 unsigned short& __val,
bool& __arg_id,
614 basic_format_parse_context<_CharT>& __pc)
616 if (__format::__is_digit(*__first))
618 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
620 __throw_format_error(
"format error: invalid width or precision "
625 else if (*__first ==
'{')
629 if (__first == __last)
630 __format::__unmatched_left_brace_in_format_string();
632 __val = __pc.next_arg_id();
635 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
636 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
637 __format::__invalid_arg_id_in_format_string();
639 __pc.check_arg_id(__v);
642#if __cpp_lib_format >= 202305L
643 __pc.check_dynamic_spec_integral(__val);
652 _M_parse_width(iterator __first, iterator __last,
653 basic_format_parse_context<_CharT>& __pc)
655 bool __arg_id =
false;
657 __throw_format_error(
"format error: width must be non-zero in "
659 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
661 if (__next != __first)
662 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
668 _M_parse_precision(iterator __first, iterator __last,
669 basic_format_parse_context<_CharT>& __pc)
671 if (__first[0] !=
'.')
674 iterator __next = ++__first;
675 bool __arg_id =
false;
676 if (__next != __last)
677 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
679 if (__next == __first)
680 __throw_format_error(
"format error: missing precision after '.' in "
682 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
688 _M_parse_locale(iterator __first, iterator )
noexcept
698 template<
typename _Context>
700 _M_get_width(_Context& __ctx)
const
703 if (_M_width_kind == _WP_value)
705 else if (_M_width_kind == _WP_from_arg)
706 __width = __format::__int_from_arg(__ctx.arg(_M_width));
710 template<
typename _Context>
712 _M_get_precision(_Context& __ctx)
const
715 if (_M_prec_kind == _WP_value)
717 else if (_M_prec_kind == _WP_from_arg)
718 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
723 template<
typename _Int>
725 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
729 else if (__sign == _Sign_plus)
731 else if (__sign == _Sign_space)
739 template<
typename _Out,
typename _CharT>
740 requires output_iterator<_Out, const _CharT&>
742 __write(_Out __out, basic_string_view<_CharT> __str)
744 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
750 for (_CharT __c : __str)
757 template<
typename _Out,
typename _CharT>
759 __write_padded(_Out __out, basic_string_view<_CharT> __str,
760 _Align __align,
size_t __nfill,
char32_t __fill_char)
762 const size_t __buflen = 0x20;
763 _CharT __padding_chars[__buflen];
764 __padding_chars[0] = _CharT();
765 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
767 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
770 while (__n > __padding.size())
772 __o = __format::__write(
std::move(__o), __padding);
773 __n -= __padding.size();
776 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
779 size_t __l, __r, __max;
780 if (__align == _Align_centre)
783 __r = __l + (__nfill & 1);
786 else if (__align == _Align_right)
799 using namespace __unicode;
800 if constexpr (__literal_encoding_is_unicode<_CharT>())
801 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
804 const char32_t __arr[1]{ __fill_char };
805 _Utf_view<_CharT,
const char32_t(&)[1]> __v(__arr);
806 basic_string<_CharT> __padstr(__v.begin(), __v.end());
807 __padding = __padstr;
809 __out = __format::__write(
std::move(__out), __padding);
810 __out = __format::__write(
std::move(__out), __str);
812 __out = __format::__write(
std::move(__out), __padding);
816 if (__max < __buflen)
817 __padding.remove_suffix(__buflen - __max);
821 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
823 __out = __format::__write(
std::move(__out), __str);
831 template<
typename _CharT,
typename _Out>
833 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
834 size_t __estimated_width,
835 basic_format_context<_Out, _CharT>& __fc,
836 const _Spec<_CharT>& __spec,
837 _Align __align = _Align_left)
839 size_t __width = __spec._M_get_width(__fc);
841 if (__width <= __estimated_width)
842 return __format::__write(__fc.out(), __str);
844 const size_t __nfill = __width - __estimated_width;
847 __align = __spec._M_align;
849 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
854 struct _Optional_locale
856 [[__gnu__::__always_inline__]]
857 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
859 _Optional_locale(
const locale& __loc) noexcept
860 : _M_loc(__loc), _M_hasval(
true)
863 _Optional_locale(
const _Optional_locale& __l) noexcept
864 : _M_dummy(), _M_hasval(__l._M_hasval)
867 std::construct_at(&_M_loc, __l._M_loc);
871 operator=(
const _Optional_locale& __l)
noexcept
883 else if (__l._M_hasval)
885 std::construct_at(&_M_loc, __l._M_loc);
891 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
894 operator=(locale&& __loc)
noexcept
900 std::construct_at(&_M_loc,
std::move(__loc));
911 std::construct_at(&_M_loc);
917 bool has_value() const noexcept {
return _M_hasval; }
920 char _M_dummy =
'\0';
923 bool _M_hasval =
false;
926#ifdef _GLIBCXX_USE_WCHAR_T
927 template<
typename _CharT>
928 concept __char = same_as<_CharT, char> || same_as<_CharT, wchar_t>;
930 template<
typename _CharT>
931 concept __char = same_as<_CharT, char>;
934 template<__
char _CharT>
935 struct __formatter_str
937 constexpr typename basic_format_parse_context<_CharT>::iterator
938 parse(basic_format_parse_context<_CharT>& __pc)
940 auto __first = __pc.begin();
941 const auto __last = __pc.end();
942 _Spec<_CharT> __spec{};
944 auto __finalize = [
this, &__spec] {
948 auto __finished = [&] {
949 if (__first == __last || *__first ==
'}')
960 __first = __spec._M_parse_fill_and_align(__first, __last);
964 __first = __spec._M_parse_width(__first, __last, __pc);
968 __first = __spec._M_parse_precision(__first, __last, __pc);
974#if __cpp_lib_format_ranges
975 else if (*__first ==
'?')
977 __spec._M_type = _Pres_esc;
985 __format::__failed_to_parse_format_spec();
988 template<
typename _Out>
990 format(basic_string_view<_CharT> __s,
991 basic_format_context<_Out, _CharT>& __fc)
const
993 if (_M_spec._M_type == _Pres_esc)
998 if (_M_spec._M_width_kind == _WP_none
999 && _M_spec._M_prec_kind == _WP_none)
1000 return __format::__write(__fc.out(), __s);
1002 size_t __estimated_width;
1003 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1005 if (_M_spec._M_prec_kind != _WP_none)
1007 size_t __prec = _M_spec._M_get_precision(__fc);
1008 __estimated_width = __unicode::__truncate(__s, __prec);
1011 __estimated_width = __unicode::__field_width(__s);
1015 __s = __s.substr(0, _M_spec._M_get_precision(__fc));
1016 __estimated_width = __s.size();
1019 return __format::__write_padded_as_spec(__s, __estimated_width,
1023#if __cpp_lib_format_ranges
1025 set_debug_format() noexcept
1026 { _M_spec._M_type = _Pres_esc; }
1030 _Spec<_CharT> _M_spec{};
1033 template<__
char _CharT>
1034 struct __formatter_int
1038 static constexpr _Pres_type _AsInteger = _Pres_d;
1039 static constexpr _Pres_type _AsBool = _Pres_s;
1040 static constexpr _Pres_type _AsChar = _Pres_c;
1042 constexpr typename basic_format_parse_context<_CharT>::iterator
1043 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1045 _Spec<_CharT> __spec{};
1046 __spec._M_type = __type;
1048 const auto __last = __pc.end();
1049 auto __first = __pc.begin();
1051 auto __finalize = [
this, &__spec] {
1055 auto __finished = [&] {
1056 if (__first == __last || *__first ==
'}')
1067 __first = __spec._M_parse_fill_and_align(__first, __last);
1071 __first = __spec._M_parse_sign(__first, __last);
1075 __first = __spec._M_parse_alternate_form(__first, __last);
1079 __first = __spec._M_parse_zero_fill(__first, __last);
1083 __first = __spec._M_parse_width(__first, __last, __pc);
1087 __first = __spec._M_parse_locale(__first, __last);
1094 __spec._M_type = _Pres_b;
1098 __spec._M_type = _Pres_B;
1104 if (__type != _AsBool)
1106 __spec._M_type = _Pres_c;
1111 __spec._M_type = _Pres_d;
1115 __spec._M_type = _Pres_o;
1119 __spec._M_type = _Pres_x;
1123 __spec._M_type = _Pres_X;
1127 if (__type == _AsBool)
1129 __spec._M_type = _Pres_s;
1133#if __cpp_lib_format_ranges
1135 if (__type == _AsChar)
1137 __spec._M_type = _Pres_esc;
1147 __format::__failed_to_parse_format_spec();
1150 template<
typename _Tp>
1151 constexpr typename basic_format_parse_context<_CharT>::iterator
1152 _M_parse(basic_format_parse_context<_CharT>& __pc)
1154 if constexpr (is_same_v<_Tp, bool>)
1156 auto __end = _M_do_parse(__pc, _AsBool);
1157 if (_M_spec._M_type == _Pres_s)
1158 if (_M_spec._M_sign || _M_spec._M_alt || _M_spec._M_zero_fill)
1159 __throw_format_error(
"format error: format-spec contains "
1160 "invalid formatting options for "
1164 else if constexpr (__char<_Tp>)
1166 auto __end = _M_do_parse(__pc, _AsChar);
1167 if (_M_spec._M_type == _Pres_c || _M_spec._M_type == _Pres_esc)
1168 if (_M_spec._M_sign || _M_spec._M_alt || _M_spec._M_zero_fill
1170 __throw_format_error(
"format error: format-spec contains "
1171 "invalid formatting options for "
1176 return _M_do_parse(__pc, _AsInteger);
1179 template<
typename _Int,
typename _Out>
1180 typename basic_format_context<_Out, _CharT>::iterator
1181 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1183 if (_M_spec._M_type == _Pres_c)
1184 return _M_format_character(_S_to_character(__i), __fc);
1186 char __buf[
sizeof(_Int) * __CHAR_BIT__ + 3];
1187 to_chars_result __res{};
1189 string_view __base_prefix;
1190 make_unsigned_t<_Int> __u;
1192 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1196 char* __start = __buf + 3;
1197 char*
const __end = __buf +
sizeof(__buf);
1198 char*
const __start_digits = __start;
1200 switch (_M_spec._M_type)
1204 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1205 __res = to_chars(__start, __end, __u, 2);
1209 return _M_format_character(_S_to_character(__i), __fc);
1215 __res = to_chars(__start, __end, __u, 10);
1219 __base_prefix =
"0";
1220 __res = to_chars(__start, __end, __u, 8);
1224 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1225 __res = to_chars(__start, __end, __u, 16);
1226 if (_M_spec._M_type == _Pres_X)
1227 for (
auto __p = __start; __p != __res.ptr; ++__p)
1228#
if __has_builtin(__builtin_toupper)
1229 *__p = __builtin_toupper(*__p);
1235 __builtin_unreachable();
1238 if (_M_spec._M_alt && __base_prefix.size())
1240 __start -= __base_prefix.size();
1241 __builtin_memcpy(__start, __base_prefix.data(),
1242 __base_prefix.size());
1244 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1246 return _M_format_int(string_view(__start, __res.ptr - __start),
1247 __start_digits - __start, __fc);
1250 template<
typename _Out>
1251 typename basic_format_context<_Out, _CharT>::iterator
1252 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1254 if (_M_spec._M_type == _Pres_c)
1255 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1256 if (_M_spec._M_type != _Pres_s)
1257 return format(
static_cast<unsigned char>(__i), __fc);
1259 basic_string<_CharT> __s;
1261 if (_M_spec._M_localized) [[unlikely]]
1263 auto& __np = std::use_facet<numpunct<_CharT>>(__fc.locale());
1264 __s = __i ? __np.truename() : __np.falsename();
1265 __est_width = __s.size();
1269 if constexpr (is_same_v<char, _CharT>)
1270 __s = __i ?
"true" :
"false";
1272 __s = __i ? L
"true" : L
"false";
1273 __est_width = __s.size();
1276 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1280 [[__gnu__::__always_inline__]]
1282 _S_character_width(_CharT __c)
1285 if constexpr (
sizeof(_CharT) > 1u &&
1286 __unicode::__literal_encoding_is_unicode<_CharT>())
1287 return __unicode::__field_width(__c);
1292 template<
typename _Out>
1293 typename basic_format_context<_Out, _CharT>::iterator
1294 _M_format_character(_CharT __c,
1295 basic_format_context<_Out, _CharT>& __fc)
const
1297 return __format::__write_padded_as_spec({&__c, 1u},
1298 _S_character_width(__c),
1302 template<
typename _Int>
1304 _S_to_character(_Int __i)
1307 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1309 if (_Traits::__min <= __i && __i <= _Traits::__max)
1310 return static_cast<_CharT
>(__i);
1312 else if constexpr (is_signed_v<_Int>)
1314 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1315 return static_cast<_CharT
>(__i);
1317 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1318 return static_cast<_CharT
>(__i);
1319 __throw_format_error(
"format error: integer not representable as "
1323 template<
typename _Out>
1324 typename basic_format_context<_Out, _CharT>::iterator
1325 _M_format_int(string_view __narrow_str,
size_t __prefix_len,
1326 basic_format_context<_Out, _CharT>& __fc)
const
1328 size_t __width = _M_spec._M_get_width(__fc);
1330 basic_string_view<_CharT> __str;
1331 if constexpr (is_same_v<char, _CharT>)
1332 __str = __narrow_str;
1333#ifdef _GLIBCXX_USE_WCHAR_T
1336 size_t __n = __narrow_str.size();
1337 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
1338 std::__to_wstring_numeric(__narrow_str.data(), __n, __p);
1343 if (_M_spec._M_localized)
1345 const auto& __l = __fc.locale();
1346 if (__l.name() !=
"C")
1348 auto& __np = use_facet<numpunct<_CharT>>(__l);
1349 string __grp = __np.grouping();
1352 size_t __n = __str.size() - __prefix_len;
1353 auto __p = (_CharT*)__builtin_alloca(2 * __n
1356 auto __s = __str.data();
1357 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1358 __s += __prefix_len;
1359 auto __end = std::__add_grouping(__p + __prefix_len,
1360 __np.thousands_sep(),
1364 __str = {__p, size_t(__end - __p)};
1369 if (__width <= __str.size())
1370 return __format::__write(__fc.out(), __str);
1372 char32_t __fill_char = _M_spec._M_fill;
1373 _Align __align = _M_spec._M_align;
1375 size_t __nfill = __width - __str.size();
1376 auto __out = __fc.out();
1377 if (__align == _Align_default)
1379 __align = _Align_right;
1380 if (_M_spec._M_zero_fill)
1382 __fill_char = _CharT(
'0');
1384 if (__prefix_len != 0)
1386 __out = __format::__write(
std::move(__out),
1387 __str.substr(0, __prefix_len));
1388 __str.remove_prefix(__prefix_len);
1392 __fill_char = _CharT(
' ');
1394 return __format::__write_padded(
std::move(__out), __str,
1395 __align, __nfill, __fill_char);
1398#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__
1399 template<
typename _Tp>
1401 =
typename __conditional_t<(
sizeof(_Tp) <=
sizeof(
long long)),
1403 type_identity<unsigned __int128>>::type;
1406 template<
typename _Int>
1407 static to_chars_result
1408 to_chars(
char* __first,
char* __last, _Int __value,
int __base)
1409 {
return std::__to_chars_i<_Int>(__first, __last, __value, __base); }
1412 _Spec<_CharT> _M_spec{};
1423#undef _GLIBCXX_FORMAT_F128
1425#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1428 using __float128_t = __ieee128;
1429# define _GLIBCXX_FORMAT_F128 1
1431#ifdef __LONG_DOUBLE_IEEE128__
1435 to_chars(
char*,
char*, __ibm128)
noexcept
1436 __asm(
"_ZSt8to_charsPcS_e");
1439 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1440 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1443 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1444 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1445#elif __cplusplus == 202002L
1447 to_chars(
char*,
char*, __ieee128)
noexcept
1448 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1451 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1452 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1455 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1456 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1459#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1462 using __float128_t =
long double;
1463# define _GLIBCXX_FORMAT_F128 1
1465#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1468 using __float128_t = _Float128;
1469# define _GLIBCXX_FORMAT_F128 2
1471# if __cplusplus == 202002L
1475 to_chars(
char*,
char*, _Float128)
noexcept
1476# if _GLIBCXX_INLINE_VERSION
1477 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1479 __asm(
"_ZSt8to_charsPcS_DF128_");
1483 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1484# if _GLIBCXX_INLINE_VERSION
1485 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1487 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
1491 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
1492# if _GLIBCXX_INLINE_VERSION
1493 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
1495 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
1500 using std::to_chars;
1503 template<
typename _Tp>
1504 concept __formattable_float
1505 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
1506 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
1508 template<__
char _CharT>
1509 struct __formatter_fp
1511 constexpr typename basic_format_parse_context<_CharT>::iterator
1512 parse(basic_format_parse_context<_CharT>& __pc)
1514 _Spec<_CharT> __spec{};
1515 const auto __last = __pc.end();
1516 auto __first = __pc.begin();
1518 auto __finalize = [
this, &__spec] {
1522 auto __finished = [&] {
1523 if (__first == __last || *__first ==
'}')
1534 __first = __spec._M_parse_fill_and_align(__first, __last);
1538 __first = __spec._M_parse_sign(__first, __last);
1542 __first = __spec._M_parse_alternate_form(__first, __last);
1546 __first = __spec._M_parse_zero_fill(__first, __last);
1550 if (__first[0] !=
'.')
1552 __first = __spec._M_parse_width(__first, __last, __pc);
1557 __first = __spec._M_parse_precision(__first, __last, __pc);
1561 __first = __spec._M_parse_locale(__first, __last);
1568 __spec._M_type = _Pres_a;
1572 __spec._M_type = _Pres_A;
1576 __spec._M_type = _Pres_e;
1580 __spec._M_type = _Pres_E;
1584 __spec._M_type = _Pres_f;
1588 __spec._M_type = _Pres_F;
1592 __spec._M_type = _Pres_g;
1596 __spec._M_type = _Pres_G;
1604 __format::__failed_to_parse_format_spec();
1607 template<
typename _Fp,
typename _Out>
1608 typename basic_format_context<_Out, _CharT>::iterator
1609 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
1613 to_chars_result __res{};
1616 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
1618 __prec = _M_spec._M_get_precision(__fc);
1620 char* __start = __buf + 1;
1621 char* __end = __buf +
sizeof(__buf);
1624 bool __upper =
false;
1625 bool __trailing_zeros =
false;
1628 switch (_M_spec._M_type)
1635 if (_M_spec._M_type != _Pres_A)
1637 __fmt = chars_format::hex;
1645 __fmt = chars_format::scientific;
1652 __fmt = chars_format::fixed;
1659 __trailing_zeros =
true;
1661 __fmt = chars_format::general;
1665 __fmt = chars_format::general;
1668 __builtin_unreachable();
1672 auto __to_chars = [&](
char* __b,
char* __e) {
1674 return __format::to_chars(__b, __e, __v, __fmt, __prec);
1675 else if (__fmt != chars_format{})
1676 return __format::to_chars(__b, __e, __v, __fmt);
1678 return __format::to_chars(__b, __e, __v);
1682 __res = __to_chars(__start, __end);
1684 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
1688 size_t __guess = 8 + __prec;
1689 if (__fmt == chars_format::fixed)
1691 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
1692 || is_same_v<_Fp, long double>)
1697 if constexpr (is_same_v<_Fp, float>)
1698 __builtin_frexpf(__v, &__exp);
1699 else if constexpr (is_same_v<_Fp, double>)
1700 __builtin_frexp(__v, &__exp);
1701 else if constexpr (is_same_v<_Fp, long double>)
1702 __builtin_frexpl(__v, &__exp);
1704 __guess += 1U + __exp * 4004U / 13301U;
1707 __guess += numeric_limits<_Fp>::max_exponent10;
1709 if (__guess <=
sizeof(__buf)) [[unlikely]]
1710 __guess =
sizeof(__buf) * 2;
1719 auto __overwrite = [&__to_chars, &__res] (
char* __p,
size_t __n)
1721 __res = __to_chars(__p + 1, __p + __n - 1);
1722 return __res.ec == errc{} ? __res.ptr - __p : 0;
1727 __start = __dynbuf.
data() + 1;
1728 __end = __dynbuf.
data() + __dynbuf.
size();
1730 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
1736 for (
char* __p = __start; __p != __res.ptr; ++__p)
1740 bool __have_sign =
true;
1742 if (!__builtin_signbit(__v))
1744 if (_M_spec._M_sign == _Sign_plus)
1746 else if (_M_spec._M_sign == _Sign_space)
1749 __have_sign =
false;
1752 string_view __narrow_str(__start, __res.ptr - __start);
1756 if (_M_spec._M_alt && __builtin_isfinite(__v))
1758 string_view __s = __narrow_str;
1762 size_t __d = __s.find(
'.');
1763 if (__d != __s.npos)
1765 __p = __s.find(__expc, __d + 1);
1766 if (__p == __s.npos)
1770 if (__trailing_zeros)
1773 if (__s[__have_sign] !=
'0')
1775 __sigfigs = __p - __have_sign - 1;
1780 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
1785 __p = __s.find(__expc);
1786 if (__p == __s.npos)
1789 __sigfigs = __d - __have_sign;
1792 if (__trailing_zeros && __prec != 0)
1797 __z = __prec - __sigfigs;
1800 if (
size_t __extras =
int(__d == __p) + __z)
1802 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
1806 __builtin_memmove(__start + __p + __extras,
1810 __start[__p++] =
'.';
1811 __builtin_memset(__start + __p,
'0', __z);
1812 __narrow_str = {__s.data(), __s.size() + __extras};
1816 __dynbuf.
reserve(__s.size() + __extras);
1817 if (__dynbuf.
empty())
1819 __dynbuf = __s.
substr(0, __p);
1823 __dynbuf.
append(__z,
'0');
1824 __dynbuf.
append(__s.substr(__p));
1828 __dynbuf.
insert(__p, __extras,
'0');
1830 __dynbuf[__p] =
'.';
1832 __narrow_str = __dynbuf;
1837 basic_string<_CharT> __wstr;
1838 basic_string_view<_CharT> __str;
1839 if constexpr (is_same_v<_CharT, char>)
1840 __str = __narrow_str;
1841#ifdef _GLIBCXX_USE_WCHAR_T
1844 __wstr = std::__to_wstring_numeric(__narrow_str);
1849 if (_M_spec._M_localized && __builtin_isfinite(__v))
1851 __wstr = _M_localize(__str, __expc, __fc.locale());
1852 if (!__wstr.empty())
1856 size_t __width = _M_spec._M_get_width(__fc);
1858 if (__width <= __str.size())
1859 return __format::__write(__fc.out(), __str);
1861 char32_t __fill_char = _M_spec._M_fill;
1862 _Align __align = _M_spec._M_align;
1864 size_t __nfill = __width - __str.size();
1865 auto __out = __fc.out();
1866 if (__align == _Align_default)
1868 __align = _Align_right;
1869 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
1871 __fill_char = _CharT(
'0');
1873 if (!__format::__is_xdigit(__narrow_str[0]))
1875 *__out++ = __str[0];
1876 __str.remove_prefix(1);
1880 __fill_char = _CharT(
' ');
1882 return __format::__write_padded(
std::move(__out), __str,
1883 __align, __nfill, __fill_char);
1887 basic_string<_CharT>
1888 _M_localize(basic_string_view<_CharT> __str,
char __expc,
1889 const locale& __loc)
const
1891 basic_string<_CharT> __lstr;
1893 if (__loc == locale::classic())
1896 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
1897 const _CharT __point = __np.decimal_point();
1898 const string __grp = __np.grouping();
1900 _CharT __dot, __exp;
1901 if constexpr (is_same_v<_CharT, char>)
1924 __builtin_unreachable();
1928 if (__grp.empty() && __point == __dot)
1931 size_t __d = __str.find(__dot);
1932 size_t __e = min(__d, __str.find(__exp));
1933 if (__e == __str.npos)
1935 const size_t __r = __str.size() - __e;
1936 auto __overwrite = [&](_CharT* __p, size_t) {
1938 auto __end = std::__add_grouping(__p, __np.thousands_sep(),
1939 __grp.data(), __grp.size(),
1940 __str.data(), __str.data() + __e);
1943 if (__d != __str.npos)
1949 const size_t __rlen = __str.size() - __e;
1951 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
1954 return (__end - __p);
1956 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
1960 _Spec<_CharT> _M_spec{};
1967 template<__format::__
char _CharT>
1968 struct formatter<_CharT, _CharT>
1970 formatter() =
default;
1972 constexpr typename basic_format_parse_context<_CharT>::iterator
1973 parse(basic_format_parse_context<_CharT>& __pc)
1975 return _M_f.template _M_parse<_CharT>(__pc);
1978 template<
typename _Out>
1979 typename basic_format_context<_Out, _CharT>::iterator
1980 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
1982 if (_M_f._M_spec._M_type == __format::_Pres_none
1983 || _M_f._M_spec._M_type == __format::_Pres_c)
1984 return _M_f._M_format_character(__u, __fc);
1985 else if (_M_f._M_spec._M_type == __format::_Pres_esc)
1991 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
1994#if __cpp_lib_format_ranges
1996 set_debug_format() noexcept
1997 { _M_f._M_spec._M_type = __format::_Pres_esc; }
2001 __format::__formatter_int<_CharT> _M_f;
2004#ifdef _GLIBCXX_USE_WCHAR_T
2007 struct formatter<char, wchar_t>
2009 formatter() =
default;
2011 constexpr typename basic_format_parse_context<wchar_t>::iterator
2012 parse(basic_format_parse_context<wchar_t>& __pc)
2014 return _M_f._M_parse<
char>(__pc);
2017 template<
typename _Out>
2018 typename basic_format_context<_Out, wchar_t>::iterator
2019 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2021 if (_M_f._M_spec._M_type == __format::_Pres_none
2022 || _M_f._M_spec._M_type == __format::_Pres_c)
2023 return _M_f._M_format_character(__u, __fc);
2024 else if (_M_f._M_spec._M_type == __format::_Pres_esc)
2030 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2033#if __cpp_lib_format_ranges
2035 set_debug_format() noexcept
2036 { _M_f._M_spec._M_type = __format::_Pres_esc; }
2040 __format::__formatter_int<wchar_t> _M_f;
2047 template<__format::__
char _CharT>
2048 struct formatter<_CharT*, _CharT>
2050 formatter() =
default;
2052 [[__gnu__::__always_inline__]]
2053 constexpr typename basic_format_parse_context<_CharT>::iterator
2054 parse(basic_format_parse_context<_CharT>& __pc)
2055 {
return _M_f.parse(__pc); }
2057 template<
typename _Out>
2058 [[__gnu__::__nonnull__]]
2059 typename basic_format_context<_Out, _CharT>::iterator
2060 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2061 {
return _M_f.format(__u, __fc); }
2063#if __cpp_lib_format_ranges
2064 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2068 __format::__formatter_str<_CharT> _M_f;
2071 template<__format::__
char _CharT>
2072 struct formatter<const _CharT*, _CharT>
2074 formatter() =
default;
2076 [[__gnu__::__always_inline__]]
2077 constexpr typename basic_format_parse_context<_CharT>::iterator
2078 parse(basic_format_parse_context<_CharT>& __pc)
2079 {
return _M_f.parse(__pc); }
2081 template<
typename _Out>
2082 [[__gnu__::__nonnull__]]
2083 typename basic_format_context<_Out, _CharT>::iterator
2084 format(
const _CharT* __u,
2085 basic_format_context<_Out, _CharT>& __fc)
const
2086 {
return _M_f.format(__u, __fc); }
2088#if __cpp_lib_format_ranges
2089 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2093 __format::__formatter_str<_CharT> _M_f;
2096 template<__format::__
char _CharT,
size_t _Nm>
2097 struct formatter<_CharT[_Nm], _CharT>
2099 formatter() =
default;
2101 [[__gnu__::__always_inline__]]
2102 constexpr typename basic_format_parse_context<_CharT>::iterator
2103 parse(basic_format_parse_context<_CharT>& __pc)
2104 {
return _M_f.parse(__pc); }
2106 template<
typename _Out>
2107 typename basic_format_context<_Out, _CharT>::iterator
2108 format(
const _CharT (&__u)[_Nm],
2109 basic_format_context<_Out, _CharT>& __fc)
const
2110 {
return _M_f.format({__u, _Nm}, __fc); }
2112#if __cpp_lib_format_ranges
2113 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2117 __format::__formatter_str<_CharT> _M_f;
2120 template<
typename _Traits,
typename _Alloc>
2121 struct formatter<basic_string<char, _Traits, _Alloc>, char>
2123 formatter() =
default;
2125 [[__gnu__::__always_inline__]]
2126 constexpr typename basic_format_parse_context<char>::iterator
2127 parse(basic_format_parse_context<char>& __pc)
2128 {
return _M_f.parse(__pc); }
2130 template<
typename _Out>
2131 typename basic_format_context<_Out, char>::iterator
2132 format(
const basic_string<char, _Traits, _Alloc>& __u,
2133 basic_format_context<_Out, char>& __fc)
const
2134 {
return _M_f.format(__u, __fc); }
2136#if __cpp_lib_format_ranges
2137 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2141 __format::__formatter_str<char> _M_f;
2144#ifdef _GLIBCXX_USE_WCHAR_T
2145 template<
typename _Traits,
typename _Alloc>
2146 struct formatter<basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2148 formatter() =
default;
2150 [[__gnu__::__always_inline__]]
2151 constexpr typename basic_format_parse_context<wchar_t>::iterator
2152 parse(basic_format_parse_context<wchar_t>& __pc)
2153 {
return _M_f.parse(__pc); }
2155 template<
typename _Out>
2156 typename basic_format_context<_Out, wchar_t>::iterator
2157 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2158 basic_format_context<_Out, wchar_t>& __fc)
const
2159 {
return _M_f.format(__u, __fc); }
2161#if __cpp_lib_format_ranges
2162 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2166 __format::__formatter_str<wchar_t> _M_f;
2170 template<
typename _Traits>
2171 struct formatter<basic_string_view<char, _Traits>, char>
2173 formatter() =
default;
2175 [[__gnu__::__always_inline__]]
2176 constexpr typename basic_format_parse_context<char>::iterator
2177 parse(basic_format_parse_context<char>& __pc)
2178 {
return _M_f.parse(__pc); }
2180 template<
typename _Out>
2181 typename basic_format_context<_Out, char>::iterator
2182 format(basic_string_view<char, _Traits> __u,
2183 basic_format_context<_Out, char>& __fc)
const
2184 {
return _M_f.format(__u, __fc); }
2186#if __cpp_lib_format_ranges
2187 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2191 __format::__formatter_str<char> _M_f;
2194#ifdef _GLIBCXX_USE_WCHAR_T
2195 template<
typename _Traits>
2196 struct formatter<basic_string_view<wchar_t, _Traits>, wchar_t>
2198 formatter() =
default;
2200 [[__gnu__::__always_inline__]]
2201 constexpr typename basic_format_parse_context<wchar_t>::iterator
2202 parse(basic_format_parse_context<wchar_t>& __pc)
2203 {
return _M_f.parse(__pc); }
2205 template<
typename _Out>
2206 typename basic_format_context<_Out, wchar_t>::iterator
2207 format(basic_string_view<wchar_t, _Traits> __u,
2208 basic_format_context<_Out, wchar_t>& __fc)
const
2209 {
return _M_f.format(__u, __fc); }
2211#if __cpp_lib_format_ranges
2212 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2216 __format::__formatter_str<wchar_t> _M_f;
2226 template<
typename _Tp>
2227 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2229#if defined __SIZEOF_INT128__
2230 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2231 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2235 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2236 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2237#ifdef _GLIBCXX_USE_CHAR8_T
2238 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2240 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2241 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2246 template<
typename _Tp, __format::__
char _CharT>
2247 requires __format::__is_formattable_integer<_Tp>
2248 struct formatter<_Tp, _CharT>
2250 formatter() =
default;
2252 [[__gnu__::__always_inline__]]
2253 constexpr typename basic_format_parse_context<_CharT>::iterator
2254 parse(basic_format_parse_context<_CharT>& __pc)
2256 return _M_f.template _M_parse<_Tp>(__pc);
2259 template<
typename _Out>
2260 typename basic_format_context<_Out, _CharT>::iterator
2261 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2262 {
return _M_f.format(__u, __fc); }
2265 __format::__formatter_int<_CharT> _M_f;
2268#if defined __glibcxx_to_chars
2270 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2271 struct formatter<_Tp, _CharT>
2273 formatter() =
default;
2275 [[__gnu__::__always_inline__]]
2276 constexpr typename basic_format_parse_context<_CharT>::iterator
2277 parse(basic_format_parse_context<_CharT>& __pc)
2278 {
return _M_f.parse(__pc); }
2280 template<
typename _Out>
2281 typename basic_format_context<_Out, _CharT>::iterator
2282 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2283 {
return _M_f.format(__u, __fc); }
2286 __format::__formatter_fp<_CharT> _M_f;
2289#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2291 template<__format::__
char _CharT>
2292 struct formatter<long double, _CharT>
2294 formatter() =
default;
2296 [[__gnu__::__always_inline__]]
2297 constexpr typename basic_format_parse_context<_CharT>::iterator
2298 parse(basic_format_parse_context<_CharT>& __pc)
2299 {
return _M_f.parse(__pc); }
2301 template<
typename _Out>
2302 typename basic_format_context<_Out, _CharT>::iterator
2303 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2304 {
return _M_f.format((
double)__u, __fc); }
2307 __format::__formatter_fp<_CharT> _M_f;
2311#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2313 template<__format::__
char _CharT>
2314 struct formatter<_Float16, _CharT>
2316 formatter() =
default;
2318 [[__gnu__::__always_inline__]]
2319 constexpr typename basic_format_parse_context<_CharT>::iterator
2320 parse(basic_format_parse_context<_CharT>& __pc)
2321 {
return _M_f.parse(__pc); }
2323 template<
typename _Out>
2324 typename basic_format_context<_Out, _CharT>::iterator
2325 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
2326 {
return _M_f.format((
float)__u, __fc); }
2329 __format::__formatter_fp<_CharT> _M_f;
2333#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2335 template<__format::__
char _CharT>
2336 struct formatter<_Float32, _CharT>
2338 formatter() =
default;
2340 [[__gnu__::__always_inline__]]
2341 constexpr typename basic_format_parse_context<_CharT>::iterator
2342 parse(basic_format_parse_context<_CharT>& __pc)
2343 {
return _M_f.parse(__pc); }
2345 template<
typename _Out>
2346 typename basic_format_context<_Out, _CharT>::iterator
2347 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
2348 {
return _M_f.format((
float)__u, __fc); }
2351 __format::__formatter_fp<_CharT> _M_f;
2355#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2357 template<__format::__
char _CharT>
2358 struct formatter<_Float64, _CharT>
2360 formatter() =
default;
2362 [[__gnu__::__always_inline__]]
2363 constexpr typename basic_format_parse_context<_CharT>::iterator
2364 parse(basic_format_parse_context<_CharT>& __pc)
2365 {
return _M_f.parse(__pc); }
2367 template<
typename _Out>
2368 typename basic_format_context<_Out, _CharT>::iterator
2369 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
2370 {
return _M_f.format((
double)__u, __fc); }
2373 __format::__formatter_fp<_CharT> _M_f;
2377#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128 == 1
2379 template<__format::__
char _CharT>
2380 struct formatter<_Float128, _CharT>
2382 formatter() =
default;
2384 [[__gnu__::__always_inline__]]
2385 constexpr typename basic_format_parse_context<_CharT>::iterator
2386 parse(basic_format_parse_context<_CharT>& __pc)
2387 {
return _M_f.parse(__pc); }
2389 template<
typename _Out>
2390 typename basic_format_context<_Out, _CharT>::iterator
2391 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
2392 {
return _M_f.format((__format::__float128_t)__u, __fc); }
2395 __format::__formatter_fp<_CharT> _M_f;
2399#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2401 template<__format::__
char _CharT>
2402 struct formatter<
__gnu_cxx::__bfloat16_t, _CharT>
2404 formatter() =
default;
2406 [[__gnu__::__always_inline__]]
2407 constexpr typename basic_format_parse_context<_CharT>::iterator
2408 parse(basic_format_parse_context<_CharT>& __pc)
2409 {
return _M_f.parse(__pc); }
2411 template<
typename _Out>
2412 typename basic_format_context<_Out, _CharT>::iterator
2413 format(__gnu_cxx::__bfloat16_t __u,
2414 basic_format_context<_Out, _CharT>& __fc)
const
2415 {
return _M_f.format((
float)__u, __fc); }
2418 __format::__formatter_fp<_CharT> _M_f;
2426 template<__format::__
char _CharT>
2427 struct formatter<const void*, _CharT>
2429 formatter() =
default;
2431 constexpr typename basic_format_parse_context<_CharT>::iterator
2432 parse(basic_format_parse_context<_CharT>& __pc)
2434 __format::_Spec<_CharT> __spec{};
2435 const auto __last = __pc.end();
2436 auto __first = __pc.begin();
2438 auto __finalize = [
this, &__spec] {
2442 auto __finished = [&] {
2443 if (__first == __last || *__first ==
'}')
2454 __first = __spec._M_parse_fill_and_align(__first, __last);
2460#if __glibcxx_format >= 202304L
2461 __first = __spec._M_parse_zero_fill(__first, __last);
2466 __first = __spec._M_parse_width(__first, __last, __pc);
2468 if (__first != __last)
2470 if (*__first ==
'p')
2472#if __glibcxx_format >= 202304L
2473 else if (*__first ==
'P')
2475 __spec._M_type = __format::_Pres_P;
2484 __format::__failed_to_parse_format_spec();
2487 template<
typename _Out>
2488 typename basic_format_context<_Out, _CharT>::iterator
2489 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2491 auto __u =
reinterpret_cast<__UINTPTR_TYPE__
>(__v);
2492 char __buf[2 +
sizeof(__v) * 2];
2493 auto [__ptr, __ec] = std::to_chars(__buf + 2,
std::end(__buf),
2495 int __n = __ptr - __buf;
2498#if __glibcxx_format >= 202304L
2499 if (_M_spec._M_type == __format::_Pres_P)
2502 for (
auto __p = __buf + 2; __p != __ptr; ++__p)
2503#
if __has_builtin(__builtin_toupper)
2504 *__p = __builtin_toupper(*__p);
2511 basic_string_view<_CharT> __str;
2512 if constexpr (is_same_v<_CharT, char>)
2513 __str = string_view(__buf, __n);
2514#ifdef _GLIBCXX_USE_WCHAR_T
2517 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
2518 std::__to_wstring_numeric(__buf, __n, __p);
2519 __str = wstring_view(__p, __n);
2523#if __glibcxx_format >= 202304L
2524 if (_M_spec._M_zero_fill)
2526 size_t __width = _M_spec._M_get_width(__fc);
2527 if (__width <= __str.size())
2528 return __format::__write(__fc.out(), __str);
2530 auto __out = __fc.out();
2532 __out = __format::__write(
std::move(__out), __str.substr(0, 2));
2533 __str.remove_prefix(2);
2534 size_t __nfill = __width - __n;
2535 return __format::__write_padded(
std::move(__out), __str,
2536 __format::_Align_right,
2537 __nfill, _CharT(
'0'));
2541 return __format::__write_padded_as_spec(__str, __n, __fc, _M_spec,
2542 __format::_Align_right);
2546 __format::_Spec<_CharT> _M_spec{};
2549 template<__format::__
char _CharT>
2550 struct formatter<void*, _CharT>
2552 formatter() =
default;
2554 [[__gnu__::__always_inline__]]
2555 constexpr typename basic_format_parse_context<_CharT>::iterator
2556 parse(basic_format_parse_context<_CharT>& __pc)
2557 {
return _M_f.parse(__pc); }
2559 template<
typename _Out>
2560 typename basic_format_context<_Out, _CharT>::iterator
2561 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2562 {
return _M_f.format(__v, __fc); }
2565 formatter<const void*, _CharT> _M_f;
2568 template<__format::__
char _CharT>
2569 struct formatter<nullptr_t, _CharT>
2571 formatter() =
default;
2573 [[__gnu__::__always_inline__]]
2574 constexpr typename basic_format_parse_context<_CharT>::iterator
2575 parse(basic_format_parse_context<_CharT>& __pc)
2576 {
return _M_f.parse(__pc); }
2578 template<
typename _Out>
2579 typename basic_format_context<_Out, _CharT>::iterator
2580 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
2581 {
return _M_f.format(
nullptr, __fc); }
2584 formatter<const void*, _CharT> _M_f;
2588#if defined _GLIBCXX_USE_WCHAR_T && __cpp_lib_format_ranges
2592 namespace __format {
struct __disabled; }
2596 struct formatter<char*,
wchar_t>
2597 :
private formatter<__format::__disabled, wchar_t> { };
2599 struct formatter<const char*,
wchar_t>
2600 :
private formatter<__format::__disabled, wchar_t> { };
2601 template<
size_t _Nm>
2602 struct formatter<char[_Nm], wchar_t>
2603 :
private formatter<__format::__disabled, wchar_t> { };
2604 template<
class _Traits,
class _Allocator>
2605 struct formatter<basic_string<char, _Traits, _Allocator>, wchar_t>
2606 :
private formatter<__format::__disabled, wchar_t> { };
2607 template<
class _Traits>
2608 struct formatter<basic_string_view<char, _Traits>, wchar_t>
2609 :
private formatter<__format::__disabled, wchar_t> { };
2615 template<
typename _Tp,
typename _Context,
2617 =
typename _Context::template formatter_type<remove_const_t<_Tp>>,
2618 typename _ParseContext
2619 = basic_format_parse_context<typename _Context::char_type>>
2620 concept __parsable_with
2621 = semiregular<_Formatter>
2622 &&
requires (_Formatter __f, _ParseContext __pc)
2624 { __f.parse(__pc) } -> same_as<typename _ParseContext::iterator>;
2627 template<
typename _Tp,
typename _Context,
2629 =
typename _Context::template formatter_type<remove_const_t<_Tp>>,
2630 typename _ParseContext
2631 = basic_format_parse_context<typename _Context::char_type>>
2632 concept __formattable_with
2633 = semiregular<_Formatter>
2634 &&
requires (
const _Formatter __cf, _Tp&& __t, _Context __fc)
2636 { __cf.format(__t, __fc) } -> same_as<typename _Context::iterator>;
2640 template<
typename _CharT>
2641 using _Iter_for = back_insert_iterator<basic_string<_CharT>>;
2643 template<
typename _Tp,
typename _CharT,
2644 typename _Context = basic_format_context<_Iter_for<_CharT>, _CharT>>
2645 concept __formattable_impl
2646 = __parsable_with<_Tp, _Context> && __formattable_with<_Tp, _Context>;
2653#if __cplusplus > 202002L
2655 template<
typename _Tp,
typename _CharT>
2657 = __format::__formattable_impl<remove_reference_t<_Tp>, _CharT>;
2660#if __cpp_lib_format_ranges
2664 template<
typename _Rg,
typename _CharT>
2665 concept __const_formattable_range
2666 = ranges::input_range<const _Rg>
2667 && formattable<ranges::range_reference_t<const _Rg>, _CharT>;
2669 template<
typename _Rg,
typename _CharT>
2670 using __maybe_const_range
2671 = conditional_t<__const_formattable_range<_Rg, _CharT>,
const _Rg, _Rg>;
2678 template<
typename _Out>
2679 struct format_to_n_result
2682 iter_difference_t<_Out>
size;
2685_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
2686template<
typename,
typename>
class vector;
2687_GLIBCXX_END_NAMESPACE_CONTAINER
2692 template<
typename _CharT>
2695 _Sink<_CharT>* _M_sink =
nullptr;
2698 using iterator_category = output_iterator_tag;
2699 using value_type = void;
2700 using difference_type = ptrdiff_t;
2701 using pointer = void;
2702 using reference = void;
2704 _Sink_iter() =
default;
2705 _Sink_iter(
const _Sink_iter&) =
default;
2706 _Sink_iter& operator=(
const _Sink_iter&) =
default;
2708 [[__gnu__::__always_inline__]]
2710 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(
std::
addressof(__sink)) { }
2712 [[__gnu__::__always_inline__]]
2713 constexpr _Sink_iter&
2714 operator=(_CharT __c)
2716 _M_sink->_M_write(__c);
2720 [[__gnu__::__always_inline__]]
2721 constexpr _Sink_iter&
2722 operator=(basic_string_view<_CharT> __s)
2724 _M_sink->_M_write(__s);
2728 [[__gnu__::__always_inline__]]
2729 constexpr _Sink_iter&
2732 [[__gnu__::__always_inline__]]
2733 constexpr _Sink_iter&
2734 operator++() {
return *
this; }
2736 [[__gnu__::__always_inline__]]
2737 constexpr _Sink_iter
2738 operator++(
int) {
return *
this; }
2741 _M_reserve(
size_t __n)
const
2742 {
return _M_sink->_M_reserve(__n); }
2748 template<
typename _CharT>
2751 friend class _Sink_iter<_CharT>;
2753 span<_CharT> _M_span;
2754 typename span<_CharT>::iterator _M_next;
2760 virtual void _M_overflow() = 0;
2764 [[__gnu__::__always_inline__]]
2766 _Sink(span<_CharT> __span) noexcept
2767 : _M_span(__span), _M_next(__span.begin())
2771 [[__gnu__::__always_inline__]]
2773 _M_used() const noexcept
2774 {
return _M_span.first(_M_next - _M_span.begin()); }
2777 [[__gnu__::__always_inline__]]
2778 constexpr span<_CharT>
2779 _M_unused() const noexcept
2780 {
return _M_span.subspan(_M_next - _M_span.begin()); }
2783 [[__gnu__::__always_inline__]]
2785 _M_rewind() noexcept
2786 { _M_next = _M_span.begin(); }
2790 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
2793 _M_next = __s.begin() + __pos;
2798 _M_write(_CharT __c)
2801 if (_M_next - _M_span.begin() == std::ssize(_M_span)) [[unlikely]]
2806 _M_write(basic_string_view<_CharT> __s)
2808 span __to = _M_unused();
2809 while (__to.size() <= __s.size())
2811 __s.copy(__to.data(), __to.size());
2812 _M_next += __to.size();
2813 __s.remove_prefix(__to.size());
2819 __s.copy(__to.data(), __s.size());
2820 _M_next += __s.size();
2829 explicit operator bool() const noexcept {
return _M_sink; }
2831 _CharT* get() const noexcept {
return _M_sink->_M_next.operator->(); }
2833 void _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
2841 virtual _Reservation
2842 _M_reserve(
size_t __n)
2844 if (__n <= _M_unused().
size())
2847 if (__n <= _M_span.size())
2850 if (__n <= _M_unused().
size())
2863 _Sink(
const _Sink&) =
delete;
2864 _Sink& operator=(
const _Sink&) =
delete;
2866 [[__gnu__::__always_inline__]]
2867 constexpr _Sink_iter<_CharT>
2869 {
return _Sink_iter<_CharT>(*
this); }
2873 template<
typename _CharT>
2874 class _Buf_sink :
public _Sink<_CharT>
2877 _CharT _M_buf[32 *
sizeof(
void*) /
sizeof(_CharT)];
2879 [[__gnu__::__always_inline__]]
2881 _Buf_sink() noexcept
2882 : _Sink<_CharT>(_M_buf)
2886 using _GLIBCXX_STD_C::vector;
2890 template<
typename _Seq>
2891 class _Seq_sink final :
public _Buf_sink<typename _Seq::value_type>
2893 using _CharT =
typename _Seq::value_type;
2899 _M_overflow()
override
2901 auto __s = this->_M_used();
2902 if (__s.empty()) [[unlikely]]
2906 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
2908 if constexpr (__is_specialization_of<_Seq, basic_string>)
2909 _M_seq.append(__s.data(), __s.size());
2911 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
2917 typename _Sink<_CharT>::_Reservation
2918 _M_reserve(
size_t __n)
override
2927 if constexpr (__is_specialization_of<_Seq, basic_string>
2928 || __is_specialization_of<_Seq, vector>)
2931 if (this->_M_used().size()) [[unlikely]]
2932 _Seq_sink::_M_overflow();
2935 const auto __sz = _M_seq.size();
2936 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
2937 _M_seq.__resize_and_overwrite(__sz + __n,
2938 [](
auto,
auto __n2) {
2942 _M_seq.resize(__sz + __n);
2946 this->_M_reset(_M_seq, __sz);
2950 return _Sink<_CharT>::_M_reserve(__n);
2954 _M_bump(
size_t __n)
override
2956 if constexpr (__is_specialization_of<_Seq, basic_string>
2957 || __is_specialization_of<_Seq, vector>)
2959 auto __s = this->_M_used();
2960 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
2962 _M_seq.resize(__s.size() + __n);
2964 this->_M_reset(this->_M_buf);
2972 [[__gnu__::__always_inline__]]
2973 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
2976 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
2980 using _Sink<_CharT>::out;
2985 if (this->_M_used().
size() != 0)
2986 _Seq_sink::_M_overflow();
2995 auto __s = this->_M_used();
2998 if (__s.size() != 0)
2999 _Seq_sink::_M_overflow();
3006 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
3008 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3016 template<
typename _CharT,
typename _OutIter>
3017 class _Iter_sink :
public _Buf_sink<_CharT>
3020 iter_difference_t<_OutIter> _M_max;
3023 size_t _M_count = 0;
3026 _M_overflow()
override
3028 auto __s = this->_M_used();
3030 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3031 else if (_M_count <
static_cast<size_t>(_M_max))
3033 auto __max = _M_max - _M_count;
3034 span<_CharT> __first;
3035 if (__max < __s.size())
3036 __first = __s.first(
static_cast<size_t>(__max));
3039 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3042 _M_count += __s.size();
3046 [[__gnu__::__always_inline__]]
3048 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3049 : _M_out(
std::
move(__out)), _M_max(__max)
3052 using _Sink<_CharT>::out;
3054 format_to_n_result<_OutIter>
3057 if (this->_M_used().
size() != 0)
3058 _Iter_sink::_M_overflow();
3059 iter_difference_t<_OutIter> __count(_M_count);
3071 template<
typename _CharT, contiguous_iterator _OutIter>
3072 requires same_as<iter_value_t<_OutIter>, _CharT>
3073 class _Iter_sink<_CharT, _OutIter> :
public _Sink<_CharT>
3076 iter_difference_t<_OutIter> _M_max = -1;
3078 size_t _M_count = 0;
3084 _M_overflow()
override
3086 if (this->_M_unused().
size() != 0)
3089 auto __s = this->_M_used();
3093 _M_count += __s.size();
3097 this->_M_reset(this->_M_buf);
3103 this->_M_reset({__s.data(), __s.size() + 1024}, __s.size());
3107 typename _Sink<_CharT>::_Reservation
3108 _M_reserve(
size_t __n)
final
3110 auto __avail = this->_M_unused();
3111 if (__n > __avail.size())
3116 auto __s = this->_M_used();
3117 this->_M_reset({__s.data(), __s.size() + __n}, __s.size());
3124 _S_make_span(_CharT* __ptr, iter_difference_t<_OutIter> __n,
3125 span<_CharT> __buf)
noexcept
3132 if constexpr (!is_integral_v<iter_difference_t<_OutIter>>
3133 ||
sizeof(__n) >
sizeof(
size_t))
3136 auto __m = iter_difference_t<_OutIter>((
size_t)-1);
3140 return {__ptr, (size_t)__n};
3143#if __has_builtin(__builtin_dynamic_object_size)
3144 if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3145 return {__ptr, __bytes /
sizeof(_CharT)};
3148 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3149 __n = (1024 - __off) /
sizeof(_CharT);
3150 if (__n > 0) [[likely]]
3151 return {__ptr,
static_cast<size_t>(__n)};
3158 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1) noexcept
3159 : _Sink<_CharT>(_S_make_span(
std::to_address(__out), __n, _M_buf)),
3160 _M_first(__out), _M_max(__n)
3163 format_to_n_result<_OutIter>
3166 auto __s = this->_M_used();
3167 if (__s.data() == _M_buf)
3170 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3171 return { _M_first + _M_max, __count };
3175 iter_difference_t<_OutIter> __count(__s.size());
3176 return { _M_first + __count, __count };
3181 enum _Arg_t :
unsigned char {
3182 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
3183 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
3184 _Arg_i128, _Arg_u128,
3185 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
3186#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3188 _Arg_f128 = _Arg_ldbl,
3189 _Arg_ibm128 = _Arg_next_value_,
3196 template<
typename _Context>
3199 using _CharT =
typename _Context::char_type;
3215 unsigned long long _M_ull;
3218#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3219 long double _M_ldbl;
3221 const _CharT* _M_str;
3222 basic_string_view<_CharT> _M_sv;
3224 _HandleBase _M_handle;
3225#ifdef __SIZEOF_INT128__
3227 unsigned __int128 _M_u128;
3229#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3232#elif _GLIBCXX_FORMAT_F128 == 2
3233 __float128_t _M_f128;
3237 [[__gnu__::__always_inline__]]
3238 _Arg_value() : _M_none() { }
3241 template<
typename _Tp>
3242 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
3243 { _S_get<_Tp>() = __val; }
3246 template<
typename _Tp,
typename _Self>
3247 [[__gnu__::__always_inline__]]
3249 _S_get(_Self& __u)
noexcept
3251 if constexpr (is_same_v<_Tp, bool>)
3253 else if constexpr (is_same_v<_Tp, _CharT>)
3255 else if constexpr (is_same_v<_Tp, int>)
3257 else if constexpr (is_same_v<_Tp, unsigned>)
3259 else if constexpr (is_same_v<_Tp, long long>)
3261 else if constexpr (is_same_v<_Tp, unsigned long long>)
3263 else if constexpr (is_same_v<_Tp, float>)
3265 else if constexpr (is_same_v<_Tp, double>)
3267#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3268 else if constexpr (is_same_v<_Tp, long double>)
3271 else if constexpr (is_same_v<_Tp, __ieee128>)
3273 else if constexpr (is_same_v<_Tp, __ibm128>)
3274 return __u._M_ibm128;
3276 else if constexpr (is_same_v<_Tp, const _CharT*>)
3278 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
3280 else if constexpr (is_same_v<_Tp, const void*>)
3282#ifdef __SIZEOF_INT128__
3283 else if constexpr (is_same_v<_Tp, __int128>)
3285 else if constexpr (is_same_v<_Tp, unsigned __int128>)
3288#if _GLIBCXX_FORMAT_F128 == 2
3289 else if constexpr (is_same_v<_Tp, __float128_t>)
3292 else if constexpr (derived_from<_Tp, _HandleBase>)
3293 return static_cast<_Tp&
>(__u._M_handle);
3297 template<
typename _Tp>
3298 [[__gnu__::__always_inline__]]
3301 {
return _S_get<_Tp>(*
this); }
3303 template<
typename _Tp>
3304 [[__gnu__::__always_inline__]]
3306 _M_get() const noexcept
3307 {
return _S_get<_Tp>(*
this); }
3309 template<
typename _Tp>
3310 [[__gnu__::__always_inline__]]
3312 _M_set(_Tp __v)
noexcept
3314 if constexpr (derived_from<_Tp, _HandleBase>)
3315 std::construct_at(&_M_handle, __v);
3317 _S_get<_Tp>(*
this) = __v;
3322 template<
typename _Context,
typename... _Args>
3325 template<
typename _Visitor,
typename _Ctx>
3326 decltype(
auto) __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
3328 template<
typename _Ch,
typename _Tp>
3330 __to_arg_t_enum() noexcept;
3334 template<typename _Context>
3335 class basic_format_arg
3337 using _CharT =
typename _Context::char_type;
3339 template<
typename _Tp>
3340 static constexpr bool __formattable
3341 = __format::__formattable_with<_Tp, _Context>;
3344 class handle :
public __format::_Arg_value<_Context>::_HandleBase
3346 using _Base =
typename __format::_Arg_value<_Context>::_HandleBase;
3349 template<
typename _Tp>
3350 using __maybe_const_t
3351 = __conditional_t<__formattable<const _Tp>,
const _Tp, _Tp>;
3353 template<
typename _Tq>
3355 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
3356 _Context& __format_ctx,
const void* __ptr)
3358 using _Td = remove_const_t<_Tq>;
3359 typename _Context::template formatter_type<_Td> __f;
3360 __parse_ctx.advance_to(__f.parse(__parse_ctx));
3361 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
3362 __format_ctx.advance_to(__f.format(__val, __format_ctx));
3365 template<
typename _Tp>
3367 handle(_Tp& __val)
noexcept
3369 this->_M_ptr = __builtin_addressof(__val);
3370 auto __func = _S_format<__maybe_const_t<_Tp>>;
3371 this->_M_func =
reinterpret_cast<void(*)()
>(__func);
3374 friend class basic_format_arg<_Context>;
3377 handle(
const handle&) =
default;
3378 handle& operator=(
const handle&) =
default;
3380 [[__gnu__::__always_inline__]]
3382 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
3384 using _Func = void(*)(basic_format_parse_context<_CharT>&,
3385 _Context&,
const void*);
3386 auto __f =
reinterpret_cast<_Func
>(this->_M_func);
3387 __f(__pc, __fc, this->_M_ptr);
3391 [[__gnu__::__always_inline__]]
3392 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
3394 [[nodiscard,__gnu__::__always_inline__]]
3395 explicit operator bool() const noexcept
3396 {
return _M_type != __format::_Arg_none; }
3398#if __cpp_lib_format >= 202306L
3399 template<
typename _Visitor>
3401 visit(
this basic_format_arg __arg, _Visitor&& __vis)
3402 {
return __arg._M_visit_user(std::forward<_Visitor>(__vis), __arg._M_type); }
3404 template<
typename _Res,
typename _Visitor>
3406 visit(
this basic_format_arg __arg, _Visitor&& __vis)
3407 {
return __arg._M_visit_user(std::forward<_Visitor>(__vis), __arg._M_type); }
3411 template<
typename _Ctx>
3412 friend class basic_format_args;
3414 template<
typename _Ctx,
typename... _Args>
3415 friend class __format::_Arg_store;
3417 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
3419 __format::_Arg_value<_Context> _M_val;
3420 __format::_Arg_t _M_type;
3425 template<
typename _Tp>
3426 static consteval auto
3429 using _Td = remove_const_t<_Tp>;
3430 if constexpr (is_same_v<_Td, bool>)
3431 return type_identity<bool>();
3432 else if constexpr (is_same_v<_Td, _CharT>)
3433 return type_identity<_CharT>();
3434 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
3435 return type_identity<_CharT>();
3436#ifdef __SIZEOF_INT128__
3437 else if constexpr (is_same_v<_Td, __int128>)
3438 return type_identity<__int128>();
3439 else if constexpr (is_same_v<_Td, unsigned __int128>)
3440 return type_identity<unsigned __int128>();
3442 else if constexpr (__is_signed_integer<_Td>::value)
3444 if constexpr (
sizeof(_Td) <=
sizeof(
int))
3445 return type_identity<int>();
3446 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
3447 return type_identity<long long>();
3449 else if constexpr (__is_unsigned_integer<_Td>::value)
3451 if constexpr (
sizeof(_Td) <=
sizeof(
unsigned))
3452 return type_identity<unsigned>();
3453 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
3454 return type_identity<unsigned long long>();
3456 else if constexpr (is_same_v<_Td, float>)
3457 return type_identity<float>();
3458 else if constexpr (is_same_v<_Td, double>)
3459 return type_identity<double>();
3460#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3461 else if constexpr (is_same_v<_Td, long double>)
3462 return type_identity<long double>();
3464 else if constexpr (is_same_v<_Td, __ibm128>)
3465 return type_identity<__ibm128>();
3466 else if constexpr (is_same_v<_Td, __ieee128>)
3467 return type_identity<__ieee128>();
3470#if defined(__FLT16_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3471 else if constexpr (is_same_v<_Td, _Float16>)
3472 return type_identity<float>();
3475#if defined(__BFLT16_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3476 else if constexpr (is_same_v<_Td,
decltype(0.0bf16)>)
3477 return type_identity<float>();
3480#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3481 else if constexpr (is_same_v<_Td, _Float32>)
3482 return type_identity<float>();
3485#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3486 else if constexpr (is_same_v<_Td, _Float64>)
3487 return type_identity<double>();
3490#if _GLIBCXX_FORMAT_F128
3492 else if constexpr (is_same_v<_Td, _Float128>)
3493 return type_identity<__format::__float128_t>();
3495# if __SIZEOF_FLOAT128__
3496 else if constexpr (is_same_v<_Td, __float128>)
3497 return type_identity<__format::__float128_t>();
3500 else if constexpr (__is_specialization_of<_Td, basic_string_view>
3501 || __is_specialization_of<_Td, basic_string>)
3503 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
3504 return type_identity<basic_string_view<_CharT>>();
3506 return type_identity<handle>();
3508 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
3509 return type_identity<const _CharT*>();
3510 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
3511 return type_identity<const _CharT*>();
3512 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
3513 return type_identity<const void*>();
3514 else if constexpr (is_same_v<_Td, nullptr_t>)
3515 return type_identity<const void*>();
3517 return type_identity<handle>();
3521 template<
typename _Tp>
3522 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
3525 template<
typename _Tp>
3526 static consteval __format::_Arg_t
3529 using namespace __format;
3530 if constexpr (is_same_v<_Tp, bool>)
3532 else if constexpr (is_same_v<_Tp, _CharT>)
3534 else if constexpr (is_same_v<_Tp, int>)
3536 else if constexpr (is_same_v<_Tp, unsigned>)
3538 else if constexpr (is_same_v<_Tp, long long>)
3540 else if constexpr (is_same_v<_Tp, unsigned long long>)
3542 else if constexpr (is_same_v<_Tp, float>)
3544 else if constexpr (is_same_v<_Tp, double>)
3546#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3547 else if constexpr (is_same_v<_Tp, long double>)
3551 else if constexpr (is_same_v<_Tp, __ibm128>)
3553 else if constexpr (is_same_v<_Tp, __ieee128>)
3556 else if constexpr (is_same_v<_Tp, const _CharT*>)
3558 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
3560 else if constexpr (is_same_v<_Tp, const void*>)
3562#ifdef __SIZEOF_INT128__
3563 else if constexpr (is_same_v<_Tp, __int128>)
3565 else if constexpr (is_same_v<_Tp, unsigned __int128>)
3569#if _GLIBCXX_FORMAT_F128 == 2
3570 else if constexpr (is_same_v<_Tp, __format::__float128_t>)
3573 else if constexpr (is_same_v<_Tp, handle>)
3577 template<
typename _Tp>
3579 _M_set(_Tp __v)
noexcept
3581 _M_type = _S_to_enum<_Tp>();
3585 template<
typename _Tp>
3586 requires __format::__formattable_with<_Tp, _Context>
3588 basic_format_arg(_Tp& __v)
noexcept
3590 using _Td = _Normalize<_Tp>;
3591 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
3592 _M_set(_Td{__v.data(), __v.size()});
3593 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
3594 && is_same_v<_CharT, wchar_t>)
3595 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
3597 _M_set(
static_cast<_Td
>(__v));
3600 template<
typename _Ctx,
typename... _Argz>
3602 make_format_args(_Argz&...)
noexcept;
3604 template<
typename _Visitor,
typename _Ctx>
3605 friend decltype(
auto)
3606 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
3608 template<
typename _Visitor,
typename _Ctx>
3609 friend decltype(
auto)
3610 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
3612 template<
typename _Ch,
typename _Tp>
3613 friend consteval __format::_Arg_t
3614 __format::__to_arg_t_enum() noexcept;
3616 template<typename _Visitor>
3618 _M_visit(_Visitor&& __vis, __format::_Arg_t __type)
3620 using namespace __format;
3624 return std::forward<_Visitor>(__vis)(_M_val._M_none);
3626 return std::forward<_Visitor>(__vis)(_M_val._M_bool);
3628 return std::forward<_Visitor>(__vis)(_M_val._M_c);
3630 return std::forward<_Visitor>(__vis)(_M_val._M_i);
3632 return std::forward<_Visitor>(__vis)(_M_val._M_u);
3634 return std::forward<_Visitor>(__vis)(_M_val._M_ll);
3636 return std::forward<_Visitor>(__vis)(_M_val._M_ull);
3637#if __glibcxx_to_chars
3639 return std::forward<_Visitor>(__vis)(_M_val._M_flt);
3641 return std::forward<_Visitor>(__vis)(_M_val._M_dbl);
3642#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3644 return std::forward<_Visitor>(__vis)(_M_val._M_ldbl);
3647 return std::forward<_Visitor>(__vis)(_M_val._M_f128);
3649 return std::forward<_Visitor>(__vis)(_M_val._M_ibm128);
3653 return std::forward<_Visitor>(__vis)(_M_val._M_str);
3655 return std::forward<_Visitor>(__vis)(_M_val._M_sv);
3657 return std::forward<_Visitor>(__vis)(_M_val._M_ptr);
3660 auto& __h =
static_cast<handle&
>(_M_val._M_handle);
3661 return std::forward<_Visitor>(__vis)(__h);
3663#ifdef __SIZEOF_INT128__
3665 return std::forward<_Visitor>(__vis)(_M_val._M_i128);
3667 return std::forward<_Visitor>(__vis)(_M_val._M_u128);
3670#if _GLIBCXX_FORMAT_F128 == 2
3672 return std::forward<_Visitor>(__vis)(_M_val._M_f128);
3677 __builtin_unreachable();
3681 template<
typename _Visitor>
3683 _M_visit_user(_Visitor&& __vis, __format::_Arg_t __type)
3685 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
3687 constexpr bool __user_facing = __is_one_of<_Tp,
3688 monostate, bool, _CharT,
3689 int,
unsigned int,
long long int,
unsigned long long int,
3690 float, double,
long double,
3691 const _CharT*, basic_string_view<_CharT>,
3692 const void*, handle>::value;
3693 if constexpr (__user_facing)
3694 return std::forward<_Visitor>(__vis)(__val);
3698 return std::forward<_Visitor>(__vis)(__h);
3704 template<
typename _Visitor,
typename _Context>
3705 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
3706 inline decltype(auto)
3707 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
3709 return __arg._M_visit_user(std::forward<_Visitor>(__vis), __arg._M_type);
3715 template<
typename _Visitor,
typename _Ctx>
3716 inline decltype(
auto)
3717 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
3719 return __arg._M_visit(std::forward<_Visitor>(__vis), __arg._M_type);
3722 struct _WidthPrecVisitor
3724 template<
typename _Tp>
3726 operator()(_Tp& __arg)
const
3728 if constexpr (is_same_v<_Tp, monostate>)
3729 __format::__invalid_arg_id_in_format_string();
3733 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
3737 if constexpr (__is_unsigned_integer<_Tp>::value)
3739 else if constexpr (__is_signed_integer<_Tp>::value)
3743 __throw_format_error(
"format error: argument used for width or "
3744 "precision must be a non-negative integer");
3748#pragma GCC diagnostic push
3749#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
3750 template<
typename _Context>
3752 __int_from_arg(
const basic_format_arg<_Context>& __arg)
3753 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
3756 template<
int _Bits,
size_t _Nm>
3758 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
3760 __UINT64_TYPE__ __packed_types = 0;
3761 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
3762 __packed_types = (__packed_types << _Bits) | *__i;
3763 return __packed_types;
3768 template<
typename _Context>
3769 class basic_format_args
3771 static constexpr int _S_packed_type_bits = 5;
3772 static constexpr int _S_packed_type_mask = 0b11111;
3773 static constexpr int _S_max_packed_args = 12;
3775 static_assert( __format::_Arg_max_ <= (1 << _S_packed_type_bits) );
3777 template<
typename... _Args>
3778 using _Store = __format::_Arg_store<_Context, _Args...>;
3780 template<
typename _Ctx,
typename... _Args>
3781 friend class __format::_Arg_store;
3783 using uint64_t = __UINT64_TYPE__;
3784 using _Format_arg = basic_format_arg<_Context>;
3785 using _Format_arg_val = __format::_Arg_value<_Context>;
3791 uint64_t _M_packed_size : 4;
3792 uint64_t _M_unpacked_size : 60;
3795 const _Format_arg_val* _M_values;
3796 const _Format_arg* _M_args;
3800 _M_size() const noexcept
3801 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
3803 typename __format::_Arg_t
3804 _M_type(
size_t __i)
const noexcept
3806 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
3807 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
3810 template<
typename _Ctx,
typename... _Args>
3812 make_format_args(_Args&...)
noexcept;
3815 template<
typename... _Args>
3816 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
3818 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
3821 template<
typename... _Args>
3822 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
3824 [[nodiscard,__gnu__::__always_inline__]]
3825 basic_format_arg<_Context>
3826 get(
size_t __i)
const noexcept
3828 basic_format_arg<_Context> __arg;
3829 if (__i < _M_packed_size)
3831 __arg._M_type = _M_type(__i);
3832 __arg._M_val = _M_values[__i];
3834 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
3835 __arg = _M_args[__i];
3842 template<
typename _Context,
typename... _Args>
3843 basic_format_args(__format::_Arg_store<_Context, _Args...>)
3844 -> basic_format_args<_Context>;
3846 template<
typename _Context,
typename... _Args>
3848 make_format_args(_Args&... __fmt_args)
noexcept;
3851 template<
typename _Context,
typename... _Args>
3852 class __format::_Arg_store
3854 friend std::basic_format_args<_Context>;
3856 template<
typename _Ctx,
typename... _Argz>
3858#if _GLIBCXX_INLINE_VERSION
3861 make_format_args(_Argz&...)
noexcept;
3865 static constexpr bool _S_values_only
3866 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
3869 = __conditional_t<_S_values_only,
3870 __format::_Arg_value<_Context>,
3871 basic_format_arg<_Context>>;
3873 _Element_t _M_args[
sizeof...(_Args)];
3875 template<
typename _Tp>
3877 _S_make_elt(_Tp& __v)
3879 using _Tq = remove_const_t<_Tp>;
3880 using _CharT =
typename _Context::char_type;
3881 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
3882 "std::formatter must be specialized for the type "
3883 "of each format arg");
3884 using __format::__formattable_with;
3885 if constexpr (is_const_v<_Tp>)
3886 if constexpr (!__formattable_with<_Tp, _Context>)
3887 if constexpr (__formattable_with<_Tq, _Context>)
3888 static_assert(__formattable_with<_Tp, _Context>,
3889 "format arg must be non-const because its "
3890 "std::formatter specialization has a "
3891 "non-const reference parameter");
3892 basic_format_arg<_Context> __arg(__v);
3893 if constexpr (_S_values_only)
3894 return __arg._M_val;
3899 template<
typename... _Tp>
3900 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
3901 [[__gnu__::__always_inline__]]
3902 _Arg_store(_Tp&... __a) noexcept
3903 : _M_args{_S_make_elt(__a)...}
3907 template<
typename _Context>
3908 class __format::_Arg_store<_Context>
3911 template<
typename _Context>
3912 template<
typename... _Args>
3914 basic_format_args<_Context>::
3915 basic_format_args(
const _Store<_Args...>& __store)
noexcept
3917 if constexpr (
sizeof...(_Args) == 0)
3920 _M_unpacked_size = 0;
3923 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
3926 _M_packed_size =
sizeof...(_Args);
3929 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
3931 _M_values = __store._M_args;
3938 _M_unpacked_size =
sizeof...(_Args);
3940 _M_args = __store._M_args;
3945 template<
typename _Context = format_context,
typename... _Args>
3946 [[nodiscard,__gnu__::__always_inline__]]
3948 make_format_args(_Args&... __fmt_args)
noexcept
3950 using _Fmt_arg = basic_format_arg<_Context>;
3951 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
3952 _Normalize<_Args>...>;
3953 return _Store(__fmt_args...);
3956#ifdef _GLIBCXX_USE_WCHAR_T
3958 template<
typename... _Args>
3959 [[nodiscard,__gnu__::__always_inline__]]
3961 make_wformat_args(_Args&... __args)
noexcept
3962 {
return std::make_format_args<wformat_context>(__args...); }
3968 template<
typename _Out,
typename _CharT,
typename _Context>
3970 __do_vformat_to(_Out, basic_string_view<_CharT>,
3971 const basic_format_args<_Context>&,
3972 const locale* =
nullptr);
3974 template<
typename _CharT>
struct __formatter_chrono;
3992 template<
typename _Out,
typename _CharT>
3993 class basic_format_context
3995 static_assert( output_iterator<_Out, const _CharT&> );
3997 basic_format_args<basic_format_context> _M_args;
3999 __format::_Optional_locale _M_loc;
4001 basic_format_context(basic_format_args<basic_format_context> __args,
4003 : _M_args(__args), _M_out(
std::
move(__out))
4006 basic_format_context(basic_format_args<basic_format_context> __args,
4008 : _M_args(__args), _M_out(
std::
move(__out)), _M_loc(__loc)
4014 basic_format_context(
const basic_format_context&) =
delete;
4015 basic_format_context& operator=(
const basic_format_context&) =
delete;
4017 template<
typename _Out2,
typename _CharT2,
typename _Context2>
4019 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
4020 const basic_format_args<_Context2>&,
4023 friend __format::__formatter_chrono<_CharT>;
4026 ~basic_format_context() =
default;
4028 using iterator = _Out;
4029 using char_type = _CharT;
4030 template<
typename _Tp>
4031 using formatter_type = formatter<_Tp, _CharT>;
4034 basic_format_arg<basic_format_context>
4035 arg(
size_t __id)
const noexcept
4036 {
return _M_args.get(__id); }
4042 iterator out() {
return std::move(_M_out); }
4044 void advance_to(iterator __it) { _M_out =
std::move(__it); }
4056 template<
typename _CharT>
4059 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
4061 struct _Parse_context : basic_format_parse_context<_CharT>
4063 using basic_format_parse_context<_CharT>::basic_format_parse_context;
4064 const _Arg_t* _M_types =
nullptr;
4068 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
4069 : _M_pc(__str, __nargs)
4072 constexpr iterator
begin() const noexcept {
return _M_pc.begin(); }
4073 constexpr iterator
end() const noexcept {
return _M_pc.end(); }
4078 basic_string_view<_CharT> __fmt = _M_fmt_str();
4080 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
4082 _M_pc.advance_to(
begin() + 1);
4083 _M_format_arg(_M_pc.next_arg_id());
4087 size_t __lbr = __fmt.find(
'{');
4088 size_t __rbr = __fmt.find(
'}');
4090 while (__fmt.size())
4092 auto __cmp = __lbr <=> __rbr;
4096 _M_pc.advance_to(
end());
4101 if (__lbr + 1 == __fmt.size()
4102 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
4103 __format::__unmatched_left_brace_in_format_string();
4104 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
4105 iterator __last =
begin() + __lbr + int(__is_escape);
4106 _M_on_chars(__last);
4107 _M_pc.advance_to(__last + 1);
4108 __fmt = _M_fmt_str();
4111 if (__rbr != __fmt.npos)
4113 __lbr = __fmt.find(
'{');
4117 _M_on_replacement_field();
4118 __fmt = _M_fmt_str();
4119 __lbr = __fmt.find(
'{');
4120 __rbr = __fmt.find(
'}');
4125 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
4126 __format::__unmatched_right_brace_in_format_string();
4127 iterator __last =
begin() + __rbr;
4128 _M_on_chars(__last);
4129 _M_pc.advance_to(__last + 1);
4130 __fmt = _M_fmt_str();
4131 if (__lbr != __fmt.npos)
4133 __rbr = __fmt.find(
'}');
4138 constexpr basic_string_view<_CharT>
4139 _M_fmt_str() const noexcept
4142 constexpr virtual void _M_on_chars(iterator) { }
4144 constexpr void _M_on_replacement_field()
4146 auto __next =
begin();
4150 __id = _M_pc.next_arg_id();
4151 else if (*__next ==
':')
4153 __id = _M_pc.next_arg_id();
4154 _M_pc.advance_to(++__next);
4158 auto [__i, __ptr] = __format::__parse_arg_id(
begin(),
end());
4159 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
4160 __format::__invalid_arg_id_in_format_string();
4161 _M_pc.check_arg_id(__id = __i);
4164 _M_pc.advance_to(++__ptr);
4167 _M_pc.advance_to(__ptr);
4169 _M_format_arg(__id);
4171 __format::__unmatched_left_brace_in_format_string();
4172 _M_pc.advance_to(
begin() + 1);
4175 constexpr virtual void _M_format_arg(
size_t __id) = 0;
4179 template<
typename _Out,
typename _CharT>
4180 class _Formatting_scanner :
public _Scanner<_CharT>
4183 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
4184 basic_string_view<_CharT> __str)
4185 : _Scanner<_CharT>(__str), _M_fc(__fc)
4189 basic_format_context<_Out, _CharT>& _M_fc;
4191 using iterator =
typename _Scanner<_CharT>::iterator;
4194 _M_on_chars(iterator __last)
override
4196 basic_string_view<_CharT> __str(this->
begin(), __last);
4197 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
4201 _M_format_arg(
size_t __id)
override
4203 using _Context = basic_format_context<_Out, _CharT>;
4204 using handle =
typename basic_format_arg<_Context>::handle;
4206 __format::__visit_format_arg([
this](
auto& __arg) {
4208 using _Formatter =
typename _Context::template formatter_type<_Type>;
4209 if constexpr (is_same_v<_Type, monostate>)
4210 __format::__invalid_arg_id_in_format_string();
4211 else if constexpr (is_same_v<_Type, handle>)
4212 __arg.format(this->_M_pc, this->_M_fc);
4213 else if constexpr (is_default_constructible_v<_Formatter>)
4216 this->_M_pc.advance_to(__f.parse(this->_M_pc));
4217 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
4220 static_assert(__format::__formattable_with<_Type, _Context>);
4221 }, _M_fc.arg(__id));
4225 template<
typename _CharT,
typename _Tp>
4227 __to_arg_t_enum() noexcept
4229 using _Context = __format::__format_context<_CharT>;
4230 using _Fmt_arg = basic_format_arg<_Context>;
4231 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
4232 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
4236 template<
typename _CharT,
typename... _Args>
4237 class _Checking_scanner :
public _Scanner<_CharT>
4240 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
4241 "std::formatter must be specialized for each type being formatted");
4245 _Checking_scanner(basic_string_view<_CharT> __str)
4246 : _Scanner<_CharT>(__str, sizeof...(_Args))
4248#if __cpp_lib_format >= 202305L
4249 this->_M_pc._M_types = _M_types.data();
4255 _M_format_arg(
size_t __id)
override
4257 if constexpr (
sizeof...(_Args) != 0)
4259 if (__id <
sizeof...(_Args))
4261 _M_parse_format_spec<_Args...>(__id);
4265 __builtin_unreachable();
4268 template<
typename _Tp,
typename... _OtherArgs>
4270 _M_parse_format_spec(
size_t __id)
4274 formatter<_Tp, _CharT> __f;
4275 this->_M_pc.advance_to(__f.parse(this->_M_pc));
4277 else if constexpr (
sizeof...(_OtherArgs) != 0)
4278 _M_parse_format_spec<_OtherArgs...>(__id - 1);
4280 __builtin_unreachable();
4283#if __cpp_lib_format >= 202305L
4284 array<_Arg_t,
sizeof...(_Args)>
4285 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
4289 template<
typename _Out,
typename _CharT,
typename _Context>
4291 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
4292 const basic_format_args<_Context>& __args,
4293 const locale* __loc)
4295 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
4296 _Sink_iter<_CharT> __sink_out;
4298 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4301 __sink_out = __sink.out();
4303 if constexpr (is_same_v<_CharT, char>)
4305 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
4307 bool __done =
false;
4308 __format::__visit_format_arg([&](
auto& __arg) {
4309 using _Tp = remove_cvref_t<
decltype(__arg)>;
4310 if constexpr (is_same_v<_Tp, bool>)
4312 size_t __len = 4 + !__arg;
4313 const char* __chars[] = {
"false",
"true" };
4314 if (
auto __res = __sink_out._M_reserve(__len))
4316 __builtin_memcpy(__res.get(), __chars[__arg], __len);
4317 __res._M_bump(__len);
4321 else if constexpr (is_same_v<_Tp, char>)
4323 if (
auto __res = __sink_out._M_reserve(1))
4325 *__res.get() = __arg;
4330 else if constexpr (is_integral_v<_Tp>)
4332 make_unsigned_t<_Tp> __uval;
4333 const bool __neg = __arg < 0;
4335 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
4338 const auto __n = __detail::__to_chars_len(__uval);
4339 if (
auto __res = __sink_out._M_reserve(__n + __neg))
4341 auto __ptr = __res.get();
4343 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
4345 __res._M_bump(__n + __neg);
4349 else if constexpr (is_convertible_v<_Tp, string_view>)
4351 string_view __sv = __arg;
4352 if (
auto __res = __sink_out._M_reserve(__sv.size()))
4354 __builtin_memcpy(__res.get(), __sv.data(), __sv.size());
4355 __res._M_bump(__sv.size());
4363 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4366 return std::move(__sink)._M_finish().out;
4370 auto __ctx = __loc ==
nullptr
4371 ? _Context(__args, __sink_out)
4372 : _Context(__args, __sink_out, *__loc);
4373 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
4374 __scanner._M_scan();
4376 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4379 return std::move(__sink)._M_finish().out;
4381#pragma GCC diagnostic pop
4386#if __cpp_lib_format >= 202305L
4389 template<
typename _CharT>
4390 template<
typename... _Ts>
4392 basic_format_parse_context<_CharT>::
4393 __check_dynamic_spec(
size_t __id)
noexcept
4395 if (__id >= _M_num_args)
4396 __format::__invalid_arg_id_in_format_string();
4397 if constexpr (
sizeof...(_Ts) != 0)
4399 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
4400 auto __arg =
static_cast<_Parse_ctx*
>(
this)->_M_types[__id];
4401 __format::_Arg_t __types[] = {
4402 __format::__to_arg_t_enum<_CharT, _Ts>()...
4404 for (
auto __t : __types)
4408 __invalid_dynamic_spec(
"arg(id) type does not match");
4413 template<
typename _CharT,
typename... _Args>
4414 template<
typename _Tp>
4415 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
4417 basic_format_string<_CharT, _Args...>::
4418 basic_format_string(
const _Tp& __s)
4421 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
4423 __scanner._M_scan();
4428 template<
typename _Out>
requires output_iterator<_Out, const char&>
4429 [[__gnu__::__always_inline__]]
4431 vformat_to(_Out __out, string_view __fmt, format_args __args)
4432 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
4434#ifdef _GLIBCXX_USE_WCHAR_T
4435 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
4436 [[__gnu__::__always_inline__]]
4438 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
4439 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
4442 template<
typename _Out>
requires output_iterator<_Out, const char&>
4443 [[__gnu__::__always_inline__]]
4445 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
4448 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
4451#ifdef _GLIBCXX_USE_WCHAR_T
4452 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
4453 [[__gnu__::__always_inline__]]
4455 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
4456 wformat_args __args)
4458 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
4464 vformat(string_view __fmt, format_args __args)
4466 __format::_Str_sink<char> __buf;
4467 std::vformat_to(__buf.out(), __fmt, __args);
4471#ifdef _GLIBCXX_USE_WCHAR_T
4474 vformat(wstring_view __fmt, wformat_args __args)
4476 __format::_Str_sink<wchar_t> __buf;
4477 std::vformat_to(__buf.out(), __fmt, __args);
4484 vformat(
const locale& __loc, string_view __fmt, format_args __args)
4486 __format::_Str_sink<char> __buf;
4487 std::vformat_to(__buf.out(), __loc, __fmt, __args);
4491#ifdef _GLIBCXX_USE_WCHAR_T
4494 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
4496 __format::_Str_sink<wchar_t> __buf;
4497 std::vformat_to(__buf.out(), __loc, __fmt, __args);
4502 template<
typename... _Args>
4505 format(format_string<_Args...> __fmt, _Args&&... __args)
4506 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
4508#ifdef _GLIBCXX_USE_WCHAR_T
4509 template<
typename... _Args>
4512 format(wformat_string<_Args...> __fmt, _Args&&... __args)
4513 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
4516 template<
typename... _Args>
4519 format(
const locale& __loc, format_string<_Args...> __fmt,
4522 return std::vformat(__loc, __fmt.get(),
4523 std::make_format_args(__args...));
4526#ifdef _GLIBCXX_USE_WCHAR_T
4527 template<
typename... _Args>
4530 format(
const locale& __loc, wformat_string<_Args...> __fmt,
4533 return std::vformat(__loc, __fmt.get(),
4534 std::make_wformat_args(__args...));
4538 template<
typename _Out,
typename... _Args>
4539 requires output_iterator<_Out, const char&>
4541 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
4543 return std::vformat_to(
std::move(__out), __fmt.get(),
4544 std::make_format_args(__args...));
4547#ifdef _GLIBCXX_USE_WCHAR_T
4548 template<
typename _Out,
typename... _Args>
4549 requires output_iterator<_Out, const wchar_t&>
4551 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
4553 return std::vformat_to(
std::move(__out), __fmt.get(),
4554 std::make_wformat_args(__args...));
4558 template<
typename _Out,
typename... _Args>
4559 requires output_iterator<_Out, const char&>
4561 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
4564 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
4565 std::make_format_args(__args...));
4568#ifdef _GLIBCXX_USE_WCHAR_T
4569 template<
typename _Out,
typename... _Args>
4570 requires output_iterator<_Out, const wchar_t&>
4572 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
4575 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
4576 std::make_wformat_args(__args...));
4580 template<
typename _Out,
typename... _Args>
4581 requires output_iterator<_Out, const char&>
4582 inline format_to_n_result<_Out>
4583 format_to_n(_Out __out, iter_difference_t<_Out> __n,
4584 format_string<_Args...> __fmt, _Args&&... __args)
4586 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
4587 std::vformat_to(__sink.out(), __fmt.get(),
4588 std::make_format_args(__args...));
4592#ifdef _GLIBCXX_USE_WCHAR_T
4593 template<
typename _Out,
typename... _Args>
4594 requires output_iterator<_Out, const wchar_t&>
4595 inline format_to_n_result<_Out>
4596 format_to_n(_Out __out, iter_difference_t<_Out> __n,
4597 wformat_string<_Args...> __fmt, _Args&&... __args)
4599 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
4600 std::vformat_to(__sink.out(), __fmt.get(),
4601 std::make_wformat_args(__args...));
4606 template<
typename _Out,
typename... _Args>
4607 requires output_iterator<_Out, const char&>
4608 inline format_to_n_result<_Out>
4609 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
4610 format_string<_Args...> __fmt, _Args&&... __args)
4612 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
4613 std::vformat_to(__sink.out(), __loc, __fmt.get(),
4614 std::make_format_args(__args...));
4618#ifdef _GLIBCXX_USE_WCHAR_T
4619 template<
typename _Out,
typename... _Args>
4620 requires output_iterator<_Out, const wchar_t&>
4621 inline format_to_n_result<_Out>
4622 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
4623 wformat_string<_Args...> __fmt, _Args&&... __args)
4625 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
4626 std::vformat_to(__sink.out(), __loc, __fmt.get(),
4627 std::make_wformat_args(__args...));
4636 template<
typename _CharT>
4637 class _Counting_sink final :
public _Iter_sink<_CharT, _CharT*>
4640 _Counting_sink() : _Iter_sink<_CharT, _CharT*>(nullptr, 0) { }
4642 [[__gnu__::__always_inline__]]
4645 {
return this->_M_count + this->_M_used().size(); }
4648 template<
typename _CharT>
4649 class _Counting_sink :
public _Buf_sink<_CharT>
4651 size_t _M_count = 0;
4654 _M_overflow()
override
4656 if (!std::is_constant_evaluated())
4657 _M_count += this->_M_used().size();
4662 _Counting_sink() =
default;
4664 [[__gnu__::__always_inline__]]
4668 _Counting_sink::_M_overflow();
4676 template<
typename... _Args>
4679 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
4681 __format::_Counting_sink<char> __buf;
4682 std::vformat_to(__buf.out(), __fmt.get(),
4683 std::make_format_args(__args...));
4684 return __buf.count();
4687#ifdef _GLIBCXX_USE_WCHAR_T
4688 template<
typename... _Args>
4691 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
4693 __format::_Counting_sink<wchar_t> __buf;
4694 std::vformat_to(__buf.out(), __fmt.get(),
4695 std::make_wformat_args(__args...));
4696 return __buf.count();
4700 template<
typename... _Args>
4703 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
4706 __format::_Counting_sink<char> __buf;
4707 std::vformat_to(__buf.out(), __loc, __fmt.get(),
4708 std::make_format_args(__args...));
4709 return __buf.count();
4712#ifdef _GLIBCXX_USE_WCHAR_T
4713 template<
typename... _Args>
4716 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
4719 __format::_Counting_sink<wchar_t> __buf;
4720 std::vformat_to(__buf.out(), __loc, __fmt.get(),
4721 std::make_wformat_args(__args...));
4722 return __buf.count();
4726#if __cpp_lib_format_ranges
4729 enum class range_format {
4739 template<
typename _Rg>
4740 constexpr auto format_kind = not defined(format_kind<_Rg>);
4742 template<
typename _Tp>
4743 consteval range_format
4746 using _Ref = ranges::range_reference_t<_Tp>;
4747 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
4748 return range_format::disabled;
4749 else if constexpr (
requires {
typename _Tp::key_type; })
4751 if constexpr (
requires {
typename _Tp::mapped_type; })
4753 using _Up = remove_cvref_t<_Ref>;
4754 if constexpr (__is_pair<_Up>)
4755 return range_format::map;
4756 else if constexpr (__is_specialization_of<_Up, tuple>)
4757 if constexpr (tuple_size_v<_Up> == 2)
4758 return range_format::map;
4760 return range_format::set;
4763 return range_format::sequence;
4768 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
4769 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
4772 template<
typename _Tp,
typename _CharT =
char>
4773 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
4774 class range_formatter;
4780 template<range_format _Kind, ranges::input_range _Rg,
typename _CharT>
4781 struct __range_default_formatter;
4787 template<ranges::input_range _Rg,
typename _CharT>
4788 requires (format_kind<_Rg> != range_format::disabled)
4789 && formattable<ranges::range_reference_t<_Rg>, _CharT>
4790 struct formatter<_Rg, _CharT>
4791 : __format::__range_default_formatter<format_kind<_Rg>, _Rg, _CharT>
4795_GLIBCXX_END_NAMESPACE_VERSION
4798#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.
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
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.
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
basic_string< char > string
A string of char.
basic_string< wchar_t > wstring
A string of wchar_t.
ISO C++ entities toplevel namespace is std.
chars_format
floating-point format for primitive numerical conversion
_CharT toupper(_CharT __c, const locale &__loc)
Convenience interface to ctype.toupper(__c).
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
GNU extensions for public use.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
const _CharT * data() const noexcept
Return const pointer to contents.
void __resize_and_overwrite(size_type __n, _Operation __op)
Non-standard version of resize_and_overwrite for C++11 and above.
basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
void reserve(size_type __res_arg)
Attempt to preallocate enough memory for specified number of characters.
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
basic_string & append(const basic_string &__str)
Append a string to this string.
bool empty() const noexcept
size_type capacity() const noexcept
Container class for localization functionality.