add dune-file and .opam.
[xkcd936.git] / lib / lines.ml
blob1423e3ed290983abff2af3a54b95bcec575cb01b
1 open Bigarray
3 let rec step arr dx look stop idx =
4 if idx = stop
5 then stop
6 else
7 match arr.{idx + look} with
8 | '\n' -> idx
9 | _ -> step arr dx look stop (idx + dx)
11 (* walk back until we hit the begin of the line *)
12 let back arr idx = step arr (-1) (-1) 0 idx
14 (* walk forward until we hit the end of the line *)
15 let forw arr len idx = step arr 1 0 len idx
17 let line arr len idx =
18 let off = back arr idx
19 and pos = forw arr len idx in
20 pos - off |> Array1.sub arr off |> Bigstring.to_string