Roboruka
Knihovna pro obsluhu RoboRuky.
format-inl.h
1// Formatting library for C++ - implementation
2//
3// Copyright (c) 2012 - 2016, Victor Zverovich
4// All rights reserved.
5//
6// For the license information refer to format.h.
7
8#ifndef FMT_FORMAT_INL_H_
9#define FMT_FORMAT_INL_H_
10
11#include <cassert>
12#include <cctype>
13#include <climits>
14#include <cmath>
15#include <cstdarg>
16#include <cstring> // for std::memmove
17#include <cwchar>
18
19#include "format.h"
20#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
21# include <locale>
22#endif
23
24#ifdef _WIN32
25# include <io.h>
26# include <windows.h>
27#endif
28
29#ifdef _MSC_VER
30# pragma warning(push)
31# pragma warning(disable : 4702) // unreachable code
32#endif
33
34// Dummy implementations of strerror_r and strerror_s called if corresponding
35// system functions are not available.
36inline fmt::internal::null<> strerror_r(int, char*, ...) { return {}; }
37inline fmt::internal::null<> strerror_s(char*, std::size_t, ...) { return {}; }
38
39FMT_BEGIN_NAMESPACE
40namespace internal {
41
42FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
43 print(stderr, "{}:{}: assertion failed: {}", file, line, message);
44 std::abort();
45}
46
47#ifndef _MSC_VER
48# define FMT_SNPRINTF snprintf
49#else // _MSC_VER
50inline int fmt_snprintf(char* buffer, size_t size, const char* format, ...) {
51 va_list args;
52 va_start(args, format);
53 int result = vsnprintf_s(buffer, size, _TRUNCATE, format, args);
54 va_end(args);
55 return result;
56}
57# define FMT_SNPRINTF fmt_snprintf
58#endif // _MSC_VER
59
60// A portable thread-safe version of strerror.
61// Sets buffer to point to a string describing the error code.
62// This can be either a pointer to a string stored in buffer,
63// or a pointer to some static immutable string.
64// Returns one of the following values:
65// 0 - success
66// ERANGE - buffer is not large enough to store the error message
67// other - failure
68// Buffer should be at least of size 1.
69FMT_FUNC int safe_strerror(int error_code, char*& buffer,
70 std::size_t buffer_size) FMT_NOEXCEPT {
71 FMT_ASSERT(buffer != nullptr && buffer_size != 0, "invalid buffer");
72
73 class dispatcher {
74 private:
75 int error_code_;
76 char*& buffer_;
77 std::size_t buffer_size_;
78
79 // A noop assignment operator to avoid bogus warnings.
80 void operator=(const dispatcher&) {}
81
82 // Handle the result of XSI-compliant version of strerror_r.
83 int handle(int result) {
84 // glibc versions before 2.13 return result in errno.
85 return result == -1 ? errno : result;
86 }
87
88 // Handle the result of GNU-specific version of strerror_r.
89 FMT_MAYBE_UNUSED
90 int handle(char* message) {
91 // If the buffer is full then the message is probably truncated.
92 if (message == buffer_ && strlen(buffer_) == buffer_size_ - 1)
93 return ERANGE;
94 buffer_ = message;
95 return 0;
96 }
97
98 // Handle the case when strerror_r is not available.
99 FMT_MAYBE_UNUSED
100 int handle(internal::null<>) {
101 return fallback(strerror_s(buffer_, buffer_size_, error_code_));
102 }
103
104 // Fallback to strerror_s when strerror_r is not available.
105 FMT_MAYBE_UNUSED
106 int fallback(int result) {
107 // If the buffer is full then the message is probably truncated.
108 return result == 0 && strlen(buffer_) == buffer_size_ - 1 ? ERANGE
109 : result;
110 }
111
112#if !FMT_MSC_VER
113 // Fallback to strerror if strerror_r and strerror_s are not available.
114 int fallback(internal::null<>) {
115 errno = 0;
116 buffer_ = strerror(error_code_);
117 return errno;
118 }
119#endif
120
121 public:
122 dispatcher(int err_code, char*& buf, std::size_t buf_size)
123 : error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {}
124
125 int run() { return handle(strerror_r(error_code_, buffer_, buffer_size_)); }
126 };
127 return dispatcher(error_code, buffer, buffer_size).run();
128}
129
130FMT_FUNC void format_error_code(internal::buffer<char>& out, int error_code,
131 string_view message) FMT_NOEXCEPT {
132 // Report error code making sure that the output fits into
133 // inline_buffer_size to avoid dynamic memory allocation and potential
134 // bad_alloc.
135 out.resize(0);
136 static const char SEP[] = ": ";
137 static const char ERROR_STR[] = "error ";
138 // Subtract 2 to account for terminating null characters in SEP and ERROR_STR.
139 std::size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2;
140 auto abs_value = static_cast<uint32_or_64_or_128_t<int>>(error_code);
141 if (internal::is_negative(error_code)) {
142 abs_value = 0 - abs_value;
143 ++error_code_size;
144 }
145 error_code_size += internal::to_unsigned(internal::count_digits(abs_value));
146 internal::writer w(out);
147 if (message.size() <= inline_buffer_size - error_code_size) {
148 w.write(message);
149 w.write(SEP);
150 }
151 w.write(ERROR_STR);
152 w.write(error_code);
153 assert(out.size() <= inline_buffer_size);
154}
155
156FMT_FUNC void report_error(format_func func, int error_code,
157 string_view message) FMT_NOEXCEPT {
158 memory_buffer full_message;
159 func(full_message, error_code, message);
160 // Don't use fwrite_fully because the latter may throw.
161 (void)std::fwrite(full_message.data(), full_message.size(), 1, stderr);
162 std::fputc('\n', stderr);
163}
164
165// A wrapper around fwrite that throws on error.
166FMT_FUNC void fwrite_fully(const void* ptr, size_t size, size_t count,
167 FILE* stream) {
168 size_t written = std::fwrite(ptr, size, count, stream);
169 if (written < count) FMT_THROW(system_error(errno, "cannot write to file"));
170}
171} // namespace internal
172
173#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
174namespace internal {
175
176template <typename Locale>
177locale_ref::locale_ref(const Locale& loc) : locale_(&loc) {
178 static_assert(std::is_same<Locale, std::locale>::value, "");
179}
180
181template <typename Locale> Locale locale_ref::get() const {
182 static_assert(std::is_same<Locale, std::locale>::value, "");
183 return locale_ ? *static_cast<const std::locale*>(locale_) : std::locale();
184}
185
186template <typename Char> FMT_FUNC std::string grouping_impl(locale_ref loc) {
187 return std::use_facet<std::numpunct<Char>>(loc.get<std::locale>()).grouping();
188}
189template <typename Char> FMT_FUNC Char thousands_sep_impl(locale_ref loc) {
190 return std::use_facet<std::numpunct<Char>>(loc.get<std::locale>())
191 .thousands_sep();
192}
193template <typename Char> FMT_FUNC Char decimal_point_impl(locale_ref loc) {
194 return std::use_facet<std::numpunct<Char>>(loc.get<std::locale>())
195 .decimal_point();
196}
197} // namespace internal
198#else
199template <typename Char>
200FMT_FUNC std::string internal::grouping_impl(locale_ref) {
201 return "\03";
202}
203template <typename Char>
204FMT_FUNC Char internal::thousands_sep_impl(locale_ref) {
205 return FMT_STATIC_THOUSANDS_SEPARATOR;
206}
207template <typename Char>
208FMT_FUNC Char internal::decimal_point_impl(locale_ref) {
209 return '.';
210}
211#endif
212
213FMT_API FMT_FUNC format_error::~format_error() FMT_NOEXCEPT = default;
214FMT_API FMT_FUNC system_error::~system_error() FMT_NOEXCEPT = default;
215
216FMT_FUNC void system_error::init(int err_code, string_view format_str,
217 format_args args) {
218 error_code_ = err_code;
219 memory_buffer buffer;
220 format_system_error(buffer, err_code, vformat(format_str, args));
221 std::runtime_error& base = *this;
222 base = std::runtime_error(to_string(buffer));
223}
224
225namespace internal {
226
227template <> FMT_FUNC int count_digits<4>(internal::fallback_uintptr n) {
228 // fallback_uintptr is always stored in little endian.
229 int i = static_cast<int>(sizeof(void*)) - 1;
230 while (i > 0 && n.value[i] == 0) --i;
231 auto char_digits = std::numeric_limits<unsigned char>::digits / 4;
232 return i >= 0 ? i * char_digits + count_digits<4, unsigned>(n.value[i]) : 1;
233}
234
235template <typename T>
236const char basic_data<T>::digits[] =
237 "0001020304050607080910111213141516171819"
238 "2021222324252627282930313233343536373839"
239 "4041424344454647484950515253545556575859"
240 "6061626364656667686970717273747576777879"
241 "8081828384858687888990919293949596979899";
242
243template <typename T>
244const char basic_data<T>::hex_digits[] = "0123456789abcdef";
245
246#define FMT_POWERS_OF_10(factor) \
247 factor * 10, (factor)*100, (factor)*1000, (factor)*10000, (factor)*100000, \
248 (factor)*1000000, (factor)*10000000, (factor)*100000000, \
249 (factor)*1000000000
250
251template <typename T>
252const uint64_t basic_data<T>::powers_of_10_64[] = {
253 1, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ULL),
254 10000000000000000000ULL};
255
256template <typename T>
257const uint32_t basic_data<T>::zero_or_powers_of_10_32[] = {0,
258 FMT_POWERS_OF_10(1)};
259
260template <typename T>
261const uint64_t basic_data<T>::zero_or_powers_of_10_64[] = {
262 0, FMT_POWERS_OF_10(1), FMT_POWERS_OF_10(1000000000ULL),
263 10000000000000000000ULL};
264
265// Normalized 64-bit significands of pow(10, k), for k = -348, -340, ..., 340.
266// These are generated by support/compute-powers.py.
267template <typename T>
268const uint64_t basic_data<T>::pow10_significands[] = {
269 0xfa8fd5a0081c0288, 0xbaaee17fa23ebf76, 0x8b16fb203055ac76,
270 0xcf42894a5dce35ea, 0x9a6bb0aa55653b2d, 0xe61acf033d1a45df,
271 0xab70fe17c79ac6ca, 0xff77b1fcbebcdc4f, 0xbe5691ef416bd60c,
272 0x8dd01fad907ffc3c, 0xd3515c2831559a83, 0x9d71ac8fada6c9b5,
273 0xea9c227723ee8bcb, 0xaecc49914078536d, 0x823c12795db6ce57,
274 0xc21094364dfb5637, 0x9096ea6f3848984f, 0xd77485cb25823ac7,
275 0xa086cfcd97bf97f4, 0xef340a98172aace5, 0xb23867fb2a35b28e,
276 0x84c8d4dfd2c63f3b, 0xc5dd44271ad3cdba, 0x936b9fcebb25c996,
277 0xdbac6c247d62a584, 0xa3ab66580d5fdaf6, 0xf3e2f893dec3f126,
278 0xb5b5ada8aaff80b8, 0x87625f056c7c4a8b, 0xc9bcff6034c13053,
279 0x964e858c91ba2655, 0xdff9772470297ebd, 0xa6dfbd9fb8e5b88f,
280 0xf8a95fcf88747d94, 0xb94470938fa89bcf, 0x8a08f0f8bf0f156b,
281 0xcdb02555653131b6, 0x993fe2c6d07b7fac, 0xe45c10c42a2b3b06,
282 0xaa242499697392d3, 0xfd87b5f28300ca0e, 0xbce5086492111aeb,
283 0x8cbccc096f5088cc, 0xd1b71758e219652c, 0x9c40000000000000,
284 0xe8d4a51000000000, 0xad78ebc5ac620000, 0x813f3978f8940984,
285 0xc097ce7bc90715b3, 0x8f7e32ce7bea5c70, 0xd5d238a4abe98068,
286 0x9f4f2726179a2245, 0xed63a231d4c4fb27, 0xb0de65388cc8ada8,
287 0x83c7088e1aab65db, 0xc45d1df942711d9a, 0x924d692ca61be758,
288 0xda01ee641a708dea, 0xa26da3999aef774a, 0xf209787bb47d6b85,
289 0xb454e4a179dd1877, 0x865b86925b9bc5c2, 0xc83553c5c8965d3d,
290 0x952ab45cfa97a0b3, 0xde469fbd99a05fe3, 0xa59bc234db398c25,
291 0xf6c69a72a3989f5c, 0xb7dcbf5354e9bece, 0x88fcf317f22241e2,
292 0xcc20ce9bd35c78a5, 0x98165af37b2153df, 0xe2a0b5dc971f303a,
293 0xa8d9d1535ce3b396, 0xfb9b7cd9a4a7443c, 0xbb764c4ca7a44410,
294 0x8bab8eefb6409c1a, 0xd01fef10a657842c, 0x9b10a4e5e9913129,
295 0xe7109bfba19c0c9d, 0xac2820d9623bf429, 0x80444b5e7aa7cf85,
296 0xbf21e44003acdd2d, 0x8e679c2f5e44ff8f, 0xd433179d9c8cb841,
297 0x9e19db92b4e31ba9, 0xeb96bf6ebadf77d9, 0xaf87023b9bf0ee6b,
298};
299
300// Binary exponents of pow(10, k), for k = -348, -340, ..., 340, corresponding
301// to significands above.
302template <typename T>
303const int16_t basic_data<T>::pow10_exponents[] = {
304 -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, -954,
305 -927, -901, -874, -847, -821, -794, -768, -741, -715, -688, -661,
306 -635, -608, -582, -555, -529, -502, -475, -449, -422, -396, -369,
307 -343, -316, -289, -263, -236, -210, -183, -157, -130, -103, -77,
308 -50, -24, 3, 30, 56, 83, 109, 136, 162, 189, 216,
309 242, 269, 295, 322, 348, 375, 402, 428, 455, 481, 508,
310 534, 561, 588, 614, 641, 667, 694, 720, 747, 774, 800,
311 827, 853, 880, 907, 933, 960, 986, 1013, 1039, 1066};
312
313template <typename T>
314const char basic_data<T>::foreground_color[] = "\x1b[38;2;";
315template <typename T>
316const char basic_data<T>::background_color[] = "\x1b[48;2;";
317template <typename T> const char basic_data<T>::reset_color[] = "\x1b[0m";
318template <typename T> const wchar_t basic_data<T>::wreset_color[] = L"\x1b[0m";
319template <typename T> const char basic_data<T>::signs[] = {0, '-', '+', ' '};
320
321template <typename T> struct bits {
322 static FMT_CONSTEXPR_DECL const int value =
323 static_cast<int>(sizeof(T) * std::numeric_limits<unsigned char>::digits);
324};
325
326class fp;
327template <int SHIFT = 0> fp normalize(fp value);
328
329// Lower (upper) boundary is a value half way between a floating-point value
330// and its predecessor (successor). Boundaries have the same exponent as the
331// value so only significands are stored.
332struct boundaries {
333 uint64_t lower;
334 uint64_t upper;
335};
336
337// A handmade floating-point number f * pow(2, e).
338class fp {
339 private:
340 using significand_type = uint64_t;
341
342 public:
343 significand_type f;
344 int e;
345
346 // All sizes are in bits.
347 // Subtract 1 to account for an implicit most significant bit in the
348 // normalized form.
349 static FMT_CONSTEXPR_DECL const int double_significand_size =
350 std::numeric_limits<double>::digits - 1;
351 static FMT_CONSTEXPR_DECL const uint64_t implicit_bit =
352 1ULL << double_significand_size;
353 static FMT_CONSTEXPR_DECL const int significand_size =
354 bits<significand_type>::value;
355
356 fp() : f(0), e(0) {}
357 fp(uint64_t f_val, int e_val) : f(f_val), e(e_val) {}
358
359 // Constructs fp from an IEEE754 double. It is a template to prevent compile
360 // errors on platforms where double is not IEEE754.
361 template <typename Double> explicit fp(Double d) { assign(d); }
362
363 // Assigns d to this and return true iff predecessor is closer than successor.
364 template <typename Double, FMT_ENABLE_IF(sizeof(Double) == sizeof(uint64_t))>
365 bool assign(Double d) {
366 // Assume double is in the format [sign][exponent][significand].
367 using limits = std::numeric_limits<Double>;
368 const int exponent_size =
369 bits<Double>::value - double_significand_size - 1; // -1 for sign
370 const uint64_t significand_mask = implicit_bit - 1;
371 const uint64_t exponent_mask = (~0ULL >> 1) & ~significand_mask;
372 const int exponent_bias = (1 << exponent_size) - limits::max_exponent - 1;
373 auto u = bit_cast<uint64_t>(d);
374 f = u & significand_mask;
375 int biased_e =
376 static_cast<int>((u & exponent_mask) >> double_significand_size);
377 // Predecessor is closer if d is a normalized power of 2 (f == 0) other than
378 // the smallest normalized number (biased_e > 1).
379 bool is_predecessor_closer = f == 0 && biased_e > 1;
380 if (biased_e != 0)
381 f += implicit_bit;
382 else
383 biased_e = 1; // Subnormals use biased exponent 1 (min exponent).
384 e = biased_e - exponent_bias - double_significand_size;
385 return is_predecessor_closer;
386 }
387
388 template <typename Double, FMT_ENABLE_IF(sizeof(Double) != sizeof(uint64_t))>
389 bool assign(Double) {
390 *this = fp();
391 return false;
392 }
393
394 // Assigns d to this together with computing lower and upper boundaries,
395 // where a boundary is a value half way between the number and its predecessor
396 // (lower) or successor (upper). The upper boundary is normalized and lower
397 // has the same exponent but may be not normalized.
398 template <typename Double> boundaries assign_with_boundaries(Double d) {
399 bool is_lower_closer = assign(d);
400 fp lower =
401 is_lower_closer ? fp((f << 2) - 1, e - 2) : fp((f << 1) - 1, e - 1);
402 // 1 in normalize accounts for the exponent shift above.
403 fp upper = normalize<1>(fp((f << 1) + 1, e - 1));
404 lower.f <<= lower.e - upper.e;
405 return boundaries{lower.f, upper.f};
406 }
407
408 template <typename Double> boundaries assign_float_with_boundaries(Double d) {
409 assign(d);
410 constexpr int min_normal_e = std::numeric_limits<float>::min_exponent -
411 std::numeric_limits<double>::digits;
412 significand_type half_ulp = 1 << (std::numeric_limits<double>::digits -
413 std::numeric_limits<float>::digits - 1);
414 if (min_normal_e > e) half_ulp <<= min_normal_e - e;
415 fp upper = normalize<0>(fp(f + half_ulp, e));
416 fp lower = fp(
417 f - (half_ulp >> ((f == implicit_bit && e > min_normal_e) ? 1 : 0)), e);
418 lower.f <<= lower.e - upper.e;
419 return boundaries{lower.f, upper.f};
420 }
421};
422
423// Normalizes the value converted from double and multiplied by (1 << SHIFT).
424template <int SHIFT> fp normalize(fp value) {
425 // Handle subnormals.
426 const auto shifted_implicit_bit = fp::implicit_bit << SHIFT;
427 while ((value.f & shifted_implicit_bit) == 0) {
428 value.f <<= 1;
429 --value.e;
430 }
431 // Subtract 1 to account for hidden bit.
432 const auto offset =
433 fp::significand_size - fp::double_significand_size - SHIFT - 1;
434 value.f <<= offset;
435 value.e -= offset;
436 return value;
437}
438
439inline bool operator==(fp x, fp y) { return x.f == y.f && x.e == y.e; }
440
441// Computes lhs * rhs / pow(2, 64) rounded to nearest with half-up tie breaking.
442inline uint64_t multiply(uint64_t lhs, uint64_t rhs) {
443#if FMT_USE_INT128
444 auto product = static_cast<__uint128_t>(lhs) * rhs;
445 auto f = static_cast<uint64_t>(product >> 64);
446 return (static_cast<uint64_t>(product) & (1ULL << 63)) != 0 ? f + 1 : f;
447#else
448 // Multiply 32-bit parts of significands.
449 uint64_t mask = (1ULL << 32) - 1;
450 uint64_t a = lhs >> 32, b = lhs & mask;
451 uint64_t c = rhs >> 32, d = rhs & mask;
452 uint64_t ac = a * c, bc = b * c, ad = a * d, bd = b * d;
453 // Compute mid 64-bit of result and round.
454 uint64_t mid = (bd >> 32) + (ad & mask) + (bc & mask) + (1U << 31);
455 return ac + (ad >> 32) + (bc >> 32) + (mid >> 32);
456#endif
457}
458
459inline fp operator*(fp x, fp y) { return {multiply(x.f, y.f), x.e + y.e + 64}; }
460
461// Returns a cached power of 10 `c_k = c_k.f * pow(2, c_k.e)` such that its
462// (binary) exponent satisfies `min_exponent <= c_k.e <= min_exponent + 28`.
463inline fp get_cached_power(int min_exponent, int& pow10_exponent) {
464 const int64_t one_over_log2_10 = 0x4d104d42; // round(pow(2, 32) / log2(10))
465 int index = static_cast<int>(
466 ((min_exponent + fp::significand_size - 1) * one_over_log2_10 +
467 ((int64_t(1) << 32) - 1)) // ceil
468 >> 32 // arithmetic shift
469 );
470 // Decimal exponent of the first (smallest) cached power of 10.
471 const int first_dec_exp = -348;
472 // Difference between 2 consecutive decimal exponents in cached powers of 10.
473 const int dec_exp_step = 8;
474 index = (index - first_dec_exp - 1) / dec_exp_step + 1;
475 pow10_exponent = first_dec_exp + index * dec_exp_step;
476 return {data::pow10_significands[index], data::pow10_exponents[index]};
477}
478
479// A simple accumulator to hold the sums of terms in bigint::square if uint128_t
480// is not available.
481struct accumulator {
482 uint64_t lower;
483 uint64_t upper;
484
485 accumulator() : lower(0), upper(0) {}
486 explicit operator uint32_t() const { return static_cast<uint32_t>(lower); }
487
488 void operator+=(uint64_t n) {
489 lower += n;
490 if (lower < n) ++upper;
491 }
492 void operator>>=(int shift) {
493 assert(shift == 32);
494 (void)shift;
495 lower = (upper << 32) | (lower >> 32);
496 upper >>= 32;
497 }
498};
499
500class bigint {
501 private:
502 // A bigint is stored as an array of bigits (big digits), with bigit at index
503 // 0 being the least significant one.
504 using bigit = uint32_t;
505 using double_bigit = uint64_t;
506 enum { bigits_capacity = 32 };
508 int exp_;
509
510 bigit operator[](int index) const { return bigits_[to_unsigned(index)]; }
511 bigit& operator[](int index) { return bigits_[to_unsigned(index)]; }
512
513 static FMT_CONSTEXPR_DECL const int bigit_bits = bits<bigit>::value;
514
515 friend struct formatter<bigint>;
516
517 void subtract_bigits(int index, bigit other, bigit& borrow) {
518 auto result = static_cast<double_bigit>((*this)[index]) - other - borrow;
519 (*this)[index] = static_cast<bigit>(result);
520 borrow = static_cast<bigit>(result >> (bigit_bits * 2 - 1));
521 }
522
523 void remove_leading_zeros() {
524 int num_bigits = static_cast<int>(bigits_.size()) - 1;
525 while (num_bigits > 0 && (*this)[num_bigits] == 0) --num_bigits;
526 bigits_.resize(to_unsigned(num_bigits + 1));
527 }
528
529 // Computes *this -= other assuming aligned bigints and *this >= other.
530 void subtract_aligned(const bigint& other) {
531 FMT_ASSERT(other.exp_ >= exp_, "unaligned bigints");
532 FMT_ASSERT(compare(*this, other) >= 0, "");
533 bigit borrow = 0;
534 int i = other.exp_ - exp_;
535 for (size_t j = 0, n = other.bigits_.size(); j != n; ++i, ++j) {
536 subtract_bigits(i, other.bigits_[j], borrow);
537 }
538 while (borrow > 0) subtract_bigits(i, 0, borrow);
539 remove_leading_zeros();
540 }
541
542 void multiply(uint32_t value) {
543 const double_bigit wide_value = value;
544 bigit carry = 0;
545 for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
546 double_bigit result = bigits_[i] * wide_value + carry;
547 bigits_[i] = static_cast<bigit>(result);
548 carry = static_cast<bigit>(result >> bigit_bits);
549 }
550 if (carry != 0) bigits_.push_back(carry);
551 }
552
553 void multiply(uint64_t value) {
554 const bigit mask = ~bigit(0);
555 const double_bigit lower = value & mask;
556 const double_bigit upper = value >> bigit_bits;
557 double_bigit carry = 0;
558 for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
559 double_bigit result = bigits_[i] * lower + (carry & mask);
560 carry =
561 bigits_[i] * upper + (result >> bigit_bits) + (carry >> bigit_bits);
562 bigits_[i] = static_cast<bigit>(result);
563 }
564 while (carry != 0) {
565 bigits_.push_back(carry & mask);
566 carry >>= bigit_bits;
567 }
568 }
569
570 public:
571 bigint() : exp_(0) {}
572 explicit bigint(uint64_t n) { assign(n); }
573 ~bigint() { assert(bigits_.capacity() <= bigits_capacity); }
574
575 bigint(const bigint&) = delete;
576 void operator=(const bigint&) = delete;
577
578 void assign(const bigint& other) {
579 bigits_.resize(other.bigits_.size());
580 auto data = other.bigits_.data();
581 std::copy(data, data + other.bigits_.size(), bigits_.data());
582 exp_ = other.exp_;
583 }
584
585 void assign(uint64_t n) {
586 size_t num_bigits = 0;
587 do {
588 bigits_[num_bigits++] = n & ~bigit(0);
589 n >>= bigit_bits;
590 } while (n != 0);
591 bigits_.resize(num_bigits);
592 exp_ = 0;
593 }
594
595 int num_bigits() const { return static_cast<int>(bigits_.size()) + exp_; }
596
597 bigint& operator<<=(int shift) {
598 assert(shift >= 0);
599 exp_ += shift / bigit_bits;
600 shift %= bigit_bits;
601 if (shift == 0) return *this;
602 bigit carry = 0;
603 for (size_t i = 0, n = bigits_.size(); i < n; ++i) {
604 bigit c = bigits_[i] >> (bigit_bits - shift);
605 bigits_[i] = (bigits_[i] << shift) + carry;
606 carry = c;
607 }
608 if (carry != 0) bigits_.push_back(carry);
609 return *this;
610 }
611
612 template <typename Int> bigint& operator*=(Int value) {
613 FMT_ASSERT(value > 0, "");
614 multiply(uint32_or_64_or_128_t<Int>(value));
615 return *this;
616 }
617
618 friend int compare(const bigint& lhs, const bigint& rhs) {
619 int num_lhs_bigits = lhs.num_bigits(), num_rhs_bigits = rhs.num_bigits();
620 if (num_lhs_bigits != num_rhs_bigits)
621 return num_lhs_bigits > num_rhs_bigits ? 1 : -1;
622 int i = static_cast<int>(lhs.bigits_.size()) - 1;
623 int j = static_cast<int>(rhs.bigits_.size()) - 1;
624 int end = i - j;
625 if (end < 0) end = 0;
626 for (; i >= end; --i, --j) {
627 bigit lhs_bigit = lhs[i], rhs_bigit = rhs[j];
628 if (lhs_bigit != rhs_bigit) return lhs_bigit > rhs_bigit ? 1 : -1;
629 }
630 if (i != j) return i > j ? 1 : -1;
631 return 0;
632 }
633
634 // Returns compare(lhs1 + lhs2, rhs).
635 friend int add_compare(const bigint& lhs1, const bigint& lhs2,
636 const bigint& rhs) {
637 int max_lhs_bigits = (std::max)(lhs1.num_bigits(), lhs2.num_bigits());
638 int num_rhs_bigits = rhs.num_bigits();
639 if (max_lhs_bigits + 1 < num_rhs_bigits) return -1;
640 if (max_lhs_bigits > num_rhs_bigits) return 1;
641 auto get_bigit = [](const bigint& n, int i) -> bigit {
642 return i >= n.exp_ && i < n.num_bigits() ? n[i - n.exp_] : 0;
643 };
644 double_bigit borrow = 0;
645 int min_exp = (std::min)((std::min)(lhs1.exp_, lhs2.exp_), rhs.exp_);
646 for (int i = num_rhs_bigits - 1; i >= min_exp; --i) {
647 double_bigit sum =
648 static_cast<double_bigit>(get_bigit(lhs1, i)) + get_bigit(lhs2, i);
649 bigit rhs_bigit = get_bigit(rhs, i);
650 if (sum > rhs_bigit + borrow) return 1;
651 borrow = rhs_bigit + borrow - sum;
652 if (borrow > 1) return -1;
653 borrow <<= bigit_bits;
654 }
655 return borrow != 0 ? -1 : 0;
656 }
657
658 // Assigns pow(10, exp) to this bigint.
659 void assign_pow10(int exp) {
660 assert(exp >= 0);
661 if (exp == 0) return assign(1);
662 // Find the top bit.
663 int bitmask = 1;
664 while (exp >= bitmask) bitmask <<= 1;
665 bitmask >>= 1;
666 // pow(10, exp) = pow(5, exp) * pow(2, exp). First compute pow(5, exp) by
667 // repeated squaring and multiplication.
668 assign(5);
669 bitmask >>= 1;
670 while (bitmask != 0) {
671 square();
672 if ((exp & bitmask) != 0) *this *= 5;
673 bitmask >>= 1;
674 }
675 *this <<= exp; // Multiply by pow(2, exp) by shifting.
676 }
677
678 void square() {
680 int num_bigits = static_cast<int>(bigits_.size());
681 int num_result_bigits = 2 * num_bigits;
682 bigits_.resize(to_unsigned(num_result_bigits));
683 using accumulator_t = conditional_t<FMT_USE_INT128, uint128_t, accumulator>;
684 auto sum = accumulator_t();
685 for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) {
686 // Compute bigit at position bigit_index of the result by adding
687 // cross-product terms n[i] * n[j] such that i + j == bigit_index.
688 for (int i = 0, j = bigit_index; j >= 0; ++i, --j) {
689 // Most terms are multiplied twice which can be optimized in the future.
690 sum += static_cast<double_bigit>(n[i]) * n[j];
691 }
692 (*this)[bigit_index] = static_cast<bigit>(sum);
693 sum >>= bits<bigit>::value; // Compute the carry.
694 }
695 // Do the same for the top half.
696 for (int bigit_index = num_bigits; bigit_index < num_result_bigits;
697 ++bigit_index) {
698 for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;)
699 sum += static_cast<double_bigit>(n[i++]) * n[j--];
700 (*this)[bigit_index] = static_cast<bigit>(sum);
701 sum >>= bits<bigit>::value;
702 }
703 --num_result_bigits;
704 remove_leading_zeros();
705 exp_ *= 2;
706 }
707
708 // Divides this bignum by divisor, assigning the remainder to this and
709 // returning the quotient.
710 int divmod_assign(const bigint& divisor) {
711 FMT_ASSERT(this != &divisor, "");
712 if (compare(*this, divisor) < 0) return 0;
713 int num_bigits = static_cast<int>(bigits_.size());
714 FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1u] != 0, "");
715 int exp_difference = exp_ - divisor.exp_;
716 if (exp_difference > 0) {
717 // Align bigints by adding trailing zeros to simplify subtraction.
718 bigits_.resize(to_unsigned(num_bigits + exp_difference));
719 for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j)
720 bigits_[j] = bigits_[i];
721 std::uninitialized_fill_n(bigits_.data(), exp_difference, 0);
722 exp_ -= exp_difference;
723 }
724 int quotient = 0;
725 do {
726 subtract_aligned(divisor);
727 ++quotient;
728 } while (compare(*this, divisor) >= 0);
729 return quotient;
730 }
731};
732
733enum class round_direction { unknown, up, down };
734
735// Given the divisor (normally a power of 10), the remainder = v % divisor for
736// some number v and the error, returns whether v should be rounded up, down, or
737// whether the rounding direction can't be determined due to error.
738// error should be less than divisor / 2.
739inline round_direction get_round_direction(uint64_t divisor, uint64_t remainder,
740 uint64_t error) {
741 FMT_ASSERT(remainder < divisor, ""); // divisor - remainder won't overflow.
742 FMT_ASSERT(error < divisor, ""); // divisor - error won't overflow.
743 FMT_ASSERT(error < divisor - error, ""); // error * 2 won't overflow.
744 // Round down if (remainder + error) * 2 <= divisor.
745 if (remainder <= divisor - remainder && error * 2 <= divisor - remainder * 2)
746 return round_direction::down;
747 // Round up if (remainder - error) * 2 >= divisor.
748 if (remainder >= error &&
749 remainder - error >= divisor - (remainder - error)) {
750 return round_direction::up;
751 }
752 return round_direction::unknown;
753}
754
755namespace digits {
756enum result {
757 more, // Generate more digits.
758 done, // Done generating digits.
759 error // Digit generation cancelled due to an error.
760};
761}
762
763// A version of count_digits optimized for grisu_gen_digits.
764inline int grisu_count_digits(uint32_t n) {
765 if (n < 10) return 1;
766 if (n < 100) return 2;
767 if (n < 1000) return 3;
768 if (n < 10000) return 4;
769 if (n < 100000) return 5;
770 if (n < 1000000) return 6;
771 if (n < 10000000) return 7;
772 if (n < 100000000) return 8;
773 if (n < 1000000000) return 9;
774 return 10;
775}
776
777// Generates output using the Grisu digit-gen algorithm.
778// error: the size of the region (lower, upper) outside of which numbers
779// definitely do not round to value (Delta in Grisu3).
780template <typename Handler>
781FMT_ALWAYS_INLINE digits::result grisu_gen_digits(fp value, uint64_t error,
782 int& exp, Handler& handler) {
783 const fp one(1ULL << -value.e, value.e);
784 // The integral part of scaled value (p1 in Grisu) = value / one. It cannot be
785 // zero because it contains a product of two 64-bit numbers with MSB set (due
786 // to normalization) - 1, shifted right by at most 60 bits.
787 auto integral = static_cast<uint32_t>(value.f >> -one.e);
788 FMT_ASSERT(integral != 0, "");
789 FMT_ASSERT(integral == value.f >> -one.e, "");
790 // The fractional part of scaled value (p2 in Grisu) c = value % one.
791 uint64_t fractional = value.f & (one.f - 1);
792 exp = grisu_count_digits(integral); // kappa in Grisu.
793 // Divide by 10 to prevent overflow.
794 auto result = handler.on_start(data::powers_of_10_64[exp - 1] << -one.e,
795 value.f / 10, error * 10, exp);
796 if (result != digits::more) return result;
797 // Generate digits for the integral part. This can produce up to 10 digits.
798 do {
799 uint32_t digit = 0;
800 auto divmod_integral = [&](uint32_t divisor) {
801 digit = integral / divisor;
802 integral %= divisor;
803 };
804 // This optimization by Milo Yip reduces the number of integer divisions by
805 // one per iteration.
806 switch (exp) {
807 case 10:
808 divmod_integral(1000000000);
809 break;
810 case 9:
811 divmod_integral(100000000);
812 break;
813 case 8:
814 divmod_integral(10000000);
815 break;
816 case 7:
817 divmod_integral(1000000);
818 break;
819 case 6:
820 divmod_integral(100000);
821 break;
822 case 5:
823 divmod_integral(10000);
824 break;
825 case 4:
826 divmod_integral(1000);
827 break;
828 case 3:
829 divmod_integral(100);
830 break;
831 case 2:
832 divmod_integral(10);
833 break;
834 case 1:
835 digit = integral;
836 integral = 0;
837 break;
838 default:
839 FMT_ASSERT(false, "invalid number of digits");
840 }
841 --exp;
842 uint64_t remainder =
843 (static_cast<uint64_t>(integral) << -one.e) + fractional;
844 result = handler.on_digit(static_cast<char>('0' + digit),
845 data::powers_of_10_64[exp] << -one.e, remainder,
846 error, exp, true);
847 if (result != digits::more) return result;
848 } while (exp > 0);
849 // Generate digits for the fractional part.
850 for (;;) {
851 fractional *= 10;
852 error *= 10;
853 char digit =
854 static_cast<char>('0' + static_cast<char>(fractional >> -one.e));
855 fractional &= one.f - 1;
856 --exp;
857 result = handler.on_digit(digit, one.f, fractional, error, exp, false);
858 if (result != digits::more) return result;
859 }
860}
861
862// The fixed precision digit handler.
863struct fixed_handler {
864 char* buf;
865 int size;
866 int precision;
867 int exp10;
868 bool fixed;
869
870 digits::result on_start(uint64_t divisor, uint64_t remainder, uint64_t error,
871 int& exp) {
872 // Non-fixed formats require at least one digit and no precision adjustment.
873 if (!fixed) return digits::more;
874 // Adjust fixed precision by exponent because it is relative to decimal
875 // point.
876 precision += exp + exp10;
877 // Check if precision is satisfied just by leading zeros, e.g.
878 // format("{:.2f}", 0.001) gives "0.00" without generating any digits.
879 if (precision > 0) return digits::more;
880 if (precision < 0) return digits::done;
881 auto dir = get_round_direction(divisor, remainder, error);
882 if (dir == round_direction::unknown) return digits::error;
883 buf[size++] = dir == round_direction::up ? '1' : '0';
884 return digits::done;
885 }
886
887 digits::result on_digit(char digit, uint64_t divisor, uint64_t remainder,
888 uint64_t error, int, bool integral) {
889 FMT_ASSERT(remainder < divisor, "");
890 buf[size++] = digit;
891 if (size < precision) return digits::more;
892 if (!integral) {
893 // Check if error * 2 < divisor with overflow prevention.
894 // The check is not needed for the integral part because error = 1
895 // and divisor > (1 << 32) there.
896 if (error >= divisor || error >= divisor - error) return digits::error;
897 } else {
898 FMT_ASSERT(error == 1 && divisor > 2, "");
899 }
900 auto dir = get_round_direction(divisor, remainder, error);
901 if (dir != round_direction::up)
902 return dir == round_direction::down ? digits::done : digits::error;
903 ++buf[size - 1];
904 for (int i = size - 1; i > 0 && buf[i] > '9'; --i) {
905 buf[i] = '0';
906 ++buf[i - 1];
907 }
908 if (buf[0] > '9') {
909 buf[0] = '1';
910 buf[size++] = '0';
911 }
912 return digits::done;
913 }
914};
915
916// The shortest representation digit handler.
917struct grisu_shortest_handler {
918 char* buf;
919 int size;
920 // Distance between scaled value and upper bound (wp_W in Grisu3).
921 uint64_t diff;
922
923 digits::result on_start(uint64_t, uint64_t, uint64_t, int&) {
924 return digits::more;
925 }
926
927 // Decrement the generated number approaching value from above.
928 void round(uint64_t d, uint64_t divisor, uint64_t& remainder,
929 uint64_t error) {
930 while (
931 remainder < d && error - remainder >= divisor &&
932 (remainder + divisor < d || d - remainder >= remainder + divisor - d)) {
933 --buf[size - 1];
934 remainder += divisor;
935 }
936 }
937
938 // Implements Grisu's round_weed.
939 digits::result on_digit(char digit, uint64_t divisor, uint64_t remainder,
940 uint64_t error, int exp, bool integral) {
941 buf[size++] = digit;
942 if (remainder >= error) return digits::more;
943 uint64_t unit = integral ? 1 : data::powers_of_10_64[-exp];
944 uint64_t up = (diff - 1) * unit; // wp_Wup
945 round(up, divisor, remainder, error);
946 uint64_t down = (diff + 1) * unit; // wp_Wdown
947 if (remainder < down && error - remainder >= divisor &&
948 (remainder + divisor < down ||
949 down - remainder > remainder + divisor - down)) {
950 return digits::error;
951 }
952 return 2 * unit <= remainder && remainder <= error - 4 * unit
953 ? digits::done
954 : digits::error;
955 }
956};
957
958// Formats value using a variation of the Fixed-Precision Positive
959// Floating-Point Printout ((FPP)^2) algorithm by Steele & White:
960// https://fmt.dev/p372-steele.pdf.
961template <typename Double>
962void fallback_format(Double d, buffer<char>& buf, int& exp10) {
963 bigint numerator; // 2 * R in (FPP)^2.
964 bigint denominator; // 2 * S in (FPP)^2.
965 // lower and upper are differences between value and corresponding boundaries.
966 bigint lower; // (M^- in (FPP)^2).
967 bigint upper_store; // upper's value if different from lower.
968 bigint* upper = nullptr; // (M^+ in (FPP)^2).
969 fp value;
970 // Shift numerator and denominator by an extra bit or two (if lower boundary
971 // is closer) to make lower and upper integers. This eliminates multiplication
972 // by 2 during later computations.
973 // TODO: handle float
974 int shift = value.assign(d) ? 2 : 1;
975 uint64_t significand = value.f << shift;
976 if (value.e >= 0) {
977 numerator.assign(significand);
978 numerator <<= value.e;
979 lower.assign(1);
980 lower <<= value.e;
981 if (shift != 1) {
982 upper_store.assign(1);
983 upper_store <<= value.e + 1;
984 upper = &upper_store;
985 }
986 denominator.assign_pow10(exp10);
987 denominator <<= 1;
988 } else if (exp10 < 0) {
989 numerator.assign_pow10(-exp10);
990 lower.assign(numerator);
991 if (shift != 1) {
992 upper_store.assign(numerator);
993 upper_store <<= 1;
994 upper = &upper_store;
995 }
996 numerator *= significand;
997 denominator.assign(1);
998 denominator <<= shift - value.e;
999 } else {
1000 numerator.assign(significand);
1001 denominator.assign_pow10(exp10);
1002 denominator <<= shift - value.e;
1003 lower.assign(1);
1004 if (shift != 1) {
1005 upper_store.assign(1ULL << 1);
1006 upper = &upper_store;
1007 }
1008 }
1009 if (!upper) upper = &lower;
1010 // Invariant: value == (numerator / denominator) * pow(10, exp10).
1011 bool even = (value.f & 1) == 0;
1012 int num_digits = 0;
1013 char* data = buf.data();
1014 for (;;) {
1015 int digit = numerator.divmod_assign(denominator);
1016 bool low = compare(numerator, lower) - even < 0; // numerator <[=] lower.
1017 // numerator + upper >[=] pow10:
1018 bool high = add_compare(numerator, *upper, denominator) + even > 0;
1019 data[num_digits++] = static_cast<char>('0' + digit);
1020 if (low || high) {
1021 if (!low) {
1022 ++data[num_digits - 1];
1023 } else if (high) {
1024 int result = add_compare(numerator, numerator, denominator);
1025 // Round half to even.
1026 if (result > 0 || (result == 0 && (digit % 2) != 0))
1027 ++data[num_digits - 1];
1028 }
1029 buf.resize(to_unsigned(num_digits));
1030 exp10 -= num_digits - 1;
1031 return;
1032 }
1033 numerator *= 10;
1034 lower *= 10;
1035 if (upper != &lower) *upper *= 10;
1036 }
1037}
1038
1039// Formats value using the Grisu algorithm
1040// (https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf)
1041// if T is a IEEE754 binary32 or binary64 and snprintf otherwise.
1042template <typename T>
1043int format_float(T value, int precision, float_specs specs, buffer<char>& buf) {
1044 static_assert(!std::is_same<T, float>::value, "");
1045 FMT_ASSERT(value >= 0, "value is negative");
1046
1047 const bool fixed = specs.format == float_format::fixed;
1048 if (value <= 0) { // <= instead of == to silence a warning.
1049 if (precision <= 0 || !fixed) {
1050 buf.push_back('0');
1051 return 0;
1052 }
1053 buf.resize(to_unsigned(precision));
1054 std::uninitialized_fill_n(buf.data(), precision, '0');
1055 return -precision;
1056 }
1057
1058 if (!specs.use_grisu) return snprintf_float(value, precision, specs, buf);
1059
1060 int exp = 0;
1061 const int min_exp = -60; // alpha in Grisu.
1062 int cached_exp10 = 0; // K in Grisu.
1063 if (precision < 0) {
1064 fp fp_value;
1065 auto boundaries = specs.binary32
1066 ? fp_value.assign_float_with_boundaries(value)
1067 : fp_value.assign_with_boundaries(value);
1068 fp_value = normalize(fp_value);
1069 // Find a cached power of 10 such that multiplying value by it will bring
1070 // the exponent in the range [min_exp, -32].
1071 const fp cached_pow = get_cached_power(
1072 min_exp - (fp_value.e + fp::significand_size), cached_exp10);
1073 // Multiply value and boundaries by the cached power of 10.
1074 fp_value = fp_value * cached_pow;
1075 boundaries.lower = multiply(boundaries.lower, cached_pow.f);
1076 boundaries.upper = multiply(boundaries.upper, cached_pow.f);
1077 assert(min_exp <= fp_value.e && fp_value.e <= -32);
1078 --boundaries.lower; // \tilde{M}^- - 1 ulp -> M^-_{\downarrow}.
1079 ++boundaries.upper; // \tilde{M}^+ + 1 ulp -> M^+_{\uparrow}.
1080 // Numbers outside of (lower, upper) definitely do not round to value.
1081 grisu_shortest_handler handler{buf.data(), 0,
1082 boundaries.upper - fp_value.f};
1083 auto result =
1084 grisu_gen_digits(fp(boundaries.upper, fp_value.e),
1085 boundaries.upper - boundaries.lower, exp, handler);
1086 if (result == digits::error) {
1087 exp += handler.size - cached_exp10 - 1;
1088 fallback_format(value, buf, exp);
1089 return exp;
1090 }
1091 buf.resize(to_unsigned(handler.size));
1092 } else {
1093 if (precision > 17) return snprintf_float(value, precision, specs, buf);
1094 fp normalized = normalize(fp(value));
1095 const auto cached_pow = get_cached_power(
1096 min_exp - (normalized.e + fp::significand_size), cached_exp10);
1097 normalized = normalized * cached_pow;
1098 fixed_handler handler{buf.data(), 0, precision, -cached_exp10, fixed};
1099 if (grisu_gen_digits(normalized, 1, exp, handler) == digits::error)
1100 return snprintf_float(value, precision, specs, buf);
1101 int num_digits = handler.size;
1102 if (!fixed) {
1103 // Remove trailing zeros.
1104 while (num_digits > 0 && buf[num_digits - 1] == '0') {
1105 --num_digits;
1106 ++exp;
1107 }
1108 }
1109 buf.resize(to_unsigned(num_digits));
1110 }
1111 return exp - cached_exp10;
1112}
1113
1114template <typename T>
1115int snprintf_float(T value, int precision, float_specs specs,
1116 buffer<char>& buf) {
1117 // Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail.
1118 FMT_ASSERT(buf.capacity() > buf.size(), "empty buffer");
1119 static_assert(!std::is_same<T, float>::value, "");
1120
1121 // Subtract 1 to account for the difference in precision since we use %e for
1122 // both general and exponent format.
1123 if (specs.format == float_format::general ||
1124 specs.format == float_format::exp)
1125 precision = (precision >= 0 ? precision : 6) - 1;
1126
1127 // Build the format string.
1128 enum { max_format_size = 7 }; // Ths longest format is "%#.*Le".
1129 char format[max_format_size];
1130 char* format_ptr = format;
1131 *format_ptr++ = '%';
1132 if (specs.showpoint && specs.format == float_format::hex) *format_ptr++ = '#';
1133 if (precision >= 0) {
1134 *format_ptr++ = '.';
1135 *format_ptr++ = '*';
1136 }
1137 if (std::is_same<T, long double>()) *format_ptr++ = 'L';
1138 *format_ptr++ = specs.format != float_format::hex
1139 ? (specs.format == float_format::fixed ? 'f' : 'e')
1140 : (specs.upper ? 'A' : 'a');
1141 *format_ptr = '\0';
1142
1143 // Format using snprintf.
1144 auto offset = buf.size();
1145 for (;;) {
1146 auto begin = buf.data() + offset;
1147 auto capacity = buf.capacity() - offset;
1148#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1149 if (precision > 100000)
1150 throw std::runtime_error(
1151 "fuzz mode - avoid large allocation inside snprintf");
1152#endif
1153 // Suppress the warning about a nonliteral format string.
1154 // Cannot use auto becase of a bug in MinGW (#1532).
1155 int (*snprintf_ptr)(char*, size_t, const char*, ...) = FMT_SNPRINTF;
1156 int result = precision >= 0
1157 ? snprintf_ptr(begin, capacity, format, precision, value)
1158 : snprintf_ptr(begin, capacity, format, value);
1159 if (result < 0) {
1160 buf.reserve(buf.capacity() + 1); // The buffer will grow exponentially.
1161 continue;
1162 }
1163 auto size = to_unsigned(result);
1164 // Size equal to capacity means that the last character was truncated.
1165 if (size >= capacity) {
1166 buf.reserve(size + offset + 1); // Add 1 for the terminating '\0'.
1167 continue;
1168 }
1169 auto is_digit = [](char c) { return c >= '0' && c <= '9'; };
1170 if (specs.format == float_format::fixed) {
1171 if (precision == 0) {
1172 buf.resize(size);
1173 return 0;
1174 }
1175 // Find and remove the decimal point.
1176 auto end = begin + size, p = end;
1177 do {
1178 --p;
1179 } while (is_digit(*p));
1180 int fraction_size = static_cast<int>(end - p - 1);
1181 std::memmove(p, p + 1, to_unsigned(fraction_size));
1182 buf.resize(size - 1);
1183 return -fraction_size;
1184 }
1185 if (specs.format == float_format::hex) {
1186 buf.resize(size + offset);
1187 return 0;
1188 }
1189 // Find and parse the exponent.
1190 auto end = begin + size, exp_pos = end;
1191 do {
1192 --exp_pos;
1193 } while (*exp_pos != 'e');
1194 char sign = exp_pos[1];
1195 assert(sign == '+' || sign == '-');
1196 int exp = 0;
1197 auto p = exp_pos + 2; // Skip 'e' and sign.
1198 do {
1199 assert(is_digit(*p));
1200 exp = exp * 10 + (*p++ - '0');
1201 } while (p != end);
1202 if (sign == '-') exp = -exp;
1203 int fraction_size = 0;
1204 if (exp_pos != begin + 1) {
1205 // Remove trailing zeros.
1206 auto fraction_end = exp_pos - 1;
1207 while (*fraction_end == '0') --fraction_end;
1208 // Move the fractional part left to get rid of the decimal point.
1209 fraction_size = static_cast<int>(fraction_end - begin - 1);
1210 std::memmove(begin + 1, begin + 2, to_unsigned(fraction_size));
1211 }
1212 buf.resize(to_unsigned(fraction_size) + offset + 1);
1213 return exp - fraction_size;
1214 }
1215}
1216
1217// A public domain branchless UTF-8 decoder by Christopher Wellons:
1218// https://github.com/skeeto/branchless-utf8
1219/* Decode the next character, c, from buf, reporting errors in e.
1220 *
1221 * Since this is a branchless decoder, four bytes will be read from the
1222 * buffer regardless of the actual length of the next character. This
1223 * means the buffer _must_ have at least three bytes of zero padding
1224 * following the end of the data stream.
1225 *
1226 * Errors are reported in e, which will be non-zero if the parsed
1227 * character was somehow invalid: invalid byte sequence, non-canonical
1228 * encoding, or a surrogate half.
1229 *
1230 * The function returns a pointer to the next character. When an error
1231 * occurs, this pointer will be a guess that depends on the particular
1232 * error, but it will always advance at least one byte.
1233 */
1234FMT_FUNC const char* utf8_decode(const char* buf, uint32_t* c, int* e) {
1235 static const char lengths[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1236 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
1237 0, 0, 2, 2, 2, 2, 3, 3, 4, 0};
1238 static const int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07};
1239 static const uint32_t mins[] = {4194304, 0, 128, 2048, 65536};
1240 static const int shiftc[] = {0, 18, 12, 6, 0};
1241 static const int shifte[] = {0, 6, 4, 2, 0};
1242
1243 auto s = reinterpret_cast<const unsigned char*>(buf);
1244 int len = lengths[s[0] >> 3];
1245
1246 // Compute the pointer to the next character early so that the next
1247 // iteration can start working on the next character. Neither Clang
1248 // nor GCC figure out this reordering on their own.
1249 const char* next = buf + len + !len;
1250
1251 // Assume a four-byte character and load four bytes. Unused bits are
1252 // shifted out.
1253 *c = uint32_t(s[0] & masks[len]) << 18;
1254 *c |= uint32_t(s[1] & 0x3f) << 12;
1255 *c |= uint32_t(s[2] & 0x3f) << 6;
1256 *c |= uint32_t(s[3] & 0x3f) << 0;
1257 *c >>= shiftc[len];
1258
1259 // Accumulate the various error conditions.
1260 *e = (*c < mins[len]) << 6; // non-canonical encoding
1261 *e |= ((*c >> 11) == 0x1b) << 7; // surrogate half?
1262 *e |= (*c > 0x10FFFF) << 8; // out of range?
1263 *e |= (s[1] & 0xc0) >> 2;
1264 *e |= (s[2] & 0xc0) >> 4;
1265 *e |= (s[3]) >> 6;
1266 *e ^= 0x2a; // top two bits of each tail byte correct?
1267 *e >>= shifte[len];
1268
1269 return next;
1270}
1271} // namespace internal
1272
1273template <> struct formatter<internal::bigint> {
1274 format_parse_context::iterator parse(format_parse_context& ctx) {
1275 return ctx.begin();
1276 }
1277
1278 format_context::iterator format(const internal::bigint& n,
1279 format_context& ctx) {
1280 auto out = ctx.out();
1281 bool first = true;
1282 for (auto i = n.bigits_.size(); i > 0; --i) {
1283 auto value = n.bigits_[i - 1u];
1284 if (first) {
1285 out = format_to(out, "{:x}", value);
1286 first = false;
1287 continue;
1288 }
1289 out = format_to(out, "{:08x}", value);
1290 }
1291 if (n.exp_ > 0)
1292 out = format_to(out, "p{}", n.exp_ * internal::bigint::bigit_bits);
1293 return out;
1294 }
1295};
1296
1297FMT_FUNC internal::utf8_to_utf16::utf8_to_utf16(string_view s) {
1298 auto transcode = [this](const char* p) {
1299 auto cp = uint32_t();
1300 auto error = 0;
1301 p = utf8_decode(p, &cp, &error);
1302 if (error != 0) FMT_THROW(std::runtime_error("invalid utf8"));
1303 if (cp <= 0xFFFF) {
1304 buffer_.push_back(static_cast<wchar_t>(cp));
1305 } else {
1306 cp -= 0x10000;
1307 buffer_.push_back(static_cast<wchar_t>(0xD800 + (cp >> 10)));
1308 buffer_.push_back(static_cast<wchar_t>(0xDC00 + (cp & 0x3FF)));
1309 }
1310 return p;
1311 };
1312 auto p = s.data();
1313 const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars.
1314 if (s.size() >= block_size) {
1315 for (auto end = p + s.size() - block_size + 1; p < end;) p = transcode(p);
1316 }
1317 if (auto num_chars_left = s.data() + s.size() - p) {
1318 char buf[2 * block_size - 1] = {};
1319 memcpy(buf, p, to_unsigned(num_chars_left));
1320 p = buf;
1321 do {
1322 p = transcode(p);
1323 } while (p - buf < num_chars_left);
1324 }
1325 buffer_.push_back(0);
1326}
1327
1328FMT_FUNC void format_system_error(internal::buffer<char>& out, int error_code,
1329 string_view message) FMT_NOEXCEPT {
1330 FMT_TRY {
1331 memory_buffer buf;
1332 buf.resize(inline_buffer_size);
1333 for (;;) {
1334 char* system_message = &buf[0];
1335 int result =
1336 internal::safe_strerror(error_code, system_message, buf.size());
1337 if (result == 0) {
1338 internal::writer w(out);
1339 w.write(message);
1340 w.write(": ");
1341 w.write(system_message);
1342 return;
1343 }
1344 if (result != ERANGE)
1345 break; // Can't get error message, report error code instead.
1346 buf.resize(buf.size() * 2);
1347 }
1348 }
1349 FMT_CATCH(...) {}
1350 format_error_code(out, error_code, message);
1351}
1352
1353FMT_FUNC void internal::error_handler::on_error(const char* message) {
1354 FMT_THROW(format_error(message));
1355}
1356
1357FMT_FUNC void report_system_error(int error_code,
1358 fmt::string_view message) FMT_NOEXCEPT {
1359 report_error(format_system_error, error_code, message);
1360}
1361
1362FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
1363 memory_buffer buffer;
1364 internal::vformat_to(buffer, format_str,
1365 basic_format_args<buffer_context<char>>(args));
1366#ifdef _WIN32
1367 auto fd = _fileno(f);
1368 if (_isatty(fd)) {
1369 internal::utf8_to_utf16 u16(string_view(buffer.data(), buffer.size()));
1370 auto written = DWORD();
1371 if (!WriteConsoleW(reinterpret_cast<HANDLE>(_get_osfhandle(fd)),
1372 u16.c_str(), static_cast<DWORD>(u16.size()), &written,
1373 nullptr)) {
1374 FMT_THROW(format_error("failed to write to console"));
1375 }
1376 return;
1377 }
1378#endif
1379 internal::fwrite_fully(buffer.data(), 1, buffer.size(), f);
1380}
1381
1382#ifdef _WIN32
1383// Print assuming legacy (non-Unicode) encoding.
1384FMT_FUNC void internal::vprint_mojibake(std::FILE* f, string_view format_str,
1385 format_args args) {
1386 memory_buffer buffer;
1387 internal::vformat_to(buffer, format_str,
1388 basic_format_args<buffer_context<char>>(args));
1389 fwrite_fully(buffer.data(), 1, buffer.size(), f);
1390}
1391#endif
1392
1393FMT_FUNC void vprint(string_view format_str, format_args args) {
1394 vprint(stdout, format_str, args);
1395}
1396
1397FMT_END_NAMESPACE
1398
1399#ifdef _MSC_VER
1400# pragma warning(pop)
1401#endif
1402
1403#endif // FMT_FORMAT_INL_H_
Definition: core.h:1480
Definition: core.h:551
FMT_CONSTEXPR iterator begin() const FMT_NOEXCEPT
Definition: core.h:568
Definition: format.h:631
Definition: core.h:351
FMT_CONSTEXPR size_t size() const
Definition: core.h:397
FMT_CONSTEXPR const Char * data() const
Definition: core.h:394
Definition: format.h:724
Definition: core.h:645
void resize(std::size_t new_size)
Definition: core.h:698
std::size_t capacity() const FMT_NOEXCEPT
Definition: core.h:687
std::size_t size() const FMT_NOEXCEPT
Definition: core.h:684
T * data() FMT_NOEXCEPT
Definition: core.h:690
Definition: format.h:2786
Definition: core.h:1579