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
65#if !__has_builtin(__builtin_toupper)
69#pragma GCC diagnostic push
70#pragma GCC diagnostic ignored "-Wpedantic"
71#pragma GCC diagnostic ignored "-Wc++23-extensions"
73namespace std _GLIBCXX_VISIBILITY(default)
75_GLIBCXX_BEGIN_NAMESPACE_VERSION
78 template<
typename _CharT,
typename... _Args>
struct basic_format_string;
84 template<
typename _CharT>
class _Sink;
86 template<
typename _CharT>
89 template<
typename _CharT>
90 using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
92 template<
typename _CharT>
93 struct _Runtime_format_string
95 [[__gnu__::__always_inline__]]
96 _Runtime_format_string(basic_string_view<_CharT> __s) noexcept
99 _Runtime_format_string(
const _Runtime_format_string&) =
delete;
100 void operator=(
const _Runtime_format_string&) =
delete;
103 basic_string_view<_CharT> _M_str;
105 template<
typename,
typename...>
friend struct std::basic_format_string;
110 using format_context = __format::__format_context<char>;
111#ifdef _GLIBCXX_USE_WCHAR_T
112 using wformat_context = __format::__format_context<wchar_t>;
116 template<
typename _Context>
class basic_format_args;
117 using format_args = basic_format_args<format_context>;
118#ifdef _GLIBCXX_USE_WCHAR_T
119 using wformat_args = basic_format_args<wformat_context>;
124 template<
typename _Context>
125 class basic_format_arg;
131 template<
typename _CharT,
typename... _Args>
132 struct basic_format_string
134 template<
typename _Tp>
135 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
137 basic_format_string(
const _Tp& __s);
139 [[__gnu__::__always_inline__]]
140 basic_format_string(__format::_Runtime_format_string<_CharT> __s) noexcept
144 [[__gnu__::__always_inline__]]
145 constexpr basic_string_view<_CharT>
150 basic_string_view<_CharT> _M_str;
153 template<
typename... _Args>
154 using format_string = basic_format_string<char, type_identity_t<_Args>...>;
156#ifdef _GLIBCXX_USE_WCHAR_T
157 template<
typename... _Args>
159 = basic_format_string<wchar_t, type_identity_t<_Args>...>;
162#if __cpp_lib_format >= 202311L
163 [[__gnu__::__always_inline__]]
164 inline __format::_Runtime_format_string<char>
165 runtime_format(string_view __fmt)
noexcept
168#ifdef _GLIBCXX_USE_WCHAR_T
169 [[__gnu__::__always_inline__]]
170 inline __format::_Runtime_format_string<wchar_t>
171 runtime_format(wstring_view __fmt)
noexcept
179 template<
typename _Tp,
typename _CharT>
182 formatter() =
delete;
183 formatter(
const formatter&) =
delete;
184 formatter& operator=(
const formatter&) =
delete;
188 class format_error :
public runtime_error
191 explicit format_error(
const string& __what) : runtime_error(__what) { }
192 explicit format_error(
const char* __what) : runtime_error(__what) { }
198 __throw_format_error(
const char* __what)
199 { _GLIBCXX_THROW_OR_ABORT(format_error(__what)); }
207 __unmatched_left_brace_in_format_string()
208 { __throw_format_error(
"format error: unmatched '{' in format string"); }
212 __unmatched_right_brace_in_format_string()
213 { __throw_format_error(
"format error: unmatched '}' in format string"); }
217 __conflicting_indexing_in_format_string()
218 { __throw_format_error(
"format error: conflicting indexing style in format string"); }
222 __invalid_arg_id_in_format_string()
223 { __throw_format_error(
"format error: invalid arg-id in format string"); }
227 __failed_to_parse_format_spec()
228 { __throw_format_error(
"format error: failed to parse format-spec"); }
230 template<
typename _CharT>
class _Scanner;
236 template<
typename _CharT>
class basic_format_parse_context;
237 using format_parse_context = basic_format_parse_context<char>;
238#ifdef _GLIBCXX_USE_WCHAR_T
239 using wformat_parse_context = basic_format_parse_context<wchar_t>;
242 template<
typename _CharT>
243 class basic_format_parse_context
246 using char_type = _CharT;
247 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
248 using iterator = const_iterator;
251 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
252 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
255 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
256 void operator=(
const basic_format_parse_context&) =
delete;
258 constexpr const_iterator
begin() const noexcept {
return _M_begin; }
259 constexpr const_iterator
end() const noexcept {
return _M_end; }
262 advance_to(const_iterator __it)
noexcept
268 if (_M_indexing == _Manual)
269 __format::__conflicting_indexing_in_format_string();
274 if (std::is_constant_evaluated())
275 if (_M_next_arg_id == _M_num_args)
276 __format::__invalid_arg_id_in_format_string();
277 return _M_next_arg_id++;
281 check_arg_id(
size_t __id)
283 if (_M_indexing == _Auto)
284 __format::__conflicting_indexing_in_format_string();
285 _M_indexing = _Manual;
287 if (std::is_constant_evaluated())
288 if (__id >= _M_num_args)
289 __format::__invalid_arg_id_in_format_string();
292#if __cpp_lib_format >= 202305L
293 template<
typename... _Ts>
295 check_dynamic_spec(
size_t __id)
noexcept
297 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
298 "template arguments for check_dynamic_spec<Ts...>(id) "
299 "must be unique and must be one of the allowed types");
301 __check_dynamic_spec<_Ts...>(__id);
306 check_dynamic_spec_integral(
size_t __id)
noexcept
309 __check_dynamic_spec<int, unsigned,
long long,
310 unsigned long long>(__id);
315 check_dynamic_spec_string(
size_t __id)
noexcept
318 __check_dynamic_spec<const _CharT*, basic_string_view<_CharT>>(__id);
324 template<
typename _Tp,
typename... _Ts>
325 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
327 template<
typename... _Ts>
329 __valid_types_for_check_dynamic_spec()
333 if constexpr (
sizeof...(_Ts) == 0)
342 = __once<bool, _Ts...>
343 + __once<char_type, _Ts...>
344 + __once<int, _Ts...>
345 + __once<
unsigned int, _Ts...>
346 + __once<
long long int, _Ts...>
347 + __once<
unsigned long long int, _Ts...>
348 + __once<float, _Ts...>
349 + __once<double, _Ts...>
350 + __once<
long double, _Ts...>
351 + __once<
const char_type*, _Ts...>
352 + __once<basic_string_view<char_type>, _Ts...>
353 + __once<
const void*, _Ts...>;
354 return __sum ==
sizeof...(_Ts);
358 template<
typename... _Ts>
360 __check_dynamic_spec(
size_t __id)
noexcept;
363 static void __invalid_dynamic_spec(
const char*);
365 friend __format::_Scanner<_CharT>;
370 basic_format_parse_context(basic_string_view<_CharT> __fmt,
371 size_t __num_args) noexcept
372 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
378 enum _Indexing { _Unknown, _Manual, _Auto };
379 _Indexing _M_indexing = _Unknown;
380 size_t _M_next_arg_id = 0;
381 size_t _M_num_args = 0;
385 template<
typename _Tp,
template<
typename...>
class _Class>
386 constexpr bool __is_specialization_of =
false;
387 template<
template<
typename...>
class _Class,
typename... _Args>
388 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
393 template<
typename _CharT>
394 constexpr pair<unsigned short, const _CharT*>
395 __parse_integer(
const _CharT* __first,
const _CharT* __last)
397 if (__first == __last)
398 __builtin_unreachable();
400 if constexpr (is_same_v<_CharT, char>)
402 const auto __start = __first;
403 unsigned short __val = 0;
405 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
406 && __first != __start) [[likely]]
407 return {__val, __first};
411 constexpr int __n = 32;
413 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
414 __buf[__i] = __first[__i];
415 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
416 if (__ptr) [[likely]]
417 return {__v, __first + (__ptr - __buf)};
422 template<
typename _CharT>
423 constexpr pair<unsigned short, const _CharT*>
424 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
426 if (__first == __last)
427 __builtin_unreachable();
430 return {0, __first + 1};
432 if (
'1' <= *__first && *__first <=
'9')
434 const unsigned short __id = *__first -
'0';
435 const auto __next = __first + 1;
437 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
438 return {__id, __next};
440 return __format::__parse_integer(__first, __last);
448 _Pres_d = 1, _Pres_b, _Pres_B, _Pres_o, _Pres_x, _Pres_X, _Pres_c,
450 _Pres_a = 1, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F, _Pres_g, _Pres_G,
451 _Pres_p = 0, _Pres_P,
476 template<
typename _Context>
478 __int_from_arg(
const basic_format_arg<_Context>& __arg);
480 constexpr bool __is_digit(
char __c)
481 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
483 constexpr bool __is_xdigit(
char __c)
484 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
486 template<
typename _CharT>
492 unsigned _M_localized : 1;
493 unsigned _M_zero_fill : 1;
494 _WidthPrec _M_width_kind : 2;
495 _WidthPrec _M_prec_kind : 2;
496 _Pres_type _M_type : 4;
497 unsigned _M_reserved : 1;
498 unsigned _M_reserved2 : 16;
499 unsigned short _M_width;
500 unsigned short _M_prec;
501 char32_t _M_fill =
' ';
503 using iterator =
typename basic_string_view<_CharT>::iterator;
505 static constexpr _Align
506 _S_align(_CharT __c)
noexcept
510 case '<':
return _Align_left;
511 case '>':
return _Align_right;
512 case '^':
return _Align_centre;
513 default:
return _Align_default;
519 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
523 using namespace __unicode;
524 if constexpr (__literal_encoding_is_unicode<_CharT>())
527 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
530 auto __beg = __uv.begin();
531 char32_t __c = *__beg++;
532 if (__is_scalar_value(__c))
533 if (
auto __next = __beg.base(); __next != __last)
534 if (_Align __align = _S_align(*__next))
542 else if (__last - __first >= 2)
543 if (_Align __align = _S_align(__first[1]))
550 if (_Align __align = _S_align(__first[0]))
560 static constexpr _Sign
561 _S_sign(_CharT __c)
noexcept
565 case '+':
return _Sign_plus;
566 case '-':
return _Sign_minus;
567 case ' ':
return _Sign_space;
568 default:
return _Sign_default;
574 _M_parse_sign(iterator __first, iterator)
noexcept
576 if (_Sign __sign = _S_sign(*__first))
586 _M_parse_alternate_form(iterator __first, iterator)
noexcept
598 _M_parse_zero_fill(iterator __first, iterator )
noexcept
609 static constexpr iterator
610 _S_parse_width_or_precision(iterator __first, iterator __last,
611 unsigned short& __val,
bool& __arg_id,
612 basic_format_parse_context<_CharT>& __pc)
614 if (__format::__is_digit(*__first))
616 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
618 __throw_format_error(
"format error: invalid width or precision "
623 else if (*__first ==
'{')
627 if (__first == __last)
628 __format::__unmatched_left_brace_in_format_string();
630 __val = __pc.next_arg_id();
633 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
634 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
635 __format::__invalid_arg_id_in_format_string();
637 __pc.check_arg_id(__v);
640#if __cpp_lib_format >= 202305L
641 __pc.check_dynamic_spec_integral(__val);
650 _M_parse_width(iterator __first, iterator __last,
651 basic_format_parse_context<_CharT>& __pc)
653 bool __arg_id =
false;
655 __throw_format_error(
"format error: width must be non-zero in "
657 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
659 if (__next != __first)
660 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
666 _M_parse_precision(iterator __first, iterator __last,
667 basic_format_parse_context<_CharT>& __pc)
669 if (__first[0] !=
'.')
672 iterator __next = ++__first;
673 bool __arg_id =
false;
674 if (__next != __last)
675 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
677 if (__next == __first)
678 __throw_format_error(
"format error: missing precision after '.' in "
680 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
686 _M_parse_locale(iterator __first, iterator )
noexcept
696 template<
typename _Context>
698 _M_get_width(_Context& __ctx)
const
701 if (_M_width_kind == _WP_value)
703 else if (_M_width_kind == _WP_from_arg)
704 __width = __format::__int_from_arg(__ctx.arg(_M_width));
708 template<
typename _Context>
710 _M_get_precision(_Context& __ctx)
const
713 if (_M_prec_kind == _WP_value)
715 else if (_M_prec_kind == _WP_from_arg)
716 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
721 template<
typename _Int>
723 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
727 else if (__sign == _Sign_plus)
729 else if (__sign == _Sign_space)
737 template<
typename _Out,
typename _CharT>
738 requires output_iterator<_Out, const _CharT&>
740 __write(_Out __out, basic_string_view<_CharT> __str)
742 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
748 for (_CharT __c : __str)
755 template<
typename _Out,
typename _CharT>
757 __write_padded(_Out __out, basic_string_view<_CharT> __str,
758 _Align __align,
size_t __nfill,
char32_t __fill_char)
760 const size_t __buflen = 0x20;
761 _CharT __padding_chars[__buflen];
762 __padding_chars[0] = _CharT();
763 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
765 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
768 while (__n > __padding.size())
770 __o = __format::__write(
std::move(__o), __padding);
771 __n -= __padding.size();
774 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
777 size_t __l, __r, __max;
778 if (__align == _Align_centre)
781 __r = __l + (__nfill & 1);
784 else if (__align == _Align_right)
797 using namespace __unicode;
798 if constexpr (__literal_encoding_is_unicode<_CharT>())
799 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
802 const char32_t __arr[1]{ __fill_char };
803 _Utf_view<_CharT,
const char32_t(&)[1]> __v(__arr);
804 basic_string<_CharT> __padstr(__v.begin(), __v.end());
805 __padding = __padstr;
807 __out = __format::__write(
std::move(__out), __padding);
808 __out = __format::__write(
std::move(__out), __str);
810 __out = __format::__write(
std::move(__out), __padding);
814 if (__max < __buflen)
815 __padding.remove_suffix(__buflen - __max);
819 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
821 __out = __format::__write(
std::move(__out), __str);
829 template<
typename _CharT,
typename _Out>
831 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
832 size_t __estimated_width,
833 basic_format_context<_Out, _CharT>& __fc,
834 const _Spec<_CharT>& __spec,
835 _Align __align = _Align_left)
837 size_t __width = __spec._M_get_width(__fc);
839 if (__width <= __estimated_width)
840 return __format::__write(__fc.out(), __str);
842 const size_t __nfill = __width - __estimated_width;
845 __align = __spec._M_align;
847 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
852 struct _Optional_locale
854 [[__gnu__::__always_inline__]]
855 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
857 _Optional_locale(
const locale& __loc) noexcept
858 : _M_loc(__loc), _M_hasval(
true)
861 _Optional_locale(
const _Optional_locale& __l) noexcept
862 : _M_dummy(), _M_hasval(__l._M_hasval)
865 std::construct_at(&_M_loc, __l._M_loc);
869 operator=(
const _Optional_locale& __l)
noexcept
881 else if (__l._M_hasval)
883 std::construct_at(&_M_loc, __l._M_loc);
889 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
892 operator=(locale&& __loc)
noexcept
898 std::construct_at(&_M_loc,
std::move(__loc));
909 std::construct_at(&_M_loc);
915 bool has_value() const noexcept {
return _M_hasval; }
918 char _M_dummy =
'\0';
921 bool _M_hasval =
false;
924 template<__
char _CharT>
925 struct __formatter_str
927 constexpr typename basic_format_parse_context<_CharT>::iterator
928 parse(basic_format_parse_context<_CharT>& __pc)
930 auto __first = __pc.begin();
931 const auto __last = __pc.end();
932 _Spec<_CharT> __spec{};
934 auto __finalize = [
this, &__spec] {
938 auto __finished = [&] {
939 if (__first == __last || *__first ==
'}')
950 __first = __spec._M_parse_fill_and_align(__first, __last);
954 __first = __spec._M_parse_width(__first, __last, __pc);
958 __first = __spec._M_parse_precision(__first, __last, __pc);
964#if __cpp_lib_format_ranges
965 else if (*__first ==
'?')
967 __spec._M_type = _Pres_esc;
975 __format::__failed_to_parse_format_spec();
978 template<
typename _Out>
980 format(basic_string_view<_CharT> __s,
981 basic_format_context<_Out, _CharT>& __fc)
const
983 if (_M_spec._M_type == _Pres_esc)
988 if (_M_spec._M_width_kind == _WP_none
989 && _M_spec._M_prec_kind == _WP_none)
990 return __format::__write(__fc.out(), __s);
992 size_t __estimated_width;
993 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
995 if (_M_spec._M_prec_kind != _WP_none)
997 size_t __prec = _M_spec._M_get_precision(__fc);
998 __estimated_width = __unicode::__truncate(__s, __prec);
1001 __estimated_width = __unicode::__field_width(__s);
1005 __s = __s.substr(0, _M_spec._M_get_precision(__fc));
1006 __estimated_width = __s.size();
1009 return __format::__write_padded_as_spec(__s, __estimated_width,
1013#if __cpp_lib_format_ranges
1015 set_debug_format() noexcept
1016 { _M_spec._M_type = _Pres_esc; }
1020 _Spec<_CharT> _M_spec{};
1023 template<__
char _CharT>
1024 struct __formatter_int
1028 static constexpr _Pres_type _AsInteger = _Pres_d;
1029 static constexpr _Pres_type _AsBool = _Pres_s;
1030 static constexpr _Pres_type _AsChar = _Pres_c;
1032 constexpr typename basic_format_parse_context<_CharT>::iterator
1033 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1035 _Spec<_CharT> __spec{};
1036 __spec._M_type = __type;
1038 const auto __last = __pc.end();
1039 auto __first = __pc.begin();
1041 auto __finalize = [
this, &__spec] {
1045 auto __finished = [&] {
1046 if (__first == __last || *__first ==
'}')
1057 __first = __spec._M_parse_fill_and_align(__first, __last);
1061 __first = __spec._M_parse_sign(__first, __last);
1065 __first = __spec._M_parse_alternate_form(__first, __last);
1069 __first = __spec._M_parse_zero_fill(__first, __last);
1073 __first = __spec._M_parse_width(__first, __last, __pc);
1077 __first = __spec._M_parse_locale(__first, __last);
1084 __spec._M_type = _Pres_b;
1088 __spec._M_type = _Pres_B;
1094 if (__type != _AsBool)
1096 __spec._M_type = _Pres_c;
1101 __spec._M_type = _Pres_d;
1105 __spec._M_type = _Pres_o;
1109 __spec._M_type = _Pres_x;
1113 __spec._M_type = _Pres_X;
1117 if (__type == _AsBool)
1119 __spec._M_type = _Pres_s;
1123#if __cpp_lib_format_ranges
1125 if (__type == _AsChar)
1127 __spec._M_type = _Pres_esc;
1137 __format::__failed_to_parse_format_spec();
1140 template<
typename _Tp>
1141 constexpr typename basic_format_parse_context<_CharT>::iterator
1142 _M_parse(basic_format_parse_context<_CharT>& __pc)
1144 if constexpr (is_same_v<_Tp, bool>)
1146 auto __end = _M_do_parse(__pc, _AsBool);
1147 if (_M_spec._M_type == _Pres_s)
1148 if (_M_spec._M_sign || _M_spec._M_alt || _M_spec._M_zero_fill)
1149 __throw_format_error(
"format error: format-spec contains "
1150 "invalid formatting options for "
1154 else if constexpr (__char<_Tp>)
1156 auto __end = _M_do_parse(__pc, _AsChar);
1157 if (_M_spec._M_type == _Pres_c || _M_spec._M_type == _Pres_esc)
1158 if (_M_spec._M_sign || _M_spec._M_alt || _M_spec._M_zero_fill
1160 __throw_format_error(
"format error: format-spec contains "
1161 "invalid formatting options for "
1166 return _M_do_parse(__pc, _AsInteger);
1169 template<
typename _Int,
typename _Out>
1170 typename basic_format_context<_Out, _CharT>::iterator
1171 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1173 if (_M_spec._M_type == _Pres_c)
1174 return _M_format_character(_S_to_character(__i), __fc);
1176 char __buf[
sizeof(_Int) * __CHAR_BIT__ + 3];
1177 to_chars_result __res{};
1179 string_view __base_prefix;
1180 make_unsigned_t<_Int> __u;
1182 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1186 char* __start = __buf + 3;
1187 char*
const __end = __buf +
sizeof(__buf);
1188 char*
const __start_digits = __start;
1190 switch (_M_spec._M_type)
1194 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1195 __res = to_chars(__start, __end, __u, 2);
1199 return _M_format_character(_S_to_character(__i), __fc);
1205 __res = to_chars(__start, __end, __u, 10);
1209 __base_prefix =
"0";
1210 __res = to_chars(__start, __end, __u, 8);
1214 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1215 __res = to_chars(__start, __end, __u, 16);
1216 if (_M_spec._M_type == _Pres_X)
1217 for (
auto __p = __start; __p != __res.ptr; ++__p)
1218#
if __has_builtin(__builtin_toupper)
1219 *__p = __builtin_toupper(*__p);
1225 __builtin_unreachable();
1228 if (_M_spec._M_alt && __base_prefix.size())
1230 __start -= __base_prefix.size();
1231 __builtin_memcpy(__start, __base_prefix.data(),
1232 __base_prefix.size());
1234 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1236 return _M_format_int(string_view(__start, __res.ptr - __start),
1237 __start_digits - __start, __fc);
1240 template<
typename _Out>
1241 typename basic_format_context<_Out, _CharT>::iterator
1242 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1244 if (_M_spec._M_type == _Pres_c)
1245 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1246 if (_M_spec._M_type != _Pres_s)
1247 return format(
static_cast<unsigned char>(__i), __fc);
1249 basic_string<_CharT> __s;
1251 if (_M_spec._M_localized) [[unlikely]]
1253 auto& __np = std::use_facet<numpunct<_CharT>>(__fc.locale());
1254 __s = __i ? __np.truename() : __np.falsename();
1255 __est_width = __s.size();
1259 if constexpr (is_same_v<char, _CharT>)
1260 __s = __i ?
"true" :
"false";
1262 __s = __i ? L
"true" : L
"false";
1263 __est_width = __s.size();
1266 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1270 [[__gnu__::__always_inline__]]
1272 _S_character_width(_CharT __c)
1275 if constexpr (
sizeof(_CharT) > 1u &&
1276 __unicode::__literal_encoding_is_unicode<_CharT>())
1277 return __unicode::__field_width(__c);
1282 template<
typename _Out>
1283 typename basic_format_context<_Out, _CharT>::iterator
1284 _M_format_character(_CharT __c,
1285 basic_format_context<_Out, _CharT>& __fc)
const
1287 return __format::__write_padded_as_spec({&__c, 1u},
1288 _S_character_width(__c),
1292 template<
typename _Int>
1294 _S_to_character(_Int __i)
1297 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1299 if (_Traits::__min <= __i && __i <= _Traits::__max)
1300 return static_cast<_CharT
>(__i);
1302 else if constexpr (is_signed_v<_Int>)
1304 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1305 return static_cast<_CharT
>(__i);
1307 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1308 return static_cast<_CharT
>(__i);
1309 __throw_format_error(
"format error: integer not representable as "
1313 template<
typename _Out>
1314 typename basic_format_context<_Out, _CharT>::iterator
1315 _M_format_int(string_view __narrow_str,
size_t __prefix_len,
1316 basic_format_context<_Out, _CharT>& __fc)
const
1318 size_t __width = _M_spec._M_get_width(__fc);
1320 basic_string_view<_CharT> __str;
1321 if constexpr (is_same_v<char, _CharT>)
1322 __str = __narrow_str;
1323#ifdef _GLIBCXX_USE_WCHAR_T
1326 size_t __n = __narrow_str.size();
1327 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
1328 std::__to_wstring_numeric(__narrow_str.data(), __n, __p);
1333 if (_M_spec._M_localized)
1335 const auto& __l = __fc.locale();
1336 if (__l.name() !=
"C")
1338 auto& __np = use_facet<numpunct<_CharT>>(__l);
1339 string __grp = __np.grouping();
1342 size_t __n = __str.size() - __prefix_len;
1343 auto __p = (_CharT*)__builtin_alloca(2 * __n
1346 auto __s = __str.data();
1347 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1348 __s += __prefix_len;
1349 auto __end = std::__add_grouping(__p + __prefix_len,
1350 __np.thousands_sep(),
1354 __str = {__p, size_t(__end - __p)};
1359 if (__width <= __str.size())
1360 return __format::__write(__fc.out(), __str);
1362 char32_t __fill_char = _M_spec._M_fill;
1363 _Align __align = _M_spec._M_align;
1365 size_t __nfill = __width - __str.size();
1366 auto __out = __fc.out();
1367 if (__align == _Align_default)
1369 __align = _Align_right;
1370 if (_M_spec._M_zero_fill)
1372 __fill_char = _CharT(
'0');
1374 if (__prefix_len != 0)
1376 __out = __format::__write(
std::move(__out),
1377 __str.substr(0, __prefix_len));
1378 __str.remove_prefix(__prefix_len);
1382 __fill_char = _CharT(
' ');
1384 return __format::__write_padded(
std::move(__out), __str,
1385 __align, __nfill, __fill_char);
1388#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__
1389 template<
typename _Tp>
1391 =
typename __conditional_t<(
sizeof(_Tp) <=
sizeof(
long long)),
1393 type_identity<unsigned __int128>>::type;
1396 template<
typename _Int>
1397 static to_chars_result
1398 to_chars(
char* __first,
char* __last, _Int __value,
int __base)
1399 {
return std::__to_chars_i<_Int>(__first, __last, __value, __base); }
1402 _Spec<_CharT> _M_spec{};
1413#undef _GLIBCXX_FORMAT_F128
1415#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1418 using __float128_t = __ieee128;
1419# define _GLIBCXX_FORMAT_F128 1
1421#ifdef __LONG_DOUBLE_IEEE128__
1425 to_chars(
char*,
char*, __ibm128)
noexcept
1426 __asm(
"_ZSt8to_charsPcS_e");
1429 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1430 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1433 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1434 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1435#elif __cplusplus == 202002L
1437 to_chars(
char*,
char*, __ieee128)
noexcept
1438 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1441 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1442 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1445 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1446 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1449#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1452 using __float128_t =
long double;
1453# define _GLIBCXX_FORMAT_F128 1
1455#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1458 using __float128_t = _Float128;
1459# define _GLIBCXX_FORMAT_F128 2
1461# if __cplusplus == 202002L
1465 to_chars(
char*,
char*, _Float128)
noexcept
1466# if _GLIBCXX_INLINE_VERSION
1467 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1469 __asm(
"_ZSt8to_charsPcS_DF128_");
1473 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1474# if _GLIBCXX_INLINE_VERSION
1475 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1477 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
1481 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
1482# if _GLIBCXX_INLINE_VERSION
1483 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
1485 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
1490 using std::to_chars;
1493 template<
typename _Tp>
1494 concept __formattable_float
1495 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
1496 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
1498 template<__
char _CharT>
1499 struct __formatter_fp
1501 constexpr typename basic_format_parse_context<_CharT>::iterator
1502 parse(basic_format_parse_context<_CharT>& __pc)
1504 _Spec<_CharT> __spec{};
1505 const auto __last = __pc.end();
1506 auto __first = __pc.begin();
1508 auto __finalize = [
this, &__spec] {
1512 auto __finished = [&] {
1513 if (__first == __last || *__first ==
'}')
1524 __first = __spec._M_parse_fill_and_align(__first, __last);
1528 __first = __spec._M_parse_sign(__first, __last);
1532 __first = __spec._M_parse_alternate_form(__first, __last);
1536 __first = __spec._M_parse_zero_fill(__first, __last);
1540 if (__first[0] !=
'.')
1542 __first = __spec._M_parse_width(__first, __last, __pc);
1547 __first = __spec._M_parse_precision(__first, __last, __pc);
1551 __first = __spec._M_parse_locale(__first, __last);
1558 __spec._M_type = _Pres_a;
1562 __spec._M_type = _Pres_A;
1566 __spec._M_type = _Pres_e;
1570 __spec._M_type = _Pres_E;
1574 __spec._M_type = _Pres_f;
1578 __spec._M_type = _Pres_F;
1582 __spec._M_type = _Pres_g;
1586 __spec._M_type = _Pres_G;
1594 __format::__failed_to_parse_format_spec();
1597 template<
typename _Fp,
typename _Out>
1598 typename basic_format_context<_Out, _CharT>::iterator
1599 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
1603 to_chars_result __res{};
1606 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
1608 __prec = _M_spec._M_get_precision(__fc);
1610 char* __start = __buf + 1;
1611 char* __end = __buf +
sizeof(__buf);
1614 bool __upper =
false;
1615 bool __trailing_zeros =
false;
1618 switch (_M_spec._M_type)
1625 if (_M_spec._M_type != _Pres_A)
1627 __fmt = chars_format::hex;
1635 __fmt = chars_format::scientific;
1642 __fmt = chars_format::fixed;
1649 __trailing_zeros =
true;
1651 __fmt = chars_format::general;
1655 __fmt = chars_format::general;
1658 __builtin_unreachable();
1662 auto __to_chars = [&](
char* __b,
char* __e) {
1664 return __format::to_chars(__b, __e, __v, __fmt, __prec);
1665 else if (__fmt != chars_format{})
1666 return __format::to_chars(__b, __e, __v, __fmt);
1668 return __format::to_chars(__b, __e, __v);
1672 __res = __to_chars(__start, __end);
1674 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
1678 size_t __guess = 8 + __prec;
1679 if (__fmt == chars_format::fixed)
1681 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
1682 || is_same_v<_Fp, long double>)
1687 if constexpr (is_same_v<_Fp, float>)
1688 __builtin_frexpf(__v, &__exp);
1689 else if constexpr (is_same_v<_Fp, double>)
1690 __builtin_frexp(__v, &__exp);
1691 else if constexpr (is_same_v<_Fp, long double>)
1692 __builtin_frexpl(__v, &__exp);
1694 __guess += 1U + __exp * 4004U / 13301U;
1697 __guess += numeric_limits<_Fp>::max_exponent10;
1699 if (__guess <=
sizeof(__buf)) [[unlikely]]
1700 __guess =
sizeof(__buf) * 2;
1709 auto __overwrite = [&__to_chars, &__res] (
char* __p,
size_t __n)
1711 __res = __to_chars(__p + 1, __p + __n - 1);
1712 return __res.ec == errc{} ? __res.ptr - __p : 0;
1717 __start = __dynbuf.
data() + 1;
1718 __end = __dynbuf.
data() + __dynbuf.
size();
1720 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
1726 for (
char* __p = __start; __p != __res.ptr; ++__p)
1730 bool __have_sign =
true;
1732 if (!__builtin_signbit(__v))
1734 if (_M_spec._M_sign == _Sign_plus)
1736 else if (_M_spec._M_sign == _Sign_space)
1739 __have_sign =
false;
1742 string_view __narrow_str(__start, __res.ptr - __start);
1746 if (_M_spec._M_alt && __builtin_isfinite(__v))
1748 string_view __s = __narrow_str;
1752 size_t __d = __s.find(
'.');
1753 if (__d != __s.npos)
1755 __p = __s.find(__expc, __d + 1);
1756 if (__p == __s.npos)
1760 if (__trailing_zeros)
1763 if (__s[__have_sign] !=
'0')
1765 __sigfigs = __p - __have_sign - 1;
1770 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
1775 __p = __s.find(__expc);
1776 if (__p == __s.npos)
1779 __sigfigs = __d - __have_sign;
1782 if (__trailing_zeros && __prec != 0)
1787 __z = __prec - __sigfigs;
1790 if (
size_t __extras =
int(__d == __p) + __z)
1792 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
1796 __builtin_memmove(__start + __p + __extras,
1800 __start[__p++] =
'.';
1801 __builtin_memset(__start + __p,
'0', __z);
1802 __narrow_str = {__s.data(), __s.size() + __extras};
1806 __dynbuf.
reserve(__s.size() + __extras);
1807 if (__dynbuf.
empty())
1809 __dynbuf = __s.
substr(0, __p);
1813 __dynbuf.
append(__z,
'0');
1814 __dynbuf.
append(__s.substr(__p));
1818 __dynbuf.
insert(__p, __extras,
'0');
1820 __dynbuf[__p] =
'.';
1822 __narrow_str = __dynbuf;
1827 basic_string<_CharT> __wstr;
1828 basic_string_view<_CharT> __str;
1829 if constexpr (is_same_v<_CharT, char>)
1830 __str = __narrow_str;
1831#ifdef _GLIBCXX_USE_WCHAR_T
1834 __wstr = std::__to_wstring_numeric(__narrow_str);
1839 if (_M_spec._M_localized && __builtin_isfinite(__v))
1841 __wstr = _M_localize(__str, __expc, __fc.locale());
1842 if (!__wstr.empty())
1846 size_t __width = _M_spec._M_get_width(__fc);
1848 if (__width <= __str.size())
1849 return __format::__write(__fc.out(), __str);
1851 char32_t __fill_char = _M_spec._M_fill;
1852 _Align __align = _M_spec._M_align;
1854 size_t __nfill = __width - __str.size();
1855 auto __out = __fc.out();
1856 if (__align == _Align_default)
1858 __align = _Align_right;
1859 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
1861 __fill_char = _CharT(
'0');
1863 if (!__format::__is_xdigit(__narrow_str[0]))
1865 *__out++ = __str[0];
1866 __str.remove_prefix(1);
1870 __fill_char = _CharT(
' ');
1872 return __format::__write_padded(
std::move(__out), __str,
1873 __align, __nfill, __fill_char);
1877 basic_string<_CharT>
1878 _M_localize(basic_string_view<_CharT> __str,
char __expc,
1879 const locale& __loc)
const
1881 basic_string<_CharT> __lstr;
1883 if (__loc == locale::classic())
1886 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
1887 const _CharT __point = __np.decimal_point();
1888 const string __grp = __np.grouping();
1890 _CharT __dot, __exp;
1891 if constexpr (is_same_v<_CharT, char>)
1914 __builtin_unreachable();
1918 if (__grp.empty() && __point == __dot)
1921 size_t __d = __str.find(__dot);
1922 size_t __e = min(__d, __str.find(__exp));
1923 if (__e == __str.npos)
1925 const size_t __r = __str.size() - __e;
1926 auto __overwrite = [&](_CharT* __p, size_t) {
1928 auto __end = std::__add_grouping(__p, __np.thousands_sep(),
1929 __grp.data(), __grp.size(),
1930 __str.data(), __str.data() + __e);
1933 if (__d != __str.npos)
1939 const size_t __rlen = __str.size() - __e;
1941 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
1944 return (__end - __p);
1946 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
1950 _Spec<_CharT> _M_spec{};
1957 template<__format::__
char _CharT>
1958 struct formatter<_CharT, _CharT>
1960 formatter() =
default;
1962 constexpr typename basic_format_parse_context<_CharT>::iterator
1963 parse(basic_format_parse_context<_CharT>& __pc)
1965 return _M_f.template _M_parse<_CharT>(__pc);
1968 template<
typename _Out>
1969 typename basic_format_context<_Out, _CharT>::iterator
1970 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
1972 if (_M_f._M_spec._M_type == __format::_Pres_none
1973 || _M_f._M_spec._M_type == __format::_Pres_c)
1974 return _M_f._M_format_character(__u, __fc);
1975 else if (_M_f._M_spec._M_type == __format::_Pres_esc)
1981 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
1984#if __cpp_lib_format_ranges
1986 set_debug_format() noexcept
1987 { _M_f._M_spec._M_type = __format::_Pres_esc; }
1991 __format::__formatter_int<_CharT> _M_f;
1994#ifdef _GLIBCXX_USE_WCHAR_T
1997 struct formatter<char, wchar_t>
1999 formatter() =
default;
2001 constexpr typename basic_format_parse_context<wchar_t>::iterator
2002 parse(basic_format_parse_context<wchar_t>& __pc)
2004 return _M_f._M_parse<
char>(__pc);
2007 template<
typename _Out>
2008 typename basic_format_context<_Out, wchar_t>::iterator
2009 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2011 if (_M_f._M_spec._M_type == __format::_Pres_none
2012 || _M_f._M_spec._M_type == __format::_Pres_c)
2013 return _M_f._M_format_character(__u, __fc);
2014 else if (_M_f._M_spec._M_type == __format::_Pres_esc)
2020 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2023#if __cpp_lib_format_ranges
2025 set_debug_format() noexcept
2026 { _M_f._M_spec._M_type = __format::_Pres_esc; }
2030 __format::__formatter_int<wchar_t> _M_f;
2037 template<__format::__
char _CharT>
2038 struct formatter<_CharT*, _CharT>
2040 formatter() =
default;
2042 [[__gnu__::__always_inline__]]
2043 constexpr typename basic_format_parse_context<_CharT>::iterator
2044 parse(basic_format_parse_context<_CharT>& __pc)
2045 {
return _M_f.parse(__pc); }
2047 template<
typename _Out>
2048 [[__gnu__::__nonnull__]]
2049 typename basic_format_context<_Out, _CharT>::iterator
2050 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2051 {
return _M_f.format(__u, __fc); }
2053#if __cpp_lib_format_ranges
2054 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2058 __format::__formatter_str<_CharT> _M_f;
2061 template<__format::__
char _CharT>
2062 struct formatter<const _CharT*, _CharT>
2064 formatter() =
default;
2066 [[__gnu__::__always_inline__]]
2067 constexpr typename basic_format_parse_context<_CharT>::iterator
2068 parse(basic_format_parse_context<_CharT>& __pc)
2069 {
return _M_f.parse(__pc); }
2071 template<
typename _Out>
2072 [[__gnu__::__nonnull__]]
2073 typename basic_format_context<_Out, _CharT>::iterator
2074 format(
const _CharT* __u,
2075 basic_format_context<_Out, _CharT>& __fc)
const
2076 {
return _M_f.format(__u, __fc); }
2078#if __cpp_lib_format_ranges
2079 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2083 __format::__formatter_str<_CharT> _M_f;
2086 template<__format::__
char _CharT,
size_t _Nm>
2087 struct formatter<_CharT[_Nm], _CharT>
2089 formatter() =
default;
2091 [[__gnu__::__always_inline__]]
2092 constexpr typename basic_format_parse_context<_CharT>::iterator
2093 parse(basic_format_parse_context<_CharT>& __pc)
2094 {
return _M_f.parse(__pc); }
2096 template<
typename _Out>
2097 typename basic_format_context<_Out, _CharT>::iterator
2098 format(
const _CharT (&__u)[_Nm],
2099 basic_format_context<_Out, _CharT>& __fc)
const
2100 {
return _M_f.format({__u, _Nm}, __fc); }
2102#if __cpp_lib_format_ranges
2103 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2107 __format::__formatter_str<_CharT> _M_f;
2110 template<
typename _Traits,
typename _Alloc>
2111 struct formatter<basic_string<char, _Traits, _Alloc>, char>
2113 formatter() =
default;
2115 [[__gnu__::__always_inline__]]
2116 constexpr typename basic_format_parse_context<char>::iterator
2117 parse(basic_format_parse_context<char>& __pc)
2118 {
return _M_f.parse(__pc); }
2120 template<
typename _Out>
2121 typename basic_format_context<_Out, char>::iterator
2122 format(
const basic_string<char, _Traits, _Alloc>& __u,
2123 basic_format_context<_Out, char>& __fc)
const
2124 {
return _M_f.format(__u, __fc); }
2126#if __cpp_lib_format_ranges
2127 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2131 __format::__formatter_str<char> _M_f;
2134#ifdef _GLIBCXX_USE_WCHAR_T
2135 template<
typename _Traits,
typename _Alloc>
2136 struct formatter<basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2138 formatter() =
default;
2140 [[__gnu__::__always_inline__]]
2141 constexpr typename basic_format_parse_context<wchar_t>::iterator
2142 parse(basic_format_parse_context<wchar_t>& __pc)
2143 {
return _M_f.parse(__pc); }
2145 template<
typename _Out>
2146 typename basic_format_context<_Out, wchar_t>::iterator
2147 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2148 basic_format_context<_Out, wchar_t>& __fc)
const
2149 {
return _M_f.format(__u, __fc); }
2151#if __cpp_lib_format_ranges
2152 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2156 __format::__formatter_str<wchar_t> _M_f;
2160 template<
typename _Traits>
2161 struct formatter<basic_string_view<char, _Traits>, char>
2163 formatter() =
default;
2165 [[__gnu__::__always_inline__]]
2166 constexpr typename basic_format_parse_context<char>::iterator
2167 parse(basic_format_parse_context<char>& __pc)
2168 {
return _M_f.parse(__pc); }
2170 template<
typename _Out>
2171 typename basic_format_context<_Out, char>::iterator
2172 format(basic_string_view<char, _Traits> __u,
2173 basic_format_context<_Out, char>& __fc)
const
2174 {
return _M_f.format(__u, __fc); }
2176#if __cpp_lib_format_ranges
2177 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2181 __format::__formatter_str<char> _M_f;
2184#ifdef _GLIBCXX_USE_WCHAR_T
2185 template<
typename _Traits>
2186 struct formatter<basic_string_view<wchar_t, _Traits>, wchar_t>
2188 formatter() =
default;
2190 [[__gnu__::__always_inline__]]
2191 constexpr typename basic_format_parse_context<wchar_t>::iterator
2192 parse(basic_format_parse_context<wchar_t>& __pc)
2193 {
return _M_f.parse(__pc); }
2195 template<
typename _Out>
2196 typename basic_format_context<_Out, wchar_t>::iterator
2197 format(basic_string_view<wchar_t, _Traits> __u,
2198 basic_format_context<_Out, wchar_t>& __fc)
const
2199 {
return _M_f.format(__u, __fc); }
2201#if __cpp_lib_format_ranges
2202 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2206 __format::__formatter_str<wchar_t> _M_f;
2216 template<
typename _Tp>
2217 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2219#if defined __SIZEOF_INT128__
2220 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2221 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2225 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2226 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2227#ifdef _GLIBCXX_USE_CHAR8_T
2228 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2230 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2231 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2236 template<
typename _Tp, __format::__
char _CharT>
2237 requires __format::__is_formattable_integer<_Tp>
2238 struct formatter<_Tp, _CharT>
2240 formatter() =
default;
2242 [[__gnu__::__always_inline__]]
2243 constexpr typename basic_format_parse_context<_CharT>::iterator
2244 parse(basic_format_parse_context<_CharT>& __pc)
2246 return _M_f.template _M_parse<_Tp>(__pc);
2249 template<
typename _Out>
2250 typename basic_format_context<_Out, _CharT>::iterator
2251 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2252 {
return _M_f.format(__u, __fc); }
2255 __format::__formatter_int<_CharT> _M_f;
2258#if defined __glibcxx_to_chars
2260 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2261 struct formatter<_Tp, _CharT>
2263 formatter() =
default;
2265 [[__gnu__::__always_inline__]]
2266 constexpr typename basic_format_parse_context<_CharT>::iterator
2267 parse(basic_format_parse_context<_CharT>& __pc)
2268 {
return _M_f.parse(__pc); }
2270 template<
typename _Out>
2271 typename basic_format_context<_Out, _CharT>::iterator
2272 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2273 {
return _M_f.format(__u, __fc); }
2276 __format::__formatter_fp<_CharT> _M_f;
2279#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2281 template<__format::__
char _CharT>
2282 struct formatter<long double, _CharT>
2284 formatter() =
default;
2286 [[__gnu__::__always_inline__]]
2287 constexpr typename basic_format_parse_context<_CharT>::iterator
2288 parse(basic_format_parse_context<_CharT>& __pc)
2289 {
return _M_f.parse(__pc); }
2291 template<
typename _Out>
2292 typename basic_format_context<_Out, _CharT>::iterator
2293 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2294 {
return _M_f.format((
double)__u, __fc); }
2297 __format::__formatter_fp<_CharT> _M_f;
2301#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2303 template<__format::__
char _CharT>
2304 struct formatter<_Float16, _CharT>
2306 formatter() =
default;
2308 [[__gnu__::__always_inline__]]
2309 constexpr typename basic_format_parse_context<_CharT>::iterator
2310 parse(basic_format_parse_context<_CharT>& __pc)
2311 {
return _M_f.parse(__pc); }
2313 template<
typename _Out>
2314 typename basic_format_context<_Out, _CharT>::iterator
2315 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
2316 {
return _M_f.format((
float)__u, __fc); }
2319 __format::__formatter_fp<_CharT> _M_f;
2323#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2325 template<__format::__
char _CharT>
2326 struct formatter<_Float32, _CharT>
2328 formatter() =
default;
2330 [[__gnu__::__always_inline__]]
2331 constexpr typename basic_format_parse_context<_CharT>::iterator
2332 parse(basic_format_parse_context<_CharT>& __pc)
2333 {
return _M_f.parse(__pc); }
2335 template<
typename _Out>
2336 typename basic_format_context<_Out, _CharT>::iterator
2337 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
2338 {
return _M_f.format((
float)__u, __fc); }
2341 __format::__formatter_fp<_CharT> _M_f;
2345#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2347 template<__format::__
char _CharT>
2348 struct formatter<_Float64, _CharT>
2350 formatter() =
default;
2352 [[__gnu__::__always_inline__]]
2353 constexpr typename basic_format_parse_context<_CharT>::iterator
2354 parse(basic_format_parse_context<_CharT>& __pc)
2355 {
return _M_f.parse(__pc); }
2357 template<
typename _Out>
2358 typename basic_format_context<_Out, _CharT>::iterator
2359 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
2360 {
return _M_f.format((
double)__u, __fc); }
2363 __format::__formatter_fp<_CharT> _M_f;
2367#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128 == 1
2369 template<__format::__
char _CharT>
2370 struct formatter<_Float128, _CharT>
2372 formatter() =
default;
2374 [[__gnu__::__always_inline__]]
2375 constexpr typename basic_format_parse_context<_CharT>::iterator
2376 parse(basic_format_parse_context<_CharT>& __pc)
2377 {
return _M_f.parse(__pc); }
2379 template<
typename _Out>
2380 typename basic_format_context<_Out, _CharT>::iterator
2381 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
2382 {
return _M_f.format((__format::__float128_t)__u, __fc); }
2385 __format::__formatter_fp<_CharT> _M_f;
2389#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2391 template<__format::__
char _CharT>
2392 struct formatter<
__gnu_cxx::__bfloat16_t, _CharT>
2394 formatter() =
default;
2396 [[__gnu__::__always_inline__]]
2397 constexpr typename basic_format_parse_context<_CharT>::iterator
2398 parse(basic_format_parse_context<_CharT>& __pc)
2399 {
return _M_f.parse(__pc); }
2401 template<
typename _Out>
2402 typename basic_format_context<_Out, _CharT>::iterator
2403 format(__gnu_cxx::__bfloat16_t __u,
2404 basic_format_context<_Out, _CharT>& __fc)
const
2405 {
return _M_f.format((
float)__u, __fc); }
2408 __format::__formatter_fp<_CharT> _M_f;
2416 template<__format::__
char _CharT>
2417 struct formatter<const void*, _CharT>
2419 formatter() =
default;
2421 constexpr typename basic_format_parse_context<_CharT>::iterator
2422 parse(basic_format_parse_context<_CharT>& __pc)
2424 __format::_Spec<_CharT> __spec{};
2425 const auto __last = __pc.end();
2426 auto __first = __pc.begin();
2428 auto __finalize = [
this, &__spec] {
2432 auto __finished = [&] {
2433 if (__first == __last || *__first ==
'}')
2444 __first = __spec._M_parse_fill_and_align(__first, __last);
2450#if __glibcxx_format >= 202304L
2451 __first = __spec._M_parse_zero_fill(__first, __last);
2456 __first = __spec._M_parse_width(__first, __last, __pc);
2458 if (__first != __last)
2460 if (*__first ==
'p')
2462#if __glibcxx_format >= 202304L
2463 else if (*__first ==
'P')
2465 __spec._M_type = __format::_Pres_P;
2474 __format::__failed_to_parse_format_spec();
2477 template<
typename _Out>
2478 typename basic_format_context<_Out, _CharT>::iterator
2479 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2481 auto __u =
reinterpret_cast<__UINTPTR_TYPE__
>(__v);
2482 char __buf[2 +
sizeof(__v) * 2];
2483 auto [__ptr, __ec] = std::to_chars(__buf + 2,
std::end(__buf),
2485 int __n = __ptr - __buf;
2488#if __glibcxx_format >= 202304L
2489 if (_M_spec._M_type == __format::_Pres_P)
2492 for (
auto __p = __buf + 2; __p != __ptr; ++__p)
2493#
if __has_builtin(__builtin_toupper)
2494 *__p = __builtin_toupper(*__p);
2501 basic_string_view<_CharT> __str;
2502 if constexpr (is_same_v<_CharT, char>)
2503 __str = string_view(__buf, __n);
2504#ifdef _GLIBCXX_USE_WCHAR_T
2507 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
2508 std::__to_wstring_numeric(__buf, __n, __p);
2509 __str = wstring_view(__p, __n);
2513#if __glibcxx_format >= 202304L
2514 if (_M_spec._M_zero_fill)
2516 size_t __width = _M_spec._M_get_width(__fc);
2517 if (__width <= __str.size())
2518 return __format::__write(__fc.out(), __str);
2520 auto __out = __fc.out();
2522 __out = __format::__write(
std::move(__out), __str.substr(0, 2));
2523 __str.remove_prefix(2);
2524 size_t __nfill = __width - __n;
2525 return __format::__write_padded(
std::move(__out), __str,
2526 __format::_Align_right,
2527 __nfill, _CharT(
'0'));
2531 return __format::__write_padded_as_spec(__str, __n, __fc, _M_spec,
2532 __format::_Align_right);
2536 __format::_Spec<_CharT> _M_spec{};
2539 template<__format::__
char _CharT>
2540 struct formatter<void*, _CharT>
2542 formatter() =
default;
2544 [[__gnu__::__always_inline__]]
2545 constexpr typename basic_format_parse_context<_CharT>::iterator
2546 parse(basic_format_parse_context<_CharT>& __pc)
2547 {
return _M_f.parse(__pc); }
2549 template<
typename _Out>
2550 typename basic_format_context<_Out, _CharT>::iterator
2551 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2552 {
return _M_f.format(__v, __fc); }
2555 formatter<const void*, _CharT> _M_f;
2558 template<__format::__
char _CharT>
2559 struct formatter<nullptr_t, _CharT>
2561 formatter() =
default;
2563 [[__gnu__::__always_inline__]]
2564 constexpr typename basic_format_parse_context<_CharT>::iterator
2565 parse(basic_format_parse_context<_CharT>& __pc)
2566 {
return _M_f.parse(__pc); }
2568 template<
typename _Out>
2569 typename basic_format_context<_Out, _CharT>::iterator
2570 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
2571 {
return _M_f.format(
nullptr, __fc); }
2574 formatter<const void*, _CharT> _M_f;
2578#if defined _GLIBCXX_USE_WCHAR_T && __cpp_lib_format_ranges
2582 namespace __format {
struct __disabled; }
2586 struct formatter<char*,
wchar_t>
2587 :
private formatter<__format::__disabled, wchar_t> { };
2589 struct formatter<const char*,
wchar_t>
2590 :
private formatter<__format::__disabled, wchar_t> { };
2591 template<
size_t _Nm>
2592 struct formatter<char[_Nm], wchar_t>
2593 :
private formatter<__format::__disabled, wchar_t> { };
2594 template<
class _Traits,
class _Allocator>
2595 struct formatter<basic_string<char, _Traits, _Allocator>, wchar_t>
2596 :
private formatter<__format::__disabled, wchar_t> { };
2597 template<
class _Traits>
2598 struct formatter<basic_string_view<char, _Traits>, wchar_t>
2599 :
private formatter<__format::__disabled, wchar_t> { };
2605 template<
typename _Tp,
typename _Context,
2607 =
typename _Context::template formatter_type<remove_const_t<_Tp>>,
2608 typename _ParseContext
2609 = basic_format_parse_context<typename _Context::char_type>>
2610 concept __parsable_with
2611 = semiregular<_Formatter>
2612 &&
requires (_Formatter __f, _ParseContext __pc)
2614 { __f.parse(__pc) } -> same_as<typename _ParseContext::iterator>;
2617 template<
typename _Tp,
typename _Context,
2619 =
typename _Context::template formatter_type<remove_const_t<_Tp>>,
2620 typename _ParseContext
2621 = basic_format_parse_context<typename _Context::char_type>>
2622 concept __formattable_with
2623 = semiregular<_Formatter>
2624 &&
requires (
const _Formatter __cf, _Tp&& __t, _Context __fc)
2626 { __cf.format(__t, __fc) } -> same_as<typename _Context::iterator>;
2630 template<
typename _CharT>
2631 using _Iter_for = back_insert_iterator<basic_string<_CharT>>;
2633 template<
typename _Tp,
typename _CharT,
2634 typename _Context = basic_format_context<_Iter_for<_CharT>, _CharT>>
2635 concept __formattable_impl
2636 = __parsable_with<_Tp, _Context> && __formattable_with<_Tp, _Context>;
2643#if __cplusplus > 202002L
2645 template<
typename _Tp,
typename _CharT>
2647 = __format::__formattable_impl<remove_reference_t<_Tp>, _CharT>;
2650#if __cpp_lib_format_ranges
2654 template<
typename _Rg,
typename _CharT>
2655 concept __const_formattable_range
2656 = ranges::input_range<const _Rg>
2657 && formattable<ranges::range_reference_t<const _Rg>, _CharT>;
2659 template<
typename _Rg,
typename _CharT>
2660 using __maybe_const_range
2661 = conditional_t<__const_formattable_range<_Rg, _CharT>,
const _Rg, _Rg>;
2668 template<
typename _Out>
2669 struct format_to_n_result
2672 iter_difference_t<_Out>
size;
2675_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
2676template<
typename,
typename>
class vector;
2677_GLIBCXX_END_NAMESPACE_CONTAINER
2682 template<
typename _CharT>
2685 _Sink<_CharT>* _M_sink =
nullptr;
2688 using iterator_category = output_iterator_tag;
2689 using value_type = void;
2690 using difference_type = ptrdiff_t;
2691 using pointer = void;
2692 using reference = void;
2694 _Sink_iter() =
default;
2695 _Sink_iter(
const _Sink_iter&) =
default;
2696 _Sink_iter& operator=(
const _Sink_iter&) =
default;
2698 [[__gnu__::__always_inline__]]
2700 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(
std::
addressof(__sink)) { }
2702 [[__gnu__::__always_inline__]]
2703 constexpr _Sink_iter&
2704 operator=(_CharT __c)
2706 _M_sink->_M_write(__c);
2710 [[__gnu__::__always_inline__]]
2711 constexpr _Sink_iter&
2712 operator=(basic_string_view<_CharT> __s)
2714 _M_sink->_M_write(__s);
2718 [[__gnu__::__always_inline__]]
2719 constexpr _Sink_iter&
2722 [[__gnu__::__always_inline__]]
2723 constexpr _Sink_iter&
2724 operator++() {
return *
this; }
2726 [[__gnu__::__always_inline__]]
2727 constexpr _Sink_iter
2728 operator++(
int) {
return *
this; }
2731 _M_reserve(
size_t __n)
const
2732 {
return _M_sink->_M_reserve(__n); }
2738 template<
typename _CharT>
2741 friend class _Sink_iter<_CharT>;
2743 span<_CharT> _M_span;
2744 typename span<_CharT>::iterator _M_next;
2750 virtual void _M_overflow() = 0;
2754 [[__gnu__::__always_inline__]]
2756 _Sink(span<_CharT> __span) noexcept
2757 : _M_span(__span), _M_next(__span.begin())
2761 [[__gnu__::__always_inline__]]
2763 _M_used() const noexcept
2764 {
return _M_span.first(_M_next - _M_span.begin()); }
2767 [[__gnu__::__always_inline__]]
2768 constexpr span<_CharT>
2769 _M_unused() const noexcept
2770 {
return _M_span.subspan(_M_next - _M_span.begin()); }
2773 [[__gnu__::__always_inline__]]
2775 _M_rewind() noexcept
2776 { _M_next = _M_span.begin(); }
2780 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
2783 _M_next = __s.begin() + __pos;
2788 _M_write(_CharT __c)
2791 if (_M_next - _M_span.begin() == std::ssize(_M_span)) [[unlikely]]
2796 _M_write(basic_string_view<_CharT> __s)
2798 span __to = _M_unused();
2799 while (__to.size() <= __s.size())
2801 __s.copy(__to.data(), __to.size());
2802 _M_next += __to.size();
2803 __s.remove_prefix(__to.size());
2809 __s.copy(__to.data(), __s.size());
2810 _M_next += __s.size();
2819 explicit operator bool() const noexcept {
return _M_sink; }
2821 _CharT* get() const noexcept {
return _M_sink->_M_next.operator->(); }
2823 void _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
2831 virtual _Reservation
2832 _M_reserve(
size_t __n)
2834 if (__n <= _M_unused().
size())
2837 if (__n <= _M_span.size())
2840 if (__n <= _M_unused().
size())
2853 _Sink(
const _Sink&) =
delete;
2854 _Sink& operator=(
const _Sink&) =
delete;
2856 [[__gnu__::__always_inline__]]
2857 constexpr _Sink_iter<_CharT>
2859 {
return _Sink_iter<_CharT>(*
this); }
2863 template<
typename _CharT>
2864 class _Buf_sink :
public _Sink<_CharT>
2867 _CharT _M_buf[32 *
sizeof(
void*) /
sizeof(_CharT)];
2869 [[__gnu__::__always_inline__]]
2871 _Buf_sink() noexcept
2872 : _Sink<_CharT>(_M_buf)
2876 using _GLIBCXX_STD_C::vector;
2880 template<
typename _Seq>
2881 class _Seq_sink final :
public _Buf_sink<typename _Seq::value_type>
2883 using _CharT =
typename _Seq::value_type;
2889 _M_overflow()
override
2891 auto __s = this->_M_used();
2892 if (__s.empty()) [[unlikely]]
2896 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
2898 if constexpr (__is_specialization_of<_Seq, basic_string>)
2899 _M_seq.append(__s.data(), __s.size());
2901 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
2907 typename _Sink<_CharT>::_Reservation
2908 _M_reserve(
size_t __n)
override
2917 if constexpr (__is_specialization_of<_Seq, basic_string>
2918 || __is_specialization_of<_Seq, vector>)
2921 if (this->_M_used().size()) [[unlikely]]
2922 _Seq_sink::_M_overflow();
2925 const auto __sz = _M_seq.size();
2926 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
2927 _M_seq.__resize_and_overwrite(__sz + __n,
2928 [](
auto,
auto __n2) {
2932 _M_seq.resize(__sz + __n);
2936 this->_M_reset(_M_seq, __sz);
2940 return _Sink<_CharT>::_M_reserve(__n);
2944 _M_bump(
size_t __n)
override
2946 if constexpr (__is_specialization_of<_Seq, basic_string>
2947 || __is_specialization_of<_Seq, vector>)
2949 auto __s = this->_M_used();
2950 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
2952 _M_seq.resize(__s.size() + __n);
2954 this->_M_reset(this->_M_buf);
2962 [[__gnu__::__always_inline__]]
2963 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
2966 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
2970 using _Sink<_CharT>::out;
2975 if (this->_M_used().
size() != 0)
2976 _Seq_sink::_M_overflow();
2985 auto __s = this->_M_used();
2988 if (__s.size() != 0)
2989 _Seq_sink::_M_overflow();
2996 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
2998 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
3006 template<
typename _CharT,
typename _OutIter>
3007 class _Iter_sink :
public _Buf_sink<_CharT>
3010 iter_difference_t<_OutIter> _M_max;
3013 size_t _M_count = 0;
3016 _M_overflow()
override
3018 auto __s = this->_M_used();
3020 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3021 else if (_M_count <
static_cast<size_t>(_M_max))
3023 auto __max = _M_max - _M_count;
3024 span<_CharT> __first;
3025 if (__max < __s.size())
3026 __first = __s.first(
static_cast<size_t>(__max));
3029 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3032 _M_count += __s.size();
3036 [[__gnu__::__always_inline__]]
3038 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3039 : _M_out(
std::
move(__out)), _M_max(__max)
3042 using _Sink<_CharT>::out;
3044 format_to_n_result<_OutIter>
3047 if (this->_M_used().
size() != 0)
3048 _Iter_sink::_M_overflow();
3049 iter_difference_t<_OutIter> __count(_M_count);
3061 template<
typename _CharT, contiguous_iterator _OutIter>
3062 requires same_as<iter_value_t<_OutIter>, _CharT>
3063 class _Iter_sink<_CharT, _OutIter> :
public _Sink<_CharT>
3066 iter_difference_t<_OutIter> _M_max = -1;
3068 size_t _M_count = 0;
3074 _M_overflow()
override
3076 if (this->_M_unused().
size() != 0)
3079 auto __s = this->_M_used();
3083 _M_count += __s.size();
3087 this->_M_reset(this->_M_buf);
3093 this->_M_reset({__s.data(), __s.size() + 1024}, __s.size());
3097 typename _Sink<_CharT>::_Reservation
3098 _M_reserve(
size_t __n)
final
3100 auto __avail = this->_M_unused();
3101 if (__n > __avail.size())
3106 auto __s = this->_M_used();
3107 this->_M_reset({__s.data(), __s.size() + __n}, __s.size());
3114 _S_make_span(_CharT* __ptr, iter_difference_t<_OutIter> __n,
3115 span<_CharT> __buf)
noexcept
3122 if constexpr (!is_integral_v<iter_difference_t<_OutIter>>
3123 ||
sizeof(__n) >
sizeof(
size_t))
3126 auto __m = iter_difference_t<_OutIter>((
size_t)-1);
3130 return {__ptr, (size_t)__n};
3133#if __has_builtin(__builtin_dynamic_object_size)
3134 if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3135 return {__ptr, __bytes /
sizeof(_CharT)};
3138 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3139 __n = (1024 - __off) /
sizeof(_CharT);
3140 if (__n > 0) [[likely]]
3141 return {__ptr,
static_cast<size_t>(__n)};
3148 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1) noexcept
3149 : _Sink<_CharT>(_S_make_span(
std::to_address(__out), __n, _M_buf)),
3150 _M_first(__out), _M_max(__n)
3153 format_to_n_result<_OutIter>
3156 auto __s = this->_M_used();
3157 if (__s.data() == _M_buf)
3160 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3161 return { _M_first + _M_max, __count };
3165 iter_difference_t<_OutIter> __count(__s.size());
3166 return { _M_first + __count, __count };
3171 enum _Arg_t :
unsigned char {
3172 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
3173 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
3174 _Arg_i128, _Arg_u128,
3175 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
3176#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3178 _Arg_f128 = _Arg_ldbl,
3179 _Arg_ibm128 = _Arg_next_value_,
3186 template<
typename _Context>
3189 using _CharT =
typename _Context::char_type;
3205 unsigned long long _M_ull;
3208#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3209 long double _M_ldbl;
3211 const _CharT* _M_str;
3212 basic_string_view<_CharT> _M_sv;
3214 _HandleBase _M_handle;
3215#ifdef __SIZEOF_INT128__
3217 unsigned __int128 _M_u128;
3219#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3222#elif _GLIBCXX_FORMAT_F128 == 2
3223 __float128_t _M_f128;
3227 [[__gnu__::__always_inline__]]
3228 _Arg_value() : _M_none() { }
3231 template<
typename _Tp>
3232 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
3233 { _S_get<_Tp>() = __val; }
3236 template<
typename _Tp,
typename _Self>
3237 [[__gnu__::__always_inline__]]
3239 _S_get(_Self& __u)
noexcept
3241 if constexpr (is_same_v<_Tp, bool>)
3243 else if constexpr (is_same_v<_Tp, _CharT>)
3245 else if constexpr (is_same_v<_Tp, int>)
3247 else if constexpr (is_same_v<_Tp, unsigned>)
3249 else if constexpr (is_same_v<_Tp, long long>)
3251 else if constexpr (is_same_v<_Tp, unsigned long long>)
3253 else if constexpr (is_same_v<_Tp, float>)
3255 else if constexpr (is_same_v<_Tp, double>)
3257#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3258 else if constexpr (is_same_v<_Tp, long double>)
3261 else if constexpr (is_same_v<_Tp, __ieee128>)
3263 else if constexpr (is_same_v<_Tp, __ibm128>)
3264 return __u._M_ibm128;
3266 else if constexpr (is_same_v<_Tp, const _CharT*>)
3268 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
3270 else if constexpr (is_same_v<_Tp, const void*>)
3272#ifdef __SIZEOF_INT128__
3273 else if constexpr (is_same_v<_Tp, __int128>)
3275 else if constexpr (is_same_v<_Tp, unsigned __int128>)
3278#if _GLIBCXX_FORMAT_F128 == 2
3279 else if constexpr (is_same_v<_Tp, __float128_t>)
3282 else if constexpr (derived_from<_Tp, _HandleBase>)
3283 return static_cast<_Tp&
>(__u._M_handle);
3287 template<
typename _Tp>
3288 [[__gnu__::__always_inline__]]
3291 {
return _S_get<_Tp>(*
this); }
3293 template<
typename _Tp>
3294 [[__gnu__::__always_inline__]]
3296 _M_get() const noexcept
3297 {
return _S_get<_Tp>(*
this); }
3299 template<
typename _Tp>
3300 [[__gnu__::__always_inline__]]
3302 _M_set(_Tp __v)
noexcept
3304 if constexpr (derived_from<_Tp, _HandleBase>)
3305 std::construct_at(&_M_handle, __v);
3307 _S_get<_Tp>(*
this) = __v;
3312 template<
typename _Context,
typename... _Args>
3315 template<
typename _Visitor,
typename _Ctx>
3316 decltype(
auto) __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
3318 template<
typename _Ch,
typename _Tp>
3320 __to_arg_t_enum() noexcept;
3324 template<typename _Context>
3325 class basic_format_arg
3327 using _CharT =
typename _Context::char_type;
3329 template<
typename _Tp>
3330 static constexpr bool __formattable
3331 = __format::__formattable_with<_Tp, _Context>;
3334 class handle :
public __format::_Arg_value<_Context>::_HandleBase
3336 using _Base =
typename __format::_Arg_value<_Context>::_HandleBase;
3339 template<
typename _Tp>
3340 using __maybe_const_t
3341 = __conditional_t<__formattable<const _Tp>,
const _Tp, _Tp>;
3343 template<
typename _Tq>
3345 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
3346 _Context& __format_ctx,
const void* __ptr)
3348 using _Td = remove_const_t<_Tq>;
3349 typename _Context::template formatter_type<_Td> __f;
3350 __parse_ctx.advance_to(__f.parse(__parse_ctx));
3351 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
3352 __format_ctx.advance_to(__f.format(__val, __format_ctx));
3355 template<
typename _Tp>
3357 handle(_Tp& __val)
noexcept
3359 this->_M_ptr = __builtin_addressof(__val);
3360 auto __func = _S_format<__maybe_const_t<_Tp>>;
3361 this->_M_func =
reinterpret_cast<void(*)()
>(__func);
3364 friend class basic_format_arg<_Context>;
3367 handle(
const handle&) =
default;
3368 handle& operator=(
const handle&) =
default;
3370 [[__gnu__::__always_inline__]]
3372 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
3374 using _Func = void(*)(basic_format_parse_context<_CharT>&,
3375 _Context&,
const void*);
3376 auto __f =
reinterpret_cast<_Func
>(this->_M_func);
3377 __f(__pc, __fc, this->_M_ptr);
3381 [[__gnu__::__always_inline__]]
3382 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
3384 [[nodiscard,__gnu__::__always_inline__]]
3385 explicit operator bool() const noexcept
3386 {
return _M_type != __format::_Arg_none; }
3388#if __cpp_lib_format >= 202306L
3389 template<
typename _Visitor>
3391 visit(
this basic_format_arg __arg, _Visitor&& __vis)
3392 {
return __arg._M_visit_user(std::forward<_Visitor>(__vis), __arg._M_type); }
3394 template<
typename _Res,
typename _Visitor>
3396 visit(
this basic_format_arg __arg, _Visitor&& __vis)
3397 {
return __arg._M_visit_user(std::forward<_Visitor>(__vis), __arg._M_type); }
3401 template<
typename _Ctx>
3402 friend class basic_format_args;
3404 template<
typename _Ctx,
typename... _Args>
3405 friend class __format::_Arg_store;
3407 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
3409 __format::_Arg_value<_Context> _M_val;
3410 __format::_Arg_t _M_type;
3415 template<
typename _Tp>
3416 static consteval auto
3419 using _Td = remove_const_t<_Tp>;
3420 if constexpr (is_same_v<_Td, bool>)
3421 return type_identity<bool>();
3422 else if constexpr (is_same_v<_Td, _CharT>)
3423 return type_identity<_CharT>();
3424 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
3425 return type_identity<_CharT>();
3426#ifdef __SIZEOF_INT128__
3427 else if constexpr (is_same_v<_Td, __int128>)
3428 return type_identity<__int128>();
3429 else if constexpr (is_same_v<_Td, unsigned __int128>)
3430 return type_identity<unsigned __int128>();
3432 else if constexpr (__is_signed_integer<_Td>::value)
3434 if constexpr (
sizeof(_Td) <=
sizeof(
int))
3435 return type_identity<int>();
3436 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
3437 return type_identity<long long>();
3439 else if constexpr (__is_unsigned_integer<_Td>::value)
3441 if constexpr (
sizeof(_Td) <=
sizeof(
unsigned))
3442 return type_identity<unsigned>();
3443 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
3444 return type_identity<unsigned long long>();
3446 else if constexpr (is_same_v<_Td, float>)
3447 return type_identity<float>();
3448 else if constexpr (is_same_v<_Td, double>)
3449 return type_identity<double>();
3450#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3451 else if constexpr (is_same_v<_Td, long double>)
3452 return type_identity<long double>();
3454 else if constexpr (is_same_v<_Td, __ibm128>)
3455 return type_identity<__ibm128>();
3456 else if constexpr (is_same_v<_Td, __ieee128>)
3457 return type_identity<__ieee128>();
3460#if defined(__FLT16_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3461 else if constexpr (is_same_v<_Td, _Float16>)
3462 return type_identity<float>();
3465#if defined(__BFLT16_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3466 else if constexpr (is_same_v<_Td,
decltype(0.0bf16)>)
3467 return type_identity<float>();
3470#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3471 else if constexpr (is_same_v<_Td, _Float32>)
3472 return type_identity<float>();
3475#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3476 else if constexpr (is_same_v<_Td, _Float64>)
3477 return type_identity<double>();
3480#if _GLIBCXX_FORMAT_F128
3482 else if constexpr (is_same_v<_Td, _Float128>)
3483 return type_identity<__format::__float128_t>();
3485# if __SIZEOF_FLOAT128__
3486 else if constexpr (is_same_v<_Td, __float128>)
3487 return type_identity<__format::__float128_t>();
3490 else if constexpr (__is_specialization_of<_Td, basic_string_view>
3491 || __is_specialization_of<_Td, basic_string>)
3493 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
3494 return type_identity<basic_string_view<_CharT>>();
3496 return type_identity<handle>();
3498 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
3499 return type_identity<const _CharT*>();
3500 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
3501 return type_identity<const _CharT*>();
3502 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
3503 return type_identity<const void*>();
3504 else if constexpr (is_same_v<_Td, nullptr_t>)
3505 return type_identity<const void*>();
3507 return type_identity<handle>();
3511 template<
typename _Tp>
3512 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
3515 template<
typename _Tp>
3516 static consteval __format::_Arg_t
3519 using namespace __format;
3520 if constexpr (is_same_v<_Tp, bool>)
3522 else if constexpr (is_same_v<_Tp, _CharT>)
3524 else if constexpr (is_same_v<_Tp, int>)
3526 else if constexpr (is_same_v<_Tp, unsigned>)
3528 else if constexpr (is_same_v<_Tp, long long>)
3530 else if constexpr (is_same_v<_Tp, unsigned long long>)
3532 else if constexpr (is_same_v<_Tp, float>)
3534 else if constexpr (is_same_v<_Tp, double>)
3536#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3537 else if constexpr (is_same_v<_Tp, long double>)
3541 else if constexpr (is_same_v<_Tp, __ibm128>)
3543 else if constexpr (is_same_v<_Tp, __ieee128>)
3546 else if constexpr (is_same_v<_Tp, const _CharT*>)
3548 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
3550 else if constexpr (is_same_v<_Tp, const void*>)
3552#ifdef __SIZEOF_INT128__
3553 else if constexpr (is_same_v<_Tp, __int128>)
3555 else if constexpr (is_same_v<_Tp, unsigned __int128>)
3559#if _GLIBCXX_FORMAT_F128 == 2
3560 else if constexpr (is_same_v<_Tp, __format::__float128_t>)
3563 else if constexpr (is_same_v<_Tp, handle>)
3567 template<
typename _Tp>
3569 _M_set(_Tp __v)
noexcept
3571 _M_type = _S_to_enum<_Tp>();
3575 template<
typename _Tp>
3576 requires __format::__formattable_with<_Tp, _Context>
3578 basic_format_arg(_Tp& __v)
noexcept
3580 using _Td = _Normalize<_Tp>;
3581 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
3582 _M_set(_Td{__v.data(), __v.size()});
3583 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
3584 && is_same_v<_CharT, wchar_t>)
3585 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
3587 _M_set(
static_cast<_Td
>(__v));
3590 template<
typename _Ctx,
typename... _Argz>
3592 make_format_args(_Argz&...)
noexcept;
3594 template<
typename _Visitor,
typename _Ctx>
3595 friend decltype(
auto)
3596 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
3598 template<
typename _Visitor,
typename _Ctx>
3599 friend decltype(
auto)
3600 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
3602 template<
typename _Ch,
typename _Tp>
3603 friend consteval __format::_Arg_t
3604 __format::__to_arg_t_enum() noexcept;
3606 template<typename _Visitor>
3608 _M_visit(_Visitor&& __vis, __format::_Arg_t __type)
3610 using namespace __format;
3614 return std::forward<_Visitor>(__vis)(_M_val._M_none);
3616 return std::forward<_Visitor>(__vis)(_M_val._M_bool);
3618 return std::forward<_Visitor>(__vis)(_M_val._M_c);
3620 return std::forward<_Visitor>(__vis)(_M_val._M_i);
3622 return std::forward<_Visitor>(__vis)(_M_val._M_u);
3624 return std::forward<_Visitor>(__vis)(_M_val._M_ll);
3626 return std::forward<_Visitor>(__vis)(_M_val._M_ull);
3627#if __glibcxx_to_chars
3629 return std::forward<_Visitor>(__vis)(_M_val._M_flt);
3631 return std::forward<_Visitor>(__vis)(_M_val._M_dbl);
3632#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3634 return std::forward<_Visitor>(__vis)(_M_val._M_ldbl);
3637 return std::forward<_Visitor>(__vis)(_M_val._M_f128);
3639 return std::forward<_Visitor>(__vis)(_M_val._M_ibm128);
3643 return std::forward<_Visitor>(__vis)(_M_val._M_str);
3645 return std::forward<_Visitor>(__vis)(_M_val._M_sv);
3647 return std::forward<_Visitor>(__vis)(_M_val._M_ptr);
3650 auto& __h =
static_cast<handle&
>(_M_val._M_handle);
3651 return std::forward<_Visitor>(__vis)(__h);
3653#ifdef __SIZEOF_INT128__
3655 return std::forward<_Visitor>(__vis)(_M_val._M_i128);
3657 return std::forward<_Visitor>(__vis)(_M_val._M_u128);
3660#if _GLIBCXX_FORMAT_F128 == 2
3662 return std::forward<_Visitor>(__vis)(_M_val._M_f128);
3667 __builtin_unreachable();
3671 template<
typename _Visitor>
3673 _M_visit_user(_Visitor&& __vis, __format::_Arg_t __type)
3675 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
3677 constexpr bool __user_facing = __is_one_of<_Tp,
3678 monostate, bool, _CharT,
3679 int,
unsigned int,
long long int,
unsigned long long int,
3680 float, double,
long double,
3681 const _CharT*, basic_string_view<_CharT>,
3682 const void*, handle>::value;
3683 if constexpr (__user_facing)
3684 return std::forward<_Visitor>(__vis)(__val);
3688 return std::forward<_Visitor>(__vis)(__h);
3694 template<
typename _Visitor,
typename _Context>
3695 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
3696 inline decltype(auto)
3697 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
3699 return __arg._M_visit_user(std::forward<_Visitor>(__vis), __arg._M_type);
3705 template<
typename _Visitor,
typename _Ctx>
3706 inline decltype(
auto)
3707 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
3709 return __arg._M_visit(std::forward<_Visitor>(__vis), __arg._M_type);
3712 struct _WidthPrecVisitor
3714 template<
typename _Tp>
3716 operator()(_Tp& __arg)
const
3718 if constexpr (is_same_v<_Tp, monostate>)
3719 __format::__invalid_arg_id_in_format_string();
3723 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
3727 if constexpr (__is_unsigned_integer<_Tp>::value)
3729 else if constexpr (__is_signed_integer<_Tp>::value)
3733 __throw_format_error(
"format error: argument used for width or "
3734 "precision must be a non-negative integer");
3738#pragma GCC diagnostic push
3739#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
3740 template<
typename _Context>
3742 __int_from_arg(
const basic_format_arg<_Context>& __arg)
3743 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
3746 template<
int _Bits,
size_t _Nm>
3748 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
3750 __UINT64_TYPE__ __packed_types = 0;
3751 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
3752 __packed_types = (__packed_types << _Bits) | *__i;
3753 return __packed_types;
3758 template<
typename _Context>
3759 class basic_format_args
3761 static constexpr int _S_packed_type_bits = 5;
3762 static constexpr int _S_packed_type_mask = 0b11111;
3763 static constexpr int _S_max_packed_args = 12;
3765 static_assert( __format::_Arg_max_ <= (1 << _S_packed_type_bits) );
3767 template<
typename... _Args>
3768 using _Store = __format::_Arg_store<_Context, _Args...>;
3770 template<
typename _Ctx,
typename... _Args>
3771 friend class __format::_Arg_store;
3773 using uint64_t = __UINT64_TYPE__;
3774 using _Format_arg = basic_format_arg<_Context>;
3775 using _Format_arg_val = __format::_Arg_value<_Context>;
3781 uint64_t _M_packed_size : 4;
3782 uint64_t _M_unpacked_size : 60;
3785 const _Format_arg_val* _M_values;
3786 const _Format_arg* _M_args;
3790 _M_size() const noexcept
3791 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
3793 typename __format::_Arg_t
3794 _M_type(
size_t __i)
const noexcept
3796 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
3797 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
3800 template<
typename _Ctx,
typename... _Args>
3802 make_format_args(_Args&...)
noexcept;
3805 template<
typename... _Args>
3806 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
3808 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
3811 template<
typename... _Args>
3812 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
3814 [[nodiscard,__gnu__::__always_inline__]]
3815 basic_format_arg<_Context>
3816 get(
size_t __i)
const noexcept
3818 basic_format_arg<_Context> __arg;
3819 if (__i < _M_packed_size)
3821 __arg._M_type = _M_type(__i);
3822 __arg._M_val = _M_values[__i];
3824 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
3825 __arg = _M_args[__i];
3832 template<
typename _Context,
typename... _Args>
3833 basic_format_args(__format::_Arg_store<_Context, _Args...>)
3834 -> basic_format_args<_Context>;
3836 template<
typename _Context,
typename... _Args>
3838 make_format_args(_Args&... __fmt_args)
noexcept;
3841 template<
typename _Context,
typename... _Args>
3842 class __format::_Arg_store
3844 friend std::basic_format_args<_Context>;
3846 template<
typename _Ctx,
typename... _Argz>
3848#if _GLIBCXX_INLINE_VERSION
3851 make_format_args(_Argz&...)
noexcept;
3855 static constexpr bool _S_values_only
3856 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
3859 = __conditional_t<_S_values_only,
3860 __format::_Arg_value<_Context>,
3861 basic_format_arg<_Context>>;
3863 _Element_t _M_args[
sizeof...(_Args)];
3865 template<
typename _Tp>
3867 _S_make_elt(_Tp& __v)
3869 using _Tq = remove_const_t<_Tp>;
3870 using _CharT =
typename _Context::char_type;
3871 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
3872 "std::formatter must be specialized for the type "
3873 "of each format arg");
3874 using __format::__formattable_with;
3875 if constexpr (is_const_v<_Tp>)
3876 if constexpr (!__formattable_with<_Tp, _Context>)
3877 if constexpr (__formattable_with<_Tq, _Context>)
3878 static_assert(__formattable_with<_Tp, _Context>,
3879 "format arg must be non-const because its "
3880 "std::formatter specialization has a "
3881 "non-const reference parameter");
3882 basic_format_arg<_Context> __arg(__v);
3883 if constexpr (_S_values_only)
3884 return __arg._M_val;
3889 template<
typename... _Tp>
3890 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
3891 [[__gnu__::__always_inline__]]
3892 _Arg_store(_Tp&... __a) noexcept
3893 : _M_args{_S_make_elt(__a)...}
3897 template<
typename _Context>
3898 class __format::_Arg_store<_Context>
3901 template<
typename _Context>
3902 template<
typename... _Args>
3904 basic_format_args<_Context>::
3905 basic_format_args(
const _Store<_Args...>& __store)
noexcept
3907 if constexpr (
sizeof...(_Args) == 0)
3910 _M_unpacked_size = 0;
3913 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
3916 _M_packed_size =
sizeof...(_Args);
3919 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
3921 _M_values = __store._M_args;
3928 _M_unpacked_size =
sizeof...(_Args);
3930 _M_args = __store._M_args;
3935 template<
typename _Context = format_context,
typename... _Args>
3936 [[nodiscard,__gnu__::__always_inline__]]
3938 make_format_args(_Args&... __fmt_args)
noexcept
3940 using _Fmt_arg = basic_format_arg<_Context>;
3941 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
3942 _Normalize<_Args>...>;
3943 return _Store(__fmt_args...);
3946#ifdef _GLIBCXX_USE_WCHAR_T
3948 template<
typename... _Args>
3949 [[nodiscard,__gnu__::__always_inline__]]
3951 make_wformat_args(_Args&... __args)
noexcept
3952 {
return std::make_format_args<wformat_context>(__args...); }
3958 template<
typename _Out,
typename _CharT,
typename _Context>
3960 __do_vformat_to(_Out, basic_string_view<_CharT>,
3961 const basic_format_args<_Context>&,
3962 const locale* =
nullptr);
3964 template<
typename _CharT>
struct __formatter_chrono;
3982 template<
typename _Out,
typename _CharT>
3983 class basic_format_context
3985 static_assert( output_iterator<_Out, const _CharT&> );
3987 basic_format_args<basic_format_context> _M_args;
3989 __format::_Optional_locale _M_loc;
3991 basic_format_context(basic_format_args<basic_format_context> __args,
3993 : _M_args(__args), _M_out(
std::
move(__out))
3996 basic_format_context(basic_format_args<basic_format_context> __args,
3998 : _M_args(__args), _M_out(
std::
move(__out)), _M_loc(__loc)
4004 basic_format_context(
const basic_format_context&) =
delete;
4005 basic_format_context& operator=(
const basic_format_context&) =
delete;
4007 template<
typename _Out2,
typename _CharT2,
typename _Context2>
4009 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
4010 const basic_format_args<_Context2>&,
4013 friend __format::__formatter_chrono<_CharT>;
4016 ~basic_format_context() =
default;
4018 using iterator = _Out;
4019 using char_type = _CharT;
4020 template<
typename _Tp>
4021 using formatter_type = formatter<_Tp, _CharT>;
4024 basic_format_arg<basic_format_context>
4025 arg(
size_t __id)
const noexcept
4026 {
return _M_args.get(__id); }
4032 iterator out() {
return std::move(_M_out); }
4034 void advance_to(iterator __it) { _M_out =
std::move(__it); }
4046 template<
typename _CharT>
4049 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
4051 struct _Parse_context : basic_format_parse_context<_CharT>
4053 using basic_format_parse_context<_CharT>::basic_format_parse_context;
4054 const _Arg_t* _M_types =
nullptr;
4058 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
4059 : _M_pc(__str, __nargs)
4062 constexpr iterator
begin() const noexcept {
return _M_pc.begin(); }
4063 constexpr iterator
end() const noexcept {
return _M_pc.end(); }
4068 basic_string_view<_CharT> __fmt = _M_fmt_str();
4070 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
4072 _M_pc.advance_to(
begin() + 1);
4073 _M_format_arg(_M_pc.next_arg_id());
4077 size_t __lbr = __fmt.find(
'{');
4078 size_t __rbr = __fmt.find(
'}');
4080 while (__fmt.size())
4082 auto __cmp = __lbr <=> __rbr;
4086 _M_pc.advance_to(
end());
4091 if (__lbr + 1 == __fmt.size()
4092 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
4093 __format::__unmatched_left_brace_in_format_string();
4094 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
4095 iterator __last =
begin() + __lbr + int(__is_escape);
4096 _M_on_chars(__last);
4097 _M_pc.advance_to(__last + 1);
4098 __fmt = _M_fmt_str();
4101 if (__rbr != __fmt.npos)
4103 __lbr = __fmt.find(
'{');
4107 _M_on_replacement_field();
4108 __fmt = _M_fmt_str();
4109 __lbr = __fmt.find(
'{');
4110 __rbr = __fmt.find(
'}');
4115 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
4116 __format::__unmatched_right_brace_in_format_string();
4117 iterator __last =
begin() + __rbr;
4118 _M_on_chars(__last);
4119 _M_pc.advance_to(__last + 1);
4120 __fmt = _M_fmt_str();
4121 if (__lbr != __fmt.npos)
4123 __rbr = __fmt.find(
'}');
4128 constexpr basic_string_view<_CharT>
4129 _M_fmt_str() const noexcept
4132 constexpr virtual void _M_on_chars(iterator) { }
4134 constexpr void _M_on_replacement_field()
4136 auto __next =
begin();
4140 __id = _M_pc.next_arg_id();
4141 else if (*__next ==
':')
4143 __id = _M_pc.next_arg_id();
4144 _M_pc.advance_to(++__next);
4148 auto [__i, __ptr] = __format::__parse_arg_id(
begin(),
end());
4149 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
4150 __format::__invalid_arg_id_in_format_string();
4151 _M_pc.check_arg_id(__id = __i);
4154 _M_pc.advance_to(++__ptr);
4157 _M_pc.advance_to(__ptr);
4159 _M_format_arg(__id);
4161 __format::__unmatched_left_brace_in_format_string();
4162 _M_pc.advance_to(
begin() + 1);
4165 constexpr virtual void _M_format_arg(
size_t __id) = 0;
4169 template<
typename _Out,
typename _CharT>
4170 class _Formatting_scanner :
public _Scanner<_CharT>
4173 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
4174 basic_string_view<_CharT> __str)
4175 : _Scanner<_CharT>(__str), _M_fc(__fc)
4179 basic_format_context<_Out, _CharT>& _M_fc;
4181 using iterator =
typename _Scanner<_CharT>::iterator;
4184 _M_on_chars(iterator __last)
override
4186 basic_string_view<_CharT> __str(this->
begin(), __last);
4187 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
4191 _M_format_arg(
size_t __id)
override
4193 using _Context = basic_format_context<_Out, _CharT>;
4194 using handle =
typename basic_format_arg<_Context>::handle;
4196 __format::__visit_format_arg([
this](
auto& __arg) {
4198 using _Formatter =
typename _Context::template formatter_type<_Type>;
4199 if constexpr (is_same_v<_Type, monostate>)
4200 __format::__invalid_arg_id_in_format_string();
4201 else if constexpr (is_same_v<_Type, handle>)
4202 __arg.format(this->_M_pc, this->_M_fc);
4203 else if constexpr (is_default_constructible_v<_Formatter>)
4206 this->_M_pc.advance_to(__f.parse(this->_M_pc));
4207 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
4210 static_assert(__format::__formattable_with<_Type, _Context>);
4211 }, _M_fc.arg(__id));
4215 template<
typename _CharT,
typename _Tp>
4217 __to_arg_t_enum() noexcept
4219 using _Context = __format::__format_context<_CharT>;
4220 using _Fmt_arg = basic_format_arg<_Context>;
4221 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
4222 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
4226 template<
typename _CharT,
typename... _Args>
4227 class _Checking_scanner :
public _Scanner<_CharT>
4230 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
4231 "std::formatter must be specialized for each type being formatted");
4235 _Checking_scanner(basic_string_view<_CharT> __str)
4236 : _Scanner<_CharT>(__str, sizeof...(_Args))
4238#if __cpp_lib_format >= 202305L
4239 this->_M_pc._M_types = _M_types.data();
4245 _M_format_arg(
size_t __id)
override
4247 if constexpr (
sizeof...(_Args) != 0)
4249 if (__id <
sizeof...(_Args))
4251 _M_parse_format_spec<_Args...>(__id);
4255 __builtin_unreachable();
4258 template<
typename _Tp,
typename... _OtherArgs>
4260 _M_parse_format_spec(
size_t __id)
4264 formatter<_Tp, _CharT> __f;
4265 this->_M_pc.advance_to(__f.parse(this->_M_pc));
4267 else if constexpr (
sizeof...(_OtherArgs) != 0)
4268 _M_parse_format_spec<_OtherArgs...>(__id - 1);
4270 __builtin_unreachable();
4273#if __cpp_lib_format >= 202305L
4274 array<_Arg_t,
sizeof...(_Args)>
4275 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
4279 template<
typename _Out,
typename _CharT,
typename _Context>
4281 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
4282 const basic_format_args<_Context>& __args,
4283 const locale* __loc)
4285 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
4286 _Sink_iter<_CharT> __sink_out;
4288 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4291 __sink_out = __sink.out();
4293 if constexpr (is_same_v<_CharT, char>)
4295 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
4297 bool __done =
false;
4298 __format::__visit_format_arg([&](
auto& __arg) {
4299 using _Tp = remove_cvref_t<
decltype(__arg)>;
4300 if constexpr (is_same_v<_Tp, bool>)
4302 size_t __len = 4 + !__arg;
4303 const char* __chars[] = {
"false",
"true" };
4304 if (
auto __res = __sink_out._M_reserve(__len))
4306 __builtin_memcpy(__res.get(), __chars[__arg], __len);
4307 __res._M_bump(__len);
4311 else if constexpr (is_same_v<_Tp, char>)
4313 if (
auto __res = __sink_out._M_reserve(1))
4315 *__res.get() = __arg;
4320 else if constexpr (is_integral_v<_Tp>)
4322 make_unsigned_t<_Tp> __uval;
4323 const bool __neg = __arg < 0;
4325 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
4328 const auto __n = __detail::__to_chars_len(__uval);
4329 if (
auto __res = __sink_out._M_reserve(__n + __neg))
4331 auto __ptr = __res.get();
4333 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
4335 __res._M_bump(__n + __neg);
4339 else if constexpr (is_convertible_v<_Tp, string_view>)
4341 string_view __sv = __arg;
4342 if (
auto __res = __sink_out._M_reserve(__sv.size()))
4344 __builtin_memcpy(__res.get(), __sv.data(), __sv.size());
4345 __res._M_bump(__sv.size());
4353 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4356 return std::move(__sink)._M_finish().out;
4360 auto __ctx = __loc ==
nullptr
4361 ? _Context(__args, __sink_out)
4362 : _Context(__args, __sink_out, *__loc);
4363 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
4364 __scanner._M_scan();
4366 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4369 return std::move(__sink)._M_finish().out;
4371#pragma GCC diagnostic pop
4376#if __cpp_lib_format >= 202305L
4379 template<
typename _CharT>
4380 template<
typename... _Ts>
4382 basic_format_parse_context<_CharT>::
4383 __check_dynamic_spec(
size_t __id)
noexcept
4385 if (__id >= _M_num_args)
4386 __format::__invalid_arg_id_in_format_string();
4387 if constexpr (
sizeof...(_Ts) != 0)
4389 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
4390 auto __arg =
static_cast<_Parse_ctx*
>(
this)->_M_types[__id];
4391 __format::_Arg_t __types[] = {
4392 __format::__to_arg_t_enum<_CharT, _Ts>()...
4394 for (
auto __t : __types)
4398 __invalid_dynamic_spec(
"arg(id) type does not match");
4403 template<
typename _CharT,
typename... _Args>
4404 template<
typename _Tp>
4405 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
4407 basic_format_string<_CharT, _Args...>::
4408 basic_format_string(
const _Tp& __s)
4411 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
4413 __scanner._M_scan();
4418 template<
typename _Out>
requires output_iterator<_Out, const char&>
4419 [[__gnu__::__always_inline__]]
4421 vformat_to(_Out __out, string_view __fmt, format_args __args)
4422 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
4424#ifdef _GLIBCXX_USE_WCHAR_T
4425 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
4426 [[__gnu__::__always_inline__]]
4428 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
4429 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
4432 template<
typename _Out>
requires output_iterator<_Out, const char&>
4433 [[__gnu__::__always_inline__]]
4435 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
4438 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
4441#ifdef _GLIBCXX_USE_WCHAR_T
4442 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
4443 [[__gnu__::__always_inline__]]
4445 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
4446 wformat_args __args)
4448 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
4454 vformat(string_view __fmt, format_args __args)
4456 __format::_Str_sink<char> __buf;
4457 std::vformat_to(__buf.out(), __fmt, __args);
4461#ifdef _GLIBCXX_USE_WCHAR_T
4464 vformat(wstring_view __fmt, wformat_args __args)
4466 __format::_Str_sink<wchar_t> __buf;
4467 std::vformat_to(__buf.out(), __fmt, __args);
4474 vformat(
const locale& __loc, string_view __fmt, format_args __args)
4476 __format::_Str_sink<char> __buf;
4477 std::vformat_to(__buf.out(), __loc, __fmt, __args);
4481#ifdef _GLIBCXX_USE_WCHAR_T
4484 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
4486 __format::_Str_sink<wchar_t> __buf;
4487 std::vformat_to(__buf.out(), __loc, __fmt, __args);
4492 template<
typename... _Args>
4495 format(format_string<_Args...> __fmt, _Args&&... __args)
4496 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
4498#ifdef _GLIBCXX_USE_WCHAR_T
4499 template<
typename... _Args>
4502 format(wformat_string<_Args...> __fmt, _Args&&... __args)
4503 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
4506 template<
typename... _Args>
4509 format(
const locale& __loc, format_string<_Args...> __fmt,
4512 return std::vformat(__loc, __fmt.get(),
4513 std::make_format_args(__args...));
4516#ifdef _GLIBCXX_USE_WCHAR_T
4517 template<
typename... _Args>
4520 format(
const locale& __loc, wformat_string<_Args...> __fmt,
4523 return std::vformat(__loc, __fmt.get(),
4524 std::make_wformat_args(__args...));
4528 template<
typename _Out,
typename... _Args>
4529 requires output_iterator<_Out, const char&>
4531 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
4533 return std::vformat_to(
std::move(__out), __fmt.get(),
4534 std::make_format_args(__args...));
4537#ifdef _GLIBCXX_USE_WCHAR_T
4538 template<
typename _Out,
typename... _Args>
4539 requires output_iterator<_Out, const wchar_t&>
4541 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
4543 return std::vformat_to(
std::move(__out), __fmt.get(),
4544 std::make_wformat_args(__args...));
4548 template<
typename _Out,
typename... _Args>
4549 requires output_iterator<_Out, const char&>
4551 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
4554 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
4555 std::make_format_args(__args...));
4558#ifdef _GLIBCXX_USE_WCHAR_T
4559 template<
typename _Out,
typename... _Args>
4560 requires output_iterator<_Out, const wchar_t&>
4562 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
4565 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
4566 std::make_wformat_args(__args...));
4570 template<
typename _Out,
typename... _Args>
4571 requires output_iterator<_Out, const char&>
4572 inline format_to_n_result<_Out>
4573 format_to_n(_Out __out, iter_difference_t<_Out> __n,
4574 format_string<_Args...> __fmt, _Args&&... __args)
4576 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
4577 std::vformat_to(__sink.out(), __fmt.get(),
4578 std::make_format_args(__args...));
4582#ifdef _GLIBCXX_USE_WCHAR_T
4583 template<
typename _Out,
typename... _Args>
4584 requires output_iterator<_Out, const wchar_t&>
4585 inline format_to_n_result<_Out>
4586 format_to_n(_Out __out, iter_difference_t<_Out> __n,
4587 wformat_string<_Args...> __fmt, _Args&&... __args)
4589 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
4590 std::vformat_to(__sink.out(), __fmt.get(),
4591 std::make_wformat_args(__args...));
4596 template<
typename _Out,
typename... _Args>
4597 requires output_iterator<_Out, const char&>
4598 inline format_to_n_result<_Out>
4599 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
4600 format_string<_Args...> __fmt, _Args&&... __args)
4602 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
4603 std::vformat_to(__sink.out(), __loc, __fmt.get(),
4604 std::make_format_args(__args...));
4608#ifdef _GLIBCXX_USE_WCHAR_T
4609 template<
typename _Out,
typename... _Args>
4610 requires output_iterator<_Out, const wchar_t&>
4611 inline format_to_n_result<_Out>
4612 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
4613 wformat_string<_Args...> __fmt, _Args&&... __args)
4615 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
4616 std::vformat_to(__sink.out(), __loc, __fmt.get(),
4617 std::make_wformat_args(__args...));
4626 template<
typename _CharT>
4627 class _Counting_sink final :
public _Iter_sink<_CharT, _CharT*>
4630 _Counting_sink() : _Iter_sink<_CharT, _CharT*>(nullptr, 0) { }
4632 [[__gnu__::__always_inline__]]
4635 {
return this->_M_count + this->_M_used().size(); }
4638 template<
typename _CharT>
4639 class _Counting_sink :
public _Buf_sink<_CharT>
4641 size_t _M_count = 0;
4644 _M_overflow()
override
4646 if (!std::is_constant_evaluated())
4647 _M_count += this->_M_used().size();
4652 _Counting_sink() =
default;
4654 [[__gnu__::__always_inline__]]
4658 _Counting_sink::_M_overflow();
4666 template<
typename... _Args>
4669 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
4671 __format::_Counting_sink<char> __buf;
4672 std::vformat_to(__buf.out(), __fmt.get(),
4673 std::make_format_args(__args...));
4674 return __buf.count();
4677#ifdef _GLIBCXX_USE_WCHAR_T
4678 template<
typename... _Args>
4681 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
4683 __format::_Counting_sink<wchar_t> __buf;
4684 std::vformat_to(__buf.out(), __fmt.get(),
4685 std::make_wformat_args(__args...));
4686 return __buf.count();
4690 template<
typename... _Args>
4693 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
4696 __format::_Counting_sink<char> __buf;
4697 std::vformat_to(__buf.out(), __loc, __fmt.get(),
4698 std::make_format_args(__args...));
4699 return __buf.count();
4702#ifdef _GLIBCXX_USE_WCHAR_T
4703 template<
typename... _Args>
4706 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
4709 __format::_Counting_sink<wchar_t> __buf;
4710 std::vformat_to(__buf.out(), __loc, __fmt.get(),
4711 std::make_wformat_args(__args...));
4712 return __buf.count();
4716#if __cpp_lib_format_ranges
4719 enum class range_format {
4729 template<
typename _Rg>
4730 constexpr auto format_kind = not defined(format_kind<_Rg>);
4732 template<
typename _Tp>
4733 consteval range_format
4736 using _Ref = ranges::range_reference_t<_Tp>;
4737 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
4738 return range_format::disabled;
4739 else if constexpr (
requires {
typename _Tp::key_type; })
4741 if constexpr (
requires {
typename _Tp::mapped_type; })
4743 using _Up = remove_cvref_t<_Ref>;
4744 if constexpr (__is_pair<_Up>)
4745 return range_format::map;
4746 else if constexpr (__is_specialization_of<_Up, tuple>)
4747 if constexpr (tuple_size_v<_Up> == 2)
4748 return range_format::map;
4750 return range_format::set;
4753 return range_format::sequence;
4758 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
4759 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
4762 template<
typename _Tp,
typename _CharT =
char>
4763 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
4764 class range_formatter;
4770 template<range_format _Kind, ranges::input_range _Rg,
typename _CharT>
4771 struct __range_default_formatter;
4777 template<ranges::input_range _Rg,
typename _CharT>
4778 requires (format_kind<_Rg> != range_format::disabled)
4779 && formattable<ranges::range_reference_t<_Rg>, _CharT>
4780 struct formatter<_Rg, _CharT>
4781 : __format::__range_default_formatter<format_kind<_Rg>, _Rg, _CharT>
4785_GLIBCXX_END_NAMESPACE_VERSION
4788#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.