mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathformat
More file actions
6438 lines (5659 loc) · 186 KB
/
format
File metadata and controls
6438 lines (5659 loc) · 186 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// <format> Formatting -*- C++ -*-
// Copyright The GNU Toolchain Authors.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file include/format
* This is a Standard C++ Library header.
*/
#ifndef _GLIBCXX_FORMAT
#define _GLIBCXX_FORMAT 1
#ifdef _GLIBCXX_SYSHDR
#pragma GCC system_header
#endif
#include <bits/requires_hosted.h> // for std::string
#define __glibcxx_want_format
#define __glibcxx_want_format_ranges
#define __glibcxx_want_format_uchar
#define __glibcxx_want_constexpr_exceptions
#define __glibcxx_want_constexpr_format
#include <bits/version.h>
#ifdef __cpp_lib_format // C++ >= 20 && HOSTED
#include <array>
#include <charconv>
#include <concepts>
#include <limits>
#include <locale>
#include <optional>
#include <span>
#include <string_view>
#include <string>
#include <bits/monostate.h>
#include <bits/formatfwd.h>
#include <bits/ranges_base.h> // input_range, range_reference_t
#include <bits/ranges_util.h> // subrange
#include <bits/ranges_algobase.h> // ranges::copy
#include <bits/stl_iterator.h> // counted_iterator
#include <bits/stl_pair.h> // __is_pair
#include <bits/unicode.h> // __is_scalar_value, _Utf_view, etc.
#include <bits/utility.h> // tuple_size_v
#include <ext/numeric_traits.h> // __int_traits
#ifdef __glibcxx_constexpr_format // C++ >= 26 && HOSTED && CXX11 strings
# define _GLIBCXX_CONSTEXPR_FORMAT constexpr
#else
# define _GLIBCXX_CONSTEXPR_FORMAT
#endif
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic" // __int128
#pragma GCC diagnostic ignored "-Wc++23-extensions" // bf16
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
// [format.fmt.string], class template basic_format_string
template<typename _CharT, typename... _Args> struct basic_format_string;
/// @cond undocumented
namespace __format
{
// STATICALLY-WIDEN, see C++20 [time.general]
// It doesn't matter for format strings (which can only be char or wchar_t)
// but this returns the narrow string for anything that isn't wchar_t. This
// is done because const char* can be inserted into any ostream type, and
// will be widened at runtime if necessary.
template<typename _CharT>
consteval auto
_Widen(const char* __narrow, const wchar_t* __wide)
{
if constexpr (is_same_v<_CharT, wchar_t>)
return __wide;
else
return __narrow;
}
#define _GLIBCXX_WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
#define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
// Size for stack located buffer
template<typename _CharT>
constexpr size_t __stackbuf_size = 32 * sizeof(void*) / sizeof(_CharT);
// Type-erased character sinks.
template<typename _CharT> class _Sink;
template<typename _CharT> class _Fixedbuf_sink;
template<typename _Out, typename _CharT> class _Padding_sink;
template<typename _Out, typename _CharT> class _Escaping_sink;
// Output iterator that writes to a type-erase character sink.
template<typename _CharT>
class _Sink_iter;
// Output iterator that ignores the characters
template<typename _CharT>
class _Drop_iter;
// An unspecified output iterator type used in the `formattable` concept.
template<typename _CharT>
struct _Iter_for
{ using type = _Drop_iter<_CharT>; };
template<typename _CharT>
using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>;
template<typename _CharT>
struct _Dynamic_format_string
{
[[__gnu__::__always_inline__]]
_GLIBCXX_CONSTEXPR_FORMAT
_Dynamic_format_string(basic_string_view<_CharT> __s) noexcept
: _M_str(__s) { }
_Dynamic_format_string(const _Dynamic_format_string&) = delete;
void operator=(const _Dynamic_format_string&) = delete;
private:
basic_string_view<_CharT> _M_str;
template<typename, typename...> friend struct std::basic_format_string;
};
} // namespace __format
/// @endcond
using format_context = __format::__format_context<char>;
#ifdef _GLIBCXX_USE_WCHAR_T
using wformat_context = __format::__format_context<wchar_t>;
#endif
// [format.args], class template basic_format_args
template<typename _Context> class basic_format_args;
using format_args = basic_format_args<format_context>;
#ifdef _GLIBCXX_USE_WCHAR_T
using wformat_args = basic_format_args<wformat_context>;
#endif
// [format.arguments], arguments
// [format.arg], class template basic_format_arg
template<typename _Context>
class basic_format_arg;
/** A compile-time checked format string for the specified argument types.
*
* @since C++23 but available as an extension in C++20.
*/
template<typename _CharT, typename... _Args>
struct basic_format_string
{
template<typename _Tp>
requires convertible_to<const _Tp&, basic_string_view<_CharT>>
consteval
basic_format_string(const _Tp& __s) noexcept;
[[__gnu__::__always_inline__]]
_GLIBCXX_CONSTEXPR_FORMAT
basic_format_string(__format::_Dynamic_format_string<_CharT> __s) noexcept
: _M_str(__s._M_str)
{ }
[[__gnu__::__always_inline__]]
constexpr basic_string_view<_CharT>
get() const noexcept
{ return _M_str; }
private:
basic_string_view<_CharT> _M_str;
};
template<typename... _Args>
using format_string = basic_format_string<char, type_identity_t<_Args>...>;
#ifdef _GLIBCXX_USE_WCHAR_T
template<typename... _Args>
using wformat_string
= basic_format_string<wchar_t, type_identity_t<_Args>...>;
#endif
#if __cpp_lib_format >= 202603L // >= C++26
[[__gnu__::__always_inline__]]
inline _GLIBCXX_CONSTEXPR_FORMAT __format::_Dynamic_format_string<char>
dynamic_format(string_view __fmt) noexcept
{ return __fmt; }
#ifdef _GLIBCXX_USE_WCHAR_T
[[__gnu__::__always_inline__]]
inline _GLIBCXX_CONSTEXPR_FORMAT __format::_Dynamic_format_string<wchar_t>
dynamic_format(wstring_view __fmt) noexcept
{ return __fmt; }
#endif
#endif // C++26
// [format.formatter], formatter
/// The primary template of std::formatter is disabled.
template<typename _Tp, typename _CharT>
struct formatter
{
formatter() = delete; // No std::formatter specialization for this type.
formatter(const formatter&) = delete;
formatter& operator=(const formatter&) = delete;
};
// [format.error], class format_error
class format_error : public runtime_error
{
public:
_GLIBCXX_CONSTEXPR_FORMAT
explicit format_error(const string& __what)
: runtime_error(__what) { }
_GLIBCXX_CONSTEXPR_FORMAT explicit
format_error(const char* __what)
: runtime_error(__what) { }
};
/// @cond undocumented
[[noreturn]]
inline _GLIBCXX_CONSTEXPR_FORMAT void
__throw_format_error(const char* __what)
{ _GLIBCXX_THROW_OR_ABORT(format_error(__what)); }
namespace __format
{
// XXX use named functions for each constexpr error?
[[noreturn]]
inline _GLIBCXX_CONSTEXPR_FORMAT void
__unmatched_left_brace_in_format_string()
{ __throw_format_error("format error: unmatched '{' in format string"); }
[[noreturn]]
inline _GLIBCXX_CONSTEXPR_FORMAT void
__unmatched_right_brace_in_format_string()
{ __throw_format_error("format error: unmatched '}' in format string"); }
[[noreturn]]
inline _GLIBCXX_CONSTEXPR_FORMAT void
__conflicting_indexing_in_format_string()
{ __throw_format_error("format error: conflicting indexing style in format string"); }
[[noreturn]]
inline _GLIBCXX_CONSTEXPR_FORMAT void
__invalid_arg_id_in_format_string()
{ __throw_format_error("format error: invalid arg-id in format string"); }
[[noreturn]]
inline _GLIBCXX_CONSTEXPR_FORMAT void
__failed_to_parse_format_spec()
{ __throw_format_error("format error: failed to parse format-spec"); }
template<typename _CharT> class _Scanner;
} // namespace __format
/// @endcond
// [format.parse.ctx], class template basic_format_parse_context
template<typename _CharT> class basic_format_parse_context;
using format_parse_context = basic_format_parse_context<char>;
#ifdef _GLIBCXX_USE_WCHAR_T
using wformat_parse_context = basic_format_parse_context<wchar_t>;
#endif
template<typename _CharT>
class basic_format_parse_context
{
public:
using char_type = _CharT;
using const_iterator = typename basic_string_view<_CharT>::const_iterator;
using iterator = const_iterator;
constexpr explicit
basic_format_parse_context(basic_string_view<_CharT> __fmt) noexcept
: _M_begin(__fmt.begin()), _M_end(__fmt.end())
{ }
basic_format_parse_context(const basic_format_parse_context&) = delete;
void operator=(const basic_format_parse_context&) = delete;
constexpr const_iterator begin() const noexcept { return _M_begin; }
constexpr const_iterator end() const noexcept { return _M_end; }
constexpr void
advance_to(const_iterator __it) noexcept
{ _M_begin = __it; }
constexpr size_t
next_arg_id()
{
if (_M_indexing == _Manual)
__format::__conflicting_indexing_in_format_string();
_M_indexing = _Auto;
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3825. Missing compile-time argument id check in next_arg_id
if (std::is_constant_evaluated())
if (_M_next_arg_id == _M_num_args)
__format::__invalid_arg_id_in_format_string();
return _M_next_arg_id++;
}
constexpr void
check_arg_id(size_t __id)
{
if (_M_indexing == _Auto)
__format::__conflicting_indexing_in_format_string();
_M_indexing = _Manual;
if (std::is_constant_evaluated())
if (__id >= _M_num_args)
__format::__invalid_arg_id_in_format_string();
}
#if __cpp_lib_format >= 202305L
template<typename... _Ts>
constexpr void
check_dynamic_spec(size_t __id) noexcept
{
static_assert(__valid_types_for_check_dynamic_spec<_Ts...>(),
"template arguments for check_dynamic_spec<Ts...>(id) "
"must be unique and must be one of the allowed types");
if consteval {
__check_dynamic_spec<_Ts...>(__id);
}
}
constexpr void
check_dynamic_spec_integral(size_t __id) noexcept
{
if consteval {
__check_dynamic_spec<int, unsigned, long long,
unsigned long long>(__id);
}
}
constexpr void
check_dynamic_spec_string(size_t __id) noexcept
{
if consteval {
__check_dynamic_spec<const _CharT*, basic_string_view<_CharT>>(__id);
}
}
private:
// True if _Tp occurs exactly once in _Ts.
template<typename _Tp, typename... _Ts>
static constexpr bool __once = (is_same_v<_Tp, _Ts> + ...) == 1;
template<typename... _Ts>
consteval bool
__valid_types_for_check_dynamic_spec()
{
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 4142. check_dynamic_spec should require at least one type
if constexpr (sizeof...(_Ts) == 0)
return false;
else
{
// The types in Ts... are unique. Each type in Ts... is one of
// bool, char_type, int, unsigned int, long long int,
// unsigned long long int, float, double, long double,
// const char_type*, basic_string_view<char_type>, or const void*.
unsigned __sum
= __once<bool, _Ts...>
+ __once<char_type, _Ts...>
+ __once<int, _Ts...>
+ __once<unsigned int, _Ts...>
+ __once<long long int, _Ts...>
+ __once<unsigned long long int, _Ts...>
+ __once<float, _Ts...>
+ __once<double, _Ts...>
+ __once<long double, _Ts...>
+ __once<const char_type*, _Ts...>
+ __once<basic_string_view<char_type>, _Ts...>
+ __once<const void*, _Ts...>;
return __sum == sizeof...(_Ts);
}
}
template<typename... _Ts>
consteval void
__check_dynamic_spec(size_t __id) noexcept;
// This must not be constexpr.
static void __invalid_dynamic_spec(const char*);
friend __format::_Scanner<_CharT>;
#endif
// This constructor should only be used by the implementation.
constexpr explicit
basic_format_parse_context(basic_string_view<_CharT> __fmt,
size_t __num_args) noexcept
: _M_begin(__fmt.begin()), _M_end(__fmt.end()), _M_num_args(__num_args)
{ }
private:
iterator _M_begin;
iterator _M_end;
enum _Indexing { _Unknown, _Manual, _Auto };
_Indexing _M_indexing = _Unknown;
size_t _M_next_arg_id = 0;
size_t _M_num_args = 0;
};
/// @cond undocumented
template<typename _Tp, template<typename...> class _Class>
constexpr bool __is_specialization_of = false;
template<template<typename...> class _Class, typename... _Args>
constexpr bool __is_specialization_of<_Class<_Args...>, _Class> = true;
namespace __format
{
// pre: first != last
template<typename _CharT>
constexpr pair<unsigned short, const _CharT*>
__parse_integer(const _CharT* __first, const _CharT* __last)
{
if (__first == __last)
__builtin_unreachable();
if constexpr (is_same_v<_CharT, char>)
{
const auto __start = __first;
unsigned short __val = 0;
// N.B. std::from_chars is not constexpr in C++20.
if (__detail::__from_chars_alnum<true>(__first, __last, __val, 10)
&& __first != __start) [[likely]]
return {__val, __first};
}
else
{
constexpr int __n = 32;
char __buf[__n]{};
for (int __i = 0; __i < __n && (__first + __i) != __last; ++__i)
__buf[__i] = __first[__i];
auto [__v, __ptr] = __format::__parse_integer(__buf, __buf + __n);
if (__ptr) [[likely]]
return {__v, __first + (__ptr - __buf)};
}
return {0, nullptr};
}
template<typename _CharT>
constexpr pair<unsigned short, const _CharT*>
__parse_arg_id(const _CharT* __first, const _CharT* __last)
{
if (__first == __last)
__builtin_unreachable();
if (*__first == '0')
return {0, __first + 1}; // No leading zeros allowed, so '0...' == 0
if ('1' <= *__first && *__first <= '9')
{
const unsigned short __id = *__first - '0';
const auto __next = __first + 1;
// Optimize for most likely case of single digit arg-id.
if (__next == __last || !('0' <= *__next && *__next <= '9'))
return {__id, __next};
else
return __format::__parse_integer(__first, __last);
}
return {0, nullptr};
}
enum class _Pres_type : unsigned char {
_Pres_none = 0, // Default type (not valid for integer presentation types).
_Pres_s = 1, // For strings, bool, ranges
// Presentation types for integral types (including bool and charT).
_Pres_c = 2, _Pres_x, _Pres_X, _Pres_d, _Pres_o, _Pres_b, _Pres_B,
// Presentation types for floating-point types
_Pres_g = 1, _Pres_G, _Pres_a, _Pres_A, _Pres_e, _Pres_E, _Pres_f, _Pres_F,
// For pointers, the value are same as hexadecimal presentations for integers
_Pres_p = _Pres_x, _Pres_P = _Pres_X,
_Pres_max = 0xf,
};
using enum _Pres_type;
enum class _Sign : unsigned char {
_Sign_default,
_Sign_plus,
_Sign_minus, // XXX does this need to be distinct from _Sign_default?
_Sign_space,
};
using enum _Sign;
enum _WidthPrec : unsigned char {
_WP_none, // No width/prec specified.
_WP_value, // Fixed width/prec specified.
_WP_from_arg // Use a formatting argument for width/prec.
};
using enum _WidthPrec;
template<typename _Context>
_GLIBCXX_CONSTEXPR_FORMAT size_t
__int_from_arg(const basic_format_arg<_Context>& __arg);
constexpr bool __is_digit(char __c)
{ return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
constexpr bool __is_xdigit(char __c)
{ return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
// Used to make _Spec a non-C++98 POD, so the tail-padding is used.
// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#pod
struct _SpecBase
{ };
template<typename _CharT>
struct _Spec : _SpecBase
{
unsigned short _M_width;
unsigned short _M_prec;
char32_t _M_fill = ' ';
_Align _M_align : 2;
_Sign _M_sign : 2;
unsigned _M_alt : 1;
unsigned _M_localized : 1;
unsigned _M_zero_fill : 1;
_WidthPrec _M_width_kind : 2;
_WidthPrec _M_prec_kind : 2;
unsigned _M_debug : 1;
_Pres_type _M_type : 4;
unsigned _M_reserved : 8;
// This class has 8 bits of tail padding, that can be used by
// derived classes.
using iterator = typename basic_string_view<_CharT>::iterator;
static constexpr _Align
_S_align(_CharT __c) noexcept
{
switch (__c)
{
case '<': return _Align_left;
case '>': return _Align_right;
case '^': return _Align_centre;
default: return _Align_default;
}
}
// pre: __first != __last
constexpr iterator
_M_parse_fill_and_align(iterator __first, iterator __last) noexcept
{ return _M_parse_fill_and_align(__first, __last, "{"); }
// pre: __first != __last
constexpr iterator
_M_parse_fill_and_align(iterator __first, iterator __last, string_view __not_fill) noexcept
{
for (char __c : __not_fill)
if (*__first == static_cast<_CharT>(__c))
return __first;
using namespace __unicode;
if constexpr (__literal_encoding_is_unicode<_CharT>())
{
// Accept any UCS scalar value as fill character.
_Utf32_view<ranges::subrange<iterator>> __uv({__first, __last});
if (!__uv.empty())
{
auto __beg = __uv.begin();
char32_t __c = *__beg++;
if (__is_scalar_value(__c))
if (auto __next = __beg.base(); __next != __last)
if (_Align __align = _S_align(*__next); __align != _Align_default)
{
_M_fill = __c;
_M_align = __align;
return ++__next;
}
}
}
else if (__last - __first >= 2)
if (_Align __align = _S_align(__first[1]); __align != _Align_default)
{
_M_fill = *__first;
_M_align = __align;
return __first + 2;
}
if (_Align __align = _S_align(__first[0]); __align != _Align_default)
{
_M_fill = ' ';
_M_align = __align;
return __first + 1;
}
return __first;
}
static constexpr _Sign
_S_sign(_CharT __c) noexcept
{
switch (__c)
{
case '+': return _Sign_plus;
case '-': return _Sign_minus;
case ' ': return _Sign_space;
default: return _Sign_default;
}
}
// pre: __first != __last
constexpr iterator
_M_parse_sign(iterator __first, iterator) noexcept
{
if (_Sign __sign = _S_sign(*__first); __sign != _Sign_default)
{
_M_sign = __sign;
return __first + 1;
}
return __first;
}
// pre: *__first is valid
constexpr iterator
_M_parse_alternate_form(iterator __first, iterator) noexcept
{
if (*__first == '#')
{
_M_alt = true;
++__first;
}
return __first;
}
// pre: __first != __last
constexpr iterator
_M_parse_zero_fill(iterator __first, iterator /* __last */) noexcept
{
if (*__first == '0')
{
_M_zero_fill = true;
++__first;
}
return __first;
}
// pre: __first != __last
static constexpr iterator
_S_parse_width_or_precision(iterator __first, iterator __last,
unsigned short& __val, bool& __arg_id,
basic_format_parse_context<_CharT>& __pc)
{
if (__format::__is_digit(*__first))
{
auto [__v, __ptr] = __format::__parse_integer(__first, __last);
if (!__ptr)
__throw_format_error("format error: invalid width or precision "
"in format-spec");
__first = __ptr;
__val = __v;
}
else if (*__first == '{')
{
__arg_id = true;
++__first;
if (__first == __last)
__format::__unmatched_left_brace_in_format_string();
if (*__first == '}')
__val = __pc.next_arg_id();
else
{
auto [__v, __ptr] = __format::__parse_arg_id(__first, __last);
if (__ptr == nullptr || __ptr == __last || *__ptr != '}')
__format::__invalid_arg_id_in_format_string();
__first = __ptr;
__pc.check_arg_id(__v);
__val = __v;
}
#if __cpp_lib_format >= 202305L
__pc.check_dynamic_spec_integral(__val);
#endif
++__first; // past the '}'
}
return __first;
}
// pre: __first != __last
constexpr iterator
_M_parse_width(iterator __first, iterator __last,
basic_format_parse_context<_CharT>& __pc)
{
bool __arg_id = false;
if (*__first == '0')
__throw_format_error("format error: width must be non-zero in "
"format string");
auto __next = _S_parse_width_or_precision(__first, __last, _M_width,
__arg_id, __pc);
if (__next != __first)
_M_width_kind = __arg_id ? _WP_from_arg : _WP_value;
return __next;
}
// pre: __first != __last
constexpr iterator
_M_parse_precision(iterator __first, iterator __last,
basic_format_parse_context<_CharT>& __pc)
{
if (__first[0] != '.')
return __first;
iterator __next = ++__first;
bool __arg_id = false;
if (__next != __last)
__next = _S_parse_width_or_precision(__first, __last, _M_prec,
__arg_id, __pc);
if (__next == __first)
__throw_format_error("format error: missing precision after '.' in "
"format string");
_M_prec_kind = __arg_id ? _WP_from_arg : _WP_value;
return __next;
}
// pre: __first != __last
constexpr iterator
_M_parse_locale(iterator __first, iterator /* __last */) noexcept
{
if (*__first == 'L')
{
_M_localized = true;
++__first;
}
return __first;
}
template<typename _Context>
_GLIBCXX_CONSTEXPR_FORMAT size_t
_M_get_width(_Context& __ctx) const
{
size_t __width = 0;
if (_M_width_kind == _WP_value)
__width = _M_width;
else if (_M_width_kind == _WP_from_arg)
__width = __format::__int_from_arg(__ctx.arg(_M_width));
return __width;
}
template<typename _Context>
_GLIBCXX_CONSTEXPR_FORMAT size_t
_M_get_precision(_Context& __ctx) const
{
size_t __prec = -1;
if (_M_prec_kind == _WP_value)
__prec = _M_prec;
else if (_M_prec_kind == _WP_from_arg)
__prec = __format::__int_from_arg(__ctx.arg(_M_prec));
return __prec;
}
};
template<typename _Int>
inline _GLIBCXX_CONSTEXPR_FORMAT char*
__put_sign(_Int __i, _Sign __sign, char* __dest) noexcept
{
if (__i < 0)
*__dest = '-';
else if (__sign == _Sign_plus)
*__dest = '+';
else if (__sign == _Sign_space)
*__dest = ' ';
else
++__dest;
return __dest;
}
// Write STR to OUT (and do so efficiently if OUT is a _Sink_iter).
template<typename _Out, typename _CharT>
requires output_iterator<_Out, const _CharT&>
inline _GLIBCXX_CONSTEXPR_FORMAT _Out
__write(_Out __out, basic_string_view<_CharT> __str)
{
if constexpr (is_same_v<_Out, _Sink_iter<_CharT>>)
{
if (__str.size())
__out = __str;
}
else
for (_CharT __c : __str)
*__out++ = __c;
return __out;
}
// Write STR to OUT with NFILL copies of FILL_CHAR specified by ALIGN.
// pre: __align != _Align_default
template<typename _Out, typename _CharT>
_GLIBCXX_CONSTEXPR_FORMAT _Out
__write_padded(_Out __out, basic_string_view<_CharT> __str,
_Align __align, size_t __nfill, char32_t __fill_char)
{
const size_t __buflen = 0x20;
_CharT __padding_chars[__buflen];
__padding_chars[0] = _CharT();
basic_string_view<_CharT> __padding{__padding_chars, __buflen};
auto __pad = [&__padding] (size_t __n, _Out& __o) {
if (__n == 0)
return;
while (__n > __padding.size())
{
__o = __format::__write(std::move(__o), __padding);
__n -= __padding.size();
}
if (__n != 0)
__o = __format::__write(std::move(__o), __padding.substr(0, __n));
};
size_t __l, __r, __max;
if (__align == _Align_centre)
{
__l = __nfill / 2;
__r = __l + (__nfill & 1);
__max = __r;
}
else if (__align == _Align_right)
{
__l = __nfill;
__r = 0;
__max = __l;
}
else
{
__l = 0;
__r = __nfill;
__max = __r;
}
using namespace __unicode;
if constexpr (__literal_encoding_is_unicode<_CharT>())
if (!__is_single_code_unit<_CharT>(__fill_char)) [[unlikely]]
{
// Encode fill char as multiple code units of type _CharT.
const char32_t __arr[1]{ __fill_char };
_Utf_view<_CharT, span<const char32_t, 1>> __v(__arr);
basic_string<_CharT> __padstr(__v.begin(), __v.end());
__padding = __padstr;
while (__l-- > 0)
__out = __format::__write(std::move(__out), __padding);
__out = __format::__write(std::move(__out), __str);
while (__r-- > 0)
__out = __format::__write(std::move(__out), __padding);
return __out;
}
if (__max < __buflen)
__padding.remove_suffix(__buflen - __max);
else
__max = __buflen;
char_traits<_CharT>::assign(__padding_chars, __max, __fill_char);
__pad(__l, __out);
__out = __format::__write(std::move(__out), __str);
__pad(__r, __out);
return __out;
}
// Write STR to OUT, with alignment and padding as determined by SPEC.
// pre: __spec._M_align != _Align_default || __align != _Align_default
template<typename _CharT, typename _Out>
_GLIBCXX_CONSTEXPR_FORMAT _Out
__write_padded_as_spec(basic_string_view<type_identity_t<_CharT>> __str,
size_t __estimated_width,
basic_format_context<_Out, _CharT>& __fc,
const _Spec<_CharT>& __spec,
_Align __align = _Align_left)
{
size_t __width = __spec._M_get_width(__fc);
if (__width <= __estimated_width)
return __format::__write(__fc.out(), __str);
const size_t __nfill = __width - __estimated_width;
if (__spec._M_align != _Align_default)
__align = __spec._M_align;
return __format::__write_padded(__fc.out(), __str, __align, __nfill,
__spec._M_fill);
}
template<typename _CharT>
_GLIBCXX_CONSTEXPR_FORMAT size_t
__truncate(basic_string_view<_CharT>& __s, size_t __prec)
{
if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
{
if (__prec != (size_t)-1)
return __unicode::__truncate(__s, __prec);
else
return __unicode::__field_width(__s);
}
else
{
__s = __s.substr(0, __prec);
return __s.size();
}
}
enum class _Term_char : unsigned char {
_Term_none,
_Term_quote,
_Term_apos,
};
using enum _Term_char;
template<typename _CharT>
struct _Escapes
{
using _Str_view = basic_string_view<_CharT>;
static consteval
_Str_view _S_all()
{ return _GLIBCXX_WIDEN("\t\\t\n\\n\r\\r\\\\\\\"\\\"'\\'\\u\\x"); }
static consteval
_Str_view _S_tab()
{ return _S_all().substr(0, 3); }
static consteval
_Str_view _S_newline()
{ return _S_all().substr(3, 3); }
static consteval
_Str_view _S_return()
{ return _S_all().substr(6, 3); }
static consteval
_Str_view _S_bslash()
{ return _S_all().substr(9, 3); }
static consteval
_Str_view _S_quote()
{ return _S_all().substr(12, 3); }
static consteval
_Str_view _S_apos()
{ return _S_all().substr(15, 3); }
static consteval
_Str_view _S_u()
{ return _S_all().substr(18, 2); }
static consteval
_Str_view _S_x()
{ return _S_all().substr(20, 2); }
static constexpr
_Str_view _S_term(_Term_char __term)
{
switch (__term)
{
case _Term_none:
return _Str_view();
case _Term_quote:
return _S_quote().substr(0, 1);
case _Term_apos:
return _S_apos().substr(0, 1);
}
__builtin_unreachable();
}
};
template<typename _CharT>
struct _Separators
{
using _Str_view = basic_string_view<_CharT>;
static consteval
_Str_view _S_all()
{ return _GLIBCXX_WIDEN("[]{}(), : "); }
static consteval
_Str_view _S_squares()
{ return _S_all().substr(0, 2); }