Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
027d730547 |
No files matched your search
@@ -64,6 +64,10 @@ so the result can be larger than the value by a factor of its nesting depth (see
|
|||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
|
Once the output reaches 512 characters, the result reserves 1 MiB, and four times as much whenever that is not enough,
|
||||||
|
up to 64 MiB. This saves copies and page faults while the string grows, but the returned string can have a much larger
|
||||||
|
`capacity()` than `size()`. Call `shrink_to_fit()` on a result that is kept for long.
|
||||||
|
|
||||||
Floating-point numbers are written with the fewest digits that read back as the same value (for `#!cpp double`; see
|
Floating-point numbers are written with the fewest digits that read back as the same value (for `#!cpp double`; see
|
||||||
[number handling](../../features/types/number_handling.md#number-serialization)).
|
[number handling](../../features/types/number_handling.md#number-serialization)).
|
||||||
|
|
||||||
@@ -106,3 +110,4 @@ Binary values are serialized as an object containing two keys:
|
|||||||
- Error handler `keep` added in version 3.13.0.
|
- Error handler `keep` added in version 3.13.0.
|
||||||
- Doubles are written with the shortest digits (Żmij instead of Grisu2) since version 3.13.0; about 0.1% of doubles are
|
- Doubles are written with the shortest digits (Żmij instead of Grisu2) since version 3.13.0; about 0.1% of doubles are
|
||||||
written differently, most of them with fewer digits.
|
written differently, most of them with fewer digits.
|
||||||
|
- The result reserves capacity for outputs of 512 characters or more since version 3.13.0.
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
#endif // JSON_NO_IO
|
#endif // JSON_NO_IO
|
||||||
|
|
||||||
#include <nlohmann/detail/macro_scope.hpp>
|
#include <nlohmann/detail/macro_scope.hpp>
|
||||||
|
#include <nlohmann/detail/meta/cpp_future.hpp>
|
||||||
|
|
||||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||||
namespace detail
|
namespace detail
|
||||||
@@ -400,6 +401,68 @@ class output_string_adapter : public output_adapter_protocol<CharType>
|
|||||||
StringType& str;
|
StringType& str;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief output adapter for the string that basic_json::dump() returns
|
||||||
|
|
||||||
|
A string that grows by appending reallocates and copies its contents each time
|
||||||
|
it doubles, and with glibc every new big block is fresh memory that faults in
|
||||||
|
page by page. Once a single write of at least 512 characters arrives (a full
|
||||||
|
flush of the serializer's buffer or a long string, so the output is at least
|
||||||
|
that big), this adapter reserves 1 MiB, and four times as much whenever that is
|
||||||
|
not enough, until 64 MiB; beyond that the string grows by itself. Smaller
|
||||||
|
outputs never reserve. The capacity is tracked here, as StringType::capacity()
|
||||||
|
is not required; a StringType without reserve() just appends.
|
||||||
|
*/
|
||||||
|
template<typename CharType, typename StringType>
|
||||||
|
class output_dump_string_adapter : public output_adapter_protocol<CharType>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit output_dump_string_adapter(StringType& s) noexcept
|
||||||
|
: str(s)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void write_character(CharType c) override
|
||||||
|
{
|
||||||
|
str.push_back(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_characters(const CharType* s, std::size_t length) override
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(length >= 512))
|
||||||
|
{
|
||||||
|
grow(str.size() + length);
|
||||||
|
}
|
||||||
|
str.append(s, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void grow(const std::size_t needed)
|
||||||
|
{
|
||||||
|
const std::size_t first_reserve = 1048576; // 1 MiB
|
||||||
|
const std::size_t last_reserve = 64 * first_reserve;
|
||||||
|
if (needed > reserved && reserved < last_reserve)
|
||||||
|
{
|
||||||
|
const std::size_t next = reserved == 0 ? first_reserve : reserved * 4;
|
||||||
|
reserved = (std::max)(needed, next);
|
||||||
|
reserve_string(str, reserved, priority_tag<1> {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename String>
|
||||||
|
static auto reserve_string(String& string, std::size_t n, priority_tag<1> /*unused*/)
|
||||||
|
-> decltype(string.reserve(n), void())
|
||||||
|
{
|
||||||
|
string.reserve(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename String>
|
||||||
|
static void reserve_string(String& /*string*/, std::size_t /*n*/, priority_tag<0> /*unused*/) noexcept
|
||||||
|
{}
|
||||||
|
|
||||||
|
StringType& str;
|
||||||
|
std::size_t reserved = 0;
|
||||||
|
};
|
||||||
|
|
||||||
/// @brief output sink forwarding to a type-erased output adapter
|
/// @brief output sink forwarding to a type-erased output adapter
|
||||||
///
|
///
|
||||||
/// Wraps the polymorphic output_adapter_t so the same binary_writer template can
|
/// Wraps the polymorphic output_adapter_t so the same binary_writer template can
|
||||||
|
|||||||
@@ -10,17 +10,8 @@
|
|||||||
|
|
||||||
#include <algorithm> // max
|
#include <algorithm> // max
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <cstdint> // uint64_t
|
|
||||||
#ifndef JSON_NO_IO
|
#ifndef JSON_NO_IO
|
||||||
#include <cstdio> // FILE, fread, ftell
|
#include <cstdio> // FILE, fread
|
||||||
#if defined(_WIN32)
|
|
||||||
#include <stdio.h> // _fileno, _ftelli64 // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers)
|
|
||||||
#include <sys/types.h> // (for sys/stat.h)
|
|
||||||
#include <sys/stat.h> // _fstat64, _S_IFMT, _S_IFREG
|
|
||||||
#elif defined(__unix__) || defined(__APPLE__)
|
|
||||||
#include <stdio.h> // fileno // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers)
|
|
||||||
#include <sys/stat.h> // fstat, S_ISREG
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
#include <istream> // istream, streambuf
|
#include <istream> // istream, streambuf
|
||||||
#include <string> // basic_string, char_traits, string
|
#include <string> // basic_string, char_traits, string
|
||||||
@@ -140,26 +131,6 @@ adapter_input<typename std::decay<Adapter>::type> make_adapter_input(Adapter&& i
|
|||||||
return {std::forward<Adapter>(ia)};
|
return {std::forward<Adapter>(ia)};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// the size of a read block, and the least room offered to a read: one page,
|
|
||||||
/// the usual size of a filesystem block. It is a conventional size, not a tuned
|
|
||||||
/// one: it only has to be large enough that a read is never tiny and that the
|
|
||||||
/// read which finds the end of the input needs no regrowth of the string
|
|
||||||
constexpr std::size_t read_block_size() noexcept
|
|
||||||
{
|
|
||||||
return 4096;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// make room for at least n characters in buf: never asks for less than its
|
|
||||||
/// capacity, because before C++20 such a request may shrink the string (as
|
|
||||||
/// libstdc++ before GCC 11 does) and move its characters
|
|
||||||
inline void reserve_at_least(std::string& buf, std::size_t n)
|
|
||||||
{
|
|
||||||
if (n > buf.capacity())
|
|
||||||
{
|
|
||||||
buf.reserve(n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// append the rest of a stream: its characters need no conversion, so they are
|
/// append the rest of a stream: its characters need no conversion, so they are
|
||||||
/// read in blocks from the stream buffer, straight into buf (reusing its
|
/// read in blocks from the stream buffer, straight into buf (reusing its
|
||||||
/// capacity). As the library's stream adapter, this reads the stream buffer
|
/// capacity). As the library's stream adapter, this reads the stream buffer
|
||||||
@@ -171,20 +142,10 @@ inline void append_stream(std::string& buf, std::istream& is)
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// room for the rest when the stream buffer knows how much is left (for a
|
|
||||||
// file with libstdc++, the rest of the file), and for the read that finds
|
|
||||||
// the end: the input is then read in one block, without a reallocation and
|
|
||||||
// copy at each doubling of the string (only a hint: the loop below reads
|
|
||||||
// what is there)
|
|
||||||
const std::streamsize rest = sb->in_avail();
|
|
||||||
if (rest > 0 && static_cast<std::size_t>(rest) < buf.max_size() - buf.size() - read_block_size())
|
|
||||||
{
|
|
||||||
reserve_at_least(buf, buf.size() + static_cast<std::size_t>(rest) + read_block_size());
|
|
||||||
}
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
const std::size_t used = buf.size();
|
const std::size_t used = buf.size();
|
||||||
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
|
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
|
||||||
buf.resize(used + room);
|
buf.resize(used + room);
|
||||||
const std::streamsize got = sb->sgetn(&buf[used], static_cast<std::streamsize>(room));
|
const std::streamsize got = sb->sgetn(&buf[used], static_cast<std::streamsize>(room));
|
||||||
buf.resize(used + static_cast<std::size_t>((std::max)(got, std::streamsize{0})));
|
buf.resize(used + static_cast<std::size_t>((std::max)(got, std::streamsize{0})));
|
||||||
@@ -207,55 +168,15 @@ struct is_c_stream
|
|||||||
{};
|
{};
|
||||||
|
|
||||||
#ifndef JSON_NO_IO
|
#ifndef JSON_NO_IO
|
||||||
/// the number of bytes left in a C stream that is a regular file (from its
|
|
||||||
/// size and position), 0 for anything else and on other systems: a hint for
|
|
||||||
/// the room to reserve; f is not null
|
|
||||||
inline std::uint64_t file_rest(std::FILE* f) noexcept
|
|
||||||
{
|
|
||||||
#if defined(_WIN32)
|
|
||||||
struct _stat64 st {};
|
|
||||||
if (::_fstat64(_fileno(f), &st) == 0 && (st.st_mode & _S_IFMT) == _S_IFREG)
|
|
||||||
{
|
|
||||||
const auto here = ::_ftelli64(f);
|
|
||||||
if (here >= 0 && st.st_size > here)
|
|
||||||
{
|
|
||||||
return static_cast<std::uint64_t>(st.st_size - here);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#elif defined(__unix__) || defined(__APPLE__)
|
|
||||||
struct stat st {};
|
|
||||||
if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode))
|
|
||||||
{
|
|
||||||
const auto here = std::ftell(f);
|
|
||||||
if (here >= 0 && st.st_size > here)
|
|
||||||
{
|
|
||||||
return static_cast<std::uint64_t>(st.st_size - here);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static_cast<void>(f);
|
|
||||||
#endif
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// append the rest of a C stream: in blocks with fread, not character by
|
/// append the rest of a C stream: in blocks with fread, not character by
|
||||||
/// character (the library's file adapter reads with fgetc), and in one block
|
/// character (the library's file adapter reads with fgetc);
|
||||||
/// when it is a regular file of known size; f is not null
|
/// f is not null
|
||||||
inline void append_file(std::string& buf, std::FILE* f)
|
inline void append_file(std::string& buf, std::FILE* f)
|
||||||
{
|
{
|
||||||
// room for the rest of a regular file and the byte that shows its end:
|
|
||||||
// the file is then read in one block, without a reallocation and copy at
|
|
||||||
// each doubling of the string (only a hint: the loop below reads what is
|
|
||||||
// there)
|
|
||||||
const std::uint64_t rest = file_rest(f);
|
|
||||||
if (rest > 0 && rest < buf.max_size() - buf.size() - 1)
|
|
||||||
{
|
|
||||||
reserve_at_least(buf, buf.size() + static_cast<std::size_t>(rest) + 1);
|
|
||||||
}
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
const std::size_t used = buf.size();
|
const std::size_t used = buf.size();
|
||||||
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
|
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
|
||||||
buf.resize(used + room);
|
buf.resize(used + room);
|
||||||
const std::size_t got = std::fread(&buf[used], 1, room, f);
|
const std::size_t got = std::fread(&buf[used], 1, room, f);
|
||||||
buf.resize(used + got);
|
buf.resize(used + got);
|
||||||
|
|||||||
@@ -2766,7 +2766,7 @@ public:
|
|||||||
const error_handler_t error_handler = error_handler_t::strict) const
|
const error_handler_t error_handler = error_handler_t::strict) const
|
||||||
{
|
{
|
||||||
string_t result;
|
string_t result;
|
||||||
detail::output_string_adapter<char, string_t> string_adapter(result);
|
detail::output_dump_string_adapter<char, string_t> string_adapter(result);
|
||||||
|
|
||||||
if (indent >= 0)
|
if (indent >= 0)
|
||||||
{
|
{
|
||||||
|
|||||||
Generated
+65
-1
@@ -29739,6 +29739,8 @@ NLOHMANN_JSON_NAMESPACE_END
|
|||||||
|
|
||||||
// #include <nlohmann/detail/macro_scope.hpp>
|
// #include <nlohmann/detail/macro_scope.hpp>
|
||||||
|
|
||||||
|
// #include <nlohmann/detail/meta/cpp_future.hpp>
|
||||||
|
|
||||||
|
|
||||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||||
namespace detail
|
namespace detail
|
||||||
@@ -30114,6 +30116,68 @@ class output_string_adapter : public output_adapter_protocol<CharType>
|
|||||||
StringType& str;
|
StringType& str;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief output adapter for the string that basic_json::dump() returns
|
||||||
|
|
||||||
|
A string that grows by appending reallocates and copies its contents each time
|
||||||
|
it doubles, and with glibc every new big block is fresh memory that faults in
|
||||||
|
page by page. Once a single write of at least 512 characters arrives (a full
|
||||||
|
flush of the serializer's buffer or a long string, so the output is at least
|
||||||
|
that big), this adapter reserves 1 MiB, and four times as much whenever that is
|
||||||
|
not enough, until 64 MiB; beyond that the string grows by itself. Smaller
|
||||||
|
outputs never reserve. The capacity is tracked here, as StringType::capacity()
|
||||||
|
is not required; a StringType without reserve() just appends.
|
||||||
|
*/
|
||||||
|
template<typename CharType, typename StringType>
|
||||||
|
class output_dump_string_adapter : public output_adapter_protocol<CharType>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit output_dump_string_adapter(StringType& s) noexcept
|
||||||
|
: str(s)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void write_character(CharType c) override
|
||||||
|
{
|
||||||
|
str.push_back(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_characters(const CharType* s, std::size_t length) override
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(length >= 512))
|
||||||
|
{
|
||||||
|
grow(str.size() + length);
|
||||||
|
}
|
||||||
|
str.append(s, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void grow(const std::size_t needed)
|
||||||
|
{
|
||||||
|
const std::size_t first_reserve = 1048576; // 1 MiB
|
||||||
|
const std::size_t last_reserve = 64 * first_reserve;
|
||||||
|
if (needed > reserved && reserved < last_reserve)
|
||||||
|
{
|
||||||
|
const std::size_t next = reserved == 0 ? first_reserve : reserved * 4;
|
||||||
|
reserved = (std::max)(needed, next);
|
||||||
|
reserve_string(str, reserved, priority_tag<1> {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename String>
|
||||||
|
static auto reserve_string(String& string, std::size_t n, priority_tag<1> /*unused*/)
|
||||||
|
-> decltype(string.reserve(n), void())
|
||||||
|
{
|
||||||
|
string.reserve(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename String>
|
||||||
|
static void reserve_string(String& /*string*/, std::size_t /*n*/, priority_tag<0> /*unused*/) noexcept
|
||||||
|
{}
|
||||||
|
|
||||||
|
StringType& str;
|
||||||
|
std::size_t reserved = 0;
|
||||||
|
};
|
||||||
|
|
||||||
/// @brief output sink forwarding to a type-erased output adapter
|
/// @brief output sink forwarding to a type-erased output adapter
|
||||||
///
|
///
|
||||||
/// Wraps the polymorphic output_adapter_t so the same binary_writer template can
|
/// Wraps the polymorphic output_adapter_t so the same binary_writer template can
|
||||||
@@ -42972,7 +43036,7 @@ public:
|
|||||||
const error_handler_t error_handler = error_handler_t::strict) const
|
const error_handler_t error_handler = error_handler_t::strict) const
|
||||||
{
|
{
|
||||||
string_t result;
|
string_t result;
|
||||||
detail::output_string_adapter<char, string_t> string_adapter(result);
|
detail::output_dump_string_adapter<char, string_t> string_adapter(result);
|
||||||
|
|
||||||
if (indent >= 0)
|
if (indent >= 0)
|
||||||
{
|
{
|
||||||
|
|||||||
Generated
+5
-84
@@ -7956,17 +7956,8 @@ NLOHMANN_JSON_NAMESPACE_END
|
|||||||
|
|
||||||
#include <algorithm> // max
|
#include <algorithm> // max
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <cstdint> // uint64_t
|
|
||||||
#ifndef JSON_NO_IO
|
#ifndef JSON_NO_IO
|
||||||
#include <cstdio> // FILE, fread, ftell
|
#include <cstdio> // FILE, fread
|
||||||
#if defined(_WIN32)
|
|
||||||
#include <stdio.h> // _fileno, _ftelli64 // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers)
|
|
||||||
#include <sys/types.h> // (for sys/stat.h)
|
|
||||||
#include <sys/stat.h> // _fstat64, _S_IFMT, _S_IFREG
|
|
||||||
#elif defined(__unix__) || defined(__APPLE__)
|
|
||||||
#include <stdio.h> // fileno // NOLINT(hicpp-deprecated-headers,modernize-deprecated-headers)
|
|
||||||
#include <sys/stat.h> // fstat, S_ISREG
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
#include <istream> // istream, streambuf
|
#include <istream> // istream, streambuf
|
||||||
#include <string> // basic_string, char_traits, string
|
#include <string> // basic_string, char_traits, string
|
||||||
@@ -8087,26 +8078,6 @@ adapter_input<typename std::decay<Adapter>::type> make_adapter_input(Adapter&& i
|
|||||||
return {std::forward<Adapter>(ia)};
|
return {std::forward<Adapter>(ia)};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// the size of a read block, and the least room offered to a read: one page,
|
|
||||||
/// the usual size of a filesystem block. It is a conventional size, not a tuned
|
|
||||||
/// one: it only has to be large enough that a read is never tiny and that the
|
|
||||||
/// read which finds the end of the input needs no regrowth of the string
|
|
||||||
constexpr std::size_t read_block_size() noexcept
|
|
||||||
{
|
|
||||||
return 4096;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// make room for at least n characters in buf: never asks for less than its
|
|
||||||
/// capacity, because before C++20 such a request may shrink the string (as
|
|
||||||
/// libstdc++ before GCC 11 does) and move its characters
|
|
||||||
inline void reserve_at_least(std::string& buf, std::size_t n)
|
|
||||||
{
|
|
||||||
if (n > buf.capacity())
|
|
||||||
{
|
|
||||||
buf.reserve(n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// append the rest of a stream: its characters need no conversion, so they are
|
/// append the rest of a stream: its characters need no conversion, so they are
|
||||||
/// read in blocks from the stream buffer, straight into buf (reusing its
|
/// read in blocks from the stream buffer, straight into buf (reusing its
|
||||||
/// capacity). As the library's stream adapter, this reads the stream buffer
|
/// capacity). As the library's stream adapter, this reads the stream buffer
|
||||||
@@ -8118,20 +8089,10 @@ inline void append_stream(std::string& buf, std::istream& is)
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// room for the rest when the stream buffer knows how much is left (for a
|
|
||||||
// file with libstdc++, the rest of the file), and for the read that finds
|
|
||||||
// the end: the input is then read in one block, without a reallocation and
|
|
||||||
// copy at each doubling of the string (only a hint: the loop below reads
|
|
||||||
// what is there)
|
|
||||||
const std::streamsize rest = sb->in_avail();
|
|
||||||
if (rest > 0 && static_cast<std::size_t>(rest) < buf.max_size() - buf.size() - read_block_size())
|
|
||||||
{
|
|
||||||
reserve_at_least(buf, buf.size() + static_cast<std::size_t>(rest) + read_block_size());
|
|
||||||
}
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
const std::size_t used = buf.size();
|
const std::size_t used = buf.size();
|
||||||
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
|
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
|
||||||
buf.resize(used + room);
|
buf.resize(used + room);
|
||||||
const std::streamsize got = sb->sgetn(&buf[used], static_cast<std::streamsize>(room));
|
const std::streamsize got = sb->sgetn(&buf[used], static_cast<std::streamsize>(room));
|
||||||
buf.resize(used + static_cast<std::size_t>((std::max)(got, std::streamsize{0})));
|
buf.resize(used + static_cast<std::size_t>((std::max)(got, std::streamsize{0})));
|
||||||
@@ -8154,55 +8115,15 @@ struct is_c_stream
|
|||||||
{};
|
{};
|
||||||
|
|
||||||
#ifndef JSON_NO_IO
|
#ifndef JSON_NO_IO
|
||||||
/// the number of bytes left in a C stream that is a regular file (from its
|
|
||||||
/// size and position), 0 for anything else and on other systems: a hint for
|
|
||||||
/// the room to reserve; f is not null
|
|
||||||
inline std::uint64_t file_rest(std::FILE* f) noexcept
|
|
||||||
{
|
|
||||||
#if defined(_WIN32)
|
|
||||||
struct _stat64 st {};
|
|
||||||
if (::_fstat64(_fileno(f), &st) == 0 && (st.st_mode & _S_IFMT) == _S_IFREG)
|
|
||||||
{
|
|
||||||
const auto here = ::_ftelli64(f);
|
|
||||||
if (here >= 0 && st.st_size > here)
|
|
||||||
{
|
|
||||||
return static_cast<std::uint64_t>(st.st_size - here);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#elif defined(__unix__) || defined(__APPLE__)
|
|
||||||
struct stat st {};
|
|
||||||
if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode))
|
|
||||||
{
|
|
||||||
const auto here = std::ftell(f);
|
|
||||||
if (here >= 0 && st.st_size > here)
|
|
||||||
{
|
|
||||||
return static_cast<std::uint64_t>(st.st_size - here);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static_cast<void>(f);
|
|
||||||
#endif
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// append the rest of a C stream: in blocks with fread, not character by
|
/// append the rest of a C stream: in blocks with fread, not character by
|
||||||
/// character (the library's file adapter reads with fgetc), and in one block
|
/// character (the library's file adapter reads with fgetc);
|
||||||
/// when it is a regular file of known size; f is not null
|
/// f is not null
|
||||||
inline void append_file(std::string& buf, std::FILE* f)
|
inline void append_file(std::string& buf, std::FILE* f)
|
||||||
{
|
{
|
||||||
// room for the rest of a regular file and the byte that shows its end:
|
|
||||||
// the file is then read in one block, without a reallocation and copy at
|
|
||||||
// each doubling of the string (only a hint: the loop below reads what is
|
|
||||||
// there)
|
|
||||||
const std::uint64_t rest = file_rest(f);
|
|
||||||
if (rest > 0 && rest < buf.max_size() - buf.size() - 1)
|
|
||||||
{
|
|
||||||
reserve_at_least(buf, buf.size() + static_cast<std::size_t>(rest) + 1);
|
|
||||||
}
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
const std::size_t used = buf.size();
|
const std::size_t used = buf.size();
|
||||||
const std::size_t room = (std::max)(buf.capacity() - used, read_block_size());
|
const std::size_t room = (std::max)(buf.capacity() - used, std::size_t{4096});
|
||||||
buf.resize(used + room);
|
buf.resize(used + room);
|
||||||
const std::size_t got = std::fread(&buf[used], 1, room, f);
|
const std::size_t got = std::fread(&buf[used], 1, room, f);
|
||||||
buf.resize(used + got);
|
buf.resize(used + got);
|
||||||
|
|||||||
@@ -484,6 +484,22 @@ TEST_CASE("alternative string type")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("alternative string type with a big dump")
|
||||||
|
{
|
||||||
|
// dump() reserves capacity in the result once the output is big
|
||||||
|
alt_json j = alt_json::array();
|
||||||
|
nlohmann::json reference = nlohmann::json::array();
|
||||||
|
for (int i = 0; i < 300000; ++i)
|
||||||
|
{
|
||||||
|
j.push_back(i);
|
||||||
|
reference.push_back(i);
|
||||||
|
}
|
||||||
|
j.push_back(alt_string(600, 'x'));
|
||||||
|
reference.push_back(std::string(600, 'x'));
|
||||||
|
const std::string expected = reference.dump(2);
|
||||||
|
CHECK(j.dump(2) == expected);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("alternative string type with json_view")
|
TEST_CASE("alternative string type with json_view")
|
||||||
{
|
{
|
||||||
// regression test for #5994: basic_json_view::dump() called
|
// regression test for #5994: basic_json_view::dump() called
|
||||||
|
|||||||
@@ -800,6 +800,30 @@ class failing_adapter : public nlohmann::detail::output_adapter_protocol<char>
|
|||||||
} // namespace
|
} // namespace
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
// a string type with only what the adapter of dump() needs, and no reserve()
|
||||||
|
struct appending_string
|
||||||
|
{
|
||||||
|
std::string str;
|
||||||
|
|
||||||
|
std::size_t size() const noexcept
|
||||||
|
{
|
||||||
|
return str.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_back(char c)
|
||||||
|
{
|
||||||
|
str.push_back(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append(const char* s, std::size_t length)
|
||||||
|
{
|
||||||
|
str.append(s, length);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
TEST_CASE("output adapters")
|
TEST_CASE("output adapters")
|
||||||
{
|
{
|
||||||
SECTION("writing a single character")
|
SECTION("writing a single character")
|
||||||
@@ -825,6 +849,18 @@ TEST_CASE("output adapters")
|
|||||||
CHECK(as_string == "ef");
|
CHECK(as_string == "ef");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("the adapter of dump() with a string type without reserve()")
|
||||||
|
{
|
||||||
|
// dump() reserves capacity for big outputs only if the string type
|
||||||
|
// has reserve(); without it, the adapter just appends
|
||||||
|
appending_string as_string {std::string()};
|
||||||
|
nlohmann::detail::output_dump_string_adapter<char, appending_string> dump_adapter(as_string);
|
||||||
|
dump_adapter.write_character('g');
|
||||||
|
const std::string block(600, 'h');
|
||||||
|
dump_adapter.write_characters(block.data(), block.size());
|
||||||
|
CHECK(as_string.str == "g" + block);
|
||||||
|
}
|
||||||
|
|
||||||
#if !defined(JSON_NOEXCEPTION)
|
#if !defined(JSON_NOEXCEPTION)
|
||||||
SECTION("a sink that is destroyed with characters left over")
|
SECTION("a sink that is destroyed with characters left over")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -535,33 +535,6 @@ TEST_CASE("json_view")
|
|||||||
CHECK_THROWS_AS(from_null = json_document::parse(static_cast<std::FILE*>(nullptr)), json::parse_error&);
|
CHECK_THROWS_AS(from_null = json_document::parse(static_cast<std::FILE*>(nullptr)), json::parse_error&);
|
||||||
}
|
}
|
||||||
|
|
||||||
// a regular file is read in one block, from its position to its end
|
|
||||||
std::FILE* tail_file = std::tmpfile(); // NOLINT(cppcoreguidelines-owning-memory)
|
|
||||||
if (tail_file != nullptr)
|
|
||||||
{
|
|
||||||
const std::string prefix = "garbage ";
|
|
||||||
CHECK(std::fwrite(prefix.data(), 1, prefix.size(), tail_file) == prefix.size());
|
|
||||||
CHECK(std::fwrite(text.data(), 1, text.size(), tail_file) == text.size());
|
|
||||||
CHECK(std::fseek(tail_file, static_cast<long>(prefix.size()), SEEK_SET) == 0);
|
|
||||||
#if defined(_WIN32) || defined(__unix__) || defined(__APPLE__)
|
|
||||||
CHECK(nlohmann::detail::view::file_rest(tail_file) == text.size());
|
|
||||||
#endif
|
|
||||||
const json_document from_tail = json_document::parse(tail_file);
|
|
||||||
CHECK(from_tail.source() == text);
|
|
||||||
CHECK(from_tail.root().materialize() == json::parse(text));
|
|
||||||
CHECK(nlohmann::detail::view::file_rest(tail_file) == 0);
|
|
||||||
CHECK(std::fclose(tail_file) == 0); // NOLINT(cppcoreguidelines-owning-memory)
|
|
||||||
}
|
|
||||||
|
|
||||||
// a stream is read from its position to its end as well, in one block
|
|
||||||
// when its buffer knows how much is left
|
|
||||||
std::istringstream tail_stream("garbage " + text);
|
|
||||||
tail_stream.seekg(8);
|
|
||||||
const json_document from_tail_stream = json_document::parse(tail_stream);
|
|
||||||
CHECK(from_tail_stream.source() == text);
|
|
||||||
CHECK(from_tail_stream.root().materialize() == json::parse(text));
|
|
||||||
CHECK(tail_stream.eof());
|
|
||||||
|
|
||||||
// a stream without a buffer is an empty input
|
// a stream without a buffer is an empty input
|
||||||
std::istream no_buffer(nullptr);
|
std::istream no_buffer(nullptr);
|
||||||
CHECK(json_document::parse(no_buffer, false).is_discarded());
|
CHECK(json_document::parse(no_buffer, false).is_discarded());
|
||||||
|
|||||||
@@ -1717,3 +1717,45 @@ TEST_CASE("runs of multi-byte characters are escaped like the reference under en
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("dump() of outputs around the sizes where the result reserves capacity")
|
||||||
|
{
|
||||||
|
// the result reserves 1 MiB from the first write of at least 512
|
||||||
|
// characters, and four times as much whenever that is not enough; the
|
||||||
|
// output must not depend on it
|
||||||
|
const std::size_t mib = 1048576;
|
||||||
|
for (const std::size_t length :
|
||||||
|
{
|
||||||
|
std::size_t{0}, std::size_t{1}, std::size_t{509}, std::size_t{510}, std::size_t{511}, std::size_t{512},
|
||||||
|
std::size_t{513}, std::size_t{1023}, std::size_t{1024}, mib - 3, mib - 2, mib - 1, mib, mib + 1,
|
||||||
|
(4 * mib) - 2, (4 * mib) + 1
|
||||||
|
})
|
||||||
|
{
|
||||||
|
CAPTURE(length)
|
||||||
|
const std::string s(length, 'x');
|
||||||
|
const json j = s;
|
||||||
|
CHECK(j.dump() == "\"" + s + "\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
// many writes of the serializer's buffer, compact and indented
|
||||||
|
for (const std::size_t count :
|
||||||
|
{
|
||||||
|
std::size_t{0}, std::size_t{1}, std::size_t{100}, std::size_t{200000}
|
||||||
|
})
|
||||||
|
{
|
||||||
|
CAPTURE(count)
|
||||||
|
json j = json::array();
|
||||||
|
std::string compact = "[";
|
||||||
|
std::string indented = "[";
|
||||||
|
for (std::size_t i = 0; i < count; ++i)
|
||||||
|
{
|
||||||
|
j.push_back(i);
|
||||||
|
compact += (i == 0 ? "" : ",") + std::to_string(i);
|
||||||
|
indented += (i == 0 ? "\n " : ",\n ") + std::to_string(i);
|
||||||
|
}
|
||||||
|
compact += "]";
|
||||||
|
indented += count == 0 ? "]" : "\n]";
|
||||||
|
CHECK(j.dump() == compact);
|
||||||
|
CHECK(j.dump(1) == indented);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user