package core_extended

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
include module type of struct include Core.String end
type t = string
include sig ... end
val typerep_of_t : t Typerep_lib.Std.Typerep.t
val typename_of_t : t Typerep_lib.Std.Typename.t
module Caseless = Core.String.Caseless
include module type of struct include Base.String end with type t := t with module Caseless := Base.String.Caseless
include sig ... end
val blit : (t, bytes) Base.Blit.blit
  • deprecated [since 2017-10] Use [Bytes.blit] instead
val blito : (t, bytes) Base.Blit.blito
  • deprecated [since 2017-10] Use [Bytes.blito] instead
val unsafe_blit : (t, bytes) Base.Blit.blit
  • deprecated [since 2017-10] Use [Bytes.unsafe_blit] instead
val sub : (t, t) Base.Blit.sub
val subo : (t, t) Base.Blit.subo
include Base.Container.S0 with type t := t with type elt = char
type elt = char
val mem : t -> elt -> bool

Checks whether the provided element is there, using equality on elts.

val iter : t -> f:(elt -> unit) -> unit

iter must allow exceptions raised in f to escape, terminating the iteration cleanly. The same holds for all functions below taking an f.

val fold : t -> init:'accum -> f:('accum -> elt -> 'accum) -> 'accum

fold t ~init ~f returns f (... f (f (f init e1) e2) e3 ...) en, where e1..en are the elements of t.

val fold_result : t -> init:'accum -> f:('accum -> elt -> ('accum, 'e) Base.Result.t) -> ('accum, 'e) Base.Result.t

fold_result t ~init ~f is a short-circuiting version of fold that runs in the Result monad. If f returns an Error _, that value is returned without any additional invocations of f.

val fold_until : t -> init:'accum -> f: ('accum -> elt -> ('accum, 'stop) Base.Container_intf.Export.Continue_or_stop.t) -> ('accum, 'stop) Base.Container_intf.Export.Finished_or_stopped_early.t

fold_until t ~init ~f is a short-circuiting version of fold. If f returns Stop _ the computation ceases and results in that value. If f returns Continue _, the fold will proceed.

val exists : t -> f:(elt -> bool) -> bool

Returns true if and only if there exists an element for which the provided function evaluates to true. This is a short-circuiting operation.

val for_all : t -> f:(elt -> bool) -> bool

Returns true if and only if the provided function evaluates to true for all elements. This is a short-circuiting operation.

val count : t -> f:(elt -> bool) -> int

Returns the number of elements for which the provided function evaluates to true.

val sum : (module Base.Commutative_group.S with type t = 'sum) -> t -> f:(elt -> 'sum) -> 'sum

Returns the sum of f i for all i in the container.

val find : t -> f:(elt -> bool) -> elt option

Returns as an option the first element for which f evaluates to true.

val find_map : t -> f:(elt -> 'a option) -> 'a option

Returns the first evaluation of f that returns Some, and returns None if there is no such element.

val to_list : t -> elt list
val to_array : t -> elt array
val min_elt : t -> cmp:(elt -> elt -> int) -> elt option

Returns a min (resp. max) element from the collection using the provided cmp function. In case of a tie, the first element encountered while traversing the collection is returned. The implementation uses fold so it has the same complexity as fold. Returns None iff the collection is empty.

val max_elt : t -> cmp:(elt -> elt -> int) -> elt option
include Base.Identifiable.S with type t := t
include sig ... end
include Base.Stringable.S with type t := t
include Base.Comparable.S with type t := t
include Base.Comparable_intf.Polymorphic_compare with type t := t
include Base.Comparisons.Infix with type t := t
include Base.Comparator.S with type t := t
type comparator_witness = Core.String.comparator_witness
include Base.Comparable_intf.Validate with type t := t
include Base.Pretty_printer.S with type t := t
val max_length : int

Maximum length of a string.

val length : t -> int
val get : t -> int -> char
val unsafe_get : string -> int -> char

unsafe_get t i is like get t i but does not perform bounds checking. The caller must ensure that it is a memory-safe operation.

val create : int -> bytes
  • deprecated [since 2017-10] Use [Bytes.create] instead
val make : int -> char -> t
val copy : t -> t
val init : int -> f:(int -> char) -> t
val fill : bytes -> pos:int -> len:int -> char -> unit
  • deprecated [since 2017-10] Use [Bytes.fill] instead
val (^) : t -> t -> t

String append. Also available unqualified, but re-exported here for documentation purposes.

Note that a ^ b must copy both a and b into a newly-allocated result string, so a ^ b ^ c ^ ... ^ z is quadratic in the number of strings. String.concat does not have this problem -- it allocates the result buffer only once. The Rope module provides a data structure which uses a similar trick to achieve fast concatenation at either end of a string.

val concat : ?sep:t -> t list -> t

concatanate all strings in the list using separator sep (default sep "")

val escaped : t -> t

Warning: Only returns a copy if changes are necessary! Special characters are represented by escape sequences, following the lexical conventions of Objective Caml.

val contains : ?pos:int -> ?len:int -> t -> char -> bool
val uppercase : t -> t
val lowercase : t -> t
val capitalize : t -> t
val uncapitalize : t -> t
val index : t -> char -> int option
val index_exn : t -> char -> int
val rindex : t -> char -> int option
val rindex_exn : t -> char -> int
val index_from : t -> int -> char -> int option
val index_from_exn : t -> int -> char -> int
val rindex_from : t -> int -> char -> int option
val rindex_from_exn : t -> int -> char -> int
module Search_pattern = Core.String.Search_pattern

Substring search and replace functions. They use the Knuth-Morris-Pratt algorithm (KMP) under the hood.

val substr_index : ?pos:int -> t -> pattern:t -> int option

Substring search and replace convenience functions. They call Search_pattern.create and then forget the preprocessed pattern when the search is complete. pos < 0 or pos >= length t result in no match (hence substr_index returns None and substr_index_exn raises). may_overlap indicates whether to report overlapping matches, see Search_pattern.index_all.

val substr_index_exn : ?pos:int -> t -> pattern:t -> int
val substr_index_all : t -> may_overlap:bool -> pattern:t -> int list
val substr_replace_first : ?pos:int -> t -> pattern:t -> with_:t -> t
val substr_replace_all : t -> pattern:t -> with_:t -> t

As with Search_pattern.replace_all, the result may still contain pattern.

val is_substring : t -> substring:t -> bool

is_substring ~substring:"bar" "foo bar baz" is true

val slice : t -> int -> int -> t

slice s start stop gets a slice of s between start and stop. start and stop will be normalized before the access. (viz. Array.normalize).

val to_list_rev : t -> char list

Returns the reversed list of characters contained in a list.

val rev : t -> t

rev t returns t in reverse order.

val nget : t -> int -> char

nget s i Gets the char at normalized position i in s.

val nset : bytes -> int -> char -> unit

nset s i c Sets the char at normalized position i to c.

  • deprecated [since 2017-10]
val is_suffix : t -> suffix:t -> bool

is_suffix s ~suffix returns true if s ends with suffix.

val is_prefix : t -> prefix:t -> bool

is_prefix s ~prefix returns true if s starts with prefix.

val lsplit2_exn : t -> on:char -> t * t

If the string s contains the character on, then lsplit2_exn s ~on returns a pair containing s split around the first appearance of on (from the left).

  • raises Not_found

    When on cannot be found in s

val rsplit2_exn : t -> on:char -> t * t

If the string s contains the character on, then rsplit2_exn s ~on returns a pair containing s split around the first appearance of on (from the right).

  • raises Not_found

    When on cannot be found in s

val lsplit2 : t -> on:char -> (t * t) option

lsplit2 s ~on optionally returns s split into two strings around the first appearance of on from the left

val rsplit2 : t -> on:char -> (t * t) option

rsplit2 s ~on optionally returns s split into two strings around the first appearance of on from the right

val split : t -> on:char -> t list

split s ~on

  • returns

    a list of substrings of s that are separated by on. Consecutive on characters will cause multiple empty strings in the result. Splitting the empty string returns a list of the empty string, not the empty list.

val split_on_chars : t -> on:char list -> t list

split_on_chars s ~on

  • returns

    a list of all substrings of s that are separated by one of the chars from on. on are not grouped. So a grouping of on in the source string will produce multiple empty string splits in the result.

val split_lines : t -> t list

split_lines t returns the list of lines that comprise t. The lines do not include the trailing "\n" or "\r\n".

val lfindi : ?pos:int -> t -> f:(int -> char -> bool) -> int option

lfindi ?pos t ~f returns the smallest i >= pos such that f i t.[i], if there is such an i. By default, pos = 0.

val rfindi : ?pos:int -> t -> f:(int -> char -> bool) -> int option

rfindi ?pos t ~f returns the largest i <= pos such that f i t.[i], if there is such an i. By default pos = length t - 1.

Warning: the following strip functions may return the same string passed in

val lstrip : ?drop:(char -> bool) -> t -> t

lstrip ?drop s returns a string with consecutive chars satisfying drop (by default white space, e.g. tabs, spaces, newlines, and carriage returns) stripped from the beginning of s.

val rstrip : ?drop:(char -> bool) -> t -> t

rstrip ?drop s returns a string with consecutive chars satisfying drop (by default white space, e.g. tabs, spaces, newlines, and carriage returns) stripped from the end of s.

val strip : ?drop:(char -> bool) -> t -> t

strip ?drop s returns a string with consecutive chars satisfying drop (by default white space, e.g. tabs, spaces, newlines, and carriage returns) stripped from the beginning and end of s.

val map : t -> f:(char -> char) -> t

map f s applies f to each character in s, and returns the resulting string.

val mapi : t -> f:(int -> char -> char) -> t

mapi f s applies f to each character in s and its index, and returns the resulting string.

val foldi : t -> init:'a -> f:(int -> 'a -> char -> 'a) -> 'a

foldi works similarly to fold, but also pass in index of each character to f

val concat_map : ?sep:t -> t -> f:(char -> t) -> t

Like map, but allows replacement of a single character with zero or two or more characters.

val filter : t -> f:(char -> bool) -> t

filter s ~f:predicate discards characters not satisfying predicate

val tr : target:char -> replacement:char -> t -> t

tr target replacement s replaces every instance of target in s with replacement.

val tr_inplace : target:char -> replacement:char -> bytes -> unit

tr_inplace target replacement s destructively modifies s (in place!) replacing every instance of target in s with replacement.

  • deprecated [since 2017-10] Use [Bytes.tr] instead
val chop_suffix_exn : t -> suffix:t -> t

chop_suffix_exn s ~suffix returns a copy of s without the trailing suffix

  • raises Invalid_argument

    if suffix is not a suffix of s

val chop_prefix_exn : t -> prefix:t -> t

chop_prefix_exn s ~prefix returns a copy of s without the leading prefix

  • raises Invalid_argument

    if prefix is not a prefix of s

val chop_suffix : t -> suffix:t -> t option
val chop_prefix : t -> prefix:t -> t option
val suffix : t -> int -> t

suffix s n returns the longest suffix of s of length less than or equal to n

val prefix : t -> int -> t

prefix s n returns the longest prefix of s of length less than or equal to n

val drop_suffix : t -> int -> t

drop_suffix s n drops the longest suffix of s of length less than or equal to n

val drop_prefix : t -> int -> t

drop_prefix s n drops the longest prefix of s of length less than or equal to n

val concat_array : ?sep:t -> t array -> t

concat_array sep ar like String.concat, but operates on arrays

val is_empty : t -> bool

is_empty s returns true iff s is empty (i.e. its length is 0).

val of_char : char -> t
val of_char_list : char list -> t
module Escaping = Core.String.Escaping

Operations for escaping and unescaping strings, with parameterized escape and escapeworthy characters. Escaping/unescaping using this module is more efficient than using Pcre. Benchmark code can be found in core/benchmarks/string_escaping.ml.

val set : bytes -> int -> char -> unit
  • deprecated [since 2017-10] Use [Bytes.set] instead
val unsafe_set : bytes -> int -> char -> unit
  • deprecated [since 2017-10] Use [Bytes.unsafe_set] instead
val take_while : t -> f:(char -> bool) -> t

take_while s ~f returns the longest prefix of s satisfying for_all prefix ~f (See lstrip to drop such a prefix)

val rtake_while : t -> f:(char -> bool) -> t

rtake_while s ~f returns the longest suffix of s satisfying for_all suffix ~f (See rstrip to drop such a suffix)

include Core_kernel.Hexdump.S with type t := t
module Hexdump = Core.String.Hexdump
include Core_kernel.Identifiable.S with type t := t and type comparator_witness := comparator_witness
include sig ... end
val bin_read_t : t Bin_prot.Read.reader
val __bin_read_t__ : (Base.Int.t -> t) Bin_prot.Read.reader
val bin_reader_t : t Bin_prot.Type_class.reader
val bin_size_t : t Bin_prot.Size.sizer
val bin_write_t : t Bin_prot.Write.writer
val bin_writer_t : t Bin_prot.Type_class.writer
val bin_shape_t : Bin_prot.Shape.t
val t_of_sexp : Sexplib.Sexp.t -> t
include Core_kernel.Identifiable.S_common with type t := t
include sig ... end
val sexp_of_t : t -> Sexplib.Sexp.t
include Base.Stringable.S with type t := t
val of_string : string -> t
val to_string : t -> string
include Base.Pretty_printer.S with type t := t
val pp : Caml.Format.formatter -> t -> unit
include Core_kernel.Comparable.S_binable with type t := t with type comparator_witness := comparator_witness
include Base.Comparable_intf.S with type t := t with type comparator_witness := comparator_witness
include Base.Comparable_intf.Polymorphic_compare with type t := t
include Base.Comparisons.Infix with type t := t
val (>=) : t -> t -> bool
val (<=) : t -> t -> bool
val (=) : t -> t -> bool
val (>) : t -> t -> bool
val (<) : t -> t -> bool
val (<>) : t -> t -> bool
val equal : t -> t -> bool
val compare : t -> t -> int

compare t1 t2 returns 0 if t1 is equal to t2, a negative integer if t1 is less than t2, and a positive integer if t1 is greater than t2.

val min : t -> t -> t
val max : t -> t -> t
val ascending : t -> t -> int

ascending is identical to compare. descending x y = ascending y x. These are intended to be mnemonic when used like List.sort ~cmp:ascending and List.sort ~cmp:descending, since they cause the list to be sorted in ascending or descending order, respectively.

val descending : t -> t -> int
val between : t -> low:t -> high:t -> bool
val clamp_exn : t -> min:t -> max:t -> t

clamp_exn t ~min ~max returns t', the closest value to t such that between t' ~low:min ~high:max is true.

Raises if not (min <= max).

val clamp : t -> min:t -> max:t -> t Base.Or_error.t
include Base.Comparator.S with type t := t with type comparator_witness := comparator_witness
include Base.Comparable_intf.Validate with type t := t
val validate_lbound : min:t Base.Maybe_bound.t -> t Base.Validate.check
val validate_ubound : max:t Base.Maybe_bound.t -> t Base.Validate.check
val validate_bound : min:t Base.Maybe_bound.t -> max:t Base.Maybe_bound.t -> t Base.Validate.check
module Replace_polymorphic_compare = Core.String.Replace_polymorphic_compare
module Map = Core.String.Map
module Set = Core.String.Set
include Core_kernel.Hashable.S_binable with type t := t
include sig ... end
val hash_fold_t : Base.Hash.state -> t -> Base.Hash.state
val hash : t -> Base.Hash.hash_value
module Table = Core.String.Table
module Hash_set = Core.String.Hash_set
module Hash_queue = Core.String.Hash_queue

Like gen, but generate strings with the given distribution of characters.

Like gen', but generate strings with the given length.

module Stable = Core.String.Stable

Note that string is already stable by itself, since as a primitive type it is an integral part of the sexp / bin_io protocol. String.Stable exists only to introduce String.Stable.Set and String.Stable.Map, and provide interface uniformity with other stable types.

include module type of struct include Extended_string end
val collate : string -> string -> int

collate s1 s2 sorts string in an order that's usaully more suited for human consumption by treating ints specially, e.g. it will output: ["rfc1.txt";"rfc822.txt";"rfc2086.txt"].

It works by splitting the strings in numerical and non-numerical chunks and comparing chunks two by two from left to right (and starting on a non numerical chunk):

  • Non_numerical chunks are compared using lexicographical ordering.
  • Numerical chunks are compared based on the values of the represented ints and the number of trailing zeros.

It is a total order.

val unescaped : ?strict:bool -> string -> string

unescaped s is the inverse operation of escaped: it takes a string where all the special characters are escaped following the lexical convention of OCaml and returns an unescaped copy. The strict switch is on by default and makes the function treat illegal backslashes as errors. When strict is false every illegal backslash except escaped numeral greater than 255 is copied literally. The aforementioned numerals still raise errors. This mimics the behaviour of the ocaml lexer.

val unescaped_res : ?strict:bool -> string -> (string, int * string) Core.Result.t

Same as unescaped but instead of raising Failure _ returns an error message with the position in the string in case of failure.

val squeeze : string -> string

squeeze str reduces all sequences of spaces, newlines, tables, and * carriage returns to single spaces.

val is_substring_deprecated : substring:string -> string -> bool

Use Core.String.is_substring instead of this function. This wrapper is here (for now) to maintain bug compatibility.

val pad_left : ?char:char -> string -> int -> string

pad_left ~char s len Returns s padded to the length len by adding characters char to the left of the string. If s is already longer than len it is returned unchanged.

val pad_right : ?char:char -> string -> int -> string
val line_break : len:int -> string -> string list

deprecated in favour of word_wrap

val word_wrap : ?trailing_nl:bool -> ?soft_limit:int -> ?hard_limit:int -> ?nl:string -> string -> string

word_wrap ~soft_limit s

Wraps the string so that it fits the length soft_limit. It doesn't break words unless we go over hard_limit.

if nl is passed it is inserted instead of the normal newline character.

val edit_distance : ?transpose:unit -> string -> string -> int

Gives the Levenshtein distance between 2 strings, which is the number of insertions, deletions, and substitutions necessary to turn either string into the other. With the transpose argument, it alsos considers transpositions (Damerau-Levenshtein distance).

OCaml

Innovation. Community. Security.