Module Prime_string
Amendment to the standard library String
structure.
The original String
structure is not included, include Unprime_string
for a full replacement.
Construction and Conversion
Iteration over Elements
val fold : (char -> 'a -> 'a) -> string -> 'a -> 'a
fold f s
isf s.[n - 1] ∘ ... ∘ f s[0]
wheren = length s
.
val foldi : (int -> char -> 'a -> 'a) -> string -> 'a -> 'a
foldi f s
isf (n - 1) s.[n - 1] ∘ ... ∘ f 0 s[0]
wheren = length s
.
val count : (char -> bool) -> string -> int
count f s
is the number of occurrences of a characters ins
which fulfilf
.
Search Primitives
val skip_while : (char -> bool) -> string -> int -> int
skip_while f s
increments a position until it reaches the end ofs
or it points to a character ins
on whichf
returns false. In other words ifj = skip_while f s i
, thenslice i j s
is the largest substring starting ati
on whichf
is constant true.
val skip_until : (char -> bool) -> string -> int -> int
skip_until f
is an optimisation ofskip_while (not ∘ f)
.
val rskip_while : (char -> bool) -> string -> int -> int
rskip_while f s
decrements a position as long as the result points to a character ins
on whichf
is true.
val rskip_until : (char -> bool) -> string -> int -> int
rskip_until f
is an optimization ofrskip_while (not ∘ f)
.
Substring Predicates
Slicing
val slice : int -> int -> string -> string
slice i j s
is the slice ofs
from positioni
up to but not includingj
.
val slice_from : int -> string -> string
slice_from i s
is a shortcut forslice i (String.length s) s
.
val cut_affix : string -> string -> (string * string) option
cut_affix afx s
returns the substrings before and after the leftmost occurrence ofafx
ins
orNone
ifafx
does not occur ins
.
val rcut_affix : string -> string -> (string * string) option
rcut_affix afx s
returns the substrings before and after the rightmost occurrence ofafx
ins
orNone
ifafx
does not occur ins
.
val chop_affix : string -> string -> string list
chop_affix afx s
returns the substrings before, between, and after matches ofafx
ins
, except forchop_affix afx ""
, which always gives[]
. In other wordschop_affix afx
provides a primitive way of extracting the operands of an infix operatorafx
. Ifafx
can overlap, it is unspecified which match is used.
val cut_consecutive : (char -> bool) -> string -> (string * string) option
cut_consecutive f s
returns the substrings before and after the leftmost consecutive sequence of bytes satisfyingf
, orNone
if notString.exists f s
.
val rcut_consecutive : (char -> bool) -> string -> (string * string) option
cut_consecutive f s
returns the substrings before and after the rightmost consecutive sequence of bytes satisfyingf
, orNone
if notString.exists f s
.
val chop_consecutive : (char -> bool) -> string -> string list
chop_consecutive f s
returns the non-empty substrings before, between, and after consecutive sequences of bytesc
for whichf c
is true. In particularchop_consecutive Char.is_space
is suitable for splitting words separated by ASCII white-spaces.