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

Function try_replace_all

rust/stringzilla.rs:1662–1682  ·  view source on GitHub ↗

Tries to replace all non-overlapping occurrences of `needle` inside `buffer` in place. The algorithm mirrors the C++ `replace_all` logic: - equal-length replacements simply overwrite matches, - shorter replacements compact forward without allocating, - longer replacements count matches once, resize once, and rewrite from the back. Returns the number of replacements performed.

(buffer: &mut Vec<u8>, needle: &[u8], replacement: &[u8])

Source from the content-addressed store, hash-verified

1660///
1661/// Returns the number of replacements performed.
1662pub fn try_replace_all(buffer: &mut Vec<u8>, needle: &[u8], replacement: &[u8]) -> Result<usize, Status> {
1663 replace_all_with_finder(
1664 buffer,
1665 needle.len(),
1666 replacement,
1667 |haystack, start| {
1668 if start >= haystack.len() {
1669 None
1670 } else {
1671 find(&haystack[start..], needle).map(|offset| start + offset)
1672 }
1673 },
1674 |haystack, end| {
1675 if end == 0 {
1676 None
1677 } else {
1678 rfind(&haystack[..end], needle)
1679 }
1680 },
1681 )
1682}
1683
1684/// Tries to replace all non-overlapping bytes in `buffer` that belong to `byteset` with `replacement`.
1685///

Callers 5

replace_all_same_lengthFunction · 0.85
replace_all_shrinksFunction · 0.85
replace_all_growsFunction · 0.85
basic_stringClass · 0.85

Calls 4

replace_all_with_finderFunction · 0.85
findFunction · 0.85
rfindFunction · 0.85
lenMethod · 0.80

Tested by 4

replace_all_same_lengthFunction · 0.68
replace_all_shrinksFunction · 0.68
replace_all_growsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…