From bba3fdbb65eea6620ab0a5064f7950b7e30af8f6 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 24 Sep 2026 22:37:46 +0200 Subject: [PATCH] Let the BSON index-name helper only fill its output parameter It returned a reference to the string it filled, so callers held a second name for index_name. Addresses review feedback. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/output/binary_writer.hpp | 14 +++++++------- single_include/nlohmann/json.hpp | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index ef26732b9..5a0722a18 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1331,16 +1331,15 @@ class binary_writer }; /*! - @brief the name BSON gives the array element with index @a index + @brief creates the name BSON gives the array element with index @a index @param[out] name receives the decimal index */ - static const string_t& bson_index_name(const std::size_t index, string_t& name) + static void create_bson_index_name(const std::size_t index, string_t& name) { // the index is built as a std::string; convert explicitly, as the // two are only implicitly convertible for some string types const auto key = std::to_string(index); name = string_t(key.data(), key.size()); - return name; } /*! @@ -1400,7 +1399,8 @@ class binary_writer while (nested == nullptr && current.index < array.size()) { const BasicJsonType& el = array[current.index]; - current.entries_size += calc_bson_entry_header_size(bson_index_name(current.index, index_name), el); + create_bson_index_name(current.index, index_name); + current.entries_size += calc_bson_entry_header_size(index_name, el); ++current.index; if (el.is_structured()) { @@ -1489,16 +1489,16 @@ class binary_writer while (nested == nullptr && current.index < array.size()) { const BasicJsonType& el = array[current.index]; - const string_t& name = bson_index_name(current.index, index_name); + create_bson_index_name(current.index, index_name); ++current.index; if (el.is_structured()) { - nested_name = &name; + nested_name = &index_name; nested = ⪙ } else { - write_bson_value(name, el); + write_bson_value(index_name, el); } } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 9d059a01e..abd01cde2 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20359,16 +20359,15 @@ class binary_writer }; /*! - @brief the name BSON gives the array element with index @a index + @brief creates the name BSON gives the array element with index @a index @param[out] name receives the decimal index */ - static const string_t& bson_index_name(const std::size_t index, string_t& name) + static void create_bson_index_name(const std::size_t index, string_t& name) { // the index is built as a std::string; convert explicitly, as the // two are only implicitly convertible for some string types const auto key = std::to_string(index); name = string_t(key.data(), key.size()); - return name; } /*! @@ -20428,7 +20427,8 @@ class binary_writer while (nested == nullptr && current.index < array.size()) { const BasicJsonType& el = array[current.index]; - current.entries_size += calc_bson_entry_header_size(bson_index_name(current.index, index_name), el); + create_bson_index_name(current.index, index_name); + current.entries_size += calc_bson_entry_header_size(index_name, el); ++current.index; if (el.is_structured()) { @@ -20517,16 +20517,16 @@ class binary_writer while (nested == nullptr && current.index < array.size()) { const BasicJsonType& el = array[current.index]; - const string_t& name = bson_index_name(current.index, index_name); + create_bson_index_name(current.index, index_name); ++current.index; if (el.is_structured()) { - nested_name = &name; + nested_name = &index_name; nested = ⪙ } else { - write_bson_value(name, el); + write_bson_value(index_name, el); } } }