Skip to content

Commit

Permalink
Fix Unicode::eq to not equal when one side is a substring of the other (
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar authored Nov 12, 2019
1 parent 4788cba commit c14856b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit c14856b

Please sign in to comment.