Hi Tim, Andrew,

Tim, I agree on the namespace. A namespace for strings should be designed
as a whole. String functions are spread over ext/standard, mbstring, intl
and PCRE today, and that's work for a separate effort. One class shouldn't
decide it. I think ext/standard belongs in the global namespace, so I'd
move the classes there.

I also agree the names should say what the classes do, without the
algorithm. Some options:

- StrSetMatcher, StrSetReplacer, StrSetMatch
- LiteralSetMatcher, LiteralSetReplacer, LiteralSetMatch
- StrSet for matching and StrMap for replacing

Which reads best to you?

Andrew, my goal isn't a new interface but a new mechanism: build the
automaton once and apply it to many texts. A function can't keep it between
calls, so it needs an object. Whether it pays off depends on how often the
dictionary is reused, and only the caller knows that. Take 1,469 bot
patterns on User-Agents. A prepared object is 11-12 times faster than the
fastest existing method. Building it for a single call is 27-31 times
slower. So I wouldn't let str_replace() or strtr() pick it on their own. A
shortcut for very small dictionaries can go into the classes later without
an API change.

On Tue, Nov 24, 2026 at 2:10 AM Tim Düsterhus <[email protected]> wrote:

> Hi
>
> On 2026-11-23 23:52, Andrew F wrote:
> >> 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().)
>
> Yes. I acknowledge that myself in the *literal sentence* that follows
> after the part you quoted. My point about “this is a specific type of
> matching that is being performed by those classes” stands though, and I
> believe the classes should be renamed.
>
> Best regards
> Tim Düsterhus
>


-- 
Best regards,
Artem Ukrainskiy

Reply via email to