Skip to main content

Normalizer

Struct Normalizer 

Source
pub struct Normalizer { /* private fields */ }
Expand description

Folds case and diacritics, identically for terms and queries.

Both foldings are on by default (Normalizer::new). Diacritic folding runs first, then case folding; the result depends only on the input and the two flags, and normalising twice gives the same result as normalising once.

use keyhammer::text::Normalizer;

let n = Normalizer::new();
assert_eq!(n.normalize("Ação"), "acao");
assert_eq!(n.normalize("naïve CAFÉ"), "naive cafe");
// A decomposed accent (e + U+0301) is dropped too.
assert_eq!(n.normalize("cafe\u{301}"), "cafe");
// No Turkish tailoring: every i-like letter becomes i.
assert_eq!(n.normalize("Iıİi"), "iiii");
// Other scripts are copied unchanged.
assert_eq!(n.normalize("Ωmega"), "Ωmega");

Implementations§

Source§

impl Normalizer

Source

pub const fn new() -> Self

Case folding and diacritic folding.

Source

pub const fn with_case_folding(self, on: bool) -> Self

The same normaliser with case folding on or off.

Source

pub const fn with_diacritic_folding(self, on: bool) -> Self

The same normaliser with diacritic folding on or off.

Source

pub const fn folds_case(self) -> bool

Whether case is folded.

Source

pub const fn folds_diacritics(self) -> bool

Whether diacritics are folded.

Source

pub fn normalize_char(self, c: char, emit: impl FnMut(char))

Calls emit with the characters c normalises to (none, one or up to three), in order.

use keyhammer::text::Normalizer;
let mut out = String::new();
Normalizer::new().normalize_char('ß', |c| out.push(c));
assert_eq!(out, "ss");
Source

pub fn normalize(&self, s: &str) -> String

The normalised form of s.

Source

pub fn normalize_into(&self, s: &str, out: &mut String)

Clears out and writes the normalised form of s into it, reusing its allocation.

Source

pub fn normalize_mapped(&self, s: &str, map: &mut SourceMap) -> String

The normalised form of s, and in map where each of its code points came from (see SourceMap). map is cleared first.

use keyhammer::text::{Normalizer, SourceMap};

let mut map = SourceMap::new();
let n = Normalizer::new().normalize_mapped("Maße", &mut map);
assert_eq!(n, "masse");
// "ss" (output code points 2..4) comes from "ß" (input code point 2).
assert_eq!(map.to_source(2..4), Some(2..3));
assert_eq!(map.to_source(3..5), Some(2..4));

Trait Implementations§

Source§

impl Clone for Normalizer

Source§

fn clone(&self) -> Normalizer

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Normalizer

Source§

impl Debug for Normalizer

Source§

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

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

impl Default for Normalizer

Source§

fn default() -> Self

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

impl Eq for Normalizer

Source§

impl Hash for Normalizer

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 PartialEq for Normalizer

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Normalizer

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, dest: *mut u8)

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