From 780ff80b179f40f2c87bcb044496307d30c045d7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 5 Sep 2026 21:50:34 +0200 Subject: [PATCH] Share the position-counter bump between get() and get_ignoring_pending_unget() Signed-off-by: Niels Lohmann --- include/nlohmann/detail/input/lexer.hpp | 15 +++++++++++---- single_include/nlohmann/json.hpp | 15 +++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 5861bd49d..0590e1da7 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -1446,8 +1446,7 @@ scan_number_done: */ char_int_type get() { - ++position.chars_read_total; - ++position.chars_read_current_line; + advance_position(); if (next_unget) { @@ -1462,6 +1461,15 @@ scan_number_done: return track_after_read(); } + /// shared head of get() / get_ignoring_pending_unget(): bump the + /// per-character position counters (line-count-on-'\n' bookkeeping is + /// handled afterwards, in track_after_read(), once `current` is known) + void advance_position() noexcept + { + ++position.chars_read_total; + ++position.chars_read_current_line; + } + /// shared tail of get() / get_ignoring_pending_unget(): capture the /// character for error messages (if needed) and update line/column /// bookkeeping for the character now in `current` @@ -1497,8 +1505,7 @@ scan_number_done: { JSON_ASSERT(!next_unget); - ++position.chars_read_total; - ++position.chars_read_current_line; + advance_position(); current = ia.get_character(); return track_after_read(); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2a787ce8a..2aa86b0d1 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -9229,8 +9229,7 @@ scan_number_done: */ char_int_type get() { - ++position.chars_read_total; - ++position.chars_read_current_line; + advance_position(); if (next_unget) { @@ -9245,6 +9244,15 @@ scan_number_done: return track_after_read(); } + /// shared head of get() / get_ignoring_pending_unget(): bump the + /// per-character position counters (line-count-on-'\n' bookkeeping is + /// handled afterwards, in track_after_read(), once `current` is known) + void advance_position() noexcept + { + ++position.chars_read_total; + ++position.chars_read_current_line; + } + /// shared tail of get() / get_ignoring_pending_unget(): capture the /// character for error messages (if needed) and update line/column /// bookkeeping for the character now in `current` @@ -9280,8 +9288,7 @@ scan_number_done: { JSON_ASSERT(!next_unget); - ++position.chars_read_total; - ++position.chars_read_current_line; + advance_position(); current = ia.get_character(); return track_after_read();