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

Diff of /code/trunk/pcre_dfa_exec.c

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

revision 383 by ph10, Sun Mar 8 15:26:59 2009 UTC revision 427 by ph10, Fri Aug 28 09:55:54 2009 UTC
# Line 3  Line 3 
3  *************************************************/  *************************************************/
4    
5  /* PCRE is a library of functions to support regular expressions whose syntax  /* PCRE is a library of functions to support regular expressions whose syntax
6  and semantics are as close as possible to those of the Perl 5 language (but see  and semantics are as close as possible to those of the Perl 5 language (but see
7  below for why this module is different).  below for why this module is different).
8    
9                         Written by Philip Hazel                         Written by Philip Hazel
# Line 61  applications. */ Line 61  applications. */
61  #define SP "                   "  #define SP "                   "
62    
63    
   
64  /*************************************************  /*************************************************
65  *      Code parameters and static tables         *  *      Code parameters and static tables         *
66  *************************************************/  *************************************************/
# Line 512  for (;;) Line 511  for (;;)
511      stateblock *current_state = active_states + i;      stateblock *current_state = active_states + i;
512      const uschar *code;      const uschar *code;
513      int state_offset = current_state->offset;      int state_offset = current_state->offset;
514      int count, codevalue;      int count, codevalue, rrc;
515    
516  #ifdef DEBUG  #ifdef DEBUG
517      printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset);      printf ("%.*sProcessing state %d c=", rlevel*2-2, SP, state_offset);
# Line 2158  for (;;) Line 2157  for (;;)
2157    
2158  /* ========================================================================== */  /* ========================================================================== */
2159        /* These are the opcodes for fancy brackets of various kinds. We have        /* These are the opcodes for fancy brackets of various kinds. We have
2160        to use recursion in order to handle them. The "always failing" assersion        to use recursion in order to handle them. The "always failing" assertion
2161        (?!) is optimised when compiling to OP_FAIL, so we have to support that,        (?!) is optimised to OP_FAIL when compiling, so we have to support that,
2162        though the other "backtracking verbs" are not supported. */        though the other "backtracking verbs" are not supported. */
2163    
2164        case OP_FAIL:        case OP_FAIL:
# Line 2201  for (;;) Line 2200  for (;;)
2200          {          {
2201          int local_offsets[1000];          int local_offsets[1000];
2202          int local_workspace[1000];          int local_workspace[1000];
2203          int condcode = code[LINK_SIZE+1];          int codelink = GET(code, 1);
2204            int condcode;
2205    
2206            /* Because of the way auto-callout works during compile, a callout item
2207            is inserted between OP_COND and an assertion condition. This does not
2208            happen for the other conditions. */
2209    
2210            if (code[LINK_SIZE+1] == OP_CALLOUT)
2211              {
2212              rrc = 0;
2213              if (pcre_callout != NULL)
2214                {
2215                pcre_callout_block cb;
2216                cb.version          = 1;   /* Version 1 of the callout block */
2217                cb.callout_number   = code[LINK_SIZE+2];
2218                cb.offset_vector    = offsets;
2219                cb.subject          = (PCRE_SPTR)start_subject;
2220                cb.subject_length   = end_subject - start_subject;
2221                cb.start_match      = current_subject - start_subject;
2222                cb.current_position = ptr - start_subject;
2223                cb.pattern_position = GET(code, LINK_SIZE + 3);
2224                cb.next_item_length = GET(code, 3 + 2*LINK_SIZE);
2225                cb.capture_top      = 1;
2226                cb.capture_last     = -1;
2227                cb.callout_data     = md->callout_data;
2228                if ((rrc = (*pcre_callout)(&cb)) < 0) return rrc;   /* Abandon */
2229                }
2230              if (rrc > 0) break;                      /* Fail this thread */
2231              code += _pcre_OP_lengths[OP_CALLOUT];    /* Skip callout data */
2232              }
2233    
2234            condcode = code[LINK_SIZE+1];
2235    
2236          /* Back reference conditions are not supported */          /* Back reference conditions are not supported */
2237    
# Line 2210  for (;;) Line 2240  for (;;)
2240          /* The DEFINE condition is always false */          /* The DEFINE condition is always false */
2241    
2242          if (condcode == OP_DEF)          if (condcode == OP_DEF)
2243            {            { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
           ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0);  
           }  
2244    
2245          /* The only supported version of OP_RREF is for the value RREF_ANY,          /* The only supported version of OP_RREF is for the value RREF_ANY,
2246          which means "test if in any recursion". We can't test for specifically          which means "test if in any recursion". We can't test for specifically
# Line 2222  for (;;) Line 2250  for (;;)
2250            {            {
2251            int value = GET2(code, LINK_SIZE+2);            int value = GET2(code, LINK_SIZE+2);
2252            if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;            if (value != RREF_ANY) return PCRE_ERROR_DFA_UCOND;
2253            if (recursing > 0) { ADD_ACTIVE(state_offset + LINK_SIZE + 4, 0); }            if (recursing > 0)
2254              else { ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0); }              { ADD_ACTIVE(state_offset + LINK_SIZE + 4, 0); }
2255              else { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2256            }            }
2257    
2258          /* Otherwise, the condition is an assertion */          /* Otherwise, the condition is an assertion */
# Line 2253  for (;;) Line 2282  for (;;)
2282                  (condcode == OP_ASSERT || condcode == OP_ASSERTBACK))                  (condcode == OP_ASSERT || condcode == OP_ASSERTBACK))
2283              { ADD_ACTIVE(endasscode + LINK_SIZE + 1 - start_code, 0); }              { ADD_ACTIVE(endasscode + LINK_SIZE + 1 - start_code, 0); }
2284            else            else
2285              { ADD_ACTIVE(state_offset + GET(code, 1) + LINK_SIZE + 1, 0); }              { ADD_ACTIVE(state_offset + codelink + LINK_SIZE + 1, 0); }
2286            }            }
2287          }          }
2288        break;        break;
# Line 2405  for (;;) Line 2434  for (;;)
2434        /* Handle callouts */        /* Handle callouts */
2435    
2436        case OP_CALLOUT:        case OP_CALLOUT:
2437          rrc = 0;
2438        if (pcre_callout != NULL)        if (pcre_callout != NULL)
2439          {          {
         int rrc;  
2440          pcre_callout_block cb;          pcre_callout_block cb;
2441          cb.version          = 1;   /* Version 1 of the callout block */          cb.version          = 1;   /* Version 1 of the callout block */
2442          cb.callout_number   = code[1];          cb.callout_number   = code[1];
# Line 2422  for (;;) Line 2451  for (;;)
2451          cb.capture_last     = -1;          cb.capture_last     = -1;
2452          cb.callout_data     = md->callout_data;          cb.callout_data     = md->callout_data;
2453          if ((rrc = (*pcre_callout)(&cb)) < 0) return rrc;   /* Abandon */          if ((rrc = (*pcre_callout)(&cb)) < 0) return rrc;   /* Abandon */
         if (rrc == 0) { ADD_ACTIVE(state_offset + 2 + 2*LINK_SIZE, 0); }  
2454          }          }
2455          if (rrc == 0)
2456            { ADD_ACTIVE(state_offset + _pcre_OP_lengths[OP_CALLOUT], 0); }
2457        break;        break;
2458    
2459    
# Line 2443  for (;;) Line 2473  for (;;)
2473    
2474    if (new_count <= 0)    if (new_count <= 0)
2475      {      {
2476      if (match_count < 0 &&                     /* No matches found */      if (rlevel == 1 &&                               /* Top level, and */
2477          rlevel == 1 &&                         /* Top level match function */          (                                            /* either... */
2478          (md->moptions & PCRE_PARTIAL) != 0 &&  /* Want partial matching */          (md->moptions & PCRE_PARTIAL_HARD) != 0      /* Hard partial */
2479          ptr >= end_subject &&                  /* Reached end of subject */          ||                                           /* or... */
2480          ptr > current_subject)                 /* Matched non-empty string */          ((md->moptions & PCRE_PARTIAL_SOFT) != 0 &&  /* Soft partial and */
2481             match_count < 0)                            /* no matches */
2482            ) &&                                         /* And... */
2483            ptr >= end_subject &&                     /* Reached end of subject */
2484            ptr > current_subject)                    /* Matched non-empty string */
2485        {        {
2486        if (offsetcount >= 2)        if (offsetcount >= 2)
2487          {          {
# Line 2615  switch ((((options & PCRE_NEWLINE_BITS) Line 2649  switch ((((options & PCRE_NEWLINE_BITS)
2649           PCRE_NEWLINE_BITS)           PCRE_NEWLINE_BITS)
2650    {    {
2651    case 0: newline = NEWLINE; break;   /* Compile-time default */    case 0: newline = NEWLINE; break;   /* Compile-time default */
2652    case PCRE_NEWLINE_CR: newline = '\r'; break;    case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
2653    case PCRE_NEWLINE_LF: newline = '\n'; break;    case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
2654    case PCRE_NEWLINE_CR+    case PCRE_NEWLINE_CR+
2655         PCRE_NEWLINE_LF: newline = ('\r' << 8) | '\n'; break;         PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
2656    case PCRE_NEWLINE_ANY: newline = -1; break;    case PCRE_NEWLINE_ANY: newline = -1; break;
2657    case PCRE_NEWLINE_ANYCRLF: newline = -2; break;    case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
2658    default: return PCRE_ERROR_BADNEWLINE;    default: return PCRE_ERROR_BADNEWLINE;
# Line 2714  if ((re->flags & PCRE_REQCHSET) != 0) Line 2748  if ((re->flags & PCRE_REQCHSET) != 0)
2748    }    }
2749    
2750  /* Call the main matching function, looping for a non-anchored regex after a  /* Call the main matching function, looping for a non-anchored regex after a
2751  failed match. Unless restarting, optimize by moving to the first match  failed match. If not restarting, perform certain optimizations at the start of
2752  character if possible, when not anchored. Then unless wanting a partial match,  a match. */
 check for a required later character. */  
2753    
2754  for (;;)  for (;;)
2755    {    {
# Line 2726  for (;;) Line 2759  for (;;)
2759      {      {
2760      const uschar *save_end_subject = end_subject;      const uschar *save_end_subject = end_subject;
2761    
2762      /* Advance to a unique first char if possible. If firstline is TRUE, the      /* If firstline is TRUE, the start of the match is constrained to the first
2763      start of the match is constrained to the first line of a multiline string.      line of a multiline string. Implement this by temporarily adjusting
2764      Implement this by temporarily adjusting end_subject so that we stop      end_subject so that we stop scanning at a newline. If the match fails at
2765      scanning at a newline. If the match fails at the newline, later code breaks      the newline, later code breaks this loop. */
     this loop. */  
2766    
2767      if (firstline)      if (firstline)
2768        {        {
# Line 2750  for (;;) Line 2782  for (;;)
2782        end_subject = t;        end_subject = t;
2783        }        }
2784    
2785      if (first_byte >= 0)      /* There are some optimizations that avoid running the match if a known
2786        starting point is not found, or if a known later character is not present.
2787        However, there is an option that disables these, for testing and for
2788        ensuring that all callouts do actually occur. */
2789    
2790        if ((options & PCRE_NO_START_OPTIMIZE) == 0)
2791        {        {
       if (first_byte_caseless)  
         while (current_subject < end_subject &&  
                lcc[*current_subject] != first_byte)  
           current_subject++;  
       else  
         while (current_subject < end_subject && *current_subject != first_byte)  
           current_subject++;  
       }  
2792    
2793      /* Or to just after a linebreak for a multiline match if possible */        /* Advance to a known first byte. */
2794    
2795      else if (startline)        if (first_byte >= 0)
       {  
       if (current_subject > md->start_subject + start_offset)  
2796          {          {
2797  #ifdef SUPPORT_UTF8          if (first_byte_caseless)
2798          if (utf8)            while (current_subject < end_subject &&
2799                     lcc[*current_subject] != first_byte)
2800                current_subject++;
2801            else
2802              while (current_subject < end_subject &&
2803                     *current_subject != first_byte)
2804                current_subject++;
2805            }
2806    
2807          /* Or to just after a linebreak for a multiline match if possible */
2808    
2809          else if (startline)
2810            {
2811            if (current_subject > md->start_subject + start_offset)
2812            {            {
2813            while (current_subject < end_subject && !WAS_NEWLINE(current_subject))  #ifdef SUPPORT_UTF8
2814              if (utf8)
2815              {              {
2816              current_subject++;              while (current_subject < end_subject &&
2817              while(current_subject < end_subject &&                     !WAS_NEWLINE(current_subject))
2818                    (*current_subject & 0xc0) == 0x80)                {
2819                current_subject++;                current_subject++;
2820                  while(current_subject < end_subject &&
2821                        (*current_subject & 0xc0) == 0x80)
2822                    current_subject++;
2823                  }
2824              }              }
2825            }            else
         else  
2826  #endif  #endif
2827          while (current_subject < end_subject && !WAS_NEWLINE(current_subject))            while (current_subject < end_subject && !WAS_NEWLINE(current_subject))
2828            current_subject++;              current_subject++;
2829    
2830          /* If we have just passed a CR and the newline option is ANY or            /* If we have just passed a CR and the newline option is ANY or
2831          ANYCRLF, and we are now at a LF, advance the match position by one more            ANYCRLF, and we are now at a LF, advance the match position by one
2832          character. */            more character. */
2833    
2834          if (current_subject[-1] == '\r' &&            if (current_subject[-1] == CHAR_CR &&
2835               (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&                 (md->nltype == NLTYPE_ANY || md->nltype == NLTYPE_ANYCRLF) &&
2836               current_subject < end_subject &&                 current_subject < end_subject &&
2837               *current_subject == '\n')                 *current_subject == CHAR_NL)
2838            current_subject++;              current_subject++;
2839              }
2840          }          }
       }  
2841    
2842      /* Or to a non-unique first char after study */        /* Or to a non-unique first char after study */
2843    
2844      else if (start_bits != NULL)        else if (start_bits != NULL)
       {  
       while (current_subject < end_subject)  
2845          {          {
2846          register unsigned int c = *current_subject;          while (current_subject < end_subject)
2847          if ((start_bits[c/8] & (1 << (c&7))) == 0) current_subject++;            {
2848            else break;            register unsigned int c = *current_subject;
2849              if ((start_bits[c/8] & (1 << (c&7))) == 0) current_subject++;
2850                else break;
2851              }
2852          }          }
2853        }        }
2854    
# Line 2825  for (;;) Line 2870  for (;;)
2870    showed up when somebody was matching /^C/ on a 32-megabyte string... so we    showed up when somebody was matching /^C/ on a 32-megabyte string... so we
2871    don't do this when the string is sufficiently long.    don't do this when the string is sufficiently long.
2872    
2873    ALSO: this processing is disabled when partial matching is requested.    ALSO: this processing is disabled when partial matching is requested, and can
2874    */    also be explicitly deactivated. */
2875    
2876    if (req_byte >= 0 &&    if ((options & PCRE_NO_START_OPTIMIZE) == 0 &&
2877          req_byte >= 0 &&
2878        end_subject - current_subject < REQ_BYTE_MAX &&        end_subject - current_subject < REQ_BYTE_MAX &&
2879        (options & PCRE_PARTIAL) == 0)        (options & PCRE_PARTIAL) == 0)
2880      {      {
# Line 2904  for (;;) Line 2950  for (;;)
2950    not contain any explicit matches for \r or \n, and the newline option is CRLF    not contain any explicit matches for \r or \n, and the newline option is CRLF
2951    or ANY or ANYCRLF, advance the match position by one more character. */    or ANY or ANYCRLF, advance the match position by one more character. */
2952    
2953    if (current_subject[-1] == '\r' &&    if (current_subject[-1] == CHAR_CR &&
2954        current_subject < end_subject &&        current_subject < end_subject &&
2955        *current_subject == '\n' &&        *current_subject == CHAR_NL &&
2956        (re->flags & PCRE_HASCRORLF) == 0 &&        (re->flags & PCRE_HASCRORLF) == 0 &&
2957          (md->nltype == NLTYPE_ANY ||          (md->nltype == NLTYPE_ANY ||
2958           md->nltype == NLTYPE_ANYCRLF ||           md->nltype == NLTYPE_ANYCRLF ||

Legend:
Removed from v.383  
changed lines
  Added in v.427

  ViewVC Help
Powered by ViewVC 1.1.5