Skip to content

Commit 5c48158

Browse files
committed
optimize and simplify cursor
2 parents 60a5fff + c3fc13c commit 5c48158

3 files changed

Lines changed: 25 additions & 132 deletions

File tree

src/Parser/Cursor.php

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,11 @@ private function charFromIndex(int $offset = 0): ?string
4545

4646
$char = $this->chars[$pos];
4747

48-
$nextPos = $pos + 1;
49-
50-
if (
51-
$char === "\r"
52-
&& $nextPos < $this->length
53-
&& $this->chars[$nextPos] === "\n"
54-
) {
48+
if ($char === "\r" && ($this->chars[$pos + 1] ?? null) === "\n") {
5549
return "\n";
5650
}
5751

58-
return $char !== '' ? $char : null;
52+
return $char;
5953
}
6054

6155
public function currentChar(): ?string
@@ -73,18 +67,21 @@ public function next(int $chars = 1): void
7367
$this->peekOffset = 0;
7468

7569
for ($i = 0; $i < $chars; $i++) {
76-
$isClRf = $this->slice($this->index, 2) === "\r\n";
70+
$isClRf = ($this->chars[$this->index] ?? null) === "\r"
71+
&& ($this->chars[$this->index + 1] ?? null) === "\n";
7772

78-
$this->incrementIndex($isClRf ? 2 : 1);
73+
$this->index += $isClRf ? 2 : 1;
7974
}
8075
}
8176

8277
public function peek(int $chars = 1): ?string
8378
{
8479
for ($i = 0; $i < $chars; $i++) {
85-
$currentPeek = $this->slice($this->index + $this->peekOffset, 2);
80+
$pos = $this->index + $this->peekOffset;
81+
$isClRf = ($this->chars[$pos] ?? null) === "\r"
82+
&& ($this->chars[$pos + 1] ?? null) === "\n";
8683

87-
$this->peekOffset += $currentPeek === "\r\n" ? 2 : 1;
84+
$this->peekOffset += $isClRf ? 2 : 1;
8885
}
8986

9087
return $this->currentPeek();
@@ -97,8 +94,7 @@ public function resetPeek(int $offset = 0): void
9794

9895
public function skipToPeek(): void
9996
{
100-
$this->incrementIndex($this->peekOffset);
101-
97+
$this->index += $this->peekOffset;
10298
$this->peekOffset = 0;
10399
}
104100

@@ -118,15 +114,12 @@ public function peekBlankInline(): void
118114

119115
public function skipBlankInline(): string
120116
{
121-
$start = $this->peekOffset;
117+
$start = $this->index + $this->peekOffset;
122118

123119
$this->peekBlankInline();
124-
125-
$blank = $this->slice($this->index + $start, $this->peekOffset - $start);
126-
127120
$this->skipToPeek();
128121

129-
return $blank;
122+
return str_repeat(' ', $this->index - $start);
130123
}
131124

132125
public function peekBlankBlock(): string
@@ -183,6 +176,17 @@ public function skipBlank(): void
183176
$this->skipToPeek();
184177
}
185178

179+
protected function latestNewline(): int
180+
{
181+
for ($i = $this->index - 1; $i >= 0; $i--) {
182+
if ($this->chars[$i] === "\n") {
183+
return $i;
184+
}
185+
}
186+
187+
return 0;
188+
}
189+
186190
public function expectChar(string $char): void
187191
{
188192
if ($this->currentChar() === $char) {
@@ -224,14 +228,4 @@ public function takeChar(Closure $callback): ?string
224228

225229
return $char;
226230
}
227-
228-
protected function setIndex(int $index): void
229-
{
230-
$this->index = $index;
231-
}
232-
233-
private function incrementIndex(int $offset = 1): void
234-
{
235-
$this->index += $offset;
236-
}
237231
}

src/Parser/FluentCursor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ public function nextLineIsComment(int $level): bool
118118

119119
public function skipToNextEntryStart(int $junkStart): void
120120
{
121-
$lastNewline = mb_strrpos($this->slice(0, $this->index), "\n") ?: 0;
121+
$lastNewline = $this->latestNewline();
122122

123123
if ($junkStart < $lastNewline) {
124124
// Last seen newline is after the junk start. It's safe to rewind
125125
// without the risk of resuming at the same broken entry.
126-
$this->setIndex($lastNewline);
126+
$this->index = $lastNewline;
127127
}
128128

129129
while ($this->currentChar() !== null) {

tests/Benchmark/CursorBench.php

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)