From f7aaecf6a02536db2452bdab5e811d59a46df263 Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Wed, 29 Apr 2026 20:21:07 +0100 Subject: [PATCH] Use strcspn for the fast-skip in clean() Switch from a regex "not in this character set" pattern to strcspn which does the same job but in C, without the PCRE overhead. --- src/PhpFileCleaner.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/PhpFileCleaner.php b/src/PhpFileCleaner.php index 4591aa2..c2de14a 100644 --- a/src/PhpFileCleaner.php +++ b/src/PhpFileCleaner.php @@ -24,7 +24,7 @@ class PhpFileCleaner private static $typeConfig; /** @var non-empty-string */ - private static $restPattern; + private static $rejectChars; /** * @readonly @@ -60,7 +60,7 @@ public static function setTypeConfig(array $types): void ]; } - self::$restPattern = '{[^?"\'index += 1; - if ($this->match(self::$restPattern, $match)) { - $clean .= $char . $match[0]; - $this->index += \strlen($match[0]); + $skip = strcspn($this->contents, self::$rejectChars, $this->index); + if ($skip > 0) { + $clean .= $char . \substr($this->contents, $this->index, $skip); + $this->index += $skip; } else { $clean .= $char; }