API / Minn\Support / Utf8

Utf8 final class

Minn\Support\Utf8 · public/minn/src/Minn/Support/Utf8.php · 182 lines

UTF-8 as the reference reads it. Well-formed means no overlong forms, surrogates, code points past U+10FFFF, stray continuation bytes or truncated sequences; noncharacters, NUL and a byte order mark pass. Split into characters (the multibyte fallbacks in compat.php), a malformed byte is one character and so is the longest start of a sequence that breaks off, so "\xE2\x82" counts once.

Constants and properties

privateCHARACTER = '[\\x00-\\x7F]|[\\xC2-\\xDF][\\x80-\\xBF]|\\xE0[\\xA0-\\xBF][\\x80-\\xBF]|[\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]{2}|\\xED[\\x80-\\x9F][\\x80-\\xBF]|\\xF0[\\x90-\\xBF][\\x80-\\xBF]{2}|[\\xF1-\\xF3][\\x80-\\xBF]{3}|\\xF4[\\x80-\\x8F][\\x80-\\xBF]{2}'

One well-formed character.

privateBROKEN = '\\xE0[\\xA0-\\xBF]|[\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]|\\xED[\\x80-\\x9F]|\\xF0[\\x90-\\xBF][\\x80-\\xBF]?|[\\xF1-\\xF3][\\x80-\\xBF]{1,2}|\\xF4[\\x80-\\x8F][\\x80-\\xBF]?'

The start of a sequence that breaks off.

privateCHARSETS = array ( 'cp1250' => 'Windows-1250', 'cp1251' => 'Windows-1251', 'cp1256' => 'Windows-1256', 'cp1257' => 'Windows-1257', 'cp850' => 'CP850', 'cp866' => 'CP866', 'latin2' => 'ISO-8859-2', 'latin5' => 'ISO-8859-9', 'latin7' => 'ISO-8859-13', 'greek' => 'ISO-8859-7', 'hebrew' => 'ISO-8859-8', 'koi8r' => 'KOI8-R', 'koi8u' => 'KOI8-U', 'sjis' => 'SJIS', 'cp932' => 'CP932', 'ujis' => 'EUC-JP', 'eucjpms' => 'eucJP-win', 'euckr' => 'EUC-KR', 'gbk' => 'CP936', 'gb2312' => 'EUC-CN', 'gb18030' => 'GB18030', 'big5' => 'BIG-5', 'ucs2' => 'UCS-2', 'utf16' => 'UTF-16', 'utf32' => 'UTF-32', )

The database charsets a query's text is held to, by the multibyte name of each.

Public

staticisValid(string $bytes): bool#

True for well-formed UTF-8, including the empty string.

staticcharacters(string $bytes): array#

The bytes cut into characters, a malformed byte or a broken-off sequence counting as one.

@return list<string>
staticscan(string $bytes, int $at, ?int $maxBytes = NULL, ?int $maxCodePoints = NULL): array#

Reads well-formed characters from $at, as the reference's scanner does (probe utf8). It stops $maxBytes past $at (a character begun before that is read whole), after $maxCodePoints, or at a malformed byte. The answer: the characters read, where it stopped, the length of the malformed run there (a broken-off start of a sequence, else one byte, cut at the byte limit; 0 when there is none), and whether a character that could be a noncharacter was one (null when none could be: only EF and four-byte sequences hold them).

@return array{int, int, int, ?bool}
staticisNoncharacter(int $code): bool#

Whether a code point is a noncharacter: U+FDD0 to U+FDEF, or the last two of any plane.

statichasNoncharacters(string $bytes): bool#

Whether any well-formed character is a noncharacter, malformed bytes stepped over.

staticscrub(string $bytes): string#

The bytes with each malformed run (as characters() cuts them) replaced by U+FFFD.

staticspan(string $bytes, int $offset, int $maxCodePoints): array#

How many bytes from $offset the first $maxCodePoints characters take, and how many there were.

@return array{int, int}
statictoLatin1(string $bytes): string#

UTF-8 as ISO-8859-1: a character past U+00FF, or a malformed run, becomes "?".

statickeptBy(string $bytes, string $charset): string#

What a column in this charset keeps: utf8mb4 drops each malformed run, utf8 (utf8mb3) every four-byte character too; any other charset keeps the bytes.

staticfits(string $bytes, string|false $charset): bool#

Whether text can go to a table in this charset as the reference's database door judges it (probe invalid-text): anything for a binary or latin1 table or one without text; otherwise well-formed UTF-8, and of that only what the charset holds (no four-byte character for utf8, no byte past ASCII for ascii).

staticfromLatin1(string $bytes): string#

ISO-8859-1 as UTF-8: each byte the character of its value.

staticfromCodePoint(int $code): ?string#

The UTF-8 bytes for a code point, or null for a negative one, a surrogate, or one past U+10FFFF.

staticfirstCodePoint(string $bytes): ?int#

The code point of the first character, or null when the bytes do not start with a whole, well-formed one.