Module Kwstring

module Kwstring: sig .. end

String Functions


Author(s): Keith Waclena

exception NoTransformation
exception used by smap
val chars : char -> char -> char list
chars lo hi: generate a list of the characters between lo and hi inclusive.
val (--) : char -> char -> char list
Infix version of chars
val rev : string -> string
rev str: reverse the string
val compare_nocase : string -> string -> int
compare two strings, ignoring case

Faster than fun a b -> String.compare (String.lowercase a) (String.lowercase b) on long strings that differ near the beginning; slower (about 2x) if they don't differ or only differ near the end. So, better for random long strings. Tail-recursive.
Returns an int like Pervasives.compare (-, 0, +)

a : a string
val fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a
fold_left f a str is f (... (f (f a str.[0]) str.[1]) ...) str.[n-1].

Tail-recursive.
Returns whatever f combines

f : function f a c "combines" a (the result so far) and c (the current character)
a : identity element
str : string to fold
val foldl : ('a -> char -> 'a) -> 'a -> string -> 'a
Short name for Kwstring.fold_left.
val fold_right : (char -> 'a -> 'a) -> string -> 'a -> 'a
fold_right f str a is f str.[0] (f str.[1] (... (f str.[n-1] a) ...)).

Not tail-recursive.
Returns whatever f combines

f : function f c a "combines" a (the result so far) and c (the current character)
str : string to fold
a : identity element
val foldr : (char -> 'a -> 'a) -> string -> 'a -> 'a
Short name for Kwstring.fold_right.
val map : (char -> char) -> string -> string
Deprecated.Use String.map instead
map f str applies f to each char in str, returning a new string of the results.
Returns a new string
f : function (char -> char) to transform each character
str : a string
val smap : (char -> string) -> string -> string
smap f str applies f to each char in str, returning a new string of the results. Unlike map, f can transform a character into a string, so the length of the resulting string can be longer than the original. For example, you can HTML-quote a string with: smap (function | '&' -> "&amp;" | '<' -> "&lt;" | c -> String.make 1 c) If f raises NoTransformation, the character will be mapped into the new string unchanged; this is more efficient (by about a factor of two) as it saves on string construction in f. Hence the example above can be written: smap (function | '&' -> "&amp;" | '<' -> "&lt;" | _ -> raise NoTransformation)
Returns a new string
f : function (char -> string) to transform each character
str : a string
val split : ?_tr:'a -> ?merge:bool -> string -> string -> string list
split ?merge sep str: split str on a separator character (any of the characters in sep).
Returns list of strings
_tr : DEPRECATED
merge : consider runs of consecutive chars from sep to be one separator; so ~merge:false is good for /etc/passwd while ~merge:true is good for lexing "words" from text
sep : string interpreted as set of characters to split on
val wsplit : ?merge:bool -> string -> string list
wsplit ?merge str: shorthand for split " \t\n\r".
val whitespace : string
whitespace (space, tab, return, newline) suitable for trimleft, trimright and trim.
val trimleft : string -> string -> string
Trim leading sequence of characters from a string

Not tail recursive.
Returns trimmed string

cs : string of characters to trim, treated as a set
str : string to be trimmed
val trimright : string -> string -> string
Trim trailing sequence of characters from a string

Tail recursive.
Returns trimmed string

cs : string of characters to trim, treated as a set
str : string to be trimmed
val trim : string -> string -> string
Trim leading AND trailing sequence of characters from a string
Returns trimmed string
cs : string of characters to trim, treated as a set
str : string to be trimmed
val trimto : int -> string -> string
trim string to a maximum length by deleting excessive trailing characters

Returns a new string.

max : maximum length of string
str : the string to trim
val from : int -> string -> string
Return a right-hand substring of a string i.e. from some point to the end
start : index of first desired character in string; will be first character in result
str : the string
val contains : string -> string -> bool
contains cset str: test whether any of the characters in cset are contained in str.

Like flip String.contains except cset is a string interpreted as a set of characters.

cset : a string interpreted as a set of characters
str : string of characters to examine
val skipwhile : string -> string -> string
skip leading characters in string

Partially apply for efficiency.
Returns string without leading characters

cs : string of characters (interpreted as a set) to skip
val splitwhile : string -> string -> string * string
split string into two: first of pair is leading sequence of cs, second is remainder of string

Partially apply for efficiency.
Returns pair of leading component, rest of string

cs : string of characters (interpreted as a set) making up leading component
val splitat : int -> string -> string * string
splitat n str: given an integer and a string, splits the string into two strings (returned as a tuple) at the position corresponding to the given integer.

If n < 0, take it as an index from the end of the string. So splitat ~-1 "foo" = ("fo", "o").

If the integer is greater than the length of the string, it returns a tuple containing the entire string as its first element and the empty string as its second element.
Returns the two parts of the string

n : the split position
str : the string
val prefix : string -> string -> bool
prefix pre str: is pre a prefix of str?
type side = 
| Left
| Right
| Both
type of pad's ~side paramater
val pad : ?c:char -> ?trunc:bool -> ?side:side -> int -> string -> string
pad ?c ?trunc ?side n s: pad a string out to some size.

With ~side:Both, can also be thought of as centering text.
Returns padded string with length >= n (trunc = false) or length = n (trunc = true)

c : character to pad with (default: ' ' (space))
trunc : whether to truncate string longer than n to n chars (default: false)
side : which side of string tp pad: Left, Right or Both; also determines which side gets truncated (default: Right)
n : desired length of string
s : string to pad
val repeat : char -> int -> string
repeat c n: return a string consisting of the character c repeated n times.
c : character to repeat
n : desired length of string
val base2 : ?width:int -> int -> string
convert int n to binary (base-2) string representation
width : width of string in characters (default: use minium number of chars)
n : integer to convert
val of_chars : ?len:int -> char list -> string
of_chars ?len list: turn list of chars into a string
Returns the string
len : size of internal buffer (set to List.length list for greatest efficiency)
list : the list
val untabify : ?tc:char -> ?stop:int -> string -> string
untabify ?tc ?stop str: convert all tabs in str to multiple spaces, preserving columns.
tc : the tabulation character (default: '\t')
stop : the tab stop (default: 8)
str : the string to untabify