Scarcely variabled programming and Pascal

Colin Runciman · ACM SIGPLAN Notices · 1979

this paper Ian Wand suggested text-justification as a problem which might not yield very readily to a scarcely variabled approach. The following final program is annotated only by its internal comments; fuller notes on its development would be too extensive to include here. PROGRAM textjustification; (* Description: formats input, aligning word boundaries with constant line boundaries by space padding and throwing away original line structure. "words" are maximal non-space character sequences. Restrictions: the program may give wrong or unformatted output if input contains the "mark" character or words half "length" characters long or more. *) CONST length = 65; space = ' '; mark = '|'; TYPEcount = 0..length; position = 1..length; line = ARRAY[position] OF char; VAR ln: line; xs: count; (* ln is a buffer for input characters. ln[1]..ln[length-xs], suitably formatted, become an output line (exception: last line). The variable xs has been introduced to improve efficiency; without it a more involved function "excess" must be called more often. *) FUNCTION excess(l: line; p: position; c: char): count; (* excess(l, p, c) returns the difference: length minus the index of the first word end at or below p, assuming c to be the next input character following l[p]. *) BEGIN IF (c = space) AND (l[p] <> space) THEN excess := length-p ELSE IF p > 1 THEN excess := excess(l, p-1, l[p]) ELSE excess := 0 END; FUNCTION gaps(l: line; p: position): count; (* gaps(l, p) returns the number of spaces occuring in l[2]..l[p]. *) BEGIN IF p < 2 THEN gaps := 0 ELSE gaps := ord(l[p]=space) + gaps(l, p-1) END; PROCEDURE roughlyfill(VAR l: line; p: position; x: count); (* roughlyfill(l, p, x) fills l[p]..l[length] with consecutive input values, except that: A. the first x values are instead copied f...

Read the paper · More papers on PaperTik