Re: [RFC] Prepared Literal String Matching and Replacement
| From: | Andrew F | Date: | Mon, 23 Nov 2026 22:52:42 +0000 |
| Subject: | Re: [RFC] Prepared Literal String Matching and Replacement | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-133214@lists.php.net to get a copy of this message | ||
On Nov 23, 2026, at 12:46, Tim Düsterhus <tim@bastelstu.be> wrote:
> As an example, I do not find it obvious at all that the namespace should be called
>
Str rather than string (or String),
> matching the name of the type.
Agreed on this bit. We're still getting rid of legacy names like "integer" and
"double"; we should avoid introducing new naming surprises.
> The unqualified names of the three classes are also awfully generic and potentially preclude
> future extensions: There clearly are more possible ways of “matching strings” than a scan for
> (sub)string literals. The new stdlib APIs that we've introduced lately clearly specify the
> “algorithm” or standard they implement in the name (e.g.
> Random\Engine\Xoshiro256StarStar or
> Uri\Rfc3986\Uri).
I have to disagree with this bit, though. The fundamental functionality which these methods provide
is string matching and replacement. The use of Aho-Corasick to implement this is an implementation
detail which users should not need to be concerned with, and which PHP should be able to change if a
better implementation is found. (By way of example: the standard sorting function is called sort(),
not quicksort().)
On a more general note, this RFC is conflating two distinct improvements to PHP: a better interface
for match-and-replace operations, and a faster implementation of those operations. Tying those two
changes together feels less than ideal - are there ways that we could e.g. make existing functions
like str_replace() use Aho-Corasick in situations where it would be helpful, or to make
Matcher/Replacer abstain from using it in cases where a simple search would be faster?
-- Andrew F