JFLib

ui/i18n_collate.rs#

Source: lib/ui/i18n_collate.rs

i18n_collate -- sorting by language (collation) and Unicode case mapping

  collate(a, b) -> Ordering           two texts in the order of the user's language
                                      (locale_tag(): the format locale, as Windows'
                                      CompareStringEx): base letters first, then accents,
                                      then case: "a" < "A" < "á" < "ä" < "b"
  collator() / Collator::new(tag)     a collator with options: .strength(COLL_PRIMARY ..
                                      COLL_IDENTICAL), .numeric(true) ("Datei 2" before
                                      "Datei 10"), .phonebook(true) (German phone book:
                                      ä = ae); .compare(a, b), .key(s) (a sort key: keys
                                      compare like their texts), .sort(items, text).  The
                                      tag may carry "-u-co-phonebk", "-u-kn", "-u-ks-level1"
  Locale::collate(a, b) / collator()  the same for a fixed region
  to_upper(s) / to_lower(s) / fold(s) case mapping of all of Unicode in the user's language:
                                      ß -> SS, "ΟΔΟΣ" -> "οδος" (final sigma), Turkish and
                                      Azeri i / İ and ı / I; fold() for comparisons (ß and
                                      ẞ -> ss, ς -> σ)
  to_upper_in(lang, s) / to_lower_in / fold_in   the same for a language
  eq_ignore_case(a, b)                equal except for case (also composed against
                                      decomposed accents: "É" = "e\u{301}")

The collation is a compact form of the Unicode Collation Algorithm, not the CLDR data:
every character becomes collation elements (primary: the letter, secondary: its accents,
tertiary: case and variants such as ß or ſ); the canonical decompositions come from the
generated table i18n_unicode.rs (tools/gen_unicode.py).  Order of the primaries: spaces,
punctuation and symbols (by code point), digits (by value), Latin letters a .. z, then the
letters of the other scripts by code point (Greek, Cyrillic, Hebrew, Arabic, ..., kana as
hiragana, CJK).  Language rules where they are usual: German (ä like a; phone book ä = ae),
Swedish / Finnish (å ä ö after z), Danish / Norwegian (æ ø å after z, aa = å), Spanish
(ñ after n), Czech / Slovak (č ř š ž, ch after h), Polish (ą ć ę ł ń ó ś ź ż), Turkish
(ç ğ ı ö ş ü, I is the capital of ı).  Everywhere: ß = ss, æ = ae, œ = oe, ø ł đ like o l d
with a secondary difference, ð þ ŋ ı letters of their own, Cyrillic і ї й ґ є ў ђ ј љ њ ћ
џ in their alphabetical places, Hebrew final forms as variants.

core and alloc bring the default case mappings of all of Unicode (char::to_uppercase,
str::to_lowercase with the final sigma); this module adds the language rules (Turkish,
Azeri) and case folding.  Not done: Lithuanian dot rules, Greek upper case without accents.
/// collation strength: base letters only ("a" = "A" = "ä")
COLL_PRIMARY: u32 = 1
/// collation strength: base letters and accents ("a" = "A" < "ä"): sorting without case
COLL_SECONDARY: u32 = 2
/// collation strength: base letters, accents and case ("a" < "A" < "ä"; the default)
COLL_TERTIARY: u32 = 3
/// collation strength: as COLL_TERTIARY, then the code points (only equal texts compare equal)
COLL_IDENTICAL: u32 = 4
  • struct Collator compares texts in the order of a language (see the module comment); copy it freely

impl Collator

  • fn new(tag: &str) -> Collator the collator of a language (a BCP 47 tag or a POSIX name; unknown languages sort by the common rules); "-u-co-phonebk", "-u-kn" and "-u-ks-level1 / 2 / 3 / identic" set the options as in ICU, also "de-DE_phoneb" (Windows) and "@collation=phonebook" (POSIX)

  • fn strength(mut self, s: u32) -> Collator the same collator with another strength (COLL_PRIMARY .. COLL_IDENTICAL)

  • fn numeric(mut self, on: bool) -> Collator numbers by their value: "Datei 2" before "Datei 10", "007" = "7" up to the tertiary level

  • fn phonebook(mut self, on: bool) -> Collator German phone book order (ä = ae, ö = oe, ü = ue); no effect in other languages

  • fn compare(&self, a: &str, b: &str) -> Ordering compare two texts

  • fn key(&self, s: &str) -> Vec<u32> the sort key of a text: keys compare as their texts do (sort_by_cached_key, indexes)

  • fn sort<T>(&self, items: &mut [T], text: impl Fn(&T) -> &str) sort items by a text of each (stable; each key is computed once)

  • fn collator() -> Collator the user's collator (the language of locale_tag(), tertiary strength)

  • fn collate(a: &str, b: &str) -> Ordering compare two texts in the order of the user's language (see the module comment)

impl Locale

  • fn collate(&self, a: &str, b: &str) -> Ordering compare two texts in the order of the region's language

  • fn collator(&self) -> Collator the collator of the region's language

  • fn to_upper(s: &str) -> String upper case in the user's language: "straße" -> "STRASSE", Turkish "i" -> "İ"

  • fn to_lower(s: &str) -> String lower case in the user's language: "ΟΔΟΣ" -> "οδος", Turkish "I" -> "ı"

  • fn fold(s: &str) -> String case folding in the user's language (for comparisons, not for display): "Straße" -> "strasse", "ΣΟΦΟΣ" and "σοφος" -> "σοφοσ"

  • fn eq_ignore_case(a: &str, b: &str) -> bool equal except for case in the user's language ("STRASSE" = "Straße"); composed and decomposed accents are equal too

  • fn to_upper_in(lang: &str, s: &str) -> String upper case in a language (BCP 47 tag)

  • fn to_lower_in(lang: &str, s: &str) -> String lower case in a language (BCP 47 tag)

  • fn fold_in(lang: &str, s: &str) -> String case folding in a language (BCP 47 tag): the full folding of Unicode's CaseFolding.txt, Turkish and Azeri with their I / ı and İ / i

Source: docs/API.md in the JFLib repository.