An extension of the standard StringLabels
. If you open Base
, you'll get these
extensions in the String
module.
include sig ... end
val hash_fold_t : Base.Hash.state ‑> t ‑> Base.Hash.state
val hash : t ‑> Base.Hash.hash_value
val t_of_sexp : Base.Sexp.t ‑> t
val sexp_of_t : t ‑> Base.Sexp.t
module Caseless : sig ... end
Caseless
compares and hashes strings ignoring case, so that for example
Caseless.equal "OCaml" "ocaml"
and Caseless.("apple" < "Banana")
are true
, and
Caseless.Map
, Caseless.Table
lookup and Caseless.Set
membership is
case-insensitive.
val unsafe_blit : (t, bytes) Base.Blit.blit
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
val length : t ‑> int
val is_empty : t ‑> bool
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_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, 'final) Base__.Container_intf.Continue_or_stop.t) ‑> finish:('accum ‑> 'final) ‑> 'final
fold_until t ~init ~f ~finish
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. If f
never returns Stop _
, the final result
is computed by finish
.
Returns true
if and only if there exists an element for which the provided
function evaluates to true
. This is a short-circuiting operation.
Returns true
if and only if the provided function evaluates to true
for all
elements. This is a short-circuiting operation.
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.
include Base.Identifiable.S 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
val t_of_sexp : Base.Sexp.t ‑> t
val sexp_of_t : t ‑> Base.Sexp.t
include Base.Comparable.S with type t := t
include Base__.Comparable_intf.Polymorphic_compare
ascending
is identical to compare
. descending x y = ascending y x
. These are
intended to be mnemonic when used like List.sort ~compare:ascending
and List.sort
~cmp:descending
, since they cause the list to be sorted in ascending or descending
order, respectively.
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
val comparator : (t, comparator_witness) Base.Comparator.comparator
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
external length : t ‑> int = "%string_length"
external get : t ‑> int ‑> char = "%string_safe_get"
external unsafe_get : string ‑> int ‑> char = "%string_unsafe_get"
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 make : int ‑> char ‑> t
val init : int ‑> f:(int ‑> char) ‑> t
val fill : bytes ‑> pos:int ‑> len:int ‑> char ‑> unit
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.
Warning: Only returns a copy if changes are necessary! Special characters are represented by escape sequences, following the lexical conventions of OCaml.
val contains : ?pos:int ‑> ?len:int ‑> t ‑> char ‑> bool
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 : sig ... end
Substring search and replace functions. They use the Knuth-Morris-Pratt algorithm (KMP) under the hood.
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
.
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
Caml.Not_found
or Not_found_s
when on
cannot be found in s
.
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
Caml.Not_found
or Not_found_s
when on
cannot be found in s
.
lsplit2 s ~on
optionally returns s
split into two strings around the
first appearance of on
from the left.
rsplit2 s ~on
optionally returns s
split into two strings around the first
appearance of on
from the right.
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.
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.
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.
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
.
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
.
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 foldi : t ‑> init:'a ‑> f:(int ‑> 'a ‑> char ‑> 'a) ‑> 'a
foldi
works similarly to fold
, but also passes the index of each character to
f
.
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
.
chop_suffix_exn s ~suffix
returns a copy of s
without the trailing suffix
,
raising Invalid_argument
if suffix
is not a suffix of s
.
chop_prefix_exn s ~prefix
returns a copy of s
without the leading prefix
,
raising Invalid_argument
if prefix
is not a prefix of s
.
concat_array sep ar
like String.concat, but operates on arrays.
val is_empty : t ‑> bool
val of_char : char ‑> t
val of_char_list : char list ‑> t
module Escaping : sig ... end
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.