API / Minn\Support / Utf8

Utf8 final class

Minn\Support\Utf8 · public/minn/src/Minn/Support/Utf8.php · 154 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.

Used by 1

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.

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.

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.