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;
88 template<
typename _CharT>
90 _Widen(
const char* __narrow,
const wchar_t* __wide)
92 if constexpr (is_same_v<_CharT, wchar_t>)
97#define _GLIBCXX_WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
98#define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
101 template<
typename _CharT>
102 constexpr size_t __stackbuf_size = 32 *
sizeof(
void*) /
sizeof(_CharT);
105 template<
typename _CharT>
class _Sink;
106 template<
typename _CharT>
class _Fixedbuf_sink;
107 template<
typename _Seq>
class _Seq_sink;
109 template<
typename _CharT,
typename _Alloc = allocator<_CharT>>
111 = _Seq_sink<basic_string<_CharT, char_traits<_CharT>, _Alloc>>;
117 template<
typename _CharT>
121 template<
typename _CharT>
123 {
using type = back_insert_iterator<basic_string<_CharT>>; };
125 template<
typename _CharT>
126 using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
128 template<
typename _CharT>
129 struct _Runtime_format_string
131 [[__gnu__::__always_inline__]]
132 _Runtime_format_string(basic_string_view<_CharT> __s) noexcept
135 _Runtime_format_string(
const _Runtime_format_string&) =
delete;
136 void operator=(
const _Runtime_format_string&) =
delete;
139 basic_string_view<_CharT> _M_str;
141 template<
typename,
typename...>
friend struct std::basic_format_string;
147 using format_context = __format::__format_context<char>;
148#ifdef _GLIBCXX_USE_WCHAR_T
149 using wformat_context = __format::__format_context<wchar_t>;
153 template<
typename _Context>
class basic_format_args;
154 using format_args = basic_format_args<format_context>;
155#ifdef _GLIBCXX_USE_WCHAR_T
156 using wformat_args = basic_format_args<wformat_context>;
161 template<
typename _Context>
162 class basic_format_arg;
168 template<
typename _CharT,
typename... _Args>
169 struct basic_format_string
171 template<
typename _Tp>
172 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
174 basic_format_string(
const _Tp& __s);
176 [[__gnu__::__always_inline__]]
177 basic_format_string(__format::_Runtime_format_string<_CharT> __s) noexcept
181 [[__gnu__::__always_inline__]]
182 constexpr basic_string_view<_CharT>
187 basic_string_view<_CharT> _M_str;
190 template<
typename... _Args>
191 using format_string = basic_format_string<char, type_identity_t<_Args>...>;
193#ifdef _GLIBCXX_USE_WCHAR_T
194 template<
typename... _Args>
196 = basic_format_string<wchar_t, type_identity_t<_Args>...>;
199#if __cpp_lib_format >= 202311L
200 [[__gnu__::__always_inline__]]
201 inline __format::_Runtime_format_string<char>
202 runtime_format(string_view __fmt)
noexcept
205#ifdef _GLIBCXX_USE_WCHAR_T
206 [[__gnu__::__always_inline__]]
207 inline __format::_Runtime_format_string<wchar_t>
208 runtime_format(wstring_view __fmt)
noexcept
216 template<
typename _Tp,
typename _CharT>
219 formatter() =
delete;
220 formatter(
const formatter&) =
delete;
221 formatter& operator=(
const formatter&) =
delete;
228 explicit format_error(
const string& __what) : runtime_error(__what) { }
229 explicit format_error(
const char* __what) : runtime_error(__what) { }
235 __throw_format_error(
const char* __what)
236 { _GLIBCXX_THROW_OR_ABORT(format_error(__what)); }
244 __unmatched_left_brace_in_format_string()
245 { __throw_format_error(
"format error: unmatched '{' in format string"); }
249 __unmatched_right_brace_in_format_string()
250 { __throw_format_error(
"format error: unmatched '}' in format string"); }
254 __conflicting_indexing_in_format_string()
255 { __throw_format_error(
"format error: conflicting indexing style in format string"); }
259 __invalid_arg_id_in_format_string()
260 { __throw_format_error(
"format error: invalid arg-id in format string"); }
264 __failed_to_parse_format_spec()
265 { __throw_format_error(
"format error: failed to parse format-spec"); }
267 template<
typename _CharT>
class _Scanner;
273 template<
typename _CharT>
class basic_format_parse_context;
274 using format_parse_context = basic_format_parse_context<char>;
275#ifdef _GLIBCXX_USE_WCHAR_T
276 using wformat_parse_context = basic_format_parse_context<wchar_t>;
279 template<
typename _CharT>
280 class basic_format_parse_context
283 using char_type = _CharT;
284 using const_iterator =
typename basic_string_view<_CharT>::const_iterator;
285 using iterator = const_iterator;
288 basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
289 : _M_begin(__fmt.begin()), _M_end(__fmt.end())
292 basic_format_parse_context(
const basic_format_parse_context&) =
delete;
293 void operator=(
const basic_format_parse_context&) =
delete;
295 constexpr const_iterator
begin() const noexcept {
return _M_begin; }
296 constexpr const_iterator
end() const noexcept {
return _M_end; }
299 advance_to(const_iterator __it)
noexcept
305 if (_M_indexing == _Manual)
306 __format::__conflicting_indexing_in_format_string();
311 if (std::is_constant_evaluated())
312 if (_M_next_arg_id == _M_num_args)
313 __format::__invalid_arg_id_in_format_string();
314 return _M_next_arg_id++;
318 check_arg_id(
size_t __id)
320 if (_M_indexing == _Auto)
321 __format::__conflicting_indexing_in_format_string();
322 _M_indexing = _Manual;
324 if (std::is_constant_evaluated())
325 if (__id >= _M_num_args)
326 __format::__invalid_arg_id_in_format_string();
329#if __cpp_lib_format >= 202305L
330 template<
typename... _Ts>
332 check_dynamic_spec(
size_t __id)
noexcept
334 static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
335 "template arguments for check_dynamic_spec<Ts...>(id) "
336 "must be unique and must be one of the allowed types");
338 __check_dynamic_spec<_Ts...>(__id);
343 check_dynamic_spec_integral(
size_t __id)
noexcept
346 __check_dynamic_spec<int, unsigned,
long long,
347 unsigned long long>(__id);
352 check_dynamic_spec_string(
size_t __id)
noexcept
355 __check_dynamic_spec<const _CharT*, basic_string_view<_CharT>>(__id);
361 template<
typename _Tp,
typename... _Ts>
362 static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
364 template<
typename... _Ts>
366 __valid_types_for_check_dynamic_spec()
370 if constexpr (
sizeof...(_Ts) == 0)
379 = __once<bool, _Ts...>
380 + __once<char_type, _Ts...>
381 + __once<int, _Ts...>
382 + __once<
unsigned int, _Ts...>
383 + __once<
long long int, _Ts...>
384 + __once<
unsigned long long int, _Ts...>
385 + __once<float, _Ts...>
386 + __once<double, _Ts...>
387 + __once<
long double, _Ts...>
388 + __once<
const char_type*, _Ts...>
389 + __once<basic_string_view<char_type>, _Ts...>
390 + __once<
const void*, _Ts...>;
391 return __sum ==
sizeof...(_Ts);
395 template<
typename... _Ts>
397 __check_dynamic_spec(
size_t __id)
noexcept;
400 static void __invalid_dynamic_spec(
const char*);
402 friend __format::_Scanner<_CharT>;
407 basic_format_parse_context(basic_string_view<_CharT> __fmt,
408 size_t __num_args) noexcept
409 : _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
415 enum _Indexing { _Unknown, _Manual, _Auto };
416 _Indexing _M_indexing = _Unknown;
417 size_t _M_next_arg_id = 0;
418 size_t _M_num_args = 0;
422 template<
typename _Tp,
template<
typename...>
class _Class>
423 constexpr bool __is_specialization_of =
false;
424 template<
template<
typename...>
class _Class,
typename... _Args>
425 constexpr bool __is_specialization_of<_Class<_Args...>, _Class> =
true;
430 template<
typename _CharT>
431 constexpr pair<unsigned short, const _CharT*>
432 __parse_integer(
const _CharT* __first,
const _CharT* __last)
434 if (__first == __last)
435 __builtin_unreachable();
437 if constexpr (is_same_v<_CharT, char>)
439 const auto __start = __first;
440 unsigned short __val = 0;
442 if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
443 && __first != __start) [[likely]]
444 return {__val, __first};
448 constexpr int __n = 32;
450 for (
int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
451 __buf[__i] = __first[__i];
452 auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
453 if (__ptr) [[likely]]
454 return {__v, __first + (__ptr - __buf)};
459 template<
typename _CharT>
460 constexpr pair<unsigned short, const _CharT*>
461 __parse_arg_id(
const _CharT* __first,
const _CharT* __last)
463 if (__first == __last)
464 __builtin_unreachable();
467 return {0, __first + 1};
469 if (
'1' <= *__first && *__first <=
'9')
471 const unsigned short __id = *__first -
'0';
472 const auto __next = __first + 1;
474 if (__next == __last || !(
'0' <= *__next && *__next <=
'9'))
475 return {__id, __next};
477 return __format::__parse_integer(__first, __last);
485 _Pres_d = 1, _Pres_b, _Pres_B, _Pres_o, _Pres_x, _Pres_X, _Pres_c,
487 _Pres_a = 1, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F, _Pres_g, _Pres_G,
488 _Pres_p = 0, _Pres_P,
490 _Pres_seq = 0, _Pres_str,
514 template<
typename _Context>
516 __int_from_arg(
const basic_format_arg<_Context>& __arg);
518 constexpr bool __is_digit(
char __c)
519 {
return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
521 constexpr bool __is_xdigit(
char __c)
522 {
return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
524 template<
typename _CharT>
530 unsigned _M_localized : 1;
531 unsigned _M_zero_fill : 1;
532 _WidthPrec _M_width_kind : 2;
533 _WidthPrec _M_prec_kind : 2;
534 _Pres_type _M_type : 4;
535 unsigned _M_reserved : 1;
536 unsigned _M_reserved2 : 16;
537 unsigned short _M_width;
538 unsigned short _M_prec;
539 char32_t _M_fill =
' ';
541 using iterator =
typename basic_string_view<_CharT>::iterator;
543 static constexpr _Align
544 _S_align(_CharT __c)
noexcept
548 case '<':
return _Align_left;
549 case '>':
return _Align_right;
550 case '^':
return _Align_centre;
551 default:
return _Align_default;
557 _M_parse_fill_and_align(iterator __first, iterator __last)
noexcept
558 {
return _M_parse_fill_and_align(__first, __last,
"{"); }
562 _M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill)
noexcept
564 for (
char __c : __not_fill)
565 if (*__first ==
static_cast<_CharT
>(__c))
568 using namespace __unicode;
569 if constexpr (__literal_encoding_is_unicode<_CharT>())
572 _Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
575 auto __beg = __uv.begin();
576 char32_t __c = *__beg++;
577 if (__is_scalar_value(__c))
578 if (
auto __next = __beg.base(); __next != __last)
579 if (_Align __align = _S_align(*__next))
587 else if (__last - __first >= 2)
588 if (_Align __align = _S_align(__first[1]))
595 if (_Align __align = _S_align(__first[0]))
604 static constexpr _Sign
605 _S_sign(_CharT __c)
noexcept
609 case '+':
return _Sign_plus;
610 case '-':
return _Sign_minus;
611 case ' ':
return _Sign_space;
612 default:
return _Sign_default;
618 _M_parse_sign(iterator __first, iterator)
noexcept
620 if (_Sign __sign = _S_sign(*__first))
630 _M_parse_alternate_form(iterator __first, iterator)
noexcept
642 _M_parse_zero_fill(iterator __first, iterator )
noexcept
653 static constexpr iterator
654 _S_parse_width_or_precision(iterator __first, iterator __last,
655 unsigned short& __val,
bool& __arg_id,
656 basic_format_parse_context<_CharT>& __pc)
658 if (__format::__is_digit(*__first))
660 auto [__v, __ptr] = __format::__parse_integer(__first, __last);
662 __throw_format_error(
"format error: invalid width or precision "
667 else if (*__first ==
'{')
671 if (__first == __last)
672 __format::__unmatched_left_brace_in_format_string();
674 __val = __pc.next_arg_id();
677 auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
678 if (__ptr ==
nullptr || __ptr == __last || *__ptr !=
'}')
679 __format::__invalid_arg_id_in_format_string();
681 __pc.check_arg_id(__v);
684#if __cpp_lib_format >= 202305L
685 __pc.check_dynamic_spec_integral(__val);
694 _M_parse_width(iterator __first, iterator __last,
695 basic_format_parse_context<_CharT>& __pc)
697 bool __arg_id =
false;
699 __throw_format_error(
"format error: width must be non-zero in "
701 auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
703 if (__next != __first)
704 _M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
710 _M_parse_precision(iterator __first, iterator __last,
711 basic_format_parse_context<_CharT>& __pc)
713 if (__first[0] !=
'.')
716 iterator __next = ++__first;
717 bool __arg_id =
false;
718 if (__next != __last)
719 __next = _S_parse_width_or_precision(__first, __last, _M_prec,
721 if (__next == __first)
722 __throw_format_error(
"format error: missing precision after '.' in "
724 _M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
730 _M_parse_locale(iterator __first, iterator )
noexcept
740 template<
typename _Context>
742 _M_get_width(_Context& __ctx)
const
745 if (_M_width_kind == _WP_value)
747 else if (_M_width_kind == _WP_from_arg)
748 __width = __format::__int_from_arg(__ctx.arg(_M_width));
752 template<
typename _Context>
754 _M_get_precision(_Context& __ctx)
const
757 if (_M_prec_kind == _WP_value)
759 else if (_M_prec_kind == _WP_from_arg)
760 __prec = __format::__int_from_arg(__ctx.arg(_M_prec));
765 template<
typename _Int>
767 __put_sign(_Int __i, _Sign __sign,
char* __dest)
noexcept
771 else if (__sign == _Sign_plus)
773 else if (__sign == _Sign_space)
781 template<
typename _Out,
typename _CharT>
782 requires output_iterator<_Out, const _CharT&>
784 __write(_Out __out, basic_string_view<_CharT> __str)
786 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
792 for (_CharT __c : __str)
799 template<
typename _Out,
typename _CharT>
801 __write_padded(_Out __out, basic_string_view<_CharT> __str,
802 _Align __align,
size_t __nfill,
char32_t __fill_char)
804 const size_t __buflen = 0x20;
805 _CharT __padding_chars[__buflen];
806 __padding_chars[0] = _CharT();
807 basic_string_view<_CharT> __padding{__padding_chars, __buflen};
809 auto __pad = [&__padding] (
size_t __n, _Out& __o) {
812 while (__n > __padding.size())
814 __o = __format::__write(
std::move(__o), __padding);
815 __n -= __padding.size();
818 __o = __format::__write(
std::move(__o), __padding.substr(0, __n));
821 size_t __l, __r, __max;
822 if (__align == _Align_centre)
825 __r = __l + (__nfill & 1);
828 else if (__align == _Align_right)
841 using namespace __unicode;
842 if constexpr (__literal_encoding_is_unicode<_CharT>())
843 if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
846 const char32_t __arr[1]{ __fill_char };
847 _Utf_view<_CharT,
const char32_t(&)[1]> __v(__arr);
848 basic_string<_CharT> __padstr(__v.begin(), __v.end());
849 __padding = __padstr;
851 __out = __format::__write(
std::move(__out), __padding);
852 __out = __format::__write(
std::move(__out), __str);
854 __out = __format::__write(
std::move(__out), __padding);
858 if (__max < __buflen)
859 __padding.remove_suffix(__buflen - __max);
863 char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
865 __out = __format::__write(
std::move(__out), __str);
873 template<
typename _CharT,
typename _Out>
875 __write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
876 size_t __estimated_width,
877 basic_format_context<_Out, _CharT>& __fc,
878 const _Spec<_CharT>& __spec,
879 _Align __align = _Align_left)
881 size_t __width = __spec._M_get_width(__fc);
883 if (__width <= __estimated_width)
884 return __format::__write(__fc.out(), __str);
886 const size_t __nfill = __width - __estimated_width;
889 __align = __spec._M_align;
891 return __format::__write_padded(__fc.out(), __str, __align, __nfill,
896 enum class _Term_char :
unsigned char {
901 template<
typename _CharT>
904 using _Str_view = basic_string_view<_CharT>;
908 {
return _GLIBCXX_WIDEN(
"\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
911 _CharT _S_term(_Term_char __term)
912 {
return _S_all()[
static_cast<unsigned char>(__term)]; }
916 {
return _S_all().substr(0, 3); }
919 _Str_view _S_newline()
920 {
return _S_all().substr(3, 3); }
923 _Str_view _S_return()
924 {
return _S_all().substr(6, 3); }
927 _Str_view _S_bslash()
928 {
return _S_all().substr(9, 3); }
932 {
return _S_all().substr(12, 3); }
936 {
return _S_all().substr(15, 3); }
940 {
return _S_all().substr(18, 2); }
944 {
return _S_all().substr(20, 2); }
947 template<
typename _CharT>
950 using _Str_view = basic_string_view<_CharT>;
954 {
return _GLIBCXX_WIDEN(
"[]{}(), : "); }
957 _Str_view _S_squares()
958 {
return _S_all().substr(0, 2); }
961 _Str_view _S_braces()
962 {
return _S_all().substr(2, 2); }
965 _Str_view _S_parens()
966 {
return _S_all().substr(4, 2); }
970 {
return _S_all().substr(6, 2); }
974 {
return _S_all().substr(8, 2); }
977 template<
typename _CharT>
978 constexpr bool __should_escape_ascii(_CharT __c, _Term_char __term)
980 using _Esc = _Escapes<_CharT>;
983 case _Esc::_S_tab()[0]:
984 case _Esc::_S_newline()[0]:
985 case _Esc::_S_return()[0]:
986 case _Esc::_S_bslash()[0]:
988 case _Esc::_S_quote()[0]:
989 return __term == _Term_char::_Tc_quote;
990 case _Esc::_S_apos()[0]:
991 return __term == _Term_char::_Tc_apos;
993 return (__c >= 0 && __c < 0x20) || __c == 0x7f;
998 constexpr bool __should_escape_unicode(
char32_t __c,
bool __prev_esc)
1000 if (__unicode::__should_escape_category(__c))
1004 return __unicode::__grapheme_cluster_break_property(__c)
1005 == __unicode::_Gcb_property::_Gcb_Extend;
1008 using uint_least32_t = __UINT_LEAST32_TYPE__;
1009 template<
typename _Out,
typename _CharT>
1011 __write_escape_seq(_Out __out, uint_least32_t __val,
1012 basic_string_view<_CharT> __prefix)
1014 constexpr size_t __max = 8;
1016 const string_view __narrow(
1018 std::__to_chars_i<uint_least32_t>(__buf, __buf + __max, __val, 16).ptr);
1020 __out = __format::__write(__out, __prefix);
1021 *__out = _Separators<_CharT>::_S_braces()[0];
1023 if constexpr (is_same_v<char, _CharT>)
1024 __out = __format::__write(__out, __narrow);
1025#ifdef _GLIBCXX_USE_WCHAR_T
1028 wchar_t __wbuf[__max];
1029 const size_t __n = __narrow.size();
1030 std::__to_wstring_numeric(__narrow.data(), __n, __wbuf);
1031 __out = __format::__write(__out, wstring_view(__wbuf, __n));
1034 *__out = _Separators<_CharT>::_S_braces()[1];
1038 template<
typename _Out,
typename _CharT>
1040 __write_escaped_char(_Out __out, _CharT __c)
1042 using _UChar = make_unsigned_t<_CharT>;
1043 using _Esc = _Escapes<_CharT>;
1046 case _Esc::_S_tab()[0]:
1047 return __format::__write(__out, _Esc::_S_tab().substr(1, 2));
1048 case _Esc::_S_newline()[0]:
1049 return __format::__write(__out, _Esc::_S_newline().substr(1, 2));
1050 case _Esc::_S_return()[0]:
1051 return __format::__write(__out, _Esc::_S_return().substr(1, 2));
1052 case _Esc::_S_bslash()[0]:
1053 return __format::__write(__out, _Esc::_S_bslash().substr(1, 2));
1054 case _Esc::_S_quote()[0]:
1055 return __format::__write(__out, _Esc::_S_quote().substr(1, 2));
1056 case _Esc::_S_apos()[0]:
1057 return __format::__write(__out, _Esc::_S_apos().substr(1, 2));
1059 return __format::__write_escape_seq(__out,
1060 static_cast<_UChar
>(__c),
1065 template<
typename _CharT,
typename _Out>
1067 __write_escaped_ascii(_Out __out,
1068 basic_string_view<_CharT> __str,
1071 using _Str_view = basic_string_view<_CharT>;
1072 auto __first = __str.begin();
1073 auto const __last = __str.end();
1074 while (__first != __last)
1076 auto __print = __first;
1078 while (__print != __last
1079 && !__format::__should_escape_ascii(*__print, __term))
1082 if (__print != __first)
1083 __out = __format::__write(__out, _Str_view(__first, __print));
1085 if (__print == __last)
1089 __out = __format::__write_escaped_char(__out, *__first);
1095 template<
typename _CharT,
typename _Out>
1097 __write_escaped_unicode(_Out __out,
1098 basic_string_view<_CharT> __str,
1101 using _Str_view = basic_string_view<_CharT>;
1102 using _UChar = make_unsigned_t<_CharT>;
1103 using _Esc = _Escapes<_CharT>;
1105 static constexpr char32_t __replace = U
'\uFFFD';
1106 static constexpr _Str_view __replace_rep = []
1109 if constexpr (is_same_v<char, _CharT>)
1110 return "\xEF\xBF\xBD";
1115 __unicode::_Utf_view<char32_t, _Str_view> __v(
std::move(__str));
1116 auto __first = __v.begin();
1117 auto const __last = __v.end();
1119 bool __prev_esc =
true;
1120 while (__first != __last)
1122 bool __esc_ascii =
false;
1123 bool __esc_unicode =
false;
1124 bool __esc_replace =
false;
1125 auto __should_escape = [&](
auto const& __it)
1129 = __format::__should_escape_ascii(*__it.base(), __term);
1130 if (__format::__should_escape_unicode(*__it, __prev_esc))
1131 return __esc_unicode =
true;
1132 if (*__it == __replace)
1134 _Str_view __units(__it.base(), __it._M_units());
1135 return __esc_replace = (__units != __replace_rep);
1140 auto __print = __first;
1141 while (__print != __last && !__should_escape(__print))
1147 if (__print != __first)
1148 __out = __format::__write(__out, _Str_view(__first.base(), __print.base()));
1150 if (__print == __last)
1155 __out = __format::__write_escaped_char(__out, *__first.base());
1156 else if (__esc_unicode)
1157 __out = __format::__write_escape_seq(__out, *__first, _Esc::_S_u());
1159 for (_CharT __c : _Str_view(__first.base(), __first._M_units()))
1160 __out = __format::__write_escape_seq(__out,
1161 static_cast<_UChar
>(__c),
1170 template<
typename _CharT,
typename _Out>
1172 __write_escaped(_Out __out, basic_string_view<_CharT> __str, _Term_char __term)
1174 *__out = _Escapes<_CharT>::_S_term(__term);
1177 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1178 __out = __format::__write_escaped_unicode(__out, __str, __term);
1179 else if constexpr (is_same_v<char, _CharT>
1180 && __unicode::__literal_encoding_is_extended_ascii())
1181 __out = __format::__write_escaped_ascii(__out, __str, __term);
1184 __out = __format::__write_escaped_ascii(__out, __str, __term);
1186 *__out = _Escapes<_CharT>::_S_term(__term);
1191 struct _Optional_locale
1193 [[__gnu__::__always_inline__]]
1194 _Optional_locale() : _M_dummy(), _M_hasval(false) { }
1196 _Optional_locale(
const locale& __loc) noexcept
1197 : _M_loc(__loc), _M_hasval(
true)
1200 _Optional_locale(
const _Optional_locale& __l) noexcept
1201 : _M_dummy(), _M_hasval(__l._M_hasval)
1204 std::construct_at(&_M_loc, __l._M_loc);
1208 operator=(
const _Optional_locale& __l)
noexcept
1213 _M_loc = __l._M_loc;
1220 else if (__l._M_hasval)
1222 std::construct_at(&_M_loc, __l._M_loc);
1228 ~_Optional_locale() {
if (_M_hasval) _M_loc.~locale(); }
1231 operator=(locale&& __loc)
noexcept
1237 std::construct_at(&_M_loc,
std::move(__loc));
1248 std::construct_at(&_M_loc);
1254 bool has_value() const noexcept {
return _M_hasval; }
1257 char _M_dummy =
'\0';
1260 bool _M_hasval =
false;
1263 template<__
char _CharT>
1264 struct __formatter_str
1266 __formatter_str() =
default;
1269 __formatter_str(_Spec<_CharT> __spec) noexcept
1273 constexpr typename basic_format_parse_context<_CharT>::iterator
1274 parse(basic_format_parse_context<_CharT>& __pc)
1276 auto __first = __pc.begin();
1277 const auto __last = __pc.end();
1278 _Spec<_CharT> __spec{};
1280 auto __finalize = [
this, &__spec] {
1284 auto __finished = [&] {
1285 if (__first == __last || *__first ==
'}')
1296 __first = __spec._M_parse_fill_and_align(__first, __last);
1300 __first = __spec._M_parse_width(__first, __last, __pc);
1304 __first = __spec._M_parse_precision(__first, __last, __pc);
1308 if (*__first ==
's')
1310#if __glibcxx_format_ranges
1311 else if (*__first ==
'?')
1313 __spec._M_type = _Pres_esc;
1321 __format::__failed_to_parse_format_spec();
1324 template<
typename _Out>
1326 format(basic_string_view<_CharT> __s,
1327 basic_format_context<_Out, _CharT>& __fc)
const
1329 constexpr auto __term = __format::_Term_char::_Tc_quote;
1330 const auto __write_direct = [&]
1332 if (_M_spec._M_type == _Pres_esc)
1333 return __format::__write_escaped(__fc.out(), __s, __term);
1335 return __format::__write(__fc.out(), __s);
1338 if (_M_spec._M_width_kind == _WP_none
1339 && _M_spec._M_prec_kind == _WP_none)
1340 return __write_direct();
1342 const size_t __prec =
1343 _M_spec._M_prec_kind != _WP_none
1344 ? _M_spec._M_get_precision(__fc)
1345 : basic_string_view<_CharT>::npos;
1347 const size_t __estimated_width = _S_trunc(__s, __prec);
1349 if (_M_spec._M_get_width(__fc) <= __estimated_width
1350 && _M_spec._M_prec_kind == _WP_none)
1351 return __write_direct();
1353 if (_M_spec._M_type != _Pres_esc)
1354 return __format::__write_padded_as_spec(__s, __estimated_width,
1357 __format::_Str_sink<_CharT> __sink;
1358 __format::__write_escaped(__sink.out(), __s, __term);
1359 basic_string_view<_CharT> __escaped(__sink.view().data(),
1360 __sink.view().size());
1361 const size_t __escaped_width = _S_trunc(__escaped, __prec);
1365 return __format::__write_padded_as_spec(__escaped, __escaped_width,
1369#if __glibcxx_format_ranges
1370 template<ranges::input_range _Rg,
typename _Out>
1371 requires same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _CharT>
1372 typename basic_format_context<_Out, _CharT>::iterator
1373 _M_format_range(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
1375 using _String = basic_string<_CharT>;
1376 using _String_view = basic_string_view<_CharT>;
1377 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
1379 const size_t __n(ranges::distance(__rg));
1380 if constexpr (ranges::contiguous_range<_Rg>)
1381 return format(_String_view(ranges::data(__rg), __n), __fc);
1382 else if (__n <= __format::__stackbuf_size<_CharT>)
1384 _CharT __buf[__format::__stackbuf_size<_CharT>];
1385 ranges::copy(__rg, __buf);
1386 return format(_String_view(__buf, __n), __fc);
1388 else if constexpr (ranges::sized_range<_Rg>)
1389 return format(_String(from_range, __rg), __fc);
1390 else if constexpr (ranges::random_access_range<_Rg>)
1392 ranges::iterator_t<_Rg> __first = ranges::begin(__rg);
1393 ranges::subrange __sub(__first, __first + __n);
1394 return format(_String(from_range, __sub), __fc);
1399 ranges::subrange __sub(__rg, __n);
1400 return format(_String(from_range, __sub), __fc);
1404 return format(_String(from_range, __rg), __fc);
1408 set_debug_format() noexcept
1409 { _M_spec._M_type = _Pres_esc; }
1414 _S_trunc(basic_string_view<_CharT>& __s,
size_t __prec)
1416 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
1418 if (__prec != basic_string_view<_CharT>::npos)
1419 return __unicode::__truncate(__s, __prec);
1421 return __unicode::__field_width(__s);
1425 __s = __s.substr(0, __prec);
1430 _Spec<_CharT> _M_spec{};
1433 template<__
char _CharT>
1434 struct __formatter_int
1438 static constexpr _Pres_type _AsInteger = _Pres_d;
1439 static constexpr _Pres_type _AsBool = _Pres_s;
1440 static constexpr _Pres_type _AsChar = _Pres_c;
1442 constexpr typename basic_format_parse_context<_CharT>::iterator
1443 _M_do_parse(basic_format_parse_context<_CharT>& __pc, _Pres_type __type)
1445 _Spec<_CharT> __spec{};
1446 __spec._M_type = __type;
1448 const auto __last = __pc.end();
1449 auto __first = __pc.begin();
1451 auto __finalize = [
this, &__spec] {
1455 auto __finished = [&] {
1456 if (__first == __last || *__first ==
'}')
1467 __first = __spec._M_parse_fill_and_align(__first, __last);
1471 __first = __spec._M_parse_sign(__first, __last);
1475 __first = __spec._M_parse_alternate_form(__first, __last);
1479 __first = __spec._M_parse_zero_fill(__first, __last);
1483 __first = __spec._M_parse_width(__first, __last, __pc);
1487 __first = __spec._M_parse_locale(__first, __last);
1494 __spec._M_type = _Pres_b;
1498 __spec._M_type = _Pres_B;
1504 if (__type != _AsBool)
1506 __spec._M_type = _Pres_c;
1511 __spec._M_type = _Pres_d;
1515 __spec._M_type = _Pres_o;
1519 __spec._M_type = _Pres_x;
1523 __spec._M_type = _Pres_X;
1527 if (__type == _AsBool)
1529 __spec._M_type = _Pres_s;
1533#if __glibcxx_format_ranges
1535 if (__type == _AsChar)
1537 __spec._M_type = _Pres_esc;
1547 __format::__failed_to_parse_format_spec();
1550 template<
typename _Tp>
1551 constexpr typename basic_format_parse_context<_CharT>::iterator
1552 _M_parse(basic_format_parse_context<_CharT>& __pc)
1554 if constexpr (is_same_v<_Tp, bool>)
1556 auto __end = _M_do_parse(__pc, _AsBool);
1557 if (_M_spec._M_type == _Pres_s)
1558 if (_M_spec._M_sign || _M_spec._M_alt || _M_spec._M_zero_fill)
1559 __throw_format_error(
"format error: format-spec contains "
1560 "invalid formatting options for "
1564 else if constexpr (__char<_Tp>)
1566 auto __end = _M_do_parse(__pc, _AsChar);
1567 if (_M_spec._M_type == _Pres_c || _M_spec._M_type == _Pres_esc)
1568 if (_M_spec._M_sign || _M_spec._M_alt || _M_spec._M_zero_fill
1570 __throw_format_error(
"format error: format-spec contains "
1571 "invalid formatting options for "
1576 return _M_do_parse(__pc, _AsInteger);
1579 template<
typename _Int,
typename _Out>
1580 typename basic_format_context<_Out, _CharT>::iterator
1581 format(_Int __i, basic_format_context<_Out, _CharT>& __fc)
const
1583 if (_M_spec._M_type == _Pres_c)
1584 return _M_format_character(_S_to_character(__i), __fc);
1586 char __buf[
sizeof(_Int) * __CHAR_BIT__ + 3];
1587 to_chars_result __res{};
1589 string_view __base_prefix;
1590 make_unsigned_t<_Int> __u;
1592 __u = -
static_cast<make_unsigned_t<_Int>
>(__i);
1596 char* __start = __buf + 3;
1597 char*
const __end = __buf +
sizeof(__buf);
1598 char*
const __start_digits = __start;
1600 switch (_M_spec._M_type)
1604 __base_prefix = _M_spec._M_type == _Pres_b ?
"0b" :
"0B";
1605 __res = to_chars(__start, __end, __u, 2);
1609 return _M_format_character(_S_to_character(__i), __fc);
1615 __res = to_chars(__start, __end, __u, 10);
1619 __base_prefix =
"0";
1620 __res = to_chars(__start, __end, __u, 8);
1624 __base_prefix = _M_spec._M_type == _Pres_x ?
"0x" :
"0X";
1625 __res = to_chars(__start, __end, __u, 16);
1626 if (_M_spec._M_type == _Pres_X)
1627 for (
auto __p = __start; __p != __res.ptr; ++__p)
1628#
if __has_builtin(__builtin_toupper)
1629 *__p = __builtin_toupper(*__p);
1635 __builtin_unreachable();
1638 if (_M_spec._M_alt && __base_prefix.size())
1640 __start -= __base_prefix.size();
1641 __builtin_memcpy(__start, __base_prefix.data(),
1642 __base_prefix.size());
1644 __start = __format::__put_sign(__i, _M_spec._M_sign, __start - 1);
1646 return _M_format_int(string_view(__start, __res.ptr - __start),
1647 __start_digits - __start, __fc);
1650 template<
typename _Out>
1651 typename basic_format_context<_Out, _CharT>::iterator
1652 format(
bool __i, basic_format_context<_Out, _CharT>& __fc)
const
1654 if (_M_spec._M_type == _Pres_c)
1655 return _M_format_character(
static_cast<unsigned char>(__i), __fc);
1656 if (_M_spec._M_type != _Pres_s)
1657 return format(
static_cast<unsigned char>(__i), __fc);
1659 basic_string<_CharT> __s;
1661 if (_M_spec._M_localized) [[unlikely]]
1664 __s = __i ? __np.truename() : __np.falsename();
1665 __est_width = __s.size();
1669 if constexpr (is_same_v<char, _CharT>)
1670 __s = __i ?
"true" :
"false";
1672 __s = __i ? L
"true" : L
"false";
1673 __est_width = __s.size();
1676 return __format::__write_padded_as_spec(__s, __est_width, __fc,
1680 [[__gnu__::__always_inline__]]
1682 _S_character_width(_CharT __c)
1685 if constexpr (
sizeof(_CharT) > 1u &&
1686 __unicode::__literal_encoding_is_unicode<_CharT>())
1687 return __unicode::__field_width(__c);
1692 template<
typename _Out>
1693 typename basic_format_context<_Out, _CharT>::iterator
1694 _M_format_character(_CharT __c,
1695 basic_format_context<_Out, _CharT>& __fc)
const
1697 return __format::__write_padded_as_spec({&__c, 1u},
1698 _S_character_width(__c),
1702 template<
typename _Out>
1703 typename basic_format_context<_Out, _CharT>::iterator
1704 _M_format_character_escaped(_CharT __c,
1705 basic_format_context<_Out, _CharT>& __fc)
const
1707 using _Esc = _Escapes<_CharT>;
1708 constexpr auto __term = __format::_Term_char::_Tc_apos;
1709 const basic_string_view<_CharT> __in(&__c, 1u);
1710 if (_M_spec._M_get_width(__fc) <= 3u)
1711 return __format::__write_escaped(__fc.out(), __in, __term);
1714 __format::_Fixedbuf_sink<_CharT> __sink(__buf);
1715 __format::__write_escaped(__sink.out(), __in, __term);
1717 const basic_string_view<_CharT> __escaped = __sink.view();
1718 size_t __estimated_width;
1719 if (__escaped[1] == _Esc::_S_bslash()[0])
1720 __estimated_width = __escaped.size();
1722 __estimated_width = 2 + _S_character_width(__c);
1723 return __format::__write_padded_as_spec(__escaped,
1728 template<
typename _Int>
1730 _S_to_character(_Int __i)
1733 if constexpr (is_signed_v<_Int> == is_signed_v<_CharT>)
1735 if (_Traits::__min <= __i && __i <= _Traits::__max)
1736 return static_cast<_CharT
>(__i);
1738 else if constexpr (is_signed_v<_Int>)
1740 if (__i >= 0 && make_unsigned_t<_Int>(__i) <= _Traits::__max)
1741 return static_cast<_CharT
>(__i);
1743 else if (__i <= make_unsigned_t<_CharT>(_Traits::__max))
1744 return static_cast<_CharT
>(__i);
1745 __throw_format_error(
"format error: integer not representable as "
1749 template<
typename _Out>
1750 typename basic_format_context<_Out, _CharT>::iterator
1751 _M_format_int(string_view __narrow_str,
size_t __prefix_len,
1752 basic_format_context<_Out, _CharT>& __fc)
const
1754 size_t __width = _M_spec._M_get_width(__fc);
1756 basic_string_view<_CharT> __str;
1757 if constexpr (is_same_v<char, _CharT>)
1758 __str = __narrow_str;
1759#ifdef _GLIBCXX_USE_WCHAR_T
1762 size_t __n = __narrow_str.size();
1763 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
1764 std::__to_wstring_numeric(__narrow_str.data(), __n, __p);
1769 if (_M_spec._M_localized)
1771 const auto& __l = __fc.locale();
1772 if (__l.name() !=
"C")
1774 auto& __np = use_facet<numpunct<_CharT>>(__l);
1775 string __grp = __np.grouping();
1778 size_t __n = __str.size() - __prefix_len;
1779 auto __p = (_CharT*)__builtin_alloca(2 * __n
1782 auto __s = __str.data();
1783 char_traits<_CharT>::copy(__p, __s, __prefix_len);
1784 __s += __prefix_len;
1785 auto __end = std::__add_grouping(__p + __prefix_len,
1786 __np.thousands_sep(),
1790 __str = {__p, size_t(__end - __p)};
1795 if (__width <= __str.size())
1796 return __format::__write(__fc.out(), __str);
1798 char32_t __fill_char = _M_spec._M_fill;
1799 _Align __align = _M_spec._M_align;
1801 size_t __nfill = __width - __str.size();
1802 auto __out = __fc.out();
1803 if (__align == _Align_default)
1805 __align = _Align_right;
1806 if (_M_spec._M_zero_fill)
1808 __fill_char = _CharT(
'0');
1810 if (__prefix_len != 0)
1812 __out = __format::__write(
std::move(__out),
1813 __str.substr(0, __prefix_len));
1814 __str.remove_prefix(__prefix_len);
1818 __fill_char = _CharT(
' ');
1820 return __format::__write_padded(
std::move(__out), __str,
1821 __align, __nfill, __fill_char);
1824#if defined __SIZEOF_INT128__ && defined __STRICT_ANSI__
1825 template<
typename _Tp>
1827 =
typename __conditional_t<(
sizeof(_Tp) <=
sizeof(
long long)),
1828 std::make_unsigned<_Tp>,
1829 type_identity<unsigned __int128>>::type;
1832 template<
typename _Int>
1833 static to_chars_result
1834 to_chars(
char* __first,
char* __last, _Int __value,
int __base)
1835 {
return std::__to_chars_i<_Int>(__first, __last, __value, __base); }
1838 _Spec<_CharT> _M_spec{};
1849#undef _GLIBCXX_FORMAT_F128
1851#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
1854 using __float128_t = __ieee128;
1855# define _GLIBCXX_FORMAT_F128 1
1857#ifdef __LONG_DOUBLE_IEEE128__
1861 to_chars(
char*,
char*, __ibm128)
noexcept
1862 __asm(
"_ZSt8to_charsPcS_e");
1865 to_chars(
char*,
char*, __ibm128, chars_format)
noexcept
1866 __asm(
"_ZSt8to_charsPcS_eSt12chars_format");
1869 to_chars(
char*,
char*, __ibm128, chars_format,
int)
noexcept
1870 __asm(
"_ZSt8to_charsPcS_eSt12chars_formati");
1871#elif __cplusplus == 202002L
1873 to_chars(
char*,
char*, __ieee128)
noexcept
1874 __asm(
"_ZSt8to_charsPcS_u9__ieee128");
1877 to_chars(
char*,
char*, __ieee128, chars_format)
noexcept
1878 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_format");
1881 to_chars(
char*,
char*, __ieee128, chars_format,
int)
noexcept
1882 __asm(
"_ZSt8to_charsPcS_u9__ieee128St12chars_formati");
1885#elif defined _GLIBCXX_LDOUBLE_IS_IEEE_BINARY128
1888 using __float128_t =
long double;
1889# define _GLIBCXX_FORMAT_F128 1
1891#elif __FLT128_DIG__ && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
1894 using __float128_t = _Float128;
1895# define _GLIBCXX_FORMAT_F128 2
1897# if __cplusplus == 202002L
1901 to_chars(
char*,
char*, _Float128)
noexcept
1902# if _GLIBCXX_INLINE_VERSION
1903 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_");
1905 __asm(
"_ZSt8to_charsPcS_DF128_");
1909 to_chars(
char*,
char*, _Float128, chars_format)
noexcept
1910# if _GLIBCXX_INLINE_VERSION
1911 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatE");
1913 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_format");
1917 to_chars(
char*,
char*, _Float128, chars_format,
int)
noexcept
1918# if _GLIBCXX_INLINE_VERSION
1919 __asm(
"_ZNSt3__88to_charsEPcS0_DF128_NS_12chars_formatEi");
1921 __asm(
"_ZSt8to_charsPcS_DF128_St12chars_formati");
1926 using std::to_chars;
1929 template<
typename _Tp>
1930 concept __formattable_float
1931 = is_same_v<remove_cv_t<_Tp>, _Tp> &&
requires (_Tp __t,
char* __p)
1932 { __format::to_chars(__p, __p, __t, chars_format::scientific, 6); };
1934 template<__
char _CharT>
1935 struct __formatter_fp
1937 constexpr typename basic_format_parse_context<_CharT>::iterator
1938 parse(basic_format_parse_context<_CharT>& __pc)
1940 _Spec<_CharT> __spec{};
1941 const auto __last = __pc.end();
1942 auto __first = __pc.begin();
1944 auto __finalize = [
this, &__spec] {
1948 auto __finished = [&] {
1949 if (__first == __last || *__first ==
'}')
1960 __first = __spec._M_parse_fill_and_align(__first, __last);
1964 __first = __spec._M_parse_sign(__first, __last);
1968 __first = __spec._M_parse_alternate_form(__first, __last);
1972 __first = __spec._M_parse_zero_fill(__first, __last);
1976 if (__first[0] !=
'.')
1978 __first = __spec._M_parse_width(__first, __last, __pc);
1983 __first = __spec._M_parse_precision(__first, __last, __pc);
1987 __first = __spec._M_parse_locale(__first, __last);
1994 __spec._M_type = _Pres_a;
1998 __spec._M_type = _Pres_A;
2002 __spec._M_type = _Pres_e;
2006 __spec._M_type = _Pres_E;
2010 __spec._M_type = _Pres_f;
2014 __spec._M_type = _Pres_F;
2018 __spec._M_type = _Pres_g;
2022 __spec._M_type = _Pres_G;
2030 __format::__failed_to_parse_format_spec();
2033 template<
typename _Fp,
typename _Out>
2034 typename basic_format_context<_Out, _CharT>::iterator
2035 format(_Fp __v, basic_format_context<_Out, _CharT>& __fc)
const
2039 to_chars_result __res{};
2042 bool __use_prec = _M_spec._M_prec_kind != _WP_none;
2044 __prec = _M_spec._M_get_precision(__fc);
2046 char* __start = __buf + 1;
2047 char* __end = __buf +
sizeof(__buf);
2050 bool __upper =
false;
2051 bool __trailing_zeros =
false;
2054 switch (_M_spec._M_type)
2061 if (_M_spec._M_type != _Pres_A)
2063 __fmt = chars_format::hex;
2071 __fmt = chars_format::scientific;
2078 __fmt = chars_format::fixed;
2085 __trailing_zeros =
true;
2087 __fmt = chars_format::general;
2091 __fmt = chars_format::general;
2094 __builtin_unreachable();
2098 auto __to_chars = [&](
char* __b,
char* __e) {
2100 return __format::to_chars(__b, __e, __v, __fmt, __prec);
2101 else if (__fmt != chars_format{})
2102 return __format::to_chars(__b, __e, __v, __fmt);
2104 return __format::to_chars(__b, __e, __v);
2108 __res = __to_chars(__start, __end);
2110 if (__builtin_expect(__res.ec == errc::value_too_large, 0))
2114 size_t __guess = 8 + __prec;
2115 if (__fmt == chars_format::fixed)
2117 if constexpr (is_same_v<_Fp, float> || is_same_v<_Fp, double>
2118 || is_same_v<_Fp, long double>)
2123 if constexpr (is_same_v<_Fp, float>)
2124 __builtin_frexpf(__v, &__exp);
2125 else if constexpr (is_same_v<_Fp, double>)
2126 __builtin_frexp(__v, &__exp);
2127 else if constexpr (is_same_v<_Fp, long double>)
2128 __builtin_frexpl(__v, &__exp);
2130 __guess += 1U + __exp * 4004U / 13301U;
2133 __guess += numeric_limits<_Fp>::max_exponent10;
2135 if (__guess <=
sizeof(__buf)) [[unlikely]]
2136 __guess =
sizeof(__buf) * 2;
2145 auto __overwrite = [&__to_chars, &__res] (
char* __p,
size_t __n)
2147 __res = __to_chars(__p + 1, __p + __n - 1);
2148 return __res.ec == errc{} ? __res.ptr - __p : 0;
2153 __start = __dynbuf.
data() + 1;
2154 __end = __dynbuf.
data() + __dynbuf.
size();
2156 while (__builtin_expect(__res.ec == errc::value_too_large, 0));
2162 for (
char* __p = __start; __p != __res.ptr; ++__p)
2166 bool __have_sign =
true;
2168 if (!__builtin_signbit(__v))
2170 if (_M_spec._M_sign == _Sign_plus)
2172 else if (_M_spec._M_sign == _Sign_space)
2175 __have_sign =
false;
2178 string_view __narrow_str(__start, __res.ptr - __start);
2182 if (_M_spec._M_alt && __builtin_isfinite(__v))
2184 string_view __s = __narrow_str;
2188 size_t __d = __s.find(
'.');
2189 if (__d != __s.npos)
2191 __p = __s.find(__expc, __d + 1);
2192 if (__p == __s.npos)
2196 if (__trailing_zeros)
2199 if (__s[__have_sign] !=
'0')
2201 __sigfigs = __p - __have_sign - 1;
2206 __sigfigs = __p - __s.find_first_not_of(
'0', __d + 1);
2211 __p = __s.find(__expc);
2212 if (__p == __s.npos)
2215 __sigfigs = __d - __have_sign;
2218 if (__trailing_zeros && __prec != 0)
2223 __z = __prec - __sigfigs;
2226 if (
size_t __extras =
int(__d == __p) + __z)
2228 if (__dynbuf.
empty() && __extras <=
size_t(__end - __res.ptr))
2232 __builtin_memmove(__start + __p + __extras,
2236 __start[__p++] =
'.';
2237 __builtin_memset(__start + __p,
'0', __z);
2238 __narrow_str = {__s.data(), __s.size() + __extras};
2242 __dynbuf.
reserve(__s.size() + __extras);
2243 if (__dynbuf.
empty())
2245 __dynbuf = __s.
substr(0, __p);
2249 __dynbuf.
append(__z,
'0');
2250 __dynbuf.
append(__s.substr(__p));
2254 __dynbuf.
insert(__p, __extras,
'0');
2256 __dynbuf[__p] =
'.';
2258 __narrow_str = __dynbuf;
2263 basic_string<_CharT> __wstr;
2264 basic_string_view<_CharT> __str;
2265 if constexpr (is_same_v<_CharT, char>)
2266 __str = __narrow_str;
2267#ifdef _GLIBCXX_USE_WCHAR_T
2270 __wstr = std::__to_wstring_numeric(__narrow_str);
2275 if (_M_spec._M_localized && __builtin_isfinite(__v))
2277 auto __s = _M_localize(__str, __expc, __fc.locale());
2282 size_t __width = _M_spec._M_get_width(__fc);
2284 if (__width <= __str.size())
2285 return __format::__write(__fc.out(), __str);
2287 char32_t __fill_char = _M_spec._M_fill;
2288 _Align __align = _M_spec._M_align;
2290 size_t __nfill = __width - __str.size();
2291 auto __out = __fc.out();
2292 if (__align == _Align_default)
2294 __align = _Align_right;
2295 if (_M_spec._M_zero_fill && __builtin_isfinite(__v))
2297 __fill_char = _CharT(
'0');
2299 if (!__format::__is_xdigit(__narrow_str[0]))
2301 *__out++ = __str[0];
2302 __str.remove_prefix(1);
2306 __fill_char = _CharT(
' ');
2308 return __format::__write_padded(
std::move(__out), __str,
2309 __align, __nfill, __fill_char);
2313 basic_string<_CharT>
2314 _M_localize(basic_string_view<_CharT> __str,
char __expc,
2315 const locale& __loc)
const
2317 basic_string<_CharT> __lstr;
2319 if (__loc == locale::classic())
2322 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
2323 const _CharT __point = __np.decimal_point();
2324 const string __grp = __np.grouping();
2326 _CharT __dot, __exp;
2327 if constexpr (is_same_v<_CharT, char>)
2350 __builtin_unreachable();
2354 if (__grp.empty() && __point == __dot)
2357 size_t __d = __str.find(__dot);
2358 size_t __e = min(__d, __str.find(__exp));
2359 if (__e == __str.npos)
2361 const size_t __r = __str.size() - __e;
2362 auto __overwrite = [&](_CharT* __p, size_t) {
2365 if (
auto __c = __str.front(); __c ==
'-' || __c ==
'+' || __c ==
' ')
2370 auto __end = std::__add_grouping(__p + __off, __np.thousands_sep(),
2371 __grp.data(), __grp.size(),
2372 __str.data() + __off,
2373 __str.data() + __e);
2376 if (__d != __str.npos)
2382 const size_t __rlen = __str.size() - __e;
2384 char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
2387 return (__end - __p);
2389 __lstr.__resize_and_overwrite(__e * 2 + __r, __overwrite);
2393 _Spec<_CharT> _M_spec{};
2400 template<__format::__
char _CharT>
2401 struct formatter<_CharT, _CharT>
2403 formatter() =
default;
2405 constexpr typename basic_format_parse_context<_CharT>::iterator
2406 parse(basic_format_parse_context<_CharT>& __pc)
2408 return _M_f.template _M_parse<_CharT>(__pc);
2411 template<
typename _Out>
2412 typename basic_format_context<_Out, _CharT>::iterator
2413 format(_CharT __u, basic_format_context<_Out, _CharT>& __fc)
const
2415 if (_M_f._M_spec._M_type == __format::_Pres_none
2416 || _M_f._M_spec._M_type == __format::_Pres_c)
2417 return _M_f._M_format_character(__u, __fc);
2418 else if (_M_f._M_spec._M_type == __format::_Pres_esc)
2419 return _M_f._M_format_character_escaped(__u, __fc);
2421 return _M_f.format(
static_cast<make_unsigned_t<_CharT>
>(__u), __fc);
2424#if __glibcxx_format_ranges
2426 set_debug_format() noexcept
2427 { _M_f._M_spec._M_type = __format::_Pres_esc; }
2431 __format::__formatter_int<_CharT> _M_f;
2434#ifdef _GLIBCXX_USE_WCHAR_T
2437 struct formatter<char, wchar_t>
2439 formatter() =
default;
2441 constexpr typename basic_format_parse_context<wchar_t>::iterator
2442 parse(basic_format_parse_context<wchar_t>& __pc)
2444 return _M_f._M_parse<
char>(__pc);
2447 template<
typename _Out>
2448 typename basic_format_context<_Out, wchar_t>::iterator
2449 format(
char __u, basic_format_context<_Out, wchar_t>& __fc)
const
2451 if (_M_f._M_spec._M_type == __format::_Pres_none
2452 || _M_f._M_spec._M_type == __format::_Pres_c)
2453 return _M_f._M_format_character(__u, __fc);
2454 else if (_M_f._M_spec._M_type == __format::_Pres_esc)
2455 return _M_f._M_format_character_escaped(__u, __fc);
2457 return _M_f.format(
static_cast<unsigned char>(__u), __fc);
2460#if __glibcxx_format_ranges
2462 set_debug_format() noexcept
2463 { _M_f._M_spec._M_type = __format::_Pres_esc; }
2467 __format::__formatter_int<wchar_t> _M_f;
2474 template<__format::__
char _CharT>
2475 struct formatter<_CharT*, _CharT>
2477 formatter() =
default;
2479 [[__gnu__::__always_inline__]]
2480 constexpr typename basic_format_parse_context<_CharT>::iterator
2481 parse(basic_format_parse_context<_CharT>& __pc)
2482 {
return _M_f.parse(__pc); }
2484 template<
typename _Out>
2485 [[__gnu__::__nonnull__]]
2486 typename basic_format_context<_Out, _CharT>::iterator
2487 format(_CharT* __u, basic_format_context<_Out, _CharT>& __fc)
const
2488 {
return _M_f.format(__u, __fc); }
2490#if __glibcxx_format_ranges
2491 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2495 __format::__formatter_str<_CharT> _M_f;
2498 template<__format::__
char _CharT>
2499 struct formatter<const _CharT*, _CharT>
2501 formatter() =
default;
2503 [[__gnu__::__always_inline__]]
2504 constexpr typename basic_format_parse_context<_CharT>::iterator
2505 parse(basic_format_parse_context<_CharT>& __pc)
2506 {
return _M_f.parse(__pc); }
2508 template<
typename _Out>
2509 [[__gnu__::__nonnull__]]
2510 typename basic_format_context<_Out, _CharT>::iterator
2511 format(
const _CharT* __u,
2512 basic_format_context<_Out, _CharT>& __fc)
const
2513 {
return _M_f.format(__u, __fc); }
2515#if __glibcxx_format_ranges
2516 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2520 __format::__formatter_str<_CharT> _M_f;
2523 template<__format::__
char _CharT,
size_t _Nm>
2524 struct formatter<_CharT[_Nm], _CharT>
2526 formatter() =
default;
2528 [[__gnu__::__always_inline__]]
2529 constexpr typename basic_format_parse_context<_CharT>::iterator
2530 parse(basic_format_parse_context<_CharT>& __pc)
2531 {
return _M_f.parse(__pc); }
2533 template<
typename _Out>
2534 typename basic_format_context<_Out, _CharT>::iterator
2535 format(
const _CharT (&__u)[_Nm],
2536 basic_format_context<_Out, _CharT>& __fc)
const
2537 {
return _M_f.format({__u, _Nm}, __fc); }
2539#if __glibcxx_format_ranges
2540 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2544 __format::__formatter_str<_CharT> _M_f;
2547 template<
typename _Traits,
typename _Alloc>
2548 struct formatter<
basic_string<char, _Traits, _Alloc>, char>
2550 formatter() =
default;
2552 [[__gnu__::__always_inline__]]
2553 constexpr typename basic_format_parse_context<char>::iterator
2554 parse(basic_format_parse_context<char>& __pc)
2555 {
return _M_f.parse(__pc); }
2557 template<
typename _Out>
2558 typename basic_format_context<_Out, char>::iterator
2559 format(
const basic_string<char, _Traits, _Alloc>& __u,
2560 basic_format_context<_Out, char>& __fc)
const
2561 {
return _M_f.format(__u, __fc); }
2563#if __glibcxx_format_ranges
2564 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2568 __format::__formatter_str<char> _M_f;
2571#ifdef _GLIBCXX_USE_WCHAR_T
2572 template<
typename _Traits,
typename _Alloc>
2573 struct formatter<
basic_string<wchar_t, _Traits, _Alloc>, wchar_t>
2575 formatter() =
default;
2577 [[__gnu__::__always_inline__]]
2578 constexpr typename basic_format_parse_context<wchar_t>::iterator
2579 parse(basic_format_parse_context<wchar_t>& __pc)
2580 {
return _M_f.parse(__pc); }
2582 template<
typename _Out>
2583 typename basic_format_context<_Out, wchar_t>::iterator
2584 format(
const basic_string<wchar_t, _Traits, _Alloc>& __u,
2585 basic_format_context<_Out, wchar_t>& __fc)
const
2586 {
return _M_f.format(__u, __fc); }
2588#if __glibcxx_format_ranges
2589 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2593 __format::__formatter_str<wchar_t> _M_f;
2597 template<
typename _Traits>
2600 formatter() =
default;
2602 [[__gnu__::__always_inline__]]
2603 constexpr typename basic_format_parse_context<char>::iterator
2604 parse(basic_format_parse_context<char>& __pc)
2605 {
return _M_f.parse(__pc); }
2607 template<
typename _Out>
2608 typename basic_format_context<_Out, char>::iterator
2609 format(basic_string_view<char, _Traits> __u,
2610 basic_format_context<_Out, char>& __fc)
const
2611 {
return _M_f.format(__u, __fc); }
2613#if __glibcxx_format_ranges
2614 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2618 __format::__formatter_str<char> _M_f;
2621#ifdef _GLIBCXX_USE_WCHAR_T
2622 template<
typename _Traits>
2625 formatter() =
default;
2627 [[__gnu__::__always_inline__]]
2628 constexpr typename basic_format_parse_context<wchar_t>::iterator
2629 parse(basic_format_parse_context<wchar_t>& __pc)
2630 {
return _M_f.parse(__pc); }
2632 template<
typename _Out>
2633 typename basic_format_context<_Out, wchar_t>::iterator
2634 format(basic_string_view<wchar_t, _Traits> __u,
2635 basic_format_context<_Out, wchar_t>& __fc)
const
2636 {
return _M_f.format(__u, __fc); }
2638#if __glibcxx_format_ranges
2639 constexpr void set_debug_format() noexcept { _M_f.set_debug_format(); }
2643 __format::__formatter_str<wchar_t> _M_f;
2653 template<
typename _Tp>
2654 constexpr bool __is_formattable_integer = __is_integer<_Tp>::__value;
2656#if defined __SIZEOF_INT128__
2657 template<>
inline constexpr bool __is_formattable_integer<__int128> =
true;
2658 template<>
inline constexpr bool __is_formattable_integer<unsigned __int128>
2662 template<>
inline constexpr bool __is_formattable_integer<char> =
false;
2663 template<>
inline constexpr bool __is_formattable_integer<wchar_t> =
false;
2664#ifdef _GLIBCXX_USE_CHAR8_T
2665 template<>
inline constexpr bool __is_formattable_integer<char8_t> =
false;
2667 template<>
inline constexpr bool __is_formattable_integer<char16_t> =
false;
2668 template<>
inline constexpr bool __is_formattable_integer<char32_t> =
false;
2673 template<
typename _Tp, __format::__
char _CharT>
2674 requires __format::__is_formattable_integer<_Tp>
2675 struct formatter<_Tp, _CharT>
2677 formatter() =
default;
2679 [[__gnu__::__always_inline__]]
2680 constexpr typename basic_format_parse_context<_CharT>::iterator
2681 parse(basic_format_parse_context<_CharT>& __pc)
2683 return _M_f.template _M_parse<_Tp>(__pc);
2686 template<
typename _Out>
2687 typename basic_format_context<_Out, _CharT>::iterator
2688 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2689 {
return _M_f.format(__u, __fc); }
2692 __format::__formatter_int<_CharT> _M_f;
2695#if defined __glibcxx_to_chars
2697 template<__format::__formattable_
float _Tp, __format::__
char _CharT>
2698 struct formatter<_Tp, _CharT>
2700 formatter() =
default;
2702 [[__gnu__::__always_inline__]]
2703 constexpr typename basic_format_parse_context<_CharT>::iterator
2704 parse(basic_format_parse_context<_CharT>& __pc)
2705 {
return _M_f.parse(__pc); }
2707 template<
typename _Out>
2708 typename basic_format_context<_Out, _CharT>::iterator
2709 format(_Tp __u, basic_format_context<_Out, _CharT>& __fc)
const
2710 {
return _M_f.format(__u, __fc); }
2713 __format::__formatter_fp<_CharT> _M_f;
2716#if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
2718 template<__format::__
char _CharT>
2719 struct formatter<long double, _CharT>
2721 formatter() =
default;
2723 [[__gnu__::__always_inline__]]
2724 constexpr typename basic_format_parse_context<_CharT>::iterator
2725 parse(basic_format_parse_context<_CharT>& __pc)
2726 {
return _M_f.parse(__pc); }
2728 template<
typename _Out>
2729 typename basic_format_context<_Out, _CharT>::iterator
2730 format(
long double __u, basic_format_context<_Out, _CharT>& __fc)
const
2731 {
return _M_f.format((
double)__u, __fc); }
2734 __format::__formatter_fp<_CharT> _M_f;
2738#if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2740 template<__format::__
char _CharT>
2741 struct formatter<_Float16, _CharT>
2743 formatter() =
default;
2745 [[__gnu__::__always_inline__]]
2746 constexpr typename basic_format_parse_context<_CharT>::iterator
2747 parse(basic_format_parse_context<_CharT>& __pc)
2748 {
return _M_f.parse(__pc); }
2750 template<
typename _Out>
2751 typename basic_format_context<_Out, _CharT>::iterator
2752 format(_Float16 __u, basic_format_context<_Out, _CharT>& __fc)
const
2753 {
return _M_f.format((
float)__u, __fc); }
2756 __format::__formatter_fp<_CharT> _M_f;
2760#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2762 template<__format::__
char _CharT>
2763 struct formatter<_Float32, _CharT>
2765 formatter() =
default;
2767 [[__gnu__::__always_inline__]]
2768 constexpr typename basic_format_parse_context<_CharT>::iterator
2769 parse(basic_format_parse_context<_CharT>& __pc)
2770 {
return _M_f.parse(__pc); }
2772 template<
typename _Out>
2773 typename basic_format_context<_Out, _CharT>::iterator
2774 format(_Float32 __u, basic_format_context<_Out, _CharT>& __fc)
const
2775 {
return _M_f.format((
float)__u, __fc); }
2778 __format::__formatter_fp<_CharT> _M_f;
2782#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2784 template<__format::__
char _CharT>
2785 struct formatter<_Float64, _CharT>
2787 formatter() =
default;
2789 [[__gnu__::__always_inline__]]
2790 constexpr typename basic_format_parse_context<_CharT>::iterator
2791 parse(basic_format_parse_context<_CharT>& __pc)
2792 {
return _M_f.parse(__pc); }
2794 template<
typename _Out>
2795 typename basic_format_context<_Out, _CharT>::iterator
2796 format(_Float64 __u, basic_format_context<_Out, _CharT>& __fc)
const
2797 {
return _M_f.format((
double)__u, __fc); }
2800 __format::__formatter_fp<_CharT> _M_f;
2804#if defined(__FLT128_DIG__) && _GLIBCXX_FORMAT_F128 == 1
2806 template<__format::__
char _CharT>
2807 struct formatter<_Float128, _CharT>
2809 formatter() =
default;
2811 [[__gnu__::__always_inline__]]
2812 constexpr typename basic_format_parse_context<_CharT>::iterator
2813 parse(basic_format_parse_context<_CharT>& __pc)
2814 {
return _M_f.parse(__pc); }
2816 template<
typename _Out>
2817 typename basic_format_context<_Out, _CharT>::iterator
2818 format(_Float128 __u, basic_format_context<_Out, _CharT>& __fc)
const
2819 {
return _M_f.format((__format::__float128_t)__u, __fc); }
2822 __format::__formatter_fp<_CharT> _M_f;
2826#if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2828 template<__format::__
char _CharT>
2829 struct formatter<__gnu_cxx::__bfloat16_t, _CharT>
2831 formatter() =
default;
2833 [[__gnu__::__always_inline__]]
2834 constexpr typename basic_format_parse_context<_CharT>::iterator
2835 parse(basic_format_parse_context<_CharT>& __pc)
2836 {
return _M_f.parse(__pc); }
2838 template<
typename _Out>
2839 typename basic_format_context<_Out, _CharT>::iterator
2840 format(__gnu_cxx::__bfloat16_t __u,
2841 basic_format_context<_Out, _CharT>& __fc)
const
2842 {
return _M_f.format((
float)__u, __fc); }
2845 __format::__formatter_fp<_CharT> _M_f;
2853 template<__format::__
char _CharT>
2854 struct formatter<const void*, _CharT>
2856 formatter() =
default;
2858 constexpr typename basic_format_parse_context<_CharT>::iterator
2859 parse(basic_format_parse_context<_CharT>& __pc)
2861 __format::_Spec<_CharT> __spec{};
2862 const auto __last = __pc.end();
2863 auto __first = __pc.begin();
2865 auto __finalize = [
this, &__spec] {
2869 auto __finished = [&] {
2870 if (__first == __last || *__first ==
'}')
2881 __first = __spec._M_parse_fill_and_align(__first, __last);
2887#if __glibcxx_format >= 202304L
2888 __first = __spec._M_parse_zero_fill(__first, __last);
2893 __first = __spec._M_parse_width(__first, __last, __pc);
2895 if (__first != __last)
2897 if (*__first ==
'p')
2899#if __glibcxx_format >= 202304L
2900 else if (*__first ==
'P')
2902 __spec._M_type = __format::_Pres_P;
2911 __format::__failed_to_parse_format_spec();
2914 template<
typename _Out>
2915 typename basic_format_context<_Out, _CharT>::iterator
2916 format(
const void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2918 auto __u =
reinterpret_cast<__UINTPTR_TYPE__
>(__v);
2919 char __buf[2 +
sizeof(__v) * 2];
2920 auto [__ptr, __ec] = std::to_chars(__buf + 2,
std::end(__buf),
2922 int __n = __ptr - __buf;
2925#if __glibcxx_format >= 202304L
2926 if (_M_spec._M_type == __format::_Pres_P)
2929 for (
auto __p = __buf + 2; __p != __ptr; ++__p)
2930#
if __has_builtin(__builtin_toupper)
2931 *__p = __builtin_toupper(*__p);
2939 if constexpr (is_same_v<_CharT, char>)
2940 __str = string_view(__buf, __n);
2941#ifdef _GLIBCXX_USE_WCHAR_T
2944 auto __p = (_CharT*)__builtin_alloca(__n *
sizeof(_CharT));
2945 std::__to_wstring_numeric(__buf, __n, __p);
2946 __str = wstring_view(__p, __n);
2950#if __glibcxx_format >= 202304L
2951 if (_M_spec._M_zero_fill)
2953 size_t __width = _M_spec._M_get_width(__fc);
2954 if (__width <= __str.size())
2955 return __format::__write(__fc.out(), __str);
2957 auto __out = __fc.out();
2959 __out = __format::__write(
std::move(__out), __str.substr(0, 2));
2960 __str.remove_prefix(2);
2961 size_t __nfill = __width - __n;
2962 return __format::__write_padded(
std::move(__out), __str,
2963 __format::_Align_right,
2964 __nfill, _CharT(
'0'));
2968 return __format::__write_padded_as_spec(__str, __n, __fc, _M_spec,
2969 __format::_Align_right);
2973 __format::_Spec<_CharT> _M_spec{};
2976 template<__format::__
char _CharT>
2977 struct formatter<void*, _CharT>
2979 formatter() =
default;
2981 [[__gnu__::__always_inline__]]
2982 constexpr typename basic_format_parse_context<_CharT>::iterator
2983 parse(basic_format_parse_context<_CharT>& __pc)
2984 {
return _M_f.parse(__pc); }
2986 template<
typename _Out>
2987 typename basic_format_context<_Out, _CharT>::iterator
2988 format(
void* __v, basic_format_context<_Out, _CharT>& __fc)
const
2989 {
return _M_f.format(__v, __fc); }
2992 formatter<const void*, _CharT> _M_f;
2995 template<__format::__
char _CharT>
2996 struct formatter<nullptr_t, _CharT>
2998 formatter() =
default;
3000 [[__gnu__::__always_inline__]]
3001 constexpr typename basic_format_parse_context<_CharT>::iterator
3002 parse(basic_format_parse_context<_CharT>& __pc)
3003 {
return _M_f.parse(__pc); }
3005 template<
typename _Out>
3006 typename basic_format_context<_Out, _CharT>::iterator
3007 format(nullptr_t, basic_format_context<_Out, _CharT>& __fc)
const
3008 {
return _M_f.format(
nullptr, __fc); }
3011 formatter<const void*, _CharT> _M_f;
3015#if defined _GLIBCXX_USE_WCHAR_T && __glibcxx_format_ranges
3019 struct __formatter_disabled
3021 __formatter_disabled() =
delete;
3022 __formatter_disabled(
const __formatter_disabled&) =
delete;
3023 __formatter_disabled& operator=(
const __formatter_disabled&) =
delete;
3027 struct formatter<char*,
wchar_t>
3028 :
private __formatter_disabled { };
3030 struct formatter<const char*,
wchar_t>
3031 :
private __formatter_disabled { };
3032 template<
size_t _Nm>
3033 struct formatter<char[_Nm], wchar_t>
3034 :
private __formatter_disabled { };
3035 template<
class _Traits,
class _Allocator>
3036 struct formatter<
basic_string<char, _Traits, _Allocator>, wchar_t>
3037 :
private __formatter_disabled { };
3038 template<
class _Traits>
3040 :
private __formatter_disabled { };
3045 template<
typename _Out>
3046 struct format_to_n_result
3049 iter_difference_t<_Out>
size;
3052_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3053template<
typename,
typename>
class vector;
3054_GLIBCXX_END_NAMESPACE_CONTAINER
3059 template<
typename _CharT>
3062 _Sink<_CharT>* _M_sink =
nullptr;
3065 using iterator_category = output_iterator_tag;
3066 using value_type = void;
3067 using difference_type = ptrdiff_t;
3068 using pointer = void;
3069 using reference = void;
3071 _Sink_iter() =
default;
3072 _Sink_iter(
const _Sink_iter&) =
default;
3073 _Sink_iter& operator=(
const _Sink_iter&) =
default;
3075 [[__gnu__::__always_inline__]]
3077 _Sink_iter(_Sink<_CharT>& __sink) : _M_sink(std::
addressof(__sink)) { }
3079 [[__gnu__::__always_inline__]]
3080 constexpr _Sink_iter&
3081 operator=(_CharT __c)
3083 _M_sink->_M_write(__c);
3087 [[__gnu__::__always_inline__]]
3088 constexpr _Sink_iter&
3089 operator=(basic_string_view<_CharT> __s)
3091 _M_sink->_M_write(__s);
3095 [[__gnu__::__always_inline__]]
3096 constexpr _Sink_iter&
3099 [[__gnu__::__always_inline__]]
3100 constexpr _Sink_iter&
3101 operator++() {
return *
this; }
3103 [[__gnu__::__always_inline__]]
3104 constexpr _Sink_iter
3105 operator++(
int) {
return *
this; }
3108 _M_reserve(
size_t __n)
const
3109 {
return _M_sink->_M_reserve(__n); }
3115 template<
typename _CharT>
3118 friend class _Sink_iter<_CharT>;
3120 span<_CharT> _M_span;
3121 typename span<_CharT>::iterator _M_next;
3127 virtual void _M_overflow() = 0;
3131 [[__gnu__::__always_inline__]]
3133 _Sink(span<_CharT> __span) noexcept
3134 : _M_span(__span), _M_next(__span.begin())
3138 [[__gnu__::__always_inline__]]
3140 _M_used() const noexcept
3141 {
return _M_span.first(_M_next - _M_span.begin()); }
3144 [[__gnu__::__always_inline__]]
3145 constexpr span<_CharT>
3146 _M_unused() const noexcept
3147 {
return _M_span.subspan(_M_next - _M_span.begin()); }
3150 [[__gnu__::__always_inline__]]
3152 _M_rewind() noexcept
3153 { _M_next = _M_span.begin(); }
3157 _M_reset(span<_CharT> __s,
size_t __pos = 0) noexcept
3160 _M_next = __s.begin() + __pos;
3165 _M_write(_CharT __c)
3168 if (_M_next - _M_span.begin() == std::ssize(_M_span)) [[unlikely]]
3173 _M_write(basic_string_view<_CharT> __s)
3175 span __to = _M_unused();
3176 while (__to.size() <= __s.size())
3178 __s.copy(__to.data(), __to.size());
3179 _M_next += __to.size();
3180 __s.remove_prefix(__to.size());
3186 __s.copy(__to.data(), __s.size());
3187 _M_next += __s.size();
3196 explicit operator bool() const noexcept {
return _M_sink; }
3198 _CharT* get() const noexcept {
return _M_sink->_M_next.operator->(); }
3200 void _M_bump(
size_t __n) { _M_sink->_M_bump(__n); }
3208 virtual _Reservation
3209 _M_reserve(
size_t __n)
3211 if (__n <= _M_unused().
size())
3214 if (__n <= _M_span.size())
3217 if (__n <= _M_unused().
size())
3230 _Sink(
const _Sink&) =
delete;
3231 _Sink& operator=(
const _Sink&) =
delete;
3233 [[__gnu__::__always_inline__]]
3234 constexpr _Sink_iter<_CharT>
3236 {
return _Sink_iter<_CharT>(*
this); }
3240 template<
typename _CharT>
3241 class _Fixedbuf_sink final :
public _Sink<_CharT>
3244 _M_overflow()
override
3246 __glibcxx_assert(
false);
3251 [[__gnu__::__always_inline__]]
3253 _Fixedbuf_sink(span<_CharT> __buf)
3254 : _Sink<_CharT>(__buf)
3257 constexpr basic_string_view<_CharT>
3260 auto __s = this->_M_used();
3261 return basic_string_view<_CharT>(__s.data(), __s.size());
3266 template<
typename _CharT>
3267 class _Buf_sink :
public _Sink<_CharT>
3270 _CharT _M_buf[__stackbuf_size<_CharT>];
3272 [[__gnu__::__always_inline__]]
3274 _Buf_sink() noexcept
3275 : _Sink<_CharT>(_M_buf)
3279 using _GLIBCXX_STD_C::vector;
3283 template<
typename _Seq>
3284 class _Seq_sink final :
public _Buf_sink<typename _Seq::value_type>
3286 using _CharT =
typename _Seq::value_type;
3292 _M_overflow()
override
3294 auto __s = this->_M_used();
3295 if (__s.empty()) [[unlikely]]
3299 _GLIBCXX_DEBUG_ASSERT(__s.data() != _M_seq.data());
3301 if constexpr (__is_specialization_of<_Seq, basic_string>)
3302 _M_seq.append(__s.data(), __s.size());
3304 _M_seq.insert(_M_seq.end(), __s.begin(), __s.end());
3310 typename _Sink<_CharT>::_Reservation
3311 _M_reserve(
size_t __n)
override
3320 if constexpr (__is_specialization_of<_Seq, basic_string>
3321 || __is_specialization_of<_Seq, vector>)
3324 if (this->_M_used().
size()) [[unlikely]]
3325 _Seq_sink::_M_overflow();
3328 const auto __sz = _M_seq.size();
3329 if constexpr (is_same_v<string, _Seq> || is_same_v<wstring, _Seq>)
3330 _M_seq.__resize_and_overwrite(__sz + __n,
3331 [](
auto,
auto __n2) {
3335 _M_seq.resize(__sz + __n);
3339 this->_M_reset(_M_seq, __sz);
3343 return _Sink<_CharT>::_M_reserve(__n);
3347 _M_bump(
size_t __n)
override
3349 if constexpr (__is_specialization_of<_Seq, basic_string>
3350 || __is_specialization_of<_Seq, vector>)
3352 auto __s = this->_M_used();
3353 _GLIBCXX_DEBUG_ASSERT(__s.data() == _M_seq.data());
3355 _M_seq.resize(__s.size() + __n);
3357 this->_M_reset(this->_M_buf);
3365 [[__gnu__::__always_inline__]]
3366 _Seq_sink() noexcept(is_nothrow_default_constructible_v<_Seq>)
3369 _Seq_sink(_Seq&& __s)
noexcept(is_nothrow_move_constructible_v<_Seq>)
3370 : _M_seq(std::
move(__s))
3373 using _Sink<_CharT>::out;
3378 if (this->_M_used().
size() != 0)
3379 _Seq_sink::_M_overflow();
3388 auto __s = this->_M_used();
3391 if (__s.size() != 0)
3392 _Seq_sink::_M_overflow();
3402 template<
typename _CharT,
typename _OutIter>
3403 class _Iter_sink :
public _Buf_sink<_CharT>
3406 iter_difference_t<_OutIter> _M_max;
3409 size_t _M_count = 0;
3412 _M_overflow()
override
3414 auto __s = this->_M_used();
3416 _M_out = ranges::copy(__s,
std::move(_M_out)).out;
3417 else if (_M_count <
static_cast<size_t>(_M_max))
3419 auto __max = _M_max - _M_count;
3420 span<_CharT> __first;
3421 if (__max < __s.size())
3422 __first = __s.first(
static_cast<size_t>(__max));
3425 _M_out = ranges::copy(__first,
std::move(_M_out)).out;
3428 _M_count += __s.size();
3432 [[__gnu__::__always_inline__]]
3434 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max = -1)
3435 : _M_out(std::
move(__out)), _M_max(__max)
3438 using _Sink<_CharT>::out;
3440 format_to_n_result<_OutIter>
3443 if (this->_M_used().
size() != 0)
3444 _Iter_sink::_M_overflow();
3445 iter_difference_t<_OutIter> __count(_M_count);
3457 template<
typename _CharT, contiguous_iterator _OutIter>
3458 requires same_as<iter_value_t<_OutIter>, _CharT>
3459 class _Iter_sink<_CharT, _OutIter> :
public _Sink<_CharT>
3462 iter_difference_t<_OutIter> _M_max = -1;
3464 size_t _M_count = 0;
3470 _M_overflow()
override
3472 if (this->_M_unused().
size() != 0)
3475 auto __s = this->_M_used();
3479 _M_count += __s.size();
3483 this->_M_reset(this->_M_buf);
3489 this->_M_reset({__s.data(), __s.size() + 1024}, __s.size());
3493 typename _Sink<_CharT>::_Reservation
3494 _M_reserve(
size_t __n)
final
3496 auto __avail = this->_M_unused();
3497 if (__n > __avail.size())
3502 auto __s = this->_M_used();
3503 this->_M_reset({__s.data(), __s.size() + __n}, __s.size());
3510 _S_make_span(_CharT* __ptr, iter_difference_t<_OutIter> __n,
3511 span<_CharT> __buf)
noexcept
3518 if constexpr (!is_integral_v<iter_difference_t<_OutIter>>
3519 ||
sizeof(__n) >
sizeof(size_t))
3522 auto __m = iter_difference_t<_OutIter>((
size_t)-1);
3526 return {__ptr, (size_t)__n};
3529#if __has_builtin(__builtin_dynamic_object_size)
3530 if (
size_t __bytes = __builtin_dynamic_object_size(__ptr, 2))
3531 return {__ptr, __bytes /
sizeof(_CharT)};
3534 const auto __off =
reinterpret_cast<__UINTPTR_TYPE__
>(__ptr) % 1024;
3535 __n = (1024 - __off) /
sizeof(_CharT);
3536 if (__n > 0) [[likely]]
3537 return {__ptr,
static_cast<size_t>(__n)};
3544 _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __n = -1) noexcept
3545 : _Sink<_CharT>(_S_make_span(std::to_address(__out), __n, _M_buf)),
3546 _M_first(__out), _M_max(__n)
3549 format_to_n_result<_OutIter>
3552 auto __s = this->_M_used();
3553 if (__s.data() == _M_buf)
3556 iter_difference_t<_OutIter> __count(_M_count + __s.size());
3557 return { _M_first + _M_max, __count };
3561 iter_difference_t<_OutIter> __count(__s.size());
3562 return { _M_first + __count, __count };
3567 enum _Arg_t :
unsigned char {
3568 _Arg_none, _Arg_bool, _Arg_c, _Arg_i, _Arg_u, _Arg_ll, _Arg_ull,
3569 _Arg_flt, _Arg_dbl, _Arg_ldbl, _Arg_str, _Arg_sv, _Arg_ptr, _Arg_handle,
3570 _Arg_i128, _Arg_u128,
3571 _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f64,
3572#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3574 _Arg_f128 = _Arg_ldbl,
3575 _Arg_ibm128 = _Arg_next_value_,
3582 template<
typename _Context>
3585 using _CharT =
typename _Context::char_type;
3601 unsigned long long _M_ull;
3604#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3605 long double _M_ldbl;
3607 const _CharT* _M_str;
3608 basic_string_view<_CharT> _M_sv;
3610 _HandleBase _M_handle;
3611#ifdef __SIZEOF_INT128__
3613 unsigned __int128 _M_u128;
3615#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3618#elif _GLIBCXX_FORMAT_F128 == 2
3619 __float128_t _M_f128;
3623 [[__gnu__::__always_inline__]]
3624 _Arg_value() : _M_none() { }
3627 template<
typename _Tp>
3628 _Arg_value(in_place_type_t<_Tp>, _Tp __val)
3629 { _S_get<_Tp>() = __val; }
3632 template<
typename _Tp,
typename _Self>
3633 [[__gnu__::__always_inline__]]
3635 _S_get(_Self& __u)
noexcept
3637 if constexpr (is_same_v<_Tp, bool>)
3639 else if constexpr (is_same_v<_Tp, _CharT>)
3641 else if constexpr (is_same_v<_Tp, int>)
3643 else if constexpr (is_same_v<_Tp, unsigned>)
3645 else if constexpr (is_same_v<_Tp, long long>)
3647 else if constexpr (is_same_v<_Tp, unsigned long long>)
3649 else if constexpr (is_same_v<_Tp, float>)
3651 else if constexpr (is_same_v<_Tp, double>)
3653#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3654 else if constexpr (is_same_v<_Tp, long double>)
3657 else if constexpr (is_same_v<_Tp, __ieee128>)
3659 else if constexpr (is_same_v<_Tp, __ibm128>)
3660 return __u._M_ibm128;
3662 else if constexpr (is_same_v<_Tp, const _CharT*>)
3664 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
3666 else if constexpr (is_same_v<_Tp, const void*>)
3668#ifdef __SIZEOF_INT128__
3669 else if constexpr (is_same_v<_Tp, __int128>)
3671 else if constexpr (is_same_v<_Tp, unsigned __int128>)
3674#if _GLIBCXX_FORMAT_F128 == 2
3675 else if constexpr (is_same_v<_Tp, __float128_t>)
3678 else if constexpr (derived_from<_Tp, _HandleBase>)
3679 return static_cast<_Tp&
>(__u._M_handle);
3683 template<
typename _Tp>
3684 [[__gnu__::__always_inline__]]
3687 {
return _S_get<_Tp>(*
this); }
3689 template<
typename _Tp>
3690 [[__gnu__::__always_inline__]]
3692 _M_get() const noexcept
3693 {
return _S_get<_Tp>(*
this); }
3695 template<
typename _Tp>
3696 [[__gnu__::__always_inline__]]
3698 _M_set(_Tp __v)
noexcept
3700 if constexpr (derived_from<_Tp, _HandleBase>)
3701 std::construct_at(&_M_handle, __v);
3703 _S_get<_Tp>(*
this) = __v;
3708 template<
typename _Context,
typename... _Args>
3711 template<
typename _Visitor,
typename _Ctx>
3712 decltype(
auto) __visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
3714 template<
typename _Ch,
typename _Tp>
3716 __to_arg_t_enum() noexcept;
3720 template<typename _Context>
3721 class basic_format_arg
3723 using _CharT =
typename _Context::char_type;
3725 template<
typename _Tp>
3726 static constexpr bool __formattable
3727 = __format::__formattable_with<_Tp, _Context>;
3730 class handle :
public __format::_Arg_value<_Context>::_HandleBase
3732 using _Base =
typename __format::_Arg_value<_Context>::_HandleBase;
3735 template<
typename _Tp>
3736 using __maybe_const_t
3737 = __conditional_t<__formattable<const _Tp>,
const _Tp, _Tp>;
3739 template<
typename _Tq>
3741 _S_format(basic_format_parse_context<_CharT>& __parse_ctx,
3742 _Context& __format_ctx,
const void* __ptr)
3744 using _Td = remove_const_t<_Tq>;
3745 typename _Context::template formatter_type<_Td> __f;
3746 __parse_ctx.advance_to(__f.parse(__parse_ctx));
3747 _Tq& __val = *
const_cast<_Tq*
>(
static_cast<const _Td*
>(__ptr));
3748 __format_ctx.advance_to(__f.format(__val, __format_ctx));
3751 template<
typename _Tp>
3753 handle(_Tp& __val)
noexcept
3755 this->_M_ptr = __builtin_addressof(__val);
3756 auto __func = _S_format<__maybe_const_t<_Tp>>;
3757 this->_M_func =
reinterpret_cast<void(*)()
>(__func);
3760 friend class basic_format_arg<_Context>;
3763 handle(
const handle&) =
default;
3764 handle& operator=(
const handle&) =
default;
3766 [[__gnu__::__always_inline__]]
3768 format(basic_format_parse_context<_CharT>& __pc, _Context& __fc)
const
3770 using _Func = void(*)(basic_format_parse_context<_CharT>&,
3771 _Context&,
const void*);
3772 auto __f =
reinterpret_cast<_Func
>(this->_M_func);
3773 __f(__pc, __fc, this->_M_ptr);
3777 [[__gnu__::__always_inline__]]
3778 basic_format_arg() noexcept : _M_type(__format::_Arg_none) { }
3780 [[nodiscard,__gnu__::__always_inline__]]
3781 explicit operator bool() const noexcept
3782 {
return _M_type != __format::_Arg_none; }
3784#if __cpp_lib_format >= 202306L
3785 template<
typename _Visitor>
3787 visit(
this basic_format_arg __arg, _Visitor&& __vis)
3790 template<
typename _Res,
typename _Visitor>
3792 visit(
this basic_format_arg __arg, _Visitor&& __vis)
3797 template<
typename _Ctx>
3798 friend class basic_format_args;
3800 template<
typename _Ctx,
typename... _Args>
3801 friend class __format::_Arg_store;
3803 static_assert(is_trivially_copyable_v<__format::_Arg_value<_Context>>);
3805 __format::_Arg_value<_Context> _M_val;
3806 __format::_Arg_t _M_type;
3811 template<
typename _Tp>
3812 static consteval auto
3815 using _Td = remove_const_t<_Tp>;
3816 if constexpr (is_same_v<_Td, bool>)
3817 return type_identity<bool>();
3818 else if constexpr (is_same_v<_Td, _CharT>)
3819 return type_identity<_CharT>();
3820 else if constexpr (is_same_v<_Td, char> && is_same_v<_CharT, wchar_t>)
3821 return type_identity<_CharT>();
3822#ifdef __SIZEOF_INT128__
3823 else if constexpr (is_same_v<_Td, __int128>)
3824 return type_identity<__int128>();
3825 else if constexpr (is_same_v<_Td, unsigned __int128>)
3826 return type_identity<unsigned __int128>();
3828 else if constexpr (__is_signed_integer<_Td>::value)
3830 if constexpr (
sizeof(_Td) <=
sizeof(int))
3831 return type_identity<int>();
3832 else if constexpr (
sizeof(_Td) <=
sizeof(
long long))
3833 return type_identity<long long>();
3835 else if constexpr (__is_unsigned_integer<_Td>::value)
3837 if constexpr (
sizeof(_Td) <=
sizeof(unsigned))
3838 return type_identity<unsigned>();
3839 else if constexpr (
sizeof(_Td) <=
sizeof(
unsigned long long))
3840 return type_identity<unsigned long long>();
3842 else if constexpr (is_same_v<_Td, float>)
3843 return type_identity<float>();
3844 else if constexpr (is_same_v<_Td, double>)
3845 return type_identity<double>();
3846#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3847 else if constexpr (is_same_v<_Td, long double>)
3848 return type_identity<long double>();
3850 else if constexpr (is_same_v<_Td, __ibm128>)
3851 return type_identity<__ibm128>();
3852 else if constexpr (is_same_v<_Td, __ieee128>)
3853 return type_identity<__ieee128>();
3856#if defined(__FLT16_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3857 else if constexpr (is_same_v<_Td, _Float16>)
3858 return type_identity<float>();
3861#if defined(__BFLT16_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3862 else if constexpr (is_same_v<_Td,
decltype(0.0bf16)>)
3863 return type_identity<float>();
3866#if defined(__FLT32_DIG__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
3867 else if constexpr (is_same_v<_Td, _Float32>)
3868 return type_identity<float>();
3871#if defined(__FLT64_DIG__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
3872 else if constexpr (is_same_v<_Td, _Float64>)
3873 return type_identity<double>();
3876#if _GLIBCXX_FORMAT_F128
3878 else if constexpr (is_same_v<_Td, _Float128>)
3879 return type_identity<__format::__float128_t>();
3881# if __SIZEOF_FLOAT128__
3882 else if constexpr (is_same_v<_Td, __float128>)
3883 return type_identity<__format::__float128_t>();
3886 else if constexpr (__is_specialization_of<_Td, basic_string_view>
3887 || __is_specialization_of<_Td, basic_string>)
3889 if constexpr (is_same_v<typename _Td::value_type, _CharT>)
3890 return type_identity<basic_string_view<_CharT>>();
3892 return type_identity<handle>();
3894 else if constexpr (is_same_v<decay_t<_Td>,
const _CharT*>)
3895 return type_identity<const _CharT*>();
3896 else if constexpr (is_same_v<decay_t<_Td>, _CharT*>)
3897 return type_identity<const _CharT*>();
3898 else if constexpr (is_void_v<remove_pointer_t<_Td>>)
3899 return type_identity<const void*>();
3900 else if constexpr (is_same_v<_Td, nullptr_t>)
3901 return type_identity<const void*>();
3903 return type_identity<handle>();
3907 template<
typename _Tp>
3908 using _Normalize =
typename decltype(_S_to_arg_type<_Tp>())::type;
3911 template<
typename _Tp>
3912 static consteval __format::_Arg_t
3915 using namespace __format;
3916 if constexpr (is_same_v<_Tp, bool>)
3918 else if constexpr (is_same_v<_Tp, _CharT>)
3920 else if constexpr (is_same_v<_Tp, int>)
3922 else if constexpr (is_same_v<_Tp, unsigned>)
3924 else if constexpr (is_same_v<_Tp, long long>)
3926 else if constexpr (is_same_v<_Tp, unsigned long long>)
3928 else if constexpr (is_same_v<_Tp, float>)
3930 else if constexpr (is_same_v<_Tp, double>)
3932#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
3933 else if constexpr (is_same_v<_Tp, long double>)
3937 else if constexpr (is_same_v<_Tp, __ibm128>)
3939 else if constexpr (is_same_v<_Tp, __ieee128>)
3942 else if constexpr (is_same_v<_Tp, const _CharT*>)
3944 else if constexpr (is_same_v<_Tp, basic_string_view<_CharT>>)
3946 else if constexpr (is_same_v<_Tp, const void*>)
3948#ifdef __SIZEOF_INT128__
3949 else if constexpr (is_same_v<_Tp, __int128>)
3951 else if constexpr (is_same_v<_Tp, unsigned __int128>)
3955#if _GLIBCXX_FORMAT_F128 == 2
3956 else if constexpr (is_same_v<_Tp, __format::__float128_t>)
3959 else if constexpr (is_same_v<_Tp, handle>)
3963 template<
typename _Tp>
3965 _M_set(_Tp __v)
noexcept
3967 _M_type = _S_to_enum<_Tp>();
3971 template<
typename _Tp>
3972 requires __format::__formattable_with<_Tp, _Context>
3974 basic_format_arg(_Tp& __v)
noexcept
3976 using _Td = _Normalize<_Tp>;
3977 if constexpr (is_same_v<_Td, basic_string_view<_CharT>>)
3978 _M_set(_Td{__v.data(), __v.size()});
3979 else if constexpr (is_same_v<remove_const_t<_Tp>,
char>
3980 && is_same_v<_CharT, wchar_t>)
3981 _M_set(
static_cast<_Td
>(
static_cast<unsigned char>(__v)));
3983 _M_set(
static_cast<_Td
>(__v));
3986 template<
typename _Ctx,
typename... _Argz>
3988 make_format_args(_Argz&...)
noexcept;
3990 template<
typename _Visitor,
typename _Ctx>
3991 friend decltype(
auto)
3992 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx>);
3994 template<
typename _Visitor,
typename _Ctx>
3995 friend decltype(
auto)
3996 __format::__visit_format_arg(_Visitor&&, basic_format_arg<_Ctx>);
3998 template<
typename _Ch,
typename _Tp>
3999 friend consteval __format::_Arg_t
4000 __format::__to_arg_t_enum() noexcept;
4002 template<typename _Visitor>
4004 _M_visit(_Visitor&& __vis, __format::_Arg_t __type)
4006 using namespace __format;
4023#if __glibcxx_to_chars
4028#ifndef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
4046 auto& __h =
static_cast<handle&
>(_M_val._M_handle);
4049#ifdef __SIZEOF_INT128__
4056#if _GLIBCXX_FORMAT_F128 == 2
4063 __builtin_unreachable();
4067 template<
typename _Visitor>
4069 _M_visit_user(_Visitor&& __vis, __format::_Arg_t __type)
4071 return _M_visit([&__vis]<
typename _Tp>(_Tp& __val) ->
decltype(
auto)
4073 constexpr bool __user_facing = __is_one_of<_Tp,
4074 monostate, bool, _CharT,
4075 int,
unsigned int,
long long int,
unsigned long long int,
4076 float, double,
long double,
4077 const _CharT*, basic_string_view<_CharT>,
4078 const void*, handle>::value;
4079 if constexpr (__user_facing)
4090 template<
typename _Visitor,
typename _Context>
4091 _GLIBCXX26_DEPRECATED_SUGGEST(
"std::basic_format_arg::visit")
4092 inline decltype(auto)
4093 visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg)
4101 template<
typename _Visitor,
typename _Ctx>
4102 inline decltype(
auto)
4103 __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Ctx> __arg)
4108 struct _WidthPrecVisitor
4110 template<
typename _Tp>
4112 operator()(_Tp& __arg)
const
4114 if constexpr (is_same_v<_Tp, monostate>)
4115 __format::__invalid_arg_id_in_format_string();
4119 else if constexpr (
sizeof(_Tp) <=
sizeof(
long long))
4123 if constexpr (__is_unsigned_integer<_Tp>::value)
4125 else if constexpr (__is_signed_integer<_Tp>::value)
4129 __throw_format_error(
"format error: argument used for width or "
4130 "precision must be a non-negative integer");
4134#pragma GCC diagnostic push
4135#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4136 template<
typename _Context>
4138 __int_from_arg(
const basic_format_arg<_Context>& __arg)
4139 {
return __format::__visit_format_arg(_WidthPrecVisitor(), __arg); }
4142 template<
int _Bits,
size_t _Nm>
4144 __pack_arg_types(
const array<_Arg_t, _Nm>& __types)
4146 __UINT64_TYPE__ __packed_types = 0;
4147 for (
auto __i = __types.rbegin(); __i != __types.rend(); ++__i)
4148 __packed_types = (__packed_types << _Bits) | *__i;
4149 return __packed_types;
4154 template<
typename _Context>
4155 class basic_format_args
4157 static constexpr int _S_packed_type_bits = 5;
4158 static constexpr int _S_packed_type_mask = 0b11111;
4159 static constexpr int _S_max_packed_args = 12;
4161 static_assert( __format::_Arg_max_ <= (1 << _S_packed_type_bits) );
4163 template<
typename... _Args>
4164 using _Store = __format::_Arg_store<_Context, _Args...>;
4166 template<
typename _Ctx,
typename... _Args>
4167 friend class __format::_Arg_store;
4169 using uint64_t = __UINT64_TYPE__;
4170 using _Format_arg = basic_format_arg<_Context>;
4171 using _Format_arg_val = __format::_Arg_value<_Context>;
4177 uint64_t _M_packed_size : 4;
4178 uint64_t _M_unpacked_size : 60;
4181 const _Format_arg_val* _M_values;
4182 const _Format_arg* _M_args;
4186 _M_size() const noexcept
4187 {
return _M_packed_size ? _M_packed_size : _M_unpacked_size; }
4189 typename __format::_Arg_t
4190 _M_type(
size_t __i)
const noexcept
4192 uint64_t __t = _M_unpacked_size >> (__i * _S_packed_type_bits);
4193 return static_cast<__format::_Arg_t
>(__t & _S_packed_type_mask);
4196 template<
typename _Ctx,
typename... _Args>
4198 make_format_args(_Args&...)
noexcept;
4201 template<
typename... _Args>
4202 static consteval array<__format::_Arg_t,
sizeof...(_Args)>
4204 {
return {_Format_arg::template _S_to_enum<_Args>()...}; }
4207 template<
typename... _Args>
4208 basic_format_args(
const _Store<_Args...>& __store)
noexcept;
4210 [[nodiscard,__gnu__::__always_inline__]]
4211 basic_format_arg<_Context>
4212 get(
size_t __i)
const noexcept
4214 basic_format_arg<_Context> __arg;
4215 if (__i < _M_packed_size)
4217 __arg._M_type = _M_type(__i);
4218 __arg._M_val = _M_values[__i];
4220 else if (_M_packed_size == 0 && __i < _M_unpacked_size)
4221 __arg = _M_args[__i];
4228 template<
typename _Context,
typename... _Args>
4229 basic_format_args(__format::_Arg_store<_Context, _Args...>)
4230 -> basic_format_args<_Context>;
4232 template<
typename _Context,
typename... _Args>
4234 make_format_args(_Args&... __fmt_args)
noexcept;
4237 template<
typename _Context,
typename... _Args>
4238 class __format::_Arg_store
4240 friend std::basic_format_args<_Context>;
4242 template<
typename _Ctx,
typename... _Argz>
4244#if _GLIBCXX_INLINE_VERSION
4247 make_format_args(_Argz&...)
noexcept;
4251 static constexpr bool _S_values_only
4252 =
sizeof...(_Args) <= basic_format_args<_Context>::_S_max_packed_args;
4255 = __conditional_t<_S_values_only,
4256 __format::_Arg_value<_Context>,
4257 basic_format_arg<_Context>>;
4259 _Element_t _M_args[
sizeof...(_Args)];
4261 template<
typename _Tp>
4263 _S_make_elt(_Tp& __v)
4265 using _Tq = remove_const_t<_Tp>;
4266 using _CharT =
typename _Context::char_type;
4267 static_assert(is_default_constructible_v<formatter<_Tq, _CharT>>,
4268 "std::formatter must be specialized for the type "
4269 "of each format arg");
4270 using __format::__formattable_with;
4271 if constexpr (is_const_v<_Tp>)
4272 if constexpr (!__formattable_with<_Tp, _Context>)
4273 if constexpr (__formattable_with<_Tq, _Context>)
4274 static_assert(__formattable_with<_Tp, _Context>,
4275 "format arg must be non-const because its "
4276 "std::formatter specialization has a "
4277 "non-const reference parameter");
4278 basic_format_arg<_Context> __arg(__v);
4279 if constexpr (_S_values_only)
4280 return __arg._M_val;
4285 template<
typename... _Tp>
4286 requires (
sizeof...(_Tp) ==
sizeof...(_Args))
4287 [[__gnu__::__always_inline__]]
4288 _Arg_store(_Tp&... __a) noexcept
4289 : _M_args{_S_make_elt(__a)...}
4293 template<
typename _Context>
4294 class __format::_Arg_store<_Context>
4297 template<
typename _Context>
4298 template<
typename... _Args>
4300 basic_format_args<_Context>::
4301 basic_format_args(
const _Store<_Args...>& __store)
noexcept
4303 if constexpr (
sizeof...(_Args) == 0)
4306 _M_unpacked_size = 0;
4309 else if constexpr (
sizeof...(_Args) <= _S_max_packed_args)
4312 _M_packed_size =
sizeof...(_Args);
4315 = __format::__pack_arg_types<_S_packed_type_bits>(_S_types_to_pack<_Args...>());
4317 _M_values = __store._M_args;
4324 _M_unpacked_size =
sizeof...(_Args);
4326 _M_args = __store._M_args;
4331 template<
typename _Context = format_context,
typename... _Args>
4332 [[nodiscard,__gnu__::__always_inline__]]
4334 make_format_args(_Args&... __fmt_args)
noexcept
4336 using _Fmt_arg = basic_format_arg<_Context>;
4337 using _Store = __format::_Arg_store<_Context,
typename _Fmt_arg::template
4338 _Normalize<_Args>...>;
4339 return _Store(__fmt_args...);
4342#ifdef _GLIBCXX_USE_WCHAR_T
4344 template<
typename... _Args>
4345 [[nodiscard,__gnu__::__always_inline__]]
4347 make_wformat_args(_Args&... __args)
noexcept
4348 {
return std::make_format_args<wformat_context>(__args...); }
4354 template<
typename _Out,
typename _CharT,
typename _Context>
4356 __do_vformat_to(_Out, basic_string_view<_CharT>,
4357 const basic_format_args<_Context>&,
4358 const locale* =
nullptr);
4360 template<
typename _CharT>
struct __formatter_chrono;
4378 template<
typename _Out,
typename _CharT>
4379 class basic_format_context
4381 static_assert( output_iterator<_Out, const _CharT&> );
4383 basic_format_args<basic_format_context> _M_args;
4385 __format::_Optional_locale _M_loc;
4387 basic_format_context(basic_format_args<basic_format_context> __args,
4389 : _M_args(__args), _M_out(std::
move(__out))
4392 basic_format_context(basic_format_args<basic_format_context> __args,
4393 _Out __out,
const std::locale& __loc)
4394 : _M_args(__args), _M_out(std::
move(__out)), _M_loc(__loc)
4400 basic_format_context(
const basic_format_context&) =
delete;
4401 basic_format_context& operator=(
const basic_format_context&) =
delete;
4403 template<
typename _Out2,
typename _CharT2,
typename _Context2>
4405 __format::__do_vformat_to(_Out2, basic_string_view<_CharT2>,
4406 const basic_format_args<_Context2>&,
4409 friend __format::__formatter_chrono<_CharT>;
4412 ~basic_format_context() =
default;
4414 using iterator = _Out;
4415 using char_type = _CharT;
4416 template<
typename _Tp>
4417 using formatter_type = formatter<_Tp, _CharT>;
4420 basic_format_arg<basic_format_context>
4421 arg(
size_t __id)
const noexcept
4422 {
return _M_args.get(__id); }
4425 std::locale locale() {
return _M_loc.value(); }
4428 iterator out() {
return std::move(_M_out); }
4430 void advance_to(iterator __it) { _M_out =
std::move(__it); }
4442 template<
typename _CharT>
4445 using iterator =
typename basic_format_parse_context<_CharT>::iterator;
4447 struct _Parse_context : basic_format_parse_context<_CharT>
4449 using basic_format_parse_context<_CharT>::basic_format_parse_context;
4450 const _Arg_t* _M_types =
nullptr;
4454 _Scanner(basic_string_view<_CharT> __str,
size_t __nargs = (
size_t)-1)
4455 : _M_pc(__str, __nargs)
4458 constexpr iterator
begin() const noexcept {
return _M_pc.begin(); }
4459 constexpr iterator
end() const noexcept {
return _M_pc.end(); }
4464 basic_string_view<_CharT> __fmt = _M_fmt_str();
4466 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
4468 _M_pc.advance_to(
begin() + 1);
4469 _M_format_arg(_M_pc.next_arg_id());
4473 size_t __lbr = __fmt.find(
'{');
4474 size_t __rbr = __fmt.find(
'}');
4476 while (__fmt.size())
4478 auto __cmp = __lbr <=> __rbr;
4482 _M_pc.advance_to(
end());
4487 if (__lbr + 1 == __fmt.size()
4488 || (__rbr == __fmt.npos && __fmt[__lbr + 1] !=
'{'))
4489 __format::__unmatched_left_brace_in_format_string();
4490 const bool __is_escape = __fmt[__lbr + 1] ==
'{';
4491 iterator __last =
begin() + __lbr + int(__is_escape);
4492 _M_on_chars(__last);
4493 _M_pc.advance_to(__last + 1);
4494 __fmt = _M_fmt_str();
4497 if (__rbr != __fmt.npos)
4499 __lbr = __fmt.find(
'{');
4503 _M_on_replacement_field();
4504 __fmt = _M_fmt_str();
4505 __lbr = __fmt.find(
'{');
4506 __rbr = __fmt.find(
'}');
4511 if (++__rbr == __fmt.size() || __fmt[__rbr] !=
'}')
4512 __format::__unmatched_right_brace_in_format_string();
4513 iterator __last =
begin() + __rbr;
4514 _M_on_chars(__last);
4515 _M_pc.advance_to(__last + 1);
4516 __fmt = _M_fmt_str();
4517 if (__lbr != __fmt.npos)
4519 __rbr = __fmt.find(
'}');
4524 constexpr basic_string_view<_CharT>
4525 _M_fmt_str() const noexcept
4528 constexpr virtual void _M_on_chars(iterator) { }
4530 constexpr void _M_on_replacement_field()
4532 auto __next =
begin();
4536 __id = _M_pc.next_arg_id();
4537 else if (*__next ==
':')
4539 __id = _M_pc.next_arg_id();
4540 _M_pc.advance_to(++__next);
4544 auto [__i, __ptr] = __format::__parse_arg_id(
begin(),
end());
4545 if (!__ptr || !(*__ptr ==
'}' || *__ptr ==
':'))
4546 __format::__invalid_arg_id_in_format_string();
4547 _M_pc.check_arg_id(__id = __i);
4550 _M_pc.advance_to(++__ptr);
4553 _M_pc.advance_to(__ptr);
4555 _M_format_arg(__id);
4557 __format::__unmatched_left_brace_in_format_string();
4558 _M_pc.advance_to(
begin() + 1);
4561 constexpr virtual void _M_format_arg(
size_t __id) = 0;
4565 template<
typename _Out,
typename _CharT>
4566 class _Formatting_scanner :
public _Scanner<_CharT>
4569 _Formatting_scanner(basic_format_context<_Out, _CharT>& __fc,
4570 basic_string_view<_CharT> __str)
4571 : _Scanner<_CharT>(__str), _M_fc(__fc)
4575 basic_format_context<_Out, _CharT>& _M_fc;
4577 using iterator =
typename _Scanner<_CharT>::iterator;
4580 _M_on_chars(iterator __last)
override
4582 basic_string_view<_CharT> __str(this->
begin(), __last);
4583 _M_fc.advance_to(__format::__write(_M_fc.out(), __str));
4587 _M_format_arg(
size_t __id)
override
4589 using _Context = basic_format_context<_Out, _CharT>;
4590 using handle =
typename basic_format_arg<_Context>::handle;
4592 __format::__visit_format_arg([
this](
auto& __arg) {
4594 using _Formatter =
typename _Context::template formatter_type<_Type>;
4595 if constexpr (is_same_v<_Type, monostate>)
4596 __format::__invalid_arg_id_in_format_string();
4597 else if constexpr (is_same_v<_Type, handle>)
4598 __arg.format(this->_M_pc, this->_M_fc);
4599 else if constexpr (is_default_constructible_v<_Formatter>)
4602 this->_M_pc.advance_to(__f.parse(this->_M_pc));
4603 this->_M_fc.advance_to(__f.format(__arg, this->_M_fc));
4606 static_assert(__format::__formattable_with<_Type, _Context>);
4607 }, _M_fc.arg(__id));
4611 template<
typename _CharT,
typename _Tp>
4613 __to_arg_t_enum() noexcept
4615 using _Context = __format::__format_context<_CharT>;
4616 using _Fmt_arg = basic_format_arg<_Context>;
4617 using _NormalizedTp =
typename _Fmt_arg::template _Normalize<_Tp>;
4618 return _Fmt_arg::template _S_to_enum<_NormalizedTp>();
4622 template<
typename _CharT,
typename... _Args>
4623 class _Checking_scanner :
public _Scanner<_CharT>
4626 (is_default_constructible_v<formatter<_Args, _CharT>> && ...),
4627 "std::formatter must be specialized for each type being formatted");
4631 _Checking_scanner(basic_string_view<_CharT> __str)
4632 : _Scanner<_CharT>(__str, sizeof...(_Args))
4634#if __cpp_lib_format >= 202305L
4635 this->_M_pc._M_types = _M_types.data();
4641 _M_format_arg(
size_t __id)
override
4643 if constexpr (
sizeof...(_Args) != 0)
4645 if (__id <
sizeof...(_Args))
4647 _M_parse_format_spec<_Args...>(__id);
4651 __builtin_unreachable();
4654 template<
typename _Tp,
typename... _OtherArgs>
4656 _M_parse_format_spec(
size_t __id)
4660 formatter<_Tp, _CharT> __f;
4661 this->_M_pc.advance_to(__f.parse(this->_M_pc));
4663 else if constexpr (
sizeof...(_OtherArgs) != 0)
4664 _M_parse_format_spec<_OtherArgs...>(__id - 1);
4666 __builtin_unreachable();
4669#if __cpp_lib_format >= 202305L
4670 array<_Arg_t,
sizeof...(_Args)>
4671 _M_types{ { __format::__to_arg_t_enum<_CharT, _Args>()... } };
4675 template<
typename _Out,
typename _CharT,
typename _Context>
4677 __do_vformat_to(_Out __out, basic_string_view<_CharT> __fmt,
4678 const basic_format_args<_Context>& __args,
4679 const locale* __loc)
4681 _Iter_sink<_CharT, _Out> __sink(
std::move(__out));
4682 _Sink_iter<_CharT> __sink_out;
4684 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4687 __sink_out = __sink.out();
4689 if constexpr (is_same_v<_CharT, char>)
4691 if (__fmt.size() == 2 && __fmt[0] ==
'{' && __fmt[1] ==
'}')
4693 bool __done =
false;
4694 __format::__visit_format_arg([&](
auto& __arg) {
4695 using _Tp = remove_cvref_t<
decltype(__arg)>;
4696 if constexpr (is_same_v<_Tp, bool>)
4698 size_t __len = 4 + !__arg;
4699 const char* __chars[] = {
"false",
"true" };
4700 if (
auto __res = __sink_out._M_reserve(__len))
4702 __builtin_memcpy(__res.get(), __chars[__arg], __len);
4703 __res._M_bump(__len);
4707 else if constexpr (is_same_v<_Tp, char>)
4709 if (
auto __res = __sink_out._M_reserve(1))
4711 *__res.get() = __arg;
4716 else if constexpr (is_integral_v<_Tp>)
4718 make_unsigned_t<_Tp> __uval;
4719 const bool __neg = __arg < 0;
4721 __uval = make_unsigned_t<_Tp>(~__arg) + 1u;
4724 const auto __n = __detail::__to_chars_len(__uval);
4725 if (
auto __res = __sink_out._M_reserve(__n + __neg))
4727 auto __ptr = __res.get();
4729 __detail::__to_chars_10_impl(__ptr + (
int)__neg, __n,
4731 __res._M_bump(__n + __neg);
4735 else if constexpr (is_convertible_v<_Tp, string_view>)
4737 string_view __sv = __arg;
4738 if (
auto __res = __sink_out._M_reserve(__sv.size()))
4740 __builtin_memcpy(__res.get(), __sv.data(), __sv.size());
4741 __res._M_bump(__sv.size());
4749 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4752 return std::move(__sink)._M_finish().out;
4756 auto __ctx = __loc ==
nullptr
4757 ? _Context(__args, __sink_out)
4758 : _Context(__args, __sink_out, *__loc);
4759 _Formatting_scanner<_Sink_iter<_CharT>, _CharT> __scanner(__ctx, __fmt);
4760 __scanner._M_scan();
4762 if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
4765 return std::move(__sink)._M_finish().out;
4767#pragma GCC diagnostic pop
4772#if __cpp_lib_format >= 202305L
4775 template<
typename _CharT>
4776 template<
typename... _Ts>
4778 basic_format_parse_context<_CharT>::
4779 __check_dynamic_spec(
size_t __id)
noexcept
4781 if (__id >= _M_num_args)
4782 __format::__invalid_arg_id_in_format_string();
4783 if constexpr (
sizeof...(_Ts) != 0)
4785 using _Parse_ctx = __format::_Scanner<_CharT>::_Parse_context;
4786 auto __arg =
static_cast<_Parse_ctx*
>(
this)->_M_types[__id];
4787 __format::_Arg_t __types[] = {
4788 __format::__to_arg_t_enum<_CharT, _Ts>()...
4790 for (
auto __t : __types)
4794 __invalid_dynamic_spec(
"arg(id) type does not match");
4799 template<
typename _CharT,
typename... _Args>
4800 template<
typename _Tp>
4801 requires convertible_to<const _Tp&, basic_string_view<_CharT>>
4803 basic_format_string<_CharT, _Args...>::
4804 basic_format_string(
const _Tp& __s)
4807 __format::_Checking_scanner<_CharT, remove_cvref_t<_Args>...>
4809 __scanner._M_scan();
4814 template<
typename _Out>
requires output_iterator<_Out, const char&>
4815 [[__gnu__::__always_inline__]]
4817 vformat_to(_Out __out, string_view __fmt, format_args __args)
4818 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
4820#ifdef _GLIBCXX_USE_WCHAR_T
4821 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
4822 [[__gnu__::__always_inline__]]
4824 vformat_to(_Out __out, wstring_view __fmt, wformat_args __args)
4825 {
return __format::__do_vformat_to(
std::move(__out), __fmt, __args); }
4828 template<
typename _Out>
requires output_iterator<_Out, const char&>
4829 [[__gnu__::__always_inline__]]
4831 vformat_to(_Out __out,
const locale& __loc, string_view __fmt,
4834 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
4837#ifdef _GLIBCXX_USE_WCHAR_T
4838 template<
typename _Out>
requires output_iterator<_Out, const wchar_t&>
4839 [[__gnu__::__always_inline__]]
4841 vformat_to(_Out __out,
const locale& __loc, wstring_view __fmt,
4842 wformat_args __args)
4844 return __format::__do_vformat_to(
std::move(__out), __fmt, __args, &__loc);
4850 vformat(string_view __fmt, format_args __args)
4852 __format::_Str_sink<char> __buf;
4853 std::vformat_to(__buf.out(), __fmt, __args);
4857#ifdef _GLIBCXX_USE_WCHAR_T
4860 vformat(wstring_view __fmt, wformat_args __args)
4862 __format::_Str_sink<wchar_t> __buf;
4863 std::vformat_to(__buf.out(), __fmt, __args);
4870 vformat(
const locale& __loc, string_view __fmt, format_args __args)
4872 __format::_Str_sink<char> __buf;
4873 std::vformat_to(__buf.out(), __loc, __fmt, __args);
4877#ifdef _GLIBCXX_USE_WCHAR_T
4880 vformat(
const locale& __loc, wstring_view __fmt, wformat_args __args)
4882 __format::_Str_sink<wchar_t> __buf;
4883 std::vformat_to(__buf.out(), __loc, __fmt, __args);
4888 template<
typename... _Args>
4891 format(format_string<_Args...> __fmt, _Args&&... __args)
4892 {
return std::vformat(__fmt.get(), std::make_format_args(__args...)); }
4894#ifdef _GLIBCXX_USE_WCHAR_T
4895 template<
typename... _Args>
4898 format(wformat_string<_Args...> __fmt, _Args&&... __args)
4899 {
return std::vformat(__fmt.get(), std::make_wformat_args(__args...)); }
4902 template<
typename... _Args>
4905 format(
const locale& __loc, format_string<_Args...> __fmt,
4908 return std::vformat(__loc, __fmt.get(),
4909 std::make_format_args(__args...));
4912#ifdef _GLIBCXX_USE_WCHAR_T
4913 template<
typename... _Args>
4916 format(
const locale& __loc, wformat_string<_Args...> __fmt,
4919 return std::vformat(__loc, __fmt.get(),
4920 std::make_wformat_args(__args...));
4924 template<
typename _Out,
typename... _Args>
4925 requires output_iterator<_Out, const char&>
4927 format_to(_Out __out, format_string<_Args...> __fmt, _Args&&... __args)
4929 return std::vformat_to(
std::move(__out), __fmt.get(),
4930 std::make_format_args(__args...));
4933#ifdef _GLIBCXX_USE_WCHAR_T
4934 template<
typename _Out,
typename... _Args>
4935 requires output_iterator<_Out, const wchar_t&>
4937 format_to(_Out __out, wformat_string<_Args...> __fmt, _Args&&... __args)
4939 return std::vformat_to(
std::move(__out), __fmt.get(),
4940 std::make_wformat_args(__args...));
4944 template<
typename _Out,
typename... _Args>
4945 requires output_iterator<_Out, const char&>
4947 format_to(_Out __out,
const locale& __loc, format_string<_Args...> __fmt,
4950 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
4951 std::make_format_args(__args...));
4954#ifdef _GLIBCXX_USE_WCHAR_T
4955 template<
typename _Out,
typename... _Args>
4956 requires output_iterator<_Out, const wchar_t&>
4958 format_to(_Out __out,
const locale& __loc, wformat_string<_Args...> __fmt,
4961 return std::vformat_to(
std::move(__out), __loc, __fmt.get(),
4962 std::make_wformat_args(__args...));
4966 template<
typename _Out,
typename... _Args>
4967 requires output_iterator<_Out, const char&>
4968 inline format_to_n_result<_Out>
4969 format_to_n(_Out __out, iter_difference_t<_Out> __n,
4970 format_string<_Args...> __fmt, _Args&&... __args)
4972 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
4973 std::vformat_to(__sink.out(), __fmt.get(),
4974 std::make_format_args(__args...));
4978#ifdef _GLIBCXX_USE_WCHAR_T
4979 template<
typename _Out,
typename... _Args>
4980 requires output_iterator<_Out, const wchar_t&>
4981 inline format_to_n_result<_Out>
4982 format_to_n(_Out __out, iter_difference_t<_Out> __n,
4983 wformat_string<_Args...> __fmt, _Args&&... __args)
4985 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
4986 std::vformat_to(__sink.out(), __fmt.get(),
4987 std::make_wformat_args(__args...));
4992 template<
typename _Out,
typename... _Args>
4993 requires output_iterator<_Out, const char&>
4994 inline format_to_n_result<_Out>
4995 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
4996 format_string<_Args...> __fmt, _Args&&... __args)
4998 __format::_Iter_sink<char, _Out> __sink(
std::move(__out), __n);
4999 std::vformat_to(__sink.out(), __loc, __fmt.get(),
5000 std::make_format_args(__args...));
5004#ifdef _GLIBCXX_USE_WCHAR_T
5005 template<
typename _Out,
typename... _Args>
5006 requires output_iterator<_Out, const wchar_t&>
5007 inline format_to_n_result<_Out>
5008 format_to_n(_Out __out, iter_difference_t<_Out> __n,
const locale& __loc,
5009 wformat_string<_Args...> __fmt, _Args&&... __args)
5011 __format::_Iter_sink<wchar_t, _Out> __sink(
std::move(__out), __n);
5012 std::vformat_to(__sink.out(), __loc, __fmt.get(),
5013 std::make_wformat_args(__args...));
5022 template<
typename _CharT>
5023 class _Counting_sink final :
public _Iter_sink<_CharT, _CharT*>
5026 _Counting_sink() : _Iter_sink<_CharT, _CharT*>(nullptr, 0) { }
5028 [[__gnu__::__always_inline__]]
5031 {
return this->_M_count + this->_M_used().size(); }
5034 template<
typename _CharT>
5035 class _Counting_sink :
public _Buf_sink<_CharT>
5037 size_t _M_count = 0;
5040 _M_overflow()
override
5042 if (!std::is_constant_evaluated())
5043 _M_count += this->_M_used().size();
5048 _Counting_sink() =
default;
5050 [[__gnu__::__always_inline__]]
5054 _Counting_sink::_M_overflow();
5062 template<
typename... _Args>
5065 formatted_size(format_string<_Args...> __fmt, _Args&&... __args)
5067 __format::_Counting_sink<char> __buf;
5068 std::vformat_to(__buf.out(), __fmt.get(),
5069 std::make_format_args(__args...));
5070 return __buf.count();
5073#ifdef _GLIBCXX_USE_WCHAR_T
5074 template<
typename... _Args>
5077 formatted_size(wformat_string<_Args...> __fmt, _Args&&... __args)
5079 __format::_Counting_sink<wchar_t> __buf;
5080 std::vformat_to(__buf.out(), __fmt.get(),
5081 std::make_wformat_args(__args...));
5082 return __buf.count();
5086 template<
typename... _Args>
5089 formatted_size(
const locale& __loc, format_string<_Args...> __fmt,
5092 __format::_Counting_sink<char> __buf;
5093 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5094 std::make_format_args(__args...));
5095 return __buf.count();
5098#ifdef _GLIBCXX_USE_WCHAR_T
5099 template<
typename... _Args>
5102 formatted_size(
const locale& __loc, wformat_string<_Args...> __fmt,
5105 __format::_Counting_sink<wchar_t> __buf;
5106 std::vformat_to(__buf.out(), __loc, __fmt.get(),
5107 std::make_wformat_args(__args...));
5108 return __buf.count();
5112#if __glibcxx_format_ranges
5115 enum class range_format {
5133 template<
typename _Rg>
5134 constexpr auto format_kind = []{
5135 static_assert(
false,
"cannot use primary template of 'std::format_kind'");
5136 return type_identity<_Rg>{};
5140 template<
typename _Tp>
5141 consteval range_format
5144 using _Ref = ranges::range_reference_t<_Tp>;
5145 if constexpr (is_same_v<remove_cvref_t<_Ref>, _Tp>)
5146 return range_format::disabled;
5147 else if constexpr (
requires {
typename _Tp::key_type; })
5149 if constexpr (
requires {
typename _Tp::mapped_type; })
5151 using _Up = remove_cvref_t<_Ref>;
5152 if constexpr (__is_pair<_Up>)
5153 return range_format::map;
5154 else if constexpr (__is_specialization_of<_Up, tuple>)
5155 if constexpr (tuple_size_v<_Up> == 2)
5156 return range_format::map;
5158 return range_format::set;
5161 return range_format::sequence;
5166 template<ranges::input_range _Rg>
requires same_as<_Rg, remove_cvref_t<_Rg>>
5167 constexpr range_format format_kind<_Rg> = __fmt_kind<_Rg>();
5172 template<
typename _CharT,
typename _Out,
typename _Callback>
5173 typename basic_format_context<_Out, _CharT>::iterator
5174 __format_padded(basic_format_context<_Out, _CharT>& __fc,
5175 const _Spec<_CharT>& __spec,
5180 static_assert(is_same_v<_Out, __format::_Sink_iter<_CharT>>);
5182 if (__spec._M_get_width(__fc) == 0)
5183 return __call(__fc);
5187 _Restore_out(basic_format_context<_Sink_iter<_CharT>, _CharT>& __fc)
5188 : _M_ctx(std::
addressof(__fc)), _M_out(__fc.out())
5194 _M_ctx->advance_to(_M_out);
5202 basic_format_context<_Sink_iter<_CharT>, _CharT>* _M_ctx;
5203 _Sink_iter<_CharT> _M_out;
5206 _Restore_out __restore(__fc);
5210 _Str_sink<_CharT> __buf;
5211 __fc.advance_to(__buf.out());
5213 __restore._M_trigger();
5215 basic_string_view<_CharT> __str(__buf.view());
5217 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
5218 __width = __unicode::__field_width(__str);
5220 __width = __str.size();
5222 return __format::__write_padded_as_spec(__str, __width, __fc, __spec);
5227 template<
typename _Rg,
typename _CharT>
5228 concept __simply_formattable_range
5229 = __const_formattable_range<_Rg, _CharT>
5230 && same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>,
5231 remove_cvref_t<ranges::range_reference_t<const _Rg>>>;
5233 template<
size_t _Pos,
typename _Tp,
typename _CharT>
5234 struct __indexed_formatter_storage
5239 basic_format_parse_context<_CharT> __pc({});
5240 if (_M_formatter.parse(__pc) != __pc.end())
5241 __format::__failed_to_parse_format_spec();
5244 template<
typename _Out>
5246 _M_format(__maybe_const<_Tp, _CharT>& __elem,
5247 basic_format_context<_Out, _CharT>& __fc,
5248 basic_string_view<_CharT> __sep)
const
5250 if constexpr (_Pos != 0)
5251 __fc.advance_to(__format::__write(__fc.out(), __sep));
5252 __fc.advance_to(_M_formatter.format(__elem, __fc));
5255 [[__gnu__::__always_inline__]]
5259 if constexpr (__has_debug_format<formatter<_Tp, _CharT>>)
5260 _M_formatter.set_debug_format();
5264 formatter<_Tp, _CharT> _M_formatter;
5267 template<
typename _CharT,
typename... _Tps>
5268 class __tuple_formatter
5270 using _String_view = basic_string_view<_CharT>;
5271 using _Seps = __format::_Separators<_CharT>;
5275 set_separator(basic_string_view<_CharT> __sep)
noexcept
5279 set_brackets(basic_string_view<_CharT> __open,
5280 basic_string_view<_CharT> __close)
noexcept
5288 constexpr typename basic_format_parse_context<_CharT>::iterator
5289 parse(basic_format_parse_context<_CharT>& __pc)
5291 auto __first = __pc.begin();
5292 const auto __last = __pc.end();
5293 __format::_Spec<_CharT> __spec{};
5295 auto __finished = [&]
5297 if (__first != __last && *__first !=
'}')
5301 _M_felems._M_parse();
5302 _M_felems.set_debug_format();
5309 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5313 __first = __spec._M_parse_width(__first, __last, __pc);
5317 if (*__first ==
'n')
5320 _M_open = _M_close = _String_view();
5322 else if (*__first ==
'm')
5325 if constexpr (
sizeof...(_Tps) == 2)
5327 _M_sep = _Seps::_S_colon();
5328 _M_open = _M_close = _String_view();
5331 __throw_format_error(
"format error: 'm' specifier requires range"
5332 " of pair or tuple of two elements");
5338 __format::__failed_to_parse_format_spec();
5342 template<
typename _Tuple,
typename _Out,
size_t... _Ids>
5343 typename basic_format_context<_Out, _CharT>::iterator
5344 _M_format(_Tuple& __tuple, index_sequence<_Ids...>,
5345 basic_format_context<_Out, _CharT>& __fc)
const
5346 {
return _M_format_elems(std::get<_Ids>(__tuple)..., __fc); }
5348 template<
typename _Out>
5349 typename basic_format_context<_Out, _CharT>::iterator
5350 _M_format_elems(__maybe_const<_Tps, _CharT>&... __elems,
5351 basic_format_context<_Out, _CharT>& __fc)
const
5353 return __format::__format_padded(
5355 [
this, &__elems...](basic_format_context<_Out, _CharT>& __nfc)
5357 __nfc.advance_to(__format::__write(__nfc.out(), _M_open));
5358 _M_felems._M_format(__elems..., __nfc, _M_sep);
5359 return __format::__write(__nfc.out(), _M_close);
5364 template<
size_t... _Ids>
5365 struct __formatters_storage
5366 : __indexed_formatter_storage<_Ids, _Tps, _CharT>...
5368 template<
size_t _Id,
typename _Up>
5369 using _Base = __indexed_formatter_storage<_Id, _Up, _CharT>;
5374 (_Base<_Ids, _Tps>::_M_parse(), ...);
5377 template<
typename _Out>
5379 _M_format(__maybe_const<_Tps, _CharT>&... __elems,
5380 basic_format_context<_Out, _CharT>& __fc,
5381 _String_view __sep)
const
5383 (_Base<_Ids, _Tps>::_M_format(__elems, __fc, __sep), ...);
5389 (_Base<_Ids, _Tps>::set_debug_format(), ...);
5393 template<
size_t... _Ids>
5395 _S_create_storage(index_sequence<_Ids...>)
5396 -> __formatters_storage<_Ids...>;
5398 =
decltype(_S_create_storage(index_sequence_for<_Tps...>()));
5400 _Spec<_CharT> _M_spec{};
5401 _String_view _M_open = _Seps::_S_parens().substr(0, 1);
5402 _String_view _M_close = _Seps::_S_parens().substr(1, 1);
5403 _String_view _M_sep = _Seps::_S_comma();
5404 _Formatters _M_felems;
5407 template<
typename _Tp>
5408 concept __is_map_formattable
5409 = __is_pair<_Tp> || (__is_tuple_v<_Tp> && tuple_size_v<_Tp> == 2);
5415 template<__format::__
char _CharT, formattable<_CharT> _Fp,
5416 formattable<_CharT> _Sp>
5417 struct formatter<
pair<_Fp, _Sp>, _CharT>
5418 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Fp>,
5419 remove_cvref_t<_Sp>>
5422 using __maybe_const_pair
5423 = __conditional_t<formattable<const _Fp, _CharT>
5424 && formattable<const _Sp, _CharT>,
5425 const pair<_Fp, _Sp>, pair<_Fp, _Sp>>;
5429 template<
typename _Out>
5430 typename basic_format_context<_Out, _CharT>::iterator
5431 format(__maybe_const_pair& __p,
5432 basic_format_context<_Out, _CharT>& __fc)
const
5433 {
return this->_M_format_elems(__p.first, __p.second, __fc); }
5436 template<__format::__
char _CharT, formattable<_CharT>... _Tps>
5437 struct formatter<tuple<_Tps...>, _CharT>
5438 : __format::__tuple_formatter<_CharT, remove_cvref_t<_Tps>...>
5441 using __maybe_const_tuple
5442 = __conditional_t<(formattable<const _Tps, _CharT> && ...),
5443 const tuple<_Tps...>, tuple<_Tps...>>;
5447 template<
typename _Out>
5448 typename basic_format_context<_Out, _CharT>::iterator
5449 format(__maybe_const_tuple& __t,
5450 basic_format_context<_Out, _CharT>& __fc)
const
5451 {
return this->_M_format(__t, index_sequence_for<_Tps...>(), __fc); }
5455 template<
typename _Tp, __format::__
char _CharT>
5456 requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
5457 class range_formatter
5459 using _String_view = basic_string_view<_CharT>;
5460 using _Seps = __format::_Separators<_CharT>;
5464 set_separator(basic_string_view<_CharT> __sep)
noexcept
5468 set_brackets(basic_string_view<_CharT> __open,
5469 basic_string_view<_CharT> __close)
noexcept
5475 constexpr formatter<_Tp, _CharT>&
5476 underlying() noexcept
5479 constexpr const formatter<_Tp, _CharT>&
5480 underlying() const noexcept
5485 constexpr typename basic_format_parse_context<_CharT>::iterator
5486 parse(basic_format_parse_context<_CharT>& __pc)
5488 auto __first = __pc.begin();
5489 const auto __last = __pc.end();
5490 __format::_Spec<_CharT> __spec{};
5491 bool __no_brace =
false;
5493 auto __finished = [&]
5494 {
return __first == __last || *__first ==
'}'; };
5496 auto __finalize = [&]
5502 auto __parse_val = [&](_String_view __nfs = _String_view())
5504 basic_format_parse_context<_CharT> __npc(__nfs);
5505 if (_M_fval.parse(__npc) != __npc.end())
5506 __format::__failed_to_parse_format_spec();
5507 if constexpr (__format::__has_debug_format<formatter<_Tp, _CharT>>)
5508 _M_fval.set_debug_format();
5509 return __finalize();
5513 return __parse_val();
5515 __first = __spec._M_parse_fill_and_align(__first, __last,
"{:");
5517 return __parse_val();
5519 __first = __spec._M_parse_width(__first, __last, __pc);
5521 return __parse_val();
5523 if (*__first ==
'?')
5526 __spec._M_type = __format::_Pres_esc;
5527 if (__finished() || *__first !=
's')
5528 __throw_format_error(
"format error: '?' is allowed only in"
5529 " combination with 's'");
5532 if (*__first ==
's')
5535 if constexpr (same_as<_Tp, _CharT>)
5537 if (__spec._M_type != __format::_Pres_esc)
5538 __spec._M_type = __format::_Pres_str;
5540 return __finalize();
5541 __throw_format_error(
"format error: element format specifier"
5542 " cannot be provided when 's' specifier is used");
5545 __throw_format_error(
"format error: 's' specifier requires"
5546 " range of character types");
5550 return __parse_val();
5552 if (*__first ==
'n')
5555 _M_open = _M_close = _String_view();
5560 return __parse_val();
5562 if (*__first ==
'm')
5564 _String_view __m(__first, 1);
5566 if constexpr (__format::__is_map_formattable<_Tp>)
5568 _M_sep = _Seps::_S_comma();
5571 _M_open = _Seps::_S_braces().substr(0, 1);
5572 _M_close = _Seps::_S_braces().substr(1, 1);
5575 return __parse_val(__m);
5576 __throw_format_error(
"format error: element format specifier"
5577 " cannot be provided when 'm' specifier is used");
5580 __throw_format_error(
"format error: 'm' specifier requires"
5581 " range of pairs or tuples of two elements");
5585 return __parse_val();
5587 if (*__first ==
':')
5589 __pc.advance_to(++__first);
5590 __first = _M_fval.parse(__pc);
5594 return __finalize();
5596 __format::__failed_to_parse_format_spec();
5601 template<ranges::input_range _Rg,
typename _Out>
5602 requires formattable<ranges::range_reference_t<_Rg>, _CharT> &&
5603 same_as<remove_cvref_t<ranges::range_reference_t<_Rg>>, _Tp>
5604 typename basic_format_context<_Out, _CharT>::iterator
5605 format(_Rg&& __rg, basic_format_context<_Out, _CharT>& __fc)
const
5607 using _Range = remove_reference_t<_Rg>;
5608 if constexpr (__format::__simply_formattable_range<_Range, _CharT>)
5609 return _M_format<const _Range>(__rg, __fc);
5611 return _M_format(__rg, __fc);
5615 template<ranges::input_range _Rg,
typename _Out>
5616 typename basic_format_context<_Out, _CharT>::iterator
5617 _M_format(_Rg& __rg, basic_format_context<_Out, _CharT>& __fc)
const
5619 if constexpr (same_as<_Tp, _CharT>)
5620 if (_M_spec._M_type == __format::_Pres_str
5621 || _M_spec._M_type == __format::_Pres_esc)
5623 __format::__formatter_str __fstr(_M_spec);
5624 return __fstr._M_format_range(__rg, __fc);
5626 return __format::__format_padded(
5628 [
this, &__rg](basic_format_context<_Out, _CharT>& __nfc)
5629 {
return _M_format_elems(__rg, __nfc); });
5633 template<ranges::input_range _Rg,
typename _Out>
5634 typename basic_format_context<_Out, _CharT>::iterator
5635 _M_format_elems(_Rg& __rg,
5636 basic_format_context<_Out, _CharT>& __fc)
const
5638 auto __out = __format::__write(__fc.out(), _M_open);
5640 auto __first = ranges::begin(__rg);
5641 auto const __last = ranges::end(__rg);
5642 if (__first == __last)
5643 return __format::__write(__out, _M_close);
5645 __fc.advance_to(__out);
5646 __out = _M_fval.format(*__first, __fc);
5647 for (++__first; __first != __last; ++__first)
5649 __out = __format::__write(__out, _M_sep);
5650 __fc.advance_to(__out);
5651 __out = _M_fval.format(*__first, __fc);
5654 return __format::__write(__out, _M_close);
5657 __format::_Spec<_CharT> _M_spec{};
5658 _String_view _M_open = _Seps::_S_squares().substr(0, 1);
5659 _String_view _M_close = _Seps::_S_squares().substr(1, 1);
5660 _String_view _M_sep = _Seps::_S_comma();
5661 formatter<_Tp, _CharT> _M_fval;
5669 template<ranges::input_range _Rg, __format::__
char _CharT>
5670 requires (format_kind<_Rg> != range_format::disabled)
5671 && formattable<ranges::range_reference_t<_Rg>, _CharT>
5672 struct formatter<_Rg, _CharT>
5675 static const bool _S_range_format_is_string =
5676 (format_kind<_Rg> == range_format::string)
5677 || (format_kind<_Rg> == range_format::debug_string);
5678 using _Vt = remove_cvref_t<
5679 ranges::range_reference_t<
5680 __format::__maybe_const_range<_Rg, _CharT>>>;
5682 static consteval bool _S_is_correct()
5684 if constexpr (_S_range_format_is_string)
5685 static_assert(same_as<_Vt, _CharT>);
5689 static_assert(_S_is_correct());
5692 constexpr formatter() noexcept
5694 using _Seps = __format::_Separators<_CharT>;
5695 if constexpr (format_kind<_Rg> == range_format::map)
5697 static_assert(__format::__is_map_formattable<_Vt>);
5698 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
5699 _Seps::_S_braces().substr(1, 1));
5700 _M_under.underlying().set_brackets({}, {});
5701 _M_under.underlying().set_separator(_Seps::_S_colon());
5703 else if constexpr (format_kind<_Rg> == range_format::set)
5704 _M_under.set_brackets(_Seps::_S_braces().substr(0, 1),
5705 _Seps::_S_braces().substr(1, 1));
5709 set_separator(basic_string_view<_CharT> __sep)
noexcept
5710 requires (format_kind<_Rg> == range_format::sequence)
5711 { _M_under.set_separator(__sep); }
5714 set_brackets(basic_string_view<_CharT> __open,
5715 basic_string_view<_CharT> __close)
noexcept
5716 requires (format_kind<_Rg> == range_format::sequence)
5717 { _M_under.set_brackets(__open, __close); }
5721 constexpr typename basic_format_parse_context<_CharT>::iterator
5722 parse(basic_format_parse_context<_CharT>& __pc)
5724 auto __res = _M_under.parse(__pc);
5725 if constexpr (format_kind<_Rg> == range_format::debug_string)
5726 _M_under.set_debug_format();
5732 template<
typename _Out>
5733 typename basic_format_context<_Out, _CharT>::iterator
5734 format(__format::__maybe_const_range<_Rg, _CharT>& __rg,
5735 basic_format_context<_Out, _CharT>& __fc)
const
5737 if constexpr (_S_range_format_is_string)
5738 return _M_under._M_format_range(__rg, __fc);
5740 return _M_under.format(__rg, __fc);
5744 using _Formatter_under
5745 = __conditional_t<_S_range_format_is_string,
5746 __format::__formatter_str<_CharT>,
5747 range_formatter<_Vt, _CharT>>;
5748 _Formatter_under _M_under;
5751#undef _GLIBCXX_WIDEN
5753_GLIBCXX_END_NAMESPACE_VERSION
5756#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.
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
_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.
const _Facet & use_facet(const locale &__loc)
Return a facet.
basic_string< char > string
A string of char.
ISO C++ entities toplevel namespace is std.
chars_format
floating-point format for primitive numerical conversion
_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.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
One of two subclasses of exception.
A non-owning reference to a string.
Managing sequences of characters and character-like objects.
constexpr size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
constexpr void reserve(size_type __res_arg)
Attempt to preallocate enough memory for specified number of characters.
constexpr const _CharT * data() const noexcept
Return const pointer to contents.
constexpr basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
constexpr void __resize_and_overwrite(size_type __n, _Operation __op)
Non-standard version of resize_and_overwrite for C++11 and above.
constexpr basic_string & append(const basic_string &__str)
Append a string to this string.
constexpr iterator insert(const_iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
constexpr size_type capacity() const noexcept
constexpr bool empty() const noexcept
A standard container which offers fixed time access to individual elements in any order.