Skip to content

Commit 83335ee

Browse files
committed
Touch up PR 1345
1 parent 4b4810a commit 83335ee

1 file changed

Lines changed: 25 additions & 24 deletions

File tree

‎src/read.rs‎

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ where
383383
#[cfg(feature = "raw_value")]
384384
fn begin_raw_buffering(&mut self) {
385385
let position = self.position();
386-
// A peeked byte has advanced the iterator but is not buffered yet.
387386
self.raw_buffering_start_position = Position {
388387
line: position.line,
389388
column: position.column - usize::from(self.ch.is_some()),
@@ -401,12 +400,17 @@ where
401400
Ok(raw) => raw,
402401
Err(err) => {
403402
let prefix = &err.as_bytes()[..=err.utf8_error().valid_up_to()];
404-
let line = self.raw_buffering_start_position.line
405-
+ memchr::memchr_iter(b'\n', prefix).count();
406-
let column = match memchr::memrchr(b'\n', prefix) {
407-
Some(index) => prefix.len() - index - 1,
408-
None => self.raw_buffering_start_position.column + prefix.len(),
409-
};
403+
let line;
404+
let column;
405+
if let Some(last_newline) = memchr::memrchr(b'\n', prefix) {
406+
line = self.raw_buffering_start_position.line
407+
+ memchr::memchr_iter(b'\n', &prefix[..last_newline]).count()
408+
+ 1;
409+
column = prefix.len() - last_newline - 1;
410+
} else {
411+
line = self.raw_buffering_start_position.line;
412+
column = self.raw_buffering_start_position.column + prefix.len();
413+
}
410414
return Err(Error::syntax(
411415
ErrorCode::InvalidUnicodeCodePoint,
412416
line,
@@ -899,26 +903,23 @@ where
899903
fn as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str> {
900904
match str::from_utf8(slice) {
901905
Ok(s) => Ok(s),
902-
Err(err) => error_invalid_unicode(read, slice, err),
906+
Err(err) => {
907+
// Point at the first invalid UTF-8 byte. `read` is positioned at
908+
// the closing quote of the string whose content is `slice`. If the
909+
// JSON string contained escape sequences, the column position is
910+
// approximate.
911+
let position = read.position();
912+
Err(Error::syntax(
913+
ErrorCode::InvalidUnicodeCodePoint,
914+
position.line,
915+
position
916+
.column
917+
.saturating_sub(slice.len() - err.valid_up_to()),
918+
))
919+
}
903920
}
904921
}
905922

906-
/// Point at the first invalid UTF-8 byte instead of the end of the current
907-
/// string / value. `read` is positioned just past the last consumed byte of
908-
/// `slice` (for example the closing quote of a string).
909-
fn error_invalid_unicode<'de, R, T>(read: &R, slice: &[u8], err: str::Utf8Error) -> Result<T>
910-
where
911-
R: ?Sized + Read<'de>,
912-
{
913-
let position = read.position();
914-
let back = slice.len() - err.valid_up_to();
915-
Err(Error::syntax(
916-
ErrorCode::InvalidUnicodeCodePoint,
917-
position.line,
918-
position.column.saturating_sub(back),
919-
))
920-
}
921-
922923
/// Parses a JSON escape sequence and appends it into the scratch space. Assumes
923924
/// the previous byte read was a backslash.
924925
fn parse_escape<'de, R: Read<'de>>(

0 commit comments

Comments
 (0)