Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format.{pp_print_opt, pp_print_list, pp_print_text} #6009

Closed
vicuna opened this issue May 7, 2013 · 6 comments
Closed

Format.{pp_print_opt, pp_print_list, pp_print_text} #6009

vicuna opened this issue May 7, 2013 · 6 comments

Comments

@vicuna
Copy link

vicuna commented May 7, 2013

Original bug ID: 6009
Reporter: @dbuenzli
Status: acknowledged (set by @gasche on 2013-05-07T14:45:15Z)
Resolution: open
Priority: normal
Severity: feature
Version: 4.00.1
Category: standard library
Tags: patch
Monitored by: meyer @hcarty

Bug description

Ok, 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;                      
  in
  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
  done;
  if !left <> len then flush ()

File attachments

@vicuna
Copy link
Author

vicuna commented May 7, 2013

Comment author: @gasche

That's mildly unrelated, but have you considered using Pprint ( http://gallium.inria.fr/~fpottier/pprint/ ) instead of the Format module? You may appreciate the simplicity and smaller codebase.

@vicuna
Copy link
Author

vicuna commented May 7, 2013

Comment author: @dbuenzli

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.

@vicuna
Copy link
Author

vicuna commented May 7, 2013

Comment author: @gasche

Good point.

@vicuna
Copy link
Author

vicuna commented May 7, 2013

Comment author: meyer

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.

@vicuna
Copy link
Author

vicuna commented Aug 18, 2013

Comment author: @gasche

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.

@vicuna vicuna added the stdlib label Mar 14, 2019
@nojb
Copy link
Contributor

nojb commented Mar 15, 2019

These functions have all been merged into the stdlib.

@nojb nojb closed this as completed Mar 15, 2019
caasi added a commit to caasi/ocaml_aoc_2023 that referenced this issue Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants