Debian Codegraph @

podofo/3rdparty/fmtlib/fmt/format.h

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 Formatting library for C++
3
4 Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 --- Optional exception to the license ---
26
27 As an exception, if, as a result of your compiling your source code, portions
28 of this Software are embedded into a machine-executable object form of such
29 source code, you may redistribute such embedded portions in such object form
30 without including the above copyright and permission notices.
31 */
32
33 #ifndef FMT_FORMAT_H_
34 #define FMT_FORMAT_H_
35
36 #ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
37 # define _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
38 # define FMT_REMOVE_TRANSITIVE_INCLUDES
39 #endif
40
41 #include "base.h"
42
43 // libc++ supports string_view in pre-c++17.
44 #if FMT_HAS_INCLUDE(<string_view>) && \
45 (FMT_CPLUSPLUS >= 201703L || defined(_LIBCPP_VERSION))
46 # define FMT_USE_STRING_VIEW
47 #endif
48
49 #ifndef FMT_MODULE
50 # include <stdint.h> // uint32_t
51 # include <stdlib.h> // malloc, free
52 # include <string.h> // memcpy
53
54 # include <cmath> // std::signbit
55 # include <limits> // std::numeric_limits
56 # if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_DUAL_ABI)
57 // Workaround for pre gcc 5 libstdc++.
58 # include <memory> // std::allocator_traits
59 # endif
60 # include <stdexcept> // std::runtime_error
61 # include <string> // std::string
62 # include <system_error> // std::system_error
63
64 // Check FMT_CPLUSPLUS to avoid a warning in MSVC.
65 # if FMT_HAS_INCLUDE(<bit>) && FMT_CPLUSPLUS > 201703L
66 # include <bit> // std::bit_cast
67 # endif
68
69 # if defined(FMT_USE_STRING_VIEW)
70 # include <string_view>
71 # endif
72
73 # if FMT_MSC_VERSION
74 # include <intrin.h> // _BitScanReverse[64], _umul128
75 # endif
76 #endif // FMT_MODULE
77
78 #if defined(FMT_USE_NONTYPE_TEMPLATE_ARGS)
79 // Use the provided definition.
80 #elif defined(__NVCOMPILER)
81 # define FMT_USE_NONTYPE_TEMPLATE_ARGS 0
82 #elif FMT_GCC_VERSION >= 903 && FMT_CPLUSPLUS >= 201709L
83 # define FMT_USE_NONTYPE_TEMPLATE_ARGS 1
84 #elif defined(__cpp_nontype_template_args) && \
85 __cpp_nontype_template_args >= 201911L
86 # define FMT_USE_NONTYPE_TEMPLATE_ARGS 1
87 #elif FMT_CLANG_VERSION >= 1200 && FMT_CPLUSPLUS >= 202002L
88 # define FMT_USE_NONTYPE_TEMPLATE_ARGS 1
89 #else
90 # define FMT_USE_NONTYPE_TEMPLATE_ARGS 0
91 #endif
92
93 #if defined __cpp_inline_variables && __cpp_inline_variables >= 201606L
94 # define FMT_INLINE_VARIABLE inline
95 #else
96 # define FMT_INLINE_VARIABLE
97 #endif
98
99 // Check if RTTI is disabled.
100 #ifdef FMT_USE_RTTI
101 // Use the provided definition.
102 #elif defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || defined(_CPPRTTI) || \
103 defined(__INTEL_RTTI__) || defined(__RTTI)
104 // __RTTI is for EDG compilers. _CPPRTTI is for MSVC.
105 # define FMT_USE_RTTI 1
106 #else
107 # define FMT_USE_RTTI 0
108 #endif
109
110 // Visibility when compiled as a shared library/object.
111 #if defined(FMT_LIB_EXPORT) || defined(FMT_SHARED)
112 # define FMT_SO_VISIBILITY(value) FMT_VISIBILITY(value)
113 #else
114 # define FMT_SO_VISIBILITY(value)
115 #endif
116
117 #if FMT_GCC_VERSION || FMT_CLANG_VERSION
118 # define FMT_NOINLINE __attribute__((noinline))
119 #else
120 # define FMT_NOINLINE
121 #endif
122
123 // Detect constexpr std::string.
124 #if !FMT_USE_CONSTEVAL
125 # define FMT_USE_CONSTEXPR_STRING 0
126 #elif defined(__cpp_lib_constexpr_string) && \
127 __cpp_lib_constexpr_string >= 201907L
128 # if FMT_CLANG_VERSION && FMT_GLIBCXX_RELEASE
129 // clang + libstdc++ are able to work only starting with gcc13.3
130 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113294
131 # if FMT_GLIBCXX_RELEASE < 13
132 # define FMT_USE_CONSTEXPR_STRING 0
133 # elif FMT_GLIBCXX_RELEASE == 13 && __GLIBCXX__ < 20240521
134 # define FMT_USE_CONSTEXPR_STRING 0
135 # else
136 # define FMT_USE_CONSTEXPR_STRING 1
137 # endif
138 # else
139 # define FMT_USE_CONSTEXPR_STRING 1
140 # endif
141 #else
142 # define FMT_USE_CONSTEXPR_STRING 0
143 #endif
144 #if FMT_USE_CONSTEXPR_STRING
145 # define FMT_CONSTEXPR_STRING constexpr
146 #else
147 # define FMT_CONSTEXPR_STRING
148 #endif
149
150 // GCC 4.9 doesn't support qualified names in specializations.
151 namespace std {
152 template <typename T> struct iterator_traits<fmt::basic_appender<T>> {
153 using iterator_category = output_iterator_tag;
154 using value_type = T;
155 using difference_type =
156 decltype(static_cast<int*>(nullptr) - static_cast<int*>(nullptr));
157 using pointer = void;
158 using reference = void;
159 };
160 } // namespace std
161
162 #ifdef FMT_THROW
163 // Use the provided definition.
164 #elif FMT_USE_EXCEPTIONS
165 # define FMT_THROW(x) throw x
166 #else
167 # define FMT_THROW(x) ::fmt::assert_fail(__FILE__, __LINE__, (x).what())
168 #endif
169
170 #ifdef __clang_analyzer__
171 # define FMT_CLANG_ANALYZER 1
172 #else
173 # define FMT_CLANG_ANALYZER 0
174 #endif
175
176 // Defining FMT_REDUCE_INT_INSTANTIATIONS to 1, will reduce the number of
177 // integer formatter template instantiations to just one by only using the
178 // largest integer type. This results in a reduction in binary size but will
179 // cause a decrease in integer formatting performance.
180 #if !defined(FMT_REDUCE_INT_INSTANTIATIONS)
181 # define FMT_REDUCE_INT_INSTANTIATIONS 0
182 #endif
183
184 FMT_BEGIN_NAMESPACE
185
186 template <typename Char, typename Traits, typename Allocator>
187 struct is_contiguous<std::basic_string<Char, Traits, Allocator>>
188 : std::true_type {};
189
190 namespace detail {
191
192 // __builtin_clz is broken in clang with Microsoft codegen:
193 // https://github.com/fmtlib/fmt/issues/519.
194 #if !FMT_MSC_VERSION
195 # if FMT_HAS_BUILTIN(__builtin_clz) || FMT_GCC_VERSION || FMT_ICC_VERSION
196 # define FMT_BUILTIN_CLZ(n) __builtin_clz(n)
197 # endif
198 # if FMT_HAS_BUILTIN(__builtin_clzll) || FMT_GCC_VERSION || FMT_ICC_VERSION
199 # define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n)
200 # endif
201 #endif
202
203 // Some compilers masquerade as both MSVC and GCC but otherwise support
204 // __builtin_clz and __builtin_clzll, so only define FMT_BUILTIN_CLZ using the
205 // MSVC intrinsics if the clz and clzll builtins are not available.
206 #if FMT_MSC_VERSION && !defined(FMT_BUILTIN_CLZLL)
207 // Avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning.
208 # ifndef __clang__
209 # pragma intrinsic(_BitScanReverse)
210 # ifdef _WIN64
211 # pragma intrinsic(_BitScanReverse64)
212 # endif
213 # endif
214
215 inline auto clz(uint32_t x) -> int {
216 FMT_ASSERT(x != 0, "");
217 unsigned long r = 0;
218 _BitScanReverse(&r, x);
219 return 31 ^ static_cast<int>(r);
220 }
221 # define FMT_BUILTIN_CLZ(n) detail::clz(n)
222
223 inline auto clzll(uint64_t x) -> int {
224 FMT_ASSERT(x != 0, "");
225 unsigned long r = 0;
226 # ifdef _WIN64
227 _BitScanReverse64(&r, x);
228 # else
229 // Scan the high 32 bits.
230 if (_BitScanReverse(&r, static_cast<uint32_t>(x >> 32)))
231 return 63 ^ static_cast<int>(r + 32);
232 // Scan the low 32 bits.
233 _BitScanReverse(&r, static_cast<uint32_t>(x));
234 # endif
235 return 63 ^ static_cast<int>(r);
236 }
237 # define FMT_BUILTIN_CLZLL(n) detail::clzll(n)
238 #endif // FMT_MSC_VERSION && !defined(FMT_BUILTIN_CLZLL)
239
240 FMT_CONSTEXPR inline void abort_fuzzing_if(bool condition) {
241 ignore_unused(condition);
242 #ifdef FMT_FUZZ
243 if (condition) throw std::runtime_error("fuzzing limit reached");
244 #endif
245 }
246
247 #if defined(FMT_USE_STRING_VIEW)
248 template <typename Char> using std_string_view = std::basic_string_view<Char>;
249 #else
250 template <typename Char> struct std_string_view {
251 operator basic_string_view<Char>() const;
252 };
253 #endif
254
255 template <typename Char, Char... C> struct string_literal {
256 static constexpr Char value[sizeof...(C)] = {C...};
257 constexpr operator basic_string_view<Char>() const {
258 return {value, sizeof...(C)};
259 }
260 };
261 #if FMT_CPLUSPLUS < 201703L
262 template <typename Char, Char... C>
263 constexpr Char string_literal<Char, C...>::value[sizeof...(C)];
264 #endif
265
266 // Implementation of std::bit_cast for pre-C++20.
267 template <typename To, typename From, FMT_ENABLE_IF(sizeof(To) == sizeof(From))>
268 FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To {
269 #ifdef __cpp_lib_bit_cast
270 if (is_constant_evaluated()) return std::bit_cast<To>(from);
271 #endif
272 auto to = To();
273 // The cast suppresses a bogus -Wclass-memaccess on GCC.
274 memcpy(static_cast<void*>(&to), &from, sizeof(to));
275 return to;
276 }
277
278 inline auto is_big_endian() -> bool {
279 #ifdef _WIN32
280 return false;
281 #elif defined(__BIG_ENDIAN__)
282 return true;
283 #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
284 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
285 #else
286 struct bytes {
287 char data[sizeof(int)];
288 };
289 return bit_cast<bytes>(1).data[0] == 0;
290 #endif
291 }
292
293 class uint128 {
294 private:
295 uint64_t lo_, hi_;
296
297 public:
298 constexpr uint128(uint64_t hi, uint64_t lo) : lo_(lo), hi_(hi) {}
299 constexpr uint128(uint64_t value = 0) : lo_(value), hi_(0) {}
300
301 constexpr auto high() const noexcept -> uint64_t { return hi_; }
302 constexpr auto low() const noexcept -> uint64_t { return lo_; }
303
304 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
305 constexpr explicit operator T() const {
306 return static_cast<T>(lo_);
307 }
308
309 friend constexpr auto operator==(const uint128& lhs, const uint128& rhs)
310 -> bool {
311 return lhs.hi_ == rhs.hi_ && lhs.lo_ == rhs.lo_;
312 }
313 friend constexpr auto operator!=(const uint128& lhs, const uint128& rhs)
314 -> bool {
315 return !(lhs == rhs);
316 }
317 friend constexpr auto operator>(const uint128& lhs, const uint128& rhs)
318 -> bool {
319 return lhs.hi_ != rhs.hi_ ? lhs.hi_ > rhs.hi_ : lhs.lo_ > rhs.lo_;
320 }
321 friend constexpr auto operator|(const uint128& lhs, const uint128& rhs)
322 -> uint128 {
323 return {lhs.hi_ | rhs.hi_, lhs.lo_ | rhs.lo_};
324 }
325 friend constexpr auto operator&(const uint128& lhs, const uint128& rhs)
326 -> uint128 {
327 return {lhs.hi_ & rhs.hi_, lhs.lo_ & rhs.lo_};
328 }
•329 friend constexpr auto operator~(const uint128& n) -> uint128 {
330 return {~n.hi_, ~n.lo_};
331 }
332 friend FMT_CONSTEXPR auto operator+(const uint128& lhs, const uint128& rhs)
333 -> uint128 {
334 auto result = uint128(lhs);
335 result += rhs;
336 return result;
337 }
338 friend FMT_CONSTEXPR auto operator*(const uint128& lhs, uint32_t rhs)
339 -> uint128 {
340 FMT_ASSERT(lhs.hi_ == 0, "");
341 uint64_t hi = (lhs.lo_ >> 32) * rhs;
342 uint64_t lo = (lhs.lo_ & ~uint32_t()) * rhs;
343 uint64_t new_lo = (hi << 32) + lo;
344 return {(hi >> 32) + (new_lo < lo ? 1 : 0), new_lo};
345 }
346 friend constexpr auto operator-(const uint128& lhs, uint64_t rhs) -> uint128 {
347 return {lhs.hi_ - (lhs.lo_ < rhs ? 1 : 0), lhs.lo_ - rhs};
348 }
349 FMT_CONSTEXPR auto operator>>(int shift) const -> uint128 {
350 if (shift == 64) return {0, hi_};
351 if (shift > 64) return uint128(0, hi_) >> (shift - 64);
352 return {hi_ >> shift, (hi_ << (64 - shift)) | (lo_ >> shift)};
353 }
354 FMT_CONSTEXPR auto operator<<(int shift) const -> uint128 {
355 if (shift == 64) return {lo_, 0};
356 if (shift > 64) return uint128(lo_, 0) << (shift - 64);
357 return {hi_ << shift | (lo_ >> (64 - shift)), (lo_ << shift)};
358 }
359 FMT_CONSTEXPR auto operator>>=(int shift) -> uint128& {
360 return *this = *this >> shift;
361 }
362 FMT_CONSTEXPR void operator+=(uint128 n) {
363 uint64_t new_lo = lo_ + n.lo_;
364 uint64_t new_hi = hi_ + n.hi_ + (new_lo < lo_ ? 1 : 0);
365 FMT_ASSERT(new_hi >= hi_, "");
366 lo_ = new_lo;
367 hi_ = new_hi;
368 }
369 FMT_CONSTEXPR void operator&=(uint128 n) {
370 lo_ &= n.lo_;
371 hi_ &= n.hi_;
372 }
373
374 FMT_CONSTEXPR20 auto operator+=(uint64_t n) noexcept -> uint128& {
375 if (is_constant_evaluated()) {
376 lo_ += n;
377 hi_ += (lo_ < n ? 1 : 0);
378 return *this;
379 }
380 #if FMT_HAS_BUILTIN(__builtin_addcll) && !defined(__ibmxl__)
381 ullong carry;
382 lo_ = __builtin_addcll(lo_, n, 0, &carry);
383 hi_ += carry;
384 #elif FMT_HAS_BUILTIN(__builtin_ia32_addcarryx_u64) && !defined(__ibmxl__)
385 ullong result;
386 auto carry = __builtin_ia32_addcarryx_u64(0, lo_, n, &result);
387 lo_ = result;
388 hi_ += carry;
389 #elif defined(_MSC_VER) && defined(_M_AMD64)
390 auto carry = _addcarry_u64(0, lo_, n, &lo_);
391 _addcarry_u64(carry, hi_, 0, &hi_);
392 #else
393 lo_ += n;
394 hi_ += (lo_ < n ? 1 : 0);
395 #endif
396 return *this;
397 }
398 };
399
400 using uint128_t = conditional_t<FMT_USE_INT128, native_uint128, uint128>;
401
402 #ifdef UINTPTR_MAX
403 using uintptr_t = ::uintptr_t;
404 #else
405 using uintptr_t = uint128_t;
406 #endif
407
408 // Returns the largest possible value for type T. Same as
409 // std::numeric_limits<T>::max() but shorter and not affected by the max macro.
410 template <typename T> constexpr auto max_value() -> T {
411 return (std::numeric_limits<T>::max)();
412 }
413 template <typename T> constexpr auto num_bits() -> int {
414 return std::numeric_limits<T>::digits;
415 }
416 // std::numeric_limits<T>::digits may return 0 for 128-bit ints.
417 template <> constexpr auto num_bits<native_int128>() -> int { return 128; }
418 template <> constexpr auto num_bits<native_uint128>() -> int { return 128; }
419 template <> constexpr auto num_bits<uint128>() -> int { return 128; }
420
421 // A heterogeneous bit_cast used for converting 96-bit long double to uint128_t
422 // and 128-bit pointers to uint128.
423 template <typename To, typename From, FMT_ENABLE_IF(sizeof(To) > sizeof(From))>
424 inline auto bit_cast(const From& from) -> To {
425 constexpr auto size = static_cast<int>(sizeof(From) / sizeof(unsigned short));
426 struct data_t {
427 unsigned short value[static_cast<unsigned>(size)];
428 } data = bit_cast<data_t>(from);
429 auto result = To();
430 if (is_big_endian()) {
431 for (int i = 0; i < size; ++i)
432 result = (result << num_bits<unsigned short>()) | data.value[i];
433 } else {
434 for (int i = size - 1; i >= 0; --i)
435 result = (result << num_bits<unsigned short>()) | data.value[i];
436 }
437 return result;
438 }
439
440 template <typename UInt>
441 FMT_CONSTEXPR20 inline auto countl_zero_fallback(UInt n) -> int {
442 int lz = 0;
443 constexpr UInt msb_mask = static_cast<UInt>(1) << (num_bits<UInt>() - 1);
444 for (; (n & msb_mask) == 0; n <<= 1) lz++;
445 return lz;
446 }
447
448 FMT_CONSTEXPR20 inline auto countl_zero(uint32_t n) -> int {
449 #ifdef FMT_BUILTIN_CLZ
450 if (!is_constant_evaluated()) return FMT_BUILTIN_CLZ(n);
451 #endif
452 return countl_zero_fallback(n);
453 }
454
455 FMT_CONSTEXPR20 inline auto countl_zero(uint64_t n) -> int {
456 #ifdef FMT_BUILTIN_CLZLL
457 if (!is_constant_evaluated()) return FMT_BUILTIN_CLZLL(n);
458 #endif
459 return countl_zero_fallback(n);
460 }
461
462 FMT_INLINE void assume(bool condition) {
463 (void)condition;
464 #if FMT_HAS_BUILTIN(__builtin_assume) && !FMT_ICC_VERSION
465 __builtin_assume(condition);
466 #elif FMT_GCC_VERSION
467 if (!condition) __builtin_unreachable();
468 #endif
469 }
470
471 // Attempts to reserve space for n extra characters in the output range.
472 // Returns a pointer to the reserved range or a reference to it.
473 template <typename OutputIt,
474 FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value&&
475 is_contiguous<typename OutputIt::container>::value)>
476 #if FMT_CLANG_VERSION >= 307 && !FMT_ICC_VERSION
477 __attribute__((no_sanitize("undefined")))
478 #endif
479 FMT_CONSTEXPR20 inline auto reserve(OutputIt it, size_t n) ->
480 typename OutputIt::value_type* {
481 auto& c = get_container(it);
482 size_t size = c.size();
483 c.resize(size + n);
484 return &c[size];
485 }
486
487 template <typename T>
488 FMT_CONSTEXPR20 inline auto reserve(basic_appender<T> it, size_t n)
489 -> basic_appender<T> {
490 buffer<T>& buf = get_container(it);
491 buf.try_reserve(buf.size() + n);
492 return it;
493 }
494
495 template <typename Iterator>
496 constexpr auto reserve(Iterator& it, size_t) -> Iterator& {
497 return it;
498 }
499
500 template <typename OutputIt>
501 using reserve_iterator =
502 remove_reference_t<decltype(reserve(std::declval<OutputIt&>(), 0))>;
503
504 template <typename T, typename OutputIt>
505 constexpr auto to_pointer(OutputIt, size_t) -> T* {
506 return nullptr;
507 }
508 template <typename T> FMT_CONSTEXPR auto to_pointer(T*& ptr, size_t n) -> T* {
509 T* begin = ptr;
510 ptr += n;
511 return begin;
512 }
513 template <typename T>
514 FMT_CONSTEXPR20 auto to_pointer(basic_appender<T> it, size_t n) -> T* {
515 buffer<T>& buf = get_container(it);
516 buf.try_reserve(buf.size() + n);
517 auto size = buf.size();
518 if (buf.capacity() < size + n) return nullptr;
519 buf.try_resize(size + n);
520 return buf.data() + size;
521 }
522
523 template <typename OutputIt,
524 FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value&&
525 is_contiguous<typename OutputIt::container>::value)>
526 inline auto base_iterator(OutputIt it,
527 typename OutputIt::container_type::value_type*)
528 -> OutputIt {
529 return it;
530 }
531
532 template <typename Iterator>
533 constexpr auto base_iterator(Iterator, Iterator it) -> Iterator {
534 return it;
535 }
536
537 // <algorithm> is spectacularly slow to compile in C++20 so use a simple fill_n
538 // instead (#1998).
539 template <typename OutputIt, typename Size, typename T>
540 FMT_CONSTEXPR auto fill_n(OutputIt out, Size count, const T& value)
541 -> OutputIt {
542 for (Size i = 0; i < count; ++i) *out++ = value;
543 return out;
544 }
545 template <typename T, typename Size>
546 FMT_CONSTEXPR20 auto fill_n(T* out, Size count, char value) -> T* {
547 if (is_constant_evaluated()) return fill_n<T*, Size, T>(out, count, value);
548 static_assert(sizeof(T) == 1,
549 "sizeof(T) must be 1 to use char for initialization");
550 memset(out, value, to_unsigned(count));
551 return out + count;
552 }
553
554 template <typename T, typename V, typename OutputIt>
555 FMT_CONSTEXPR auto copy(basic_string_view<V> s, OutputIt out) -> OutputIt {
556 return copy<T>(s.begin(), s.end(), out);
557 }
558
559 template <typename OutChar, typename InputIt, typename OutputIt>
560 FMT_CONSTEXPR FMT_NOINLINE auto copy_noinline(InputIt begin, InputIt end,
561 OutputIt out) -> OutputIt {
562 return copy<OutChar>(begin, end, out);
563 }
564
565 // A public domain branchless UTF-8 decoder by Christopher Wellons:
566 // https://github.com/skeeto/branchless-utf8
567 /* Decode the next character, c, from s, reporting errors in e.
568 *
569 * Since this is a branchless decoder, four bytes will be read from the
570 * buffer regardless of the actual length of the next character. This
571 * means the buffer _must_ have at least three bytes of zero padding
572 * following the end of the data stream.
573 *
574 * Errors are reported in e, which will be non-zero if the parsed
575 * character was somehow invalid: invalid byte sequence, non-canonical
576 * encoding, or a surrogate half.
577 *
578 * The function returns a pointer to the next character. When an error
579 * occurs, this pointer will be a guess that depends on the particular
580 * error, but it will always advance at least one byte.
581 */
582 FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e)
583 -> const char* {
584 constexpr int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07};
585 constexpr uint32_t mins[] = {4194304, 0, 128, 2048, 65536};
586 constexpr int shiftc[] = {0, 18, 12, 6, 0};
587 constexpr int shifte[] = {0, 6, 4, 2, 0};
588
589 int len = "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0\0\0\2\2\2\2\3\3\4"
590 [static_cast<unsigned char>(*s) >> 3];
591 // Compute the pointer to the next character early so that the next
592 // iteration can start working on the next character. Neither Clang
593 // nor GCC figure out this reordering on their own.
594 const char* next = s + len + !len;
595
596 using uchar = unsigned char;
597
598 // Assume a four-byte character and load four bytes. Unused bits are
599 // shifted out.
600 *c = uint32_t(uchar(s[0]) & masks[len]) << 18;
601 *c |= uint32_t(uchar(s[1]) & 0x3f) << 12;
602 *c |= uint32_t(uchar(s[2]) & 0x3f) << 6;
603 *c |= uint32_t(uchar(s[3]) & 0x3f) << 0;
604 *c >>= shiftc[len];
605
606 // Accumulate the various error conditions.
607 *e = (*c < mins[len]) << 6; // non-canonical encoding
608 *e |= ((*c >> 11) == 0x1b) << 7; // surrogate half?
609 *e |= (*c > 0x10FFFF) << 8; // out of range?
610 *e |= (uchar(s[1]) & 0xc0) >> 2;
611 *e |= (uchar(s[2]) & 0xc0) >> 4;
612 *e |= uchar(s[3]) >> 6;
613 *e ^= 0x2a; // top two bits of each tail byte correct?
614 *e >>= shifte[len];
615
616 return next;
617 }
618
619 constexpr FMT_INLINE_VARIABLE uint32_t invalid_code_point = ~uint32_t();
620
621 // Invokes f(cp, sv) for every code point cp in s with sv being the string view
622 // corresponding to the code point. cp is invalid_code_point on error.
623 template <typename F>
624 FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) {
625 auto decode = [f](const char* buf_ptr, const char* ptr) {
626 auto cp = uint32_t();
627 auto error = 0;
628 auto end = utf8_decode(buf_ptr, &cp, &error);
629 bool result = f(error ? invalid_code_point : cp,
630 string_view(ptr, error ? 1 : to_unsigned(end - buf_ptr)));
631 return result ? (error ? buf_ptr + 1 : end) : nullptr;
632 };
633
634 auto p = s.data();
635 const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars.
636 if (s.size() >= block_size) {
637 for (auto end = p + s.size() - block_size + 1; p < end;) {
638 p = decode(p, p);
639 if (!p) return;
640 }
641 }
642 auto num_chars_left = to_unsigned(s.data() + s.size() - p);
643 if (num_chars_left == 0) return;
644
645 // Suppress bogus -Wstringop-overflow.
646 if (FMT_GCC_VERSION) num_chars_left &= 3;
647 char buf[2 * block_size - 1] = {};
648 copy<char>(p, p + num_chars_left, buf);
649 const char* buf_ptr = buf;
650 do {
651 auto end = decode(buf_ptr, p);
652 if (!end) return;
653 p += end - buf_ptr;
654 buf_ptr = end;
655 } while (buf_ptr < buf + num_chars_left);
656 }
657
658 FMT_CONSTEXPR inline auto display_width_of(uint32_t cp) noexcept -> size_t {
659 return to_unsigned(
660 1 + (cp >= 0x1100 &&
661 (cp <= 0x115f || // Hangul Jamo init. consonants
662 cp == 0x2329 || // LEFT-POINTING ANGLE BRACKET
663 cp == 0x232a || // RIGHT-POINTING ANGLE BRACKET
664 // CJK ... Yi except IDEOGRAPHIC HALF FILL SPACE:
665 (cp >= 0x2e80 && cp <= 0xa4cf && cp != 0x303f) ||
666 (cp >= 0xac00 && cp <= 0xd7a3) || // Hangul Syllables
667 (cp >= 0xf900 && cp <= 0xfaff) || // CJK Compatibility Ideographs
668 (cp >= 0xfe10 && cp <= 0xfe19) || // Vertical Forms
669 (cp >= 0xfe30 && cp <= 0xfe6f) || // CJK Compatibility Forms
670 (cp >= 0xff00 && cp <= 0xff60) || // Fullwidth Forms
671 (cp >= 0xffe0 && cp <= 0xffe6) || // Fullwidth Forms
672 (cp >= 0x20000 && cp <= 0x2fffd) || // CJK
673 (cp >= 0x30000 && cp <= 0x3fffd) ||
674 // Miscellaneous Symbols and Pictographs + Emoticons:
675 (cp >= 0x1f300 && cp <= 0x1f64f) ||
676 // Supplemental Symbols and Pictographs:
677 (cp >= 0x1f900 && cp <= 0x1f9ff))));
678 }
679
680 template <typename T> struct is_integral : std::is_integral<T> {};
681 template <> struct is_integral<native_int128> : std::true_type {};
682 template <> struct is_integral<uint128_t> : std::true_type {};
683
684 template <typename T>
685 using is_signed =
686 std::integral_constant<bool, std::numeric_limits<T>::is_signed ||
687 std::is_same<T, native_int128>::value>;
688
689 template <typename T>
690 using is_integer =
691 bool_constant<is_integral<T>::value && !std::is_same<T, bool>::value &&
692 !std::is_same<T, char>::value &&
693 !std::is_same<T, wchar_t>::value>;
694
695 #if defined(FMT_USE_FLOAT128)
696 // Use the provided definition.
697 #elif FMT_CLANG_VERSION >= 309 && FMT_HAS_INCLUDE(<quadmath.h>)
698 # define FMT_USE_FLOAT128 1
699 #elif FMT_GCC_VERSION && defined(_GLIBCXX_USE_FLOAT128) && \
700 !defined(__STRICT_ANSI__)
701 # define FMT_USE_FLOAT128 1
702 #else
703 # define FMT_USE_FLOAT128 0
704 #endif
705 #if FMT_USE_FLOAT128
706 using float128 = __float128;
707 #else
708 struct float128 {};
709 #endif
710
711 template <typename T> using is_float128 = std::is_same<T, float128>;
712
713 template <typename T> struct is_floating_point : std::is_floating_point<T> {};
714 template <> struct is_floating_point<float128> : std::true_type {};
715
716 template <typename T, bool = is_floating_point<T>::value>
717 struct is_fast_float : bool_constant<std::numeric_limits<T>::is_iec559 &&
718 sizeof(T) <= sizeof(double)> {};
719 template <typename T> struct is_fast_float<T, false> : std::false_type {};
720
721 template <typename T>
722 using fast_float_t = conditional_t<sizeof(T) == sizeof(double), double, float>;
723
724 template <typename T>
725 using is_double_double = bool_constant<std::numeric_limits<T>::digits == 106>;
726
727 FMT_API auto allocate(size_t size) -> void*;
728
729 // An allocator that uses malloc/free to allow removing dependency on the C++
730 // standard library runtime. std::decay is used for back_inserter to be found by
731 // ADL when applied to memory_buffer.
732 template <typename T> struct allocator : private std::decay<void> {
733 using value_type = T;
734
735 auto allocate(size_t n) -> T* {
736 FMT_ASSERT(n <= max_value<size_t>() / sizeof(T), "");
737 return static_cast<T*>(detail::allocate(n * sizeof(T)));
738 }
739
740 void deallocate(T* p, size_t) { free(p); }
741
742 constexpr friend auto operator==(allocator, allocator) noexcept -> bool {
743 return true; // All instances of this allocator are equivalent.
744 }
745 constexpr friend auto operator!=(allocator, allocator) noexcept -> bool {
746 return false;
747 }
748 };
749
750 template <typename Formatter>
751 FMT_CONSTEXPR auto maybe_set_debug_format(Formatter& f, bool set)
752 -> decltype(f.set_debug_format(set)) {
753 f.set_debug_format(set);
754 }
755 template <typename Formatter>
756 FMT_CONSTEXPR void maybe_set_debug_format(Formatter&, ...) {}
757
758 } // namespace detail
759
760 FMT_BEGIN_EXPORT
761
762 // The number of characters to store in the basic_memory_buffer object itself
763 // to avoid dynamic memory allocation.
764 enum { inline_buffer_size = 500 };
765
766 /**
767 * A dynamically growing memory buffer for trivially copyable/constructible
768 * types with the first `SIZE` elements stored in the object itself. Most
769 * commonly used via the `memory_buffer` alias for `char`.
770 *
771 * **Example**:
772 *
773 * auto out = fmt::memory_buffer();
774 * fmt::format_to(std::back_inserter(out), "The answer is {}.", 42);
775 *
776 * This will append "The answer is 42." to `out`. The buffer content can be
777 * converted to `std::string` with `to_string(out)`.
778 */
779 template <typename T, size_t SIZE = inline_buffer_size,
780 typename Allocator = detail::allocator<T>>
781 class basic_memory_buffer : public detail::buffer<T> {
782 private:
783 T store_[SIZE];
784
785 // Don't inherit from Allocator to avoid generating type_info for it.
786 FMT_NO_UNIQUE_ADDRESS Allocator alloc_;
787
788 // Deallocate memory allocated by the buffer.
789 FMT_CONSTEXPR20 void deallocate() {
790 T* data = this->data();
791 if (data != store_) alloc_.deallocate(data, this->capacity());
792 }
793
794 static FMT_CONSTEXPR20 void grow(detail::buffer<T>& buf, size_t size) {
795 detail::abort_fuzzing_if(size > 5000);
796 auto& self = static_cast<basic_memory_buffer&>(buf);
797 const size_t max_size =
798 std::allocator_traits<Allocator>::max_size(self.alloc_);
799 size_t old_capacity = buf.capacity();
800 size_t new_capacity = old_capacity + old_capacity / 2;
801 if (size > new_capacity)
802 new_capacity = size;
803 else if (new_capacity > max_size)
804 new_capacity = max_of(size, max_size);
805 T* old_data = buf.data();
806 T* new_data = self.alloc_.allocate(new_capacity);
807 // Suppress a bogus -Wstringop-overflow in gcc 13.1 (#3481).
808 detail::assume(buf.size() <= new_capacity);
809 // The following code doesn't throw, so the raw pointer above doesn't leak.
810 memcpy(new_data, old_data, buf.size() * sizeof(T));
811 self.set(new_data, new_capacity);
812 // deallocate must not throw according to the standard, but even if it does,
813 // the buffer already uses the new storage and will deallocate it in
814 // destructor.
815 if (old_data != self.store_) self.alloc_.deallocate(old_data, old_capacity);
816 }
817
818 public:
819 using value_type = T;
820 using const_reference = const T&;
821
822 FMT_CONSTEXPR explicit basic_memory_buffer(
823 const Allocator& alloc = Allocator())
824 : detail::buffer<T>(grow), alloc_(alloc) {
825 this->set(store_, SIZE);
826 if (detail::is_constant_evaluated()) detail::fill_n(store_, SIZE, T());
827 }
828 FMT_CONSTEXPR20 ~basic_memory_buffer() { deallocate(); }
829
830 private:
831 template <typename Alloc = Allocator,
832 FMT_ENABLE_IF(std::allocator_traits<Alloc>::
833 propagate_on_container_move_assignment::value)>
834 FMT_CONSTEXPR20 auto move_alloc(basic_memory_buffer& other) -> bool {
835 alloc_ = std::move(other.alloc_);
836 return true;
837 }
838 // If the allocator does not propagate then copy the data from other.
839 template <typename Alloc = Allocator,
840 FMT_ENABLE_IF(!std::allocator_traits<Alloc>::
841 propagate_on_container_move_assignment::value)>
842 FMT_CONSTEXPR20 auto move_alloc(basic_memory_buffer& other) -> bool {
843 T* data = other.data();
844 if (alloc_ == other.alloc_ || data == other.store_) return true;
845 size_t size = other.size();
846 // Perform copy operation, allocators are different.
847 this->resize(size);
848 detail::copy<T>(data, data + size, this->data());
849 return false;
850 }
851
852 // Move data from other to this buffer.
853 FMT_CONSTEXPR20 void move(basic_memory_buffer& other) {
854 T* data = other.data();
855 size_t size = other.size(), capacity = other.capacity();
856 if (!move_alloc(other)) return;
857 if (data == other.store_) {
858 this->set(store_, capacity);
859 detail::copy<T>(other.store_, other.store_ + size, store_);
860 } else {
861 this->set(data, capacity);
862 // Set pointer to the inline array so that delete is not called
863 // when deallocating.
864 other.set(other.store_, 0);
865 other.clear();
866 }
867 this->resize(size);
868 }
869
870 public:
871 /// Constructs a `basic_memory_buffer` object moving the content of the other
872 /// object to it.
873 FMT_CONSTEXPR20 basic_memory_buffer(basic_memory_buffer&& other) noexcept
874 : detail::buffer<T>(grow) {
875 move(other);
876 }
877
878 /// Moves the content of the other `basic_memory_buffer` object to this one.
879 auto operator=(basic_memory_buffer&& other) noexcept -> basic_memory_buffer& {
880 FMT_ASSERT(this != &other, "");
881 deallocate();
882 move(other);
883 return *this;
884 }
885
886 // Returns a copy of the allocator associated with this buffer.
887 auto get_allocator() const -> Allocator { return alloc_; }
888
889 /// Resizes the buffer to contain `count` elements. If T is a POD type new
890 /// elements may not be initialized.
891 FMT_CONSTEXPR void resize(size_t count) { this->try_resize(count); }
892
893 /// Increases the buffer capacity to `new_capacity`.
894 void reserve(size_t new_capacity) { this->try_reserve(new_capacity); }
895
896 using detail::buffer<T>::append;
897 template <typename ContiguousRange>
898 FMT_CONSTEXPR20 void append(const ContiguousRange& range) {
899 append(range.data(), range.data() + range.size());
900 }
901 };
902
903 using memory_buffer = basic_memory_buffer<char>;
904
905 template <size_t SIZE>
906 FMT_NODISCARD auto to_string(const basic_memory_buffer<char, SIZE>& buf)
907 -> std::string {
908 auto size = buf.size();
909 detail::assume(size < std::string().max_size());
910 return {buf.data(), size};
911 }
912
913 // A writer to a buffered stream. It doesn't own the underlying stream.
914 class writer {
915 private:
916 detail::buffer<char>* buf_;
917
918 // We cannot create a file buffer in advance because any write to a FILE may
919 // invalidate it.
920 FILE* file_;
921
922 public:
923 inline writer(FILE* f) : buf_(nullptr), file_(f) {}
924 inline writer(detail::buffer<char>& buf) : buf_(&buf) {}
925
926 /// Formats `args` according to specifications in `fmt` and writes the
927 /// output to the file.
928 template <typename... T> void print(format_string<T...> fmt, T&&... args) {
929 if (buf_)
930 fmt::format_to(appender(*buf_), fmt, std::forward<T>(args)...);
931 else
932 fmt::print(file_, fmt, std::forward<T>(args)...);
933 }
934 };
935
936 class string_buffer {
937 private:
938 std::string str_;
939 detail::container_buffer<std::string> buf_;
940
941 public:
942 inline string_buffer() : buf_(str_) {}
943
944 inline operator writer() { return buf_; }
945 inline auto str() -> std::string& { return str_; }
946 };
947
948 template <typename T, size_t SIZE, typename Allocator>
949 struct is_contiguous<basic_memory_buffer<T, SIZE, Allocator>> : std::true_type {
950 };
951
952 // Suppress a misleading warning in older versions of clang.
953 FMT_PRAGMA_CLANG(diagnostic ignored "-Wweak-vtables")
954
955 /// An error reported from a formatting function.
956 class FMT_SO_VISIBILITY("default") format_error : public std::runtime_error {
957 public:
958 using std::runtime_error::runtime_error;
959 };
960
961 class loc_value;
962
963 FMT_END_EXPORT
964 namespace detail {
965 FMT_API auto write_console(int fd, string_view text) -> bool;
966 FMT_API void print(FILE*, string_view);
967 } // namespace detail
968
969 namespace detail {
970 template <typename Char, size_t N> struct fixed_string {
971 FMT_CONSTEXPR fixed_string(const Char (&s)[N]) {
972 detail::copy<Char, const Char*, Char*>(static_cast<const Char*>(s), s + N,
973 data);
974 }
975 Char data[N] = {};
976 };
977
978 // Converts a compile-time string to basic_string_view.
979 FMT_EXPORT template <typename Char, size_t N>
980 constexpr auto compile_string_to_view(const Char (&s)[N])
981 -> basic_string_view<Char> {
982 // Remove trailing NUL character if needed. Won't be present if this is used
983 // with a raw character array (i.e. not defined as a string).
984 return {s, N - (std::char_traits<Char>::to_int_type(s[N - 1]) == 0 ? 1 : 0)};
985 }
986 FMT_EXPORT template <typename Char>
987 constexpr auto compile_string_to_view(basic_string_view<Char> s)
988 -> basic_string_view<Char> {
989 return s;
990 }
991
992 // Returns true if value is negative, false otherwise.
993 // Same as `value < 0` but doesn't produce warnings if T is an unsigned type.
994 template <typename T, FMT_ENABLE_IF(is_signed<T>::value)>
995 constexpr auto is_negative(T value) -> bool {
996 return value < 0;
997 }
998 template <typename T, FMT_ENABLE_IF(!is_signed<T>::value)>
999 constexpr auto is_negative(T) -> bool {
1000 return false;
1001 }
1002
1003 // Smallest of uint32_t, uint64_t, uint128_t that is large enough to
1004 // represent all values of an integral type T.
1005 template <typename T>
1006 using uint32_or_64_or_128_t =
1007 conditional_t<num_bits<T>() <= 32 && !FMT_REDUCE_INT_INSTANTIATIONS,
1008 uint32_t,
1009 conditional_t<num_bits<T>() <= 64, uint64_t, uint128_t>>;
1010 template <typename T>
1011 using uint64_or_128_t = conditional_t<num_bits<T>() <= 64, uint64_t, uint128_t>;
1012
1013 #define FMT_POWERS_OF_10(factor) \
1014 factor * 10, (factor) * 100, (factor) * 1000, (factor) * 10000, \
1015 (factor) * 100000, (factor) * 1000000, (factor) * 10000000, \
1016 (factor) * 100000000, (factor) * 1000000000
1017
1018 // Converts value in the range [0, 100) to a string. GCC generates a bit better
1019 // code when value is pointer-size (https://www.godbolt.org/z/5fEPMT1cc).
1020 inline auto digits2(size_t value) noexcept -> const char* {
1021 // Align data since unaligned access may be slower when crossing a
1022 // hardware-specific boundary.
1023 alignas(2) static constexpr char data[] =
1024 "0001020304050607080910111213141516171819"
1025 "2021222324252627282930313233343536373839"
1026 "4041424344454647484950515253545556575859"
1027 "6061626364656667686970717273747576777879"
1028 "8081828384858687888990919293949596979899";
1029 return &data[value * 2];
1030 }
1031
1032 // Given i in [0, 100), let x be the first 7 digits after
1033 // the decimal point of i / 100 in base 2, the first 2 bytes
1034 // after digits2_i(x) is the string representation of i.
1035 inline auto digits2_i(size_t value) noexcept -> const char* {
1036 alignas(2) static constexpr char data[] =
1037 "00010203 0405060707080910 1112"
1038 "131414151617 18192021 222324 "
1039 "25262728 2930313232333435 3637"
1040 "383939404142 43444546 474849 "
1041 "50515253 5455565757585960 6162"
1042 "636464656667 68697071 727374 "
1043 "75767778 7980818282838485 8687"
1044 "888989909192 93949596 979899 ";
1045 return &data[value * 2];
1046 }
1047
1048 template <typename Char> constexpr auto getsign(sign s) -> Char {
1049 return static_cast<Char>(static_cast<char>(
1050 ((' ' << 24) | ('+' << 16) | ('-' << 8)) >> (static_cast<int>(s) * 8)));
1051 }
1052
1053 template <typename T> FMT_CONSTEXPR auto count_digits_fallback(T n) -> int {
1054 int count = 1;
1055 for (;;) {
1056 // Integer division is slow so do it for a group of four digits instead
1057 // of for every digit. The idea comes from the talk by Alexandrescu
1058 // "Three Optimization Tips for C++". See speed-test for a comparison.
1059 if (n < 10) return count;
1060 if (n < 100) return count + 1;
1061 if (n < 1000) return count + 2;
1062 if (n < 10000) return count + 3;
1063 n /= 10000u;
1064 count += 4;
1065 }
1066 }
1067 #if FMT_USE_INT128
1068 FMT_CONSTEXPR inline auto count_digits(native_uint128 n) -> int {
1069 return count_digits_fallback(n);
1070 }
1071 #endif
1072
1073 #ifdef FMT_BUILTIN_CLZLL
1074 // It is a separate function rather than a part of count_digits to workaround
1075 // the lack of static constexpr in constexpr functions.
1076 inline auto do_count_digits(uint64_t n) -> int {
1077 // This has comparable performance to the version by Kendall Willets
1078 // (https://github.com/fmtlib/format-benchmark/blob/master/digits10)
1079 // but uses smaller tables.
1080 // Maps bsr(n) to ceil(log10(pow(2, bsr(n) + 1) - 1)).
1081 static constexpr uint8_t bsr2log10[] = {
1082 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
1083 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10,
1084 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
1085 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20};
1086 auto t = bsr2log10[FMT_BUILTIN_CLZLL(n | 1) ^ 63];
1087 static constexpr uint64_t zero_or_powers_of_10[] = {
1088 0, 0, FMT_POWERS_OF_10(1U), FMT_POWERS_OF_10(1000000000ULL),
1089 10000000000000000000ULL};
1090 return t - (n < zero_or_powers_of_10[t]);
1091 }
1092 #endif
1093
1094 // Returns the number of decimal digits in n. Leading zeros are not counted
1095 // except for n == 0 in which case count_digits returns 1.
1096 FMT_CONSTEXPR20 inline auto count_digits(uint64_t n) -> int {
1097 #ifdef FMT_BUILTIN_CLZLL
1098 if (!is_constant_evaluated() && !FMT_OPTIMIZE_SIZE) return do_count_digits(n);
1099 #endif
1100 return count_digits_fallback(n);
1101 }
1102
1103 // Counts the number of digits in n. BITS = log2(radix).
1104 template <int BITS, typename UInt>
1105 FMT_CONSTEXPR auto count_digits(UInt n) -> int {
1106 #ifdef FMT_BUILTIN_CLZ
1107 if (!is_constant_evaluated() && num_bits<UInt>() == 32)
1108 return (FMT_BUILTIN_CLZ(static_cast<uint32_t>(n) | 1) ^ 31) / BITS + 1;
1109 #endif
1110 // Lambda avoids unreachable code warnings from NVHPC.
1111 return [](UInt m) {
1112 int num_digits = 0;
1113 do {
1114 ++num_digits;
1115 } while ((m >>= BITS) != 0);
1116 return num_digits;
1117 }(n);
1118 }
1119
1120 #ifdef FMT_BUILTIN_CLZ
1121 // It is a separate function rather than a part of count_digits to workaround
1122 // the lack of static constexpr in constexpr functions.
1123 FMT_INLINE auto do_count_digits(uint32_t n) -> int {
1124 // An optimization by Kendall Willets from https://bit.ly/3uOIQrB.
1125 // This increments the upper 32 bits (log10(T) - 1) when >= T is added.
1126 # define FMT_INC(T) (((sizeof(#T) - 1ull) << 32) - T)
1127 static constexpr uint64_t table[] = {
1128 FMT_INC(0), FMT_INC(0), FMT_INC(0), // 8
1129 FMT_INC(10), FMT_INC(10), FMT_INC(10), // 64
1130 FMT_INC(100), FMT_INC(100), FMT_INC(100), // 512
1131 FMT_INC(1000), FMT_INC(1000), FMT_INC(1000), // 4096
1132 FMT_INC(10000), FMT_INC(10000), FMT_INC(10000), // 32k
1133 FMT_INC(100000), FMT_INC(100000), FMT_INC(100000), // 256k
1134 FMT_INC(1000000), FMT_INC(1000000), FMT_INC(1000000), // 2048k
1135 FMT_INC(10000000), FMT_INC(10000000), FMT_INC(10000000), // 16M
1136 FMT_INC(100000000), FMT_INC(100000000), FMT_INC(100000000), // 128M
1137 FMT_INC(1000000000), FMT_INC(1000000000), FMT_INC(1000000000), // 1024M
1138 FMT_INC(1000000000), FMT_INC(1000000000) // 4B
1139 };
1140 auto inc = table[FMT_BUILTIN_CLZ(n | 1) ^ 31];
1141 return static_cast<int>((n + inc) >> 32);
1142 }
1143 #endif
1144
1145 // Optional version of count_digits for better performance on 32-bit platforms.
1146 FMT_CONSTEXPR20 inline auto count_digits(uint32_t n) -> int {
1147 #ifdef FMT_BUILTIN_CLZ
1148 if (!is_constant_evaluated() && !FMT_OPTIMIZE_SIZE) return do_count_digits(n);
1149 #endif
1150 return count_digits_fallback(n);
1151 }
1152
1153 template <typename Int> constexpr auto digits10() noexcept -> int {
1154 return std::numeric_limits<Int>::digits10;
1155 }
1156 template <> constexpr auto digits10<native_int128>() noexcept -> int {
1157 return 38;
1158 }
1159 template <> constexpr auto digits10<uint128_t>() noexcept -> int { return 38; }
1160
1161 template <typename Char> struct thousands_sep_result {
1162 std::string grouping;
1163 Char thousands_sep;
1164 };
1165
1166 template <typename Char>
1167 FMT_API auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result<Char>;
1168 template <typename Char>
1169 inline auto thousands_sep(locale_ref loc) -> thousands_sep_result<Char> {
1170 auto result = thousands_sep_impl<char>(loc);
1171 return {std::move(result.grouping), Char(result.thousands_sep)};
1172 }
1173 template <>
1174 inline auto thousands_sep(locale_ref loc) -> thousands_sep_result<wchar_t> {
1175 return thousands_sep_impl<wchar_t>(loc);
1176 }
1177
1178 template <typename Char>
1179 FMT_API auto decimal_point_impl(locale_ref loc) -> Char;
1180 template <typename Char> inline auto decimal_point(locale_ref loc) -> Char {
1181 return Char(decimal_point_impl<char>(loc));
1182 }
1183 template <> inline auto decimal_point(locale_ref loc) -> wchar_t {
1184 return decimal_point_impl<wchar_t>(loc);
1185 }
1186
1187 #ifndef FMT_HEADER_ONLY
1188 FMT_BEGIN_EXPORT
1189 extern template FMT_API auto thousands_sep_impl<char>(locale_ref)
1190 -> thousands_sep_result<char>;
1191 extern template FMT_API auto thousands_sep_impl<wchar_t>(locale_ref)
1192 -> thousands_sep_result<wchar_t>;
1193 extern template FMT_API auto decimal_point_impl(locale_ref) -> char;
1194 extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t;
1195 FMT_END_EXPORT
1196 #endif // FMT_HEADER_ONLY
1197
1198 // Compares two characters for equality.
1199 template <typename Char> auto equal2(const Char* lhs, const char* rhs) -> bool {
1200 return lhs[0] == Char(rhs[0]) && lhs[1] == Char(rhs[1]);
1201 }
1202 inline auto equal2(const char* lhs, const char* rhs) -> bool {
1203 return memcmp(lhs, rhs, 2) == 0;
1204 }
1205
1206 // Writes a two-digit value to out.
1207 template <typename Char>
1208 FMT_CONSTEXPR20 FMT_INLINE void write2digits(Char* out, size_t value) {
1209 if (!is_constant_evaluated() && std::is_same<Char, char>::value &&
1210 !FMT_OPTIMIZE_SIZE) {
1211 memcpy(out, digits2(value), 2);
1212 return;
1213 }
1214 *out++ = static_cast<Char>('0' + value / 10);
1215 *out = static_cast<Char>('0' + value % 10);
1216 }
1217
1218 template <typename Char>
1219 FMT_INLINE void write2digits_i(Char* out, size_t value) {
1220 if (std::is_same<Char, char>::value && !FMT_OPTIMIZE_SIZE) {
1221 memcpy(out, digits2_i(value), 2);
1222 return;
1223 }
1224 *out++ = static_cast<Char>(digits2_i(value)[0]);
1225 *out = static_cast<Char>(digits2_i(value)[1]);
1226 }
1227
1228 // Formats a decimal unsigned integer value writing to out pointing to a buffer
1229 // of specified size. The caller must ensure that the buffer is large enough.
1230 template <typename Char, typename UInt>
1231 FMT_CONSTEXPR20 auto do_format_decimal(Char* out, UInt value, int size)
1232 -> Char* {
1233 FMT_ASSERT(size >= count_digits(value), "invalid digit count");
1234 unsigned n = to_unsigned(size);
1235 while (value >= 100) {
1236 n -= 2;
1237 if (!is_constant_evaluated() && sizeof(UInt) == 4) {
1238 auto p = value * static_cast<uint64_t>((1ull << 39) / 100 + 1);
1239 write2digits_i(out + n, p >> (39 - 7) & ((1 << 7) - 1));
1240 value = static_cast<UInt>(p >> 39) +
1241 (static_cast<UInt>(value >= (100u << 25)) << 25);
1242 } else {
1243 // Integer division is slow so do it for a group of two digits instead
1244 // of for every digit. The idea comes from the talk by Alexandrescu
1245 // "Three Optimization Tips for C++". See speed-test for a comparison.
1246 write2digits(out + n, static_cast<unsigned>(value % 100));
1247 value /= 100;
1248 }
1249 }
1250 if (value >= 10) {
1251 n -= 2;
1252 write2digits(out + n, static_cast<unsigned>(value));
1253 } else {
1254 out[--n] = static_cast<Char>('0' + value);
1255 }
1256 return out + n;
1257 }
1258
1259 template <typename Char, typename UInt>
1260 FMT_CONSTEXPR FMT_INLINE auto format_decimal(Char* out, UInt value,
1261 int num_digits) -> Char* {
1262 do_format_decimal(out, value, num_digits);
1263 return out + num_digits;
1264 }
1265
1266 template <typename Char, typename UInt, typename OutputIt,
1267 FMT_ENABLE_IF(!std::is_pointer<remove_cvref_t<OutputIt>>::value)>
1268 FMT_CONSTEXPR auto format_decimal(OutputIt out, UInt value, int num_digits)
1269 -> OutputIt {
1270 if (auto ptr = to_pointer<Char>(out, to_unsigned(num_digits))) {
1271 do_format_decimal(ptr, value, num_digits);
1272 return out;
1273 }
1274 // Buffer is large enough to hold all digits (digits10 + 1).
1275 char buffer[digits10<UInt>() + 1];
1276 if (is_constant_evaluated()) fill_n(buffer, sizeof(buffer), '\0');
1277 do_format_decimal(buffer, value, num_digits);
1278 return copy_noinline<Char>(buffer, buffer + num_digits, out);
1279 }
1280
1281 template <typename Char, typename UInt>
1282 FMT_CONSTEXPR auto do_format_base2e(int base_bits, Char* out, UInt value,
1283 int size, bool upper = false) -> Char* {
1284 out += size;
1285 do {
1286 const char* digits = upper ? "0123456789ABCDEF" : "0123456789abcdef";
1287 unsigned digit = static_cast<unsigned>(value & ((1u << base_bits) - 1));
1288 *--out = static_cast<Char>(base_bits < 4 ? static_cast<char>('0' + digit)
1289 : digits[digit]);
1290 } while ((value >>= base_bits) != 0);
1291 return out;
1292 }
1293
1294 // Formats an unsigned integer in the power of two base (binary, octal, hex).
1295 template <typename Char, typename UInt>
1296 FMT_CONSTEXPR auto format_base2e(int base_bits, Char* out, UInt value,
1297 int num_digits, bool upper = false) -> Char* {
1298 do_format_base2e(base_bits, out, value, num_digits, upper);
1299 return out + num_digits;
1300 }
1301
1302 template <typename Char, typename OutputIt, typename UInt,
1303 FMT_ENABLE_IF(is_back_insert_iterator<OutputIt>::value)>
1304 FMT_CONSTEXPR inline auto format_base2e(int base_bits, OutputIt out, UInt value,
1305 int num_digits, bool upper = false)
1306 -> OutputIt {
1307 if (auto ptr = to_pointer<Char>(out, to_unsigned(num_digits))) {
1308 format_base2e(base_bits, ptr, value, num_digits, upper);
1309 return out;
1310 }
1311 // Make buffer large enough for any base.
1312 char buffer[num_bits<UInt>()];
1313 if (is_constant_evaluated()) fill_n(buffer, sizeof(buffer), '\0');
1314 format_base2e(base_bits, buffer, value, num_digits, upper);
1315 return detail::copy_noinline<Char>(buffer, buffer + num_digits, out);
1316 }
1317
1318 // A converter from UTF-8 to UTF-16.
1319 class utf8_to_utf16 {
1320 private:
1321 basic_memory_buffer<wchar_t> buffer_;
1322
1323 public:
1324 FMT_API explicit utf8_to_utf16(string_view s);
1325 inline operator basic_string_view<wchar_t>() const {
1326 return {&buffer_[0], size()};
1327 }
1328 inline auto size() const -> size_t { return buffer_.size() - 1; }
1329 inline auto c_str() const -> const wchar_t* { return &buffer_[0]; }
1330 inline auto str() const -> std::wstring { return {&buffer_[0], size()}; }
1331 };
1332
1333 enum class to_utf8_error_policy { abort, replace, wtf };
1334
1335 inline void to_utf8_3bytes(buffer<char>& buf, uint32_t cp) {
1336 buf.push_back(static_cast<char>(0xe0 | (cp >> 12)));
1337 buf.push_back(static_cast<char>(0x80 | ((cp & 0xfff) >> 6)));
1338 buf.push_back(static_cast<char>(0x80 | (cp & 0x3f)));
1339 }
1340
1341 // A converter from UTF-16/UTF-32 (host endian) to UTF-8.
1342 template <typename WChar, typename Buffer = memory_buffer> class to_utf8 {
1343 private:
1344 Buffer buffer_;
1345
1346 public:
1347 to_utf8() {}
1348 explicit to_utf8(basic_string_view<WChar> s,
1349 to_utf8_error_policy policy = to_utf8_error_policy::abort) {
1350 static_assert(sizeof(WChar) == 2 || sizeof(WChar) == 4,
1351 "expected utf16 or utf32");
1352 if (!convert(s, policy)) {
1353 FMT_THROW(std::runtime_error(sizeof(WChar) == 2 ? "invalid utf16"
1354 : "invalid utf32"));
1355 }
1356 }
1357 operator string_view() const { return string_view(&buffer_[0], size()); }
1358 auto size() const -> size_t { return buffer_.size() - 1; }
1359 auto c_str() const -> const char* { return &buffer_[0]; }
1360 auto str() const -> std::string { return std::string(&buffer_[0], size()); }
1361
1362 // Performs conversion returning a bool instead of throwing exception on
1363 // conversion error. This method may still throw in case of memory allocation
1364 // error.
1365 auto convert(basic_string_view<WChar> s,
1366 to_utf8_error_policy policy = to_utf8_error_policy::abort)
1367 -> bool {
1368 if (!convert(buffer_, s, policy)) return false;
1369 buffer_.push_back(0);
1370 return true;
1371 }
1372 static auto convert(Buffer& buf, basic_string_view<WChar> s,
1373 to_utf8_error_policy policy = to_utf8_error_policy::abort)
1374 -> bool {
1375 for (auto p = s.begin(); p != s.end(); ++p) {
1376 uint32_t c = static_cast<uint32_t>(*p);
1377 if (sizeof(WChar) == 2 && c >= 0xd800 && c <= 0xdfff) {
1378 // Handle a surrogate pair.
1379 ++p;
1380 if (p == s.end() || (c & 0xfc00) != 0xd800 || (*p & 0xfc00) != 0xdc00) {
1381 switch (policy) {
1382 case to_utf8_error_policy::abort: return false;
1383 case to_utf8_error_policy::replace:
1384 buf.append(string_view("\xEF\xBF\xBD"));
1385 break;
1386 case to_utf8_error_policy::wtf: to_utf8_3bytes(buf, c); break;
1387 }
1388 --p;
1389 continue;
1390 }
1391 c = (c << 10) + static_cast<uint32_t>(*p) - 0x35fdc00;
1392 }
1393 if (c < 0x80) {
1394 buf.push_back(static_cast<char>(c));
1395 } else if (c < 0x800) {
1396 buf.push_back(static_cast<char>(0xc0 | (c >> 6)));
1397 buf.push_back(static_cast<char>(0x80 | (c & 0x3f)));
1398 } else if ((c >= 0x800 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xffff)) {
1399 to_utf8_3bytes(buf, c);
1400 } else if (c >= 0x10000 && c <= 0x10ffff) {
1401 buf.push_back(static_cast<char>(0xf0 | (c >> 18)));
1402 buf.push_back(static_cast<char>(0x80 | ((c & 0x3ffff) >> 12)));
1403 buf.push_back(static_cast<char>(0x80 | ((c & 0xfff) >> 6)));
1404 buf.push_back(static_cast<char>(0x80 | (c & 0x3f)));
1405 } else {
1406 return false;
1407 }
1408 }
1409 return true;
1410 }
1411 };
1412
1413 // Computes 128-bit result of multiplication of two 64-bit unsigned integers.
1414 FMT_INLINE auto umul128(uint64_t x, uint64_t y) noexcept -> uint128 {
1415 #if FMT_USE_INT128
1416 auto p = static_cast<native_uint128>(x) * static_cast<native_uint128>(y);
1417 return {static_cast<uint64_t>(p >> 64), static_cast<uint64_t>(p)};
1418 #elif defined(_MSC_VER) && defined(_M_AMD64)
1419 auto hi = uint64_t();
1420 auto lo = _umul128(x, y, &hi);
1421 return {hi, lo};
1422 #else
1423 const uint64_t mask = static_cast<uint64_t>(max_value<uint32_t>());
1424
1425 uint64_t a = x >> 32;
1426 uint64_t b = x & mask;
1427 uint64_t c = y >> 32;
1428 uint64_t d = y & mask;
1429
1430 uint64_t ac = a * c;
1431 uint64_t bc = b * c;
1432 uint64_t ad = a * d;
1433 uint64_t bd = b * d;
1434
1435 uint64_t intermediate = (bd >> 32) + (ad & mask) + (bc & mask);
1436
1437 return {ac + (intermediate >> 32) + (ad >> 32) + (bc >> 32),
1438 (intermediate << 32) + (bd & mask)};
1439 #endif
1440 }
1441
1442 namespace dragonbox {
1443 // Computes floor(log10(pow(2, e))) for e in [-2620, 2620] using the method from
1444 // https://fmt.dev/papers/Dragonbox.pdf#page=28, section 6.1.
1445 inline auto floor_log10_pow2(int e) noexcept -> int {
1446 FMT_ASSERT(e <= 2620 && e >= -2620, "too large exponent");
1447 static_assert((-1 >> 1) == -1, "right shift is not arithmetic");
1448 return (e * 315653) >> 20;
1449 }
1450
1451 inline auto floor_log2_pow10(int e) noexcept -> int {
1452 FMT_ASSERT(e <= 1233 && e >= -1233, "too large exponent");
1453 return (e * 1741647) >> 19;
1454 }
1455
1456 // Computes upper 64 bits of multiplication of two 64-bit unsigned integers.
1457 inline auto umul128_upper64(uint64_t x, uint64_t y) noexcept -> uint64_t {
1458 #if FMT_USE_INT128
1459 auto p = static_cast<native_uint128>(x) * static_cast<native_uint128>(y);
1460 return static_cast<uint64_t>(p >> 64);
1461 #elif defined(_MSC_VER) && defined(_M_AMD64)
1462 return __umulh(x, y);
1463 #else
1464 return umul128(x, y).high();
1465 #endif
1466 }
1467
1468 // Computes upper 128 bits of multiplication of a 64-bit unsigned integer and a
1469 // 128-bit unsigned integer.
1470 inline auto umul192_upper128(uint64_t x, uint128 y) noexcept -> uint128 {
1471 uint128 r = umul128(x, y.high());
1472 r += umul128_upper64(x, y.low());
1473 return r;
1474 }
1475
1476 FMT_API auto get_cached_power(int k) noexcept -> uint128;
1477
1478 // Type-specific information that Dragonbox uses.
1479 template <typename T, typename Enable = void> struct float_info;
1480
1481 template <> struct float_info<float> {
1482 using carrier_uint = uint32_t;
1483 static constexpr int exponent_bits = 8;
1484 static constexpr int kappa = 1;
1485 static constexpr int big_divisor = 100;
1486 static constexpr int small_divisor = 10;
1487 static constexpr int min_k = -31;
1488 enum { max_k = 46 };
1489 static constexpr int shorter_interval_tie_lower_threshold = -35;
1490 static constexpr int shorter_interval_tie_upper_threshold = -35;
1491 };
1492
1493 template <> struct float_info<double> {
1494 using carrier_uint = uint64_t;
1495 static constexpr int exponent_bits = 11;
1496 static constexpr int kappa = 2;
1497 static constexpr int big_divisor = 1000;
1498 static constexpr int small_divisor = 100;
1499 static constexpr int min_k = -292;
1500 enum { max_k = 341 };
1501 static constexpr int shorter_interval_tie_lower_threshold = -77;
1502 static constexpr int shorter_interval_tie_upper_threshold = -77;
1503 };
1504
1505 // An 80- or 128-bit floating point number.
1506 template <typename T>
1507 struct float_info<T, enable_if_t<std::numeric_limits<T>::digits == 64 ||
1508 std::numeric_limits<T>::digits == 113 ||
1509 is_float128<T>::value>> {
1510 using carrier_uint = detail::uint128_t;
1511 static const int exponent_bits = 15;
1512 };
1513
1514 // A double-double floating point number.
1515 template <typename T>
1516 struct float_info<T, enable_if_t<is_double_double<T>::value>> {
1517 using carrier_uint = detail::uint128_t;
1518 };
1519
1520 template <typename T> struct decimal_fp {
1521 using significand_type = typename float_info<T>::carrier_uint;
1522 significand_type significand;
1523 int exponent;
1524 };
1525
1526 template <typename T> FMT_API auto to_decimal(T x) noexcept -> decimal_fp<T>;
1527 } // namespace dragonbox
1528
1529 // Returns true iff Float has the implicit bit which is not stored.
1530 template <typename Float> constexpr auto has_implicit_bit() -> bool {
1531 // An 80-bit FP number has a 64-bit significand an no implicit bit.
1532 return std::numeric_limits<Float>::digits != 64;
1533 }
1534
1535 // Returns the number of significand bits stored in Float. The implicit bit is
1536 // not counted since it is not stored.
1537 template <typename Float> constexpr auto num_significand_bits() -> int {
1538 // std::numeric_limits may not support __float128.
1539 return is_float128<Float>() ? 112
1540 : (std::numeric_limits<Float>::digits -
1541 (has_implicit_bit<Float>() ? 1 : 0));
1542 }
1543
1544 template <typename Float>
1545 constexpr auto exponent_mask() ->
1546 typename dragonbox::float_info<Float>::carrier_uint {
1547 using float_uint = typename dragonbox::float_info<Float>::carrier_uint;
1548 return ((float_uint(1) << dragonbox::float_info<Float>::exponent_bits) - 1)
1549 << num_significand_bits<Float>();
1550 }
1551 template <typename Float> constexpr auto exponent_bias() -> int {
1552 // std::numeric_limits may not support __float128.
1553 return is_float128<Float>() ? 16383
1554 : std::numeric_limits<Float>::max_exponent - 1;
1555 }
1556
1557 FMT_CONSTEXPR inline auto compute_exp_size(int exp) -> int {
1558 auto prefix_size = 2; // sign + 'e'
1559 auto abs_exp = exp >= 0 ? exp : -exp;
1560 if (abs_exp < 100) return prefix_size + 2;
1561 return prefix_size + (abs_exp >= 1000 ? 4 : 3);
1562 }
1563
1564 // Writes the exponent exp in the form "[+-]d{2,3}" to buffer.
1565 template <typename Char, typename OutputIt>
1566 FMT_CONSTEXPR auto write_exponent(int exp, OutputIt out) -> OutputIt {
1567 FMT_ASSERT(-10000 < exp && exp < 10000, "exponent out of range");
1568 if (exp < 0) {
1569 *out++ = static_cast<Char>('-');
1570 exp = -exp;
1571 } else {
1572 *out++ = static_cast<Char>('+');
1573 }
1574 auto uexp = static_cast<uint32_t>(exp);
1575 if (is_constant_evaluated()) {
1576 if (uexp < 10) *out++ = '0';
1577 return format_decimal<Char>(out, uexp, count_digits(uexp));
1578 }
1579 if (uexp >= 100u) {
1580 const char* top = digits2(uexp / 100);
1581 if (uexp >= 1000u) *out++ = static_cast<Char>(top[0]);
1582 *out++ = static_cast<Char>(top[1]);
1583 uexp %= 100;
1584 }
1585 const char* d = digits2(uexp);
1586 *out++ = static_cast<Char>(d[0]);
1587 *out++ = static_cast<Char>(d[1]);
1588 return out;
1589 }
1590
1591 // A floating-point number f * pow(2, e) where F is an unsigned type.
1592 template <typename F> struct basic_fp {
1593 F f;
1594 int e;
1595
1596 static constexpr int num_significand_bits =
1597 static_cast<int>(sizeof(F) * num_bits<unsigned char>());
1598
1599 constexpr basic_fp() : f(0), e(0) {}
1600 constexpr basic_fp(uint64_t f_val, int e_val) : f(f_val), e(e_val) {}
1601
1602 // Constructs fp from an IEEE754 floating-point number.
1603 template <typename Float> FMT_CONSTEXPR basic_fp(Float n) { assign(n); }
1604
1605 // Assigns n to this and return true iff predecessor is closer than successor.
1606 template <typename Float, FMT_ENABLE_IF(!is_double_double<Float>::value)>
1607 FMT_CONSTEXPR auto assign(Float n) -> bool {
1608 static_assert(std::numeric_limits<Float>::digits <= 113, "unsupported FP");
1609 // Assume Float is in the format [sign][exponent][significand].
1610 using carrier_uint = typename dragonbox::float_info<Float>::carrier_uint;
1611 const auto num_float_significand_bits =
1612 detail::num_significand_bits<Float>();
1613 const auto implicit_bit = carrier_uint(1) << num_float_significand_bits;
1614 const auto significand_mask = implicit_bit - 1;
1615 auto u = bit_cast<carrier_uint>(n);
1616 f = static_cast<F>(u & significand_mask);
1617 auto biased_e = static_cast<int>((u & exponent_mask<Float>()) >>
1618 num_float_significand_bits);
1619 // The predecessor is closer if n is a normalized power of 2 (f == 0)
1620 // other than the smallest normalized number (biased_e > 1).
1621 auto is_predecessor_closer = f == 0 && biased_e > 1;
1622 if (biased_e == 0)
1623 biased_e = 1; // Subnormals use biased exponent 1 (min exponent).
1624 else if (has_implicit_bit<Float>())
1625 f += static_cast<F>(implicit_bit);
1626 e = biased_e - exponent_bias<Float>() - num_float_significand_bits;
1627 if (!has_implicit_bit<Float>()) ++e;
1628 return is_predecessor_closer;
1629 }
1630
1631 template <typename Float, FMT_ENABLE_IF(is_double_double<Float>::value)>
1632 FMT_CONSTEXPR auto assign(Float n) -> bool {
1633 static_assert(std::numeric_limits<double>::is_iec559, "unsupported FP");
1634 return assign(static_cast<double>(n));
1635 }
1636 };
1637
1638 using fp = basic_fp<ullong>;
1639
1640 // Normalizes the value converted from double and multiplied by (1 << SHIFT).
1641 template <int SHIFT = 0, typename F>
1642 FMT_CONSTEXPR auto normalize(basic_fp<F> value) -> basic_fp<F> {
1643 // Handle subnormals.
1644 const auto implicit_bit = F(1) << num_significand_bits<double>();
1645 const auto shifted_implicit_bit = implicit_bit << SHIFT;
1646 while ((value.f & shifted_implicit_bit) == 0) {
1647 value.f <<= 1;
1648 --value.e;
1649 }
1650 // Subtract 1 to account for hidden bit.
1651 const auto offset = basic_fp<F>::num_significand_bits -
1652 num_significand_bits<double>() - SHIFT - 1;
1653 value.f <<= offset;
1654 value.e -= offset;
1655 return value;
1656 }
1657
1658 // Computes lhs * rhs / pow(2, 64) rounded to nearest with half-up tie breaking.
1659 FMT_CONSTEXPR inline auto multiply(uint64_t lhs, uint64_t rhs) -> uint64_t {
1660 #if FMT_USE_INT128
1661 auto product = static_cast<__uint128_t>(lhs) * rhs;
1662 auto f = static_cast<uint64_t>(product >> 64);
1663 return (static_cast<uint64_t>(product) & (1ULL << 63)) != 0 ? f + 1 : f;
1664 #else
1665 // Multiply 32-bit parts of significands.
1666 uint64_t mask = (1ULL << 32) - 1;
1667 uint64_t a = lhs >> 32, b = lhs & mask;
1668 uint64_t c = rhs >> 32, d = rhs & mask;
1669 uint64_t ac = a * c, bc = b * c, ad = a * d, bd = b * d;
1670 // Compute mid 64-bit of result and round.
1671 uint64_t mid = (bd >> 32) + (ad & mask) + (bc & mask) + (1U << 31);
1672 return ac + (ad >> 32) + (bc >> 32) + (mid >> 32);
1673 #endif
1674 }
1675
1676 FMT_CONSTEXPR inline auto operator*(fp x, fp y) -> fp {
1677 return {multiply(x.f, y.f), x.e + y.e + 64};
1678 }
1679
1680 template <typename T, bool doublish = num_bits<T>() == num_bits<double>()>
1681 using convert_float_result =
1682 conditional_t<std::is_same<T, float>::value || doublish, double, T>;
1683
1684 template <typename T>
1685 constexpr auto convert_float(T value) -> convert_float_result<T> {
1686 return static_cast<convert_float_result<T>>(value);
1687 }
1688
1689 template <bool C, typename T, typename F, FMT_ENABLE_IF(C)>
1690 auto select(T true_value, F) -> T {
1691 return true_value;
1692 }
1693 template <bool C, typename T, typename F, FMT_ENABLE_IF(!C)>
1694 auto select(T, F false_value) -> F {
1695 return false_value;
1696 }
1697
1698 template <typename Char, typename OutputIt>
1699 FMT_CONSTEXPR FMT_NOINLINE auto fill(OutputIt it, size_t n,
1700 const basic_specs& specs) -> OutputIt {
1701 auto fill_size = specs.fill_size();
1702 if (fill_size == 1) return detail::fill_n(it, n, specs.fill_unit<Char>());
1703 if (const Char* data = specs.fill<Char>()) {
1704 for (size_t i = 0; i < n; ++i) it = copy<Char>(data, data + fill_size, it);
1705 }
1706 return it;
1707 }
1708
1709 // Writes the output of f, padded according to format specifications in specs.
1710 // size: output size in code units.
1711 // width: output display width in (terminal) column positions.
1712 template <typename Char, align default_align = align::left, typename OutputIt,
1713 typename F>
1714 FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs& specs,
1715 size_t size, size_t width, F&& f) -> OutputIt {
1716 static_assert(default_align == align::left || default_align == align::right,
1717 "");
1718 unsigned spec_width = to_unsigned(specs.width);
1719 size_t padding = spec_width > width ? spec_width - width : 0;
1720 // Shifts are encoded as string literals because static constexpr is not
1721 // supported in constexpr functions.
1722 auto* shifts =
1723 default_align == align::left ? "\x1f\x1f\x00\x01" : "\x00\x1f\x00\x01";
1724 size_t left_padding = padding >> shifts[static_cast<int>(specs.align())];
1725 size_t right_padding = padding - left_padding;
1726 auto it = reserve(out, size + padding * specs.fill_size());
1727 if (left_padding != 0) it = fill<Char>(it, left_padding, specs);
1728 it = f(it);
1729 if (right_padding != 0) it = fill<Char>(it, right_padding, specs);
1730 return base_iterator(out, it);
1731 }
1732
1733 template <typename Char, align default_align = align::left, typename OutputIt,
1734 typename F>
1735 constexpr auto write_padded(OutputIt out, const format_specs& specs,
1736 size_t size, F&& f) -> OutputIt {
1737 return write_padded<Char, default_align>(out, specs, size, size, f);
1738 }
1739
1740 template <typename Char, align default_align = align::left, typename OutputIt>
1741 FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes,
1742 const format_specs& specs = {}) -> OutputIt {
1743 return write_padded<Char, default_align>(
1744 out, specs, bytes.size(), [bytes](reserve_iterator<OutputIt> it) {
1745 const char* data = bytes.data();
1746 return copy<Char>(data, data + bytes.size(), it);
1747 });
1748 }
1749
1750 template <typename Char, typename OutputIt, typename UIntPtr>
1751 auto write_ptr(OutputIt out, UIntPtr value, const format_specs* specs)
1752 -> OutputIt {
1753 int num_digits = count_digits<4>(value);
1754 auto size = to_unsigned(num_digits) + size_t(2);
1755 auto write = [=](reserve_iterator<OutputIt> it) {
1756 *it++ = static_cast<Char>('0');
1757 *it++ = static_cast<Char>('x');
1758 return format_base2e<Char>(4, it, value, num_digits);
1759 };
1760 return specs ? write_padded<Char, align::right>(out, *specs, size, write)
1761 : base_iterator(out, write(reserve(out, size)));
1762 }
1763
1764 // Returns true iff the code point cp is printable.
1765 FMT_API auto is_printable(uint32_t cp) -> bool;
1766
1767 inline auto needs_escape(uint32_t cp) -> bool {
1768 if (cp < 0x20 || cp == 0x7f || cp == '"' || cp == '\\') return true;
1769 if FMT_CONSTEXPR20 (FMT_OPTIMIZE_SIZE > 1) return false;
1770 return !is_printable(cp);
1771 }
1772
1773 template <typename Char> struct find_escape_result {
1774 const Char* begin;
1775 const Char* end;
1776 uint32_t cp;
1777 };
1778
1779 template <typename Char>
1780 auto find_escape(const Char* begin, const Char* end)
1781 -> find_escape_result<Char> {
1782 for (; begin != end; ++begin) {
1783 uint32_t cp = static_cast<unsigned_char<Char>>(*begin);
1784 if (sizeof(Char) == 1 && cp >= 0x80) continue;
1785 if (needs_escape(cp)) return {begin, begin + 1, cp};
1786 }
1787 return {begin, nullptr, 0};
1788 }
1789
1790 inline auto find_escape(const char* begin, const char* end)
1791 -> find_escape_result<char> {
1792 if FMT_CONSTEXPR20 (!use_utf8) return find_escape<char>(begin, end);
1793 auto result = find_escape_result<char>{end, nullptr, 0};
1794 for_each_codepoint(string_view(begin, to_unsigned(end - begin)),
1795 [&](uint32_t cp, string_view sv) {
1796 if (needs_escape(cp)) {
1797 result = {sv.begin(), sv.end(), cp};
1798 return false;
1799 }
1800 return true;
1801 });
1802 return result;
1803 }
1804
1805 template <size_t width, typename Char, typename OutputIt>
1806 auto write_codepoint(OutputIt out, char prefix, uint32_t cp) -> OutputIt {
1807 *out++ = static_cast<Char>('\\');
1808 *out++ = static_cast<Char>(prefix);
1809 Char buf[width];
1810 fill_n(buf, width, static_cast<Char>('0'));
1811 format_base2e(4, buf, cp, width);
1812 return copy<Char>(buf, buf + width, out);
1813 }
1814
1815 template <typename OutputIt, typename Char>
1816 auto write_escaped_cp(OutputIt out, const find_escape_result<Char>& escape)
1817 -> OutputIt {
1818 auto c = static_cast<Char>(escape.cp);
1819 switch (escape.cp) {
1820 case '\n':
1821 *out++ = static_cast<Char>('\\');
1822 c = static_cast<Char>('n');
1823 break;
1824 case '\r':
1825 *out++ = static_cast<Char>('\\');
1826 c = static_cast<Char>('r');
1827 break;
1828 case '\t':
1829 *out++ = static_cast<Char>('\\');
1830 c = static_cast<Char>('t');
1831 break;
1832 case '"': FMT_FALLTHROUGH;
1833 case '\'': FMT_FALLTHROUGH;
1834 case '\\': *out++ = static_cast<Char>('\\'); break;
1835 default:
1836 if (escape.cp < 0x100) return write_codepoint<2, Char>(out, 'x', escape.cp);
1837 if (escape.cp < 0x10000)
1838 return write_codepoint<4, Char>(out, 'u', escape.cp);
1839 if (escape.cp < 0x110000)
1840 return write_codepoint<8, Char>(out, 'U', escape.cp);
1841 for (Char escape_char : basic_string_view<Char>(
1842 escape.begin, to_unsigned(escape.end - escape.begin))) {
1843 out = write_codepoint<2, Char>(out, 'x',
1844 static_cast<uint32_t>(escape_char) & 0xFF);
1845 }
1846 return out;
1847 }
1848 *out++ = c;
1849 return out;
1850 }
1851
1852 template <typename Char, typename OutputIt>
1853 auto write_escaped_string(OutputIt out, basic_string_view<Char> str)
1854 -> OutputIt {
1855 *out++ = static_cast<Char>('"');
1856 auto begin = str.begin(), end = str.end();
1857 do {
1858 auto escape = find_escape(begin, end);
1859 out = copy<Char>(begin, escape.begin, out);
1860 begin = escape.end;
1861 if (!begin) break;
1862 out = write_escaped_cp<OutputIt, Char>(out, escape);
1863 } while (begin != end);
1864 *out++ = static_cast<Char>('"');
1865 return out;
1866 }
1867
1868 template <typename Char, typename OutputIt>
1869 auto write_escaped_char(OutputIt out, Char v) -> OutputIt {
1870 Char v_array[1] = {v};
1871 *out++ = static_cast<Char>('\'');
1872 if ((needs_escape(static_cast<uint32_t>(v)) && v != static_cast<Char>('"')) ||
1873 v == static_cast<Char>('\'')) {
1874 out = write_escaped_cp(out,
1875 find_escape_result<Char>{v_array, v_array + 1,
1876 static_cast<uint32_t>(v)});
1877 } else {
1878 *out++ = v;
1879 }
1880 *out++ = static_cast<Char>('\'');
1881 return out;
1882 }
1883
1884 template <typename Char, typename OutputIt>
1885 FMT_CONSTEXPR auto write_char(OutputIt out, Char value,
1886 const format_specs& specs) -> OutputIt {
1887 bool is_debug = specs.type() == presentation_type::debug;
1888 return write_padded<Char>(out, specs, 1, [=](reserve_iterator<OutputIt> it) {
1889 if (is_debug) return write_escaped_char(it, value);
1890 *it++ = value;
1891 return it;
1892 });
1893 }
1894
1895 template <typename Char> class digit_grouping {
1896 private:
1897 std::string grouping_;
1898 std::basic_string<Char> thousands_sep_;
1899
1900 struct next_state {
1901 std::string::const_iterator group;
1902 int pos;
1903 };
1904 auto initial_state() const -> next_state { return {grouping_.begin(), 0}; }
1905
1906 // Returns the next digit group separator position.
1907 auto next(next_state& state) const -> int {
1908 if (thousands_sep_.empty()) return max_value<int>();
1909 if (state.group == grouping_.end()) return state.pos += grouping_.back();
1910 if (*state.group <= 0 || *state.group == max_value<char>())
1911 return max_value<int>();
1912 state.pos += *state.group++;
1913 return state.pos;
1914 }
1915
1916 public:
1917 explicit digit_grouping(locale_ref loc, bool localized = true) {
1918 if (!localized) return;
1919 auto sep = thousands_sep<Char>(loc);
1920 grouping_ = std::move(sep.grouping);
1921 if (sep.thousands_sep) thousands_sep_.assign(1, sep.thousands_sep);
1922 }
1923 digit_grouping(std::string grouping, std::basic_string<Char> sep)
1924 : grouping_(std::move(grouping)), thousands_sep_(std::move(sep)) {}
1925
1926 auto has_separator() const -> bool { return !thousands_sep_.empty(); }
1927
1928 auto count_separators(int num_digits) const -> int {
1929 int count = 0;
1930 auto state = initial_state();
1931 while (num_digits > next(state)) ++count;
1932 return count;
1933 }
1934
1935 // Applies grouping to digits and writes the output to out.
1936 template <typename Out, typename C>
1937 auto apply(Out out, basic_string_view<C> digits) const -> Out {
1938 auto num_digits = static_cast<int>(digits.size());
1939 auto separators = basic_memory_buffer<int>();
1940 separators.push_back(0);
1941 auto state = initial_state();
1942 while (int i = next(state)) {
1943 if (i >= num_digits) break;
1944 separators.push_back(i);
1945 }
1946 for (int i = 0, sep_index = static_cast<int>(separators.size() - 1);
1947 i < num_digits; ++i) {
1948 if (num_digits - i == separators[sep_index]) {
1949 out = copy<Char>(thousands_sep_.data(),
1950 thousands_sep_.data() + thousands_sep_.size(), out);
1951 --sep_index;
1952 }
1953 *out++ = static_cast<Char>(digits[to_unsigned(i)]);
1954 }
1955 return out;
1956 }
1957 };
1958
1959 FMT_CONSTEXPR inline void prefix_append(unsigned& prefix, unsigned value) {
1960 prefix |= prefix != 0 ? value << 8 : value;
1961 prefix += (1u + (value > 0xff ? 1 : 0)) << 24;
1962 }
1963
1964 // Writes a decimal integer with digit grouping.
1965 template <typename OutputIt, typename UInt, typename Char>
1966 auto write_int(OutputIt out, UInt value, unsigned prefix,
1967 const format_specs& specs, const digit_grouping<Char>& grouping)
1968 -> OutputIt {
1969 static_assert(std::is_same<uint64_or_128_t<UInt>, UInt>::value, "");
1970 int num_digits = 0;
1971 auto buffer = memory_buffer();
1972 switch (specs.type()) {
1973 default: FMT_ASSERT(false, ""); FMT_FALLTHROUGH;
1974 case presentation_type::none:
1975 case presentation_type::dec:
1976 num_digits = count_digits(value);
1977 format_decimal<char>(appender(buffer), value, num_digits);
1978 break;
1979 case presentation_type::hex:
1980 if (specs.alt())
1981 prefix_append(prefix, unsigned(specs.upper() ? 'X' : 'x') << 8 | '0');
1982 num_digits = count_digits<4>(value);
1983 format_base2e<char>(4, appender(buffer), value, num_digits, specs.upper());
1984 break;
1985 case presentation_type::oct:
1986 num_digits = count_digits<3>(value);
1987 // Octal prefix '0' is counted as a digit, so only add it if precision
1988 // is not greater than the number of digits.
1989 if (specs.alt() && specs.precision <= num_digits && value != 0)
1990 prefix_append(prefix, '0');
1991 format_base2e<char>(3, appender(buffer), value, num_digits);
1992 break;
1993 case presentation_type::bin:
1994 if (specs.alt())
1995 prefix_append(prefix, unsigned(specs.upper() ? 'B' : 'b') << 8 | '0');
1996 num_digits = count_digits<1>(value);
1997 format_base2e<char>(1, appender(buffer), value, num_digits);
1998 break;
1999 case presentation_type::chr:
2000 return write_char<Char>(out, static_cast<Char>(value), specs);
2001 }
2002
2003 unsigned size = (prefix != 0 ? prefix >> 24 : 0) + to_unsigned(num_digits) +
2004 to_unsigned(grouping.count_separators(num_digits));
2005 return write_padded<Char, align::right>(
2006 out, specs, size, size, [&](reserve_iterator<OutputIt> it) {
2007 for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8)
2008 *it++ = static_cast<Char>(p & 0xff);
2009 return grouping.apply(it, string_view(buffer.data(), buffer.size()));
2010 });
2011 }
2012
2013 #if FMT_USE_LOCALE
2014 // Writes a localized value.
2015 FMT_API auto write_loc(appender out, loc_value value, const format_specs& specs,
2016 locale_ref loc) -> bool;
2017 auto write_loc(basic_appender<wchar_t> out, loc_value value,
2018 const format_specs& specs, locale_ref loc) -> bool;
2019 #endif
2020 template <typename OutputIt>
2021 inline auto write_loc(OutputIt, const loc_value&, const format_specs&,
2022 locale_ref) -> bool {
2023 return false;
2024 }
2025
2026 template <typename UInt> struct write_int_arg {
2027 UInt abs_value;
2028 unsigned prefix;
2029 };
2030
2031 template <typename T>
2032 FMT_CONSTEXPR auto make_write_int_arg(T value, sign s)
2033 -> write_int_arg<uint32_or_64_or_128_t<T>> {
2034 auto prefix = 0u;
2035 auto abs_value = static_cast<uint32_or_64_or_128_t<T>>(value);
2036 if (is_negative(value)) {
2037 prefix = 0x01000000 | '-';
2038 abs_value = 0 - abs_value;
2039 } else {
2040 constexpr unsigned prefixes[4] = {0, 0, 0x1000000u | '+', 0x1000000u | ' '};
2041 prefix = prefixes[static_cast<int>(s)];
2042 }
2043 return {abs_value, prefix};
2044 }
2045
2046 template <typename Char = char> struct loc_writer {
2047 basic_appender<Char> out;
2048 const format_specs& specs;
2049 std::basic_string<Char> sep;
2050 std::string grouping;
2051 std::basic_string<Char> decimal_point;
2052
2053 template <typename T, FMT_ENABLE_IF(is_integer<T>::value)>
2054 auto operator()(T value) -> bool {
2055 auto arg = make_write_int_arg(value, specs.sign());
2056 write_int(out, static_cast<uint64_or_128_t<T>>(arg.abs_value), arg.prefix,
2057 specs, digit_grouping<Char>(grouping, sep));
2058 return true;
2059 }
2060
2061 template <typename T, FMT_ENABLE_IF(!is_integer<T>::value)>
2062 auto operator()(T) -> bool {
2063 return false;
2064 }
2065 };
2066
2067 // Size and padding computation separate from write_int to avoid template bloat.
2068 struct size_padding {
2069 unsigned size;
2070 unsigned padding;
2071
2072 FMT_CONSTEXPR size_padding(int num_digits, unsigned prefix,
2073 const format_specs& specs)
2074 : size((prefix >> 24) + to_unsigned(num_digits)), padding(0) {
2075 if (specs.align() == align::numeric) {
2076 auto width = to_unsigned(specs.width);
2077 if (width > size) {
2078 padding = width - size;
2079 size = width;
2080 }
2081 } else if (specs.precision > num_digits) {
2082 size = (prefix >> 24) + to_unsigned(specs.precision);
2083 padding = to_unsigned(specs.precision - num_digits);
2084 }
2085 }
2086 };
2087
2088 template <typename Char, typename OutputIt, typename T>
2089 FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg<T> arg,
2090 const format_specs& specs) -> OutputIt {
2091 static_assert(std::is_same<T, uint32_or_64_or_128_t<T>>::value, "");
2092
2093 constexpr size_t buffer_size = num_bits<T>();
2094 char buffer[buffer_size];
2095 if (is_constant_evaluated()) fill_n(buffer, buffer_size, '\0');
2096 const char* begin = nullptr;
2097 const char* end = buffer + buffer_size;
2098
2099 auto abs_value = arg.abs_value;
2100 auto prefix = arg.prefix;
2101 switch (specs.type()) {
2102 default: FMT_ASSERT(false, ""); FMT_FALLTHROUGH;
2103 case presentation_type::none:
2104 case presentation_type::dec:
2105 begin = do_format_decimal(buffer, abs_value, buffer_size);
2106 break;
2107 case presentation_type::hex:
2108 begin = do_format_base2e(4, buffer, abs_value, buffer_size, specs.upper());
2109 if (specs.alt())
2110 prefix_append(prefix, unsigned(specs.upper() ? 'X' : 'x') << 8 | '0');
2111 break;
2112 case presentation_type::oct: {
2113 begin = do_format_base2e(3, buffer, abs_value, buffer_size);
2114 // Octal prefix '0' is counted as a digit, so only add it if precision
2115 // is not greater than the number of digits.
2116 auto num_digits = end - begin;
2117 if (specs.alt() && specs.precision <= num_digits && abs_value != 0)
2118 prefix_append(prefix, '0');
2119 break;
2120 }
2121 case presentation_type::bin:
2122 begin = do_format_base2e(1, buffer, abs_value, buffer_size);
2123 if (specs.alt())
2124 prefix_append(prefix, unsigned(specs.upper() ? 'B' : 'b') << 8 | '0');
2125 break;
2126 case presentation_type::chr:
2127 return write_char<Char>(out, static_cast<Char>(abs_value), specs);
2128 }
2129
2130 // Write an integer in the format
2131 // <left-padding><prefix><numeric-padding><digits><right-padding>
2132 // prefix contains chars in three lower bytes and the size in the fourth byte.
2133 int num_digits = static_cast<int>(end - begin);
2134 // Slightly faster check for specs.width == 0 && specs.precision == -1.
2135 if ((specs.width | (specs.precision + 1)) == 0) {
2136 auto it = reserve(out, to_unsigned(num_digits) + (prefix >> 24));
2137 for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8)
2138 *it++ = static_cast<Char>(p & 0xff);
2139 return base_iterator(out, copy<Char>(begin, end, it));
2140 }
2141 auto sp = size_padding(num_digits, prefix, specs);
2142 unsigned padding = sp.padding;
2143 return write_padded<Char, align::right>(
2144 out, specs, sp.size, [=](reserve_iterator<OutputIt> it) {
2145 for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8)
2146 *it++ = static_cast<Char>(p & 0xff);
2147 it = detail::fill_n(it, padding, static_cast<Char>('0'));
2148 return copy<Char>(begin, end, it);
2149 });
2150 }
2151
2152 template <typename Char, typename OutputIt, typename T>
2153 FMT_CONSTEXPR FMT_NOINLINE auto write_int_noinline(OutputIt out,
2154 write_int_arg<T> arg,
2155 const format_specs& specs)
2156 -> OutputIt {
2157 return write_int<Char>(out, arg, specs);
2158 }
2159
2160 template <typename Char, typename T,
2161 FMT_ENABLE_IF(is_integral<T>::value &&
2162 !std::is_same<T, bool>::value &&
2163 !std::is_same<T, Char>::value)>
2164 FMT_CONSTEXPR FMT_INLINE auto write(basic_appender<Char> out, T value,
2165 const format_specs& specs, locale_ref loc)
2166 -> basic_appender<Char> {
2167 if (specs.localized() && write_loc(out, value, specs, loc)) return out;
2168 return write_int_noinline<Char>(out, make_write_int_arg(value, specs.sign()),
2169 specs);
2170 }
2171
2172 // An inlined version of write used in format string compilation.
2173 template <typename Char, typename OutputIt, typename T,
2174 FMT_ENABLE_IF(is_integral<T>::value &&
2175 !std::is_same<T, bool>::value &&
2176 !std::is_same<T, Char>::value &&
2177 !std::is_same<OutputIt, basic_appender<Char>>::value)>
2178 FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
2179 const format_specs& specs, locale_ref loc)
2180 -> OutputIt {
2181 if (specs.localized() && write_loc(out, value, specs, loc)) return out;
2182 return write_int<Char>(out, make_write_int_arg(value, specs.sign()), specs);
2183 }
2184
2185 template <typename Char, typename OutputIt>
2186 FMT_CONSTEXPR auto write(OutputIt out, Char value, const format_specs& specs,
2187 locale_ref loc = {}) -> OutputIt {
2188 // char is formatted as unsigned char for consistency across platforms.
2189 using unsigned_type =
2190 conditional_t<std::is_same<Char, char>::value, unsigned char, unsigned>;
2191 return check_char_specs(specs)
2192 ? write_char<Char>(out, value, specs)
2193 : write<Char>(out, static_cast<unsigned_type>(value), specs, loc);
2194 }
2195
2196 template <typename Char, typename OutputIt,
2197 FMT_ENABLE_IF(std::is_same<Char, char>::value)>
2198 FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
2199 const format_specs& specs) -> OutputIt {
2200 bool is_debug = specs.type() == presentation_type::debug;
2201 if (specs.precision < 0 && specs.width == 0) {
2202 auto&& it = reserve(out, s.size());
2203 return is_debug ? write_escaped_string(it, s) : copy<char>(s, it);
2204 }
2205
2206 size_t display_width_limit =
2207 specs.precision < 0 ? SIZE_MAX : to_unsigned(specs.precision);
2208 size_t display_width =
2209 !is_debug || specs.precision == 0 ? 0 : 1; // Account for opening '"'.
2210 size_t size = !is_debug || specs.precision == 0 ? 0 : 1;
2211 for_each_codepoint(s, [&](uint32_t cp, string_view sv) {
2212 if (is_debug && needs_escape(cp)) {
2213 counting_buffer<char> buf;
2214 write_escaped_cp(basic_appender<char>(buf),
2215 find_escape_result<char>{sv.begin(), sv.end(), cp});
2216 // We're reinterpreting bytes as display width. That's okay
2217 // because write_escaped_cp() only writes ASCII characters.
2218 size_t cp_width = buf.count();
2219 if (display_width + cp_width <= display_width_limit) {
2220 display_width += cp_width;
2221 size += cp_width;
2222 // If this is the end of the string, account for closing '"'.
2223 if (display_width < display_width_limit && sv.end() == s.end()) {
2224 ++display_width;
2225 ++size;
2226 }
2227 return true;
2228 }
2229
2230 size += display_width_limit - display_width;
2231 display_width = display_width_limit;
2232 return false;
2233 }
2234
2235 size_t cp_width = display_width_of(cp);
2236 if (cp_width + display_width <= display_width_limit) {
2237 display_width += cp_width;
2238 size += sv.size();
2239 // If this is the end of the string, account for closing '"'.
2240 if (is_debug && display_width < display_width_limit &&
2241 sv.end() == s.end()) {
2242 ++display_width;
2243 ++size;
2244 }
2245 return true;
2246 }
2247
2248 return false;
2249 });
2250
2251 struct bounded_output_iterator {
2252 reserve_iterator<OutputIt> underlying_iterator;
2253 size_t bound;
2254
2255 FMT_CONSTEXPR auto operator*() -> bounded_output_iterator& { return *this; }
2256 FMT_CONSTEXPR auto operator++() -> bounded_output_iterator& {
2257 return *this;
2258 }
2259 FMT_CONSTEXPR auto operator++(int) -> bounded_output_iterator& {
2260 return *this;
2261 }
2262 FMT_CONSTEXPR auto operator=(char c) -> bounded_output_iterator& {
2263 if (bound > 0) {
2264 *underlying_iterator++ = c;
2265 --bound;
2266 }
2267 return *this;
2268 }
2269 };
2270
2271 return write_padded<char>(
2272 out, specs, size, display_width, [=](reserve_iterator<OutputIt> it) {
2273 return is_debug
2274 ? write_escaped_string(bounded_output_iterator{it, size}, s)
2275 .underlying_iterator
2276 : copy<char>(s.data(), s.data() + size, it);
2277 });
2278 }
2279
2280 template <typename Char, typename OutputIt,
2281 FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
2282 FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
2283 const format_specs& specs) -> OutputIt {
2284 auto data = s.data();
2285 auto size = s.size();
2286 if (specs.precision >= 0 && to_unsigned(specs.precision) < size)
2287 size = to_unsigned(specs.precision);
2288
2289 bool is_debug = specs.type() == presentation_type::debug;
2290 if (is_debug) {
2291 auto buf = counting_buffer<Char>();
2292 write_escaped_string(basic_appender<Char>(buf), s);
2293 size = buf.count();
2294 }
2295
2296 return write_padded<Char>(
2297 out, specs, size, [=](reserve_iterator<OutputIt> it) {
2298 return is_debug ? write_escaped_string(it, s)
2299 : copy<Char>(data, data + size, it);
2300 });
2301 }
2302
2303 template <typename Char, typename OutputIt>
2304 FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
2305 const format_specs& specs, locale_ref) -> OutputIt {
2306 return write<Char>(out, s, specs);
2307 }
2308
2309 template <typename Char, typename OutputIt>
2310 FMT_CONSTEXPR auto write(OutputIt out, const Char* s, const format_specs& specs,
2311 locale_ref) -> OutputIt {
2312 if (specs.type() == presentation_type::pointer)
2313 return write_ptr<Char>(out, bit_cast<uintptr_t>(s), &specs);
2314 if (!s) report_error("string pointer is null");
2315 return write<Char>(out, basic_string_view<Char>(s), specs, {});
2316 }
2317
2318 template <typename Char, typename OutputIt, typename T,
2319 FMT_ENABLE_IF(is_integral<T>::value &&
2320 !std::is_same<T, bool>::value &&
2321 !std::is_same<T, Char>::value)>
2322 FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt {
2323 auto abs_value = static_cast<uint32_or_64_or_128_t<T>>(value);
2324 bool negative = is_negative(value);
2325 // Don't do -abs_value since it trips unsigned-integer-overflow sanitizer.
2326 if (negative) abs_value = ~abs_value + 1;
2327 int num_digits = count_digits(abs_value);
2328 auto size = (negative ? 1 : 0) + static_cast<size_t>(num_digits);
2329 if (auto ptr = to_pointer<Char>(out, size)) {
2330 if (negative) *ptr++ = static_cast<Char>('-');
2331 format_decimal<Char>(ptr, abs_value, num_digits);
2332 return out;
2333 }
2334 if (negative) *out++ = static_cast<Char>('-');
2335 return format_decimal<Char>(out, abs_value, num_digits);
2336 }
2337
2338 template <typename Char>
2339 FMT_CONSTEXPR auto parse_align(const Char* begin, const Char* end,
2340 format_specs& specs) -> const Char* {
2341 FMT_ASSERT(begin != end, "");
2342 auto alignment = align::none;
2343 auto p = begin + code_point_length(begin);
2344 if (end - p <= 0) p = begin;
2345 for (;;) {
2346 switch (to_ascii(*p)) {
2347 case '<': alignment = align::left; break;
2348 case '>': alignment = align::right; break;
2349 case '^': alignment = align::center; break;
2350 }
2351 if (alignment != align::none) {
2352 if (p != begin) {
2353 auto c = *begin;
2354 if (c == '}') return begin;
2355 if (c == '{') {
2356 report_error("invalid fill character '{'");
2357 return begin;
2358 }
2359 specs.set_fill(basic_string_view<Char>(begin, to_unsigned(p - begin)));
2360 begin = p + 1;
2361 } else {
2362 ++begin;
2363 }
2364 break;
2365 }
2366 if (p == begin) break;
2367 p = begin;
2368 }
2369 specs.set_align(alignment);
2370 return begin;
2371 }
2372
2373 template <typename Char, typename OutputIt>
2374 FMT_CONSTEXPR20 auto write_nonfinite(OutputIt out, bool isnan,
2375 format_specs specs, sign s) -> OutputIt {
2376 auto str =
2377 isnan ? (specs.upper() ? "NAN" : "nan") : (specs.upper() ? "INF" : "inf");
2378 constexpr size_t str_size = 3;
2379 auto size = str_size + (s != sign::none ? 1 : 0);
2380 // Replace '0'-padding with space for non-finite values.
2381 const bool is_zero_fill =
2382 specs.fill_size() == 1 && specs.fill_unit<Char>() == '0';
2383 if (is_zero_fill) specs.set_fill(' ');
2384 return write_padded<Char>(out, specs, size,
2385 [=](reserve_iterator<OutputIt> it) {
2386 if (s != sign::none)
2387 *it++ = detail::getsign<Char>(s);
2388 return copy<Char>(str, str + str_size, it);
2389 });
2390 }
2391
2392 // A decimal floating-point number significand * pow(10, exp).
2393 struct big_decimal_fp {
2394 const char* significand;
2395 int significand_size;
2396 int exponent;
2397 };
2398
2399 constexpr auto get_significand_size(const big_decimal_fp& f) -> int {
2400 return f.significand_size;
2401 }
2402 template <typename T>
2403 inline auto get_significand_size(const dragonbox::decimal_fp<T>& f) -> int {
2404 return count_digits(f.significand);
2405 }
2406
2407 template <typename Char, typename OutputIt>
2408 constexpr auto write_significand(OutputIt out, const char* significand,
2409 int significand_size) -> OutputIt {
2410 return copy<Char>(significand, significand + significand_size, out);
2411 }
2412 template <typename Char, typename OutputIt, typename UInt>
2413 inline auto write_significand(OutputIt out, UInt significand,
2414 int significand_size) -> OutputIt {
2415 return format_decimal<Char>(out, significand, significand_size);
2416 }
2417 template <typename Char, typename OutputIt, typename T, typename Grouping>
2418 FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand,
2419 int significand_size, int exponent,
2420 const Grouping& grouping) -> OutputIt {
2421 if (!grouping.has_separator()) {
2422 out = write_significand<Char>(out, significand, significand_size);
2423 return detail::fill_n(out, exponent, static_cast<Char>('0'));
2424 }
2425 auto buffer = memory_buffer();
2426 write_significand<char>(appender(buffer), significand, significand_size);
2427 detail::fill_n(appender(buffer), exponent, '0');
2428 return grouping.apply(out, string_view(buffer.data(), buffer.size()));
2429 }
2430
2431 template <typename Char, typename UInt,
2432 FMT_ENABLE_IF(std::is_integral<UInt>::value)>
2433 inline auto write_significand(Char* out, UInt significand, int significand_size,
2434 int integral_size, Char decimal_point) -> Char* {
2435 if (!decimal_point) return format_decimal(out, significand, significand_size);
2436 out += significand_size + 1;
2437 Char* end = out;
2438 int floating_size = significand_size - integral_size;
2439 for (int i = floating_size / 2; i > 0; --i) {
2440 out -= 2;
2441 write2digits(out, static_cast<size_t>(significand % 100));
2442 significand /= 100;
2443 }
2444 if (floating_size % 2 != 0) {
2445 *--out = static_cast<Char>('0' + significand % 10);
2446 significand /= 10;
2447 }
2448 *--out = decimal_point;
2449 format_decimal(out - integral_size, significand, integral_size);
2450 return end;
2451 }
2452
2453 template <typename OutputIt, typename UInt, typename Char,
2454 FMT_ENABLE_IF(!std::is_pointer<remove_cvref_t<OutputIt>>::value)>
2455 inline auto write_significand(OutputIt out, UInt significand,
2456 int significand_size, int integral_size,
2457 Char decimal_point) -> OutputIt {
2458 // Buffer is large enough to hold digits (digits10 + 1) and a decimal point.
2459 Char buffer[digits10<UInt>() + 2];
2460 auto end = write_significand(buffer, significand, significand_size,
2461 integral_size, decimal_point);
2462 return detail::copy_noinline<Char>(buffer, end, out);
2463 }
2464
2465 template <typename OutputIt, typename Char>
2466 FMT_CONSTEXPR auto write_significand(OutputIt out, const char* significand,
2467 int significand_size, int integral_size,
2468 Char decimal_point) -> OutputIt {
2469 out = detail::copy_noinline<Char>(significand, significand + integral_size,
2470 out);
2471 if (!decimal_point) return out;
2472 *out++ = decimal_point;
2473 return detail::copy_noinline<Char>(significand + integral_size,
2474 significand + significand_size, out);
2475 }
2476
2477 template <typename OutputIt, typename Char, typename T, typename Grouping>
2478 FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand,
2479 int significand_size, int integral_size,
2480 Char decimal_point,
2481 const Grouping& grouping) -> OutputIt {
2482 if (!grouping.has_separator()) {
2483 return write_significand(out, significand, significand_size, integral_size,
2484 decimal_point);
2485 }
2486 auto buffer = basic_memory_buffer<Char>();
2487 write_significand(basic_appender<Char>(buffer), significand, significand_size,
2488 integral_size, decimal_point);
2489 grouping.apply(
2490 out, basic_string_view<Char>(buffer.data(), to_unsigned(integral_size)));
2491 return detail::copy_noinline<Char>(buffer.data() + integral_size,
2492 buffer.end(), out);
2493 }
2494
2495 // Numbers with exponents greater or equal to the returned value will use
2496 // the exponential notation.
2497 template <typename T> FMT_CONSTEVAL auto exp_upper() -> int {
2498 return std::numeric_limits<T>::digits10 != 0
2499 ? min_of(16, std::numeric_limits<T>::digits10 + 1)
2500 : 16;
2501 }
2502
2503 // Use the fixed notation if the exponent is in [-4, exp_upper),
2504 // e.g. 0.0001 instead of 1e-04. Otherwise use the exponent notation.
2505 constexpr auto use_fixed(int exp, int exp_upper) -> bool {
2506 return exp >= -4 && exp < exp_upper;
2507 }
2508
2509 template <typename Char> class fallback_digit_grouping {
2510 public:
2511 constexpr fallback_digit_grouping(locale_ref, bool) {}
2512
2513 constexpr auto has_separator() const -> bool { return false; }
2514
2515 constexpr auto count_separators(int) const -> int { return 0; }
2516
2517 template <typename Out, typename C>
2518 constexpr auto apply(Out out, basic_string_view<C>) const -> Out {
2519 return out;
2520 }
2521 };
2522
2523 template <typename Char, typename Grouping, typename OutputIt,
2524 typename DecimalFP>
2525 FMT_CONSTEXPR20 auto write_fixed(OutputIt out, const DecimalFP& f,
2526 int significand_size, Char decimal_point,
2527 const format_specs& specs, sign s,
2528 locale_ref loc = {}) -> OutputIt {
2529 using iterator = reserve_iterator<OutputIt>;
2530
2531 int exp = f.exponent + significand_size;
2532 long long size = significand_size + (s != sign::none ? 1 : 0);
2533 if (f.exponent >= 0) {
2534 // 1234e5 -> 123400000[.0+]
2535 size += f.exponent;
2536 int num_zeros = specs.precision - exp;
2537 abort_fuzzing_if(num_zeros > 5000);
2538 if (specs.alt()) {
2539 ++size;
2540 if (num_zeros <= 0 && specs.type() != presentation_type::fixed)
2541 num_zeros = 0;
2542 if (num_zeros > 0) size += num_zeros;
2543 }
2544 auto grouping = Grouping(loc, specs.localized());
2545 size += grouping.count_separators(exp);
2546 return write_padded<Char, align::right>(
2547 out, specs, static_cast<size_t>(size), [&](iterator it) {
2548 if (s != sign::none) *it++ = detail::getsign<Char>(s);
2549 it = write_significand<Char>(it, f.significand, significand_size,
2550 f.exponent, grouping);
2551 if (!specs.alt()) return it;
2552 *it++ = decimal_point;
2553 return num_zeros > 0 ? detail::fill_n(it, num_zeros, Char('0')) : it;
2554 });
2555 }
2556 if (exp > 0) {
2557 // 1234e-2 -> 12.34[0+]
2558 int num_zeros = specs.alt() ? specs.precision - significand_size : 0;
2559 size += 1 + max_of(num_zeros, 0);
2560 auto grouping = Grouping(loc, specs.localized());
2561 size += grouping.count_separators(exp);
2562 return write_padded<Char, align::right>(
2563 out, specs, static_cast<size_t>(size), [&](iterator it) {
2564 if (s != sign::none) *it++ = detail::getsign<Char>(s);
2565 it = write_significand(it, f.significand, significand_size, exp,
2566 decimal_point, grouping);
2567 return num_zeros > 0 ? detail::fill_n(it, num_zeros, Char('0')) : it;
2568 });
2569 }
2570 // 1234e-6 -> 0.001234
2571 int num_zeros = -exp;
2572 if (significand_size == 0 && specs.precision >= 0 &&
2573 specs.precision < num_zeros) {
2574 num_zeros = specs.precision;
2575 }
2576 bool pointy = num_zeros != 0 || significand_size != 0 || specs.alt();
2577 size += 1 + (pointy ? 1 : 0) + num_zeros;
2578 return write_padded<Char, align::right>(
2579 out, specs, static_cast<size_t>(size), [&](iterator it) {
2580 if (s != sign::none) *it++ = detail::getsign<Char>(s);
2581 *it++ = Char('0');
2582 if (!pointy) return it;
2583 *it++ = decimal_point;
2584 it = detail::fill_n(it, num_zeros, Char('0'));
2585 return write_significand<Char>(it, f.significand, significand_size);
2586 });
2587 }
2588
2589 template <typename Char, typename Grouping, typename OutputIt,
2590 typename DecimalFP>
2591 FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f,
2592 const format_specs& specs, sign s,
2593 int exp_upper, locale_ref loc) -> OutputIt {
2594 Char point = specs.localized() ? detail::decimal_point<Char>(loc) : Char('.');
2595 int significand_size = get_significand_size(f);
2596 int exp = f.exponent + significand_size - 1;
2597 if (specs.type() == presentation_type::fixed ||
2598 (specs.type() != presentation_type::exp &&
2599 use_fixed(exp, specs.precision > 0 ? specs.precision : exp_upper))) {
2600 return write_fixed<Char, Grouping>(out, f, significand_size, point, specs,
2601 s, loc);
2602 }
2603
2604 // Write value in the exponential format.
2605 int num_zeros = 0;
2606 long long size = significand_size + (s != sign::none ? 1 : 0);
2607 if (specs.alt()) {
2608 num_zeros = max_of(specs.precision - significand_size, 0);
2609 size += num_zeros;
2610 } else if (significand_size == 1) {
2611 point = Char();
2612 }
2613 size += (point ? 1 : 0) + compute_exp_size(exp);
2614 char exp_char = specs.upper() ? 'E' : 'e';
2615 auto write = [=](reserve_iterator<OutputIt> it) {
2616 if (s != sign::none) *it++ = detail::getsign<Char>(s);
2617 // Insert a decimal point after the first digit and add an exponent.
2618 it = write_significand(it, f.significand, significand_size, 1, point);
2619 if (num_zeros > 0) it = detail::fill_n(it, num_zeros, Char('0'));
2620 *it++ = Char(exp_char);
2621 return write_exponent<Char>(exp, it);
2622 };
2623 size_t usize = static_cast<size_t>(size);
2624 return specs.width > 0
2625 ? write_padded<Char, align::right>(out, specs, usize, write)
2626 : base_iterator(out, write(reserve(out, usize)));
2627 }
2628
2629 template <typename Char, typename OutputIt, typename DecimalFP>
2630 FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f,
2631 const format_specs& specs, sign s,
2632 int exp_upper, locale_ref loc) -> OutputIt {
2633 if (is_constant_evaluated()) {
2634 return do_write_float<Char, fallback_digit_grouping<Char>>(out, f, specs, s,
2635 exp_upper, loc);
2636 } else {
2637 return do_write_float<Char, digit_grouping<Char>>(out, f, specs, s,
2638 exp_upper, loc);
2639 }
2640 }
2641
2642 template <typename T> constexpr auto isnan(T value) -> bool {
2643 return value != value; // std::isnan doesn't support __float128.
2644 }
2645
2646 template <typename T, typename Enable = void>
2647 struct has_isfinite : std::false_type {};
2648
2649 template <typename T>
2650 struct has_isfinite<T, enable_if_t<sizeof(std::isfinite(T())) != 0>>
2651 : std::true_type {};
2652
2653 template <typename T,
2654 FMT_ENABLE_IF(is_floating_point<T>::value&& has_isfinite<T>::value)>
2655 FMT_CONSTEXPR20 auto isfinite(T value) -> bool {
2656 constexpr T inf = T(std::numeric_limits<double>::infinity());
2657 if (is_constant_evaluated())
2658 return !detail::isnan(value) && value < inf && value > -inf;
2659 return std::isfinite(value);
2660 }
2661 template <typename T, FMT_ENABLE_IF(!has_isfinite<T>::value)>
2662 FMT_CONSTEXPR auto isfinite(T value) -> bool {
2663 T inf = T(std::numeric_limits<double>::infinity());
2664 // std::isfinite doesn't support __float128.
2665 return !detail::isnan(value) && value < inf && value > -inf;
2666 }
2667
2668 template <typename T, FMT_ENABLE_IF(is_floating_point<T>::value)>
2669 FMT_INLINE FMT_CONSTEXPR auto signbit(T value) -> bool {
2670 if (is_constant_evaluated()) {
2671 #ifdef __cpp_if_constexpr
2672 if constexpr (std::numeric_limits<double>::is_iec559) {
2673 auto bits = detail::bit_cast<uint64_t>(static_cast<double>(value));
2674 return (bits >> (num_bits<uint64_t>() - 1)) != 0;
2675 }
2676 #endif
2677 }
2678 return std::signbit(static_cast<double>(value));
2679 }
2680
2681 inline FMT_CONSTEXPR20 void adjust_precision(int& precision, int exp10) {
2682 // Adjust fixed precision by exponent because it is relative to decimal
2683 // point.
2684 if (exp10 > 0 && precision > max_value<int>() - exp10)
2685 FMT_THROW(format_error("number is too big"));
2686 precision += exp10;
2687 }
2688
2689 class bigint {
2690 private:
2691 // A bigint is a number in the form bigit_[N - 1] ... bigit_[0] * 32^exp_.
2692 using bigit = uint32_t; // A big digit.
2693 using double_bigit = uint64_t;
2694 enum { bigit_bits = num_bits<bigit>() };
2695 enum { bigits_capacity = 32 };
2696 basic_memory_buffer<bigit, bigits_capacity> bigits_;
2697 int exp_;
2698
2699 friend struct formatter<bigint>;
2700
2701 FMT_CONSTEXPR auto get_bigit(int i) const -> bigit {
2702 return i >= exp_ && i < num_bigits() ? bigits_[i - exp_] : 0;
2703 }
2704
2705 FMT_CONSTEXPR void subtract_bigits(int index, bigit other, bigit& borrow) {
2706 auto result = double_bigit(bigits_[index]) - other - borrow;
2707 bigits_[index] = static_cast<bigit>(result);
2708 borrow = static_cast<bigit>(result >> (bigit_bits * 2 - 1));
2709 }
2710
2711 FMT_CONSTEXPR void remove_leading_zeros() {
2712 int num_bigits = static_cast<int>(bigits_.size()) - 1;
2713 while (num_bigits > 0 && bigits_[num_bigits] == 0) --num_bigits;
2714 bigits_.resize(to_unsigned(num_bigits + 1));
2715 }
2716
2717 // Computes *this -= other assuming aligned bigints and *this >= other.
2718 FMT_CONSTEXPR void subtract_aligned(const bigint& other) {
2719 FMT_ASSERT(other.exp_ >= exp_, "unaligned bigints");
2720 FMT_ASSERT(compare(*this, other) >= 0, "");
2721 bigit borrow = 0;
2722 int i = other.exp_ - exp_;
2723 for (size_t j = 0, n = other.bigits_.size(); j != n; ++i, ++j)
2724 subtract_bigits(i, other.bigits_[j], borrow);
2725 if (borrow != 0) subtract_bigits(i, 0, borrow);
2726 FMT_ASSERT(borrow == 0, "");
2727 remove_leading_zeros();
2728 }
2729
2730 FMT_CONSTEXPR void multiply(uint32_t value) {
2731 bigit carry = 0;
2732 const double_bigit wide_value = value;
2733 for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
2734 double_bigit result = bigits_[i] * wide_value + carry;
2735 bigits_[i] = static_cast<bigit>(result);
2736 carry = static_cast<bigit>(result >> bigit_bits);
2737 }
2738 if (carry != 0) bigits_.push_back(carry);
2739 }
2740
2741 template <typename UInt, FMT_ENABLE_IF(std::is_same<UInt, uint64_t>::value ||
2742 std::is_same<UInt, uint128_t>::value)>
2743 FMT_CONSTEXPR void multiply(UInt value) {
2744 using half_uint =
2745 conditional_t<std::is_same<UInt, uint128_t>::value, uint64_t, uint32_t>;
2746 const int shift = num_bits<half_uint>() - bigit_bits;
2747 const UInt lower = static_cast<half_uint>(value);
2748 const UInt upper = value >> num_bits<half_uint>();
2749 UInt carry = 0;
2750 for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
2751 UInt result = lower * bigits_[i] + static_cast<bigit>(carry);
2752 carry = (upper * bigits_[i] << shift) + (result >> bigit_bits) +
2753 (carry >> bigit_bits);
2754 bigits_[i] = static_cast<bigit>(result);
2755 }
2756 while (carry != 0) {
2757 bigits_.push_back(static_cast<bigit>(carry));
2758 carry >>= bigit_bits;
2759 }
2760 }
2761
2762 template <typename UInt, FMT_ENABLE_IF(std::is_same<UInt, uint64_t>::value ||
2763 std::is_same<UInt, uint128_t>::value)>
2764 FMT_CONSTEXPR void assign(UInt n) {
2765 size_t num_bigits = 0;
2766 do {
2767 bigits_[num_bigits++] = static_cast<bigit>(n);
2768 n >>= bigit_bits;
2769 } while (n != 0);
2770 bigits_.resize(num_bigits);
2771 exp_ = 0;
2772 }
2773
2774 public:
2775 FMT_CONSTEXPR bigint() : exp_(0) {}
2776 explicit bigint(uint64_t n) { assign(n); }
2777
2778 bigint(const bigint&) = delete;
2779 void operator=(const bigint&) = delete;
2780
2781 FMT_CONSTEXPR void assign(const bigint& other) {
2782 auto size = other.bigits_.size();
2783 bigits_.resize(size);
2784 auto data = other.bigits_.data();
2785 copy<bigit>(data, data + size, bigits_.data());
2786 exp_ = other.exp_;
2787 }
2788
2789 template <typename Int> FMT_CONSTEXPR void operator=(Int n) {
2790 FMT_ASSERT(n > 0, "");
2791 assign(uint64_or_128_t<Int>(n));
2792 }
2793
2794 FMT_CONSTEXPR auto num_bigits() const -> int {
2795 return static_cast<int>(bigits_.size()) + exp_;
2796 }
2797
2798 FMT_CONSTEXPR auto operator<<=(int shift) -> bigint& {
2799 FMT_ASSERT(shift >= 0, "");
2800 exp_ += shift / bigit_bits;
2801 shift %= bigit_bits;
2802 if (shift == 0) return *this;
2803 bigit carry = 0;
2804 for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
2805 bigit c = bigits_[i] >> (bigit_bits - shift);
2806 bigits_[i] = (bigits_[i] << shift) + carry;
2807 carry = c;
2808 }
2809 if (carry != 0) bigits_.push_back(carry);
2810 return *this;
2811 }
2812
2813 template <typename Int> FMT_CONSTEXPR auto operator*=(Int value) -> bigint& {
2814 FMT_ASSERT(value > 0, "");
2815 multiply(uint32_or_64_or_128_t<Int>(value));
2816 return *this;
2817 }
2818
2819 friend FMT_CONSTEXPR auto compare(const bigint& b1, const bigint& b2) -> int {
2820 int num_bigits1 = b1.num_bigits(), num_bigits2 = b2.num_bigits();
2821 if (num_bigits1 != num_bigits2) return num_bigits1 > num_bigits2 ? 1 : -1;
2822 int i = static_cast<int>(b1.bigits_.size()) - 1;
2823 int j = static_cast<int>(b2.bigits_.size()) - 1;
2824 int end = i - j;
2825 if (end < 0) end = 0;
2826 for (; i >= end; --i, --j) {
2827 bigit b1_bigit = b1.bigits_[i], b2_bigit = b2.bigits_[j];
2828 if (b1_bigit != b2_bigit) return b1_bigit > b2_bigit ? 1 : -1;
2829 }
2830 if (i != j) return i > j ? 1 : -1;
2831 return 0;
2832 }
2833
2834 // Returns compare(lhs1 + lhs2, rhs).
2835 friend FMT_CONSTEXPR auto add_compare(const bigint& lhs1, const bigint& lhs2,
2836 const bigint& rhs) -> int {
2837 int max_lhs_bigits = max_of(lhs1.num_bigits(), lhs2.num_bigits());
2838 int num_rhs_bigits = rhs.num_bigits();
2839 if (max_lhs_bigits + 1 < num_rhs_bigits) return -1;
2840 if (max_lhs_bigits > num_rhs_bigits) return 1;
2841 double_bigit borrow = 0;
2842 int min_exp = min_of(min_of(lhs1.exp_, lhs2.exp_), rhs.exp_);
2843 for (int i = num_rhs_bigits - 1; i >= min_exp; --i) {
2844 double_bigit sum = double_bigit(lhs1.get_bigit(i)) + lhs2.get_bigit(i);
2845 bigit rhs_bigit = rhs.get_bigit(i);
2846 if (sum > rhs_bigit + borrow) return 1;
2847 borrow = rhs_bigit + borrow - sum;
2848 if (borrow > 1) return -1;
2849 borrow <<= bigit_bits;
2850 }
2851 return borrow != 0 ? -1 : 0;
2852 }
2853
2854 // Assigns pow(10, exp) to this bigint.
2855 FMT_CONSTEXPR20 void assign_pow10(int exp) {
2856 FMT_ASSERT(exp >= 0, "");
2857 if (exp == 0) return *this = 1;
2858 int bitmask = 1 << (num_bits<unsigned>() -
2859 countl_zero(static_cast<uint32_t>(exp)) - 1);
2860 // pow(10, exp) = pow(5, exp) * pow(2, exp). First compute pow(5, exp) by
2861 // repeated squaring and multiplication.
2862 *this = 5;
2863 bitmask >>= 1;
2864 while (bitmask != 0) {
2865 square();
2866 if ((exp & bitmask) != 0) *this *= 5;
2867 bitmask >>= 1;
2868 }
2869 *this <<= exp; // Multiply by pow(2, exp) by shifting.
2870 }
2871
2872 FMT_CONSTEXPR20 void square() {
2873 int num_bigits = static_cast<int>(bigits_.size());
2874 int num_result_bigits = 2 * num_bigits;
2875 basic_memory_buffer<bigit, bigits_capacity> n(std::move(bigits_));
2876 bigits_.resize(to_unsigned(num_result_bigits));
2877 auto sum = uint128_t();
2878 for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) {
2879 // Compute bigit at position bigit_index of the result by adding
2880 // cross-product terms n[i] * n[j] such that i + j == bigit_index.
2881 for (int i = 0, j = bigit_index; j >= 0; ++i, --j) {
2882 // Most terms are multiplied twice which can be optimized in the future.
2883 sum += double_bigit(n[i]) * n[j];
2884 }
2885 bigits_[bigit_index] = static_cast<bigit>(sum);
2886 sum >>= num_bits<bigit>(); // Compute the carry.
2887 }
2888 // Do the same for the top half.
2889 for (int bigit_index = num_bigits; bigit_index < num_result_bigits;
2890 ++bigit_index) {
2891 for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;)
2892 sum += double_bigit(n[i++]) * n[j--];
2893 bigits_[bigit_index] = static_cast<bigit>(sum);
2894 sum >>= num_bits<bigit>();
2895 }
2896 remove_leading_zeros();
2897 exp_ *= 2;
2898 }
2899
2900 // If this bigint has a bigger exponent than other, adds trailing zero to make
2901 // exponents equal. This simplifies some operations such as subtraction.
2902 FMT_CONSTEXPR void align(const bigint& other) {
2903 int exp_difference = exp_ - other.exp_;
2904 if (exp_difference <= 0) return;
2905 int num_bigits = static_cast<int>(bigits_.size());
2906 bigits_.resize(to_unsigned(num_bigits + exp_difference));
2907 for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j)
2908 bigits_[j] = bigits_[i];
2909 fill_n(bigits_.data(), to_unsigned(exp_difference), 0U);
2910 exp_ -= exp_difference;
2911 }
2912
2913 // Divides this bignum by divisor, assigning the remainder to this and
2914 // returning the quotient.
2915 FMT_CONSTEXPR auto divmod_assign(const bigint& divisor) -> int {
2916 FMT_ASSERT(this != &divisor, "");
2917 if (compare(*this, divisor) < 0) return 0;
2918 FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1u] != 0, "");
2919 align(divisor);
2920 int quotient = 0;
2921 do {
2922 subtract_aligned(divisor);
2923 ++quotient;
2924 } while (compare(*this, divisor) >= 0);
2925 return quotient;
2926 }
2927 };
2928
2929 // format_dragon flags.
2930 enum dragon {
2931 predecessor_closer = 1,
2932 fixup = 2, // Run fixup to correct exp10 which can be off by one.
2933 fixed = 4,
2934 };
2935
2936 // Formats a floating-point number using a variation of the Fixed-Precision
2937 // Positive Floating-Point Printout ((FPP)^2) algorithm by Steele & White:
2938 // https://fmt.dev/papers/p372-steele.pdf.
2939 FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value,
2940 unsigned flags, int num_digits,
2941 buffer<char>& buf, int& exp10) {
2942 bigint numerator; // 2 * R in (FPP)^2.
2943 bigint denominator; // 2 * S in (FPP)^2.
2944 // lower and upper are differences between value and corresponding boundaries.
2945 bigint lower; // (M^- in (FPP)^2).
2946 bigint upper_store; // upper's value if different from lower.
2947 bigint* upper = nullptr; // (M^+ in (FPP)^2).
2948 // Shift numerator and denominator by an extra bit or two (if lower boundary
2949 // is closer) to make lower and upper integers. This eliminates multiplication
2950 // by 2 during later computations.
2951 bool is_predecessor_closer = (flags & dragon::predecessor_closer) != 0;
2952 int shift = is_predecessor_closer ? 2 : 1;
2953 if (value.e >= 0) {
2954 numerator = value.f;
2955 numerator <<= value.e + shift;
2956 lower = 1;
2957 lower <<= value.e;
2958 if (is_predecessor_closer) {
2959 upper_store = 1;
2960 upper_store <<= value.e + 1;
2961 upper = &upper_store;
2962 }
2963 denominator.assign_pow10(exp10);
2964 denominator <<= shift;
2965 } else if (exp10 < 0) {
2966 numerator.assign_pow10(-exp10);
2967 lower.assign(numerator);
2968 if (is_predecessor_closer) {
2969 upper_store.assign(numerator);
2970 upper_store <<= 1;
2971 upper = &upper_store;
2972 }
2973 numerator *= value.f;
2974 numerator <<= shift;
2975 denominator = 1;
2976 denominator <<= shift - value.e;
2977 } else {
2978 numerator = value.f;
2979 numerator <<= shift;
2980 denominator.assign_pow10(exp10);
2981 denominator <<= shift - value.e;
2982 lower = 1;
2983 if (is_predecessor_closer) {
2984 upper_store = 1ULL << 1;
2985 upper = &upper_store;
2986 }
2987 }
2988 int even = static_cast<int>((value.f & 1) == 0);
2989 if (!upper) upper = &lower;
2990 bool shortest = num_digits < 0;
2991 if ((flags & dragon::fixup) != 0) {
2992 if (add_compare(numerator, *upper, denominator) + even <= 0) {
2993 --exp10;
2994 numerator *= 10;
2995 if (num_digits < 0) {
2996 lower *= 10;
2997 if (upper != &lower) *upper *= 10;
2998 }
2999 }
3000 if ((flags & dragon::fixed) != 0) adjust_precision(num_digits, exp10 + 1);
3001 }
3002 // Invariant: value == (numerator / denominator) * pow(10, exp10).
3003 if (shortest) {
3004 // Generate the shortest representation.
3005 num_digits = 0;
3006 char* data = buf.data();
3007 for (;;) {
3008 int digit = numerator.divmod_assign(denominator);
3009 bool low = compare(numerator, lower) - even < 0; // numerator <[=] lower.
3010 // numerator + upper >[=] pow10:
3011 bool high = add_compare(numerator, *upper, denominator) + even > 0;
3012 data[num_digits++] = static_cast<char>('0' + digit);
3013 if (low || high) {
3014 if (!low) {
3015 ++data[num_digits - 1];
3016 } else if (high) {
3017 int result = add_compare(numerator, numerator, denominator);
3018 // Round half to even.
3019 if (result > 0 || (result == 0 && (digit % 2) != 0))
3020 ++data[num_digits - 1];
3021 }
3022 buf.try_resize(to_unsigned(num_digits));
3023 exp10 -= num_digits - 1;
3024 return;
3025 }
3026 numerator *= 10;
3027 lower *= 10;
3028 if (upper != &lower) *upper *= 10;
3029 }
3030 }
3031 // Generate the given number of digits.
3032 exp10 -= num_digits - 1;
3033 if (num_digits <= 0) {
3034 auto digit = '0';
3035 if (num_digits == 0) {
3036 denominator *= 10;
3037 digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0';
3038 }
3039 buf.push_back(digit);
3040 return;
3041 }
3042 buf.try_resize(to_unsigned(num_digits));
3043 for (int i = 0; i < num_digits - 1; ++i) {
3044 int digit = numerator.divmod_assign(denominator);
3045 buf[i] = static_cast<char>('0' + digit);
3046 numerator *= 10;
3047 }
3048 int digit = numerator.divmod_assign(denominator);
3049 auto result = add_compare(numerator, numerator, denominator);
3050 if (result > 0 || (result == 0 && (digit % 2) != 0)) {
3051 if (digit == 9) {
3052 const auto overflow = '0' + 10;
3053 buf[num_digits - 1] = overflow;
3054 // Propagate the carry.
3055 for (int i = num_digits - 1; i > 0 && buf[i] == overflow; --i) {
3056 buf[i] = '0';
3057 ++buf[i - 1];
3058 }
3059 if (buf[0] == overflow) {
3060 buf[0] = '1';
3061 if ((flags & dragon::fixed) != 0)
3062 buf.push_back('0');
3063 else
3064 ++exp10;
3065 }
3066 return;
3067 }
3068 ++digit;
3069 }
3070 buf[num_digits - 1] = static_cast<char>('0' + digit);
3071 }
3072
3073 // Formats a floating-point number using the hexfloat format.
3074 template <typename Float, FMT_ENABLE_IF(!is_double_double<Float>::value)>
3075 FMT_CONSTEXPR20 void format_hexfloat(Float value, format_specs specs,
3076 buffer<char>& buf) {
3077 // float is passed as double to reduce the number of instantiations and to
3078 // simplify implementation.
3079 static_assert(!std::is_same<Float, float>::value, "");
3080
3081 using info = dragonbox::float_info<Float>;
3082
3083 // Assume Float is in the format [sign][exponent][significand].
3084 using carrier_uint = typename info::carrier_uint;
3085
3086 const auto num_float_significand_bits = detail::num_significand_bits<Float>();
3087
3088 basic_fp<carrier_uint> f(value);
3089 f.e += num_float_significand_bits;
3090 if (!has_implicit_bit<Float>()) --f.e;
3091
3092 const auto num_fraction_bits =
3093 num_float_significand_bits + (has_implicit_bit<Float>() ? 1 : 0);
3094 const auto num_xdigits = (num_fraction_bits + 3) / 4;
3095
3096 const auto leading_shift = ((num_xdigits - 1) * 4);
3097 const auto leading_mask = carrier_uint(0xF) << leading_shift;
3098 const auto leading_xdigit =
3099 static_cast<uint32_t>((f.f & leading_mask) >> leading_shift);
3100 if (leading_xdigit > 1) f.e -= (32 - countl_zero(leading_xdigit) - 1);
3101
3102 int print_xdigits = num_xdigits - 1;
3103 if (specs.precision >= 0 && print_xdigits > specs.precision) {
3104 const int shift = ((print_xdigits - specs.precision - 1) * 4);
3105 const auto mask = carrier_uint(0xF) << shift;
3106 const auto v = static_cast<uint32_t>((f.f & mask) >> shift);
3107
3108 if (v >= 8) {
3109 const auto inc = carrier_uint(1) << (shift + 4);
3110 f.f += inc;
3111 f.f &= ~(inc - 1);
3112 }
3113
3114 // Check long double overflow
3115 if (!has_implicit_bit<Float>()) {
3116 const auto implicit_bit = carrier_uint(1) << num_float_significand_bits;
3117 if ((f.f & implicit_bit) == implicit_bit) {
3118 f.f >>= 4;
3119 f.e += 4;
3120 }
3121 }
3122
3123 print_xdigits = specs.precision;
3124 }
3125
3126 char xdigits[num_bits<carrier_uint>() / 4];
3127 detail::fill_n(xdigits, sizeof(xdigits), '0');
3128 format_base2e(4, xdigits, f.f, num_xdigits, specs.upper());
3129
3130 // Remove zero tail
3131 while (print_xdigits > 0 && xdigits[print_xdigits] == '0') --print_xdigits;
3132
3133 buf.push_back('0');
3134 buf.push_back(specs.upper() ? 'X' : 'x');
3135 buf.push_back(xdigits[0]);
3136 if (specs.alt() || print_xdigits > 0 || print_xdigits < specs.precision)
3137 buf.push_back('.');
3138 buf.append(xdigits + 1, xdigits + 1 + print_xdigits);
3139 for (; print_xdigits < specs.precision; ++print_xdigits) buf.push_back('0');
3140
3141 buf.push_back(specs.upper() ? 'P' : 'p');
3142
3143 uint32_t abs_e;
3144 if (f.e < 0) {
3145 buf.push_back('-');
3146 abs_e = static_cast<uint32_t>(-f.e);
3147 } else {
3148 buf.push_back('+');
3149 abs_e = static_cast<uint32_t>(f.e);
3150 }
3151 format_decimal<char>(appender(buf), abs_e, detail::count_digits(abs_e));
3152 }
3153
3154 template <typename Float, FMT_ENABLE_IF(is_double_double<Float>::value)>
3155 FMT_CONSTEXPR20 void format_hexfloat(Float value, format_specs specs,
3156 buffer<char>& buf) {
3157 format_hexfloat(static_cast<double>(value), specs, buf);
3158 }
3159
3160 constexpr auto fractional_part_rounding_thresholds(int index) -> uint32_t {
3161 // For checking rounding thresholds.
3162 // The kth entry is chosen to be the smallest integer such that the
3163 // upper 32-bits of 10^(k+1) times it is strictly bigger than 5 * 10^k.
3164 // It is equal to ceil(2^31 + 2^32/10^(k + 1)).
3165 // These are stored in a string literal because we cannot have static arrays
3166 // in constexpr functions and non-static ones are poorly optimized.
3167 return uint32_t(u"\x9999\x828f\x8041\x8006\x8000\x8000\x8000\x8000"[index])
3168 << 16u |
3169 uint32_t(u"\x999a\x5c29\x8938\x8db9\xa7c6\x10c7\x01ae\x002b"[index]);
3170 }
3171
3172 template <typename Float>
3173 FMT_CONSTEXPR20 auto format_float(Float value, int precision,
3174 const format_specs& specs, bool binary32,
3175 buffer<char>& buf) -> int {
3176 // float is passed as double to reduce the number of instantiations.
3177 static_assert(!std::is_same<Float, float>::value, "");
3178 auto converted_value = convert_float(value);
3179
3180 const bool fixed = specs.type() == presentation_type::fixed;
3181 if (value == 0) {
3182 if (precision <= 0 || !fixed) {
3183 buf.push_back('0');
3184 return 0;
3185 }
3186 buf.try_resize(to_unsigned(precision));
3187 fill_n(buf.data(), precision, '0');
3188 return -precision;
3189 }
3190
3191 int exp = 0;
3192 bool use_dragon = true;
3193 unsigned dragon_flags = 0;
3194 if (!is_fast_float<Float>() || is_constant_evaluated()) {
3195 const auto inv_log2_10 = 0.3010299956639812; // 1 / log2(10)
3196 using info = dragonbox::float_info<decltype(converted_value)>;
3197 const auto f = basic_fp<typename info::carrier_uint>(converted_value);
3198 // Compute exp, an approximate power of 10, such that
3199 // 10^(exp - 1) <= value < 10^exp or 10^exp <= value < 10^(exp + 1).
3200 // This is based on log10(value) == log2(value) / log2(10) and approximation
3201 // of log2(value) by e + num_fraction_bits idea from double-conversion.
3202 auto e = (f.e + count_digits<1>(f.f) - 1) * inv_log2_10 - 1e-10;
3203 exp = static_cast<int>(e);
3204 if (e > exp) ++exp; // Compute ceil.
3205 dragon_flags = dragon::fixup;
3206 } else {
3207 // Extract significand bits and exponent bits.
3208 using info = dragonbox::float_info<double>;
3209 auto br = bit_cast<uint64_t>(static_cast<double>(value));
3210
3211 const uint64_t significand_mask =
3212 (static_cast<uint64_t>(1) << num_significand_bits<double>()) - 1;
3213 uint64_t significand = (br & significand_mask);
3214 int exponent = static_cast<int>((br & exponent_mask<double>()) >>
3215 num_significand_bits<double>());
3216
3217 if (exponent != 0) { // Check if normal.
3218 exponent -= exponent_bias<double>() + num_significand_bits<double>();
3219 significand |=
3220 (static_cast<uint64_t>(1) << num_significand_bits<double>());
3221 significand <<= 1;
3222 } else {
3223 // Normalize subnormal inputs.
3224 FMT_ASSERT(significand != 0, "zeros should not appear here");
3225 int shift = countl_zero(significand);
3226 FMT_ASSERT(shift >= num_bits<uint64_t>() - num_significand_bits<double>(),
3227 "");
3228 shift -= (num_bits<uint64_t>() - num_significand_bits<double>() - 2);
3229 exponent = (std::numeric_limits<double>::min_exponent -
3230 num_significand_bits<double>()) -
3231 shift;
3232 significand <<= shift;
3233 }
3234
3235 // Compute the first several nonzero decimal significand digits.
3236 // We call the number we get the first segment.
3237 const int k = info::kappa - dragonbox::floor_log10_pow2(exponent);
3238 exp = -k;
3239 const int beta = exponent + dragonbox::floor_log2_pow10(k);
3240 uint64_t first_segment;
3241 bool has_more_segments;
3242 int digits_in_the_first_segment;
3243 {
3244 const auto r = dragonbox::umul192_upper128(
3245 significand << beta, dragonbox::get_cached_power(k));
3246 first_segment = r.high();
3247 has_more_segments = r.low() != 0;
3248
3249 // The first segment can have 18 ~ 19 digits.
3250 if (first_segment >= 1000000000000000000ULL) {
3251 digits_in_the_first_segment = 19;
3252 } else {
3253 // When it is of 18-digits, we align it to 19-digits by adding a bogus
3254 // zero at the end.
3255 digits_in_the_first_segment = 18;
3256 first_segment *= 10;
3257 }
3258 }
3259
3260 // Compute the actual number of decimal digits to print.
3261 if (fixed) adjust_precision(precision, exp + digits_in_the_first_segment);
3262
3263 // Use Dragon4 only when there might be not enough digits in the first
3264 // segment.
3265 if (digits_in_the_first_segment > precision) {
3266 use_dragon = false;
3267
3268 if (precision <= 0) {
3269 exp += digits_in_the_first_segment;
3270
3271 if (precision < 0) {
3272 // Nothing to do, since all we have are just leading zeros.
3273 buf.try_resize(0);
3274 } else {
3275 // We may need to round-up.
3276 buf.try_resize(1);
3277 if ((first_segment | static_cast<uint64_t>(has_more_segments)) >
3278 5000000000000000000ULL) {
3279 buf[0] = '1';
3280 } else {
3281 buf[0] = '0';
3282 }
3283 }
3284 } // precision <= 0
3285 else {
3286 exp += digits_in_the_first_segment - precision;
3287
3288 // When precision > 0, we divide the first segment into three
3289 // subsegments, each with 9, 9, and 0 ~ 1 digits so that each fits
3290 // in 32-bits which usually allows faster calculation than in
3291 // 64-bits. Since some compiler (e.g. MSVC) doesn't know how to optimize
3292 // division-by-constant for large 64-bit divisors, we do it here
3293 // manually. The magic number 7922816251426433760 below is equal to
3294 // ceil(2^(64+32) / 10^10).
3295 const uint32_t first_subsegment = static_cast<uint32_t>(
3296 dragonbox::umul128_upper64(first_segment, 7922816251426433760ULL) >>
3297 32);
3298 const uint64_t second_third_subsegments =
3299 first_segment - first_subsegment * 10000000000ULL;
3300
3301 uint64_t prod;
3302 uint32_t digits;
3303 bool should_round_up;
3304 int number_of_digits_to_print = min_of(precision, 9);
3305
3306 // Print a 9-digits subsegment, either the first or the second.
3307 auto print_subsegment = [&](uint32_t subsegment, char* buffer) {
3308 int number_of_digits_printed = 0;
3309
3310 // If we want to print an odd number of digits from the subsegment,
3311 if ((number_of_digits_to_print & 1) != 0) {
3312 // Convert to 64-bit fixed-point fractional form with 1-digit
3313 // integer part. The magic number 720575941 is a good enough
3314 // approximation of 2^(32 + 24) / 10^8; see
3315 // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case
3316 // for details.
3317 prod = ((subsegment * static_cast<uint64_t>(720575941)) >> 24) + 1;
3318 digits = static_cast<uint32_t>(prod >> 32);
3319 *buffer = static_cast<char>('0' + digits);
3320 number_of_digits_printed++;
3321 }
3322 // If we want to print an even number of digits from the
3323 // first_subsegment,
3324 else {
3325 // Convert to 64-bit fixed-point fractional form with 2-digits
3326 // integer part. The magic number 450359963 is a good enough
3327 // approximation of 2^(32 + 20) / 10^7; see
3328 // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case
3329 // for details.
3330 prod = ((subsegment * static_cast<uint64_t>(450359963)) >> 20) + 1;
3331 digits = static_cast<uint32_t>(prod >> 32);
3332 write2digits(buffer, digits);
3333 number_of_digits_printed += 2;
3334 }
3335
3336 // Print all digit pairs.
3337 while (number_of_digits_printed < number_of_digits_to_print) {
3338 prod = static_cast<uint32_t>(prod) * static_cast<uint64_t>(100);
3339 digits = static_cast<uint32_t>(prod >> 32);
3340 write2digits(buffer + number_of_digits_printed, digits);
3341 number_of_digits_printed += 2;
3342 }
3343 };
3344
3345 // Print first subsegment.
3346 print_subsegment(first_subsegment, buf.data());
3347
3348 // Perform rounding if the first subsegment is the last subsegment to
3349 // print.
3350 if (precision <= 9) {
3351 // Rounding inside the subsegment.
3352 // We round-up if:
3353 // - either the fractional part is strictly larger than 1/2, or
3354 // - the fractional part is exactly 1/2 and the last digit is odd.
3355 // We rely on the following observations:
3356 // - If fractional_part >= threshold, then the fractional part is
3357 // strictly larger than 1/2.
3358 // - If the MSB of fractional_part is set, then the fractional part
3359 // must be at least 1/2.
3360 // - When the MSB of fractional_part is set, either
3361 // second_third_subsegments being nonzero or has_more_segments
3362 // being true means there are further digits not printed, so the
3363 // fractional part is strictly larger than 1/2.
3364 if (precision < 9) {
3365 uint32_t fractional_part = static_cast<uint32_t>(prod);
3366 should_round_up =
3367 fractional_part >= fractional_part_rounding_thresholds(
3368 8 - number_of_digits_to_print) ||
3369 ((fractional_part >> 31) &
3370 ((digits & 1) | (second_third_subsegments != 0) |
3371 has_more_segments)) != 0;
3372 }
3373 // Rounding at the subsegment boundary.
3374 // In this case, the fractional part is at least 1/2 if and only if
3375 // second_third_subsegments >= 5000000000ULL, and is strictly larger
3376 // than 1/2 if we further have either second_third_subsegments >
3377 // 5000000000ULL or has_more_segments == true.
3378 else {
3379 should_round_up = second_third_subsegments > 5000000000ULL ||
3380 (second_third_subsegments == 5000000000ULL &&
3381 ((digits & 1) != 0 || has_more_segments));
3382 }
3383 }
3384 // Otherwise, print the second subsegment.
3385 else {
3386 // Compilers are not aware of how to leverage the maximum value of
3387 // second_third_subsegments to find out a better magic number which
3388 // allows us to eliminate an additional shift. 1844674407370955162 =
3389 // ceil(2^64/10) < ceil(2^64*(10^9/(10^10 - 1))).
3390 const uint32_t second_subsegment =
3391 static_cast<uint32_t>(dragonbox::umul128_upper64(
3392 second_third_subsegments, 1844674407370955162ULL));
3393 const uint32_t third_subsegment =
3394 static_cast<uint32_t>(second_third_subsegments) -
3395 second_subsegment * 10;
3396
3397 number_of_digits_to_print = precision - 9;
3398 print_subsegment(second_subsegment, buf.data() + 9);
3399
3400 // Rounding inside the subsegment.
3401 if (precision < 18) {
3402 // The condition third_subsegment != 0 implies that the segment was
3403 // of 19 digits, so in this case the third segment should be
3404 // consisting of a genuine digit from the input.
3405 uint32_t fractional_part = static_cast<uint32_t>(prod);
3406 should_round_up =
3407 fractional_part >= fractional_part_rounding_thresholds(
3408 8 - number_of_digits_to_print) ||
3409 ((fractional_part >> 31) &
3410 ((digits & 1) | (third_subsegment != 0) |
3411 has_more_segments)) != 0;
3412 }
3413 // Rounding at the subsegment boundary.
3414 else {
3415 // In this case, the segment must be of 19 digits, thus
3416 // the third subsegment should be consisting of a genuine digit from
3417 // the input.
3418 should_round_up = third_subsegment > 5 ||
3419 (third_subsegment == 5 &&
3420 ((digits & 1) != 0 || has_more_segments));
3421 }
3422 }
3423
3424 // Round-up if necessary.
3425 if (should_round_up) {
3426 ++buf[precision - 1];
3427 for (int i = precision - 1; i > 0 && buf[i] > '9'; --i) {
3428 buf[i] = '0';
3429 ++buf[i - 1];
3430 }
3431 if (buf[0] > '9') {
3432 buf[0] = '1';
3433 if (fixed)
3434 buf[precision++] = '0';
3435 else
3436 ++exp;
3437 }
3438 }
3439 buf.try_resize(to_unsigned(precision));
3440 }
3441 } // if (digits_in_the_first_segment > precision)
3442 else {
3443 // Adjust the exponent for its use in Dragon4.
3444 exp += digits_in_the_first_segment - 1;
3445 }
3446 }
3447 if (use_dragon) {
3448 auto f = basic_fp<uint128_t>();
3449 bool is_predecessor_closer = binary32 ? f.assign(static_cast<float>(value))
3450 : f.assign(converted_value);
3451 if (is_predecessor_closer) dragon_flags |= dragon::predecessor_closer;
3452 if (fixed) dragon_flags |= dragon::fixed;
3453 // Limit precision to the maximum possible number of significant digits in
3454 // an IEEE754 double because we don't need to generate zeros.
3455 const int max_double_digits = 767;
3456 if (precision > max_double_digits) precision = max_double_digits;
3457 format_dragon(f, dragon_flags, precision, buf, exp);
3458 }
3459 if (!fixed && !specs.alt()) {
3460 // Remove trailing zeros.
3461 auto num_digits = buf.size();
3462 while (num_digits > 0 && buf[num_digits - 1] == '0') {
3463 --num_digits;
3464 ++exp;
3465 }
3466 buf.try_resize(num_digits);
3467 }
3468 return exp;
3469 }
3470
3471 template <typename Char, typename OutputIt, typename T,
3472 FMT_ENABLE_IF(is_floating_point<T>::value)>
3473 FMT_CONSTEXPR20 auto write(OutputIt out, T value, format_specs specs,
3474 locale_ref loc = {}) -> OutputIt {
3475 if (specs.localized() && write_loc(out, value, specs, loc)) return out;
3476
3477 // Use signbit because value < 0 is false for NaN.
3478 sign s = detail::signbit(value) ? sign::minus : specs.sign();
3479
3480 if (!detail::isfinite(value))
3481 return write_nonfinite<Char>(out, detail::isnan(value), specs, s);
3482
3483 if (specs.align() == align::numeric && s != sign::none) {
3484 *out++ = detail::getsign<Char>(s);
3485 s = sign::none;
3486 if (specs.width != 0) --specs.width;
3487 }
3488
3489 const int exp_upper = detail::exp_upper<T>();
3490 int precision = specs.precision;
3491 if (precision < 0) {
3492 if (specs.type() != presentation_type::none) {
3493 precision = 6;
3494 } else if (is_fast_float<T>::value && !is_constant_evaluated()) {
3495 // Use Dragonbox for the shortest format.
3496 auto dec = dragonbox::to_decimal(static_cast<fast_float_t<T>>(value));
3497 return write_float<Char>(out, dec, specs, s, exp_upper, loc);
3498 }
3499 }
3500
3501 memory_buffer buffer;
3502 if (specs.type() == presentation_type::hexfloat) {
3503 if (s != sign::none) buffer.push_back(detail::getsign<char>(s));
3504 format_hexfloat(convert_float(value), specs, buffer);
3505 return write_bytes<Char, align::right>(out, {buffer.data(), buffer.size()},
3506 specs);
3507 }
3508
3509 if (specs.type() == presentation_type::exp) {
3510 if (precision == max_value<int>())
3511 report_error("number is too big");
3512 else
3513 ++precision;
3514 if (specs.precision != 0) specs.set_alt();
3515 } else if (specs.type() == presentation_type::fixed) {
3516 if (specs.precision != 0) specs.set_alt();
3517 } else if (precision == 0) {
3518 precision = 1;
3519 }
3520 int exp = format_float(convert_float(value), precision, specs,
3521 std::is_same<T, float>(), buffer);
3522
3523 specs.precision = precision;
3524 auto f = big_decimal_fp{buffer.data(), static_cast<int>(buffer.size()), exp};
3525 return write_float<Char>(out, f, specs, s, exp_upper, loc);
3526 }
3527
3528 template <typename Char, typename OutputIt, typename T,
3529 FMT_ENABLE_IF(is_fast_float<T>::value)>
3530 FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt {
3531 if (is_constant_evaluated()) return write<Char>(out, value, format_specs());
3532
3533 auto s = detail::signbit(value) ? sign::minus : sign::none;
3534 auto mask = exponent_mask<fast_float_t<T>>();
3535 if ((bit_cast<decltype(mask)>(value) & mask) == mask)
3536 return write_nonfinite<Char>(out, std::isnan(value), {}, s);
3537
3538 auto dec = dragonbox::to_decimal(static_cast<fast_float_t<T>>(value));
3539 auto significand = dec.significand;
3540 int significand_size = count_digits(significand);
3541 int exponent = dec.exponent + significand_size - 1;
3542 if (use_fixed(exponent, detail::exp_upper<T>())) {
3543 return write_fixed<Char, fallback_digit_grouping<Char>>(
3544 out, dec, significand_size, Char('.'), {}, s);
3545 }
3546
3547 // Write value in the exponential format.
3548 const char* prefix = "e+";
3549 int abs_exponent = exponent;
3550 if (exponent < 0) {
3551 abs_exponent = -exponent;
3552 prefix = "e-";
3553 }
3554 auto has_decimal_point = significand_size != 1;
3555 size_t size = std::is_pointer<OutputIt>::value
3556 ? 0u
3557 : to_unsigned((s != sign::none ? 1 : 0) + significand_size +
3558 (has_decimal_point ? 1 : 0) +
3559 (abs_exponent >= 100 ? 5 : 4));
3560 if (auto ptr = to_pointer<Char>(out, size)) {
3561 if (s != sign::none) *ptr++ = Char('-');
3562 if (has_decimal_point) {
3563 auto begin = ptr;
3564 ptr = format_decimal<Char>(ptr, significand, significand_size + 1);
3565 *begin = begin[1];
3566 begin[1] = '.';
3567 } else {
3568 *ptr++ = static_cast<Char>('0' + significand);
3569 }
3570 if (std::is_same<Char, char>::value) {
3571 memcpy(ptr, prefix, 2);
3572 ptr += 2;
3573 } else {
3574 *ptr++ = static_cast<Char>(prefix[0]);
3575 *ptr++ = static_cast<Char>(prefix[1]);
3576 }
3577 if (abs_exponent >= 100) {
3578 *ptr++ = static_cast<Char>('0' + abs_exponent / 100);
3579 abs_exponent %= 100;
3580 }
3581 write2digits(ptr, static_cast<unsigned>(abs_exponent));
3582 return select<std::is_pointer<OutputIt>::value>(ptr + 2, out);
3583 }
3584 auto it = reserve(out, size);
3585 if (s != sign::none) *it++ = Char('-');
3586 // Insert a decimal point after the first digit and add an exponent.
3587 it = write_significand(it, significand, significand_size, 1,
3588 has_decimal_point ? Char('.') : Char());
3589 *it++ = Char('e');
3590 it = write_exponent<Char>(exponent, it);
3591 return base_iterator(out, it);
3592 }
3593
3594 template <typename Char, typename OutputIt, typename T,
3595 FMT_ENABLE_IF(is_floating_point<T>::value &&
3596 !is_fast_float<T>::value)>
3597 inline auto write(OutputIt out, T value) -> OutputIt {
3598 return write<Char>(out, value, {});
3599 }
3600
3601 template <typename Char, typename OutputIt>
3602 auto write(OutputIt out, monostate, format_specs = {}, locale_ref = {})
3603 -> OutputIt {
3604 FMT_ASSERT(false, "");
3605 return out;
3606 }
3607
3608 template <typename Char, typename OutputIt>
3609 FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> value)
3610 -> OutputIt {
3611 return copy_noinline<Char>(value.begin(), value.end(), out);
3612 }
3613
3614 template <typename Char, typename OutputIt, typename T,
3615 FMT_ENABLE_IF(has_to_string_view<T>::value)>
3616 constexpr auto write(OutputIt out, const T& value) -> OutputIt {
3617 return write<Char>(out, to_string_view(value));
3618 }
3619
3620 // FMT_ENABLE_IF() condition separated to workaround an MSVC bug.
3621 template <
3622 typename Char, typename OutputIt, typename T,
3623 bool check = std::is_enum<T>::value && !std::is_same<T, Char>::value &&
3624 mapped_type_constant<T, Char>::value != type::custom_type,
3625 FMT_ENABLE_IF(check)>
3626 FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt {
3627 return write<Char>(out, static_cast<underlying_t<T>>(value));
3628 }
3629
3630 template <typename Char, typename OutputIt, typename T,
3631 FMT_ENABLE_IF(std::is_same<T, bool>::value)>
3632 FMT_CONSTEXPR auto write(OutputIt out, T value, const format_specs& specs = {},
3633 locale_ref = {}) -> OutputIt {
3634 return specs.type() != presentation_type::none &&
3635 specs.type() != presentation_type::string
3636 ? write<Char>(out, value ? 1 : 0, specs, {})
3637 : write_bytes<Char>(out, value ? "true" : "false", specs);
3638 }
3639
3640 template <typename Char, typename OutputIt>
3641 FMT_CONSTEXPR auto write(OutputIt out, Char value) -> OutputIt {
3642 auto it = reserve(out, 1);
3643 *it++ = value;
3644 return base_iterator(out, it);
3645 }
3646
3647 template <typename Char, typename OutputIt>
3648 FMT_CONSTEXPR20 auto write(OutputIt out, const Char* value) -> OutputIt {
3649 if (value) return write(out, basic_string_view<Char>(value));
3650 report_error("string pointer is null");
3651 return out;
3652 }
3653
3654 template <typename Char, typename OutputIt, typename T,
3655 FMT_ENABLE_IF(std::is_same<T, void>::value)>
3656 auto write(OutputIt out, const T* value, const format_specs& specs = {},
3657 locale_ref = {}) -> OutputIt {
3658 return write_ptr<Char>(out, bit_cast<uintptr_t>(value), &specs);
3659 }
3660
3661 template <typename Char, typename OutputIt, typename T,
3662 FMT_ENABLE_IF(mapped_type_constant<T, Char>::value ==
3663 type::custom_type &&
3664 !std::is_fundamental<T>::value)>
3665 FMT_CONSTEXPR auto write(OutputIt out, const T& value) -> OutputIt {
3666 auto f = formatter<T, Char>();
3667 auto parse_ctx = parse_context<Char>({});
3668 f.parse(parse_ctx);
3669 auto ctx = basic_format_context<OutputIt, Char>(out, {}, {});
3670 return f.format(value, ctx);
3671 }
3672
3673 template <typename T>
3674 using is_builtin =
3675 bool_constant<std::is_same<T, int>::value || FMT_BUILTIN_TYPES>;
3676
3677 // An argument visitor that formats the argument and writes it via the output
3678 // iterator. It's a class and not a generic lambda for compatibility with C++11.
3679 template <typename Char> struct default_arg_formatter {
3680 using context = buffered_context<Char>;
3681
3682 basic_appender<Char> out;
3683
3684 void operator()(monostate) { report_error("argument not found"); }
3685
3686 template <typename T, FMT_ENABLE_IF(is_builtin<T>::value)>
3687 void operator()(T value) {
3688 write<Char>(out, value);
3689 }
3690
3691 template <typename T, FMT_ENABLE_IF(!is_builtin<T>::value)>
3692 void operator()(T) {
3693 FMT_ASSERT(false, "");
3694 }
3695
3696 void operator()(typename basic_format_arg<context>::handle h) {
3697 // Use a null locale since the default format must be unlocalized.
3698 auto parse_ctx = parse_context<Char>({});
3699 auto format_ctx = context(out, {}, {});
3700 h.format(parse_ctx, format_ctx);
3701 }
3702 };
3703
3704 template <typename Char> struct arg_formatter {
3705 basic_appender<Char> out;
3706 const format_specs& specs;
3707 FMT_NO_UNIQUE_ADDRESS locale_ref locale;
3708
3709 template <typename T, FMT_ENABLE_IF(is_builtin<T>::value)>
3710 FMT_CONSTEXPR FMT_INLINE void operator()(T value) {
3711 detail::write<Char>(out, value, specs, locale);
3712 }
3713
3714 template <typename T, FMT_ENABLE_IF(!is_builtin<T>::value)>
3715 void operator()(T) {
3716 FMT_ASSERT(false, "");
3717 }
3718
3719 void operator()(typename basic_format_arg<buffered_context<Char>>::handle) {
3720 // User-defined types are handled separately because they require access
3721 // to the parse context.
3722 }
3723 };
3724
3725 struct dynamic_spec_getter {
3726 template <typename T, FMT_ENABLE_IF(is_integer<T>::value)>
3727 FMT_CONSTEXPR auto operator()(T value) -> ullong {
3728 return is_negative(value) ? ~0ull : static_cast<ullong>(value);
3729 }
3730
3731 template <typename T, FMT_ENABLE_IF(!is_integer<T>::value)>
3732 FMT_CONSTEXPR auto operator()(T) -> ullong {
3733 report_error("width/precision is not integer");
3734 return 0;
3735 }
3736 };
3737
3738 template <typename Context>
3739 FMT_CONSTEXPR void handle_dynamic_spec(
3740 arg_id_kind kind, int& value,
3741 const arg_ref<typename Context::char_type>& ref, Context& ctx) {
3742 if (kind == arg_id_kind::none) return;
3743 auto arg =
3744 kind == arg_id_kind::index ? ctx.arg(ref.index) : ctx.arg(ref.name);
3745 if (!arg) report_error("argument not found");
3746 ullong result = arg.visit(dynamic_spec_getter());
3747 if (result > to_unsigned(max_value<int>()))
3748 report_error("width/precision is out of range");
3749 value = static_cast<int>(result);
3750 }
3751
3752 #if FMT_USE_NONTYPE_TEMPLATE_ARGS
3753 template <typename T, typename Char, size_t N,
3754 fmt::detail::fixed_string<Char, N> Str>
3755 struct static_named_arg : view {
3756 static constexpr auto name = Str.data;
3757
3758 const T& value;
3759 static_named_arg(const T& v) : value(v) {}
3760 };
3761
3762 template <typename T, typename Char, size_t N,
3763 fmt::detail::fixed_string<Char, N> Str>
3764 struct is_named_arg<static_named_arg<T, Char, N, Str>> : std::true_type {};
3765
3766 template <typename T, typename Char, size_t N,
3767 fmt::detail::fixed_string<Char, N> Str>
3768 struct is_static_named_arg<static_named_arg<T, Char, N, Str>> : std::true_type {
3769 };
3770
3771 template <typename Char, size_t N, fmt::detail::fixed_string<Char, N> Str>
3772 struct udl_arg {
3773 template <typename T> auto operator=(T&& value) const {
3774 return static_named_arg<T, Char, N, Str>(std::forward<T>(value));
3775 }
3776 };
3777 #else
3778 template <typename Char> struct udl_arg {
3779 const Char* str;
3780
3781 template <typename T> auto operator=(T&& value) const -> named_arg<T, Char> {
3782 return {str, std::forward<T>(value)};
3783 }
3784 };
3785 #endif // FMT_USE_NONTYPE_TEMPLATE_ARGS
3786
3787 template <typename Char = char> struct format_handler {
3788 parse_context<Char> parse_ctx;
3789 buffered_context<Char> ctx;
3790
3791 void on_text(const Char* begin, const Char* end) {
3792 copy_noinline<Char>(begin, end, ctx.out());
3793 }
3794
3795 FMT_CONSTEXPR auto on_arg_id() -> int { return parse_ctx.next_arg_id(); }
3796 FMT_CONSTEXPR auto on_arg_id(int id) -> int {
3797 parse_ctx.check_arg_id(id);
3798 return id;
3799 }
3800 FMT_CONSTEXPR auto on_arg_id(basic_string_view<Char> id) -> int {
3801 parse_ctx.check_arg_id(id);
3802 int arg_id = ctx.arg_id(id);
3803 if (arg_id < 0) report_error("argument not found");
3804 return arg_id;
3805 }
3806
3807 FMT_INLINE void on_replacement_field(int id, const Char*) {
3808 ctx.arg(id).visit(default_arg_formatter<Char>{ctx.out()});
3809 }
3810
3811 auto on_format_specs(int id, const Char* begin, const Char* end)
3812 -> const Char* {
3813 auto arg = ctx.arg(id);
3814 if (!arg) report_error("argument not found");
3815 // Not using a visitor for custom types gives better codegen.
3816 if (arg.format_custom(begin, parse_ctx, ctx)) return parse_ctx.begin();
3817
3818 auto specs = dynamic_format_specs<Char>();
3819 begin = parse_format_specs(begin, end, specs, parse_ctx, arg.type());
3820 if (specs.dynamic()) {
3821 handle_dynamic_spec(specs.dynamic_width(), specs.width, specs.width_ref,
3822 ctx);
3823 handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
3824 specs.precision_ref, ctx);
3825 }
3826
3827 arg.visit(arg_formatter<Char>{ctx.out(), specs, ctx.locale()});
3828 return begin;
3829 }
3830
3831 FMT_NORETURN void on_error(const char* message) { report_error(message); }
3832 };
3833
3834 // It is used in format-inl.h and os.cc.
3835 using format_func = void (*)(detail::buffer<char>&, int, const char*);
3836 FMT_API void do_report_error(format_func func, int error_code,
3837 const char* message) noexcept;
3838
3839 FMT_API void format_error_code(buffer<char>& out, int error_code,
3840 string_view message) noexcept;
3841
3842 template <typename T, typename Char, type TYPE>
3843 template <typename FormatContext>
3844 FMT_CONSTEXPR auto native_formatter<T, Char, TYPE>::format(
3845 const T& val, FormatContext& ctx) const -> decltype(ctx.out()) {
3846 if (!specs_.dynamic())
3847 return write<Char>(ctx.out(), val, specs_, ctx.locale());
3848 auto specs = format_specs(specs_);
3849 handle_dynamic_spec(specs.dynamic_width(), specs.width, specs_.width_ref,
3850 ctx);
3851 handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
3852 specs_.precision_ref, ctx);
3853 return write<Char>(ctx.out(), val, specs, ctx.locale());
3854 }
3855 } // namespace detail
3856
3857 FMT_BEGIN_EXPORT
3858
3859 // A generic formatting context with custom output iterator and character
3860 // (code unit) support. Char is the format string code unit type which can be
3861 // different from OutputIt::value_type.
3862 template <typename OutputIt, typename Char> class generic_context {
3863 private:
3864 OutputIt out_;
3865 basic_format_args<generic_context> args_;
3866 locale_ref loc_;
3867
3868 public:
3869 using char_type = Char;
3870 using iterator = OutputIt;
3871 enum { builtin_types = FMT_BUILTIN_TYPES };
3872
3873 constexpr generic_context(OutputIt out,
3874 basic_format_args<generic_context> args,
3875 locale_ref loc = {})
3876 : out_(out), args_(args), loc_(loc) {}
3877 generic_context(generic_context&&) = default;
3878 generic_context(const generic_context&) = delete;
3879 void operator=(const generic_context&) = delete;
3880
3881 constexpr auto arg(int id) const -> basic_format_arg<generic_context> {
3882 return args_.get(id);
3883 }
3884 auto arg(basic_string_view<Char> name) const
3885 -> basic_format_arg<generic_context> {
3886 return args_.get(name);
3887 }
3888 constexpr auto arg_id(basic_string_view<Char> name) const -> int {
3889 return args_.get_id(name);
3890 }
3891
3892 constexpr auto out() const -> iterator { return out_; }
3893
3894 FMT_CONSTEXPR void advance_to(iterator it) {
3895 if (!detail::is_back_insert_iterator<iterator>()) out_ = it;
3896 }
3897
3898 constexpr auto locale() const -> locale_ref { return loc_; }
3899 };
3900
3901 class loc_value {
3902 private:
3903 basic_format_arg<context> value_;
3904
3905 public:
3906 template <typename T, FMT_ENABLE_IF(!detail::is_float128<T>::value)>
3907 loc_value(T value) : value_(value) {}
3908
3909 template <typename T, FMT_ENABLE_IF(detail::is_float128<T>::value)>
3910 loc_value(T) {}
3911
3912 template <typename Visitor> auto visit(Visitor&& vis) -> decltype(vis(0)) {
3913 return value_.visit(vis);
3914 }
3915 };
3916
3917 // A locale facet that formats values in UTF-8.
3918 // It is parameterized on the locale to avoid the heavy <locale> include.
3919 template <typename Locale> class format_facet : public Locale::facet {
3920 private:
3921 std::string separator_;
3922 std::string grouping_;
3923 std::string decimal_point_;
3924
3925 protected:
3926 virtual auto do_put(appender out, loc_value val,
3927 const format_specs& specs) const -> bool;
3928
3929 public:
3930 static FMT_API typename Locale::id id;
3931
3932 explicit format_facet(Locale& loc);
3933 explicit format_facet(string_view sep = "", std::string grouping = "\3",
3934 std::string decimal_point = ".")
3935 : separator_(sep.data(), sep.size()),
3936 grouping_(std::move(grouping)),
3937 decimal_point_(std::move(decimal_point)) {}
3938
3939 auto put(appender out, loc_value val, const format_specs& specs) const
3940 -> bool {
3941 return do_put(out, val, specs);
3942 }
3943 };
3944
3945 #define FMT_FORMAT_AS(Type, Base) \
3946 template <typename Char> \
3947 struct formatter<Type, Char> : formatter<Base, Char> { \
3948 template <typename FormatContext> \
3949 FMT_CONSTEXPR auto format(Type value, FormatContext& ctx) const \
3950 -> decltype(ctx.out()) { \
3951 return formatter<Base, Char>::format(value, ctx); \
3952 } \
3953 }
3954
3955 FMT_FORMAT_AS(signed char, int);
3956 FMT_FORMAT_AS(unsigned char, unsigned);
3957 FMT_FORMAT_AS(short, int);
3958 FMT_FORMAT_AS(unsigned short, unsigned);
3959 FMT_FORMAT_AS(long, detail::long_type);
3960 FMT_FORMAT_AS(unsigned long, detail::ulong_type);
3961 FMT_FORMAT_AS(Char*, const Char*);
3962 FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>);
3963 FMT_FORMAT_AS(std::nullptr_t, const void*);
3964 FMT_FORMAT_AS(void*, const void*);
3965
3966 template <typename Char, size_t N>
3967 struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> {};
3968
3969 template <typename Char, typename Traits, typename Allocator>
3970 class formatter<std::basic_string<Char, Traits, Allocator>, Char>
3971 : public formatter<basic_string_view<Char>, Char> {};
3972
3973 template <typename Char>
3974 struct formatter<detail::float128, Char>
3975 : detail::native_formatter<detail::float128, Char,
3976 detail::type::float_type> {};
3977
3978 template <typename T, typename Char>
3979 struct formatter<T, Char, void_t<detail::format_as_result<T>>>
3980 : formatter<detail::format_as_result<T>, Char> {
3981 template <typename FormatContext>
3982 FMT_CONSTEXPR auto format(const T& value, FormatContext& ctx) const
3983 -> decltype(ctx.out()) {
3984 auto&& val = format_as(value); // Make an lvalue reference for format.
3985 return formatter<detail::format_as_result<T>, Char>::format(val, ctx);
3986 }
3987 };
3988
3989 /**
3990 * Converts `p` to `const void*` for pointer formatting.
3991 *
3992 * **Example**:
3993 *
3994 * auto s = fmt::format("{}", fmt::ptr(p));
3995 */
3996 template <typename T> auto ptr(T p) -> const void* {
3997 static_assert(std::is_pointer<T>::value, "fmt::ptr used with non-pointer");
3998 return detail::bit_cast<const void*>(p);
3999 }
4000
4001 /**
4002 * Converts `e` to the underlying type.
4003 *
4004 * **Example**:
4005 *
4006 * enum class color { red, green, blue };
4007 * auto s = fmt::format("{}", fmt::underlying(color::red)); // s == "0"
4008 */
4009 template <typename Enum>
4010 constexpr auto underlying(Enum e) noexcept -> underlying_t<Enum> {
4011 return static_cast<underlying_t<Enum>>(e);
4012 }
4013
4014 namespace enums {
4015 template <typename Enum, FMT_ENABLE_IF(std::is_enum<Enum>::value)>
4016 constexpr auto format_as(Enum e) noexcept -> underlying_t<Enum> {
4017 return static_cast<underlying_t<Enum>>(e);
4018 }
4019 } // namespace enums
4020
4021 struct bytes {
4022 string_view data;
4023
4024 inline explicit bytes(string_view s) : data(s) {}
4025 };
4026
4027 template <> struct formatter<bytes> {
4028 private:
4029 detail::dynamic_format_specs<> specs_;
4030
4031 public:
4032 FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* {
4033 return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
4034 detail::type::string_type);
4035 }
4036
4037 template <typename FormatContext>
4038 auto format(bytes b, FormatContext& ctx) const -> decltype(ctx.out()) {
4039 auto specs = specs_;
4040 detail::handle_dynamic_spec(specs.dynamic_width(), specs.width,
4041 specs.width_ref, ctx);
4042 detail::handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
4043 specs.precision_ref, ctx);
4044 return detail::write_bytes<char>(ctx.out(), b.data, specs);
4045 }
4046 };
4047
4048 // group_digits_view is not derived from view because it copies the argument.
4049 template <typename T> struct group_digits_view {
4050 T value;
4051 };
4052
4053 /**
4054 * Returns a view that formats an integer value using ',' as a
4055 * locale-independent thousands separator.
4056 *
4057 * **Example**:
4058 *
4059 * fmt::print("{}", fmt::group_digits(12345));
4060 * // Output: "12,345"
4061 */
4062 template <typename T> auto group_digits(T value) -> group_digits_view<T> {
4063 return {value};
4064 }
4065
4066 template <typename T> struct formatter<group_digits_view<T>> : formatter<T> {
4067 private:
4068 detail::dynamic_format_specs<> specs_;
4069
4070 public:
4071 FMT_CONSTEXPR auto parse(parse_context<>& ctx) -> const char* {
4072 return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
4073 detail::type::int_type);
4074 }
4075
4076 template <typename FormatContext>
4077 auto format(group_digits_view<T> view, FormatContext& ctx) const
4078 -> decltype(ctx.out()) {
4079 auto specs = specs_;
4080 detail::handle_dynamic_spec(specs.dynamic_width(), specs.width,
4081 specs.width_ref, ctx);
4082 detail::handle_dynamic_spec(specs.dynamic_precision(), specs.precision,
4083 specs.precision_ref, ctx);
4084 auto arg = detail::make_write_int_arg(view.value, specs.sign());
4085 return detail::write_int(
4086 ctx.out(), static_cast<detail::uint64_or_128_t<T>>(arg.abs_value),
4087 arg.prefix, specs, detail::digit_grouping<char>("\3", ","));
4088 }
4089 };
4090
4091 template <typename T, typename Char> struct nested_view {
4092 const formatter<T, Char>* fmt;
4093 const T* value;
4094 };
4095
4096 template <typename T, typename Char>
4097 struct formatter<nested_view<T, Char>, Char> {
4098 FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
4099 return ctx.begin();
4100 }
4101 template <typename FormatContext>
4102 auto format(nested_view<T, Char> view, FormatContext& ctx) const
4103 -> decltype(ctx.out()) {
4104 return view.fmt->format(*view.value, ctx);
4105 }
4106 };
4107
4108 template <typename T, typename Char = char> struct nested_formatter {
4109 private:
4110 basic_specs specs_;
4111 int width_;
4112 formatter<T, Char> formatter_;
4113
4114 public:
4115 constexpr nested_formatter() : width_(0) {}
4116
4117 FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
4118 auto it = ctx.begin(), end = ctx.end();
4119 if (it == end) return it;
4120 auto specs = format_specs();
4121 it = detail::parse_align(it, end, specs);
4122 specs_ = specs;
4123 Char c = *it;
4124 auto width_ref = detail::arg_ref<Char>();
4125 if ((c >= '0' && c <= '9') || c == '{') {
4126 it = detail::parse_width(it, end, specs, width_ref, ctx);
4127 width_ = specs.width;
4128 }
4129 ctx.advance_to(it);
4130 return formatter_.parse(ctx);
4131 }
4132
4133 template <typename FormatContext, typename F>
4134 auto write_padded(FormatContext& ctx, F write) const -> decltype(ctx.out()) {
4135 if (width_ == 0) return write(ctx.out());
4136 auto buf = basic_memory_buffer<Char>();
4137 write(basic_appender<Char>(buf));
4138 auto specs = format_specs();
4139 specs.width = width_;
4140 specs.copy_fill_from(specs_);
4141 specs.set_align(specs_.align());
4142 return detail::write<Char>(
4143 ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs);
4144 }
4145
4146 auto nested(const T& value) const -> nested_view<T, Char> {
4147 return nested_view<T, Char>{&formatter_, &value};
4148 }
4149 };
4150
4151 inline namespace literals {
4152 #if FMT_USE_NONTYPE_TEMPLATE_ARGS
4153 /**
4154 * User-defined literal equivalent of `fmt::arg`, but with compile-time checks.
4155 *
4156 * **Example**:
4157 *
4158 * using namespace fmt::literals;
4159 * fmt::print("The answer is {answer}.", "answer"_a=42);
4160 */
4161 template <detail::fixed_string S> constexpr auto operator""_a() {
4162 using char_t = remove_cvref_t<decltype(*S.data)>;
4163 return detail::udl_arg<char_t, sizeof(S.data) / sizeof(char_t), S>();
4164 }
4165 #else
4166 /**
4167 * User-defined literal equivalent of `fmt::arg`.
4168 *
4169 * **Example**:
4170 *
4171 * using namespace fmt::literals;
4172 * fmt::print("The answer is {answer}.", "answer"_a=42);
4173 */
4174 constexpr auto operator""_a(const char* s, size_t) -> detail::udl_arg<char> {
4175 return {s};
4176 }
4177 #endif // FMT_USE_NONTYPE_TEMPLATE_ARGS
4178 } // namespace literals
4179
4180 /// A fast integer formatter.
4181 class format_int {
4182 private:
4183 // Buffer should be large enough to hold all digits (digits10 + 1),
4184 // a sign and a null character.
4185 enum { buffer_size = std::numeric_limits<ullong>::digits10 + 3 };
4186 mutable char buffer_[buffer_size];
4187 char* str_;
4188
4189 template <typename UInt>
4190 FMT_CONSTEXPR20 auto format_unsigned(UInt value) -> char* {
4191 auto n = static_cast<detail::uint32_or_64_or_128_t<UInt>>(value);
4192 return detail::do_format_decimal(buffer_, n, buffer_size - 1);
4193 }
4194
4195 template <typename Int>
4196 FMT_CONSTEXPR20 auto format_signed(Int value) -> char* {
4197 auto abs_value = static_cast<detail::uint32_or_64_or_128_t<Int>>(value);
4198 bool negative = value < 0;
4199 if (negative) abs_value = 0 - abs_value;
4200 auto begin = format_unsigned(abs_value);
4201 if (negative) *--begin = '-';
4202 return begin;
4203 }
4204
4205 public:
4206 FMT_CONSTEXPR20 explicit format_int(int value) : str_(format_signed(value)) {}
4207 FMT_CONSTEXPR20 explicit format_int(long value)
4208 : str_(format_signed(value)) {}
4209 FMT_CONSTEXPR20 explicit format_int(long long value)
4210 : str_(format_signed(value)) {}
4211 FMT_CONSTEXPR20 explicit format_int(unsigned value)
4212 : str_(format_unsigned(value)) {}
4213 FMT_CONSTEXPR20 explicit format_int(unsigned long value)
4214 : str_(format_unsigned(value)) {}
4215 FMT_CONSTEXPR20 explicit format_int(ullong value)
4216 : str_(format_unsigned(value)) {}
4217
4218 /// Returns the number of characters written to the output buffer.
4219 FMT_CONSTEXPR20 auto size() const -> size_t {
4220 return detail::to_unsigned(buffer_ - str_ + buffer_size - 1);
4221 }
4222
4223 /// Returns a pointer to the output buffer content. No terminating null
4224 /// character is appended.
4225 FMT_CONSTEXPR20 auto data() const -> const char* { return str_; }
4226
4227 /// Returns a pointer to the output buffer content with terminating null
4228 /// character appended.
4229 FMT_CONSTEXPR20 auto c_str() const -> const char* {
4230 buffer_[buffer_size - 1] = '\0';
4231 return str_;
4232 }
4233
4234 /// Returns the content of the output buffer as an `std::string`.
4235 inline auto str() const -> std::string { return {str_, size()}; }
4236 };
4237
4238 #if FMT_CLANG_ANALYZER
4239 # define FMT_STRING_IMPL(s, base) s
4240 #else
4241 # define FMT_STRING_IMPL(s, base) \
4242 [] { \
4243 /* Use the hidden visibility as a workaround for a GCC bug (#1973). */ \
4244 /* Use a macro-like name to avoid shadowing warnings. */ \
4245 struct FMT_VISIBILITY("hidden") FMT_COMPILE_STRING : base { \
4246 using char_type = fmt::remove_cvref_t<decltype(s[0])>; \
4247 constexpr explicit operator fmt::basic_string_view<char_type>() \
4248 const { \
4249 return fmt::detail::compile_string_to_view<char_type>(s); \
4250 } \
4251 }; \
4252 using FMT_STRING_VIEW = \
4253 fmt::basic_string_view<typename FMT_COMPILE_STRING::char_type>; \
4254 fmt::detail::ignore_unused(FMT_STRING_VIEW(FMT_COMPILE_STRING())); \
4255 return FMT_COMPILE_STRING(); \
4256 }()
4257 #endif // FMT_CLANG_ANALYZER
4258
4259 /**
4260 * Constructs a legacy compile-time format string from a string literal `s`.
4261 *
4262 * **Example**:
4263 *
4264 * // A compile-time error because 'd' is an invalid specifier for strings.
4265 * std::string s = fmt::format(FMT_STRING("{:d}"), "foo");
4266 */
4267 #if FMT_USE_CONSTEVAL
4268 # define FMT_STRING(s) s
4269 #else
4270 # define FMT_STRING(s) FMT_STRING_IMPL(s, fmt::detail::compile_string)
4271 #endif // FMT_USE_CONSTEVAL
4272
4273 FMT_API auto vsystem_error(int error_code, string_view fmt, format_args args)
4274 -> std::system_error;
4275
4276 /**
4277 * Constructs `std::system_error` with a message formatted with
4278 * `fmt::format(fmt, args...)`.
4279 * `error_code` is a system error code as given by `errno`.
4280 *
4281 * **Example**:
4282 *
4283 * // This throws std::system_error with the description
4284 * // cannot open file 'madeup': No such file or directory
4285 * // or similar (system message may vary).
4286 * const char* filename = "madeup";
4287 * FILE* file = fopen(filename, "r");
4288 * if (!file)
4289 * throw fmt::system_error(errno, "cannot open file '{}'", filename);
4290 */
4291 template <typename... T>
4292 auto system_error(int error_code, format_string<T...> fmt, T&&... args)
4293 -> std::system_error {
4294 return vsystem_error(error_code, fmt.str, vargs<T...>{{args...}});
4295 }
4296
4297 /**
4298 * Formats an error message for an error returned by an operating system or a
4299 * language runtime, for example a file opening error, and writes it to `out`.
4300 * The format is the same as the one used by `std::system_error(ec, message)`
4301 * where `ec` is `std::error_code(error_code, std::generic_category())`.
4302 * It is implementation-defined but normally looks like:
4303 *
4304 * <message>: <system-message>
4305 *
4306 * where `<message>` is the passed message and `<system-message>` is the system
4307 * message corresponding to the error code.
4308 * `error_code` is a system error code as given by `errno`.
4309 */
4310 FMT_API void format_system_error(detail::buffer<char>& out, int error_code,
4311 const char* message) noexcept;
4312
4313 // Reports a system error without throwing an exception.
4314 // Can be used to report errors from destructors.
4315 FMT_API void report_system_error(int error_code, const char* message) noexcept;
4316
4317 inline auto vformat(locale_ref loc, string_view fmt, format_args args)
4318 -> std::string {
4319 auto buf = memory_buffer();
4320 detail::vformat_to(buf, fmt, args, loc);
4321 return {buf.data(), buf.size()};
4322 }
4323
4324 template <typename... T>
4325 FMT_INLINE auto format(locale_ref loc, format_string<T...> fmt, T&&... args)
4326 -> std::string {
4327 return vformat(loc, fmt.str, vargs<T...>{{args...}});
4328 }
4329
4330 template <typename OutputIt,
4331 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
4332 auto vformat_to(OutputIt out, locale_ref loc, string_view fmt, format_args args)
4333 -> OutputIt {
4334 auto&& buf = detail::get_buffer<char>(out);
4335 detail::vformat_to(buf, fmt, args, loc);
4336 return detail::get_iterator(buf, out);
4337 }
4338
4339 template <typename OutputIt, typename... T,
4340 FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
4341 FMT_INLINE auto format_to(OutputIt out, locale_ref loc, format_string<T...> fmt,
4342 T&&... args) -> OutputIt {
4343 return fmt::vformat_to(out, loc, fmt.str, vargs<T...>{{args...}});
4344 }
4345
4346 template <typename... T>
4347 FMT_NODISCARD FMT_INLINE auto formatted_size(locale_ref loc,
4348 format_string<T...> fmt,
4349 T&&... args) -> size_t {
4350 auto buf = detail::counting_buffer<>();
4351 detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}}, loc);
4352 return buf.count();
4353 }
4354
4355 FMT_API auto vformat(string_view fmt, format_args args) -> std::string;
4356
4357 /**
4358 * Formats `args` according to specifications in `fmt` and returns the result
4359 * as a string.
4360 *
4361 * **Example**:
4362 *
4363 * #include <fmt/format.h>
4364 * std::string message = fmt::format("The answer is {}.", 42);
4365 */
4366 template <typename... T>
4367 FMT_NODISCARD FMT_INLINE auto format(format_string<T...> fmt, T&&... args)
4368 -> std::string {
4369 return vformat(fmt.str, vargs<T...>{{args...}});
4370 }
4371
4372 /**
4373 * Converts `value` to `std::string` using the default format for type `T`.
4374 *
4375 * **Example**:
4376 *
4377 * std::string answer = fmt::to_string(42);
4378 */
4379 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
4380 FMT_NODISCARD FMT_CONSTEXPR_STRING auto to_string(T value) -> std::string {
4381 // The buffer should be large enough to store the number including the sign
4382 // or "false" for bool.
4383 char buffer[max_of(detail::digits10<T>() + 2, 5)];
4384 return {buffer, detail::write<char>(buffer, value)};
4385 }
4386
4387 template <typename T, FMT_ENABLE_IF(detail::use_format_as<T>::value)>
4388 FMT_NODISCARD FMT_CONSTEXPR_STRING auto to_string(const T& value)
4389 -> std::string {
4390 return to_string(format_as(value));
4391 }
4392
4393 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value &&
4394 !detail::use_format_as<T>::value)>
4395 FMT_NODISCARD FMT_CONSTEXPR_STRING auto to_string(const T& value)
4396 -> std::string {
4397 auto buffer = memory_buffer();
4398 detail::write<char>(appender(buffer), value);
4399 return {buffer.data(), buffer.size()};
4400 }
4401
4402 FMT_END_EXPORT
4403 FMT_END_NAMESPACE
4404
4405 #ifdef FMT_HEADER_ONLY
4406 # define FMT_FUNC inline
4407 # include "format-inl.h"
4408 #endif
4409
4410 // Restore _LIBCPP_REMOVE_TRANSITIVE_INCLUDES.
4411 #ifdef FMT_REMOVE_TRANSITIVE_INCLUDES
4412 # undef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
4413 #endif
4414
4415 #endif // FMT_FORMAT_H_