:recycle: add reserve_indent helper

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp
index 9dabecf..e0b5dcc 100644
--- a/include/nlohmann/detail/output/serializer.hpp
+++ b/include/nlohmann/detail/output/serializer.hpp
@@ -126,11 +126,7 @@
 
                     // 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();
@@ -200,11 +196,7 @@
 
                     // 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();
@@ -262,11 +254,7 @@
 
                     // 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);
 
@@ -376,6 +364,25 @@
         }
     }
 
+    /*!
+    @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
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp
index 662c9af..7408238 100644
--- a/single_include/nlohmann/json.hpp
+++ b/single_include/nlohmann/json.hpp
@@ -19300,11 +19300,7 @@
 
                     // 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 @@
 
                     // 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 @@
 
                     // 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 @@
         }
     }
 
+    /*!
+    @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
diff --git a/tests/src/unit-serialization.cpp b/tests/src/unit-serialization.cpp
index c0f2a8e..f55ed84 100644
--- a/tests/src/unit-serialization.cpp
+++ b/tests/src/unit-serialization.cpp
@@ -311,14 +311,16 @@
     SECTION("round-trip dump/parse")
     {
         constexpr std::array<long double, 13> values =
-        {{
-            0.0L, -0.0L, 1.0L, -1.0L,
-            0.5L, -0.5L, 1.5L, -2.25L,
-            1.23e45L, 1.23e-45L,
-            (std::numeric_limits<long double>::min)(),
-            std::numeric_limits<long double>::lowest(),
-            (std::numeric_limits<long double>::max)()
-        }};
+        {
+            {
+                0.0L, -0.0L, 1.0L, -1.0L,
+                0.5L, -0.5L, 1.5L, -2.25L,
+                1.23e45L, 1.23e-45L,
+                (std::numeric_limits<long double>::min)(),
+                std::numeric_limits<long double>::lowest(),
+                (std::numeric_limits<long double>::max)()
+            }
+        };
 
         for (long double v : values)
         {