diff --git a/proost/src/replace_word.rs b/proost/src/replace_word.rs index c55607f4617654c9e62b7165edf8be5302ce221b..86fe93e352acd1900ef5c026c03bec8bf0f9ab1a 100644 --- a/proost/src/replace_word.rs +++ b/proost/src/replace_word.rs @@ -1,22 +1,10 @@ /// Variation of the std replace function that only replace full words -pub fn replace_word(input: &String, from: &str, to: &str) -> String { - let mut result = String::new(); - let mut last_end = 0; - let input_len = input.len(); - let from_len = from.len(); - for (start, _) in input.match_indices(from) { - if (start == 0 || input.as_bytes()[start - 1] == b' ') - && (start + from_len == input_len || input.as_bytes()[start + from_len] == b' ') - { - result.push_str(input.get(last_end..start).unwrap_or_else(|| unreachable!())); - result.push_str(to); - last_end = start + from_len; - } +pub fn replace_inplace(input: &mut String, from: &str, to: &str) { + let mut offset = 0; + + while let Some(pos) = input[offset..].find(from) { + input.replace_range(offset + pos..from.len(), to); + + offset += pos + (to.len() - from.len()); } - result.push_str( - input - .get(last_end..input_len) - .unwrap_or_else(|| unreachable!()), - ); - result }