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 Duplicate (S : Set.S) : sig ... end

Mixed useful things

Sourceval remove_duplicates : 'a list -> 'a list

remove_duplicates l removes duplicate elements from the list l. As a side-effect, the list might be reordered.

Sourceval remove_dups : ('a -> 'a -> int) -> ('a -> 'a -> bool) -> 'a list -> 'a list

remove_dups compare eq l as remove_duplicates but with parameterised comparison and equality

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 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 []

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 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 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 list_init : int -> (int -> 'a) -> 'a list

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.

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

Sets of Integers

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.

Sourceval open_output_with_check : string option -> string -> Format.formatter * (out_channel * string * string option * string)
Sourceval open_output_with_check_unformatted : string option -> string -> out_channel * string * string option * string
Sourceval close_output_with_check : (out_channel * string * string option * string) -> unit
OCaml

Innovation. Community. Security.