Skip to content

Commit f51ae03

Browse files
committed
refactor(path): Clarify index operation
1 parent f013b6d commit f51ae03

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/path/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ impl std::fmt::Display for ParseError {
4040

4141
impl std::error::Error for ParseError {}
4242

43-
fn sindex_to_uindex(index: isize, len: usize) -> Option<usize> {
43+
/// Convert a relative index into an absolute index
44+
fn abs_index(index: isize, len: usize) -> Option<usize> {
4445
if index >= 0 {
4546
Some(index as usize)
4647
} else {
@@ -80,7 +81,7 @@ impl Expression {
8081
Self::Subscript(expr, index) => match expr.get(root) {
8182
Some(value) => match value.kind {
8283
ValueKind::Array(ref array) => {
83-
let index = sindex_to_uindex(index, array.len())?;
84+
let index = abs_index(index, array.len())?;
8485

8586
if index >= array.len() {
8687
None
@@ -141,7 +142,7 @@ impl Expression {
141142

142143
match value.kind {
143144
ValueKind::Array(ref mut array) => {
144-
let index = sindex_to_uindex(index, array.len())?;
145+
let index = abs_index(index, array.len())?;
145146

146147
if index >= array.len() {
147148
array.resize(index + 1, Value::new(None, ValueKind::Nil));
@@ -216,7 +217,7 @@ impl Expression {
216217
}
217218

218219
if let ValueKind::Array(ref mut array) = parent.kind {
219-
let uindex = sindex_to_uindex(index, array.len()).unwrap();
220+
let uindex = abs_index(index, array.len()).unwrap();
220221
if uindex >= array.len() {
221222
array.resize(uindex + 1, Value::new(None, ValueKind::Nil));
222223
}

0 commit comments

Comments
 (0)