From d479d346774fd2d4b4752a5ec13c9cd3573a647b Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Sep 2026 22:42:18 +0000 Subject: [PATCH] Un-nest the run-length ternary in the string escaper clang-tidy's readability-avoid-nested-conditional-operator (newly enforced by the ci_clang_tidy job's clang) rejected the nested ?: that picked the run length: EnsureAscii ? (is_ascii_copyable(...) ? scan : 0) : bulk. Compute it in an immediately-invoked lambda instead. The value stays const and the laziness is unchanged - the ascii scan still runs only on the ensure_ascii path when the first byte is copyable, and string_bulk_run only on the other path (EnsureAscii is a template bool, so the dead branch is folded away). Output is byte-for-byte identical; single_include regenerated. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01L1oJ2ggRHS37zeVe94QTA1 Signed-off-by: Claude --- include/nlohmann/detail/output/serializer.hpp | 15 ++++++++++----- single_include/nlohmann/json.hpp | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 84a9c370d..8650694c0 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -850,11 +850,16 @@ class serializer // for the scan. Without it, text whose characters all have to be // escaped - CJK under ensure_ascii, where every byte is >= 0x80 - // runs the scanner once per character only to be told zero. - const std::size_t run = EnsureAscii - ? (is_ascii_copyable(data[i]) - ? find_ascii_copyable_run(data + i, s.size() - i) - : 0) - : string_bulk_run(data + i, s.size() - i); + const std::size_t run = [&]() -> std::size_t + { + if (EnsureAscii) + { + return is_ascii_copyable(data[i]) + ? find_ascii_copyable_run(data + i, s.size() - i) + : 0; + } + return string_bulk_run(data + i, s.size() - i); + }(); if (run != 0) { // emit any bytes still pending in string_buffer first to diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 0d99e22ab..4d1911295 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -21910,11 +21910,16 @@ class serializer // for the scan. Without it, text whose characters all have to be // escaped - CJK under ensure_ascii, where every byte is >= 0x80 - // runs the scanner once per character only to be told zero. - const std::size_t run = EnsureAscii - ? (is_ascii_copyable(data[i]) - ? find_ascii_copyable_run(data + i, s.size() - i) - : 0) - : string_bulk_run(data + i, s.size() - i); + const std::size_t run = [&]() -> std::size_t + { + if (EnsureAscii) + { + return is_ascii_copyable(data[i]) + ? find_ascii_copyable_run(data + i, s.size() - i) + : 0; + } + return string_bulk_run(data + i, s.size() - i); + }(); if (run != 0) { // emit any bytes still pending in string_buffer first to