[comp.lang.pascal] pstrings - corrections

ian@mucs.UX.CS.MAN.AC.UK (Ian Cottam) (03/20/88)

My Unbounded-length Strings in ISO level 1 Pascal has just appeared on
the net.  Unfortunately a couple of bugs crept in!  If you have already
taken a copy and the routines below look different then please use
these instead.
ian@ux.cs.man.ac.uk
__________________________

# include "strings.h"

procedure readS{(var f: Text; var s: String)};
{
* Reads a string from text file f; eoln terminating.  The input is
* left pointing to the beginning of the next line, if any.
*
* precondition:
*    f open for reading & not eof(f)
}
	const BufferLength = 120;     
	var t : String;
	    i : Nat0; 
	 line : packed array [1..BufferLength] of Char;
	    
begin
	i := 0;
	while not eoln(f) and (i <> BufferLength) do begin
		i := i+1;
		read(f, line[i])
	end;
	if i = 0 then assignS(s, nil) else assignS(s, mk(line, i));
	{ --  Check for more characters on the input line }
	if not eoln(f) then begin
		{ --  Get the rest }
		t := nil;
		readS(f, t);
		assignS(s, concatS(s, t))
	end
	else get(f)
end{ -- readS};

______________________________________
# include "strings.h"

procedure readtS{(var f: Text; var s: String; function stop(c:Char):Boolean)};
{
* Reads a string from text file f; eoln or stop(c) returning true
* (whichever occurs first) terminating.  In either case,
* input is left positioned at the terminator.
*
* precondition:
*    f open for reading & not eof(f)
}
	const BufferLength = 120;
	var t : String;
	    i : Nat0; 
	 line : packed array [1..BufferLength] of Char;
begin
	i := 0;
	while not eoln(f) and (i <> BufferLength) and not stop(f^) do begin
		i := i+1;
		read(f, line[i])
	end;
	if i = 0 then assignS(s, nil) else assignS(s, mk(line, i));
	{ --  Check for more characters on the input line }
	if not stop(f^) and not eoln(f) then begin
		{ --  Get the rest }
		t := nil;
		readtS(f, t, stop);
		assignS(s, concatS(s, t))
	end
end{ -- readtS};

_______________________________________
# include "strings.h"

procedure initvalparamS{(var s: String)};
{
* Initialises s, which should be a value parameter, to be
* safely useable within the current procedure.
*
* increase ref count for a by-value param
}
begin
	if s <> nil then s^.REFS := s^.REFS + 1
end{ -- initvalparamS};