public static function safeStrlen(string $str) : int
{
static $exists = null;
if ($exists === null) {
$exists = \is_callable('\\mb_strlen');
}
if ($exists) {
$length = \mb_strlen($str, '8bit');
if ($length === false) {
throw new CannotPerformOperation('mb_strlen() failed unexpectedly');
}
} else {
// If we reached here, we can rely on strlen to count bytes:
$length = \strlen($str);
if ($length === false) {
throw new CannotPerformOperation('strlen() failed unexpectedly');
}
}
return $length;
}