Function regex_automata::util::interpolate::bytes

source ·
pub fn bytes(
    replacement: &[u8],
    append: impl FnMut(usize, &mut Vec<u8>),
    name_to_index: impl FnMut(&str) -> Option<usize>,
    dst: &mut Vec<u8>,
)
Expand description

Accepts a replacement byte string and interpolates capture references with their corresponding values.

append should be a function that appends the byte string value of a capture group at a particular index to the byte string given. If the capture group index is invalid, then nothing should be appended.

name_to_index should be a function that maps a capture group name to a capture group index. If the given name doesn’t exist, then None should be returned.

Finally, dst is where the final interpolated contents should be written. If replacement contains no capture group references, then dst will be equivalent to replacement.

See the module documentation for details about the format supported.

§Example

use regex_automata::util::interpolate;

let mut dst = vec![];
interpolate::bytes(
    b"foo $bar baz",
    |index, dst| {
        if index == 0 {
            dst.extend_from_slice(b"BAR");
        }
    },
    |name| {
        if name == "bar" {
            Some(0)
        } else {
            None
        }
    },
    &mut dst,
);
assert_eq!(&b"foo BAR baz"[..], dst);