Skip to content

Commit

Permalink
Check for ASCII in from impl as well
Browse files Browse the repository at this point in the history
This addresses seanmonstar#76
which reports that there is an inconsistency between
`UniCase::new()` and `UniCase::from()`
for ASCII strings where the latter doesn't check for encoding.
  • Loading branch information
samadDotDev committed Jan 8, 2025
1 parent c42d962 commit ff6931c
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,11 @@ macro_rules! into_impl {

impl<S: AsRef<str>> From<S> for UniCase<S> {
fn from(s: S) -> Self {
UniCase::unicode(s)
if s.as_ref().is_ascii() {
UniCase::ascii(s)
} else {
UniCase::unicode(s)
}
}
}

Expand Down Expand Up @@ -308,6 +312,7 @@ impl<S: FromStr + AsRef<str>> FromStr for UniCase<S> {
#[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};
Expand All @@ -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::<String>::from(String::from("foobar"));
let d = UniCase::<Cow<str>>::from(Cow::from("foobar"));

assert!(a.is_ascii());
assert!(b.is_ascii());
assert!(c.is_ascii());
assert!(d.is_ascii());
}

#[test]
Expand Down

0 comments on commit ff6931c

Please sign in to comment.