daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Feb 16th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. From 2ee07322f47ebf1a365628684dab9d45f506968e Mon Sep 17 00:00:00 2001
  2. From: Gabriel Scherer <gabriel.scherer@inria.fr>
  3. Date: Fri, 16 Aug 2013 16:08:38 +0200
  4. Subject: [PATCH] =?UTF-8?q?Format:=20pp=5Fprint=5Flist=20and=20pp=5Fprint=5F?=
  5.  =?UTF-8?q?text,=20contributed=20by=20Daniel=20B=C3=BCnzli?=
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9.  
  10. ---
  11.  stdlib/format.ml  |   35 +++++++++++++++++++++++++++++++++++
  12.  stdlib/format.mli |   21 +++++++++++++++++++++
  13.  2 files changed, 56 insertions(+)
  14.  
  15. diff --git a/stdlib/format.ml b/stdlib/format.ml
  16. index fc2df51..0222293 100644
  17. --- a/stdlib/format.ml
  18. +++ b/stdlib/format.ml
  19. @@ -747,6 +747,41 @@ let pp_set_tab state () =
  20.      enqueue_advance state elem
  21.  ;;
  22.  
  23. +
  24. +(* Convenience functions *)
  25. +
  26. +(* To format a list *)
  27. +let rec pp_print_list ?(pp_sep = pp_print_cut) pp_v ppf = function
  28. +  | [] -> ()
  29. +  | [v] -> pp_v ppf v
  30. +  | v :: vs ->
  31. +    pp_v ppf v;
  32. +    pp_sep ppf ();
  33. +    pp_print_list ~pp_sep pp_v ppf vs
  34. +
  35. +(* To format free-flowing text *)
  36. +let pp_print_text ppf s =
  37. +  let len = String.length s in
  38. +  let left = ref 0 in
  39. +  let right = ref 0 in
  40. +  let flush () =
  41. +    pp_print_string ppf (String.sub s !left (!right - !left));
  42. +    incr right; left := !right;
  43. +  in
  44. +  while (!right <> len) do
  45. +    match s.[!right] with
  46. +      | '\n' ->
  47. +        flush ();
  48. +        pp_force_newline ppf ()
  49. +      | ' ' ->
  50. +        flush (); pp_print_space ppf ()
  51. +      (* there is no specific support for '\t'
  52. +         as it is unclear what a right semantics would be *)
  53. +      | _ -> incr right
  54. +  done;
  55. +  if !left <> len then flush ()
  56. +
  57. +
  58.  (**************************************************************
  59.  
  60.    Procedures to control the pretty-printers
  61. diff --git a/stdlib/format.mli b/stdlib/format.mli
  62. index 2df4779..1d8662b 100644
  63. --- a/stdlib/format.mli
  64. +++ b/stdlib/format.mli
  65. @@ -564,6 +564,27 @@ val pp_get_formatter_out_functions :
  66.     evaluation of these primitives. For instance,
  67.     [print_string] is equal to [pp_print_string std_formatter]. *)
  68.  
  69. +(** {6 Convenience formatting functions.} *)
  70. +
  71. +val pp_print_list:
  72. +  ?pp_sep:(formatter -> unit -> unit) ->
  73. +  (formatter -> 'a -> unit) -> (formatter -> 'a list -> unit)
  74. +(** [pp_print_list ?pp_sep pp_v ppf l] prints the list [l]. [pp_v] is
  75. +    used on the elements of [l] and each element is separated by
  76. +    a call to [pp_sep] (defaults to {!pp_print_cut}). Does nothing on
  77. +    empty lists.
  78. +
  79. +    @since 4.02
  80. +*)
  81. +
  82. +val pp_print_text : formatter -> string -> unit
  83. +(** [pp_print_text ppf s] prints [s] with spaces and newlines
  84. +    respectively printed with {!pp_print_space} and
  85. +    {!pp_force_newline}.
  86. +
  87. +    @since 4.02
  88. +*)
  89. +
  90.  (** {6 [printf] like functions for pretty-printing.} *)
  91.  
  92.  val fprintf : formatter -> ('a, formatter, unit) format -> 'a;;
  93. --
  94. 1.7.10.4
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top