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

Commit f850da3

Browse files
authored
Merge pull request #228 from dtolnay/octal
Treat YAML 1.1 style octals with sign as string, not base 10 number
2 parents 983b5b3 + 57f2e66 commit f850da3

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎src/de.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,10 @@ where
987987
return visitor.visit_i64(n);
988988
}
989989
}
990-
if v.len() > 1 && v.starts_with('0') && v.bytes().all(|b| b.is_ascii_digit()) {
990+
if {
991+
let v = v.trim_start_matches(&['-', '+'][..]);
992+
v.len() > 1 && v.starts_with('0') && v[1..].bytes().all(|b| b.is_ascii_digit())
993+
} {
991994
// After handling the different number encodings above if we are left
992995
// with leading zero(s) followed by numeric characters this is in fact a
993996
// string according to the YAML 1.2 spec.

‎tests/test_de.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,17 @@ fn test_numbers() {
339339
let value = serde_yaml::from_str::<Value>(yaml).unwrap();
340340
match value {
341341
Value::Number(number) => assert_eq!(number.to_string(), expected),
342-
_ => panic!("expected number"),
342+
_ => panic!("expected number. input={:?}, result={:?}", yaml, value),
343+
}
344+
}
345+
346+
// NOT numbers.
347+
let cases = ["0127", "+0127", "-0127"];
348+
for yaml in &cases {
349+
let value = serde_yaml::from_str::<Value>(yaml).unwrap();
350+
match value {
351+
Value::String(string) => assert_eq!(string, *yaml),
352+
_ => panic!("expected string. input={:?}, result={:?}", yaml, value),
343353
}
344354
}
345355
}

0 commit comments

Comments
 (0)