Merge pull request #320 from ehuss/crlf-error-index

Fix error line/column when using CRLF line endings.
This commit is contained in:
Eric Huss 2019-08-13 14:51:03 -07:00 committed by GitHub
commit 03c4f5349c
2 changed files with 31 additions and 1 deletions

View file

@ -1775,7 +1775,10 @@ impl<'a> Deserializer<'a> {
/// All indexes are 0-based. /// All indexes are 0-based.
fn to_linecol(&self, offset: usize) -> (usize, usize) { fn to_linecol(&self, offset: usize) -> (usize, usize) {
let mut cur = 0; let mut cur = 0;
for (i, line) in self.input.lines().enumerate() { // Use split_terminator instead of lines so that if there is a `\r`,
// it is included in the offset calculation. The `+1` values below
// account for the `\n`.
for (i, line) in self.input.split_terminator('\n').enumerate() {
if cur + line.len() + 1 > offset { if cur + line.len() + 1 > offset {
return (i, offset - cur); return (i, offset - cur);
} }

View file

@ -323,3 +323,30 @@ fn serde_derive_deserialize_errors() {
"invalid type: integer `1`, expected a string for key `p_b` at line 4 column 34" "invalid type: integer `1`, expected a string for key `p_b` at line 4 column 34"
); );
} }
#[test]
fn error_handles_crlf() {
bad!(
"\r\n\
[t1]\r\n\
[t2]\r\n\
a = 1\r\n\
a = 2\r\n\
",
toml::Value,
"duplicate key: `a` for key `t2` at line 3 column 1"
);
// Should be the same as above.
bad!(
"\n\
[t1]\n\
[t2]\n\
a = 1\n\
a = 2\n\
",
toml::Value,
"duplicate key: `a` for key `t2` at line 3 column 1"
);
}