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
open! Import
module Reader = struct
module T = struct
type t =
{ id : int
; name : string option [@ignore]
; length : int [@ignore]
; unsafe_get : int -> char [@ignore]
}
[@@deriving sexp]
let compare t1 t2 = if phys_equal t1 t2 then 0 else Int.compare t1.id t2.id
end
include T
include Comparable.Make (T)
let name r = Option.value r.name ~default:(Int.to_string r.id)
end
type string_source =
{ name : string option
; content : string
}
[@@deriving sexp]
let compare_string_source str_src1 str_src2 =
if phys_equal str_src1 str_src2
then 0
else (
match Option.compare String.compare str_src1.name str_src2.name with
| 0 -> String.compare str_src1.content str_src2.content
| n -> n)
;;
module T = struct
type t =
[ `File of string
| `String of string_source
| `Reader of Reader.t
]
[@@deriving sexp]
let compare t1 t2 =
if phys_equal t1 t2
then 0
else (
match t1, t2 with
| `File fname1, `File fname2 -> String.compare fname1 fname2
| `String str_src1, `String str_src2 -> compare_string_source str_src1 str_src2
| `Reader rd1, `Reader rd2 -> Reader.compare rd1 rd2
| _, _ ->
Stdlib.compare t1 t2)
;;
end
include T
include Comparable.Make (T)
let name = function
| `File name -> Some name
| `String { name; _ } -> name
| `Reader reader -> Some (Reader.name reader)
;;
let length = function
| `File filename ->
(try In_channel.(with_open_bin filename length) |> Int64.to_int with
| _ -> invalid_argf "file size is larger than an OCaml 63-bit integer")
| `String { content; _ } -> String.length content
| `Reader { Reader.length; _ } -> length
;;