mirror of
https://github.com/nlohmann/json.git
synced 2026-07-12 05:25:09 +00:00
Extend memcpy fast path to sized sentinels (e.g. std::counted_iterator)
Extend the memcpy fast path in iterator_input_adapter to sized sentinels of a different type, not just same-type iterator pairs. std::counted_iterator paired with std::default_sentinel_t already satisfies std::contiguous_iterator and std::sized_sentinel_for, so std::ranges::distance (C++20) lets that combination reach the fast path too, instead of silently falling back to the byte-by-byte path. - iterator_is_contiguous now also allows std::sized_sentinel_for<SentinelType, IteratorType> under C++20, gated the same way as the existing std::contiguous_iterator detection. - get_elements_impl's fast path uses std::ranges::distance under C++20 (works for both same-type and sized-sentinel pairs) and falls back to std::distance pre-C++20, where SentinelType is always IteratorType. - Add a C++20-only test exercising json::parse/accept with std::counted_iterator + std::default_sentinel_t. - Document std::default_sentinel_t + std::counted_iterator as a SentinelType example across parse.md, accept.md, sax_parse.md, and the five from_*.md pages, replacing an earlier ambiguously worded bullet. Addresses review feedback: https://github.com/nlohmann/json/pull/5265#discussion_r3564237584 Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -6,6 +6,13 @@
|
||||
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// cmake/test.cmake selects the C++ standard versions with which to build a
|
||||
// unit test based on the presence of JSON_HAS_CPP_<VERSION> macros.
|
||||
// When using macros that are only defined for particular versions of the standard
|
||||
// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding
|
||||
// version macro in a comment close by, like this:
|
||||
// JSON_HAS_CPP_<VERSION> (do not remove; see note at top of file)
|
||||
|
||||
#include "doctest_compatibility.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
@@ -13,6 +20,10 @@ using nlohmann::json;
|
||||
|
||||
#include <list>
|
||||
|
||||
#if defined(__cpp_lib_concepts) && defined(JSON_HAS_CPP_20)
|
||||
#include <iterator>
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
TEST_CASE("Use arbitrary stdlib container")
|
||||
@@ -201,4 +212,21 @@ TEST_CASE("Parse with heterogeneous iterator and sentinel types")
|
||||
CHECK(j2.at(0) == 1);
|
||||
}
|
||||
|
||||
#if defined(__cpp_lib_concepts) && defined(JSON_HAS_CPP_20)
|
||||
// JSON_HAS_CPP_20 (do not remove; see note at top of file)
|
||||
TEST_CASE("Parse with std::counted_iterator and std::default_sentinel_t")
|
||||
{
|
||||
const std::string json_str = R"({"key":"value","array":[1,2,3]})";
|
||||
const auto len = static_cast<std::iter_difference_t<std::string::const_iterator>>(json_str.size());
|
||||
|
||||
std::counted_iterator first(json_str.begin(), len);
|
||||
const json j = json::parse(first, std::default_sentinel);
|
||||
CHECK(j["key"] == "value");
|
||||
CHECK(j["array"].size() == 3);
|
||||
|
||||
std::counted_iterator first2(json_str.begin(), len);
|
||||
CHECK(json::accept(first2, std::default_sentinel));
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user