CollatorBorrowed

Struct CollatorBorrowed 

Source
pub struct CollatorBorrowed<'a> { /* private fields */ }
Expand description

Compares strings according to culturally-relevant ordering, borrowed version.

Implementations§

Source§

impl CollatorBorrowed<'static>

Source

pub fn try_new( prefs: CollatorPreferences, options: CollatorOptions, ) -> Result<CollatorBorrowed<'static>, DataError>

Creates a collator for the given locale and options from compiled data.

Source

pub const fn static_to_owned(self) -> Collator

Cheaply converts a CollatorBorrowed<'static> into a Collator.

Note: Due to branching and indirection, using Collator might inhibit some compile-time optimizations that are possible with CollatorBorrowed.

Source§

impl CollatorBorrowed<'_>

Source

pub fn resolved_options(&self) -> ResolvedCollatorOptions

The resolved options showing how the default options, the requested options, and the options from locale data were combined.

Source

pub fn compare(&self, left: &str, right: &str) -> Ordering

Compare guaranteed well-formed UTF-8 slices.

Source

pub fn compare_utf8(&self, left: &[u8], right: &[u8]) -> Ordering

Compare potentially ill-formed UTF-8 slices. Ill-formed input is compared as if errors had been replaced with REPLACEMENT CHARACTERs according to the WHATWG Encoding Standard.

Source

pub fn compare_utf16(&self, left: &[u16], right: &[u16]) -> Ordering

Compare potentially ill-formed UTF-16 slices. Unpaired surrogates are compared as if each one was a REPLACEMENT CHARACTER.

Source

pub fn write_sort_key_to<S>( &self, s: &str, sink: &mut S, ) -> Result<<S as CollationKeySink>::Output, <S as CollationKeySink>::Error>
where S: CollationKeySink + ?Sized, <S as CollationKeySink>::State: Default,

Given valid UTF-8, write the sort key bytes up to the collator’s strength.

The bytes are written to an implementor of [CollationKeySink], a no-std version of std::io::Write. This trait is currently unstable, but it is implemented by Vec<u8>, VecDeque<u8>, SmallVec<[u8; N]>, and &mut [u8] (returning an error if the slice is too small).

If two sort keys generated at the same strength are compared bytewise, the result is the same as a collation comparison of the original strings at that strength.

For identical strength, the UTF-8 NFD normalization is appended for breaking ties.

No terminating zero byte is written to the output, so the output is not a valid C string, but the caller may append a zero afterward if a C string is desired.

⚠️ Generating a sort key is expensive relative to comparison because to compare, the collator skips identical prefixes before doing more complex comparison. Only use sort keys if you expect to compare them many times so as to amortize the cost of generating them. Measurement of this performance trade-off would be a good idea.

⚠️ Sort keys, if stored durably, should be presumed to be invalidated by a CLDR update, a new version of Unicode, or an update to the ICU4X code. Applications using sort keys must be prepared to recompute them if required and should take the performance of such an operation into account when deciding to use sort keys.

⚠️ If you should store sort keys in a database that is or becomes so large that regenerating sort keys becomes impractical, you should not expect ICU4X to support your using an older, frozen copy of the sort key generation algorithm with a later version of the library.

§Example
use icu_collator::{
    options::{CollatorOptions, Strength},
    Collator,
};
use icu_locale::locale;
let locale = locale!("utf").into();
let mut options = CollatorOptions::default();
options.strength = Some(Strength::Primary);
let collator = Collator::try_new(locale, options).unwrap();

let mut k1 = Vec::new();
let Ok(()) = collator.write_sort_key_to("hello", &mut k1);
let mut k2 = Vec::new();
let Ok(()) = collator.write_sort_key_to("Héłłö", &mut k2);
assert_eq!(k1, k2);
Source

pub fn write_sort_key_utf8_to<S>( &self, s: &[u8], sink: &mut S, ) -> Result<<S as CollationKeySink>::Output, <S as CollationKeySink>::Error>
where S: CollationKeySink + ?Sized, <S as CollationKeySink>::State: Default,

Given potentially invalid UTF-8, write the sort key bytes up to the collator’s strength.

For further details, see Self::write_sort_key_to.

Source

pub fn write_sort_key_utf16_to<S>( &self, s: &[u16], sink: &mut S, ) -> Result<<S as CollationKeySink>::Output, <S as CollationKeySink>::Error>
where S: CollationKeySink + ?Sized, <S as CollationKeySink>::State: Default,

Given potentially invalid UTF-16, write the sort key bytes up to the collator’s strength.

For further details, see Self::write_sort_key_to.

Trait Implementations§

Source§

impl<'a> Debug for CollatorBorrowed<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for CollatorBorrowed<'a>

§

impl<'a> RefUnwindSafe for CollatorBorrowed<'a>

§

impl<'a> Send for CollatorBorrowed<'a>

§

impl<'a> Sync for CollatorBorrowed<'a>

§

impl<'a> Unpin for CollatorBorrowed<'a>

§

impl<'a> UnwindSafe for CollatorBorrowed<'a>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

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>,

Source§

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,