Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/re_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,8 @@ impl<'c, 't> Iterator for SubCaptureMatches<'c, 't> {
///
/// In general, users of this crate shouldn't need to implement this trait,
/// since implementations are already provided for `&[u8]` and
/// `FnMut(&Captures) -> Vec<u8>`, which covers most use cases.
/// `FnMut(&Captures) -> Vec<u8>` (or any `FnMut(&Captures) -> T`
/// where `T: AsRef<[u8]>`), which covers most use cases.
pub trait Replacer {
/// Appends text to `dst` to replace the current match.
///
Expand Down Expand Up @@ -1141,9 +1142,9 @@ impl<'a> Replacer for &'a [u8] {
}
}

impl<F> Replacer for F where F: FnMut(&Captures) -> Vec<u8> {
impl<F, T> Replacer for F where F: FnMut(&Captures) -> T, T: AsRef<[u8]> {
fn replace_append(&mut self, caps: &Captures, dst: &mut Vec<u8>) {
dst.extend_from_slice(&(*self)(caps));
dst.extend_from_slice((*self)(caps).as_ref());
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/re_unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,8 @@ impl<'r, 't> Iterator for Matches<'r, 't> {
///
/// In general, users of this crate shouldn't need to implement this trait,
/// since implementations are already provided for `&str` and
/// `FnMut(&Captures) -> String`, which covers most use cases.
/// `FnMut(&Captures) -> String` (or any `FnMut(&Captures) -> T`
/// where `T: AsRef<str>`), which covers most use cases.
pub trait Replacer {
/// Appends text to `dst` to replace the current match.
///
Expand Down Expand Up @@ -1183,9 +1184,9 @@ impl<'a> Replacer for &'a str {
}
}

impl<F> Replacer for F where F: FnMut(&Captures) -> String {
impl<F, T> Replacer for F where F: FnMut(&Captures) -> T, T: AsRef<str> {
fn replace_append(&mut self, caps: &Captures, dst: &mut String) {
dst.push_str(&(*self)(caps));
dst.push_str((*self)(caps).as_ref());
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/macros_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
macro_rules! text { ($text:expr) => { $text.as_bytes() } }
macro_rules! t { ($re:expr) => { text!($re) } }
macro_rules! match_text { ($text:expr) => { $text.as_bytes() } }
macro_rules! use_ { ($($path: tt)*) => { use regex::bytes::$($path)*; } }

macro_rules! bytes { ($text:expr) => { $text } }

Expand Down
1 change: 1 addition & 0 deletions tests/macros_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
macro_rules! text { ($text:expr) => { $text } }
macro_rules! t { ($text:expr) => { text!($text) } }
macro_rules! match_text { ($text:expr) => { $text.as_str() } }
macro_rules! use_ { ($($path: tt)*) => { use regex::$($path)*; } }

macro_rules! no_expand {
($text:expr) => {{
Expand Down
6 changes: 6 additions & 0 deletions tests/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ replace!(no_expand1, replace,
r"(\S+)\s+(\S+)", "w1 w2", no_expand!("$2 $1"), "$2 $1");
replace!(no_expand2, replace,
r"(\S+)\s+(\S+)", "w1 w2", no_expand!("$$1"), "$$1");
use_!(Captures);
replace!(closure_returning_reference, replace, r"(\d+)", "age: 26",
| captures: &Captures | &match_text!(captures.get(1).unwrap())[0..1], "age: 2");
replace!(closure_returning_value, replace, r"\d+", "age: 26",
| _captures: &Captures | t!("Z").to_owned(), "age: Z");


// See https://github.com/rust-lang/regex/issues/314
replace!(match_at_start_replace_with_empty, replace_all, r"foo", "foobar", t!(""), "bar");
Expand Down