Rename pp to pp_hexdump; provide pp for brevity (requested by @palainp)

This commit is contained in:
Hannes Mehnert 2024-03-18 11:52:29 +01:00
parent b50f03c148
commit 663777d365
3 changed files with 21 additions and 3 deletions

View file

@ -1,3 +1,8 @@
## v0.2.0 (2024-03-18)
* Rename pp to pp_hexdump
* Add a pp which just prints the hex with some spaces, but no newlines
## v0.1.0 (2024-03-14) ## v0.1.0 (2024-03-14)
* Initial release * Initial release

11
ohex.ml
View file

@ -78,7 +78,16 @@ let printable_ascii c =
let i = int_of_char c in let i = int_of_char c in
not (i < 0x20 || i >= 0x7f) not (i < 0x20 || i >= 0x7f)
let pp ?(row_numbers = true) ?(chars = true) () ppf s = let pp ppf s =
String.iteri (fun idx c ->
Format.fprintf ppf "%02x" (int_of_char c);
if idx mod 2 = 1 then
Format.pp_print_string ppf " ";
if idx mod 8 = 7 then
Format.pp_print_string ppf " ")
s
let pp_hexdump ?(row_numbers = true) ?(chars = true) () ppf s =
String.iteri (fun idx c -> String.iteri (fun idx c ->
if idx mod 16 = 0 && row_numbers then if idx mod 16 = 0 && row_numbers then
Format.fprintf ppf "%06x " idx; Format.fprintf ppf "%06x " idx;

View file

@ -41,9 +41,13 @@ val encode_into : string -> bytes -> ?off:int -> unit -> unit
@raise Invalid_argument if [dst] does not contain enough space. *) @raise Invalid_argument if [dst] does not contain enough space. *)
val pp : ?row_numbers:bool -> ?chars:bool -> unit -> val pp : Format.formatter -> string -> unit
(** [pp ppf s] pretty-prints the string [s] in hexadecimal. Some spaces are
emitted for easier readability. No newline is emitted. *)
val pp_hexdump : ?row_numbers:bool -> ?chars:bool -> unit ->
Format.formatter -> string -> unit Format.formatter -> string -> unit
(** [pp ~row_numbers ~chars () ppf s] pretty-prints the string [s] in (** [pp_hexdump ~row_numbers ~chars () ppf s] pretty-prints the string [s] in
hexadecimal (similar to [hexdump -C]). If [row_numbers] is provided hexadecimal (similar to [hexdump -C]). If [row_numbers] is provided
(defaults to [true]), each output line is prefixed with the row number. (defaults to [true]), each output line is prefixed with the row number.
If [chars] is provided (defaults to [true]), in the last column the ASCII If [chars] is provided (defaults to [true]), in the last column the ASCII