♻️ add reserve_indent helper

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-06-30 18:56:27 +02:00
parent 3249b180cc
commit 250787cb54
3 changed files with 54 additions and 38 deletions
+22 -15
View File
@@ -19300,11 +19300,7 @@ class serializer
// variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<std::size_t>(new_indent)), indent_char);
JSON_ASSERT(indent_string.size() >= new_indent);
}
reserve_indent(new_indent);
// first n-1 elements
auto i = val.m_data.m_value.object->cbegin();
@@ -19374,11 +19370,7 @@ class serializer
// variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<std::size_t>(new_indent)), indent_char);
JSON_ASSERT(indent_string.size() >= new_indent);
}
reserve_indent(new_indent);
// first n-1 elements
for (auto i = val.m_data.m_value.array->cbegin();
@@ -19436,11 +19428,7 @@ class serializer
// variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step;
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<std::size_t>(new_indent)), indent_char);
JSON_ASSERT(indent_string.size() >= new_indent);
}
reserve_indent(new_indent);
o->write_characters(indent_string.c_str(), new_indent);
@@ -19550,6 +19538,25 @@ class serializer
}
}
/*!
@brief grow the indentation string so it can hold at least @a new_indent characters
The indentation string is grown by doubling its size, but at least to
@a new_indent characters, so that it can be used as a source for writing
@a new_indent indentation characters. The newly added characters use the
configured @ref indent_char.
@param[in] new_indent the required number of indentation characters
*/
void reserve_indent(const std::size_t new_indent)
{
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize((std::max)(indent_string.size() * 2, new_indent), indent_char);
}
JSON_ASSERT(indent_string.size() >= new_indent);
}
JSON_PRIVATE_UNLESS_TESTED:
/*!
@brief dump escaped string