conversions for CCString

This commit is contained in:
Simon Cruanes 2014-07-11 22:39:40 +02:00
parent af84e2dcc7
commit d7992d4a57
3 changed files with 31 additions and 7 deletions

View file

@ -41,10 +41,9 @@ module type S = sig
(** {2 Conversions} *) (** {2 Conversions} *)
val to_gen : t -> char gen val to_gen : t -> char gen
val to_seq : t -> char sequence val to_seq : t -> char sequence
val to_klist : t -> char klist val to_klist : t -> char klist
val to_list : t -> char list
val pp : Buffer.t -> t -> unit val pp : Buffer.t -> t -> unit
end end
@ -59,6 +58,10 @@ let hash s = Hashtbl.hash s
let length = String.length 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 _is_sub ~sub i s j ~len =
let rec check k = let rec check k =
if k = len if k = len
@ -220,6 +223,26 @@ let of_klist l =
let to_klist s = _to_klist s 0 (String.length s) 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 = let pp buf s =
Buffer.add_char buf '"'; Buffer.add_char buf '"';
Buffer.add_string buf s; Buffer.add_string buf s;
@ -252,6 +275,7 @@ module Sub = struct
let to_seq (s,i,len) k = let to_seq (s,i,len) k =
for i=i to i+len-1 do k s.[i] done 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_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) = let pp buf (s,i,len) =
Buffer.add_char buf '"'; Buffer.add_char buf '"';

View file

@ -45,10 +45,9 @@ module type S = sig
(** {2 Conversions} *) (** {2 Conversions} *)
val to_gen : t -> char gen val to_gen : t -> char gen
val to_seq : t -> char sequence val to_seq : t -> char sequence
val to_klist : t -> char klist val to_klist : t -> char klist
val to_list : t -> char list
val pp : Buffer.t -> t -> unit val pp : Buffer.t -> t -> unit
end end
@ -64,10 +63,12 @@ val compare : t -> t -> int
val hash : t -> int val hash : t -> int
val of_gen : char gen -> t val of_gen : char gen -> t
val of_seq : char sequence -> t val of_seq : char sequence -> t
val of_klist : char klist -> 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 val find : ?start:int -> sub:t -> t -> int
(** Find [sub] in the string, returns its first index or -1. (** Find [sub] in the string, returns its first index or -1.

View file

@ -7,7 +7,6 @@ let print_int_list l =
Buffer.contents b Buffer.contents b
let print_int_int_list l = 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 let b = Buffer.create 20 in
CCList.pp (CCPair.pp CCInt.pp CCInt.pp) b l; CCList.pp (CCPair.pp CCInt.pp CCInt.pp) b l;
Buffer.contents b Buffer.contents b