Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #228 from dtolnay/octal
Browse files Browse the repository at this point in the history
Treat YAML 1.1 style octals with sign as string, not base 10 number
  • Loading branch information
dtolnay authored Dec 13, 2021
2 parents 983b5b3 + 57f2e66 commit f850da3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,10 @@ where
return visitor.visit_i64(n);
}
}
if v.len() > 1 && v.starts_with('0') && v.bytes().all(|b| b.is_ascii_digit()) {
if {
let v = v.trim_start_matches(&['-', '+'][..]);
v.len() > 1 && v.starts_with('0') && v[1..].bytes().all(|b| b.is_ascii_digit())
} {
// After handling the different number encodings above if we are left
// with leading zero(s) followed by numeric characters this is in fact a
// string according to the YAML 1.2 spec.
Expand Down
12 changes: 11 additions & 1 deletion tests/test_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,17 @@ fn test_numbers() {
let value = serde_yaml::from_str::<Value>(yaml).unwrap();
match value {
Value::Number(number) => assert_eq!(number.to_string(), expected),
_ => panic!("expected number"),
_ => panic!("expected number. input={:?}, result={:?}", yaml, value),
}
}

// NOT numbers.
let cases = ["0127", "+0127", "-0127"];
for yaml in &cases {
let value = serde_yaml::from_str::<Value>(yaml).unwrap();
match value {
Value::String(string) => assert_eq!(string, *yaml),
_ => panic!("expected string. input={:?}, result={:?}", yaml, value),
}
}
}
Expand Down

0 comments on commit f850da3

Please sign in to comment.