Re: [RFC] Deprecations for PHP 8.4

From: Date: Mon, 08 Jul 2024 16:03:13 +0000
Subject: Re: [RFC] Deprecations for PHP 8.4
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-124291@lists.php.net to get a copy of this message
On Mon, Jul 8, 2024 at 6:43 PM Alexandru Pătrănescu <drealecs@gmail.com> wrote: > > I'm hoping this can be simplified further, but to get to the point, I also > think we should have a userland replacement suggestion in the RFC. > > Managed to simplify it like this and I find it reasonable enough: function strtok2(string $string, ?string $token = null): string|false { static $tokenGenerator = null; if ($token) { $tokenGenerator = (function(string $characters) use ($string): \Generator { $pos = 0; while (true) { $pos += \strspn($string, $characters, $pos); $len = \strcspn($string, $characters, $pos); if ($len === 0) return; $token = \substr($string, $pos, $len); $characters = yield $token; $pos += $len; } })($token); return $tokenGenerator->current() ?? false; } return $tokenGenerator?->send($string) ?? false; } Alex

« previous php.internals (#124291) next »