Module Base__.String
val hash_fold_t : Base.Hash.state -> t -> Base.Hash.state
val hash : t -> Base.Hash.hash_value
include Base.Sexpable.S with type t := t
val t_of_sexp : Base.Sexp.t -> t
val sexp_of_t : t -> Base.Sexp.t
val blit : (t, bytes) Base.Blit.blit
val blito : (t, bytes) Base.Blit.blito
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
val iter : t -> f:(elt -> unit) -> unit
iter
must allow exceptions raised inf
to escape, terminating the iteration cleanly. The same holds for all functions below taking anf
.
val fold : t -> init:'accum -> f:('accum -> elt -> 'accum) -> 'accum
fold t ~init ~f
returnsf (... f (f (f init e1) e2) e3 ...) en
, wheree1..en
are the elements oft
.
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 offold
that runs in theResult
monad. Iff
returns anError _
, that value is returned without any additional invocations off
.
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 offold
. Iff
returnsStop _
the computation ceases and results in that value. Iff
returnsContinue _
, the fold will proceed. Iff
never returnsStop _
, the final result is computed byfinish
.Example:
type maybe_negative = | Found_negative of int | All_nonnegative of { sum : int } (** [first_neg_or_sum list] returns the first negative number in [list], if any, otherwise returns the sum of the list. *) let first_neg_or_sum = List.fold_until ~init:0 ~f:(fun sum x -> if x < 0 then Stop (Found_negative x) else Continue (sum + x)) ~finish:(fun sum -> All_nonnegative { sum }) ;; let x = first_neg_or_sum [1; 2; 3; 4; 5] val x : maybe_negative = All_nonnegative {sum = 15} let y = first_neg_or_sum [1; 2; -3; 4; 5] val y : maybe_negative = Found_negative -3
val exists : t -> f:(elt -> bool) -> bool
Returns
true
if and only if there exists an element for which the provided function evaluates totrue
. This is a short-circuiting operation.
val for_all : t -> f:(elt -> bool) -> bool
Returns
true
if and only if the provided function evaluates totrue
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__.Container_intf.Summable with type t = 'sum) -> t -> f:(elt -> 'sum) -> 'sum
Returns the sum of
f i
for alli
in the container.
val find : t -> f:(elt -> bool) -> elt option
Returns as an
option
the first element for whichf
evaluates to true.
val find_map : t -> f:(elt -> 'a option) -> 'a option
Returns the first evaluation of
f
that returnsSome
, and returnsNone
if there is no such element.
val to_list : t -> elt list
val to_array : t -> elt array
val min_elt : t -> compare:(elt -> elt -> int) -> elt option
Returns a min (resp. max) element from the collection using the provided
compare
function. In case of a tie, the first element encountered while traversing the collection is returned. The implementation usesfold
so it has the same complexity asfold
. ReturnsNone
iff the collection is empty.
include Base.Identifiable.S with type t := t
val hash_fold_t : Base.Hash.state -> t -> Base.Hash.state
val hash : t -> Base.Hash.hash_value
include Base.Sexpable.S with type t := t
val t_of_sexp : Base.Sexp.t -> t
val sexp_of_t : t -> Base.Sexp.t
include Base.Stringable.S with type t := t
include Base.Comparable.S with type t := t
include Base__.Comparable_intf.Polymorphic_compare
val ascending : t -> t -> int
ascending
is identical tocompare
.descending x y = ascending y x
. These are intended to be mnemonic when used likeList.sort ~compare:ascending
andList.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
between t ~low ~high
meanslow <= t <= high
val clamp_exn : t -> min:t -> max:t -> t
clamp_exn t ~min ~max
returnst'
, the closest value tot
such thatbetween 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
val length : t -> int
val get : t -> int -> char
val unsafe_get : string -> int -> char
unsafe_get t i
is likeget t i
but does not perform bounds checking. The caller must ensure that it is a memory-safe operation.
val create : int -> bytes
val make : int -> char -> t
val copy : t -> t
Assuming you haven't passed -unsafe-string to the compiler, strings are immutable, so there'd be no motivation to make a copy.
val init : int -> f:(int -> char) -> t
val fill : bytes -> pos:int -> len:int -> char -> unit
val (^) : t -> t -> t
String append. Also available unqualified, but re-exported here for documentation purposes.
Note that
a ^ b
must copy botha
andb
into a newly-allocated result string, soa ^ b ^ c ^ ... ^ z
is quadratic in the number of strings.String.concat
does not have this problem -- it allocates the result buffer only once.
val concat : ?sep:t -> t list -> t
Concatenates all strings in the list using separator
sep
(with a default separator""
).
val escaped : t -> t
Special characters are represented by escape sequences, following the lexical conventions of OCaml.
val contains : ?pos:int -> ?len:int -> t -> char -> bool
val uppercase : t -> t
Operates on the whole string using the US-ASCII character set, e.g.
uppercase "foo" = "FOO"
.
val lowercase : t -> t
val capitalize : t -> t
Operates on just the first character using the US-ASCII character set, e.g.
capitalize "foo" = "Foo"
.
module Caseless : sig ... end
Caseless
compares and hashes strings ignoring case, so that for exampleCaseless.equal "OCaml" "ocaml"
andCaseless.("apple" < "Banana")
aretrue
, andCaseless.Map
,Caseless.Table
lookup andCaseless.Set
membership is case-insensitive.
val index : t -> char -> int option
index_exn
andindex_from_exn
raiseCaml.Not_found
orNot_found_s
whenchar
cannot be found ins
.
val index_exn : t -> char -> int
val index_from : t -> int -> char -> int option
val index_from_exn : t -> int -> char -> int
val rindex : t -> char -> int option
rindex_exn
andrindex_from_exn
raiseCaml.Not_found
orNot_found_s
whenchar
cannot be found ins
.
val rindex_exn : t -> 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.
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
orpos >= length t
result in no match (hencesubstr_index
returnsNone
andsubstr_index_exn
raises).may_overlap
indicates whether to report overlapping matches, seeSearch_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 containpattern
.
val is_substring_at : t -> pos:int -> substring:t -> bool
is_substring_at "foo bar baz" ~pos:4 ~substring:"bar"
is true.
val to_list_rev : t -> char list
Returns the reversed list of characters contained in a list.
val lsplit2_exn : t -> on:char -> t * t
If the string
s
contains the characteron
, thenlsplit2_exn s ~on
returns a pair containings
split around the first appearance ofon
(from the left). RaisesCaml.Not_found
orNot_found_s
whenon
cannot be found ins
.
val rsplit2_exn : t -> on:char -> t * t
If the string
s
contains the characteron
, thenrsplit2_exn s ~on
returns a pair containings
split around the first appearance ofon
(from the right). RaisesCaml.Not_found
orNot_found_s
whenon
cannot be found ins
.
val lsplit2 : t -> on:char -> (t * t) option
lsplit2 s ~on
optionally returnss
split into two strings around the first appearance ofon
from the left.
val rsplit2 : t -> on:char -> (t * t) option
rsplit2 s ~on
optionally returnss
split into two strings around the first appearance ofon
from the right.
val split : t -> on:char -> t list
split s ~on
returns a list of substrings ofs
that are separated byon
. Consecutiveon
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 ofs
that are separated by one of the chars fromon
.on
are not grouped. So a grouping ofon
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 compriset
. 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 smallesti >= pos
such thatf i t.[i]
, if there is such ani
. By default,pos = 0
.
val rfindi : ?pos:int -> t -> f:(int -> char -> bool) -> int option
rfindi ?pos t ~f
returns the largesti <= pos
such thatf i t.[i]
, if there is such ani
. By defaultpos = length t - 1
.
val lstrip : ?drop:(char -> bool) -> t -> t
lstrip ?drop s
returns a string with consecutive chars satisfyingdrop
(by default white space, e.g. tabs, spaces, newlines, and carriage returns) stripped from the beginning ofs
.
val rstrip : ?drop:(char -> bool) -> t -> t
rstrip ?drop s
returns a string with consecutive chars satisfyingdrop
(by default white space, e.g. tabs, spaces, newlines, and carriage returns) stripped from the end ofs
.
val strip : ?drop:(char -> bool) -> t -> t
strip ?drop s
returns a string with consecutive chars satisfyingdrop
(by default white space, e.g. tabs, spaces, newlines, and carriage returns) stripped from the beginning and end ofs
.
val map : t -> f:(char -> char) -> t
val mapi : t -> f:(int -> char -> char) -> t
Like
map
, but passes each character's index tof
along with the char.
val foldi : t -> init:'a -> f:(int -> 'a -> char -> 'a) -> 'a
foldi
works similarly tofold
, but also passes the index of each character tof
.
val concat_map : ?sep:t -> t -> f:(char -> t) -> t
Like
map
, but allows the 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 satisfyingpredicate
.
val tr : target:char -> replacement:char -> t -> t
tr ~target ~replacement s
replaces every instance oftarget
ins
withreplacement
.
val tr_inplace : target:char -> replacement:char -> bytes -> unit
tr_inplace ~target ~replacement s
destructively modifiess
(in place!), replacing every instance oftarget
ins
withreplacement
.
val tr_multi : target:t -> replacement:t -> (t -> t) Base.Staged.t
tr_multi ~target ~replacement
returns a function that replaces every instance of a character intarget
with the corresponding character inreplacement
.If
replacement
is shorter thantarget
, it is lengthened by repeating its last character. Emptyreplacement
is illegal unlesstarget
also is.If
target
contains multiple copies of the same character, the last correspondingreplacement
character is used. Note that character ranges are not supported, so~target:"a-z"
means the literal characters'a'
,'-'
, and'z'
.
val chop_suffix_exn : t -> suffix:t -> t
chop_suffix_exn s ~suffix
returnss
without the trailingsuffix
, raisingInvalid_argument
ifsuffix
is not a suffix ofs
.
val chop_prefix_exn : t -> prefix:t -> t
chop_prefix_exn s ~prefix
returnss
without the leadingprefix
, raisingInvalid_argument
ifprefix
is not a prefix ofs
.
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 ofs
of length less than or equal ton
.
val prefix : t -> int -> t
prefix s n
returns the longest prefix ofs
of length less than or equal ton
.
val drop_suffix : t -> int -> t
drop_suffix s n
drops the longest suffix ofs
of length less than or equal ton
.
val drop_prefix : t -> int -> t
drop_prefix s n
drops the longest prefix ofs
of length less than or equal ton
.
val concat_array : ?sep:t -> t array -> t
concat_array sep ar
likeString.concat
, but operates on arrays.
val hash : t -> int
Slightly faster hash function on strings.
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.