diff --git a/CHANGES.md b/CHANGES.md index 3f1a17a..0ce1cf1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) * Initial release diff --git a/ohex.ml b/ohex.ml index 8224253..ef60c8d 100644 --- a/ohex.ml +++ b/ohex.ml @@ -78,7 +78,16 @@ let printable_ascii c = let i = int_of_char c in 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 -> if idx mod 16 = 0 && row_numbers then Format.fprintf ppf "%06x " idx; diff --git a/ohex.mli b/ohex.mli index f7020fb..c77fc64 100644 --- a/ohex.mli +++ b/ohex.mli @@ -41,9 +41,13 @@ val encode_into : string -> bytes -> ?off:int -> unit -> unit @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 -(** [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 (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