On Mon, Jul 8, 2024 at 6:43 PM Alexandru Pătrănescu <[email protected]>
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