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

Diff of /code/trunk/pcre_compile.c

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

revision 905 by zherczeg, Mon Jan 23 19:26:03 2012 UTC revision 978 by ph10, Sun Jun 17 16:55:07 2012 UTC
# Line 489  static const char error_texts[] = Line 489  static const char error_texts[] =
489    "too many forward references\0"    "too many forward references\0"
490    "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"    "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
491    "invalid UTF-16 string\0"    "invalid UTF-16 string\0"
492      /* 75 */
493      "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
494      "character value in \\u.... sequence is too large\0"
495    ;    ;
496    
497  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
# Line 829  else Line 832  else
832            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
833  #endif  #endif
834            }            }
835    
836    #ifdef COMPILE_PCRE8
837            if (c > (utf ? 0x10ffff : 0xff))
838    #else
839    #ifdef COMPILE_PCRE16
840            if (c > (utf ? 0x10ffff : 0xffff))
841    #endif
842    #endif
843              {
844              *errorcodeptr = ERR76;
845              }
846            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
847          }          }
848        }        }
849      else      else
# Line 2225  for (;;) Line 2240  for (;;)
2240        {        {
2241        case OP_CHAR:        case OP_CHAR:
2242        case OP_CHARI:        case OP_CHARI:
2243          case OP_NOT:
2244          case OP_NOTI:
2245        case OP_EXACT:        case OP_EXACT:
2246        case OP_EXACTI:        case OP_EXACTI:
2247          case OP_NOTEXACT:
2248          case OP_NOTEXACTI:
2249        case OP_UPTO:        case OP_UPTO:
2250        case OP_UPTOI:        case OP_UPTOI:
2251          case OP_NOTUPTO:
2252          case OP_NOTUPTOI:
2253        case OP_MINUPTO:        case OP_MINUPTO:
2254        case OP_MINUPTOI:        case OP_MINUPTOI:
2255          case OP_NOTMINUPTO:
2256          case OP_NOTMINUPTOI:
2257        case OP_POSUPTO:        case OP_POSUPTO:
2258        case OP_POSUPTOI:        case OP_POSUPTOI:
2259          case OP_NOTPOSUPTO:
2260          case OP_NOTPOSUPTOI:
2261        case OP_STAR:        case OP_STAR:
2262        case OP_STARI:        case OP_STARI:
2263          case OP_NOTSTAR:
2264          case OP_NOTSTARI:
2265        case OP_MINSTAR:        case OP_MINSTAR:
2266        case OP_MINSTARI:        case OP_MINSTARI:
2267          case OP_NOTMINSTAR:
2268          case OP_NOTMINSTARI:
2269        case OP_POSSTAR:        case OP_POSSTAR:
2270        case OP_POSSTARI:        case OP_POSSTARI:
2271          case OP_NOTPOSSTAR:
2272          case OP_NOTPOSSTARI:
2273        case OP_PLUS:        case OP_PLUS:
2274        case OP_PLUSI:        case OP_PLUSI:
2275          case OP_NOTPLUS:
2276          case OP_NOTPLUSI:
2277        case OP_MINPLUS:        case OP_MINPLUS:
2278        case OP_MINPLUSI:        case OP_MINPLUSI:
2279          case OP_NOTMINPLUS:
2280          case OP_NOTMINPLUSI:
2281        case OP_POSPLUS:        case OP_POSPLUS:
2282        case OP_POSPLUSI:        case OP_POSPLUSI:
2283          case OP_NOTPOSPLUS:
2284          case OP_NOTPOSPLUSI:
2285        case OP_QUERY:        case OP_QUERY:
2286        case OP_QUERYI:        case OP_QUERYI:
2287          case OP_NOTQUERY:
2288          case OP_NOTQUERYI:
2289        case OP_MINQUERY:        case OP_MINQUERY:
2290        case OP_MINQUERYI:        case OP_MINQUERYI:
2291          case OP_NOTMINQUERY:
2292          case OP_NOTMINQUERYI:
2293        case OP_POSQUERY:        case OP_POSQUERY:
2294        case OP_POSQUERYI:        case OP_POSQUERYI:
2295          case OP_NOTPOSQUERY:
2296          case OP_NOTPOSQUERYI:
2297        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2298        break;        break;
2299        }        }
# Line 3069  if (next >= 0) switch(op_code) Line 3112  if (next >= 0) switch(op_code)
3112  #endif  /* SUPPORT_UTF */  #endif  /* SUPPORT_UTF */
3113    return (c != TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */    return (c != TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */
3114    
   /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These  
   opcodes are not used for multi-byte characters, because they are coded using  
   an XCLASS instead. */  
   
3115    case OP_NOT:    case OP_NOT:
3116    return (c = *previous) == next;  #ifdef SUPPORT_UTF
3117      GETCHARTEST(c, previous);
3118    #else
3119      c = *previous;
3120    #endif
3121      return c == next;
3122    
3123    case OP_NOTI:    case OP_NOTI:
3124    if ((c = *previous) == next) return TRUE;  #ifdef SUPPORT_UTF
3125      GETCHARTEST(c, previous);
3126    #else
3127      c = *previous;
3128    #endif
3129      if (c == next) return TRUE;
3130  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3131    if (utf)    if (utf)
3132      {      {
3133      unsigned int othercase;      unsigned int othercase;
3134      if (next < 128) othercase = cd->fcc[next]; else      if (next < 128) othercase = cd->fcc[next]; else
3135  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3136      othercase = UCD_OTHERCASE(next);      othercase = UCD_OTHERCASE((unsigned int)next);
3137  #else  #else
3138      othercase = NOTACHAR;      othercase = NOTACHAR;
3139  #endif  #endif
# Line 3092  if (next >= 0) switch(op_code) Line 3141  if (next >= 0) switch(op_code)
3141      }      }
3142    else    else
3143  #endif  /* SUPPORT_UTF */  #endif  /* SUPPORT_UTF */
3144    return (c == (int)(TABLE_GET((unsigned int)next, cd->fcc, next)));  /* Non-UTF-8 mode */    return (c == TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */
3145    
3146    /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.    /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
3147    When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */    When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3148    
3149    case OP_DIGIT:    case OP_DIGIT:
3150    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;    return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;
3151    
3152    case OP_NOT_DIGIT:    case OP_NOT_DIGIT:
3153    return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0;    return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;
3154    
3155    case OP_WHITESPACE:    case OP_WHITESPACE:
3156    return next > 127 || (cd->ctypes[next] & ctype_space) == 0;    return next > 255 || (cd->ctypes[next] & ctype_space) == 0;
3157    
3158    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3159    return next <= 127 && (cd->ctypes[next] & ctype_space) != 0;    return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;
3160    
3161    case OP_WORDCHAR:    case OP_WORDCHAR:
3162    return next > 127 || (cd->ctypes[next] & ctype_word) == 0;    return next > 255 || (cd->ctypes[next] & ctype_word) == 0;
3163    
3164    case OP_NOT_WORDCHAR:    case OP_NOT_WORDCHAR:
3165    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;    return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;
3166    
3167    case OP_HSPACE:    case OP_HSPACE:
3168    case OP_NOT_HSPACE:    case OP_NOT_HSPACE:
# Line 3191  switch(op_code) Line 3240  switch(op_code)
3240    switch(-next)    switch(-next)
3241      {      {
3242      case ESC_d:      case ESC_d:
3243      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;
3244    
3245      case ESC_D:      case ESC_D:
3246      return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;
3247    
3248      case ESC_s:      case ESC_s:
3249      return c > 127 || (cd->ctypes[c] & ctype_space) == 0;      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;
3250    
3251      case ESC_S:      case ESC_S:
3252      return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;
3253    
3254      case ESC_w:      case ESC_w:
3255      return c > 127 || (cd->ctypes[c] & ctype_word) == 0;      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;
3256    
3257      case ESC_W:      case ESC_W:
3258      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;
3259    
3260      case ESC_h:      case ESC_h:
3261      case ESC_H:      case ESC_H:
# Line 3315  switch(op_code) Line 3364  switch(op_code)
3364    return next == -ESC_d;    return next == -ESC_d;
3365    
3366    case OP_WHITESPACE:    case OP_WHITESPACE:
3367    return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;    return next == -ESC_S || next == -ESC_d || next == -ESC_w;
3368    
3369    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3370    return next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_s || next == -ESC_h || next == -ESC_v || next == -ESC_R;
3371    
3372    case OP_HSPACE:    case OP_HSPACE:
3373    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||
# Line 4482  for (;; ptr++) Line 4531  for (;; ptr++)
4531        LONE_SINGLE_CHARACTER:        LONE_SINGLE_CHARACTER:
4532    
4533        /* Only the value of 1 matters for class_single_char. */        /* Only the value of 1 matters for class_single_char. */
4534    
4535        if (class_single_char < 2) class_single_char++;        if (class_single_char < 2) class_single_char++;
4536    
4537        /* If class_charcount is 1, we saw precisely one character. As long as        /* If class_charcount is 1, we saw precisely one character. As long as
4538        there were no negated characters >= 128 and there was no use of \p or \P,        there was no use of \p or \P, in other words, no use of any XCLASS
4539        in other words, no use of any XCLASS features, we can optimize.        features, we can optimize.
   
       In UTF-8 mode, we can optimize the negative case only if there were no  
       characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR  
       operate on single-bytes characters only. This is an historical hangover.  
       Maybe one day we can tidy these opcodes to handle multi-byte characters.  
4540    
4541        The optimization throws away the bit map. We turn the item into a        The optimization throws away the bit map. We turn the item into a
4542        1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.        1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.
4543        Note that OP_NOT[I] does not support multibyte characters. In the positive        In the positive case, it can cause firstchar to be set. Otherwise, there
4544        case, it can cause firstchar to be set. Otherwise, there can be no first        can be no first char if this item is first, whatever repeat count may
4545        char if this item is first, whatever repeat count may follow. In the case        follow. In the case of reqchar, save the previous value for reinstating. */
       of reqchar, save the previous value for reinstating. */  
4546    
 #ifdef SUPPORT_UTF  
       if (class_single_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET  
         && (!utf || !negate_class || c < (MAX_VALUE_FOR_SINGLE_CHAR + 1)))  
 #else  
4547        if (class_single_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)        if (class_single_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
 #endif  
4548          {          {
4549          ptr++;          ptr++;
4550          zeroreqchar = reqchar;          zeroreqchar = reqchar;
4551    
         /* The OP_NOT[I] opcodes work on single characters only. */  
   
4552          if (negate_class)          if (negate_class)
4553            {            {
4554            if (firstchar == REQ_UNSET) firstchar = REQ_NONE;            if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4555            zerofirstchar = firstchar;            zerofirstchar = firstchar;
4556            *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;            *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4557            *code++ = c;  #ifdef SUPPORT_UTF
4558              if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4559                code += PRIV(ord2utf)(c, code);
4560              else
4561    #endif
4562                *code++ = c;
4563            goto NOT_CHAR;            goto NOT_CHAR;
4564            }            }
4565    
# Line 4775  for (;; ptr++) Line 4817  for (;; ptr++)
4817    
4818      /* Now handle repetition for the different types of item. */      /* Now handle repetition for the different types of item. */
4819    
4820      /* If previous was a character match, abolish the item and generate a      /* If previous was a character or negated character match, abolish the item
4821      repeat item instead. If a char item has a minumum of more than one, ensure      and generate a repeat item instead. If a char item has a minimum of more
4822      that it is set in reqchar - it might not be if a sequence such as x{3} is      than one, ensure that it is set in reqchar - it might not be if a sequence
4823      the first thing in a branch because the x will have gone into firstchar      such as x{3} is the first thing in a branch because the x will have gone
4824      instead.  */      into firstchar instead.  */
4825    
4826      if (*previous == OP_CHAR || *previous == OP_CHARI)      if (*previous == OP_CHAR || *previous == OP_CHARI
4827        {          || *previous == OP_NOT || *previous == OP_NOTI)
4828        op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;        {
4829          switch (*previous)
4830            {
4831            default: /* Make compiler happy. */
4832            case OP_CHAR:  op_type = OP_STAR - OP_STAR; break;
4833            case OP_CHARI: op_type = OP_STARI - OP_STAR; break;
4834            case OP_NOT:   op_type = OP_NOTSTAR - OP_STAR; break;
4835            case OP_NOTI:  op_type = OP_NOTSTARI - OP_STAR; break;
4836            }
4837    
4838        /* Deal with UTF characters that take up more than one character. It's        /* Deal with UTF characters that take up more than one character. It's
4839        easier to write this out separately than try to macrify it. Use c to        easier to write this out separately than try to macrify it. Use c to
# Line 4806  for (;; ptr++) Line 4856  for (;; ptr++)
4856        with UTF disabled, or for a single character UTF character. */        with UTF disabled, or for a single character UTF character. */
4857          {          {
4858          c = code[-1];          c = code[-1];
4859          if (repeat_min > 1) reqchar = c | req_caseopt | cd->req_varyopt;          if (*previous <= OP_CHARI && repeat_min > 1)
4860              reqchar = c | req_caseopt | cd->req_varyopt;
4861          }          }
4862    
4863        /* If the repetition is unlimited, it pays to see if the next thing on        /* If the repetition is unlimited, it pays to see if the next thing on
# Line 4825  for (;; ptr++) Line 4876  for (;; ptr++)
4876        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
4877        }        }
4878    
     /* If previous was a single negated character ([^a] or similar), we use  
     one of the special opcodes, replacing it. The code is shared with single-  
     character repeats by setting opt_type to add a suitable offset into  
     repeat_type. We can also test for auto-possessification. OP_NOT and OP_NOTI  
     are currently used only for single-byte chars. */  
   
     else if (*previous == OP_NOT || *previous == OP_NOTI)  
       {  
       op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR;  
       c = previous[1];  
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
       goto OUTPUT_SINGLE_REPEAT;  
       }  
   
4879      /* If previous was a character type match (\d or similar), abolish it and      /* If previous was a character type match (\d or similar), abolish it and
4880      create a suitable repeat item. The code is shared with single-character      create a suitable repeat item. The code is shared with single-character
4881      repeats by setting op_type to add a suitable offset into repeat_type. Note      repeats by setting op_type to add a suitable offset into repeat_type. Note
# Line 5585  for (;; ptr++) Line 5616  for (;; ptr++)
5616          arg = ++ptr;          arg = ++ptr;
5617          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5618          arglen = (int)(ptr - arg);          arglen = (int)(ptr - arg);
5619            if (arglen > (int)MAX_MARK)
5620              {
5621              *errorcodeptr = ERR75;
5622              goto FAILED;
5623              }
5624          }          }
5625    
5626        if (*ptr != CHAR_RIGHT_PARENTHESIS)        if (*ptr != CHAR_RIGHT_PARENTHESIS)
# Line 6836  for (;; ptr++) Line 6872  for (;; ptr++)
6872        /* For the rest (including \X when Unicode properties are supported), we        /* For the rest (including \X when Unicode properties are supported), we
6873        can obtain the OP value by negating the escape value in the default        can obtain the OP value by negating the escape value in the default
6874        situation when PCRE_UCP is not set. When it *is* set, we substitute        situation when PCRE_UCP is not set. When it *is* set, we substitute
6875        Unicode property tests. */        Unicode property tests. Note that \b and \B do a one-character
6876          lookbehind. */
6877    
6878        else        else
6879          {          {
6880            if ((-c == ESC_b || -c == ESC_B) && cd->max_lookbehind == 0)
6881              cd->max_lookbehind = 1;
6882  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
6883          if (-c >= ESC_DU && -c <= ESC_wu)          if (-c >= ESC_DU && -c <= ESC_wu)
6884            {            {
# Line 7147  for (;;) Line 7186  for (;;)
7186          *ptrptr = ptr;          *ptrptr = ptr;
7187          return FALSE;          return FALSE;
7188          }          }
7189        else { PUT(reverse_count, 0, fixed_length); }        else
7190            {
7191            if (fixed_length > cd->max_lookbehind)
7192              cd->max_lookbehind = fixed_length;
7193            PUT(reverse_count, 0, fixed_length);
7194            }
7195        }        }
7196      }      }
7197    
# Line 7817  cd->start_pattern = (const pcre_uchar *) Line 7861  cd->start_pattern = (const pcre_uchar *)
7861  cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));  cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));
7862  cd->req_varyopt = 0;  cd->req_varyopt = 0;
7863  cd->assert_depth = 0;  cd->assert_depth = 0;
7864    cd->max_lookbehind = 0;
7865  cd->external_options = options;  cd->external_options = options;
7866  cd->external_flags = 0;  cd->external_flags = 0;
7867  cd->open_caps = NULL;  cd->open_caps = NULL;
# Line 7867  re->magic_number = MAGIC_NUMBER; Line 7912  re->magic_number = MAGIC_NUMBER;
7912  re->size = (int)size;  re->size = (int)size;
7913  re->options = cd->external_options;  re->options = cd->external_options;
7914  re->flags = cd->external_flags;  re->flags = cd->external_flags;
 re->dummy1 = 0;  
7915  re->first_char = 0;  re->first_char = 0;
7916  re->req_char = 0;  re->req_char = 0;
7917  re->name_table_offset = sizeof(REAL_PCRE) / sizeof(pcre_uchar);  re->name_table_offset = sizeof(REAL_PCRE) / sizeof(pcre_uchar);
# Line 7887  field; this time it's used for rememberi Line 7931  field; this time it's used for rememberi
7931  cd->final_bracount = cd->bracount;  /* Save for checking forward references */  cd->final_bracount = cd->bracount;  /* Save for checking forward references */
7932  cd->assert_depth = 0;  cd->assert_depth = 0;
7933  cd->bracount = 0;  cd->bracount = 0;
7934    cd->max_lookbehind = 0;
7935  cd->names_found = 0;  cd->names_found = 0;
7936  cd->name_table = (pcre_uchar *)re + re->name_table_offset;  cd->name_table = (pcre_uchar *)re + re->name_table_offset;
7937  codestart = cd->name_table + re->name_entry_size * re->name_count;  codestart = cd->name_table + re->name_entry_size * re->name_count;
# Line 7908  code = (pcre_uchar *)codestart; Line 7953  code = (pcre_uchar *)codestart;
7953    &firstchar, &reqchar, NULL, cd, NULL);    &firstchar, &reqchar, NULL, cd, NULL);
7954  re->top_bracket = cd->bracount;  re->top_bracket = cd->bracount;
7955  re->top_backref = cd->top_backref;  re->top_backref = cd->top_backref;
7956    re->max_lookbehind = cd->max_lookbehind;
7957  re->flags = cd->external_flags | PCRE_MODE;  re->flags = cd->external_flags | PCRE_MODE;
7958    
7959  if (cd->had_accept) reqchar = REQ_NONE;   /* Must disable after (*ACCEPT) */  if (cd->had_accept) reqchar = REQ_NONE;   /* Must disable after (*ACCEPT) */
# Line 7995  if (cd->check_lookbehind) Line 8041  if (cd->check_lookbehind)
8041                      (fixed_length == -4)? ERR70 : ERR25;                      (fixed_length == -4)? ERR70 : ERR25;
8042          break;          break;
8043          }          }
8044          if (fixed_length > cd->max_lookbehind) cd->max_lookbehind = fixed_length;
8045        PUT(cc, 1, fixed_length);        PUT(cc, 1, fixed_length);
8046        }        }
8047      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;

Legend:
Removed from v.905  
changed lines
  Added in v.978

  ViewVC Help
Powered by ViewVC 1.1.5