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; and the ArabicInterleaved locale mixes the two scripts, so
that, e.g., alif sorts between A and B, and bā’ between B and C. 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: TailoringThe table of weights to be used: DUCET or CLDR (with a choice of locale for the latter)
shifting: boolThe approach to handling variable-weight characters: “non-ignorable” (i.e., false) or
“shifted” (i.e., true)
tiebreak: boolWhether 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);