package libsail

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Libsail.UtilSource

Various non Sail specific utility functions

Sourceval opt_colors : bool ref
Sourceval opt_verbosity : int ref
Sourceval last : 'a list -> 'a
Sourceval last_opt : 'a list -> 'a option
Sourceval butlast : 'a list -> 'a list
Sourcemodule Option_monad : sig ... end
Sourcemodule State_monad (S : sig ... end) : sig ... end
Sourceval lex_ord_list : ('a -> 'a -> int) -> 'a list -> 'a list -> int

Lift a comparison order to the lexical order on lists

Sourceval assoc_equal_opt : ('a -> 'a -> bool) -> 'a -> ('a * 'b) list -> 'b option

assoc_equal_opt and assoc_compare_opt are like List.assoc_opt but take equality/comparison functions as arguments, rather than relying on OCaml's built in equality

Sourceval assoc_compare_opt : ('a -> 'a -> int) -> 'a -> ('a * 'b) list -> 'b option
Sourceval power : int -> int -> int
Sourceval update_last : ('a -> 'a) -> 'a list -> 'a list

Apply a function to the last element of a list

Sourceval update_first : ('a -> 'a) -> 'a list -> 'a list
Sourceval map_last : (bool -> 'a -> 'b) -> 'a list -> 'b list

Map but pass true to the function for the last element

Sourceval iter_last : (bool -> 'a -> unit) -> 'a list -> unit

Option Functions

Sourceval option_cases : 'a option -> ('a -> 'b) -> (unit -> 'b) -> 'b

option_cases None f_s f_n returns f_n, whereas option_cases (Some x) f_s f_n returns f_s x.

Sourceval option_binop : ('a -> 'a -> 'b) -> 'a option -> 'a option -> 'b option

option_binop f (Some x) (Some y) returns Some (f x y), and in all other cases, option_binop returns None.

Sourceval option_get_exn : exn -> 'a option -> 'a

option_get_exn exn None throws the exception exn, whereas option_get_exn exn (Some x) returns x.

Sourceval option_these : 'a option list -> 'a list

option_these xs extracts the elements of the list xs wrapped in Some.

Sourceval option_all : 'a option list -> 'a list option

option_all xs extracts the elements of the list xs if all of them are wrapped in Some. If any are None then the result is None is None. option_all [] is Some []

Sourceval result_all : ('a, 'e) Result.t list -> ('a list, 'e) Result.t

List Functions

Sourceval list_empty : 'a list -> bool
Sourceval list_index : ('a -> bool) -> 'a list -> int option

list_index p l returns the first index i such that the predicate p (l!i) holds. If no such i exists, None is returned.

Sourceval option_first : ('a -> 'b option) -> 'a list -> 'b option

option_first f l searches for the first element x of l such that the f x is not None. If such an element exists, f x is returned, otherwise None.

Sourceval map_changed : ('a -> 'a option) -> 'a list -> 'a list option

map_changed f l maps f over l. If for all elements of l the function f returns None, then map_changed f l returns None. Otherwise, it uses x for all elements, where f x returns None, and returns the resulting list.

Sourceval map_changed_default : ('a -> 'b) -> ('a -> 'b option) -> 'a list -> 'b list option

map_changed_default d f l maps f over l. If for all elements of l the function f returns None, then map_changed f l returns None. Otherwise, it uses d x for all elements x, where f x returns None, and returns the resulting list.

Sourceval list_iter_sep : (unit -> unit) -> ('a -> unit) -> 'a list -> unit

list_iter sf f [a1; ...; an] applies function f in turn to a1; ...; an and calls sf () in between. It is equivalent to begin f a1; sf(); f a2; sf(); ...; f an; () end.

Sourceval map_split : ('a -> ('b, 'c) result) -> 'a list -> 'b list * 'c list
Sourceval map_all : ('a -> 'b option) -> 'a list -> 'b list option

map_all f l maps f over l. If at least one entry is None, None is returned. Otherwise, the Some function is removed from the list.

Sourceval map_if : ('a -> bool) -> ('a -> 'a) -> 'a list -> 'a list
Sourceval map_exists : ('b -> bool) -> ('a -> 'b) -> 'a list -> bool
Sourceval list_to_front : int -> 'a list -> 'a list

list_to_front i l resorts the list l by bringing the element at index i to the front.

Throws Failure if i is not smaller than the length of l

Sourceval undo_list_to_front : int -> 'a list -> 'a list

undo_list_to_front i l resorts the list l by moving the head element to index index i It's the inverse of list_to_front i l.

Sourceval split_after : int -> 'a list -> 'a list * 'a list

split_after n l splits the first n elemenst from list l, i.e. it returns two lists l1 and l2, with length l1 = n and l1 @ l2 = l. Fails if n is too small or large.

Sourceval split3 : ('a * 'b * 'c) list -> 'a list * 'b list * 'c list

split3 l splits a list of triples into a triple of lists

Sourceval compare_list : ('a -> 'b -> int) -> 'a list -> 'b list -> int
Sourceval take : int -> 'a list -> 'a list
Sourceval drop : int -> 'a list -> 'a list
Sourceval delimit_list : ('a -> bool) -> 'a list -> 'a list list
Sourceval take_drop : ('a -> bool) -> 'a list -> 'a list * 'a list
Sourceval find_rest_opt : ('a -> bool) -> 'a list -> ('a * 'a list) option
Sourceval find_next : ('a -> bool) -> 'a list -> 'a list * ('a * 'a list) option
Sourceval find_index_opt : ('a -> bool) -> 'a list -> (int * 'a) option

find an item in a list and return that item as well as its index

Sourceval find_map : ('a -> 'b option) -> 'a list -> 'b option
Sourceval fold_left_concat_map : ('a -> 'b -> 'a * 'c list) -> 'a -> 'b list -> 'a * 'c list
Sourceval fold_left_last : (bool -> 'a -> 'b -> 'a) -> 'a -> 'b list -> 'a
Sourceval fold_left_index : (int -> 'a -> 'b -> 'a) -> 'a -> 'b list -> 'a
Sourceval fold_left_index_last : (int -> bool -> 'a -> 'b -> 'a) -> 'a -> 'b list -> 'a
Sourceval fold_left_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a list -> 'acc * 'b list
Sourceval list_init : int -> (int -> 'a) -> 'a list

String utilities

Sourceval starts_with : prefix:string -> string -> bool

Alternative to String.starts_with for OCaml < 4.13

Sourceval remove_suffix : string -> string -> string option

remove_suffix s suffix checks if s ends with suffix and returns the rest of the string if so

Sourceval levenshtein_distance : ?osa:bool -> string -> string -> int

Compute the levenshtein distance between two strings using the Wagner–Fischer algorithm. If ~osa is true computes the optimal string alignment distance, which is similar but allows swaps as a single action.

Files

Sourceval copy_file : string -> string -> unit

copy_file src dst copies file src to file dst. Only files are supported, no directories.

Sourceval move_file : string -> string -> unit

move_file src dst moves file src to file dst. In contrast to Sys.rename no error is produced, if dst already exists. Moreover, in case Sys.rename fails for some reason (e.g. because it does not work over filesystem boundaries), copy_file and Sys.remove are used as fallback.

Sourceval input_byte_opt : in_channel -> int option

input_byte_opt chan tries to read a byte b from input channel chan, and returns Some b in case of success, or None if the end of the file was reached.

Sourceval same_content_files : string -> string -> bool

same_content_files file1 file2 checks, whether the files file1 and file2 have the same content. If at least one of the files does not exist, false is returned. same_content_files throws an exception, if one of the files exists, but cannot be read.

Sourceval read_whole_file : string -> string

read_whole_file filename reads the contents of the file and returns it as a string.

Strings

Sourceval string_to_list : string -> char list

string_to_list l translates the string l to the list of its characters.

Useful sets

Sourcemodule IntSet : Set.S with type elt = int and type t = Set.Make(Int).t

Sets of integers and strings

Sourcemodule IntMap : Map.S with type key = int and type 'a t = 'a Map.Make(Int).t
Sourcemodule StringSet : Set.S with type elt = string and type t = Set.Make(String).t
Sourcemodule StringMap : Map.S with type key = string and type 'a t = 'a Map.Make(String).t
Sourcemodule IntIntSet : Set.S with type elt = int * int

Formatting functions

Sourceval string_of_list : string -> ('a -> string) -> 'a list -> string
Sourceval string_of_option : ('a -> string) -> 'a option -> string
Sourceval split_on_char : char -> string -> string list

Terminal color codes

Sourceval termcode : int -> string
Sourceval bold : string -> string
Sourceval dim : string -> string
Sourceval darkgray : string -> string
Sourceval green : string -> string
Sourceval red : string -> string
Sourceval red_bg : string -> string
Sourceval yellow : string -> string
Sourceval cyan : string -> string
Sourceval blue : string -> string
Sourceval magenta : string -> string
Sourceval clear : string -> string

Encoding schemes for strings

z-encoding will take any string with ASCII characters in the range 32-126 inclusive, and map it to a string that just contains ASCII upper and lower case letters and numbers, prefixed with the letter z. This mapping is one-to-one.

Sourceval zencode_string : string -> string
Sourceval zencode_upper_string : string -> string
Sourceval file_encode_string : string -> string

Encode string for use as a filename. We can't use zencode directly because some operating systems make the mistake of being case-insensitive.

Misc output functions

Sourceval log_line : string -> int -> string -> string
Sourceval header : string -> int -> string
Sourceval progress : string -> string -> int -> int -> unit
Sourceval always_replace_files : bool ref

always_replace_files determines whether Sail only updates modified files. If it is set to true, all output files are written, regardless of whether the files existed before. If it is set to false and an output file already exists, the output file is only updated, if its content really changes.

Sourcetype checked_output = {
  1. channel : out_channel;
  2. temp_file_name : string;
  3. directory : string option;
  4. file_name : string;
}
Sourceval open_output_with_check : ?directory:string -> string -> checked_output

open_output_with_check file_name will open a file for output, but will only actually create or update said file if the contents are different to the existing contents of said file when the output is closed. This means it works well for compiler artifacts that are referenced by tools like Make. The 'check' that the file actually needs updating happens in the close_output_with_check function, so it is especially important that any call to this is matched with that function otherwise no actual output will happen.

Takes an optional directory argument, which will cause the file to be created in a subdirectory of the current directory. The subdirectory will be created (by the close_output_with_check function) if it doesn't exist.

Sourceval open_output_with_check_formatted : ?directory:string -> string -> Format.formatter * checked_output
Sourceval close_output_with_check : checked_output -> unit
Sourceval to_upper_camel_case : string -> string
OCaml

Innovation. Community. Security.