Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit df0a708

Browse files
committed
rebase on slyce
1 parent 81d4d2c commit df0a708

File tree

4 files changed

+13
-67
lines changed

4 files changed

+13
-67
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ pest = "2.1.3"
1111
pest_derive = "2.1.0"
1212
serde = { version = "1.0", features = ["derive"] }
1313
serde_json = "1.0.57"
14+
slyce = "0.2.1"

src/ast.rs

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
use serde_json::Value;
8-
use std::cmp::Ordering;
8+
use slyce::Slice;
99
use std::iter;
1010

1111
/// A path is a tree of selector nodes.
@@ -63,13 +63,6 @@ pub enum UnionElement {
6363
Index(i64),
6464
}
6565

66-
#[derive(Debug)]
67-
pub struct Slice {
68-
pub start: Option<isize>,
69-
pub end: Option<isize>,
70-
pub step: Option<isize>,
71-
}
72-
7366
type Iter<'a> = Box<dyn Iterator<Item = &'a Value> + 'a>;
7467

7568
impl Path {
@@ -101,7 +94,7 @@ impl UnionElement {
10194
UnionElement::Name(name) => Box::new(v.get(name).into_iter()),
10295
UnionElement::Slice(slice) => {
10396
if let Value::Array(arr) = v {
104-
Box::new(array_slice(arr, slice.start, slice.end, slice.step))
97+
Box::new(slice.apply(arr))
10598
} else {
10699
Box::new(iter::empty())
107100
}
@@ -111,59 +104,6 @@ impl UnionElement {
111104
}
112105
}
113106

114-
fn array_slice(
115-
arr: &[Value],
116-
optional_start: Option<isize>,
117-
optional_end: Option<isize>,
118-
optional_step: Option<isize>,
119-
) -> Iter<'_> {
120-
let step = optional_step.unwrap_or(1);
121-
122-
let len = arr.len();
123-
124-
let start = optional_start
125-
.map(|s| if s < 0 { s + (len as isize) } else { s })
126-
.unwrap_or(if step > 0 { 0 } else { (len as isize) - 1 });
127-
128-
let end = optional_end
129-
.map(|e| if e < 0 { e + (len as isize) } else { e })
130-
.unwrap_or(if step > 0 { len as isize } else { -1 });
131-
132-
let mut sl = vec![];
133-
match step.cmp(&0) {
134-
Ordering::Greater => {
135-
let strt = if start < 0 { 0 } else { start as usize }; // avoid CPU attack
136-
let e = if end > (len as isize) {
137-
len
138-
} else {
139-
end as usize
140-
}; // avoid CPU attack
141-
for i in (strt..e).step_by(step as usize) {
142-
if i < len {
143-
sl.push(&arr[i]);
144-
}
145-
}
146-
}
147-
148-
Ordering::Less => {
149-
let strt = if start > (len as isize) {
150-
len as isize
151-
} else {
152-
start
153-
}; // avoid CPU attack
154-
let e = if end < -1 { -1 } else { end }; // avoid CPU attack
155-
for i in (-strt..-e).step_by(-step as usize) {
156-
if 0 <= -i && -i < (len as isize) {
157-
sl.push(&arr[-i as usize]);
158-
}
159-
}
160-
}
161-
162-
Ordering::Equal => (),
163-
}
164-
Box::new(sl.into_iter())
165-
}
166-
167107
fn abs_index(index: i64, node: &Value) -> usize {
168108
if index >= 0 {
169109
index as usize

src/parser.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
pub use crate::ast::*;
88
use crate::pest::Parser;
9+
use slyce::{Index, Slice};
910

1011
#[derive(Parser)]
1112
#[grammar = "grammar.pest"]
@@ -94,7 +95,11 @@ fn parse_union_array_slice(matcher_rule: pest::iterators::Pair<Rule>) -> UnionEl
9495
}
9596
}
9697

97-
UnionElement::Slice(Slice { start, end, step })
98+
UnionElement::Slice(Slice {
99+
start: start.map(Index::from).unwrap_or_default(),
100+
end: end.map(Index::from).unwrap_or_default(),
101+
step,
102+
})
98103
}
99104

100105
fn unescape(contents: &str) -> String {

tests/cts.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@
599599
"document": [0, 1, 2, 3],
600600
"result": [2, 1, 0]
601601
}, {
602-
"name": "union array slice, larger negative step",
602+
"name": "union array slice, larger negative step",
603603
"selector": "$[::-2]",
604604
"document": [0, 1, 2, 3],
605605
"result": [3, 1]
@@ -667,12 +667,12 @@
667667
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
668668
"result": []
669669
}, {
670-
"name": "union array slice, default indices with empty array",
670+
"name": "union array slice, default indices with empty array",
671671
"selector": "$[:]",
672672
"document": [],
673673
"result": []
674674
}, {
675-
"name": "union array slice, negative step with empty array",
675+
"name": "union array slice, negative step with empty array",
676676
"selector": "$[::-1]",
677677
"document": [],
678678
"result": []
@@ -702,7 +702,7 @@
702702
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
703703
"result": [9, 8, 7, 6, 5, 4, 3, 2, 1]
704704
}, {
705-
"name": "union array slice, excessively small to value with negative step",
705+
"name": "union array slice, excessively small to value with negative step",
706706
"selector": "$[3:-113667776004:-1]",
707707
"document": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
708708
"result": [3, 2, 1, 0]

0 commit comments

Comments
 (0)