let utf_8_segments (boundary : Uuseg.boundary) (s : string) : string list =
let flush_segment (buf : Buffer.t) (acc : string list) : string list =
let segment : string = Buffer.contents buf in
let _ : unit = Buffer.clear buf in
if segment = "" then acc else segment :: acc
in
let rec add (buf : Buffer.t) (acc : string list) (segmenter : Uuseg.t) (v : [ `Await | `End | `Uchar of Uchar.t ]) : string list =
match ((Uuseg.add segmenter v) : Uuseg.ret) with
| `Uchar u ->
let _ : unit = Buffer.add_utf_8_uchar buf u in
add buf acc segmenter `Await
| `Boundary -> add buf (flush_segment buf acc) segmenter `Await
| `Await
| `End -> acc
in
let rec loop (buf : Buffer.t) (acc : string list) (s : string) (i : int) (max : int) (segmenter : Uuseg.t) : string list =
if i > max then flush_segment buf (add buf acc segmenter `End) else
let dec : Uchar.utf_decode = String.get_utf_8_uchar s i in
let acc : string list = add buf acc segmenter (`Uchar (Uchar.utf_decode_uchar dec)) in
loop buf acc s (i + Uchar.utf_decode_length dec) max segmenter
in
let buf : Buffer.t = Buffer.create 42 in
let segmenter : Uuseg.t = Uuseg.create boundary in
List.rev (loop buf [] s 0 (String.length s - 1) segmenter)