diff --git a/README.adoc b/README.adoc index e5bbfdca..6935c6fa 100644 --- a/README.adoc +++ b/README.adoc @@ -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) diff --git a/_oasis b/_oasis index 86cb7993..c24d5aa9 100644 --- a/_oasis +++ b/_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 diff --git a/doc/intro.txt b/doc/intro.txt index 8384761a..3d921c84 100644 --- a/doc/intro.txt +++ b/doc/intro.txt @@ -43,7 +43,6 @@ CCMap CCOpt CCOrd CCPair -CCPrint CCRandom CCRef CCResult diff --git a/src/core/CCList.ml b/src/core/CCList.ml index f09ea9fc..e6c702a4 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -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]) + *) diff --git a/src/core/CCPrint.ml b/src/core/CCPrint.ml deleted file mode 100644 index 3d1fd7b3..00000000 --- a/src/core/CCPrint.ml +++ /dev/null @@ -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 diff --git a/src/core/CCPrint.mli b/src/core/CCPrint.mli deleted file mode 100644 index bd6f5d85..00000000 --- a/src/core/CCPrint.mli +++ /dev/null @@ -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 -]} *) diff --git a/src/data/CCDeque.ml b/src/data/CCDeque.ml index 8de3afcb..1ebe6109 100644 --- a/src/data/CCDeque.ml +++ b/src/data/CCDeque.ml @@ -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 *) diff --git a/src/data/CCFQueue.ml b/src/data/CCFQueue.ml index 0f01245d..e1cb7736 100644 --- a/src/data/CCFQueue.ml +++ b/src/data/CCFQueue.ml @@ -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} *) diff --git a/src/data/CCHashconsedSet.ml b/src/data/CCHashconsedSet.ml index 3b34d28d..a4f7b907 100644 --- a/src/data/CCHashconsedSet.ml +++ b/src/data/CCHashconsedSet.ml @@ -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 \ diff --git a/src/data/CCTrie.ml b/src/data/CCTrie.ml index 569b6bdc..d2f2f1e3 100644 --- a/src/data/CCTrie.ml +++ b/src/data/CCTrie.ml @@ -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" ] \ diff --git a/src/string/CCParse.ml b/src/string/CCParse.ml index db34c1ec..62db92d5 100644 --- a/src/string/CCParse.ml +++ b/src/string/CCParse.ml @@ -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