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

Diff of /code/trunk/pcredemo.c

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

revision 477 by ph10, Sun Jan 3 15:53:09 2010 UTC revision 566 by ph10, Wed Nov 3 18:32:55 2010 UTC
# Line 7  of calling the PCRE regular expression l Line 7  of calling the PCRE regular expression l
7  pcresample documentation for a short discussion ("man pcresample" if you have  pcresample documentation for a short discussion ("man pcresample" if you have
8  the PCRE man pages installed).  the PCRE man pages installed).
9    
10  In Unix-like environments, if PCRE is installed in your standard system  In Unix-like environments, if PCRE is installed in your standard system
11  libraries, you should be able to compile this program using this command:  libraries, you should be able to compile this program using this command:
12    
13  gcc -Wall pcredemo.c -lpcre -o pcredemo  gcc -Wall pcredemo.c -lpcre -o pcredemo
# Line 50  const char *error; Line 50  const char *error;
50  char *pattern;  char *pattern;
51  char *subject;  char *subject;
52  unsigned char *name_table;  unsigned char *name_table;
53    unsigned int option_bits;
54  int erroffset;  int erroffset;
55  int find_all;  int find_all;
56    int crlf_is_newline;
57  int namecount;  int namecount;
58  int name_entry_size;  int name_entry_size;
59  int ovector[OVECCOUNT];  int ovector[OVECCOUNT];
60  int subject_length;  int subject_length;
61  int rc, i;  int rc, i;
62    int utf8;
63    
64    
65  /**************************************************************************  /**************************************************************************
# Line 238  if (namecount <= 0) printf("No named sub Line 241  if (namecount <= 0) printf("No named sub
241  * subject is not a valid match; other possibilities must be tried. The   *  * subject is not a valid match; other possibilities must be tried. The   *
242  * second flag restricts PCRE to one match attempt at the initial string  *  * second flag restricts PCRE to one match attempt at the initial string  *
243  * position. If this match succeeds, an alternative to the empty string   *  * position. If this match succeeds, an alternative to the empty string   *
244  * match has been found, and we can proceed round the loop.               *  * match has been found, and we can print it and proceed round the loop,  *
245    * advancing by the length of whatever was found. If this match does not  *
246    * succeed, we still stay in the loop, advancing by just one character.   *
247    * In UTF-8 mode, which can be set by (*UTF8) in the pattern, this may be *
248    * more than one byte.                                                    *
249    *                                                                        *
250    * However, there is a complication concerned with newlines. When the     *
251    * newline convention is such that CRLF is a valid newline, we want must  *
252    * advance by two characters rather than one. The newline convention can  *
253    * be set in the regex by (*CR), etc.; if not, we must find the default.  *
254  *************************************************************************/  *************************************************************************/
255    
256  if (!find_all)  if (!find_all)     /* Check for -g */
257    {    {
258    pcre_free(re);   /* Release the memory used for the compiled pattern */    pcre_free(re);   /* Release the memory used for the compiled pattern */
259    return 0;        /* Finish unless -g was given */    return 0;        /* Finish unless -g was given */
260    }    }
261    
262    /* Before running the loop, check for UTF-8 and whether CRLF is a valid newline
263    sequence. First, find the options with which the regex was compiled; extract
264    the UTF-8 state, and mask off all but the newline options. */
265    
266    (void)pcre_fullinfo(re, NULL, PCRE_INFO_OPTIONS, &option_bits);
267    utf8 = option_bits & PCRE_UTF8;
268    option_bits &= PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|PCRE_NEWLINE_CRLF|
269                   PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF;
270    
271    /* If no newline options were set, find the default newline convention from the
272    build configuration. */
273    
274    if (option_bits == 0)
275      {
276      int d;
277      (void)pcre_config(PCRE_CONFIG_NEWLINE, &d);
278      /* Note that these values are always the ASCII ones, even in
279      EBCDIC environments. CR = 13, NL = 10. */
280      option_bits = (d == 13)? PCRE_NEWLINE_CR :
281              (d == 10)? PCRE_NEWLINE_LF :
282              (d == (13<<8 | 10))? PCRE_NEWLINE_CRLF :
283              (d == -2)? PCRE_NEWLINE_ANYCRLF :
284              (d == -1)? PCRE_NEWLINE_ANY : 0;
285      }
286    
287    /* See if CRLF is a valid newline sequence. */
288    
289    crlf_is_newline =
290         option_bits == PCRE_NEWLINE_ANY ||
291         option_bits == PCRE_NEWLINE_CRLF ||
292         option_bits == PCRE_NEWLINE_ANYCRLF;
293    
294  /* Loop for second and subsequent matches */  /* Loop for second and subsequent matches */
295    
296  for (;;)  for (;;)
# Line 280  for (;;) Line 324  for (;;)
324    is zero, it just means we have found all possible matches, so the loop ends.    is zero, it just means we have found all possible matches, so the loop ends.
325    Otherwise, it means we have failed to find a non-empty-string match at a    Otherwise, it means we have failed to find a non-empty-string match at a
326    point where there was a previous empty-string match. In this case, we do what    point where there was a previous empty-string match. In this case, we do what
327    Perl does: advance the matching position by one, and continue. We do this by    Perl does: advance the matching position by one character, and continue. We
328    setting the "end of previous match" offset, because that is picked up at the    do this by setting the "end of previous match" offset, because that is picked
329    top of the loop as the point at which to start again. */    up at the top of the loop as the point at which to start again.
330    
331      There are two complications: (a) When CRLF is a valid newline sequence, and
332      the current position is just before it, advance by an extra byte. (b)
333      Otherwise we must ensure that we skip an entire UTF-8 character if we are in
334      UTF-8 mode. */
335    
336    if (rc == PCRE_ERROR_NOMATCH)    if (rc == PCRE_ERROR_NOMATCH)
337      {      {
338      if (options == 0) break;      if (options == 0) break;                    /* All matches found */
339      ovector[1] = start_offset + 1;      ovector[1] = start_offset + 1;              /* Advance one byte */
340        if (crlf_is_newline &&                      /* If CRLF is newline & */
341            start_offset < subject_length - 1 &&    /* we are at CRLF, */
342            subject[start_offset] == '\r' &&
343            subject[start_offset + 1] == '\n')
344          ovector[1] += 1;                          /* Advance by one more. */
345        else if (utf8)                              /* Otherwise, ensure we */
346          {                                         /* advance a whole UTF-8 */
347          while (ovector[1] < subject_length)       /* character. */
348            {
349            if ((subject[ovector[1]] & 0xc0) != 0x80) break;
350            ovector[1] += 1;
351            }
352          }
353      continue;    /* Go round the loop again */      continue;    /* Go round the loop again */
354      }      }
355    

Legend:
Removed from v.477  
changed lines
  Added in v.566

  ViewVC Help
Powered by ViewVC 1.1.5