2. Rope Operations
Signals raised through the interface
The following procedures raise
RuntimeError.BoundsFault:
Fetch, if index e Length[base] or index < 0.
Any procedure returning a text rope, if the length of the rope would exceed LAST[NAT].
Any procedure returning a rope, if the length of the rope would exceed MaxLen.
Any procedure with a start parameter, if start < 0 or start exceeds the length of the rope.
Any procedure with a pos (or pos1 or pos2) parameter, if pos < 0.
There are no values of a len parameter that raise errors: if len is too long it is shortened to indicate the rest of the rope, and if len < 0 the behavior will be as if len = 0.
VerifyStructure raises VerifyFailed when it finds a problem in the rope it is examining. This error should not normally occur.
Any procedure may raise NoRope. This indicates that an internal consistency check has failed, and is not intended to be caught by normal clients. This error probably means that some unsafe operation in the client program has violated the type structure, though it may mean that the rope implementation is broken.
Details of the operations
Balance [base:
ROPE, start:
INT ← 0, len:
INT ← MaxLen, flat:
INT ← FlatMax]
RETURNS [
ROPE]
Returns the specified piece of base as a balanced tree. The leaves of the returned rope will be a maximum of flat characters if they are copied. The leaves will be a minimum of flat/2 characters in any case (unless there is a single leaf). The flat argument will be converted to MIN[MAX[flat,FlatMax],MaxLen] before being used. BoundsFault occurs when start < 0 or start > Length[base]. The len argument will be assigned MAX[MIN[len,Length[base]-start],0] before being used. Larger values of flat will result in less depth, but probably more copying.
Compare [s1, s2:
ROPE, case:
BOOL ←
TRUE]
RETURNS [Basics.Comparison]
Compares the contents of s1 and s2 lexicographically, using ASCII character codes. Returns less if s1 < s2, equal if s1 = s2, and greater if s1 > s2. If case is TRUE, the characters are compared exactly. If case is FALSE, upper case characters are translated into corresponding lower case characters before comparison.
Cat [r1, r2, r3, r4, r5:
ROPE ←
NIL]
RETURNS [
ROPE]
Returns a new rope, contents resulting from concatenating r1 .. r5. Copying occurs only when the result rope is small. BoundsFault occurs when the length of the resulting rope would exceed LAST[INT].
Concat [base, rest:
ROPE]
RETURNS [
ROPE]
Concatentates two ropes. Equivalent to, but faster than, Cat[base, rest].
ContainingPiece [ref:
ROPE, index:
INT ← 0]
RETURNS [base:
ROPE, start:
INT, len:
INT]
ContainingPiece[ref, index] returns the largest piece in ref such that base is either a text or object variant, and the characters indexed by [start..start+len) in base are the same as that indexed by [index..index+len) in ref. If index < 0 or index e Length[ref] then [NIL, 0, 0] is returned. Intended for use by applications demanding efficient access.
Equal [s1, s2:
ROPE, case:
BOOL ←
TRUE]
RETURNS [
BOOL]
Returns TRUE iff contents of s1 and s2 are the same, defined as: Length[s1] = Length[s2] AND FOR i IN [0..Length[s1]): Fetch[s1,i] = Fetch[s2,i] (modulo case significance).
Find [s1, s2:
ROPE, pos1:
INT ← 0, case:
BOOL ←
TRUE]
RETURNS [
INT]
Let N =
Find[s1, s2, pos1, case]. If N # -1 then N is the least N such that
N e pos1 & Equal[Substr[s1, N, Length[s2], s2, case]
If N = -1, then there is no such index. Note that if case is FALSE, then upper case characters are treated as their lower case equivalents. BoundsFault occurs when pos1 < 0.
Fetch [base:
ROPE, index:
INT ← 0]
RETURNS [
CHAR]
If 0 d index < Length[base], Fetch returns the indexed character in the rope, otherwise raises bounds fault. If the indexed character occurs in a client-defined rope, the client-supplied fetch routine may be called.
Flatten [base:
ROPE, start:
INT ← 0, len:
INT ← MaxLen]
RETURNS [Text]
If the piece given by base, start, and len is short enough to fit in a text variant of rope, then returns a text variant object including the contents of the specified piece. BoundsFault occurs when start < 0 or start > Length[base] or the result would more than LAST[NAT] characters. The object returned may be the same object that existed prior to the Flatten invocation.
FromChar [c:
CHAR]
RETURNS [Text]
Returns a new text rope containing the single character c.
FromProc [len:
INT, p:
PROC
RETURNS [
CHAR], maxPiece:
INT ← MaxLen]
RETURNS [
ROPE]
Returns a new rope r, where Length[r]=len, and the contents result from sequentially calling p. If len > maxPiece then the rope returned will be a balanced tree of flat ropes, each piece being no larger than maxPiece. The implementation may impose minimum and maximum limits on maxPiece (currently [24..LAST[NAT]]), and will adjust values outside of that range to be within the range.
FromRefText [s:
REF
TEXT]
RETURNS [Text]
Returns a rope with length and contents from the given REF TEXT. Copying will occur.
Index [s1:
ROPE, pos1:
INT, s2:
ROPE, case:
BOOL ←
TRUE]
RETURNS [
INT]
Similar to Find. Let N = Index[s1, pos1, s2, case]. If N # Length[s1] then N is the least integer such that
N e pos1 & Equal[Substr[s1, N, Length[s2], s2, case]
If N = Length[s1] then either no such occurence of s2 occurs in s1, or s2 is empty. Note that if case is FALSE, then upper case characters are treated as their lower case equivalents. BoundsFault occurs when pos1 < 0.
Length [base:
ROPE]
RETURNS [
INT]
Returns the number of characters in base. Note that Length[NIL] = 0.
MakeRope [base:
REF, size:
INT, fetch: FetchType, map: MapType ←
NIL,
pieceMap: PieceMapType ← NIL] RETURNS [ROPE]
Returns a new rope, implemented using the client-supplied size, base, and operations. Calling Rope operations using a client-defined rope results in calling the client-supplied procedures with the base reference. If the client does not supply map or pieceMap operations they will be implemented by default operations within the Rope implementation (using the client-supplied fetch procedure). The argument types are declared as:
FetchType: TYPE = PROC [REF, INT] RETURNS [CHAR]
MapType:
TYPE =
PROC
[base: REF, start, len: INT, action: ActionType] RETURNS [BOOL]
PieceMapType:
TYPE =
PROC
[base: REF, start, len: INT, action: PieceActionType] RETURNS [BOOL]
The fetch operation is used to fetch single characters. The map and pieceMap operations are used to map over characters and pieces from a client-defined rope, and are present for the sake of efficiency. The operations supplied should appear to be strictly functional, otherwise the correct behavior of rope operations cannot be guaranteed.
Map [base:
ROPE, start:
INT ← 0, len:
INT ← MaxLen, action: ActionType]
RETURNS [
BOOL]
Applies the action procedure to each character in the given piece, or until the action returns
TRUE. Map returns
TRUE if it was stopped by action returning
TRUE. The type of action is declared as:
ActionType: TYPE = PROC [CHAR] RETURNS [BOOL]
BoundsFault occurs when start < 0 or start > Length[base].
Match [pattern, object:
ROPE, case:
BOOL ←
TRUE]
RETURNS [
BOOL]
Returns TRUE iff object matches pattern. Matching takes place from left to right, and the * character in the pattern will match any number of characters in the object. If case is FALSE, alphabetic characters match either their upper or lower case equivalents, other such characters must match exactly.
Examples that return
TRUE:
Match["a*b", "axb", TRUE]
Match["Ab", "aB", FALSE]
Examples that return
FALSE:
Match["a*b", "aaa", TRUE]
Match["Ab", "aB", TRUE]
PieceMap [base:
ROPE, start:
INT ← 0, len:
INT ← MaxLen,
action: PieceActionType, mapUser: BOOL ← TRUE] RETURNS [BOOL]
Applies the action procedure to each leaf piece in the representation of the piece specified by base, start, and len. PieceMap terminates when all leaf pieces have been visited or the action returns TRUE. A leaf piece is either a text variant or a client-defined object. PieceMap returns TRUE if it was stopped by action returning TRUE. The leaf pieces that make up the given piece will either be of the text variant, or of the client-implemented variant, or (if mapUser is TRUE) of whatever rope variant is supplied by the client's PieceMap operation. The action routine supplied in client variants is only used if it is not NIL and the mapUser parameter is TRUE. The type of action is declared as:
PieceActionType: TYPE = PROC [ROPE, INT, INT] RETURNS [BOOL]
BoundsFault occurs when start < 0 or start > Length[base]
Replace [base:
ROPE, start:
INT ← 0, len:
INT ← MaxLen, replace:
ROPE ←
NIL]
RETURNS [
ROPE]
Returns a new rope, x, where x results from base with the piece indexed by start for len replaced by the contents of replace.
BoundsFault occurs when start < 0 or start >
Length[base] or the length of the resulting rope would exceed
LAST[
INT]. The following is true:
Equal[
Replace[base, start, len, replace],
Cat[Cat[Substr[base, 0, start], replace], Substr[base, start+len]],
TRUE]
Replace combines a delete with an insert at the same location, and is therefore suited for many text editing applications.
Run [s1:
ROPE, pos1:
INT ← 0, s2:
ROPE, pos2:
INT ← 0, case:
BOOL ←
TRUE]
RETURNS [
INT]
Returns largest number of chars N such that Equal[Substr[s1,pos1,N], Substr[s2,pos2,N], case]. If pos1 > Length[s1] or pos2 > Length[s2], then 0 will be returned. BoundsFault occurs when pos1 < 0 or pos2 < 0.
Size [base:
ROPE]
RETURNS [
INT]
Size[base] = Length[base].
SkipOver [s:
ROPE, pos:
INT ← 0, skip:
ROPE]
RETURNS [
INT]
Returns first position not before pos where Fetch[s] # a character in skip. If pos > Length[s], then pos will be returned. Otherwise, if no such position exists, Length[s] will be returned. BoundsFault occurs when pos < 0.
SkipTo [s:
ROPE, pos:
INT ← 0, skip:
ROPE]
RETURNS [
INT]
Returns first position not before pos where Fetch[s] = a character in skip. If pos > Length[s], then pos will be returned. Otherwise, if no such position exists, Length[s] will be returned. BoundsFault occurs when pos < 0.
Substr [base:
ROPE, start:
INT ← 0, len:
INT ← MaxLen]
RETURNS [
ROPE]
Returns a new rope, x, with contents from the given piece of base. Copying will not occur unless the resulting rope is small. The rope returned may have existed prior to the invocation of Substr. BoundsFault occurs when start < 0 or start > Length[base].
ToRefText [base:
ROPE]
RETURNS [
REF
TEXT]
Returns a new REF TEXT containing contents from the given rope. Copying always occurs.
Translate [base:
ROPE, start:
INT ← 0, len:
INT ← MaxLen, translator: TranslatorType ←
NIL]
RETURNS [ROPE]
Returns a new
ROPE constructed by applying the translator procedure to each character in the indicated piece. A
NIL translator performs no translation, but the returned rope will be a copy of the given rope. TranslatorType is defined as:
TranslatorType: TYPE = PROC [old: CHAR] RETURNS [new: CHAR];
BoundsFault occurs when start < 0 or start > Length[base].
VerifyStructure [s:
ROPE]
RETURNS [leaves, nodes, maxDepth:
INT]
Examines the rope s, verifying that certain representation invariants hold, and counting the leaves (text and object variants) and nodes (substr, concat, and replace variants) in the rope. The maximum depth of the rope is also returned. VerifyFailed occurs when a bad representation is discovered. This operation is intended for diagnostic use only.