XRope.mesa
Copyright Ó 1988 by Xerox Corporation. All rights reserved.
Doug Wyatt, February 19, 1988 2:18:36 pm PST
Part 1: Basic operations and definitions
XCHAR: TYPE ~ MACHINE DEPENDENT {(CARDINAL.LAST)};
XTEXT: TYPE ~ RECORD [length: NAT, chars: PACKED SEQUENCE maxLength: NAT OF XCHAR];
XROPE: TYPE ~ REF XRopeRep;
NoRope:
ERROR;
... is signalled if rope is invalid variant or some other invariant has broken. This is a serious error, and indicates that either storage is corrupted, or the user has supplied a bad routine when making a rope, or some other nasty bug.
Note: BoundsFault = RuntimeError.BoundsFault; it is raised by many of the Rope operations.
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.
Cat:
PROC [r1, r2, r3, r4, r5:
XROPE ←
NIL]
RETURNS [
XROPE];
... returns the concatenation of up to five ropes (limit based on eval stack depth). BoundsFault occurs if the result would be longer than LAST[INT].
Concat:
PROC [base, rest:
XROPE ←
NIL]
RETURNS [
XROPE];
... is the two-rope (faster) version of Cat. BoundsFault occurs if the result would be longer than LAST[INT].
Compare:
PROC [s1, s2:
XROPE, case:
BOOL ←
TRUE]
RETURNS [Basics.Comparison];
... returns the lexicographic comparison of the two ropes based on CHAR collating sequence. case => case of characters is significant.
Equal:
PROC [s1, s2:
XROPE, case:
BOOL ←
TRUE]
RETURNS [
BOOL];
... tests contents equality of s1 and s2. Faster than Compare. case => case of characters is significant.
Fetch:
PROC [base:
XROPE, index:
INT ← 0]
RETURNS [
XCHAR];
... fetches indexed character from given ropes. BoundsFault occurs if index < 0 or index is >= Length[base].
InlineFetch:
PROC [base:
XROPE, index:
INT]
RETURNS [
XCHAR] ~
INLINE {
... is the fast version of Fetch, since no procedure call is done when the rope is flat.
WITH base
SELECT
FROM text: Text =>
RETURN[text[index]];
ENDCASE => RETURN[Fetch[base]];
};
Index:
PROC [s1:
XROPE, pos1:
INT ← 0, s2:
XROPE, case:
BOOL ←
TRUE]
RETURNS [
INT];
... returns the smallest character position N such that N >= pos1 and Equal[Substr[s1, N, Length[s2]], s2, case]. If s2 does not occur in s1 at or after pos1, Length[s1] is returned. case => case of characters is significant. BoundsFault occurs when pos1 < 0.
Find:
PROC [s1, s2:
XROPE, pos1:
INT ← 0, case:
BOOL ←
TRUE]
RETURNS [
INT];
... is like Index, returning the smallest character position N such that N >= pos1 and Equal[Substr[s1, N, Length[s2]], s2, case], except that Find returns -1 if s2 is not found. case => case of characters is significant. BoundsFault occurs when pos1 < 0.
FindBackward:
PROC [s1, s2:
XROPE, pos1:
INT ← MaxLen, case:
BOOL ←
TRUE]
RETURNS [
INT];
... is like Find, except that it returns the largest character position N such that N <= pos1.
IsEmpty:
PROC [r:
XROPE]
RETURNS [
BOOL];
... is equivalent to Size[r] = 0.
Size:
PROC [base:
XROPE]
RETURNS [
INT];
... returns the # of characters in the given rope.
InlineSize:
PROC [base:
XROPE]
RETURNS [
INT] ~
INLINE {
RETURN[IF base=NIL THEN 0 ELSE base.size];
};
Replace:
PROC [base:
XROPE, start:
INT ← 0, len:
INT ← MaxLen, with:
XROPE ←
NIL]
RETURNS [
XROPE];
... returns rope with given range replaced by new. BoundsFault occurs when start < 0 or start > Size[base] or the result would be longer than LAST[INT].
Substr:
PROC [base:
XROPE, start:
INT ← 0, len:
INT ← MaxLen]
RETURNS [
XROPE];
... returns a subrope of the base. BoundsFault occurs if start < 0 or start > Size[base].
Part 2: Extended operations and definitions
Run:
PROC [s1:
XROPE, pos1:
INT ← 0, s2:
XROPE, pos2:
INT ← 0, case:
BOOL ←
TRUE]
RETURNS [
INT];
... returns largest number of chars N such that EqualSubstrs[s1,pos1,N, s2,pos2,N, case]. Returns 0 if pos1 >= Size[s1] or pos2 >= Size[s2]. Raises BoundsFault if pos1 < 0 or pos2 < 0.
IsPrefix:
PROC [prefix:
XROPE, subject:
XROPE, case:
BOOL ←
TRUE]
RETURNS [
BOOL];
... returns Run[s1: prefix, s2: subject, case: case]=Size[prefix]; that is, returns TRUE iff prefix is a prefix of subject.
Match:
PROC [pattern, object:
XROPE, case:
BOOL ←
TRUE]
RETURNS [
BOOL];
... returns TRUE iff object matches the pattern, where the pattern may contain * to indicate that 0 or more characters will match. If case is true, then case matters.
SkipTo:
PROC [s:
XROPE, pos:
INT ← 0, skip:
XROPE]
RETURNS [
INT];
... returns the lowest position N in s such that s[N] is in the skip rope and N >= pos. If pos > Size[s] or no such character occurs in s, then return Size[s]. BoundsFault occurs when pos < 0.
SkipOver:
PROC [s:
XROPE, pos:
INT ← 0, skip:
XROPE]
RETURNS [
INT];
... returns the lowest position N in s such that s[N] is NOT in the skip rope and N >= pos. If pos > Size[s] or no such character occurs in s, then return Size[s]. BoundsFault occurs when pos < 0.
Map:
PROC [base:
XROPE, start:
INT ← 0, len:
INT ← MaxLen, action: ActionType]
RETURNS [
BOOL];
... applies the action to the given range of characters in the rope. Returns TRUE when some action returns TRUE. BoundsFault occurs when start < 0 or start > Size[base].
Translate:
PROC [base:
XROPE, start:
INT ← 0, len:
INT ← MaxLen,
translator: TranslatorType ←
NIL]
RETURNS [new:
XROPE];
... applies the translation to get a new rope. If the resulting size > 0, then new does not share with the original rope! If translator = NIL, the identity translation is performed
Flatten:
PROC [base:
XROPE, start:
INT ← 0, len:
INT ← MaxLen]
RETURNS [Text];
... returns a flat rope from the given range of characters. BoundsFault occurs if the resulting length would be > LAST[NAT].
InlineFlatten:
PROC [r:
XROPE]
RETURNS [Text] =
INLINE {
... is the fast version of Flatten, since there is no procedure call for something already flat.
WITH r
SELECT
FROM text: Text =>
RETURN[text];
ENDCASE => RETURN[IF r=NIL THEN NIL ELSE Flatten[r]];
};
FromProc:
PROC [len:
INT, p:
PROC
RETURNS [
XCHAR], maxPiece:
INT ← MaxLen]
RETURNS [
XROPE];
... returns a new rope given a proc to apply for each XCHAR
FromChar:
PROC [c:
XCHAR]
RETURNS [
XROPE];
... makes a rope from a single character
MakeRope:
PROC [base:
REF, size:
INT, fetch: FetchType,
map: MapType ←
NIL, append: AppendType ←
NIL]
RETURNS [
XROPE];
... returns a rope using user-supplied procedures and data. Note that the user procedures MUST survive as long as the rope does!
AppendChars:
PROC [buffer:
REF
XTEXT, rope:
XROPE, start:
INT ← 0, len:
INT ←
LAST[
INT]]
RETURNS [charsMoved:
NAT];
... appends characters to the end of a REF XTEXT buffer, starting at start within the rope. The move stops if there are no more characters from the rope OR len characters have been moved OR the buffer is full (buffer.length = buffer.maxLength); charsMoved is always the # of characters appended. NOTE: the user is responsible for protecting buffer from concurrent modifications.
ContainingPiece:
PROC [rope:
XROPE, index:
INT ← 0]
RETURNS [base:
XROPE, start:
INT, len:
INT];
... finds the largest piece containg the given index such that the result is either a text or an object variant. (NIL, 0, 0) is returned if the index is NOT in the given rope.
Balance:
PROC [base:
XROPE, start:
INT ← 0, len:
INT ← MaxLen, flat:
INT ← FlatMax]
RETURNS [
XROPE];
... returns a balanced rope, possibly with much copying of components
flat' ← MIN[MAX[flat,FlatMax], LAST[NAT]]
len' ← MIN[MAX[len,0], Size[base]-start]
start < 0 OR start > Size[base] => bounds fault
the resulting maxDepth will be limited by 2+log2[len'/flat']
VerifyStructure:
PROC [s:
XROPE]
RETURNS [leaves, nodes, maxDepth:
INT];
... traverses the structure of the given rope; return the number of leaves, nodes and the max depth of the rope extra checking is performed to verify invariants a leaf is a text or object variant a node is a non-NIL, non-leaf variant shared leaves and nodes are multiply counted.
VerifyFailed:
ERROR;
occurs when VerifyStructure finds a bad egg
should not happen, of course
Part 3: Miscellaneous definitions
FetchType:
TYPE =
PROC [data:
REF, index:
INT]
RETURNS [
XCHAR];
... is the type of fetch routine used to make a user rope.
MapType:
TYPE =
PROC [base:
REF, start, len:
INT, action: ActionType]
RETURNS [quit:
BOOL ←
FALSE];
... is the type of user routine used to map over a subrope; returns TRUE if some action returns TRUE.
AppendType:
TYPE =
PROC [buffer:
REF
XTEXT, data:
REF, start:
INT]
RETURNS [charsMoved:
INT];
... is the type of user routine used to move characters to text buffer. The move should stop if there are no more characters from the rope OR block.count characters have been moved; charsMoved is always the # of characters moved. The data given is from the object variant.
ActionType:
TYPE =
PROC [c:
XCHAR]
RETURNS [quit:
BOOL ←
FALSE];
... is the type of routine applied when mapping; returns TRUE to quit from Map.
TranslatorType:
TYPE =
PROC [old:
XCHAR]
RETURNS [new:
XCHAR];
... is the type of routine supplied to Translate.
XRopeRep:
PRIVATE
TYPE =
RECORD [
size: INT,
cases:
SELECT case: *
FROM
text => [chars: PACKED SEQUENCE max: NAT OF XCHAR],
substr => [base: XROPE, start: INT, depth: INTEGER],
concat => [base, rest: XROPE, pos: INT, depth: INTEGER],
replace => [base, replace: XROPE, start, oldPos, newPos: INT, depth: INTEGER],
object => [base: REF, fetch: FetchType, map: MapType, append: AppendType]
ENDCASE
];
Text: TYPE ~ REF TextRep;
TextRep: TYPE ~ XRopeRep.text;
MaxLen: INT = LAST[INT];
FlatMax: CARDINAL = 24;