1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-18 05:28:14 +01:00

Performance touches

This commit is contained in:
Filip Halaxa 2021-12-16 15:47:30 +01:00
parent 2a61ad942a
commit db23474e97
2 changed files with 18 additions and 15 deletions

View File

@ -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 === '"') {

View File

@ -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