From ec389d3441953bbd7cb27f59df61531ba856fb33 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 1 May 2020 17:52:50 +0200 Subject: Fix parsing `]` as last input char Previously ] was duplicated in the ouput --- src/ste/Parser.php | 18 ++++++++++++++---- tests/test_trailing_closing_array_bracket/.gitignore | 3 +++ tests/test_trailing_closing_array_bracket/code.php | 12 ++++++++++++ tests/test_trailing_closing_array_bracket/test.tpl | 3 +++ tests/test_trailing_closing_array_bracket/want | 3 +++ 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/test_trailing_closing_array_bracket/.gitignore create mode 100644 tests/test_trailing_closing_array_bracket/code.php create mode 100644 tests/test_trailing_closing_array_bracket/test.tpl create mode 100644 tests/test_trailing_closing_array_bracket/want diff --git a/src/ste/Parser.php b/src/ste/Parser.php index 0280f6d..6e7bf62 100644 --- a/src/ste/Parser.php +++ b/src/ste/Parser.php @@ -43,12 +43,21 @@ class Parser * @param int $n * @return string */ - private function next($n = 1) + private function peek($n = 1) { if ($n <= 0) { throw new \InvalidArgumentException("\$n must be > 0"); } - $c = mb_substr($this->text, $this->off, $n); + return mb_substr($this->text, $this->off, $n); + } + + /** + * @param int $n + * @return string + */ + private function next($n = 1) + { + $c = $this->peek($n); $this->off = min($this->off + $n, $this->len); return $c; } @@ -455,7 +464,9 @@ class Parser $arrayfields = array(); - while ($this->next() == "[") { + while ($this->peek() == "[") { + $this->next(); + $openedat = $this->off - 1; $res = $this->parse_text( self::ESCAPES_DEFAULT, /* Escapes */ @@ -471,7 +482,6 @@ class Parser $arrayfields[] = $res[0]; } - $this->back(); return $arrayfields; } diff --git a/tests/test_trailing_closing_array_bracket/.gitignore b/tests/test_trailing_closing_array_bracket/.gitignore new file mode 100644 index 0000000..de2a41b --- /dev/null +++ b/tests/test_trailing_closing_array_bracket/.gitignore @@ -0,0 +1,3 @@ +have +*.ast +*.transc.php diff --git a/tests/test_trailing_closing_array_bracket/code.php b/tests/test_trailing_closing_array_bracket/code.php new file mode 100644 index 0000000..77f48be --- /dev/null +++ b/tests/test_trailing_closing_array_bracket/code.php @@ -0,0 +1,12 @@ +set_var_by_name("foo", array( + "foo", + "bar", + "baz", + )); +} diff --git a/tests/test_trailing_closing_array_bracket/test.tpl b/tests/test_trailing_closing_array_bracket/test.tpl new file mode 100644 index 0000000..68f6caa --- /dev/null +++ b/tests/test_trailing_closing_array_bracket/test.tpl @@ -0,0 +1,3 @@ +$foo[0] +$foo[1] +$foo[2] \ No newline at end of file diff --git a/tests/test_trailing_closing_array_bracket/want b/tests/test_trailing_closing_array_bracket/want new file mode 100644 index 0000000..86e041d --- /dev/null +++ b/tests/test_trailing_closing_array_bracket/want @@ -0,0 +1,3 @@ +foo +bar +baz -- cgit v1.2.3-70-g09d2