break: change pp functions to take unit printer for sep/stop/start

sep/stop/start -> pp_sep/pp_stop/pp_start
string -> unit printer
This commit is contained in:
Fardale 2020-07-27 00:34:37 +02:00 committed by Simon Cruanes
parent 4ac67a7518
commit 01da25cead
31 changed files with 201 additions and 137 deletions

View file

@ -373,8 +373,8 @@ val map : string IntMap.t = <abstr>
(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 *)

View file

@ -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

View file

@ -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.

View file

@ -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],

View file

@ -138,17 +138,19 @@ module Poly = struct
()
*)
let pp pp_k pp_v fmt m =
Format.fprintf fmt "@[<hov2>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 "@[<hov2>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

View file

@ -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

View file

@ -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) =

View file

@ -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

View file

@ -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])
*)

View file

@ -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. *)

View file

@ -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. *)

View file

@ -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

View file

@ -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

View file

@ -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 ()

View file

@ -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. *)

View file

@ -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 ()

View file

@ -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 ",@ "). *)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 *)

View file

@ -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 "}"

View file

@ -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

View file

@ -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 ()

View file

@ -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 ",@ "). *)

View file

@ -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

View file

@ -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.

View file

@ -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
);
()

View file

@ -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

View file

@ -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

View file

@ -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 *)