diff --git a/core/CCString.ml b/core/CCString.ml index 2f5094f3..9c46b93c 100644 --- a/core/CCString.ml +++ b/core/CCString.ml @@ -41,10 +41,9 @@ module type S = sig (** {2 Conversions} *) val to_gen : t -> char gen - val to_seq : t -> char sequence - val to_klist : t -> char klist + val to_list : t -> char list val pp : Buffer.t -> t -> unit end @@ -59,6 +58,10 @@ let hash s = Hashtbl.hash s let length = String.length +let rec _to_list s acc i len = + if len=0 then List.rev acc + else _to_list s (s.[i]::acc) (i+1) (len-1) + let _is_sub ~sub i s j ~len = let rec check k = if k = len @@ -220,6 +223,26 @@ let of_klist l = let to_klist s = _to_klist s 0 (String.length s) +let to_list s = _to_list s [] 0 (String.length s) + +let of_list l = + let s = String.make (List.length l) ' ' in + List.iteri (fun i c -> s.[i] <- c) l; + s + +(*$T + of_list ['a'; 'b'; 'c'] = "abc" + of_list [] = "" +*) + +let of_array a = + let s = String.make (Array.length a) ' ' in + Array.iteri (fun i c -> s.[i] <- c) a; + s + +let to_array s = + Array.init (String.length s) (fun i -> s.[i]) + let pp buf s = Buffer.add_char buf '"'; Buffer.add_string buf s; @@ -252,6 +275,7 @@ module Sub = struct let to_seq (s,i,len) k = for i=i to i+len-1 do k s.[i] done let to_klist (s,i,len) = _to_klist s i len + let to_list (s,i,len) = _to_list s [] i len let pp buf (s,i,len) = Buffer.add_char buf '"'; diff --git a/core/CCString.mli b/core/CCString.mli index 83e72342..19fbe9fc 100644 --- a/core/CCString.mli +++ b/core/CCString.mli @@ -45,10 +45,9 @@ module type S = sig (** {2 Conversions} *) val to_gen : t -> char gen - val to_seq : t -> char sequence - val to_klist : t -> char klist + val to_list : t -> char list val pp : Buffer.t -> t -> unit end @@ -64,10 +63,12 @@ val compare : t -> t -> int val hash : t -> int val of_gen : char gen -> t - val of_seq : char sequence -> t - val of_klist : char klist -> t +val of_list : char list -> t +val of_array : char array -> t + +val to_array : t -> char array val find : ?start:int -> sub:t -> t -> int (** Find [sub] in the string, returns its first index or -1. diff --git a/tests/helpers.ml b/tests/helpers.ml index 2da169d1..76f66577 100644 --- a/tests/helpers.ml +++ b/tests/helpers.ml @@ -7,7 +7,6 @@ let print_int_list l = Buffer.contents b let print_int_int_list l = - let printer fmt (i,j) = Format.fprintf fmt "%d, %d" i j in let b = Buffer.create 20 in CCList.pp (CCPair.pp CCInt.pp CCInt.pp) b l; Buffer.contents b