| 1 |
# icu_collator [](https://crates.io/crates/icu_collator) |
| 2 |
|
| 3 |
<!-- cargo-rdme start --> |
| 4 |
|
| 5 |
Comparing strings according to language-dependent conventions. |
| 6 |
|
| 7 |
This module is published as its own crate ([`icu_collator`](https://docs.rs/icu_collator/latest/icu_collator/)) |
| 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 |
`Collator` is the main structure of the component. It accepts a set of arguments |
| 10 |
which allow it to collect necessary data from the data provider, and once |
| 11 |
instantiated, can be used to compare strings. |
| 12 |
|
| 13 |
Refer to the ICU User Guide sections for Collation that give an |
| 14 |
[introduction](https://unicode-org.github.io/icu/userguide/collation/) and explain |
| 15 |
[basic concepts](https://unicode-org.github.io/icu/userguide/collation/concepts.html). |
| 16 |
|
| 17 |
## Examples |
| 18 |
|
| 19 |
As its most basic purpose, `Collator` offers locale-aware ordering: |
| 20 |
|
| 21 |
```rust |
| 22 |
use core::cmp::Ordering; |
| 23 |
use icu::collator::{options::*, *}; |
| 24 |
use icu::locale::locale; |
| 25 |
|
| 26 |
let mut options = CollatorOptions::default(); |
| 27 |
options.strength = Some(Strength::Primary); |
| 28 |
let collator_es = |
| 29 |
Collator::try_new(locale!("es-u-co-trad").into(), options).unwrap(); |
| 30 |
|
| 31 |
// "pollo" > "polvo" in traditional Spanish |
| 32 |
assert_eq!(collator_es.compare("pollo", "polvo"), Ordering::Greater); |
| 33 |
|
| 34 |
let mut options = CollatorOptions::default(); |
| 35 |
options.strength = Some(Strength::Primary); |
| 36 |
let collator_en = Collator::try_new(locale!("en").into(), options).unwrap(); |
| 37 |
|
| 38 |
// "pollo" < "polvo" according to English rules |
| 39 |
assert_eq!(collator_en.compare("pollo", "polvo"), Ordering::Less); |
| 40 |
``` |
| 41 |
|
| 42 |
### Examples of `CollatorOptions` |
| 43 |
|
| 44 |
The [`CollatorOptions`] struct configures specific custom behavior for the `Collator`. See docs |
| 45 |
for [`CollatorOptions`] for more details. Some basic descriptions and examples are below. |
| 46 |
|
| 47 |
### Strength |
| 48 |
|
| 49 |
The collation strength indicates how many levels to compare. The primary |
| 50 |
level considers base letters, i.e. 'a' and 'b' are unequal but 'E' and 'é' |
| 51 |
are equal, with higher levels dealing with distinctions such as accents |
| 52 |
and case. |
| 53 |
|
| 54 |
If an lower level isn't equal, the lower level is decisive. |
| 55 |
If the comparison result is equal on one level, |
| 56 |
but the collator's strength input value is higher than that, |
| 57 |
then the collator comparison iteratively proceeds to the next higher level. |
| 58 |
|
| 59 |
Note that lowering the strength value given to the collator means that more user-perceptible |
| 60 |
differences will compare as equal. This may make sense when sorting more complex structures |
| 61 |
where the string to be compared is just one field, and ties between strings |
| 62 |
that differ only in case, accent, or similar are resolved by comparing some |
| 63 |
secondary field in the larger structure to be sorted. |
| 64 |
|
| 65 |
Therefore, if the sort is just a string sort without some other field for |
| 66 |
resolving ties, lowering the strength means that factors that don't make |
| 67 |
sense to the user (such as the order of items prior to sorting with a stable |
| 68 |
sort algorithm or the internal details of a sorting algorithm that doesn't |
| 69 |
provide the stability property) affect the relative order of strings that |
| 70 |
do have user-perceptible differences particularly in accents or case. |
| 71 |
|
| 72 |
Lowering the strength is less of a perfomance optimization than it may seem |
| 73 |
directly from the above description. As described above, in the case |
| 74 |
of identical strings to be compared, the algorithm has to work though all |
| 75 |
the levels, from primary up to the provided strength value given to collator, without an early exit. However, this |
| 76 |
collator implements an identical prefix optimization, which examines the |
| 77 |
code units of the strings to be compared to skip the identical prefix before |
| 78 |
starting the actual collation algorithm. When the strings to be compared |
| 79 |
are identical on the byte level, they are found to be equal without the |
| 80 |
actual collation algorithm running at all! Therefore, the strength setting |
| 81 |
only has an effect (whether order effect or performance effect) for |
| 82 |
comparisons where the strings to be compared are not equal on the byte level |
| 83 |
but are equal on the primary level/strength. The common cases are that |
| 84 |
a comparison is decided on the primary level or the strings are byte |
| 85 |
equal, which narrows the performance effect of lowering the strength |
| 86 |
setting. |
| 87 |
|
| 88 |
```rust |
| 89 |
use core::cmp::Ordering; |
| 90 |
use icu::collator::{options::*, *}; |
| 91 |
|
| 92 |
// Primary Level |
| 93 |
|
| 94 |
let mut options_l1 = CollatorOptions::default(); |
| 95 |
options_l1.strength = Some(Strength::Primary); |
| 96 |
let collator_l1 = |
| 97 |
Collator::try_new(Default::default(), options_l1).unwrap(); |
| 98 |
|
| 99 |
assert_eq!(collator_l1.compare("a", "b"), Ordering::Less); // primary |
| 100 |
assert_eq!(collator_l1.compare("as", "às"), Ordering::Equal); // secondary |
| 101 |
assert_eq!(collator_l1.compare("às", "at"), Ordering::Less); |
| 102 |
assert_eq!(collator_l1.compare("ao", "Ao"), Ordering::Equal); // tertiary |
| 103 |
assert_eq!(collator_l1.compare("Ao", "aò"), Ordering::Equal); |
| 104 |
assert_eq!(collator_l1.compare("A", "Ⓐ"), Ordering::Equal); |
| 105 |
|
| 106 |
// Secondary Level |
| 107 |
|
| 108 |
let mut options_l2 = CollatorOptions::default(); |
| 109 |
options_l2.strength = Some(Strength::Secondary); |
| 110 |
let collator_l2 = |
| 111 |
Collator::try_new(Default::default(), options_l2).unwrap(); |
| 112 |
|
| 113 |
assert_eq!(collator_l2.compare("a", "b"), Ordering::Less); // primary |
| 114 |
assert_eq!(collator_l2.compare("as", "às"), Ordering::Less); // secondary |
| 115 |
assert_eq!(collator_l2.compare("às", "at"), Ordering::Less); |
| 116 |
assert_eq!(collator_l2.compare("ao", "Ao"), Ordering::Equal); // tertiary |
| 117 |
assert_eq!(collator_l2.compare("Ao", "aò"), Ordering::Less); |
| 118 |
assert_eq!(collator_l2.compare("A", "Ⓐ"), Ordering::Equal); |
| 119 |
|
| 120 |
// Tertiary Level |
| 121 |
|
| 122 |
let mut options_l3 = CollatorOptions::default(); |
| 123 |
options_l3.strength = Some(Strength::Tertiary); |
| 124 |
let collator_l3 = |
| 125 |
Collator::try_new(Default::default(), options_l3).unwrap(); |
| 126 |
|
| 127 |
assert_eq!(collator_l3.compare("a", "b"), Ordering::Less); // primary |
| 128 |
assert_eq!(collator_l3.compare("as", "às"), Ordering::Less); // secondary |
| 129 |
assert_eq!(collator_l3.compare("às", "at"), Ordering::Less); |
| 130 |
assert_eq!(collator_l3.compare("ao", "Ao"), Ordering::Less); // tertiary |
| 131 |
assert_eq!(collator_l3.compare("Ao", "aò"), Ordering::Less); |
| 132 |
assert_eq!(collator_l3.compare("A", "Ⓐ"), Ordering::Less); |
| 133 |
``` |
| 134 |
|
| 135 |
### Alternate Handling |
| 136 |
|
| 137 |
Allows alternate handling for certain customized collation orderings, including the option to |
| 138 |
ignore the special handling for the strings of such customizations. Specifically, |
| 139 |
alternate handling is used to control the handling of the so-called **variable** characters in the |
| 140 |
Unicode Collation Algorithm: whitespace, punctuation and symbols. |
| 141 |
|
| 142 |
Note that `AlternateHandling::ShiftTrimmed` and `AlternateHandling::Blanked` are |
| 143 |
unimplemented. The default is `AlternateHandling::NonIgnorable`, except |
| 144 |
for Thai, whose default is `AlternateHandling::Shifted`. |
| 145 |
|
| 146 |
```rust |
| 147 |
use core::cmp::Ordering; |
| 148 |
use icu::collator::{*, options::*}; |
| 149 |
|
| 150 |
// If alternate handling is set to `NonIgnorable`, then differences among |
| 151 |
// these characters are of the same importance as differences among letters. |
| 152 |
|
| 153 |
let mut options_3n = CollatorOptions::default(); |
| 154 |
options_3n.strength = Some(Strength::Tertiary); |
| 155 |
options_3n.alternate_handling = Some(AlternateHandling::NonIgnorable); |
| 156 |
let collator_3n = |
| 157 |
Collator::try_new(Default::default(), options_3n).unwrap(); |
| 158 |
|
| 159 |
assert_eq!(collator_3n.compare("di Silva", "Di Silva"), Ordering::Less); |
| 160 |
assert_eq!(collator_3n.compare("Di Silva", "diSilva"), Ordering::Less); |
| 161 |
assert_eq!(collator_3n.compare("diSilva", "U.S.A."), Ordering::Less); |
| 162 |
assert_eq!(collator_3n.compare("U.S.A.", "USA"), Ordering::Less); |
| 163 |
|
| 164 |
// If alternate handling is set to `Shifted`, then these characters are of only minor |
| 165 |
// importance. The Shifted value is often used in combination with Strength |
| 166 |
// set to Quaternary. |
| 167 |
|
| 168 |
let mut options_3s = CollatorOptions::default(); |
| 169 |
options_3s.strength = Some(Strength::Tertiary); |
| 170 |
options_3s.alternate_handling = Some(AlternateHandling::Shifted); |
| 171 |
let collator_3s = |
| 172 |
Collator::try_new(Default::default(), options_3s).unwrap(); |
| 173 |
|
| 174 |
assert_eq!(collator_3s.compare("di Silva", "diSilva"), Ordering::Equal); |
| 175 |
assert_eq!(collator_3s.compare("diSilva", "Di Silva"), Ordering::Less); |
| 176 |
assert_eq!(collator_3s.compare("Di Silva", "U.S.A."), Ordering::Less); |
| 177 |
assert_eq!(collator_3s.compare("U.S.A.", "USA"), Ordering::Equal); |
| 178 |
|
| 179 |
let mut options_4s = CollatorOptions::default(); |
| 180 |
options_4s.strength = Some(Strength::Quaternary); |
| 181 |
options_4s.alternate_handling = Some(AlternateHandling::Shifted); |
| 182 |
let collator_4s = |
| 183 |
Collator::try_new(Default::default(), options_4s).unwrap(); |
| 184 |
|
| 185 |
assert_eq!(collator_4s.compare("di Silva", "diSilva"), Ordering::Less); |
| 186 |
assert_eq!(collator_4s.compare("diSilva", "Di Silva"), Ordering::Less); |
| 187 |
assert_eq!(collator_4s.compare("Di Silva", "U.S.A."), Ordering::Less); |
| 188 |
assert_eq!(collator_4s.compare("U.S.A.", "USA"), Ordering::Less); |
| 189 |
``` |
| 190 |
|
| 191 |
### Case Level |
| 192 |
|
| 193 |
Whether to distinguish case in sorting, even for sorting levels higher than tertiary, |
| 194 |
without having to use tertiary level just to enable case level differences. |
| 195 |
|
| 196 |
```rust |
| 197 |
use core::cmp::Ordering; |
| 198 |
use icu::collator::{*, options::*}; |
| 199 |
|
| 200 |
// Primary |
| 201 |
|
| 202 |
let mut options = CollatorOptions::default(); |
| 203 |
options.strength = Some(Strength::Primary); |
| 204 |
options.case_level = Some(CaseLevel::Off); |
| 205 |
let primary = |
| 206 |
Collator::try_new(Default::default(), |
| 207 |
options).unwrap(); |
| 208 |
|
| 209 |
assert_eq!(primary.compare("ⓓⓔⓐⓛ", "DEAL"), Ordering::Equal); |
| 210 |
assert_eq!(primary.compare("dejavu", "dejAvu"), Ordering::Equal); |
| 211 |
assert_eq!(primary.compare("dejavu", "déjavu"), Ordering::Equal); |
| 212 |
|
| 213 |
// Primary with case level on |
| 214 |
|
| 215 |
options.strength = Some(Strength::Primary); |
| 216 |
options.case_level = Some(CaseLevel::On); |
| 217 |
let primary_and_case = |
| 218 |
Collator::try_new(Default::default(), |
| 219 |
options).unwrap(); |
| 220 |
|
| 221 |
assert_eq!(primary_and_case.compare("ⓓⓔⓐⓛ", "DEAL"), Ordering::Less); |
| 222 |
assert_eq!(primary_and_case.compare("dejavu", "dejAvu"), Ordering::Less); |
| 223 |
assert_eq!(primary_and_case.compare("dejavu", "déjavu"), Ordering::Equal); |
| 224 |
|
| 225 |
// Secondary with case level on |
| 226 |
|
| 227 |
options.strength = Some(Strength::Secondary); |
| 228 |
options.case_level = Some(CaseLevel::On); |
| 229 |
let secondary_and_case = |
| 230 |
Collator::try_new(Default::default(), |
| 231 |
options).unwrap(); |
| 232 |
|
| 233 |
assert_eq!(secondary_and_case.compare("ⓓⓔⓐⓛ", "DEAL"), Ordering::Less); |
| 234 |
assert_eq!(secondary_and_case.compare("dejavu", "dejAvu"), Ordering::Less); |
| 235 |
assert_eq!(secondary_and_case.compare("dejavu", "déjavu"), Ordering::Less); // secondary difference |
| 236 |
|
| 237 |
// Tertiary |
| 238 |
|
| 239 |
options.strength = Some(Strength::Tertiary); |
| 240 |
options.case_level = Some(CaseLevel::Off); |
| 241 |
let tertiary = |
| 242 |
Collator::try_new(Default::default(), |
| 243 |
options).unwrap(); |
| 244 |
|
| 245 |
assert_eq!(tertiary.compare("ⓓⓔⓐⓛ", "DEAL"), Ordering::Less); |
| 246 |
assert_eq!(tertiary.compare("dejavu", "dejAvu"), Ordering::Less); |
| 247 |
assert_eq!(tertiary.compare("dejavu", "déjavu"), Ordering::Less); |
| 248 |
``` |
| 249 |
|
| 250 |
|
| 251 |
### Backward second level |
| 252 |
|
| 253 |
Compare the second level in backward order. The default is `false` (off), except for Canadian |
| 254 |
French. |
| 255 |
|
| 256 |
### Examples of `CollatorPreferences` |
| 257 |
|
| 258 |
The [`CollatorPreferences`] struct configures specific custom behavior for the `Collator`, like |
| 259 |
[`CollatorOptions`]. However, unlike `CollatorOptions`, this set of preferences can also be set |
| 260 |
implicitly by the locale. See docs for [`CollatorPreferences`] for more details. |
| 261 |
Some basic descriptions and examples are below. |
| 262 |
|
| 263 |
### Case First |
| 264 |
|
| 265 |
Whether to swap the ordering of uppercase and lowercase. |
| 266 |
|
| 267 |
```rust |
| 268 |
use core::cmp::Ordering; |
| 269 |
use icu::collator::preferences::*; |
| 270 |
use icu::collator::{options::*, *}; |
| 271 |
|
| 272 |
// Use the locale's default. |
| 273 |
|
| 274 |
let mut prefs_no_case = CollatorPreferences::default(); |
| 275 |
prefs_no_case.case_first = Some(CollationCaseFirst::False); |
| 276 |
let collator_no_case = |
| 277 |
Collator::try_new(prefs_no_case, Default::default()).unwrap(); |
| 278 |
assert_eq!(collator_no_case.compare("ab", "AB"), Ordering::Less); |
| 279 |
|
| 280 |
// Lowercase is less |
| 281 |
|
| 282 |
let mut prefs_lower_less = CollatorPreferences::default(); |
| 283 |
prefs_lower_less.case_first = Some(CollationCaseFirst::Lower); |
| 284 |
let collator_lower_less = |
| 285 |
Collator::try_new(prefs_lower_less, Default::default()).unwrap(); |
| 286 |
assert_eq!(collator_lower_less.compare("ab", "AB"), Ordering::Less); |
| 287 |
|
| 288 |
// Uppercase is less |
| 289 |
|
| 290 |
let mut prefs_upper_greater = CollatorPreferences::default(); |
| 291 |
prefs_upper_greater.case_first = Some(CollationCaseFirst::Upper); |
| 292 |
let collator_upper_greater = |
| 293 |
Collator::try_new(prefs_upper_greater, Default::default()).unwrap(); |
| 294 |
assert_eq!(collator_upper_greater.compare("AB", "ab"), Ordering::Less); |
| 295 |
``` |
| 296 |
|
| 297 |
### Numeric |
| 298 |
|
| 299 |
When set to `true` (on), any sequence of decimal |
| 300 |
digits is sorted at a primary level according to the |
| 301 |
numeric value. |
| 302 |
|
| 303 |
```rust |
| 304 |
use core::cmp::Ordering; |
| 305 |
use icu::collator::preferences::*; |
| 306 |
use icu::collator::{options::*, *}; |
| 307 |
|
| 308 |
// Numerical sorting off |
| 309 |
|
| 310 |
let mut prefs_num_off = CollatorPreferences::default(); |
| 311 |
prefs_num_off.numeric_ordering = Some(CollationNumericOrdering::False); |
| 312 |
let collator_num_off = |
| 313 |
Collator::try_new(prefs_num_off, Default::default()).unwrap(); |
| 314 |
assert_eq!(collator_num_off.compare("a10b", "a2b"), Ordering::Less); |
| 315 |
|
| 316 |
// Numerical sorting on |
| 317 |
|
| 318 |
let mut prefs_num_on = CollatorPreferences::default(); |
| 319 |
prefs_num_on.numeric_ordering = Some(CollationNumericOrdering::True); |
| 320 |
let collator_num_on = |
| 321 |
Collator::try_new(prefs_num_on, Default::default()).unwrap(); |
| 322 |
assert_eq!(collator_num_on.compare("a10b", "a2b"), Ordering::Greater); |
| 323 |
``` |
| 324 |
|
| 325 |
[`CollatorOptions`]: options::CollatorOptions |
| 326 |
|
| 327 |
<!-- cargo-rdme end --> |
| 328 |
|
| 329 |
## More Information |
| 330 |
|
| 331 |
For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). |