Fix cpplint and old libstdc++ in the one-block input read

Use std::uint64_t and auto instead of long types (cpplint runtime/int), and
reserve only when the request is larger than the capacity: before GCC 11,
libstdc++ shrinks a string on a smaller reserve and moves the owned buffer.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-29 10:48:04 +01:00
1 parent a1b49b720a
commit def1acf5b3
2 files changed
+40 -16

No files matched your search

+20 -8
View File
@@ -7956,6 +7956,7 @@ NLOHMANN_JSON_NAMESPACE_END
#include <algorithm> // max
#include <cstddef> // size_t
#include <cstdint> // uint64_t
#ifndef JSON_NO_IO
#include <cstdio> // FILE, fread, ftell
#if defined(_WIN32)
@@ -8095,6 +8096,17 @@ 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
/// read in blocks from the stream buffer, straight into buf (reusing its
/// capacity). As the library's stream adapter, this reads the stream buffer
@@ -8114,7 +8126,7 @@ inline void append_stream(std::string& buf, std::istream& is)
const std::streamsize rest = sb->in_avail();
if (rest > 0 && static_cast<std::size_t>(rest) < buf.max_size() - buf.size() - read_block_size())
{
buf.reserve(buf.size() + static_cast<std::size_t>(rest) + read_block_size());
reserve_at_least(buf, buf.size() + static_cast<std::size_t>(rest) + read_block_size());
}
for (;;)
{
@@ -8145,26 +8157,26 @@ struct is_c_stream
/// 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 unsigned long long file_rest(std::FILE* f) noexcept
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 long long here = ::_ftelli64(f);
const auto here = ::_ftelli64(f);
if (here >= 0 && st.st_size > here)
{
return static_cast<unsigned long long>(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 long here = std::ftell(f);
const auto here = std::ftell(f);
if (here >= 0 && st.st_size > here)
{
return static_cast<unsigned long long>(st.st_size - here);
return static_cast<std::uint64_t>(st.st_size - here);
}
}
#else
@@ -8182,10 +8194,10 @@ inline void append_file(std::string& buf, std::FILE* f)
// 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 unsigned long long rest = file_rest(f);
const std::uint64_t rest = file_rest(f);
if (rest > 0 && rest < buf.max_size() - buf.size() - 1)
{
buf.reserve(buf.size() + static_cast<std::size_t>(rest) + 1);
reserve_at_least(buf, buf.size() + static_cast<std::size_t>(rest) + 1);
}
for (;;)
{