Expand description
Unicode normalization (NFC, NFD, NFKC, NFKD) where every output character
carries an isize that aligns it to the input.
use unicode_normalize_aligned::UnicodeNormalization;
// U+2126 OHM SIGN maps to U+03A9 GREEK CAPITAL LETTER OMEGA.
let pairs: Vec<(char, isize)> = "\u{2126} A\u{30A}".nfc().collect();
assert_eq!(pairs, [('\u{3A9}', 0), (' ', 0), ('\u{C5}', -1)]);§The alignment tag
Each output item is (char, isize). The tag is a change count in chars,
consumed positionally in output order:
0: this character replaces exactly one input character.+1: this character is newly inserted and consumes no input.-N: this character replaces one input character and removes theNinput characters after it.
Three rules produce every tag. A character that fully decomposes to
d1..dn yields d1 tagged 0 and d2..dn tagged +1. Canonical
reordering moves characters together with their tags. Composing two
characters with tags a and b yields one character tagged a + b - 1.
Everything else passes through tagged 0.
Two invariants follow for every input: the tags sum to the output length
minus the input length, and walking the output while consuming
1 - min(tag, 0) input positions per non-inserted character lands exactly
on the input end.
§Scope
Tables are generated from Unicode 17.0.0 and the crate passes the full
NormalizationTest for that version. The crate is no_std, has no runtime
dependencies, and normalizes any stream-safe text (UAX15-D4) without
allocating. A combining run of 32 or more characters spills the
decomposition buffer once, and a run of 33 or more allocates one
ordering scratch when it closes. NFC and NFKC spill a second
blocked-mark buffer from 34 combining characters. The first mark
composes with the starter, so that buffer holds one fewer character
than the run.
Modules§
- char
- Per-character queries backed by the same tables as the iterators.
Structs§
- Decompositions
- Streaming NFD or NFKD. Yields
(char, isize): the output character and its alignment change count. See the crate docs for the tag contract. - Recompositions
- Streaming NFC or NFKC. Yields
(char, isize): the output character and its alignment change count. See the crate docs for the tag contract.
Enums§
- IsNormalized
- Answer of a quick check.
Constants§
- UNICODE_
VERSION - Unicode version the tables were generated from.
Traits§
- Unicode
Normalization - Iterator adapters for the four normalization forms.
Functions§
- is_nfc
- Whether
sis in NFC. Resolves aMaybequick check by normalizing and comparing, without building a string. - is_
nfc_ quick - Quick check for NFC over a character iterator.
- is_nfd
- Whether
sis in NFD. Resolves aMaybequick check by normalizing and comparing, without building a string. - is_
nfd_ quick - Quick check for NFD over a character iterator.
- is_nfkc
- Whether
sis in NFKC. Resolves aMaybequick check by normalizing and comparing, without building a string. - is_
nfkc_ quick - Quick check for NFKC over a character iterator.
- is_nfkd
- Whether
sis in NFKD. Resolves aMaybequick check by normalizing and comparing, without building a string. - is_
nfkd_ quick - Quick check for NFKD over a character iterator.