@@ -176,7 +176,7 @@ let index (value : int) (json : Json.t) =
176
176
| `List _ -> Output. return `Null
177
177
| _ -> Error (make_error (" [" ^ string_of_int value ^ " ]" ) json)
178
178
179
- let slice (start : int option ) (end_ : int option ) (json : Json.t ) =
179
+ let slice (start : int option ) (finish : int option ) (json : Json.t ) =
180
180
let start =
181
181
match (json, start) with
182
182
| `String s , Some start when start > String. length s -> String. length s
@@ -185,36 +185,36 @@ let slice (start : int option) (end_ : int option) (json : Json.t) =
185
185
| `List l , Some start when start < 0 -> start + List. length l
186
186
| (`String _ | `List _ ), Some start -> start
187
187
| (`String _ | `List _ ), None -> 0
188
- | _ -> assert false
188
+ | _ -> (* slice can't be parsed outside of List or String *) assert false
189
189
in
190
- let end_ =
191
- match (json, end_ ) with
190
+ let finish =
191
+ match (json, finish ) with
192
192
| `String s , None -> String. length s
193
193
| `String s , Some end_ when end_ > String. length s -> String. length s
194
194
| `String s , Some end_ when end_ < 0 -> end_ + String. length s
195
195
| `List l , None -> List. length l
196
196
| `List l , Some end_ when end_ > List. length l -> List. length l
197
197
| `List l , Some end_ when end_ < 0 -> end_ + List. length l
198
198
| (`String _ | `List _ ), Some end_ -> end_
199
- | _ -> assert false
199
+ | _ -> (* slice can't be parsed outside of List or String *) assert false
200
200
in
201
201
match json with
202
- | `String _s when end_ < start -> Output. return (`String " " )
203
- | `String s -> Output. return (`String (String. sub s start (end_ - start)))
204
- | `List _l when end_ < start -> Output. return (`List [] )
202
+ | `String _s when finish < start -> Output. return (`String " " )
203
+ | `String s -> Output. return (`String (String. sub s start (finish - start)))
204
+ | `List _l when finish < start -> Output. return (`List [] )
205
205
| `List l ->
206
206
let sliced =
207
207
List. fold_left
208
208
(fun (acc , i ) x ->
209
- if i > = start && i < end_ then (x :: acc, i + 1 ) else (acc, i + 1 ))
209
+ if i > = start && i < finish then (x :: acc, i + 1 ) else (acc, i + 1 ))
210
210
([] , 0 ) l
211
211
|> fst |> List. rev
212
212
in
213
213
Output. return (`List sliced)
214
214
| _ ->
215
215
Error
216
216
(make_error
217
- (" [" ^ string_of_int start ^ " :" ^ string_of_int end_ ^ " ]" )
217
+ (" [" ^ string_of_int start ^ " :" ^ string_of_int finish ^ " ]" )
218
218
json)
219
219
220
220
let rec compile expression json : (Json.t list, string) result =
@@ -224,7 +224,7 @@ let rec compile expression json : (Json.t list, string) result =
224
224
| Keys -> keys json
225
225
| Key (key , opt ) -> member key opt json
226
226
| Index idx -> index idx json
227
- | Slice (start , end_ ) -> slice start end_ json
227
+ | Slice (start , finish ) -> slice start finish json
228
228
| Head -> head json
229
229
| Tail -> tail json
230
230
| Length -> length json
0 commit comments