Module Astring.String
Strings, substrings, string sets and maps.
A string s of length l is a zero-based indexed sequence of l bytes. An index i of s is an integer in the range [0;l-1], it represents the ith byte of s which can be accessed using the string indexing operator s.[i].
Important. OCaml's strings became immutable since 4.02. Whenever possible compile your code with the -safe-string option. This module does not expose any mutable operation on strings and assumes strings are immutable. See the porting guide.
v0.8.3 - homepage
String
val v : len:int -> (int -> char) -> stringv len fis a stringsof lengthlenwiths.[i] = f ifor all indicesiofs.fis invoked in increasing index order.- raises Invalid_argument
if
lenis not in the range [0;Sys.max_string_length].
val get : string -> int -> charget s iis the byte ofs' at indexi. This is equivalent to thes.[i]notation.- raises Invalid_argument
if
iis not an index ofs.
val head : ?rev:bool -> string -> char optionhead sisSome (get s h)withh = 0ifrev = false(default) orh = length s - 1ifrev = true.Noneis returned ifsis empty.
val get_head : ?rev:bool -> string -> charget_head sis likeheadbut- raises Invalid_argument
if
sis empty.
Appending strings
val append : string -> string -> stringappend s s'appendss'tos. This is equivalent tos ^ s'.- raises Invalid_argument
if the result is longer than
Sys.max_string_length.
val concat : ?sep:string -> string list -> stringconcat ~sep ssconcatenates the list of stringsss, separating each consecutive elements in the listsswithsep(defaults toempty).- raises Invalid_argument
if the result is longer than
Sys.max_string_length.
Predicates
val is_prefix : affix:string -> string -> boolis_prefix ~affix sistrueiffaffix.[i] = s.[i]for all indicesiofaffix.
val is_infix : affix:string -> string -> boolis_infix ~affix sistrueiff there exists an indexjinssuch that for all indicesiofaffixwe haveaffix.[i] = s.[j + i].
val is_suffix : affix:string -> string -> boolis_suffix ~affix sis true iffaffix.[n - i] = s.[m - i]for all indicesiofaffixwithn = String.length affix - 1andm = String.length s - 1.
val for_all : (char -> bool) -> string -> boolfor_all p sistrueiff for all indicesiofs,p s.[i] = true.
Extracting substrings
Tip. These functions extract substrings as new strings. Using substrings may be less wasteful and more flexible.
val with_range : ?first:int -> ?len:int -> string -> stringwith_range ~first ~len sare the consecutive bytes ofswhose indices exist in the range [first;first + len - 1].firstdefaults to0andlentomax_int. Note thatfirstcan be any integer andlenany positive integer.- raises Invalid_argument
if
lenis negative.
val with_index_range : ?first:int -> ?last:int -> string -> stringwith_index_range ~first ~last sare the consecutive bytes ofswhose indices exist in the range [first;last].firstdefaults to0andlasttoString.length s - 1.Note that both
firstandlastcan be any integer. Iffirst > lastthe interval is empty and the empty string is returned.
val trim : ?drop:(char -> bool) -> string -> stringtrim ~drop sisswith prefix and suffix bytes satisfyingdropinsremoved.dropdefaults toChar.Ascii.is_white.
val span : ?rev:bool -> ?min:int -> ?max:int -> ?sat:(char -> bool) -> string -> string * stringspan ~rev ~min ~max ~sat sis(l, r)where:- if
revisfalse(default),lis at leastminand at mostmaxconsecutivesatsatisfying initial bytes ofsoremptyif there are no such bytes.rare the remaining bytes ofs. - if
revistrue,ris at leastminand at mostmaxconsecutivesatsatisfying final bytes ofsoremptyif there are no such bytes.lare the remaining the bytes ofs.
If
maxis unspecified the span is unlimited. Ifminis unspecified it defaults to0. Ifmin > maxthe condition can't be satisfied and the left or right span, depending onrev, is always empty.satdefaults to(fun _ -> true).The invariant
l ^ r = sholds.- raises Invalid_argument
if
maxorminis negative.
- if
val take : ?rev:bool -> ?min:int -> ?max:int -> ?sat:(char -> bool) -> string -> stringtake ~rev ~min ~max ~sat sis the matching span ofspanwithout the remaining one. In other words:(if rev then snd else fst) @@ span ~rev ~min ~max ~sat s
val drop : ?rev:bool -> ?min:int -> ?max:int -> ?sat:(char -> bool) -> string -> stringdrop ~rev ~min ~max ~sat sis the remaining span ofspanwithout the matching span. In other words:(if rev then fst else snd) @@ span ~rev ~min ~max ~sat s
val cut : ?rev:bool -> sep:string -> string -> (string * string) optioncut ~sep sis either the pairSome (l,r)of the two (possibly empty) substrings ofsthat are delimited by the first match of the non empty separator stringseporNoneifsepcan't be matched ins. Matching starts from the beginning ofs(revisfalse, default) or the end (revistrue).The invariant
l ^ sep ^ r = sholds.- raises Invalid_argument
if
sepis the empty string.
val cuts : ?rev:bool -> ?empty:bool -> sep:string -> string -> string listcuts sep sis the list of all substrings ofsthat are delimited by matches of the non empty separator stringsep. Empty substrings are omitted in the list ifemptyisfalse(defaults totrue).Matching separators in
sstarts from the beginning ofs(revisfalse, default) or the end (revistrue). Once one is found, the separator is skipped and matching starts again, that is separator matches can't overlap. If there is no separator match ins, the list[s]is returned.The following invariants hold:
concat ~sep (cuts ~empty:true ~sep s) = scuts ~empty:true ~sep s <> []
- raises Invalid_argument
if
sepis the empty string.
val fields : ?empty:bool -> ?is_sep:(char -> bool) -> string -> string listfields ~empty ~is_sep sis the list of (possibly empty) substrings that are delimited by bytes for whichis_sepistrue. Empty substrings are omitted in the list ifemptyisfalse(defaults totrue).is_sepdefaults toChar.Ascii.is_white.
Substrings
type subThe type for substrings.
val sub_with_range : ?first:int -> ?len:int -> string -> subsub_with_rangeis likewith_rangebut returns a substring value. Iffirstis smaller than0the empty string at the start ofsis returned. Iffirstis greater than the last index ofsthe empty string at the end ofsis returned.
val sub_with_index_range : ?first:int -> ?last:int -> string -> subsub_with_index_rangeis likewith_index_rangebut returns a substring value. Iffirstandlastare smaller than0the empty string at the start ofsis returned. Iffirstand is greater than the last index ofsthe empty string at the end ofsis returned. Iffirst > lastandfirstis an index ofsthe empty string atfirstis returned.
module Sub : sig ... endSubstrings.
Traversing strings
val find : ?rev:bool -> ?start:int -> (char -> bool) -> string -> int optionfind ~rev ~start sat sis:- If
revisfalse(default). The smallest indexi, if any, greater or equal tostartsuch thatsat s.[i]istrue.startdefaults to0. - If
revistrue. The greatest indexi, if any, smaller or equal tostartsuch thatsat s.[i]istrue.startdefaults toString.length s - 1.
Note that
startcan be any integer.- If
val find_sub : ?rev:bool -> ?start:int -> sub:string -> string -> int optionfind_sub ~rev ~start ~sub sis:- If
revisfalse(default). The smallest indexi, if any, greater or equal tostartsuch thatsubcan be found starting atiinsthat iss.[i] = sub.[0],s.[i+1] = sub.[1], ...startdefaults to0. - If
revistrue. The greatest indexi, if any, smaller or equal tostartsuch thatsubcan be found starting atiinsthat iss.[i] = sub.[0],s.[i+1] = sub.[1], ...startdefaults toString.length s - 1.
Note that
startcan be any integer.- If
val filter : (char -> bool) -> string -> stringfilter sat sis the string made of the bytes ofsthat satisfysat, in the same order.
val filter_map : (char -> char option) -> string -> stringfilter_map f sis the string made of the bytes ofsas mapped byf, in the same order.
val map : (char -> char) -> string -> stringmap f siss'withs'.[i] = f s.[i]for all indicesiofs.fis invoked in increasing index order.
val mapi : (int -> char -> char) -> string -> stringmapi f siss'withs'.[i] = f i s.[i]for all indicesiofs.fis invoked in increasing index order.
val fold_left : ('a -> char -> 'a) -> 'a -> string -> 'afold_left f acc sisf (...(f (f acc s.[0]) s.[1])...) s.[m]withm = String.length s - 1.
val fold_right : (char -> 'a -> 'a) -> string -> 'a -> 'afold_right f s accisf s.[0] (f s.[1] (...(f s.[m] acc) )...)withm = String.length s - 1.
Uniqueness
Strings as US-ASCII character sequences
module Ascii : sig ... endUS-ASCII string support.
Pretty printing
val pp : Stdlib.Format.formatter -> string -> unitpp ppf sprintss's bytes onppf.
val dump : Stdlib.Format.formatter -> string -> unitdump ppf sprintssas a syntactically valid OCaml string onppfusingAscii.escape_string.
String sets and maps
module Set : sig ... endString sets.
module Map : sig ... endString maps.
OCaml base type conversions
val to_char : string -> char optionto_char sis the single byte insorNoneif there is no byte or more than one ins.
val of_bool : bool -> stringof_bool bis a string representation forb. Relies onPervasives.string_of_bool.
val to_bool : string -> bool optionto_bool sis aboolfroms, if any. Relies onPervasives.bool_of_string.
val of_int : int -> stringof_int iis a string representation fori. Relies onPervasives.string_of_int.
val to_int : string -> int optionto_intis anintfroms, if any. Relies onPervasives.int_of_string.
val of_nativeint : nativeint -> stringof_nativeint iis a string representation fori. Relies onNativeint.of_string.
val to_nativeint : string -> nativeint optionto_nativeintis annativeintfroms, if any. Relies onNativeint.to_string.
val of_int32 : int32 -> stringof_int32 iis a string representation fori. Relies onInt32.of_string.
val to_int32 : string -> int32 optionto_int32is anint32froms, if any. Relies onInt32.to_string.
val of_int64 : int64 -> stringof_int64 iis a string representation fori. Relies onInt64.of_string.
val to_int64 : string -> int64 optionto_int64is anint64froms, if any. Relies onInt64.to_string.