From 7ed17a4456fc51a8be114109bae949894a74c17f Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 5 Sep 2026 22:05:57 +0200 Subject: [PATCH] Extract current_is_whitespace() to deduplicate skip_whitespace()'s two whitespace checks Signed-off-by: Niels Lohmann --- include/nlohmann/detail/input/lexer.hpp | 20 ++++++++++++++------ single_include/nlohmann/json.hpp | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 0590e1da7..c241e793b 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -1704,6 +1704,12 @@ scan_number_done: return true; } + /// whether `current` is one of the four JSON whitespace characters + bool current_is_whitespace() const noexcept + { + return current == ' ' || current == '\t' || current == '\n' || current == '\r'; + } + void skip_whitespace() { // the first character may be a pending unget() left over from the @@ -1712,6 +1718,11 @@ scan_number_done: // nothing below calls unget() get(); + if (!current_is_whitespace()) + { + return; + } + // this is written as an if-guarded do-while (rather than a plain // while loop) because that shape is what lets both GCC and Clang // keep the input adapter's read pointer in a register across @@ -1719,14 +1730,11 @@ scan_number_done: // optimization in testing, turning long whitespace runs (e.g. the // indentation of pretty-printed JSON) from a register-only loop // into one that reloads the pointer from memory every character - if (current == ' ' || current == '\t' || current == '\n' || current == '\r') + do { - do - { - get_ignoring_pending_unget(); - } - while (current == ' ' || current == '\t' || current == '\n' || current == '\r'); + get_ignoring_pending_unget(); } + while (current_is_whitespace()); } token_type scan() diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2aa86b0d1..f247453e8 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -9487,6 +9487,12 @@ scan_number_done: return true; } + /// whether `current` is one of the four JSON whitespace characters + bool current_is_whitespace() const noexcept + { + return current == ' ' || current == '\t' || current == '\n' || current == '\r'; + } + void skip_whitespace() { // the first character may be a pending unget() left over from the @@ -9495,6 +9501,11 @@ scan_number_done: // nothing below calls unget() get(); + if (!current_is_whitespace()) + { + return; + } + // this is written as an if-guarded do-while (rather than a plain // while loop) because that shape is what lets both GCC and Clang // keep the input adapter's read pointer in a register across @@ -9502,14 +9513,11 @@ scan_number_done: // optimization in testing, turning long whitespace runs (e.g. the // indentation of pretty-printed JSON) from a register-only loop // into one that reloads the pointer from memory every character - if (current == ' ' || current == '\t' || current == '\n' || current == '\r') + do { - do - { - get_ignoring_pending_unget(); - } - while (current == ' ' || current == '\t' || current == '\n' || current == '\r'); + get_ignoring_pending_unget(); } + while (current_is_whitespace()); } token_type scan()