From a8c8561a83aa50173edb013c6ab8b5fe3e7aef92 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 12 Aug 2015 00:10:27 +0200 Subject: [PATCH] add more printers --- src/bigarray/CCBigstring.ml | 12 ++++++++++++ src/bigarray/CCBigstring.mli | 4 ++++ src/data/CCFQueue.ml | 11 +++++++++++ src/data/CCFQueue.mli | 3 +++ src/data/CCIntMap.ml | 14 ++++++++++++++ src/data/CCIntMap.mli | 6 ++++++ src/data/CCPersistentArray.ml | 10 ++++++++++ src/data/CCPersistentArray.mli | 4 ++++ 8 files changed, 64 insertions(+) diff --git a/src/bigarray/CCBigstring.ml b/src/bigarray/CCBigstring.ml index 2e0cee84..093466c7 100644 --- a/src/bigarray/CCBigstring.ml +++ b/src/bigarray/CCBigstring.ml @@ -179,6 +179,7 @@ let blit_of_string a i b j len = type 'a gen = unit -> 'a option type 'a sequence = ('a -> unit) -> unit +type 'a printer = Format.formatter -> 'a -> unit let to_seq a k = iter k a @@ -203,6 +204,17 @@ let to_seq_slice a i len = let to_gen_slice a i len = to_gen (sub a i len) +let print out s = + Format.pp_print_string out "bigstring \""; + iter + (function + | '\n' -> Format.pp_print_string out "\\n" + | '\t' -> Format.pp_print_string out "\\t" + | '\\' -> Format.pp_print_string out "\\\\" + | c -> Format.pp_print_char out c + ) s; + Format.pp_print_char out '"' + (** {2 Memory-map} *) let map_file_descr ?pos ?(shared=false) fd len = diff --git a/src/bigarray/CCBigstring.mli b/src/bigarray/CCBigstring.mli index dbd6ebc9..75d488fa 100644 --- a/src/bigarray/CCBigstring.mli +++ b/src/bigarray/CCBigstring.mli @@ -99,6 +99,7 @@ val blit_of_string : string -> int -> t -> int -> int -> unit type 'a gen = unit -> 'a option type 'a sequence = ('a -> unit) -> unit +type 'a printer = Format.formatter -> 'a -> unit val to_seq : t -> char sequence @@ -108,6 +109,9 @@ val to_seq_slice : t -> int -> int -> char sequence val to_gen_slice : t -> int -> int -> char gen +val print : t printer +(** @since NEXT_RELEASE *) + (** {2 Memory-map} *) val with_map_file : diff --git a/src/data/CCFQueue.ml b/src/data/CCFQueue.ml index 0f828d8c..ba44bc2e 100644 --- a/src/data/CCFQueue.ml +++ b/src/data/CCFQueue.ml @@ -28,6 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. type 'a sequence = ('a -> unit) -> unit type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist] type 'a equal = 'a -> 'a -> bool +type 'a printer = Format.formatter -> 'a -> unit (** {2 Basics} *) @@ -465,3 +466,13 @@ let (--) a b = 0 -- 0 |> to_list = [0] *) +let print pp_x out d = + let first = ref true in + Format.fprintf out "@[queue {"; + iter + (fun x -> + if !first then first:= false else Format.fprintf out ";@ "; + pp_x out x + ) d; + Format.fprintf out "}@]" + diff --git a/src/data/CCFQueue.mli b/src/data/CCFQueue.mli index aac4a484..43020cc2 100644 --- a/src/data/CCFQueue.mli +++ b/src/data/CCFQueue.mli @@ -28,6 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. type 'a sequence = ('a -> unit) -> unit type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist] type 'a equal = 'a -> 'a -> bool +type 'a printer = Format.formatter -> 'a -> unit (** {2 Basics} *) @@ -148,3 +149,5 @@ val (--) : int -> int -> int t (** [a -- b] is the integer range from [a] to [b], both included. @since 0.10 *) +val print : 'a printer -> 'a t printer +(** @since NEXT_RELEASE *) diff --git a/src/data/CCIntMap.ml b/src/data/CCIntMap.ml index c3fecc7f..68e581d6 100644 --- a/src/data/CCIntMap.ml +++ b/src/data/CCIntMap.ml @@ -274,3 +274,17 @@ let rec as_tree t () = match t with | L (k, v) -> `Node (`Leaf (k, v), []) | N (prefix, switch, l, r) -> `Node (`Node (prefix, switch), [as_tree l; as_tree r]) + +type 'a printer = Format.formatter -> 'a -> unit + +let print pp_x out m = + Format.fprintf out "@[intmap {@,"; + let first = ref true in + iter + (fun k v -> + if !first then first := false else Format.pp_print_string out ", "; + Format.fprintf out "%d -> " k; + pp_x out v; + Format.pp_print_cut out () + ) m; + Format.fprintf out "}@]" diff --git a/src/data/CCIntMap.mli b/src/data/CCIntMap.mli index 61a78c00..970bf851 100644 --- a/src/data/CCIntMap.mli +++ b/src/data/CCIntMap.mli @@ -94,3 +94,9 @@ val highest_bit : int -> int type 'a tree = unit -> [`Nil | `Node of 'a * 'a tree list] val as_tree : 'a t -> [`Node of int * int | `Leaf of int * 'a ] tree + +type 'a printer = Format.formatter -> 'a -> unit + +val print : 'a printer -> 'a t printer +(** @since NEXT_RELEASE *) + diff --git a/src/data/CCPersistentArray.ml b/src/data/CCPersistentArray.ml index f674cc22..4929eed4 100644 --- a/src/data/CCPersistentArray.ml +++ b/src/data/CCPersistentArray.ml @@ -89,4 +89,14 @@ let of_seq seq = seq (fun x -> l := x :: !l); of_list (List.rev !l) +type 'a printer = Format.formatter -> 'a -> unit + +let print pp_item out v = + Format.fprintf out "[|"; + iteri + (fun i x -> + if i > 0 then Format.fprintf out ";@ "; + pp_item out x + ) v; + Format.fprintf out "|]" diff --git a/src/data/CCPersistentArray.mli b/src/data/CCPersistentArray.mli index ae0bebfd..57f7fb64 100644 --- a/src/data/CCPersistentArray.mli +++ b/src/data/CCPersistentArray.mli @@ -102,4 +102,8 @@ val to_seq : 'a t -> 'a sequence val of_seq : 'a sequence -> 'a t +type 'a printer = Format.formatter -> 'a -> unit + +val print : 'a printer -> 'a t printer +(** @since NEXT_RELEASE *)