better Format printers (using break hints)

This commit is contained in:
Simon Cruanes 2014-08-07 15:14:10 +02:00
parent 072131dd3e
commit 8f46630634
8 changed files with 19 additions and 11 deletions

View file

@ -234,7 +234,7 @@ let _pp_i ~sep pp_item buf a i j =
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;
if k > i then (Format.pp_print_string fmt sep; Format.pp_print_cut fmt ());
pp_item fmt a.(k)
done

View file

@ -36,4 +36,4 @@ type 'a printer = Buffer.t -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
let pp buf = Printf.bprintf buf "%B"
let print fmt = Format.fprintf fmt "%B"
let print fmt = Format.pp_print_bool fmt

View file

@ -230,5 +230,5 @@ let pp pp_x buf e = match e with
| `Error s -> Printf.bprintf buf "error(%s)" s
let print pp_x fmt e = match e with
| `Ok x -> Format.fprintf fmt "ok(%a)" pp_x x
| `Error s -> Format.fprintf fmt "error(%s)" s
| `Ok x -> Format.fprintf fmt "@[ok(@,%a)@]" pp_x x
| `Error s -> Format.fprintf fmt "@[error(@,%s)@]" s

View file

@ -46,4 +46,4 @@ let random_small = random 100
let random_range i j st = i + random (j-i) st
let pp buf = Printf.bprintf buf "%d"
let print fmt = Format.fprintf fmt "%d"
let print fmt = Format.pp_print_int fmt

View file

@ -361,7 +361,11 @@ let pp ?(sep=",") pp_item buf l =
let print ?(sep=",") pp_item fmt l =
let rec pp fmt l = match l() with
| `Nil -> ()
| `Cons (x,l') -> Format.pp_print_string fmt sep; pp_item fmt x; pp fmt l'
| `Cons (x,l') ->
Format.pp_print_string fmt sep;
Format.pp_print_cut fmt ();
pp_item fmt x;
pp fmt l'
in
match l() with
| `Nil -> ()

View file

@ -679,8 +679,11 @@ let print ?(start="[") ?(stop="]") ?(sep=", ") pp_item fmt l =
| x::((y::xs) as l) ->
pp_item fmt x;
Format.pp_print_string fmt sep;
Format.pp_print_cut fmt ();
print fmt l
| x::[] -> pp_item fmt x
| [] -> ()
in
Format.fprintf fmt "@[%s%a%s@]" start print l stop
Format.pp_print_string fmt start;
print fmt l;
Format.pp_print_string fmt stop

View file

@ -343,7 +343,8 @@ module Make(H : HashedType) : S with type key = H.t = struct
let first = ref true in
iter t
(fun k v ->
if !first then first:=false else Format.pp_print_string fmt ", ";
if !first then first:=false
else (Format.pp_print_string fmt ", "; Format.pp_print_cut fmt ());
Format.fprintf fmt "%a -> %a" pp_k k pp_v v
);
Format.pp_print_string fmt "}"

View file

@ -486,10 +486,10 @@ let pp ?(start="[") ?(stop="]") ?(sep=", ") pp_item buf v =
Buffer.add_string buf stop
let print ?(start="[") ?(stop="]") ?(sep=", ") pp_item fmt v =
Format.fprintf fmt "@[%s" start;
Format.pp_print_string fmt start;
iteri
(fun i x ->
if i > 0 then Format.pp_print_string fmt sep;
if i > 0 then (Format.pp_print_string fmt sep; Format.pp_print_cut fmt());
pp_item fmt x
) v;
Format.fprintf fmt "%s@]" stop
Format.pp_print_string fmt stop