Module CCString

module CCString: sig .. end

Basic String Utils

Consider using Containers_string.KMP for pattern search, or Regex libraries.


type 'a gen = unit -> 'a option 
type 'a sequence = ('a -> unit) -> unit 
type 'a klist = unit -> [ `Cons of 'a * 'a klist | `Nil ] 

Common Signature


module type S = sig .. end

Strings


include String
val equal : string -> string -> bool
val compare : string -> string -> int
val hash : string -> int
val init : int -> (int -> char) -> string
Analog to Array.init.
Since 0.3.3
val rev : string -> string
rev s returns the reverse of s
Since 0.17
val pad : ?side:[ `Left | `Right ] -> ?c:char -> int -> string -> string
pad n str ensures that str is at least n bytes long, and pads it on the side with c if it's not the case.
Since 0.17
side : determines where padding occurs (default: `Left)
c : the char used to pad (default: ' ')
val of_char : char -> string
of_char 'a' = "a"
Since 0.19
val of_gen : char gen -> string
val of_seq : char sequence -> string
val of_klist : char klist -> string
val of_list : char list -> string
val of_array : char array -> string
val to_array : string -> char array
val find : ?start:int -> sub:string -> string -> int
Find sub in string, returns its first index or -1.
val find_all : ?start:int -> sub:string -> string -> int gen
find_all ~sub s finds all occurrences of sub in s, even overlapping instances.
Since 0.17
start : starting position in s
val find_all_l : ?start:int -> sub:string -> string -> int list
find_all ~sub s finds all occurrences of sub in s and returns them in a list
Since 0.17
start : starting position in s
val mem : ?start:int -> sub:string -> string -> bool
mem ~sub s is true iff sub is a substring of s
Since 0.12
val rfind : sub:string -> string -> int
Find sub in string from the right, returns its first index or -1. Should only be used with very small sub
Since 0.12
val replace : ?which:[ `All | `Left | `Right ] ->
sub:string -> by:string -> string -> string
replace ~sub ~by s replaces some occurrences of sub by by in s
Since 0.14
Raises Invalid_argument if sub = ""
which : decides whether the occurrences to replace are:
val is_sub : sub:string -> int -> string -> int -> len:int -> bool
is_sub ~sub i s j ~len returns true iff the substring of sub starting at position i and of length len is a substring of s starting at position j
val repeat : string -> int -> string
The same string, repeated n times
val prefix : pre:string -> string -> bool
prefix ~pre s returns true iff pre is a prefix of s
val suffix : suf:string -> string -> bool
suffix ~suf s returns true iff suf is a suffix of s
Since 0.7
val chop_prefix : pre:string -> string -> string option
chop_pref ~pre s removes pre from s if pre really is a prefix of s, returns None otherwise
Since 0.17
val chop_suffix : suf:string -> string -> string option
chop_suffix ~suf s removes suf from s if suf really is a suffix of s, returns None otherwise
Since 0.17
val take : int -> string -> string
take n s keeps only the n first chars of s
Since 0.17
val drop : int -> string -> string
drop n s removes the n first chars of s
Since 0.17
val take_drop : int -> string -> string * string
take_drop n s = take n s, drop n s
Since 0.17
val lines : string -> string list
lines s returns a list of the lines of s (splits along '\n')
Since 0.10
val lines_gen : string -> string gen
lines_gen s returns a generator of the lines of s (splits along '\n')
Since 0.10
val concat_gen : sep:string -> string gen -> string
concat_gen ~sep g concatenates all strings of g, separated with sep.
Since 0.10
val unlines : string list -> string
unlines l concatenates all strings of l, separated with '\n'
Since 0.10
val unlines_gen : string gen -> string
unlines_gen g concatenates all strings of g, separated with '\n'
Since 0.10
val set : string -> int -> char -> string
set s i c creates a new string which is a copy of s, except for index i, which becomes c.
Since 0.12
Raises Invalid_argument if i is an invalid index
val iter : (char -> unit) -> string -> unit
Alias to String.iter
Since 0.12
val iteri : (int -> char -> unit) -> string -> unit
Iter on chars with their index
Since 0.12
val map : (char -> char) -> string -> string
Map chars
Since 0.12
val mapi : (int -> char -> char) -> string -> string
Map chars with their index
Since 0.12
val filter_map : (char -> char option) -> string -> string
Since 0.17
val filter : (char -> bool) -> string -> string
Since 0.17
val flat_map : ?sep:string -> (char -> string) -> string -> string
Map each chars to a string, then concatenates them all
Since 0.12
sep : optional separator between each generated string
val for_all : (char -> bool) -> string -> bool
True for all chars?
Since 0.12
val exists : (char -> bool) -> string -> bool
True for some char?
Since 0.12
include CCString.S
val ltrim : t -> t
trim space on the left (see String.trim for more details)
Since 1.2
val rtrim : t -> t
trim space on the right (see String.trim for more details)
Since 1.2

Operations on 2 strings


val map2 : (char -> char -> char) -> string -> string -> string
Map pairs of chars
Since 0.12
Raises Invalid_argument if the strings have not the same length
val iter2 : (char -> char -> unit) -> string -> string -> unit
Iterate on pairs of chars
Since 0.12
Raises Invalid_argument if the strings have not the same length
val iteri2 : (int -> char -> char -> unit) -> string -> string -> unit
Iterate on pairs of chars with their index
Since 0.12
Raises Invalid_argument if the strings have not the same length
val fold2 : ('a -> char -> char -> 'a) -> 'a -> string -> string -> 'a
Fold on pairs of chars
Since 0.12
Raises Invalid_argument if the strings have not the same length
val for_all2 : (char -> char -> bool) -> string -> string -> bool
All pairs of chars respect the predicate?
Since 0.12
Raises Invalid_argument if the strings have not the same length
val exists2 : (char -> char -> bool) -> string -> string -> bool
Exists a pair of chars?
Since 0.12
Raises Invalid_argument if the strings have not the same length

Ascii functions

Those functions are deprecated in String since 4.03, so we provide a stable alias for them even in older versions

val capitalize_ascii : string -> string
See String.
Since 0.18
val uncapitalize_ascii : string -> string
See String.
Since 0.18
val uppercase_ascii : string -> string
See String.
Since 0.18
val lowercase_ascii : string -> string
See String.
Since 0.18
val equal_caseless : string -> string -> bool
Comparison without respect to ascii lowercase.
Since 1.2

Finding

A relatively efficient algorithm for finding sub-strings

module Find: sig .. end

Splitting


module Split: sig .. end
val split_on_char : char -> string -> string list
Split the string along the given char
Since 1.2
val split : by:string -> string -> string list
Alias to CCString.Split.list_cpy
Since 1.2

Utils


val compare_versions : string -> string -> int
compare_versions a b compares version strings a and b, considering that numbers are above text.
Since 0.13
val edit_distance : string -> string -> int
Edition distance between two strings. This satisfies the classical distance axioms: it is always positive, symmetric, and satisfies the formula distance a b + distance b c >= distance a c

Slices

A contiguous part of a string
module Sub: sig .. end