/[pcre]/code/trunk/pcregrep.c
ViewVC logotype

Diff of /code/trunk/pcregrep.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1502 by ph10, Mon Sep 15 13:56:18 2014 UTC revision 1543 by ph10, Tue Apr 7 15:52:11 2015 UTC
# Line 1582  while (ptr < endptr) Line 1582  while (ptr < endptr)
1582    int endlinelength;    int endlinelength;
1583    int mrc = 0;    int mrc = 0;
1584    int startoffset = 0;    int startoffset = 0;
1585      int prevoffsets[2];
1586    unsigned int options = 0;    unsigned int options = 0;
1587    BOOL match;    BOOL match;
1588    char *matchptr = ptr;    char *matchptr = ptr;
1589    char *t = ptr;    char *t = ptr;
1590    size_t length, linelength;    size_t length, linelength;
1591    
1592      prevoffsets[0] = prevoffsets[1] = -1;
1593    
1594    /* At this point, ptr is at the start of a line. We need to find the length    /* At this point, ptr is at the start of a line. We need to find the length
1595    of the subject string to pass to pcre_exec(). In multiline mode, it is the    of the subject string to pass to pcre_exec(). In multiline mode, it is the
# Line 1729  while (ptr < endptr) Line 1732  while (ptr < endptr)
1732        {        {
1733        if (!invert)        if (!invert)
1734          {          {
1735          if (printname != NULL) fprintf(stdout, "%s:", printname);          int oldstartoffset = startoffset;
1736          if (number) fprintf(stdout, "%d:", linenumber);  
1737            /* It is possible, when a lookbehind assertion contains \K, for the
1738          /* Handle --line-offsets */          same string to be found again. The code below advances startoffset, but
1739            until it is past the "bumpalong" offset that gave the match, the same
1740          if (line_offsets)          substring will be returned. The PCRE1 library does not return the
1741            fprintf(stdout, "%d,%d\n", (int)(matchptr + offsets[0] - ptr),          bumpalong offset, so all we can do is ignore repeated strings. (PCRE2
1742              offsets[1] - offsets[0]);          does this better.) */
1743    
1744          /* Handle --file-offsets */          if (prevoffsets[0] != offsets[0] || prevoffsets[1] != offsets[1])
   
         else if (file_offsets)  
           fprintf(stdout, "%d,%d\n",  
             (int)(filepos + matchptr + offsets[0] - ptr),  
             offsets[1] - offsets[0]);  
   
         /* Handle --only-matching, which may occur many times */  
   
         else  
1745            {            {
1746            BOOL printed = FALSE;            prevoffsets[0] = offsets[0];
1747            omstr *om;            prevoffsets[1] = offsets[1];
1748    
1749            for (om = only_matching; om != NULL; om = om->next)            if (printname != NULL) fprintf(stdout, "%s:", printname);
1750              if (number) fprintf(stdout, "%d:", linenumber);
1751    
1752              /* Handle --line-offsets */
1753    
1754              if (line_offsets)
1755                fprintf(stdout, "%d,%d\n", (int)(matchptr + offsets[0] - ptr),
1756                  offsets[1] - offsets[0]);
1757    
1758              /* Handle --file-offsets */
1759    
1760              else if (file_offsets)
1761                fprintf(stdout, "%d,%d\n",
1762                  (int)(filepos + matchptr + offsets[0] - ptr),
1763                  offsets[1] - offsets[0]);
1764    
1765              /* Handle --only-matching, which may occur many times */
1766    
1767              else
1768              {              {
1769              int n = om->groupnum;              BOOL printed = FALSE;
1770              if (n < mrc)              omstr *om;
1771    
1772                for (om = only_matching; om != NULL; om = om->next)
1773                {                {
1774                int plen = offsets[2*n + 1] - offsets[2*n];                int n = om->groupnum;
1775                if (plen > 0)                if (n < mrc)
1776                  {                  {
1777                  if (printed) fprintf(stdout, "%s", om_separator);                  int plen = offsets[2*n + 1] - offsets[2*n];
1778                  if (do_colour) fprintf(stdout, "%c[%sm", 0x1b, colour_string);                  if (plen > 0)
1779                  FWRITE(matchptr + offsets[n*2], 1, plen, stdout);                    {
1780                  if (do_colour) fprintf(stdout, "%c[00m", 0x1b);                    if (printed) fprintf(stdout, "%s", om_separator);
1781                  printed = TRUE;                    if (do_colour) fprintf(stdout, "%c[%sm", 0x1b, colour_string);
1782                      FWRITE(matchptr + offsets[n*2], 1, plen, stdout);
1783                      if (do_colour) fprintf(stdout, "%c[00m", 0x1b);
1784                      printed = TRUE;
1785                      }
1786                  }                  }
1787                }                }
1788    
1789                if (printed || printname != NULL || number) fprintf(stdout, "\n");
1790              }              }
1791              }
1792    
1793            if (printed || printname != NULL || number) fprintf(stdout, "\n");          /* Prepare to repeat to find the next match. If the patterned contained
1794            }          a lookbehind tht included \K, it is possible that the end of the match
1795            might be at or before the actual strting offset we have just used. We
1796          /* Prepare to repeat to find the next match */          need to start one character further on. Unfortunately, for unanchored
1797            patterns, the actual start offset can be greater that the one that was
1798            set as a result of "bumpalong". PCRE1 does not return the actual start
1799            offset, so we have to check against the original start offset. This may
1800            lead to duplicates - we we need the fudge above to avoid printing them.
1801            (PCRE2 does this better.) */
1802    
1803          match = FALSE;          match = FALSE;
1804          if (line_buffered) fflush(stdout);          if (line_buffered) fflush(stdout);
1805          rc = 0;                      /* Had some success */          rc = 0;                      /* Had some success */
1806          startoffset = offsets[1];    /* Restart after the match */          startoffset = offsets[1];    /* Restart after the match */
1807            if (startoffset <= oldstartoffset)
1808              {
1809              if ((size_t)startoffset >= length)
1810                goto END_ONE_MATCH;              /* We were at the end */
1811              startoffset = oldstartoffset + 1;
1812              if (utf8)
1813                while ((matchptr[startoffset] & 0xc0) == 0x80) startoffset++;
1814              }
1815          goto ONLY_MATCHING_RESTART;          goto ONLY_MATCHING_RESTART;
1816          }          }
1817        }        }
# Line 1974  while (ptr < endptr) Line 2008  while (ptr < endptr)
2008    /* Advance to after the newline and increment the line number. The file    /* Advance to after the newline and increment the line number. The file
2009    offset to the current line is maintained in filepos. */    offset to the current line is maintained in filepos. */
2010    
2011      END_ONE_MATCH:
2012    ptr += linelength + endlinelength;    ptr += linelength + endlinelength;
2013    filepos += (int)(linelength + endlinelength);    filepos += (int)(linelength + endlinelength);
2014    linenumber++;    linenumber++;

Legend:
Removed from v.1502  
changed lines
  Added in v.1543

  ViewVC Help
Powered by ViewVC 1.1.5