Source file decoders_util.ml

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
(** Util module used for native builds (excluded in bs-config.json) *)
module My_result = struct
  type ('good, 'bad) t = ('good, 'bad) result = | Ok of 'good | Error of 'bad

  let return x = Ok x
  let map f e = match e with
    | Ok x -> Ok (f x)
    | Error s -> Error s
  let map_err f e = match e with
    | Ok _ as res -> res
    | Error y -> Error (f y)
  let flat_map f e = match e with
    | Ok x -> f x
    | Error s -> Error s

  module Infix = struct
    let (>|=) e f = map f e
    let (>>=) e f = flat_map f e
  end
end

module My_opt = struct
  let return x = Some x
  let map f = function None -> None | Some x -> Some (f x)
  let flat_map f o = match o with
    | None -> None
    | Some x -> f x
end

module My_list = struct
  let take n l =
    let rec direct i n l = match l with
      | [] -> []
      | _ when i=0 -> safe n [] l
      | x::l' ->
        if n > 0
        then x :: direct (i-1) (n-1) l'
        else []
    and safe n acc l = match l with
      | [] -> List.rev acc
      | _ when n=0 -> List.rev acc
      | x::l' -> safe (n-1) (x::acc) l'
    in
    direct 500 n l
      
  let map f l =
    let rec direct f i l = match l with
      | [] -> []
      | [x] -> [f x]
      | [x1;x2] -> let y1 = f x1 in [y1; f x2]
      | [x1;x2;x3] -> let y1 = f x1 in let y2 = f x2 in [y1; y2; f x3]
      | _ when i=0 -> List.rev (List.rev_map f l)
      | x1::x2::x3::x4::l' ->
        let y1 = f x1 in
        let y2 = f x2 in
        let y3 = f x3 in
        let y4 = f x4 in
        y1 :: y2 :: y3 :: y4 :: direct f (i-1) l'
    in
    direct f 500 l

  let all_some l =
    try Some (map (function Some x -> x | None -> raise Exit) l)
    with Exit -> None

  let mapi f l =
    let r = ref 0 in
    map
      (fun x ->
         let y = f !r x in
         incr r; y
      ) l

  let find_map f l =
    let rec aux f = function
      | [] -> None
      | x::l' ->
        match f x with
          | Some _ as res -> res
          | None -> aux f l'
    in aux f l

  let filter_map f l =
    let rec recurse acc l = match l with
      | [] -> List.rev acc
      | x::l' ->
        let acc' = match f x with | None -> acc | Some y -> y::acc in
        recurse acc' l'
    in recurse [] l

  let fold_left = List.fold_left
end

let with_file_in file f =
  let ic = open_in file in
  try
    let res = f ic in
    close_in ic;
    res
  with
  | e -> 
    close_in_noerr ic;
    raise e

let read_all ic : string =
  let buf = ref (Bytes.create 2048) in
  let len = ref 0 in
  try
    while true do
      (* resize *)
      if !len = Bytes.length !buf then (
        buf := Bytes.extend !buf 0 !len;
      );
      assert (Bytes.length !buf > !len);
      let n = input ic !buf !len (Bytes.length !buf - !len) in
      len := !len + n;
      if n = 0 then raise Exit;  (* exhausted *)
    done;
    assert false (* never reached*)
  with Exit ->
    Bytes.sub_string !buf 0 !len