add various functions on CCUtf8_string

This commit is contained in:
Simon Cruanes 2018-03-07 00:17:48 -06:00
parent 79089677af
commit 4a9b41c3cd
2 changed files with 41 additions and 0 deletions

View file

@ -29,6 +29,8 @@ module Dec = struct
{ s=s; i=idx; len=String.length s; } { s=s; i=idx; len=String.length s; }
end end
let n_bytes = length
exception Malformed of string * int exception Malformed of string * int
(** Malformed string at given offset *) (** Malformed string at given offset *)
@ -134,6 +136,8 @@ let fold ?idx f acc s =
in in
aux acc aux acc
let n_chars = fold (fun x _ -> x+1) 0
let to_list ?(idx=0) s : uchar list = let to_list ?(idx=0) s : uchar list =
fold ~idx (fun acc x -> x :: acc) [] s |> List.rev fold ~idx (fun acc x -> x :: acc) [] s |> List.rev
@ -185,6 +189,27 @@ let of_list l : t =
List.iter (code_to_string buf) l; List.iter (code_to_string buf) l;
Buffer.contents buf Buffer.contents buf
let map f s : t =
let buf = Buffer.create (n_bytes s) in
iter (fun c -> code_to_string buf (f c)) s;
Buffer.contents buf
let filter_map f s : t =
let buf = Buffer.create (n_bytes s) in
iter
(fun c -> match f c with
| None -> ()
| Some c -> code_to_string buf c)
s;
Buffer.contents buf
let flat_map f s : t =
let buf = Buffer.create (n_bytes s) in
iter (fun c -> iter (code_to_string buf) (f c)) s;
Buffer.contents buf
let append = Pervasives.(^)
let unsafe_of_string s = s let unsafe_of_string s = s
let is_valid (s:string) : bool = let is_valid (s:string) : bool =

View file

@ -53,6 +53,22 @@ val fold : ?idx:int -> ('a -> uchar -> 'a) -> 'a -> t -> 'a
val iter : ?idx:int -> (uchar -> unit) -> t -> unit val iter : ?idx:int -> (uchar -> unit) -> t -> unit
val n_chars : t -> int
(** Number of characters *)
val n_bytes : t -> int
(** Number of bytes *)
val map : (uchar -> uchar) -> t -> t
val filter_map : (uchar -> uchar option) -> t -> t
val flat_map : (uchar -> t) -> t -> t
val append : t -> t -> t
val concat : t -> t list -> t
val of_seq : uchar sequence -> t val of_seq : uchar sequence -> t
val of_gen : uchar gen -> t val of_gen : uchar gen -> t