package pp-binary-ints

  1. Overview
  2. Docs

pp-binary-ints

A library for pretty printing boolean integers.

Library pp-binary-ints

The entry point of this library is the module: Pp_binary_ints.

Examples

The library provides four main functions.

The options to customize the outputs can be found in Pp_binary_ints.Flags.

We also offer versions of these functions for `int32`, `int64`, `nativeint`, in the following modules.

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 = 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;;
OCaml

Innovation. Community. Security.