From 8be3d9668649b7d1286f38cc2394d9d0ddcea08d Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Thu, 7 Oct 2021 14:46:14 -0600 Subject: [PATCH] Fix #75, implement UnicodeNormalization for char --- src/lib.rs | 38 +++++++++++++++++++++++++++++++++++++- src/test.rs | 5 +++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 2c3a090..062dc85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,10 @@ pub use crate::recompose::Recompositions; pub use crate::replace::Replacements; pub use crate::stream_safe::StreamSafe; pub use crate::tables::UNICODE_VERSION; -use core::str::Chars; +use core::{ + str::Chars, + option, +}; mod no_std_prelude; @@ -166,6 +169,39 @@ impl<'a> UnicodeNormalization> for &'a str { } } + +impl UnicodeNormalization> for char { + #[inline] + fn nfd(self) -> Decompositions> { + decompose::new_canonical(Some(self).into_iter()) + } + + #[inline] + fn nfkd(self) -> Decompositions> { + decompose::new_compatible(Some(self).into_iter()) + } + + #[inline] + fn nfc(self) -> Recompositions> { + recompose::new_canonical(Some(self).into_iter()) + } + + #[inline] + fn nfkc(self) -> Recompositions> { + recompose::new_compatible(Some(self).into_iter()) + } + + #[inline] + fn cjk_compat_variants(self) -> Replacements> { + replace::new_cjk_compat_variants(Some(self).into_iter()) + } + + #[inline] + fn stream_safe(self) -> StreamSafe> { + StreamSafe::new(Some(self).into_iter()) + } +} + impl> UnicodeNormalization for I { #[inline] fn nfd(self) -> Decompositions { diff --git a/src/test.rs b/src/test.rs index 2e87a87..4c37149 100644 --- a/src/test.rs +++ b/src/test.rs @@ -105,6 +105,11 @@ fn test_nfkc() { ); } +#[test] +fn test_normalize_char() { + assert_eq!('\u{2126}'.nfd().to_string(), "\u{3a9}") +} + #[test] fn test_is_combining_mark_ascii() { for cp in 0..0x7f {