Skip to content

Commit ddabbb9

Browse files
committed
Preparing 1.0.0 release.
1 parent 14797f6 commit ddabbb9

25 files changed

+626
-267
lines changed

CHANGES.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.0] - 2022-01-12
8+
### Changed
9+
- Default flags have changed to padding with zeros, with separators and
10+
prefixes, and zero printing behaves just like non-zero printing. Since the
11+
primary purpose of this library is debugging, this should help.
12+
- `*.{make_pp_int,make_to_string}` no longer take an optional `flags` parameter,
13+
and instead takes optional boolean parameters `zero_padding`, `left_padding`,
14+
`separators`, `prefix`, `suffix`, `zero_special`.
15+
- Renamed `*.{pp_binary_int}` to `*.{pp_int_with}` for consistency.
16+
17+
### Fixed
18+
- Padding with zeros no longer assumes prefixes are always 2 characters long.
19+
720
## [0.1.1] - 2021-12-31
821
### Added
922
- Support for `int32`, `int64`, and `nativeint`.

README.md

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ opam install pp-binary-ints
2323
The library provides four main functions.
2424

2525
- `Int.to_string` converts ints to strings.
26-
- `Int.to_string_with ~flags ~min_width` converts ints to strings, customizing the output with `flags` and `min_width`.
26+
- `Int.make_to_string` converts ints to strings, customizing the output with the optional arguments.
2727
- `Int.pp_int` is a simple `Format` module style pretty printer.
28-
- `Int.pp_binary_int ~flags ~min_width` is a customizable `Format` module style pretty printer.
28+
- `Int.make_pp_int` is a customizable `Format` module style pretty printer with the customization controlled by the optional arguments.
29+
- `Int.to_string_with` is a lower level function converts ints to strings, customizing the output with the `~flags` and `~min_width` named arguments.
30+
- `Int.pp_int_with` is a lower level customizable `Format` module style pretty printer which takes in named arguments `~flags` and `~min_width`.
2931

3032
There are also versions available for `int32`, `int64`, and `nativeint` in the
3133
modules
@@ -36,66 +38,87 @@ modules
3638
A generic functor to generate binary-int printers is provided in the `MakePP`
3739
module.
3840

41+
The following demonstrates using the library in a toplevel/REPL.
42+
3943
## Basic use
4044

4145
```ocaml
4246
# #require "pp-binary-ints";;
4347
# module Pp_Bin = Pp_binary_ints.Int;;
4448
# Pp_Bin.to_string 0b110111;;
45-
- : string = "110111"
49+
- : string = "0b11_0111"
4650
# Pp_Bin.to_string 0o777;;
47-
- : string = "111111111"
51+
string = "0b1_1111_1111"
4852
# Pp_Bin.to_string 1234;;
49-
- : string = "10011010010"
53+
- : string = "0b100_1101_0010"
5054
```
5155

5256
## Customizing padding and minimum width
5357

5458
```ocaml
5559
# #require "pp-binary-ints";;
5660
# module Pp_Bin = Pp_binary_ints.Int;;
57-
# (* Zero Padding *);;
58-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with padding = Zeros } ~min_width:13 0b110111;;
59-
- : string = "0000000110111"
60-
# (* Default is space padding on the right *);;
61-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.default ~min_width:13 0b110111;;
62-
- : string = "110111 "
61+
# (* Space Padding *);;
62+
# Pp_Bin.make_to_string ~zero_padding:false ~min_width:13 () 0b110111;;
63+
- : string = "0b11_0111 "
6364
# (* Space padding on the left is also possible *);;
64-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with padding = Left} ~min_width:13 0b110111;;
65-
- : string = " 110111"
65+
# Pp_Bin.make_to_string ~zero_padding:false ~left_padding:true ~min_width:13 () 0b110111;;
66+
- : string = " 0b11_0111"
6667
```
6768

6869
## Separators and prefixes
6970

7071
```ocaml
71-
# (* Separate every 4 digits with _ *);;
72-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with separators = true } ~min_width:1 0b110111;;
73-
- : string = "11_0111"
74-
# (* Prefix non-zero *);;
75-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true } ~min_width:1 0b110111;;
72+
# (* Turn off _ separators *);;
73+
# Pp_Bin.make_to_string ~separators:false ~min_width:1 () 0b110111;;
7674
- : string = "0b110111"
77-
# (* Prefix non-zero with separators *);;
78-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true; separators = true } ~min_width:1 0b110111;;
79-
- : string = "0b11_0111"
75+
# (* Turn off prefixes *);;
76+
# Pp_Bin.make_to_string ~prefix:false ~min_width:1 () 0b110111;;
77+
- : string = "11_0111"
78+
# (* Turn off both separatorns and prefixes *);;
79+
# Pp_Bin.make_to_string ~separators:false ~prefix:false ~min_width:1 () 0b110111;;
80+
- : string = "110111"
8081
```
8182

8283
## Zero printing behaviour
8384

84-
We support pretty printing `0` (zero) both how OCaml's `Printf` woould print it,
85-
as well as printing it similar to how we print non zero integers. The default
86-
behaviour is to follow `Printf`'s zero printing and not print a prefix, but this
87-
can be changed by setting the `zero_printing` flag to `InheritNonZero`.
85+
You can ask the library to treat `0` (zero) specially and not add a prefix to
86+
it. While it won't add a prefix to it, padding will still be added.
8887

8988
```ocaml
90-
# (* Prefix's are not added to zero by default *);;
91-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true } ~min_width:1 0;;
89+
# (* Don't prefix zero *);;
90+
# Pp_Bin.make_to_string ~zero_special:true ~min_width:1 () 0;;
9291
- : string = "0"
93-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:1 0;;
94-
- : string = "0b0"
95-
# (* All the above options can be combined *);;
96-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ padding = Zeros; separators = true; prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:8 0;;
97-
- : string = "0b0_0000"
98-
# (* The library is careful not to write "0b_" when prefixing, 'b' is always followed by a digit *);;
99-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ padding = Zeros; separators = true; prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:7 0;;
100-
- : string = "0b00000"
92+
# Pp_Bin.make_to_string ~zero_special:true ~min_width:1 () 0b110111;;
93+
- : string = "0b11_0111"
94+
(* Zero Padding still adds zeros to fill up the sapce *)
95+
# Pp_Bin.make_to_string ~zero_special:true ~min_width:9 () 0;;
96+
- : string = "0000_0000"
97+
# Pp_Bin.make_to_string ~zero_special:true ~min_width:9 () 0b110111;;
98+
- : string = "0b11_0111"
99+
```
100+
101+
# Printing Binary Ints in the REPL
102+
103+
```ocaml
104+
# #use "topfind";;
105+
# #require "pp-binary-ints";;
106+
# #install_printer Pp_binary_ints.Int.pp_int;;
107+
# 0;;
108+
- : int = 0b0
109+
# 7;;
110+
- : int = 0b111
111+
```
112+
113+
114+
You can also add the following to your `.ocamlinit` file so that integers are
115+
always printed using this library.
116+
117+
```ocaml
118+
#use "topfind";;
119+
#require "pp-binary-ints";;
120+
#install_printer Pp_binary_ints.Int.pp_int;;
121+
#install_printer Pp_binary_ints.Int32.pp_int;;
122+
#install_printer Pp_binary_ints.Int64.pp_int;;
123+
#install_printer Pp_binary_ints.Nativeint.pp_int;;
101124
```

dune-project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
(maintainers "Ifaz Kabir")
88
(source (github ifazk/pp-binary-ints))
99
(documentation https://ifazk.github.io/pp-binary-ints/)
10-
(version 0.1.1)
10+
(version 1.0.0)
1111

1212
(package
1313
(name pp-binary-ints)

lib/Flags.ml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,28 @@ type padding =
44
| Zeros
55

66
type zero_printing =
7-
| OCaml (* depends on padding setting: bunch of zeros (no separators, no prefixs), or space padding on the left or right *)
7+
| OCaml (* depends on padding setting: no prefixes for zero padding, or space padding on the left or right *)
88
| InheritNonZero
99

1010
type flags =
1111
{ padding: padding
1212
; separators: bool
1313
; prefix_non_zero: bool
14+
; suffix : bool
1415
; zero_printing: zero_printing
1516
}
1617

1718
let default =
18-
{ padding = Right
19-
; separators = false
20-
; prefix_non_zero = false
21-
; zero_printing = OCaml
19+
{ padding = Zeros
20+
; separators = true
21+
; prefix_non_zero = true
22+
; suffix = true
23+
; zero_printing = InheritNonZero
2224
}
25+
26+
let make_flags ?(zero_padding = true) ?(left_padding=false) ?(separators=true)
27+
?(prefix=true) ?(suffix=true) ?(zero_special=false) () =
28+
let padding = if zero_padding then Zeros else (if left_padding then Left else Right) in
29+
let prefix_non_zero = prefix in
30+
let zero_printing = if zero_special then OCaml else InheritNonZero in
31+
{ padding; separators; prefix_non_zero; suffix; zero_printing }

lib/Flags.mli

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,22 @@ type flags = {
1313
padding : padding;
1414
separators : bool;
1515
prefix_non_zero : bool;
16+
suffix : bool;
1617
zero_printing : zero_printing;
1718
}
1819
(** [flags] are passed to pretty printing functions to customize the output. *)
1920

2021
val default : flags
2122
(** A default set of flags. *)
23+
24+
val make_flags : ?zero_padding:bool -> ?left_padding:bool -> ?separators:bool -> ?prefix:bool -> ?suffix:bool -> ?zero_special:bool -> unit -> flags
25+
(** A function for creating flags. The [left_padding] prameter is ignored if
26+
[zero_padding] is set to [true]. The default values are as follows.
27+
{ul {- [zero_padding=true]}
28+
{- [left_padding=false]}
29+
{- [separators=true]}
30+
{- [prefix=true]}
31+
{- [suffix=true]}
32+
{- [zero_special=false]}
33+
}
34+
*)

lib/index.mld

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ The library provides four main functions.
1010

1111
{ul
1212
{- {!module-Pp_binary_ints.module-Int.val-to_string} converts ints to strings.}
13-
{- {!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.}
13+
{- {!module-Pp_binary_ints.module-Int.val-make_to_string} converts ints to strings, customizing the output with the optional arguments.}
1414
{- {!module-Pp_binary_ints.module-Int.val-pp_int} is a simple {!module-Format} module style pretty printer.}
15-
{- {!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].}
15+
{- {!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.}
16+
{- {!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.}
17+
{- {!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].}
1618
}
1719

1820
The options to customize the outputs can be found in {!module-Pp_binary_ints.Flags}.
@@ -25,66 +27,87 @@ the following modules.
2527
{- {!module-Pp_binary_ints.module-Nativeint}}
2628
}
2729

30+
The following demonstrates using the library in a toplevel/REPL.
31+
2832
{2 Basic use}
2933

3034
{[
3135
# #require "pp-binary-ints";;
3236
# module Pp_Bin = Pp_binary_ints.Int;;
3337
# Pp_Bin.to_string 0b110111;;
34-
- : string = "110111"
38+
- : string = "0b11_0111"
3539
# Pp_Bin.to_string 0o777;;
36-
- : string = "111111111"
40+
string = "0b1_1111_1111"
3741
# Pp_Bin.to_string 1234;;
38-
- : string = "10011010010"
42+
- : string = "0b100_1101_0010"
3943
]}
4044

4145
{2 Customizing padding and minimum width}
4246

4347
{[
4448
# #require "pp-binary-ints";;
4549
# module Pp_Bin = Pp_binary_ints.Int;;
46-
# (* Zero Padding *);;
47-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with padding = Zeros } ~min_width:13 0b110111;;
48-
- : string = "0000000110111"
49-
# (* Default is space padding on the right *);;
50-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.default ~min_width:13 0b110111;;
51-
- : string = "110111 "
50+
# (* Space Padding *);;
51+
# Pp_Bin.make_to_string ~zero_padding:false ~min_width:13 () 0b110111;;
52+
- : string = "0b11_0111 "
5253
# (* Space padding on the left is also possible *);;
53-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with padding = Left} ~min_width:13 0b110111;;
54-
- : string = " 110111"
54+
# Pp_Bin.make_to_string ~zero_padding:false ~left_padding:true ~min_width:13 () 0b110111;;
55+
- : string = " 0b11_0111"
5556
]}
5657

5758
{2 Separators and prefixes}
5859

5960
{[
60-
# (* Separate every 4 digits with _ *);;
61-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with separators = true } ~min_width:1 0b110111;;
62-
- : string = "11_0111"
63-
# (* Prefix non-zero *);;
64-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true } ~min_width:1 0b110111;;
61+
# (* Turn off _ separators *);;
62+
# Pp_Bin.make_to_string ~separators:false ~min_width:1 () 0b110111;;
6563
- : string = "0b110111"
66-
# (* Prefix non-zero with separators *);;
67-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true; separators = true } ~min_width:1 0b110111;;
68-
- : string = "0b11_0111"
64+
# (* Turn off prefixes *);;
65+
# Pp_Bin.make_to_string ~prefix:false ~min_width:1 () 0b110111;;
66+
- : string = "11_0111"
67+
# (* Turn off both separatorns and prefixes *);;
68+
# Pp_Bin.make_to_string ~separators:false ~prefix:false ~min_width:1 () 0b110111;;
69+
- : string = "110111"
6970
]}
7071

7172
{2 Zero printing behaviour}
7273

73-
We support pretty printing `0` (zero) both how OCaml's `Printf` woould print it,
74-
as well as printing it similar to how we print non zero integers. The default
75-
behaviour is to follow [Printf]'s zero printing and not print a prefix, but this
76-
can be changed by setting the [zero_printing] flag to [InheritNonZero].
74+
You can ask the library to treat [0] (zero) specially and not add a prefix to
75+
it. While it won't add a prefix to it, padding will still be added.
7776

7877
{[
79-
# (* Prefix's are not added to zero by default *);;
80-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true } ~min_width:1 0;;
78+
# (* Don't prefix zero *);;
79+
# Pp_Bin.make_to_string ~zero_special:true ~min_width:1 () 0;;
8180
- : string = "0"
82-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ default with prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:1 0;;
83-
- : string = "0b0"
84-
# (* All the above options can be combined *);;
85-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ padding = Zeros; separators = true; prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:8 0;;
86-
- : string = "0b0_0000"
87-
# (* The library is careful not to write "0b_" when prefixing, 'b' is always followed by a digit *);;
88-
# Pp_Bin.to_string_with ~flags:Pp_Bin.Flags.{ padding = Zeros; separators = true; prefix_non_zero = true; zero_printing = InheritNonZero } ~min_width:7 0;;
89-
- : string = "0b00000"
81+
# Pp_Bin.make_to_string ~zero_special:true ~min_width:1 () 0b110111;;
82+
- : string = "0b11_0111"
83+
(* Zero Padding still adds zeros to fill up the sapce *)
84+
# Pp_Bin.make_to_string ~zero_special:true ~min_width:9 () 0;;
85+
- : string = "0000_0000"
86+
# Pp_Bin.make_to_string ~zero_special:true ~min_width:9 () 0b110111;;
87+
- : string = "0b11_0111"
88+
]}
89+
90+
{1 Printing Binary Ints in the REPL}
91+
92+
{[
93+
# #use "topfind";;
94+
# #require "pp-binary-ints";;
95+
# #install_printer Pp_binary_ints.Int.pp_int;;
96+
# 0;;
97+
- : int = 0b0
98+
# 7;;
99+
- : int = 0b111
100+
]}
101+
102+
103+
You can also add the following to your [.ocamlinit] file so that integers are
104+
always printed using this library.
105+
106+
{[
107+
#use "topfind";;
108+
#require "pp-binary-ints";;
109+
#install_printer Pp_binary_ints.Int.pp_int;;
110+
#install_printer Pp_binary_ints.Int32.pp_int;;
111+
#install_printer Pp_binary_ints.Int64.pp_int;;
112+
#install_printer Pp_binary_ints.Nativeint.pp_int;;
90113
]}

0 commit comments

Comments
 (0)