DIRECTORY Basics USING [BoundsCheck, Comparison, RawBytes], Rope USING [ActionType, Compare, Equal, Find, Map, Match, SkipOver, SkipTo, ROPE, Text, TextBound]; RefText: CEDAR DEFINITIONS IMPORTS Basics, Rope = BEGIN PureText: TYPE = REF READONLY TEXT; ROPE: TYPE = Rope.ROPE; MaxLen: TextBound = TextBound.LAST; TextBound: TYPE = Rope.TextBound; New: PROC [nChars: TextBound] RETURNS [REF TEXT]; Error: ERROR [ec: ErrorCode]; ErrorCode: TYPE = { clientModifiedReleasedText }; ObtainScratch: PROC [nChars: TextBound ¬ line] RETURNS [REF TEXT]; line: TextBound = 100; page: TextBound = 512; ReleaseScratch: PROC [t: REF TEXT]; Append: PROC [to: REF TEXT, from: PureText, start: TextBound ¬ 0, len: TextBound ¬ MaxLen] RETURNS [REF TEXT]; AppendTextRope: PROC [to: REF TEXT, from: Rope.Text, start: TextBound ¬ 0, len: TextBound ¬ MaxLen] RETURNS [REF TEXT] = INLINE { RETURN[Append[to, TrustTextRopeAsText[from], start, len]]; }; AppendRope: PROC [to: REF TEXT, from: ROPE, start: INT ¬ 0, len: TextBound ¬ MaxLen] RETURNS [REF TEXT]; AppendChar: PROC [to: REF TEXT, from: CHAR] RETURNS [REF TEXT]; InlineAppendChar: PROC [to: REF TEXT, from: CHAR] RETURNS [REF TEXT] = INLINE { IF to.length >= to.maxLength THEN RETURN [AppendChar[to, from]]; to[to.length] ¬ from; to.length ¬ to.length + 1; RETURN [to]; }; ReserveChars: PROC [to: REF TEXT, nChars: TextBound] RETURNS [REF TEXT]; InlineReserveChars: PROC [to: REF TEXT, nChars: TextBound] RETURNS [REF TEXT] = INLINE { IF LOOPHOLE[to.maxLength, INTEGER]-LOOPHOLE[nChars, INTEGER] < LOOPHOLE[to.length, INTEGER] THEN RETURN [ReserveChars[to, nChars]]; RETURN [to]; }; Compare: PROC [s1, s2: PureText, case: BOOL ¬ TRUE] RETURNS [Basics.Comparison] = INLINE { RETURN [Rope.Compare[TrustTextAsRope[s1], TrustTextAsRope[s2], case]]; }; Equal: PROC [s1, s2: PureText, case: BOOL ¬ TRUE] RETURNS [BOOL] = INLINE { RETURN [Rope.Equal[TrustTextAsRope[s1], TrustTextAsRope[s2], case]]; }; Fetch: PROC [base: PureText, index: TextBound] RETURNS [CHAR] = INLINE { RETURN [base[Basics.BoundsCheck[index, Length[base]]]]; }; Find: PROC [s1, s2: PureText, pos1: TextBound ¬ 0, case: BOOL ¬ TRUE] RETURNS [INTEGER] = INLINE { RETURN [Rope.Find[TrustTextAsRope[s1], TrustTextAsRope[s2], pos1, case]]; }; Length: PROC [base: PureText] RETURNS [TextBound] = INLINE { RETURN [IF base = NIL THEN 0 ELSE base.length]; }; ActionType: TYPE = PROC[CHAR] RETURNS[BOOL]; Map: PROC [s: PureText, start: TextBound ¬ 0, len: TextBound ¬ MaxLen, action: ActionType] RETURNS [quit: BOOL] = INLINE { RETURN [Rope.Map[TrustTextAsRope[s], start, len, action]]; }; Match: PROC [pattern, object: PureText, case: BOOL ¬ TRUE] RETURNS [BOOL] = INLINE { RETURN [Rope.Match[TrustTextAsRope[pattern], TrustTextAsRope[object], case]]; }; SkipTo: PROC [s: PureText, pos: TextBound ¬ 0, skip: PureText] RETURNS [TextBound] = INLINE { RETURN [Rope.SkipTo[TrustTextAsRope[s], pos, TrustTextAsRope[skip]]]; }; SkipOver: PROC [s: PureText, pos: TextBound ¬ 0, skip: PureText] RETURNS [TextBound] = INLINE { RETURN [Rope.SkipOver[TrustTextAsRope[s], pos, TrustTextAsRope[skip]]]; }; Literal: PROC [text: REF TEXT] RETURNS [REF TEXT] ~ INLINE { RETURN[text] }; TrustTextAsRope: PROC [text: PureText] RETURNS [Rope.Text] = TRUSTED INLINE { RETURN [LOOPHOLE[text]]; }; TrustTextRopeAsText: PROC [rope: Rope.Text] RETURNS [PureText] = TRUSTED INLINE { RETURN [LOOPHOLE[rope]]; }; BasePointer: TYPE ~ POINTER TO Basics.RawBytes; BaseFromTextPointer: PROC [p: POINTER--TO TEXT--] RETURNS [BasePointer] = TRUSTED INLINE { RETURN[LOOPHOLE[p, BasePointer]+SIZE[TEXT[0]]] }; BaseFromText: PROC [text: PureText] RETURNS [BasePointer] = TRUSTED INLINE { RETURN[BaseFromTextPointer[LOOPHOLE[text]]] }; BaseFromTextRope: PROC [rope: Rope.Text] RETURNS [BasePointer] = TRUSTED INLINE { RETURN[BaseFromTextPointer[LOOPHOLE[rope]]] }; END. r RefText.mesa Copyright Σ 1985, 1986, 1991, 1992 by Xerox Corporation. All rights reserved. MBrown on September 16, 1983 2:52 pm Paul Rovner on August 8, 1983 11:48 am Russ Atkinson on January 29, 1985 6:57:40 pm PST Carl Hauser, March 3, 1988 5:03:31 pm PST Doug Wyatt, November 22, 1991 4:17 pm PST Michael Plass, February 21, 1992 4:35 pm PST Introduction This interface includes some simple procedures for REF TEXT. As much as possible the operations are parallel to the Rope interface's operations on ROPE. When reading from a REF TEXT, the package treats NIL and "" equivalently (Length[NIL] = 0, Fetch[NIL, 0] raises BoundsFault). But Appending to a NIL REF TEXT raises PointerFault. If you are doing large numbers of REF TEXT allocations, maybe you should be using ROPE. In a "piece" defined by [s: REF TEXT, start: TextBound, len: TextBound], len is interpreted as follows: IF start > s.length THEN BoundsFault ELSE len _ MIN[len, s.length-start]. The resulting len value is called the "effective len" below. A boolean "case" parameter should be understood to mean "case significant". If case, upper case characters are treated as distinct from lower case characters. If ~case, upper case characters are converted to lower case before comparison. Creating TEXT Allocates a TEXT with length = 0, maxLength >= nChars, and returns a REF to it. Some programs allocate and discard TEXT frequently. To improve the performance of these programs, the RefText package manages a small pool of "scratch" TEXTs. The expected usage of this pool is for a client to get a scratch TEXT using the ObtainScratch procedure, manipulate this TEXT for awhile, and then return it to the pool using ReleaseScratch. A client who retains a REF to a scratch TEXT after releasing it is not playing by the rules (the same TEXT will surely be handed out to someone else), but this is still "safe" in the Cedar sense (the storage invariants are not compromised by the error). This error may be raised by ObtainScratch, below. ! Error [clientModifiedReleasedText] Returns a REF to a TEXT from with length = 0, maxLength >= nChars. This TEXT is generally obtained from a pool of TEXTs. ObtainScratch should only be called with the expectation of calling ReleaseScratch (below) later on the resulting TEXT. (It is ok for a client to occasionally "forget" to release a TEXT obtained with this procedure, so for instance there is no need to call ReleaseScratch in UNWIND catch phrases unless UNWIND is expected most of the time!) Raises Error [clientModifiedReleasedText] if the TEXT it wanted to return has been tampered with (in a detectable way) since it was released with ReleaseScratch; this indicates that some client is not playing by the rules. With the availability of a sufficiently good garbage collector, the implementation of ObtainScratch might be the same as New. A couple common arguments for ObtainScratch. Caller asserts that it has no further interest in the TEXT pointed to by t. Noop if t was not obtained from scratch pool. Writing TEXT ! PointerFault (if to = NIL) ! BoundsFault (if to.length > to.maxLength or length of result text would exceed MaxLen) ! PointerFault (if to = NIL) ! BoundsFault (if to.length > to.maxLength or length of result text would exceed MaxLen) ! PointerFault (if to = NIL) ! BoundsFault (if to.length > to.maxLength or length of result text would exceed MaxLen) ! PointerFault (if to = NIL) ! BoundsFault (if to.length > to.maxLength or length of result text would exceed MaxLen) ! PointerFault (if to = NIL) ! BoundsFault (if to.length > to.maxLength or length of result text would exceed MaxLen) ! PointerFault (if to = NIL) ! BoundsFault (if length of result text would exceed MaxLen) The client wishes to append nChars characters to the REF TEXT to without overflowing it. The result text t satisfies (Compare[to, t] = $equal AND to.maxLength >= to.length + nChars). ! PointerFault (if to = NIL) ! BoundsFault (if length of result text would exceed MaxLen) Reading TEXT returns lexicographic comparison of the REF TEXT contents case => case of characters is significant returns s1 = s2 (true iff s1 and s2 contain same sequence of characters, modulo the case parameter) ! BoundsFault (if base = NIL or index >= base.length) fetches indexed character from given REF TEXT. returns position in s1 where s2 occurs (starts looking at pos1) does NOT do *-matching (use Match below for this) returns -1 if not found (including pos1 >= Length[s1]) returns the length of the REF TEXT (0 if NIL). Type of procedure applied to each character by Map. ! BoundsFault (if start > s.length) Applies the action to each char in the given piece of s, in ascending order, until action[char] = TRUE or no more chars. Returns TRUE iff stopped by action[char] = TRUE. 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. Examine s[pos .. s.length), and return the lowest index in this range such that s[i] is contained in the "skip" string. If no such character exists, return s.length. Examine s[pos .. s.length), and return the lowest index in this range such that s[i] is NOT contained in the skip string. If no such character exists, return s.length. Miscellaneous The identity function, useful to convey the desired type of a literal to the compiler in certain contexts, e.g. when the target type is a REF ANY. It is sometimes OK to treat a REF TEXT as a ROPE, provided that 1. You do NOT alter the text while there is a ROPE reference to the object 2. You do not care about the runtime type of the object It is sometimes OK to treat a Rope.Text as a REF TEXT. You must promise not to modify it! Κa–(cedarcode) style•NewlineDelimiter ™codešœ ™ K™NKšœ$™$Kšœ&™&Kšœ0™0K™)K™)K™,—K˜šΟk ˜ Kšœœ%˜1KšœœBœ˜c—K˜KšΟnœœ ˜Kšœ ˜Kšœ˜head™ K™™K˜K™³K˜K™WK˜™gK™I—Kšœ<™Kšœ œœœ˜D—Kšœ˜ Kšœ˜K™——™ š žœœœœœœ˜ZK™9Kšœ)™)Kšœ@˜FKšœ˜K˜—šžœœœœœœœ˜KKšœc™cKšœ>˜DKšœ˜K˜—š žœœ$œœœ˜HK™5K™.Kšœ1˜7Kšœ˜K˜—šžœœ/œœœœœ˜bKšœ?™?Kšœ1™1Kšœ6™6KšœC˜IKšœ˜K˜—šžœœœœ˜Kš œœœœœ ˜AK˜——K˜Kšœ˜—…—,λ