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])
| 1660 | /// |
| 1661 | /// Returns the number of replacements performed. |
| 1662 | pub 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 | /// |
searching dependent graphs…