ALGOL 68/prelude: Difference between revisions

Added a string in string implementation for use with implementations other than Algol 68.
(Document errata.a68 and graph_2d.a68)
(Added a string in string implementation for use with implementations other than Algol 68.)
Line 43:
Used in: [[Plot coordinate pairs#ALGOL 68|Plot coordinate pairs]].
* c.f. [[ALGOL_68/prelude/graph_2d.a68]]
 
 
=== string in string ===
The Algol 68G compiler/interpreter provides a procedure string in string, similar to the standard char in string.
<br/>
Here is a version for other implementations of Algol 68:
<lang algol68># A string in string procedure for use with compilers other than Algol 68G #
# returns TRUE if s is in t, FALSE otherwise. #
# if pos is not NIL: #
# if s is in t, pos is set to the starting position os s in t, #
# pos is left unchanged is s is not in t #
PROC string in string = ( STRING s, REF INT pos, STRING t )BOOL:
IF s = "" THEN
# s is empty #
IF REF INT(pos) ISNT REF INT(NIL) THEN pos := LWB t FI;
TRUE
ELSE
# s and t are non-empty #
BOOL found := FALSE;
CHAR first char = s[ LWB s ];
INT first pos := LWB t;
INT end pos = UPB t;
INT s length = ( UPB s - LWB s ) + 1;
WHILE NOT found AND first pos <= end pos DO
found := char in string( first char, first pos, t[ first pos : @ first pos ] );
IF NOT found THEN
# the first character is not present #
first pos := end pos + 1
ELIF s = t[ first pos : first pos + ( s length - 1 ) ] THEN
# found the full string s at first pos #
IF REF INT(pos) ISNT REF INT(NIL) THEN pos := first pos FI
ELSE
# haven't got the full string s at first pos #
first pos +:= 1;
found := FALSE
FI
OD;
found
FI # string in string # ;</lang>
 
 
=== The classic [http://web.comlab.ox.ac.uk/people/Jeremy.Gibbons/wg21/ UNESCO IFIP Working Group 2.1]'s standard prelude contents ===
3,043

edits