Repository navigation
Use faster strspn() over regex matching - #7005
Conversation
staabm
commented
Nov 8, 2026
|
//cc @SanderMuller |
|
This pull request has been marked as ready for review. |
SanderMuller
left a comment
There was a problem hiding this comment.
The new check gives the same result as the old regex. I compared both on 201,281 strings. These were every byte value alone and next to a space or a newline. They also included random strings of up to 8 characters made from whitespace, \x85, \xA0, NUL and a letter. There was no difference, also with the UTF-8 ctype locale that the CLI sets.
There is one difference, and it is a fix. On 2,000,000 spaces followed by x, Strings::match() throws RegexpException: Backtrack limit was exhausted, with and without JIT. strspn() gives the correct result there.
The existing tests cover the change: 2 of the 5 rule tests fail when the new condition always skips. On the PR head, the rule tests, phpcs and make phpstan pass. The 4 red integration jobs are also red on #7007.
The effect on a full run is small. In self-analysis the rule checks 5,884 last nodes in 2,947 files, and only an InlineHTML node gets to strspn(). With the largest saving in your table, that is at most about 2 ms per run.