let read_from_native_string ~buf ~index =
try
let first_char = buf.[index] |> int_of_char in
let size, mask =
if first_char lsr 7 = 0 then (1, 0b0111_1111)
else if first_char lsr 5 = 0b110 then (2, 0b0001_1111)
else if first_char lsr 4 = 0b1110 then (3, 0b0000_1111)
else if first_char lsr 3 = 0b11110 then (4, 0b0000_0111)
else if first_char lsr 2 = 0b111110 then (5, 0b0000_0011)
else if first_char lsr 1 = 0b1111110 then (6, 0b0000_0001)
else raise Not_found
in
let the_int = ref (first_char land mask) in
for i = 1 to size - 1 do
let the_char = buf.[index + i] |> int_of_char in
if (the_char lsr 6) = 0b10
then (
the_int := (!the_int lsl 6) lor (the_char land 0b0011_1111);
) else raise Not_found;
done;
Some (!the_int, size)
with _ -> None