pub struct Collator {
pub tailoring: Tailoring,
pub shifting: bool,
pub tiebreak: bool,
/* private fields */
}
Expand description
The Collator
struct is the entry point for this library’s API. It defines the options to be
used in collation. The method collate
will then compare two string references (or byte slices)
according to the selected options, and return an Ordering
value.
You can choose between two tables of character weights: DUCET and CLDR. With the CLDR table,
there is a further choice of locale tailoring. The Root
locale represents the table in its
unmodified form. The ArabicScript
locale shifts the weights of Arabic-script letters so that
they sort before the Latin script. Further locales will be added over time.
You can also choose between two approaches to the handling of variable-weight characters: “non-ignorable” and “shifted.” Finally, you can select whether to use byte-value comparison as a tiebreaker when two strings produce identical Unicode Collation Algorithm sort keys.
The default for Collator
is to use the CLDR table with the Root
locale; to use the “shifted”
approach for variable-weight characters; and to break ties with byte-value comparison. This
should be a good starting point for collation in many languages.
Fields§
§tailoring: Tailoring
The table of weights to be used: DUCET or CLDR (with a choice of locale for the latter)
shifting: bool
The approach to handling variable-weight characters: “non-ignorable” (i.e., false
) or
“shifted” (i.e., true
)
tiebreak: bool
Whether to use byte-value comparison as a tiebreaker when two strings produce identical Unicode Collation Algorithm sort keys
Implementations§
source§impl Collator
impl Collator
sourcepub fn new(tailoring: Tailoring, shifting: bool, tiebreak: bool) -> Self
pub fn new(tailoring: Tailoring, shifting: bool, tiebreak: bool) -> Self
Create a new Collator
with the specified options. NB: it is also possible to call
Collator::default()
.
sourcepub fn collate<T: AsRef<[u8]> + Eq + Ord + ?Sized>(
&mut self,
a: &T,
b: &T
) -> Ordering
pub fn collate<T: AsRef<[u8]> + Eq + Ord + ?Sized>( &mut self, a: &T, b: &T ) -> Ordering
This is the primary method in the library. It accepts as arguments two string references or
byte slices; compares them using the options chosen; and returns an Ordering
value. This
is designed to be passed to the sort_by
(or sort_unstable_by
) function in the standard
library. Simple usage might look like the following…
use feruca::{Collator};
let mut collator = Collator::default();
let mut names = ["Peng", "Peña", "Ernie", "Émile"];
names.sort_unstable_by(|a, b| collator.collate(a, b));
let expected = ["Émile", "Ernie", "Peña", "Peng"];
assert_eq!(names, expected);
Trait Implementations§
source§impl Ord for Collator
impl Ord for Collator
source§impl PartialEq for Collator
impl PartialEq for Collator
source§impl PartialOrd for Collator
impl PartialOrd for Collator
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more