Use the shared recursion limit in diff()

diff_depth_limit() is gone in favor of detail::recursion_depth_limit().

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-09-24 06:53:49 +02:00
parent 0dfd72df6c
commit f4d08227bc
3 changed files with 15 additions and 29 deletions
+7 -14
View File
@@ -5386,23 +5386,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// compares them with operator== on the way, which recurses as well, // compares them with operator== on the way, which recurses as well,
// so values nested deeply enough used to exhaust the call stack. // so values nested deeply enough used to exhaust the call stack.
// Both only descend as far as the source is nested, so a source // Both only descend as far as the source is nested, so a source
// nested no more than diff_depth_limit() levels deep - all but a // nested no more than detail::recursion_depth_limit() levels deep -
// vanishing minority - is diffed recursively as before; deeper ones // all but a vanishing minority - is diffed recursively as before;
// are diffed without the call stack. // deeper ones are diffed without the call stack.
if (JSON_HEDLEY_LIKELY(!nesting_exceeds(source, diff_depth_limit()))) if (JSON_HEDLEY_LIKELY(!nesting_exceeds(source, detail::recursion_depth_limit())))
{ {
return diff_recursively(source, target, path); return diff_recursively(source, target, path);
} }
return diff_iteratively(source, target, path); return diff_iteratively(source, target, path);
} }
JSON_PRIVATE_UNLESS_TESTED:
/// the nesting depth up to which @ref diff recurses
static constexpr std::size_t diff_depth_limit() noexcept
{
return 128;
}
private: private:
/*! /*!
@brief whether @a j is nested more than @a limit levels deep @brief whether @a j is nested more than @a limit levels deep
@@ -5479,7 +5472,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
basic_json added_ops{}; basic_json added_ops{};
}; };
/// @ref diff for a @a source nested no more than @ref diff_depth_limit levels deep /// @ref diff for a @a source nested no more than @ref detail::recursion_depth_limit levels deep
static basic_json diff_recursively(const basic_json& source, const basic_json& target, static basic_json diff_recursively(const basic_json& source, const basic_json& target,
const string_t& path) const string_t& path)
{ {
@@ -5719,7 +5712,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@brief @ref diff without the call stack @brief @ref diff without the call stack
Produces the same patch as @ref diff_recursively. Only used for a source Produces the same patch as @ref diff_recursively. Only used for a source
nested more deeply than @ref diff_depth_limit; any arrays and objects nested more deeply than @ref detail::recursion_depth_limit; any arrays and objects
below it that are not nested that deeply are diffed recursively. below it that are not nested that deeply are diffed recursively.
*/ */
static basic_json diff_iteratively(const basic_json& source, const basic_json& target, static basic_json diff_iteratively(const basic_json& source, const basic_json& target,
@@ -5762,7 +5755,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// arrays and objects that are not nested too deeply for the call // arrays and objects that are not nested too deeply for the call
// stack are diffed recursively, which can skip equal parts // stack are diffed recursively, which can skip equal parts
if (!nesting_exceeds(s, diff_depth_limit())) if (!nesting_exceeds(s, detail::recursion_depth_limit()))
{ {
const basic_json partial = diff_recursively(s, t, current_path); const basic_json partial = diff_recursively(s, t, current_path);
result.insert(result.end(), partial.begin(), partial.end()); result.insert(result.end(), partial.begin(), partial.end());
+7 -14
View File
@@ -29745,23 +29745,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// compares them with operator== on the way, which recurses as well, // compares them with operator== on the way, which recurses as well,
// so values nested deeply enough used to exhaust the call stack. // so values nested deeply enough used to exhaust the call stack.
// Both only descend as far as the source is nested, so a source // Both only descend as far as the source is nested, so a source
// nested no more than diff_depth_limit() levels deep - all but a // nested no more than detail::recursion_depth_limit() levels deep -
// vanishing minority - is diffed recursively as before; deeper ones // all but a vanishing minority - is diffed recursively as before;
// are diffed without the call stack. // deeper ones are diffed without the call stack.
if (JSON_HEDLEY_LIKELY(!nesting_exceeds(source, diff_depth_limit()))) if (JSON_HEDLEY_LIKELY(!nesting_exceeds(source, detail::recursion_depth_limit())))
{ {
return diff_recursively(source, target, path); return diff_recursively(source, target, path);
} }
return diff_iteratively(source, target, path); return diff_iteratively(source, target, path);
} }
JSON_PRIVATE_UNLESS_TESTED:
/// the nesting depth up to which @ref diff recurses
static constexpr std::size_t diff_depth_limit() noexcept
{
return 128;
}
private: private:
/*! /*!
@brief whether @a j is nested more than @a limit levels deep @brief whether @a j is nested more than @a limit levels deep
@@ -29838,7 +29831,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
basic_json added_ops{}; basic_json added_ops{};
}; };
/// @ref diff for a @a source nested no more than @ref diff_depth_limit levels deep /// @ref diff for a @a source nested no more than @ref detail::recursion_depth_limit levels deep
static basic_json diff_recursively(const basic_json& source, const basic_json& target, static basic_json diff_recursively(const basic_json& source, const basic_json& target,
const string_t& path) const string_t& path)
{ {
@@ -30078,7 +30071,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@brief @ref diff without the call stack @brief @ref diff without the call stack
Produces the same patch as @ref diff_recursively. Only used for a source Produces the same patch as @ref diff_recursively. Only used for a source
nested more deeply than @ref diff_depth_limit; any arrays and objects nested more deeply than @ref detail::recursion_depth_limit; any arrays and objects
below it that are not nested that deeply are diffed recursively. below it that are not nested that deeply are diffed recursively.
*/ */
static basic_json diff_iteratively(const basic_json& source, const basic_json& target, static basic_json diff_iteratively(const basic_json& source, const basic_json& target,
@@ -30121,7 +30114,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// arrays and objects that are not nested too deeply for the call // arrays and objects that are not nested too deeply for the call
// stack are diffed recursively, which can skip equal parts // stack are diffed recursively, which can skip equal parts
if (!nesting_exceeds(s, diff_depth_limit())) if (!nesting_exceeds(s, detail::recursion_depth_limit()))
{ {
const basic_json partial = diff_recursively(s, t, current_path); const basic_json partial = diff_recursively(s, t, current_path);
result.insert(result.end(), partial.begin(), partial.end()); result.insert(result.end(), partial.begin(), partial.end());
+1 -1
View File
@@ -1813,7 +1813,7 @@ TEST_CASE("JSON patch: diff of deeply nested values")
SECTION("the diff reproduces the target at every depth") SECTION("the diff reproduces the target at every depth")
{ {
// every depth on either side of the nesting depth up to which diff() // every depth on either side of the nesting depth up to which diff()
// recurses (basic_json::diff_depth_limit(), 128) // recurses (detail::recursion_depth_limit(), 128)
for (std::size_t depth = 0; depth <= 300; ++depth) for (std::size_t depth = 0; depth <= 300; ++depth)
{ {
CAPTURE(depth); CAPTURE(depth);