Expand description
§unicode_lang
The Unicode text primitives a language front end needs: identifier rules, normalization, and display width — with correct-by-construction tables and no third-party dependencies.
A lexer has to answer three Unicode questions that the standard library does
not: may this scalar start or continue an identifier? (UAX #31), are
these two strings the same once normalized? (UAX #15), and how many
columns does this text occupy? (UAX #11). unicode-lang answers all three
from compact lookup tables generated directly from the Unicode Character
Database, so the crate carries no runtime dependencies and every query is a
branch-predictable binary search.
§At a glance
- Identifiers —
is_xid_start,is_xid_continue, and the whole-stringis_xidimplement the UAX #31XIDprofile. - Width —
char_widthandstr_widthgive monospace column counts (0 / 1 / 2),wcwidth-style. - Normalization —
normalizeandis_normalized, selected byForm, implement all four forms (NFC, NFD, NFKC, NFKD) and are verified against the official conformance suite. (Requires theallocfeature, enabled by default viastd.) UNICODE_VERSIONrecords the UCD version the tables were built from.
§Example
use unicode_lang::{char_width, is_xid, normalize, Form};
// Recognise an identifier that mixes scripts.
assert!(is_xid("Δpressure"));
// Fold a compatibility ligature and recompose.
assert_eq!(normalize("file", Form::Nfkc), "file");
// Measure a mixed-width string for column alignment.
assert_eq!(char_width('世'), 2);§no_std
The crate is no_std. Identifier and width queries need no allocator and
are available with no features at all. Normalization allocates its output,
so it is gated behind the alloc feature (implied by the default std
feature); disable default features for a bare no_std build without
normalization. The optional serde feature derives Serialize /
Deserialize for Form.
Enums§
- Form
alloc - A Unicode normalization form, selecting the target shape for
normalizeandis_normalized.
Constants§
- UNICODE_
VERSION - Unicode version these tables were generated from.
Functions§
- char_
width - Returns the number of monospace columns
coccupies:0,1, or2. - is_
normalized alloc - Returns
trueifsis already in normalization formform. - is_xid
- Returns
trueifsis a well-formed Unicode identifier under the default UAX #31 profile: it is non-empty, its first scalar isis_xid_start, and every remaining scalar isis_xid_continue. - is_
xid_ continue - Returns
trueifcmay continue a Unicode identifier — that is, ifchas theXID_Continueproperty. - is_
xid_ start - Returns
trueifcmay begin a Unicode identifier — that is, ifchas theXID_Startproperty. - normalize
alloc - Returns
snormalized toform. - str_
width - Returns the total monospace width of
s: the sum ofchar_widthover its scalar values.