Core.String
This module extends Base.String
.
include Sexplib0.Sexpable.S with type t := string
val t_sexp_grammar : string Sexplib0.Sexp_grammar.t
val sub : (string, string) Base.Blit.sub
sub
with no bounds checking, and always returns a new copy
val subo : (string, string) Base.Blit.subo
include Base.Indexed_container.S0_with_creators
with type t := string
with type elt = char
include Base.Container.S0_with_creators
with type t := string
with type elt = char
val of_list : elt list -> string
val of_array : elt array -> string
E.g., append (of_list [a; b]) (of_list [c; d; e])
is of_list [a; b; c; d; e]
map f (of_list [a1; ...; an])
applies f
to a1
, a2
, ..., an
, in order, and builds a result equivalent to of_list [f a1; ...; f an]
.
val filter : string -> f:(elt -> bool) -> string
filter t ~f
returns all the elements of t
that satisfy the predicate f
.
filter_map t ~f
applies f
to every x
in t
. The result contains every y
for which f x
returns Some y
.
val partition_tf : string -> f:(elt -> bool) -> string * string
partition_tf t ~f
returns a pair t1, t2
, where t1
is all elements of t
that satisfy f
, and t2
is all elements of t
that do not satisfy f
. The "tf" suffix is mnemonic to remind readers that the result is (trues, falses).
include Base.Container.S0 with type t := string with type elt := elt
val iter : string -> 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 : string -> init:'acc -> f:('acc -> elt -> 'acc) -> 'acc
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 :
string ->
init:'acc ->
f:('acc -> elt -> ('acc, 'e) Base.Result.t) ->
('acc, '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 :
string ->
init:'acc ->
f:('acc -> elt -> ('acc, 'final) Base.Container.Continue_or_stop.t) ->
finish:('acc -> '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
.
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 : string -> 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 : string -> 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 : string -> f:(elt -> bool) -> int
Returns the number of elements for which the provided function evaluates to true.
val sum :
(module Base.Container.Summable with type t = 'sum) ->
string ->
f:(elt -> 'sum) ->
'sum
Returns the sum of f i
for all i
in the container.
Returns as an option
the first element for which f
evaluates to true.
val find_map : string -> 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 : string -> elt list
val to_array : string -> elt array
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 uses fold
so it has the same complexity as fold
. Returns None
iff the collection is empty.
These are all like their equivalents in Container
except that an index starting at 0 is added as the first argument to f
.
val foldi : string -> init:_ -> f:(int -> _ -> elt -> _) -> _
val iteri : string -> f:(int -> elt -> unit) -> unit
val existsi : string -> f:(int -> elt -> bool) -> bool
val for_alli : string -> f:(int -> elt -> bool) -> bool
val counti : string -> f:(int -> elt -> bool) -> int
val find_mapi : string -> f:(int -> elt -> 'a option) -> 'a option
val init : int -> f:(int -> elt) -> string
init n ~f
is equivalent to of_list [f 0; f 1; ...; f (n-1)]
. It raises an exception if n < 0
.
mapi
is like map. Additionally, it passes in the index of each element as the first argument to the mapped function.
val filteri : string -> f:(int -> elt -> bool) -> string
include Base.Identifiable.S with type t := string
include Sexplib0.Sexpable.S with type t := string
include Base.Stringable.S with type t := string
include Base.Comparable.S with type t := string
include Base.Comparisons.S with type t := string
include Base.Comparisons.Infix with type t := string
include Base.Comparator.S with type t := string
type comparator_witness = Base.String.comparator_witness
include Base.Pretty_printer.S with type t := string
include Base.Invariant.S with type t := string
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.
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.
Concatenates all strings in the list using separator sep
(with a default separator ""
).
Special characters are represented by escape sequences, following the lexical conventions of OCaml.
Operates on the whole string using the US-ASCII character set, e.g. uppercase "foo" = "FOO"
.
Operates on just the first character using the US-ASCII character set, e.g. capitalize "foo" = "Foo"
.
index
gives the index of the first appearance of char
in the string when searching from left to right, or None
if it's not found. rindex
does the same but searches from the right.
For example, String.index "Foo" 'o'
is Some 1
while String.rindex "Foo" 'o'
is Some 2
.
The _exn
versions return the actual index (instead of an option) when char
is found, and raise Stdlib.Not_found
or Not_found_s
otherwise.
val to_sequence : string -> char Base.Sequence.t
Produce a sequence of the characters in a string.
val of_sequence : char Base.Sequence.t -> string
Read the characters in a full sequence and produce a string.
module Search_pattern = Base.String.Search_pattern
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
.
As with Search_pattern.replace_all
, the result may still contain pattern
.
is_substring ~substring:"bar" "foo bar baz"
is true.
is_substring_at "foo bar baz" ~pos:4 ~substring:"bar"
is true.
is_suffix s ~suffix
returns true
if s
ends with suffix
.
is_prefix s ~prefix
returns true
if s
starts with prefix
.
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 Stdlib.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 Stdlib.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"
.
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
.
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
.
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
.
Like map
, but allows the replacement of a single character with zero or two or more characters.
tr ~target ~replacement s
replaces every instance of target
in s
with replacement
.
val tr_multi :
target:string ->
replacement:string ->
(string -> string) Base.Staged.t
tr_multi ~target ~replacement
returns a function that replaces every instance of a character in target
with the corresponding character in replacement
.
If replacement
is shorter than target
, it is lengthened by repeating its last character. Empty replacement
is illegal unless target
also is.
If target
contains multiple copies of the same character, the last corresponding replacement
character is used. Note that character ranges are not supported, so ~target:"a-z"
means the literal characters 'a'
, '-'
, and 'z'
.
chop_suffix_exn s ~suffix
returns s
without the trailing suffix
, raising Invalid_argument
if suffix
is not a suffix of s
.
chop_prefix_exn s ~prefix
returns s
without the leading prefix
, raising Invalid_argument
if prefix
is not a prefix of s
.
chop_suffix_if_exists s ~suffix
returns s
without the trailing suffix
, or just s
if suffix
isn't a suffix of s
.
Equivalent to chop_suffix s ~suffix |> Option.value ~default:s
, but avoids allocating the intermediate option.
chop_prefix_if_exists s ~prefix
returns s
without the leading prefix
, or just s
if prefix
isn't a prefix of s
.
Equivalent to chop_prefix s ~prefix |> Option.value ~default:s
, but avoids allocating the intermediate option.
suffix s n
returns the longest suffix of s
of length less than or equal to n
.
prefix s n
returns the longest prefix of s
of length less than or equal to n
.
drop_suffix s n
drops the longest suffix of s
of length less than or equal to n
.
drop_prefix s n
drops the longest prefix of s
of length less than or equal to n
.
Produces the longest common suffix, or ""
if the list is empty.
Produces the longest common prefix, or ""
if the list is empty.
Produces the length of the longest common suffix, or 0 if the list is empty.
Produces the length of the longest common prefix, or 0 if the list is empty.
Produces the length of the longest common suffix.
Produces the length of the longest common prefix.
concat_array sep ar
like String.concat
, but operates on arrays.
Builds a multiline text from a list of lines. Each line is terminated and then concatenated. Equivalent to:
String.concat (List.map lines ~f:(fun line ->
line ^ if crlf then "\r\n" else "\n"))
pad_left ?char s ~len
returns s
padded to the length len
by adding characters char
to the beginning of the string. If s is already longer than len
it is returned unchanged.
pad_right ?char ~s len
returns s
padded to the length len
by adding characters char
to the end of the string. If s is already longer than len
it is returned unchanged.
Reports the Levenshtein edit distance between two strings. Computes the minimum number of single-character insertions, deletions, and substitutions needed to transform one into the other.
For strings of length M and N, its time complexity is O(M*N) and its space complexity is O(min(M,N)).
module Escaping = Base.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.
include Bin_prot.Binable.S_local with type t := t
include Bin_prot.Binable.S_local_only_functions with type t := t
include Bin_prot.Binable.S_only_functions with type t := t
val bin_size_t__local : t Bin_prot.Size.sizer_local
val bin_write_t__local : t Bin_prot.Write.writer_local
include Typerep_lib.Typerepable.S with type t := t
val typerep_of_t : t Typerep_lib.Std_internal.Typerep.t
val typename_of_t : t Typerep_lib.Typename.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.
slice t start stop
returns a new string including elements t.(start)
through t.(stop-1)
, normalized Python-style with the exception that stop = 0
is treated as stop = length t
.
val nget : t -> int -> char
nget s i
gets the char at normalized position i
in s
.
take_while s ~f
returns the longest prefix of s
satisfying for_all prefix ~f
(See lstrip
to drop such a prefix)
rtake_while s ~f
returns the longest suffix of s
satisfying for_all suffix ~f
(See rstrip
to drop such a suffix)
include Identifiable.S
with type t := t
and type comparator_witness := comparator_witness
include Bin_prot.Binable.S with type t := t
include Bin_prot.Binable.S_only_functions with type t := t
val bin_size_t : t Bin_prot.Size.sizer
val bin_write_t : t Bin_prot.Write.writer
val bin_read_t : t Bin_prot.Read.reader
val __bin_read_t__ : (int -> t) Bin_prot.Read.reader
This function only needs implementation if t
exposed to be a polymorphic variant. Despite what the type reads, this does *not* produce a function after reading; instead it takes the constructor tag (int) before reading and reads the rest of the variant t
afterwards.
val bin_shape_t : Bin_prot.Shape.t
val bin_writer_t : t Bin_prot.Type_class.writer
val bin_reader_t : t Bin_prot.Type_class.reader
val bin_t : t Bin_prot.Type_class.t
include Ppx_hash_lib.Hashable.S with type t := t
include Sexplib0.Sexpable.S with type t := t
val t_of_sexp : Sexplib0.Sexp.t -> t
include Ppx_compare_lib.Comparable.S with type t := t
include Ppx_hash_lib.Hashable.S with type t := t
val sexp_of_t : t -> Sexplib0.Sexp.t
include Base.Pretty_printer.S with type t := t
val pp : Base.Formatter.t -> t -> unit
include Comparable.S_binable
with type t := t
with type comparator_witness := comparator_witness
include Base.Comparable.S
with type t := t
with type comparator_witness := comparator_witness
include Base.Comparisons.S with type t := t
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
.
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
with type comparator_witness := comparator_witness
val validate_lbound : min:t Maybe_bound.t -> t Validate.check
val validate_ubound : max:t Maybe_bound.t -> t Validate.check
val validate_bound :
min:t Maybe_bound.t ->
max:t Maybe_bound.t ->
t Validate.check
module Replace_polymorphic_compare :
Base.Comparable.Comparisons with type t := t
include Comparator.S
with type t := t
with type comparator_witness := comparator_witness
val comparator : (t, comparator_witness) Base.Comparator.comparator
module Map :
Map.S_binable
with type Key.t = t
with type Key.comparator_witness = comparator_witness
module Set :
Set.S_binable
with type Elt.t = t
with type Elt.comparator_witness = comparator_witness
include Hashable.S_binable with type t := t
include Ppx_hash_lib.Hashable.S with type t := t
val hash_fold_t : Base.Hash.state -> t -> Base.Hash.state
val hash : t -> Base.Hash.hash_value
val hashable : t Base.Hashable.t
module Table : Hashtbl.S_binable with type key = t
module Hash_set : Hash_set.S_binable with type elt = t
module Hash_queue : Hash_queue.S with type key = t
include Diffable.S_atomic with type t := t
module Diff : sig ... end
include Quickcheckable.S with type t := t
val quickcheck_generator : t Base_quickcheck.Generator.t
val quickcheck_observer : t Base_quickcheck.Observer.t
val quickcheck_shrinker : t Base_quickcheck.Shrinker.t
val gen_nonempty : t Quickcheck.Generator.t
Like quickcheck_generator
, but without empty strings.
val gen' : char Quickcheck.Generator.t -> t Quickcheck.Generator.t
Like quickcheck_generator
, but generate strings with the given distribution of characters.
val gen_nonempty' : char Quickcheck.Generator.t -> t Quickcheck.Generator.t
Like gen'
, but without empty strings.
val gen_with_length :
int ->
char Quickcheck.Generator.t ->
t Quickcheck.Generator.t
Like gen'
, but generate strings with the given length.
module type Utf = sig ... end
module type Utf_as_string = Utf with type t = private string
Iterface for Unicode encodings, specialized for string representation.
module Utf8 :
Utf
with type t = Base.String.Utf8.t
and type comparator_witness = Base.String.Utf8.comparator_witness
module Utf16le :
Utf
with type t = Base.String.Utf16le.t
and type comparator_witness = Base.String.Utf16le.comparator_witness
module Utf16be :
Utf
with type t = Base.String.Utf16be.t
and type comparator_witness = Base.String.Utf16be.comparator_witness
module Utf32le :
Utf
with type t = Base.String.Utf32le.t
and type comparator_witness = Base.String.Utf32le.comparator_witness
module Utf32be :
Utf
with type t = Base.String.Utf32be.t
and type comparator_witness = Base.String.Utf32be.comparator_witness
module Stable : sig ... end
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
, String.Stable.Map
, String.Stable.Table
, and provide interface uniformity with other stable types.