I think it is a general utility function to process the input from the user, and worth to be somewhere, perhaps is module String, instead of everybody having to write his own version.
Here is my version:
Code: Select all
PROCEDURE Trim* (VAR s: ARRAY OF CHAR);
(* Leading and trailing spaces are removed *)
VAR indexLastChar, beg, end, i, j: INTEGER;
BEGIN
indexLastChar := LEN(s$) - 1;
beg := 0; WHILE s[beg] = " " DO INC(beg) END;
IF s[beg] = 0X THEN (* s is all spaces *)
s[0] := 0X
ELSE
end := indexLastChar; WHILE s[end] = " " DO DEC(end) END;
IF beg > 0 THEN
i := 0; j := beg;
WHILE j <= end DO s[i] := s[j]; INC(i); INC(j) END;
s[i] := 0X
ELSIF end < indexLastChar THEN
s[end + 1] := 0X
END
END
END Trim;