diff --git a/README.md b/README.md index 638d48ba..3da70fbb 100644 --- a/README.md +++ b/README.md @@ -373,8 +373,8 @@ val map : string IntMap.t = (IntMap.pp CCFormat.int CCFormat.string_quoted) map;; map = - 1->"1", 2->"2", 3->"3", 4->"4", 5->"5", - 6->"6", 7->"7", 8->"8", 9->"9" + 1 -> "1", 2 -> "2", 3 -> "3", 4 -> "4", 5 + -> "5", 6 -> "6", 7 -> "7", 8 -> "8", 9 -> "9" - : unit = () # (* options are good *) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index baed3d57..fd1b6595 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -506,17 +506,23 @@ let random_non_empty g st = let n = 1 + Random.State.int st 1_000 in random_len n g st -let pp ?(sep=", ") pp_item out a = +let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun out () -> Format.fprintf out ",@ ") pp_item out a = + pp_start out (); for k = 0 to Array.length a-1 do - if k > 0 then (Format.pp_print_string out sep; Format.pp_print_cut out ()); + if k > 0 then pp_sep out (); pp_item out a.(k) - done + done; + pp_stop out () -let pp_i ?(sep=", ") pp_item out a = +let pp_i ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun out () -> Format.fprintf out ",@ ") pp_item out a = + pp_start out (); for k = 0 to Array.length a - 1 do - if k > 0 then (Format.pp_print_string out sep; Format.pp_print_cut out ()); + if k > 0 then pp_sep out (); pp_item k out a.(k) - done + done; + pp_stop out () let to_string ?(sep=", ") item_to_string a = Array.to_list a diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index 76e7842d..6620cddd 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -202,15 +202,22 @@ val to_gen : 'a t -> 'a gen (** {2 IO} *) -val pp: ?sep:string -> 'a printer -> 'a t printer -(** [pp ~sep pp_item ppf a] formats the array [a] on [ppf]. - Each element is formatted with [pp_item] and elements are separated - by [sep] (defaults to ", "). *) +val pp: ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + 'a printer -> 'a t printer +(** [pp ~pp_start ~pp_stop ~pp_sep pp_item ppf a] formats the array [a] on [ppf]. + Each element is formatted with [pp_item], [pp_start] is called at the beginning, + [pp_stop] is called at the end, [pp_sep] is called between each elements. + By defaults [pp_start] and [pp_stop] does nothing and [pp_sep] defaults to + (fun out -> Format.fprintf out ",@ "). *) -val pp_i: ?sep:string -> (int -> 'a printer) -> 'a t printer -(** [pp_i ~sep pp_item ppf a] prints the array [a] on [ppf]. +val pp_i: ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + (int -> 'a printer) -> 'a t printer +(** [pp_i ~pp_start ~pp_stop ~pp_sep pp_item ppf a] prints the array [a] on [ppf]. The printing function [pp_item] is giving both index and element. - Elements are separated by [sep] (defaults to ", "). *) + [pp_start] is called at the beginning, + [pp_stop] is called at the end, [pp_sep] is called between each elements. + By defaults [pp_start] and [pp_stop] does nothing and [pp_sep] defaults to + (fun out -> Format.fprintf out ",@ "). *) val rev : 'a t -> 'a t (** [rev a] copies the array [a] and reverses it in place. diff --git a/src/core/CCArrayLabels.mli b/src/core/CCArrayLabels.mli index d8043b96..4cde8cf4 100644 --- a/src/core/CCArrayLabels.mli +++ b/src/core/CCArrayLabels.mli @@ -208,15 +208,22 @@ val to_gen : 'a t -> 'a gen (** {2 IO} *) -val pp: ?sep:string -> 'a printer -> 'a t printer -(** [pp ~sep pp_item ppf a] formats the array [a] on [ppf]. - Each element is formatted with [pp_item] and elements are separated - by [sep] (defaults to ", "). *) +val pp: ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + 'a printer -> 'a t printer +(** [pp ~pp_start ~pp_stop ~pp_sep pp_item ppf a] formats the array [a] on [ppf]. + Each element is formatted with [pp_item], [pp_start] is called at the beginning, + [pp_stop] is called at the end, [pp_sep] is called between each elements. + By defaults [pp_start] and [pp_stop] does nothing and [pp_sep] defaults to + (fun out -> Format.fprintf out ",@ "). *) -val pp_i: ?sep:string -> (int -> 'a printer) -> 'a t printer -(** [pp_i ~sep pp_item ppf a] prints the array [a] on [ppf]. +val pp_i: ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + (int -> 'a printer) -> 'a t printer +(** [pp_i ~pp_start ~pp_stop ~pp_sep pp_item ppf a] prints the array [a] on [ppf]. The printing function [pp_item] is giving both index and element. - Elements are separated by [sep] (defaults to ", "). *) + [pp_start] is called at the beginning, + [pp_stop] is called at the end, [pp_sep] is called between each elements. + By defaults [pp_start] and [pp_stop] does nothing and [pp_sep] defaults to + (fun out -> Format.fprintf out ",@ "). *) val map2 : f:('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t (** [map2 ~f a b] applies function [f] to all elements of [a] and [b], diff --git a/src/core/CCHashtbl.ml b/src/core/CCHashtbl.ml index 9ba920dc..64b9145a 100644 --- a/src/core/CCHashtbl.ml +++ b/src/core/CCHashtbl.ml @@ -138,17 +138,19 @@ module Poly = struct () *) - let pp pp_k pp_v fmt m = - Format.fprintf fmt "@[tbl {@,"; + let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun fmt () -> Format.fprintf fmt ",@ ") + ?(pp_arrow=fun fmt () -> Format.fprintf fmt "@ -> ") pp_k pp_v fmt m = + pp_start fmt (); let first = ref true in Hashtbl.iter (fun k v -> - if !first then first := false else Format.fprintf fmt ",@ "; + if !first then first := false else pp_sep fmt (); pp_k fmt k; - Format.pp_print_string fmt " -> "; + pp_arrow fmt (); pp_v fmt v; ) m; - Format.fprintf fmt "@,}@]" + pp_stop fmt () end include Poly @@ -266,7 +268,8 @@ module type S = sig to [tbl] and [v] is returned. @since 1.0 *) - val pp : key printer -> 'a printer -> 'a t printer + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + ?pp_arrow:unit printer -> key printer -> 'a printer -> 'a t printer (** Printer for tables. Renamed from [print] since 2.0. @since 0.13 *) @@ -393,17 +396,18 @@ module Make(X : Hashtbl.HashedType) List.iter (fun (k,v) -> add tbl k v) l; tbl - let pp pp_k pp_v fmt m = - Format.fprintf fmt "@[tbl {@,"; + let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun fmt () -> Format.fprintf fmt ",@ ") + ?(pp_arrow=fun fmt () -> Format.fprintf fmt "@ -> ") pp_k pp_v fmt m = + pp_start fmt (); let first = ref true in iter (fun k v -> - if !first then first := false else Format.pp_print_string fmt ", "; + if !first then first := false else pp_sep fmt (); pp_k fmt k; - Format.pp_print_string fmt " -> "; + pp_arrow fmt (); pp_v fmt v; - Format.pp_print_cut fmt () ) m; - Format.fprintf fmt "}@]" + pp_stop fmt () end diff --git a/src/core/CCHashtbl.mli b/src/core/CCHashtbl.mli index 3ed4eefd..a7a4d4a0 100644 --- a/src/core/CCHashtbl.mli +++ b/src/core/CCHashtbl.mli @@ -132,9 +132,14 @@ module Poly : sig to [tbl] and [v] is returned. @since 1.0 *) - val pp : 'a printer -> 'b printer -> ('a, 'b) Hashtbl.t printer - (** [pp pp_k pp_v] returns a table printer given a [pp_k] printer + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + ?pp_arrow:unit printer -> 'a printer -> 'b printer -> ('a, 'b) Hashtbl.t printer + (** [pp ~pp_start ~pp_stop ~pp_sep ~pp arrow pp_k pp_v] returns a table printer + given a [pp_k] printer for individual key and a [pp_v] printer for individual value. + [pp_start] and [pp_stop] control the opening and closing delimiters, + by default print nothing. [pp_sep] control the separator between binding. + [pp_arrow] control the arrow between the key and value. Renamed from [print] since 2.0. @since 0.13 *) end @@ -260,9 +265,14 @@ module type S = sig to [tbl] and [v] is returned. @since 1.0 *) - val pp : key printer -> 'a printer -> 'a t printer - (** [pp pp_k pp_v] returns a table printer given a [pp_k] printer + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + ?pp_arrow:unit printer -> key printer -> 'a printer -> 'a t printer + (** [pp ~pp_start ~pp_stop ~pp_sep ~pp arrow pp_k pp_v] returns a table printer + given a [pp_k] printer for individual key and a [pp_v] printer for individual value. + [pp_start] and [pp_stop] control the opening and closing delimiters, + by default print nothing. [pp_sep] control the separator between binding. + [pp_arrow] control the arrow between the key and value. Renamed from [print] since 2.0. @since 0.13 *) end diff --git a/src/core/CCHeap.ml b/src/core/CCHeap.ml index 8042e03f..69d0cc64 100644 --- a/src/core/CCHeap.ml +++ b/src/core/CCHeap.ml @@ -213,7 +213,8 @@ module type S = sig (** Print the heap in a string @since 2.7 *) - val pp : ?sep:string -> elt printer -> t printer + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + elt printer -> t printer (** Printer. Renamed from {!print} since 2.0 @since 0.16 *) @@ -434,13 +435,16 @@ module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct = (List.sort Stdlib.compare l |> List.map string_of_int |> String.concat " ")) *) - let pp ?(sep=",") pp_elt out h = + let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun out () -> Format.fprintf out ",") pp_elt out h = let first=ref true in + pp_start out (); iter (fun x -> - if !first then first := false else Format.fprintf out "%s@," sep; + if !first then first := false else pp_sep out (); pp_elt out x) - h + h; + pp_stop out (); end module Make_from_compare(E : TOTAL_ORD) = diff --git a/src/core/CCHeap.mli b/src/core/CCHeap.mli index 35e71040..b3da17d4 100644 --- a/src/core/CCHeap.mli +++ b/src/core/CCHeap.mli @@ -164,9 +164,13 @@ module type S = sig (converted to a string using [f]). @since 2.7 *) - val pp : ?sep:string -> elt printer -> t printer - (** [pp ?sep ppf h] prints [h] on [ppf]. - Elements are separated by [sep] (default ","). + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + elt printer -> t printer + (** [pp ?pp_start ?pp_stop ?pp_sep ppf h] prints [h] on [ppf]. + Each element is formatted with [ppf], [pp_start] is called at the beginning, + [pp_stop] is called at the end, [pp_sep] is called between each elements. + By defaults [pp_start] and [pp_stop] does nothing and [pp_sep] defaults to + (fun out -> Format.fprintf out ",@ "). Renamed from {!print} since 2.0 @since 0.16 *) end diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 362e7689..84ec3402 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1790,24 +1790,25 @@ include Infix (** {2 IO} *) -let pp ?(start="") ?(stop="") ?(sep=", ") pp_item fmt l = +let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun fmt () ->Format.fprintf fmt ",@ ") pp_item fmt l = let rec print fmt l = match l with | x::((_::_) as l) -> pp_item fmt x; - Format.pp_print_string fmt sep; - Format.pp_print_cut fmt (); + pp_sep fmt (); print fmt l | x::[] -> pp_item fmt x | [] -> () in - Format.pp_print_string fmt start; + pp_start fmt (); print fmt l; - Format.pp_print_string fmt stop + pp_stop fmt () (*$= & ~printer:(fun s->s) "[1, 2, 3]" \ (CCFormat.to_string \ - (CCFormat.hbox(CCList.pp ~start:"[" ~stop:"]" CCFormat.int)) \ + (CCFormat.hbox(CCList.pp ~pp_start:(fun fmt () -> Format.fprintf fmt "[") \ + ~pp_stop:(fun fmt () -> Format.fprintf fmt "]") CCFormat.int)) \ [1;2;3]) *) diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 7863d77e..f273181f 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -822,6 +822,6 @@ include CCShimsMkLet_.S with type 'a t_let := 'a list (** {2 IO} *) -val pp : ?start:string -> ?stop:string -> ?sep:string -> +val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> 'a printer -> 'a t printer -(** [pp ?start ?stop ?sep ppf l] prints the contents of a list. *) +(** [pp ?pp_start ?pp_stop ?pp_sep ppf l] prints the contents of a list. *) diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index 083b3b4a..761047f3 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -784,7 +784,7 @@ val to_gen : 'a t -> 'a gen val of_gen : 'a gen -> 'a t (** [of_gen gen] builds a list from a given [gen]. In the result, elements appear in the same order as they did in the source [gen]. *) - + (** {2 Infix Operators} It is convenient to {!open CCList.Infix} to access the infix operators without cluttering the scope too much. @@ -798,7 +798,7 @@ module Infix : sig val (@) : 'a t -> 'a t -> 'a t (** [l1 @ l2] concatenates two lists [l1] and [l2]. As {!append}. *) - + val (<*>) : ('a -> 'b) t -> 'a t -> 'b t (** [funs <*> l] is [product (fun f x -> f x) funs l]. *) @@ -826,6 +826,6 @@ include CCShimsMkLet_.S with type 'a t_let := 'a list (** {2 IO} *) -val pp : ?start:string -> ?stop:string -> ?sep:string -> +val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> 'a printer -> 'a t printer -(** [pp ?start ?stop ?sep ppf l] prints the contents of a list. *) +(** [pp ?pp_start ?pp_stop ?pp_sep ppf l] prints the contents of a list. *) diff --git a/src/core/CCMap.ml b/src/core/CCMap.ml index 71569760..d5283f88 100644 --- a/src/core/CCMap.ml +++ b/src/core/CCMap.ml @@ -99,9 +99,8 @@ module type S = sig val to_list : 'a t -> (key * 'a) list - val pp : - ?start:string -> ?stop:string -> ?arrow:string -> ?sep:string -> - key printer -> 'a printer -> 'a t printer + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_arrow:unit printer -> + ?pp_sep:unit printer -> key printer -> 'a printer -> 'a t printer end module Make(O : Map.OrderedType) = struct @@ -223,19 +222,18 @@ module Make(O : Map.OrderedType) = struct let to_list m = fold (fun k v acc -> (k,v)::acc) m [] - let pp ?(start="") ?(stop="") ?(arrow="->") ?(sep=", ") pp_k pp_v fmt m = - Format.pp_print_string fmt start; + let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_arrow=fun fmt () -> Format.fprintf fmt "@ -> ") + ?(pp_sep=fun fmt () -> Format.fprintf fmt ",@ ") pp_k pp_v fmt m = + pp_start fmt (); let first = ref true in iter (fun k v -> if !first then first := false - else ( - Format.pp_print_string fmt sep; - Format.pp_print_cut fmt () - ); + else pp_sep fmt (); pp_k fmt k; - Format.pp_print_string fmt arrow; + pp_arrow fmt (); pp_v fmt v) m; - Format.pp_print_string fmt stop + pp_stop fmt () end diff --git a/src/core/CCMap.mli b/src/core/CCMap.mli index 39bc0bc7..84f1d24a 100644 --- a/src/core/CCMap.mli +++ b/src/core/CCMap.mli @@ -128,10 +128,10 @@ module type S = sig val to_list : 'a t -> (key * 'a) list (** [to_list m] builds a list of the bindings of the given map [m]. *) - val pp : - ?start:string -> ?stop:string -> ?arrow:string -> ?sep:string -> - key printer -> 'a printer -> 'a t printer - (** [pp ?start ?stop ?arrow ?sep pp_key pp_v m] pretty-prints the contents of the map. *) + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_arrow:unit printer -> + ?pp_sep:unit printer -> key printer -> 'a printer -> 'a t printer + (** [pp ?pp_start ?pp_stop ?pp_arrow ?pp_sep pp_key pp_v m] pretty-prints the + contents of the map. *) end module Make(O : Map.OrderedType) : S diff --git a/src/core/CCPair.ml b/src/core/CCPair.ml index ea1a9a2c..329c6e82 100644 --- a/src/core/CCPair.ml +++ b/src/core/CCPair.ml @@ -47,5 +47,10 @@ let to_string ?(sep=", ") a_to_string b_to_string (x,y) = type 'a printer = Format.formatter -> 'a -> unit -let pp ?(sep=", ") pa pb out (x,y) = - Format.fprintf out "%a%s@,%a" pa x sep pb y +let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun out () -> Format.fprintf out ",@ ") pa pb out (x,y) = + pp_start out (); + pa out x; + pp_sep out (); + pb out y; + pp_stop out () diff --git a/src/core/CCPair.mli b/src/core/CCPair.mli index b82ecfa8..a105767c 100644 --- a/src/core/CCPair.mli +++ b/src/core/CCPair.mli @@ -73,5 +73,7 @@ val to_string : ?sep:string -> ('a -> string) -> ('b -> string) -> ('a * 'b) -> type 'a printer = Format.formatter -> 'a -> unit -val pp : ?sep:string -> 'a printer -> 'b printer -> ('a * 'b) printer -(** Print a pair given an optional separator and a method for printing each of its elements. *) +val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + 'a printer -> 'b printer -> ('a * 'b) printer +(** Print a pair given an optional separator, an optional start and stop and a + method for printing each of its elements. *) diff --git a/src/core/CCSeq.ml b/src/core/CCSeq.ml index 69221e47..1b3aacd9 100644 --- a/src/core/CCSeq.ml +++ b/src/core/CCSeq.ml @@ -527,15 +527,19 @@ end (** {2 IO} *) -let pp ?(sep=",") pp_item fmt l = +let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun out () -> Format.fprintf out ",@ ") pp_item fmt l = + pp_start fmt (); let rec pp fmt l = match l() with | Nil -> () | Cons (x,l') -> - Format.pp_print_string fmt sep; + pp_sep fmt (); Format.pp_print_cut fmt (); pp_item fmt x; pp fmt l' in - match l() with + begin match l() with | Nil -> () | Cons (x,l') -> pp_item fmt x; pp fmt l' + end; + pp_stop fmt () diff --git a/src/core/CCSeq.mli b/src/core/CCSeq.mli index 7fda8dee..c2ea70b8 100644 --- a/src/core/CCSeq.mli +++ b/src/core/CCSeq.mli @@ -272,6 +272,10 @@ val of_gen : 'a gen -> 'a t (** {2 IO} *) -val pp : ?sep:string -> 'a printer -> 'a t printer -(** Print the list with the given separator (default ","). - Do not print opening/closing delimiters. *) +val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + 'a printer -> 'a t printer +(** [pp ~pp_start ~pp_stop ~pp_sep pp_item ppf s] formats the sequence [s] on [ppf]. + Each element is formatted with [pp_item], [pp_start] is called at the beginning, + [pp_stop] is called at the end, [pp_sep] is called between each elements. + By defaults [pp_start] and [pp_stop] does nothing and [pp_sep] defaults to + (fun out -> Format.fprintf out ",@ "). *) diff --git a/src/core/CCSet.ml b/src/core/CCSet.ml index ae4c3039..c801a740 100644 --- a/src/core/CCSet.ml +++ b/src/core/CCSet.ml @@ -81,8 +81,7 @@ module type S = sig (** Print the set in a string @since 2.7 *) - val pp : - ?start:string -> ?stop:string -> ?sep:string -> + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> elt printer -> t printer (** Print the set. *) end @@ -182,17 +181,15 @@ module Make(O : Map.OrderedType) = struct |> List.map string_of_int |> String.concat " ")) *) - let pp ?(start="") ?(stop="") ?(sep=", ") pp_x fmt m = - Format.pp_print_string fmt start; + let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun fmt () -> Format.fprintf fmt ",@ ") pp_x fmt m = + pp_start fmt (); let first = ref true in iter (fun x -> if !first then first := false - else ( - Format.pp_print_string fmt sep; - Format.pp_print_cut fmt () - ); + else pp_sep fmt (); pp_x fmt x) m; - Format.pp_print_string fmt stop + pp_stop fmt () end diff --git a/src/core/CCSet.mli b/src/core/CCSet.mli index 6779e710..9cc84d74 100644 --- a/src/core/CCSet.mli +++ b/src/core/CCSet.mli @@ -73,14 +73,12 @@ module type S = sig val to_list : t -> elt list (** [to_list t] converts the set [t] to a list of the elements. *) - val to_string : - ?start:string -> ?stop:string -> ?sep:string -> + val to_string : ?start:string -> ?stop:string -> ?sep:string -> (elt -> string) -> t -> string (** Print the set in a string @since 2.7 *) - val pp : - ?start:string -> ?stop:string -> ?sep:string -> + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> elt printer -> t printer (** Print the set. *) end diff --git a/src/core/CCVector.ml b/src/core/CCVector.ml index 39f602ad..b2588a37 100644 --- a/src/core/CCVector.ml +++ b/src/core/CCVector.ml @@ -1069,14 +1069,15 @@ let to_gen v = let to_string ?(start="") ?(stop="") ?(sep=", ") item_to_string v = start ^ (to_list v |> List.map item_to_string |> String.concat sep) ^ stop -let pp ?(start="") ?(stop="") ?(sep=", ") pp_item fmt v = - Format.pp_print_string fmt start; +let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun fmt () -> Format.fprintf fmt ",@ ") pp_item fmt v = + pp_start fmt (); iteri (fun i x -> - if i > 0 then (Format.pp_print_string fmt sep; Format.pp_print_cut fmt()); + if i > 0 then pp_sep fmt (); pp_item fmt x ) v; - Format.pp_print_string fmt stop + pp_stop fmt () include CCShimsMkLet_.Make2(struct type nonrec ('a,'e) t = ('a,'e) t diff --git a/src/core/CCVector.mli b/src/core/CCVector.mli index 037bbec4..f799e956 100644 --- a/src/core/CCVector.mli +++ b/src/core/CCVector.mli @@ -347,8 +347,13 @@ val to_string : (** Print the vector in a string @since 2.7 *) -val pp : ?start:string -> ?stop:string -> ?sep:string -> +val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> 'a printer -> ('a,_) t printer +(** [pp ~pp_start ~pp_stop ~pp_sep pp_item ppf v] formats the vector [v] on [ppf]. + Each element is formatted with [pp_item], [pp_start] is called at the beginning, + [pp_stop] is called at the end, [pp_sep] is called between each elements. + By defaults [pp_start] and [pp_stop] does nothing and [pp_sep] defaults to + (fun out -> Format.fprintf out ",@ "). *) (** Let operators on OCaml >= 4.08.0, nothing otherwise @since 2.8 *) diff --git a/src/data/CCHashSet.ml b/src/data/CCHashSet.ml index a120661f..b5d6ad50 100644 --- a/src/data/CCHashSet.ml +++ b/src/data/CCHashSet.ml @@ -85,7 +85,7 @@ module type S = sig val add_iter : t -> elt iter -> unit - val pp : ?sep:string -> elt printer -> t printer + val pp : ?pp_sep:unit printer -> elt printer -> t printer (** [pp pp_elt] returns a set printer, given a printer for individual elements *) end @@ -221,17 +221,14 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct seq (insert s); s - let pp ?(sep=",") pp_x out s = + let pp ?(pp_sep=fun out () -> Format.fprintf out ",@ ") pp_x out s = Format.pp_print_string out "{"; let first = ref true in Tbl.iter (fun x _ -> if !first then first := false - else ( - Format.pp_print_string out sep; - Format.pp_print_cut out (); - ); + else pp_sep out (); pp_x out x ) s; Format.pp_print_string out "}" diff --git a/src/data/CCHashSet.mli b/src/data/CCHashSet.mli index b0dd376a..6bf97dcf 100644 --- a/src/data/CCHashSet.mli +++ b/src/data/CCHashSet.mli @@ -89,7 +89,7 @@ module type S = sig val add_iter : t -> elt iter -> unit - val pp : ?sep:string -> elt printer -> t printer + val pp : ?pp_sep:unit printer -> elt printer -> t printer (** [pp pp_elt] returns a set printer, given a printer for individual elements. *) end diff --git a/src/data/CCImmutArray.ml b/src/data/CCImmutArray.ml index 86144911..fca18714 100644 --- a/src/data/CCImmutArray.ml +++ b/src/data/CCImmutArray.ml @@ -118,14 +118,11 @@ let to_gen a = type 'a printer = Format.formatter -> 'a -> unit -let pp ?(start="") ?(stop="") ?(sep=", ") pp_item out a = - Format.pp_print_string out start; +let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun out () -> Format.fprintf out ",@ ") pp_item out a = + pp_start out (); for k = 0 to Array.length a - 1 do - if k > 0 then ( - Format.pp_print_string out sep; - Format.pp_print_cut out () - ); + if k > 0 then pp_sep out (); pp_item out a.(k) done; - Format.pp_print_string out stop; - () + pp_stop out () diff --git a/src/data/CCImmutArray.mli b/src/data/CCImmutArray.mli index b9bceb78..ae97d01e 100644 --- a/src/data/CCImmutArray.mli +++ b/src/data/CCImmutArray.mli @@ -88,7 +88,12 @@ val to_gen : 'a t -> 'a gen type 'a printer = Format.formatter -> 'a -> unit -val pp : - ?start:string -> ?stop:string -> ?sep:string -> +val pp: ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> 'a printer -> 'a t printer +(** [pp ~pp_start ~pp_stop ~pp_sep pp_item ppf a] formats the array [a] on [ppf]. + Each element is formatted with [pp_item], [pp_start] is called at the beginning, + [pp_stop] is called at the end, [pp_sep] is called between each elements. + By defaults [pp_start] and [pp_stop] does nothing and [pp_sep] defaults to + (fun out -> Format.fprintf out ",@ "). *) + diff --git a/src/data/CCPersistentHashtbl.ml b/src/data/CCPersistentHashtbl.ml index af9ff54f..b682b193 100644 --- a/src/data/CCPersistentHashtbl.ml +++ b/src/data/CCPersistentHashtbl.ml @@ -110,7 +110,8 @@ module type S = sig val equal : 'a equal -> 'a t equal - val pp : ?sep:string -> ?arrow:string -> key printer -> 'a printer -> 'a t printer + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + ?pp_arrow:unit printer -> key printer -> 'a printer -> 'a t printer val stats : _ t -> Hashtbl.statistics (** Statistics on the internal table. @@ -633,15 +634,20 @@ module Make(H : HashedType) : S with type key = H.t = struct | Some v' -> eq v v' ) t1 - let pp ?(sep=",") ?(arrow="->") pp_k pp_v fmt t = + let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_sep=fun fmt () -> Format.fprintf fmt ",@ ") + ?(pp_arrow=fun fmt () -> Format.fprintf fmt "@ -> ") pp_k pp_v fmt t = let first = ref true in + pp_start fmt (); iter t (fun k v -> if !first then first:=false - else (Format.pp_print_string fmt sep; Format.pp_print_cut fmt ()); - Format.fprintf fmt "%a %s %a" pp_k k arrow pp_v v + else pp_sep fmt (); + pp_k fmt k; + pp_arrow fmt (); + pp_v fmt v ); - () + pp_stop fmt () let stats t = let a = reroot_ t in diff --git a/src/data/CCPersistentHashtbl.mli b/src/data/CCPersistentHashtbl.mli index d7af21d0..efe5a619 100644 --- a/src/data/CCPersistentHashtbl.mli +++ b/src/data/CCPersistentHashtbl.mli @@ -117,7 +117,8 @@ module type S = sig val equal : 'a equal -> 'a t equal - val pp : ?sep:string -> ?arrow:string -> key printer -> 'a printer -> 'a t printer + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> + ?pp_arrow:unit printer -> key printer -> 'a printer -> 'a t printer val stats : _ t -> Hashtbl.statistics (** Statistics on the internal table. diff --git a/src/data/CCRAL.ml b/src/data/CCRAL.ml index 43594e6c..6daeda1c 100644 --- a/src/data/CCRAL.ml +++ b/src/data/CCRAL.ml @@ -624,14 +624,11 @@ include Infix type 'a printer = Format.formatter -> 'a -> unit -let pp ?(sep=", ") pp_item fmt l = +let pp ?(pp_sep=fun fmt () -> Format.fprintf fmt ",@ ") pp_item fmt l = let first = ref true in iter l ~f:(fun x -> - if !first then first := false else ( - Format.pp_print_string fmt sep; - Format.pp_print_cut fmt (); - ); + if !first then first := false else pp_sep fmt (); pp_item fmt x ); () diff --git a/src/data/CCRAL.mli b/src/data/CCRAL.mli index 1abdc2bb..6f5bcbad 100644 --- a/src/data/CCRAL.mli +++ b/src/data/CCRAL.mli @@ -187,4 +187,4 @@ include module type of Infix type 'a printer = Format.formatter -> 'a -> unit -val pp : ?sep:string -> 'a printer -> 'a t printer +val pp : ?pp_sep:unit printer -> 'a printer -> 'a t printer diff --git a/src/data/CCWBTree.ml b/src/data/CCWBTree.ml index 39aa4f6a..de659897 100644 --- a/src/data/CCWBTree.ml +++ b/src/data/CCWBTree.ml @@ -161,7 +161,8 @@ module type S = sig val to_gen : 'a t -> (key * 'a) gen - val pp : key printer -> 'a printer -> 'a t printer + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_arrow:unit printer -> + ?pp_sep:unit printer -> key printer -> 'a printer -> 'a t printer (**/**) val node_ : key -> 'a -> 'a t -> 'a t -> 'a t @@ -588,19 +589,21 @@ module MakeFull(K : KEY) : S with type key = K.t = struct Some (k,v) in next - let pp pp_k pp_v fmt m = - let start = "[" and stop = "]" and arrow = "->" and sep = ","in - Format.pp_print_string fmt start; + let pp ?(pp_start=fun _ () -> ()) ?(pp_stop=fun _ () -> ()) + ?(pp_arrow=fun fmt () -> Format.fprintf fmt "@ -> ") + ?(pp_sep=fun fmt () -> Format.fprintf fmt ",@ ") + pp_k pp_v fmt m = + pp_start fmt (); let first = ref true in iter m ~f:(fun k v -> - if !first then first := false else Format.pp_print_string fmt sep; + if !first then first := false else pp_sep fmt (); pp_k fmt k; - Format.pp_print_string fmt arrow; + pp_arrow fmt (); pp_v fmt v; Format.pp_print_cut fmt () ); - Format.pp_print_string fmt stop + pp_stop fmt (); end module Make(X : ORD) = MakeFull(struct diff --git a/src/data/CCWBTree.mli b/src/data/CCWBTree.mli index 71e0a719..4518ff81 100644 --- a/src/data/CCWBTree.mli +++ b/src/data/CCWBTree.mli @@ -126,7 +126,8 @@ module type S = sig val to_gen : 'a t -> (key * 'a) gen - val pp : key printer -> 'a printer -> 'a t printer + val pp : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_arrow:unit printer -> + ?pp_sep:unit printer -> key printer -> 'a printer -> 'a t printer (** Renamed from [val print]. @since 2.0 *)