remove buffer printers, rename pretty-printers to pp

This commit is contained in:
Simon Cruanes 2016-11-03 18:24:11 +01:00
parent d4d7bc1de2
commit 13b283a91d
32 changed files with 99 additions and 281 deletions

View file

@ -9,6 +9,7 @@ type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int type 'a ord = 'a -> 'a -> int
type 'a random_gen = Random.State.t -> 'a type 'a random_gen = Random.State.t -> 'a
type 'a printer = Format.formatter -> 'a -> unit
module type S = sig module type S = sig
type 'a t type 'a t
@ -155,17 +156,11 @@ module type S = sig
(** {2 IO} *) (** {2 IO} *)
val pp: ?sep:string -> (Buffer.t -> 'a -> unit) -> val pp: ?sep:string -> 'a printer -> 'a t printer
Buffer.t -> 'a t -> unit
(** Print an array of items with printing function *) (** Print an array of items with printing function *)
val pp_i: ?sep:string -> (Buffer.t -> int -> 'a -> unit) -> val pp_i: ?sep:string -> (int -> 'a printer) -> 'a t printer
Buffer.t -> 'a t -> unit
(** Print an array, giving the printing function both index and item *) (** Print an array, giving the printing function both index and item *)
val print : ?sep:string -> (Format.formatter -> 'a -> unit) ->
Format.formatter -> 'a t -> unit
(** Print an array of items with printing function *)
end end
(** {2 General Implementation} (** {2 General Implementation}
@ -299,22 +294,16 @@ let _choose a i j st =
if i>=j then raise Not_found; if i>=j then raise Not_found;
a.(i+Random.State.int st (j-i)) a.(i+Random.State.int st (j-i))
let _pp ~sep pp_item buf a i j = let _pp ~sep pp_item out a i j =
for k = i to j - 1 do for k = i to j - 1 do
if k > i then Buffer.add_string buf sep; if k > i then (Format.pp_print_string out sep; Format.pp_print_cut out ());
pp_item buf a.(k) pp_item out a.(k)
done done
let _pp_i ~sep pp_item buf a i j = let _pp_i ~sep pp_item out a i j =
for k = i to j - 1 do for k = i to j - 1 do
if k > i then Buffer.add_string buf sep; if k > i then (Format.pp_print_string out sep; Format.pp_print_cut out ());
pp_item buf k a.(k) pp_item k out a.(k)
done
let _print ~sep pp_item fmt a i j =
for k = i to j - 1 do
if k > i then (Format.pp_print_string fmt sep; Format.pp_print_cut fmt ());
pp_item fmt a.(k)
done done
let _to_gen a i j = let _to_gen a i j =
@ -654,11 +643,9 @@ let random_non_empty g st =
let n = 1 + Random.State.int st 1_000 in let n = 1 + Random.State.int st 1_000 in
random_len n g st random_len n g st
let pp ?(sep=", ") pp_item buf a = _pp ~sep pp_item buf a 0 (Array.length a) let pp ?(sep=", ") pp_item out a = _pp ~sep pp_item out a 0 (Array.length a)
let pp_i ?(sep=", ") pp_item buf a = _pp_i ~sep pp_item buf a 0 (Array.length a) let pp_i ?(sep=", ") pp_item out a = _pp_i ~sep pp_item out a 0 (Array.length a)
let print ?(sep=", ") pp_item fmt a = _print ~sep pp_item fmt a 0 (Array.length a)
let to_seq a k = iter k a let to_seq a k = iter k a
@ -900,10 +887,8 @@ module Sub = struct
let pp ?(sep=", ") pp_item buf a = _pp ~sep pp_item buf a.arr a.i a.j let pp ?(sep=", ") pp_item buf a = _pp ~sep pp_item buf a.arr a.i a.j
let pp_i ?(sep=", ") pp_item buf a = let pp_i ?(sep=", ") pp_item out a =
_pp_i ~sep (fun out k x -> pp_item out (k-a.i) x) buf a.arr a.i a.j _pp_i ~sep (fun k out x -> pp_item (k-a.i) out x) out a.arr a.i a.j
let print ?(sep=", ") pp_item fmt a = _print ~sep pp_item fmt a.arr a.i a.j
let to_seq a k = iter k a let to_seq a k = iter k a

View file

@ -9,6 +9,7 @@ type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int type 'a ord = 'a -> 'a -> int
type 'a random_gen = Random.State.t -> 'a type 'a random_gen = Random.State.t -> 'a
type 'a printer = Format.formatter -> 'a -> unit
(** {2 Abstract Signature} *) (** {2 Abstract Signature} *)
@ -157,17 +158,11 @@ module type S = sig
(** {2 IO} *) (** {2 IO} *)
val pp: ?sep:string -> (Buffer.t -> 'a -> unit) -> val pp: ?sep:string -> 'a printer -> 'a t printer
Buffer.t -> 'a t -> unit
(** Print an array of items with printing function *) (** Print an array of items with printing function *)
val pp_i: ?sep:string -> (Buffer.t -> int -> 'a -> unit) -> val pp_i: ?sep:string -> (int -> 'a printer) -> 'a t printer
Buffer.t -> 'a t -> unit
(** Print an array, giving the printing function both index and item *) (** Print an array, giving the printing function both index and item *)
val print : ?sep:string -> (Format.formatter -> 'a -> unit) ->
Format.formatter -> 'a t -> unit
(** Print an array of items with printing function *)
end end
(** {2 Arrays} *) (** {2 Arrays} *)

View file

@ -9,8 +9,6 @@ let compare (a:bool) b = Pervasives.compare a b
let negate x = not x let negate x = not x
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
let pp buf = Printf.bprintf buf "%B" let pp = Format.pp_print_bool
let print fmt = Format.pp_print_bool fmt

View file

@ -13,10 +13,6 @@ val equal : t -> t -> bool
val negate : t -> t val negate : t -> t
(** Negation on booleans (functional version of [not]) *) (** Negation on booleans (functional version of [not]) *)
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
val pp : t printer val pp : t printer
(** Printer for booleans *)
val print : t formatter

View file

@ -42,12 +42,10 @@ let equal (a:float) b = a=b
let hash = Hashtbl.hash let hash = Hashtbl.hash
let compare (a:float) b = Pervasives.compare a b let compare (a:float) b = Pervasives.compare a b
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a type 'a random_gen = Random.State.t -> 'a
let pp buf = Printf.bprintf buf "%f" let pp = Format.pp_print_float
let print fmt = Format.pp_print_float fmt
let fsign a = let fsign a =
if is_nan a then nan if is_nan a then nan

View file

@ -41,12 +41,10 @@ val equal : t -> t -> bool
val compare : t -> t -> int val compare : t -> t -> int
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a type 'a random_gen = Random.State.t -> 'a
val pp : t printer val pp : t printer
val print : t formatter
val hash : t -> int val hash : t -> int

View file

@ -37,16 +37,14 @@ let pow a b =
pow 0 1 = 0 pow 0 1 = 0
*) *)
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a type 'a random_gen = Random.State.t -> 'a
let random n st = Random.State.int st n let random n st = Random.State.int st n
let random_small = random 100 let random_small = random 100
let random_range i j st = i + random (j-i) st let random_range i j st = i + random (j-i) st
let pp buf = Printf.bprintf buf "%d" let pp fmt = Format.pp_print_int fmt
let print fmt = Format.pp_print_int fmt
let most_significant_bit = let most_significant_bit =
(-1) lxor ((-1) lsr 1) (-1) lxor ((-1) lsr 1)
@ -79,7 +77,7 @@ let to_binary_gen (out:output) n =
in in
loop false most_significant_bit n loop false most_significant_bit n
let print_binary out n = let pp_binary out n =
to_binary_gen (Format.pp_print_char out) n to_binary_gen (Format.pp_print_char out) n
let to_string_binary n = let to_string_binary n =

View file

@ -23,8 +23,7 @@ val pow : t -> t -> t
Raises [Invalid_argument] if [a = b = 0] or [b] < 0. Raises [Invalid_argument] if [a = b = 0] or [b] < 0.
@since 0.11 *) @since 0.11 *)
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a type 'a random_gen = Random.State.t -> 'a
val random : int -> t random_gen val random : int -> t random_gen
@ -32,7 +31,6 @@ val random_small : t random_gen
val random_range : int -> int -> t random_gen val random_range : int -> int -> t random_gen
val pp : t printer val pp : t printer
val print : t formatter
val to_string : t -> string val to_string : t -> string
(** @since 0.13 *) (** @since 0.13 *)
@ -40,7 +38,7 @@ val to_string : t -> string
val of_string : string -> t option val of_string : string -> t option
(** @since 0.13 *) (** @since 0.13 *)
val print_binary : t formatter val pp_binary : t printer
(** prints as "0b00101010". (** prints as "0b00101010".
@since 0.20 *) @since 0.20 *)

View file

@ -1136,8 +1136,7 @@ end
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist] type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist]
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a type 'a random_gen = Random.State.t -> 'a
let random_len len g st = let random_len len g st =
@ -1221,17 +1220,7 @@ end
(** {2 IO} *) (** {2 IO} *)
let pp ?(start="") ?(stop="") ?(sep=", ") pp_item buf l = let pp ?(start="") ?(stop="") ?(sep=", ") pp_item fmt l =
let rec print l = match l with
| x::((_::_) as l) ->
pp_item buf x;
Buffer.add_string buf sep;
print l
| x::[] -> pp_item buf x
| [] -> ()
in Buffer.add_string buf start; print l; Buffer.add_string buf stop
let print ?(start="") ?(stop="") ?(sep=", ") pp_item fmt l =
let rec print fmt l = match l with let rec print fmt l = match l with
| x::((_::_) as l) -> | x::((_::_) as l) ->
pp_item fmt x; pp_item fmt x;
@ -1246,5 +1235,8 @@ let print ?(start="") ?(stop="") ?(sep=", ") pp_item fmt l =
Format.pp_print_string fmt stop Format.pp_print_string fmt stop
(*$= & ~printer:(fun s->s) (*$= & ~printer:(fun s->s)
"[1, 2, 3]" (CCFormat.to_string (CCFormat.hbox(print ~start:"[" ~stop:"]" CCFormat.int)) [1;2;3]) "[1, 2, 3]" \
(CCFormat.to_string \
(CCFormat.hbox(CCList.pp ~start:"[" ~stop:"]" CCFormat.int)) \
[1;2;3])
*) *)

View file

@ -488,8 +488,7 @@ end
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist] type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist]
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a type 'a random_gen = Random.State.t -> 'a
val random : 'a random_gen -> 'a t random_gen val random : 'a random_gen -> 'a t random_gen
@ -533,6 +532,3 @@ end
val pp : ?start:string -> ?stop:string -> ?sep:string -> val pp : ?start:string -> ?stop:string -> ?sep:string ->
'a printer -> 'a t printer 'a printer -> 'a t printer
val print : ?start:string -> ?stop:string -> ?sep:string ->
'a formatter -> 'a t formatter

View file

@ -4,8 +4,7 @@
(** {1 Extensions of Standard Map} *) (** {1 Extensions of Standard Map} *)
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
module type S = sig module type S = sig
include Map.S include Map.S
@ -55,10 +54,6 @@ module type S = sig
val pp : val pp :
?start:string -> ?stop:string -> ?arrow:string -> ?sep:string -> ?start:string -> ?stop:string -> ?arrow:string -> ?sep:string ->
key printer -> 'a printer -> 'a t printer key printer -> 'a printer -> 'a t printer
val print :
?start:string -> ?stop:string -> ?arrow:string -> ?sep:string ->
key formatter -> 'a formatter -> 'a t formatter
end end
module Make(O : Map.OrderedType) = struct module Make(O : Map.OrderedType) = struct
@ -113,19 +108,7 @@ module Make(O : Map.OrderedType) = struct
let to_list m = let to_list m =
fold (fun k v acc -> (k,v)::acc) m [] fold (fun k v acc -> (k,v)::acc) m []
let pp ?(start="") ?(stop="") ?(arrow="->") ?(sep=", ") pp_k pp_v buf m = let pp ?(start="") ?(stop="") ?(arrow="->") ?(sep=", ") pp_k pp_v fmt m =
let first = ref true in
Buffer.add_string buf start;
iter
(fun k v ->
if !first then first := false else Buffer.add_string buf sep;
pp_k buf k;
Buffer.add_string buf arrow;
pp_v buf v)
m;
Buffer.add_string buf stop
let print ?(start="") ?(stop="") ?(arrow="->") ?(sep=", ") pp_k pp_v fmt m =
Format.pp_print_string fmt start; Format.pp_print_string fmt start;
let first = ref true in let first = ref true in
iter iter

View file

@ -7,8 +7,7 @@
@since 0.5 *) @since 0.5 *)
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
module type S = sig module type S = sig
include Map.S include Map.S
@ -58,10 +57,6 @@ module type S = sig
val pp : val pp :
?start:string -> ?stop:string -> ?arrow:string -> ?sep:string -> ?start:string -> ?stop:string -> ?arrow:string -> ?sep:string ->
key printer -> 'a printer -> 'a t printer key printer -> 'a printer -> 'a t printer
val print :
?start:string -> ?stop:string -> ?arrow:string -> ?sep:string ->
key formatter -> 'a formatter -> 'a t formatter
end end
module Make(O : Map.OrderedType) : S module Make(O : Map.OrderedType) : S

View file

@ -147,8 +147,7 @@ end
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a fmt = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a type 'a random_gen = Random.State.t -> 'a
let random g st = let random g st =
@ -184,10 +183,6 @@ let to_seq o k = match o with
| None -> () | None -> ()
| Some x -> k x | Some x -> k x
let pp ppx buf o = match o with let pp ppx out = function
| None -> Buffer.add_string buf "None"
| Some x -> Buffer.add_string buf "Some "; ppx buf x
let print ppx out = function
| None -> Format.pp_print_string out "None" | None -> Format.pp_print_string out "None"
| Some x -> Format.fprintf out "@[Some %a@]" ppx x | Some x -> Format.fprintf out "@[Some %a@]" ppx x

View file

@ -130,8 +130,7 @@ val of_list : 'a list -> 'a t
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a fmt = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a type 'a random_gen = Random.State.t -> 'a
val random : 'a random_gen -> 'a t random_gen val random : 'a random_gen -> 'a t random_gen
@ -145,6 +144,3 @@ val to_gen : 'a t -> 'a gen
val to_seq : 'a t -> 'a sequence val to_seq : 'a t -> 'a sequence
val pp : 'a printer -> 'a t printer val pp : 'a printer -> 'a t printer
val print : 'a fmt -> 'a t fmt
(** @since 0.13 *)

View file

@ -42,11 +42,8 @@ let compare f g (x1,y1) (x2,y2) =
let c = f x1 x2 in let c = f x1 x2 in
if c <> 0 then c else g y1 y2 if c <> 0 then c else g y1 y2
type 'a printer = Buffer.t -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
let pp pp_x pp_y buf (x,y) = type 'a printer = Format.formatter -> 'a -> unit
Printf.bprintf buf "(%a, %a)" pp_x x pp_y y
let print pa pb fmt (x,y) = let pp ?(sep=", ") pa pb out (x,y) =
Format.fprintf fmt "(%a, %a)" pa x pb y Format.fprintf out "%a%s@,%a" pa x sep pb y

View file

@ -63,9 +63,6 @@ val equal : ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> ('a * 'b) -> ('a * 'b) -
val compare : ('a -> 'a -> int) -> ('b -> 'b -> int) -> ('a * 'b) -> ('a * 'b) -> int val compare : ('a -> 'a -> int) -> ('b -> 'b -> int) -> ('a * 'b) -> ('a * 'b) -> int
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
val pp : 'a printer -> 'b printer -> ('a*'b) printer val pp : ?sep:string -> 'a printer -> 'b printer -> ('a*'b) printer
val print : 'a formatter -> 'b formatter -> ('a*'b) formatter

View file

@ -5,8 +5,7 @@
@since 0.9 *) @since 0.9 *)
type 'a print = Format.formatter -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a pp = Buffer.t -> 'a -> unit
type 'a ord = 'a -> 'a -> int type 'a ord = 'a -> 'a -> int
type 'a eq = 'a -> 'a -> bool type 'a eq = 'a -> 'a -> bool
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
@ -36,6 +35,4 @@ let equal f r1 r2 = f !r1 !r2
let to_list r = [!r] let to_list r = [!r]
let to_seq r yield = yield !r let to_seq r yield = yield !r
let print pp_x fmt r = pp_x fmt !r let pp pp_x out r = pp_x out !r
let pp pp_x buf r = pp_x buf !r

View file

@ -4,8 +4,7 @@
(** {1 References} (** {1 References}
@since 0.9 *) @since 0.9 *)
type 'a print = Format.formatter -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a pp = Buffer.t -> 'a -> unit
type 'a ord = 'a -> 'a -> int type 'a ord = 'a -> 'a -> int
type 'a eq = 'a -> 'a -> bool type 'a eq = 'a -> 'a -> bool
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
@ -40,5 +39,4 @@ val to_list : 'a t -> 'a list
val to_seq : 'a t -> 'a sequence val to_seq : 'a t -> 'a sequence
val print : 'a print -> 'a t print val pp : 'a printer -> 'a t printer
val pp : 'a pp -> 'a t pp

View file

@ -6,8 +6,7 @@
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int type 'a ord = 'a -> 'a -> int
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
(** {2 Basics} *) (** {2 Basics} *)
@ -257,18 +256,10 @@ let to_err = function
(** {2 IO} *) (** {2 IO} *)
let pp pp_x buf e = match e with let pp pp_x fmt e = match e with
| Ok x -> Printf.bprintf buf "ok(%a)" pp_x x
| Error s -> Printf.bprintf buf "error(%s)" s
let pp' pp_x pp_e buf e = match e with
| Ok x -> Printf.bprintf buf "ok(%a)" pp_x x
| Error s -> Printf.bprintf buf "error(%a)" pp_e s
let print pp_x fmt e = match e with
| Ok x -> Format.fprintf fmt "@[ok(@,%a)@]" pp_x x | Ok x -> Format.fprintf fmt "@[ok(@,%a)@]" pp_x x
| Error s -> Format.fprintf fmt "@[error(@,%s)@]" s | Error s -> Format.fprintf fmt "@[error(@,%s)@]" s
let print' pp_x pp_e fmt e = match e with let pp' pp_x pp_e fmt e = match e with
| Ok x -> Format.fprintf fmt "@[ok(@,%a)@]" pp_x x | Ok x -> Format.fprintf fmt "@[ok(@,%a)@]" pp_x x
| Error s -> Format.fprintf fmt "@[error(@,%a)@]" pp_e s | Error s -> Format.fprintf fmt "@[error(@,%a)@]" pp_e s

View file

@ -10,8 +10,7 @@
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int type 'a ord = 'a -> 'a -> int
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
(** {2 Basics} *) (** {2 Basics} *)
@ -194,12 +193,4 @@ val to_err : ('a, 'b) t -> ('a, 'b) error
val pp : 'a printer -> ('a, string) t printer val pp : 'a printer -> ('a, string) t printer
val pp': 'a printer -> 'e printer -> ('a, 'e) t printer val pp': 'a printer -> 'e printer -> ('a, 'e) t printer
(** Printer that is generic on the error type (** Printer that is generic on the error type *)
@since 0.19 *)
val print : 'a formatter -> ('a, string) t formatter
val print' : 'a formatter -> 'e formatter -> ('a, 'e) t formatter
(** Printer that is generic on the error type
@since 0.19 *)

View file

@ -4,8 +4,7 @@
(** {1 Wrapper around Set} *) (** {1 Wrapper around Set} *)
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
module type S = sig module type S = sig
include Set.S include Set.S
@ -27,10 +26,6 @@ module type S = sig
val pp : val pp :
?start:string -> ?stop:string -> ?sep:string -> ?start:string -> ?stop:string -> ?sep:string ->
elt printer -> t printer elt printer -> t printer
val print :
?start:string -> ?stop:string -> ?sep:string ->
elt formatter -> t formatter
end end
module Make(O : Map.OrderedType) = struct module Make(O : Map.OrderedType) = struct
@ -51,17 +46,7 @@ module Make(O : Map.OrderedType) = struct
let to_list = elements let to_list = elements
let pp ?(start="") ?(stop="") ?(sep=", ") pp_x buf m = let pp ?(start="") ?(stop="") ?(sep=", ") pp_x fmt m =
let first = ref true in
Buffer.add_string buf start;
iter
(fun x ->
if !first then first := false else Buffer.add_string buf sep;
pp_x buf x)
m;
Buffer.add_string buf stop
let print ?(start="") ?(stop="") ?(sep=", ") pp_x fmt m =
Format.pp_print_string fmt start; Format.pp_print_string fmt start;
let first = ref true in let first = ref true in
iter iter

View file

@ -6,8 +6,7 @@
@since 0.9 *) @since 0.9 *)
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
module type S = sig module type S = sig
include Set.S include Set.S
@ -29,10 +28,6 @@ module type S = sig
val pp : val pp :
?start:string -> ?stop:string -> ?sep:string -> ?start:string -> ?stop:string -> ?sep:string ->
elt printer -> t printer elt printer -> t printer
val print :
?start:string -> ?stop:string -> ?sep:string ->
elt formatter -> t formatter
end end
module Make(O : Set.OrderedType) : S module Make(O : Set.OrderedType) : S

View file

@ -11,8 +11,7 @@ type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist]
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int type 'a ord = 'a -> 'a -> int
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
(** A vector of 'a. *) (** A vector of 'a. *)
type ('a,'mut) t = { type ('a,'mut) t = {
@ -737,16 +736,7 @@ let to_klist v =
else `Cons (v.vec.(i), aux (i+1)) else `Cons (v.vec.(i), aux (i+1))
in aux 0 in aux 0
let pp ?(start="") ?(stop="") ?(sep=", ") pp_item buf v = let pp ?(start="") ?(stop="") ?(sep=", ") pp_item fmt v =
Buffer.add_string buf start;
iteri
(fun i x ->
if i > 0 then Buffer.add_string buf sep;
pp_item buf x
) v;
Buffer.add_string buf stop
let print ?(start="") ?(stop="") ?(sep=", ") pp_item fmt v =
Format.pp_print_string fmt start; Format.pp_print_string fmt start;
iteri iteri
(fun i x -> (fun i x ->

View file

@ -24,8 +24,7 @@ type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist]
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int type 'a ord = 'a -> 'a -> int
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
val freeze : ('a, _) t -> ('a, ro) t val freeze : ('a, _) t -> ('a, ro) t
(** Make an immutable vector (no copy! Don't use the old version)*) (** Make an immutable vector (no copy! Don't use the old version)*)
@ -275,6 +274,3 @@ val to_gen : ('a,_) t -> 'a gen
val pp : ?start:string -> ?stop:string -> ?sep:string -> val pp : ?start:string -> ?stop:string -> ?sep:string ->
'a printer -> ('a,_) t printer 'a printer -> ('a,_) t printer
val print : ?start:string -> ?stop:string -> ?sep:string ->
'a formatter -> ('a,_) t formatter

View file

@ -4,8 +4,7 @@
(** {1 Persistent hash-table on top of OCaml's hashtables} *) (** {1 Persistent hash-table on top of OCaml's hashtables} *)
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
module type HashedType = sig module type HashedType = sig
@ -111,9 +110,7 @@ module type S = sig
val equal : 'a equal -> 'a t equal val equal : 'a equal -> 'a t equal
val pp : key printer -> 'a printer -> 'a t printer val pp : ?sep:string -> ?arrow:string -> key printer -> 'a printer -> 'a t printer
val print : key formatter -> 'a formatter -> 'a t formatter
val stats : _ t -> Hashtbl.statistics val stats : _ t -> Hashtbl.statistics
(** Statistics on the internal table. (** Statistics on the internal table.
@ -636,26 +633,15 @@ module Make(H : HashedType) : S with type key = H.t = struct
| Some v' -> eq v v' | Some v' -> eq v v'
) t1 ) t1
let pp pp_k pp_v buf t = let pp ?(sep=",") ?(arrow="->") pp_k pp_v fmt t =
Buffer.add_string buf "{";
let first = ref true in
iter t
(fun k v ->
if !first then first:=false else Buffer.add_string buf ", ";
Printf.bprintf buf "%a -> %a" pp_k k pp_v v
);
Buffer.add_string buf "}"
let print pp_k pp_v fmt t =
Format.pp_print_string fmt "{";
let first = ref true in let first = ref true in
iter t iter t
(fun k v -> (fun k v ->
if !first then first:=false if !first then first:=false
else (Format.pp_print_string fmt ", "; Format.pp_print_cut fmt ()); else (Format.pp_print_string fmt sep; Format.pp_print_cut fmt ());
Format.fprintf fmt "%a -> %a" pp_k k pp_v v Format.fprintf fmt "%a %s %a" pp_k k arrow pp_v v
); );
Format.pp_print_string fmt "}" ()
let stats t = let stats t =
let a = reroot_ t in let a = reroot_ t in

View file

@ -11,8 +11,7 @@ old values).
This module is not thread-safe. *) This module is not thread-safe. *)
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
module type HashedType = sig module type HashedType = sig
@ -118,9 +117,7 @@ module type S = sig
val equal : 'a equal -> 'a t equal val equal : 'a equal -> 'a t equal
val pp : key printer -> 'a printer -> 'a t printer val pp : ?sep:string -> ?arrow:string -> key printer -> 'a printer -> 'a t printer
val print : key formatter -> 'a formatter -> 'a t formatter
val stats : _ t -> Hashtbl.statistics val stats : _ t -> Hashtbl.statistics
(** Statistics on the internal table. (** Statistics on the internal table.

View file

@ -7,8 +7,7 @@ type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int type 'a ord = 'a -> 'a -> int
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type + 'a t = unit -> type + 'a t = unit ->
[ `Nil [ `Nil
@ -528,16 +527,7 @@ end
(** {2 IO} *) (** {2 IO} *)
let pp ?(sep=",") pp_item buf l = let pp ?(sep=",") pp_item fmt l =
let rec pp buf l = match l() with
| `Nil -> ()
| `Cons (x,l') -> Buffer.add_string buf sep; pp_item buf x; pp buf l'
in
match l() with
| `Nil -> ()
| `Cons (x,l') -> pp_item buf x; pp buf l'
let print ?(sep=",") pp_item fmt l =
let rec pp fmt l = match l() with let rec pp fmt l = match l() with
| `Nil -> () | `Nil -> ()
| `Cons (x,l') -> | `Cons (x,l') ->

View file

@ -7,8 +7,7 @@ type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int type 'a ord = 'a -> 'a -> int
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
(** {2 Basics} *) (** {2 Basics} *)
@ -211,7 +210,7 @@ val (<.>) : ('a -> 'b) t -> 'a t -> 'b t
@since 0.13 *) @since 0.13 *)
(** {2 Infix operators} (** {2 Infix operators}
@since 0.17 *) @since 0.17 *)
module Infix : sig module Infix : sig
@ -270,7 +269,3 @@ val of_gen : 'a gen -> 'a t
val pp : ?sep:string -> 'a printer -> 'a t printer val pp : ?sep:string -> 'a printer -> 'a t printer
(** Print the list with the given separator (default ","). (** Print the list with the given separator (default ",").
Does not print opening/closing delimiters *) Does not print opening/closing delimiters *)
val print : ?sep:string -> 'a formatter -> 'a t formatter
(** Print the list with the given separator (default ",").
Does not print opening/closing delimiters *)

View file

@ -31,8 +31,7 @@ is a structural type. *)
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist] type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist]
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type +'a t = unit -> [`Nil | `Node of 'a * 'a t list] type +'a t = unit -> [`Nil | `Node of 'a * 'a t list]
@ -186,7 +185,7 @@ let find ?pset f t =
(** {2 Pretty-printing} *) (** {2 Pretty-printing} *)
let print pp_x fmt t = let pp pp_x fmt t =
(* at depth [lvl] *) (* at depth [lvl] *)
let rec pp fmt t = match t with let rec pp fmt t = match t with
| `Nil -> () | `Nil -> ()
@ -273,7 +272,7 @@ module Dot = struct
Format.pp_print_char fmt ','; Format.pp_print_char fmt ',';
_pp_attrs fmt l' _pp_attrs fmt l'
let print fmt (name,l) = let pp out (name,l) =
(* nodes already printed *) (* nodes already printed *)
let tbl = Hashtbl.create 32 in let tbl = Hashtbl.create 32 in
(* fresh name generator *) (* fresh name generator *)
@ -303,11 +302,11 @@ module Dot = struct
let name, attrs = get_name x in let name, attrs = get_name x in
begin match parent with begin match parent with
| None -> () | None -> ()
| Some n -> Format.fprintf fmt " %s -> %s;@," n name | Some n -> Format.fprintf out " %s -> %s;@," n name
end; end;
if not (Hashtbl.mem tbl name) then ( if not (Hashtbl.mem tbl name) then (
Hashtbl.add tbl name (); Hashtbl.add tbl name ();
Format.fprintf fmt "@[%s [%a];@]@," name _pp_attrs attrs; Format.fprintf out "@[%s [%a];@]@," name _pp_attrs attrs;
List.fold_left List.fold_left
(fun q y -> FQ.push q (Some name, y)) q l (fun q y -> FQ.push q (Some name, y)) q l
) else q ) else q
@ -318,23 +317,18 @@ module Dot = struct
FQ.empty l FQ.empty l
in in
(* preamble *) (* preamble *)
Format.fprintf fmt "@[<hv 2>digraph \"%s\" {@," name; Format.fprintf out "@[<hv 2>digraph \"%s\" {@," name;
aux q; aux q;
Format.fprintf fmt "}@]@."; Format.fprintf out "}@]@.";
() ()
let pp buf t = let pp_single name out t = pp out (singleton ~name t)
let fmt = Format.formatter_of_buffer buf in
print fmt t;
Format.pp_print_flush fmt ()
let pp_single name buf t = pp buf (singleton ~name t)
let print_to_file filename g = let print_to_file filename g =
let oc = open_out filename in let oc = open_out filename in
let fmt = Format.formatter_of_out_channel oc in let fmt = Format.formatter_of_out_channel oc in
try try
print fmt g; pp fmt g;
Format.pp_print_flush fmt (); Format.pp_print_flush fmt ();
close_out oc close_out oc
with e -> with e ->

View file

@ -31,8 +31,7 @@ is a structural type. *)
type 'a sequence = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist] type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist]
type 'a printer = Buffer.t -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
(** {2 Basics} *) (** {2 Basics} *)
@ -119,7 +118,7 @@ Example (tree of calls for naive Fibonacci function):
]} ]}
*) *)
val print : 'a formatter -> 'a t formatter val pp : 'a printer -> 'a t printer
(** A pretty-printer using S-expressions and boxes to render the tree. (** A pretty-printer using S-expressions and boxes to render the tree.
Empty nodes are not rendered; sharing is ignored. Empty nodes are not rendered; sharing is ignored.
@since 0.9 *) @since 0.9 *)
@ -150,13 +149,10 @@ module Dot : sig
val singleton : name:string -> attribute list t -> graph val singleton : name:string -> attribute list t -> graph
val pp : graph printer
(** Print the graph in DOT *)
val pp_single : string -> attribute list t printer val pp_single : string -> attribute list t printer
val print : graph formatter val pp : graph printer
(** Printer with indentation, etc. (** Printer to DOT with indentation, etc.
@since 0.6.1 *) @since 0.6.1 *)
val print_to_file : string -> graph -> unit val print_to_file : string -> graph -> unit

View file

@ -110,7 +110,7 @@ exception ParseError of line_num * col_num * (unit -> string)
let l = CCList.(1 -- n) in let l = CCList.(1 -- n) in
let l_printed = let l_printed =
CCFormat.to_string (CCList.print ~sep:"," ~start:"[" ~stop:"]" CCInt.print) l in CCFormat.(to_string (list ~start:"[" ~stop:"]" ~sep:"," int)) l in
let l' = CCParse.parse_string_exn ~p l_printed in let l' = CCParse.parse_string_exn ~p l_printed in
@ -136,7 +136,7 @@ exception ParseError of line_num * col_num * (unit -> string)
(fun oc -> (fun oc ->
let fmt = Format.formatter_of_out_channel oc in let fmt = Format.formatter_of_out_channel oc in
Format.fprintf fmt "@[%a@]@." Format.fprintf fmt "@[%a@]@."
(CCList.print ~sep:"," ~start:"[" ~stop:"]" CCInt.print) l); CCFormat.(list ~start:"[" ~stop:"]" ~sep:"," int) l);
(* parse it back *) (* parse it back *)
CCParse.parse_file_exn ~size:1024 ~file:name ~p) CCParse.parse_file_exn ~size:1024 ~file:name ~p)
in in

View file

@ -15,22 +15,22 @@ let install_printer s =
() ()
let install_printers = List.iter install_printer let install_printers = List.iter install_printer
let pp_rw_vector pp_x out (v: _ CCVector.vector) = CCVector.print pp_x out v let pp_rw_vector pp_x out (v: _ CCVector.vector) = CCVector.pp pp_x out v
let pp_ro_vector pp_x out (v: _ CCVector.ro_vector) = CCVector.print pp_x out v let pp_ro_vector pp_x out (v: _ CCVector.ro_vector) = CCVector.pp pp_x out v
let pp_klist (ppx:Format.formatter -> 'a -> unit) out l = CCKList.print ppx out l let pp_klist (ppx:Format.formatter -> 'a -> unit) out l = CCKList.pp ppx out l
let () = let () =
install_printers install_printers
[ "CCHashtbl.print" [ "CCHashtbl.pp"
; "Containers_top.pp_rw_vector" ; "Containers_top.pp_rw_vector"
; "Containers_top.pp_ro_vector" ; "Containers_top.pp_ro_vector"
; "CCBV.print" ; "CCBV.pp"
; "CCDeque.print" ; "CCDeque.pp"
; "CCFQueue.print" ; "CCFQueue.pp"
; "CCIntMap.print" ; "CCIntMap.pp"
; "CCPersistentArray.print" ; "CCPersistentArray.pp"
; "CCBigstring.print" ; "CCBigstring.pp"
; "Containers_top.pp_klist" ; "Containers_top.pp_klist"
; "CCKTree.print" ; "CCKTree.pp"
; "CCSexpM.print" ; "CCSexpM.pp"
] ]