Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow negative indexes in std.slice #117

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions crates/jrsonnet-evaluator/src/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,34 @@
/// For arrays, nothing will be copied on this call, instead [`ArrValue::Slice`] view will be returned.
pub fn slice(
self,
index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
index: Option<i32>,
end: Option<i32>,
step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
) -> Result<Self> {
match &self {
IndexableVal::Str(s) => {
let index = index.as_deref().copied().unwrap_or(0);
let end = end.as_deref().copied().unwrap_or(usize::MAX);
let mut computed_len = None;
let mut get_len = || {
computed_len.map_or_else(
|| {
let len = s.chars().count();
let _ = computed_len.insert(len);
len
},
|len| len,
)
};
let mut get_idx = |pos: Option<i32>, default| {
match pos {
Some(v) if v < 0 => get_len().saturating_sub((-v) as usize),
// No need to clamp, as iterator interface is used
Some(v) => v as usize,
None => default,
}
};

let index = get_idx(index, 0);
let end = get_idx(end, usize::MAX);
let step = step.as_deref().copied().unwrap_or(1);

if index >= end {
Expand All @@ -258,8 +278,13 @@
))
}
IndexableVal::Arr(arr) => {
let index = index.as_deref().copied().unwrap_or(0);
let end = end.as_deref().copied().unwrap_or(usize::MAX).min(arr.len());
let get_idx = |pos: Option<i32>, len: usize, default| match pos {
Some(v) if v < 0 => len.saturating_sub((-v) as usize),
Some(v) => (v as usize).min(len),
None => default,
};
let index = get_idx(index, arr.len(), 0);
let end = get_idx(end, arr.len(), arr.len());
let step = step.as_deref().copied().unwrap_or(1);

if index >= end {
Expand Down Expand Up @@ -352,7 +377,7 @@
}
impl PartialEq for StrValue {
// False positive, into_flat returns not StrValue, but IStr, thus no infinite recursion here.
#[allow(clippy::unconditional_recursion)]

Check warning on line 380 in crates/jrsonnet-evaluator/src/val.rs

View workflow job for this annotation

GitHub Actions / clippy

unknown lint: `clippy::unconditional_recursion`

warning: unknown lint: `clippy::unconditional_recursion` --> crates/jrsonnet-evaluator/src/val.rs:380:10 | 380 | #[allow(clippy::unconditional_recursion)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `unconditional_recursion` | = note: `#[warn(unknown_lints)]` on by default
fn eq(&self, other: &Self) -> bool {
let a = self.clone().into_flat();
let b = other.clone().into_flat();
Expand Down
4 changes: 2 additions & 2 deletions crates/jrsonnet-stdlib/src/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val
#[builtin]
pub fn builtin_slice(
indexable: IndexableVal,
index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
index: Option<i32>,
end: Option<i32>,
step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
) -> Result<Val> {
indexable.slice(index, end, step).map(Val::from)
Expand Down
Loading