forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmtlib_format.h
4486 lines (3990 loc) · 146 KB
/
fmtlib_format.h
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
#pragma once
/*
Formatting library for C++
Copyright (c) 2012 - present, Victor Zverovich
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--- Optional exception to the license ---
As an exception, if, as a result of your compiling your source code, portions
of this Software are embedded into a machine-executable object form of such
source code, you may redistribute such embedded portions in such object form
without including the above copyright and permission notices.
*/
#ifndef CATA_SRC_FMTLIB_FORMAT_H
#define CATA_SRC_FMTLIB_FORMAT_H
#include <algorithm>
#include <cerrno>
#include <cmath>
#include <cstdint>
#include <limits>
#include <memory>
#include <stdexcept>
#include "fmtlib_core.h"
#ifdef __INTEL_COMPILER
# define FMT_ICC_VERSION __INTEL_COMPILER
#elif defined(__ICL)
# define FMT_ICC_VERSION __ICL
#else
# define FMT_ICC_VERSION 0
#endif
#ifdef __NVCC__
# define FMT_CUDA_VERSION (__CUDACC_VER_MAJOR__ * 100 + __CUDACC_VER_MINOR__)
#else
# define FMT_CUDA_VERSION 0
#endif
#ifdef __has_builtin
# define FMT_HAS_BUILTIN(x) __has_builtin(x)
#else
# define FMT_HAS_BUILTIN(x) 0
#endif
#if FMT_GCC_VERSION || FMT_CLANG_VERSION
# define FMT_NOINLINE __attribute__((noinline))
#else
# define FMT_NOINLINE
#endif
#if __cplusplus == 201103L || __cplusplus == 201402L
# if defined(__INTEL_COMPILER) || defined(__PGI)
# define FMT_FALLTHROUGH
# elif defined(__clang__)
# define FMT_FALLTHROUGH [[clang::fallthrough]]
# elif FMT_GCC_VERSION >= 700 && \
(!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 520)
# define FMT_FALLTHROUGH [[gnu::fallthrough]]
# else
# define FMT_FALLTHROUGH
# endif
#elif FMT_HAS_CPP17_ATTRIBUTE(fallthrough) || \
(defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
# define FMT_FALLTHROUGH [[fallthrough]]
#else
# define FMT_FALLTHROUGH
#endif
#ifndef FMT_MAYBE_UNUSED
# if FMT_HAS_CPP17_ATTRIBUTE(maybe_unused)
# define FMT_MAYBE_UNUSED [[maybe_unused]]
# else
# define FMT_MAYBE_UNUSED
# endif
#endif
#ifndef FMT_THROW
# if FMT_EXCEPTIONS
# if FMT_MSC_VER || FMT_NVCC
FMT_BEGIN_NAMESPACE
namespace detail
{
template <typename Exception> inline void do_throw( const Exception &x )
{
// Silence unreachable code warnings in MSVC and NVCC because these
// are nearly impossible to fix in a generic code.
volatile bool b = true;
if( b ) {
throw x;
}
}
} // namespace detail
FMT_END_NAMESPACE
# define FMT_THROW(x) detail::do_throw(x)
# else
# define FMT_THROW(x) throw x
# endif
# else
# define FMT_THROW(x) \
do { \
static_cast<void>(sizeof(x)); \
FMT_ASSERT(false, ""); \
} while (false)
# endif
#endif
#if FMT_EXCEPTIONS
# define FMT_TRY try
# define FMT_CATCH(x) catch (x)
#else
# define FMT_TRY if (true)
# define FMT_CATCH(x) if (false)
#endif
#ifndef FMT_USE_USER_DEFINED_LITERALS
// EDG based compilers (Intel, NVIDIA, Elbrus, etc), GCC and MSVC support UDLs.
# if (FMT_HAS_FEATURE(cxx_user_literals) || FMT_GCC_VERSION >= 407 || \
FMT_MSC_VER >= 1900) && \
(!defined(__EDG_VERSION__) || __EDG_VERSION__ >= /* UDL feature */ 480)
# define FMT_USE_USER_DEFINED_LITERALS 1
# else
# define FMT_USE_USER_DEFINED_LITERALS 0
# endif
#endif
#ifndef FMT_USE_UDL_TEMPLATE
// EDG frontend based compilers (icc, nvcc, PGI, etc) and GCC < 6.4 do not
// properly support UDL templates and GCC >= 9 warns about them.
# if FMT_USE_USER_DEFINED_LITERALS && \
(!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 501) && \
((FMT_GCC_VERSION >= 604 && __cplusplus >= 201402L) || \
FMT_CLANG_VERSION >= 304) && \
!defined(__PGI) && !defined(__NVCC__)
# define FMT_USE_UDL_TEMPLATE 1
# else
# define FMT_USE_UDL_TEMPLATE 0
# endif
#endif
#ifndef FMT_USE_FLOAT
# define FMT_USE_FLOAT 1
#endif
#ifndef FMT_USE_DOUBLE
# define FMT_USE_DOUBLE 1
#endif
#ifndef FMT_USE_LONG_DOUBLE
# define FMT_USE_LONG_DOUBLE 1
#endif
// Defining FMT_REDUCE_INT_INSTANTIATIONS to 1, will reduce the number of
// int_writer template instances to just one by only using the largest integer
// type. This results in a reduction in binary size but will cause a decrease in
// integer formatting performance.
#if !defined(FMT_REDUCE_INT_INSTANTIATIONS)
# define FMT_REDUCE_INT_INSTANTIATIONS 0
#endif
// __builtin_clz is broken in clang with Microsoft CodeGen:
// https://github.com/fmtlib/fmt/issues/519
#if (FMT_GCC_VERSION || FMT_HAS_BUILTIN(__builtin_clz)) && !FMT_MSC_VER
# define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
#endif
#if (FMT_GCC_VERSION || FMT_HAS_BUILTIN(__builtin_clzll)) && !FMT_MSC_VER
# define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
#endif
#if (FMT_GCC_VERSION || FMT_HAS_BUILTIN(__builtin_ctz))
# define FMT_BUILTIN_CTZ(n) __builtin_ctz(n)
#endif
#if (FMT_GCC_VERSION || FMT_HAS_BUILTIN(__builtin_ctzll))
# define FMT_BUILTIN_CTZLL(n) __builtin_ctzll(n)
#endif
#if FMT_MSC_VER
# include <intrin.h> // _BitScanReverse[64], _BitScanForward[64], _umul128
#endif
// Some compilers masquerade as both MSVC and GCC-likes or otherwise support
// __builtin_clz and __builtin_clzll, so only define FMT_BUILTIN_CLZ using the
// MSVC intrinsics if the clz and clzll builtins are not available.
#if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL) && \
!defined(FMT_BUILTIN_CTZLL) && !defined(_MANAGED)
FMT_BEGIN_NAMESPACE
namespace detail
{
// Avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning.
# ifndef __clang__
# pragma intrinsic(_BitScanForward)
# pragma intrinsic(_BitScanReverse)
# endif
# if defined(_WIN64) && !defined(__clang__)
# pragma intrinsic(_BitScanForward64)
# pragma intrinsic(_BitScanReverse64)
# endif
inline int clz( uint32_t x )
{
unsigned long r = 0;
_BitScanReverse( &r, x );
FMT_ASSERT( x != 0, "" );
// Static analysis complains about using uninitialized data
// "r", but the only way that can happen is if "x" is 0,
// which the callers guarantee to not happen.
FMT_SUPPRESS_MSC_WARNING( 6102 )
return 31 ^ static_cast<int>( r );
}
# define FMT_BUILTIN_CLZ(n) detail::clz(n)
inline int clzll( uint64_t x )
{
unsigned long r = 0;
# ifdef _WIN64
_BitScanReverse64( &r, x );
# else
// Scan the high 32 bits.
if( _BitScanReverse( &r, static_cast<uint32_t>( x >> 32 ) ) ) {
return 63 ^ ( r + 32 );
}
// Scan the low 32 bits.
_BitScanReverse( &r, static_cast<uint32_t>( x ) );
# endif
FMT_ASSERT( x != 0, "" );
FMT_SUPPRESS_MSC_WARNING( 6102 ) // Suppress a bogus static analysis warning.
return 63 ^ static_cast<int>( r );
}
# define FMT_BUILTIN_CLZLL(n) detail::clzll(n)
inline int ctz( uint32_t x )
{
unsigned long r = 0;
_BitScanForward( &r, x );
FMT_ASSERT( x != 0, "" );
FMT_SUPPRESS_MSC_WARNING( 6102 ) // Suppress a bogus static analysis warning.
return static_cast<int>( r );
}
# define FMT_BUILTIN_CTZ(n) detail::ctz(n)
inline int ctzll( uint64_t x )
{
unsigned long r = 0;
FMT_ASSERT( x != 0, "" );
FMT_SUPPRESS_MSC_WARNING( 6102 ) // Suppress a bogus static analysis warning.
# ifdef _WIN64
_BitScanForward64( &r, x );
# else
// Scan the low 32 bits.
if( _BitScanForward( &r, static_cast<uint32_t>( x ) ) ) {
return static_cast<int>( r );
}
// Scan the high 32 bits.
_BitScanForward( &r, static_cast<uint32_t>( x >> 32 ) );
r += 32;
# endif
return static_cast<int>( r );
}
# define FMT_BUILTIN_CTZLL(n) detail::ctzll(n)
} // namespace detail
FMT_END_NAMESPACE
#endif
// Enable the deprecated numeric alignment.
#ifndef FMT_DEPRECATED_NUMERIC_ALIGN
# define FMT_DEPRECATED_NUMERIC_ALIGN 0
#endif
FMT_BEGIN_NAMESPACE
namespace detail
{
// An equivalent of `*reinterpret_cast<Dest*>(&source)` that doesn't have
// undefined behavior (e.g. due to type aliasing).
// Example: uint64_t d = bit_cast<uint64_t>(2.718);
template <typename Dest, typename Source>
inline Dest bit_cast( const Source &source )
{
static_assert( sizeof( Dest ) == sizeof( Source ), "size mismatch" );
Dest dest;
std::memcpy( &dest, &source, sizeof( dest ) );
return dest;
}
inline bool is_big_endian()
{
const auto u = 1u;
struct bytes {
char data[sizeof( u )];
};
return bit_cast<bytes>( u ).data[0] == 0;
}
// A fallback implementation of uintptr_t for systems that lack it.
struct fallback_uintptr {
unsigned char value[sizeof( void * )];
fallback_uintptr() = default;
explicit fallback_uintptr( const void *p ) {
*this = bit_cast<fallback_uintptr>( p );
if( is_big_endian() ) {
for( size_t i = 0, j = sizeof( void * ) - 1; i < j; ++i, --j ) {
std::swap( value[i], value[j] );
}
}
}
};
#ifdef UINTPTR_MAX
using uintptr_t = ::uintptr_t;
inline uintptr_t to_uintptr( const void *p )
{
return bit_cast<uintptr_t>( p );
}
#else
using uintptr_t = fallback_uintptr;
inline fallback_uintptr to_uintptr( const void *p )
{
return fallback_uintptr( p );
}
#endif
// Returns the largest possible value for type T. Same as
// std::numeric_limits<T>::max() but shorter and not affected by the max macro.
template <typename T> constexpr T max_value()
{
return ( std::numeric_limits<T>::max )();
}
template <typename T> constexpr int num_bits()
{
return std::numeric_limits<T>::digits;
}
// std::numeric_limits<T>::digits may return 0 for 128-bit ints.
template <> constexpr int num_bits<int128_t>()
{
return 128;
}
template <> constexpr int num_bits<uint128_t>()
{
return 128;
}
template <> constexpr int num_bits<fallback_uintptr>()
{
return static_cast<int>( sizeof( void * ) *
std::numeric_limits<unsigned char>::digits );
}
FMT_INLINE void assume( bool condition )
{
( void )condition;
#if FMT_HAS_BUILTIN(__builtin_assume)
__builtin_assume( condition );
#endif
}
// An approximation of iterator_t for pre-C++20 systems.
template <typename T>
using iterator_t = decltype( std::begin( std::declval<T &>() ) );
template <typename T> using sentinel_t = decltype( std::end( std::declval<T &>() ) );
// A workaround for std::string not having mutable data() until C++17.
template <typename Char> inline Char *get_data( std::basic_string<Char> &s )
{
return &s[0];
}
template <typename Container>
inline typename Container::value_type *get_data( Container &c )
{
return c.data();
}
#if defined(_SECURE_SCL) && _SECURE_SCL
// Make a checked iterator to avoid MSVC warnings.
template <typename T> using checked_ptr = stdext::checked_array_iterator<T *>;
template <typename T> checked_ptr<T> make_checked( T *p, size_t size )
{
return {p, size};
}
#else
template <typename T> using checked_ptr = T*;
template <typename T> inline T *make_checked( T *p, size_t )
{
return p;
}
#endif
template <typename Container, FMT_ENABLE_IF( is_contiguous<Container>::value )>
#if FMT_CLANG_VERSION
__attribute__( ( no_sanitize( "undefined" ) ) )
#endif
inline checked_ptr<typename Container::value_type>
reserve( std::back_insert_iterator<Container> it, size_t n )
{
Container &c = get_container( it );
size_t size = c.size();
c.resize( size + n );
return make_checked( get_data( c ) + size, n );
}
template <typename T>
inline buffer_appender<T> reserve( buffer_appender<T> it, size_t n )
{
buffer<T> &buf = get_container( it );
buf.try_reserve( buf.size() + n );
return it;
}
template <typename Iterator> inline Iterator &reserve( Iterator &it, size_t )
{
return it;
}
template <typename T, typename OutputIt>
constexpr T *to_pointer( OutputIt, size_t )
{
return nullptr;
}
template <typename T> T *to_pointer( buffer_appender<T> it, size_t n )
{
buffer<T> &buf = get_container( it );
auto size = buf.size();
if( buf.capacity() < size + n ) {
return nullptr;
}
buf.try_resize( size + n );
return buf.data() + size;
}
template <typename Container, FMT_ENABLE_IF( is_contiguous<Container>::value )>
inline std::back_insert_iterator<Container> base_iterator(
std::back_insert_iterator<Container> &it,
checked_ptr<typename Container::value_type> )
{
return it;
}
template <typename Iterator>
inline Iterator base_iterator( Iterator, Iterator it )
{
return it;
}
// An output iterator that counts the number of objects written to it and
// discards them.
class counting_iterator
{
private:
size_t count_;
public:
using iterator_category = std::output_iterator_tag;
using difference_type = std::ptrdiff_t;
using pointer = void;
using reference = void;
using _Unchecked_type = counting_iterator; // Mark iterator as checked.
struct value_type {
// NOLINTNEXTLINE(misc-unconventional-assign-operator)
template <typename T> void operator=( const T & ) {}
};
counting_iterator() : count_( 0 ) {}
size_t count() const {
return count_;
}
counting_iterator &operator++() {
++count_;
return *this;
}
counting_iterator operator++( int ) {
auto it = *this;
++*this;
return it;
}
friend counting_iterator operator+( counting_iterator it, difference_type n ) {
it.count_ += static_cast<size_t>( n );
return it;
}
value_type operator*() const {
return {};
}
};
template <typename OutputIt> class truncating_iterator_base
{
protected:
OutputIt out_;
size_t limit_;
size_t count_;
truncating_iterator_base( OutputIt out, size_t limit )
: out_( out ), limit_( limit ), count_( 0 ) {}
public:
using iterator_category = std::output_iterator_tag;
using value_type = typename std::iterator_traits<OutputIt>::value_type;
using difference_type = void;
using pointer = void;
using reference = void;
using _Unchecked_type =
truncating_iterator_base; // Mark iterator as checked.
OutputIt base() const {
return out_;
}
size_t count() const {
return count_;
}
};
// An output iterator that truncates the output and counts the number of objects
// written to it.
template <typename OutputIt,
typename Enable = typename std::is_void<
typename std::iterator_traits<OutputIt>::value_type>::type>
class truncating_iterator;
template <typename OutputIt>
class truncating_iterator<OutputIt, std::false_type>
: public truncating_iterator_base<OutputIt>
{
mutable typename truncating_iterator_base<OutputIt>::value_type blackhole_;
public:
using value_type = typename truncating_iterator_base<OutputIt>::value_type;
truncating_iterator( OutputIt out, size_t limit )
: truncating_iterator_base<OutputIt>( out, limit ) {}
truncating_iterator &operator++() {
if( this->count_++ < this->limit_ ) {
++this->out_;
}
return *this;
}
truncating_iterator operator++( int ) {
auto it = *this;
++*this;
return it;
}
value_type &operator*() const {
return this->count_ < this->limit_ ? *this->out_ : blackhole_;
}
};
template <typename OutputIt>
class truncating_iterator<OutputIt, std::true_type>
: public truncating_iterator_base<OutputIt>
{
public:
truncating_iterator( OutputIt out, size_t limit )
: truncating_iterator_base<OutputIt>( out, limit ) {}
template <typename T> truncating_iterator &operator=( T val ) {
if( this->count_++ < this->limit_ ) {
*this->out_++ = val;
}
return *this;
}
truncating_iterator &operator++() {
return *this;
}
truncating_iterator &operator++( int ) {
return *this;
}
truncating_iterator &operator*() {
return *this;
}
};
template <typename Char>
inline size_t count_code_points( basic_string_view<Char> s )
{
return s.size();
}
// Counts the number of code points in a UTF-8 string.
inline size_t count_code_points( basic_string_view<char> s )
{
const char *data = s.data();
size_t num_code_points = 0;
for( size_t i = 0, size = s.size(); i != size; ++i ) {
if( ( data[i] & 0xc0 ) != 0x80 ) {
++num_code_points;
}
}
return num_code_points;
}
inline size_t count_code_points( basic_string_view<char8_type> s )
{
return count_code_points( basic_string_view<char>(
reinterpret_cast<const char *>( s.data() ), s.size() ) );
}
template <typename Char>
inline size_t code_point_index( basic_string_view<Char> s, size_t n )
{
size_t size = s.size();
return n < size ? n : size;
}
// Calculates the index of the nth code point in a UTF-8 string.
inline size_t code_point_index( basic_string_view<char8_type> s, size_t n )
{
const char8_type *data = s.data();
size_t num_code_points = 0;
for( size_t i = 0, size = s.size(); i != size; ++i ) {
if( ( data[i] & 0xc0 ) != 0x80 && ++num_code_points > n ) {
return i;
}
}
return s.size();
}
template <typename InputIt, typename OutChar>
using needs_conversion = bool_constant <
std::is_same<typename std::iterator_traits<InputIt>::value_type,
char>::value &&
std::is_same<OutChar, char8_type>::value >;
template < typename OutChar, typename InputIt, typename OutputIt,
FMT_ENABLE_IF( !needs_conversion<InputIt, OutChar>::value ) >
OutputIt copy_str( InputIt begin, InputIt end, OutputIt it )
{
return std::copy( begin, end, it );
}
template <typename OutChar, typename InputIt, typename OutputIt,
FMT_ENABLE_IF( needs_conversion<InputIt, OutChar>::value )>
OutputIt copy_str( InputIt begin, InputIt end, OutputIt it )
{
return std::transform( begin, end, it,
[]( char c ) {
return static_cast<char8_type>( c );
} );
}
template <typename Char, typename InputIt>
inline counting_iterator copy_str( InputIt begin, InputIt end,
counting_iterator it )
{
return it + ( end - begin );
}
template <typename T>
using is_fast_float = bool_constant < std::numeric_limits<T>::is_iec559 &&
sizeof( T ) <= sizeof( double ) >;
#ifndef FMT_USE_FULL_CACHE_DRAGONBOX
# define FMT_USE_FULL_CACHE_DRAGONBOX 0
#endif
template <typename T>
template <typename U>
void buffer<T>::append( const U *begin, const U *end )
{
do {
auto count = to_unsigned( end - begin );
try_reserve( size_ + count );
auto free_cap = capacity_ - size_;
if( free_cap < count ) {
count = free_cap;
}
std::uninitialized_copy_n( begin, count, make_checked( ptr_ + size_, count ) );
size_ += count;
begin += count;
} while( begin != end );
}
template <typename OutputIt, typename T, typename Traits>
void iterator_buffer<OutputIt, T, Traits>::flush()
{
out_ = std::copy_n( data_, this->limit( this->size() ), out_ );
this->clear();
}
} // namespace detail
// The number of characters to store in the basic_memory_buffer object itself
// to avoid dynamic memory allocation.
enum { inline_buffer_size = 500 };
/**
\rst
A dynamically growing memory buffer for trivially copyable/constructible types
with the first ``SIZE`` elements stored in the object itself.
You can use one of the following type aliases for common character types:
+----------------+------------------------------+
| Type | Definition |
+================+==============================+
| memory_buffer | basic_memory_buffer<char> |
+----------------+------------------------------+
| wmemory_buffer | basic_memory_buffer<wchar_t> |
+----------------+------------------------------+
**Example**::
fmt::memory_buffer out;
format_to(out, "The answer is {}.", 42);
This will append the following output to the ``out`` object:
.. code-block:: none
The answer is 42.
The output can be converted to an ``std::string`` with ``to_string(out)``.
\endrst
*/
template <typename T, size_t SIZE = inline_buffer_size,
typename Allocator = std::allocator<T>>
class basic_memory_buffer final : public detail::buffer<T>
{
private:
T store_[SIZE];
// Don't inherit from Allocator avoid generating type_info for it.
Allocator alloc_;
// Deallocate memory allocated by the buffer.
void deallocate() {
T *data = this->data();
if( data != store_ ) {
alloc_.deallocate( data, this->capacity() );
}
}
protected:
// NOLINTNEXTLINE(modernize-use-override)
void grow( size_t size ) final FMT_OVERRIDE;
public:
using value_type = T;
using const_reference = const T&;
explicit basic_memory_buffer( const Allocator &alloc = Allocator() )
: alloc_( alloc ) {
this->set( store_, SIZE );
}
~basic_memory_buffer() {
deallocate();
}
private:
// Move data from other to this buffer.
void move( basic_memory_buffer &other ) {
alloc_ = std::move( other.alloc_ );
T *data = other.data();
size_t size = other.size(), capacity = other.capacity();
if( data == other.store_ ) {
this->set( store_, capacity );
std::uninitialized_copy( other.store_, other.store_ + size,
detail::make_checked( store_, capacity ) );
} else {
this->set( data, capacity );
// Set pointer to the inline array so that delete is not called
// when deallocating.
other.set( other.store_, 0 );
}
this->resize( size );
}
public:
/**
\rst
Constructs a :class:`fmt::basic_memory_buffer` object moving the content
of the other object to it.
\endrst
*/
basic_memory_buffer( basic_memory_buffer &&other ) FMT_NOEXCEPT { move( other ); }
/**
\rst
Moves the content of the other ``basic_memory_buffer`` object to this one.
\endrst
*/
basic_memory_buffer &operator=( basic_memory_buffer &&other ) FMT_NOEXCEPT {
FMT_ASSERT( this != &other, "" );
deallocate();
move( other );
return *this;
}
// Returns a copy of the allocator associated with this buffer.
Allocator get_allocator() const {
return alloc_;
}
/**
Resizes the buffer to contain *count* elements. If T is a POD type new
elements may not be initialized.
*/
void resize( size_t count ) {
this->try_resize( count );
}
/** Increases the buffer capacity to *new_capacity*. */
void reserve( size_t new_capacity ) {
this->try_reserve( new_capacity );
}
// Directly append data into the buffer
using detail::buffer<T>::append;
template <typename ContiguousRange>
void append( const ContiguousRange &range ) {
append( range.data(), range.data() + range.size() );
}
};
template <typename T, size_t SIZE, typename Allocator>
void basic_memory_buffer<T, SIZE, Allocator>::grow( size_t size )
{
#ifdef FMT_FUZZ
if( size > 5000 ) {
throw std::runtime_error( "fuzz mode - won't grow that much" );
}
#endif
size_t old_capacity = this->capacity();
size_t new_capacity = old_capacity + old_capacity / 2;
if( size > new_capacity ) {
new_capacity = size;
}
T *old_data = this->data();
T *new_data =
std::allocator_traits<Allocator>::allocate( alloc_, new_capacity );
// The following code doesn't throw, so the raw pointer above doesn't leak.
std::uninitialized_copy( old_data, old_data + this->size(),
detail::make_checked( new_data, new_capacity ) );
this->set( new_data, new_capacity );
// deallocate must not throw according to the standard, but even if it does,
// the buffer already uses the new storage and will deallocate it in
// destructor.
if( old_data != store_ ) {
alloc_.deallocate( old_data, old_capacity );
}
}
using memory_buffer = basic_memory_buffer<char>;
using wmemory_buffer = basic_memory_buffer<wchar_t>;
template <typename T, size_t SIZE, typename Allocator>
struct is_contiguous<basic_memory_buffer<T, SIZE, Allocator>> : std::true_type {
};
/** A formatting error such as invalid format string. */
FMT_CLASS_API
class FMT_API format_error : public std::runtime_error
{
public:
explicit format_error( const char *message ) : std::runtime_error( message ) {}
explicit format_error( const std::string &message )
: std::runtime_error( message ) {}
format_error( const format_error & ) = default;
format_error &operator=( const format_error & ) = default;
format_error( format_error && ) = default;
format_error &operator=( format_error && ) = default;
~format_error() FMT_NOEXCEPT FMT_OVERRIDE;
};
namespace detail
{
template <typename T>
using is_signed =
std::integral_constant < bool, std::numeric_limits<T>::is_signed ||
std::is_same<T, int128_t>::value >;
// Returns true if value is negative, false otherwise.
// Same as `value < 0` but doesn't produce warnings if T is an unsigned type.
template <typename T, FMT_ENABLE_IF( is_signed<T>::value )>
FMT_CONSTEXPR bool is_negative( T value )
{
return value < 0;
}
template < typename T, FMT_ENABLE_IF( !is_signed<T>::value ) >
FMT_CONSTEXPR bool is_negative( T )
{
return false;
}
template <typename T, FMT_ENABLE_IF( std::is_floating_point<T>::value )>
FMT_CONSTEXPR bool is_supported_floating_point( T )
{
return ( std::is_same<T, float>::value && FMT_USE_FLOAT ) ||
( std::is_same<T, double>::value && FMT_USE_DOUBLE ) ||
( std::is_same<T, long double>::value && FMT_USE_LONG_DOUBLE );
}
// Smallest of uint32_t, uint64_t, uint128_t that is large enough to
// represent all values of an integral type T.
template <typename T>
using uint32_or_64_or_128_t =
conditional_t < num_bits<T>() <= 32 && !FMT_REDUCE_INT_INSTANTIATIONS,
uint32_t,
conditional_t<num_bits<T>() <= 64, uint64_t, uint128_t>>;
// 128-bit integer type used internally
struct FMT_EXTERN_TEMPLATE_API uint128_wrapper {
uint128_wrapper() = default;
#if FMT_USE_INT128
uint128_t internal_;
uint128_wrapper( uint64_t high, uint64_t low ) FMT_NOEXCEPT
:
internal_{static_cast<uint128_t>( low ) |
( static_cast<uint128_t>( high ) << 64 )} {}
uint128_wrapper( uint128_t u ) : internal_{u} {}
uint64_t high() const FMT_NOEXCEPT {
return uint64_t( internal_ >> 64 );
}
uint64_t low() const FMT_NOEXCEPT {
return uint64_t( internal_ );
}
uint128_wrapper &operator+=( uint64_t n ) FMT_NOEXCEPT {
internal_ += n;
return *this;
}
#else
uint64_t high_;
uint64_t low_;
uint128_wrapper( uint64_t high, uint64_t low ) FMT_NOEXCEPT :
high_{high},
low_{low} {}
uint64_t high() const FMT_NOEXCEPT {
return high_;
}
uint64_t low() const FMT_NOEXCEPT {
return low_;
}
uint128_wrapper &operator+=( uint64_t n ) FMT_NOEXCEPT {
# if defined(_MSC_VER) && defined(_M_X64)
unsigned char carry = _addcarry_u64( 0, low_, n, &low_ );
_addcarry_u64( carry, high_, 0, &high_ );
return *this;
# else
uint64_t sum = low_ + n;
high_ += ( sum < low_ ? 1 : 0 );
low_ = sum;
return *this;
# endif
}
#endif
};
// Table entry type for divisibility test used internally
template <typename T> struct FMT_EXTERN_TEMPLATE_API divtest_table_entry {
// NOLINTNEXTLINE(cata-no-long)
T mod_inv;
// NOLINTNEXTLINE(cata-no-long)
T max_quotient;
};
// Static data is placed in this class template for the header-only config.
template <typename T = void> struct FMT_EXTERN_TEMPLATE_API basic_data {
static const uint64_t powers_of_10_64[];
static const uint32_t zero_or_powers_of_10_32_new[];
static const uint64_t zero_or_powers_of_10_64_new[];
static const uint64_t grisu_pow10_significands[];
static const int16_t grisu_pow10_exponents[];
static const divtest_table_entry<uint32_t> divtest_table_for_pow5_32[];
static const divtest_table_entry<uint64_t> divtest_table_for_pow5_64[];
static const uint64_t dragonbox_pow10_significands_64[];
static const uint128_wrapper dragonbox_pow10_significands_128[];
// log10(2) = 0x0.4d104d427de7fbcc...
static const uint64_t log10_2_significand = 0x4d104d427de7fbcc;