mirror of
https://github.com/nlohmann/json.git
synced 2026-08-20 16:13:19 +00:00
Restore the column when ungetting a newline
The contiguous number fast path never reads the character that terminates
a number token, while scan_number() reads it and then ungets it. When that
character is a newline, get() has already cleared chars_read_current_line,
and unget() could only restore lines_read - leaving the column at 0. The
two paths therefore reported different columns for the same document:
json::parse("[01\n]") -> line 1, column 3
json::parse(stringstream) -> line 1, column 0
Remember the column the newline was read at so unget() can restore it.
Both paths now report the position the offending token actually starts at,
which also fixes the pre-existing column-0 artifact for streaming input.
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -1615,6 +1615,9 @@ scan_number_done:
|
||||
if (current == '\n')
|
||||
{
|
||||
++position.lines_read;
|
||||
// remember the column the newline was read at: chars_read_current_line
|
||||
// is about to be cleared, and a matching unget() cannot reconstruct it
|
||||
chars_read_before_newline = position.chars_read_current_line;
|
||||
position.chars_read_current_line = 0;
|
||||
}
|
||||
|
||||
@@ -1648,12 +1651,20 @@ scan_number_done:
|
||||
--position.chars_read_total;
|
||||
|
||||
// in case we "unget" a newline, we have to also decrement the lines_read
|
||||
// and restore the column that get() cleared when it saw the newline;
|
||||
// chars_read_current_line == 0 can only mean the last get() read one
|
||||
if (position.chars_read_current_line == 0)
|
||||
{
|
||||
if (position.lines_read > 0)
|
||||
{
|
||||
--position.lines_read;
|
||||
}
|
||||
|
||||
// chars_read_before_newline counts the newline itself, which is the
|
||||
// character being ungotten, hence the -1
|
||||
position.chars_read_current_line = (chars_read_before_newline > 0)
|
||||
? chars_read_before_newline - 1
|
||||
: 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1927,6 +1938,10 @@ scan_number_done:
|
||||
/// the start position of the current token
|
||||
position_t position {};
|
||||
|
||||
/// the value chars_read_current_line had when the last newline was read, so
|
||||
/// that unget() can restore the column instead of leaving it at 0
|
||||
std::size_t chars_read_before_newline = 0;
|
||||
|
||||
/// raw input token string for error messages; only populated for streaming
|
||||
/// adapters (seekable adapters reconstruct it lazily via token_string_start)
|
||||
std::vector<char_type> token_string {};
|
||||
|
||||
Reference in New Issue
Block a user