From 11cffc6df8184ac37e40e25d80c3a9a4cda00101 Mon Sep 17 00:00:00 2001 From: Ethiraric Date: Sun, 14 Jul 2024 16:57:26 +0200 Subject: [PATCH] Fix issue with deeply indented block scalars. Fixes #2 --- parser/src/scanner.rs | 5 ++++- parser/tests/basic.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/parser/src/scanner.rs b/parser/src/scanner.rs index 5bae1ae..6eac393 100644 --- a/parser/src/scanner.rs +++ b/parser/src/scanner.rs @@ -1853,7 +1853,10 @@ impl> Scanner { while !self.buffer.is_empty() && self.mark.col < indent && self.ch() == ' ' { self.skip_blank(); } - if !(!self.buffer.is_empty() && self.mark.col < indent && self.ch() == ' ') { + // If we reached our indent, we can break. We must also break if we have + // reached content or EOF; that is, the buffer is not empty and the next + // character is not a space. + if self.mark.col == indent || (!self.buffer.is_empty() && self.ch() != ' ') { break; } } diff --git a/parser/tests/basic.rs b/parser/tests/basic.rs index ac89929..c3532fa 100644 --- a/parser/tests/basic.rs +++ b/parser/tests/basic.rs @@ -201,6 +201,33 @@ foobar"; ] ); } +#[test] +fn test_large_block_scalar_indent() { + // https://github.com/Ethiraric/yaml-rust2/issues/29 + // https://github.com/saphyr-rs/saphyr-parser/issues/2 + // Tests the `loop` fallback of `skip_block_scalar_indent`. The indent in the YAML string must + // be greater than `BUFFER_LEN - 2`. The second line is further indented with spaces, and the + // resulting string should be "a\n b". + let s = " +a: |- + a + b +"; + + assert_eq!( + run_parser(s).unwrap(), + [ + Event::StreamStart, + Event::DocumentStart, + Event::MappingStart(0, None), + Event::Scalar("a".to_string(), TScalarStyle::Plain, 0, None), + Event::Scalar("a\n b".to_string(), TScalarStyle::Literal, 0, None), + Event::MappingEnd, + Event::DocumentEnd, + Event::StreamEnd, + ] + ); +} #[test] fn test_bad_docstart() {