#![cfg_attr(not(feature = "use_std"), no_std)]
#![cfg_attr(feature = "pattern", feature(pattern))]
#[cfg(not(feature = "use_std"))]
extern crate core as std;
use std::cmp;
use std::usize;
extern crate memchr;
mod tw;
#[cfg(all(feature="benchmarks", any(target_arch = "x86", target_arch = "x86_64")))]
pub mod pcmp;
#[cfg(all(not(feature="benchmarks"), any(target_arch = "x86", target_arch = "x86_64")))]
mod pcmp;
#[cfg(feature="benchmarks")]
pub mod bmh;
#[cfg(feature = "pattern")]
use std::str::pattern::{
Pattern,
Searcher,
ReverseSearcher,
SearchStep,
};
#[inline]
pub fn find_str(text: &str, pattern: &str) -> Option<usize> {
find_bytes(text.as_bytes(), pattern.as_bytes())
}
pub fn find_bytes(text: &[u8], pattern: &[u8]) -> Option<usize> {
if pattern.is_empty() {
Some(0)
} else if text.len() < pattern.len() {
return None;
} else if pattern.len() == 1 {
memchr::memchr(pattern[0], text)
} else {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
let compile_time_disable = option_env!("TWOWAY_TEST_DISABLE_PCMP")
.map(|s| !s.is_empty())
.unwrap_or(false);
if !compile_time_disable && pcmp::is_supported() {
return unsafe { pcmp::find_inner(text, pattern) };
}
}
let mut searcher = TwoWaySearcher::new(pattern, text.len());
let is_long = searcher.memory == usize::MAX;
if is_long {
searcher.next::<MatchOnly>(text, pattern, true).map(|t| t.0)
} else {
searcher.next::<MatchOnly>(text, pattern, false).map(|t| t.0)
}
}
}
#[inline]
pub fn rfind_str(text: &str, pattern: &str) -> Option<usize> {
rfind_bytes(text.as_bytes(), pattern.as_bytes())
}
pub fn rfind_bytes(text: &[u8], pattern: &[u8]) -> Option<usize> {
if pattern.is_empty() {
Some(text.len())
} else if pattern.len() == 1 {
memchr::memrchr(pattern[0], text)
} else {
let mut searcher = TwoWaySearcher::new(pattern, text.len());
let is_long = searcher.memory == usize::MAX;
if is_long {
searcher.next_back::<MatchOnly>(text, pattern, true).map(|t| t.0)
} else {
searcher.next_back::<MatchOnly>(text, pattern, false).map(|t| t.0)
}
}
}
#[doc(hidden)]
pub struct Str<'a>(pub &'a str);
#[cfg(feature = "pattern")]
impl<'a, 'b> Pattern<'a> for Str<'b> {
type Searcher = StrSearcher<'a, 'b>;
#[inline]
fn into_searcher(self, haystack: &'a str) -> StrSearcher<'a, 'b> {
StrSearcher::new(haystack, self.0)
}
#[inline]
fn is_prefix_of(self, haystack: &'a str) -> bool {
let self_ = self.0;
haystack.is_char_boundary(self_.len()) &&
self_ == &haystack[..self_.len()]
}
#[inline]
fn is_suffix_of(self, haystack: &'a str) -> bool {
let self_ = self.0;
self_.len() <= haystack.len() &&
haystack.is_char_boundary(haystack.len() - self_.len()) &&
self_ == &haystack[haystack.len() - self_.len()..]
}
}
#[derive(Clone, Debug)]
#[doc(hidden)]
pub struct StrSearcher<'a, 'b> {
haystack: &'a str,
needle: &'b str,
searcher: StrSearcherImpl,
}
#[derive(Clone, Debug)]
enum StrSearcherImpl {
Empty(EmptyNeedle),
TwoWay(TwoWaySearcher),
}
#[derive(Clone, Debug)]
struct EmptyNeedle {
position: usize,
end: usize,
is_match_fw: bool,
is_match_bw: bool,
}
impl<'a, 'b> StrSearcher<'a, 'b> {
pub fn new(haystack: &'a str, needle: &'b str) -> StrSearcher<'a, 'b> {
if needle.is_empty() {
StrSearcher {
haystack: haystack,
needle: needle,
searcher: StrSearcherImpl::Empty(EmptyNeedle {
position: 0,
end: haystack.len(),
is_match_fw: true,
is_match_bw: true,
}),
}
} else {
StrSearcher {
haystack: haystack,
needle: needle,
searcher: StrSearcherImpl::TwoWay(
TwoWaySearcher::new(needle.as_bytes(), haystack.len())
),
}
}
}
}
#[cfg(feature = "pattern")]
unsafe impl<'a, 'b> Searcher<'a> for StrSearcher<'a, 'b> {
fn haystack(&self) -> &'a str { self.haystack }
#[inline]
fn next(&mut self) -> SearchStep {
match self.searcher {
StrSearcherImpl::Empty(ref mut searcher) => {
let is_match = searcher.is_match_fw;
searcher.is_match_fw = !searcher.is_match_fw;
let pos = searcher.position;
match self.haystack[pos..].chars().next() {
_ if is_match => SearchStep::Match(pos, pos),
None => SearchStep::Done,
Some(ch) => {
searcher.position += ch.len_utf8();
SearchStep::Reject(pos, searcher.position)
}
}
}
StrSearcherImpl::TwoWay(ref mut searcher) => {
if searcher.position == self.haystack.len() {
return SearchStep::Done;
}
let is_long = searcher.memory == usize::MAX;
match searcher.next::<RejectAndMatch>(self.haystack.as_bytes(),
self.needle.as_bytes(),
is_long)
{
SearchStep::Reject(a, mut b) => {
while !self.haystack.is_char_boundary(b) {
b += 1;
}
searcher.position = cmp::max(b, searcher.position);
SearchStep::Reject(a, b)
}
otherwise => otherwise,
}
}
}
}
#[inline(always)]
fn next_match(&mut self) -> Option<(usize, usize)> {
match self.searcher {
StrSearcherImpl::Empty(..) => {
loop {
match self.next() {
SearchStep::Match(a, b) => return Some((a, b)),
SearchStep::Done => return None,
SearchStep::Reject(..) => { }
}
}
}
StrSearcherImpl::TwoWay(ref mut searcher) => {
let is_long = searcher.memory == usize::MAX;
if is_long {
searcher.next::<MatchOnly>(self.haystack.as_bytes(),
self.needle.as_bytes(),
true)
} else {
searcher.next::<MatchOnly>(self.haystack.as_bytes(),
self.needle.as_bytes(),
false)
}
}
}
}
}
#[cfg(feature = "pattern")]
unsafe impl<'a, 'b> ReverseSearcher<'a> for StrSearcher<'a, 'b> {
#[inline]
fn next_back(&mut self) -> SearchStep {
match self.searcher {
StrSearcherImpl::Empty(ref mut searcher) => {
let is_match = searcher.is_match_bw;
searcher.is_match_bw = !searcher.is_match_bw;
let end = searcher.end;
match self.haystack[..end].chars().next_back() {
_ if is_match => SearchStep::Match(end, end),
None => SearchStep::Done,
Some(ch) => {
searcher.end -= ch.len_utf8();
SearchStep::Reject(searcher.end, end)
}
}
}
StrSearcherImpl::TwoWay(ref mut searcher) => {
if searcher.end == 0 {
return SearchStep::Done;
}
let is_long = searcher.memory == usize::MAX;
match searcher.next_back::<RejectAndMatch>(self.haystack.as_bytes(),
self.needle.as_bytes(),
is_long)
{
SearchStep::Reject(mut a, b) => {
while !self.haystack.is_char_boundary(a) {
a -= 1;
}
searcher.end = cmp::min(a, searcher.end);
SearchStep::Reject(a, b)
}
otherwise => otherwise,
}
}
}
}
#[inline]
fn next_match_back(&mut self) -> Option<(usize, usize)> {
match self.searcher {
StrSearcherImpl::Empty(..) => {
loop {
match self.next_back() {
SearchStep::Match(a, b) => return Some((a, b)),
SearchStep::Done => return None,
SearchStep::Reject(..) => { }
}
}
}
StrSearcherImpl::TwoWay(ref mut searcher) => {
let is_long = searcher.memory == usize::MAX;
if is_long {
searcher.next_back::<MatchOnly>(self.haystack.as_bytes(),
self.needle.as_bytes(),
true)
} else {
searcher.next_back::<MatchOnly>(self.haystack.as_bytes(),
self.needle.as_bytes(),
false)
}
}
}
}
}
#[derive(Clone, Debug)]
#[doc(hidden)]
pub struct TwoWaySearcher {
crit_pos: usize,
crit_pos_back: usize,
period: usize,
byteset: u64,
position: usize,
end: usize,
memory: usize,
memory_back: usize,
}
impl TwoWaySearcher {
pub fn new(needle: &[u8], end: usize) -> TwoWaySearcher {
let (crit_pos, period) = TwoWaySearcher::crit_params(needle);
if &needle[..crit_pos] == &needle[period.. period + crit_pos] {
let crit_pos_back = needle.len() - cmp::max(
TwoWaySearcher::reverse_maximal_suffix(needle, period, false),
TwoWaySearcher::reverse_maximal_suffix(needle, period, true));
TwoWaySearcher {
crit_pos: crit_pos,
crit_pos_back: crit_pos_back,
period: period,
byteset: Self::byteset_create(&needle[..period]),
position: 0,
end: end,
memory: 0,
memory_back: needle.len(),
}
} else {
TwoWaySearcher {
crit_pos: crit_pos,
crit_pos_back: crit_pos,
period: cmp::max(crit_pos, needle.len() - crit_pos) + 1,
byteset: Self::byteset_create(needle),
position: 0,
end: end,
memory: usize::MAX,
memory_back: usize::MAX,
}
}
}
#[inline(always)]
fn crit_params(needle: &[u8]) -> (usize, usize) {
let (crit_pos_false, period_false) = TwoWaySearcher::maximal_suffix(needle, false);
let (crit_pos_true, period_true) = TwoWaySearcher::maximal_suffix(needle, true);
if crit_pos_false > crit_pos_true {
(crit_pos_false, period_false)
} else {
(crit_pos_true, period_true)
}
}
#[inline]
fn byteset_create(bytes: &[u8]) -> u64 {
bytes.iter().fold(0, |a, &b| (1 << (b & 0x3f)) | a)
}
#[inline(always)]
fn byteset_contains(&self, byte: u8) -> bool {
(self.byteset >> ((byte & 0x3f) as usize)) & 1 != 0
}
#[inline(always)]
fn next<S>(&mut self, haystack: &[u8], needle: &[u8], long_period: bool)
-> S::Output
where S: TwoWayStrategy
{
let old_pos = self.position;
let needle_last = needle.len() - 1;
'search: loop {
let tail_byte = match haystack.get(self.position + needle_last) {
Some(&b) => b,
None => {
self.position = haystack.len();
return S::rejecting(old_pos, self.position);
}
};
if S::use_early_reject() && old_pos != self.position {
return S::rejecting(old_pos, self.position);
}
if !self.byteset_contains(tail_byte) {
self.position += needle.len();
if !long_period {
self.memory = 0;
}
continue 'search;
}
let start = if long_period { self.crit_pos }
else { cmp::max(self.crit_pos, self.memory) };
for i in start..needle.len() {
if needle[i] != haystack[self.position + i] {
self.position += i - self.crit_pos + 1;
if !long_period {
self.memory = 0;
}
continue 'search;
}
}
let start = if long_period { 0 } else { self.memory };
for i in (start..self.crit_pos).rev() {
if needle[i] != haystack[self.position + i] {
self.position += self.period;
if !long_period {
self.memory = needle.len() - self.period;
}
continue 'search;
}
}
let match_pos = self.position;
self.position += needle.len();
if !long_period {
self.memory = 0;
}
return S::matching(match_pos, match_pos + needle.len());
}
}
#[inline]
fn next_back<S>(&mut self, haystack: &[u8], needle: &[u8], long_period: bool)
-> S::Output
where S: TwoWayStrategy
{
let old_end = self.end;
'search: loop {
let front_byte = match haystack.get(self.end.wrapping_sub(needle.len())) {
Some(&b) => b,
None => {
self.end = 0;
return S::rejecting(0, old_end);
}
};
if S::use_early_reject() && old_end != self.end {
return S::rejecting(self.end, old_end);
}
if !self.byteset_contains(front_byte) {
self.end -= needle.len();
if !long_period {
self.memory_back = needle.len();
}
continue 'search;
}
let crit = if long_period { self.crit_pos_back }
else { cmp::min(self.crit_pos_back, self.memory_back) };
for i in (0..crit).rev() {
if needle[i] != haystack[self.end - needle.len() + i] {
self.end -= self.crit_pos_back - i;
if !long_period {
self.memory_back = needle.len();
}
continue 'search;
}
}
let needle_end = if long_period { needle.len() }
else { self.memory_back };
for i in self.crit_pos_back..needle_end {
if needle[i] != haystack[self.end - needle.len() + i] {
self.end -= self.period;
if !long_period {
self.memory_back = self.period;
}
continue 'search;
}
}
let match_pos = self.end - needle.len();
self.end -= needle.len();
if !long_period {
self.memory_back = needle.len();
}
return S::matching(match_pos, match_pos + needle.len());
}
}
#[inline]
pub fn maximal_suffix(arr: &[u8], order_greater: bool) -> (usize, usize) {
let mut left = 0;
let mut right = 1;
let mut offset = 0;
let mut period = 1;
while let Some(&a) = arr.get(right + offset) {
let b = arr[left + offset];
if (a < b && !order_greater) || (a > b && order_greater) {
right += offset + 1;
offset = 0;
period = right - left;
} else if a == b {
if offset + 1 == period {
right += offset + 1;
offset = 0;
} else {
offset += 1;
}
} else {
left = right;
right += 1;
offset = 0;
period = 1;
}
}
(left, period)
}
pub fn reverse_maximal_suffix(arr: &[u8], known_period: usize,
order_greater: bool) -> usize
{
let mut left = 0;
let mut right = 1;
let mut offset = 0;
let mut period = 1;
let n = arr.len();
while right + offset < n {
let a = arr[n - (1 + right + offset)];
let b = arr[n - (1 + left + offset)];
if (a < b && !order_greater) || (a > b && order_greater) {
right += offset + 1;
offset = 0;
period = right - left;
} else if a == b {
if offset + 1 == period {
right += offset + 1;
offset = 0;
} else {
offset += 1;
}
} else {
left = right;
right += 1;
offset = 0;
period = 1;
}
if period == known_period {
break;
}
}
debug_assert!(period <= known_period);
left
}
}
trait TwoWayStrategy {
type Output;
fn use_early_reject() -> bool;
fn rejecting(usize, usize) -> Self::Output;
fn matching(usize, usize) -> Self::Output;
}
enum MatchOnly { }
impl TwoWayStrategy for MatchOnly {
type Output = Option<(usize, usize)>;
#[inline]
fn use_early_reject() -> bool { false }
#[inline]
fn rejecting(_a: usize, _b: usize) -> Self::Output { None }
#[inline]
fn matching(a: usize, b: usize) -> Self::Output { Some((a, b)) }
}
#[cfg(feature = "pattern")]
enum RejectAndMatch { }
#[cfg(feature = "pattern")]
impl TwoWayStrategy for RejectAndMatch {
type Output = SearchStep;
#[inline]
fn use_early_reject() -> bool { true }
#[inline]
fn rejecting(a: usize, b: usize) -> Self::Output { SearchStep::Reject(a, b) }
#[inline]
fn matching(a: usize, b: usize) -> Self::Output { SearchStep::Match(a, b) }
}
#[cfg(feature = "pattern")]
#[cfg(test)]
impl<'a, 'b> StrSearcher<'a, 'b> {
fn twoway(&self) -> &TwoWaySearcher {
match self.searcher {
StrSearcherImpl::TwoWay(ref inner) => inner,
_ => panic!("Not a TwoWaySearcher"),
}
}
}
#[cfg(feature = "pattern")]
#[test]
fn test_basic() {
let t = StrSearcher::new("", "aab");
println!("{:?}", t);
let t = StrSearcher::new("", "abaaaba");
println!("{:?}", t);
let mut t = StrSearcher::new("GCATCGCAGAGAGTATACAGTACG", "GCAGAGAG");
println!("{:?}", t);
loop {
match t.next() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
let mut t = StrSearcher::new("GCATCGCAGAGAGTATACAGTACG", "GCAGAGAG");
println!("{:?}", t);
loop {
match t.next_back() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
let mut t = StrSearcher::new("banana", "nana");
println!("{:?}", t);
loop {
match t.next() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
}
#[cfg(feature = "pattern")]
#[cfg(test)]
fn contains(hay: &str, n: &str) -> bool {
let mut tws = StrSearcher::new(hay, n);
loop {
match tws.next() {
SearchStep::Done => return false,
SearchStep::Match(..) => return true,
_ => { }
}
}
}
#[cfg(feature = "pattern")]
#[cfg(test)]
fn contains_rev(hay: &str, n: &str) -> bool {
let mut tws = StrSearcher::new(hay, n);
loop {
match tws.next_back() {
SearchStep::Done => return false,
SearchStep::Match(..) => return true,
rej => { println!("{:?}", rej); }
}
}
}
#[cfg(feature = "pattern")]
#[test]
fn test_contains() {
let h = "";
let n = "";
assert!(contains(h, n));
assert!(contains_rev(h, n));
let h = "BDC\0\0\0";
let n = "BDC\u{0}";
assert!(contains(h, n));
assert!(contains_rev(h, n));
let h = "ADA\0";
let n = "ADA";
assert!(contains(h, n));
assert!(contains_rev(h, n));
let h = "\u{0}\u{0}\u{0}\u{0}";
let n = "\u{0}";
assert!(contains(h, n));
assert!(contains_rev(h, n));
}
#[cfg(feature = "pattern")]
#[test]
fn test_rev_2() {
let h = "BDC\0\0\0";
let n = "BDC\u{0}";
let mut t = StrSearcher::new(h, n);
println!("{:?}", t);
println!("{:?}", h.contains(&n));
loop {
match t.next_back() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
let h = "aabaabx";
let n = "aabaab";
let mut t = StrSearcher::new(h, n);
println!("{:?}", t);
assert_eq!(t.twoway().crit_pos, 2);
assert_eq!(t.twoway().crit_pos_back, 5);
loop {
match t.next_back() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
let h = "abababac";
let n = "ababab";
let mut t = StrSearcher::new(h, n);
println!("{:?}", t);
assert_eq!(t.twoway().crit_pos, 1);
assert_eq!(t.twoway().crit_pos_back, 5);
loop {
match t.next_back() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
let h = "abababac";
let n = "abab";
let mut t = StrSearcher::new(h, n);
println!("{:?}", t);
loop {
match t.next_back() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
let h = "baabbbaabc";
let n = "baabb";
let t = StrSearcher::new(h, n);
println!("{:?}", t);
assert_eq!(t.twoway().crit_pos, 3);
assert_eq!(t.twoway().crit_pos_back, 3);
let h = "aabaaaabaabxx";
let n = "aabaaaabaa";
let mut t = StrSearcher::new(h, n);
println!("{:?}", t);
loop {
match t.next_back() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
let h = "babbabax";
let n = "babbab";
let mut t = StrSearcher::new(h, n);
println!("{:?}", t);
assert_eq!(t.twoway().crit_pos, 2);
assert_eq!(t.twoway().crit_pos_back, 4);
loop {
match t.next_back() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
let h = "xacbaabcax";
let n = "abca";
let mut t = StrSearcher::new(h, n);
assert_eq!(t.next_match_back(), Some((5, 9)));
let h = "xacbaacbxxcba";
let m = "acba";
let mut s = StrSearcher::new(h, m);
assert_eq!(s.next_match_back(), Some((1, 5)));
assert_eq!(s.twoway().crit_pos, 1);
assert_eq!(s.twoway().crit_pos_back, 2);
}
#[cfg(feature = "pattern")]
#[test]
fn test_rev_unicode() {
let h = "ααααααβ";
let n = "αβ";
let mut t = StrSearcher::new(h, n);
println!("{:?}", t);
loop {
match t.next() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
let mut t = StrSearcher::new(h, n);
loop {
match t.next_back() {
SearchStep::Done => break,
m => println!("{:?}", m),
}
}
}
#[test]
fn maximal_suffix() {
assert_eq!((2, 1), TwoWaySearcher::maximal_suffix(b"aab", false));
assert_eq!((0, 3), TwoWaySearcher::maximal_suffix(b"aab", true));
assert_eq!((0, 3), TwoWaySearcher::maximal_suffix(b"aabaa", true));
assert_eq!((2, 3), TwoWaySearcher::maximal_suffix(b"aabaa", false));
assert_eq!((0, 7), TwoWaySearcher::maximal_suffix(b"gcagagag", false));
assert_eq!((2, 2), TwoWaySearcher::maximal_suffix(b"gcagagag", true));
assert_eq!((2, 2), TwoWaySearcher::maximal_suffix(b"banana", false));
assert_eq!((1, 2), TwoWaySearcher::maximal_suffix(b"banana", true));
assert_eq!((0, 6), TwoWaySearcher::maximal_suffix(b"zanana", false));
assert_eq!((1, 2), TwoWaySearcher::maximal_suffix(b"zanana", true));
}
#[test]
fn maximal_suffix_verbose() {
fn maximal_suffix(arr: &[u8], order_greater: bool) -> (usize, usize) {
let mut left: usize = 0;
let mut right = 1;
let mut offset = 0;
let mut period = 1;
macro_rules! asstr {
($e:expr) => (::std::str::from_utf8($e).unwrap())
}
while let Some(&a) = arr.get(right + offset) {
debug_assert!(left <= right);
let b = unsafe { *arr.get_unchecked(left + offset) };
println!("str={}, l={}, r={}, offset={}, p={}", asstr!(arr), left, right, offset, period);
if (a < b && !order_greater) || (a > b && order_greater) {
right += offset + 1;
offset = 0;
period = right - left;
} else if a == b {
if offset + 1 == period {
right += offset + 1;
offset = 0;
} else {
offset += 1;
}
} else {
left = right;
right += 1;
offset = 0;
period = 1;
}
}
println!("str={}, l={}, r={}, offset={}, p={} ==END==", asstr!(arr), left, right, offset, period);
(left, period)
}
fn reverse_maximal_suffix(arr: &[u8], known_period: usize, order_greater: bool) -> usize {
let n = arr.len();
let mut left: usize = 0;
let mut right = 1;
let mut offset = 0;
let mut period = 1;
macro_rules! asstr {
($e:expr) => (::std::str::from_utf8($e).unwrap())
}
while right + offset < n {
debug_assert!(left <= right);
let a = unsafe { *arr.get_unchecked(n - (1 + right + offset)) };
let b = unsafe { *arr.get_unchecked(n - (1 + left + offset)) };
println!("str={}, l={}, r={}, offset={}, p={}", asstr!(arr), left, right, offset, period);
if (a < b && !order_greater) || (a > b && order_greater) {
right += offset + 1;
offset = 0;
period = right - left;
if period == known_period {
break;
}
} else if a == b {
if offset + 1 == period {
right += offset + 1;
offset = 0;
} else {
offset += 1;
}
} else {
left = right;
right += 1;
offset = 0;
period = 1;
}
}
println!("str={}, l={}, r={}, offset={}, p={} ==END==", asstr!(arr), left, right, offset, period);
debug_assert!(period == known_period);
left
}
assert_eq!((2, 2), maximal_suffix(b"banana", false));
assert_eq!((1, 2), maximal_suffix(b"banana", true));
assert_eq!((0, 7), maximal_suffix(b"gcagagag", false));
assert_eq!((2, 2), maximal_suffix(b"gcagagag", true));
assert_eq!((2, 1), maximal_suffix(b"bac", false));
assert_eq!((1, 2), maximal_suffix(b"bac", true));
assert_eq!((0, 9), maximal_suffix(b"baaaaaaaa", false));
assert_eq!((1, 1), maximal_suffix(b"baaaaaaaa", true));
assert_eq!((2, 3), maximal_suffix(b"babbabbab", false));
assert_eq!((1, 3), maximal_suffix(b"babbabbab", true));
assert_eq!(2, reverse_maximal_suffix(b"babbabbab", 3, false));
assert_eq!(1, reverse_maximal_suffix(b"babbabbab", 3, true));
assert_eq!((0, 2), maximal_suffix(b"bababa", false));
assert_eq!((1, 2), maximal_suffix(b"bababa", true));
assert_eq!(1, reverse_maximal_suffix(b"bababa", 2, false));
assert_eq!(0, reverse_maximal_suffix(b"bababa", 2, true));
assert_eq!((2, 2), maximal_suffix(b"abca", false));
assert_eq!((0, 3), maximal_suffix(b"abca", true));
assert_eq!((3, 2), maximal_suffix(b"abcda", false));
assert_eq!((0, 4), maximal_suffix(b"abcda", true));
assert_eq!((1, 3), maximal_suffix(b"acba", false));
assert_eq!((0, 3), maximal_suffix(b"acba", true));
}
#[cfg(feature = "pattern")]
#[test]
fn test_find_rfind() {
fn find(hay: &str, pat: &str) -> Option<usize> {
let mut t = pat.into_searcher(hay);
t.next_match().map(|(x, _)| x)
}
fn rfind(hay: &str, pat: &str) -> Option<usize> {
let mut t = pat.into_searcher(hay);
t.next_match_back().map(|(x, _)| x)
}
let string = "Việt Namacbaabcaabaaba";
for (i, ci) in string.char_indices() {
let ip = i + ci.len_utf8();
for j in string[ip..].char_indices()
.map(|(i, _)| i)
.chain(Some(string.len() - ip))
{
let pat = &string[i..ip + j];
assert!(match find(string, pat) {
None => false,
Some(x) => x <= i,
});
assert!(match rfind(string, pat) {
None => false,
Some(x) => x >= i,
});
}
}
}