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 template<
typename _Out>
1281 typename basic_format_context<_Out, _CharT>::iterator
1282 _M_format_character(_CharT __c,
1283 basic_format_context<_Out, _CharT>& __fc)
const
1285 return __format::__write_padded_as_spec({&__c, 1u}, 1, __fc, _M_spec);
1288 template<
typename _Int>
1290 _S_to_character(_Int __i)
1293 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1295 if (_Traits::__min <= __i && __i <= _Traits::__max)
1296 return static_cast<_CharT
>(__i);
1298 else if constexpr (is_signed_v<_Int>)
1300 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1301 return static_cast<_CharT
>(__i);
1303 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1304 return static_cast<_CharT
>(__i);
1305 __throw_format_error(
"format error: integer not representable as "
1309 template<
typename _Out>
1310 typename basic_format_context<_Out, _CharT>::iterator
1311 _M_format_int(string_view __narrow_str,
size_t __prefix_len,
1312 basic_format_context<_Out, _CharT>& __fc)
const
1314 size_t __width = _M_spec._M_get_width(__fc);
1316 basic_string_view<_CharT> __str;
1317 if constexpr (is_same_v<char, _CharT>)
1318 __str = __narrow_str;
1319#ifdef _GLIBCXX_USE_WCHAR_T
1322 size_t __n = __narrow_str.size();
1323 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
1324 std::__to_wstring_numeric(__narrow_str.data(), __n, __p);
1329 if (_M_spec._M_localized)
1331 const auto& __l = __fc.locale();
1332 if (__l.name() !=
"C")
1334 auto& __np = use_facet<numpunct<_CharT>>(__l);
1335 string __grp = __np.grouping();
1338 size_t __n = __str.size() - __prefix_len;
1339 auto __p = (_CharT*)__builtin_alloca(2 * __n
1342 auto __s = __str.data();
1343 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1344 __s += __prefix_len;
1345 auto __end = std::__add_grouping(__p + __prefix_len,
1346 __np.thousands_sep(),
1350 __str = {__p, size_t(__end - __p)};
1355 if (__width <= __str.size())
1356 return __format::__write(__fc.out(), __str);
1358 char32_t __fill_char = _M_spec._M_fill;
1359 _Align __align = _M_spec._M_align;
1361 size_t __nfill = __width - __str.size();
1362 auto __out = __fc.out();
1363 if (__align == _Align_default)
1365 __align = _Align_right;
1366 if (_M_spec._M_zero_fill)
1368 __fill_char = _CharT(
'0');
1370 if (__prefix_len != 0)
1372 __out = __format::__write(
std::move(__out),
1373 __str.substr(0, __prefix_len));
1374 __str.remove_prefix(__prefix_len);
1378 __fill_char = _CharT(
' ');
1380 return __format::__write_padded(
std::move(__out), __str,
1381 __align, __nfill, __fill_char);
1384#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__
1385 template<
typename _Tp>
1387 =
typename __conditional_t<(
sizeof(_Tp) <=
sizeof(
long long)),
1389 type_identity<unsigned __int128>>::type;
1392 template<
typename _Int>
1393 static to_chars_result
1394 to_chars(
char* __first,
char* __last, _Int __value,
int __base)
1395 {
return std::__to_chars_i<_Int>(__first, __last, __value, __base); }
1398 _Spec<_CharT> _M_spec{};
1409#undef _GLIBCXX_FORMAT_F128
1411#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1414 using __float128_t = __ieee128;
1415# define _GLIBCXX_FORMAT_F128 1
1417#ifdef __LONG_DOUBLE_IEEE128__
1421 to_chars(
char*,
char*, __ibm128)
noexcept
1422 __asm(
"_ZSt8to_charsPcS_e");
1425 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1426 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1429 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1430 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1431#elif __cplusplus == 202002L
1433 to_chars(
char*,
char*, __ieee128)
noexcept
1434 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1437 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1438 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1441 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1442 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1445#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1448 using __float128_t =
long double;
1449# define _GLIBCXX_FORMAT_F128 1
1451#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1454 using __float128_t = _Float128;
1455# define _GLIBCXX_FORMAT_F128 2
1457# if __cplusplus == 202002L
1461 to_chars(
char*,
char*, _Float128)
noexcept
1462# if _GLIBCXX_INLINE_VERSION
1463 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1465 __asm(
"_ZSt8to_charsPcS_DF128_");
1469 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1470# if _GLIBCXX_INLINE_VERSION
1471 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1473 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
1477 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
1478# if _GLIBCXX_INLINE_VERSION
1479 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
1481 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
1486 using std::to_chars;
1489 template<
typename _Tp>
1490 concept __formattable_float
1491 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
1492 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
1494 template<__
char _CharT>
1495 struct __formatter_fp
1497 constexpr typename basic_format_parse_context<_CharT>::iterator
1498 parse(basic_format_parse_context<_CharT>& __pc)
1500 _Spec<_CharT> __spec{};
1501 const auto __last = __pc.end();
1502 auto __first = __pc.begin();
1504 auto __finalize = [
this, &__spec] {
1508 auto __finished = [&] {
1509 if (__first == __last || *__first ==
'}')
1520 __first = __spec._M_parse_fill_and_align(__first, __last);
1524 __first = __spec._M_parse_sign(__first, __last);
1528 __first = __spec._M_parse_alternate_form(__first, __last);
1532 __first = __spec._M_parse_zero_fill(__first, __last);
1536 if (__first[0] !=
'.')
1538 __first = __spec._M_parse_width(__first, __last, __pc);
1543 __first = __spec._M_parse_precision(__first, __last, __pc);
1547 __first = __spec._M_parse_locale(__first, __last);
1554 __spec._M_type = _Pres_a;
1558 __spec._M_type = _Pres_A;
1562 __spec._M_type = _Pres_e;
1566 __spec._M_type = _Pres_E;
1570 __spec._M_type = _Pres_f;
1574 __spec._M_type = _Pres_F;
1578 __spec._M_type = _Pres_g;
1582 __spec._M_type = _Pres_G;
1590 __format::__failed_to_parse_format_spec();
1593 template<
typename _Fp,
typename _Out>
1594 typename basic_format_context<_Out, _CharT>::iterator
1595 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
1599 to_chars_result __res{};
1602 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
1604 __prec = _M_spec._M_get_precision(__fc);
1606 char* __start = __buf + 1;
1607 char* __end = __buf +
sizeof(__buf);
1610 bool __upper =
false;
1611 bool __trailing_zeros =
false;
1614 switch (_M_spec._M_type)
1621 if (_M_spec._M_type != _Pres_A)
1623 __fmt = chars_format::hex;
1631 __fmt = chars_format::scientific;
1638 __fmt = chars_format::fixed;
1645 __trailing_zeros =
true;
1647 __fmt = chars_format::general;
1651 __fmt = chars_format::general;
1654 __builtin_unreachable();
1658 auto __to_chars = [&](
char* __b,
char* __e) {
1660 return __format::to_chars(__b, __e, __v, __fmt, __prec);
1661 else if (__fmt != chars_format{})
1662 return __format::to_chars(__b, __e, __v, __fmt);
1664 return __format::to_chars(__b, __e, __v);
1668 __res = __to_chars(__start, __end);
1670 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
1674 size_t __guess = 8 + __prec;
1675 if (__fmt == chars_format::fixed)
1677 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
1678 || is_same_v<_Fp, long double>)
1683 if constexpr (is_same_v<_Fp, float>)
1684 __builtin_frexpf(__v, &__exp);
1685 else if constexpr (is_same_v<_Fp, double>)
1686 __builtin_frexp(__v, &__exp);
1687 else if constexpr (is_same_v<_Fp, long double>)
1688 __builtin_frexpl(__v, &__exp);
1690 __guess += 1U + __exp * 4004U / 13301U;
1693 __guess += numeric_limits<_Fp>::max_exponent10;
1695 if (__guess <=
sizeof(__buf)) [[unlikely]]
1696 __guess =
sizeof(__buf) * 2;
1705 auto __overwrite = [&__to_chars, &__res] (
char* __p,
size_t __n)
1707 __res = __to_chars(__p + 1, __p + __n - 1);
1708 return __res.ec == errc{} ? __res.ptr - __p : 0;
1713 __start = __dynbuf.
data() + 1;
1714 __end = __dynbuf.
data() + __dynbuf.
size();
1716 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
1722 for (
char* __p = __start; __p != __res.ptr; ++__p)
1726 bool __have_sign =
true;
1728 if (!__builtin_signbit(__v))
1730 if (_M_spec._M_sign == _Sign_plus)
1732 else if (_M_spec._M_sign == _Sign_space)
1735 __have_sign =
false;
1738 string_view __narrow_str(__start, __res.ptr - __start);
1742 if (_M_spec._M_alt && __builtin_isfinite(__v))
1744 string_view __s = __narrow_str;
1748 size_t __d = __s.find(
'.');
1749 if (__d != __s.npos)
1751 __p = __s.find(__expc, __d + 1);
1752 if (__p == __s.npos)
1756 if (__trailing_zeros)
1759 if (__s[__have_sign] !=
'0')
1761 __sigfigs = __p - __have_sign - 1;
1766 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
1771 __p = __s.find(__expc);
1772 if (__p == __s.npos)
1775 __sigfigs = __d - __have_sign;
1778 if (__trailing_zeros && __prec != 0)
1783 __z = __prec - __sigfigs;
1786 if (
size_t __extras =
int(__d == __p) + __z)
1788 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
1792 __builtin_memmove(__start + __p + __extras,
1796 __start[__p++] =
'.';
1797 __builtin_memset(__start + __p,
'0', __z);
1798 __narrow_str = {__s.data(), __s.size() + __extras};
1802 __dynbuf.
reserve(__s.size() + __extras);
1803 if (__dynbuf.
empty())
1805 __dynbuf = __s.
substr(0, __p);
1809 __dynbuf.
append(__z,
'0');
1810 __dynbuf.
append(__s.substr(__p));
1814 __dynbuf.
insert(__p, __extras,
'0');
1816 __dynbuf[__p] =
'.';
1818 __narrow_str = __dynbuf;
1823 basic_string<_CharT> __wstr;
1824 basic_string_view<_CharT> __str;
1825 if constexpr (is_same_v<_CharT, char>)
1826 __str = __narrow_str;
1827#ifdef _GLIBCXX_USE_WCHAR_T
1830 __wstr = std::__to_wstring_numeric(__narrow_str);
1835 if (_M_spec._M_localized && __builtin_isfinite(__v))
1837 __wstr = _M_localize(__str, __expc, __fc.locale());
1838 if (!__wstr.empty())
1842 size_t __width = _M_spec._M_get_width(__fc);
1844 if (__width <= __str.size())
1845 return __format::__write(__fc.out(), __str);
1847 char32_t __fill_char = _M_spec._M_fill;
1848 _Align __align = _M_spec._M_align;
1850 size_t __nfill = __width - __str.size();
1851 auto __out = __fc.out();
1852 if (__align == _Align_default)
1854 __align = _Align_right;
1855 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
1857 __fill_char = _CharT(
'0');
1859 if (!__format::__is_xdigit(__narrow_str[0]))
1861 *__out++ = __str[0];
1862 __str.remove_prefix(1);
1866 __fill_char = _CharT(
' ');
1868 return __format::__write_padded(
std::move(__out), __str,
1869 __align, __nfill, __fill_char);
1873 basic_string<_CharT>
1874 _M_localize(basic_string_view<_CharT> __str,
char __expc,
1875 const locale& __loc)
const
1877 basic_string<_CharT> __lstr;
1879 if (__loc == locale::classic())
1882 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
1883 const _CharT __point = __np.decimal_point();
1884 const string __grp = __np.grouping();
1886 _CharT __dot, __exp;
1887 if constexpr (is_same_v<_CharT, char>)
1910 __builtin_unreachable();
1914 if (__grp.empty() && __point == __dot)
1917 size_t __d = __str.find(__dot);
1918 size_t __e = min(__d, __str.find(__exp));
1919 if (__e == __str.npos)
1921 const size_t __r = __str.size() - __e;
1922 auto __overwrite = [&](_CharT* __p, size_t) {
1924 auto __end = std::__add_grouping(__p, __np.thousands_sep(),
1925 __grp.data(), __grp.size(),
1926 __str.data(), __str.data() + __e);
1929 if (__d != __str.npos)
1935 const size_t __rlen = __str.size() - __e;
1937 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
1940 return (__end - __p);
1942 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
1946 _Spec<_CharT> _M_spec{};
1953 template<__format::__
char _CharT>
1954 struct formatter<_CharT, _CharT>
1956 formatter() =
default;
1958 constexpr typename basic_format_parse_context<_CharT>::iterator
1959 parse(basic_format_parse_context<_CharT>& __pc)
1961 return _M_f.template _M_parse<_CharT>(__pc);
1964 template<
typename _Out>
1965 typename basic_format_context<_Out, _CharT>::iterator
1966 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
1968 if (_M_f._M_spec._M_type == __format::_Pres_none
1969 || _M_f._M_spec._M_type == __format::_Pres_c)
1970 return _M_f._M_format_character(__u, __fc);
1971 else if (_M_f._M_spec._M_type == __format::_Pres_esc)
1977 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
1980#if __cpp_lib_format_ranges
1982 set_debug_format() noexcept
1983 { _M_f._M_spec._M_type = __format::_Pres_esc; }
1987 __format::__formatter_int<_CharT> _M_f;
1990#ifdef _GLIBCXX_USE_WCHAR_T
1993 struct formatter<char, wchar_t>
1995 formatter() =
default;
1997 constexpr typename basic_format_parse_context<wchar_t>::iterator
1998 parse(basic_format_parse_context<wchar_t>& __pc)
2000 return _M_f._M_parse<
char>(__pc);
2003 template<
typename _Out>
2004 typename basic_format_context<_Out, wchar_t>::iterator
2005 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2007 if (_M_f._M_spec._M_type == __format::_Pres_none
2008 || _M_f._M_spec._M_type == __format::_Pres_c)
2009 return _M_f._M_format_character(__u, __fc);
2010 else if (_M_f._M_spec._M_type == __format::_Pres_esc)
2016 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2019#if __cpp_lib_format_ranges
2021 set_debug_format() noexcept
2022 { _M_f._M_spec._M_type = __format::_Pres_esc; }
2026 __format::__formatter_int<wchar_t> _M_f;
2033 template<__format::__
char _CharT>
2034 struct formatter<_CharT*, _CharT>
2036 formatter() =
default;
2038 [[__gnu__::__always_inline__]]
2039 constexpr typename basic_format_parse_context<_CharT>::iterator
2040 parse(basic_format_parse_context<_CharT>& __pc)
2041 {
return _M_f.parse(__pc); }
2043 template<
typename _Out>
2044 [[__gnu__::__nonnull__]]
2045 typename basic_format_context<_Out, _CharT>::iterator
2046 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2047 {
return _M_f.format(__u, __fc); }
2049#if __cpp_lib_format_ranges
2050 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2054 __format::__formatter_str<_CharT> _M_f;
2057 template<__format::__
char _CharT>
2058 struct formatter<const _CharT*, _CharT>
2060 formatter() =
default;
2062 [[__gnu__::__always_inline__]]
2063 constexpr typename basic_format_parse_context<_CharT>::iterator
2064 parse(basic_format_parse_context<_CharT>& __pc)
2065 {
return _M_f.parse(__pc); }
2067 template<
typename _Out>
2068 [[__gnu__::__nonnull__]]
2069 typename basic_format_context<_Out, _CharT>::iterator
2070 format(
const _CharT* __u,
2071 basic_format_context<_Out, _CharT>& __fc)
const
2072 {
return _M_f.format(__u, __fc); }
2074#if __cpp_lib_format_ranges
2075 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2079 __format::__formatter_str<_CharT> _M_f;
2082 template<__format::__
char _CharT,
size_t _Nm>
2083 struct formatter<_CharT[_Nm], _CharT>
2085 formatter() =
default;
2087 [[__gnu__::__always_inline__]]
2088 constexpr typename basic_format_parse_context<_CharT>::iterator
2089 parse(basic_format_parse_context<_CharT>& __pc)
2090 {
return _M_f.parse(__pc); }
2092 template<
typename _Out>
2093 typename basic_format_context<_Out, _CharT>::iterator
2094 format(
const _CharT (&__u)[_Nm],
2095 basic_format_context<_Out, _CharT>& __fc)
const
2096 {
return _M_f.format({__u, _Nm}, __fc); }
2098#if __cpp_lib_format_ranges
2099 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2103 __format::__formatter_str<_CharT> _M_f;
2106 template<
typename _Traits,
typename _Alloc>
2107 struct formatter<basic_string<char, _Traits, _Alloc>, char>
2109 formatter() =
default;
2111 [[__gnu__::__always_inline__]]
2112 constexpr typename basic_format_parse_context<char>::iterator
2113 parse(basic_format_parse_context<char>& __pc)
2114 {
return _M_f.parse(__pc); }
2116 template<
typename _Out>
2117 typename basic_format_context<_Out, char>::iterator
2118 format(
const basic_string<char, _Traits, _Alloc>& __u,
2119 basic_format_context<_Out, char>& __fc)
const
2120 {
return _M_f.format(__u, __fc); }
2122#if __cpp_lib_format_ranges
2123 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2127 __format::__formatter_str<char> _M_f;
2130#ifdef _GLIBCXX_USE_WCHAR_T
2131 template<
typename _Traits,
typename _Alloc>
2132 struct formatter<basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2134 formatter() =
default;
2136 [[__gnu__::__always_inline__]]
2137 constexpr typename basic_format_parse_context<wchar_t>::iterator
2138 parse(basic_format_parse_context<wchar_t>& __pc)
2139 {
return _M_f.parse(__pc); }
2141 template<
typename _Out>
2142 typename basic_format_context<_Out, wchar_t>::iterator
2143 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2144 basic_format_context<_Out, wchar_t>& __fc)
const
2145 {
return _M_f.format(__u, __fc); }
2147#if __cpp_lib_format_ranges
2148 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2152 __format::__formatter_str<wchar_t> _M_f;
2156 template<
typename _Traits>
2157 struct formatter<basic_string_view<char, _Traits>, char>
2159 formatter() =
default;
2161 [[__gnu__::__always_inline__]]
2162 constexpr typename basic_format_parse_context<char>::iterator
2163 parse(basic_format_parse_context<char>& __pc)
2164 {
return _M_f.parse(__pc); }
2166 template<
typename _Out>
2167 typename basic_format_context<_Out, char>::iterator
2168 format(basic_string_view<char, _Traits> __u,
2169 basic_format_context<_Out, char>& __fc)
const
2170 {
return _M_f.format(__u, __fc); }
2172#if __cpp_lib_format_ranges
2173 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2177 __format::__formatter_str<char> _M_f;
2180#ifdef _GLIBCXX_USE_WCHAR_T
2181 template<
typename _Traits>
2182 struct formatter<basic_string_view<wchar_t, _Traits>, wchar_t>
2184 formatter() =
default;
2186 [[__gnu__::__always_inline__]]
2187 constexpr typename basic_format_parse_context<wchar_t>::iterator
2188 parse(basic_format_parse_context<wchar_t>& __pc)
2189 {
return _M_f.parse(__pc); }
2191 template<
typename _Out>
2192 typename basic_format_context<_Out, wchar_t>::iterator
2193 format(basic_string_view<wchar_t, _Traits> __u,
2194 basic_format_context<_Out, wchar_t>& __fc)
const
2195 {
return _M_f.format(__u, __fc); }
2197#if __cpp_lib_format_ranges
2198 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2202 __format::__formatter_str<wchar_t> _M_f;
2212 template<
typename _Tp>
2213 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2215#if defined __SIZEOF_INT128__
2216 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2217 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2221 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2222 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2223#ifdef _GLIBCXX_USE_CHAR8_T
2224 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2226 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2227 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2232 template<
typename _Tp, __format::__
char _CharT>
2233 requires __format::__is_formattable_integer<_Tp>
2234 struct formatter<_Tp, _CharT>
2236 formatter() =
default;
2238 [[__gnu__::__always_inline__]]
2239 constexpr typename basic_format_parse_context<_CharT>::iterator
2240 parse(basic_format_parse_context<_CharT>& __pc)
2242 return _M_f.template _M_parse<_Tp>(__pc);
2245 template<
typename _Out>
2246 typename basic_format_context<_Out, _CharT>::iterator
2247 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2248 {
return _M_f.format(__u, __fc); }
2251 __format::__formatter_int<_CharT> _M_f;
2254#if defined __glibcxx_to_chars
2256 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2257 struct formatter<_Tp, _CharT>
2259 formatter() =
default;
2261 [[__gnu__::__always_inline__]]
2262 constexpr typename basic_format_parse_context<_CharT>::iterator
2263 parse(basic_format_parse_context<_CharT>& __pc)
2264 {
return _M_f.parse(__pc); }
2266 template<
typename _Out>
2267 typename basic_format_context<_Out, _CharT>::iterator
2268 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2269 {
return _M_f.format(__u, __fc); }
2272 __format::__formatter_fp<_CharT> _M_f;
2275#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2277 template<__format::__
char _CharT>
2278 struct formatter<long double, _CharT>
2280 formatter() =
default;
2282 [[__gnu__::__always_inline__]]
2283 constexpr typename basic_format_parse_context<_CharT>::iterator
2284 parse(basic_format_parse_context<_CharT>& __pc)
2285 {
return _M_f.parse(__pc); }
2287 template<
typename _Out>
2288 typename basic_format_context<_Out, _CharT>::iterator
2289 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2290 {
return _M_f.format((
double)__u, __fc); }
2293 __format::__formatter_fp<_CharT> _M_f;
2297#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2299 template<__format::__
char _CharT>
2300 struct formatter<_Float16, _CharT>
2302 formatter() =
default;
2304 [[__gnu__::__always_inline__]]
2305 constexpr typename basic_format_parse_context<_CharT>::iterator
2306 parse(basic_format_parse_context<_CharT>& __pc)
2307 {
return _M_f.parse(__pc); }
2309 template<
typename _Out>
2310 typename basic_format_context<_Out, _CharT>::iterator
2311 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
2312 {
return _M_f.format((
float)__u, __fc); }
2315 __format::__formatter_fp<_CharT> _M_f;
2319#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2321 template<__format::__
char _CharT>
2322 struct formatter<_Float32, _CharT>
2324 formatter() =
default;
2326 [[__gnu__::__always_inline__]]
2327 constexpr typename basic_format_parse_context<_CharT>::iterator
2328 parse(basic_format_parse_context<_CharT>& __pc)
2329 {
return _M_f.parse(__pc); }
2331 template<
typename _Out>
2332 typename basic_format_context<_Out, _CharT>::iterator
2333 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
2334 {
return _M_f.format((
float)__u, __fc); }
2337 __format::__formatter_fp<_CharT> _M_f;
2341#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2343 template<__format::__
char _CharT>
2344 struct formatter<_Float64, _CharT>
2346 formatter() =
default;
2348 [[__gnu__::__always_inline__]]
2349 constexpr typename basic_format_parse_context<_CharT>::iterator
2350 parse(basic_format_parse_context<_CharT>& __pc)
2351 {
return _M_f.parse(__pc); }
2353 template<
typename _Out>
2354 typename basic_format_context<_Out, _CharT>::iterator
2355 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
2356 {
return _M_f.format((
double)__u, __fc); }
2359 __format::__formatter_fp<_CharT> _M_f;
2363#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128 == 1
2365 template<__format::__
char _CharT>
2366 struct formatter<_Float128, _CharT>
2368 formatter() =
default;
2370 [[__gnu__::__always_inline__]]
2371 constexpr typename basic_format_parse_context<_CharT>::iterator
2372 parse(basic_format_parse_context<_CharT>& __pc)
2373 {
return _M_f.parse(__pc); }
2375 template<
typename _Out>
2376 typename basic_format_context<_Out, _CharT>::iterator
2377 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
2378 {
return _M_f.format((__format::__float128_t)__u, __fc); }
2381 __format::__formatter_fp<_CharT> _M_f;
2385#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2387 template<__format::__
char _CharT>
2388 struct formatter<
__gnu_cxx::__bfloat16_t, _CharT>
2390 formatter() =
default;
2392 [[__gnu__::__always_inline__]]
2393 constexpr typename basic_format_parse_context<_CharT>::iterator
2394 parse(basic_format_parse_context<_CharT>& __pc)
2395 {
return _M_f.parse(__pc); }
2397 template<
typename _Out>
2398 typename basic_format_context<_Out, _CharT>::iterator
2399 format(__gnu_cxx::__bfloat16_t __u,
2400 basic_format_context<_Out, _CharT>& __fc)
const
2401 {
return _M_f.format((
float)__u, __fc); }
2404 __format::__formatter_fp<_CharT> _M_f;
2412 template<__format::__
char _CharT>
2413 struct formatter<const void*, _CharT>
2415 formatter() =
default;
2417 constexpr typename basic_format_parse_context<_CharT>::iterator
2418 parse(basic_format_parse_context<_CharT>& __pc)
2420 __format::_Spec<_CharT> __spec{};
2421 const auto __last = __pc.end();
2422 auto __first = __pc.begin();
2424 auto __finalize = [
this, &__spec] {
2428 auto __finished = [&] {
2429 if (__first == __last || *__first ==
'}')
2440 __first = __spec._M_parse_fill_and_align(__first, __last);
2446#if __glibcxx_format >= 202304L
2447 __first = __spec._M_parse_zero_fill(__first, __last);
2452 __first = __spec._M_parse_width(__first, __last, __pc);
2454 if (__first != __last)
2456 if (*__first ==
'p')
2458#if __glibcxx_format >= 202304L
2459 else if (*__first ==
'P')
2461 __spec._M_type = __format::_Pres_P;
2470 __format::__failed_to_parse_format_spec();
2473 template<
typename _Out>
2474 typename basic_format_context<_Out, _CharT>::iterator
2475 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2477 auto __u =
reinterpret_cast<__UINTPTR_TYPE__
>(__v);
2478 char __buf[2 +
sizeof(__v) * 2];
2479 auto [__ptr, __ec] = std::to_chars(__buf + 2,
std::end(__buf),
2481 int __n = __ptr - __buf;
2484#if __glibcxx_format >= 202304L
2485 if (_M_spec._M_type == __format::_Pres_P)
2488 for (
auto __p = __buf + 2; __p != __ptr; ++__p)
2489#
if __has_builtin(__builtin_toupper)
2490 *__p = __builtin_toupper(*__p);
2497 basic_string_view<_CharT> __str;
2498 if constexpr (is_same_v<_CharT, char>)
2499 __str = string_view(__buf, __n);
2500#ifdef _GLIBCXX_USE_WCHAR_T
2503 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
2504 std::__to_wstring_numeric(__buf, __n, __p);
2505 __str = wstring_view(__p, __n);
2509#if __glibcxx_format >= 202304L
2510 if (_M_spec._M_zero_fill)
2512 size_t __width = _M_spec._M_get_width(__fc);
2513 if (__width <= __str.size())
2514 return __format::__write(__fc.out(), __str);
2516 auto __out = __fc.out();
2518 __out = __format::__write(
std::move(__out), __str.substr(0, 2));
2519 __str.remove_prefix(2);
2520 size_t __nfill = __width - __n;
2521 return __format::__write_padded(
std::move(__out), __str,
2522 __format::_Align_right,
2523 __nfill, _CharT(
'0'));
2527 return __format::__write_padded_as_spec(__str, __n, __fc, _M_spec,
2528 __format::_Align_right);
2532 __format::_Spec<_CharT> _M_spec{};
2535 template<__format::__
char _CharT>
2536 struct formatter<void*, _CharT>
2538 formatter() =
default;
2540 [[__gnu__::__always_inline__]]
2541 constexpr typename basic_format_parse_context<_CharT>::iterator
2542 parse(basic_format_parse_context<_CharT>& __pc)
2543 {
return _M_f.parse(__pc); }
2545 template<
typename _Out>
2546 typename basic_format_context<_Out, _CharT>::iterator
2547 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2548 {
return _M_f.format(__v, __fc); }
2551 formatter<const void*, _CharT> _M_f;
2554 template<__format::__
char _CharT>
2555 struct formatter<nullptr_t, _CharT>
2557 formatter() =
default;
2559 [[__gnu__::__always_inline__]]
2560 constexpr typename basic_format_parse_context<_CharT>::iterator
2561 parse(basic_format_parse_context<_CharT>& __pc)
2562 {
return _M_f.parse(__pc); }
2564 template<
typename _Out>
2565 typename basic_format_context<_Out, _CharT>::iterator
2566 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
2567 {
return _M_f.format(
nullptr, __fc); }
2570 formatter<const void*, _CharT> _M_f;
2574#if defined _GLIBCXX_USE_WCHAR_T && __cpp_lib_format_ranges
2578 namespace __format {
struct __disabled; }
2582 struct formatter<char*,
wchar_t>
2583 :
private formatter<__format::__disabled, wchar_t> { };
2585 struct formatter<const char*,
wchar_t>
2586 :
private formatter<__format::__disabled, wchar_t> { };
2587 template<
size_t _Nm>
2588 struct formatter<char[_Nm], wchar_t>
2589 :
private formatter<__format::__disabled, wchar_t> { };
2590 template<
class _Traits,
class _Allocator>
2591 struct formatter<basic_string<char, _Traits, _Allocator>, wchar_t>
2592 :
private formatter<__format::__disabled, wchar_t> { };
2593 template<
class _Traits>
2594 struct formatter<basic_string_view<char, _Traits>, wchar_t>
2595 :
private formatter<__format::__disabled, wchar_t> { };
2601 template<
typename _Tp,
typename _Context,
2603 =
typename _Context::template formatter_type<remove_const_t<_Tp>>,
2604 typename _ParseContext
2605 = basic_format_parse_context<typename _Context::char_type>>
2606 concept __parsable_with
2607 = semiregular<_Formatter>
2608 &&
requires (_Formatter __f, _ParseContext __pc)
2610 { __f.parse(__pc) } -> same_as<typename _ParseContext::iterator>;
2613 template<
typename _Tp,
typename _Context,
2615 =
typename _Context::template formatter_type<remove_const_t<_Tp>>,
2616 typename _ParseContext
2617 = basic_format_parse_context<typename _Context::char_type>>
2618 concept __formattable_with
2619 = semiregular<_Formatter>
2620 &&
requires (
const _Formatter __cf, _Tp&& __t, _Context __fc)
2622 { __cf.format(__t, __fc) } -> same_as<typename _Context::iterator>;
2626 template<
typename _CharT>
2627 using _Iter_for = back_insert_iterator<basic_string<_CharT>>;
2629 template<
typename _Tp,
typename _CharT,
2630 typename _Context = basic_format_context<_Iter_for<_CharT>, _CharT>>
2631 concept __formattable_impl
2632 = __parsable_with<_Tp, _Context> && __formattable_with<_Tp, _Context>;
2639#if __cplusplus > 202002L
2641 template<
typename _Tp,
typename _CharT>
2643 = __format::__formattable_impl<remove_reference_t<_Tp>, _CharT>;
2646#if __cpp_lib_format_ranges
2650 template<
typename _Rg,
typename _CharT>
2651 concept __const_formattable_range
2652 = ranges::input_range<const _Rg>
2653 && formattable<ranges::range_reference_t<const _Rg>, _CharT>;
2655 template<
typename _Rg,
typename _CharT>
2656 using __maybe_const_range
2657 = conditional_t<__const_formattable_range<_Rg, _CharT>,
const _Rg, _Rg>;
2664 template<
typename _Out>
2665 struct format_to_n_result
2668 iter_difference_t<_Out>
size;
2671_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
2672template<
typename,
typename>
class vector;
2673_GLIBCXX_END_NAMESPACE_CONTAINER
2678 template<
typename _CharT>
2681 _Sink<_CharT>* _M_sink =
nullptr;
2684 using iterator_category = output_iterator_tag;
2685 using value_type = void;
2686 using difference_type = ptrdiff_t;
2687 using pointer = void;
2688 using reference = void;
2690 _Sink_iter() =
default;
2691 _Sink_iter(
const _Sink_iter&) =
default;
2692 _Sink_iter& operator=(
const _Sink_iter&) =
default;
2694 [[__gnu__::__always_inline__]]
2696 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(
std::
addressof(__sink)) { }
2698 [[__gnu__::__always_inline__]]
2699 constexpr _Sink_iter&
2700 operator=(_CharT __c)
2702 _M_sink->_M_write(__c);
2706 [[__gnu__::__always_inline__]]
2707 constexpr _Sink_iter&
2708 operator=(basic_string_view<_CharT> __s)
2710 _M_sink->_M_write(__s);
2714 [[__gnu__::__always_inline__]]
2715 constexpr _Sink_iter&
2718 [[__gnu__::__always_inline__]]
2719 constexpr _Sink_iter&
2720 operator++() {
return *
this; }
2722 [[__gnu__::__always_inline__]]
2723 constexpr _Sink_iter
2724 operator++(
int) {
return *
this; }
2727 _M_reserve(
size_t __n)
const
2728 {
return _M_sink->_M_reserve(__n); }
2734 template<
typename _CharT>
2737 friend class _Sink_iter<_CharT>;
2739 span<_CharT> _M_span;
2740 typename span<_CharT>::iterator _M_next;
2746 virtual void _M_overflow() = 0;
2750 [[__gnu__::__always_inline__]]
2752 _Sink(span<_CharT> __span) noexcept
2753 : _M_span(__span), _M_next(__span.begin())
2757 [[__gnu__::__always_inline__]]
2759 _M_used() const noexcept
2760 {
return _M_span.first(_M_next - _M_span.begin()); }
2763 [[__gnu__::__always_inline__]]
2764 constexpr span<_CharT>
2765 _M_unused() const noexcept
2766 {
return _M_span.subspan(_M_next - _M_span.begin()); }
2769 [[__gnu__::__always_inline__]]
2771 _M_rewind() noexcept
2772 { _M_next = _M_span.begin(); }
2776 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
2779 _M_next = __s.begin() + __pos;
2784 _M_write(_CharT __c)
2787 if (_M_next - _M_span.begin() == std::ssize(_M_span)) [[unlikely]]
2792 _M_write(basic_string_view<_CharT> __s)
2794 span __to = _M_unused();
2795 while (__to.size() <= __s.size())
2797 __s.copy(__to.data(), __to.size());
2798 _M_next += __to.size();
2799 __s.remove_prefix(__to.size());
2805 __s.copy(__to.data(), __s.size());
2806 _M_next += __s.size();
2815 explicit operator bool() const noexcept {
return _M_sink; }
2817 _CharT* get() const noexcept {
return _M_sink->_M_next.operator->(); }
2819 void _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
2827 virtual _Reservation
2828 _M_reserve(
size_t __n)
2830 if (__n <= _M_unused().
size())
2833 if (__n <= _M_span.size())
2836 if (__n <= _M_unused().
size())
2849 _Sink(
const _Sink&) =
delete;
2850 _Sink& operator=(
const _Sink&) =
delete;
2852 [[__gnu__::__always_inline__]]
2853 constexpr _Sink_iter<_CharT>
2855 {
return _Sink_iter<_CharT>(*
this); }
2859 template<
typename _CharT>
2860 class _Buf_sink :
public _Sink<_CharT>
2863 _CharT _M_buf[32 *
sizeof(
void*) /
sizeof(_CharT)];
2865 [[__gnu__::__always_inline__]]
2867 _Buf_sink() noexcept
2868 : _Sink<_CharT>(_M_buf)
2872 using _GLIBCXX_STD_C::vector;
2876 template<
typename _Seq>
2877 class _Seq_sink final :
public _Buf_sink<typename _Seq::value_type>
2879 using _CharT =
typename _Seq::value_type;
2885 _M_overflow()
override
2887 auto __s = this->_M_used();
2888 if (__s.empty()) [[unlikely]]
2892 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
2894 if constexpr (__is_specialization_of<_Seq, basic_string>)
2895 _M_seq.append(__s.data(), __s.size());
2897 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
2903 typename _Sink<_CharT>::_Reservation
2904 _M_reserve(
size_t __n)
override
2913 if constexpr (__is_specialization_of<_Seq, basic_string>
2914 || __is_specialization_of<_Seq, vector>)
2917 if (this->_M_used().size()) [[unlikely]]
2918 _Seq_sink::_M_overflow();
2921 const auto __sz = _M_seq.size();
2922 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
2923 _M_seq.__resize_and_overwrite(__sz + __n,
2924 [](
auto,
auto __n2) {
2928 _M_seq.resize(__sz + __n);
2932 this->_M_reset(_M_seq, __sz);
2936 return _Sink<_CharT>::_M_reserve(__n);
2940 _M_bump(
size_t __n)
override
2942 if constexpr (__is_specialization_of<_Seq, basic_string>
2943 || __is_specialization_of<_Seq, vector>)
2945 auto __s = this->_M_used();
2946 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
2948 _M_seq.resize(__s.size() + __n);
2950 this->_M_reset(this->_M_buf);
2958 [[__gnu__::__always_inline__]]
2959 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
2962 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
2966 using _Sink<_CharT>::out;
2971 if (this->_M_used().
size() != 0)
2972 _Seq_sink::_M_overflow();
2981 auto __s = this->_M_used();
2984 if (__s.size() != 0)
2985 _Seq_sink::_M_overflow();
2992 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
2994 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3002 template<
typename _CharT,
typename _OutIter>
3003 class _Iter_sink :
public _Buf_sink<_CharT>
3006 iter_difference_t<_OutIter> _M_max;
3009 size_t _M_count = 0;
3012 _M_overflow()
override
3014 auto __s = this->_M_used();
3016 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3017 else if (_M_count <
static_cast<size_t>(_M_max))
3019 auto __max = _M_max - _M_count;
3020 span<_CharT> __first;
3021 if (__max < __s.size())
3022 __first = __s.first(
static_cast<size_t>(__max));
3025 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3028 _M_count += __s.size();
3032 [[__gnu__::__always_inline__]]
3034 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3035 : _M_out(
std::
move(__out)), _M_max(__max)
3038 using _Sink<_CharT>::out;
3040 format_to_n_result<_OutIter>
3043 if (this->_M_used().
size() != 0)
3044 _Iter_sink::_M_overflow();
3045 iter_difference_t<_OutIter> __count(_M_count);
3057 template<
typename _CharT, contiguous_iterator _OutIter>
3058 requires same_as<iter_value_t<_OutIter>, _CharT>
3059 class _Iter_sink<_CharT, _OutIter> :
public _Sink<_CharT>
3062 iter_difference_t<_OutIter> _M_max = -1;
3064 size_t _M_count = 0;
3070 _M_overflow()
override
3072 if (this->_M_unused().
size() != 0)
3075 auto __s = this->_M_used();
3079 _M_count += __s.size();
3083 this->_M_reset(this->_M_buf);
3089 this->_M_reset({__s.data(), __s.size() + 1024}, __s.size());
3093 typename _Sink<_CharT>::_Reservation
3094 _M_reserve(
size_t __n)
final
3096 auto __avail = this->_M_unused();
3097 if (__n > __avail.size())
3102 auto __s = this->_M_used();
3103 this->_M_reset({__s.data(), __s.size() + __n}, __s.size());
3110 _S_make_span(_CharT* __ptr, iter_difference_t<_OutIter> __n,
3111 span<_CharT> __buf)
noexcept
3118 if constexpr (!is_integral_v<iter_difference_t<_OutIter>>
3119 ||
sizeof(__n) >
sizeof(
size_t))
3122 auto __m = iter_difference_t<_OutIter>((
size_t)-1);
3126 return {__ptr, (size_t)__n};
3129#if __has_builtin(__builtin_dynamic_object_size)
3130 if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3131 return {__ptr, __bytes /
sizeof(_CharT)};
3134 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3135 __n = (1024 - __off) /
sizeof(_CharT);
3136 if (__n > 0) [[likely]]
3137 return {__ptr,
static_cast<size_t>(__n)};
3144 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1) noexcept
3145 : _Sink<_CharT>(_S_make_span(
std::to_address(__out), __n, _M_buf)),
3146 _M_first(__out), _M_max(__n)
3149 format_to_n_result<_OutIter>
3152 auto __s = this->_M_used();
3153 if (__s.data() == _M_buf)
3156 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3157 return { _M_first + _M_max, __count };
3161 iter_difference_t<_OutIter> __count(__s.size());
3162 return { _M_first + __count, __count };
3167 enum _Arg_t :
unsigned char {
3168 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
3169 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
3170 _Arg_i128, _Arg_u128,
3171 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
3172#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3174 _Arg_f128 = _Arg_ldbl,
3175 _Arg_ibm128 = _Arg_next_value_,
3182 template<
typename _Context>
3185 using _CharT =
typename _Context::char_type;
3201 unsigned long long _M_ull;
3204#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3205 long double _M_ldbl;
3207 const _CharT* _M_str;
3208 basic_string_view<_CharT> _M_sv;
3210 _HandleBase _M_handle;
3211#ifdef __SIZEOF_INT128__
3213 unsigned __int128 _M_u128;
3215#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3218#elif _GLIBCXX_FORMAT_F128 == 2
3219 __float128_t _M_f128;
3223 [[__gnu__::__always_inline__]]
3224 _Arg_value() : _M_none() { }
3227 template<
typename _Tp>
3228 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
3229 { _S_get<_Tp>() = __val; }
3232 template<
typename _Tp,
typename _Self>
3233 [[__gnu__::__always_inline__]]
3235 _S_get(_Self& __u)
noexcept
3237 if constexpr (is_same_v<_Tp, bool>)
3239 else if constexpr (is_same_v<_Tp, _CharT>)
3241 else if constexpr (is_same_v<_Tp, int>)
3243 else if constexpr (is_same_v<_Tp, unsigned>)
3245 else if constexpr (is_same_v<_Tp, long long>)
3247 else if constexpr (is_same_v<_Tp, unsigned long long>)
3249 else if constexpr (is_same_v<_Tp, float>)
3251 else if constexpr (is_same_v<_Tp, double>)
3253#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3254 else if constexpr (is_same_v<_Tp, long double>)
3257 else if constexpr (is_same_v<_Tp, __ieee128>)
3259 else if constexpr (is_same_v<_Tp, __ibm128>)
3260 return __u._M_ibm128;
3262 else if constexpr (is_same_v<_Tp, const _CharT*>)
3264 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
3266 else if constexpr (is_same_v<_Tp, const void*>)
3268#ifdef __SIZEOF_INT128__
3269 else if constexpr (is_same_v<_Tp, __int128>)
3271 else if constexpr (is_same_v<_Tp, unsigned __int128>)
3274#if _GLIBCXX_FORMAT_F128 == 2
3275 else if constexpr (is_same_v<_Tp, __float128_t>)
3278 else if constexpr (derived_from<_Tp, _HandleBase>)
3279 return static_cast<_Tp&
>(__u._M_handle);
3283 template<
typename _Tp>
3284 [[__gnu__::__always_inline__]]
3287 {
return _S_get<_Tp>(*
this); }
3289 template<
typename _Tp>
3290 [[__gnu__::__always_inline__]]
3292 _M_get() const noexcept
3293 {
return _S_get<_Tp>(*
this); }
3295 template<
typename _Tp>
3296 [[__gnu__::__always_inline__]]
3298 _M_set(_Tp __v)
noexcept
3300 if constexpr (derived_from<_Tp, _HandleBase>)
3301 std::construct_at(&_M_handle, __v);
3303 _S_get<_Tp>(*
this) = __v;
3308 template<
typename _Context,
typename... _Args>
3311 template<
typename _Visitor,
typename _Ctx>
3312 decltype(
auto) __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
3314 template<
typename _Ch,
typename _Tp>
3316 __to_arg_t_enum() noexcept;
3320 template<typename _Context>
3321 class basic_format_arg
3323 using _CharT =
typename _Context::char_type;
3325 template<
typename _Tp>
3326 static constexpr bool __formattable
3327 = __format::__formattable_with<_Tp, _Context>;
3330 class handle :
public __format::_Arg_value<_Context>::_HandleBase
3332 using _Base =
typename __format::_Arg_value<_Context>::_HandleBase;
3335 template<
typename _Tp>
3336 using __maybe_const_t
3337 = __conditional_t<__formattable<const _Tp>,
const _Tp, _Tp>;
3339 template<
typename _Tq>
3341 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
3342 _Context& __format_ctx,
const void* __ptr)
3344 using _Td = remove_const_t<_Tq>;
3345 typename _Context::template formatter_type<_Td> __f;
3346 __parse_ctx.advance_to(__f.parse(__parse_ctx));
3347 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
3348 __format_ctx.advance_to(__f.format(__val, __format_ctx));
3351 template<
typename _Tp>
3353 handle(_Tp& __val)
noexcept
3355 this->_M_ptr = __builtin_addressof(__val);
3356 auto __func = _S_format<__maybe_const_t<_Tp>>;
3357 this->_M_func =
reinterpret_cast<void(*)()
>(__func);
3360 friend class basic_format_arg<_Context>;
3363 handle(
const handle&) =
default;
3364 handle& operator=(
const handle&) =
default;
3366 [[__gnu__::__always_inline__]]
3368 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
3370 using _Func = void(*)(basic_format_parse_context<_CharT>&,
3371 _Context&,
const void*);
3372 auto __f =
reinterpret_cast<_Func
>(this->_M_func);
3373 __f(__pc, __fc, this->_M_ptr);
3377 [[__gnu__::__always_inline__]]
3378 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
3380 [[nodiscard,__gnu__::__always_inline__]]
3381 explicit operator bool() const noexcept
3382 {
return _M_type != __format::_Arg_none; }
3384#if __cpp_lib_format >= 202306L
3385 template<
typename _Visitor>
3387 visit(
this basic_format_arg __arg, _Visitor&& __vis)
3388 {
return __arg._M_visit_user(std::forward<_Visitor>(__vis), __arg._M_type); }
3390 template<
typename _Res,
typename _Visitor>
3392 visit(
this basic_format_arg __arg, _Visitor&& __vis)
3393 {
return __arg._M_visit_user(std::forward<_Visitor>(__vis), __arg._M_type); }
3397 template<
typename _Ctx>
3398 friend class basic_format_args;
3400 template<
typename _Ctx,
typename... _Args>
3401 friend class __format::_Arg_store;
3403 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
3405 __format::_Arg_value<_Context> _M_val;
3406 __format::_Arg_t _M_type;
3411 template<
typename _Tp>
3412 static consteval auto
3415 using _Td = remove_const_t<_Tp>;
3416 if constexpr (is_same_v<_Td, bool>)
3417 return type_identity<bool>();
3418 else if constexpr (is_same_v<_Td, _CharT>)
3419 return type_identity<_CharT>();
3420 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
3421 return type_identity<_CharT>();
3422#ifdef __SIZEOF_INT128__
3423 else if constexpr (is_same_v<_Td, __int128>)
3424 return type_identity<__int128>();
3425 else if constexpr (is_same_v<_Td, unsigned __int128>)
3426 return type_identity<unsigned __int128>();
3428 else if constexpr (__is_signed_integer<_Td>::value)
3430 if constexpr (
sizeof(_Td) <=
sizeof(
int))
3431 return type_identity<int>();
3432 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
3433 return type_identity<long long>();
3435 else if constexpr (__is_unsigned_integer<_Td>::value)
3437 if constexpr (
sizeof(_Td) <=
sizeof(
unsigned))
3438 return type_identity<unsigned>();
3439 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
3440 return type_identity<unsigned long long>();
3442 else if constexpr (is_same_v<_Td, float>)
3443 return type_identity<float>();
3444 else if constexpr (is_same_v<_Td, double>)
3445 return type_identity<double>();
3446#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3447 else if constexpr (is_same_v<_Td, long double>)
3448 return type_identity<long double>();
3450 else if constexpr (is_same_v<_Td, __ibm128>)
3451 return type_identity<__ibm128>();
3452 else if constexpr (is_same_v<_Td, __ieee128>)
3453 return type_identity<__ieee128>();
3456#if defined(__FLT16_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3457 else if constexpr (is_same_v<_Td, _Float16>)
3458 return type_identity<float>();
3461#if defined(__BFLT16_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3462 else if constexpr (is_same_v<_Td,
decltype(0.0bf16)>)
3463 return type_identity<float>();
3466#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3467 else if constexpr (is_same_v<_Td, _Float32>)
3468 return type_identity<float>();
3471#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3472 else if constexpr (is_same_v<_Td, _Float64>)
3473 return type_identity<double>();
3476#if _GLIBCXX_FORMAT_F128
3478 else if constexpr (is_same_v<_Td, _Float128>)
3479 return type_identity<__format::__float128_t>();
3481# if __SIZEOF_FLOAT128__
3482 else if constexpr (is_same_v<_Td, __float128>)
3483 return type_identity<__format::__float128_t>();
3486 else if constexpr (__is_specialization_of<_Td, basic_string_view>
3487 || __is_specialization_of<_Td, basic_string>)
3489 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
3490 return type_identity<basic_string_view<_CharT>>();
3492 return type_identity<handle>();
3494 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
3495 return type_identity<const _CharT*>();
3496 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
3497 return type_identity<const _CharT*>();
3498 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
3499 return type_identity<const void*>();
3500 else if constexpr (is_same_v<_Td, nullptr_t>)
3501 return type_identity<const void*>();
3503 return type_identity<handle>();
3507 template<
typename _Tp>
3508 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
3511 template<
typename _Tp>
3512 static consteval __format::_Arg_t
3515 using namespace __format;
3516 if constexpr (is_same_v<_Tp, bool>)
3518 else if constexpr (is_same_v<_Tp, _CharT>)
3520 else if constexpr (is_same_v<_Tp, int>)
3522 else if constexpr (is_same_v<_Tp, unsigned>)
3524 else if constexpr (is_same_v<_Tp, long long>)
3526 else if constexpr (is_same_v<_Tp, unsigned long long>)
3528 else if constexpr (is_same_v<_Tp, float>)
3530 else if constexpr (is_same_v<_Tp, double>)
3532#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3533 else if constexpr (is_same_v<_Tp, long double>)
3537 else if constexpr (is_same_v<_Tp, __ibm128>)
3539 else if constexpr (is_same_v<_Tp, __ieee128>)
3542 else if constexpr (is_same_v<_Tp, const _CharT*>)
3544 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
3546 else if constexpr (is_same_v<_Tp, const void*>)
3548#ifdef __SIZEOF_INT128__
3549 else if constexpr (is_same_v<_Tp, __int128>)
3551 else if constexpr (is_same_v<_Tp, unsigned __int128>)
3555#if _GLIBCXX_FORMAT_F128 == 2
3556 else if constexpr (is_same_v<_Tp, __format::__float128_t>)
3559 else if constexpr (is_same_v<_Tp, handle>)
3563 template<
typename _Tp>
3565 _M_set(_Tp __v)
noexcept
3567 _M_type = _S_to_enum<_Tp>();
3571 template<
typename _Tp>
3572 requires __format::__formattable_with<_Tp, _Context>
3574 basic_format_arg(_Tp& __v)
noexcept
3576 using _Td = _Normalize<_Tp>;
3577 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
3578 _M_set(_Td{__v.data(), __v.size()});
3579 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
3580 && is_same_v<_CharT, wchar_t>)
3581 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
3583 _M_set(
static_cast<_Td
>(__v));
3586 template<
typename _Ctx,
typename... _Argz>
3588 make_format_args(_Argz&...)
noexcept;
3590 template<
typename _Visitor,
typename _Ctx>
3591 friend decltype(
auto)
3592 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
3594 template<
typename _Visitor,
typename _Ctx>
3595 friend decltype(
auto)
3596 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
3598 template<
typename _Ch,
typename _Tp>
3599 friend consteval __format::_Arg_t
3600 __format::__to_arg_t_enum() noexcept;
3602 template<typename _Visitor>
3604 _M_visit(_Visitor&& __vis, __format::_Arg_t __type)
3606 using namespace __format;
3610 return std::forward<_Visitor>(__vis)(_M_val._M_none);
3612 return std::forward<_Visitor>(__vis)(_M_val._M_bool);
3614 return std::forward<_Visitor>(__vis)(_M_val._M_c);
3616 return std::forward<_Visitor>(__vis)(_M_val._M_i);
3618 return std::forward<_Visitor>(__vis)(_M_val._M_u);
3620 return std::forward<_Visitor>(__vis)(_M_val._M_ll);
3622 return std::forward<_Visitor>(__vis)(_M_val._M_ull);
3623#if __glibcxx_to_chars
3625 return std::forward<_Visitor>(__vis)(_M_val._M_flt);
3627 return std::forward<_Visitor>(__vis)(_M_val._M_dbl);
3628#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3630 return std::forward<_Visitor>(__vis)(_M_val._M_ldbl);
3633 return std::forward<_Visitor>(__vis)(_M_val._M_f128);
3635 return std::forward<_Visitor>(__vis)(_M_val._M_ibm128);
3639 return std::forward<_Visitor>(__vis)(_M_val._M_str);
3641 return std::forward<_Visitor>(__vis)(_M_val._M_sv);
3643 return std::forward<_Visitor>(__vis)(_M_val._M_ptr);
3646 auto& __h =
static_cast<handle&
>(_M_val._M_handle);
3647 return std::forward<_Visitor>(__vis)(__h);
3649#ifdef __SIZEOF_INT128__
3651 return std::forward<_Visitor>(__vis)(_M_val._M_i128);
3653 return std::forward<_Visitor>(__vis)(_M_val._M_u128);
3656#if _GLIBCXX_FORMAT_F128 == 2
3658 return std::forward<_Visitor>(__vis)(_M_val._M_f128);
3663 __builtin_unreachable();
3667 template<
typename _Visitor>
3669 _M_visit_user(_Visitor&& __vis, __format::_Arg_t __type)
3671 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
3673 constexpr bool __user_facing = __is_one_of<_Tp,
3674 monostate, bool, _CharT,
3675 int,
unsigned int,
long long int,
unsigned long long int,
3676 float, double,
long double,
3677 const _CharT*, basic_string_view<_CharT>,
3678 const void*, handle>::value;
3679 if constexpr (__user_facing)
3680 return std::forward<_Visitor>(__vis)(__val);
3684 return std::forward<_Visitor>(__vis)(__h);
3690 template<
typename _Visitor,
typename _Context>
3691 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
3692 inline decltype(auto)
3693 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
3695 return __arg._M_visit_user(std::forward<_Visitor>(__vis), __arg._M_type);
3701 template<
typename _Visitor,
typename _Ctx>
3702 inline decltype(
auto)
3703 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
3705 return __arg._M_visit(std::forward<_Visitor>(__vis), __arg._M_type);
3708 struct _WidthPrecVisitor
3710 template<
typename _Tp>
3712 operator()(_Tp& __arg)
const
3714 if constexpr (is_same_v<_Tp, monostate>)
3715 __format::__invalid_arg_id_in_format_string();
3719 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
3723 if constexpr (__is_unsigned_integer<_Tp>::value)
3725 else if constexpr (__is_signed_integer<_Tp>::value)
3729 __throw_format_error(
"format error: argument used for width or "
3730 "precision must be a non-negative integer");
3734#pragma GCC diagnostic push
3735#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
3736 template<
typename _Context>
3738 __int_from_arg(
const basic_format_arg<_Context>& __arg)
3739 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
3742 template<
int _Bits,
size_t _Nm>
3744 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
3746 __UINT64_TYPE__ __packed_types = 0;
3747 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
3748 __packed_types = (__packed_types << _Bits) | *__i;
3749 return __packed_types;
3754 template<
typename _Context>
3755 class basic_format_args
3757 static constexpr int _S_packed_type_bits = 5;
3758 static constexpr int _S_packed_type_mask = 0b11111;
3759 static constexpr int _S_max_packed_args = 12;
3761 static_assert( __format::_Arg_max_ <= (1 << _S_packed_type_bits) );
3763 template<
typename... _Args>
3764 using _Store = __format::_Arg_store<_Context, _Args...>;
3766 template<
typename _Ctx,
typename... _Args>
3767 friend class __format::_Arg_store;
3769 using uint64_t = __UINT64_TYPE__;
3770 using _Format_arg = basic_format_arg<_Context>;
3771 using _Format_arg_val = __format::_Arg_value<_Context>;
3777 uint64_t _M_packed_size : 4;
3778 uint64_t _M_unpacked_size : 60;
3781 const _Format_arg_val* _M_values;
3782 const _Format_arg* _M_args;
3786 _M_size() const noexcept
3787 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
3789 typename __format::_Arg_t
3790 _M_type(
size_t __i)
const noexcept
3792 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
3793 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
3796 template<
typename _Ctx,
typename... _Args>
3798 make_format_args(_Args&...)
noexcept;
3801 template<
typename... _Args>
3802 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
3804 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
3807 template<
typename... _Args>
3808 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
3810 [[nodiscard,__gnu__::__always_inline__]]
3811 basic_format_arg<_Context>
3812 get(
size_t __i)
const noexcept
3814 basic_format_arg<_Context> __arg;
3815 if (__i < _M_packed_size)
3817 __arg._M_type = _M_type(__i);
3818 __arg._M_val = _M_values[__i];
3820 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
3821 __arg = _M_args[__i];
3828 template<
typename _Context,
typename... _Args>
3829 basic_format_args(__format::_Arg_store<_Context, _Args...>)
3830 -> basic_format_args<_Context>;
3832 template<
typename _Context,
typename... _Args>
3834 make_format_args(_Args&... __fmt_args)
noexcept;
3837 template<
typename _Context,
typename... _Args>
3838 class __format::_Arg_store
3840 friend std::basic_format_args<_Context>;
3842 template<
typename _Ctx,
typename... _Argz>
3844#if _GLIBCXX_INLINE_VERSION
3847 make_format_args(_Argz&...)
noexcept;
3851 static constexpr bool _S_values_only
3852 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
3855 = __conditional_t<_S_values_only,
3856 __format::_Arg_value<_Context>,
3857 basic_format_arg<_Context>>;
3859 _Element_t _M_args[
sizeof...(_Args)];
3861 template<
typename _Tp>
3863 _S_make_elt(_Tp& __v)
3865 using _Tq = remove_const_t<_Tp>;
3866 using _CharT =
typename _Context::char_type;
3867 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
3868 "std::formatter must be specialized for the type "
3869 "of each format arg");
3870 using __format::__formattable_with;
3871 if constexpr (is_const_v<_Tp>)
3872 if constexpr (!__formattable_with<_Tp, _Context>)
3873 if constexpr (__formattable_with<_Tq, _Context>)
3874 static_assert(__formattable_with<_Tp, _Context>,
3875 "format arg must be non-const because its "
3876 "std::formatter specialization has a "
3877 "non-const reference parameter");
3878 basic_format_arg<_Context> __arg(__v);
3879 if constexpr (_S_values_only)
3880 return __arg._M_val;
3885 template<
typename... _Tp>
3886 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
3887 [[__gnu__::__always_inline__]]
3888 _Arg_store(_Tp&... __a) noexcept
3889 : _M_args{_S_make_elt(__a)...}
3893 template<
typename _Context>
3894 class __format::_Arg_store<_Context>
3897 template<
typename _Context>
3898 template<
typename... _Args>
3900 basic_format_args<_Context>::
3901 basic_format_args(
const _Store<_Args...>& __store)
noexcept
3903 if constexpr (
sizeof...(_Args) == 0)
3906 _M_unpacked_size = 0;
3909 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
3912 _M_packed_size =
sizeof...(_Args);
3915 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
3917 _M_values = __store._M_args;
3924 _M_unpacked_size =
sizeof...(_Args);
3926 _M_args = __store._M_args;
3931 template<
typename _Context = format_context,
typename... _Args>
3932 [[nodiscard,__gnu__::__always_inline__]]
3934 make_format_args(_Args&... __fmt_args)
noexcept
3936 using _Fmt_arg = basic_format_arg<_Context>;
3937 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
3938 _Normalize<_Args>...>;
3939 return _Store(__fmt_args...);
3942#ifdef _GLIBCXX_USE_WCHAR_T
3944 template<
typename... _Args>
3945 [[nodiscard,__gnu__::__always_inline__]]
3947 make_wformat_args(_Args&... __args)
noexcept
3948 {
return std::make_format_args<wformat_context>(__args...); }
3954 template<
typename _Out,
typename _CharT,
typename _Context>
3956 __do_vformat_to(_Out, basic_string_view<_CharT>,
3957 const basic_format_args<_Context>&,
3958 const locale* =
nullptr);
3960 template<
typename _CharT>
struct __formatter_chrono;
3978 template<
typename _Out,
typename _CharT>
3979 class basic_format_context
3981 static_assert( output_iterator<_Out, const _CharT&> );
3983 basic_format_args<basic_format_context> _M_args;
3985 __format::_Optional_locale _M_loc;
3987 basic_format_context(basic_format_args<basic_format_context> __args,
3989 : _M_args(__args), _M_out(
std::
move(__out))
3992 basic_format_context(basic_format_args<basic_format_context> __args,
3994 : _M_args(__args), _M_out(
std::
move(__out)), _M_loc(__loc)
4000 basic_format_context(
const basic_format_context&) =
delete;
4001 basic_format_context& operator=(
const basic_format_context&) =
delete;
4003 template<
typename _Out2,
typename _CharT2,
typename _Context2>
4005 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
4006 const basic_format_args<_Context2>&,
4009 friend __format::__formatter_chrono<_CharT>;
4012 ~basic_format_context() =
default;
4014 using iterator = _Out;
4015 using char_type = _CharT;
4016 template<
typename _Tp>
4017 using formatter_type = formatter<_Tp, _CharT>;
4020 basic_format_arg<basic_format_context>
4021 arg(
size_t __id)
const noexcept
4022 {
return _M_args.get(__id); }
4028 iterator out() {
return std::move(_M_out); }
4030 void advance_to(iterator __it) { _M_out =
std::move(__it); }
4042 template<
typename _CharT>
4045 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
4047 struct _Parse_context : basic_format_parse_context<_CharT>
4049 using basic_format_parse_context<_CharT>::basic_format_parse_context;
4050 const _Arg_t* _M_types =
nullptr;
4054 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
4055 : _M_pc(__str, __nargs)
4058 constexpr iterator
begin() const noexcept {
return _M_pc.begin(); }
4059 constexpr iterator
end() const noexcept {
return _M_pc.end(); }
4064 basic_string_view<_CharT> __fmt = _M_fmt_str();
4066 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
4068 _M_pc.advance_to(
begin() + 1);
4069 _M_format_arg(_M_pc.next_arg_id());
4073 size_t __lbr = __fmt.find(
'{');
4074 size_t __rbr = __fmt.find(
'}');
4076 while (__fmt.size())
4078 auto __cmp = __lbr <=> __rbr;
4082 _M_pc.advance_to(
end());
4087 if (__lbr + 1 == __fmt.size()
4088 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
4089 __format::__unmatched_left_brace_in_format_string();
4090 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
4091 iterator __last =
begin() + __lbr + int(__is_escape);
4092 _M_on_chars(__last);
4093 _M_pc.advance_to(__last + 1);
4094 __fmt = _M_fmt_str();
4097 if (__rbr != __fmt.npos)
4099 __lbr = __fmt.find(
'{');
4103 _M_on_replacement_field();
4104 __fmt = _M_fmt_str();
4105 __lbr = __fmt.find(
'{');
4106 __rbr = __fmt.find(
'}');
4111 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
4112 __format::__unmatched_right_brace_in_format_string();
4113 iterator __last =
begin() + __rbr;
4114 _M_on_chars(__last);
4115 _M_pc.advance_to(__last + 1);
4116 __fmt = _M_fmt_str();
4117 if (__lbr != __fmt.npos)
4119 __rbr = __fmt.find(
'}');
4124 constexpr basic_string_view<_CharT>
4125 _M_fmt_str() const noexcept
4128 constexpr virtual void _M_on_chars(iterator) { }
4130 constexpr void _M_on_replacement_field()
4132 auto __next =
begin();
4136 __id = _M_pc.next_arg_id();
4137 else if (*__next ==
':')
4139 __id = _M_pc.next_arg_id();
4140 _M_pc.advance_to(++__next);
4144 auto [__i, __ptr] = __format::__parse_arg_id(
begin(),
end());
4145 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
4146 __format::__invalid_arg_id_in_format_string();
4147 _M_pc.check_arg_id(__id = __i);
4150 _M_pc.advance_to(++__ptr);
4153 _M_pc.advance_to(__ptr);
4155 _M_format_arg(__id);
4157 __format::__unmatched_left_brace_in_format_string();
4158 _M_pc.advance_to(
begin() + 1);
4161 constexpr virtual void _M_format_arg(
size_t __id) = 0;
4165 template<
typename _Out,
typename _CharT>
4166 class _Formatting_scanner :
public _Scanner<_CharT>
4169 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
4170 basic_string_view<_CharT> __str)
4171 : _Scanner<_CharT>(__str), _M_fc(__fc)
4175 basic_format_context<_Out, _CharT>& _M_fc;
4177 using iterator =
typename _Scanner<_CharT>::iterator;
4180 _M_on_chars(iterator __last)
override
4182 basic_string_view<_CharT> __str(this->
begin(), __last);
4183 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
4187 _M_format_arg(
size_t __id)
override
4189 using _Context = basic_format_context<_Out, _CharT>;
4190 using handle =
typename basic_format_arg<_Context>::handle;
4192 __format::__visit_format_arg([
this](
auto& __arg) {
4194 using _Formatter =
typename _Context::template formatter_type<_Type>;
4195 if constexpr (is_same_v<_Type, monostate>)
4196 __format::__invalid_arg_id_in_format_string();
4197 else if constexpr (is_same_v<_Type, handle>)
4198 __arg.format(this->_M_pc, this->_M_fc);
4199 else if constexpr (is_default_constructible_v<_Formatter>)
4202 this->_M_pc.advance_to(__f.parse(this->_M_pc));
4203 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
4206 static_assert(__format::__formattable_with<_Type, _Context>);
4207 }, _M_fc.arg(__id));
4211 template<
typename _CharT,
typename _Tp>
4213 __to_arg_t_enum() noexcept
4215 using _Context = __format::__format_context<_CharT>;
4216 using _Fmt_arg = basic_format_arg<_Context>;
4217 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
4218 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
4222 template<
typename _CharT,
typename... _Args>
4223 class _Checking_scanner :
public _Scanner<_CharT>
4226 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
4227 "std::formatter must be specialized for each type being formatted");
4231 _Checking_scanner(basic_string_view<_CharT> __str)
4232 : _Scanner<_CharT>(__str, sizeof...(_Args))
4234#if __cpp_lib_format >= 202305L
4235 this->_M_pc._M_types = _M_types.data();
4241 _M_format_arg(
size_t __id)
override
4243 if constexpr (
sizeof...(_Args) != 0)
4245 if (__id <
sizeof...(_Args))
4247 _M_parse_format_spec<_Args...>(__id);
4251 __builtin_unreachable();
4254 template<
typename _Tp,
typename... _OtherArgs>
4256 _M_parse_format_spec(
size_t __id)
4260 formatter<_Tp, _CharT> __f;
4261 this->_M_pc.advance_to(__f.parse(this->_M_pc));
4263 else if constexpr (
sizeof...(_OtherArgs) != 0)
4264 _M_parse_format_spec<_OtherArgs...>(__id - 1);
4266 __builtin_unreachable();
4269#if __cpp_lib_format >= 202305L
4270 array<_Arg_t,
sizeof...(_Args)>
4271 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
4275 template<
typename _Out,
typename _CharT,
typename _Context>
4277 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
4278 const basic_format_args<_Context>& __args,
4279 const locale* __loc)
4281 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
4282 _Sink_iter<_CharT> __sink_out;
4284 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4287 __sink_out = __sink.out();
4289 if constexpr (is_same_v<_CharT, char>)
4291 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
4293 bool __done =
false;
4294 __format::__visit_format_arg([&](
auto& __arg) {
4295 using _Tp = remove_cvref_t<
decltype(__arg)>;
4296 if constexpr (is_same_v<_Tp, bool>)
4298 size_t __len = 4 + !__arg;
4299 const char* __chars[] = {
"false",
"true" };
4300 if (
auto __res = __sink_out._M_reserve(__len))
4302 __builtin_memcpy(__res.get(), __chars[__arg], __len);
4303 __res._M_bump(__len);
4307 else if constexpr (is_same_v<_Tp, char>)
4309 if (
auto __res = __sink_out._M_reserve(1))
4311 *__res.get() = __arg;
4316 else if constexpr (is_integral_v<_Tp>)
4318 make_unsigned_t<_Tp> __uval;
4319 const bool __neg = __arg < 0;
4321 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
4324 const auto __n = __detail::__to_chars_len(__uval);
4325 if (
auto __res = __sink_out._M_reserve(__n + __neg))
4327 auto __ptr = __res.get();
4329 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
4331 __res._M_bump(__n + __neg);
4335 else if constexpr (is_convertible_v<_Tp, string_view>)
4337 string_view __sv = __arg;
4338 if (
auto __res = __sink_out._M_reserve(__sv.size()))
4340 __builtin_memcpy(__res.get(), __sv.data(), __sv.size());
4341 __res._M_bump(__sv.size());
4349 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4352 return std::move(__sink)._M_finish().out;
4356 auto __ctx = __loc ==
nullptr
4357 ? _Context(__args, __sink_out)
4358 : _Context(__args, __sink_out, *__loc);
4359 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
4360 __scanner._M_scan();
4362 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4365 return std::move(__sink)._M_finish().out;
4367#pragma GCC diagnostic pop
4372#if __cpp_lib_format >= 202305L
4375 template<
typename _CharT>
4376 template<
typename... _Ts>
4378 basic_format_parse_context<_CharT>::
4379 __check_dynamic_spec(
size_t __id)
noexcept
4381 if (__id >= _M_num_args)
4382 __format::__invalid_arg_id_in_format_string();
4383 if constexpr (
sizeof...(_Ts) != 0)
4385 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
4386 auto __arg =
static_cast<_Parse_ctx*
>(
this)->_M_types[__id];
4387 __format::_Arg_t __types[] = {
4388 __format::__to_arg_t_enum<_CharT, _Ts>()...
4390 for (
auto __t : __types)
4394 __invalid_dynamic_spec(
"arg(id) type does not match");
4399 template<
typename _CharT,
typename... _Args>
4400 template<
typename _Tp>
4401 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
4403 basic_format_string<_CharT, _Args...>::
4404 basic_format_string(
const _Tp& __s)
4407 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
4409 __scanner._M_scan();
4414 template<
typename _Out>
requires output_iterator<_Out, const char&>
4415 [[__gnu__::__always_inline__]]
4417 vformat_to(_Out __out, string_view __fmt, format_args __args)
4418 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
4420#ifdef _GLIBCXX_USE_WCHAR_T
4421 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
4422 [[__gnu__::__always_inline__]]
4424 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
4425 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
4428 template<
typename _Out>
requires output_iterator<_Out, const char&>
4429 [[__gnu__::__always_inline__]]
4431 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
4434 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
4437#ifdef _GLIBCXX_USE_WCHAR_T
4438 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
4439 [[__gnu__::__always_inline__]]
4441 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
4442 wformat_args __args)
4444 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
4450 vformat(string_view __fmt, format_args __args)
4452 __format::_Str_sink<char> __buf;
4453 std::vformat_to(__buf.out(), __fmt, __args);
4457#ifdef _GLIBCXX_USE_WCHAR_T
4460 vformat(wstring_view __fmt, wformat_args __args)
4462 __format::_Str_sink<wchar_t> __buf;
4463 std::vformat_to(__buf.out(), __fmt, __args);
4470 vformat(
const locale& __loc, string_view __fmt, format_args __args)
4472 __format::_Str_sink<char> __buf;
4473 std::vformat_to(__buf.out(), __loc, __fmt, __args);
4477#ifdef _GLIBCXX_USE_WCHAR_T
4480 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
4482 __format::_Str_sink<wchar_t> __buf;
4483 std::vformat_to(__buf.out(), __loc, __fmt, __args);
4488 template<
typename... _Args>
4491 format(format_string<_Args...> __fmt, _Args&&... __args)
4492 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
4494#ifdef _GLIBCXX_USE_WCHAR_T
4495 template<
typename... _Args>
4498 format(wformat_string<_Args...> __fmt, _Args&&... __args)
4499 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
4502 template<
typename... _Args>
4505 format(
const locale& __loc, format_string<_Args...> __fmt,
4508 return std::vformat(__loc, __fmt.get(),
4509 std::make_format_args(__args...));
4512#ifdef _GLIBCXX_USE_WCHAR_T
4513 template<
typename... _Args>
4516 format(
const locale& __loc, wformat_string<_Args...> __fmt,
4519 return std::vformat(__loc, __fmt.get(),
4520 std::make_wformat_args(__args...));
4524 template<
typename _Out,
typename... _Args>
4525 requires output_iterator<_Out, const char&>
4527 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
4529 return std::vformat_to(
std::move(__out), __fmt.get(),
4530 std::make_format_args(__args...));
4533#ifdef _GLIBCXX_USE_WCHAR_T
4534 template<
typename _Out,
typename... _Args>
4535 requires output_iterator<_Out, const wchar_t&>
4537 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
4539 return std::vformat_to(
std::move(__out), __fmt.get(),
4540 std::make_wformat_args(__args...));
4544 template<
typename _Out,
typename... _Args>
4545 requires output_iterator<_Out, const char&>
4547 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
4550 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
4551 std::make_format_args(__args...));
4554#ifdef _GLIBCXX_USE_WCHAR_T
4555 template<
typename _Out,
typename... _Args>
4556 requires output_iterator<_Out, const wchar_t&>
4558 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
4561 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
4562 std::make_wformat_args(__args...));
4566 template<
typename _Out,
typename... _Args>
4567 requires output_iterator<_Out, const char&>
4568 inline format_to_n_result<_Out>
4569 format_to_n(_Out __out, iter_difference_t<_Out> __n,
4570 format_string<_Args...> __fmt, _Args&&... __args)
4572 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
4573 std::vformat_to(__sink.out(), __fmt.get(),
4574 std::make_format_args(__args...));
4578#ifdef _GLIBCXX_USE_WCHAR_T
4579 template<
typename _Out,
typename... _Args>
4580 requires output_iterator<_Out, const wchar_t&>
4581 inline format_to_n_result<_Out>
4582 format_to_n(_Out __out, iter_difference_t<_Out> __n,
4583 wformat_string<_Args...> __fmt, _Args&&... __args)
4585 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
4586 std::vformat_to(__sink.out(), __fmt.get(),
4587 std::make_wformat_args(__args...));
4592 template<
typename _Out,
typename... _Args>
4593 requires output_iterator<_Out, const char&>
4594 inline format_to_n_result<_Out>
4595 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
4596 format_string<_Args...> __fmt, _Args&&... __args)
4598 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
4599 std::vformat_to(__sink.out(), __loc, __fmt.get(),
4600 std::make_format_args(__args...));
4604#ifdef _GLIBCXX_USE_WCHAR_T
4605 template<
typename _Out,
typename... _Args>
4606 requires output_iterator<_Out, const wchar_t&>
4607 inline format_to_n_result<_Out>
4608 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
4609 wformat_string<_Args...> __fmt, _Args&&... __args)
4611 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
4612 std::vformat_to(__sink.out(), __loc, __fmt.get(),
4613 std::make_wformat_args(__args...));
4622 template<
typename _CharT>
4623 class _Counting_sink final :
public _Iter_sink<_CharT, _CharT*>
4626 _Counting_sink() : _Iter_sink<_CharT, _CharT*>(nullptr, 0) { }
4628 [[__gnu__::__always_inline__]]
4631 {
return this->_M_count + this->_M_used().size(); }
4634 template<
typename _CharT>
4635 class _Counting_sink :
public _Buf_sink<_CharT>
4637 size_t _M_count = 0;
4640 _M_overflow()
override
4642 if (!std::is_constant_evaluated())
4643 _M_count += this->_M_used().size();
4648 _Counting_sink() =
default;
4650 [[__gnu__::__always_inline__]]
4654 _Counting_sink::_M_overflow();
4662 template<
typename... _Args>
4665 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
4667 __format::_Counting_sink<char> __buf;
4668 std::vformat_to(__buf.out(), __fmt.get(),
4669 std::make_format_args(__args...));
4670 return __buf.count();
4673#ifdef _GLIBCXX_USE_WCHAR_T
4674 template<
typename... _Args>
4677 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
4679 __format::_Counting_sink<wchar_t> __buf;
4680 std::vformat_to(__buf.out(), __fmt.get(),
4681 std::make_wformat_args(__args...));
4682 return __buf.count();
4686 template<
typename... _Args>
4689 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
4692 __format::_Counting_sink<char> __buf;
4693 std::vformat_to(__buf.out(), __loc, __fmt.get(),
4694 std::make_format_args(__args...));
4695 return __buf.count();
4698#ifdef _GLIBCXX_USE_WCHAR_T
4699 template<
typename... _Args>
4702 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
4705 __format::_Counting_sink<wchar_t> __buf;
4706 std::vformat_to(__buf.out(), __loc, __fmt.get(),
4707 std::make_wformat_args(__args...));
4708 return __buf.count();
4712#if __cpp_lib_format_ranges
4715 enum class range_format {
4725 template<
typename _Rg>
4726 constexpr auto format_kind = not defined(format_kind<_Rg>);
4728 template<
typename _Tp>
4729 consteval range_format
4732 using _Ref = ranges::range_reference_t<_Tp>;
4733 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
4734 return range_format::disabled;
4735 else if constexpr (
requires {
typename _Tp::key_type; })
4737 if constexpr (
requires {
typename _Tp::mapped_type; })
4739 using _Up = remove_cvref_t<_Ref>;
4740 if constexpr (__is_pair<_Up>)
4741 return range_format::map;
4742 else if constexpr (__is_specialization_of<_Up, tuple>)
4743 if constexpr (tuple_size_v<_Up> == 2)
4744 return range_format::map;
4746 return range_format::set;
4749 return range_format::sequence;
4754 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
4755 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
4758 template<
typename _Tp,
typename _CharT =
char>
4759 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
4760 class range_formatter;
4766 template<range_format _Kind, ranges::input_range _Rg,
typename _CharT>
4767 struct __range_default_formatter;
4773 template<ranges::input_range _Rg,
typename _CharT>
4774 requires (format_kind<_Rg> != range_format::disabled)
4775 && formattable<ranges::range_reference_t<_Rg>, _CharT>
4776 struct formatter<_Rg, _CharT>
4777 : __format::__range_default_formatter<format_kind<_Rg>, _Rg, _CharT>
4781_GLIBCXX_END_NAMESPACE_VERSION
4784#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.