Skip to content

Commit

Permalink
Add missing Format.pp_print_bytes function.
Browse files Browse the repository at this point in the history
This is implemented through a copy using `Bytes.to_string`. While this
seems sub-optimal, avoiding a copy (either through
`Bytes.unsafe_to_string` or by adding a new formatting action) would
"capture" the bytes but would not immediately print it (formatting
might wait before making indentation decision). During this period the
bytes might be mutated again leading to a confusing behavior.

We avoid this problem by making a copy. Users who require zero-copy
output can reimplement it easily (with `Bytes.unsafe_to_string`) and
ensure proper owernship manually.
  • Loading branch information
Drup committed May 28, 2021
1 parent 9b0847d commit ef6b4b0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ Working version
- #10389, #10391, #10392: Add {Int,Int32,Int64,Nativeint}.{min,max}.
(Nicolás Ojeda Bär and Alain Frisch, review by Xavier Leroy)

- #10430: Add Format.print_bytes and Format.pp_print_bytes.
(Gabriel Radanne, review by Gabriel Scherer and David Allsopp)

### Other libraries:

- #10047: Add `Unix.realpath`
Expand Down
2 changes: 2 additions & 0 deletions stdlib/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ stdlib__Format.cmo : format.ml \
stdlib__Either.cmi \
camlinternalFormatBasics.cmi \
camlinternalFormat.cmi \
stdlib__Bytes.cmi \
stdlib__Buffer.cmi \
stdlib__Format.cmi
stdlib__Format.cmx : format.ml \
Expand All @@ -291,6 +292,7 @@ stdlib__Format.cmx : format.ml \
stdlib__Either.cmx \
camlinternalFormatBasics.cmx \
camlinternalFormat.cmx \
stdlib__Bytes.cmx \
stdlib__Buffer.cmx \
stdlib__Format.cmi
stdlib__Format.cmi : format.mli \
Expand Down
3 changes: 3 additions & 0 deletions stdlib/format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@ let pp_print_as state isize s =
let pp_print_string state s =
pp_print_as state (String.length s) s

let pp_print_bytes state s =
pp_print_as state (Bytes.length s) (Bytes.to_string s)

(* To format an integer. *)
let pp_print_int state i = pp_print_string state (Int.to_string i)
Expand Down Expand Up @@ -1114,6 +1116,7 @@ and open_stag = pp_open_stag std_formatter
and close_stag = pp_close_stag std_formatter
and print_as = pp_print_as std_formatter
and print_string = pp_print_string std_formatter
and print_bytes = pp_print_bytes std_formatter
and print_int = pp_print_int std_formatter
and print_float = pp_print_float std_formatter
and print_char = pp_print_char std_formatter
Expand Down
6 changes: 6 additions & 0 deletions stdlib/format.mli
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ val pp_print_string : formatter -> string -> unit
val print_string : string -> unit
(** [pp_print_string ppf s] prints [s] in the current pretty-printing box. *)

val pp_print_bytes : formatter -> bytes -> unit
val print_bytes : bytes -> unit
(** [pp_print_bytes ppf b] prints [b] in the current pretty-printing box.
@since 4.13.0
*)

val pp_print_as : formatter -> int -> string -> unit
val print_as : int -> string -> unit
(** [pp_print_as ppf len s] prints [s] in the current pretty-printing box.
Expand Down

0 comments on commit ef6b4b0

Please sign in to comment.