-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.ml
More file actions
68 lines (62 loc) · 2.29 KB
/
misc.ml
File metadata and controls
68 lines (62 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
open Environment
open Error_monad
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
type 'a lazyt = unit -> 'a
type 'a lazy_list_t = LCons of 'a * ('a lazy_list_t tzresult Lwt.t lazyt)
type 'a lazy_list = 'a lazy_list_t tzresult Lwt.t
let rec (-->) i j = (* [i; i+1; ...; j-1] *)
if Compare.Int.(i >= j)
then []
else i :: (succ i --> j)
let rec (--->) i j = (* [i; i+1; ...; j-1] *)
if Compare.Int32.(i >= j)
then []
else i :: (Int32.succ i ---> j)
let split delim ?(limit = max_int) path =
let l = String.length path in
let rec do_slashes acc limit i =
if Compare.Int.(i >= l) then
List.rev acc
else if Compare.Char.(String.get path i = delim) then
do_slashes acc limit (i + 1)
else
do_split acc limit i
and do_split acc limit i =
if Compare.Int.(limit <= 0) then
if Compare.Int.(i = l) then
List.rev acc
else
List.rev (String.sub path i (l - i) :: acc)
else
do_component acc (pred limit) i i
and do_component acc limit i j =
if Compare.Int.(j >= l) then
if Compare.Int.(i = j) then
List.rev acc
else
List.rev (String.sub path i (j - i) :: acc)
else if Compare.Char.(String.get path j = delim) then
do_slashes (String.sub path i (j - i) :: acc) limit j
else
do_component acc limit i (j + 1) in
if Compare.Int.(limit > 0) then
do_slashes [] limit 0
else
[ path ]
let pp_print_paragraph ppf description =
Format.fprintf ppf "@[%a@]"
Format.(pp_print_list ~pp_sep:pp_print_space pp_print_string)
(split ' ' description)
let take n l =
let rec loop acc n = function
| xs when Compare.Int.(n <= 0) -> Some (List.rev acc, xs)
| [] -> None
| x :: xs -> loop (x :: acc) (n-1) xs in
loop [] n l