libsimtexts - text utilities
This category of operations are operations that could have
been attributes of text. They are writable in Simula, but
convenient to have. In many cases the Pos indicator of the
text passed as parameter is important (this is where the
action is started, and it is updated as part of the result).
external text procedure Front;
text T; integer i;
T:-Front(Sysin.Image); ! The part scanned by inint,;
Various people at QZ and FOA, Stockholm.
text procedure Front(T); text T; ! Pos used, unchaged;
Returns a reference to the longest subtext of T before Pos.
text procedure Rest(T); text T; ! Pos used, unchaged;
Returns a subtext reference of a text starting at Pos.
text procedure UpTo(T,I);
text T; ! pos not used, unchanged;
integer I; ! used as Pos;
Returns a reference to the longest subtext of T before
text procedure From(T,P);
text T; ! pos not used, unchanged;
integer P; ! used as Pos ;
Returns a reference to the longest subtext of T starting at
Pos = P.
text procedure TSub(T,P,L);
text T; ! pos not used;
integer P,L; ! used for Pos, Length of sub-string;
Similar to attribute Sub. In cases where T.Sub(P,L) would
have caused a run time error, TSub returns Notext.
text procedure FrontStrip(T);
text T; ! Pos not used or changed, starts at 1. ;
Returns a reference to the longest sub-text of T starting
with the first non-blank (space or tab) character.
character procedure ScanChar(T);
name T; text T; ! Pos used and incremented ;
Similar to GetChar, but returns Null when there is no more
character in T.
text procedure ScanTo(T,C);
name T; text T; ! Pos used and updated;
character C; ! character to look for;
Starting from T.Pos, looks for the next occurrence of the
character C. T.Pos will be placed after the character found
and the subtext from previous Pos up to but not including
the found character will be returned as result If no C is
found, the rest of the text is returned. Example: for
T="X,YY,ZZZ", ScanTo(T,',') will return the three strings:
"X", "YY", "ZZZ" and then notext.
text procedure Skip(T,C);
name T; text T; ! Pos used and updated ;
character C; ! At return T.GetChar <> C;
Starting from T.Pos it finds the first character different
from C and moves T.Pos to that character. Skip returns
Rest(T). If the character at T.Pos is different from C,
T.Pos will not be changed.
integer procedure Search(Master, LookFor);
text Master; ! Pos used, but not changed;
text LookFor; ! string searched for ;
Locates the string 'LookFor' in 'Master'. Starts at
Master.Pos and returns Master.Pos for 1st character of the
first substring equal to LookFor (=Master.Sub(
Search(Master,LookFor), LookFor.Length)) If no such subtext
exists, then the result is Master.Length+1. Note that
Master.Pos is not changed. In order to find succesive
occurances of LookFor, the calling program must change
Master.Pos by some other mean.
character procedure FindTrigger(Master,Triggers);
name Master; text Master; ! text searched, from pos, and
on (pos updated);
text Triggers; ! set of characters looked for. ;
Starting from current Master.pos, finds first occurrence of
any of the characters in Triggers. The result is the
character found and Master.Pos is set past the returned
character. If no Trigger is found, the returned value is
Null, and Master.Pos is set after the string.
Boolean procedure FrontCompare(String, Beginning);
text String; ! text searched, from pos and on, unchanged;
text Beginning; ! string matching with;
Answeres: starting at current pos, does String begin with a
substring equal to Beginning?
Boolean procedure Change(Master,OldT,NewT);
name Master; text Master; ! Pos valid and updated, may
denote new text;
text OldT,NewT; ! replace 'OldT' with 'NewT';
Change the subtext OldT in Master to NewT. Searches from
Master.pos and on (see Search) for OldT, when found Change
returns True and:
- if NewT.Length=OldT.Length then the string is simply
replaced in Master,
- if NewT.Length<OldT.Length then the string is replaced and
the rest of Master is compacted so Master will denote a
subtext of the original Master text,
- if NewT.Length>OldT.Length then Master will be assigned a
new (longer) string to hold the result.
In each case Master.pos is set to past the replaced string.
if OldT is NOT found, Chnage returns False and, Master.pos
is set to its length+1. Example: changing all occurrences of
OldT to NewT can be done with the following procedure:
procedure ChangeAll(Master,OldT,NewT); name Master; text
begin text Local;
while local.More do
end -- ChangeAll --;
character procedure FetChar(T,P);
text T; ! pos not used or changed;
integer P; ! used as Pos in T.;
Returns the P:th character from T (or NULL if P is out of
text T; ! pos not used or changed, but content changed;
integer P; ! used for Pos in T;
character C; ! value stored in T.;
Deposits the character C in the text T at position P. If P
is out of range, no action will be taken.
character procedure LowC(C);
character C; ! value to convert;
Returns lower case equivalent of letter, other characters
character procedure UpC(C);
character C; ! value to convert ;
Returns lower case character as upper case equivalent, other
text procedure MakeText(C,N);
character C; ! character to fill with;
integer n; ! length of retruned, new text;
Returns a new text of length N filled with character C.
text procedure Compress(T,C);
text T; ! text altered, pos not used;
character C; ! character to remove;
Returns a subtext of T with only characters different than
C. This first part of T is thus altered, but the part of T
after this subtext is unchanged. Compress returns Notext if
T contains no other characters than a number of characters =
Example: t1:-Copy( AxBxCxDx"); t2:-compress(t1,'x');"
returns: t1= ABCDCxDx", t2==t1.Sub(1,4)="ABCD"."
text procedure Today;
Returns todays date in text form: "yyyy-mm-dd"
text procedure DayTime;
Returns todays date in text form: "hh:mm:ss"