[net.lang.mod2] String Handling Procedures

ags@pucc-i (Seaman) (08/08/84)

Several people have pointed out that I was incorrect in claiming that
there are "standard" procedures for operating on strings.  I was
thinking of a module called "Strings" which was supplied by Volition
Systems as part of their utility library.  All of Volition's library
modules, standard or not, are contained on the same physical file.

Here is the definition module, as provided by Volition:
--------------------------------------------------------------------
DEFINITION MODULE Strings;

EXPORT QUALIFIED STRING, Assign, Insert, Delete,
		 Pos, Copy, Concat, Length, CompareStr;

TYPE STRING = ARRAY [0..80] OF CHAR;

PROCEDURE Assign(VAR source, dest : ARRAY OF CHAR);

PROCEDURE Insert(substr : ARRAY OF CHAR;
		 VAR str : ARRAY OF CHAR;
		 inx : CARDINAL);

PROCEDURE Delete(VAR str : ARRAY OF CHAR;
		 inx : CARDINAL;
		 len : CARDINAL);

PROCEDURE Copy(str : ARRAY OF CHAR;
	       inx : CARDINAL;
	       len : CARDINAL;
	       VAR result : ARRAY OF CHAR);

PROCEDURE Concat(s1,s2 : ARRAY OF CHAR;
		 VAR result : ARRAY OF CHAR);

PROCEDURE Length(VAR str : ARRAY OF CHAR) : CARDINAL;

PROCEDURE CompareStr(s1,s2 : ARRAY OF CHAR) : INTEGER;

END Strings.
--------------------------------------------------------

A few comments are in order.  Modula-2 allows assignment of string
constants that are shorter than the declared length of a character array,
as in

  VAR  Greeting : ARRAY [0..30] OF CHAR;
  . . .
  Greeting := "Hello";

The function "Length" returns the actual length of a string,
which may be less than the declared length.  If you want to find out
the declared length, of course, you use the standard function HIGH.

If you have two string variables with different declared lengths, such
as

  VAR Short : ARRAY [0..10] OF CHAR;
      Long  : ARRAY [0..100] OF CHAR;

then you cannot say

      Long := Short;

because the types do not agree.  However, you can say

      Assign(Short,Long);

which has the desired effect.

"CompareStr" returns -1, 0, or +1 to denote that s1 is lexically less than,
equal to, or greater than s2, respectively.

The other declarations should be self-explanatory.  I predict that this
module, or one similar to it, will eventually become part of the standard.
-- 

Dave Seaman			My hovercraft is no longer full of 
..!pur-ee!pucc-i:ags		eels (thanks to my confused cat).