util: remove code clone

Not in the right module yet but it's a start.
This commit is contained in:
Johannes Altmanninger
2026-01-15 18:17:34 +01:00
parent 97e0eda477
commit c84c006f42
3 changed files with 22 additions and 29 deletions

View File

@@ -60,11 +60,27 @@ pub const fn char_offset(base: char, offset: u32) -> char {
}
}
pub fn subslice_position<T: Eq>(a: &[T], b: &[T]) -> Option<usize> {
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<T: PartialEq>(
haystack: impl AsRef<[T]>,
needle: impl AsRef<[T]>,
) -> Option<usize> {
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.