let of_native_substring
~empty ~init ~on_new_character ~finalize
~read_character_from_native_string
s ~offset ~length =
if length = 0 then return empty
else
begin
(if offset + length > String.length s
then fail `out_of_bounds
else return ())
>>= fun () ->
let module With_exn = struct
exception WChar of int
let f buf =
let x = init () in
try
let rec loop index =
if index < offset + length
then
begin match read_character_from_native_string ~buf ~index with
| Some (s, size) when index + size <= offset + length ->
on_new_character x s;
loop (index + size)
| Some (_, _ )
| None -> raise (WChar index)
end
else ()
in
loop offset;
return (finalize x)
with
| WChar c -> fail (`wrong_char_at c)
end in
With_exn.f s
end