diff --git a/src/lib.rs b/src/lib.rs index 0ac1995..39c2dd1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -261,7 +261,11 @@ macro_rules! into_impl { impl> From for UniCase { fn from(s: S) -> Self { - UniCase::unicode(s) + if s.as_ref().is_ascii() { + UniCase::ascii(s) + } else { + UniCase::unicode(s) + } } } @@ -308,6 +312,7 @@ impl> FromStr for UniCase { #[cfg(test)] mod tests { use super::UniCase; + use alloc::borrow::Cow; use std::borrow::ToOwned; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; @@ -333,16 +338,47 @@ mod tests { let a = UniCase::new("foobar"); let b = UniCase::new("FOOBAR"); let c = UniCase::ascii("FoObAr"); + let d = UniCase::<&str>::from("foobar"); assert_eq!(a, b); - assert_eq!(b, a); assert_eq!(a, c); + assert_eq!(a, d); + + assert_eq!(b, a); + assert_eq!(b, c); + assert_eq!(b, d); + assert_eq!(c, a); + assert_eq!(c, b); + assert_eq!(c, d); + + assert_eq!(d, a); + assert_eq!(d, b); + assert_eq!(d, c); + assert_eq!(hash(&a), hash(&b)); assert_eq!(hash(&a), hash(&c)); + assert_eq!(hash(&a), hash(&d)); + + assert!(a.is_ascii()); + assert!(b.is_ascii()); + assert!(c.is_ascii()); + assert!(d.is_ascii()); + } + + #[test] + fn test_str_ascii() { + // https://github.com/seanmonstar/unicase/issues/76 + + let a = UniCase::new("foobar"); + let b = UniCase::<&str>::from("foobar"); + let c = UniCase::::from(String::from("foobar")); + let d = UniCase::>::from(Cow::from("foobar")); + assert!(a.is_ascii()); assert!(b.is_ascii()); assert!(c.is_ascii()); + assert!(d.is_ascii()); } #[test]