Locates the first matching substring within `haystack` that equals `needle`. This function is similar to the `memmem()` function in LibC, but, unlike `strstr()`, it requires the length of both haystack and needle to be known beforehand. # Arguments `haystack`: The byte slice to search. `needle`: The byte slice to find within the haystack. # Returns An `Option ` representing the starting
(haystack: H, needle: N)
| 1335 | /// An `Option<usize>` representing the starting index of the first occurrence of `needle` |
| 1336 | /// within `haystack` if found, otherwise `None`. |
| 1337 | pub fn find<H, N>(haystack: H, needle: N) -> Option<usize> |
| 1338 | where |
| 1339 | H: AsRef<[u8]>, |
| 1340 | N: AsRef<[u8]>, |
| 1341 | { |
| 1342 | let haystack_ref = haystack.as_ref(); |
| 1343 | let needle_ref = needle.as_ref(); |
| 1344 | let haystack_pointer = haystack_ref.as_ptr() as _; |
| 1345 | let haystack_length = haystack_ref.len(); |
| 1346 | let needle_pointer = needle_ref.as_ptr() as _; |
| 1347 | let needle_length = needle_ref.len(); |
| 1348 | let result = unsafe { sz_find(haystack_pointer, haystack_length, needle_pointer, needle_length) }; |
| 1349 | |
| 1350 | if result.is_null() { |
| 1351 | None |
| 1352 | } else { |
| 1353 | Some(unsafe { result.offset_from(haystack_pointer) }.try_into().unwrap()) |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | /// Locates the last matching substring within `haystack` that equals `needle`. |
| 1358 | /// This function is useful for finding the most recent or last occurrence of a pattern |
no test coverage detected
searching dependent graphs…