Expand description
Unicode character properties, and grapheme, word and line segmentation.
The crate reads its own compact tables, generated from the Unicode
Character Database. It is no_std, depends on nothing but core and
alloc, and no lookup or scan allocates or panics, whatever the input.
§Properties
core_props and rare_props return a character’s properties, each a
single table lookup and a u32. CoreProps holds the ones a text
layout reads for every character: the bidi class, the script and its
extensions, paired brackets, and the emoji and break-related flags.
RareProps holds the ones read less often: Grapheme_Cluster_Break,
East_Asian_Width, Vertical_Orientation and the casing properties.
line_break returns a character’s Line_Break.
§Segmentation
Each segmenter reads a stream of characters with their offsets, such as
Utf8Scalars or Utf16Scalars, and yields the offsets of the
boundaries inside it.
Graphemesfinds extended grapheme clusters (UAX #29).GraphemeStateis its state machine, for a scan that holds the characters already.LineBreaksfinds line break opportunities (UAX #14), under the CSS tailoringsLineOptionsselects.WordBreaksfinds word boundaries (UAX #29), with the options ofWordOptions.
Thai, Lao, Khmer and Myanmar have no rules for breaks between words, and
Chinese and Japanese words follow no rule either. Inside a run of one of
them the line and word segmenters break at the words of a Dictionary,
the longest at each position. Dictionaries holds one per script,
compiled in with the dictionaries feature or loaded at run time.
use cleft::{LineBreaks, LineOptions, Utf8Scalars, WordBreaks, WordOptions};
let text = "Can't stop, 3.14!";
let lines: Vec<usize> = LineBreaks::new(Utf8Scalars::new(text), LineOptions::new()).collect();
assert_eq!(lines, [6, 12]);
let words: Vec<usize> = WordBreaks::new(Utf8Scalars::new(text), WordOptions::new()).collect();
assert_eq!(words, [5, 6, 10, 11, 12, 16]);The data and rules are of the Unicode version UNICODE_VERSION names.
§Features
No feature is on by default.
css: the classes CSS defines and Unicode does not, read from the rare properties:EastAsianSpacingfortext-autospace,TextSpacingClassfortext-spacing-trim,EmphasisClassfortext-emphasis-skipandFirstLetterClassfor::first-letter. Without it a smaller table is compiled, still read by one lookup.case: full case mapping,uppercase,lowercaseandtitlecase, with the language tailorings ofCaseLanguage(about 5 KB of tables).dictionaries: the word dictionaries of Thai, Lao, Khmer, Myanmar, and Chinese and Japanese, generated from ICU’s word lists and compiled in (about 2.7 MB):Dictionaries::with_compiled.
§License
The crate is licensed under MIT or Apache-2.0. Its tables are generated
from the Unicode Character Database, under the Unicode License v3
(LICENSE-UNICODE). Its dictionaries are generated from ICU’s word
lists, whose notices are in THIRD-PARTY.
Structs§
- Bidi
Class - A Bidi_Class, in ICU4C’s
UCharDirectionnumbering. - Bracket
Pair Id - A bracket pair, which indexes the table of closing brackets.
- Core
Props - The core properties of a character, packed in one word.
- Dictionaries
- The word dictionaries a segmenter reads, one per script, each optional.
- Dictionary
- One script’s words, as a validated automaton.
- Grapheme
Props - The properties UAX #29 reads of a character, packed in a byte.
- Grapheme
State - The state of a forward scan for grapheme cluster boundaries.
- Graphemes
- The offsets where the extended grapheme clusters of a stream of characters start.
- Line
Break - A Line_Break value (UAX #14), in ICU4C’s
ULineBreaknumbering. - Line
Breaks - The offsets where a line may break in a stream of characters.
- Line
Options - The tailorings a
LineBreaksapplies. The default is plain UAX #14. - Rare
Props - The rare properties of a character, packed in one word.
- Script
Id - A script in ICU4C’s
UScriptCodenumbering, which indexes the table of script tags. - Script
With Ext - A character’s Script and Script_Extensions in ten bits, after ICU’s
ScriptWithExtensionsencoding. - Utf8
Scalars - The characters of a UTF-8 string, with their byte offsets.
- Utf16
Scalars - The characters of a UTF-16 string, with their code unit offsets.
- Word
Breaks - The offsets of the word boundaries in a stream of characters.
- Word
Options - The tailorings a
WordBreaksapplies. The default is plain UAX #29.
Enums§
- Bytes
- The bytes of a dictionary: compiled in, or shared.
- Case
Language - The rules a language maps case by.
- Dictionary
Error - Why bytes are not a dictionary.
- Dictionary
Script - The script a dictionary finds words in.
- East
Asian Spacing - A character’s class for
text-autospace: UTR #59’s East_Asian_Spacing. - Emphasis
Class - A character’s class for
text-emphasis-skip(CSS Text Decoration 4, section 3.5), from its General_Category. - First
Letter Class - A character’s class for
::first-letter, from its General_Category. - Grapheme
Cluster Break - Grapheme_Cluster_Break (UAX #29), in ICU4C’s numbering except for
Zwj. - Line
Strictness - How strictly lines break, as CSS’s
line-breaksets it. - Line
Word Option - Which breaks within words are allowed, as CSS’s
word-breaksets it. - Text
Spacing Class - A character’s text-spacing class (CSS Text 4, section 8.5).
Constants§
- UNICODE_
VERSION - The version of Unicode the properties and case mappings follow, as (major, minor, update).
Functions§
- core_
props - Returns the core properties of
ch: the ones a layout reads for every character. - grapheme_
props - Returns the properties UAX #29 reads of
ch. - line_
break - Returns the Line_Break property of
ch, as UAX #14 assigns it. - lowercase
- Calls
pushwith each character of the full lowercase oftext. - rare_
props - Returns the rare properties of
ch: the ones a layout reads per cluster or behind a gate. - titlecase
- Calls
pushwith each character ofsegmentwith its first character titlecased and the rest as they are. - uppercase
- Calls
pushwith each character of the full uppercase oftext.