From c0439f37802fe39ff3c3037bf57476d6b0f7a56c Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 15 Aug 2023 16:12:15 -0400 Subject: [PATCH] feat: add to_folded_case() method --- src/lib.rs | 18 ++++++++++++++++++ src/unicode/mod.rs | 13 +++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7d31b72..d3ca8de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -138,6 +138,24 @@ impl> UniCase { UniCase(Encoding::Unicode(Unicode(s))) } } + + /// Returns a copy of this string where each character is mapped to its + /// Unicode CaseFolding equivalent. + /// + /// # Note + /// + /// Unicode Case Folding is meant for string storage and matching, not for + /// display. + pub fn to_folded_case(&self) -> String { + #[cfg(not(__unicase__core_and_alloc))] + #[allow(deprecated, unused)] + use std::ascii::AsciiExt; + + match self.0 { + Encoding::Ascii(ref s) => s.0.as_ref().to_ascii_lowercase(), + Encoding::Unicode(ref s) => s.to_folded_case(), + } + } } impl UniCase { diff --git a/src/unicode/mod.rs b/src/unicode/mod.rs index 94592c1..faaae02 100644 --- a/src/unicode/mod.rs +++ b/src/unicode/mod.rs @@ -1,3 +1,5 @@ +#[cfg(all(__unicase__core_and_alloc, not(test)))] +use alloc::string::String; #[cfg(__unicase__iter_cmp)] use core::cmp::Ordering; use core::hash::{Hash, Hasher}; @@ -8,6 +10,12 @@ mod map; #[derive(Clone, Copy, Debug, Default)] pub struct Unicode(pub S); +impl> Unicode { + pub fn to_folded_case(&self) -> String { + self.0.as_ref().chars().flat_map(lookup).collect() + } +} + impl, S2: AsRef> PartialEq> for Unicode { #[inline] fn eq(&self, other: &Unicode) -> bool { @@ -184,6 +192,11 @@ mod tests { eq!("ᾲ στο διάολο", "ὰι στο διάολο"); } + #[test] + fn test_to_folded_case() { + assert_eq!(Unicode("Maße").to_folded_case(), "masse"); + } + #[cfg(feature = "nightly")] #[bench] fn bench_ascii_folding(b: &mut ::test::Bencher) {