From 2712a03f2ded0b0e2b1052836afa2f9b1aedc101 Mon Sep 17 00:00:00 2001 From: paranoiq Date: Fri, 22 Jul 2022 14:47:28 +0200 Subject: [PATCH] Add Str::ord() for UTF-8 character codes --- src/common/Str.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/common/Str.php b/src/common/Str.php index a2a2d2b6..c454a279 100644 --- a/src/common/Str.php +++ b/src/common/Str.php @@ -21,6 +21,7 @@ use Error; use Nette\Utils\Strings; use UConverter; +use function ord; use const MB_CASE_TITLE; use function array_keys; use function array_pop; @@ -78,6 +79,36 @@ public static function chr(int $code): string return Strings::chr($code); } + public static function ord(string $ch): int + { + $ord0 = ord($ch[0]); + if ($ord0 >= 0 && $ord0 <= 127) { + return $ord0; + } + $ord1 = ord($ch[1]); + if ($ord0 >= 192 && $ord0 <= 223) { + return ($ord0 - 192) * 64 + ($ord1 - 128); + } + $ord2 = ord($ch[2]); + if ($ord0 >= 224 && $ord0 <= 239) { + return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128); + } + $ord3 = ord($ch[3]); + if ($ord0 >= 240 && $ord0 <= 247) { + return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2 - 128) * 64 + ($ord3 - 128); + } + $ord4 = ord($ch[4]); + if ($ord0 >= 248 && $ord0 <= 251) { + return ($ord0 - 248) * 16777216 + ($ord1 - 128) * 262144 + ($ord2 - 128) * 4096 + ($ord3 - 128) * 64 + ($ord4 - 128); + } + $ord5 = ord($ch[5]); + if ($ord0 >= 252 && $ord0 <= 253) { + return ($ord0 - 252) * 1073741824 + ($ord1 - 128) * 16777216 + ($ord2 - 128) * 262144 + ($ord3 - 128) * 4096 + ($ord4 - 128) * 64 + ($ord5 - 128); + } + + throw new InvalidValueException($ch, "UTF-8 character"); + } + public static function startsWith(string $string, string $find): bool { return strncmp($string, $find, strlen($find)) === 0;