Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b528a2

Browse files
committed
Apply strcspn / strspn optimization to the rest of PhpFileCleaner
1 parent f89efdf commit 5b528a2

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

src/Reflection/BetterReflection/SourceLocator/PhpFileCleaner.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use function preg_quote;
1010
use function strcspn;
1111
use function strlen;
12+
use function strspn;
1213
use function substr;
1314

1415
/**
@@ -205,8 +206,13 @@ private function consumeString(string $delimiter): string
205206

206207
private function skipString(string $delimiter): void
207208
{
209+
$rejectChars = '\\' . $delimiter;
208210
$this->index += 1;
209211
while ($this->index < $this->len) {
212+
$this->index += strcspn($this->contents, $rejectChars, $this->index);
213+
if ($this->index >= $this->len) {
214+
break;
215+
}
210216
if ($this->contents[$this->index] === '\\' && ($this->peek('\\') || $this->peek($delimiter))) {
211217
$this->index += 2;
212218
continue;
@@ -223,7 +229,9 @@ private function skipComment(): void
223229
{
224230
$this->index += 2;
225231
while ($this->index < $this->len) {
226-
if ($this->contents[$this->index] === '*' && $this->peek('/')) {
232+
$this->index += strcspn($this->contents, '*', $this->index);
233+
234+
if ($this->peek('/')) {
227235
$this->index += 2;
228236
break;
229237
}
@@ -234,12 +242,7 @@ private function skipComment(): void
234242

235243
private function skipToNewline(): void
236244
{
237-
while ($this->index < $this->len) {
238-
if (in_array($this->contents[$this->index], ["\r", "\n"], true)) {
239-
return;
240-
}
241-
$this->index += 1;
242-
}
245+
$this->index += strcspn($this->contents, "\r\n", $this->index);
243246
}
244247

245248
private function skipHeredoc(string $delimiter): void
@@ -267,16 +270,10 @@ private function skipHeredoc(string $delimiter): void
267270
}
268271

269272
// skip the rest of the line
270-
while ($this->index < $this->len) {
271-
$this->skipToNewline();
272-
273-
// skip newlines
274-
while ($this->index < $this->len && ($this->contents[$this->index] === "\r" || $this->contents[$this->index] === "\n")) {
275-
$this->index += 1;
276-
}
273+
$this->skipToNewline();
277274

278-
break;
279-
}
275+
// skip newlines
276+
$this->index += strspn($this->contents, "\r\n", $this->index);
280277
}
281278
}
282279

0 commit comments

Comments
 (0)