Allow finding for empty strings with wstr::find

I hit this temporarily in a test; it seems reasonable to allow this.
std::str does too.
This commit is contained in:
Johannes Altmanninger 2024-01-19 06:08:18 +01:00
parent f356e2d82f
commit 2059e5a171
2 changed files with 5 additions and 6 deletions

View file

@ -1632,10 +1632,13 @@ pub fn restore_term_foreground_process_group_for_exit() {
#[allow(unused)]
// This function is unused in some configurations/on some platforms
fn slice_contains_slice<T: Eq>(a: &[T], b: &[T]) -> bool {
a.windows(b.len()).any(|aw| aw == b)
subslice_position(a, b).is_some()
}
pub fn subslice_position<T: Eq>(a: &[T], b: &[T]) -> Option<usize> {
if b.is_empty() {
return Some(0);
}
a.windows(b.len()).position(|aw| aw == b)
}

View file

@ -245,11 +245,7 @@ pub trait WExt {
/// Returns the index of the first match against the provided substring or `None`.
fn find(&self, search: impl AsRef<[char]>) -> Option<usize> {
fn inner(lhs: &[char], rhs: &[char]) -> Option<usize> {
lhs.windows(rhs.len()).position(|window| window == rhs)
}
inner(self.as_char_slice(), search.as_ref())
subslice_position(self.as_char_slice(), search.as_ref())
}
/// Replaces all matches of a pattern with another string.