package core

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Core.StringSource

This module extends Base.String.

include Sexplib0.Sexpable.S with type t := string
Sourceval t_sexp_grammar : string Sexplib0.Sexp_grammar.t
Sourceval sub : (string, string) Base.Blit.sub
Sourceval subo : (string, string) Base.Blit.subo
include Base.Indexed_container.S0 with type t := string with type elt = char
include Base.Container.S0 with type t := string with type elt = char
Sourcetype elt = char
Sourceval mem : string -> elt -> bool

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

Sourceval is_empty : string -> bool
Sourceval 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.

Sourceval fold : string -> 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.

Sourceval fold_result : string -> 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.

Sourceval fold_until : string -> init:'accum -> f:('accum -> elt -> ('accum, 'final) Base.Container.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.

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
Sourceval 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.

Sourceval 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.

Sourceval count : string -> f:(elt -> bool) -> int

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

Sourceval 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.

Sourceval find : string -> f:(elt -> bool) -> elt option

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

Sourceval 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.

Sourceval to_list : string -> elt list
Sourceval to_array : string -> elt array
Sourceval min_elt : string -> 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 uses fold so it has the same complexity as fold. Returns None iff the collection is empty.

Sourceval max_elt : string -> compare:(elt -> elt -> int) -> elt option

These are all like their equivalents in Container except that an index starting at 0 is added as the first argument to f.

Sourceval iteri : string -> f:(int -> elt -> unit) -> unit
Sourceval existsi : string -> f:(int -> elt -> bool) -> bool
Sourceval for_alli : string -> f:(int -> elt -> bool) -> bool
Sourceval counti : string -> f:(int -> elt -> bool) -> int
Sourceval findi : string -> f:(int -> elt -> bool) -> (int * elt) option
Sourceval find_mapi : string -> f:(int -> elt -> 'a option) -> 'a option
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
Sourcetype comparator_witness = Base.String.comparator_witness
include Base.Pretty_printer.S with type t := string
include Base.Invariant.S with type t := string
Sourceval invariant : string -> unit
Sourceval max_length : int

Maximum length of a string.

Sourceval length : string -> int
Sourceval get : string -> int -> char
Sourceval 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.

Sourceval make : int -> char -> string
Sourceval copy : string -> string

Assuming you haven't passed -unsafe-string to the compiler, strings are immutable, so there'd be no motivation to make a copy.

  • deprecated [since 2018-03] Use [Bytes.copy] instead
Sourceval init : int -> f:(int -> char) -> string
Sourceval (^) : string -> string -> string

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.

Sourceval concat : ?sep:string -> string list -> string

Concatenates all strings in the list using separator sep (with a default separator "").

Sourceval escaped : string -> string

Special characters are represented by escape sequences, following the lexical conventions of OCaml.

Sourceval contains : ?pos:int -> ?len:int -> string -> char -> bool
Sourceval uppercase : string -> string

Operates on the whole string using the US-ASCII character set, e.g. uppercase "foo" = "FOO".

Sourceval lowercase : string -> string
Sourceval capitalize : string -> string

Operates on just the first character using the US-ASCII character set, e.g. capitalize "foo" = "Foo".

Sourceval uncapitalize : string -> string

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 Caml.Not_found or Not_found_s otherwise.

Sourceval index : string -> char -> int option
Sourceval index_exn : string -> char -> int
Sourceval index_from : string -> int -> char -> int option
Sourceval index_from_exn : string -> int -> char -> int
Sourceval rindex : string -> char -> int option
Sourceval rindex_exn : string -> char -> int
Sourceval rindex_from : string -> int -> char -> int option
Sourceval rindex_from_exn : string -> int -> char -> int
Sourcemodule Search_pattern = Base.String.Search_pattern

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

Sourceval substr_index : ?pos:int -> string -> pattern:string -> 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.

Sourceval substr_index_exn : ?pos:int -> string -> pattern:string -> int
Sourceval substr_index_all : string -> may_overlap:bool -> pattern:string -> int list
Sourceval substr_replace_first : ?pos:int -> string -> pattern:string -> with_:string -> string
Sourceval substr_replace_all : string -> pattern:string -> with_:string -> string

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

Sourceval is_substring : string -> substring:string -> bool

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

Sourceval is_substring_at : string -> pos:int -> substring:string -> bool

is_substring_at "foo bar baz" ~pos:4 ~substring:"bar" is true.

Sourceval to_list_rev : string -> char list

Returns the reversed list of characters contained in a list.

Sourceval rev : string -> string

rev t returns t in reverse order.

Sourceval is_suffix : string -> suffix:string -> bool

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

Sourceval is_prefix : string -> prefix:string -> bool

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

Sourceval lsplit2_exn : string -> on:char -> string * string

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.

Sourceval rsplit2_exn : string -> on:char -> string * string

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.

Sourceval lsplit2 : string -> on:char -> (string * string) option

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

Sourceval rsplit2 : string -> on:char -> (string * string) option

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

Sourceval split : string -> on:char -> string 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.

Sourceval split_on_chars : string -> on:char list -> string 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.

Sourceval split_lines : string -> string list

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

Sourceval lfindi : ?pos:int -> string -> 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.

Sourceval rfindi : ?pos:int -> string -> 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.

Sourceval lstrip : ?drop:(char -> bool) -> string -> string

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.

Sourceval rstrip : ?drop:(char -> bool) -> string -> string

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.

Sourceval strip : ?drop:(char -> bool) -> string -> string

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.

Sourceval map : string -> f:(char -> char) -> string
Sourceval mapi : string -> f:(int -> char -> char) -> string

Like map, but passes each character's index to f along with the char.

Sourceval foldi : string -> init:'a -> f:(int -> 'a -> char -> 'a) -> 'a

foldi works similarly to fold, but also passes the index of each character to f.

Sourceval concat_map : ?sep:string -> string -> f:(char -> string) -> string

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

Sourceval filter : string -> f:(char -> bool) -> string

filter s ~f:predicate discards characters not satisfying predicate.

Sourceval filteri : string -> f:(int -> char -> bool) -> string

Like filter, but passes each character's index to f along with the char.

Sourceval tr : target:char -> replacement:char -> string -> string

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

Sourceval 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'.

Sourceval chop_suffix_exn : string -> suffix:string -> string

chop_suffix_exn s ~suffix returns s without the trailing suffix, raising Invalid_argument if suffix is not a suffix of s.

Sourceval chop_prefix_exn : string -> prefix:string -> string

chop_prefix_exn s ~prefix returns s without the leading prefix, raising Invalid_argument if prefix is not a prefix of s.

Sourceval chop_suffix : string -> suffix:string -> string option
Sourceval chop_prefix : string -> prefix:string -> string option
Sourceval chop_suffix_if_exists : string -> suffix:string -> string

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.

Sourceval chop_prefix_if_exists : string -> prefix:string -> string

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.

Sourceval suffix : string -> int -> string

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

Sourceval prefix : string -> int -> string

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

Sourceval drop_suffix : string -> int -> string

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

Sourceval drop_prefix : string -> int -> string

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

Sourceval common_suffix : string list -> string

Produces the longest common suffix, or "" if the list is empty.

Sourceval common_prefix : string list -> string

Produces the longest common prefix, or "" if the list is empty.

Sourceval common_suffix_length : string list -> int

Produces the length of the longest common suffix, or 0 if the list is empty.

Sourceval common_prefix_length : string list -> int

Produces the length of the longest common prefix, or 0 if the list is empty.

Sourceval common_suffix2 : string -> string -> string

Produces the longest common suffix.

Sourceval common_prefix2 : string -> string -> string

Produces the longest common prefix.

Sourceval common_suffix2_length : string -> string -> int

Produces the length of the longest common suffix.

Sourceval common_prefix2_length : string -> string -> int

Produces the length of the longest common prefix.

Sourceval concat_array : ?sep:string -> string array -> string

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

Sourceval of_char : char -> string
Sourceval of_char_list : char list -> string
Sourcemodule 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.

Sourcetype t = string
include Bin_prot.Binable.S with type t := t
Sourcemodule 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.

Sourceval slice : t -> int -> int -> t

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.

Sourceval nget : t -> int -> char

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

Sourceval 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)

Sourceval 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 Hexdump.S with type t := t
Sourcemodule Hexdump : sig ... end
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
Sourceval bin_size_t : t Bin_prot.Size.sizer
Sourceval bin_write_t : t Bin_prot.Write.writer
Sourceval bin_read_t : t Bin_prot.Read.reader
Sourceval __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.

Sourceval bin_shape_t : Bin_prot.Shape.t
include Ppx_hash_lib.Hashable.S with type t := t
include Sexplib0.Sexpable.S with type t := t
Sourceval 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
Sourceval sexp_of_t : t -> Sexplib0.Sexp.t
include Base.Stringable.S with type t := t
Sourceval of_string : string -> t
Sourceval to_string : t -> string
include Base.Pretty_printer.S with type t := t
Sourceval 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
include Base.Comparisons.Infix with type t := t
Sourceval (>=) : t -> t -> bool
Sourceval (<=) : t -> t -> bool
Sourceval (=) : t -> t -> bool
Sourceval (>) : t -> t -> bool
Sourceval (<) : t -> t -> bool
Sourceval (<>) : t -> t -> bool
Sourceval equal : t -> t -> bool
Sourceval 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.

Sourceval min : t -> t -> t
Sourceval max : t -> t -> t
Sourceval 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 ~compare:ascending and List.sort ~cmp:descending, since they cause the list to be sorted in ascending or descending order, respectively.

Sourceval descending : t -> t -> int
Sourceval between : t -> low:t -> high:t -> bool

between t ~low ~high means low <= t <= high

Sourceval 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).

Sourceval 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
Sourceval validate_lbound : min:t Maybe_bound.t -> t Validate.check
Sourceval validate_ubound : max:t Maybe_bound.t -> t Validate.check
Sourceval validate_bound : min:t Maybe_bound.t -> max:t Maybe_bound.t -> t Validate.check
include Hashable.S_binable with type t := t
include Ppx_hash_lib.Hashable.S with type t := t
Sourceval hash_fold_t : Base.Hash.state -> t -> Base.Hash.state
Sourceval hashable : t Base.Hashable.t
Sourcemodule Table : Hashtbl.S_binable with type key = t
Sourcemodule Hash_set : Hash_set.S_binable with type elt = t
Sourcemodule Hash_queue : Hash_queue.S with type key = t
include Quickcheckable.S with type t := t
Sourceval quickcheck_generator : t Base_quickcheck.Generator.t
Sourceval quickcheck_observer : t Base_quickcheck.Observer.t
Sourceval quickcheck_shrinker : t Base_quickcheck.Shrinker.t
Sourceval gen_nonempty : t Quickcheck.Generator.t

Like quickcheck_generator, but without empty strings.

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

Like gen', but without empty strings.

Sourceval gen_with_length : int -> char Quickcheck.Generator.t -> t Quickcheck.Generator.t

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

Sourcemodule 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.

OCaml

Innovation. Community. Security.