diff --git a/CHANGES.md b/CHANGES.md index db4de53..de81f0d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.0.0] - 2022-01-12 +### Changed +- Default flags have changed to padding with zeros, with separators and + prefixes, and zero printing behaves just like non-zero printing. Since the + primary purpose of this library is debugging, this should help. +- `*.{make_pp_int,make_to_string}` no longer take an optional `flags` parameter, + and instead takes optional boolean parameters `zero_padding`, `left_padding`, + `separators`, `prefix`, `suffix`, `zero_special`. +- Renamed `*.{pp_binary_int}` to `*.{pp_int_with}` for consistency. + +### Fixed +- Padding with zeros no longer assumes prefixes are always 2 characters long. + ## [0.1.1] - 2021-12-31 ### Added - Support for `int32`, `int64`, and `nativeint`. diff --git a/README.md b/README.md index f8808dd..5deedf3 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,11 @@ opam install pp-binary-ints The library provides four main functions. - `Int.to_string` converts ints to strings. -- `Int.to_string_with ~flags ~min_width` converts ints to strings, customizing the output with `flags` and `min_width`. +- `Int.make_to_string` converts ints to strings, customizing the output with the optional arguments. - `Int.pp_int` is a simple `Format` module style pretty printer. -- `Int.pp_binary_int ~flags ~min_width` is a customizable `Format` module style pretty printer. +- `Int.make_pp_int` is a customizable `Format` module style pretty printer with the customization controlled by the optional arguments. +- `Int.to_string_with` is a lower level function converts ints to strings, customizing the output with the `~flags` and `~min_width` named arguments. +- `Int.pp_int_with` is a lower level customizable `Format` module style pretty printer which takes in named arguments `~flags` and `~min_width`. There are also versions available for `int32`, `int64`, and `nativeint` in the modules @@ -36,17 +38,19 @@ modules A generic functor to generate binary-int printers is provided in the `MakePP` module. +The following demonstrates using the library in a toplevel/REPL. + ## Basic use ```ocaml # #require "pp-binary-ints";; # module Pp_Bin = Pp_binary_ints.Int;; # Pp_Bin.to_string 0b110111;; -- : string = "110111" +- : string = "0b11_0111" # Pp_Bin.to_string 0o777;; -- : string = "111111111" +string = "0b1_1111_1111" # Pp_Bin.to_string 1234;; -- : string = "10011010010" +- : string = "0b100_1101_0010" ``` ## Customizing padding and minimum width @@ -54,48 +58,67 @@ module. ```ocaml # #require "pp-binary-ints";; # module Pp_Bin = Pp_binary_ints.Int;; -# (* Zero Padding *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with padding = Zeros } ~min_width:13 0b110111;; -- : string = "0000000110111" -# (* Default is space padding on the right *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.default ~min_width:13 0b110111;; -- : string = "110111 " +# (* Space Padding *);; +# Pp_Bin.make_to_string ~zero_padding:false ~min_width:13 () 0b110111;; +- : string = "0b11_0111 " # (* Space padding on the left is also possible *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with padding = Left} ~min_width:13 0b110111;; -- : string = " 110111" +# Pp_Bin.make_to_string ~zero_padding:false ~left_padding:true ~min_width:13 () 0b110111;; +- : string = " 0b11_0111" ``` ## Separators and prefixes ```ocaml -# (* Separate every 4 digits with _ *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with separators = true } ~min_width:1 0b110111;; -- : string = "11_0111" -# (* Prefix non-zero *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true } ~min_width:1 0b110111;; +# (* Turn off _ separators *);; +# Pp_Bin.make_to_string ~separators:false ~min_width:1 () 0b110111;; - : string = "0b110111" -# (* Prefix non-zero with separators *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true; separators = true } ~min_width:1 0b110111;; -- : string = "0b11_0111" +# (* Turn off prefixes *);; +# Pp_Bin.make_to_string ~prefix:false ~min_width:1 () 0b110111;; +- : string = "11_0111" +# (* Turn off both separatorns and prefixes *);; +# Pp_Bin.make_to_string ~separators:false ~prefix:false ~min_width:1 () 0b110111;; +- : string = "110111" ``` ## Zero printing behaviour -We support pretty printing `0` (zero) both how OCaml's `Printf` woould print it, -as well as printing it similar to how we print non zero integers. The default -behaviour is to follow `Printf`'s zero printing and not print a prefix, but this -can be changed by setting the `zero_printing` flag to `InheritNonZero`. +You can ask the library to treat `0` (zero) specially and not add a prefix to +it. While it won't add a prefix to it, padding will still be added. ```ocaml -# (* Prefix's are not added to zero by default *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true } ~min_width:1 0;; +# (* Don't prefix zero *);; +# Pp_Bin.make_to_string ~zero_special:true ~min_width:1 () 0;; - : string = "0" -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:1 0;; -- : string = "0b0" -# (* All the above options can be combined *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ padding = Zeros; separators = true; prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:8 0;; -- : string = "0b0_0000" -# (* The library is careful not to write "0b_" when prefixing, 'b' is always followed by a digit *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ padding = Zeros; separators = true; prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:7 0;; -- : string = "0b00000" +# Pp_Bin.make_to_string ~zero_special:true ~min_width:1 () 0b110111;; +- : string = "0b11_0111" +(* Zero Padding still adds zeros to fill up the sapce *) +# Pp_Bin.make_to_string ~zero_special:true ~min_width:9 () 0;; +- : string = "0000_0000" +# Pp_Bin.make_to_string ~zero_special:true ~min_width:9 () 0b110111;; +- : string = "0b11_0111" +``` + +# Printing Binary Ints in the REPL + +```ocaml +# #use "topfind";; +# #require "pp-binary-ints";; +# #install_printer Pp_binary_ints.Int.pp_int;; +# 0;; +- : int = 0b0 +# 7;; +- : int = 0b111 +``` + + +You can also add the following to your `.ocamlinit` file so that integers are +always printed using this library. + +```ocaml +#use "topfind";; +#require "pp-binary-ints";; +#install_printer Pp_binary_ints.Int.pp_int;; +#install_printer Pp_binary_ints.Int32.pp_int;; +#install_printer Pp_binary_ints.Int64.pp_int;; +#install_printer Pp_binary_ints.Nativeint.pp_int;; ``` diff --git a/dune-project b/dune-project index ee68a97..72f3b34 100644 --- a/dune-project +++ b/dune-project @@ -7,7 +7,7 @@ (maintainers "Ifaz Kabir") (source (github ifazk/pp-binary-ints)) (documentation https://ifazk.github.io/pp-binary-ints/) -(version 0.1.1) +(version 1.0.0) (package (name pp-binary-ints) diff --git a/lib/Flags.ml b/lib/Flags.ml index 57254f9..db73cca 100644 --- a/lib/Flags.ml +++ b/lib/Flags.ml @@ -4,19 +4,28 @@ type padding = | Zeros type zero_printing = - | OCaml (* depends on padding setting: bunch of zeros (no separators, no prefixs), or space padding on the left or right *) + | OCaml (* depends on padding setting: no prefixes for zero padding, or space padding on the left or right *) | InheritNonZero type flags = { padding: padding ; separators: bool ; prefix_non_zero: bool + ; suffix : bool ; zero_printing: zero_printing } let default = - { padding = Right - ; separators = false - ; prefix_non_zero = false - ; zero_printing = OCaml + { padding = Zeros + ; separators = true + ; prefix_non_zero = true + ; suffix = true + ; zero_printing = InheritNonZero } + +let make_flags ?(zero_padding = true) ?(left_padding=false) ?(separators=true) + ?(prefix=true) ?(suffix=true) ?(zero_special=false) () = + let padding = if zero_padding then Zeros else (if left_padding then Left else Right) in + let prefix_non_zero = prefix in + let zero_printing = if zero_special then OCaml else InheritNonZero in + { padding; separators; prefix_non_zero; suffix; zero_printing } diff --git a/lib/Flags.mli b/lib/Flags.mli index 5a7ca96..dd0690c 100644 --- a/lib/Flags.mli +++ b/lib/Flags.mli @@ -13,9 +13,22 @@ type flags = { padding : padding; separators : bool; prefix_non_zero : bool; + suffix : bool; zero_printing : zero_printing; } (** [flags] are passed to pretty printing functions to customize the output. *) val default : flags (** A default set of flags. *) + +val make_flags : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> unit -> flags +(** A function for creating flags. The [left_padding] prameter is ignored if + [zero_padding] is set to [true]. The default values are as follows. +{ul {- [zero_padding=true]} + {- [left_padding=false]} + {- [separators=true]} + {- [prefix=true]} + {- [suffix=true]} + {- [zero_special=false]} +} +*) diff --git a/lib/index.mld b/lib/index.mld index 2270c2f..35a8b84 100644 --- a/lib/index.mld +++ b/lib/index.mld @@ -10,9 +10,11 @@ The library provides four main functions. {ul {- {!module-Pp_binary_ints.module-Int.val-to_string} converts ints to strings.} -{- {!module-Pp_binary_ints.module-Int.val-to_string_with} converts ints to strings, customizing the output with the [~flags] and [~min_width] named arguments.} +{- {!module-Pp_binary_ints.module-Int.val-make_to_string} converts ints to strings, customizing the output with the optional arguments.} {- {!module-Pp_binary_ints.module-Int.val-pp_int} is a simple {!module-Format} module style pretty printer.} -{- {!module-Pp_binary_ints.module-Int.val-pp_binary_int} is a customizable {!module-Format} module style pretty printer which takes in named arguments [~flags] and [~min_width].} +{- {!module-Pp_binary_ints.module-Int.val-make_pp_int} is a customizable {!module-Format} pretty printer with the customization controlled by the optional arguments.} +{- {!module-Pp_binary_ints.module-Int.val-to_string_with} is a lower level function converts ints to strings, customizing the output with the [~flags] and [~min_width] named arguments.} +{- {!module-Pp_binary_ints.module-Int.val-pp_int_with} is a lower level customizable {!module-Format} module style pretty printer which takes in named arguments [~flags] and [~min_width].} } The options to customize the outputs can be found in {!module-Pp_binary_ints.Flags}. @@ -25,17 +27,19 @@ the following modules. {- {!module-Pp_binary_ints.module-Nativeint}} } +The following demonstrates using the library in a toplevel/REPL. + {2 Basic use} {[ # #require "pp-binary-ints";; # module Pp_Bin = Pp_binary_ints.Int;; # Pp_Bin.to_string 0b110111;; -- : string = "110111" +- : string = "0b11_0111" # Pp_Bin.to_string 0o777;; -- : string = "111111111" +string = "0b1_1111_1111" # Pp_Bin.to_string 1234;; -- : string = "10011010010" +- : string = "0b100_1101_0010" ]} {2 Customizing padding and minimum width} @@ -43,48 +47,67 @@ the following modules. {[ # #require "pp-binary-ints";; # module Pp_Bin = Pp_binary_ints.Int;; -# (* Zero Padding *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with padding = Zeros } ~min_width:13 0b110111;; -- : string = "0000000110111" -# (* Default is space padding on the right *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.default ~min_width:13 0b110111;; -- : string = "110111 " +# (* Space Padding *);; +# Pp_Bin.make_to_string ~zero_padding:false ~min_width:13 () 0b110111;; +- : string = "0b11_0111 " # (* Space padding on the left is also possible *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with padding = Left} ~min_width:13 0b110111;; -- : string = " 110111" +# Pp_Bin.make_to_string ~zero_padding:false ~left_padding:true ~min_width:13 () 0b110111;; +- : string = " 0b11_0111" ]} {2 Separators and prefixes} {[ -# (* Separate every 4 digits with _ *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with separators = true } ~min_width:1 0b110111;; -- : string = "11_0111" -# (* Prefix non-zero *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true } ~min_width:1 0b110111;; +# (* Turn off _ separators *);; +# Pp_Bin.make_to_string ~separators:false ~min_width:1 () 0b110111;; - : string = "0b110111" -# (* Prefix non-zero with separators *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true; separators = true } ~min_width:1 0b110111;; -- : string = "0b11_0111" +# (* Turn off prefixes *);; +# Pp_Bin.make_to_string ~prefix:false ~min_width:1 () 0b110111;; +- : string = "11_0111" +# (* Turn off both separatorns and prefixes *);; +# Pp_Bin.make_to_string ~separators:false ~prefix:false ~min_width:1 () 0b110111;; +- : string = "110111" ]} {2 Zero printing behaviour} -We support pretty printing `0` (zero) both how OCaml's `Printf` woould print it, -as well as printing it similar to how we print non zero integers. The default -behaviour is to follow [Printf]'s zero printing and not print a prefix, but this -can be changed by setting the [zero_printing] flag to [InheritNonZero]. +You can ask the library to treat [0] (zero) specially and not add a prefix to +it. While it won't add a prefix to it, padding will still be added. {[ -# (* Prefix's are not added to zero by default *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true } ~min_width:1 0;; +# (* Don't prefix zero *);; +# Pp_Bin.make_to_string ~zero_special:true ~min_width:1 () 0;; - : string = "0" -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:1 0;; -- : string = "0b0" -# (* All the above options can be combined *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ padding = Zeros; separators = true; prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:8 0;; -- : string = "0b0_0000" -# (* The library is careful not to write "0b_" when prefixing, 'b' is always followed by a digit *);; -# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ padding = Zeros; separators = true; prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:7 0;; -- : string = "0b00000" +# Pp_Bin.make_to_string ~zero_special:true ~min_width:1 () 0b110111;; +- : string = "0b11_0111" +(* Zero Padding still adds zeros to fill up the sapce *) +# Pp_Bin.make_to_string ~zero_special:true ~min_width:9 () 0;; +- : string = "0000_0000" +# Pp_Bin.make_to_string ~zero_special:true ~min_width:9 () 0b110111;; +- : string = "0b11_0111" +]} + +{1 Printing Binary Ints in the REPL} + +{[ +# #use "topfind";; +# #require "pp-binary-ints";; +# #install_printer Pp_binary_ints.Int.pp_int;; +# 0;; +- : int = 0b0 +# 7;; +- : int = 0b111 +]} + + +You can also add the following to your [.ocamlinit] file so that integers are +always printed using this library. + +{[ +#use "topfind";; +#require "pp-binary-ints";; +#install_printer Pp_binary_ints.Int.pp_int;; +#install_printer Pp_binary_ints.Int32.pp_int;; +#install_printer Pp_binary_ints.Int64.pp_int;; +#install_printer Pp_binary_ints.Nativeint.pp_int;; ]} diff --git a/lib/int.mli b/lib/int.mli index 82050aa..fa600dc 100644 --- a/lib/int.mli +++ b/lib/int.mli @@ -2,59 +2,38 @@ type t = int -module Flags : sig - (** The Flags module contains types to modify how binary integers are - printed. *) - - type padding = Left | Right | Zeros - (** [padding] controls whether spaces are are added on the left or right or if - zero prefixes are added. *) +val pp_int : Format.formatter -> int -> unit +(** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the + formatter [fmt]. *) - type zero_printing = OCaml | InheritNonZero - (** [zero_printing] controls whether zeros printed similar to how [Printf] - prints zeros or if zeros are printed similar to non-zero integers. *) +val make_pp_int : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> ?min_width:int -> unit -> Format.formatter -> int -> unit +(** [make_pp_int ?zero_padding ?left_padding ?separators ?prefix ?suffix + ?zero_special ?min_width ?min_width ()] is just [pp__int_with + ~flags:(Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix + ?suffix ?zero_special) ~min_width], but the prameters are optional. The + default value of [min_width] is [1]. This version is nicer when using the + [Format] or [Fmt] modules. *) - type flags = { - padding : padding; - separators : bool; - prefix_non_zero : bool; - zero_printing : zero_printing; - } - (** [flags] are passed to pretty printing functions to customize the - output. *) +val to_string : int -> string +(** [to_string n] converts the integer [n] to a binary integer [n]. *) - val default : flags - (** A default set of flags. *) -end +val make_to_string : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> ?min_width:int -> unit -> int -> string +(** [make_to_string ?zero_padding ?left_padding ?separators ?prefix ?suffix + ?zero_special ?min_width ()] is just [to_string_with + ~flags:(Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix + ?suffix ?zero_special) ~min_width], but the parameters are all optional. The + default value of [min_width] is [1]. *) -val pp_binary_int : - flags:Flags.flags -> min_width:int -> Format.formatter -> int -> unit -(** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the +val pp_int_with : flags:Flags.flags -> min_width:int -> Format.formatter -> int -> unit +(** [pp_int_with ~flags ~min_width fmt n] prints the integer [n] on the formatter [fmt], customizing the output with [~flags]. [~min_width] pads the output with enough spaces or zeros so that the output is at least - [~min_width] characters, depends on [flags.padding]. *) - -val pp_int : Format.formatter -> int -> unit -(** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the formatter [fmt]. *) - -val make_pp_int : - ?flags:Flags.flags -> ?min_width:int -> unit -> Format.formatter -> int -> unit -(** [make_pp_int ?flags ?min_width ()] is just [pp_binary_int ~flags - ~min_width], but [flags] and [min_width] are optional. If omitted default - values of [Flags.default] and [1] are passed to pp_binary_int. This version - is nicer to with with the [Format] or [Fmt] modules. *) + [~min_width] characters, depends on [flags.padding]. This is a lower level + version of [make_pp_int]. *) val to_string_with : flags:Flags.flags -> min_width:int -> int -> string (** [to_string_width ~flags ~min_width n] converts the integer [n] to a binary integer [n]. customizing the output with [~flags]. [~min_width] pads the output with enough spaces or zeros so that the output is at least - [~min_width] characters, depends on [flags.padding]. *) - -val to_string : int -> string -(** [to_string n] converts the integer [n] to a binary integer [n]. *) - -val make_to_string : - ?flags:Flags.flags -> ?min_width:int -> unit -> int -> string -(** [make_to_string ?flags ?min_width ()] is just [to_string_with ~flags - ~min_width], but [flags] and [min_width] are optional. If omitted default - values of [Flags.default] and [1] are passed to to_string_with. *) + [~min_width] characters, depends on [flags.padding]. This is a lower level + version of [make_to_string]. *) diff --git a/lib/int32.mli b/lib/int32.mli index c541edc..58e61e9 100644 --- a/lib/int32.mli +++ b/lib/int32.mli @@ -2,34 +2,38 @@ type t = int32 -val pp_binary_int : - flags:Flags.flags -> min_width:int -> Format.formatter -> int32 -> unit +val pp_int : Format.formatter -> int32 -> unit (** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the - formatter [fmt], customizing the output with [~flags]. [~min_width] pads the - output with enough spaces or zeros so that the output is at least - [~min_width] characters, depends on [flags.padding]. *) + formatter [fmt]. *) -val pp_int : Format.formatter -> int32 -> unit -(** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the formatter [fmt]. *) +val make_pp_int : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> ?min_width:int -> unit -> Format.formatter -> int32 -> unit +(** [make_pp_int ?zero_padding ?left_padding ?separators ?prefix ?suffix + ?zero_special ?min_width ?min_width ()] is just [pp__int_with + ~flags:(Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix + ?suffix ?zero_special) ~min_width], but the prameters are optional. The + default value of [min_width] is [1]. This version is nicer when using the + [Format] or [Fmt] modules. *) + +val to_string : int32 -> string +(** [to_string n] converts the integer [n] to a binary integer [n]. *) -val make_pp_int : - ?flags:Flags.flags -> ?min_width:int -> unit -> Format.formatter -> int32 -> unit -(** [make_pp_int ?flags ?min_width ()] is just [pp_binary_int ~flags - ~min_width], but [flags] and [min_width] are optional. If omitted default - values of [Flags.default] and [1] are passed to pp_binary_int. This version - is nicer to with with the [Format] or [Fmt] modules. *) +val make_to_string : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> ?min_width:int -> unit -> int32 -> string +(** [make_to_string ?zero_padding ?left_padding ?separators ?prefix ?suffix + ?zero_special ?min_width ()] is just [to_string_with + ~flags:(Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix + ?suffix ?zero_special) ~min_width], but the parameters are all optional. The + default value of [min_width] is [1]. *) + +val pp_int_with : flags:Flags.flags -> min_width:int -> Format.formatter -> int32 -> unit +(** [pp_int_with ~flags ~min_width fmt n] prints the integer [n] on the + formatter [fmt], customizing the output with [~flags]. [~min_width] pads the + output with enough spaces or zeros so that the output is at least + [~min_width] characters, depends on [flags.padding]. This is a lower level + version of [make_pp_int]. *) val to_string_with : flags:Flags.flags -> min_width:int -> int32 -> string (** [to_string_width ~flags ~min_width n] converts the integer [n] to a binary integer [n]. customizing the output with [~flags]. [~min_width] pads the output with enough spaces or zeros so that the output is at least - [~min_width] characters, depends on [flags.padding]. *) - -val to_string : int32 -> string -(** [to_string n] converts the integer [n] to a binary integer [n]. *) - -val make_to_string : - ?flags:Flags.flags -> ?min_width:int -> unit -> int32 -> string -(** [make_to_string ?flags ?min_width ()] is just [to_string_with ~flags - ~min_width], but [flags] and [min_width] are optional. If omitted default - values of [Flags.default] and [1] are passed to to_string_with. *) + [~min_width] characters, depends on [flags.padding]. This is a lower level + version of [make_to_string]. *) diff --git a/lib/int64.mli b/lib/int64.mli index edb3af3..68baa65 100644 --- a/lib/int64.mli +++ b/lib/int64.mli @@ -2,34 +2,38 @@ type t = int64 -val pp_binary_int : - flags:Flags.flags -> min_width:int -> Format.formatter -> int64 -> unit +val pp_int : Format.formatter -> int64 -> unit (** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the - formatter [fmt], customizing the output with [~flags]. [~min_width] pads the - output with enough spaces or zeros so that the output is at least - [~min_width] characters, depends on [flags.padding]. *) + formatter [fmt]. *) -val pp_int : Format.formatter -> int64 -> unit -(** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the formatter [fmt]. *) +val make_pp_int : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> ?min_width:int -> unit -> Format.formatter -> int64 -> unit +(** [make_pp_int ?zero_padding ?left_padding ?separators ?prefix ?suffix + ?zero_special ?min_width ?min_width ()] is just [pp__int_with + ~flags:(Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix + ?suffix ?zero_special) ~min_width], but the prameters are optional. The + default value of [min_width] is [1]. This version is nicer when using the + [Format] or [Fmt] modules. *) + +val to_string : int64 -> string +(** [to_string n] converts the integer [n] to a binary integer [n]. *) -val make_pp_int : - ?flags:Flags.flags -> ?min_width:int -> unit -> Format.formatter -> int64 -> unit -(** [make_pp_int ?flags ?min_width ()] is just [pp_binary_int ~flags - ~min_width], but [flags] and [min_width] are optional. If omitted default - values of [Flags.default] and [1] are passed to pp_binary_int. This version - is nicer to with with the [Format] or [Fmt] modules. *) +val make_to_string : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> ?min_width:int -> unit -> int64 -> string +(** [make_to_string ?zero_padding ?left_padding ?separators ?prefix ?suffix + ?zero_special ?min_width ()] is just [to_string_with + ~flags:(Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix + ?suffix ?zero_special) ~min_width], but the parameters are all optional. The + default value of [min_width] is [1]. *) + +val pp_int_with : flags:Flags.flags -> min_width:int -> Format.formatter -> int64 -> unit +(** [pp_int_with ~flags ~min_width fmt n] prints the integer [n] on the + formatter [fmt], customizing the output with [~flags]. [~min_width] pads the + output with enough spaces or zeros so that the output is at least + [~min_width] characters, depends on [flags.padding]. This is a lower level + version of [make_pp_int]. *) val to_string_with : flags:Flags.flags -> min_width:int -> int64 -> string (** [to_string_width ~flags ~min_width n] converts the integer [n] to a binary integer [n]. customizing the output with [~flags]. [~min_width] pads the output with enough spaces or zeros so that the output is at least - [~min_width] characters, depends on [flags.padding]. *) - -val to_string : int64 -> string -(** [to_string n] converts the integer [n] to a binary integer [n]. *) - -val make_to_string : - ?flags:Flags.flags -> ?min_width:int -> unit -> int64 -> string -(** [make_to_string ?flags ?min_width ()] is just [to_string_with ~flags - ~min_width], but [flags] and [min_width] are optional. If omitted default - values of [Flags.default] and [1] are passed to to_string_with. *) + [~min_width] characters, depends on [flags.padding]. This is a lower level + version of [make_to_string]. *) diff --git a/lib/internal.ml b/lib/internal.ml index 1cf190d..f22f72c 100644 --- a/lib/internal.ml +++ b/lib/internal.ml @@ -125,11 +125,12 @@ module Make (I : S) (Dec : D) = struct let min_width = max (1 + prefix_size) min_width in push_chars ~buf ~separators ~prefix_size ~min_width ~count:0 rev_bits - let rev_buffer ~separators ~prefix ~min_width n : Buffer.t = + let rev_buffer ~separators ~prefix ~suffix ~min_width n : Buffer.t = let buf = Buffer.create 16 in (* TODO: consider min_width for initial size *) let prefix_size = (if prefix then Decorators.prefix_size else 0) in - ( Decorators.push_rev_suffix buf - ; push_bit_chars ~buf ~separators ~prefix_size ~min_width:(IntUtils.nat_minus min_width Decorators.suffix_size) n + let suffix_size = (if suffix then Decorators.suffix_size else 0) in + ( (if suffix then Decorators.push_rev_suffix buf) + ; push_bit_chars ~buf ~separators ~prefix_size ~min_width:(IntUtils.nat_minus min_width suffix_size) n ; if prefix then Decorators.push_rev_prefix buf ; buf @@ -138,10 +139,10 @@ module Make (I : S) (Dec : D) = struct (* end of assuming zero padding *) - let pp_binary_int ~flags ~min_width fmt n = + let pp_int_with ~flags ~min_width fmt n = let open Flags in let min_width = max 1 min_width in - let {padding; prefix_non_zero; separators; zero_printing} = flags in + let {padding; prefix_non_zero; suffix; separators; zero_printing} = flags in let buf = match padding with | Left | Right -> @@ -150,15 +151,15 @@ module Make (I : S) (Dec : D) = struct rev_buffer ~separators ~prefix + ~suffix ~min_width n | Zeros -> if (zero_printing = OCaml) && I.equal n I.zero then - rev_buffer ~separators ~prefix:false ~min_width I.zero + rev_buffer ~separators ~prefix:false ~suffix ~min_width I.zero else let prefix = prefix_non_zero in - let min_width = max min_width (if prefix then 3 else 1) in - rev_buffer ~separators ~prefix ~min_width n + rev_buffer ~separators ~prefix ~suffix ~min_width n in let len = Buffer.length buf in match padding with @@ -176,18 +177,20 @@ module Make (I : S) (Dec : D) = struct PPUtils.pp_rev_buffer fmt buf - let make_pp_int ?(flags=Flags.default) ?(min_width=1) () = - pp_binary_int ~flags ~min_width + let make_pp_int ?zero_padding ?left_padding ?separators ?prefix ?suffix ?zero_special ?(min_width=1) () = + let flags = Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix ?suffix ?zero_special () in + pp_int_with ~flags ~min_width let pp_int fmt n = - pp_binary_int ~flags:(Flags.default) ~min_width:1 fmt n + pp_int_with ~flags:(Flags.default) ~min_width:1 fmt n let to_string n : string = Format.asprintf "%a" pp_int n let to_string_with ~flags ~min_width : I.t -> string = - Format.asprintf "%a" (pp_binary_int ~flags ~min_width) + Format.asprintf "%a" (pp_int_with ~flags ~min_width) - let make_to_string ?(flags=Flags.default) ?(min_width=1) () = + let make_to_string ?zero_padding ?left_padding ?separators ?prefix ?suffix ?zero_special ?(min_width=1) () = + let flags = Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix ?suffix ?zero_special () in to_string_with ~flags ~min_width end diff --git a/lib/makePP.mli b/lib/makePP.mli index 3cf1b6b..4d0e9ea 100644 --- a/lib/makePP.mli +++ b/lib/makePP.mli @@ -21,32 +21,39 @@ end module Make (I : S) (Dec : D) : sig type t = I.t - val pp_binary_int : flags:Flags.flags -> min_width:int -> Format.formatter -> I.t -> unit + val pp_int : Format.formatter -> I.t -> unit (** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the - formatter [fmt], customizing the output with [~flags]. [~min_width] pads - the output with enough spaces or zeros so that the output is at least - [~min_width] characters, depends on [flags.padding]. *) + formatter [fmt]. *) - val pp_int : Format.formatter -> I.t -> unit - (** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the formatter [fmt]. *) + val make_pp_int : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> ?min_width:int -> unit -> Format.formatter -> I.t -> unit + (** [make_pp_int ?zero_padding ?left_padding ?separators ?prefix ?suffix + ?zero_special ?min_width ?min_width ()] is just [pp__int_with + ~flags:(Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix + ?suffix ?zero_special) ~min_width], but the prameters are optional. The + default value of [min_width] is [1]. This version is nicer when using the + [Format] or [Fmt] modules. *) + + val to_string : I.t -> string + (** [to_string n] converts the integer [n] to a binary integer [n]. *) - val make_pp_int : ?flags:Flags.flags -> ?min_width:int -> unit -> Format.formatter -> I.t -> unit - (** [make_pp_int ?flags ?min_width ()] is just [pp_binary_int ~flags - ~min_width], but [flags] and [min_width] are optional. If omitted default - values of [Flags.default] and [1] are passed to pp_binary_int. This - version is nicer to with with the [Format] or [Fmt] modules. *) + val make_to_string : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> ?min_width:int -> unit -> I.t -> string + (** [make_to_string ?zero_padding ?left_padding ?separators ?prefix ?suffix + ?zero_special ?min_width ()] is just [to_string_with + ~flags:(Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix + ?suffix ?zero_special) ~min_width], but the parameters are all optional. + The default value of [min_width] is [1]. *) + + val pp_int_with : flags:Flags.flags -> min_width:int -> Format.formatter -> I.t -> unit + (** [pp_int_with ~flags ~min_width fmt n] prints the integer [n] on the + formatter [fmt], customizing the output with [~flags]. [~min_width] pads + the output with enough spaces or zeros so that the output is at least + [~min_width] characters, depends on [flags.padding]. This is a lower level + version of [make_pp_int]. *) val to_string_with : flags:Flags.flags -> min_width:int -> I.t -> string (** [to_string_width ~flags ~min_width n] converts the integer [n] to a binary integer [n]. customizing the output with [~flags]. [~min_width] pads the output with enough spaces or zeros so that the output is at least - [~min_width] characters, depends on [flags.padding]. *) - - val to_string : I.t -> string - (** [to_string n] converts the integer [n] to a binary integer [n]. *) - - val make_to_string : ?flags:Flags.flags -> ?min_width:int -> unit -> I.t -> string - (** [make_to_string ?flags ?min_width ()] is just [to_string_with ~flags - ~min_width], but [flags] and [min_width] are optional. If omitted default - values of [Flags.default] and [1] are passed to to_string_with. *) + [~min_width] characters, depends on [flags.padding]. This is a lower level + version of [make_to_string]. *) end diff --git a/lib/nativeint.mli b/lib/nativeint.mli index 16069b3..3256b00 100644 --- a/lib/nativeint.mli +++ b/lib/nativeint.mli @@ -2,34 +2,38 @@ type t = nativeint -val pp_binary_int : - flags:Flags.flags -> min_width:int -> Format.formatter -> nativeint -> unit +val pp_int : Format.formatter -> nativeint -> unit (** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the - formatter [fmt], customizing the output with [~flags]. [~min_width] pads the - output with enough spaces or zeros so that the output is at least - [~min_width] characters, depends on [flags.padding]. *) + formatter [fmt]. *) -val pp_int : Format.formatter -> nativeint -> unit -(** [pp_binary_int ~flags ~min_width fmt n] prints the integer [n] on the formatter [fmt]. *) +val make_pp_int : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> ?min_width:int -> unit -> Format.formatter -> nativeint -> unit +(** [make_pp_int ?zero_padding ?left_padding ?separators ?prefix ?suffix + ?zero_special ?min_width ?min_width ()] is just [pp__int_with + ~flags:(Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix + ?suffix ?zero_special) ~min_width], but the prameters are optional. The + default value of [min_width] is [1]. This version is nicer when using the + [Format] or [Fmt] modules. *) + +val to_string : nativeint -> string +(** [to_string n] converts the integer [n] to a binary integer [n]. *) -val make_pp_int : - ?flags:Flags.flags -> ?min_width:int -> unit -> Format.formatter -> nativeint -> unit -(** [make_pp_int ?flags ?min_width ()] is just [pp_binary_int ~flags - ~min_width], but [flags] and [min_width] are optional. If omitted default - values of [Flags.default] and [1] are passed to pp_binary_int. This version - is nicer to with with the [Format] or [Fmt] modules. *) +val make_to_string : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> ?min_width:int -> unit -> nativeint -> string +(** [make_to_string ?zero_padding ?left_padding ?separators ?prefix ?suffix + ?zero_special ?min_width ()] is just [to_string_with + ~flags:(Flags.make_flags ?zero_padding ?left_padding ?separators ?prefix + ?suffix ?zero_special) ~min_width], but the parameters are all optional. The + default value of [min_width] is [1]. *) + +val pp_int_with : flags:Flags.flags -> min_width:int -> Format.formatter -> nativeint -> unit +(** [pp_int_with ~flags ~min_width fmt n] prints the integer [n] on the + formatter [fmt], customizing the output with [~flags]. [~min_width] pads the + output with enough spaces or zeros so that the output is at least + [~min_width] characters, depends on [flags.padding]. This is a lower level + version of [make_pp_int]. *) val to_string_with : flags:Flags.flags -> min_width:int -> nativeint -> string (** [to_string_width ~flags ~min_width n] converts the integer [n] to a binary integer [n]. customizing the output with [~flags]. [~min_width] pads the output with enough spaces or zeros so that the output is at least - [~min_width] characters, depends on [flags.padding]. *) - -val to_string : nativeint -> string -(** [to_string n] converts the integer [n] to a binary integer [n]. *) - -val make_to_string : - ?flags:Flags.flags -> ?min_width:int -> unit -> nativeint -> string -(** [make_to_string ?flags ?min_width ()] is just [to_string_with ~flags - ~min_width], but [flags] and [min_width] are optional. If omitted default - values of [Flags.default] and [1] are passed to to_string_with. *) + [~min_width] characters, depends on [flags.padding]. This is a lower level + version of [make_to_string]. *) diff --git a/pp-binary-ints.opam b/pp-binary-ints.opam index 562ef83..d444c44 100644 --- a/pp-binary-ints.opam +++ b/pp-binary-ints.opam @@ -1,6 +1,6 @@ # This file is generated by dune, edit dune-project instead opam-version: "2.0" -version: "0.1.1" +version: "1.0.0" synopsis: "Pretty Printing Binary Integers" description: """ Pretty Printing Binary Integers diff --git a/test/large.ml b/test/large.ml index 794f1d5..68b5bb7 100644 --- a/test/large.ml +++ b/test/large.ml @@ -1,6 +1,6 @@ open Pp_binary_ints.Int -open Flags +open Pp_binary_ints.Flags let configs = [ { default with separators = false; prefix_non_zero = false } @@ -24,11 +24,11 @@ let test_width configs min_width n : string list = (** * Five printing *) let%test "one default" = - "101" = + "0b101" = to_string 0b101 let%test "one default manual" = - "101" = + "0b101" = to_string_with ~flags:default ~min_width:1 0b101 (** ** Zero Padding, Inherit *) @@ -120,11 +120,11 @@ let%test "right inherit 6" = (** * Eight printing *) let%test "one default" = - "1000" = + "0b1000" = to_string 0b1000 let%test "one default manual" = - "1000" = + "0b1000" = to_string_with ~flags:default ~min_width:1 0b1000 (** ** Zero Padding, Inherit *) @@ -224,11 +224,11 @@ let%test "right inherit 7" = (** * Seventeen printing *) let%test "one default" = - "10001" = + "0b1_0001" = to_string 0b10001 let%test "one default manual" = - "10001" = + "0b1_0001" = to_string_with ~flags:default ~min_width:1 0b10001 (** ** Zero Padding, Inherit *) diff --git a/test/large_int32.ml b/test/large_int32.ml index feb2e1f..82f2587 100644 --- a/test/large_int32.ml +++ b/test/large_int32.ml @@ -24,11 +24,11 @@ let test_width configs min_width n : string list = (** * Five printing *) let%test "one default" = - "101l" = + "0b101l" = to_string 0b101l let%test "one default manual" = - "101l" = + "0b101l" = to_string_with ~flags:default ~min_width:1 0b101l (** ** Zero Padding, Inherit *) @@ -149,11 +149,11 @@ let%test "right inherit 7" = (** * Eight printing *) let%test "one default" = - "1000l" = + "0b1000l" = to_string 0b1000l let%test "one default manual" = - "1000l" = + "0b1000l" = to_string_with ~flags:default ~min_width:1 0b1000l (** ** Zero Padding, Inherit *) @@ -265,11 +265,11 @@ let%test "right inherit 8" = (** * Seventeen printing *) let%test "one default" = - "10001l" = + "0b1_0001l" = to_string 0b10001l let%test "one default manual" = - "10001l" = + "0b1_0001l" = to_string_with ~flags:default ~min_width:1 0b10001l (** ** Zero Padding, Inherit *) diff --git a/test/large_int64.ml b/test/large_int64.ml index b5525c9..b617495 100644 --- a/test/large_int64.ml +++ b/test/large_int64.ml @@ -24,11 +24,11 @@ let test_width configs min_width n : string list = (** * Five printing *) let%test "one default" = - "101L" = + "0b101L" = to_string 0b101L let%test "one default manual" = - "101L" = + "0b101L" = to_string_with ~flags:default ~min_width:1 0b101L (** ** Zero Padding, Inherit *) @@ -149,11 +149,11 @@ let%test "right inherit 7" = (** * Eight printing *) let%test "one default" = - "1000L" = + "0b1000L" = to_string 0b1000L let%test "one default manual" = - "1000L" = + "0b1000L" = to_string_with ~flags:default ~min_width:1 0b1000L (** ** Zero Padding, Inherit *) @@ -265,11 +265,11 @@ let%test "right inherit 8" = (** * Seventeen printing *) let%test "one default" = - "10001L" = + "0b1_0001L" = to_string 0b10001L let%test "one default manual" = - "10001L" = + "0b1_0001L" = to_string_with ~flags:default ~min_width:1 0b10001L (** ** Zero Padding, Inherit *) @@ -680,3 +680,280 @@ let%test "right inherit -1 84" = ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111L " ] = test_width right_inherit_configs 84 (-1L) + +(** * No suffix tests for -1 *) +let zero_inherit_configs = + List.map (fun x -> { x with padding = Zeros; zero_printing = InheritNonZero; suffix = false }) configs + +let left_inherit_configs = + List.map (fun x -> { x with padding = Left; zero_printing = InheritNonZero; suffix = false }) configs + +let right_inherit_configs = + List.map (fun x -> { x with padding = Right; zero_printing = InheritNonZero; suffix = false }) configs + +let test_width configs min_width n : string list = + List.map (fun flags -> to_string_with ~flags ~min_width n) configs + +let%test "no suffix zero inherit -1 1" = + ["1111111111111111111111111111111111111111111111111111111111111111" + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 1 (-1L) + +let%test "no suffix left inherit -1 1" = + ["1111111111111111111111111111111111111111111111111111111111111111" + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 1 (-1L) + +let%test "no suffix right inherit -1 1" = + ["1111111111111111111111111111111111111111111111111111111111111111" + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width right_inherit_configs 1 (-1L) + +let%test "no suffix zero inherit -1 64" = + ["1111111111111111111111111111111111111111111111111111111111111111" + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 64 (-1L) + +let%test "no suffix left inherit -1 64" = + ["1111111111111111111111111111111111111111111111111111111111111111" + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 64 (-1L) + +let%test "no suffix right inherit -1 64" = + ["1111111111111111111111111111111111111111111111111111111111111111" + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width right_inherit_configs 64 (-1L) + +let%test "no suffix zero inherit -1 65" = + ["01111111111111111111111111111111111111111111111111111111111111111" + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 65 (-1L) + +let%test "no suffix left inherit -1 65" = + [" 1111111111111111111111111111111111111111111111111111111111111111" + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 65 (-1L) + +let%test "no suffix right inherit -1 65" = + ["1111111111111111111111111111111111111111111111111111111111111111 " + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width right_inherit_configs 65 (-1L) + +let%test "no suffix zero inherit -1 66" = + ["001111111111111111111111111111111111111111111111111111111111111111" + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 66 (-1L) + +let%test "no suffix left inherit -1 66" = + [" 1111111111111111111111111111111111111111111111111111111111111111" + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 66 (-1L) + +let%test "no suffix right inherit -1 66" = + ["1111111111111111111111111111111111111111111111111111111111111111 " + ;"0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width right_inherit_configs 66 (-1L) + +let%test "no suffix zero inherit -1 67" = + ["0001111111111111111111111111111111111111111111111111111111111111111" + ;"0b01111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 67 (-1L) + +let%test "no suffix left inherit -1 68" = + [" 1111111111111111111111111111111111111111111111111111111111111111" + ;" 0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 67 (-1L) + +let%test "no suffix right inherit -1 68" = + ["1111111111111111111111111111111111111111111111111111111111111111 " + ;"0b1111111111111111111111111111111111111111111111111111111111111111 " + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width right_inherit_configs 67 (-1L) + +let%test "no suffix zero inherit -1 78" = + ["000000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"0b0000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 78 (-1L) + +let%test "no suffix left inherit -1 78" = + [" 1111111111111111111111111111111111111111111111111111111111111111" + ;" 0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 78 (-1L) + +let%test "no suffix right inherit -1 78" = + ["1111111111111111111111111111111111111111111111111111111111111111 " + ;"0b1111111111111111111111111111111111111111111111111111111111111111 " + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width right_inherit_configs 78 (-1L) + +let%test "no suffix zero inherit -1 79" = + ["0000000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"0b00000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 79 (-1L) + +let%test "no suffix left inherit -1 79" = + [" 1111111111111111111111111111111111111111111111111111111111111111" + ;" 0b1111111111111111111111111111111111111111111111111111111111111111" + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 79 (-1L) + +let%test "no suffix right inherit -1 79" = + ["1111111111111111111111111111111111111111111111111111111111111111 " + ;"0b1111111111111111111111111111111111111111111111111111111111111111 " + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width right_inherit_configs 79 (-1L) + +let%test "no suffix zero inherit -1 80" = + ["00000000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"0b000000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"01111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 80 (-1L) + +let%test "no suffix left inherit -1 80" = + [" 1111111111111111111111111111111111111111111111111111111111111111" + ;" 0b1111111111111111111111111111111111111111111111111111111111111111" + ;" 1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 80 (-1L) + +let%test "no suffix right inherit -1 80" = + ["1111111111111111111111111111111111111111111111111111111111111111 " + ;"0b1111111111111111111111111111111111111111111111111111111111111111 " + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111 " + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width right_inherit_configs 80 (-1L) + +let%test "no suffix zero inherit -1 81" = + ["000000000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"0b0000000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"0_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 81 (-1L) + +let%test "no suffix left inherit -1 82" = + [" 1111111111111111111111111111111111111111111111111111111111111111" + ;" 0b1111111111111111111111111111111111111111111111111111111111111111" + ;" 1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 81 (-1L) + +let%test "no suffix right inherit -1 81" = + ["1111111111111111111111111111111111111111111111111111111111111111 " + ;"0b1111111111111111111111111111111111111111111111111111111111111111 " + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111 " + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width right_inherit_configs 81 (-1L) + +let%test "no suffix zero inherit -1 82" = + ["0000000000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"0b00000000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"00_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b01111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 82 (-1L) + +let%test "no suffix left inherit -1 82" = + [" 1111111111111111111111111111111111111111111111111111111111111111" + ;" 0b1111111111111111111111111111111111111111111111111111111111111111" + ;" 1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;" 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 82 (-1L) + +let%test "no suffix right inherit -1 82" = + ["1111111111111111111111111111111111111111111111111111111111111111 " + ;"0b1111111111111111111111111111111111111111111111111111111111111111 " + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111 " + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111 " + ] = + test_width right_inherit_configs 82 (-1L) + +let%test "no suffix zero inherit -1 83" = + ["00000000000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"0b000000000000000001111111111111111111111111111111111111111111111111111111111111111" + ;"000_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;"0b0_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width zero_inherit_configs 83 (-1L) + +let%test "no suffix left inherit -1 83" = + [" 1111111111111111111111111111111111111111111111111111111111111111" + ;" 0b1111111111111111111111111111111111111111111111111111111111111111" + ;" 1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ;" 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111" + ] = + test_width left_inherit_configs 83 (-1L) + +let%test "no suffix right inherit -1 83" = + ["1111111111111111111111111111111111111111111111111111111111111111 " + ;"0b1111111111111111111111111111111111111111111111111111111111111111 " + ;"1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111 " + ;"0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111 " + ] = + test_width right_inherit_configs 83 (-1L) diff --git a/test/large_native64.ml b/test/large_native64.ml index 9d4db6e..f3565f9 100644 --- a/test/large_native64.ml +++ b/test/large_native64.ml @@ -24,11 +24,11 @@ let test_width configs min_width n : string list = (** * Five printing *) let%test "one default" [@tags "64-bits-only"] = - "101n" [@tags "64-bits-only"] = + "0b101n" = to_string 0b101n let%test "one default manual" [@tags "64-bits-only"] = - "101n" [@tags "64-bits-only"] = + "0b101n" = to_string_with ~flags:default ~min_width:1 0b101n (** ** Zero Padding, Inherit *) @@ -149,11 +149,11 @@ let%test "right inherit 7" [@tags "64-bits-only"] = (** * Eight printing *) let%test "one default" [@tags "64-bits-only"] = - "1000n" [@tags "64-bits-only"] = + "0b1000n" = to_string 0b1000n let%test "one default manual" [@tags "64-bits-only"] = - "1000n" [@tags "64-bits-only"] = + "0b1000n" = to_string_with ~flags:default ~min_width:1 0b1000n (** ** Zero Padding, Inherit *) @@ -265,11 +265,11 @@ let%test "right inherit 8" [@tags "64-bits-only"] = (** * Seventeen printing *) let%test "one default" [@tags "64-bits-only"] = - "10001n" [@tags "64-bits-only"] = + "0b1_0001n" = to_string 0b10001n let%test "one default manual" [@tags "64-bits-only"] = - "10001n" [@tags "64-bits-only"] = + "0b1_0001n" = to_string_with ~flags:default ~min_width:1 0b10001n (** ** Zero Padding, Inherit *) diff --git a/test/ones.ml b/test/ones.ml index e855781..86a590a 100644 --- a/test/ones.ml +++ b/test/ones.ml @@ -1,6 +1,6 @@ open Pp_binary_ints.Int -open Flags +open Pp_binary_ints.Flags let configs = [ { default with separators = false; prefix_non_zero = false } @@ -33,11 +33,11 @@ let test_width configs min_width n : string list = (** * One printing *) let%test "one default" = - "1" = + "0b1" = to_string 1 let%test "one default manual" = - "1" = + "0b1" = to_string_with ~flags:default ~min_width:1 1 (** ** Zero Padding, OCaml *) diff --git a/test/ones_int32.ml b/test/ones_int32.ml index da02dea..a07da60 100644 --- a/test/ones_int32.ml +++ b/test/ones_int32.ml @@ -33,11 +33,11 @@ let test_width configs min_width n : string list = (** * One printing *) let%test "one default" = - "1l" = + "0b1l" = to_string 1l let%test "one default manual" = - "1l" = + "0b1l" = to_string_with ~flags:default ~min_width:1 1l (** ** Zero Padding, OCaml *) diff --git a/test/ones_int64.ml b/test/ones_int64.ml index 703083b..be11f50 100644 --- a/test/ones_int64.ml +++ b/test/ones_int64.ml @@ -33,11 +33,11 @@ let test_width configs min_width n : string list = (** * One printing *) let%test "one default" = - "1L" = + "0b1L" = to_string 1L let%test "one default manual" = - "1L" = + "0b1L" = to_string_with ~flags:default ~min_width:1 1L (** ** Zero Padding, OCaml *) diff --git a/test/ones_native.ml b/test/ones_native.ml index afcec35..cac3d9f 100644 --- a/test/ones_native.ml +++ b/test/ones_native.ml @@ -33,11 +33,11 @@ let test_width configs min_width n : string list = (** * One printing *) let%test "one default" = - "1n" = + "0b1n" = to_string 1n let%test "one default manual" = - "1n" = + "0b1n" = to_string_with ~flags:default ~min_width:1 1n (** ** Zero Padding, OCaml *) diff --git a/test/zeros.ml b/test/zeros.ml index 9cf6ee5..ccc25ee 100644 --- a/test/zeros.ml +++ b/test/zeros.ml @@ -1,6 +1,6 @@ open Pp_binary_ints.Int -open Flags +open Pp_binary_ints.Flags let configs = [ { default with separators = false; prefix_non_zero = false } @@ -33,11 +33,11 @@ let test_width configs min_width n : string list = (** * Zero printing *) let%test "zero default" = - "0" = + "0b0" = to_string 0 let%test "zero default manual" = - "0" = + "0b0" = to_string_with ~flags:default ~min_width:1 0 (** ** Zero Padding, OCaml *) diff --git a/test/zeros_int32.ml b/test/zeros_int32.ml index 8b3122c..47ebb3b 100644 --- a/test/zeros_int32.ml +++ b/test/zeros_int32.ml @@ -33,11 +33,11 @@ let test_width configs min_width n : string list = (** * Zero printing *) let%test "zero default" = - "0l" = + "0b0l" = to_string 0l let%test "zero default manual" = - "0l" = + "0b0l" = to_string_with ~flags:default ~min_width:1 0l (** ** Zero Padding, OCaml *) diff --git a/test/zeros_int64.ml b/test/zeros_int64.ml index 9f54fb7..728bc71 100644 --- a/test/zeros_int64.ml +++ b/test/zeros_int64.ml @@ -33,11 +33,11 @@ let test_width configs min_width n : string list = (** * Zero printing *) let%test "zero default" = - "0L" = + "0b0L" = to_string 0L let%test "zero default manual" = - "0L" = + "0b0L" = to_string_with ~flags:default ~min_width:1 0L (** ** Zero Padding, OCaml *) diff --git a/test/zeros_native.ml b/test/zeros_native.ml index fe5b658..a1acfd5 100644 --- a/test/zeros_native.ml +++ b/test/zeros_native.ml @@ -33,11 +33,11 @@ let test_width configs min_width n : string list = (** * Zero printing *) let%test "zero default" = - "0n" = + "0b0n" = to_string 0n let%test "zero default manual" = - "0n" = + "0b0n" = to_string_with ~flags:default ~min_width:1 0n (** ** Zero Padding, OCaml *)