mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
remove CCPrint (also, update tests relying on it)
This commit is contained in:
parent
af6b1dd6e0
commit
c89186a100
11 changed files with 12 additions and 281 deletions
|
|
@ -134,7 +134,6 @@ Documentation http://cedeela.fr/~simon/software/containers[here].
|
|||
- `CCFloat`
|
||||
- `CCOrd` (combinators for total orderings)
|
||||
- `CCRandom` (combinators for random generators)
|
||||
- `CCPrint` (printing combinators)
|
||||
- `CCHash` (hashing combinators)
|
||||
- `CCError` (monadic error handling, very useful)
|
||||
- `CCIO`, basic utilities for IO (channels, files)
|
||||
|
|
|
|||
2
_oasis
2
_oasis
|
|
@ -42,7 +42,7 @@ Flag "advanced"
|
|||
|
||||
Library "containers"
|
||||
Path: src/core
|
||||
Modules: CCVector, CCPrint, CCError, CCHeap, CCList, CCOpt, CCPair,
|
||||
Modules: CCVector, CCError, CCHeap, CCList, CCOpt, CCPair,
|
||||
CCFun, CCHash, CCInt, CCBool, CCFloat, CCArray, CCRef, CCSet,
|
||||
CCOrd, CCRandom, CCString, CCHashtbl, CCMap, CCFormat, CCIO,
|
||||
CCInt64, CCChar, CCResult, Containers
|
||||
|
|
|
|||
|
|
@ -43,7 +43,6 @@ CCMap
|
|||
CCOpt
|
||||
CCOrd
|
||||
CCPair
|
||||
CCPrint
|
||||
CCRandom
|
||||
CCRef
|
||||
CCResult
|
||||
|
|
|
|||
|
|
@ -1231,10 +1231,6 @@ let pp ?(start="") ?(stop="") ?(sep=", ") pp_item buf l =
|
|||
| [] -> ()
|
||||
in Buffer.add_string buf start; print l; Buffer.add_string buf stop
|
||||
|
||||
(*$T
|
||||
CCPrint.to_string (pp ~start:"[" ~stop:"]" CCPrint.int) [1;2;3] = "[1, 2, 3]"
|
||||
*)
|
||||
|
||||
let print ?(start="") ?(stop="") ?(sep=", ") pp_item fmt l =
|
||||
let rec print fmt l = match l with
|
||||
| x::((_::_) as l) ->
|
||||
|
|
@ -1248,3 +1244,7 @@ let print ?(start="") ?(stop="") ?(sep=", ") pp_item fmt l =
|
|||
Format.pp_print_string fmt start;
|
||||
print fmt l;
|
||||
Format.pp_print_string fmt stop
|
||||
|
||||
(*$= & ~printer:(fun s->s)
|
||||
"[1, 2, 3]" (CCFormat.to_string (CCFormat.hbox(print ~start:"[" ~stop:"]" CCFormat.int)) [1;2;3])
|
||||
*)
|
||||
|
|
|
|||
|
|
@ -1,155 +0,0 @@
|
|||
|
||||
(* This file is free software, part of containers. See file "license" for more details. *)
|
||||
|
||||
(** {1 Printer Combinators}
|
||||
|
||||
This module provides combinators to build printers for user-defined types.
|
||||
It doesn't try to do {b pretty}-printing (see for instance Pprint for this),
|
||||
but a simple way to print complicated values without writing a lot of code.
|
||||
*)
|
||||
|
||||
type 'a sequence = ('a -> unit) -> unit
|
||||
|
||||
type 'a t = Buffer.t -> 'a -> unit
|
||||
(** A printer for the type ['a] *)
|
||||
|
||||
(** {2 Combinators} *)
|
||||
|
||||
let silent _buf _ = ()
|
||||
|
||||
let unit buf () = Buffer.add_string buf "()"
|
||||
let int buf i = Buffer.add_string buf (string_of_int i)
|
||||
let string buf s = Buffer.add_string buf s
|
||||
let bool buf b = Printf.bprintf buf "%B" b
|
||||
let float3 buf f = Printf.bprintf buf "%.3f" f
|
||||
let float buf f = Buffer.add_string buf (string_of_float f)
|
||||
let char buf c = Buffer.add_char buf c
|
||||
|
||||
let list ?(start="") ?(stop="") ?(sep=", ") pp buf l =
|
||||
let rec pp_list l = match l with
|
||||
| x::((_::_) as l) ->
|
||||
pp buf x;
|
||||
Buffer.add_string buf sep;
|
||||
pp_list l
|
||||
| x::[] -> pp buf x
|
||||
| [] -> ()
|
||||
in
|
||||
Buffer.add_string buf start;
|
||||
pp_list l;
|
||||
Buffer.add_string buf stop
|
||||
|
||||
let array ?(start="") ?(stop="") ?(sep=", ") pp buf a =
|
||||
Buffer.add_string buf start;
|
||||
for i = 0 to Array.length a - 1 do
|
||||
(if i > 0 then Buffer.add_string buf sep);
|
||||
pp buf a.(i)
|
||||
done;
|
||||
Buffer.add_string buf stop
|
||||
|
||||
let arrayi ?(start="") ?(stop="") ?(sep=", ") pp buf a =
|
||||
Buffer.add_string buf start;
|
||||
for i = 0 to Array.length a - 1 do
|
||||
(if i > 0 then Buffer.add_string buf sep);
|
||||
pp buf (i, a.(i))
|
||||
done;
|
||||
Buffer.add_string buf stop
|
||||
|
||||
let seq ?(start="") ?(stop="") ?(sep=", ") pp buf seq =
|
||||
Buffer.add_string buf start;
|
||||
let first = ref true in
|
||||
seq (fun x ->
|
||||
(if !first then first := false else Buffer.add_string buf sep);
|
||||
pp buf x);
|
||||
Buffer.add_string buf stop
|
||||
|
||||
let opt pp buf x = match x with
|
||||
| None -> Buffer.add_string buf "none"
|
||||
| Some x -> Printf.bprintf buf "some %a" pp x
|
||||
|
||||
let pair ppa ppb buf (a, b) =
|
||||
Printf.bprintf buf "(%a, %a)" ppa a ppb b
|
||||
|
||||
let triple ppa ppb ppc buf (a, b, c) =
|
||||
Printf.bprintf buf "(%a, %a, %a)" ppa a ppb b ppc c
|
||||
|
||||
let quad ppa ppb ppc ppd buf (a, b, c, d) =
|
||||
Printf.bprintf buf "(%a, %a, %a, %a)" ppa a ppb b ppc c ppd d
|
||||
|
||||
let map f pp buf x =
|
||||
pp buf (f x);
|
||||
()
|
||||
|
||||
(** {2 IO} *)
|
||||
|
||||
let output oc pp x =
|
||||
let buf = Buffer.create 64 in
|
||||
pp buf x;
|
||||
Buffer.output_buffer oc buf
|
||||
|
||||
let to_string pp x =
|
||||
let buf = Buffer.create 64 in
|
||||
pp buf x;
|
||||
Buffer.contents buf
|
||||
|
||||
let sprintf format =
|
||||
let buffer = Buffer.create 64 in
|
||||
Printf.kbprintf
|
||||
(fun _fmt -> Buffer.contents buffer)
|
||||
buffer
|
||||
format
|
||||
|
||||
let fprintf oc format =
|
||||
let buffer = Buffer.create 64 in
|
||||
Printf.kbprintf
|
||||
(fun _fmt -> Buffer.output_buffer oc buffer)
|
||||
buffer
|
||||
format
|
||||
|
||||
let printf format = fprintf stdout format
|
||||
let eprintf format = fprintf stderr format
|
||||
|
||||
let _with_file_out filename f =
|
||||
let oc = open_out filename in
|
||||
begin try
|
||||
let x = f oc in
|
||||
close_out oc;
|
||||
x
|
||||
with e ->
|
||||
close_out_noerr oc;
|
||||
raise e
|
||||
end
|
||||
|
||||
let to_file filename format =
|
||||
_with_file_out filename (fun oc -> fprintf oc format)
|
||||
|
||||
(** {2 Monadic IO} *)
|
||||
|
||||
module type MONAD_IO = sig
|
||||
type 'a t (** the IO monad *)
|
||||
|
||||
type output (** Output channels *)
|
||||
|
||||
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
||||
|
||||
val write : output -> string -> unit t
|
||||
end
|
||||
|
||||
module MakeIO(M : MONAD_IO) = struct
|
||||
let output out pp x =
|
||||
let buf = Buffer.create 128 in
|
||||
pp buf x;
|
||||
M.write out (Buffer.contents buf)
|
||||
|
||||
let printl out pp x =
|
||||
let buf = Buffer.create 128 in
|
||||
pp buf x;
|
||||
Buffer.add_char buf '\n';
|
||||
M.write out (Buffer.contents buf)
|
||||
|
||||
let fprintf out format =
|
||||
let buf = Buffer.create 128 in
|
||||
Printf.kbprintf
|
||||
(fun buf -> M.write out (Buffer.contents buf))
|
||||
buf
|
||||
format
|
||||
end
|
||||
|
|
@ -1,112 +0,0 @@
|
|||
|
||||
(* This file is free software, part of containers. See file "license" for more details. *)
|
||||
|
||||
(** {1 Printer Combinators}
|
||||
|
||||
This module provides combinators to build printers for user-defined types.
|
||||
It doesn't try to do {b pretty}-printing (see for instance Pprint for this),
|
||||
but a simple way to print complicated values without writing a lot of code.
|
||||
|
||||
Those combinators work with "%a". For instance to print a
|
||||
[(int * bool) list list] and a [float array], one can write:
|
||||
{[
|
||||
CCPrint.(printf "int: %d list: %a, array: %a\n"
|
||||
42
|
||||
(list (list (pair int bool))) [[1, true; 2, false]; [4, true]]
|
||||
(array float) [| 1. ; 2. ; 3e18 |] ;;
|
||||
]}
|
||||
|
||||
Remember that "%a" in this context requires two arguments:
|
||||
- a value of type ['a t] (buffer printer)
|
||||
- a value of type ['a] (value to print)
|
||||
|
||||
To define new printers, one can either use existing ones (e.g. [list int]),
|
||||
or use {!Printf.bprintf}. For instance a printer for colored points in 2D:
|
||||
|
||||
{[ type point = {x:int; y:int; colors: string list};;
|
||||
|
||||
let pp_point buf p =
|
||||
Printf.bprintf buf "{x=%d, y=%d, colors=%a}"
|
||||
p.x p.y CCPrint.(list string) p.colors;;
|
||||
]}
|
||||
*)
|
||||
|
||||
type 'a sequence = ('a -> unit) -> unit
|
||||
|
||||
type 'a t = Buffer.t -> 'a -> unit
|
||||
(** A printer for the type ['a] *)
|
||||
|
||||
(** {2 Combinators} *)
|
||||
|
||||
val silent : 'a t (** prints nothing *)
|
||||
|
||||
val unit : unit t
|
||||
val int : int t
|
||||
val string : string t
|
||||
val bool : bool t
|
||||
val float3 : float t (* 3 digits after . *)
|
||||
val float : float t
|
||||
val char : char t
|
||||
(** @since 0.14 *)
|
||||
|
||||
val list : ?start:string -> ?stop:string -> ?sep:string -> 'a t -> 'a list t
|
||||
val array : ?start:string -> ?stop:string -> ?sep:string -> 'a t -> 'a array t
|
||||
val arrayi : ?start:string -> ?stop:string -> ?sep:string -> (int * 'a) t -> 'a array t
|
||||
val seq : ?start:string -> ?stop:string -> ?sep:string -> 'a t -> 'a sequence t
|
||||
|
||||
val opt : 'a t -> 'a option t
|
||||
|
||||
val pair : 'a t -> 'b t -> ('a * 'b) t
|
||||
val triple : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t
|
||||
val quad : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t
|
||||
|
||||
val map : ('a -> 'b) -> 'b t -> 'a t
|
||||
|
||||
(** {2 IO} *)
|
||||
|
||||
val output : out_channel -> 'a t -> 'a -> unit
|
||||
val to_string : 'a t -> 'a -> string
|
||||
|
||||
val sprintf : ('a, Buffer.t, unit, string) format4 -> 'a
|
||||
(** Print into a string *)
|
||||
|
||||
val fprintf : out_channel -> ('a, Buffer.t, unit, unit) format4 -> 'a
|
||||
(** Print on a channel *)
|
||||
|
||||
val to_file : string -> ('a, Buffer.t, unit, unit) format4 -> 'a
|
||||
(** Print to the given file *)
|
||||
|
||||
val printf : ('a, Buffer.t, unit, unit) format4 -> 'a
|
||||
val eprintf : ('a, Buffer.t, unit, unit) format4 -> 'a
|
||||
|
||||
(** {2 Monadic IO} *)
|
||||
|
||||
module type MONAD_IO = sig
|
||||
type 'a t (** the IO monad *)
|
||||
type output (** Output channels *)
|
||||
|
||||
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
||||
|
||||
val write : output -> string -> unit t
|
||||
end
|
||||
|
||||
module MakeIO(M : MONAD_IO) : sig
|
||||
val output : M.output -> 'a t -> 'a -> unit M.t
|
||||
(** Output a single value *)
|
||||
|
||||
val printl : M.output -> 'a t -> 'a -> unit M.t
|
||||
(** Output a value and add a newline "\n" after. *)
|
||||
|
||||
val fprintf : M.output -> ('a, Buffer.t, unit, unit M.t) format4 -> 'a
|
||||
(** Fprintf on a monadic output *)
|
||||
end
|
||||
(** Example:
|
||||
{[ module PrintLwt = CCPrint.MakeIO(struct
|
||||
include Lwt
|
||||
type output = Lwt_io.output_channel
|
||||
let write = Lwt_io.write
|
||||
end);;
|
||||
|
||||
PrintLwt.printl Lwt_io.stdout (CCList.pp CCInt.pp) [1;2;3;4];;
|
||||
- : unit Lwt.t
|
||||
]} *)
|
||||
|
|
@ -27,7 +27,7 @@ type 'a t = {
|
|||
(** The deque, a double linked list of cells *)
|
||||
|
||||
(*$inject
|
||||
let plist l = CCPrint.to_string (CCList.pp CCInt.pp) l
|
||||
let plist l = CCFormat.(to_string (list int)) l
|
||||
let pint i = string_of_int i
|
||||
*)
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ type 'a equal = 'a -> 'a -> bool
|
|||
type 'a printer = Format.formatter -> 'a -> unit
|
||||
|
||||
(*$inject
|
||||
let pp_ilist = CCPrint.(to_string (list int))
|
||||
let pp_ilist = CCFormat.(to_string (list int))
|
||||
*)
|
||||
|
||||
(** {2 Basics} *)
|
||||
|
|
|
|||
|
|
@ -357,7 +357,7 @@ module Make(E : ELT) : S with type elt = E.t = struct
|
|||
let s = S.of_list l in S.equal s (S.union s s))
|
||||
*)
|
||||
|
||||
(*$= & ~printer:(CCPrint.to_string (CCList.pp CCInt.pp))
|
||||
(*$= & ~printer:(CCFormat.(to_string (list int)))
|
||||
[1;2;4;5;6;7;8;10] (let module S = Make(CCInt) in \
|
||||
let s1 = S.of_list [1;2;4;5; 7;8 ] in \
|
||||
let s2 = S.of_list [ 2;4; 6;7; 10] in \
|
||||
|
|
@ -400,7 +400,7 @@ module Make(E : ELT) : S with type elt = E.t = struct
|
|||
let s = S.of_list l in S.equal s (S.inter s s))
|
||||
*)
|
||||
|
||||
(*$= & ~printer:(CCPrint.to_string (CCList.pp CCInt.pp))
|
||||
(*$= & ~printer:(CCFormat.(to_string (list int)))
|
||||
[2;4;7] (let module S = Make(CCInt) in \
|
||||
let s1 = S.of_list [1;2;4;5; 7;8 ] in \
|
||||
let s2 = S.of_list [ 2;4; 6;7; 10] in \
|
||||
|
|
@ -443,7 +443,7 @@ module Make(E : ELT) : S with type elt = E.t = struct
|
|||
else diff a r2
|
||||
else a
|
||||
|
||||
(*$= & ~printer:(CCPrint.to_string (CCList.pp CCInt.pp))
|
||||
(*$= & ~printer:(CCFormat.(to_string (list int)))
|
||||
[1;5;8] (let module S = Make(CCInt) in \
|
||||
let s1 = S.of_list [1;2;4;5; 7;8 ] in \
|
||||
let s2 = S.of_list [ 2;4; 6;7; 10] in \
|
||||
|
|
|
|||
|
|
@ -662,7 +662,7 @@ module Make(W : WORD)
|
|||
let below key t =
|
||||
_half_range ~dir:Below ~p:(fun ~cur ~other -> W.compare cur other > 0) key t
|
||||
|
||||
(*$= & ~printer:CCPrint.(to_string (list (pair (list int) string)))
|
||||
(*$= & ~printer:CCFormat.(to_string (list (pair (list int) string)))
|
||||
[ [1], "1"; [1;2], "12"; [1;2;3], "123"; [2;1], "21" ] \
|
||||
(T.above [1] t1 |> Sequence.to_list)
|
||||
[ [1;2], "12"; [1;2;3], "123"; [2;1], "21" ] \
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ exception ParseError of line_num * col_num * (unit -> string)
|
|||
(*$R
|
||||
let p = U.list ~sep:"," U.word in
|
||||
let printer = function
|
||||
| `Ok l -> "Ok " ^ CCPrint.to_string (CCList.pp CCString.pp) l
|
||||
| `Ok l -> "Ok " ^ CCFormat.(to_string (list string)) l
|
||||
| `Error s -> "Error " ^ s
|
||||
in
|
||||
assert_equal ~printer
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue