[comp.std.unix] ed s/a*/./g description

eric@mks.com (Eric Gisin) (02/09/91)

Submitted-by: eric@mks.com (Eric Gisin)

Does anyone have an complete description of how s///g and awk's gsub
handle the special case of an empty pattern match?
The System V documents and POSIX.2 don't cover this.

For example, the command s/[a-z]*/./g does the following on System V ed:
	-bug-	becomes .-.-.
The following algorithm produces an extra dot:
	-bug-	becomes	.-..-.

gsub(pattern, replace, src, dst)
	while (1) {
		find next pattern in src
		copy part before match from src to dst
		copy replace to dst
		advance src to end of match
		if (at end of src)	break;
		/* special case for empty match */
		if (match was empty)
			*dst++ = *src++;
	}


Volume-Number: Volume 22, Number 118