Skip to main content

Crate unicode_normalize_aligned

Crate unicode_normalize_aligned 

Source
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 the N input 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§

UnicodeNormalization
Iterator adapters for the four normalization forms.

Functions§

is_nfc
Whether s is in NFC. Resolves a Maybe quick check by normalizing and comparing, without building a string.
is_nfc_quick
Quick check for NFC over a character iterator.
is_nfd
Whether s is in NFD. Resolves a Maybe quick check by normalizing and comparing, without building a string.
is_nfd_quick
Quick check for NFD over a character iterator.
is_nfkc
Whether s is in NFKC. Resolves a Maybe quick check by normalizing and comparing, without building a string.
is_nfkc_quick
Quick check for NFKC over a character iterator.
is_nfkd
Whether s is in NFKD. Resolves a Maybe quick check by normalizing and comparing, without building a string.
is_nfkd_quick
Quick check for NFKD over a character iterator.