|
|
|
@ -8,8 +8,6 @@
|
|
|
|
|
#ifndef FMT_FORMAT_INL_H_
|
|
|
|
|
#define FMT_FORMAT_INL_H_
|
|
|
|
|
|
|
|
|
|
#include "format.h"
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <cctype>
|
|
|
|
|
#include <climits>
|
|
|
|
@ -17,29 +15,15 @@
|
|
|
|
|
#include <cstdarg>
|
|
|
|
|
#include <cstring> // for std::memmove
|
|
|
|
|
#include <cwchar>
|
|
|
|
|
|
|
|
|
|
#include "format.h"
|
|
|
|
|
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
|
|
|
|
|
# include <locale>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if FMT_USE_WINDOWS_H
|
|
|
|
|
# if !defined(FMT_HEADER_ONLY) && !defined(WIN32_LEAN_AND_MEAN)
|
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
|
# endif
|
|
|
|
|
# if defined(NOMINMAX) || defined(FMT_WIN_MINMAX)
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
# include <io.h>
|
|
|
|
|
# include <windows.h>
|
|
|
|
|
# else
|
|
|
|
|
# define NOMINMAX
|
|
|
|
|
# include <windows.h>
|
|
|
|
|
# undef NOMINMAX
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if FMT_EXCEPTIONS
|
|
|
|
|
# define FMT_TRY try
|
|
|
|
|
# define FMT_CATCH(x) catch (x)
|
|
|
|
|
#else
|
|
|
|
|
# define FMT_TRY if (true)
|
|
|
|
|
# define FMT_CATCH(x) if (false)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
@ -73,8 +57,6 @@ inline int fmt_snprintf(char* buffer, size_t size, const char* format, ...) {
|
|
|
|
|
# define FMT_SNPRINTF fmt_snprintf
|
|
|
|
|
#endif // _MSC_VER
|
|
|
|
|
|
|
|
|
|
using format_func = void (*)(internal::buffer<char>&, int, string_view);
|
|
|
|
|
|
|
|
|
|
// A portable thread-safe version of strerror.
|
|
|
|
|
// Sets buffer to point to a string describing the error code.
|
|
|
|
|
// This can be either a pointer to a string stored in buffer,
|
|
|
|
@ -104,6 +86,7 @@ FMT_FUNC int safe_strerror(int error_code, char*& buffer,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle the result of GNU-specific version of strerror_r.
|
|
|
|
|
FMT_MAYBE_UNUSED
|
|
|
|
|
int handle(char* message) {
|
|
|
|
|
// If the buffer is full then the message is probably truncated.
|
|
|
|
|
if (message == buffer_ && strlen(buffer_) == buffer_size_ - 1)
|
|
|
|
@ -113,11 +96,13 @@ FMT_FUNC int safe_strerror(int error_code, char*& buffer,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle the case when strerror_r is not available.
|
|
|
|
|
FMT_MAYBE_UNUSED
|
|
|
|
|
int handle(internal::null<>) {
|
|
|
|
|
return fallback(strerror_s(buffer_, buffer_size_, error_code_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback to strerror_s when strerror_r is not available.
|
|
|
|
|
FMT_MAYBE_UNUSED
|
|
|
|
|
int fallback(int result) {
|
|
|
|
|
// If the buffer is full then the message is probably truncated.
|
|
|
|
|
return result == 0 && strlen(buffer_) == buffer_size_ - 1 ? ERANGE
|
|
|
|
@ -168,15 +153,6 @@ FMT_FUNC void format_error_code(internal::buffer<char>& out, int error_code,
|
|
|
|
|
assert(out.size() <= inline_buffer_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A wrapper around fwrite that throws on error.
|
|
|
|
|
FMT_FUNC void fwrite_fully(const void* ptr, size_t size, size_t count,
|
|
|
|
|
FILE* stream) {
|
|
|
|
|
size_t written = std::fwrite(ptr, size, count, stream);
|
|
|
|
|
if (written < count) {
|
|
|
|
|
FMT_THROW(system_error(errno, "cannot write to file"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC void report_error(format_func func, int error_code,
|
|
|
|
|
string_view message) FMT_NOEXCEPT {
|
|
|
|
|
memory_buffer full_message;
|
|
|
|
@ -185,6 +161,13 @@ FMT_FUNC void report_error(format_func func, int error_code,
|
|
|
|
|
(void)std::fwrite(full_message.data(), full_message.size(), 1, stderr);
|
|
|
|
|
std::fputc('\n', stderr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A wrapper around fwrite that throws on error.
|
|
|
|
|
FMT_FUNC void fwrite_fully(const void* ptr, size_t size, size_t count,
|
|
|
|
|
FILE* stream) {
|
|
|
|
|
size_t written = std::fwrite(ptr, size, count, stream);
|
|
|
|
|
if (written < count) FMT_THROW(system_error(errno, "cannot write to file"));
|
|
|
|
|
}
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
|
|
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
|
|
|
|
@ -356,6 +339,10 @@ class fp {
|
|
|
|
|
private:
|
|
|
|
|
using significand_type = uint64_t;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
significand_type f;
|
|
|
|
|
int e;
|
|
|
|
|
|
|
|
|
|
// All sizes are in bits.
|
|
|
|
|
// Subtract 1 to account for an implicit most significant bit in the
|
|
|
|
|
// normalized form.
|
|
|
|
@ -363,11 +350,6 @@ class fp {
|
|
|
|
|
std::numeric_limits<double>::digits - 1;
|
|
|
|
|
static FMT_CONSTEXPR_DECL const uint64_t implicit_bit =
|
|
|
|
|
1ULL << double_significand_size;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
significand_type f;
|
|
|
|
|
int e;
|
|
|
|
|
|
|
|
|
|
static FMT_CONSTEXPR_DECL const int significand_size =
|
|
|
|
|
bits<significand_type>::value;
|
|
|
|
|
|
|
|
|
@ -378,22 +360,6 @@ class fp {
|
|
|
|
|
// errors on platforms where double is not IEEE754.
|
|
|
|
|
template <typename Double> explicit fp(Double d) { assign(d); }
|
|
|
|
|
|
|
|
|
|
// Normalizes the value converted from double and multiplied by (1 << SHIFT).
|
|
|
|
|
template <int SHIFT> friend fp normalize(fp value) {
|
|
|
|
|
// Handle subnormals.
|
|
|
|
|
const auto shifted_implicit_bit = fp::implicit_bit << SHIFT;
|
|
|
|
|
while ((value.f & shifted_implicit_bit) == 0) {
|
|
|
|
|
value.f <<= 1;
|
|
|
|
|
--value.e;
|
|
|
|
|
}
|
|
|
|
|
// Subtract 1 to account for hidden bit.
|
|
|
|
|
const auto offset =
|
|
|
|
|
fp::significand_size - fp::double_significand_size - SHIFT - 1;
|
|
|
|
|
value.f <<= offset;
|
|
|
|
|
value.e -= offset;
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assigns d to this and return true iff predecessor is closer than successor.
|
|
|
|
|
template <typename Double, FMT_ENABLE_IF(sizeof(Double) == sizeof(uint64_t))>
|
|
|
|
|
bool assign(Double d) {
|
|
|
|
@ -406,7 +372,8 @@ class fp {
|
|
|
|
|
const int exponent_bias = (1 << exponent_size) - limits::max_exponent - 1;
|
|
|
|
|
auto u = bit_cast<uint64_t>(d);
|
|
|
|
|
f = u & significand_mask;
|
|
|
|
|
auto biased_e = (u & exponent_mask) >> double_significand_size;
|
|
|
|
|
int biased_e =
|
|
|
|
|
static_cast<int>((u & exponent_mask) >> double_significand_size);
|
|
|
|
|
// Predecessor is closer if d is a normalized power of 2 (f == 0) other than
|
|
|
|
|
// the smallest normalized number (biased_e > 1).
|
|
|
|
|
bool is_predecessor_closer = f == 0 && biased_e > 1;
|
|
|
|
@ -414,7 +381,7 @@ class fp {
|
|
|
|
|
f += implicit_bit;
|
|
|
|
|
else
|
|
|
|
|
biased_e = 1; // Subnormals use biased exponent 1 (min exponent).
|
|
|
|
|
e = static_cast<int>(biased_e - exponent_bias - double_significand_size);
|
|
|
|
|
e = biased_e - exponent_bias - double_significand_size;
|
|
|
|
|
return is_predecessor_closer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -453,6 +420,22 @@ class fp {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Normalizes the value converted from double and multiplied by (1 << SHIFT).
|
|
|
|
|
template <int SHIFT> fp normalize(fp value) {
|
|
|
|
|
// Handle subnormals.
|
|
|
|
|
const auto shifted_implicit_bit = fp::implicit_bit << SHIFT;
|
|
|
|
|
while ((value.f & shifted_implicit_bit) == 0) {
|
|
|
|
|
value.f <<= 1;
|
|
|
|
|
--value.e;
|
|
|
|
|
}
|
|
|
|
|
// Subtract 1 to account for hidden bit.
|
|
|
|
|
const auto offset =
|
|
|
|
|
fp::significand_size - fp::double_significand_size - SHIFT - 1;
|
|
|
|
|
value.f <<= offset;
|
|
|
|
|
value.e -= offset;
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool operator==(fp x, fp y) { return x.f == y.f && x.e == y.e; }
|
|
|
|
|
|
|
|
|
|
// Computes lhs * rhs / pow(2, 64) rounded to nearest with half-up tie breaking.
|
|
|
|
@ -477,14 +460,12 @@ inline fp operator*(fp x, fp y) { return {multiply(x.f, y.f), x.e + y.e + 64}; }
|
|
|
|
|
|
|
|
|
|
// Returns a cached power of 10 `c_k = c_k.f * pow(2, c_k.e)` such that its
|
|
|
|
|
// (binary) exponent satisfies `min_exponent <= c_k.e <= min_exponent + 28`.
|
|
|
|
|
FMT_FUNC fp get_cached_power(int min_exponent, int& pow10_exponent) {
|
|
|
|
|
const uint64_t one_over_log2_10 = 0x4d104d42; // round(pow(2, 32) / log2(10))
|
|
|
|
|
inline fp get_cached_power(int min_exponent, int& pow10_exponent) {
|
|
|
|
|
const int64_t one_over_log2_10 = 0x4d104d42; // round(pow(2, 32) / log2(10))
|
|
|
|
|
int index = static_cast<int>(
|
|
|
|
|
static_cast<int64_t>(
|
|
|
|
|
(min_exponent + fp::significand_size - 1) * one_over_log2_10 +
|
|
|
|
|
((uint64_t(1) << 32) - 1) // ceil
|
|
|
|
|
) >>
|
|
|
|
|
32 // arithmetic shift
|
|
|
|
|
((min_exponent + fp::significand_size - 1) * one_over_log2_10 +
|
|
|
|
|
((int64_t(1) << 32) - 1)) // ceil
|
|
|
|
|
>> 32 // arithmetic shift
|
|
|
|
|
);
|
|
|
|
|
// Decimal exponent of the first (smallest) cached power of 10.
|
|
|
|
|
const int first_dec_exp = -348;
|
|
|
|
@ -526,20 +507,23 @@ class bigint {
|
|
|
|
|
basic_memory_buffer<bigit, bigits_capacity> bigits_;
|
|
|
|
|
int exp_;
|
|
|
|
|
|
|
|
|
|
bigit operator[](int index) const { return bigits_[to_unsigned(index)]; }
|
|
|
|
|
bigit& operator[](int index) { return bigits_[to_unsigned(index)]; }
|
|
|
|
|
|
|
|
|
|
static FMT_CONSTEXPR_DECL const int bigit_bits = bits<bigit>::value;
|
|
|
|
|
|
|
|
|
|
friend struct formatter<bigint>;
|
|
|
|
|
|
|
|
|
|
void subtract_bigits(int index, bigit other, bigit& borrow) {
|
|
|
|
|
auto result = static_cast<double_bigit>(bigits_[index]) - other - borrow;
|
|
|
|
|
bigits_[index] = static_cast<bigit>(result);
|
|
|
|
|
auto result = static_cast<double_bigit>((*this)[index]) - other - borrow;
|
|
|
|
|
(*this)[index] = static_cast<bigit>(result);
|
|
|
|
|
borrow = static_cast<bigit>(result >> (bigit_bits * 2 - 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void remove_leading_zeros() {
|
|
|
|
|
int num_bigits = static_cast<int>(bigits_.size()) - 1;
|
|
|
|
|
while (num_bigits > 0 && bigits_[num_bigits] == 0) --num_bigits;
|
|
|
|
|
bigits_.resize(num_bigits + 1);
|
|
|
|
|
while (num_bigits > 0 && (*this)[num_bigits] == 0) --num_bigits;
|
|
|
|
|
bigits_.resize(to_unsigned(num_bigits + 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Computes *this -= other assuming aligned bigints and *this >= other.
|
|
|
|
@ -548,8 +532,7 @@ class bigint {
|
|
|
|
|
FMT_ASSERT(compare(*this, other) >= 0, "");
|
|
|
|
|
bigit borrow = 0;
|
|
|
|
|
int i = other.exp_ - exp_;
|
|
|
|
|
for (int j = 0, n = static_cast<int>(other.bigits_.size()); j != n;
|
|
|
|
|
++i, ++j) {
|
|
|
|
|
for (size_t j = 0, n = other.bigits_.size(); j != n; ++i, ++j) {
|
|
|
|
|
subtract_bigits(i, other.bigits_[j], borrow);
|
|
|
|
|
}
|
|
|
|
|
while (borrow > 0) subtract_bigits(i, 0, borrow);
|
|
|
|
@ -600,7 +583,7 @@ class bigint {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void assign(uint64_t n) {
|
|
|
|
|
int num_bigits = 0;
|
|
|
|
|
size_t num_bigits = 0;
|
|
|
|
|
do {
|
|
|
|
|
bigits_[num_bigits++] = n & ~bigit(0);
|
|
|
|
|
n >>= bigit_bits;
|
|
|
|
@ -641,7 +624,7 @@ class bigint {
|
|
|
|
|
int end = i - j;
|
|
|
|
|
if (end < 0) end = 0;
|
|
|
|
|
for (; i >= end; --i, --j) {
|
|
|
|
|
bigit lhs_bigit = lhs.bigits_[i], rhs_bigit = rhs.bigits_[j];
|
|
|
|
|
bigit lhs_bigit = lhs[i], rhs_bigit = rhs[j];
|
|
|
|
|
if (lhs_bigit != rhs_bigit) return lhs_bigit > rhs_bigit ? 1 : -1;
|
|
|
|
|
}
|
|
|
|
|
if (i != j) return i > j ? 1 : -1;
|
|
|
|
@ -656,7 +639,7 @@ class bigint {
|
|
|
|
|
if (max_lhs_bigits + 1 < num_rhs_bigits) return -1;
|
|
|
|
|
if (max_lhs_bigits > num_rhs_bigits) return 1;
|
|
|
|
|
auto get_bigit = [](const bigint& n, int i) -> bigit {
|
|
|
|
|
return i >= n.exp_ && i < n.num_bigits() ? n.bigits_[i - n.exp_] : 0;
|
|
|
|
|
return i >= n.exp_ && i < n.num_bigits() ? n[i - n.exp_] : 0;
|
|
|
|
|
};
|
|
|
|
|
double_bigit borrow = 0;
|
|
|
|
|
int min_exp = (std::min)((std::min)(lhs1.exp_, lhs2.exp_), rhs.exp_);
|
|
|
|
@ -696,7 +679,7 @@ class bigint {
|
|
|
|
|
basic_memory_buffer<bigit, bigits_capacity> n(std::move(bigits_));
|
|
|
|
|
int num_bigits = static_cast<int>(bigits_.size());
|
|
|
|
|
int num_result_bigits = 2 * num_bigits;
|
|
|
|
|
bigits_.resize(num_result_bigits);
|
|
|
|
|
bigits_.resize(to_unsigned(num_result_bigits));
|
|
|
|
|
using accumulator_t = conditional_t<FMT_USE_INT128, uint128_t, accumulator>;
|
|
|
|
|
auto sum = accumulator_t();
|
|
|
|
|
for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) {
|
|
|
|
@ -706,7 +689,7 @@ class bigint {
|
|
|
|
|
// Most terms are multiplied twice which can be optimized in the future.
|
|
|
|
|
sum += static_cast<double_bigit>(n[i]) * n[j];
|
|
|
|
|
}
|
|
|
|
|
bigits_[bigit_index] = static_cast<bigit>(sum);
|
|
|
|
|
(*this)[bigit_index] = static_cast<bigit>(sum);
|
|
|
|
|
sum >>= bits<bigit>::value; // Compute the carry.
|
|
|
|
|
}
|
|
|
|
|
// Do the same for the top half.
|
|
|
|
@ -714,7 +697,7 @@ class bigint {
|
|
|
|
|
++bigit_index) {
|
|
|
|
|
for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;)
|
|
|
|
|
sum += static_cast<double_bigit>(n[i++]) * n[j--];
|
|
|
|
|
bigits_[bigit_index] = static_cast<bigit>(sum);
|
|
|
|
|
(*this)[bigit_index] = static_cast<bigit>(sum);
|
|
|
|
|
sum >>= bits<bigit>::value;
|
|
|
|
|
}
|
|
|
|
|
--num_result_bigits;
|
|
|
|
@ -728,11 +711,11 @@ class bigint {
|
|
|
|
|
FMT_ASSERT(this != &divisor, "");
|
|
|
|
|
if (compare(*this, divisor) < 0) return 0;
|
|
|
|
|
int num_bigits = static_cast<int>(bigits_.size());
|
|
|
|
|
FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1] != 0, "");
|
|
|
|
|
FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1u] != 0, "");
|
|
|
|
|
int exp_difference = exp_ - divisor.exp_;
|
|
|
|
|
if (exp_difference > 0) {
|
|
|
|
|
// Align bigints by adding trailing zeros to simplify subtraction.
|
|
|
|
|
bigits_.resize(num_bigits + exp_difference);
|
|
|
|
|
bigits_.resize(to_unsigned(num_bigits + exp_difference));
|
|
|
|
|
for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j)
|
|
|
|
|
bigits_[j] = bigits_[i];
|
|
|
|
|
std::uninitialized_fill_n(bigits_.data(), exp_difference, 0);
|
|
|
|
@ -747,7 +730,7 @@ class bigint {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum round_direction { unknown, up, down };
|
|
|
|
|
enum class round_direction { unknown, up, down };
|
|
|
|
|
|
|
|
|
|
// Given the divisor (normally a power of 10), the remainder = v % divisor for
|
|
|
|
|
// some number v and the error, returns whether v should be rounded up, down, or
|
|
|
|
@ -760,13 +743,13 @@ inline round_direction get_round_direction(uint64_t divisor, uint64_t remainder,
|
|
|
|
|
FMT_ASSERT(error < divisor - error, ""); // error * 2 won't overflow.
|
|
|
|
|
// Round down if (remainder + error) * 2 <= divisor.
|
|
|
|
|
if (remainder <= divisor - remainder && error * 2 <= divisor - remainder * 2)
|
|
|
|
|
return down;
|
|
|
|
|
return round_direction::down;
|
|
|
|
|
// Round up if (remainder - error) * 2 >= divisor.
|
|
|
|
|
if (remainder >= error &&
|
|
|
|
|
remainder - error >= divisor - (remainder - error)) {
|
|
|
|
|
return up;
|
|
|
|
|
return round_direction::up;
|
|
|
|
|
}
|
|
|
|
|
return unknown;
|
|
|
|
|
return round_direction::unknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace digits {
|
|
|
|
@ -777,6 +760,20 @@ enum result {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A version of count_digits optimized for grisu_gen_digits.
|
|
|
|
|
inline int grisu_count_digits(uint32_t n) {
|
|
|
|
|
if (n < 10) return 1;
|
|
|
|
|
if (n < 100) return 2;
|
|
|
|
|
if (n < 1000) return 3;
|
|
|
|
|
if (n < 10000) return 4;
|
|
|
|
|
if (n < 100000) return 5;
|
|
|
|
|
if (n < 1000000) return 6;
|
|
|
|
|
if (n < 10000000) return 7;
|
|
|
|
|
if (n < 100000000) return 8;
|
|
|
|
|
if (n < 1000000000) return 9;
|
|
|
|
|
return 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generates output using the Grisu digit-gen algorithm.
|
|
|
|
|
// error: the size of the region (lower, upper) outside of which numbers
|
|
|
|
|
// definitely do not round to value (Delta in Grisu3).
|
|
|
|
@ -792,7 +789,7 @@ FMT_ALWAYS_INLINE digits::result grisu_gen_digits(fp value, uint64_t error,
|
|
|
|
|
FMT_ASSERT(integral == value.f >> -one.e, "");
|
|
|
|
|
// The fractional part of scaled value (p2 in Grisu) c = value % one.
|
|
|
|
|
uint64_t fractional = value.f & (one.f - 1);
|
|
|
|
|
exp = count_digits(integral); // kappa in Grisu.
|
|
|
|
|
exp = grisu_count_digits(integral); // kappa in Grisu.
|
|
|
|
|
// Divide by 10 to prevent overflow.
|
|
|
|
|
auto result = handler.on_start(data::powers_of_10_64[exp - 1] << -one.e,
|
|
|
|
|
value.f / 10, error * 10, exp);
|
|
|
|
@ -882,8 +879,8 @@ struct fixed_handler {
|
|
|
|
|
if (precision > 0) return digits::more;
|
|
|
|
|
if (precision < 0) return digits::done;
|
|
|
|
|
auto dir = get_round_direction(divisor, remainder, error);
|
|
|
|
|
if (dir == unknown) return digits::error;
|
|
|
|
|
buf[size++] = dir == up ? '1' : '0';
|
|
|
|
|
if (dir == round_direction::unknown) return digits::error;
|
|
|
|
|
buf[size++] = dir == round_direction::up ? '1' : '0';
|
|
|
|
|
return digits::done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -901,7 +898,8 @@ struct fixed_handler {
|
|
|
|
|
FMT_ASSERT(error == 1 && divisor > 2, "");
|
|
|
|
|
}
|
|
|
|
|
auto dir = get_round_direction(divisor, remainder, error);
|
|
|
|
|
if (dir != up) return dir == down ? digits::done : digits::error;
|
|
|
|
|
if (dir != round_direction::up)
|
|
|
|
|
return dir == round_direction::down ? digits::done : digits::error;
|
|
|
|
|
++buf[size - 1];
|
|
|
|
|
for (int i = size - 1; i > 0 && buf[i] > '9'; --i) {
|
|
|
|
|
buf[i] = '0';
|
|
|
|
@ -1028,7 +1026,7 @@ void fallback_format(Double d, buffer<char>& buf, int& exp10) {
|
|
|
|
|
if (result > 0 || (result == 0 && (digit % 2) != 0))
|
|
|
|
|
++data[num_digits - 1];
|
|
|
|
|
}
|
|
|
|
|
buf.resize(num_digits);
|
|
|
|
|
buf.resize(to_unsigned(num_digits));
|
|
|
|
|
exp10 -= num_digits - 1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -1043,7 +1041,7 @@ void fallback_format(Double d, buffer<char>& buf, int& exp10) {
|
|
|
|
|
// if T is a IEEE754 binary32 or binary64 and snprintf otherwise.
|
|
|
|
|
template <typename T>
|
|
|
|
|
int format_float(T value, int precision, float_specs specs, buffer<char>& buf) {
|
|
|
|
|
static_assert(!std::is_same<T, float>(), "");
|
|
|
|
|
static_assert(!std::is_same<T, float>::value, "");
|
|
|
|
|
FMT_ASSERT(value >= 0, "value is negative");
|
|
|
|
|
|
|
|
|
|
const bool fixed = specs.format == float_format::fixed;
|
|
|
|
@ -1062,25 +1060,7 @@ int format_float(T value, int precision, float_specs specs, buffer<char>& buf) {
|
|
|
|
|
int exp = 0;
|
|
|
|
|
const int min_exp = -60; // alpha in Grisu.
|
|
|
|
|
int cached_exp10 = 0; // K in Grisu.
|
|
|
|
|
if (precision != -1) {
|
|
|
|
|
if (precision > 17) return snprintf_float(value, precision, specs, buf);
|
|
|
|
|
fp normalized = normalize(fp(value));
|
|
|
|
|
const auto cached_pow = get_cached_power(
|
|
|
|
|
min_exp - (normalized.e + fp::significand_size), cached_exp10);
|
|
|
|
|
normalized = normalized * cached_pow;
|
|
|
|
|
fixed_handler handler{buf.data(), 0, precision, -cached_exp10, fixed};
|
|
|
|
|
if (grisu_gen_digits(normalized, 1, exp, handler) == digits::error)
|
|
|
|
|
return snprintf_float(value, precision, specs, buf);
|
|
|
|
|
int num_digits = handler.size;
|
|
|
|
|
if (!fixed) {
|
|
|
|
|
// Remove trailing zeros.
|
|
|
|
|
while (num_digits > 0 && buf[num_digits - 1] == '0') {
|
|
|
|
|
--num_digits;
|
|
|
|
|
++exp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
buf.resize(to_unsigned(num_digits));
|
|
|
|
|
} else {
|
|
|
|
|
if (precision < 0) {
|
|
|
|
|
fp fp_value;
|
|
|
|
|
auto boundaries = specs.binary32
|
|
|
|
|
? fp_value.assign_float_with_boundaries(value)
|
|
|
|
@ -1109,6 +1089,24 @@ int format_float(T value, int precision, float_specs specs, buffer<char>& buf) {
|
|
|
|
|
return exp;
|
|
|
|
|
}
|
|
|
|
|
buf.resize(to_unsigned(handler.size));
|
|
|
|
|
} else {
|
|
|
|
|
if (precision > 17) return snprintf_float(value, precision, specs, buf);
|
|
|
|
|
fp normalized = normalize(fp(value));
|
|
|
|
|
const auto cached_pow = get_cached_power(
|
|
|
|
|
min_exp - (normalized.e + fp::significand_size), cached_exp10);
|
|
|
|
|
normalized = normalized * cached_pow;
|
|
|
|
|
fixed_handler handler{buf.data(), 0, precision, -cached_exp10, fixed};
|
|
|
|
|
if (grisu_gen_digits(normalized, 1, exp, handler) == digits::error)
|
|
|
|
|
return snprintf_float(value, precision, specs, buf);
|
|
|
|
|
int num_digits = handler.size;
|
|
|
|
|
if (!fixed) {
|
|
|
|
|
// Remove trailing zeros.
|
|
|
|
|
while (num_digits > 0 && buf[num_digits - 1] == '0') {
|
|
|
|
|
--num_digits;
|
|
|
|
|
++exp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
buf.resize(to_unsigned(num_digits));
|
|
|
|
|
}
|
|
|
|
|
return exp - cached_exp10;
|
|
|
|
|
}
|
|
|
|
@ -1118,7 +1116,7 @@ int snprintf_float(T value, int precision, float_specs specs,
|
|
|
|
|
buffer<char>& buf) {
|
|
|
|
|
// Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail.
|
|
|
|
|
FMT_ASSERT(buf.capacity() > buf.size(), "empty buffer");
|
|
|
|
|
static_assert(!std::is_same<T, float>(), "");
|
|
|
|
|
static_assert(!std::is_same<T, float>::value, "");
|
|
|
|
|
|
|
|
|
|
// Subtract 1 to account for the difference in precision since we use %e for
|
|
|
|
|
// both general and exponent format.
|
|
|
|
@ -1131,7 +1129,7 @@ int snprintf_float(T value, int precision, float_specs specs,
|
|
|
|
|
char format[max_format_size];
|
|
|
|
|
char* format_ptr = format;
|
|
|
|
|
*format_ptr++ = '%';
|
|
|
|
|
if (specs.trailing_zeros) *format_ptr++ = '#';
|
|
|
|
|
if (specs.showpoint && specs.format == float_format::hex) *format_ptr++ = '#';
|
|
|
|
|
if (precision >= 0) {
|
|
|
|
|
*format_ptr++ = '.';
|
|
|
|
|
*format_ptr++ = '*';
|
|
|
|
@ -1153,7 +1151,8 @@ int snprintf_float(T value, int precision, float_specs specs,
|
|
|
|
|
"fuzz mode - avoid large allocation inside snprintf");
|
|
|
|
|
#endif
|
|
|
|
|
// Suppress the warning about a nonliteral format string.
|
|
|
|
|
auto snprintf_ptr = FMT_SNPRINTF;
|
|
|
|
|
// Cannot use auto becase of a bug in MinGW (#1532).
|
|
|
|
|
int (*snprintf_ptr)(char*, size_t, const char*, ...) = FMT_SNPRINTF;
|
|
|
|
|
int result = precision >= 0
|
|
|
|
|
? snprintf_ptr(begin, capacity, format, precision, value)
|
|
|
|
|
: snprintf_ptr(begin, capacity, format, value);
|
|
|
|
@ -1161,7 +1160,7 @@ int snprintf_float(T value, int precision, float_specs specs,
|
|
|
|
|
buf.reserve(buf.capacity() + 1); // The buffer will grow exponentially.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
unsigned size = to_unsigned(result);
|
|
|
|
|
auto size = to_unsigned(result);
|
|
|
|
|
// Size equal to capacity means that the last character was truncated.
|
|
|
|
|
if (size >= capacity) {
|
|
|
|
|
buf.reserve(size + offset + 1); // Add 1 for the terminating '\0'.
|
|
|
|
@ -1179,7 +1178,7 @@ int snprintf_float(T value, int precision, float_specs specs,
|
|
|
|
|
--p;
|
|
|
|
|
} while (is_digit(*p));
|
|
|
|
|
int fraction_size = static_cast<int>(end - p - 1);
|
|
|
|
|
std::memmove(p, p + 1, fraction_size);
|
|
|
|
|
std::memmove(p, p + 1, to_unsigned(fraction_size));
|
|
|
|
|
buf.resize(size - 1);
|
|
|
|
|
return -fraction_size;
|
|
|
|
|
}
|
|
|
|
@ -1208,12 +1207,67 @@ int snprintf_float(T value, int precision, float_specs specs,
|
|
|
|
|
while (*fraction_end == '0') --fraction_end;
|
|
|
|
|
// Move the fractional part left to get rid of the decimal point.
|
|
|
|
|
fraction_size = static_cast<int>(fraction_end - begin - 1);
|
|
|
|
|
std::memmove(begin + 1, begin + 2, fraction_size);
|
|
|
|
|
std::memmove(begin + 1, begin + 2, to_unsigned(fraction_size));
|
|
|
|
|
}
|
|
|
|
|
buf.resize(fraction_size + offset + 1);
|
|
|
|
|
buf.resize(to_unsigned(fraction_size) + offset + 1);
|
|
|
|
|
return exp - fraction_size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A public domain branchless UTF-8 decoder by Christopher Wellons:
|
|
|
|
|
// https://github.com/skeeto/branchless-utf8
|
|
|
|
|
/* Decode the next character, c, from buf, reporting errors in e.
|
|
|
|
|
*
|
|
|
|
|
* Since this is a branchless decoder, four bytes will be read from the
|
|
|
|
|
* buffer regardless of the actual length of the next character. This
|
|
|
|
|
* means the buffer _must_ have at least three bytes of zero padding
|
|
|
|
|
* following the end of the data stream.
|
|
|
|
|
*
|
|
|
|
|
* Errors are reported in e, which will be non-zero if the parsed
|
|
|
|
|
* character was somehow invalid: invalid byte sequence, non-canonical
|
|
|
|
|
* encoding, or a surrogate half.
|
|
|
|
|
*
|
|
|
|
|
* The function returns a pointer to the next character. When an error
|
|
|
|
|
* occurs, this pointer will be a guess that depends on the particular
|
|
|
|
|
* error, but it will always advance at least one byte.
|
|
|
|
|
*/
|
|
|
|
|
FMT_FUNC const char* utf8_decode(const char* buf, uint32_t* c, int* e) {
|
|
|
|
|
static const char lengths[] = {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, 0};
|
|
|
|
|
static const int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07};
|
|
|
|
|
static const uint32_t mins[] = {4194304, 0, 128, 2048, 65536};
|
|
|
|
|
static const int shiftc[] = {0, 18, 12, 6, 0};
|
|
|
|
|
static const int shifte[] = {0, 6, 4, 2, 0};
|
|
|
|
|
|
|
|
|
|
auto s = reinterpret_cast<const unsigned char*>(buf);
|
|
|
|
|
int len = lengths[s[0] >> 3];
|
|
|
|
|
|
|
|
|
|
// Compute the pointer to the next character early so that the next
|
|
|
|
|
// iteration can start working on the next character. Neither Clang
|
|
|
|
|
// nor GCC figure out this reordering on their own.
|
|
|
|
|
const char* next = buf + len + !len;
|
|
|
|
|
|
|
|
|
|
// Assume a four-byte character and load four bytes. Unused bits are
|
|
|
|
|
// shifted out.
|
|
|
|
|
*c = uint32_t(s[0] & masks[len]) << 18;
|
|
|
|
|
*c |= uint32_t(s[1] & 0x3f) << 12;
|
|
|
|
|
*c |= uint32_t(s[2] & 0x3f) << 6;
|
|
|
|
|
*c |= uint32_t(s[3] & 0x3f) << 0;
|
|
|
|
|
*c >>= shiftc[len];
|
|
|
|
|
|
|
|
|
|
// Accumulate the various error conditions.
|
|
|
|
|
*e = (*c < mins[len]) << 6; // non-canonical encoding
|
|
|
|
|
*e |= ((*c >> 11) == 0x1b) << 7; // surrogate half?
|
|
|
|
|
*e |= (*c > 0x10FFFF) << 8; // out of range?
|
|
|
|
|
*e |= (s[1] & 0xc0) >> 2;
|
|
|
|
|
*e |= (s[2] & 0xc0) >> 4;
|
|
|
|
|
*e |= (s[3]) >> 6;
|
|
|
|
|
*e ^= 0x2a; // top two bits of each tail byte correct?
|
|
|
|
|
*e >>= shifte[len];
|
|
|
|
|
|
|
|
|
|
return next;
|
|
|
|
|
}
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
|
|
template <> struct formatter<internal::bigint> {
|
|
|
|
@ -1226,7 +1280,7 @@ template <> struct formatter<internal::bigint> {
|
|
|
|
|
auto out = ctx.out();
|
|
|
|
|
bool first = true;
|
|
|
|
|
for (auto i = n.bigits_.size(); i > 0; --i) {
|
|
|
|
|
auto value = n.bigits_[i - 1];
|
|
|
|
|
auto value = n.bigits_[i - 1u];
|
|
|
|
|
if (first) {
|
|
|
|
|
out = format_to(out, "{:x}", value);
|
|
|
|
|
first = false;
|
|
|
|
@ -1240,101 +1294,37 @@ template <> struct formatter<internal::bigint> {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#if FMT_USE_WINDOWS_H
|
|
|
|
|
|
|
|
|
|
FMT_FUNC internal::utf8_to_utf16::utf8_to_utf16(string_view s) {
|
|
|
|
|
static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16";
|
|
|
|
|
if (s.size() > INT_MAX)
|
|
|
|
|
FMT_THROW(windows_error(ERROR_INVALID_PARAMETER, ERROR_MSG));
|
|
|
|
|
int s_size = static_cast<int>(s.size());
|
|
|
|
|
if (s_size == 0) {
|
|
|
|
|
// MultiByteToWideChar does not support zero length, handle separately.
|
|
|
|
|
buffer_.resize(1);
|
|
|
|
|
buffer_[0] = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int length = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s.data(),
|
|
|
|
|
s_size, nullptr, 0);
|
|
|
|
|
if (length == 0) FMT_THROW(windows_error(GetLastError(), ERROR_MSG));
|
|
|
|
|
buffer_.resize(length + 1);
|
|
|
|
|
length = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), s_size,
|
|
|
|
|
&buffer_[0], length);
|
|
|
|
|
if (length == 0) FMT_THROW(windows_error(GetLastError(), ERROR_MSG));
|
|
|
|
|
buffer_[length] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC internal::utf16_to_utf8::utf16_to_utf8(wstring_view s) {
|
|
|
|
|
if (int error_code = convert(s)) {
|
|
|
|
|
FMT_THROW(windows_error(error_code,
|
|
|
|
|
"cannot convert string from UTF-16 to UTF-8"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC int internal::utf16_to_utf8::convert(wstring_view s) {
|
|
|
|
|
if (s.size() > INT_MAX) return ERROR_INVALID_PARAMETER;
|
|
|
|
|
int s_size = static_cast<int>(s.size());
|
|
|
|
|
if (s_size == 0) {
|
|
|
|
|
// WideCharToMultiByte does not support zero length, handle separately.
|
|
|
|
|
buffer_.resize(1);
|
|
|
|
|
buffer_[0] = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, nullptr, 0,
|
|
|
|
|
nullptr, nullptr);
|
|
|
|
|
if (length == 0) return GetLastError();
|
|
|
|
|
buffer_.resize(length + 1);
|
|
|
|
|
length = WideCharToMultiByte(CP_UTF8, 0, s.data(), s_size, &buffer_[0],
|
|
|
|
|
length, nullptr, nullptr);
|
|
|
|
|
if (length == 0) return GetLastError();
|
|
|
|
|
buffer_[length] = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC void windows_error::init(int err_code, string_view format_str,
|
|
|
|
|
format_args args) {
|
|
|
|
|
error_code_ = err_code;
|
|
|
|
|
memory_buffer buffer;
|
|
|
|
|
internal::format_windows_error(buffer, err_code, vformat(format_str, args));
|
|
|
|
|
std::runtime_error& base = *this;
|
|
|
|
|
base = std::runtime_error(to_string(buffer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC void internal::format_windows_error(internal::buffer<char>& out,
|
|
|
|
|
int error_code,
|
|
|
|
|
string_view message) FMT_NOEXCEPT {
|
|
|
|
|
FMT_TRY {
|
|
|
|
|
wmemory_buffer buf;
|
|
|
|
|
buf.resize(inline_buffer_size);
|
|
|
|
|
for (;;) {
|
|
|
|
|
wchar_t* system_message = &buf[0];
|
|
|
|
|
int result = FormatMessageW(
|
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
|
|
|
|
|
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), system_message,
|
|
|
|
|
static_cast<uint32_t>(buf.size()), nullptr);
|
|
|
|
|
if (result != 0) {
|
|
|
|
|
utf16_to_utf8 utf8_message;
|
|
|
|
|
if (utf8_message.convert(system_message) == ERROR_SUCCESS) {
|
|
|
|
|
internal::writer w(out);
|
|
|
|
|
w.write(message);
|
|
|
|
|
w.write(": ");
|
|
|
|
|
w.write(utf8_message);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
|
break; // Can't get error message, report error code instead.
|
|
|
|
|
buf.resize(buf.size() * 2);
|
|
|
|
|
auto transcode = [this](const char* p) {
|
|
|
|
|
auto cp = uint32_t();
|
|
|
|
|
auto error = 0;
|
|
|
|
|
p = utf8_decode(p, &cp, &error);
|
|
|
|
|
if (error != 0) FMT_THROW(std::runtime_error("invalid utf8"));
|
|
|
|
|
if (cp <= 0xFFFF) {
|
|
|
|
|
buffer_.push_back(static_cast<wchar_t>(cp));
|
|
|
|
|
} else {
|
|
|
|
|
cp -= 0x10000;
|
|
|
|
|
buffer_.push_back(static_cast<wchar_t>(0xD800 + (cp >> 10)));
|
|
|
|
|
buffer_.push_back(static_cast<wchar_t>(0xDC00 + (cp & 0x3FF)));
|
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
};
|
|
|
|
|
auto p = s.data();
|
|
|
|
|
const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars.
|
|
|
|
|
if (s.size() >= block_size) {
|
|
|
|
|
for (auto end = p + s.size() - block_size + 1; p < end;) p = transcode(p);
|
|
|
|
|
}
|
|
|
|
|
if (auto num_chars_left = s.data() + s.size() - p) {
|
|
|
|
|
char buf[2 * block_size - 1] = {};
|
|
|
|
|
memcpy(buf, p, to_unsigned(num_chars_left));
|
|
|
|
|
p = buf;
|
|
|
|
|
do {
|
|
|
|
|
p = transcode(p);
|
|
|
|
|
} while (p - buf < num_chars_left);
|
|
|
|
|
}
|
|
|
|
|
FMT_CATCH(...) {}
|
|
|
|
|
format_error_code(out, error_code, message);
|
|
|
|
|
buffer_.push_back(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // FMT_USE_WINDOWS_H
|
|
|
|
|
|
|
|
|
|
FMT_FUNC void format_system_error(internal::buffer<char>& out, int error_code,
|
|
|
|
|
string_view message) FMT_NOEXCEPT {
|
|
|
|
|
FMT_TRY {
|
|
|
|
@ -1369,19 +1359,36 @@ FMT_FUNC void report_system_error(int error_code,
|
|
|
|
|
report_error(format_system_error, error_code, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if FMT_USE_WINDOWS_H
|
|
|
|
|
FMT_FUNC void report_windows_error(int error_code,
|
|
|
|
|
fmt::string_view message) FMT_NOEXCEPT {
|
|
|
|
|
report_error(internal::format_windows_error, error_code, message);
|
|
|
|
|
FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
|
|
|
|
|
memory_buffer buffer;
|
|
|
|
|
internal::vformat_to(buffer, format_str,
|
|
|
|
|
basic_format_args<buffer_context<char>>(args));
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
auto fd = _fileno(f);
|
|
|
|
|
if (_isatty(fd)) {
|
|
|
|
|
internal::utf8_to_utf16 u16(string_view(buffer.data(), buffer.size()));
|
|
|
|
|
auto written = DWORD();
|
|
|
|
|
if (!WriteConsoleW(reinterpret_cast<HANDLE>(_get_osfhandle(fd)),
|
|
|
|
|
u16.c_str(), static_cast<DWORD>(u16.size()), &written,
|
|
|
|
|
nullptr)) {
|
|
|
|
|
FMT_THROW(format_error("failed to write to console"));
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
internal::fwrite_fully(buffer.data(), 1, buffer.size(), f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
// Print assuming legacy (non-Unicode) encoding.
|
|
|
|
|
FMT_FUNC void internal::vprint_mojibake(std::FILE* f, string_view format_str,
|
|
|
|
|
format_args args) {
|
|
|
|
|
memory_buffer buffer;
|
|
|
|
|
internal::vformat_to(buffer, format_str,
|
|
|
|
|
basic_format_args<buffer_context<char>>(args));
|
|
|
|
|
internal::fwrite_fully(buffer.data(), 1, buffer.size(), f);
|
|
|
|
|
fwrite_fully(buffer.data(), 1, buffer.size(), f);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
FMT_FUNC void vprint(string_view format_str, format_args args) {
|
|
|
|
|
vprint(stdout, format_str, args);
|
|
|
|
|