Expand description
Permissive UTF-8 / UTF-16 / UTF-32 transcoding, comparison and BOM detection, with SIMD ASCII fast paths.
A Rust port of the xstd::codepoint_cvt / utf_* family. Design points
carried over:
- Permissive, non-validating codecs. Decoding never fails: truncated
UTF-8 sequences decode to
0(consuming the remainder), lone surrogates pass through, garbage-in produces garbage-out. Values are rawu32codepoints, notchar. - ASCII-only case folding for the
*_ignore_ascii_caseoperations andAsciiCasetranscoding. - Foreign endianness as a type parameter:
Utf16<true>is byte-swapped relative to the native byte order (seeUtf16Be/Utf16Lealiases). - Terminal text primitives, allocation-free and generic over the
encoding: grapheme segmentation (
graphemes, UAX #29 incl.GB9cand GB11), visible cell width (width), width truncation (truncate, returns a borrowed prefix) and greedy word wrap (wrap, yields borrowed sublines), backed by a generated UnicodeUNICODE_VERSIONproperty trie (scripts/gen_props.py).
Special-op mapping from the C++ original:
C++ (xstd) | Rust |
|---|---|
clang vector extensions (xvec) | core::simd (LLVM vector IR) |
bit_pdep / bit_pext (BMI2) | _pdep_u32 / _pext_u32 under cfg(bmi2) |
assume(..) | core::hint::assert_unchecked |
bswapw / bswapd | u16::swap_bytes / u32::swap_bytes |
lsb(mask()) | Mask::to_bitmask().trailing_zeros() |
| overlapped tail vector load/store | Simd::from_slice / copy_to_slice on an |
overlapping window at limit - N |
Deliberate fixes over the original:
- UTF-16 encode computes the low surrogate as
0xDC00 | (cp & 0x3FF); the C++0xDC00 | uint16_t(cp)corrupts pairs when bit 13 ofcp - 0x10000is set (e.g. U+12000). compareorders by decoded codepoint everywhere (the C++ mixed UTF-16 code-unit order with codepoint order) and a longer string with a trailingNULcodepoint no longer compares equal to its prefix.
Structs§
- Codepoints
- Iterator over the raw codepoints of an encoded slice.
- Grapheme
- A borrowed extended grapheme cluster and its terminal cell width.
- Graphemes
- Allocation-free iterator over extended grapheme clusters.
- Utf8
- UTF-8. Byte-oriented, so there is no foreign-endianness variant.
- Utf16
- UTF-16.
FOREIGNselects byte order opposite to native (seeUtf16Le/Utf16Be). - Utf32
- UTF-32.
FOREIGNselects byte order opposite to native (seeUtf32Le/Utf32Be). - Wrapped
- Iterator of wrapped lines, created by
wrap.
Enums§
- Ascii
Case - ASCII case transform applied while transcoding.
- Bom
- Byte order mark at the head of a raw byte stream.
- Kind
- Encoding family, used to detect “same encoding” fast paths at monomorphization time.
Constants§
- UNICODE_
VERSION - Unicode version of the generated property tables.
Traits§
- Encoding
- A UTF encoding: a stateless codec between raw
u32codepoints and code units. - Unit
- A UTF code unit:
u8(UTF-8),u16(UTF-16) oru32(UTF-32).
Functions§
- chars
- Iterates
inputaschars, substituting U+FFFD for unit sequences that decode to invalid scalar values. - codepoints
- Iterates the raw (unvalidated) codepoints of
input. - compare
- Compares two encoded strings in codepoint order.
- compare_
ignore_ ascii_ case comparewith ASCII-only case folding (utf_icompare).- detect_
bom - Detects a BOM, returning it plus the byte length to skip.
- equals
- Codepoint equality across encodings (
utf_cmpeq). - equals_
ignore_ ascii_ case equalswith ASCII-only case folding (utf_icmpeq).- from_
bytes - Identifies the encoding of a raw byte stream from its BOM (defaulting to
UTF-8) and transcodes it to
T. - graphemes
- Iterates the extended grapheme clusters of an encoded slice without allocating.
- graphemes_
str - Iterates the extended grapheme clusters of a UTF-8 string as borrowed strings.
- to_
string - Transcodes to an owned
String. - transcode
- Transcodes all of
srcinto a freshly allocated unit vector. - transcode_
into - Transcodes as much of
srcintodstas fits, returning(units_read, units_written). Never splits a codepoint: encoding stops at the last codepoint whose output fits. - transcode_
with_ case transcodewith ASCII case folding.- transcoded_
len - Unit count
srcwould occupy once transcoded toT(utf_length). - truncate
- Longest prefix of
inputno wider thanmax_widthterminal cells, cut on an extended grapheme cluster boundary. - truncate_
str truncateover UTF-8str.- width
- Visible width of
inputin terminal cells (extended grapheme clusters, UAX #11 plus emoji presentation rules, with an ASCII bulk path). - width_
str widthover UTF-8str.- wrap
- Greedily wraps
inputinto at least one borrowed terminal-width subline. - wrap_
str - Applies
wrapto a UTF-8 string.