From 67bda271741ea460a90cd3c961473afc7139ba46 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 22 Aug 2016 14:37:31 +0200 Subject: [PATCH] Literal: Recognize empty nowdoc string. The shortest nowdoc string is `<<<'A'\nA\n`. Before this patch, it was considered to be `<<<'A'\n\nA\n` but this is incorrect, only one newline is valid. --- source/rules/literals.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/source/rules/literals.rs b/source/rules/literals.rs index 67b8ba2c..b0006ef5 100644 --- a/source/rules/literals.rs +++ b/source/rules/literals.rs @@ -334,7 +334,7 @@ fn string_nowdoc(input: &[u8]) -> Result<&[u8], Literal> { return Result::Error(Error::Code(ErrorKind::Custom(StringError::InvalidOpeningCharacter as u32))); } - offset = 2; + offset = 1; for (index, item) in next_input[offset..].iter().enumerate() { if *item == '\n' as u8 { @@ -357,9 +357,16 @@ fn string_nowdoc(input: &[u8]) -> Result<&[u8], Literal> { } if next_input[lookahead_offset] == '\n' as u8 { + if index == 0 { + return Result::Done( + &next_input[lookahead_offset + 1..], + Literal::String(Vec::new()) + ); + } + return Result::Done( &next_input[lookahead_offset + 1..], - Literal::String(next_input[offset..offset + index].to_vec()) + Literal::String(next_input[offset + 1..offset + index].to_vec()) ); } } @@ -1033,7 +1040,7 @@ mod tests { #[test] fn case_string_nowdoc_empty() { - let input = b"<<<'FOO'\n\nFOO\n"; + let input = b"<<<'FOO'\nFOO\n"; let output = Result::Done(&b""[..], Literal::String(Vec::new())); assert_eq!(string_nowdoc(input), output);