Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006009OCamlstandard librarypublic2013-05-07 15:562013-12-16 15:56
Assigned To 
PrioritynormalSeverityfeatureReproducibilityhave not tried
PlatformOSOS Version
Product Version4.00.1 
Target VersionFixed in Version 
Summary0006009: Format.{pp_print_opt, pp_print_list, pp_print_text}
DescriptionOk, this time I got too tired of copy pasting that code.

First an option pretty printer.

(** [pp_print_opt pp_none pp_v ppf o] prints the optional value v. [pp_v] is used if [o] is [Some],
     [pp_none] if [o] is [None] (default prints nothing). *)

let pp_print_opt ?(pp_none = fun ppf () -> ()) pp_v ppf = function
| None -> pp_none ppf ()
| Some v -> pp_v ppf v

Then a list pretty printer.

(** [pp_print_list pp_sep pp_v ppf l] prints the list [l]. [pp_v] is used on the elements of [l] and
     each element is separated by a call to [pp_sep] (defaults to {!pp_print_cut}). Does nothing on
     empty lists. *)

let rec pp_print_list ?(pp_sep = Format.pp_print_cut) pp_v ppf = function
| [] -> ()
| v :: vs ->
    pp_v ppf v; if vs <> [] then (pp_sep ppf (); pp_print_list ~pp_sep pp_v ppf vs)

Then a string printer that hints newlines and spaces with Format's functions.

(** [pp_print_text ?nl ppf s] prints [s] with spaces and newlines respectively printed with
     {!pp_print_space} and {!pp_force_newline}. *)

let pp_print_text ppf s = (* hint spaces and new lines with Format's funs *)
  let len = String.length s in
  let left = ref 0 in
  let right = ref 0 in
  let flush () =
    Format.pp_print_string ppf (String.sub s !left (!right - !left));
    incr right; left := !right;
  while (!right <> len) do
    if s.[!right] = '\n' then (flush (); Format.pp_force_newline ppf ()) else
    if s.[!right] = ' ' then (flush (); Format.pp_print_space ppf ()) else
    incr right
  if !left <> len then flush ()

Attached Filespatch file icon 0001-Format-pp_print_list-and-pp_print_text-contributed-b.patch [^] (2,826 bytes) 2013-08-18 11:29 [Show Content]

- Relationships

-  Notes
gasche (administrator)
2013-05-07 16:10

That's mildly unrelated, but have you considered using Pprint ( [^] ) instead of the Format module? You may appreciate the simplicity and smaller codebase.
dbuenzli (reporter)
2013-05-07 16:30

Thanks for the pointer but the problem is that I'm writing OCaml libraries. I always provide pretty printers for values of the type they define so that users can quickly perform printf debugging or use in the toplevel. There would be no point in having a dependency on the library you mention however good it may be.
gasche (administrator)
2013-05-07 16:45

Good point.
meyer (developer)
2013-05-07 19:36

I support having need of such an extended interface. I needed to write again and again given functions. Eventually I turned this into mini module and copied across the project. At least pp_print_list is useful, and pp_print_text is something I didn't think of, but it looks also very useful.
gasche (administrator)
2013-08-18 22:49

I just uploaded an equivalent patch that applies as-is against the current trunk. I'm wary of optional parameters in the API (especially since Format does not use any so far), but didn't find a better way to express pp_print_list -- and omitted pp_print_opt for now because I'm not satisfied with the interface.

- Issue History
Date Modified Username Field Change
2013-05-07 15:56 dbuenzli New Issue
2013-05-07 16:10 gasche Note Added: 0009251
2013-05-07 16:30 dbuenzli Note Added: 0009252
2013-05-07 16:45 gasche Note Added: 0009253
2013-05-07 16:45 gasche Status new => acknowledged
2013-05-07 19:36 meyer Note Added: 0009254
2013-08-18 11:29 gasche File Added: 0001-Format-pp_print_list-and-pp_print_text-contributed-b.patch
2013-08-18 22:49 gasche Note Added: 0010188
2013-12-16 15:56 doligez Tag Attached: patch
2017-02-23 16:43 doligez Category OCaml standard library => standard library

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker