Skip to content

Commit 9f56534

Browse files
committed
Fix issue with deeply indented block scalars.
Fixes #29.
1 parent 0eb7936 commit 9f56534

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/scanner.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,10 @@ impl<T: Iterator<Item = char>> Scanner<T> {
18001800
while !self.buffer.is_empty() && self.mark.col < indent && self.ch() == ' ' {
18011801
self.skip_blank();
18021802
}
1803-
if !(!self.buffer.is_empty() && self.mark.col < indent && self.ch() == ' ') {
1803+
// If we reached our indent, we can break. We must also break if we have
1804+
// reached content or EOF; that is, the buffer is not empty and the next
1805+
// character is not a space.
1806+
if self.mark.col == indent || (!self.buffer.is_empty() && self.ch() != ' ') {
18041807
break;
18051808
}
18061809
}

tests/basic.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,30 @@ foobar";
260260
);
261261
}
262262

263+
#[test]
264+
fn test_large_block_scalar_indent() {
265+
// https://github.com/Ethiraric/yaml-rust2/issues/29
266+
// Tests the `loop` fallback of `skip_block_scalar_indent`. The indent in the YAML string must
267+
// be greater than `BUFFER_LEN - 2`. The second line is further indented with spaces, and the
268+
// resulting string should be "a\n b".
269+
let s = "
270+
a: |-
271+
a
272+
b
273+
";
274+
275+
let doc = &YamlLoader::load_from_str(s).unwrap()[0];
276+
let Yaml::Hash(map) = doc else {
277+
dbg!(doc);
278+
panic!()
279+
};
280+
assert_eq!(map.len(), 1);
281+
assert_eq!(
282+
map.get(&Yaml::String("a".to_string())),
283+
Some(&Yaml::String(String::from("a\n b")))
284+
);
285+
}
286+
263287
#[test]
264288
fn test_bad_docstart() {
265289
assert!(YamlLoader::load_from_str("---This used to cause an infinite loop").is_ok());

0 commit comments

Comments
 (0)