mirror of https://github.com/rwf2/Rocket.git
Fix collisions for strings with different lengths.
Previously, a collision check for strings with different lengths would succeed if one string was both a prefix and a suffix of the other. The root cause of the bug was a failure to check whether string equality terminated early due to a matching prefix. Fixes #574.
This commit is contained in:
parent
5c9aa8e2c9
commit
6f505afb33
|
@ -12,43 +12,36 @@ pub trait Collider<T: ?Sized = Self> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn index_match_until(break_c: char,
|
fn do_match_iter_until<A, B>(break_c: u8, mut a: A, mut b: B) -> bool
|
||||||
a: &str,
|
where A: Iterator<Item = u8>, B: Iterator<Item = u8>
|
||||||
b: &str,
|
{
|
||||||
dir: bool) -> Option<(isize, isize)> {
|
loop {
|
||||||
let (a_len, b_len) = (a.len() as isize, b.len() as isize);
|
match (a.next(), b.next()) {
|
||||||
let (mut i, mut j, delta) = if dir {
|
(None, Some(_)) => return false,
|
||||||
(0, 0, 1)
|
(Some(_), None) => return false,
|
||||||
} else {
|
(None, None) => return true,
|
||||||
(a_len - 1, b_len - 1, -1)
|
(Some(c1), Some(c2)) if c1 == break_c || c2 == break_c => return true,
|
||||||
};
|
(Some(c1), Some(c2)) if c1 != c2 => return false,
|
||||||
|
(Some(_), Some(_)) => continue
|
||||||
let break_b = break_c as u8;
|
|
||||||
while i >= 0 && j >= 0 && i < a_len && j < b_len {
|
|
||||||
let (c1, c2) = (a.as_bytes()[i as usize], b.as_bytes()[j as usize]);
|
|
||||||
if c1 == break_b || c2 == break_b {
|
|
||||||
break;
|
|
||||||
} else if c1 != c2 {
|
|
||||||
return None;
|
|
||||||
} else {
|
|
||||||
i += delta;
|
|
||||||
j += delta;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some((i, j))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn do_match_until(break_c: char, a: &str, b: &str, dir: bool) -> bool {
|
fn do_match_until(break_c: u8, a: &str, b: &str, dir: bool) -> bool {
|
||||||
index_match_until(break_c, a, b, dir).is_some()
|
let (a_iter, b_iter) = (a.as_bytes().iter().cloned(), b.as_bytes().iter().cloned());
|
||||||
|
if dir {
|
||||||
|
do_match_iter_until(break_c, a_iter, b_iter)
|
||||||
|
} else {
|
||||||
|
do_match_iter_until(break_c, a_iter.rev(), b_iter.rev())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Collider<str> for &'a str {
|
impl<'a> Collider<str> for &'a str {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn collides_with(&self, other: &str) -> bool {
|
fn collides_with(&self, other: &str) -> bool {
|
||||||
let (a, b) = (self, other);
|
let (a, b) = (self, other);
|
||||||
do_match_until('<', a, b, true) && do_match_until('>', a, b, false)
|
do_match_until(b'<', a, b, true) && do_match_until(b'>', a, b, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,7 +194,7 @@ mod tests {
|
||||||
assert!(unranked_collide("/<name>bob", "/<name>b"));
|
assert!(unranked_collide("/<name>bob", "/<name>b"));
|
||||||
assert!(unranked_collide("/a<b>c", "/abc"));
|
assert!(unranked_collide("/a<b>c", "/abc"));
|
||||||
assert!(unranked_collide("/a<b>c", "/azooc"));
|
assert!(unranked_collide("/a<b>c", "/azooc"));
|
||||||
assert!(unranked_collide("/a<b>", "/a"));
|
assert!(unranked_collide("/a<b>", "/ab"));
|
||||||
assert!(unranked_collide("/<b>", "/a"));
|
assert!(unranked_collide("/<b>", "/a"));
|
||||||
assert!(unranked_collide("/<a>/<b>", "/a/b<c>"));
|
assert!(unranked_collide("/<a>/<b>", "/a/b<c>"));
|
||||||
assert!(unranked_collide("/<a>/bc<b>", "/a/b<c>"));
|
assert!(unranked_collide("/<a>/bc<b>", "/a/b<c>"));
|
||||||
|
@ -226,6 +219,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn non_collisions() {
|
fn non_collisions() {
|
||||||
|
assert!(!unranked_collide("/<a>", "/"));
|
||||||
assert!(!unranked_collide("/a", "/b"));
|
assert!(!unranked_collide("/a", "/b"));
|
||||||
assert!(!unranked_collide("/a/b", "/a"));
|
assert!(!unranked_collide("/a/b", "/a"));
|
||||||
assert!(!unranked_collide("/a/b", "/a/c"));
|
assert!(!unranked_collide("/a/b", "/a/c"));
|
||||||
|
@ -242,6 +236,10 @@ mod tests {
|
||||||
assert!(!unranked_collide("/hi/<a..>", "/hi"));
|
assert!(!unranked_collide("/hi/<a..>", "/hi"));
|
||||||
assert!(!unranked_collide("/hi/<a..>", "/hi/"));
|
assert!(!unranked_collide("/hi/<a..>", "/hi/"));
|
||||||
assert!(!unranked_collide("/<a..>", "//////"));
|
assert!(!unranked_collide("/<a..>", "//////"));
|
||||||
|
assert!(!unranked_collide("/t", "/test"));
|
||||||
|
assert!(!unranked_collide("/a", "/aa"));
|
||||||
|
assert!(!unranked_collide("/a", "/aaa"));
|
||||||
|
assert!(!unranked_collide("/", "/a"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -304,6 +302,10 @@ mod tests {
|
||||||
assert!(!s_s_collide("/<a..>", "/"));
|
assert!(!s_s_collide("/<a..>", "/"));
|
||||||
assert!(!s_s_collide("/hi/<a..>", "/hi/"));
|
assert!(!s_s_collide("/hi/<a..>", "/hi/"));
|
||||||
assert!(!s_s_collide("/a/hi/<a..>", "/a/hi/"));
|
assert!(!s_s_collide("/a/hi/<a..>", "/a/hi/"));
|
||||||
|
assert!(!s_s_collide("/t", "/test"));
|
||||||
|
assert!(!s_s_collide("/a", "/aa"));
|
||||||
|
assert!(!s_s_collide("/a", "/aaa"));
|
||||||
|
assert!(!s_s_collide("/", "/a"));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mt_mt_collide(mt1: &str, mt2: &str) -> bool {
|
fn mt_mt_collide(mt1: &str, mt2: &str) -> bool {
|
||||||
|
|
Loading…
Reference in New Issue