github ↗
docs menu

Module Tolk.Helpers

Compilation targets, integer and list utilities, terminal text, the disk cache and programs.

Compilation targets

#module Target : sig ... end

Compilation targets.

Integers and lists

#val prod : int list -> int

prod l is the product of the elements of l, 1 if l is empty.

#val dedup : (module Stdlib.Hashtbl.HashedType with type t = 'a) -> 'a list -> 'a list

dedup (module H) l is l with each element kept only at its first occurrence, elements being compared with H.equal.

#val argsort : int list -> int list

argsort l is the positions of the elements of l, ordered by increasing element; equal elements keep their order. On a permutation it is the inverse permutation.

#val all_same : ('a -> 'a -> bool) -> 'a list -> bool

all_same equal l is true iff every element of l is equal to the first. It is true on [].

#val get_single_element : 'a list -> 'a

get_single_element l is the element of l.

Raises Invalid_argument if l does not have exactly one element.

#val ceildiv : int -> int -> int

ceildiv num amt is num / amt rounded up.

Raises Division_by_zero if amt is 0.

#val round_up : int -> int -> int

round_up num amt is the smallest multiple of amt greater than or equal to num, for positive amt.

Raises Division_by_zero if amt is 0.

#val floordiv : int -> int -> int

floordiv x y is x / y rounded down, and 0 if y is 0.

#val floormod : int -> int -> int

floormod x y is x - floordiv x y * y: the remainder of the division rounded down, with the sign of y. It is x if y is 0.

#val lo32 : int -> int

lo32 x is the low 32 bits of x.

#val hi32 : int -> int

hi32 x is x shifted right by 32 bits, keeping its sign.

#val data64 : int -> int * int

data64 x is (hi32 x, lo32 x).

#val data64_le : int -> int * int

data64_le x is (lo32 x, hi32 x).

Terminal text

#type color =
  1. #| Black
  2. #| Red
  3. #| Green
  4. #| Yellow
  5. #| Blue
  6. #| Magenta
  7. #| Cyan
  8. #| White
  9. #| Bright_black
  10. #| Bright_red
  11. #| Bright_green
  12. #| Bright_yellow
  13. #| Bright_blue
  14. #| Bright_magenta
  15. #| Bright_cyan
  16. #| Bright_white
  17. #| Rgb of int * int * int
    (*

    Red, green and blue, each from 0 to 255.

    *)

The type for the colors of terminal text.

#val colored : ?background:bool -> color -> string -> string

colored c s is s wrapped in the ANSI escape sequences that paint it, or its background if background is true, in c: a 24-bit color for Rgb. It is s itself when Setting.no_color is set. background defaults to false.

#val time_to_str : ?w:int -> float -> string

time_to_str ~w t is the duration of t seconds right-aligned in w columns with two decimals, followed by a two-column unit: s above 10 seconds, ms above 10 milliseconds, and us otherwise. w defaults to 8.

#val size_to_str : int -> string

size_to_str s is the size of s bytes, with two decimals in the largest of GB, MB and KB it reaches (powers of 1024), or as s B below a kilobyte.

#val ansistrip : string -> string

ansistrip s is s without its ANSI escape sequences that erase a line or set graphic attributes. The latter run from the escape character and an opening bracket to the first 'm' on the same line.

#val ansilen : string -> int

ansilen s is the number of characters of ansistrip s, counting UTF-8 encoded characters once.

#val ansipad : string -> int -> string

ansipad s w is s followed by the spaces that bring its ansilen to w, none if it has that length already.

#val strip_parens : string -> string

strip_parens s is s without its first and last characters if they are parentheses enclosing the whole of s, and s otherwise.

#val pluralize : string -> int -> string

pluralize st cnt is cnt followed by st, with an s unless cnt is 1.

#val to_function_name : string -> string

to_function_name s is ansistrip s with each character other than an ASCII letter, digit or underscore replaced by its Unicode code point in uppercase hexadecimal, at least two digits. The result is a valid C identifier unless empty or starting with a digit.

Disk cache

#val cache_dir : string

cache_dir is the directory of the files tolk caches: tolk in XDG_CACHE_HOME if that variable is set, and otherwise in ~/Library/Caches on macOS and ~/.cache elsewhere. ~ is the home directory, from USERPROFILE on Windows and HOME elsewhere. Paths are joined with the system's separator.

#val cachedb : string

cachedb is the directory of the Diskcache, from the variable CACHEDB. Defaults to cache in cache_dir, made absolute against the current directory when cache_dir is relative.

#module Diskcache : sig ... end

Persistent key-value tables.

Programs

#val system : ?input:string -> string -> string

system ~input cmd is the output of the command cmd, a program and its arguments separated by spaces, run with input on its standard input or, without input, with the process's: what it writes on its standard output and standard error, together, without the white space that starts and ends it. With Setting.debug at least 1, it prints how many bytes cmd returned and how long it took.

Raises Failure naming cmd with the reason and output if cmd cannot run or exits otherwise than with status 0.

#val cpu_objdump : string -> unit

cpu_objdump lib prints the instructions of the object file lib, as objdump -d prints them.

Raises Failure as system if objdump fails.

#val find_llvm_objdump : unit -> string

find_llvm_objdump () is the llvm-objdump to run: Homebrew's on macOS, and elsewhere the first of ROCm's and those named llvm-objdump-21, llvm-objdump-20 and llvm-objdump on PATH.

Raises Failure if there is none.

#val amdgpu_disassemble : string -> unit

amdgpu_disassemble lib prints the instructions of the AMD GPU code object lib, as find_llvm_objdump's llvm-objdump -d prints them, without the padding instructions that end it (s_nop 0 and s_code_end).

Raises Failure if there is no llvm-objdump, or as system if it fails.