package alba

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file common.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
let identity (a:'a): 'a = a

module Void:
sig
  type t
end =
  struct
    type t = int
  end


module Unit:
sig
  type t = unit
end =
  struct
    type t = unit
  end


module Int =
  struct
    type t = int
    let compare = Stdlib.compare

    let iterate (n: t) (f: 'a -> 'a) (start: 'a): 'a =
      let rec iter n v =
        if n = 0 then
          v
        else
          iter (n - 1) (f v)
      in
      iter n start
  end

module Int_set = Set.Make (Int)
module Int_map = Finite_map.Make (Int)



module Either =
  struct
    type ('a,'b) t =
      | Left of 'a
      | Right of 'b
    let left a = Left a
    let right b = Right b
  end


module Char =
  struct
    include Char
    let is_lower (c:char): bool =
      'a' <= c && c <= 'z'
    let is_upper (c:char): bool =
      'A' <= c && c <= 'Z'
    let is_letter (c:char): bool =
      is_lower c || is_upper c
    let is_digit (c:char): bool =
      '0' <= c && c <= '9'
  end


module String =
  struct
    include String

    let one (c:char): string =
      String.make 1 c

    let is_prefix (a: string) (b:string): bool =
      let len_a = length a in
      len_a <= length b && a = sub b 0 len_a

    let is_suffix (a: string) (b:string): bool =
      let len_a = length a
      and len_b = length b
      in
      len_a <= len_b
      && a = sub b  (len_b - len_a) len_a

    let find (f:char -> bool) (start:int) (s:string): int =
      let len = String.length s in
      let rec find i =
        if i = len || f s.[i] then
          i
        else
          find (i+1)
      in
      find start


    let has (f: char -> bool) (start: int) (s: string): bool =
        find f start s
        <
        length s


    let find_bwd (f:char -> bool) (beyond:int) (s:string): int =
      assert (beyond <= String.length s);
      let rec find i =
        if i = 0 || f s.[i-1] then
          i-1
        else
          find (i-1)
      in
      find beyond

    let list (s:string): char list =
      let rec list cs i =
        if i = 0 then
          cs
        else
          let j = i - 1 in
          list (s.[j]::cs) j
      in
      list [] (length s)

    let of_list (cs:char list): string =
      let rec str cs i =
        match cs with
        | [] ->
           Bytes.create i
        | c::cs ->
           let bs = str cs (i+1) in
           Bytes.set bs i c;
           bs
      in
      let bs = str cs 0 in
      Bytes.unsafe_to_string bs


    let reverse (s: string): string =
        let len = length s in
        init len (fun i -> s.[len - 1 - i])


    module To_readable =
    struct
        type t = int * string

        let has_more ((i,s): t): bool =
            i < String.length s

        let peek ((i,s): t): char =
            assert (has_more (i,s));
            s.[i]

        let advance ((i,s)): t =
            assert (has_more (i,s));
            (i + 1, s)

        let make (s: string): t =
            (0,s)
    end

    module From_readable (R: Module_types.READABLE) =
    struct
        let make_with_size (increment: int) (r: R.t): t =
            let buffer = ref (Bytes.create increment) in
            let len = ref 0
            and capacity = ref increment
            in
            let make_room () =
                if len = capacity then
                    let bnew = Bytes.create (!capacity + increment) in
                    begin
                        Bytes.blit !buffer 0 bnew 0 !len;
                        buffer := bnew;
                        capacity := !capacity + increment
                    end
            in
            let push c =
                make_room ();
                Bytes.set !buffer !len c;
                len := !len + 1;
            in
            let rec recurse r =
                if R.has_more r then
                    begin
                        push (R.peek r);
                        recurse (R.advance r)
                    end
                else
                    Bytes.sub_string !buffer 0 !len
            in
            recurse r

        let make (r: R.t): t =
            make_with_size 100 r
    end
  end

let%test _ =
    let str = "12345678901234567890123456789012" in
    let module From = String.From_readable (String.To_readable) in
    str = From.make (String.To_readable.make str)



module String_set = Set.Make(String)
module String_map = Finite_map.Make(String)


module Interval =
struct
    let find (p:int -> bool) (start:int) (beyond:int): int =
      let rec fnd i =
        if i = beyond || p i then
          i
        else
          fnd (i+1)
      in
      fnd start


    let exist (p: int -> bool) (start: int) (beyond: int): bool =
        find p start beyond <> beyond


    let forall (p: int -> bool) (start: int) (beyond: int): bool =
        let notp i = not (p i)
        in
        not (exist notp start beyond)


    let fold (a:'a) (f:int -> 'a -> 'a) (start:int) (beyond:int): 'a =
      assert (start <= beyond);
      let rec fold i a =
        if i = beyond then
          a

        else
          fold (i + 1) (f i a)
      in
      fold start a


    module Monadic (M: Module_types.MONAD) =
      struct
        let fold
              (f: int -> 'a -> 'a M.t)
              (start: int)
              (beyond: int)
              (a: 'a)
            : 'a M.t
          =
          assert (start <= beyond);
          let rec fold i a =
            if i = beyond then
              M.return a

            else
              M.(f i a >>= fold (i + 1))
          in

          fold start a
      end
end




module String_reader =
struct
    include Readable_printer.R

    let of_substring str start len =
        Readable_printer.(readable (substring str start len))


    let of_string str =
        Readable_printer.(readable (string str))
end



module Fill_reader =
  struct
    type t = {n:int; c:char}
    let has_more (r:t): bool =
      r.n > 0
    let peek (r:t): char =
      r.c
    let advance (r:t): t =
      {r with n = r.n - 1}
    let make (n:int) (c:char): t =
      {n;c}
  end




module Char_reader =
  struct
    type t = char option
    let make (c:char): t =
      Some c
    let has_more (cr:t): bool =
      cr <> None
    let peek (cr:t): char =
      match cr with
      | None -> assert false (* Illegal call! *)
      | Some c -> c
    let advance (_:t): t =
      None
  end
OCaml

Innovation. Community. Security.