MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / find

Function find

rust/stringzilla.rs:1337–1355  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1335/// An `Option<usize>` representing the starting index of the first occurrence of `needle`
1336/// within `haystack` if found, otherwise `None`.
1337pub fn find<H, N>(haystack: H, needle: N) -> Option<usize>
1338where
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

Callers 8

try_replace_allFunction · 0.85
findMethod · 0.85
sz_findMethod · 0.85
findMethod · 0.85
operator()Method · 0.85
containsMethod · 0.85
findMethod · 0.85
partition_Method · 0.85

Calls 3

as_ptrMethod · 0.80
lenMethod · 0.80
sz_findFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…