-- UnsafeSTPRubiconImpl.Mesa, last edit February 8, 1983 4:25 pm


	DIRECTORY
	LongString: TYPE USING[AppendChar, AppendString],
	UnsafeSTPRubicon: TYPE USING[];

UnsafeSTPRubiconImpl: PROGRAM 
IMPORTS LongString 
EXPORTS UnsafeSTPRubicon = {

AppendCharAndGrow: PUBLIC PROC [
    to: LONG POINTER TO LONG STRING, c: CHARACTER, z: UNCOUNTED ZONE] = {
    IF to↑ = NIL OR to↑.length >= to↑.maxlength THEN {
    	old: LONG STRING ← to↑;
    	len: CARDINAL ← IF to↑ = NIL THEN 0 ELSE to.length;
    	to↑ ← z.NEW[StringBody[len + 8] ← [length: 0, maxlength: len + 8, text: ]];
    	IF old ~= NIL THEN {
    		LongString.AppendString[to↑, old];
    		z.FREE[@old];
    		};
    	};
    LongString.AppendChar[to↑, c];
    };

AppendStringAndGrow: PUBLIC PROC [to: LONG POINTER TO LONG STRING,
    from: LONG STRING, z: UNCOUNTED ZONE] = {
    IF to↑ = NIL OR to↑.length + from.length >= to↑.maxlength THEN {
    	old: LONG STRING ← to↑;
    	len: CARDINAL ← IF to↑ = NIL THEN 0 ELSE to.length;
    	to↑ ← z.NEW[StringBody[len + from.length]
    		← [length: 0, maxlength: len + from.length, text: ]];
    	IF old ~= NIL THEN {
    		LongString.AppendString[to↑, old];
    		z.FREE[@old];
    		};
    	};
    LongString.AppendString[to↑, from];
    };

CopyToNewString: PUBLIC PROC [s: LONG STRING, z: UNCOUNTED ZONE]
    RETURNS [newS: LONG STRING] = {
    newS ← z.NEW[StringBody[s.length] ← [length: 0, maxlength: s.length, text: ]];
    LongString.AppendString[newS, s];
    };

Replace: PUBLIC PROC [to: LONG POINTER TO LONG STRING, from: LONG STRING,
    z: UNCOUNTED ZONE] = {
    IF to = NIL THEN RETURN;
    IF to↑ ~= NIL THEN z.FREE[to];
    IF from # NIL THEN to↑ ← CopyToNewString[from, z];
    };

}.