Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
22d2013b13 |
No files matched your search
@@ -13,15 +13,9 @@ function never throws an exception for invalid input, and the returned `#!cpp bo
|
||||
is returned.
|
||||
|
||||
The result is the same as that of `BasicJsonType::accept()` by design; the difference is how the check is made. This
|
||||
function runs the document's builder, which checks the text as [`parse()`](parse.md) does, while
|
||||
`BasicJsonType::accept()` runs the SAX parser with a handler that ignores every event. Use this one if you go on to
|
||||
parse the same input with [`parse()`](parse.md) anyway, so that both use the same code; either is fine for a plain
|
||||
yes/no answer.
|
||||
|
||||
When both `ignore_comments` and `ignore_trailing_commas` are `#!cpp false` (the default), the builder writes no index.
|
||||
With either option set, it builds the index as [`parse()`](parse.md) does and discards it: the result is the same, only
|
||||
the time differs. (Neither option needs the index; the variant that skips it exists only for the default options to
|
||||
keep the code size down.)
|
||||
function runs the document's index builder (and discards the index), while `BasicJsonType::accept()` runs the SAX
|
||||
parser with a handler that ignores every event. Use this one if you go on to parse the same input with
|
||||
[`parse()`](parse.md) anyway, so that both use the same code; either is fine for a plain yes/no answer.
|
||||
|
||||
## Template parameters
|
||||
|
||||
|
||||
@@ -4293,6 +4293,19 @@ class binary_reader
|
||||
return get_bytes(len, "string", result);
|
||||
}
|
||||
|
||||
/// whether @a s is valid UTF-8: ASCII eight bytes at a time, the rest
|
||||
/// sequence by sequence (as the binary writers check strings)
|
||||
static bool is_valid_utf8_string(const string_t& s, std::true_type /*is_byte*/) noexcept
|
||||
{
|
||||
return valid_utf8_string(reinterpret_cast<const unsigned char*>(s.data()), s.size());
|
||||
}
|
||||
|
||||
/// whether @a s, of a character type wider than a byte, is valid UTF-8
|
||||
static bool is_valid_utf8_string(const string_t& s, std::false_type /*is_byte*/) noexcept
|
||||
{
|
||||
return is_valid_utf8(s);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief validate a decoded text string (value or object key) against @ref error_handler
|
||||
|
||||
@@ -4312,7 +4325,16 @@ class binary_reader
|
||||
*/
|
||||
bool check_string_utf8(string_t& result, const char* context)
|
||||
{
|
||||
if (error_handler == error_handler_t::keep || is_valid_utf8(result))
|
||||
return error_handler == error_handler_t::keep || check_string_utf8_handler(result, context);
|
||||
}
|
||||
|
||||
/// @ref check_string_utf8 for an error handler other than @ref
|
||||
/// error_handler_t::keep; out of line, so that reading strings without a
|
||||
/// check does not pay for the code of the check
|
||||
JSON_HEDLEY_NEVER_INLINE
|
||||
bool check_string_utf8_handler(string_t& result, const char* context)
|
||||
{
|
||||
if (is_valid_utf8_string(result, std::integral_constant<bool, sizeof(typename string_t::value_type) == 1>()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -322,6 +322,78 @@ JSON_SCAN_SSSE3_TARGET JSON_HEDLEY_NEVER_INLINE inline bool vector_utf8_run(cons
|
||||
block_out = block;
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief skip 16-byte blocks of well-formed UTF-8 at p (a character boundary)
|
||||
|
||||
Like vector_utf8_run, but quotes, backslashes, and control characters are
|
||||
ordinary characters. Returns the start of the first block in which the check
|
||||
found an error (which may belong to a sequence begun in the block before) or
|
||||
where fewer than 16 bytes are left; a sequence may be cut at that point, so the
|
||||
caller resumes at its start, one sequence at a time.
|
||||
*/
|
||||
JSON_SCAN_SSSE3_TARGET JSON_HEDLEY_NEVER_INLINE inline const unsigned char* vector_utf8_valid_blocks(const unsigned char* block, const unsigned char* e) noexcept
|
||||
{
|
||||
using lookup = utf8_lookup4<>;
|
||||
#if JSON_SCAN_NEON
|
||||
const uint8x16_t t1h = vld1q_u8(lookup::byte_1_high.data());
|
||||
const uint8x16_t t1l = vld1q_u8(lookup::byte_1_low.data());
|
||||
const uint8x16_t t2h = vld1q_u8(lookup::byte_2_high.data());
|
||||
uint8x16_t prev = vdupq_n_u8(0);
|
||||
while (e - block >= 16)
|
||||
{
|
||||
const uint8x16_t in = vld1q_u8(block);
|
||||
if (vmaxvq_u8(vorrq_u8(in, prev)) < 0x80)
|
||||
{
|
||||
// ASCII here, and no sequence from the block before to finish
|
||||
prev = in;
|
||||
block += 16;
|
||||
continue;
|
||||
}
|
||||
const uint8x16_t prev1 = vextq_u8(prev, in, 15);
|
||||
const uint8x16_t sc = vandq_u8(vandq_u8(vqtbl1q_u8(t1h, vshrq_n_u8(prev1, 4)), vqtbl1q_u8(t1l, vandq_u8(prev1, vdupq_n_u8(0x0F)))), vqtbl1q_u8(t2h, vshrq_n_u8(in, 4)));
|
||||
const uint8x16_t must23 = vorrq_u8(vqsubq_u8(vextq_u8(prev, in, 14), vdupq_n_u8(0xE0 - 0x80)), vqsubq_u8(vextq_u8(prev, in, 13), vdupq_n_u8(0xF0 - 0x80)));
|
||||
const uint8x16_t err = veorq_u8(vandq_u8(must23, vdupq_n_u8(0x80)), sc);
|
||||
if (vmaxvq_u8(err) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
prev = in;
|
||||
block += 16;
|
||||
}
|
||||
#else
|
||||
const __m128i t1h = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_1_high.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i t1l = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_1_low.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i t2h = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_2_high.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i nibble = _mm_set1_epi8(0x0F);
|
||||
const __m128i zero = _mm_setzero_si128();
|
||||
__m128i prev = zero;
|
||||
while (e - block >= 16)
|
||||
{
|
||||
const __m128i in = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(block))); // NOLINT(bugprone-casting-through-void)
|
||||
if (_mm_movemask_epi8(_mm_or_si128(in, prev)) == 0)
|
||||
{
|
||||
prev = in;
|
||||
block += 16;
|
||||
continue;
|
||||
}
|
||||
const __m128i prev1 = _mm_alignr_epi8(in, prev, 15);
|
||||
const __m128i sc = _mm_and_si128(_mm_and_si128(_mm_shuffle_epi8(t1h, _mm_and_si128(_mm_srli_epi16(prev1, 4), nibble)),
|
||||
_mm_shuffle_epi8(t1l, _mm_and_si128(prev1, nibble))),
|
||||
_mm_shuffle_epi8(t2h, _mm_and_si128(_mm_srli_epi16(in, 4), nibble)));
|
||||
const __m128i must23 = _mm_or_si128(_mm_subs_epu8(_mm_alignr_epi8(in, prev, 14), _mm_set1_epi8(0xE0 - 0x80)),
|
||||
_mm_subs_epu8(_mm_alignr_epi8(in, prev, 13), _mm_set1_epi8(0xF0 - 0x80)));
|
||||
const __m128i err = _mm_xor_si128(_mm_and_si128(must23, _mm_set1_epi8(static_cast<char>(-128))), sc);
|
||||
if (_mm_movemask_epi8(_mm_cmpeq_epi8(err, zero)) != 0xFFFF)
|
||||
{
|
||||
break;
|
||||
}
|
||||
prev = in;
|
||||
block += 16;
|
||||
}
|
||||
#endif
|
||||
return block;
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace detail
|
||||
|
||||
@@ -236,6 +236,64 @@ inline std::size_t valid_utf8_prefix(const unsigned char* data, std::size_t n) n
|
||||
return pos;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief whether [data, data+n) is valid UTF-8, for checking whole strings
|
||||
|
||||
The same answer as valid_utf8_prefix(data, n) == n. Short ASCII strings, the
|
||||
most common keys and values, are decided in one test, and long strings are
|
||||
checked 16 bytes at a time where the vector check is available.
|
||||
*/
|
||||
inline bool valid_utf8_string(const unsigned char* data, std::size_t n) noexcept
|
||||
{
|
||||
constexpr std::uint64_t high = 0x8080808080808080ull;
|
||||
if (n <= 16)
|
||||
{
|
||||
// two overlapping words cover 8 to 16 bytes, one partial word fewer
|
||||
std::uint64_t first = 0;
|
||||
std::uint64_t last = 0;
|
||||
if (n >= 8)
|
||||
{
|
||||
std::memcpy(&first, data, sizeof(first));
|
||||
std::memcpy(&last, data + n - 8, sizeof(last));
|
||||
}
|
||||
else if (n != 0)
|
||||
{
|
||||
std::memcpy(&first, data, n);
|
||||
}
|
||||
if (((first | last) & high) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#if JSON_SCAN_VECTOR_UTF8
|
||||
else if (n >= 32
|
||||
#if JSON_SCAN_SSSE3_DISPATCH
|
||||
&& cpu_has_ssse3()
|
||||
#endif
|
||||
)
|
||||
{
|
||||
const unsigned char* block = vector_utf8_valid_blocks(data, data + n);
|
||||
// the rest from the start of a sequence that may be cut at block
|
||||
for (int i = 1; i <= 3 && block - i >= data; ++i)
|
||||
{
|
||||
const unsigned char c = block[-i];
|
||||
if (c < 0x80)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (c >= 0xC0)
|
||||
{
|
||||
block -= i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const auto checked = static_cast<std::size_t>(block - data);
|
||||
return checked + valid_utf8_prefix(block, n - checked) == n;
|
||||
}
|
||||
#endif
|
||||
return valid_utf8_prefix(data, n) == n;
|
||||
}
|
||||
|
||||
#if JSON_SCAN_VECTOR_UTF8
|
||||
/// the end of scan_string_vector from block, where the vector loop stopped
|
||||
/// (ill-formed UTF-8, or fewer than 16 bytes left): one byte or sequence at a
|
||||
|
||||
@@ -3080,6 +3080,25 @@ class binary_writer
|
||||
}
|
||||
}
|
||||
|
||||
/// write the head and the characters of @a value, at most
|
||||
/// max_text_in_reservation of them, in one reservation
|
||||
JSON_HEDLEY_ALWAYS_INLINE void write_cbor_string_reserved(const string_t& value)
|
||||
{
|
||||
CharType* const out = oa.reserve(value.size() + 2);
|
||||
CharType* p = out;
|
||||
if (value.size() <= 0x17)
|
||||
{
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(0x60 + value.size()));
|
||||
}
|
||||
else
|
||||
{
|
||||
*p++ = to_char_type(0x78);
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(value.size()));
|
||||
}
|
||||
copy_characters(p, reinterpret_cast<const CharType*>(value.data()), value.size());
|
||||
oa.commit(static_cast<std::size_t>(p - out) + value.size());
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief write a CBOR text string
|
||||
|
||||
@@ -3101,19 +3120,7 @@ class binary_writer
|
||||
// the string is written as it is: head and characters in one reservation
|
||||
if (JSON_HEDLEY_LIKELY(value.size() <= max_text_in_reservation))
|
||||
{
|
||||
CharType* const out = oa.reserve(value.size() + 2);
|
||||
CharType* p = out;
|
||||
if (value.size() <= 0x17)
|
||||
{
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(0x60 + value.size()));
|
||||
}
|
||||
else
|
||||
{
|
||||
*p++ = to_char_type(0x78);
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(value.size()));
|
||||
}
|
||||
copy_characters(p, reinterpret_cast<const CharType*>(value.data()), value.size());
|
||||
oa.commit(static_cast<std::size_t>(p - out) + value.size());
|
||||
write_cbor_string_reserved(value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -3127,6 +3134,13 @@ class binary_writer
|
||||
JSON_HEDLEY_NEVER_INLINE
|
||||
void write_cbor_checked_string(const string_t& value, const BasicJsonType& context)
|
||||
{
|
||||
if (error_handler == error_handler_t::strict && value.size() <= max_text_in_reservation)
|
||||
{
|
||||
check_utf8(value, context);
|
||||
write_cbor_string_reserved(value);
|
||||
return;
|
||||
}
|
||||
|
||||
string_t storage;
|
||||
const string_t& sanitized = sanitize_utf8_for_write(value, context, storage);
|
||||
|
||||
@@ -3141,6 +3155,27 @@ class binary_writer
|
||||
// MsgPack //
|
||||
/////////////
|
||||
|
||||
/// write the head and the characters of @a value, at most
|
||||
/// max_text_in_reservation of them, in one reservation
|
||||
JSON_HEDLEY_ALWAYS_INLINE void write_msgpack_string_reserved(const string_t& value)
|
||||
{
|
||||
CharType* const out = oa.reserve(value.size() + 2);
|
||||
CharType* p = out;
|
||||
if (value.size() <= 31)
|
||||
{
|
||||
// fixstr
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(0xA0 | value.size()));
|
||||
}
|
||||
else
|
||||
{
|
||||
// str 8
|
||||
*p++ = to_char_type(0xD9);
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(value.size()));
|
||||
}
|
||||
copy_characters(p, reinterpret_cast<const CharType*>(value.data()), value.size());
|
||||
oa.commit(static_cast<std::size_t>(p - out) + value.size());
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief write a MessagePack str
|
||||
|
||||
@@ -3158,21 +3193,7 @@ class binary_writer
|
||||
// the string is written as it is: head and characters in one reservation
|
||||
if (JSON_HEDLEY_LIKELY(value.size() <= max_text_in_reservation))
|
||||
{
|
||||
CharType* const out = oa.reserve(value.size() + 2);
|
||||
CharType* p = out;
|
||||
if (value.size() <= 31)
|
||||
{
|
||||
// fixstr
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(0xA0 | value.size()));
|
||||
}
|
||||
else
|
||||
{
|
||||
// str 8
|
||||
*p++ = to_char_type(0xD9);
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(value.size()));
|
||||
}
|
||||
copy_characters(p, reinterpret_cast<const CharType*>(value.data()), value.size());
|
||||
oa.commit(static_cast<std::size_t>(p - out) + value.size());
|
||||
write_msgpack_string_reserved(value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -3185,6 +3206,13 @@ class binary_writer
|
||||
JSON_HEDLEY_NEVER_INLINE
|
||||
void write_msgpack_checked_string(const string_t& value, const BasicJsonType& context)
|
||||
{
|
||||
if (error_handler == error_handler_t::strict && value.size() <= max_text_in_reservation)
|
||||
{
|
||||
check_utf8(value, context);
|
||||
write_msgpack_string_reserved(value);
|
||||
return;
|
||||
}
|
||||
|
||||
string_t storage;
|
||||
const string_t& sanitized = sanitize_utf8_for_write(value, context, storage);
|
||||
|
||||
@@ -4794,10 +4822,9 @@ class binary_writer
|
||||
static void check_utf8(const string_t& s, const BasicJsonType& context)
|
||||
{
|
||||
const auto* data = reinterpret_cast<const unsigned char*>(s.data());
|
||||
const std::size_t valid = valid_utf8_prefix(data, s.size());
|
||||
if (JSON_HEDLEY_UNLIKELY(valid != s.size()))
|
||||
if (JSON_HEDLEY_UNLIKELY(!valid_utf8_string(data, s.size())))
|
||||
{
|
||||
throw_invalid_utf8(data, valid, context);
|
||||
throw_invalid_utf8(data, valid_utf8_prefix(data, s.size()), context);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,8 +69,7 @@ struct parse_failure
|
||||
};
|
||||
|
||||
/// FloatType: the number_float_t of the document, whose overflow parse() rejects
|
||||
/// Store: false for accept(): the grammar, number, string and UTF-8 checks run as for parse(), but no node is written
|
||||
template<typename FloatType, bool Comments, bool TrailingCommas, bool NulIsEnd, bool Sentinel, bool Store>
|
||||
template<typename FloatType, bool Comments, bool TrailingCommas, bool NulIsEnd, bool Sentinel>
|
||||
class builder
|
||||
{
|
||||
public:
|
||||
@@ -246,87 +245,11 @@ class builder
|
||||
return p;
|
||||
}
|
||||
|
||||
/// the string from p, the first backslash, control character, ill-formed UTF-8 byte, or the end, checked and not
|
||||
/// decoded (accept() stores nothing, so the arena is not written): the checks, errors and error positions of
|
||||
/// slow_string(), for everything up to the closing quote
|
||||
NLOHMANN_VIEW_NOINLINE decoded check_string(const unsigned char* p)
|
||||
{
|
||||
const unsigned char* const end = m_end;
|
||||
for (;;)
|
||||
{
|
||||
if (p == end)
|
||||
{
|
||||
return failed(error_code::string_missing_quote, p);
|
||||
}
|
||||
const unsigned char c = *p;
|
||||
if (c == '"')
|
||||
{
|
||||
++p;
|
||||
return decoded{p, 0, 0};
|
||||
}
|
||||
if (c != '\\')
|
||||
{
|
||||
return failed(c < 0x20 ? error_code::string_control_character : error_code::string_utf8, p);
|
||||
}
|
||||
++p;
|
||||
if (p == end)
|
||||
{
|
||||
return failed(error_code::string_missing_quote, p);
|
||||
}
|
||||
const unsigned char d = *p++;
|
||||
if (d == 'u')
|
||||
{
|
||||
const int cp = hex4(p); // (moves p past the four digits)
|
||||
if (NLOHMANN_VIEW_UNLIKELY(cp < 0))
|
||||
{
|
||||
return failed(error_code::string_unicode_hex, p);
|
||||
}
|
||||
if (NLOHMANN_VIEW_UNLIKELY((cp & 0xF800) == 0xD800)) // a surrogate
|
||||
{
|
||||
if (cp >= 0xDC00)
|
||||
{
|
||||
return failed(error_code::string_surrogate_low, p);
|
||||
}
|
||||
if (end - p < 2 || p[0] != '\\' || p[1] != 'u')
|
||||
{
|
||||
return failed(error_code::string_surrogate_high, p);
|
||||
}
|
||||
p += 2;
|
||||
const int lo = hex4(p); // (moves p past the four digits)
|
||||
if (lo < 0)
|
||||
{
|
||||
return failed(error_code::string_unicode_hex, p);
|
||||
}
|
||||
if (lo < 0xDC00 || lo > 0xDFFF)
|
||||
{
|
||||
return failed(error_code::string_surrogate_high, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!(d == '"' || d == '\\' || d == '/' || d == 'b' || d == 'f' || d == 'n' || d == 'r' || d == 't'))
|
||||
{
|
||||
--p;
|
||||
return failed(error_code::string_escape, p);
|
||||
}
|
||||
if (p != end && *p == '\\')
|
||||
{
|
||||
continue; // (escapes in a row: no run of text between them)
|
||||
}
|
||||
p = scan_string_run(p, end); // the text up to the next backslash, quote, control character, or ill-formed UTF-8
|
||||
}
|
||||
}
|
||||
|
||||
/// s points to the start of the string, p to a backslash, a control
|
||||
/// character, an ill-formed UTF-8 byte, or the end: decode the string into
|
||||
/// the arena
|
||||
NLOHMANN_VIEW_NOINLINE decoded slow_string(const unsigned char* s, const unsigned char* p)
|
||||
{
|
||||
if (!enabled(Store))
|
||||
{
|
||||
// (accept(): the string is checked, not decoded)
|
||||
static_cast<void>(s);
|
||||
return check_string(p);
|
||||
}
|
||||
// single-character escapes; 0: invalid (and 'u', handled separately)
|
||||
static const std::array<char, 128> simple_escape =
|
||||
{
|
||||
@@ -687,13 +610,10 @@ run_found:
|
||||
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE bool run()
|
||||
{
|
||||
if (enabled(Store))
|
||||
{
|
||||
cold.m_doc.reserve(estimate_nodes(reinterpret_cast<const char*>(b), static_cast<std::size_t>(e - b)));
|
||||
base = cold.m_doc.nodes;
|
||||
out = base;
|
||||
cap = base + cold.m_doc.node_capacity;
|
||||
}
|
||||
cold.m_doc.reserve(estimate_nodes(reinterpret_cast<const char*>(b), static_cast<std::size_t>(e - b)));
|
||||
base = cold.m_doc.nodes;
|
||||
out = base;
|
||||
cap = base + cold.m_doc.node_capacity;
|
||||
|
||||
if (e - p >= 3 && p[0] == 0xEF && p[1] == 0xBB && p[2] == 0xBF)
|
||||
{
|
||||
@@ -877,10 +797,7 @@ close_object:
|
||||
// an array pays nothing for this)
|
||||
if (NLOHMANN_VIEW_UNLIKELY(cur_count >= document_data::index_min_members))
|
||||
{
|
||||
if (enabled(Store))
|
||||
{
|
||||
cold.note_large_object(cur_idx);
|
||||
}
|
||||
cold.note_large_object(cur_idx);
|
||||
}
|
||||
|
||||
close_container:
|
||||
@@ -1040,12 +957,6 @@ indent_done:
|
||||
/// an integer's value); two stores on little-endian targets
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE node* push_node(node_kind k, std::uint8_t flags, std::uint16_t extra, std::size_t off, std::uint64_t second)
|
||||
{
|
||||
if (!enabled(Store))
|
||||
{
|
||||
// accept(): nothing is stored, so what computes the arguments is not run either. base and out stay
|
||||
// null: their difference, which open() takes, is 0.
|
||||
return out;
|
||||
}
|
||||
if (NLOHMANN_VIEW_UNLIKELY(out == cap))
|
||||
{
|
||||
const auto n = static_cast<std::size_t>(out - base);
|
||||
@@ -1104,12 +1015,9 @@ indent_done:
|
||||
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE void close()
|
||||
{
|
||||
if (enabled(Store))
|
||||
{
|
||||
node& n = base[cur_idx];
|
||||
n.len = cur_count;
|
||||
n.next = static_cast<std::uint32_t>(out - base) - cur_idx;
|
||||
}
|
||||
node& n = base[cur_idx];
|
||||
n.len = cur_count;
|
||||
n.next = static_cast<std::uint32_t>(out - base) - cur_idx;
|
||||
if (--depth != 0)
|
||||
{
|
||||
if (NLOHMANN_VIEW_LIKELY(depth <= 64))
|
||||
@@ -1284,44 +1192,34 @@ indent_done:
|
||||
};
|
||||
|
||||
/// run the builder with compile-time options
|
||||
template<typename FloatType, bool Comments, bool TrailingCommas, bool NulIsEnd, bool Store>
|
||||
template<typename FloatType, bool Comments, bool TrailingCommas, bool NulIsEnd>
|
||||
inline bool build_with(document_data& d, const char* src, std::size_t size, bool sentinel, parse_failure& failure)
|
||||
{
|
||||
if (sentinel)
|
||||
{
|
||||
builder<FloatType, Comments, TrailingCommas, NulIsEnd, true, Store> bld(d, src, size);
|
||||
builder<FloatType, Comments, TrailingCommas, NulIsEnd, true> bld(d, src, size);
|
||||
const bool ok = bld.run();
|
||||
failure = bld.failure();
|
||||
return ok;
|
||||
}
|
||||
builder<FloatType, Comments, TrailingCommas, NulIsEnd, false, Store> bld(d, src, size);
|
||||
builder<FloatType, Comments, TrailingCommas, NulIsEnd, false> bld(d, src, size);
|
||||
const bool ok = bld.run();
|
||||
failure = bld.failure();
|
||||
return ok;
|
||||
}
|
||||
|
||||
/// sentinel: src[size] is readable and 0 (e.g. std::string); FloatType: the
|
||||
/// number_float_t of the document; validate_only: accept(), only the result is
|
||||
/// used
|
||||
/// number_float_t of the document
|
||||
template<typename FloatType, bool NulIsEnd>
|
||||
inline bool build(document_data& d, const char* src, std::size_t size, bool comments, bool trailing_commas, bool sentinel, bool validate_only,
|
||||
parse_failure& failure)
|
||||
inline bool build(document_data& d, const char* src, std::size_t size, bool comments, bool trailing_commas, bool sentinel, parse_failure& failure)
|
||||
{
|
||||
if (validate_only && !comments && !trailing_commas)
|
||||
{
|
||||
// accept() of plain JSON: nothing is stored. The other option sets
|
||||
// could skip the stores just as well; they keep the builder that
|
||||
// stores only so that there are two more instantiations of the parse
|
||||
// loop, not eight.
|
||||
return build_with<FloatType, false, false, NulIsEnd, false>(d, src, size, sentinel, failure);
|
||||
}
|
||||
if (comments)
|
||||
{
|
||||
return trailing_commas ? build_with<FloatType, true, true, NulIsEnd, true>(d, src, size, sentinel, failure)
|
||||
: build_with<FloatType, true, false, NulIsEnd, true>(d, src, size, sentinel, failure);
|
||||
return trailing_commas ? build_with<FloatType, true, true, NulIsEnd>(d, src, size, sentinel, failure)
|
||||
: build_with<FloatType, true, false, NulIsEnd>(d, src, size, sentinel, failure);
|
||||
}
|
||||
return trailing_commas ? build_with<FloatType, false, true, NulIsEnd, true>(d, src, size, sentinel, failure)
|
||||
: build_with<FloatType, false, false, NulIsEnd, true>(d, src, size, sentinel, failure);
|
||||
return trailing_commas ? build_with<FloatType, false, true, NulIsEnd>(d, src, size, sentinel, failure)
|
||||
: build_with<FloatType, false, false, NulIsEnd>(d, src, size, sentinel, failure);
|
||||
}
|
||||
|
||||
} // namespace view
|
||||
|
||||
@@ -1060,7 +1060,7 @@ class basic_json_document
|
||||
const bool ignore_trailing_commas = false)
|
||||
{
|
||||
basic_json_document d;
|
||||
d.read_range(first, last, error_mode(allow_exceptions), ignore_comments, ignore_trailing_commas);
|
||||
d.read_range(first, last, allow_exceptions, ignore_comments, ignore_trailing_commas);
|
||||
return d;
|
||||
}
|
||||
|
||||
@@ -1075,7 +1075,7 @@ class basic_json_document
|
||||
basic_json_document d;
|
||||
std::string buf;
|
||||
append_input(buf, std::forward<InputType>(input));
|
||||
d.build_owned(std::move(buf), error_mode(allow_exceptions), ignore_comments, ignore_trailing_commas);
|
||||
d.build_owned(std::move(buf), allow_exceptions, ignore_comments, ignore_trailing_commas);
|
||||
return d;
|
||||
}
|
||||
|
||||
@@ -1088,8 +1088,7 @@ class basic_json_document
|
||||
static bool accept(InputType&& input, const bool ignore_comments = false, const bool ignore_trailing_commas = false)
|
||||
{
|
||||
basic_json_document d;
|
||||
d.read_kind(std::forward<InputType>(input), on_error::validate, ignore_comments, ignore_trailing_commas,
|
||||
std::integral_constant<detail::view::input_kind, detail::view::classify_input<InputType>::value> {});
|
||||
d.read(std::forward<InputType>(input), false, ignore_comments, ignore_trailing_commas);
|
||||
return !d.is_discarded();
|
||||
}
|
||||
|
||||
@@ -1105,7 +1104,7 @@ class basic_json_document
|
||||
const bool ignore_comments = false,
|
||||
const bool ignore_trailing_commas = false)
|
||||
{
|
||||
read_kind(std::forward<InputType>(input), error_mode(allow_exceptions), ignore_comments, ignore_trailing_commas,
|
||||
read_kind(std::forward<InputType>(input), allow_exceptions, ignore_comments, ignore_trailing_commas,
|
||||
std::integral_constant<detail::view::input_kind, detail::view::classify_input<InputType>::value> {});
|
||||
}
|
||||
|
||||
@@ -1464,22 +1463,6 @@ class basic_json_document
|
||||
return idx;
|
||||
}
|
||||
|
||||
/// what a failed parse does
|
||||
enum class on_error : std::uint8_t
|
||||
{
|
||||
discard, ///< the document is discarded
|
||||
raise, ///< parse_error is thrown
|
||||
validate ///< accept(): the document is discarded, and no node is stored while parsing
|
||||
};
|
||||
|
||||
static on_error error_mode(bool allow_exceptions) noexcept
|
||||
{
|
||||
return allow_exceptions ? on_error::raise : on_error::discard;
|
||||
}
|
||||
|
||||
/// NulIsEnd of the builder: a null byte ends the input unless strict_nul_handling
|
||||
static constexpr bool nul_is_end = !detail::abi_config::strict_nul_handling;
|
||||
|
||||
/// create the storage (sized for the input) on first use
|
||||
void ensure_data(const char* src, std::size_t size)
|
||||
{
|
||||
@@ -1491,7 +1474,7 @@ class basic_json_document
|
||||
|
||||
/// parse a buffer the document takes ownership of (it replaces the
|
||||
/// previous owned copy)
|
||||
void build_owned(std::string&& buf, on_error allow_exceptions, bool comments, bool trailing_commas)
|
||||
void build_owned(std::string&& buf, bool allow_exceptions, bool comments, bool trailing_commas)
|
||||
{
|
||||
ensure_data(buf.data(), buf.size());
|
||||
m_data->owned = std::move(buf);
|
||||
@@ -1501,7 +1484,7 @@ class basic_json_document
|
||||
/// parse a copy of the input; the copy is appended to the cleared owned
|
||||
/// buffer, so that its capacity is reused
|
||||
template<typename T>
|
||||
void build_copy(T&& input, on_error allow_exceptions, bool comments, bool trailing_commas)
|
||||
void build_copy(T&& input, bool allow_exceptions, bool comments, bool trailing_commas)
|
||||
{
|
||||
if (!m_data)
|
||||
{
|
||||
@@ -1522,7 +1505,7 @@ class basic_json_document
|
||||
}
|
||||
|
||||
/// sentinel: src[size] is readable and 0 (std::string, C strings)
|
||||
void build(const char* src, std::size_t size, on_error allow_exceptions, bool comments, bool trailing_commas, bool owned, bool sentinel)
|
||||
void build(const char* src, std::size_t size, bool allow_exceptions, bool comments, bool trailing_commas, bool owned, bool sentinel)
|
||||
{
|
||||
ensure_data(src, size);
|
||||
document_data& d = *m_data;
|
||||
@@ -1549,8 +1532,7 @@ class basic_json_document
|
||||
}
|
||||
else
|
||||
{
|
||||
ok = detail::view::build<typename BasicJsonType::number_float_t, nul_is_end>(d, src, size, comments, trailing_commas, sentinel,
|
||||
allow_exceptions == on_error::validate, failure);
|
||||
ok = detail::view::build < typename BasicJsonType::number_float_t, !detail::abi_config::strict_nul_handling > (d, src, size, comments, trailing_commas, sentinel, failure);
|
||||
}
|
||||
if (NLOHMANN_VIEW_LIKELY(ok))
|
||||
{
|
||||
@@ -1563,7 +1545,7 @@ class basic_json_document
|
||||
return;
|
||||
}
|
||||
std::vector<std::uint32_t>().swap(d.large_objects);
|
||||
if (allow_exceptions == on_error::raise)
|
||||
if (allow_exceptions)
|
||||
{
|
||||
detail::view::throw_parse_failure<BasicJsonType>(failure, src, size, comments, trailing_commas);
|
||||
}
|
||||
@@ -1571,13 +1553,13 @@ class basic_json_document
|
||||
|
||||
// --- input dispatch (see detail::view::input_kind) ---
|
||||
|
||||
void read_kind(std::string&& s, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::move_string> /*unused*/)
|
||||
void read_kind(std::string&& s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::move_string> /*unused*/)
|
||||
{
|
||||
build_owned(std::move(s), ae, c, tc);
|
||||
}
|
||||
|
||||
template<typename CharT>
|
||||
void read_kind(CharT* s, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::c_string> /*unused*/)
|
||||
void read_kind(CharT* s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::c_string> /*unused*/)
|
||||
{
|
||||
if (s == nullptr)
|
||||
{
|
||||
@@ -1591,7 +1573,7 @@ class basic_json_document
|
||||
}
|
||||
|
||||
template<typename Array>
|
||||
void read_kind(Array& a, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::char_array> /*unused*/)
|
||||
void read_kind(Array& a, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::char_array> /*unused*/)
|
||||
{
|
||||
using CharT = typename std::remove_cv<typename std::remove_extent<Array>::type>::type;
|
||||
const std::size_t n = std::extent<Array>::value;
|
||||
@@ -1602,7 +1584,7 @@ class basic_json_document
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_kind(const T& s, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::borrow_range> /*unused*/)
|
||||
void read_kind(const T& s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::borrow_range> /*unused*/)
|
||||
{
|
||||
// std::basic_string guarantees data()[size()] == 0: use it as sentinel
|
||||
const auto size = static_cast<std::size_t>(s.size());
|
||||
@@ -1611,13 +1593,13 @@ class basic_json_document
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_kind(const T& s, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::copy_range> /*unused*/)
|
||||
void read_kind(const T& s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::copy_range> /*unused*/)
|
||||
{
|
||||
build_copy(s, ae, c, tc);
|
||||
}
|
||||
|
||||
template<typename CharT>
|
||||
void read_kind(CharT* s, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::wide_c_string> /*unused*/)
|
||||
void read_kind(CharT* s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::wide_c_string> /*unused*/)
|
||||
{
|
||||
if (s == nullptr)
|
||||
{
|
||||
@@ -1629,13 +1611,13 @@ class basic_json_document
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_kind(T&& input, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::adapter> /*unused*/)
|
||||
void read_kind(T&& input, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::adapter> /*unused*/)
|
||||
{
|
||||
build_copy(std::forward<T>(input), ae, c, tc);
|
||||
}
|
||||
|
||||
template<typename IteratorType>
|
||||
void read_range(IteratorType first, IteratorType last, on_error ae, bool c, bool tc)
|
||||
void read_range(IteratorType first, IteratorType last, bool ae, bool c, bool tc)
|
||||
{
|
||||
using value_type = typename std::remove_cv<typename std::iterator_traits<IteratorType>::value_type>::type;
|
||||
// borrow the range where the library's input adapter scans it as one
|
||||
@@ -1646,14 +1628,14 @@ class basic_json_document
|
||||
}
|
||||
|
||||
template<typename IteratorType>
|
||||
void read_range_impl(IteratorType first, IteratorType last, on_error ae, bool c, bool tc, std::true_type /*contiguous bytes*/)
|
||||
void read_range_impl(IteratorType first, IteratorType last, bool ae, bool c, bool tc, std::true_type /*contiguous bytes*/)
|
||||
{
|
||||
const auto size = static_cast<std::size_t>(std::distance(first, last));
|
||||
build(size == 0 ? "" : reinterpret_cast<const char*>(&*first), size, ae, c, tc, false, size == 0); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
}
|
||||
|
||||
template<typename IteratorType>
|
||||
void read_range_impl(IteratorType first, IteratorType last, on_error ae, bool c, bool tc, std::false_type /*other*/)
|
||||
void read_range_impl(IteratorType first, IteratorType last, bool ae, bool c, bool tc, std::false_type /*other*/)
|
||||
{
|
||||
build_copy(detail::view::make_adapter_input(detail::input_adapter(first, last)), ae, c, tc);
|
||||
}
|
||||
|
||||
Generated
+211
-32
@@ -12377,6 +12377,78 @@ JSON_SCAN_SSSE3_TARGET JSON_HEDLEY_NEVER_INLINE inline bool vector_utf8_run(cons
|
||||
block_out = block;
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief skip 16-byte blocks of well-formed UTF-8 at p (a character boundary)
|
||||
|
||||
Like vector_utf8_run, but quotes, backslashes, and control characters are
|
||||
ordinary characters. Returns the start of the first block in which the check
|
||||
found an error (which may belong to a sequence begun in the block before) or
|
||||
where fewer than 16 bytes are left; a sequence may be cut at that point, so the
|
||||
caller resumes at its start, one sequence at a time.
|
||||
*/
|
||||
JSON_SCAN_SSSE3_TARGET JSON_HEDLEY_NEVER_INLINE inline const unsigned char* vector_utf8_valid_blocks(const unsigned char* block, const unsigned char* e) noexcept
|
||||
{
|
||||
using lookup = utf8_lookup4<>;
|
||||
#if JSON_SCAN_NEON
|
||||
const uint8x16_t t1h = vld1q_u8(lookup::byte_1_high.data());
|
||||
const uint8x16_t t1l = vld1q_u8(lookup::byte_1_low.data());
|
||||
const uint8x16_t t2h = vld1q_u8(lookup::byte_2_high.data());
|
||||
uint8x16_t prev = vdupq_n_u8(0);
|
||||
while (e - block >= 16)
|
||||
{
|
||||
const uint8x16_t in = vld1q_u8(block);
|
||||
if (vmaxvq_u8(vorrq_u8(in, prev)) < 0x80)
|
||||
{
|
||||
// ASCII here, and no sequence from the block before to finish
|
||||
prev = in;
|
||||
block += 16;
|
||||
continue;
|
||||
}
|
||||
const uint8x16_t prev1 = vextq_u8(prev, in, 15);
|
||||
const uint8x16_t sc = vandq_u8(vandq_u8(vqtbl1q_u8(t1h, vshrq_n_u8(prev1, 4)), vqtbl1q_u8(t1l, vandq_u8(prev1, vdupq_n_u8(0x0F)))), vqtbl1q_u8(t2h, vshrq_n_u8(in, 4)));
|
||||
const uint8x16_t must23 = vorrq_u8(vqsubq_u8(vextq_u8(prev, in, 14), vdupq_n_u8(0xE0 - 0x80)), vqsubq_u8(vextq_u8(prev, in, 13), vdupq_n_u8(0xF0 - 0x80)));
|
||||
const uint8x16_t err = veorq_u8(vandq_u8(must23, vdupq_n_u8(0x80)), sc);
|
||||
if (vmaxvq_u8(err) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
prev = in;
|
||||
block += 16;
|
||||
}
|
||||
#else
|
||||
const __m128i t1h = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_1_high.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i t1l = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_1_low.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i t2h = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(lookup::byte_2_high.data()))); // NOLINT(bugprone-casting-through-void)
|
||||
const __m128i nibble = _mm_set1_epi8(0x0F);
|
||||
const __m128i zero = _mm_setzero_si128();
|
||||
__m128i prev = zero;
|
||||
while (e - block >= 16)
|
||||
{
|
||||
const __m128i in = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(block))); // NOLINT(bugprone-casting-through-void)
|
||||
if (_mm_movemask_epi8(_mm_or_si128(in, prev)) == 0)
|
||||
{
|
||||
prev = in;
|
||||
block += 16;
|
||||
continue;
|
||||
}
|
||||
const __m128i prev1 = _mm_alignr_epi8(in, prev, 15);
|
||||
const __m128i sc = _mm_and_si128(_mm_and_si128(_mm_shuffle_epi8(t1h, _mm_and_si128(_mm_srli_epi16(prev1, 4), nibble)),
|
||||
_mm_shuffle_epi8(t1l, _mm_and_si128(prev1, nibble))),
|
||||
_mm_shuffle_epi8(t2h, _mm_and_si128(_mm_srli_epi16(in, 4), nibble)));
|
||||
const __m128i must23 = _mm_or_si128(_mm_subs_epu8(_mm_alignr_epi8(in, prev, 14), _mm_set1_epi8(0xE0 - 0x80)),
|
||||
_mm_subs_epu8(_mm_alignr_epi8(in, prev, 13), _mm_set1_epi8(0xF0 - 0x80)));
|
||||
const __m128i err = _mm_xor_si128(_mm_and_si128(must23, _mm_set1_epi8(static_cast<char>(-128))), sc);
|
||||
if (_mm_movemask_epi8(_mm_cmpeq_epi8(err, zero)) != 0xFFFF)
|
||||
{
|
||||
break;
|
||||
}
|
||||
prev = in;
|
||||
block += 16;
|
||||
}
|
||||
#endif
|
||||
return block;
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace detail
|
||||
@@ -12604,6 +12676,64 @@ inline std::size_t valid_utf8_prefix(const unsigned char* data, std::size_t n) n
|
||||
return pos;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief whether [data, data+n) is valid UTF-8, for checking whole strings
|
||||
|
||||
The same answer as valid_utf8_prefix(data, n) == n. Short ASCII strings, the
|
||||
most common keys and values, are decided in one test, and long strings are
|
||||
checked 16 bytes at a time where the vector check is available.
|
||||
*/
|
||||
inline bool valid_utf8_string(const unsigned char* data, std::size_t n) noexcept
|
||||
{
|
||||
constexpr std::uint64_t high = 0x8080808080808080ull;
|
||||
if (n <= 16)
|
||||
{
|
||||
// two overlapping words cover 8 to 16 bytes, one partial word fewer
|
||||
std::uint64_t first = 0;
|
||||
std::uint64_t last = 0;
|
||||
if (n >= 8)
|
||||
{
|
||||
std::memcpy(&first, data, sizeof(first));
|
||||
std::memcpy(&last, data + n - 8, sizeof(last));
|
||||
}
|
||||
else if (n != 0)
|
||||
{
|
||||
std::memcpy(&first, data, n);
|
||||
}
|
||||
if (((first | last) & high) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#if JSON_SCAN_VECTOR_UTF8
|
||||
else if (n >= 32
|
||||
#if JSON_SCAN_SSSE3_DISPATCH
|
||||
&& cpu_has_ssse3()
|
||||
#endif
|
||||
)
|
||||
{
|
||||
const unsigned char* block = vector_utf8_valid_blocks(data, data + n);
|
||||
// the rest from the start of a sequence that may be cut at block
|
||||
for (int i = 1; i <= 3 && block - i >= data; ++i)
|
||||
{
|
||||
const unsigned char c = block[-i];
|
||||
if (c < 0x80)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (c >= 0xC0)
|
||||
{
|
||||
block -= i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const auto checked = static_cast<std::size_t>(block - data);
|
||||
return checked + valid_utf8_prefix(block, n - checked) == n;
|
||||
}
|
||||
#endif
|
||||
return valid_utf8_prefix(data, n) == n;
|
||||
}
|
||||
|
||||
#if JSON_SCAN_VECTOR_UTF8
|
||||
/// the end of scan_string_vector from block, where the vector loop stopped
|
||||
/// (ill-formed UTF-8, or fewer than 16 bytes left): one byte or sequence at a
|
||||
@@ -22069,6 +22199,19 @@ class binary_reader
|
||||
return get_bytes(len, "string", result);
|
||||
}
|
||||
|
||||
/// whether @a s is valid UTF-8: ASCII eight bytes at a time, the rest
|
||||
/// sequence by sequence (as the binary writers check strings)
|
||||
static bool is_valid_utf8_string(const string_t& s, std::true_type /*is_byte*/) noexcept
|
||||
{
|
||||
return valid_utf8_string(reinterpret_cast<const unsigned char*>(s.data()), s.size());
|
||||
}
|
||||
|
||||
/// whether @a s, of a character type wider than a byte, is valid UTF-8
|
||||
static bool is_valid_utf8_string(const string_t& s, std::false_type /*is_byte*/) noexcept
|
||||
{
|
||||
return is_valid_utf8(s);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief validate a decoded text string (value or object key) against @ref error_handler
|
||||
|
||||
@@ -22088,7 +22231,16 @@ class binary_reader
|
||||
*/
|
||||
bool check_string_utf8(string_t& result, const char* context)
|
||||
{
|
||||
if (error_handler == error_handler_t::keep || is_valid_utf8(result))
|
||||
return error_handler == error_handler_t::keep || check_string_utf8_handler(result, context);
|
||||
}
|
||||
|
||||
/// @ref check_string_utf8 for an error handler other than @ref
|
||||
/// error_handler_t::keep; out of line, so that reading strings without a
|
||||
/// check does not pay for the code of the check
|
||||
JSON_HEDLEY_NEVER_INLINE
|
||||
bool check_string_utf8_handler(string_t& result, const char* context)
|
||||
{
|
||||
if (is_valid_utf8_string(result, std::integral_constant<bool, sizeof(typename string_t::value_type) == 1>()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -30306,6 +30458,25 @@ class binary_writer
|
||||
}
|
||||
}
|
||||
|
||||
/// write the head and the characters of @a value, at most
|
||||
/// max_text_in_reservation of them, in one reservation
|
||||
JSON_HEDLEY_ALWAYS_INLINE void write_cbor_string_reserved(const string_t& value)
|
||||
{
|
||||
CharType* const out = oa.reserve(value.size() + 2);
|
||||
CharType* p = out;
|
||||
if (value.size() <= 0x17)
|
||||
{
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(0x60 + value.size()));
|
||||
}
|
||||
else
|
||||
{
|
||||
*p++ = to_char_type(0x78);
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(value.size()));
|
||||
}
|
||||
copy_characters(p, reinterpret_cast<const CharType*>(value.data()), value.size());
|
||||
oa.commit(static_cast<std::size_t>(p - out) + value.size());
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief write a CBOR text string
|
||||
|
||||
@@ -30327,19 +30498,7 @@ class binary_writer
|
||||
// the string is written as it is: head and characters in one reservation
|
||||
if (JSON_HEDLEY_LIKELY(value.size() <= max_text_in_reservation))
|
||||
{
|
||||
CharType* const out = oa.reserve(value.size() + 2);
|
||||
CharType* p = out;
|
||||
if (value.size() <= 0x17)
|
||||
{
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(0x60 + value.size()));
|
||||
}
|
||||
else
|
||||
{
|
||||
*p++ = to_char_type(0x78);
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(value.size()));
|
||||
}
|
||||
copy_characters(p, reinterpret_cast<const CharType*>(value.data()), value.size());
|
||||
oa.commit(static_cast<std::size_t>(p - out) + value.size());
|
||||
write_cbor_string_reserved(value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -30353,6 +30512,13 @@ class binary_writer
|
||||
JSON_HEDLEY_NEVER_INLINE
|
||||
void write_cbor_checked_string(const string_t& value, const BasicJsonType& context)
|
||||
{
|
||||
if (error_handler == error_handler_t::strict && value.size() <= max_text_in_reservation)
|
||||
{
|
||||
check_utf8(value, context);
|
||||
write_cbor_string_reserved(value);
|
||||
return;
|
||||
}
|
||||
|
||||
string_t storage;
|
||||
const string_t& sanitized = sanitize_utf8_for_write(value, context, storage);
|
||||
|
||||
@@ -30367,6 +30533,27 @@ class binary_writer
|
||||
// MsgPack //
|
||||
/////////////
|
||||
|
||||
/// write the head and the characters of @a value, at most
|
||||
/// max_text_in_reservation of them, in one reservation
|
||||
JSON_HEDLEY_ALWAYS_INLINE void write_msgpack_string_reserved(const string_t& value)
|
||||
{
|
||||
CharType* const out = oa.reserve(value.size() + 2);
|
||||
CharType* p = out;
|
||||
if (value.size() <= 31)
|
||||
{
|
||||
// fixstr
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(0xA0 | value.size()));
|
||||
}
|
||||
else
|
||||
{
|
||||
// str 8
|
||||
*p++ = to_char_type(0xD9);
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(value.size()));
|
||||
}
|
||||
copy_characters(p, reinterpret_cast<const CharType*>(value.data()), value.size());
|
||||
oa.commit(static_cast<std::size_t>(p - out) + value.size());
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief write a MessagePack str
|
||||
|
||||
@@ -30384,21 +30571,7 @@ class binary_writer
|
||||
// the string is written as it is: head and characters in one reservation
|
||||
if (JSON_HEDLEY_LIKELY(value.size() <= max_text_in_reservation))
|
||||
{
|
||||
CharType* const out = oa.reserve(value.size() + 2);
|
||||
CharType* p = out;
|
||||
if (value.size() <= 31)
|
||||
{
|
||||
// fixstr
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(0xA0 | value.size()));
|
||||
}
|
||||
else
|
||||
{
|
||||
// str 8
|
||||
*p++ = to_char_type(0xD9);
|
||||
*p++ = to_char_type(static_cast<std::uint8_t>(value.size()));
|
||||
}
|
||||
copy_characters(p, reinterpret_cast<const CharType*>(value.data()), value.size());
|
||||
oa.commit(static_cast<std::size_t>(p - out) + value.size());
|
||||
write_msgpack_string_reserved(value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -30411,6 +30584,13 @@ class binary_writer
|
||||
JSON_HEDLEY_NEVER_INLINE
|
||||
void write_msgpack_checked_string(const string_t& value, const BasicJsonType& context)
|
||||
{
|
||||
if (error_handler == error_handler_t::strict && value.size() <= max_text_in_reservation)
|
||||
{
|
||||
check_utf8(value, context);
|
||||
write_msgpack_string_reserved(value);
|
||||
return;
|
||||
}
|
||||
|
||||
string_t storage;
|
||||
const string_t& sanitized = sanitize_utf8_for_write(value, context, storage);
|
||||
|
||||
@@ -32020,10 +32200,9 @@ class binary_writer
|
||||
static void check_utf8(const string_t& s, const BasicJsonType& context)
|
||||
{
|
||||
const auto* data = reinterpret_cast<const unsigned char*>(s.data());
|
||||
const std::size_t valid = valid_utf8_prefix(data, s.size());
|
||||
if (JSON_HEDLEY_UNLIKELY(valid != s.size()))
|
||||
if (JSON_HEDLEY_UNLIKELY(!valid_utf8_string(data, s.size())))
|
||||
{
|
||||
throw_invalid_utf8(data, valid, context);
|
||||
throw_invalid_utf8(data, valid_utf8_prefix(data, s.size()), context);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Generated
+37
-157
@@ -1311,8 +1311,7 @@ struct parse_failure
|
||||
};
|
||||
|
||||
/// FloatType: the number_float_t of the document, whose overflow parse() rejects
|
||||
/// Store: false for accept(): the grammar, number, string and UTF-8 checks run as for parse(), but no node is written
|
||||
template<typename FloatType, bool Comments, bool TrailingCommas, bool NulIsEnd, bool Sentinel, bool Store>
|
||||
template<typename FloatType, bool Comments, bool TrailingCommas, bool NulIsEnd, bool Sentinel>
|
||||
class builder
|
||||
{
|
||||
public:
|
||||
@@ -1488,87 +1487,11 @@ class builder
|
||||
return p;
|
||||
}
|
||||
|
||||
/// the string from p, the first backslash, control character, ill-formed UTF-8 byte, or the end, checked and not
|
||||
/// decoded (accept() stores nothing, so the arena is not written): the checks, errors and error positions of
|
||||
/// slow_string(), for everything up to the closing quote
|
||||
NLOHMANN_VIEW_NOINLINE decoded check_string(const unsigned char* p)
|
||||
{
|
||||
const unsigned char* const end = m_end;
|
||||
for (;;)
|
||||
{
|
||||
if (p == end)
|
||||
{
|
||||
return failed(error_code::string_missing_quote, p);
|
||||
}
|
||||
const unsigned char c = *p;
|
||||
if (c == '"')
|
||||
{
|
||||
++p;
|
||||
return decoded{p, 0, 0};
|
||||
}
|
||||
if (c != '\\')
|
||||
{
|
||||
return failed(c < 0x20 ? error_code::string_control_character : error_code::string_utf8, p);
|
||||
}
|
||||
++p;
|
||||
if (p == end)
|
||||
{
|
||||
return failed(error_code::string_missing_quote, p);
|
||||
}
|
||||
const unsigned char d = *p++;
|
||||
if (d == 'u')
|
||||
{
|
||||
const int cp = hex4(p); // (moves p past the four digits)
|
||||
if (NLOHMANN_VIEW_UNLIKELY(cp < 0))
|
||||
{
|
||||
return failed(error_code::string_unicode_hex, p);
|
||||
}
|
||||
if (NLOHMANN_VIEW_UNLIKELY((cp & 0xF800) == 0xD800)) // a surrogate
|
||||
{
|
||||
if (cp >= 0xDC00)
|
||||
{
|
||||
return failed(error_code::string_surrogate_low, p);
|
||||
}
|
||||
if (end - p < 2 || p[0] != '\\' || p[1] != 'u')
|
||||
{
|
||||
return failed(error_code::string_surrogate_high, p);
|
||||
}
|
||||
p += 2;
|
||||
const int lo = hex4(p); // (moves p past the four digits)
|
||||
if (lo < 0)
|
||||
{
|
||||
return failed(error_code::string_unicode_hex, p);
|
||||
}
|
||||
if (lo < 0xDC00 || lo > 0xDFFF)
|
||||
{
|
||||
return failed(error_code::string_surrogate_high, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!(d == '"' || d == '\\' || d == '/' || d == 'b' || d == 'f' || d == 'n' || d == 'r' || d == 't'))
|
||||
{
|
||||
--p;
|
||||
return failed(error_code::string_escape, p);
|
||||
}
|
||||
if (p != end && *p == '\\')
|
||||
{
|
||||
continue; // (escapes in a row: no run of text between them)
|
||||
}
|
||||
p = scan_string_run(p, end); // the text up to the next backslash, quote, control character, or ill-formed UTF-8
|
||||
}
|
||||
}
|
||||
|
||||
/// s points to the start of the string, p to a backslash, a control
|
||||
/// character, an ill-formed UTF-8 byte, or the end: decode the string into
|
||||
/// the arena
|
||||
NLOHMANN_VIEW_NOINLINE decoded slow_string(const unsigned char* s, const unsigned char* p)
|
||||
{
|
||||
if (!enabled(Store))
|
||||
{
|
||||
// (accept(): the string is checked, not decoded)
|
||||
static_cast<void>(s);
|
||||
return check_string(p);
|
||||
}
|
||||
// single-character escapes; 0: invalid (and 'u', handled separately)
|
||||
static const std::array<char, 128> simple_escape =
|
||||
{
|
||||
@@ -1929,13 +1852,10 @@ run_found:
|
||||
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE bool run()
|
||||
{
|
||||
if (enabled(Store))
|
||||
{
|
||||
cold.m_doc.reserve(estimate_nodes(reinterpret_cast<const char*>(b), static_cast<std::size_t>(e - b)));
|
||||
base = cold.m_doc.nodes;
|
||||
out = base;
|
||||
cap = base + cold.m_doc.node_capacity;
|
||||
}
|
||||
cold.m_doc.reserve(estimate_nodes(reinterpret_cast<const char*>(b), static_cast<std::size_t>(e - b)));
|
||||
base = cold.m_doc.nodes;
|
||||
out = base;
|
||||
cap = base + cold.m_doc.node_capacity;
|
||||
|
||||
if (e - p >= 3 && p[0] == 0xEF && p[1] == 0xBB && p[2] == 0xBF)
|
||||
{
|
||||
@@ -2119,10 +2039,7 @@ close_object:
|
||||
// an array pays nothing for this)
|
||||
if (NLOHMANN_VIEW_UNLIKELY(cur_count >= document_data::index_min_members))
|
||||
{
|
||||
if (enabled(Store))
|
||||
{
|
||||
cold.note_large_object(cur_idx);
|
||||
}
|
||||
cold.note_large_object(cur_idx);
|
||||
}
|
||||
|
||||
close_container:
|
||||
@@ -2282,12 +2199,6 @@ indent_done:
|
||||
/// an integer's value); two stores on little-endian targets
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE node* push_node(node_kind k, std::uint8_t flags, std::uint16_t extra, std::size_t off, std::uint64_t second)
|
||||
{
|
||||
if (!enabled(Store))
|
||||
{
|
||||
// accept(): nothing is stored, so what computes the arguments is not run either. base and out stay
|
||||
// null: their difference, which open() takes, is 0.
|
||||
return out;
|
||||
}
|
||||
if (NLOHMANN_VIEW_UNLIKELY(out == cap))
|
||||
{
|
||||
const auto n = static_cast<std::size_t>(out - base);
|
||||
@@ -2346,12 +2257,9 @@ indent_done:
|
||||
|
||||
NLOHMANN_VIEW_ALWAYS_INLINE void close()
|
||||
{
|
||||
if (enabled(Store))
|
||||
{
|
||||
node& n = base[cur_idx];
|
||||
n.len = cur_count;
|
||||
n.next = static_cast<std::uint32_t>(out - base) - cur_idx;
|
||||
}
|
||||
node& n = base[cur_idx];
|
||||
n.len = cur_count;
|
||||
n.next = static_cast<std::uint32_t>(out - base) - cur_idx;
|
||||
if (--depth != 0)
|
||||
{
|
||||
if (NLOHMANN_VIEW_LIKELY(depth <= 64))
|
||||
@@ -2526,44 +2434,34 @@ indent_done:
|
||||
};
|
||||
|
||||
/// run the builder with compile-time options
|
||||
template<typename FloatType, bool Comments, bool TrailingCommas, bool NulIsEnd, bool Store>
|
||||
template<typename FloatType, bool Comments, bool TrailingCommas, bool NulIsEnd>
|
||||
inline bool build_with(document_data& d, const char* src, std::size_t size, bool sentinel, parse_failure& failure)
|
||||
{
|
||||
if (sentinel)
|
||||
{
|
||||
builder<FloatType, Comments, TrailingCommas, NulIsEnd, true, Store> bld(d, src, size);
|
||||
builder<FloatType, Comments, TrailingCommas, NulIsEnd, true> bld(d, src, size);
|
||||
const bool ok = bld.run();
|
||||
failure = bld.failure();
|
||||
return ok;
|
||||
}
|
||||
builder<FloatType, Comments, TrailingCommas, NulIsEnd, false, Store> bld(d, src, size);
|
||||
builder<FloatType, Comments, TrailingCommas, NulIsEnd, false> bld(d, src, size);
|
||||
const bool ok = bld.run();
|
||||
failure = bld.failure();
|
||||
return ok;
|
||||
}
|
||||
|
||||
/// sentinel: src[size] is readable and 0 (e.g. std::string); FloatType: the
|
||||
/// number_float_t of the document; validate_only: accept(), only the result is
|
||||
/// used
|
||||
/// number_float_t of the document
|
||||
template<typename FloatType, bool NulIsEnd>
|
||||
inline bool build(document_data& d, const char* src, std::size_t size, bool comments, bool trailing_commas, bool sentinel, bool validate_only,
|
||||
parse_failure& failure)
|
||||
inline bool build(document_data& d, const char* src, std::size_t size, bool comments, bool trailing_commas, bool sentinel, parse_failure& failure)
|
||||
{
|
||||
if (validate_only && !comments && !trailing_commas)
|
||||
{
|
||||
// accept() of plain JSON: nothing is stored. The other option sets
|
||||
// could skip the stores just as well; they keep the builder that
|
||||
// stores only so that there are two more instantiations of the parse
|
||||
// loop, not eight.
|
||||
return build_with<FloatType, false, false, NulIsEnd, false>(d, src, size, sentinel, failure);
|
||||
}
|
||||
if (comments)
|
||||
{
|
||||
return trailing_commas ? build_with<FloatType, true, true, NulIsEnd, true>(d, src, size, sentinel, failure)
|
||||
: build_with<FloatType, true, false, NulIsEnd, true>(d, src, size, sentinel, failure);
|
||||
return trailing_commas ? build_with<FloatType, true, true, NulIsEnd>(d, src, size, sentinel, failure)
|
||||
: build_with<FloatType, true, false, NulIsEnd>(d, src, size, sentinel, failure);
|
||||
}
|
||||
return trailing_commas ? build_with<FloatType, false, true, NulIsEnd, true>(d, src, size, sentinel, failure)
|
||||
: build_with<FloatType, false, false, NulIsEnd, true>(d, src, size, sentinel, failure);
|
||||
return trailing_commas ? build_with<FloatType, false, true, NulIsEnd>(d, src, size, sentinel, failure)
|
||||
: build_with<FloatType, false, false, NulIsEnd>(d, src, size, sentinel, failure);
|
||||
}
|
||||
|
||||
} // namespace view
|
||||
@@ -10203,7 +10101,7 @@ class basic_json_document
|
||||
const bool ignore_trailing_commas = false)
|
||||
{
|
||||
basic_json_document d;
|
||||
d.read_range(first, last, error_mode(allow_exceptions), ignore_comments, ignore_trailing_commas);
|
||||
d.read_range(first, last, allow_exceptions, ignore_comments, ignore_trailing_commas);
|
||||
return d;
|
||||
}
|
||||
|
||||
@@ -10218,7 +10116,7 @@ class basic_json_document
|
||||
basic_json_document d;
|
||||
std::string buf;
|
||||
append_input(buf, std::forward<InputType>(input));
|
||||
d.build_owned(std::move(buf), error_mode(allow_exceptions), ignore_comments, ignore_trailing_commas);
|
||||
d.build_owned(std::move(buf), allow_exceptions, ignore_comments, ignore_trailing_commas);
|
||||
return d;
|
||||
}
|
||||
|
||||
@@ -10231,8 +10129,7 @@ class basic_json_document
|
||||
static bool accept(InputType&& input, const bool ignore_comments = false, const bool ignore_trailing_commas = false)
|
||||
{
|
||||
basic_json_document d;
|
||||
d.read_kind(std::forward<InputType>(input), on_error::validate, ignore_comments, ignore_trailing_commas,
|
||||
std::integral_constant<detail::view::input_kind, detail::view::classify_input<InputType>::value> {});
|
||||
d.read(std::forward<InputType>(input), false, ignore_comments, ignore_trailing_commas);
|
||||
return !d.is_discarded();
|
||||
}
|
||||
|
||||
@@ -10248,7 +10145,7 @@ class basic_json_document
|
||||
const bool ignore_comments = false,
|
||||
const bool ignore_trailing_commas = false)
|
||||
{
|
||||
read_kind(std::forward<InputType>(input), error_mode(allow_exceptions), ignore_comments, ignore_trailing_commas,
|
||||
read_kind(std::forward<InputType>(input), allow_exceptions, ignore_comments, ignore_trailing_commas,
|
||||
std::integral_constant<detail::view::input_kind, detail::view::classify_input<InputType>::value> {});
|
||||
}
|
||||
|
||||
@@ -10607,22 +10504,6 @@ class basic_json_document
|
||||
return idx;
|
||||
}
|
||||
|
||||
/// what a failed parse does
|
||||
enum class on_error : std::uint8_t
|
||||
{
|
||||
discard, ///< the document is discarded
|
||||
raise, ///< parse_error is thrown
|
||||
validate ///< accept(): the document is discarded, and no node is stored while parsing
|
||||
};
|
||||
|
||||
static on_error error_mode(bool allow_exceptions) noexcept
|
||||
{
|
||||
return allow_exceptions ? on_error::raise : on_error::discard;
|
||||
}
|
||||
|
||||
/// NulIsEnd of the builder: a null byte ends the input unless strict_nul_handling
|
||||
static constexpr bool nul_is_end = !detail::abi_config::strict_nul_handling;
|
||||
|
||||
/// create the storage (sized for the input) on first use
|
||||
void ensure_data(const char* src, std::size_t size)
|
||||
{
|
||||
@@ -10634,7 +10515,7 @@ class basic_json_document
|
||||
|
||||
/// parse a buffer the document takes ownership of (it replaces the
|
||||
/// previous owned copy)
|
||||
void build_owned(std::string&& buf, on_error allow_exceptions, bool comments, bool trailing_commas)
|
||||
void build_owned(std::string&& buf, bool allow_exceptions, bool comments, bool trailing_commas)
|
||||
{
|
||||
ensure_data(buf.data(), buf.size());
|
||||
m_data->owned = std::move(buf);
|
||||
@@ -10644,7 +10525,7 @@ class basic_json_document
|
||||
/// parse a copy of the input; the copy is appended to the cleared owned
|
||||
/// buffer, so that its capacity is reused
|
||||
template<typename T>
|
||||
void build_copy(T&& input, on_error allow_exceptions, bool comments, bool trailing_commas)
|
||||
void build_copy(T&& input, bool allow_exceptions, bool comments, bool trailing_commas)
|
||||
{
|
||||
if (!m_data)
|
||||
{
|
||||
@@ -10665,7 +10546,7 @@ class basic_json_document
|
||||
}
|
||||
|
||||
/// sentinel: src[size] is readable and 0 (std::string, C strings)
|
||||
void build(const char* src, std::size_t size, on_error allow_exceptions, bool comments, bool trailing_commas, bool owned, bool sentinel)
|
||||
void build(const char* src, std::size_t size, bool allow_exceptions, bool comments, bool trailing_commas, bool owned, bool sentinel)
|
||||
{
|
||||
ensure_data(src, size);
|
||||
document_data& d = *m_data;
|
||||
@@ -10692,8 +10573,7 @@ class basic_json_document
|
||||
}
|
||||
else
|
||||
{
|
||||
ok = detail::view::build<typename BasicJsonType::number_float_t, nul_is_end>(d, src, size, comments, trailing_commas, sentinel,
|
||||
allow_exceptions == on_error::validate, failure);
|
||||
ok = detail::view::build < typename BasicJsonType::number_float_t, !detail::abi_config::strict_nul_handling > (d, src, size, comments, trailing_commas, sentinel, failure);
|
||||
}
|
||||
if (NLOHMANN_VIEW_LIKELY(ok))
|
||||
{
|
||||
@@ -10706,7 +10586,7 @@ class basic_json_document
|
||||
return;
|
||||
}
|
||||
std::vector<std::uint32_t>().swap(d.large_objects);
|
||||
if (allow_exceptions == on_error::raise)
|
||||
if (allow_exceptions)
|
||||
{
|
||||
detail::view::throw_parse_failure<BasicJsonType>(failure, src, size, comments, trailing_commas);
|
||||
}
|
||||
@@ -10714,13 +10594,13 @@ class basic_json_document
|
||||
|
||||
// --- input dispatch (see detail::view::input_kind) ---
|
||||
|
||||
void read_kind(std::string&& s, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::move_string> /*unused*/)
|
||||
void read_kind(std::string&& s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::move_string> /*unused*/)
|
||||
{
|
||||
build_owned(std::move(s), ae, c, tc);
|
||||
}
|
||||
|
||||
template<typename CharT>
|
||||
void read_kind(CharT* s, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::c_string> /*unused*/)
|
||||
void read_kind(CharT* s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::c_string> /*unused*/)
|
||||
{
|
||||
if (s == nullptr)
|
||||
{
|
||||
@@ -10734,7 +10614,7 @@ class basic_json_document
|
||||
}
|
||||
|
||||
template<typename Array>
|
||||
void read_kind(Array& a, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::char_array> /*unused*/)
|
||||
void read_kind(Array& a, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::char_array> /*unused*/)
|
||||
{
|
||||
using CharT = typename std::remove_cv<typename std::remove_extent<Array>::type>::type;
|
||||
const std::size_t n = std::extent<Array>::value;
|
||||
@@ -10745,7 +10625,7 @@ class basic_json_document
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_kind(const T& s, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::borrow_range> /*unused*/)
|
||||
void read_kind(const T& s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::borrow_range> /*unused*/)
|
||||
{
|
||||
// std::basic_string guarantees data()[size()] == 0: use it as sentinel
|
||||
const auto size = static_cast<std::size_t>(s.size());
|
||||
@@ -10754,13 +10634,13 @@ class basic_json_document
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_kind(const T& s, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::copy_range> /*unused*/)
|
||||
void read_kind(const T& s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::copy_range> /*unused*/)
|
||||
{
|
||||
build_copy(s, ae, c, tc);
|
||||
}
|
||||
|
||||
template<typename CharT>
|
||||
void read_kind(CharT* s, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::wide_c_string> /*unused*/)
|
||||
void read_kind(CharT* s, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::wide_c_string> /*unused*/)
|
||||
{
|
||||
if (s == nullptr)
|
||||
{
|
||||
@@ -10772,13 +10652,13 @@ class basic_json_document
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read_kind(T&& input, on_error ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::adapter> /*unused*/)
|
||||
void read_kind(T&& input, bool ae, bool c, bool tc, std::integral_constant<input_kind, input_kind::adapter> /*unused*/)
|
||||
{
|
||||
build_copy(std::forward<T>(input), ae, c, tc);
|
||||
}
|
||||
|
||||
template<typename IteratorType>
|
||||
void read_range(IteratorType first, IteratorType last, on_error ae, bool c, bool tc)
|
||||
void read_range(IteratorType first, IteratorType last, bool ae, bool c, bool tc)
|
||||
{
|
||||
using value_type = typename std::remove_cv<typename std::iterator_traits<IteratorType>::value_type>::type;
|
||||
// borrow the range where the library's input adapter scans it as one
|
||||
@@ -10789,14 +10669,14 @@ class basic_json_document
|
||||
}
|
||||
|
||||
template<typename IteratorType>
|
||||
void read_range_impl(IteratorType first, IteratorType last, on_error ae, bool c, bool tc, std::true_type /*contiguous bytes*/)
|
||||
void read_range_impl(IteratorType first, IteratorType last, bool ae, bool c, bool tc, std::true_type /*contiguous bytes*/)
|
||||
{
|
||||
const auto size = static_cast<std::size_t>(std::distance(first, last));
|
||||
build(size == 0 ? "" : reinterpret_cast<const char*>(&*first), size, ae, c, tc, false, size == 0); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||
}
|
||||
|
||||
template<typename IteratorType>
|
||||
void read_range_impl(IteratorType first, IteratorType last, on_error ae, bool c, bool tc, std::false_type /*other*/)
|
||||
void read_range_impl(IteratorType first, IteratorType last, bool ae, bool c, bool tc, std::false_type /*other*/)
|
||||
{
|
||||
build_copy(detail::view::make_adapter_input(detail::input_adapter(first, last)), ae, c, tc);
|
||||
}
|
||||
|
||||
@@ -403,6 +403,7 @@ TEST_CASE("guard page")
|
||||
CHECK(nlohmann::detail::find_string_special(p, n) <= n);
|
||||
CHECK(nlohmann::detail::find_ascii_copyable_run(p, n) <= n);
|
||||
CHECK(nlohmann::detail::valid_utf8_prefix(p, n) <= n);
|
||||
CHECK(nlohmann::detail::valid_utf8_string(p, n) == (nlohmann::detail::valid_utf8_prefix(p, n) == n));
|
||||
#if defined(JSON_USE_SIMDUTF) && defined(JSON_HAS_CPP_17)
|
||||
CHECK(nlohmann::detail::find_string_delimiter(p, n) <= n);
|
||||
#endif
|
||||
|
||||
@@ -59,8 +59,7 @@ built build(const std::string& text, bool comments, bool trailing_commas, bool s
|
||||
r.copy.assign(text.begin(), text.end());
|
||||
src = r.copy.data();
|
||||
}
|
||||
constexpr bool nul_is_end = !nlohmann::detail::abi_config::strict_nul_handling;
|
||||
r.ok = nlohmann::detail::view::build<FloatType, nul_is_end>(*r.data, src, text.size(), comments, trailing_commas, sentinel, false, r.failure);
|
||||
r.ok = nlohmann::detail::view::build < FloatType, !nlohmann::detail::abi_config::strict_nul_handling > (*r.data, src, text.size(), comments, trailing_commas, sentinel, r.failure);
|
||||
r.data->src = src;
|
||||
r.data->base[0] = src;
|
||||
r.data->base[1] = r.data->arena.data();
|
||||
@@ -173,22 +172,6 @@ void check_string(const std::string& content)
|
||||
}
|
||||
}
|
||||
|
||||
// accept() stores no nodes: it must accept and reject what json::accept does, for all options and for inputs with
|
||||
// and without a terminating NUL
|
||||
void check_accept(const std::string& text)
|
||||
{
|
||||
CAPTURE(text)
|
||||
const std::vector<char> bytes(text.begin(), text.end());
|
||||
for (int options = 0; options < 4; ++options)
|
||||
{
|
||||
const bool comments = (options & 1) != 0;
|
||||
const bool trailing_commas = (options & 2) != 0;
|
||||
const bool accepted = json::accept(text, comments, trailing_commas);
|
||||
CHECK(nlohmann::json_document::accept(text, comments, trailing_commas) == accepted);
|
||||
CHECK(nlohmann::json_document::accept(bytes, comments, trailing_commas) == accepted);
|
||||
}
|
||||
}
|
||||
|
||||
// a small deterministic generator of documents
|
||||
struct generator
|
||||
{
|
||||
@@ -440,133 +423,6 @@ TEST_CASE("json_view builder")
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("accept() without an index")
|
||||
{
|
||||
for (const char* text :
|
||||
{
|
||||
"", " ", "\n", "null", "nul", "true", "[", "]", "[]", "{}", "[1,2,3]", "[1,2,3", "[1,2,3,]", "{\"a\":1,}", "{\"a\"}", "{\"a\":}",
|
||||
"[1] x", "[1][2]", "\xEF\xBB\xBF[1]", "[\"a\\", "[\"\\u12\"]", "[\"\\ud800\"]", "[\"\x01\"]", "[\"\xff\"]", "[01]", "[1.]",
|
||||
"[.5]", "[-]", "[1e]", "// c\n[1]", "/* c */ {\"a\" /* x */ : 1}", "/* c", "[1] // c", "1e400", "-1e400", "123456789012345678901234567890",
|
||||
"{\"a\":1,\"a\":2}", "[[[]]]", "\"\\u00e4\\n\\ud83d\\ude00\""
|
||||
})
|
||||
{
|
||||
check_accept(text);
|
||||
}
|
||||
// a null byte inside the text, and after it
|
||||
check_accept(std::string("[1,\0]", 5));
|
||||
check_accept(std::string("[1]\0", 4));
|
||||
check_accept(std::string("\0", 1));
|
||||
|
||||
// generated documents and damaged copies, as for the builder above
|
||||
generator g;
|
||||
for (int i = 0; i < 2000; ++i)
|
||||
{
|
||||
std::string text;
|
||||
g.value(text, 0);
|
||||
check_accept(text);
|
||||
std::string damaged = text;
|
||||
const auto at = static_cast<std::size_t>(g.r(static_cast<int>(damaged.size())));
|
||||
static const char replacements[] = {'x', '"', '\\', ',', ':', ']', '}', '[', '{', '1', '-', '.', 'e', '\0', '\n', '/'}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
||||
damaged[at] = replacements[g.r(16)];
|
||||
check_accept(damaged);
|
||||
check_accept(text.substr(0, at));
|
||||
}
|
||||
|
||||
// nesting beyond the 64 levels that are kept on the builder's own stack, closed and not closed
|
||||
for (const std::size_t depth :
|
||||
{
|
||||
63, 64, 65, 66, 200, 1000
|
||||
})
|
||||
{
|
||||
check_accept(std::string(depth, '[') + std::string(depth, ']'));
|
||||
check_accept(std::string(depth, '[') + std::string(depth - 1, ']'));
|
||||
std::string objects;
|
||||
for (std::size_t i = 0; i < depth; ++i)
|
||||
{
|
||||
objects += "{\"k\":";
|
||||
}
|
||||
objects += "1" + std::string(depth, '}');
|
||||
check_accept(objects);
|
||||
check_accept(objects.substr(0, objects.size() - 1));
|
||||
}
|
||||
|
||||
// objects with enough members to get a hash index when they are indexed (and duplicate keys)
|
||||
std::string large = "{";
|
||||
for (int i = 0; i < 300; ++i)
|
||||
{
|
||||
large += (i == 0 ? "\"k" : ",\"k") + std::to_string(i % 200) + "\":" + std::to_string(i);
|
||||
}
|
||||
check_accept(large + "}");
|
||||
check_accept(large);
|
||||
check_accept("[" + large + "},{" + large.substr(1) + "}]");
|
||||
|
||||
// the overflow check uses the floating-point type of the document
|
||||
using float_json = nlohmann::basic_json<std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, float>;
|
||||
for (const char* text :
|
||||
{
|
||||
"1e39", "-1e39", "3.4028235e38", "3.4028236e38", "[1.5e38, 3.5e38]", "{\"a\": 1e-50, \"b\": 1e39}", "340282356779733661637539395458142568448"
|
||||
})
|
||||
{
|
||||
CAPTURE(text)
|
||||
CHECK(nlohmann::basic_json_document<float_json>::accept(text) == float_json::accept(text));
|
||||
}
|
||||
|
||||
// strings are checked and not decoded: runs of text of every length from 0 to 40 between valid and invalid
|
||||
// escapes, UTF-8 sequences and control characters, in values and in keys, cut near the end of the text
|
||||
const std::array<const char*, 9> valid = {{"\\n", "\\\"", "\\\\", "\\/", "\\t", "\\u00e4", "\\u20ac", "\\ud83d\\ude00", "\\u0000"}};
|
||||
const std::array<const char*, 12> invalid = {{"\\x", "\\ud800", "\\udc00", "\\u12G4", "\\u12", "\\ud83dx", "\\ud83d\\u0041", "\\ud83d\\ud83d", "\\", "\x01", "\x1f", "\x7f"}};
|
||||
const std::array<const char*, 6> utf8 = {{"\xc3\xa4", "\xe2\x82\xac", "\xf0\x9f\x98\x80", "\xc3", "\xff", "\x80"}};
|
||||
std::mt19937 rng(2029); // NOLINT(cert-msc32-c,cert-msc51-cpp,bugprone-random-generator-seed): reproducible
|
||||
for (std::size_t run = 0; run <= 40; ++run)
|
||||
{
|
||||
for (int i = 0; i < 30; ++i)
|
||||
{
|
||||
std::string content = std::string(rng() % 20, 'a');
|
||||
const auto pieces = 1 + (rng() % 6);
|
||||
for (unsigned k = 0; k < pieces; ++k)
|
||||
{
|
||||
content += valid[rng() % valid.size()];
|
||||
content += std::string(k == 0 ? run : rng() % 41, static_cast<char>('b' + (rng() % 20)));
|
||||
switch (rng() % 8)
|
||||
{
|
||||
case 0:
|
||||
content += utf8[rng() % 3];
|
||||
break;
|
||||
case 1:
|
||||
content += utf8[rng() % utf8.size()];
|
||||
break;
|
||||
case 2:
|
||||
content += invalid[rng() % invalid.size()];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (const std::string& text :
|
||||
{
|
||||
"[\"" + content + "\"]", "{\"" + content + "\":1}", "[\"" + content, "[\"" + content.substr(0, content.size() - (rng() % 4)) + "\"]"
|
||||
})
|
||||
{
|
||||
check_accept(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("escapes in a row")
|
||||
{
|
||||
for (const char* content :
|
||||
{
|
||||
"\\n", "\\n\\t\\\\", "\\u00e9\\u00e9\\u4e2d", "\\u00e9\\u00e", "\\u00e9\\", "\\n\\q", "\\n\\ud83d\\ude00\\n",
|
||||
"\\ud83d\\ude00\\ud83d", "\\n\\n\\n abc \\n\\n", "\\n\x01", "\\n\xC3\x28", "\\n\\"
|
||||
})
|
||||
{
|
||||
check_accept(std::string("[\"") + content + "\"]");
|
||||
check_accept(std::string("[\"") + content);
|
||||
check_accept(std::string("{\"") + content + "\":1}");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("test files")
|
||||
{
|
||||
for (const char* name :
|
||||
|
||||
@@ -13,6 +13,7 @@ using nlohmann::json;
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include "make_test_data_available.hpp"
|
||||
#include "test_utils.hpp"
|
||||
@@ -1631,3 +1632,40 @@ TEST_CASE("Unicode RFC 3629")
|
||||
}
|
||||
|
||||
DOCTEST_CLANG_SUPPRESS_WARNING_POP
|
||||
|
||||
TEST_CASE("UTF-8 check of whole strings")
|
||||
{
|
||||
// valid_utf8_string() decides short strings with a word test and long ones
|
||||
// 16 bytes at a time where a vector check is available; it must agree with
|
||||
// the byte-wise check for every mix of ASCII, sequences of each length,
|
||||
// ill-formed and truncated sequences, at every length and offset
|
||||
const char* const pieces[] =
|
||||
{
|
||||
"a", "\x7F", "\xC3\xA9", "\xE2\x82\xAC", "\xE0\xA0\x80", "\xEF\xBF\xBF", "\xF0\x9F\x98\x80", "\xF4\x8F\xBF\xBF",
|
||||
"\x80", "\xC0\xAF", "\xC3", "\xE2\x82", "\xED\xA0\x80", "\xF0\x9F", "\xF4\x90\x80\x80", "\xFF"
|
||||
};
|
||||
// std::mt19937's sequence is fixed by the standard; no distribution is used
|
||||
std::mt19937 rng(5);
|
||||
std::size_t valid = 0;
|
||||
for (int i = 0; i < 100000; ++i)
|
||||
{
|
||||
std::string s;
|
||||
const std::size_t length = rng() % 100;
|
||||
const std::uint32_t ascii = rng() % 101;
|
||||
while (s.size() < length)
|
||||
{
|
||||
// mostly well-formed pieces (the first eight), so that long valid
|
||||
// strings are common
|
||||
s += (rng() % 100 < ascii) ? std::string("x") : std::string(pieces[rng() % (rng() % 8 == 0 ? 16 : 8)]);
|
||||
}
|
||||
const auto* data = reinterpret_cast<const unsigned char*>(s.data());
|
||||
const bool expected = nlohmann::detail::is_valid_utf8(s);
|
||||
valid += expected ? 1 : 0;
|
||||
CAPTURE(s)
|
||||
CHECK(nlohmann::detail::valid_utf8_string(data, s.size()) == expected);
|
||||
CHECK((nlohmann::detail::valid_utf8_prefix(data, s.size()) == s.size()) == expected);
|
||||
}
|
||||
// both outcomes are common enough to be tested
|
||||
CHECK(valid > 20000);
|
||||
CHECK(valid < 80000);
|
||||
}
|
||||
Reference in new issue
Block a user