Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12a1ca5846 |
No files matched your search
@@ -168,8 +168,7 @@ library; pick one or type it to see the row):
|
||||
- Integer numbers are serialized as is; that is, no scientific notation is used.
|
||||
- Floating-point numbers are serialized with the fewest digits that read back as the same value (the closest such
|
||||
digits if there are several), in the layout of the `#!c %g` printf modifier: `#!c 1.5`, `#!c 100.0`, `#!c 1e+100`.
|
||||
Doubles are converted with the algorithm of [Żmij](https://github.com/vitaut/zmij), floats with Grisu2, which
|
||||
can write more digits than necessary.
|
||||
Doubles and floats are converted with the algorithm of [Żmij](https://github.com/vitaut/zmij).
|
||||
|
||||
!!! hint "Notes regarding precision of floating-point numbers"
|
||||
|
||||
|
||||
@@ -575,10 +575,9 @@ therefore silently changes parse results rather than raising an error. See
|
||||
specifiers, for which the library likewise provides only `#!cpp double` and `#!cpp long double` overloads
|
||||
(`#!cpp float` is promoted to `#!cpp double`).
|
||||
|
||||
If `#!cpp std::numeric_limits<NumberFloatType>` describes an IEEE 754 binary64 number, `dump` uses the algorithm of
|
||||
Żmij, which produces the shortest representation that round-trips. For IEEE 754 binary32 numbers, it uses Grisu2,
|
||||
which produces a short representation that round-trips. Otherwise the `snprintf` fallback with `max_digits10` digits is
|
||||
used.
|
||||
If `#!cpp std::numeric_limits<NumberFloatType>` describes an IEEE 754 binary64 or binary32 number, `dump` uses the
|
||||
algorithm of Żmij, which produces the shortest representation that round-trips. Otherwise the `snprintf` fallback with
|
||||
`max_digits10` digits is used.
|
||||
|
||||
### Required for the binary formats
|
||||
|
||||
@@ -591,7 +590,7 @@ binary32 or binary64 field and have no encoding for `#!cpp long double`.
|
||||
| Type | Support |
|
||||
|--------------------------|-----------------------------------------------------------------------------------------------------------------------|
|
||||
| `#!cpp double` (default) | full; shortest round-trip output through Żmij |
|
||||
| `#!cpp float` | full; short round-trip output through Grisu2 |
|
||||
| `#!cpp float` | full; shortest round-trip output through Żmij |
|
||||
| `#!cpp long double` | `dump` and `parse` only; the binary format writers do not compile, as they only handle IEEE 754 binary32 and binary64 |
|
||||
| any other type | not usable |
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ The class contains the UTF-8 Decoder from Bjoern Hoehrmann which is licensed und
|
||||
|
||||
The class contains a slightly modified version of the Grisu2 algorithm from Florian Loitsch which is licensed under the [MIT License](https://opensource.org/licenses/MIT) (see above). Copyright © 2009 [Florian Loitsch](https://florian.loitsch.com/)
|
||||
|
||||
The class contains a port of the shortest double-to-decimal conversion of [Żmij](https://github.com/vitaut/zmij) by Victor Zverovich, including the conversion of the digits to text by Xiang JunBo and the SIMD instruction sequence of Dougall Johnson, which is licensed under the [MIT License](https://opensource.org/licenses/MIT) (see above). Copyright © 2025 [Victor Zverovich](https://github.com/vitaut)
|
||||
The class contains a port of the shortest double- and float-to-decimal conversion of [Żmij](https://github.com/vitaut/zmij) by Victor Zverovich, including the conversion of the digits to text by Xiang JunBo and the SIMD instruction sequence of Dougall Johnson, which is licensed under the [MIT License](https://opensource.org/licenses/MIT) (see above). Copyright © 2025 [Victor Zverovich](https://github.com/vitaut)
|
||||
|
||||
The class contains a copy of [Hedley](https://nemequ.github.io/hedley/) from Evan Nemerson which is licensed as [CC0-1.0](https://creativecommons.org/publicdomain/zero/1.0/).
|
||||
|
||||
|
||||
@@ -49,8 +49,8 @@ namespace detail
|
||||
|
||||
/*!
|
||||
@brief implements binary to decimal floating-point conversion: the shortest
|
||||
digits of Zmij (see zmij.hpp) for IEEE 754 binary64 numbers, and the Grisu2
|
||||
algorithm for all other formats.
|
||||
digits of Zmij (see zmij.hpp) for IEEE 754 binary64 and binary32 numbers, and
|
||||
the Grisu2 algorithm for all other formats.
|
||||
|
||||
The Grisu2 implementation is a slightly modified version of the reference
|
||||
implementation which may be obtained from
|
||||
@@ -1397,8 +1397,9 @@ inline char* write_short_decimal(char* first, std::uint64_t digits, int exp) noe
|
||||
@brief whether FloatType is an IEEE 754 binary64 type (a double, or a long double
|
||||
that has the same format, as with MSVC and on Apple's Arm CPUs)
|
||||
|
||||
These are the types the conversion of Zmij (see zmij.hpp) is used for; all
|
||||
others (binary32, or a format the library does not know) use Grisu2.
|
||||
These are the types the conversion of Zmij (see zmij.hpp) is used for with
|
||||
write_shortest(); binary32 numbers use it with format_buffer(), and all
|
||||
others (e.g. the 80-bit long double of x87) use Grisu2.
|
||||
*/
|
||||
template<typename FloatType>
|
||||
constexpr bool has_binary64_format() noexcept
|
||||
@@ -1412,7 +1413,23 @@ constexpr bool has_binary64_format() noexcept
|
||||
template<typename FloatType>
|
||||
struct is_binary64 : std::integral_constant<bool, has_binary64_format<FloatType>()> {};
|
||||
|
||||
/// a positive finite float (other than binary64): Grisu2 and format_buffer()
|
||||
/// whether FloatType is an IEEE 754 binary32 type (a float); these use the
|
||||
/// conversion of Zmij as well
|
||||
template<typename FloatType>
|
||||
constexpr bool has_binary32_format() noexcept
|
||||
{
|
||||
return std::numeric_limits<FloatType>::is_iec559
|
||||
&& std::numeric_limits<FloatType>::digits == 24
|
||||
&& std::numeric_limits<FloatType>::max_exponent == 128
|
||||
&& sizeof(FloatType) == sizeof(std::uint32_t);
|
||||
}
|
||||
|
||||
/// the conversion for a floating-point type: 0 for Grisu2, 1 for Zmij on
|
||||
/// binary64, 2 for Zmij on binary32
|
||||
template<typename FloatType>
|
||||
struct float_conversion : std::integral_constant < int, (has_binary64_format<FloatType>() ? 1 : 0) + (has_binary32_format<FloatType>() ? 2 : 0) > {};
|
||||
|
||||
/// a positive finite float (other than binary64 and binary32): Grisu2 and format_buffer()
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
@@ -1466,32 +1483,79 @@ char* write_positive_zmij(char* first, const char* last, FloatType value)
|
||||
return first + len;
|
||||
}
|
||||
|
||||
/// a positive finite binary32 number: the shortest digits (Zmij), laid out by
|
||||
/// format_buffer() as the digits of Grisu2 were (only the digits differ: Grisu2
|
||||
/// leaves out a decimal on the boundary of the rounding interval and may take
|
||||
/// the other one of two equally close decimals)
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
char* write_positive_zmij_binary32(char* first, const char* last, FloatType value)
|
||||
{
|
||||
static_assert(has_binary32_format<FloatType>(),
|
||||
"internal error: this conversion of Zmij needs IEEE 754 binary32 numbers");
|
||||
const zmij::shortest_decimal d = zmij::to_shortest_of<zmij::binary32_format>(bit_cast<std::uint32_t>(value));
|
||||
std::uint64_t digits = d.has_digit ? (d.integral * 10) + d.digit : d.integral;
|
||||
int decimal_exponent = d.has_digit ? d.exponent : d.exponent + 1;
|
||||
while (digits % 10 == 0)
|
||||
{
|
||||
digits /= 10;
|
||||
++decimal_exponent;
|
||||
}
|
||||
// at most 9 digits (max_digits10), written from the end
|
||||
std::array<char, 9> buf; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init): written before read
|
||||
std::size_t start = buf.size();
|
||||
do
|
||||
{
|
||||
buf[--start] = static_cast<char>('0' + (digits % 10));
|
||||
digits /= 10;
|
||||
}
|
||||
while (digits != 0);
|
||||
const auto len = static_cast<int>(buf.size() - start);
|
||||
|
||||
JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
|
||||
JSON_ASSERT(last - first >= 2 + 3 + std::numeric_limits<FloatType>::max_digits10);
|
||||
static_cast<void>(last); // (only used in the assertions)
|
||||
std::memcpy(first, buf.data() + start, static_cast<std::size_t>(len));
|
||||
// as write_positive_grisu2()
|
||||
return format_buffer(first, len, decimal_exponent, -4, std::numeric_limits<FloatType>::digits10);
|
||||
}
|
||||
|
||||
/// a positive finite float of a format without Zmij: Grisu2
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
char* write_positive(char* first, const char* last, FloatType value, std::integral_constant<int, 0> /*grisu2*/)
|
||||
{
|
||||
return write_positive_grisu2(first, last, value);
|
||||
}
|
||||
|
||||
/// a positive finite binary64 number: Zmij (as a long double has the format of
|
||||
/// a double here, its bits are those of the double of the same value)
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
char* write_positive(char* first, const char* last, FloatType value, std::true_type /*is_binary64*/)
|
||||
char* write_positive(char* first, const char* last, FloatType value, std::integral_constant<int, 1> /*binary64*/)
|
||||
{
|
||||
return write_positive_zmij(first, last, value);
|
||||
}
|
||||
|
||||
/// a positive finite float of any other format: Grisu2
|
||||
/// a positive finite binary32 number: Zmij
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
char* write_positive(char* first, const char* last, FloatType value, std::false_type /*is_binary64*/)
|
||||
char* write_positive(char* first, const char* last, FloatType value, std::integral_constant<int, 2> /*binary32*/)
|
||||
{
|
||||
return write_positive_grisu2(first, last, value);
|
||||
return write_positive_zmij_binary32(first, last, value);
|
||||
}
|
||||
|
||||
/// a positive finite float: Zmij for binary64 numbers, Grisu2 otherwise
|
||||
/// a positive finite float: Zmij for binary64 and binary32 numbers, Grisu2 otherwise
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
char* write_positive(char* first, const char* last, FloatType value)
|
||||
{
|
||||
return write_positive(first, last, value, is_binary64<FloatType> {});
|
||||
return write_positive(first, last, value, float_conversion<FloatType> {});
|
||||
}
|
||||
|
||||
} // namespace dtoa_impl
|
||||
|
||||
@@ -160,22 +160,37 @@ struct shortest_decimal
|
||||
bool has_digit; ///< whether the shortest decimal is integral * 10 + digit
|
||||
};
|
||||
|
||||
/// The shortest decimal in the rounding interval of a positive finite double
|
||||
/// given by its bits, the closest one if there are several (to_decimal of
|
||||
/// Zmij, which keeps the last digit apart: the 15 or 16 digits before it can be
|
||||
/// converted without a multiplication by 10 first). Always inlined: GCC
|
||||
/// otherwise calls it, and its result goes through memory.
|
||||
JSON_HEDLEY_ALWAYS_INLINE shortest_decimal to_shortest(std::uint64_t bits) noexcept
|
||||
/// the bit layout of an IEEE 754 binary format: SigBits significand bits
|
||||
/// (without the implicit bit) below ExpBits exponent bits
|
||||
template<int SigBits, int ExpBits>
|
||||
struct binary_format
|
||||
{
|
||||
static constexpr unsigned sig_bits = SigBits;
|
||||
static constexpr unsigned exp_mask = (1u << static_cast<unsigned>(ExpBits)) - 1u;
|
||||
static constexpr int exp_offset = (1 << (ExpBits - 1)) - 1 + SigBits; // the bias plus the significand bits
|
||||
};
|
||||
using binary64_format = binary_format<52, 11>;
|
||||
using binary32_format = binary_format<23, 8>;
|
||||
|
||||
/// The shortest decimal in the rounding interval of a positive finite number of
|
||||
/// the format Format given by its bits, the closest one if there are several
|
||||
/// (to_decimal of Zmij, which keeps the last digit apart: the 15 or 16 digits
|
||||
/// of a normal double before it, 6 or 7 of a float, can be converted without a
|
||||
/// multiplication by 10 first). As in Zmij, the same computation serves both
|
||||
/// formats; only the bit layout differs. Always inlined: GCC otherwise calls
|
||||
/// it, and its result goes through memory.
|
||||
template<typename Format>
|
||||
JSON_HEDLEY_ALWAYS_INLINE shortest_decimal to_shortest_of(std::uint64_t bits) noexcept
|
||||
{
|
||||
constexpr int extra_shift = 9;
|
||||
const auto raw_exp = static_cast<int>((bits >> 52u) & 0x7FFu);
|
||||
std::uint64_t bin_sig = bits & ((std::uint64_t{1} << 52u) - 1);
|
||||
const auto raw_exp = static_cast<int>((bits >> Format::sig_bits) & Format::exp_mask);
|
||||
std::uint64_t bin_sig = bits & ((std::uint64_t{1} << Format::sig_bits) - 1);
|
||||
// a power of two has a narrower interval below (except the smallest normal)
|
||||
const bool regular = bin_sig != 0 || raw_exp <= 1;
|
||||
const int bin_exp = (raw_exp == 0 ? 1 : raw_exp) - 1075;
|
||||
const int bin_exp = (raw_exp == 0 ? 1 : raw_exp) - Format::exp_offset;
|
||||
if (raw_exp != 0)
|
||||
{
|
||||
bin_sig |= std::uint64_t{1} << 52u;
|
||||
bin_sig |= std::uint64_t{1} << Format::sig_bits;
|
||||
}
|
||||
// floor(log10(2^bin_exp)), or floor(log10(3/4 * 2^bin_exp)) for the irregular case
|
||||
const int dec_exp = ((bin_exp * 315653) - (regular ? 0 : 131072)) >> 20;
|
||||
@@ -217,6 +232,12 @@ JSON_HEDLEY_ALWAYS_INLINE shortest_decimal to_shortest(std::uint64_t bits) noexc
|
||||
return shortest_decimal{integral, dec_exp, static_cast<unsigned char>(digit), !round_up && !round_down};
|
||||
}
|
||||
|
||||
/// to_shortest_of() for a double given by its bits
|
||||
JSON_HEDLEY_ALWAYS_INLINE shortest_decimal to_shortest(std::uint64_t bits) noexcept
|
||||
{
|
||||
return to_shortest_of<binary64_format>(bits);
|
||||
}
|
||||
|
||||
} // namespace zmij
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -223,157 +223,6 @@ inline std::size_t validate_one_utf8(const unsigned char* data, std::size_t avai
|
||||
return 0; // invalid, incomplete, or must be diagnosed by the byte path
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the transition table of the UTF-8 DFA of utf8_dfa_run() and utf8_dfa_prefix()
|
||||
|
||||
They accept exactly what validate_one_utf8() accepts. This is a shift-based
|
||||
DFA (after Per Vognsen; see also https://orlp.net/blog/utf8-dfa/): a state is
|
||||
6 times its number, and table[b] holds, for every state s, the next state on
|
||||
byte b at bit offset s. One step is a load and a shift without a branch on the
|
||||
byte. On CJK text, it made the lexer and the serializer without SSE2/NEON 30%
|
||||
faster and the BON8 reader 15% faster than a loop over validate_one_utf8()
|
||||
(Apple M, Apple Clang).
|
||||
|
||||
The states are 0 (error, absorbing), 6 (between sequences), 12/18/24 (1/2/3
|
||||
continuation bytes of 0x80-0xBF missing), and the states after the lead bytes
|
||||
that restrict the second byte: 30 (0xE0: 0xA0-0xBF), 36 (0xED: 0x80-0x9F), 42
|
||||
(0xF0: 0x90-0xBF), and 48 (0xF4: 0x80-0x8F). The test "UTF-8 DFA" in
|
||||
unit-class_lexer.cpp checks every entry of the table.
|
||||
*/
|
||||
inline const std::uint64_t* utf8_dfa_table() noexcept
|
||||
{
|
||||
static const std::array<std::uint64_t, 256> table =
|
||||
{
|
||||
{
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000780ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000900ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000A80ull, 0x0000000000000600ull, 0x0000000000000600ull, 0x0000000000000600ull,
|
||||
0x0000000000000C00ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
}
|
||||
};
|
||||
return table.data();
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief validate a run of non-ASCII bytes as UTF-8
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()).
|
||||
@return the end of the run (@a e or a byte below 0x80) if it consists of
|
||||
complete well-formed UTF-8 sequences, otherwise nullptr
|
||||
|
||||
It stops at the first ill-formed byte: the serializer's error handlers scan
|
||||
again after every ill-formed byte, and scanning on to the end of the run would
|
||||
make them quadratic. Without the position of the error, it is faster than
|
||||
utf8_dfa_prefix(), which made the lexer 15% slower on CJK text.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_run(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
if (JSON_HEDLEY_UNLIKELY((state & 63u) == 0))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return (state & 63u) == 6 ? p : nullptr;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the end of the longest well-formed UTF-8 prefix of a run of non-ASCII bytes
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()) and
|
||||
returns the end of the longest prefix that consists of complete well-formed
|
||||
UTF-8 sequences. The whole run was valid if the result is @a e or points to a
|
||||
byte below 0x80; otherwise it points to the lead byte of the first ill-formed or
|
||||
incomplete sequence. Like utf8_dfa_run(), it stops at the first ill-formed byte.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_prefix(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
const unsigned char* last = p; // the end of the last complete sequence
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
const std::uint64_t current = state & 63u;
|
||||
if (JSON_HEDLEY_UNLIKELY(current == 0))
|
||||
{
|
||||
break;
|
||||
}
|
||||
last = (current == 6) ? p : last;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
// Return the length of the longest prefix of [data, data+n) that consists of
|
||||
// ASCII characters and complete well-formed UTF-8 sequences; n if all of it is
|
||||
// valid UTF-8. Unlike scalar_string_bulk_run(), quotes, escapes, and control
|
||||
@@ -401,12 +250,12 @@ inline std::size_t valid_utf8_prefix(const unsigned char* data, std::size_t n) n
|
||||
continue;
|
||||
}
|
||||
|
||||
const unsigned char* const last = utf8_dfa_prefix(data + pos, data + n);
|
||||
pos = static_cast<std::size_t>(last - data);
|
||||
if (pos != n && *last >= 0x80u)
|
||||
const std::size_t seq = validate_one_utf8(data + pos, n - pos);
|
||||
if (seq == 0)
|
||||
{
|
||||
break; // ill-formed or truncated
|
||||
}
|
||||
pos += seq;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
@@ -591,14 +440,18 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t scalar_string_bulk_run(const unsigned char
|
||||
}
|
||||
#endif
|
||||
#if !JSON_SCAN_VECTOR_UTF8 || JSON_SCAN_SSSE3_DISPATCH
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated as a whole
|
||||
const unsigned char* const run_end = utf8_dfa_run(p, e);
|
||||
if (JSON_HEDLEY_UNLIKELY(run_end == nullptr))
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated sequence
|
||||
// by sequence without searching for the next special byte in between
|
||||
do
|
||||
{
|
||||
// ill-formed or truncated: let the byte path diagnose it
|
||||
return static_cast<std::size_t>(utf8_dfa_prefix(p, e) - data);
|
||||
const std::size_t seq = validate_one_utf8(p, static_cast<std::size_t>(e - p));
|
||||
if (seq == 0)
|
||||
{
|
||||
return static_cast<std::size_t>(p - data); // ill-formed or truncated: let the byte path diagnose it
|
||||
}
|
||||
p += seq;
|
||||
}
|
||||
p = run_end;
|
||||
while (p != e && *p >= 0x80u);
|
||||
#endif
|
||||
}
|
||||
return static_cast<std::size_t>(p - data);
|
||||
@@ -703,7 +556,7 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t lexer_string_bulk_run(const unsigned char*
|
||||
// unaffected by this fast path.
|
||||
inline int hex_codepoint(const unsigned char* data) noexcept
|
||||
{
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table =
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
{
|
||||
{
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 00..0F
|
||||
|
||||
@@ -206,10 +206,9 @@ places, which differ in speed, diagnostics, and how they read the input:
|
||||
the bytes as is and leave the check to dump() and the binary writers.
|
||||
- the per-lead-byte switch in lexer::scan_string(): JSON text, with a
|
||||
diagnostic for each kind of error.
|
||||
- validate_one_utf8(), utf8_dfa_prefix() and valid_utf8_prefix() in
|
||||
string_scan.hpp: the lexer's bulk string scan, the bulk path of the BON8
|
||||
reader, and the BON8 writer. They must accept exactly what the lexer's
|
||||
switch accepts.
|
||||
- validate_one_utf8() and valid_utf8_prefix() in string_scan.hpp: the lexer's
|
||||
bulk string scan, the bulk path of the BON8 reader, and the BON8 writer.
|
||||
They must accept exactly what the lexer's switch accepts.
|
||||
- the byte path of binary_reader::get_bon8_string(): BON8 input without bulk
|
||||
access, and the bytes the bulk path leaves to it. It keeps its own ranges:
|
||||
taking them from utf8_shape() made BON8 decoding 2-3% slower (#5891).
|
||||
|
||||
Generated
+123
-186
@@ -7388,10 +7388,9 @@ places, which differ in speed, diagnostics, and how they read the input:
|
||||
the bytes as is and leave the check to dump() and the binary writers.
|
||||
- the per-lead-byte switch in lexer::scan_string(): JSON text, with a
|
||||
diagnostic for each kind of error.
|
||||
- validate_one_utf8(), utf8_dfa_prefix() and valid_utf8_prefix() in
|
||||
string_scan.hpp: the lexer's bulk string scan, the bulk path of the BON8
|
||||
reader, and the BON8 writer. They must accept exactly what the lexer's
|
||||
switch accepts.
|
||||
- validate_one_utf8() and valid_utf8_prefix() in string_scan.hpp: the lexer's
|
||||
bulk string scan, the bulk path of the BON8 reader, and the BON8 writer.
|
||||
They must accept exactly what the lexer's switch accepts.
|
||||
- the byte path of binary_reader::get_bon8_string(): BON8 input without bulk
|
||||
access, and the bytes the bulk path leaves to it. It keeps its own ranges:
|
||||
taking them from utf8_shape() made BON8 decoding 2-3% slower (#5891).
|
||||
@@ -13172,157 +13171,6 @@ inline std::size_t validate_one_utf8(const unsigned char* data, std::size_t avai
|
||||
return 0; // invalid, incomplete, or must be diagnosed by the byte path
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the transition table of the UTF-8 DFA of utf8_dfa_run() and utf8_dfa_prefix()
|
||||
|
||||
They accept exactly what validate_one_utf8() accepts. This is a shift-based
|
||||
DFA (after Per Vognsen; see also https://orlp.net/blog/utf8-dfa/): a state is
|
||||
6 times its number, and table[b] holds, for every state s, the next state on
|
||||
byte b at bit offset s. One step is a load and a shift without a branch on the
|
||||
byte. On CJK text, it made the lexer and the serializer without SSE2/NEON 30%
|
||||
faster and the BON8 reader 15% faster than a loop over validate_one_utf8()
|
||||
(Apple M, Apple Clang).
|
||||
|
||||
The states are 0 (error, absorbing), 6 (between sequences), 12/18/24 (1/2/3
|
||||
continuation bytes of 0x80-0xBF missing), and the states after the lead bytes
|
||||
that restrict the second byte: 30 (0xE0: 0xA0-0xBF), 36 (0xED: 0x80-0x9F), 42
|
||||
(0xF0: 0x90-0xBF), and 48 (0xF4: 0x80-0x8F). The test "UTF-8 DFA" in
|
||||
unit-class_lexer.cpp checks every entry of the table.
|
||||
*/
|
||||
inline const std::uint64_t* utf8_dfa_table() noexcept
|
||||
{
|
||||
static const std::array<std::uint64_t, 256> table =
|
||||
{
|
||||
{
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull, 0x0000000000000180ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull, 0x001200C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull, 0x000048C012306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull, 0x0000480312306000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull, 0x0000000000000300ull,
|
||||
0x0000000000000780ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000480ull, 0x0000000000000900ull, 0x0000000000000480ull, 0x0000000000000480ull,
|
||||
0x0000000000000A80ull, 0x0000000000000600ull, 0x0000000000000600ull, 0x0000000000000600ull,
|
||||
0x0000000000000C00ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull, 0x0000000000000000ull,
|
||||
}
|
||||
};
|
||||
return table.data();
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief validate a run of non-ASCII bytes as UTF-8
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()).
|
||||
@return the end of the run (@a e or a byte below 0x80) if it consists of
|
||||
complete well-formed UTF-8 sequences, otherwise nullptr
|
||||
|
||||
It stops at the first ill-formed byte: the serializer's error handlers scan
|
||||
again after every ill-formed byte, and scanning on to the end of the run would
|
||||
make them quadratic. Without the position of the error, it is faster than
|
||||
utf8_dfa_prefix(), which made the lexer 15% slower on CJK text.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_run(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
if (JSON_HEDLEY_UNLIKELY((state & 63u) == 0))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return (state & 63u) == 6 ? p : nullptr;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief the end of the longest well-formed UTF-8 prefix of a run of non-ASCII bytes
|
||||
|
||||
Validates [p, e) up to the first byte below 0x80 (see utf8_dfa_table()) and
|
||||
returns the end of the longest prefix that consists of complete well-formed
|
||||
UTF-8 sequences. The whole run was valid if the result is @a e or points to a
|
||||
byte below 0x80; otherwise it points to the lead byte of the first ill-formed or
|
||||
incomplete sequence. Like utf8_dfa_run(), it stops at the first ill-formed byte.
|
||||
*/
|
||||
JSON_HEDLEY_NEVER_INLINE inline const unsigned char* utf8_dfa_prefix(const unsigned char* p, const unsigned char* e) noexcept
|
||||
{
|
||||
const std::uint64_t* const table = utf8_dfa_table();
|
||||
std::uint64_t state = 6;
|
||||
const unsigned char* last = p; // the end of the last complete sequence
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
// a shift uses only the low 6 bits of its count on x86-64 and ARM64, so
|
||||
// the mask is free there
|
||||
state = table[*p] >> (state & 63u);
|
||||
++p;
|
||||
const std::uint64_t current = state & 63u;
|
||||
if (JSON_HEDLEY_UNLIKELY(current == 0))
|
||||
{
|
||||
break;
|
||||
}
|
||||
last = (current == 6) ? p : last;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
// Return the length of the longest prefix of [data, data+n) that consists of
|
||||
// ASCII characters and complete well-formed UTF-8 sequences; n if all of it is
|
||||
// valid UTF-8. Unlike scalar_string_bulk_run(), quotes, escapes, and control
|
||||
@@ -13350,12 +13198,12 @@ inline std::size_t valid_utf8_prefix(const unsigned char* data, std::size_t n) n
|
||||
continue;
|
||||
}
|
||||
|
||||
const unsigned char* const last = utf8_dfa_prefix(data + pos, data + n);
|
||||
pos = static_cast<std::size_t>(last - data);
|
||||
if (pos != n && *last >= 0x80u)
|
||||
const std::size_t seq = validate_one_utf8(data + pos, n - pos);
|
||||
if (seq == 0)
|
||||
{
|
||||
break; // ill-formed or truncated
|
||||
}
|
||||
pos += seq;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
@@ -13540,14 +13388,18 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t scalar_string_bulk_run(const unsigned char
|
||||
}
|
||||
#endif
|
||||
#if !JSON_SCAN_VECTOR_UTF8 || JSON_SCAN_SSSE3_DISPATCH
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated as a whole
|
||||
const unsigned char* const run_end = utf8_dfa_run(p, e);
|
||||
if (JSON_HEDLEY_UNLIKELY(run_end == nullptr))
|
||||
// a run of multi-byte sequences (e.g. CJK text) is validated sequence
|
||||
// by sequence without searching for the next special byte in between
|
||||
do
|
||||
{
|
||||
// ill-formed or truncated: let the byte path diagnose it
|
||||
return static_cast<std::size_t>(utf8_dfa_prefix(p, e) - data);
|
||||
const std::size_t seq = validate_one_utf8(p, static_cast<std::size_t>(e - p));
|
||||
if (seq == 0)
|
||||
{
|
||||
return static_cast<std::size_t>(p - data); // ill-formed or truncated: let the byte path diagnose it
|
||||
}
|
||||
p += seq;
|
||||
}
|
||||
p = run_end;
|
||||
while (p != e && *p >= 0x80u);
|
||||
#endif
|
||||
}
|
||||
return static_cast<std::size_t>(p - data);
|
||||
@@ -13652,7 +13504,7 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t lexer_string_bulk_run(const unsigned char*
|
||||
// unaffected by this fast path.
|
||||
inline int hex_codepoint(const unsigned char* data) noexcept
|
||||
{
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table =
|
||||
static const std::array<std::uint8_t, 256> hex_digit_table = // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
{
|
||||
{
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 00..0F
|
||||
@@ -36189,22 +36041,37 @@ struct shortest_decimal
|
||||
bool has_digit; ///< whether the shortest decimal is integral * 10 + digit
|
||||
};
|
||||
|
||||
/// The shortest decimal in the rounding interval of a positive finite double
|
||||
/// given by its bits, the closest one if there are several (to_decimal of
|
||||
/// Zmij, which keeps the last digit apart: the 15 or 16 digits before it can be
|
||||
/// converted without a multiplication by 10 first). Always inlined: GCC
|
||||
/// otherwise calls it, and its result goes through memory.
|
||||
JSON_HEDLEY_ALWAYS_INLINE shortest_decimal to_shortest(std::uint64_t bits) noexcept
|
||||
/// the bit layout of an IEEE 754 binary format: SigBits significand bits
|
||||
/// (without the implicit bit) below ExpBits exponent bits
|
||||
template<int SigBits, int ExpBits>
|
||||
struct binary_format
|
||||
{
|
||||
static constexpr unsigned sig_bits = SigBits;
|
||||
static constexpr unsigned exp_mask = (1u << static_cast<unsigned>(ExpBits)) - 1u;
|
||||
static constexpr int exp_offset = (1 << (ExpBits - 1)) - 1 + SigBits; // the bias plus the significand bits
|
||||
};
|
||||
using binary64_format = binary_format<52, 11>;
|
||||
using binary32_format = binary_format<23, 8>;
|
||||
|
||||
/// The shortest decimal in the rounding interval of a positive finite number of
|
||||
/// the format Format given by its bits, the closest one if there are several
|
||||
/// (to_decimal of Zmij, which keeps the last digit apart: the 15 or 16 digits
|
||||
/// of a normal double before it, 6 or 7 of a float, can be converted without a
|
||||
/// multiplication by 10 first). As in Zmij, the same computation serves both
|
||||
/// formats; only the bit layout differs. Always inlined: GCC otherwise calls
|
||||
/// it, and its result goes through memory.
|
||||
template<typename Format>
|
||||
JSON_HEDLEY_ALWAYS_INLINE shortest_decimal to_shortest_of(std::uint64_t bits) noexcept
|
||||
{
|
||||
constexpr int extra_shift = 9;
|
||||
const auto raw_exp = static_cast<int>((bits >> 52u) & 0x7FFu);
|
||||
std::uint64_t bin_sig = bits & ((std::uint64_t{1} << 52u) - 1);
|
||||
const auto raw_exp = static_cast<int>((bits >> Format::sig_bits) & Format::exp_mask);
|
||||
std::uint64_t bin_sig = bits & ((std::uint64_t{1} << Format::sig_bits) - 1);
|
||||
// a power of two has a narrower interval below (except the smallest normal)
|
||||
const bool regular = bin_sig != 0 || raw_exp <= 1;
|
||||
const int bin_exp = (raw_exp == 0 ? 1 : raw_exp) - 1075;
|
||||
const int bin_exp = (raw_exp == 0 ? 1 : raw_exp) - Format::exp_offset;
|
||||
if (raw_exp != 0)
|
||||
{
|
||||
bin_sig |= std::uint64_t{1} << 52u;
|
||||
bin_sig |= std::uint64_t{1} << Format::sig_bits;
|
||||
}
|
||||
// floor(log10(2^bin_exp)), or floor(log10(3/4 * 2^bin_exp)) for the irregular case
|
||||
const int dec_exp = ((bin_exp * 315653) - (regular ? 0 : 131072)) >> 20;
|
||||
@@ -36246,6 +36113,12 @@ JSON_HEDLEY_ALWAYS_INLINE shortest_decimal to_shortest(std::uint64_t bits) noexc
|
||||
return shortest_decimal{integral, dec_exp, static_cast<unsigned char>(digit), !round_up && !round_down};
|
||||
}
|
||||
|
||||
/// to_shortest_of() for a double given by its bits
|
||||
JSON_HEDLEY_ALWAYS_INLINE shortest_decimal to_shortest(std::uint64_t bits) noexcept
|
||||
{
|
||||
return to_shortest_of<binary64_format>(bits);
|
||||
}
|
||||
|
||||
} // namespace zmij
|
||||
} // namespace detail
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
@@ -36259,8 +36132,8 @@ namespace detail
|
||||
|
||||
/*!
|
||||
@brief implements binary to decimal floating-point conversion: the shortest
|
||||
digits of Zmij (see zmij.hpp) for IEEE 754 binary64 numbers, and the Grisu2
|
||||
algorithm for all other formats.
|
||||
digits of Zmij (see zmij.hpp) for IEEE 754 binary64 and binary32 numbers, and
|
||||
the Grisu2 algorithm for all other formats.
|
||||
|
||||
The Grisu2 implementation is a slightly modified version of the reference
|
||||
implementation which may be obtained from
|
||||
@@ -37607,8 +37480,9 @@ inline char* write_short_decimal(char* first, std::uint64_t digits, int exp) noe
|
||||
@brief whether FloatType is an IEEE 754 binary64 type (a double, or a long double
|
||||
that has the same format, as with MSVC and on Apple's Arm CPUs)
|
||||
|
||||
These are the types the conversion of Zmij (see zmij.hpp) is used for; all
|
||||
others (binary32, or a format the library does not know) use Grisu2.
|
||||
These are the types the conversion of Zmij (see zmij.hpp) is used for with
|
||||
write_shortest(); binary32 numbers use it with format_buffer(), and all
|
||||
others (e.g. the 80-bit long double of x87) use Grisu2.
|
||||
*/
|
||||
template<typename FloatType>
|
||||
constexpr bool has_binary64_format() noexcept
|
||||
@@ -37622,7 +37496,23 @@ constexpr bool has_binary64_format() noexcept
|
||||
template<typename FloatType>
|
||||
struct is_binary64 : std::integral_constant<bool, has_binary64_format<FloatType>()> {};
|
||||
|
||||
/// a positive finite float (other than binary64): Grisu2 and format_buffer()
|
||||
/// whether FloatType is an IEEE 754 binary32 type (a float); these use the
|
||||
/// conversion of Zmij as well
|
||||
template<typename FloatType>
|
||||
constexpr bool has_binary32_format() noexcept
|
||||
{
|
||||
return std::numeric_limits<FloatType>::is_iec559
|
||||
&& std::numeric_limits<FloatType>::digits == 24
|
||||
&& std::numeric_limits<FloatType>::max_exponent == 128
|
||||
&& sizeof(FloatType) == sizeof(std::uint32_t);
|
||||
}
|
||||
|
||||
/// the conversion for a floating-point type: 0 for Grisu2, 1 for Zmij on
|
||||
/// binary64, 2 for Zmij on binary32
|
||||
template<typename FloatType>
|
||||
struct float_conversion : std::integral_constant < int, (has_binary64_format<FloatType>() ? 1 : 0) + (has_binary32_format<FloatType>() ? 2 : 0) > {};
|
||||
|
||||
/// a positive finite float (other than binary64 and binary32): Grisu2 and format_buffer()
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
@@ -37676,32 +37566,79 @@ char* write_positive_zmij(char* first, const char* last, FloatType value)
|
||||
return first + len;
|
||||
}
|
||||
|
||||
/// a positive finite binary32 number: the shortest digits (Zmij), laid out by
|
||||
/// format_buffer() as the digits of Grisu2 were (only the digits differ: Grisu2
|
||||
/// leaves out a decimal on the boundary of the rounding interval and may take
|
||||
/// the other one of two equally close decimals)
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
char* write_positive_zmij_binary32(char* first, const char* last, FloatType value)
|
||||
{
|
||||
static_assert(has_binary32_format<FloatType>(),
|
||||
"internal error: this conversion of Zmij needs IEEE 754 binary32 numbers");
|
||||
const zmij::shortest_decimal d = zmij::to_shortest_of<zmij::binary32_format>(bit_cast<std::uint32_t>(value));
|
||||
std::uint64_t digits = d.has_digit ? (d.integral * 10) + d.digit : d.integral;
|
||||
int decimal_exponent = d.has_digit ? d.exponent : d.exponent + 1;
|
||||
while (digits % 10 == 0)
|
||||
{
|
||||
digits /= 10;
|
||||
++decimal_exponent;
|
||||
}
|
||||
// at most 9 digits (max_digits10), written from the end
|
||||
std::array<char, 9> buf; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init): written before read
|
||||
std::size_t start = buf.size();
|
||||
do
|
||||
{
|
||||
buf[--start] = static_cast<char>('0' + (digits % 10));
|
||||
digits /= 10;
|
||||
}
|
||||
while (digits != 0);
|
||||
const auto len = static_cast<int>(buf.size() - start);
|
||||
|
||||
JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
|
||||
JSON_ASSERT(last - first >= 2 + 3 + std::numeric_limits<FloatType>::max_digits10);
|
||||
static_cast<void>(last); // (only used in the assertions)
|
||||
std::memcpy(first, buf.data() + start, static_cast<std::size_t>(len));
|
||||
// as write_positive_grisu2()
|
||||
return format_buffer(first, len, decimal_exponent, -4, std::numeric_limits<FloatType>::digits10);
|
||||
}
|
||||
|
||||
/// a positive finite float of a format without Zmij: Grisu2
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
char* write_positive(char* first, const char* last, FloatType value, std::integral_constant<int, 0> /*grisu2*/)
|
||||
{
|
||||
return write_positive_grisu2(first, last, value);
|
||||
}
|
||||
|
||||
/// a positive finite binary64 number: Zmij (as a long double has the format of
|
||||
/// a double here, its bits are those of the double of the same value)
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
char* write_positive(char* first, const char* last, FloatType value, std::true_type /*is_binary64*/)
|
||||
char* write_positive(char* first, const char* last, FloatType value, std::integral_constant<int, 1> /*binary64*/)
|
||||
{
|
||||
return write_positive_zmij(first, last, value);
|
||||
}
|
||||
|
||||
/// a positive finite float of any other format: Grisu2
|
||||
/// a positive finite binary32 number: Zmij
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
char* write_positive(char* first, const char* last, FloatType value, std::false_type /*is_binary64*/)
|
||||
char* write_positive(char* first, const char* last, FloatType value, std::integral_constant<int, 2> /*binary32*/)
|
||||
{
|
||||
return write_positive_grisu2(first, last, value);
|
||||
return write_positive_zmij_binary32(first, last, value);
|
||||
}
|
||||
|
||||
/// a positive finite float: Zmij for binary64 numbers, Grisu2 otherwise
|
||||
/// a positive finite float: Zmij for binary64 and binary32 numbers, Grisu2 otherwise
|
||||
template<typename FloatType>
|
||||
JSON_HEDLEY_NON_NULL(1, 2)
|
||||
JSON_HEDLEY_RETURNS_NON_NULL
|
||||
char* write_positive(char* first, const char* last, FloatType value)
|
||||
{
|
||||
return write_positive(first, last, value, is_binary64<FloatType> {});
|
||||
return write_positive(first, last, value, float_conversion<FloatType> {});
|
||||
}
|
||||
|
||||
} // namespace dtoa_impl
|
||||
|
||||
@@ -2138,14 +2138,6 @@ TEST_CASE("UTF-8 validators accept the same sequences")
|
||||
CAPTURE(t)
|
||||
CHECK((dfa == expected && bulk == (expected ? length : 0) && bulk_short == 0));
|
||||
|
||||
// the DFA of the bulk paths: the whole sequence or nothing
|
||||
if (length >= 2)
|
||||
{
|
||||
const unsigned char* const run_end = nlohmann::detail::utf8_dfa_prefix(bytes.data(), bytes.data() + length);
|
||||
const unsigned char* const short_end = nlohmann::detail::utf8_dfa_prefix(bytes.data(), bytes.data() + length - 1);
|
||||
CHECK((run_end == bytes.data() + (expected ? length : 0) && short_end == bytes.data()));
|
||||
}
|
||||
|
||||
if (t < 2 && length >= 2)
|
||||
{
|
||||
// the lexer's byte path
|
||||
@@ -2173,70 +2165,6 @@ TEST_CASE("UTF-8 validators accept the same sequences")
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("UTF-8 DFA")
|
||||
{
|
||||
// utf8_dfa_prefix() and utf8_dfa_run() against a loop over
|
||||
// validate_one_utf8(): every byte
|
||||
// after a prefix that leads to each state of the DFA checks every entry of
|
||||
// its table, and the bytes after it whether it stops at the right place
|
||||
const auto reference = [](const unsigned char* p, const unsigned char* e)
|
||||
{
|
||||
while (p != e && *p >= 0x80u)
|
||||
{
|
||||
const std::size_t length = nlohmann::detail::validate_one_utf8(p, static_cast<std::size_t>(e - p));
|
||||
if (length == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
p += length;
|
||||
}
|
||||
return p;
|
||||
};
|
||||
|
||||
// between sequences, after a valid sequence, 1/2/3 continuation bytes
|
||||
// missing, and after the lead bytes that restrict the second byte
|
||||
const std::vector<std::vector<unsigned char>> prefixes =
|
||||
{
|
||||
{}, {0xC3, 0xA4}, {0xC3}, {0xE1}, {0xF1}, {0xE1, 0x80}, {0xF1, 0x80}, {0xF1, 0x80, 0x80},
|
||||
{0xE0}, {0xED}, {0xF0}, {0xF4}
|
||||
};
|
||||
const std::vector<std::vector<unsigned char>> suffixes =
|
||||
{
|
||||
{}, {0x80}, {0xBF}, {0x41}, {0xC3, 0xA4}, {0xE4, 0xB8, 0xAD}, {0xF0, 0x9F, 0x98, 0x80}, {0xFF}
|
||||
};
|
||||
for (const auto& prefix : prefixes)
|
||||
{
|
||||
for (unsigned byte = 0; byte <= 0xFF; ++byte)
|
||||
{
|
||||
for (const auto& suffix : suffixes)
|
||||
{
|
||||
std::vector<unsigned char> input(prefix);
|
||||
input.push_back(static_cast<unsigned char>(byte));
|
||||
input.insert(input.end(), suffix.begin(), suffix.end());
|
||||
const unsigned char* const begin = input.data();
|
||||
const unsigned char* const end = begin + input.size();
|
||||
CAPTURE(prefix.size())
|
||||
CAPTURE(byte)
|
||||
CAPTURE(suffix.size())
|
||||
const unsigned char* const expected = reference(begin, end);
|
||||
CHECK(nlohmann::detail::utf8_dfa_prefix(begin, end) - begin == expected - begin);
|
||||
|
||||
// utf8_dfa_run() only tells whether the whole run is valid
|
||||
const bool whole_run = expected == end || *expected < 0x80u;
|
||||
CHECK(nlohmann::detail::utf8_dfa_run(begin, end) == (whole_run ? expected : nullptr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// an ill-formed byte stops the scan at once, whatever follows
|
||||
std::vector<unsigned char> invalid(1000, 0xFF);
|
||||
CHECK(nlohmann::detail::utf8_dfa_prefix(invalid.data(), invalid.data() + invalid.size()) == invalid.data());
|
||||
CHECK(nlohmann::detail::utf8_dfa_run(invalid.data(), invalid.data() + invalid.size()) == nullptr);
|
||||
invalid[0] = 0xC3;
|
||||
invalid[1] = 0xA4;
|
||||
CHECK(nlohmann::detail::utf8_dfa_prefix(invalid.data(), invalid.data() + invalid.size()) == invalid.data() + 2);
|
||||
}
|
||||
|
||||
TEST_CASE("float_bigint")
|
||||
{
|
||||
// the big integer of the exact comparison of a float token with the
|
||||
|
||||
+126
-5
@@ -22,6 +22,7 @@ using nlohmann::detail::bit_cast;
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
@@ -711,8 +712,122 @@ void check_shortest(double v)
|
||||
CHECK(digits_and_exponent(std::string(std_text.data(), r.ptr)) == de);
|
||||
#endif
|
||||
}
|
||||
/// a basic_json whose floating-point numbers are floats
|
||||
using json_with_float = nlohmann::basic_json<std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, float>;
|
||||
|
||||
/// the correctly rounded float of a decimal text (as parse_double())
|
||||
float parse_float(const std::string& text)
|
||||
{
|
||||
const json_with_float j = json_with_float::parse(text, nullptr, false);
|
||||
return j.is_discarded() ? std::numeric_limits<float>::infinity() : j.get<float>();
|
||||
}
|
||||
|
||||
/// Check the representation of a positive finite float as check_shortest():
|
||||
/// it reads back as the same value, and no representation with fewer digits
|
||||
/// does.
|
||||
void check_shortest_float(float v)
|
||||
{
|
||||
std::array<char, 33> buf{};
|
||||
char* end = nlohmann::detail::to_chars(buf.data(), buf.data() + 32, v);
|
||||
const std::string text(buf.data(), end);
|
||||
CAPTURE(text)
|
||||
CHECK(bit_cast<std::uint32_t>(parse_float(text)) == bit_cast<std::uint32_t>(v));
|
||||
const auto de = digits_and_exponent(text);
|
||||
const std::string& digits = de.first;
|
||||
if (digits.size() > 1)
|
||||
{
|
||||
const std::uint64_t mid = std::stoull(digits.substr(0, digits.size() - 1));
|
||||
for (const std::uint64_t candidate :
|
||||
{
|
||||
mid - 1, mid, mid + 1
|
||||
})
|
||||
{
|
||||
CAPTURE(candidate)
|
||||
const std::string shorter = std::to_string(candidate) + "e" + std::to_string(de.second + 1);
|
||||
CHECK(bit_cast<std::uint32_t>(parse_float(shorter)) != bit_cast<std::uint32_t>(v));
|
||||
}
|
||||
}
|
||||
#if defined(JSON_HAS_CPP_17) && defined(__cpp_lib_to_chars) && !defined(__INTEL_COMPILER)
|
||||
std::array<char, 64> std_text {};
|
||||
const auto r = std::to_chars(std_text.data(), std_text.data() + std_text.size(), v, std::chars_format::scientific);
|
||||
CHECK(digits_and_exponent(std::string(std_text.data(), r.ptr)) == de);
|
||||
#endif
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("shortest digits of floats")
|
||||
{
|
||||
// (all 2^31 - 2^23 - 1 positive finite floats were checked against
|
||||
// std::to_chars when this was written; these are the edge cases)
|
||||
SECTION("boundary values")
|
||||
{
|
||||
for (const float v :
|
||||
{
|
||||
std::numeric_limits<float>::min(), std::numeric_limits<float>::max(), std::numeric_limits<float>::denorm_min(),
|
||||
std::nextafter(std::numeric_limits<float>::min(), 0.0f), std::nextafter(std::numeric_limits<float>::max(), 0.0f),
|
||||
1.0f, 2.0f, 0.1f, 0.3f, 1e7f, 1e-5f, 123456.7f, 16777216.0f, 16777217.0f, 7.0385307e-26f
|
||||
})
|
||||
{
|
||||
check_shortest_float(v);
|
||||
}
|
||||
// all powers of two (their rounding interval is narrower below)
|
||||
for (int e = -149; e <= 127; ++e)
|
||||
{
|
||||
check_shortest_float(std::ldexp(1.0f, e));
|
||||
}
|
||||
// powers of ten and their neighbors (from 1e-44: 1e-45 reads as the
|
||||
// smallest float, which is in the list above, and the float below it is 0)
|
||||
for (int e = -44; e <= 38; ++e)
|
||||
{
|
||||
const float p = parse_float("1e" + std::to_string(e));
|
||||
check_shortest_float(p);
|
||||
check_shortest_float(std::nextafter(p, 0.0f));
|
||||
check_shortest_float(std::nextafter(p, std::numeric_limits<float>::infinity()));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("where Grisu2 wrote other digits")
|
||||
{
|
||||
// Grisu2 leaves out the decimals on the boundary of the rounding
|
||||
// interval and may take the other one of two equally close decimals;
|
||||
// both read back as the same float either way
|
||||
const auto text = [](float v) -> std::string
|
||||
{
|
||||
std::array<char, 33> buf{};
|
||||
char* end = nlohmann::detail::to_chars(buf.data(), buf.data() + 32, v);
|
||||
return {buf.data(), end};
|
||||
};
|
||||
const auto grisu2_text = [](float v) -> std::string
|
||||
{
|
||||
std::array<char, 33> buf{};
|
||||
char* end = nlohmann::detail::dtoa_impl::write_positive_grisu2(buf.data(), buf.data() + 32, v);
|
||||
return {buf.data(), end};
|
||||
};
|
||||
// 134218208 = 2^27 + 32: 1.342182e+08 is the upper end of its rounding interval
|
||||
CHECK(text(134218208.0f) == "1.342182e+08");
|
||||
CHECK(grisu2_text(134218208.0f) == "1.3421821e+08");
|
||||
check_shortest_float(134218208.0f);
|
||||
// 2097156.25 = 2^21 + 4.25: 2097156.2 and 2097156.3 are equally close; the even digit
|
||||
CHECK(text(2097156.25f) == "2.0971562e+06");
|
||||
CHECK(grisu2_text(2097156.25f) == "2.0971563e+06");
|
||||
check_shortest_float(2097156.25f);
|
||||
}
|
||||
|
||||
SECTION("random floats")
|
||||
{
|
||||
std::mt19937 rng(5295); // NOLINT(cert-msc32-c,cert-msc51-cpp,bugprone-random-generator-seed): reproducible
|
||||
for (int i = 0; i < 100000; ++i)
|
||||
{
|
||||
const std::uint32_t bits = static_cast<std::uint32_t>(rng()) & 0x7FFFFFFFu;
|
||||
const auto v = bit_cast<float>(bits);
|
||||
if (std::isfinite(v) && bits != 0)
|
||||
{
|
||||
check_shortest_float(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("shortest digits of doubles")
|
||||
{
|
||||
SECTION("powers of ten")
|
||||
@@ -791,11 +906,16 @@ TEST_CASE("shortest digits of doubles")
|
||||
TEST_CASE("choice of the conversion")
|
||||
{
|
||||
using nlohmann::detail::dtoa_impl::is_binary64;
|
||||
using nlohmann::detail::dtoa_impl::float_conversion;
|
||||
|
||||
SECTION("by the format of the type")
|
||||
{
|
||||
// Zmij needs binary64 numbers; everything else uses Grisu2
|
||||
// Zmij needs binary64 or binary32 numbers; everything else uses Grisu2
|
||||
static_assert(!is_binary64<float>::value, "float is not binary64");
|
||||
static_assert(float_conversion<float>::value == (std::numeric_limits<float>::is_iec559 && std::numeric_limits<float>::digits == 24 ? 2 : 0),
|
||||
"float is binary32 where it is IEEE 754 with 24 digits");
|
||||
static_assert(float_conversion<double>::value == (is_binary64<double>::value ? 1 : 0), "double is binary64 or uses Grisu2");
|
||||
static_assert(float_conversion<int>::value == 0, "integers use Grisu2");
|
||||
static_assert(is_binary64<double>::value == (std::numeric_limits<double>::is_iec559 && std::numeric_limits<double>::digits == 53),
|
||||
"double is binary64 where it is IEEE 754 with 53 digits");
|
||||
static_assert(!is_binary64<int>::value, "integers are not binary64");
|
||||
@@ -805,7 +925,7 @@ TEST_CASE("choice of the conversion")
|
||||
CHECK(is_binary64<double>::value);
|
||||
}
|
||||
|
||||
SECTION("float: Grisu2, double: Zmij")
|
||||
SECTION("float and double: Zmij")
|
||||
{
|
||||
// 5.3165205877497296e+16 is one of the doubles for which Grisu2 does not find the shortest digits
|
||||
constexpr double value = 5.3165205877497296e+16;
|
||||
@@ -817,11 +937,12 @@ TEST_CASE("choice of the conversion")
|
||||
end = nlohmann::detail::dtoa_impl::write_positive_grisu2(buf.data(), last, value);
|
||||
CHECK(std::string(buf.data(), end) == "5.3165205877497296e+16");
|
||||
|
||||
constexpr float f = 1.1754944e-38f;
|
||||
// 134218208.0f is one of the floats for which Grisu2 does not find the shortest digits
|
||||
constexpr float f = 134218208.0f;
|
||||
end = nlohmann::detail::dtoa_impl::write_positive(buf.data(), last, f);
|
||||
const std::string dispatched(buf.data(), end);
|
||||
CHECK(std::string(buf.data(), end) == "1.342182e+08");
|
||||
end = nlohmann::detail::dtoa_impl::write_positive_grisu2(buf.data(), last, f);
|
||||
CHECK(dispatched == std::string(buf.data(), end));
|
||||
CHECK(std::string(buf.data(), end) == "1.3421821e+08");
|
||||
}
|
||||
|
||||
SECTION("long double with the format of a double: Zmij")
|
||||
|
||||
Reference in new issue
Block a user