module IString:Immutable strings with fast comparison.sig..end
Summary of strings vs istrings:
Pro/Con (depending on your POV):
typeistring =internalised istring_
typetemp_istring =temp istring_
If you want maximum ease of use, ignore temp_istrings, and just use
istrings. If you find that your program which builds a lot of strings is too slow,
temp_istrings MAY help improve performance.
See notes towards the end of this file.
type 'a istring_
istring and temp_istring. When you see this type as
the parameter of a function, it means that the function can take either an
internalised istring or a temporary istring.type internalised
type temp
typet =istring
IString can easily be used as parameter for standard Map
and Hash functors.val empty : istringval snoc : 'a istring_ -> char -> istringval is_empty : 'a istring_ -> boolval unsnoc : 'a istring_ -> istring * charval init : 'a istring_ -> istringval last : 'a istring_ -> charval head : 'a istring_ -> charval tail : 'a istring_ -> istringget or a fold.val length : 'a istring_ -> intval temp_istring : 'a istring_ -> temp_istring...of something to do
with the dreaded monomorphism retriction?..., recursive functions returning
temp_istring sometimes end up only accepting temp_istring, when really you
want any kind of istring. This function is used to fix that. (The
implementation of temp_istring is actually the identity function!).val equal : istring -> istring -> boolequal is recommended, as this prevents accidental
use of (==) on temp_istring, which will give unexpected result.val compare : istring -> istring -> intval lexical_compare : istring -> istring -> intval hash : 'a istring_ -> intval get : 'a istring_ -> int -> charval rev : 'a istring_ -> istringval append : 'a istring_ -> 'a istring_ -> istringval append_string : 'a istring_ -> string -> istringappend but right hand parameter is a string.val concat : ?sep:'a istring_ -> 'a istring_ list -> istringval to_string : 'a istring_ -> stringstring from an istring.val of_string : string -> istringistring from a string. O(n)val of_char : char -> istringistring from a char. O(1)val of_int : int -> istringistring from a int.val of_float : float -> istringistring from a float.val sub : 'a istring_ -> int -> int -> istringval fold_left : ('a -> char -> 'a) -> 'a -> 'b istring_ -> 'aval fold_right : (char -> 'a -> 'a) -> 'b istring_ -> 'a -> 'aval internalise : 'a istring_ -> istringtemp_istring into an
istring. On an istring, this is the identity function.
The functions below are equivelent to the functions above, but are
generally a little faster, and return temp_istring instead of istring.
Usually istrings are "internalised", so that they can be compared quickly. In the case that you are building a string in a number of steps, this may result in repeatedly internalising strings which are never compared.
For example, if you were to re-implement rev using snoc, the string would
be internalised once for each character in the string. All but one of those
strings would be thrown away and never compared. Internalising all those
strings costs CPU time, and puts extra load on the garbage collector.
By using the functions below, that return temp_istring instead of istring,
you are hinting that you don't yet need the string internalised. Actually, the
string may still get internalised sometimes, but typically it will be O(log n)
times, instead of O(n) times.
Although you can use == for lexical equality on istrings, you cannot do so
for temp_istrings.
When you are done building the string, internalise will turn it into a "real"
istring. Alternatively, many of the above functions will accept a
temp_istring and return a istring.
val unsnoc' : 'a istring_ -> temp_istring * charval snoc' : 'a istring_ -> char -> temp_istringval rev' : 'a istring_ -> temp_istringval append' : 'a istring_ -> 'a istring_ -> temp_istringval append_string' : 'a istring_ -> string -> temp_istringval concat' : ?sep:'a istring_ -> 'a istring_ list -> temp_istringval of_string' : string -> temp_istringval of_char' : char -> temp_istringval of_int' : int -> temp_istringval of_float' : float -> temp_istringval toplevel_istring_printer : Format.formatter -> istring -> unitval toplevel_temp_istring_printer : Format.formatter -> temp_istring -> unitval all_istrings : unit -> istring listval all_istrings' : unit -> string listval stats : unit -> int * int * int * int * int * int