diff --git a/crates/util/src/lib.rs b/crates/util/src/lib.rs index 952693a17..c8f30b14e 100644 --- a/crates/util/src/lib.rs +++ b/crates/util/src/lib.rs @@ -234,29 +234,6 @@ fn wcsfilecmp_leading_digits(a: &wstr, b: &wstr) -> (Ordering, usize, usize) { (ret, ai, bi) } -/// Finds `needle` in a `haystack` and returns the index of the first matching element, if any. -/// -/// # Examples -/// -/// ``` -/// use fish_util::find_subslice; -/// let haystack = b"ABC ABCDAB ABCDABCDABDE"; -/// -/// assert_eq!(find_subslice(b"ABCDABD", haystack), Some(15)); -/// assert_eq!(find_subslice(b"ABCDE", haystack), None); -/// ``` -pub fn find_subslice( - needle: impl AsRef<[T]>, - haystack: impl AsRef<[T]>, -) -> Option { - let needle = needle.as_ref(); - if needle.is_empty() { - return Some(0); - } - let haystack = haystack.as_ref(); - haystack.windows(needle.len()).position(|w| w == needle) -} - #[cfg(test)] mod tests { use super::wcsfilecmp; diff --git a/crates/widestring/src/lib.rs b/crates/widestring/src/lib.rs index 629705833..8f010c13a 100644 --- a/crates/widestring/src/lib.rs +++ b/crates/widestring/src/lib.rs @@ -60,11 +60,27 @@ pub const fn char_offset(base: char, offset: u32) -> char { } } -pub fn subslice_position(a: &[T], b: &[T]) -> Option { - if b.is_empty() { +/// Finds `needle` in a `haystack` and returns the index of the first matching element, if any. +/// +/// # Examples +/// +/// ``` +/// use fish_widestring::subslice_position; +/// let haystack = b"ABC ABCDAB ABCDABCDABDE"; +/// +/// assert_eq!(subslice_position(haystack, b"ABCDABD"), Some(15)); +/// assert_eq!(subslice_position(haystack, b"ABCDE"), None); +/// ``` +pub fn subslice_position( + haystack: impl AsRef<[T]>, + needle: impl AsRef<[T]>, +) -> Option { + let needle = needle.as_ref(); + if needle.is_empty() { return Some(0); } - a.windows(b.len()).position(|aw| aw == b) + let haystack = haystack.as_ref(); + haystack.windows(needle.len()).position(|w| w == needle) } /// Helpers to convert things to widestring. diff --git a/src/history/history.rs b/src/history/history.rs index 05e700a99..957f926bb 100644 --- a/src/history/history.rs +++ b/src/history/history.rs @@ -24,6 +24,7 @@ threads::ThreadPool, }; use fish_wcstringutil::{subsequence_in_string, trim}; +use fish_widestring::subslice_position; use std::{ borrow::Cow, collections::{BTreeMap, HashMap, HashSet}, @@ -64,7 +65,6 @@ wildcard::{ANY_STRING, wildcard_match}, wutil::{FileId, INVALID_FILE_ID, file_id_for_file, wrealpath, wstat, wunlink}, }; -use fish_util::find_subslice; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum SearchType { @@ -218,7 +218,7 @@ pub fn matches_search(&self, term: &wstr, typ: SearchType, case_sensitive: bool) match typ { SearchType::Exact => term == *content_to_match, SearchType::Contains => { - find_subslice(term.as_slice(), content_to_match.as_slice()).is_some() + subslice_position(content_to_match.as_slice(), term.as_slice()).is_some() } SearchType::Prefix => content_to_match.as_slice().starts_with(term.as_slice()), SearchType::LinePrefix => content_to_match @@ -1174,7 +1174,7 @@ fn should_import_bash_history_line(line: &wstr) -> bool { // Skip lines with [[...]] and ((...)) since we don't handle those constructs. // "<<" here is a proxy for heredocs (and herestrings). for seq in [L!("[["), L!("]]"), L!("(("), L!("))"), L!("<<")] { - if find_subslice(seq, line.as_char_slice()).is_some() { + if subslice_position(line.as_char_slice(), seq).is_some() { return false; } }