@@ -41,11 +41,13 @@ impl std::fmt::Display for ParseError {
41
41
impl std:: error:: Error for ParseError { }
42
42
43
43
/// Convert a relative index into an absolute index
44
- fn abs_index ( index : isize , len : usize ) -> Option < usize > {
44
+ fn abs_index ( index : isize , len : usize ) -> Result < usize , usize > {
45
45
if index >= 0 {
46
- Some ( index as usize )
46
+ Ok ( index as usize )
47
+ } else if let Some ( index) = len. checked_sub ( index. unsigned_abs ( ) ) {
48
+ Ok ( index)
47
49
} else {
48
- len . checked_sub ( index. unsigned_abs ( ) )
50
+ Err ( ( len as isize + index) . unsigned_abs ( ) )
49
51
}
50
52
}
51
53
@@ -81,7 +83,7 @@ impl Expression {
81
83
Self :: Subscript ( expr, index) => match expr. get ( root) {
82
84
Some ( value) => match value. kind {
83
85
ValueKind :: Array ( ref array) => {
84
- let index = abs_index ( index, array. len ( ) ) ?;
86
+ let index = abs_index ( index, array. len ( ) ) . ok ( ) ?;
85
87
array. get ( index)
86
88
}
87
89
@@ -137,7 +139,7 @@ impl Expression {
137
139
138
140
match value. kind {
139
141
ValueKind :: Array ( ref mut array) => {
140
- let index = abs_index ( index, array. len ( ) ) ?;
142
+ let index = abs_index ( index, array. len ( ) ) . ok ( ) ?;
141
143
142
144
if index >= array. len ( ) {
143
145
array. resize ( index + 1 , Value :: new ( None , ValueKind :: Nil ) ) ;
@@ -212,10 +214,21 @@ impl Expression {
212
214
}
213
215
214
216
if let ValueKind :: Array ( ref mut array) = parent. kind {
215
- let uindex = abs_index ( index, array. len ( ) ) . unwrap ( ) ;
216
- if uindex >= array. len ( ) {
217
- array. resize ( uindex + 1 , Value :: new ( None , ValueKind :: Nil ) ) ;
218
- }
217
+ let uindex = match abs_index ( index, array. len ( ) ) {
218
+ Ok ( uindex) => {
219
+ if uindex >= array. len ( ) {
220
+ array. resize ( uindex + 1 , Value :: new ( None , ValueKind :: Nil ) ) ;
221
+ }
222
+ uindex
223
+ }
224
+ Err ( insertion) => {
225
+ array. splice (
226
+ 0 ..0 ,
227
+ ( 0 ..insertion) . map ( |_| Value :: new ( None , ValueKind :: Nil ) ) ,
228
+ ) ;
229
+ 0
230
+ }
231
+ } ;
219
232
220
233
array[ uindex] = value;
221
234
}
0 commit comments