Skip to content

Commit

Permalink
perf: native implementation of std.splitLimitR (#163)
Browse files Browse the repository at this point in the history
* native implementation of std.splitLimitR

* style: fix formatting

---------

Co-authored-by: Yaroslav Bolyukin <[email protected]>
  • Loading branch information
tstenner and CertainLach authored May 17, 2024
1 parent c5b983e commit f319c35
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions crates/jrsonnet-stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
("isEmpty", builtin_is_empty::INST),
("equalsIgnoreCase", builtin_equals_ignore_case::INST),
("splitLimit", builtin_splitlimit::INST),
("splitLimitR", builtin_splitlimitr::INST),
("asciiUpper", builtin_ascii_upper::INST),
("asciiLower", builtin_ascii_lower::INST),
("findSubstr", builtin_find_substr::INST),
Expand Down
7 changes: 0 additions & 7 deletions crates/jrsonnet-stdlib/src/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
stripChars(str, chars)::
std.lstripChars(std.rstripChars(str, chars), chars),

splitLimitR(str, c, maxsplits)::
if maxsplits == -1 then
std.splitLimit(str, c, -1)
else
local revStr(str) = std.join('', std.reverse(std.stringChars(str)));
std.map(function(e) revStr(e), std.reverse(std.splitLimit(revStr(str), revStr(c), maxsplits))),

split(str, c):: std.splitLimit(str, c, -1),

mapWithIndex(func, arr)::
Expand Down
19 changes: 19 additions & 0 deletions crates/jrsonnet-stdlib/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ pub fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) ->
}
}

#[builtin]
pub fn builtin_splitlimitr(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> ArrValue {
use Either2::*;
match maxsplits {
A(n) =>
// rsplitn does not implement DoubleEndedIterator so collect into
// a temporary vec
{
str.rsplitn(n + 1, &c as &str)
.map(Val::string)
.collect::<Vec<_>>()
.into_iter()
.rev()
.collect()
}
B(_) => str.split(&c as &str).map(Val::string).collect(),
}
}

#[builtin]
pub fn builtin_ascii_upper(str: IStr) -> String {
str.to_ascii_uppercase()
Expand Down
7 changes: 7 additions & 0 deletions tests/golden/builtin_strings.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local str = 'ab::cd::ef';
{
split: std.split(str, '::'),
splitlimit: std.splitLimit(str, '::', 1),
splitlimitRNoLimit: std.splitLimit(str, '::', -1),
splitlimitR: std.splitLimitR(str, '::', 1),
}
20 changes: 20 additions & 0 deletions tests/golden/builtin_strings.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"split": [
"ab",
"cd",
"ef"
],
"splitlimit": [
"ab",
"cd::ef"
],
"splitlimitR": [
"ab::cd",
"ef"
],
"splitlimitRNoLimit": [
"ab",
"cd",
"ef"
]
}

0 comments on commit f319c35

Please sign in to comment.