let take_while_with_index t ~f =
if length t = 0 then empty
else (
let buf = make (length t) (S.get t 0) in
let rec loop idx =
match get t idx with
| Some c when f idx c -> S.set buf idx c; loop (idx + 1)
| _ -> idx
in
let new_length = loop 0 in
sub_exn buf ~index:0 ~length:new_length
)