Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Unicode::eq to not equal when one side is a substring of the other #39

Merged
merged 1 commit into from Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/lib.rs
Expand Up @@ -360,22 +360,36 @@ mod tests {
let c = UniCase::ascii("FoObAr");

assert_eq!(a, b);
assert_eq!(b, a);
assert_eq!(a, c);
assert_eq!(c, a);
assert_eq!(hash(&a), hash(&b));
assert_eq!(hash(&a), hash(&c));
assert!(a.is_ascii());
assert!(b.is_ascii());
assert!(c.is_ascii());
}


#[test]
fn test_eq_unicode() {
let a = UniCase::new("στιγμας");
let b = UniCase::new("στιγμασ");
assert_eq!(a, b);
assert_eq!(b, a);
assert_eq!(hash(&a), hash(&b));
}

#[test]
fn test_eq_unicode_left_is_substring() {
// https://github.com/seanmonstar/unicase/issues/38
let a = UniCase::unicode("foo");
let b = UniCase::unicode("foobar");

assert!(a != b);
assert!(b != a);
}

#[cfg(feature = "nightly")]
#[bench]
fn bench_unicase_ascii(b: &mut ::test::Bencher) {
Expand Down
22 changes: 19 additions & 3 deletions src/unicode/mod.rs
Expand Up @@ -11,9 +11,25 @@ pub struct Unicode<S>(pub S);
impl<S1: AsRef<str>, S2: AsRef<str>> PartialEq<Unicode<S2>> for Unicode<S1> {
#[inline]
fn eq(&self, other: &Unicode<S2>) -> bool {
self.0.as_ref().chars().flat_map(lookup)
.zip(other.0.as_ref().chars().flat_map(lookup))
.all(|(a, b)| a == b)
let mut left = self.0.as_ref().chars().flat_map(lookup);
let mut right = other.0.as_ref().chars().flat_map(lookup);

// inline Iterator::eq since not added until Rust 1.5
loop {
let x = match left.next() {
None => return right.next().is_none(),
Some(val) => val,
};

let y = match right.next() {
None => return false,
Some(val) => val,
};

if x != y {
return false;
}
}
}
}

Expand Down