(S:
sig
type t
type character
val empty : t
val is_whitespace: character -> bool
val length: t -> int
val find:
?from:int -> ?length:int -> t -> f:(character -> bool) -> int option
val find_reverse:
?from:int -> ?length:int -> t -> f:(character -> bool) -> int option
val sub_exn: t -> index:int -> length:int -> t
end) = struct
let strip ?(on=`Both) ?(whitespace=S.is_whitespace) t =
let open S in
let first_non () =
match find t ~f:(fun c -> not (whitespace c)) with
| None -> raise Not_found | Some s -> s in
let last_non () =
match find_reverse t ~f:(fun c -> not (whitespace c)) with
| None -> raise Not_found | Some s -> s in
try
match on with
| `Both ->
let index = first_non () in
let last = last_non () in
sub_exn t ~index ~length:(last - index + 1)
| `Left ->
let index = first_non () in
sub_exn t ~index ~length:(length t - index)
| `Right ->
let last = last_non () in
sub_exn t ~index:0 ~length:(last + 1)
with
| Not_found -> empty
end