pub trait fastcontains {
fn fast_contains(&self, needle: &str) -> bool;
}
impl fastcontains for str {
fn fast_contains(&self, needle: &str) -> bool {
let haystack_ptr = self.as_ptr();
let needle_ptr = needle.as_ptr();
let haystack_len = self.len();
let needle_len = needle.len();
if needle_len <= haystack_len {
let max_start = haystack_len - needle_len;
let mut i = 0;
while i <= max_start {
let mut matched = true;
let mut j = 0;
unsafe {
while j < needle_len {
if *haystack_ptr.add(i + j) != *needle_ptr.add(j) {
matched = false;
break;
}
j += 1;
}
}
if matched {
return true;
}
i += 1;
}
false
} else {
false
}
}
}
impl fastcontains for String {
fn fast_contains(&self, needle: &str) -> bool {
self.as_str().fast_contains(needle)
}
}