lines.ml 534 B

1234567891011121314151617181920
  1. open Bigarray
  2. let rec step arr dx look stop idx =
  3. match idx = stop with
  4. | true -> stop
  5. | _ -> (
  6. match arr.{idx + look} with
  7. | '\n' -> idx
  8. | _ -> step arr dx look stop (idx + dx) )
  9. (* walk back until we hit the begin of the line *)
  10. let back arr idx = step arr (-1) (-1) 0 idx
  11. (* walk forward until we hit the end of the line *)
  12. let forw arr len idx = step arr 1 0 len idx
  13. let line arr len idx =
  14. let off = back arr idx and pos = forw arr len idx in
  15. pos - off |> Array1.sub arr off |> Bigstring.to_string