Skip to content

Commit

Permalink
Add yaml 1.1 octal parsing behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
firstdorsal committed Feb 10, 2025
1 parent 36640b3 commit 750c7e8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,16 @@ fn parse_unsigned_int<T>(
return Some(int);
}
}

if unpositive.starts_with("0")
&& unpositive.len() > 1
&& unpositive.chars().all(|c| c.is_digit(8))
{
if let Ok(int) = from_str_radix(unpositive, 8) {
return Some(int);
}
}

if unpositive.starts_with(['+', '-']) {
return None;
}
Expand Down
30 changes: 28 additions & 2 deletions tests/test_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ where
serde_yaml_ng::from_str::<serde::de::IgnoredAny>(yaml).unwrap();
}

#[test]
fn test_yaml1_1_octal_values() {
let yaml = r#"
defaultValue: 0400
stringValue: "0400"
"#;
let value: Value = serde_yaml_ng::from_str(yaml).unwrap();

if let Value::Mapping(mapping) = value {
assert_eq!(
mapping
.get(&Value::String("defaultValue".to_string()))
.unwrap(),
&Value::Number(Number::from(256))
);
assert_eq!(
mapping
.get(&Value::String("stringValue".to_string()))
.unwrap(),
&Value::String("0400".to_string())
);
} else {
panic!("Expected a mapping");
}
}

#[test]
fn test_borrowed() {
let yaml = indoc! {"
Expand Down Expand Up @@ -438,8 +464,8 @@ fn test_numbers() {

// NOT numbers.
let cases = [
"0127", "+0127", "-0127", "++.inf", "+-.inf", "++1", "+-1", "-+1", "--1", "0x+1", "0x-1",
"-0x+1", "-0x-1", "++0x1", "+-0x1", "-+0x1", "--0x1",
"++.inf", "+-.inf", "++1", "+-1", "-+1", "--1", "0x+1", "0x-1", "-0x+1", "-0x-1", "++0x1",
"+-0x1", "-+0x1", "--0x1",
];
for yaml in &cases {
let value = serde_yaml_ng::from_str::<Value>(yaml).unwrap();
Expand Down
6 changes: 0 additions & 6 deletions tests/test_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ fn test_nan() {
assert_eq!(pos_nan, different_pos_nan);
}

#[test]
fn test_digits() {
let num_string = serde_yaml_ng::from_str::<Value>("01").unwrap();
assert!(num_string.is_string());
}

#[test]
fn test_into_deserializer() {
#[derive(Debug, Deserialize, PartialEq)]
Expand Down

0 comments on commit 750c7e8

Please sign in to comment.