let index_of_string_reverse ?from ?(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 last = length_of_t - 1 in
let from =
match from with
| None -> last
| Some f when f >= last -> last
| Some f -> f 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 < 0 then None
else if length_of_t = 0 then None
else if sub_length <= 0 then Some from
else
begin try
for i = from downto 0 do
if compare_substring
(t, i, sub_length)
(sub, sub_index, sub_length) = 0
then raise (Found (i))
done;
None
with Found f -> Some f
end
end in
With_exn.f ()