- SS.len = 0; /* Clean substitute space. */
- slen = psl;
- n = cp->u.s->n;
- switch (n) {
- case 0: /* Global */
- do {
- /* Locate start of replaced string. */
- re_off = match[0].rm_so;
- /* Copy leading retained string. */
- cspace(&SS, s, re_off, APPEND);
- /* Add in regular expression. */
- regsub(&SS, s, cp->u.s->new);
- /* Move past this match. */
- s += match[0].rm_eo;
- slen -= match[0].rm_eo;
- } while(match[0].rm_so != match[0].rm_eo &&
- regexec_e(re, s, REG_NOTBOL, 0, slen));
+ SS.len = 0; /* Clean substitute space. */
+ slen = psl;
+ n = cp->u.s->n;
+ lastempty = 1;
+
+ switch (n) {
+ case 0: /* Global */
+ do {
+ if (lastempty || match[0].rm_so != match[0].rm_eo) {
+ /* Locate start of replaced string. */
+ re_off = match[0].rm_so;
+ /* Copy leading retained string. */
+ cspace(&SS, s, re_off, APPEND);
+ /* Add in regular expression. */
+ regsub(&SS, s, cp->u.s->new);
+ }