Skip to content

Commit

Permalink
Add Str::ord() for UTF-8 character codes
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoiq committed Jul 22, 2022
1 parent b503974 commit 2712a03
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/common/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2712a03

Please sign in to comment.