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

Treat YAML 1.1 style octals with sign as string, not base 10 number #228

Merged
merged 2 commits into from Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/de.rs
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
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