package pp-binary-ints
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=4840fc294cf861bddecdbb3218f0ef26d37d2207d45a45d19d01f14e5528cd1b
sha512=123ec3a792e5148d324ac1406d62fcee798af8ec7a75d23986ac18018ea816d954a71e01d8edbb734fb9f5855686f63e22765da49c72802e67acb9749e6d6616
Description
Pretty Printing Binary Integers This library contains functions for pretty printing integers as unsigned binary integers.
README
pp-binary-ints
An OCaml library for printing ints as unsigned binary integers.
The pretty printers are fairly customizable. The following options are supported.
- Padding with zeros or spaces so that binary integers satisfy a minimum width.
- Prefixing the integers
0b. - Separating every four bits with
_(underscore). - Choose if zeros should be printed similar to non-zero ints.
You can find documentation for the library here.
Installation
Install this library using opam.
opam install pp-binary-intsExamples
The library provides four main functions.
Int.to_stringconverts ints to strings.Int.make_to_stringconverts ints to strings, customizing the output with the optional arguments.Int.pp_intis a simpleFormatmodule style pretty printer.Int.make_pp_intis a customizableFormatmodule style pretty printer with the customization controlled by the optional arguments.Int.to_string_withis a lower level function converts ints to strings, customizing the output with the~flagsand~min_widthnamed arguments.Int.pp_int_withis a lower level customizableFormatmodule style pretty printer which takes in named arguments~flagsand~min_width.
There are also versions available for int32, int64, and nativeint in the modules
Int32,Int64, andNativeint.
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
# #require "pp-binary-ints";;
# module Pp_Bin = Pp_binary_ints.Int;;
# Pp_Bin.to_string 0b110111;;
- : string = "0b11_0111"
# Pp_Bin.to_string 0o777;;
string = "0b1_1111_1111"
# Pp_Bin.to_string 1234;;
- : string = "0b100_1101_0010"Customizing padding and minimum width
# #require "pp-binary-ints";;
# module Pp_Bin = Pp_binary_ints.Int;;
# (* 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.make_to_string ~zero_padding:false ~left_padding:true ~min_width:13 () 0b110111;;
- : string = " 0b11_0111"Separators and prefixes
# (* Turn off _ separators *);;
# Pp_Bin.make_to_string ~separators:false ~min_width:1 () 0b110111;;
- : string = "0b110111"
# (* 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
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.
# (* Don't prefix zero *);;
# Pp_Bin.make_to_string ~zero_special:true ~min_width:1 () 0;;
- : string = "0"
# 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
# #use "topfind";;
# #require "pp-binary-ints";;
# #install_printer Pp_binary_ints.Int.pp_int;;
# 0;;
- : int = 0b0
# 7;;
- : int = 0b111You 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;;