ArpaMTMiscOpsImpl.mesa
Last Edited by: Nichols, July 13, 1983 5:13 pm
Last Edited by: Taft, February 5, 1984 1:41:45 pm PST
Last Edited by: HGM, March 4, 1984 0:02:34 am PST
John Larson, October 21, 1987 10:19:13 pm PDT
DIRECTORY
ArpaMTMiscOps,
Ascii USING [Digit],
Rope USING [Cat, Equal, Fetch, FromChar, IsEmpty, Length, ROPE, Translate, TranslatorType];
ArpaMTMiscOpsImpl:
CEDAR
PROGRAM
IMPORTS Ascii, Rope
EXPORTS ArpaMTMiscOps =
BEGIN OPEN ArpaMTMiscOps;
Lookup:
PUBLIC
PROC [name: Rope.
ROPE, table: Table]
RETURNS [
BOOL] = {
Returns TRUE if name is in table.
IF Rope.IsEmpty[name] THEN RETURN [FALSE];
WHILE table #
NIL
DO
IF name.Equal[table.first, FALSE] THEN RETURN [TRUE];
table ← table.rest;
ENDLOOP;
RETURN [FALSE];
};
AToI:
PUBLIC
PROC [r: Rope.
ROPE]
RETURNS [i:
INT] ~ {
Convert the leading digits of r into an integer.
j: INT;
i ← 0;
FOR j ← 0, j+1
WHILE j < r.Length
AND Ascii.Digit[r.Fetch[j]]
DO
i ← i*10 + (r.Fetch[j]-'0);
ENDLOOP;
};
IToA:
PUBLIC
PROC [i:
INT]
RETURNS [r: Rope.
ROPE] ~ {
Convert i into a rope.
minus: BOOL ~ i < 0;
r ← NIL;
WHILE i > 0
DO
r ← Rope.Cat[Rope.FromChar[i MOD 10 + '0], r];
i ← i / 10;
ENDLOOP;
IF r.IsEmpty
THEN
r ← "0";
IF minus
THEN
r ← Rope.Cat["-", r];
};
Substitute:
PUBLIC
PROC [r: Rope.
ROPE, oldChar, newChar:
CHAR]
RETURNS [newR: Rope.
ROPE] ~ {
Return r with newChar substituted for every instance of oldChar.
Translator: Rope.TranslatorType ~ {
IF old = oldChar THEN RETURN [newChar]
ELSE RETURN [old];
};
RETURN [r.Translate[translator: Translator]];
};
END.