Struct feruca::Collator

source ·
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

source

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().

source

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 Clone for Collator

source§

fn clone(&self) -> Collator

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Collator

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Collator

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Hash for Collator

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Collator

source§

fn cmp(&self, other: &Collator) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Collator

source§

fn eq(&self, other: &Collator) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Collator

source§

fn partial_cmp(&self, other: &Collator) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for Collator

source§

impl StructuralPartialEq for Collator

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.