Replaces at most `limit` non-overlapping matches in `text` with the replacement provided. If `limit` is 0, then all non-overlapping matches are replaced. See the documentation for `replace` for details on how to access capturing group matches in the replacement text.
(
&self,
text: &'t [u8],
limit: usize,
mut rep: R,
)
| 488 | /// See the documentation for `replace` for details on how to access |
| 489 | /// capturing group matches in the replacement text. |
| 490 | pub fn replacen<'t, R: Replacer>( |
| 491 | &self, |
| 492 | text: &'t [u8], |
| 493 | limit: usize, |
| 494 | mut rep: R, |
| 495 | ) -> Cow<'t, [u8]> { |
| 496 | if let Some(rep) = rep.no_expansion() { |
| 497 | let mut it = self.find_iter(text).enumerate().peekable(); |
| 498 | if it.peek().is_none() { |
| 499 | return Cow::Borrowed(text); |
| 500 | } |
| 501 | let mut new = Vec::with_capacity(text.len()); |
| 502 | let mut last_match = 0; |
| 503 | for (i, m) in it { |
| 504 | if limit > 0 && i >= limit { |
| 505 | break |
| 506 | } |
| 507 | new.extend_from_slice(&text[last_match..m.start()]); |
| 508 | new.extend_from_slice(&rep); |
| 509 | last_match = m.end(); |
| 510 | } |
| 511 | new.extend_from_slice(&text[last_match..]); |
| 512 | return Cow::Owned(new); |
| 513 | } |
| 514 | |
| 515 | // The slower path, which we use if the replacement needs access to |
| 516 | // capture groups. |
| 517 | let mut it = self.captures_iter(text).enumerate().peekable(); |
| 518 | if it.peek().is_none() { |
| 519 | return Cow::Borrowed(text); |
| 520 | } |
| 521 | let mut new = Vec::with_capacity(text.len()); |
| 522 | let mut last_match = 0; |
| 523 | for (i, cap) in it { |
| 524 | if limit > 0 && i >= limit { |
| 525 | break |
| 526 | } |
| 527 | // unwrap on 0 is OK because captures only reports matches |
| 528 | let m = cap.get(0).unwrap(); |
| 529 | new.extend_from_slice(&text[last_match..m.start()]); |
| 530 | rep.replace_append(&cap, &mut new); |
| 531 | last_match = m.end(); |
| 532 | } |
| 533 | new.extend_from_slice(&text[last_match..]); |
| 534 | Cow::Owned(new) |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /// Advanced or "lower level" search methods. |
no test coverage detected