ConvertUnsafeImpl.mesa, unsafe conversions to/from STRING
Copyright Ó 1985, 1986, 1987, 1988, 1991 by Xerox Corporation. All rights reserved.
For Portable Cedar
Paul Rovner, September 19, 1983 9:20 pm
Levin, September 22, 1983 10:42 am
Russ Atkinson, February 19, 1985 1:30:41 pm PST
Carl Hauser, January 17, 1988 2:32:53 pm PST
Doug Wyatt, August 24, 1991 10:24 pm PDT
Michael Plass, September 21, 1991 4:37 pm PDT
DIRECTORY
Ascii USING [Lower],
ConvertUnsafe USING [LS, SubString],
Basics USING [CopyBytes],
RefText USING [BaseFromText, BaseFromTextRope, BaseFromTextPointer, New],
Rope USING [Fetch, Size, ROPE, Text, NewText];
ConvertUnsafeImpl: CEDAR PROGRAM
IMPORTS Ascii, Basics, 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: CARDINAL ~ IF from = NIL THEN 0 ELSE from.length;
rt: REF TEXT ~ RefText.New[len];
Basics.CopyBytes[
dstBase: RefText.BaseFromText[rt], dstStart: 0,
srcBase: RefText.BaseFromTextPointer[from], srcStart: 0,
count: (rt.length ¬ len)];
RETURN [rt]
};
EqualSubStrings: PUBLIC PROC [s1, s2: SubString, case: BOOL ¬ TRUE]
RETURNS [ans: BOOL ¬ TRUE] = 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
c1: CHAR ~ b1[i1];
c2: CHAR ~ b2[i2];
IF c1#c2 THEN {
IF case OR Ascii.Lower[c1]#Ascii.Lower[c2] 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: CARDINAL ~ IF from = NIL THEN 0 ELSE from.length;
rt: Text ~ NewText[len];
Basics.CopyBytes[
dstBase: RefText.BaseFromTextRope[rt], dstStart: 0,
srcBase: RefText.BaseFromTextPointer[from], srcStart: 0,
count: len];
RETURN [rt]
};
END.
Carl Hauser, January 17, 1988 2:30:01 pm PST
removed PrincOpsUtils references and 2 chars/word assumption.
changes to: DIRECTORY, IMPORTS, ToRefText, ToRope