add CCFormat.within

This commit is contained in:
Simon Cruanes 2016-04-06 11:23:21 +02:00
parent 55c9d6c60b
commit a9b91943e8
2 changed files with 23 additions and 9 deletions

View file

@ -76,14 +76,19 @@ let opt pp fmt x = match x with
| None -> Format.pp_print_string fmt "none" | None -> Format.pp_print_string fmt "none"
| Some x -> Format.fprintf fmt "some %a" pp x | Some x -> Format.fprintf fmt "some %a" pp x
let pair ppa ppb fmt (a, b) = let pair ?(sep=", ") ppa ppb fmt (a, b) =
Format.fprintf fmt "(%a,@ %a)" ppa a ppb b Format.fprintf fmt "(%a%s@,%a)" ppa a sep ppb b
let triple ppa ppb ppc fmt (a, b, c) = let triple ?(sep=", ") ppa ppb ppc fmt (a, b, c) =
Format.fprintf fmt "(%a,@ %a,@ %a)" ppa a ppb b ppc c Format.fprintf fmt "(%a%s@,%a%s@,%a)" ppa a sep ppb b sep ppc c
let quad ppa ppb ppc ppd fmt (a, b, c, d) = let quad ?(sep=", ") ppa ppb ppc ppd fmt (a, b, c, d) =
Format.fprintf fmt "(%a,@ %a,@ %a,@ %a)" ppa a ppb b ppc c ppd d Format.fprintf fmt "(%a%s@,%a%s@,%a%s@,%a)" ppa a sep ppb b sep ppc c sep ppd d
let within a b p out x =
string out a;
p out x;
string out b
let map f pp fmt x = let map f pp fmt x =
pp fmt (f x); pp fmt (f x);

View file

@ -38,9 +38,18 @@ val seq : ?start:string -> ?stop:string -> ?sep:string -> 'a printer -> 'a seque
val opt : 'a printer -> 'a option printer val opt : 'a printer -> 'a option printer
val pair : 'a printer -> 'b printer -> ('a * 'b) printer (** In the tuple printers, the [sep] argument is only available
val triple : 'a printer -> 'b printer -> 'c printer -> ('a * 'b * 'c) printer @since NEXT_RELEASE *)
val quad : 'a printer -> 'b printer -> 'c printer -> 'd printer -> ('a * 'b * 'c * 'd) printer
val pair : ?sep:string -> 'a printer -> 'b printer -> ('a * 'b) printer
val triple : ?sep:string -> 'a printer -> 'b printer -> 'c printer -> ('a * 'b * 'c) printer
val quad : ?sep:string -> 'a printer -> 'b printer ->
'c printer -> 'd printer -> ('a * 'b * 'c * 'd) printer
val within : string -> string -> 'a printer -> 'a printer
(** [within a b p] wraps [p] inside the strings [a] and [b]. Convenient,
for instances, for brackets, parenthesis, quotes, etc.
@since NEXT_RELEASE *)
val map : ('a -> 'b) -> 'b printer -> 'a printer val map : ('a -> 'b) -> 'b printer -> 'a printer