From fbf88170576ca4be33d93a659e2ba46fedd0b193 Mon Sep 17 00:00:00 2001 From: Thiago Arrais Date: Tue, 5 Nov 2019 13:33:33 -0300 Subject: [PATCH] do not expose the matcher beyong Regexp --- src/lib/builtins/regexp.rs | 9 ++++++++- src/lib/builtins/string.rs | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/lib/builtins/regexp.rs b/src/lib/builtins/regexp.rs index 684eb6d60ab..1307c5fe7dd 100644 --- a/src/lib/builtins/regexp.rs +++ b/src/lib/builtins/regexp.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::ops::Deref; use gc::Gc; @@ -16,7 +17,7 @@ use crate::{ #[derive(Debug)] pub struct RegExp { /// Regex matcher. - pub matcher: Regex, + matcher: Regex, /// Update last_index, set if global or sticky flags are set. use_last_index: bool, /// String of parsed flags. @@ -37,6 +38,12 @@ pub struct RegExp { impl InternalState for RegExp {} +impl RegExp { + pub fn replace_all<'t>(&self, text: &'t str, rep: &str) -> Cow<'t, str> { + self.matcher.replace_all(text, rep) + } +} + fn get_argument(args: &[Value], idx: usize) -> Result { match args.get(idx) { Some(arg) => from_value(arg.clone()).map_err(to_value), diff --git a/src/lib/builtins/string.rs b/src/lib/builtins/string.rs index 377ebf2da25..54090f52a0d 100644 --- a/src/lib/builtins/string.rs +++ b/src/lib/builtins/string.rs @@ -752,7 +752,7 @@ pub fn replace(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultVal Ok(to_value(result_str)) } else { pattern.with_internal_state_ref(|pattern_re: &RegExp| { - let result_str = pattern_re.matcher.replace_all(this_str, replacement_str); + let result_str = pattern_re.replace_all(this_str, replacement_str); Ok(to_value(result_str.into_owned())) }) }