let index_of_string ?(from=0) ?(sub_index=0) ?sub_length t ~sub =
let module With_exn = struct
exception Found of int
let f () =
let length_of_t = length t in
let from =
if from <= 0 then 0 else min length_of_t from in
let total_length_of_sub = length sub in
let sub_index =
if sub_index <= 0 then 0 else sub_index in
let sub_length =
let default = max 0 (total_length_of_sub - sub_index) in
match sub_length with
| None -> default
| Some s when s >= default -> default
| Some s when s < 0 -> 0
| Some s -> s
in
if from >= length_of_t then None
else if length_of_t = 0 then None
else if sub_length <= 0 then Some from
else
begin try
for i = 0 to length_of_t - from do
if compare_substring
(t, i + from, sub_length)
(sub, sub_index, sub_length) = 0
then raise (Found (i + from))
done;
None
with Found f -> Some f
end
end in
With_exn.f ()