let slice_exn ?(start=0) ?finish t =
let length_of_t = String.length t in
let bound_check strict m x =
let out_of_ub = if strict then x > length_of_t else x >= length_of_t in
if x < 0 || (not (is_empty t) && out_of_ub) then
ksprintf failwith "slice_exn: invalid %s %d" m x
else x
in
let _ = bound_check false "start" start
and finish =
match finish with
| None -> length_of_t
| Some f -> bound_check true "finish" f
in
sub_exn t ~index:start ~length:(finish - start)