Your task this time is to implement a variant of the POSIX
expand(1) utility which expands tabs to spaces.
Your program is to take a tabstop specification and then read input on standard in and replace tab characters in the input with the appropriate amount of spaces to reach the next tabstop. The result should be written to standard out.
A tabstop specification consists of either a single number, or a comma-separated list of tabstops. In the case of a single number, it is repeated as if multiples of it occurred in a comma-separated list (i.e.
4 acts as
4,8,12,16,20,...). Each entry in a comma-separated list is a positive integer optionally prefixed by a
+ prefix indicates a relative difference to the previous value in the comma-separated list. The first value in the list must be absolute (i.e. unprefixed). The tabstops specify the column of the next non-space character (following the expanded tab), with the leftmost column taken as number 0. Tabs should always expand to at least one space.
The tabstop specification is either to be taken as the first command-line parameter to the program, or read from standard in as the first line of input (terminated by a newline), at your discretion. After the tabstop has been read, the remaining input (all input, in the former case) until EOF is to be processed and expanded. The expanded output shall be written to standard out.
All expanded tabstops, and all input, is assumed to be a maximum of 80 columns wide. All expanded tabstops are strictly increasing.
4,6,+2,+8 is equivalent to
4,6,8,16, and with both the input
is expanded into (
␣ indicates a space)
01234567890123456 (Ruler for the above, not part of the output) 1111111
Scoring is pure code-golf; shortest code wins.