Trait regex::bytes::Replacer

source ·
pub trait Replacer {
    // Required method
    fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>);

    // Provided methods
    fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>> { ... }
    fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self> { ... }
}
Expand description

A trait for types that can be used to replace matches in a haystack.

In general, users of this crate shouldn’t need to implement this trait, since implementations are already provided for &[u8] along with other variants of byte string types, as well as FnMut(&Captures) -> Vec<u8> (or any FnMut(&Captures) -> T where T: AsRef<[u8]>). Those cover most use cases, but callers can implement this trait directly if necessary.

§Example

This example shows a basic implementation of the Replacer trait. This can be done much more simply using the replacement byte string interpolation support (e.g., $first $last), but this approach avoids needing to parse the replacement byte string at all.

use regex::bytes::{Captures, Regex, Replacer};

struct NameSwapper;

impl Replacer for NameSwapper {
    fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
        dst.extend_from_slice(&caps["first"]);
        dst.extend_from_slice(b" ");
        dst.extend_from_slice(&caps["last"]);
    }
}

let re = Regex::new(r"(?<last>[^,\s]+),\s+(?<first>\S+)").unwrap();
let result = re.replace(b"Springsteen, Bruce", NameSwapper);
assert_eq!(result, &b"Bruce Springsteen"[..]);

Required Methods§

source

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>)

Appends possibly empty data to dst to replace the current match.

The current match is represented by caps, which is guaranteed to have a match at capture group 0.

For example, a no-op replacement would be dst.extend_from_slice(&caps[0]).

Provided Methods§

source

fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>>

Return a fixed unchanging replacement byte string.

When doing replacements, if access to Captures is not needed (e.g., the replacement byte string does not need $ expansion), then it can be beneficial to avoid finding sub-captures.

In general, this is called once for every call to a replacement routine such as Regex::replace_all.

source

fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self>

Returns a type that implements Replacer, but that borrows and wraps this Replacer.

This is useful when you want to take a generic Replacer (which might not be cloneable) and use it without consuming it, so it can be used more than once.

§Example
use regex::bytes::{Regex, Replacer};

fn replace_all_twice<R: Replacer>(
    re: Regex,
    src: &[u8],
    mut rep: R,
) -> Vec<u8> {
    let dst = re.replace_all(src, rep.by_ref());
    let dst = re.replace_all(&dst, rep.by_ref());
    dst.into_owned()
}

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Replacer for Vec<u8>

source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>)

source§

fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>>

source§

impl<'a> Replacer for &'a Cow<'a, [u8]>

source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>)

source§

fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>>

source§

impl<'a> Replacer for &'a Vec<u8>

source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>)

source§

fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>>

source§

impl<'a> Replacer for &'a [u8]

source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>)

source§

fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>>

source§

impl<'a> Replacer for Cow<'a, [u8]>

source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>)

source§

fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>>

source§

impl<'a, const N: usize> Replacer for &'a [u8; N]

source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>)

source§

fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>>

source§

impl<const N: usize> Replacer for [u8; N]

source§

fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>)

source§

fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>>

Implementors§

source§

impl<'a, R: Replacer + ?Sized + 'a> Replacer for ReplacerRef<'a, R>

source§

impl<'s> Replacer for NoExpand<'s>

source§

impl<F, T> Replacer for F
where F: FnMut(&Captures<'_>) -> T, T: AsRef<[u8]>,