Next: libsimediting
Up: libsim
Previous: libsim
libsimtexts
NAME
libsimtexts - text utilities
DESCRIPTION
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).
EXAMPLE
external text procedure Front;
text T; integer i;
inimage;
i:=inint;
T:-Front(Sysin.Image); ! The part scanned by inint,;
AUTHOR
Various people at QZ and FOA, Stockholm.
DETAILED INTERFACE
Front
text procedure Front(T); text T; ! Pos used, unchaged;
Returns a reference to the longest subtext of T before Pos.
Rest
text procedure Rest(T); text T; ! Pos used, unchaged;
Returns a subtext reference of a text starting at Pos.
UpTo
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
Pos=I.
From
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.
TSub
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.
FrontStrip
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.
ScanChar
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.
ScanTo
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.
Skip
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.
Search
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.
FindTrigger
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.
FrontCompare
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?
Change
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
Master,OldT,NewT;
begin text Local;
Local:- master;
while local.More do
change(Local,oldt,newt);
master:- Local
end -- ChangeAll --;
FetChar
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
range).
DepChar
procedure DepChar(T,P,C);
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.
LowC
character procedure LowC(C);
character C; ! value to convert;
Returns lower case equivalent of letter, other characters
unchanged.
UpC
character procedure UpC(C);
character C; ! value to convert ;
Returns lower case character as upper case equivalent, other
characters unchanged.
MakeText
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.
Compress
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 =
C.
Example: t1:-Copy( AxBxCxDx"); t2:-compress(t1,'x');"
returns: t1= ABCDCxDx", t2==t1.Sub(1,4)="ABCD"."
Today
text procedure Today;
Returns todays date in text form: "yyyy-mm-dd"
=DateTime.sub(1,10)
DayTime
text procedure DayTime;
Returns todays date in text form: "hh:mm:ss"
=DateTime.sub(11,8)