Module Tolk.Helpers
Compilation targets, integer and list utilities, terminal text, the disk cache and programs.
Compilation targets
Integers and lists
val prod : int list -> intprod 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 listdedup (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 listargsort 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 -> boolall_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 -> 'aget_single_element l is the element of l.
Raises Invalid_argument if l does not have exactly one element.
val ceildiv : int -> int -> intceildiv num amt is num / amt rounded up.
Raises Division_by_zero if amt is 0.
val round_up : int -> int -> intround_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 -> intfloordiv x y is x / y rounded down, and 0 if y is 0.
val floormod : int -> int -> intfloormod 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 -> intlo32 x is the low 32 bits of x.
val hi32 : int -> inthi32 x is x shifted right by 32 bits, keeping its sign.
val data64 : int -> int * intdata64 x is (hi32 x, lo32 x).
val data64_le : int -> int * intdata64_le x is (lo32 x, hi32 x).
Terminal text
type color = The type for the colors of terminal text.
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 -> stringtime_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 -> stringsize_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 -> stringansistrip 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 -> intansilen s is the number of characters of ansistrip s, counting UTF-8 encoded characters once.
val ansipad : string -> int -> stringansipad 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 -> stringstrip_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 -> stringpluralize st cnt is cnt followed by st, with an s unless cnt is 1.
val to_function_name : string -> stringto_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 : stringcache_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 : stringPrograms
val system : ?input:string -> string -> stringsystem ~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 -> unitcpu_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 -> stringfind_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 -> unitamdgpu_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.