优化下一行移动逻辑

This commit is contained in:
2020-12-19 12:46:43 +08:00
parent f995b932e4
commit 4c4be41f8b
3 changed files with 99 additions and 73 deletions

View File

@ -19,9 +19,9 @@ abstract class ReaderInterface
{
protected $currentLine = 1;
protected $currentPosition = 0;
protected $currentLinePosition = 0;
protected $nextPosition = 0;
protected $currentToken = "";
protected $currentLineDelta = 0;
/**
* 获取下一个字符
@ -73,6 +73,7 @@ abstract class ReaderInterface
{
$this->currentLine = 1;
$this->currentPosition = 0;
$this->currentLineDelta = 0;
$this->nextPosition = 0;
$this->moveToNextToken();
}
@ -104,7 +105,7 @@ abstract class ReaderInterface
public function getCurrentLinePosition(): int
{
return $this->currentLinePosition;
return $this->currentPosition - $this->currentLineDelta;
}
public function getCurrentToken(): string
@ -115,13 +116,12 @@ abstract class ReaderInterface
protected function moveCursorToNextChar(): void
{
$this->currentPosition++;
$this->currentLinePosition++;
}
protected function moveCursorToNextLine(int $chars = 1): void
{
$this->currentPosition += $chars;
$this->currentLinePosition = 0;
$this->currentLineDelta = $this->currentPosition;
$this->currentLine++;
}
}