Skip to content

Commit

Permalink
do not expose the matcher beyong Regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoarrais committed Nov 5, 2019
1 parent 5643b61 commit fbf8817
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/lib/builtins/regexp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::ops::Deref;

use gc::Gc;
Expand All @@ -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.
Expand All @@ -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<T: FromValue>(args: &[Value], idx: usize) -> Result<T, Value> {
match args.get(idx) {
Some(arg) => from_value(arg.clone()).map_err(to_value),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/builtins/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
})
}
Expand Down

0 comments on commit fbf8817

Please sign in to comment.