Expand description
UTF-8 decode helpers, ported from include/hermes/Support/UTF8.h (decode side).
These mirror the inline classifiers and _decodeUTF8SlowPath/decodeUTF8
from Support/UTF8.h. The C++ uses an advancing const char *&from; here we
use a slice plus a &mut usize index, so the raw-pointer parity lives only
in cursor.rs. The lexer always passes the NUL-terminated buffer, so an
out-of-range continuation read sees 0x00 (a non-continuation byte) and is
correctly rejected; we also guard indexes against bytes.len() defensively.
Constants§
- UTF8_
LINE_ TERMINATOR_ CHAR0 - First byte of the UTF-8 encoding of U+2028/U+2029 (e2 80 a8/a9).
Functions§
- append_
unicode_ to_ storage - Encode
cpintostoragelike the lexer’sappendUnicodeToStorage(JSLexer.h:1125-1143): code points above 0xFFFF are first split into a UTF-16 surrogate pair, and each surrogate is encoded individually into UTF-8 (technically invalid UTF-8 / WTF-8, which JS string & identifier storage allows). - convert_
utf8_ with_ surrogates_ to_ utf16 - Decode a UTF-8 sequence, which is assumed to be valid, but may possibly
contain explicitly encoded surrogate pairs, into a UTF-16 sequence. Port of
convertUTF8WithSurrogatesToUTF16(UTF8.h:216-225). - convert_
utf16_ to_ utf8_ with_ replacements - Convert a UTF-16 encoded string
u16sto valid UTF-8, combining surrogate pairs into supplementary-plane characters and replacing unpaired surrogates with U+FFFD. Port ofconvertUTF16ToUTF8WithReplacements(UTF8.cpp:99-133), dropping themaxCharactersparameter (the lexer always passes 0/unbounded). - decode_
utf8 - Decode a sequence of UTF8 encoded bytes into a Unicode codepoint, ASCII fast
path. Port of
decodeUTF8(UTF8.h:187-193). In case of decoding errors, the provided callback is invoked with an appropriate message and UNICODE_REPLACEMENT_CHARACTER is returned. - decode_
utf8_ slow_ path - Decode a sequence of UTF8 encoded bytes when it is known that the first byte
is a start of a UTF8 sequence. Port of
_decodeUTF8SlowPath(UTF8.h:77-162), readingbytesfrom*iand advancing*ipast the consumed bytes. On malformed input it invokeserrorand returns the replacement character. - encode_
utf8 - Encode a Unicode code point as UTF-8 (up to the legacy 6-byte form, matching
encodeUTF8), appending the bytes toout. Port ofUTF8.cpp:encodeUTF8. - encode_
utf16 - Encode a 32-bit value into UTF-16, appending to
out. If the value is a part of a surrogate pair, it is encoded without any conversion. Port ofencodeUTF16(UTF8.h:197-210). - is_
utf8_ continuation_ byte - \return true if this is a UTF-8 continuation byte, or in other words, this is a byte in the “middle” of a UTF-8 codepoint.
- is_
utf8_ leading_ byte - \return true if this is a UTF-8 leading byte.
- is_
utf8_ start - Check whether a byte is a regular ASCII or a UTF8 starting byte. \return true if it is UTF8 starting byte.
- match_
unicode_ line_ terminator_ offset1 - \return true if
bytesstarts with the UTF-8 encoding of U+2028 or U+2029.bytes[0]is assumed to be UTF8_LINE_TERMINATOR_CHAR0 (the caller checked).