(S: T_LENGTH_SUB_AND_SEARCH) = struct
let split t ~on =
let length_of_t = S.length t in
begin match on with
| `Character c ->
let rec loop acc from =
match S.index_of_character t ~from c with
| Some index ->
loop (S.sub_exn t ~index:from ~length:(index - from) :: acc)
(index + 1)
| None ->
(S.sub_exn t ~index:from ~length:(length_of_t - from) :: acc)
in
List.rev (loop [] 0)
| `String s ->
let length_of_s = S.length s in
let rec loop acc from =
match S.index_of_string t ~from ~sub:s with
| Some index ->
loop (S.sub_exn t ~index:from ~length:(index - from) :: acc)
(index + length_of_s)
| None ->
(S.sub_exn t ~index:from ~length:(length_of_t - from) :: acc)
in
if length_of_s > 0
then List.rev (loop [] 0)
else if length_of_t = 0
then [ t ]
else begin
let res = ref [] in
for index = length_of_t - 1 downto 0 do
res := S.sub_exn t ~index ~length:1 :: !res
done;
!res
end
end
end