From 3f7a0911713b4f247a813c319441162b7dca8645 Mon Sep 17 00:00:00 2001 From: Virgile Prevosto <virgile.prevosto@m4x.org> Date: Tue, 29 Jan 2019 19:09:07 +0100 Subject: [PATCH] [lib] ensures Integer.pp_{bin,hex} output at least a single (0) bit. --- src/libraries/stdlib/integer.ml | 6 ++++-- src/libraries/stdlib/integer.mli | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/libraries/stdlib/integer.ml b/src/libraries/stdlib/integer.ml index e41adaacae9..1a7e593f79a 100644 --- a/src/libraries/stdlib/integer.ml +++ b/src/libraries/stdlib/integer.ml @@ -165,7 +165,8 @@ let popcount = Z.popcount d.pp fmt r ; end - let pp_bin ?(nbits=0) ?(sep="") fmt v = + let pp_bin ?(nbits=1) ?(sep="") fmt v = + let nbits = if nbits <= 0 then 1 else nbits in if le zero v then ( Format.pp_print_string fmt "0b" ; pp_digits { nbits ; sep ; bsize=4 ; @@ -175,7 +176,8 @@ let popcount = Z.popcount pp_digits { nbits ; sep ; bsize=4 ; bmask = bmask_bin ; pp = pp_bin_neg } fmt 0 (Z.lognot v) ) - let pp_hex ?(nbits=0) ?(sep="") fmt v = + let pp_hex ?(nbits=1) ?(sep="") fmt v = + let nbits = if nbits <= 0 then 1 else nbits in if le zero v then ( Format.pp_print_string fmt "0x" ; pp_digits { nbits ; sep ; bsize=16 ; diff --git a/src/libraries/stdlib/integer.mli b/src/libraries/stdlib/integer.mli index 7c1c663bea3..3ed0f129747 100644 --- a/src/libraries/stdlib/integer.mli +++ b/src/libraries/stdlib/integer.mli @@ -151,15 +151,17 @@ val popcount: t -> int val pretty : ?hexa:bool -> t Pretty_utils.formatter val pp_bin : ?nbits:int -> ?sep:string -> t Pretty_utils.formatter -(** Print binary format. Digits are output by blocs of 4 bits - separated by [~sep] with at least [~nbits] total bits. +(** Print binary format. Digits are output by blocs of 4 bits + separated by [~sep] with at least [~nbits] total bits. If [nbits] is + non positive, it will be ignored. - Positive values are preffixed with ["0b"] and negative values + Positive values are prefixed with ["0b"] and negative values are printed as their 2-complement ([lnot]) with prefix ["1b"]. *) val pp_hex : ?nbits:int -> ?sep:string -> t Pretty_utils.formatter (** Print hexadecimal format. Digits are output by blocs of 16 bits - (4 hex digits) separated by [~sep] with at least [~nbits] total bits. + (4 hex digits) separated by [~sep] with at least [~nbits] total bits. + If [nbits] is non positive, it will be ignored. Positive values are preffixed with ["0x"] and negative values are printed as their 2-complement ([lnot]) with prefix ["1x"]. *) -- GitLab