From a9b91943e854cd63b23f290634cb9b0771fb71df Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 6 Apr 2016 11:23:21 +0200 Subject: [PATCH] add `CCFormat.within` --- src/core/CCFormat.ml | 17 +++++++++++------ src/core/CCFormat.mli | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/core/CCFormat.ml b/src/core/CCFormat.ml index 8fd37a8e..01a1fdcd 100644 --- a/src/core/CCFormat.ml +++ b/src/core/CCFormat.ml @@ -76,14 +76,19 @@ let opt pp fmt x = match x with | None -> Format.pp_print_string fmt "none" | Some x -> Format.fprintf fmt "some %a" pp x -let pair ppa ppb fmt (a, b) = - Format.fprintf fmt "(%a,@ %a)" ppa a ppb b +let pair ?(sep=", ") ppa ppb fmt (a, b) = + Format.fprintf fmt "(%a%s@,%a)" ppa a sep ppb b -let triple ppa ppb ppc fmt (a, b, c) = - Format.fprintf fmt "(%a,@ %a,@ %a)" ppa a ppb b ppc c +let triple ?(sep=", ") ppa ppb ppc fmt (a, b, 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) = - Format.fprintf fmt "(%a,@ %a,@ %a,@ %a)" ppa a ppb b ppc c ppd d +let quad ?(sep=", ") ppa ppb ppc ppd fmt (a, b, c, 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 = pp fmt (f x); diff --git a/src/core/CCFormat.mli b/src/core/CCFormat.mli index e678a779..414fe4aa 100644 --- a/src/core/CCFormat.mli +++ b/src/core/CCFormat.mli @@ -38,9 +38,18 @@ val seq : ?start:string -> ?stop:string -> ?sep:string -> 'a printer -> 'a seque val opt : 'a printer -> 'a option printer -val pair : 'a printer -> 'b printer -> ('a * 'b) printer -val triple : 'a printer -> 'b printer -> 'c printer -> ('a * 'b * 'c) printer -val quad : 'a printer -> 'b printer -> 'c printer -> 'd printer -> ('a * 'b * 'c * 'd) printer +(** In the tuple printers, the [sep] argument is only available + @since NEXT_RELEASE *) + +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