Skip to content

Commit

Permalink
feat: *stripChars builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvisCraft authored and CertainLach committed May 17, 2024
1 parent 0b4fbcf commit 45c3558
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crates/jrsonnet-evaluator/src/arr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use spec::{ArrayLike, *};

/// Represents a Jsonnet array value.
#[derive(Debug, Clone, Trace)]
// may contrain other ArrValue
// may contain other ArrValue
#[trace(tracking(force))]
pub struct ArrValue(Cc<TraceBox<dyn ArrayLike>>);

Expand Down
7 changes: 7 additions & 0 deletions crates/jrsonnet-evaluator/src/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ pub enum IndexableVal {
Arr(ArrValue),
}
impl IndexableVal {
pub fn empty(&self) -> bool {
match self {
Self::Str(s) => s.is_empty(),
Self::Arr(s) => s.is_empty(),
}
}

pub fn to_array(self) -> ArrValue {
match self {
Self::Str(s) => ArrValue::chars(s.chars()),
Expand Down
3 changes: 3 additions & 0 deletions crates/jrsonnet-stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
("parseOctal", builtin_parse_octal::INST),
("parseHex", builtin_parse_hex::INST),
("stringChars", builtin_string_chars::INST),
("lstripChars", builtin_lstrip_chars::INST),
("rstripChars", builtin_rstrip_chars::INST),
("stripChars", builtin_strip_chars::INST),
// Misc
("length", builtin_length::INST),
("get", builtin_get::INST),
Expand Down
16 changes: 0 additions & 16 deletions crates/jrsonnet-stdlib/src/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@

thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',

lstripChars(str, chars)::
if std.length(str) > 0 && std.member(chars, str[0]) then
std.lstripChars(str[1:], chars)
else
str,

rstripChars(str, chars)::
local len = std.length(str);
if len > 0 && std.member(chars, str[len - 1]) then
std.rstripChars(str[:len - 1], chars)
else
str,

stripChars(str, chars)::
std.lstripChars(std.rstripChars(str, chars), chars),

mapWithIndex(func, arr)::
if !std.isFunction(func) then
error ('std.mapWithIndex first param must be function, got ' + std.type(func))
Expand Down
56 changes: 50 additions & 6 deletions crates/jrsonnet-stdlib/src/strings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::{collections::BTreeSet, str::pattern::Pattern};

use jrsonnet_evaluator::{
bail,
error::{ErrorKind::*, Result},
function::builtin,
typed::{Either2, M1},
val::ArrValue,
val::{ArrValue, IndexableVal},
Either, IStr, Val,
};

Expand Down Expand Up @@ -215,6 +217,53 @@ pub fn builtin_bigint(v: Either![f64, IStr]) -> Result<Val> {
})
}

#[builtin]
pub fn builtin_string_chars(str: IStr) -> ArrValue {
ArrValue::chars(str.chars())
}

#[builtin]
pub fn builtin_lstrip_chars(str: IStr, chars: IndexableVal) -> Result<IStr> {
if str.is_empty() || chars.empty() {
return str;
}

let pattern = new_trim_pattern(chars)?;
str.as_str().trim_start_matches(pattern).into()
}

#[builtin]
pub fn builtin_rstrip_chars(str: IStr, chars: IndexableVal) -> Result<IStr> {
if str.is_empty() || chars.empty() {
return str;
}

let pattern = new_trim_pattern(chars)?;
str.as_str().trim_start_matches(pattern).into()
}

#[builtin]
pub fn builtin_strip_chars(str: IStr, chars: IndexableVal) -> Result<IStr> {
if str.is_empty() || chars.empty() {
return str;
}

let pattern = new_trim_pattern(chars)?;
str.as_str().trim_start_matches(pattern).into()
}

fn new_trim_pattern(chars: IndexableVal) -> Result<impl Fn(char) -> bool> {
let chars: BTreeSet<char> = match chars {
IndexableVal::Str(chars) => chars.chars().collect(),
IndexableVal::Arr(chars) => chars
.iter()
.filter_map(|it| it.map(|it| it.as_str()).transpose())
.collect()?,
};

Ok(|char| chars.contains(&char))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -243,8 +292,3 @@ mod tests {
assert_eq!(parse_nat::<16>("BbC").unwrap(), 0xBBC as f64);
}
}

#[builtin]
pub fn builtin_string_chars(str: IStr) -> ArrValue {
ArrValue::chars(str.chars())
}

0 comments on commit 45c3558

Please sign in to comment.