ConvertUnsafeImpl.mesa, unsafe conversions to/from STRING
Copyright © 1985 by Xerox Corporation. All rights reserved.
Russ Atkinson, February 19, 1985 1:30:41 pm PST
Paul Rovner, September 19, 1983 9:20 pm
Levin, September 22, 1983 10:42 am
Doug Wyatt, February 22, 1985 1:26:05 pm PST
DIRECTORY
Ascii USING [Lower],
ConvertUnsafe USING [LS, SubString],
PrincOpsUtils USING [LongCopy],
RefText USING [New],
Rope USING [Fetch, Size, ROPE, Text, NewText];
ConvertUnsafeImpl: CEDAR PROGRAM
IMPORTS Ascii, PrincOpsUtils, RefText, Rope
EXPORTS ConvertUnsafe
SHARES Rope
= BEGIN OPEN ConvertUnsafe, Rope;
AppendRefText: PUBLIC UNSAFE PROC [to: LS, from: REF READONLY TEXT] = UNCHECKED {
len: CARDINAL ← to.length;
FOR i: CARDINAL IN [0..from.length) DO
to[len] ← from[i];
len ← len + 1;
ENDLOOP;
to.length ← len;
};
AppendRope: PUBLIC UNSAFE PROC [to: LS, from: ROPE] = UNCHECKED {
len: CARDINAL ← to.length;
FOR i: INT IN [0..Size[from]) DO
to[len] ← Fetch[from,i];
len ← len + 1;
ENDLOOP;
to.length ← len;
};
AppendSubString: PUBLIC UNSAFE PROC [to: LS, from: SubString] = UNCHECKED {
len: CARDINAL ← to.length;
FOR i: INT IN [0..from.length) DO
to[len] ← from.base[from.offset + i];
len ← len + 1;
ENDLOOP;
to.length ← len;
};
AppendSubStringToRefText: PUBLIC PROC [to: REF TEXT, from: SubString] = TRUSTED {
len: CARDINAL ← to.length;
FOR i: INT IN [0..from.length) DO
to[len] ← from.base[from.offset + i];
len ← len + 1;
ENDLOOP;
to.length ← len;
};
ToRefText: PUBLIC PROC [from: LS] RETURNS [REF TEXT] = TRUSTED {
len: CARDINALIF from = NIL THEN 0 ELSE from.length;
nw: CARDINAL ← (len+1)/2;
rt: REF TEXT ← RefText.New[len];
rt.length ← len;
IF nw > 0 THEN
PrincOpsUtils.LongCopy[from: from+2, nwords: nw, to: LOOPHOLE[rt, LONG POINTER]+2];
RETURN [rt]
};
EqualSubStrings: PUBLIC PROC [s1, s2: SubString, case: BOOLTRUE]
RETURNS [ans: BOOLTRUE] = TRUSTED {
i1, i2, n: CARDINAL;
b1, b2: LS;
IF s1.length # s2.length THEN RETURN[FALSE];
b1 ← s1.base;
i1 ← s1.offset;
b2 ← s2.base;
i2 ← s2.offset;
FOR n ← s1.length, n - 1 WHILE n > 0 DO
IF case THEN {IF b1[i1] # b2[i2] THEN RETURN[FALSE]}
ELSE IF Ascii.Lower[b1[i1]] # Ascii.Lower[b2[i2]] THEN RETURN[FALSE];
i1 ← i1 + 1;
i2 ← i2 + 1;
ENDLOOP;
};
SubStringToRope: PUBLIC PROC [from: SubString] RETURNS [rt: Text] = TRUSTED {
len: CARDINAL = IF from.base = NIL THEN 0 ELSE from.length;
rt ← NewText[len];
rt.length ← len;
FOR i: CARDINAL IN [0..len) DO rt[i] ← from.base[from.offset + i]; ENDLOOP;
};
ToRope: PUBLIC PROC [from: LS] RETURNS [Text] = TRUSTED {
len: CARDINALIF from = NIL THEN 0 ELSE from.length;
nw: CARDINAL ← (len+1)/2;
rt: Text ← NewText[len];
IF nw > 0 THEN
PrincOpsUtils.LongCopy[from: from+2, nwords: nw, to: LOOPHOLE[rt, LONG POINTER]+2];
RETURN [rt]
};
END.