-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from CertainLach/feature/std-regex
feat: add std regex builtins
- Loading branch information
Showing
8 changed files
with
414 additions
and
89 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use std::{cell::RefCell, hash::BuildHasherDefault, num::NonZeroUsize, rc::Rc}; | ||
|
||
use ::regex::Regex; | ||
use jrsonnet_evaluator::{ | ||
error::{ErrorKind::*, Result}, | ||
val::StrValue, | ||
IStr, ObjValueBuilder, Val, | ||
}; | ||
use jrsonnet_macros::builtin; | ||
use lru::LruCache; | ||
use rustc_hash::FxHasher; | ||
|
||
pub struct RegexCacheInner { | ||
cache: RefCell<LruCache<IStr, Rc<Regex>, BuildHasherDefault<FxHasher>>>, | ||
} | ||
impl Default for RegexCacheInner { | ||
fn default() -> Self { | ||
Self { | ||
cache: RefCell::new(LruCache::with_hasher( | ||
NonZeroUsize::new(20).unwrap(), | ||
BuildHasherDefault::default(), | ||
)), | ||
} | ||
} | ||
} | ||
pub type RegexCache = Rc<RegexCacheInner>; | ||
impl RegexCacheInner { | ||
fn parse(&self, pattern: IStr) -> Result<Rc<Regex>> { | ||
let mut cache = self.cache.borrow_mut(); | ||
if let Some(found) = cache.get(&pattern) { | ||
return Ok(found.clone()); | ||
} | ||
let regex = Regex::new(&pattern) | ||
.map_err(|e| RuntimeError(format!("regex parse failed: {e}").into()))?; | ||
let regex = Rc::new(regex); | ||
cache.push(pattern, regex.clone()); | ||
Ok(regex) | ||
} | ||
} | ||
|
||
pub fn regex_match_inner(regex: &Regex, str: String) -> Result<Val> { | ||
let mut out = ObjValueBuilder::with_capacity(3); | ||
|
||
let mut captures = Vec::with_capacity(regex.captures_len()); | ||
let mut named_captures = ObjValueBuilder::with_capacity(regex.capture_names().len()); | ||
|
||
let Some(captured) = regex.captures(&str) else { | ||
return Ok(Val::Null); | ||
}; | ||
|
||
for ele in captured.iter().skip(1) { | ||
if let Some(ele) = ele { | ||
captures.push(Val::Str(StrValue::Flat(ele.as_str().into()))) | ||
} else { | ||
captures.push(Val::Str(StrValue::Flat(IStr::empty()))) | ||
} | ||
} | ||
for (i, name) in regex | ||
.capture_names() | ||
.skip(1) | ||
.enumerate() | ||
.flat_map(|(i, v)| Some((i, v?))) | ||
{ | ||
let capture = captures[i].clone(); | ||
named_captures.field(name).try_value(capture)?; | ||
} | ||
|
||
out.field("string") | ||
.value(Val::Str(captured.get(0).unwrap().as_str().into())); | ||
out.field("captures").value(Val::Arr(captures.into())); | ||
out.field("namedCaptures") | ||
.value(Val::Obj(named_captures.build())); | ||
|
||
Ok(Val::Obj(out.build())) | ||
} | ||
|
||
#[builtin(fields( | ||
cache: RegexCache, | ||
))] | ||
pub fn builtin_regex_partial_match( | ||
this: &builtin_regex_partial_match, | ||
pattern: IStr, | ||
str: String, | ||
) -> Result<Val> { | ||
let regex = this.cache.parse(pattern)?; | ||
regex_match_inner(®ex, str) | ||
} | ||
|
||
#[builtin(fields( | ||
cache: RegexCache, | ||
))] | ||
pub fn builtin_regex_full_match( | ||
this: &builtin_regex_full_match, | ||
pattern: StrValue, | ||
str: String, | ||
) -> Result<Val> { | ||
let pattern = format!("^{pattern}$").into(); | ||
let regex = this.cache.parse(pattern)?; | ||
regex_match_inner(®ex, str) | ||
} | ||
|
||
#[builtin] | ||
pub fn builtin_regex_quote_meta(pattern: String) -> String { | ||
regex::escape(&pattern) | ||
} | ||
|
||
#[builtin(fields( | ||
cache: RegexCache, | ||
))] | ||
pub fn builtin_regex_replace( | ||
this: &builtin_regex_replace, | ||
str: String, | ||
pattern: IStr, | ||
to: String, | ||
) -> Result<String> { | ||
let regex = this.cache.parse(pattern)?; | ||
let replaced = regex.replace(&str, to); | ||
Ok(replaced.to_string()) | ||
} | ||
|
||
#[builtin(fields( | ||
cache: RegexCache, | ||
))] | ||
pub fn builtin_regex_global_replace( | ||
this: &builtin_regex_global_replace, | ||
str: String, | ||
pattern: IStr, | ||
to: String, | ||
) -> Result<String> { | ||
let regex = this.cache.parse(pattern)?; | ||
let replaced = regex.replace_all(&str, to); | ||
Ok(replaced.to_string()) | ||
} |