[BACK]Return to process.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sed

Diff for /src/usr.bin/sed/process.c between version 1.16 and 1.17

version 1.16, 2011/07/24 13:59:15 version 1.17, 2011/07/26 08:47:07
Line 312 
Line 312 
 {  {
         SPACE tspace;          SPACE tspace;
         regex_t *re;          regex_t *re;
         regoff_t slen;          size_t re_off, slen;
         int n, lastempty;          int n, lastempty;
         char *s;          char *s;
   
Line 333 
Line 333 
         n = cp->u.s->n;          n = cp->u.s->n;
         lastempty = 1;          lastempty = 1;
   
         do {          switch (n) {
                 /* Copy the leading retained string. */          case 0:                                 /* Global */
                 if (n <= 1 && match[0].rm_so)                  do {
                         cspace(&SS, s, match[0].rm_so, APPEND);                          if (lastempty || match[0].rm_so != match[0].rm_eo) {
                                   /* Locate start of replaced string. */
                 /* Skip zero-length matches right after other matches. */                                  re_off = match[0].rm_so;
                 if (lastempty || match[0].rm_so ||                                  /* Copy leading retained string. */
                     match[0].rm_so != match[0].rm_eo) {                                  cspace(&SS, s, re_off, APPEND);
                         if (n <= 1) {                                  /* Add in regular expression. */
                                 /* Want this match: append replacement. */  
                                 regsub(&SS, s, cp->u.s->new);                                  regsub(&SS, s, cp->u.s->new);
                                 if (n == 1)                          }
                                         n = -1;  
                           /* Move past this match. */
                           if (match[0].rm_so != match[0].rm_eo) {
                                   s += match[0].rm_eo;
                                   slen -= match[0].rm_eo;
                                   lastempty = 0;
                         } else {                          } else {
                                 /* Want a later match: append original. */                                  if (match[0].rm_so == 0)
                                 if (match[0].rm_eo)                                          cspace(&SS, s, match[0].rm_so + 1,
                                         cspace(&SS, s, match[0].rm_eo, APPEND);                                              APPEND);
                                 n--;                                  else
                                           cspace(&SS, s + match[0].rm_so, 1,
                                               APPEND);
                                   s += match[0].rm_so + 1;
                                   slen -= match[0].rm_so + 1;
                                   lastempty = 1;
                         }                          }
                   } while (slen > 0 && regexec_e(re, s, REG_NOTBOL, 0, slen));
                   /* Copy trailing retained string. */
                   if (slen > 0)
                           cspace(&SS, s, slen, APPEND);
                   break;
           default:                                /* Nth occurrence */
                   while (--n) {
                           s += match[0].rm_eo;
                           slen -= match[0].rm_eo;
                           if (!regexec_e(re, s, REG_NOTBOL, 0, slen))
                                   return (0);
                 }                  }
                   /* FALLTHROUGH */
                 /* Move past this match. */          case 1:                                 /* 1st occurrence */
                   /* Locate start of replaced string. */
                   re_off = match[0].rm_so + (s - ps);
                   /* Copy leading retained string. */
                   cspace(&SS, ps, re_off, APPEND);
                   /* Add in regular expression. */
                   regsub(&SS, s, cp->u.s->new);
                   /* Copy trailing retained string. */
                 s += match[0].rm_eo;                  s += match[0].rm_eo;
                 slen -= match[0].rm_eo;                  slen -= match[0].rm_eo;
   
                 /*  
                  * After a zero-length match, advance one byte,  
                  * and at the end of the line, terminate.  
                  */  
                 if (match[0].rm_so == match[0].rm_eo) {  
                         if (*s == '\n')  
                                 slen = -1;  
                         else  
                                 slen--;  
                         cspace(&SS, s++, 1, APPEND);  
                         lastempty = 1;  
                 } else  
                         lastempty = 0;  
   
         } while (n >= 0 && slen >= 0 && regexec_e(re, s, REG_NOTBOL, 0, slen));  
   
         /* Did not find the requested number of matches. */  
         if (n > 1)  
                 return (0);  
   
         /* Copy the trailing retained string. */  
         if (slen > 0)  
                 cspace(&SS, s, slen, APPEND);                  cspace(&SS, s, slen, APPEND);
                   break;
           }
   
         /*          /*
          * Swap the substitute space and the pattern space, and make sure           * Swap the substitute space and the pattern space, and make sure

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.17