My agentic slop goes here. Not intended for anyone else!
1(** Byte range management for partial downloads *)
2
3type t = {
4 start : int64;
5 end_ : int64; (* inclusive *)
6}
7
8let create ~start ~end_ =
9 if start < 0L || end_ < start then
10 invalid_arg (Printf.sprintf "Invalid range: %Ld-%Ld" start end_)
11 else
12 { start; end_ }
13
14let start r = r.start
15let end_ r = r.end_
16
17let length r =
18 Int64.succ (Int64.sub r.end_ r.start)
19
20let adjacent r1 r2 =
21 Int64.succ r1.end_ = r2.start || Int64.succ r2.end_ = r1.start
22
23let overlaps r1 r2 =
24 not (r1.end_ < r2.start || r2.end_ < r1.start)
25
26let contains r pos =
27 pos >= r.start && pos <= r.end_
28
29let merge r1 r2 =
30 if overlaps r1 r2 || adjacent r1 r2 then
31 Some {
32 start = min r1.start r2.start;
33 end_ = max r1.end_ r2.end_;
34 }
35 else
36 None
37
38let compare r1 r2 =
39 Int64.compare r1.start r2.start
40
41let to_string r =
42 Printf.sprintf "%Ld-%Ld" r.start r.end_
43
44let of_string s =
45 try
46 match String.split_on_char '-' s with
47 | [start_s; end_s] ->
48 let start = Int64.of_string start_s in
49 let end_ = Int64.of_string end_s in
50 Some (create ~start ~end_)
51 | _ -> None
52 with _ -> None
53
54let pp fmt r =
55 Format.fprintf fmt "[%Ld-%Ld]" r.start r.end_