Skip to content

Commit 56150a3

Browse files
authored
Use strcspn for the fast-skip in clean() (#35)
Switch from a regex "not in this character set" pattern to strcspn which does the same job but in C, without the PCRE overhead.
1 parent 6d1e587 commit 56150a3

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

src/PhpFileCleaner.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class PhpFileCleaner
2424
private static $typeConfig;
2525

2626
/** @var non-empty-string */
27-
private static $restPattern;
27+
private static $rejectChars;
2828

2929
/**
3030
* @readonly
@@ -60,7 +60,7 @@ public static function setTypeConfig(array $types): void
6060
];
6161
}
6262

63-
self::$restPattern = '{[^?"\'</'.implode('', array_keys(self::$typeConfig)).']+}A';
63+
self::$rejectChars = '?"\'</'.implode('', array_keys(self::$typeConfig));
6464
}
6565

6666
public function __construct(string $contents, int $maxMatches)
@@ -128,9 +128,10 @@ public function clean(): string
128128
}
129129

130130
$this->index += 1;
131-
if ($this->match(self::$restPattern, $match)) {
132-
$clean .= $char . $match[0];
133-
$this->index += \strlen($match[0]);
131+
$skip = strcspn($this->contents, self::$rejectChars, $this->index);
132+
if ($skip > 0) {
133+
$clean .= $char . \substr($this->contents, $this->index, $skip);
134+
$this->index += $skip;
134135
} else {
135136
$clean .= $char;
136137
}

0 commit comments

Comments
 (0)