diff --git a/src/core/CCUtf8_string.ml b/src/core/CCUtf8_string.ml index 5cac7bb8..27e13898 100644 --- a/src/core/CCUtf8_string.ml +++ b/src/core/CCUtf8_string.ml @@ -29,6 +29,8 @@ module Dec = struct { s=s; i=idx; len=String.length s; } end +let n_bytes = length + exception Malformed of string * int (** Malformed string at given offset *) @@ -134,6 +136,8 @@ let fold ?idx f acc s = in aux acc +let n_chars = fold (fun x _ -> x+1) 0 + let to_list ?(idx=0) s : uchar list = 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; 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 is_valid (s:string) : bool = diff --git a/src/core/CCUtf8_string.mli b/src/core/CCUtf8_string.mli index 64843c18..9b90a990 100644 --- a/src/core/CCUtf8_string.mli +++ b/src/core/CCUtf8_string.mli @@ -53,6 +53,22 @@ val fold : ?idx:int -> ('a -> uchar -> 'a) -> 'a -> t -> 'a 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_gen : uchar gen -> t