Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6bf3e611c | ||
|
|
e32051125f | ||
|
|
8c1421d493 | ||
|
|
baba5db36f | ||
|
|
07876e2292 | ||
|
|
6d8d2df367 | ||
|
|
6aa10fd343 | ||
|
|
98cfd55317 | ||
|
|
e96469f783 | ||
|
|
3720e4b3aa |
No files matched your search
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <algorithm> // remove, fill, find, none_of, min
|
#include <algorithm> // copy, none_of, min, search
|
||||||
#include <array> // array
|
#include <array> // array
|
||||||
#include <clocale> // localeconv, lconv
|
#include <clocale> // localeconv, lconv
|
||||||
#include <cmath> // isfinite
|
#include <cmath> // isfinite
|
||||||
@@ -2080,31 +2080,35 @@ class serializer
|
|||||||
// check if the buffer was large enough
|
// check if the buffer was large enough
|
||||||
JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
|
JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
|
||||||
|
|
||||||
// look up the locale's thousands separator and decimal point now,
|
// look up the locale's decimal point now, matching what
|
||||||
// matching what snprintf_float() just used (see lexer::get_decimal_point())
|
// snprintf_float() just used (see get_decimal_point() in
|
||||||
|
// number_parse.hpp); it is a string, which can be longer than one
|
||||||
|
// byte (U+066B in ps_AF, #5993).
|
||||||
|
// A thousands separator needs no handling: %g never groups digits.
|
||||||
const auto* loc = std::localeconv();
|
const auto* loc = std::localeconv();
|
||||||
JSON_ASSERT(loc != nullptr);
|
JSON_ASSERT(loc != nullptr);
|
||||||
const char thousands_sep = (loc->thousands_sep == nullptr) ? '\0' : *loc->thousands_sep;
|
const char* const decimal_point = (loc->decimal_point == nullptr) ? "" : loc->decimal_point;
|
||||||
const char decimal_point = (loc->decimal_point == nullptr) ? '\0' : *loc->decimal_point;
|
const std::size_t decimal_point_len = std::char_traits<char>::length(decimal_point);
|
||||||
|
|
||||||
// erase thousands separators
|
|
||||||
if (thousands_sep != '\0')
|
|
||||||
{
|
|
||||||
// NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
|
|
||||||
const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
|
|
||||||
std::fill(end, number_buffer.end(), '\0');
|
|
||||||
JSON_ASSERT((end - number_buffer.begin()) <= len);
|
|
||||||
len = (end - number_buffer.begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert decimal point to '.'
|
// convert decimal point to '.'
|
||||||
if (decimal_point != '\0' && decimal_point != '.')
|
if ((decimal_point_len > 1) || (decimal_point_len == 1 && decimal_point[0] != '.'))
|
||||||
{
|
{
|
||||||
// NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
|
const auto first = number_buffer.begin();
|
||||||
const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
|
const auto last = first + len;
|
||||||
if (dec_pos != number_buffer.end())
|
// NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::search returns an iterator, see https://github.com/nlohmann/json/issues/3081
|
||||||
|
const auto dec_pos = std::search(first, last, decimal_point, decimal_point + decimal_point_len);
|
||||||
|
if (dec_pos != last)
|
||||||
{
|
{
|
||||||
*dec_pos = '.';
|
*dec_pos = '.';
|
||||||
|
if (decimal_point_len > 1)
|
||||||
|
{
|
||||||
|
// move the fraction and exponent behind the '.' (std::copy
|
||||||
|
// allows the overlap, as the destination is before the source)
|
||||||
|
// NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::copy returns an iterator, see https://github.com/nlohmann/json/issues/3081
|
||||||
|
const auto end = std::copy(dec_pos + static_cast<std::ptrdiff_t>(decimal_point_len), last, dec_pos + 1);
|
||||||
|
*end = '\0';
|
||||||
|
len = end - first;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1168,20 +1168,20 @@ indent_done:
|
|||||||
if (negative)
|
if (negative)
|
||||||
{
|
{
|
||||||
++p;
|
++p;
|
||||||
|
if (NLOHMANN_VIEW_UNLIKELY(cur() < '0' || cur() > '9'))
|
||||||
|
{
|
||||||
|
return fail(error_code::number_after_minus);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const unsigned char* const int_start = p;
|
const unsigned char* const int_start = p;
|
||||||
if (cur() == '0')
|
if (cur() == '0')
|
||||||
{
|
{
|
||||||
++p;
|
++p;
|
||||||
}
|
}
|
||||||
else if (!negative || NLOHMANN_VIEW_LIKELY(cur() >= '1' && cur() <= '9'))
|
|
||||||
{
|
|
||||||
// (without a sign, the dispatch saw a digit)
|
|
||||||
p = skip_digits(p + 1, e);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return fail(error_code::number_after_minus);
|
// (a digit: the dispatch or the check after the sign saw it)
|
||||||
|
p = skip_digits(p + 1, e);
|
||||||
}
|
}
|
||||||
const auto int_digits = static_cast<std::size_t>(p - int_start);
|
const auto int_digits = static_cast<std::size_t>(p - int_start);
|
||||||
std::size_t frac_digits = 0;
|
std::size_t frac_digits = 0;
|
||||||
|
|||||||
@@ -230,8 +230,7 @@ void value_range(const document_data& d, const node* n, std::size_t& start, std:
|
|||||||
|
|
||||||
/// the context basic_json puts in front of the message of an exception about
|
/// the context basic_json puts in front of the message of an exception about
|
||||||
/// a value: "(/path) " with JSON_DIAGNOSTICS (none for the root), then
|
/// a value: "(/path) " with JSON_DIAGNOSTICS (none for the root), then
|
||||||
/// "(bytes a-b) " with JSON_DIAGNOSTIC_POSITIONS (also for the root); with
|
/// "(bytes a-b) " with JSON_DIAGNOSTIC_POSITIONS (also for the root)
|
||||||
/// JSON_DIAGNOSTICS, nothing for a value that is no longer part of the document
|
|
||||||
///
|
///
|
||||||
/// This and the throw helpers that take an error_site are templates, so that
|
/// This and the throw helpers that take an error_site are templates, so that
|
||||||
/// the path and range code is instantiated only where an exception can be
|
/// the path and range code is instantiated only where an exception can be
|
||||||
@@ -252,12 +251,10 @@ std::string diagnostics(const error_site site)
|
|||||||
std::string context;
|
std::string context;
|
||||||
#if JSON_DIAGNOSTICS
|
#if JSON_DIAGNOSTICS
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
// a value that is no longer part of the document has no path, and its
|
|
||||||
// node still holds the bytes it had in the source: no context at all
|
|
||||||
const bool found = edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens);
|
const bool found = edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens);
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
{
|
||||||
// context is still empty; returning it keeps the copy elided (-Wnrvo)
|
// a value that is not found has no path, and with it no bytes
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
if (!tokens.empty())
|
if (!tokens.empty())
|
||||||
|
|||||||
Generated
+23
-19
@@ -33446,7 +33446,7 @@ NLOHMANN_JSON_NAMESPACE_END
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include <algorithm> // remove, fill, find, none_of, min
|
#include <algorithm> // copy, none_of, min, search
|
||||||
#include <array> // array
|
#include <array> // array
|
||||||
#include <clocale> // localeconv, lconv
|
#include <clocale> // localeconv, lconv
|
||||||
#include <cmath> // isfinite
|
#include <cmath> // isfinite
|
||||||
@@ -37300,31 +37300,35 @@ class serializer
|
|||||||
// check if the buffer was large enough
|
// check if the buffer was large enough
|
||||||
JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
|
JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
|
||||||
|
|
||||||
// look up the locale's thousands separator and decimal point now,
|
// look up the locale's decimal point now, matching what
|
||||||
// matching what snprintf_float() just used (see lexer::get_decimal_point())
|
// snprintf_float() just used (see get_decimal_point() in
|
||||||
|
// number_parse.hpp); it is a string, which can be longer than one
|
||||||
|
// byte (U+066B in ps_AF, #5993).
|
||||||
|
// A thousands separator needs no handling: %g never groups digits.
|
||||||
const auto* loc = std::localeconv();
|
const auto* loc = std::localeconv();
|
||||||
JSON_ASSERT(loc != nullptr);
|
JSON_ASSERT(loc != nullptr);
|
||||||
const char thousands_sep = (loc->thousands_sep == nullptr) ? '\0' : *loc->thousands_sep;
|
const char* const decimal_point = (loc->decimal_point == nullptr) ? "" : loc->decimal_point;
|
||||||
const char decimal_point = (loc->decimal_point == nullptr) ? '\0' : *loc->decimal_point;
|
const std::size_t decimal_point_len = std::char_traits<char>::length(decimal_point);
|
||||||
|
|
||||||
// erase thousands separators
|
|
||||||
if (thousands_sep != '\0')
|
|
||||||
{
|
|
||||||
// NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
|
|
||||||
const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
|
|
||||||
std::fill(end, number_buffer.end(), '\0');
|
|
||||||
JSON_ASSERT((end - number_buffer.begin()) <= len);
|
|
||||||
len = (end - number_buffer.begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert decimal point to '.'
|
// convert decimal point to '.'
|
||||||
if (decimal_point != '\0' && decimal_point != '.')
|
if ((decimal_point_len > 1) || (decimal_point_len == 1 && decimal_point[0] != '.'))
|
||||||
{
|
{
|
||||||
// NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
|
const auto first = number_buffer.begin();
|
||||||
const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
|
const auto last = first + len;
|
||||||
if (dec_pos != number_buffer.end())
|
// NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::search returns an iterator, see https://github.com/nlohmann/json/issues/3081
|
||||||
|
const auto dec_pos = std::search(first, last, decimal_point, decimal_point + decimal_point_len);
|
||||||
|
if (dec_pos != last)
|
||||||
{
|
{
|
||||||
*dec_pos = '.';
|
*dec_pos = '.';
|
||||||
|
if (decimal_point_len > 1)
|
||||||
|
{
|
||||||
|
// move the fraction and exponent behind the '.' (std::copy
|
||||||
|
// allows the overlap, as the destination is before the source)
|
||||||
|
// NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::copy returns an iterator, see https://github.com/nlohmann/json/issues/3081
|
||||||
|
const auto end = std::copy(dec_pos + static_cast<std::ptrdiff_t>(decimal_point_len), last, dec_pos + 1);
|
||||||
|
*end = '\0';
|
||||||
|
len = end - first;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Generated
+8
-11
@@ -2473,20 +2473,20 @@ indent_done:
|
|||||||
if (negative)
|
if (negative)
|
||||||
{
|
{
|
||||||
++p;
|
++p;
|
||||||
|
if (NLOHMANN_VIEW_UNLIKELY(cur() < '0' || cur() > '9'))
|
||||||
|
{
|
||||||
|
return fail(error_code::number_after_minus);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const unsigned char* const int_start = p;
|
const unsigned char* const int_start = p;
|
||||||
if (cur() == '0')
|
if (cur() == '0')
|
||||||
{
|
{
|
||||||
++p;
|
++p;
|
||||||
}
|
}
|
||||||
else if (!negative || NLOHMANN_VIEW_LIKELY(cur() >= '1' && cur() <= '9'))
|
|
||||||
{
|
|
||||||
// (without a sign, the dispatch saw a digit)
|
|
||||||
p = skip_digits(p + 1, e);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return fail(error_code::number_after_minus);
|
// (a digit: the dispatch or the check after the sign saw it)
|
||||||
|
p = skip_digits(p + 1, e);
|
||||||
}
|
}
|
||||||
const auto int_digits = static_cast<std::size_t>(p - int_start);
|
const auto int_digits = static_cast<std::size_t>(p - int_start);
|
||||||
std::size_t frac_digits = 0;
|
std::size_t frac_digits = 0;
|
||||||
@@ -5059,8 +5059,7 @@ void value_range(const document_data& d, const node* n, std::size_t& start, std:
|
|||||||
|
|
||||||
/// the context basic_json puts in front of the message of an exception about
|
/// the context basic_json puts in front of the message of an exception about
|
||||||
/// a value: "(/path) " with JSON_DIAGNOSTICS (none for the root), then
|
/// a value: "(/path) " with JSON_DIAGNOSTICS (none for the root), then
|
||||||
/// "(bytes a-b) " with JSON_DIAGNOSTIC_POSITIONS (also for the root); with
|
/// "(bytes a-b) " with JSON_DIAGNOSTIC_POSITIONS (also for the root)
|
||||||
/// JSON_DIAGNOSTICS, nothing for a value that is no longer part of the document
|
|
||||||
///
|
///
|
||||||
/// This and the throw helpers that take an error_site are templates, so that
|
/// This and the throw helpers that take an error_site are templates, so that
|
||||||
/// the path and range code is instantiated only where an exception can be
|
/// the path and range code is instantiated only where an exception can be
|
||||||
@@ -5081,12 +5080,10 @@ std::string diagnostics(const error_site site)
|
|||||||
std::string context;
|
std::string context;
|
||||||
#if JSON_DIAGNOSTICS
|
#if JSON_DIAGNOSTICS
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
// a value that is no longer part of the document has no path, and its
|
|
||||||
// node still holds the bytes it had in the source: no context at all
|
|
||||||
const bool found = edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens);
|
const bool found = edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens);
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
{
|
||||||
// context is still empty; returning it keeps the copy elided (-Wnrvo)
|
// a value that is not found has no path, and with it no bytes
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
if (!tokens.empty())
|
if (!tokens.empty())
|
||||||
|
|||||||
@@ -1038,6 +1038,14 @@ template<class T>
|
|||||||
struct sweep_allocator
|
struct sweep_allocator
|
||||||
{
|
{
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
|
// GCC 4.8's std::map takes these from the allocator instead of from
|
||||||
|
// std::allocator_traits
|
||||||
|
using pointer = T*;
|
||||||
|
using const_pointer = const T*;
|
||||||
|
using reference = T&;
|
||||||
|
using const_reference = const T&;
|
||||||
|
using size_type = std::size_t;
|
||||||
|
using difference_type = std::ptrdiff_t;
|
||||||
|
|
||||||
sweep_allocator() = default;
|
sweep_allocator() = default;
|
||||||
template<class U> sweep_allocator(const sweep_allocator<U>& /*unused*/) noexcept {}
|
template<class U> sweep_allocator(const sweep_allocator<U>& /*unused*/) noexcept {}
|
||||||
@@ -1155,9 +1163,14 @@ TEST_CASE("parse() survives a failing allocation at every point (#5985)")
|
|||||||
|
|
||||||
// With iterator debugging (MSVC STL debug builds, also used by clang-cl),
|
// With iterator debugging (MSVC STL debug builds, also used by clang-cl),
|
||||||
// containers allocate a debug proxy through the allocator inside their
|
// containers allocate a debug proxy through the allocator inside their
|
||||||
// noexcept move constructors, so failing that allocation terminates the
|
// noexcept default and move constructors, so failing that allocation
|
||||||
// program instead of throwing std::bad_alloc. Nothing to check there.
|
// terminates the program instead of throwing std::bad_alloc. Nothing to
|
||||||
|
// check there.
|
||||||
#if !(defined(_ITERATOR_DEBUG_LEVEL) && _ITERATOR_DEBUG_LEVEL > 0)
|
#if !(defined(_ITERATOR_DEBUG_LEVEL) && _ITERATOR_DEBUG_LEVEL > 0)
|
||||||
|
// Clang 3.4 and 3.5 leak memory when an element constructor throws
|
||||||
|
// inside libstdc++'s containers (a std::map copy, a std::vector
|
||||||
|
// reallocation), without any json value involved.
|
||||||
|
#if !(defined(__clang__) && !defined(__apple_build_version__) && __clang_major__ == 3 && __clang_minor__ < 6)
|
||||||
SECTION("parser callback")
|
SECTION("parser callback")
|
||||||
{
|
{
|
||||||
const auto keep_all = [](int /*depth*/, sweep_json::parse_event_t /*event*/, sweep_json& /*parsed*/) noexcept
|
const auto keep_all = [](int /*depth*/, sweep_json::parse_event_t /*event*/, sweep_json& /*parsed*/) noexcept
|
||||||
@@ -1178,7 +1191,6 @@ TEST_CASE("parse() survives a failing allocation at every point (#5985)")
|
|||||||
return sweep_json::parse(text, drop_d);
|
return sweep_json::parse(text, drop_d);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
SECTION("sax_parse into an existing value")
|
SECTION("sax_parse into an existing value")
|
||||||
{
|
{
|
||||||
@@ -1192,5 +1204,7 @@ TEST_CASE("parse() survives a failing allocation at every point (#5985)")
|
|||||||
return value;
|
return value;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -2222,9 +2222,10 @@ TEST_CASE("serialization of short strings")
|
|||||||
{
|
{
|
||||||
CAPTURE(s)
|
CAPTURE(s)
|
||||||
const json j = s;
|
const json j = s;
|
||||||
CHECK(j.dump() == "\"" + s + "\"");
|
const std::string quoted = "\"" + s + "\""; // NOLINT(performance-inefficient-string-concatenation)
|
||||||
CHECK(j.dump(-1, ' ', true) == "\"" + s + "\"");
|
CHECK(j.dump() == quoted);
|
||||||
CHECK(json({{s, s}}).dump() == "{\"" + s + "\":\"" + s + "\"}");
|
CHECK(j.dump(-1, ' ', true) == quoted);
|
||||||
|
CHECK(json({{s, s}}).dump() == "{" + quoted + ":" + quoted + "}"); // NOLINT(performance-inefficient-string-concatenation)
|
||||||
}
|
}
|
||||||
|
|
||||||
// a character that needs an escape
|
// a character that needs an escape
|
||||||
|
|||||||
@@ -160,12 +160,12 @@ class strict_key
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_value{};
|
std::string m_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
strict_key make_strict_key(const std::string& key)
|
strict_key make_strict_key(const std::string& key)
|
||||||
{
|
{
|
||||||
return strict_key(key.c_str());
|
return {key.c_str()};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Key, class Value, class Compare, class Allocator>
|
template<class Key, class Value, class Compare, class Allocator>
|
||||||
@@ -224,7 +224,9 @@ TEST_CASE("converting a value with keys that cannot be made from a std::string n
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const strict_json converted = value;
|
// direct-initialization: a copy-initialization makes GCC 4.8 instantiate the
|
||||||
|
// copy constructor, which needs a range constructor strict_object lacks
|
||||||
|
const strict_json converted(value);
|
||||||
|
|
||||||
// at() needs to make a std::string of a key for its error message
|
// at() needs to make a std::string of a key for its error message
|
||||||
const auto member = [](const strict_json & object, const char* name) -> const strict_json &
|
const auto member = [](const strict_json & object, const char* name) -> const strict_json &
|
||||||
|
|||||||
@@ -105,19 +105,53 @@ template<typename T>
|
|||||||
struct failing_allocator
|
struct failing_allocator
|
||||||
{
|
{
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
|
// GCC 4.8's std::map takes these from the allocator instead of from
|
||||||
|
// std::allocator_traits
|
||||||
|
using pointer = T*;
|
||||||
|
using const_pointer = const T*;
|
||||||
|
using reference = T&;
|
||||||
|
using const_reference = const T&;
|
||||||
|
using size_type = std::size_t;
|
||||||
|
using difference_type = std::ptrdiff_t;
|
||||||
|
|
||||||
|
template<typename U>
|
||||||
|
struct rebind
|
||||||
|
{
|
||||||
|
using other = failing_allocator<U>;
|
||||||
|
};
|
||||||
|
|
||||||
failing_allocator() = default;
|
failing_allocator() = default;
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
explicit failing_allocator(const failing_allocator<U>& /*other*/) noexcept {}
|
explicit failing_allocator(const failing_allocator<U>& /*other*/) noexcept {}
|
||||||
|
|
||||||
T* allocate(std::size_t /*n*/) // NOLINT(readability-convert-member-functions-to-static)
|
[[noreturn]] T* allocate(std::size_t /*n*/) // NOLINT(readability-convert-member-functions-to-static)
|
||||||
{
|
{
|
||||||
throw std::bad_alloc();
|
throw std::bad_alloc();
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocate(T* /*p*/, std::size_t /*n*/) noexcept {} // NOLINT(readability-convert-member-functions-to-static)
|
void deallocate(T* /*p*/, std::size_t /*n*/) noexcept {} // NOLINT(readability-convert-member-functions-to-static)
|
||||||
|
|
||||||
|
// GCC 4.8's std::map calls these on the allocator directly instead of
|
||||||
|
// going through std::allocator_traits
|
||||||
|
std::size_t max_size() const noexcept // NOLINT(readability-convert-member-functions-to-static)
|
||||||
|
{
|
||||||
|
return (std::numeric_limits<std::size_t>::max)() / sizeof(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename U, typename... Args>
|
||||||
|
void construct(U* p, Args&& ... args) // NOLINT(readability-convert-member-functions-to-static)
|
||||||
|
{
|
||||||
|
::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename U>
|
||||||
|
void destroy(U* p) noexcept // NOLINT(readability-convert-member-functions-to-static)
|
||||||
|
{
|
||||||
|
static_cast<void>(p); // unused for trivially destructible types
|
||||||
|
p->~U();
|
||||||
|
}
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
bool operator==(const failing_allocator<U>& /*other*/) const noexcept
|
bool operator==(const failing_allocator<U>& /*other*/) const noexcept
|
||||||
{
|
{
|
||||||
@@ -270,7 +304,7 @@ TEST_CASE("json_view")
|
|||||||
"\"", "\"abc", "\"\\x\"", "\"\\u12\"", "\"\\ud800\"", "\"\\udc00\"", "\"\x01\"", "\"\xff\"", "\"\xc3\"", "[1]x", "/", "/*", "[\n 1,\n x\n]", // NOLINT(modernize-raw-string-literal)
|
"\"", "\"abc", "\"\\x\"", "\"\\u12\"", "\"\\ud800\"", "\"\\udc00\"", "\"\x01\"", "\"\xff\"", "\"\xc3\"", "[1]x", "/", "/*", "[\n 1,\n x\n]", // NOLINT(modernize-raw-string-literal)
|
||||||
"1e400", "-1e400", "[1.7976931348623159e308]", "{\"a\":\n{\"b\": [1, 2,\n 3 x]}}",
|
"1e400", "-1e400", "[1.7976931348623159e308]", "{\"a\":\n{\"b\": [1, 2,\n 3 x]}}",
|
||||||
// (an error after every kind of value: the error is found by replaying the parser)
|
// (an error after every kind of value: the error is found by replaying the parser)
|
||||||
"[null, true, -1, 1, 1.5, \"s\", {\"a\": 1} 2]", "{\"a\": null, \"b\": -1, \"c\": 1.5, \"d\": \"s\", \"e\": {}, 1}"
|
R"([null, true, -1, 1, 1.5, "s", {"a": 1} 2])", R"({"a": null, "b": -1, "c": 1.5, "d": "s", "e": {}, 1})"
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
CAPTURE(text)
|
CAPTURE(text)
|
||||||
@@ -1556,7 +1590,7 @@ TEST_CASE("json_view values")
|
|||||||
CHECK_THROWS_WITH_AS(not_an_object.root().get<view_map>(), json_view_test::message("type_error.302", "", 0, 5, "type must be object, but is array").c_str(), json::type_error&);
|
CHECK_THROWS_WITH_AS(not_an_object.root().get<view_map>(), json_view_test::message("type_error.302", "", 0, 5, "type must be object, but is array").c_str(), json::type_error&);
|
||||||
#if !defined(JSON_NOEXCEPTION)
|
#if !defined(JSON_NOEXCEPTION)
|
||||||
// (and one that cannot allocate its nodes)
|
// (and one that cannot allocate its nodes)
|
||||||
using failing_map = std::map<std::string, json_view, std::less<std::string>, failing_allocator<std::pair<const std::string, json_view>>>;
|
using failing_map = std::map<std::string, json_view, std::less<std::string>, failing_allocator<std::pair<const std::string, json_view>>>; // NOLINT(modernize-use-transparent-functors)
|
||||||
const json_document one_member = json_document::parse(R"({"a":1})");
|
const json_document one_member = json_document::parse(R"({"a":1})");
|
||||||
CHECK_THROWS_AS(one_member.root().get<failing_map>(), std::bad_alloc&);
|
CHECK_THROWS_AS(one_member.root().get<failing_map>(), std::bad_alloc&);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -397,6 +397,14 @@ TEST_CASE("locale with a multi-byte decimal point")
|
|||||||
|
|
||||||
// a value the locale-independent paths convert is not affected
|
// a value the locale-independent paths convert is not affected
|
||||||
CHECK(json::parse("12.5") == 12.5);
|
CHECK(json::parse("12.5") == 12.5);
|
||||||
|
|
||||||
|
// dump() of a long double that takes the snprintf path replaces the
|
||||||
|
// whole decimal point, not only its first byte (#5993)
|
||||||
|
CHECK(long_double_json(1.5L).dump() == "1.5");
|
||||||
|
CHECK(long_double_json(2.0L).dump() == "2.0");
|
||||||
|
CHECK(long_double_json(-0.25L).dump() == "-0.25");
|
||||||
|
CHECK(long_double_json::parse(long_double_json(1.5e-7L).dump()) == long_double_json(1.5e-7L));
|
||||||
|
CHECK(long_double_json::parse(ld.dump()) == expected_long_double);
|
||||||
}
|
}
|
||||||
if (!tested)
|
if (!tested)
|
||||||
{
|
{
|
||||||
@@ -505,8 +513,8 @@ struct locale_switching_token
|
|||||||
{
|
{
|
||||||
using value_type = char;
|
using value_type = char;
|
||||||
|
|
||||||
locale_switching_token(std::string text_, const char* switch_to_)
|
locale_switching_token(std::string text_, std::string switch_to_)
|
||||||
: text(std::move(text_)), switch_to(switch_to_)
|
: text(std::move(text_)), switch_to(std::move(switch_to_))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
std::size_t size() const
|
std::size_t size() const
|
||||||
@@ -523,13 +531,13 @@ struct locale_switching_token
|
|||||||
{
|
{
|
||||||
if (!switched)
|
if (!switched)
|
||||||
{
|
{
|
||||||
switched = std::setlocale(LC_NUMERIC, switch_to) != nullptr;
|
switched = std::setlocale(LC_NUMERIC, switch_to.c_str()) != nullptr;
|
||||||
}
|
}
|
||||||
return &text[0];
|
return &text[0]; // NOLINT(readability-container-data-pointer): std::string::data() is const before C++17
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string text;
|
std::string text;
|
||||||
const char* switch_to;
|
std::string switch_to;
|
||||||
bool switched = false;
|
bool switched = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -647,7 +655,7 @@ TEST_CASE("convert_float_locale_aware")
|
|||||||
|
|
||||||
// "C" -> comma locale: strtod stops at the '.', so the conversion is repeated
|
// "C" -> comma locale: strtod stops at the '.', so the conversion is repeated
|
||||||
{
|
{
|
||||||
locale_switching_token token("1.5", comma_locale.c_str());
|
locale_switching_token token("1.5", comma_locale);
|
||||||
double value = 0;
|
double value = 0;
|
||||||
nlohmann::detail::convert_float_locale_aware(token, 1, value);
|
nlohmann::detail::convert_float_locale_aware(token, 1, value);
|
||||||
if (token.switched)
|
if (token.switched)
|
||||||
@@ -660,7 +668,7 @@ TEST_CASE("convert_float_locale_aware")
|
|||||||
// the same for the other floating-point types
|
// the same for the other floating-point types
|
||||||
REQUIRE(std::setlocale(LC_NUMERIC, "C") != nullptr);
|
REQUIRE(std::setlocale(LC_NUMERIC, "C") != nullptr);
|
||||||
{
|
{
|
||||||
locale_switching_token token("2.25", comma_locale.c_str());
|
locale_switching_token token("2.25", comma_locale);
|
||||||
float value = 0;
|
float value = 0;
|
||||||
nlohmann::detail::convert_float_locale_aware(token, 1, value);
|
nlohmann::detail::convert_float_locale_aware(token, 1, value);
|
||||||
if (token.switched)
|
if (token.switched)
|
||||||
@@ -670,7 +678,7 @@ TEST_CASE("convert_float_locale_aware")
|
|||||||
}
|
}
|
||||||
REQUIRE(std::setlocale(LC_NUMERIC, "C") != nullptr);
|
REQUIRE(std::setlocale(LC_NUMERIC, "C") != nullptr);
|
||||||
{
|
{
|
||||||
locale_switching_token token("2.25", comma_locale.c_str());
|
locale_switching_token token("2.25", comma_locale);
|
||||||
long double value = 0;
|
long double value = 0;
|
||||||
nlohmann::detail::convert_float_locale_aware(token, 1, value);
|
nlohmann::detail::convert_float_locale_aware(token, 1, value);
|
||||||
if (token.switched)
|
if (token.switched)
|
||||||
|
|||||||
@@ -122,7 +122,8 @@ TEST_CASE("JSON text with std::byte")
|
|||||||
SECTION("invalid text")
|
SECTION("invalid text")
|
||||||
{
|
{
|
||||||
CHECK_FALSE(json::accept(to_bytes("[1,\n2,")));
|
CHECK_FALSE(json::accept(to_bytes("[1,\n2,")));
|
||||||
CHECK_THROWS_WITH_AS(json::parse(to_bytes("[1,\n2,")),
|
json _;
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::parse(to_bytes("[1,\n2,")),
|
||||||
"[json.exception.parse_error.101] parse error at line 2, column 3: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
|
"[json.exception.parse_error.101] parse error at line 2, column 3: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -541,6 +541,7 @@ TEST_CASE("object_key_index")
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::vector<std::size_t> hashes;
|
std::vector<std::size_t> hashes;
|
||||||
|
hashes.reserve(initial);
|
||||||
for (std::size_t i = 0; i < initial; ++i)
|
for (std::size_t i = 0; i < initial; ++i)
|
||||||
{
|
{
|
||||||
hashes.push_back(hash_of(i));
|
hashes.push_back(hash_of(i));
|
||||||
|
|||||||
@@ -1533,7 +1533,7 @@ TEST_CASE("regression tests 1")
|
|||||||
{
|
{
|
||||||
my_json::object_t top = orig.get_ref<const my_json::object_t&>();
|
my_json::object_t top = orig.get_ref<const my_json::object_t&>();
|
||||||
top.at("x") = my_json::parse(R"({"a":{"b":1},"n":null,"arr":[10,20],"objs":[{"m":5},{"m":6}]})");
|
top.at("x") = my_json::parse(R"({"a":{"b":1},"n":null,"arr":[10,20],"objs":[{"m":5},{"m":6}]})");
|
||||||
return my_json(std::move(top));
|
return my_json(std::move(top)); // NOLINT(modernize-return-braced-init-list): {top} would be an array
|
||||||
};
|
};
|
||||||
|
|
||||||
SECTION("existing members")
|
SECTION("existing members")
|
||||||
|
|||||||
@@ -120,6 +120,7 @@ class oversized_get_area_streambuf : public std::streambuf
|
|||||||
private:
|
private:
|
||||||
std::array<char, 8> m_data{{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}};
|
std::array<char, 8> m_data{{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// how a test reads from the stream
|
// how a test reads from the stream
|
||||||
@@ -463,15 +464,18 @@ TEST_CASE("parsing from a std::istream scans the get area in bulk")
|
|||||||
CHECK(token.empty());
|
CHECK(token.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if SIZE_MAX > UINT32_MAX
|
||||||
|
// a get area larger than an int needs a 64-bit std::size_t (checked by the
|
||||||
|
// preprocessor, as a constant condition triggers MSVC's C4127)
|
||||||
SECTION("skipping more characters than gbump() takes at once")
|
SECTION("skipping more characters than gbump() takes at once")
|
||||||
{
|
{
|
||||||
// gbump() takes an int, but a get area can be larger
|
// gbump() takes an int, but a get area can be larger
|
||||||
const std::size_t claimed = sizeof(std::size_t) < 8 ? 0 : std::size_t{3} << 30u;
|
const std::size_t claimed = std::size_t{3} << 30u;
|
||||||
oversized_get_area_streambuf buf(claimed);
|
oversized_get_area_streambuf buf(claimed);
|
||||||
if (claimed == 0 || static_cast<std::size_t>(buf.in_avail()) != claimed)
|
if (static_cast<std::size_t>(buf.in_avail()) != claimed)
|
||||||
{
|
{
|
||||||
// a 32-bit size_t, or a standard library whose streambuf stores
|
// a standard library whose streambuf stores the size of the get
|
||||||
// the size of the get area in an int (MSVC)
|
// area in an int (MSVC)
|
||||||
MESSAGE("a get area larger than an int is not supported");
|
MESSAGE("a get area larger than an int is not supported");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -491,6 +495,7 @@ TEST_CASE("parsing from a std::istream scans the get area in bulk")
|
|||||||
buf.restore_get_area();
|
buf.restore_get_area();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
SECTION("SAX parsing from a stream")
|
SECTION("SAX parsing from a stream")
|
||||||
|
|||||||
Reference in new issue
Block a user