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

Add UniCase::contains() #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,23 @@ impl<S> UniCase<S> {
}
}

impl<S1: AsRef<str>> UniCase<S1> {
/// Returns true if the given pattern matches a sub-slice of this string slice.
///
/// Returns false if it does not.
#[inline]
pub fn contains<S2: AsRef<str>>(&self, pat: &UniCase<S2>) -> bool {
match (&self.0, &pat.0) {
(&Encoding::Ascii(ref x), &Encoding::Ascii(ref p)) => x.as_ref().contains(p.as_ref()),
(&Encoding::Unicode(ref x), &Encoding::Unicode(ref p)) => x.contains(p),
(&Encoding::Ascii(ref x), &Encoding::Unicode(ref p)) => Unicode(x.as_ref()).contains(p),
(&Encoding::Unicode(ref x), &Encoding::Ascii(ref p)) => {
x.contains(&Unicode(p.as_ref()))
}
}
}
}

impl<S> Deref for UniCase<S> {
type Target = S;
#[inline]
Expand Down
58 changes: 58 additions & 0 deletions src/unicode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,49 @@ impl<S1: AsRef<str>, S2: AsRef<str>> PartialEq<Unicode<S2>> for Unicode<S1> {
}
}

impl<S1: AsRef<str>> Unicode<S1> {
/// Returns true if the given pattern matches a sub-slice of this string slice.
///
/// Returns false if it does not.
#[inline]
pub fn contains<S2: AsRef<str>>(&self, pat: &Unicode<S2>) -> bool {
let mut left = self.0.as_ref().chars().flat_map(lookup);
let mut pat = pat.0.as_ref().chars().flat_map(lookup);

match pat.next() {
Some(p0) => 'out: loop {
match left.next() {
Some(e) if e == p0 => {
let mut left = left.clone();
let mut pat = pat.clone();

loop {
let p = match pat.next() {
None => break 'out true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these breaks could just be returns right? I think that would be simpler

Copy link
Contributor

@CryZe CryZe Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, depending on if there's a minimal supported rust version, you could even write

let Some(p) = pat.next() else { return true };

(The build.rs seems to imply there is, so this probably is too fancy).

Some(p) => p,
};

let e = match left.next() {
None => break 'out false,
Some(e) => e,
};

if e != p {
break;
}
}
}
Some(_) => {
continue;
}
None => break false,
}
},
None => true,
}
}
}

impl<S: AsRef<str>> Eq for Unicode<S> {}

#[cfg(__unicase__iter_cmp)]
Expand Down Expand Up @@ -198,4 +241,19 @@ mod tests {
b.bytes = "στιγμας".len() as u64;
b.iter(|| eq!("στιγμας", "στιγμασ"));
}

#[test]
fn test_contains() {
assert!(Unicode("A").contains(&Unicode("a")));
assert!(Unicode("AA").contains(&Unicode("a")));
assert!(Unicode("AAA").contains(&Unicode("aa")));
assert!(Unicode("BA").contains(&Unicode("a")));
assert!(Unicode("AB").contains(&Unicode("a")));
assert!(Unicode("BABABB").contains(&Unicode("babb")));

assert!(!Unicode("B").contains(&Unicode("a")));
assert!(!Unicode("BA").contains(&Unicode("aa")));
assert!(!Unicode("BA").contains(&Unicode("aa")));
assert!(!Unicode("BABABA").contains(&Unicode("babb")));
}
}