module Rope:Ropes ("heavyweight strings") are a scalable string implementation: they are designed for efficient operation that involve the string as a whole. Operations such as concatenation, and substring take time that is nearly independent of the length of the string. Unlike strings, ropes are a reasonable representation for very long strings such as edit buffers or mail messages.sig..end
Advantages:
Rope.Buffer instead)Rope.concat2) and taking a range
(Rope.sub), and sharing memory whenever possible.get is not O(1) -- but it is amortized O(1) if you use an
iterator (see the Rope.Iterator module). get is 2 to 3 times
slower than for a string so it should not be your main operation.
However, as soon as in addition you have a few concatenations
(especially with sharing) or splice operations, ropes will usually
outperform strings.module String = Rope to use ropes instead of strings
and, in particular, so that r.[i] gets the ith char of the
rope r. This module has most operations of String but not all:
it does not have set because ropes are immutable, not make
because it is basically useless in this context (the Rope.Buffer
is more powerful),...
To use this library in the toploop, issue #use "rope_top.ml";;
Author(s): Christophe Troestler
Version: 0.3
type t
typerope =t
Rope.texception Out_of_bounds of string
val empty : tval of_string : string -> tof_string s creates a rope from the string s. Because ropes are
immutable while strings are not, a copy of s is made.val of_substring : string -> int -> int -> tof_substring s start len create a new rope from the substring
s.[start .. start+len-1].Invalid_argument if start and len do not designate a valid
sbstring of s.val of_char : char -> tof_char c returns a rope consisting of the unique character
c. It may be useful to append a char to a given rope.val to_string : t -> stringto_string r return a string with the same content as the
rope. The rope and the string share no data.Failure if the rope is too long to fit into a string.val is_empty : t -> boolis_empty r tells whether the rope r is empty.val length : t -> intlength r returns the length of the rope. O(1) time.val get : t -> int -> charget r i returns the ith char of the rope.
Takes O(log(length r)).val sub : t -> int -> int -> tsub r i start len returns the sub-rope consisting of
characters from position start to start+len-1 (included) of
the rope r. O(log(length r)) time.Invalid_argument if i < 0, len < 0 or i + len >
Rope.length r.val concat2 : t -> t -> tconcat2 r1 r2 concatenates the ropes r1 and r2.val concat : t -> t list -> tconcat sep rl concatenates the list of ropes rl, inserting
the separator string sep between each.val iter : (char -> unit) -> t -> unititer f r execute f c for c going through every character
of rope r from left to right.val iteri : (int -> char -> unit) -> t -> unititer f r execute f i c for c going through every
character of rope r from left to right and i the index of
c.val escaped : t -> tval index : t -> char -> intindex r c returns the position of the leftmost occurrence of
character c in rope r.Not_found if c does not occur in r.val rindex : t -> char -> intrindex r c returns the position of the rightmost occurrence
of character c in rope r.Not_found if c does not occur in r.val index_from : t -> int -> char -> intRope.index, but start searching at the character
position given as second argument. Rope.index r c is
equivalent to Rope.index_from r 0 c.val rindex_from : t -> int -> char -> intRope.rindex, but start searching at the character
position given as second argument. Rope.rindex r c is
equivalent to Rope.rindex_from s (Rope.length r - 1) c.val contains : t -> char -> boolcontains r c tests if character c appears in the rope r.val contains_from : t -> int -> char -> boolcontains_from r start c tests if character c appears in
the subrope of r starting from start to the end of s.Invalid_argument if start is not a valid index of r.val rcontains_from : t -> int -> char -> boolrcontains_from r stop c tests if character c appears in
the subrope of r starting from the beginning of r to index
stop.Invalid_argument if stop is not a valid index of r.val uppercase : t -> tval lowercase : t -> tval capitalize : t -> tval uncapitalize : t -> tval compare : t -> t -> intPervasives.compare. Along with the type t, this function
compare allows the module Rope to be passed as argument to
the functors Set.Make and Map.Make.val equal : t -> t -> boolequal r1 r2 tells whether the two ropes r1 and r2 are
equal.val search_forward_string : string -> t -> int -> intsearch_forward_string p is a search function that, given a
rope r and a start index i0, will return the position of
p in r or raise Not_found if no occurrence of p in r
exists. let search = search_forward_string p takes
O(length p) and search r i0 takes O(length r - i0).
Input and output functions for ropes modelled on the standard
library Pervasives.
val input_line : ?leaf_length:int -> Pervasives.in_channel -> tEnd_of_file if the end of the file is reached at the
beginning of line.val read_line : unit -> tval print_string : t -> unitval print_endline : t -> unitval prerr_string : t -> unitval prerr_endline : t -> unitval output_string : Pervasives.out_channel -> t -> unitoutput_string oc r outputs the rope r to the output channel oc.
May also be used with a %a directive of printf.val output_rope : Pervasives.out_channel -> t -> unitRope.output_string.val balance : t -> tbalance r return a balanced copy of the rope r. Implicit
rebalancing is done by some of the above functions to avoid
gross inefficiencies but you may want to call this function
explicitely to try to improve your running times.val height : t -> intdepth r returns the depth of the rope r. This information
may be useful to decide whether you want to re-balance.val rebalancing_height : intrebalancing_height.module Iterator:sig..end
module Buffer:sig..end
Buffer module in the standard library
except that it constructs ropes.
module Regexp:sig..end
module Rope_toploop:sig..end