focaccia

Enum CaseFold

Source
pub enum CaseFold {
    Full,
    Ascii,
    Turkic,
    Lithuanian,
}
Expand description

Unicode case folding strategies.

Unicode case folding data supports both implementations that require simple case foldings (where string lengths don’t change), and implementations that allow full case folding (where string lengths may grow). Note that where they can be supported, the full case foldings are superior: for example, they allow “MASSE” and “Maße” to match.

The CaseFold enum supports the folding strategies available in Ruby.

§Examples

For Unicode text, the default folding scheme is Full. To make a comparison:

let fold = CaseFold::Full;
assert_eq!(fold.casecmp("MASSE", "Maße"), Ordering::Equal);
assert_eq!(fold.casecmp("São Paulo", "Sao Paulo"), Ordering::Greater);

assert!(fold.case_eq("MASSE", "Maße"));
assert!(!fold.case_eq("São Paulo", "Sao Paulo"));

For text known to be ASCII, Focaccia can make a more performant comparison check:

let fold = CaseFold::Ascii;
assert_eq!(fold.casecmp("Crate: focaccia", "Crate: FOCACCIA"), Ordering::Equal);
assert_eq!(fold.casecmp("Fabled", "failed"), Ordering::Less);

assert!(fold.case_eq("Crate: focaccia", "Crate: FOCACCIA"));
assert!(!fold.case_eq("Fabled", "failed"));

Turkic case folding is similar to full case folding with additional mappings for dotted and dotless I:

let fold = CaseFold::Turkic;
assert_eq!(fold.casecmp("İstanbul", "istanbul"), Ordering::Equal);
assert_ne!(fold.casecmp("İstanbul", "Istanbul"), Ordering::Equal);

assert!(fold.case_eq("İstanbul", "istanbul"));
assert!(!fold.case_eq("İstanbul", "Istanbul"));

Variants§

§

Full

Full Unicode case mapping, suitable for most languages (see Turkic and Lithuanian strategies for exceptions). Context-dependent case mapping as described in Table 3-14 of the Unicode standard is currently not supported.

§

Ascii

Only the ASCII region, i.e. the characters “A” to “Z” and “a” to “z”, are affected.

§

Turkic

Full Unicode case mapping, adapted for Turkic languages (Turkish, Azerbaijani, …). This means that upper case I is mapped to lower case dotless i, and so on.

§

Lithuanian

Currently, just full Unicode case mapping. In the future, full Unicode case mapping adapted for Lithuanian (keeping the dot on the lower case i even if there is an accent on top).

Implementations§

Source§

impl CaseFold

Source

pub const fn new() -> Self

Construct a new full Unicode case folding.

See CaseFold::Full.

§Examples
const FOLD: CaseFold = CaseFold::new();

assert_eq!(CaseFold::new(), CaseFold::Full);

assert!(CaseFold::new().case_eq("MASSE", "Maße"));
assert!(!CaseFold::new().case_eq("São Paulo", "Sao Paulo"));
Source

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

Make a case-insensitive string comparison based on the dispatching folding strategy.

Return Ordering::Equal if the given strings match when folding case. For example, when using CaseFold::Full, ß is considered equal to ss. The differences between the folding strategies are documented on the variants of the CaseFold enum.

This function is a wrapper around the underlying scheme-specific functions.

§Examples – Full case folding
let fold = CaseFold::Full;
assert_eq!(fold.casecmp("MASSE", "Maße"), Ordering::Equal);
assert_eq!(fold.casecmp("São Paulo", "Sao Paulo"), Ordering::Greater);
§Examples – ASCII case folding
let fold = CaseFold::Ascii;
assert_eq!(fold.casecmp("Crate: focaccia", "Crate: FOCACCIA"), Ordering::Equal);
assert_eq!(fold.casecmp("Fabled", "failed"), Ordering::Less);
§Examples – Turkic case folding
let fold = CaseFold::Turkic;
assert_eq!(fold.casecmp("İstanbul", "istanbul"), Ordering::Equal);
assert_ne!(fold.casecmp("İstanbul", "Istanbul"), Ordering::Equal);
Source

pub fn case_eq(self, left: &str, right: &str) -> bool

Make a case-insensitive string equality check based on the dispatching folding strategy.

Return true if the given strings match when folding case. For example, when using CaseFold::Full, ß is considered equal to ss. The differences between the folding strategies are documented on the variants of the CaseFold enum.

This function is a wrapper around the underlying scheme-specific functions.

§Examples – Full case folding
let fold = CaseFold::Full;
assert!(fold.case_eq("MASSE", "Maße"));
assert!(!fold.case_eq("São Paulo", "Sao Paulo"));
§Examples – ASCII case folding
let fold = CaseFold::Ascii;
assert!(fold.case_eq("Crate: focaccia", "Crate: FOCACCIA"));
assert!(!fold.case_eq("Fabled", "failed"));
§Examples – Turkic case folding
let fold = CaseFold::Turkic;
assert!(fold.case_eq("İstanbul", "istanbul"));
assert!(!fold.case_eq("İstanbul", "Istanbul"));

Trait Implementations§

Source§

impl Clone for CaseFold

Source§

fn clone(&self) -> CaseFold

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 CaseFold

Source§

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

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

impl Default for CaseFold

Source§

fn default() -> Self

Default to full Unicode case folding.

See CaseFold::Full.

§Examples
assert_eq!(CaseFold::default(), CaseFold::Full);

assert!(CaseFold::default().case_eq("MASSE", "Maße"));
assert!(!CaseFold::default().case_eq("São Paulo", "Sao Paulo"));
Source§

impl Hash for CaseFold

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 CaseFold

Source§

fn cmp(&self, other: &CaseFold) -> 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,

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

impl PartialEq for CaseFold

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for CaseFold

Source§

fn partial_cmp(&self, other: &CaseFold) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<Option<&[u8]>> for CaseFold

Source§

type Error = NoSuchCaseFoldingScheme

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

fn try_from(value: Option<&[u8]>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Option<&str>> for CaseFold

Source§

type Error = NoSuchCaseFoldingScheme

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

fn try_from(value: Option<&str>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for CaseFold

Source§

impl Eq for CaseFold

Source§

impl StructuralPartialEq for CaseFold

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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

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.