| 1 |
# icu_normalizer [](https://crates.io/crates/icu_normalizer) |
| 2 |
|
| 3 |
<!-- cargo-rdme start --> |
| 4 |
|
| 5 |
Normalizing text into Unicode Normalization Forms. |
| 6 |
|
| 7 |
This module is published as its own crate ([`icu_normalizer`](https://docs.rs/icu_normalizer/latest/icu_normalizer/)) |
| 8 |
and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project. |
| 9 |
|
| 10 |
## Implementation notes |
| 11 |
|
| 12 |
The normalizer operates on a lazy iterator over Unicode scalar values (Rust `char`) internally |
| 13 |
and iterating over guaranteed-valid UTF-8, potentially-invalid UTF-8, and potentially-invalid |
| 14 |
UTF-16 is a step that doesn’t leak into the normalizer internals. Ill-formed byte sequences are |
| 15 |
treated as U+FFFD. |
| 16 |
|
| 17 |
The normalizer data layout is not based on the ICU4C design at all. Instead, the normalization |
| 18 |
data layout is a clean-slate design optimized for the concept of fusing the NFD decomposition |
| 19 |
into the collator. That is, the decomposing normalizer is a by-product of the collator-motivated |
| 20 |
data layout. |
| 21 |
|
| 22 |
Notably, the decomposition data structure is optimized for a starter decomposing to itself, |
| 23 |
which is the most common case, and for a starter decomposing to a starter and a non-starter |
| 24 |
on the Basic Multilingual Plane. Notably, in this case, the collator makes use of the |
| 25 |
knowledge that the second character of such a decomposition is a non-starter. Therefore, |
| 26 |
decomposition into two starters is handled by generic fallback path that looks the |
| 27 |
decomposition from an array by offset and length instead of baking a BMP starter pair directly |
| 28 |
into a trie value. |
| 29 |
|
| 30 |
The decompositions into non-starters are hard-coded. At present in Unicode, these appear |
| 31 |
to be special cases falling into three categories: |
| 32 |
|
| 33 |
1. Deprecated combining marks. |
| 34 |
2. Particular Tibetan vowel sings. |
| 35 |
3. NFKD only: half-width kana voicing marks. |
| 36 |
|
| 37 |
Hopefully Unicode never adds more decompositions into non-starters (other than a character |
| 38 |
decomposing to itself), but if it does, a code update is needed instead of a mere data update. |
| 39 |
|
| 40 |
The composing normalizer builds on the decomposing normalizer by performing the canonical |
| 41 |
composition post-processing per spec. As an optimization, though, the composing normalizer |
| 42 |
attempts to pass through already-normalized text consisting of starters that never combine |
| 43 |
backwards and that map to themselves if followed by a character whose decomposition starts |
| 44 |
with a starter that never combines backwards. |
| 45 |
|
| 46 |
As a difference with ICU4C, the composing normalizer has only the simplest possible |
| 47 |
passthrough (only one inversion list lookup per character in the best case) and the full |
| 48 |
decompose-then-canonically-compose behavior, whereas ICU4C has other paths between these |
| 49 |
extremes. The ICU4X collator doesn't make use of the FCD concept at all in order to avoid |
| 50 |
doing the work of checking whether the FCD condition holds. |
| 51 |
|
| 52 |
<!-- cargo-rdme end --> |
| 53 |
|
| 54 |
## More Information |
| 55 |
|
| 56 |
For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). |