From db23474e97564e7bbf463629ebfed57b4d831cc5 Mon Sep 17 00:00:00 2001 From: Filip Halaxa Date: Thu, 16 Dec 2021 15:47:30 +0100 Subject: [PATCH] Performance touches --- src/Lexer.php | 19 +++++++++++-------- test/JsonMachineTest/LexerTest.php | 14 +++++++------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Lexer.php b/src/Lexer.php index 550949d..814b63f 100644 --- a/src/Lexer.php +++ b/src/Lexer.php @@ -59,12 +59,6 @@ class Lexer implements \IteratorAggregate, PositionAware continue; } - // handle CRLF newlines - if ($ignoreLF && $byte === "\n") { - $ignoreLF = false; - continue; - } - if (isset($$byte)) { ++$column; if ($tokenBuffer !== '') { @@ -80,10 +74,19 @@ class Lexer implements \IteratorAggregate, PositionAware $this->column = $column; yield $byte; // track line number and reset column for each newline - } elseif ($byte === "\r" || $byte === "\n") { - $ignoreLF = ($byte === "\r"); + } elseif ($byte === "\n") { + // handle CRLF newlines + if ($ignoreLF) { + --$column; + $ignoreLF = false; + continue; + } ++$this->line; $column = 0; + } elseif ($byte === "\r") { + ++$this->line; + $ignoreLF = true; + $column = 0; } } else { if ($byte === '"') { diff --git a/test/JsonMachineTest/LexerTest.php b/test/JsonMachineTest/LexerTest.php index 5a388b3..01193ce 100644 --- a/test/JsonMachineTest/LexerTest.php +++ b/test/JsonMachineTest/LexerTest.php @@ -35,16 +35,16 @@ class LexerTest extends \PHPUnit_Framework_TestCase { $json = file_get_contents($formattedJsonFilePath); $lexer = new Lexer(new StringChunks($json)); - $tokens = $this->tokensWithLocationalInformation(); + $expectedTokens = $this->expectedTokens(); $i = 0; - foreach ($lexer as $lexeme) { + foreach ($lexer as $token) { $i++; - $token = array_shift($tokens); + $expectedToken = array_shift($expectedTokens); - $this->assertEquals($token[0], $lexeme, 'lexeme failed with data set #' . $i); - $this->assertEquals($token[1], $lexer->getLine(), 'line failed with data set #' . $i); - $this->assertEquals($token[2], $lexer->getColumn(), 'column failed with data set #' . $i); + $this->assertEquals($expectedToken[0], $token, 'token failed with expected token #' . $i); + $this->assertEquals($expectedToken[1], $lexer->getLine(), 'line failed with expected token #' . $i); + $this->assertEquals($expectedToken[2], $lexer->getColumn(), 'column failed with expected token #' . $i); } } @@ -57,7 +57,7 @@ class LexerTest extends \PHPUnit_Framework_TestCase ]; } - private function tokensWithLocationalInformation() + private function expectedTokens() { return [ // lexeme, line, column