Fix the clang-tidy findings in json_view's dump and its tests

- the SSE2 loop of the string scan is within NOLINTBEGIN/END
  (portability-simd-intrinsics), as in to_chars.hpp
- depth_counter<false>'s leave() and get() keep the interface of
  depth_counter<true> instead of being static
- write_ascii() compares the room left as a size_t variable
- the test's texts are a std::array, and a JSON fragment with escapes
  is a raw string literal

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-22 19:46:31 +02:00
1 parent 86049706a0
commit 2cf9df3eae
3 files changed
+19 -11

No files matched your search

+6 -3
View File
@@ -141,6 +141,7 @@ inline std::size_t plain_output_run(const unsigned char* s, std::size_t n) noexc
#if NLOHMANN_VIEW_SSE2 #if NLOHMANN_VIEW_SSE2
// (16 bytes per step; with NEON, the mask of a step costs more than it saves on the // (16 bytes per step; with NEON, the mask of a step costs more than it saves on the
// short runs that are typical here) // short runs that are typical here)
// NOLINTBEGIN(portability-simd-intrinsics)
for (; i + 16 <= n; i += 16) for (; i + 16 <= n; i += 16)
{ {
const __m128i in = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(s + i))); // NOLINT(bugprone-casting-through-void) const __m128i in = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(s + i))); // NOLINT(bugprone-casting-through-void)
@@ -153,6 +154,7 @@ inline std::size_t plain_output_run(const unsigned char* s, std::size_t n) noexc
return i + static_cast<std::size_t>(count_trailing_zeros(mask)); return i + static_cast<std::size_t>(count_trailing_zeros(mask));
} }
} }
// NOLINTEND(portability-simd-intrinsics)
#endif #endif
constexpr std::uint64_t ones = 0x0101010101010101ull; constexpr std::uint64_t ones = 0x0101010101010101ull;
constexpr std::uint64_t high = 0x8080808080808080ull; constexpr std::uint64_t high = 0x8080808080808080ull;
@@ -372,11 +374,11 @@ template<>
struct depth_counter<false> struct depth_counter<false>
{ {
void enter() noexcept {} void enter() noexcept {}
std::size_t leave() noexcept std::size_t leave() noexcept // NOLINT(readability-convert-member-functions-to-static): same interface as depth_counter<true>
{ {
return 0; return 0;
} }
std::size_t get() const noexcept std::size_t get() const noexcept // NOLINT(readability-convert-member-functions-to-static): same interface as depth_counter<true>
{ {
return 0; return 0;
} }
@@ -536,7 +538,8 @@ class view_serializer
// room for k bytes // room for k bytes
const auto room = [&](std::size_t k) const auto room = [&](std::size_t k)
{ {
if (NLOHMANN_VIEW_UNLIKELY(static_cast<std::size_t>(lim - w) < k)) const auto left = static_cast<std::size_t>(lim - w);
if (NLOHMANN_VIEW_UNLIKELY(left < k))
{ {
m_out.set_cursor(w); m_out.set_cursor(w);
m_out.reserve(k); m_out.reserve(k);
+6 -3
View File
@@ -6208,6 +6208,7 @@ inline std::size_t plain_output_run(const unsigned char* s, std::size_t n) noexc
#if NLOHMANN_VIEW_SSE2 #if NLOHMANN_VIEW_SSE2
// (16 bytes per step; with NEON, the mask of a step costs more than it saves on the // (16 bytes per step; with NEON, the mask of a step costs more than it saves on the
// short runs that are typical here) // short runs that are typical here)
// NOLINTBEGIN(portability-simd-intrinsics)
for (; i + 16 <= n; i += 16) for (; i + 16 <= n; i += 16)
{ {
const __m128i in = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(s + i))); // NOLINT(bugprone-casting-through-void) const __m128i in = _mm_loadu_si128(static_cast<const __m128i*>(static_cast<const void*>(s + i))); // NOLINT(bugprone-casting-through-void)
@@ -6220,6 +6221,7 @@ inline std::size_t plain_output_run(const unsigned char* s, std::size_t n) noexc
return i + static_cast<std::size_t>(count_trailing_zeros(mask)); return i + static_cast<std::size_t>(count_trailing_zeros(mask));
} }
} }
// NOLINTEND(portability-simd-intrinsics)
#endif #endif
constexpr std::uint64_t ones = 0x0101010101010101ull; constexpr std::uint64_t ones = 0x0101010101010101ull;
constexpr std::uint64_t high = 0x8080808080808080ull; constexpr std::uint64_t high = 0x8080808080808080ull;
@@ -6439,11 +6441,11 @@ template<>
struct depth_counter<false> struct depth_counter<false>
{ {
void enter() noexcept {} void enter() noexcept {}
std::size_t leave() noexcept std::size_t leave() noexcept // NOLINT(readability-convert-member-functions-to-static): same interface as depth_counter<true>
{ {
return 0; return 0;
} }
std::size_t get() const noexcept std::size_t get() const noexcept // NOLINT(readability-convert-member-functions-to-static): same interface as depth_counter<true>
{ {
return 0; return 0;
} }
@@ -6603,7 +6605,8 @@ class view_serializer
// room for k bytes // room for k bytes
const auto room = [&](std::size_t k) const auto room = [&](std::size_t k)
{ {
if (NLOHMANN_VIEW_UNLIKELY(static_cast<std::size_t>(lim - w) < k)) const auto left = static_cast<std::size_t>(lim - w);
if (NLOHMANN_VIEW_UNLIKELY(left < k))
{ {
m_out.set_cursor(w); m_out.set_cursor(w);
m_out.reserve(k); m_out.reserve(k);
+7 -5
View File
@@ -200,11 +200,13 @@ TEST_CASE("json_view dump")
{ {
std::string content(length, 'a'); std::string content(length, 'a');
content.insert(position, special); content.insert(position, special);
const std::string texts[] = const std::array<std::string, 3> texts =
{ {
"\"" + content + "\"", {
"[\"" + content + "\"]", "\"" + content + "\"",
"{\"" + content + "\":1}", "[\"" + content + "\"]",
"{\"" + content + "\":1}",
}
}; };
for (const std::string& text : texts) for (const std::string& text : texts)
{ {
@@ -291,7 +293,7 @@ TEST_CASE("json_view dump")
std::string text; std::string text;
for (int i = 0; i < depth; ++i) for (int i = 0; i < depth; ++i)
{ {
text += i % 2 == 0 ? "[1," : "{\"k\":2,\"\\u00e9\":"; text += i % 2 == 0 ? "[1," : R"({"k":2,"\u00e9":)";
} }
text += "[]"; text += "[]";
for (int i = depth - 1; i >= 0; --i) for (int i = depth - 1; i >= 0; --i)