mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
add various functions on CCUtf8_string
This commit is contained in:
parent
79089677af
commit
4a9b41c3cd
2 changed files with 41 additions and 0 deletions
|
|
@ -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 =
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue