/[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 964 by ph10, Fri May 4 13:03:39 2012 UTC revision 1048 by ph10, Fri Sep 28 16:03:14 2012 UTC
# Line 68  COMPILE_PCREx macro will already be appr Line 68  COMPILE_PCREx macro will already be appr
68    
69  /* Macro for setting individual bits in class bitmaps. */  /* Macro for setting individual bits in class bitmaps. */
70    
71  #define SETBIT(a,b) a[b/8] |= (1 << (b%8))  #define SETBIT(a,b) a[(b)/8] |= (1 << ((b)&7))
72    
73  /* Maximum length value to check against when making sure that the integer that  /* Maximum length value to check against when making sure that the integer that
74  holds the compiled pattern length does not overflow. We make it a bit less than  holds the compiled pattern length does not overflow. We make it a bit less than
# Line 77  to check them every time. */ Line 77  to check them every time. */
77    
78  #define OFLOW_MAX (INT_MAX - 20)  #define OFLOW_MAX (INT_MAX - 20)
79    
80    /* Definitions to allow mutual recursion */
81    
82    static int
83      add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,
84        const pcre_uint32 *, unsigned int);
85    
86    static BOOL
87      compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL,
88        int, int, int *, int *, branch_chain *, compile_data *, int *);
89    
90    
91    
92  /*************************************************  /*************************************************
93  *      Code parameters and static tables         *  *      Code parameters and static tables         *
# Line 490  static const char error_texts[] = Line 501  static const char error_texts[] =
501    "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"    "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
502    "invalid UTF-16 string\0"    "invalid UTF-16 string\0"
503    /* 75 */    /* 75 */
504    "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"    "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
505      "character value in \\u.... sequence is too large\0"
506    ;    ;
507    
508  /* 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 630  static const pcre_uint8 ebcdic_chartab[] Line 642  static const pcre_uint8 ebcdic_chartab[]
642  #endif  #endif
643    
644    
 /* Definition to allow mutual recursion */  
   
 static BOOL  
   compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,  
     int *, int *, branch_chain *, compile_data *, int *);  
   
645    
646    
647  /*************************************************  /*************************************************
# Line 788  else if ((i = escapes[c - CHAR_0]) != 0) Line 794  else if ((i = escapes[c - CHAR_0]) != 0)
794    
795  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
796  /* Not alphanumeric */  /* Not alphanumeric */
797  else if (c < 'a' || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}  else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
798  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if ((i = escapes[c - 0x48]) != 0)  c = i;
799  #endif  #endif
800    
# Line 831  else Line 837  else
837            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
838  #endif  #endif
839            }            }
840    
841    #ifdef COMPILE_PCRE8
842            if (c > (utf ? 0x10ffff : 0xff))
843    #else
844    #ifdef COMPILE_PCRE16
845            if (c > (utf ? 0x10ffff : 0xffff))
846    #endif
847    #endif
848              {
849              *errorcodeptr = ERR76;
850              }
851            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
852          }          }
853        }        }
854      else      else
# Line 1841  for (;;) Line 1859  for (;;)
1859    
1860      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1861      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1862      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1863          cc += 2;
1864      cc += 1 + IMM2_SIZE + 1;      cc += 1 + IMM2_SIZE + 1;
1865      break;      break;
1866    
# Line 2079  for (;;) Line 2098  for (;;)
2098        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2099        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2100        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
2101        if (code[1 + IMM2_SIZE] == OP_PROP        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2102          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;          code += 2;
2103        break;        break;
2104    
2105        case OP_MARK:        case OP_MARK:
# Line 2199  for (;;) Line 2218  for (;;)
2218        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2219        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2220        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2221        if (code[1 + IMM2_SIZE] == OP_PROP        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2222          || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;          code += 2;
2223        break;        break;
2224    
2225        case OP_MARK:        case OP_MARK:
# Line 2525  for (code = first_significant_code(code Line 2544  for (code = first_significant_code(code
2544      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2545      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2546      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2547      if (code[1 + IMM2_SIZE] == OP_PROP      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2548        || code[1 + IMM2_SIZE] == OP_NOTPROP) code += 2;        code += 2;
2549      break;      break;
2550    
2551      /* End of branch */      /* End of branch */
# Line 2858  PUT(previous_callout, 2 + LINK_SIZE, len Line 2877  PUT(previous_callout, 2 + LINK_SIZE, len
2877  *************************************************/  *************************************************/
2878    
2879  /* This function is passed the start and end of a class range, in UTF-8 mode  /* This function is passed the start and end of a class range, in UTF-8 mode
2880  with UCP support. It searches up the characters, looking for internal ranges of  with UCP support. It searches up the characters, looking for ranges of
2881  characters in the "other" case. Each call returns the next one, updating the  characters in the "other" case. Each call returns the next one, updating the
2882  start address.  start address. A character with multiple other cases is returned on its own
2883    with a special return value.
2884    
2885  Arguments:  Arguments:
2886    cptr        points to starting character value; updated    cptr        points to starting character value; updated
# Line 2868  Arguments: Line 2888  Arguments:
2888    ocptr       where to put start of othercase range    ocptr       where to put start of othercase range
2889    odptr       where to put end of othercase range    odptr       where to put end of othercase range
2890    
2891  Yield:        TRUE when range returned; FALSE when no more  Yield:        -1 when no more
2892                   0 when a range is returned
2893                  >0 the CASESET offset for char with multiple other cases
2894                    in this case, ocptr contains the original
2895  */  */
2896    
2897  static BOOL  static int
2898  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,
2899    unsigned int *odptr)    unsigned int *odptr)
2900  {  {
2901  unsigned int c, othercase, next;  unsigned int c, othercase, next;
2902    int co;
2903    
2904    /* Find the first character that has an other case. If it has multiple other
2905    cases, return its case offset value. */
2906    
2907  for (c = *cptr; c <= d; c++)  for (c = *cptr; c <= d; c++)
2908    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }    {
2909      if ((co = UCD_CASESET(c)) != 0)
2910        {
2911        *ocptr = c++;   /* Character that has the set */
2912        *cptr = c;      /* Rest of input range */
2913        return co;
2914        }
2915      if ((othercase = UCD_OTHERCASE(c)) != c) break;
2916      }
2917    
2918  if (c > d) return FALSE;  if (c > d) return -1;  /* Reached end of range */
2919    
2920  *ocptr = othercase;  *ocptr = othercase;
2921  next = othercase + 1;  next = othercase + 1;
# Line 2891  for (++c; c <= d; c++) Line 2926  for (++c; c <= d; c++)
2926    next++;    next++;
2927    }    }
2928    
2929  *odptr = next - 1;  *odptr = next - 1;     /* End of othercase range */
2930  *cptr = c;  *cptr = c;             /* Rest of input range */
2931    return 0;
 return TRUE;  
2932  }  }
2933    
2934    
# Line 2918  Returns:       TRUE if auto-possessifyin Line 2952  Returns:       TRUE if auto-possessifyin
2952  static BOOL  static BOOL
2953  check_char_prop(int c, int ptype, int pdata, BOOL negated)  check_char_prop(int c, int ptype, int pdata, BOOL negated)
2954  {  {
2955    #ifdef SUPPORT_UCP
2956    const pcre_uint32 *p;
2957    #endif
2958    
2959  const ucd_record *prop = GET_UCD(c);  const ucd_record *prop = GET_UCD(c);
2960    
2961  switch(ptype)  switch(ptype)
2962    {    {
2963    case PT_LAMP:    case PT_LAMP:
# Line 2956  switch(ptype) Line 2995  switch(ptype)
2995    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2996            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2997            c == CHAR_UNDERSCORE) == negated;            c == CHAR_UNDERSCORE) == negated;
2998    
2999    #ifdef SUPPORT_UCP
3000      case PT_CLIST:
3001      p = PRIV(ucd_caseless_sets) + prop->caseset;
3002      for (;;)
3003        {
3004        if ((unsigned int)c < *p) return !negated;
3005        if ((unsigned int)c == *p++) return negated;
3006        }
3007      break;  /* Control never reaches here */
3008    #endif
3009    }    }
3010    
3011  return FALSE;  return FALSE;
3012  }  }
3013  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
# Line 2985  static BOOL Line 3036  static BOOL
3036  check_auto_possessive(const pcre_uchar *previous, BOOL utf,  check_auto_possessive(const pcre_uchar *previous, BOOL utf,
3037    const pcre_uchar *ptr, int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
3038  {  {
3039  pcre_int32 c, next;  pcre_int32 c = NOTACHAR;
3040    pcre_int32 next;
3041  int op_code = *previous++;  int op_code = *previous++;
3042    
3043  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
# Line 3059  if (*ptr == CHAR_ASTERISK || *ptr == CHA Line 3111  if (*ptr == CHAR_ASTERISK || *ptr == CHA
3111    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3112      return FALSE;      return FALSE;
3113    
3114  /* Now compare the next item with the previous opcode. First, handle cases when  /* If the previous item is a character, get its value. */
 the next item is a character. */  
3115    
3116  if (next >= 0) switch(op_code)  if (op_code == OP_CHAR || op_code == OP_CHARI ||
3117        op_code == OP_NOT || op_code == OP_NOTI)
3118    {    {
   case OP_CHAR:  
3119  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3120    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
3121  #else  #else
3122    c = *previous;    c = *previous;
3123  #endif  #endif
3124    return c != next;    }
3125    
3126    /* For CHARI (caseless character) we must check the other case. If we have  /* Now compare the next item with the previous opcode. First, handle cases when
3127    Unicode property support, we can use it to test the other case of  the next item is a character. */
   high-valued characters. */  
3128    
3129    case OP_CHARI:  if (next >= 0)
3130  #ifdef SUPPORT_UTF    {
3131    GETCHARTEST(c, previous);    /* For a caseless UTF match, the next character may have more than one other
3132  #else    case, which maps to the special PT_CLIST property. Check this first. */
3133    c = *previous;  
 #endif  
   if (c == next) return FALSE;  
 #ifdef SUPPORT_UTF  
   if (utf)  
     {  
     unsigned int othercase;  
     if (next < 128) othercase = cd->fcc[next]; else  
3134  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3135      othercase = UCD_OTHERCASE((unsigned int)next);    if (utf && (unsigned int)c != NOTACHAR && (options & PCRE_CASELESS) != 0)
3136  #else      {
3137      othercase = NOTACHAR;      int ocs = UCD_CASESET(next);
3138  #endif      if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);
     return (unsigned int)c != othercase;  
3139      }      }
   else  
 #endif  /* SUPPORT_UTF */  
   return (c != TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */  
   
   case OP_NOT:  
 #ifdef SUPPORT_UTF  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
3140  #endif  #endif
   return c == next;  
3141    
3142    case OP_NOTI:    switch(op_code)
3143        {
3144        case OP_CHAR:
3145        return c != next;
3146    
3147        /* For CHARI (caseless character) we must check the other case. If we have
3148        Unicode property support, we can use it to test the other case of
3149        high-valued characters. We know that next can have only one other case,
3150        because multi-other-case characters are dealt with above. */
3151    
3152        case OP_CHARI:
3153        if (c == next) return FALSE;
3154  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3155    GETCHARTEST(c, previous);      if (utf)
3156          {
3157          unsigned int othercase;
3158          if (next < 128) othercase = cd->fcc[next]; else
3159    #ifdef SUPPORT_UCP
3160          othercase = UCD_OTHERCASE((unsigned int)next);
3161  #else  #else
3162    c = *previous;        othercase = NOTACHAR;
3163  #endif  #endif
3164    if (c == next) return TRUE;        return (unsigned int)c != othercase;
3165          }
3166        else
3167    #endif  /* SUPPORT_UTF */
3168        return (c != TABLE_GET((unsigned int)next, cd->fcc, next));  /* Not UTF */
3169    
3170        case OP_NOT:
3171        return c == next;
3172    
3173        case OP_NOTI:
3174        if (c == next) return TRUE;
3175  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
3176    if (utf)      if (utf)
3177      {        {
3178      unsigned int othercase;        unsigned int othercase;
3179      if (next < 128) othercase = cd->fcc[next]; else        if (next < 128) othercase = cd->fcc[next]; else
3180  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3181      othercase = UCD_OTHERCASE((unsigned int)next);        othercase = UCD_OTHERCASE((unsigned int)next);
3182  #else  #else
3183      othercase = NOTACHAR;        othercase = NOTACHAR;
3184  #endif  #endif
3185      return (unsigned int)c == othercase;        return (unsigned int)c == othercase;
3186      }        }
3187    else      else
3188  #endif  /* SUPPORT_UTF */  #endif  /* SUPPORT_UTF */
3189    return (c == TABLE_GET((unsigned int)next, cd->fcc, next));  /* Non-UTF-8 mode */      return (c == TABLE_GET((unsigned int)next, cd->fcc, next));  /* Not UTF */
3190    
3191    /* 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.
3192    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. */
3193    
3194    case OP_DIGIT:      case OP_DIGIT:
3195    return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;      return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;
3196    
3197    case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
3198    return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;
3199    
3200    case OP_WHITESPACE:      case OP_WHITESPACE:
3201    return next > 255 || (cd->ctypes[next] & ctype_space) == 0;      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;
3202    
3203    case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
3204    return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;
3205    
3206    case OP_WORDCHAR:      case OP_WORDCHAR:
3207    return next > 255 || (cd->ctypes[next] & ctype_word) == 0;      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;
3208    
3209    case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
3210    return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;
3211    
3212    case OP_HSPACE:      case OP_HSPACE:
3213    case OP_NOT_HSPACE:      case OP_NOT_HSPACE:
3214    switch(next)      switch(next)
3215      {        {
3216      case 0x09:        HSPACE_CASES:
3217      case 0x20:        return op_code == OP_NOT_HSPACE;
     case 0xa0:  
     case 0x1680:  
     case 0x180e:  
     case 0x2000:  
     case 0x2001:  
     case 0x2002:  
     case 0x2003:  
     case 0x2004:  
     case 0x2005:  
     case 0x2006:  
     case 0x2007:  
     case 0x2008:  
     case 0x2009:  
     case 0x200A:  
     case 0x202f:  
     case 0x205f:  
     case 0x3000:  
     return op_code == OP_NOT_HSPACE;  
     default:  
     return op_code != OP_NOT_HSPACE;  
     }  
3218    
3219    case OP_ANYNL:        default:
3220    case OP_VSPACE:        return op_code != OP_NOT_HSPACE;
3221    case OP_NOT_VSPACE:        }
3222    switch(next)  
3223      {      case OP_ANYNL:
3224      case 0x0a:      case OP_VSPACE:
3225      case 0x0b:      case OP_NOT_VSPACE:
3226      case 0x0c:      switch(next)
3227      case 0x0d:        {
3228      case 0x85:        VSPACE_CASES:
3229      case 0x2028:        return op_code == OP_NOT_VSPACE;
3230      case 0x2029:  
3231      return op_code == OP_NOT_VSPACE;        default:
3232      default:        return op_code != OP_NOT_VSPACE;
3233      return op_code != OP_NOT_VSPACE;        }
     }  
3234    
3235  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3236    case OP_PROP:      case OP_PROP:
3237    return check_char_prop(next, previous[0], previous[1], FALSE);      return check_char_prop(next, previous[0], previous[1], FALSE);
3238    
3239    case OP_NOTPROP:      case OP_NOTPROP:
3240    return check_char_prop(next, previous[0], previous[1], TRUE);      return check_char_prop(next, previous[0], previous[1], TRUE);
3241  #endif  #endif
3242    
3243    default:      default:
3244    return FALSE;      return FALSE;
3245        }
3246    }    }
3247    
   
3248  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3249  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3250  generated only when PCRE_UCP is *not* set, that is, when only ASCII  generated only when PCRE_UCP is *not* set, that is, when only ASCII
# Line 3219  switch(op_code) Line 3255  switch(op_code)
3255    {    {
3256    case OP_CHAR:    case OP_CHAR:
3257    case OP_CHARI:    case OP_CHARI:
 #ifdef SUPPORT_UTF  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
3258    switch(-next)    switch(-next)
3259      {      {
3260      case ESC_d:      case ESC_d:
# Line 3248  switch(op_code) Line 3279  switch(op_code)
3279      case ESC_H:      case ESC_H:
3280      switch(c)      switch(c)
3281        {        {
3282        case 0x09:        HSPACE_CASES:
       case 0x20:  
       case 0xa0:  
       case 0x1680:  
       case 0x180e:  
       case 0x2000:  
       case 0x2001:  
       case 0x2002:  
       case 0x2003:  
       case 0x2004:  
       case 0x2005:  
       case 0x2006:  
       case 0x2007:  
       case 0x2008:  
       case 0x2009:  
       case 0x200A:  
       case 0x202f:  
       case 0x205f:  
       case 0x3000:  
3283        return -next != ESC_h;        return -next != ESC_h;
3284    
3285        default:        default:
3286        return -next == ESC_h;        return -next == ESC_h;
3287        }        }
# Line 3276  switch(op_code) Line 3290  switch(op_code)
3290      case ESC_V:      case ESC_V:
3291      switch(c)      switch(c)
3292        {        {
3293        case 0x0a:        VSPACE_CASES:
       case 0x0b:  
       case 0x0c:  
       case 0x0d:  
       case 0x85:  
       case 0x2028:  
       case 0x2029:  
3294        return -next != ESC_v;        return -next != ESC_v;
3295    
3296        default:        default:
3297        return -next == ESC_v;        return -next == ESC_v;
3298        }        }
# Line 3388  switch(op_code) Line 3397  switch(op_code)
3397    
3398    
3399  /*************************************************  /*************************************************
3400    *        Add a character or range to a class     *
3401    *************************************************/
3402    
3403    /* This function packages up the logic of adding a character or range of
3404    characters to a class. The character values in the arguments will be within the
3405    valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3406    mutually recursive with the function immediately below.
3407    
3408    Arguments:
3409      classbits     the bit map for characters < 256
3410      uchardptr     points to the pointer for extra data
3411      options       the options word
3412      cd            contains pointers to tables etc.
3413      start         start of range character
3414      end           end of range character
3415    
3416    Returns:        the number of < 256 characters added
3417                    the pointer to extra data is updated
3418    */
3419    
3420    static int
3421    add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3422      compile_data *cd, unsigned int start, unsigned int end)
3423    {
3424    unsigned int c;
3425    int n8 = 0;
3426    
3427    /* If caseless matching is required, scan the range and process alternate
3428    cases. In Unicode, there are 8-bit characters that have alternate cases that
3429    are greater than 255 and vice-versa. Sometimes we can just extend the original
3430    range. */
3431    
3432    if ((options & PCRE_CASELESS) != 0)
3433      {
3434    #ifdef SUPPORT_UCP
3435      if ((options & PCRE_UTF8) != 0)
3436        {
3437        int rc;
3438        unsigned int oc, od;
3439    
3440        options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3441        c = start;
3442    
3443        while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3444          {
3445          /* Handle a single character that has more than one other case. */
3446    
3447          if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3448            PRIV(ucd_caseless_sets) + rc, oc);
3449    
3450          /* Do nothing if the other case range is within the original range. */
3451    
3452          else if (oc >= start && od <= end) continue;
3453    
3454          /* Extend the original range if there is overlap, noting that if oc < c, we
3455          can't have od > end because a subrange is always shorter than the basic
3456          range. Otherwise, use a recursive call to add the additional range. */
3457    
3458          else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3459          else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3460          else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3461          }
3462        }
3463      else
3464    #endif  /* SUPPORT_UCP */
3465    
3466      /* Not UTF-mode, or no UCP */
3467    
3468      for (c = start; c <= end && c < 256; c++)
3469        {
3470        SETBIT(classbits, cd->fcc[c]);
3471        n8++;
3472        }
3473      }
3474    
3475    /* Now handle the original range. Adjust the final value according to the bit
3476    length - this means that the same lists of (e.g.) horizontal spaces can be used
3477    in all cases. */
3478    
3479    #ifdef COMPILE_PCRE8
3480    #ifdef SUPPORT_UTF
3481      if ((options & PCRE_UTF8) == 0)
3482    #endif
3483      if (end > 0xff) end = 0xff;
3484    #endif
3485    
3486    #ifdef COMPILE_PCRE16
3487    #ifdef SUPPORT_UTF
3488      if ((options & PCRE_UTF16) == 0)
3489    #endif
3490      if (end > 0xffff) end = 0xffff;
3491    #endif
3492    
3493    /* If all characters are less than 256, use the bit map. Otherwise use extra
3494    data. */
3495    
3496    if (end < 0x100)
3497      {
3498      for (c = start; c <= end; c++)
3499        {
3500        n8++;
3501        SETBIT(classbits, c);
3502        }
3503      }
3504    
3505    else
3506      {
3507      pcre_uchar *uchardata = *uchardptr;
3508    
3509    #ifdef SUPPORT_UTF
3510      if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3511        {
3512        if (start < end)
3513          {
3514          *uchardata++ = XCL_RANGE;
3515          uchardata += PRIV(ord2utf)(start, uchardata);
3516          uchardata += PRIV(ord2utf)(end, uchardata);
3517          }
3518        else if (start == end)
3519          {
3520          *uchardata++ = XCL_SINGLE;
3521          uchardata += PRIV(ord2utf)(start, uchardata);
3522          }
3523        }
3524      else
3525    #endif  /* SUPPORT_UTF */
3526    
3527      /* Without UTF support, character values are constrained by the bit length,
3528      and can only be > 256 for 16-bit and 32-bit libraries. */
3529    
3530    #ifdef COMPILE_PCRE8
3531        {}
3532    #else
3533      if (start < end)
3534        {
3535        *uchardata++ = XCL_RANGE;
3536        *uchardata++ = start;
3537        *uchardata++ = end;
3538        }
3539      else if (start == end)
3540        {
3541        *uchardata++ = XCL_SINGLE;
3542        *uchardata++ = start;
3543        }
3544    #endif
3545    
3546      *uchardptr = uchardata;   /* Updata extra data pointer */
3547      }
3548    
3549    return n8;    /* Number of 8-bit characters */
3550    }
3551    
3552    
3553    
3554    
3555    /*************************************************
3556    *        Add a list of characters to a class     *
3557    *************************************************/
3558    
3559    /* This function is used for adding a list of case-equivalent characters to a
3560    class, and also for adding a list of horizontal or vertical whitespace. If the
3561    list is in order (which it should be), ranges of characters are detected and
3562    handled appropriately. This function is mutually recursive with the function
3563    above.
3564    
3565    Arguments:
3566      classbits     the bit map for characters < 256
3567      uchardptr     points to the pointer for extra data
3568      options       the options word
3569      cd            contains pointers to tables etc.
3570      p             points to row of 32-bit values, terminated by NOTACHAR
3571      except        character to omit; this is used when adding lists of
3572                      case-equivalent characters to avoid including the one we
3573                      already know about
3574    
3575    Returns:        the number of < 256 characters added
3576                    the pointer to extra data is updated
3577    */
3578    
3579    static int
3580    add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3581      compile_data *cd, const pcre_uint32 *p, unsigned int except)
3582    {
3583    int n8 = 0;
3584    while (p[0] < NOTACHAR)
3585      {
3586      int n = 0;
3587      if (p[0] != except)
3588        {
3589        while(p[n+1] == p[0] + n + 1) n++;
3590        n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3591        }
3592      p += n + 1;
3593      }
3594    return n8;
3595    }
3596    
3597    
3598    
3599    /*************************************************
3600    *    Add characters not in a list to a class     *
3601    *************************************************/
3602    
3603    /* This function is used for adding the complement of a list of horizontal or
3604    vertical whitespace to a class. The list must be in order.
3605    
3606    Arguments:
3607      classbits     the bit map for characters < 256
3608      uchardptr     points to the pointer for extra data
3609      options       the options word
3610      cd            contains pointers to tables etc.
3611      p             points to row of 32-bit values, terminated by NOTACHAR
3612    
3613    Returns:        the number of < 256 characters added
3614                    the pointer to extra data is updated
3615    */
3616    
3617    static int
3618    add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3619      int options, compile_data *cd, const pcre_uint32 *p)
3620    {
3621    int n8 = 0;
3622    if (p[0] > 0)
3623      n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
3624    while (p[0] < NOTACHAR)
3625      {
3626      while (p[1] == p[0] + 1) p++;
3627      n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3628        (p[1] == NOTACHAR)? 0x10ffff : p[1] - 1);
3629      p++;
3630      }
3631    return n8;
3632    }
3633    
3634    
3635    
3636    /*************************************************
3637  *           Compile one branch                   *  *           Compile one branch                   *
3638  *************************************************/  *************************************************/
3639    
# Line 3457  pcre_uchar utf_chars[6]; Line 3703  pcre_uchar utf_chars[6];
3703  BOOL utf = FALSE;  BOOL utf = FALSE;
3704  #endif  #endif
3705    
3706  /* Helper variables for OP_XCLASS opcode (for characters > 255). */  /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
3707    class_uchardata always so that it can be passed to add_to_class() always,
3708    though it will not be used in non-UTF 8-bit cases. This avoids having to supply
3709    alternative calls for the different cases. */
3710    
3711    pcre_uchar *class_uchardata;
3712  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3713  BOOL xclass;  BOOL xclass;
 pcre_uchar *class_uchardata;  
3714  pcre_uchar *class_uchardata_base;  pcre_uchar *class_uchardata_base;
3715  #endif  #endif
3716    
# Line 3505  for (;; ptr++) Line 3754  for (;; ptr++)
3754    BOOL is_recurse;    BOOL is_recurse;
3755    BOOL reset_bracount;    BOOL reset_bracount;
3756    int class_has_8bitchar;    int class_has_8bitchar;
3757    int class_single_char;    int class_one_char;
3758    int newoptions;    int newoptions;
3759    int recno;    int recno;
3760    int refsign;    int refsign;
# Line 3803  for (;; ptr++) Line 4052  for (;; ptr++)
4052    
4053      should_flip_negation = FALSE;      should_flip_negation = FALSE;
4054    
4055      /* For optimization purposes, we track some properties of the class.      /* For optimization purposes, we track some properties of the class:
4056      class_has_8bitchar will be non-zero, if the class contains at least one      class_has_8bitchar will be non-zero if the class contains at least one <
4057      < 256 character. class_single_char will be 1 if the class contains only      256 character; class_one_char will be 1 if the class contains just one
4058      a single character. */      character. */
4059    
4060      class_has_8bitchar = 0;      class_has_8bitchar = 0;
4061      class_single_char = 0;      class_one_char = 0;
4062    
4063      /* Initialize the 32-char bit map to all zeros. We build the map in a      /* Initialize the 32-char bit map to all zeros. We build the map in a
4064      temporary bit of memory, in case the class contains only 1 character (less      temporary bit of memory, in case the class contains fewer than two
4065      than 256), because in that case the compiled code doesn't use the bit map.      8-bit characters because in that case the compiled code doesn't use the bit
4066      */      map. */
4067    
4068      memset(classbits, 0, 32 * sizeof(pcre_uint8));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
4069    
4070  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4071      xclass = FALSE;                           /* No chars >= 256 */      xclass = FALSE;
4072      class_uchardata = code + LINK_SIZE + 2;   /* For UTF-8 items */      class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */
4073      class_uchardata_base = class_uchardata;   /* For resetting in pass 1 */      class_uchardata_base = class_uchardata;   /* Save the start */
4074  #endif  #endif
4075    
4076      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
# Line 3843  for (;; ptr++) Line 4092  for (;; ptr++)
4092        /* In the pre-compile phase, accumulate the length of any extra        /* In the pre-compile phase, accumulate the length of any extra
4093        data and reset the pointer. This is so that very large classes that        data and reset the pointer. This is so that very large classes that
4094        contain a zillion > 255 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
4095        (which is on the stack). */        (which is on the stack). We have to remember that there was XCLASS data,
4096          however. */
4097    
4098        if (lengthptr != NULL)        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
4099          {          {
4100            xclass = TRUE;
4101          *lengthptr += class_uchardata - class_uchardata_base;          *lengthptr += class_uchardata - class_uchardata_base;
4102          class_uchardata = class_uchardata_base;          class_uchardata = class_uchardata_base;
4103          }          }
# Line 3905  for (;; ptr++) Line 4156  for (;; ptr++)
4156          alpha. This relies on the fact that the class table starts with          alpha. This relies on the fact that the class table starts with
4157          alpha, lower, upper as the first 3 entries. */          alpha, lower, upper as the first 3 entries. */
4158    
4159          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
4160            posix_class = 0;            posix_class = 0;
4161    
4162          /* When PCRE_UCP is set, some of the POSIX classes are converted to          /* When PCRE_UCP is set, some of the POSIX classes are converted to
# Line 3948  for (;; ptr++) Line 4199  for (;; ptr++)
4199              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
4200            }            }
4201    
4202          /* Not see if we need to remove any special characters. An option          /* Now see if we need to remove any special characters. An option
4203          value of 1 removes vertical space and 2 removes underscore. */          value of 1 removes vertical space and 2 removes underscore. */
4204    
4205          if (tabopt < 0) tabopt = -tabopt;          if (tabopt < 0) tabopt = -tabopt;
# Line 3964  for (;; ptr++) Line 4215  for (;; ptr++)
4215            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
4216    
4217          ptr = tempptr + 1;          ptr = tempptr + 1;
4218          /* Every class contains at least one < 256 characters. */          /* Every class contains at least one < 256 character. */
4219          class_has_8bitchar = 1;          class_has_8bitchar = 1;
4220          /* Every class contains at least two characters. */          /* Every class contains at least two characters. */
4221          class_single_char = 2;          class_one_char = 2;
4222          continue;    /* End of POSIX syntax handling */          continue;    /* End of POSIX syntax handling */
4223          }          }
4224    
# Line 3975  for (;; ptr++) Line 4226  for (;; ptr++)
4226        of the specials, which just set a flag. The sequence \b is a special        of the specials, which just set a flag. The sequence \b is a special
4227        case. Inside a class (and only there) it is treated as backspace. We        case. Inside a class (and only there) it is treated as backspace. We
4228        assume that other escapes have more than one character in them, so        assume that other escapes have more than one character in them, so
4229        speculatively set both class_has_8bitchar and class_single_char bigger        speculatively set both class_has_8bitchar and class_one_char bigger
4230        than one. Unrecognized escapes fall through and are either treated        than one. Unrecognized escapes fall through and are either treated
4231        as literal characters (by default), or are faulted if        as literal characters (by default), or are faulted if
4232        PCRE_EXTRA is set. */        PCRE_EXTRA is set. */
# Line 4008  for (;; ptr++) Line 4259  for (;; ptr++)
4259            /* Every class contains at least two < 256 characters. */            /* Every class contains at least two < 256 characters. */
4260            class_has_8bitchar++;            class_has_8bitchar++;
4261            /* Every class contains at least two characters. */            /* Every class contains at least two characters. */
4262            class_single_char += 2;            class_one_char += 2;
4263    
4264            switch (-c)            switch (-c)
4265              {              {
# Line 4044  for (;; ptr++) Line 4295  for (;; ptr++)
4295    
4296              /* Perl 5.004 onwards omits VT from \s, but we must preserve it              /* Perl 5.004 onwards omits VT from \s, but we must preserve it
4297              if it was previously set by something earlier in the character              if it was previously set by something earlier in the character
4298              class. */              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and
4299                EBCDIC, so we lazily just adjust the appropriate bit. */
4300    
4301              case ESC_s:              case ESC_s:
4302              classbits[0] |= cbits[cbit_space];              classbits[0] |= cbits[cbit_space];
# Line 4057  for (;; ptr++) Line 4309  for (;; ptr++)
4309              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
4310              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
4311              continue;              continue;
4312    
4313                /* The rest apply in both UCP and non-UCP cases. */
4314    
4315              case ESC_h:              case ESC_h:
4316              SETBIT(classbits, 0x09); /* VT */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4317              SETBIT(classbits, 0x20); /* SPACE */                PRIV(hspace_list), NOTACHAR);
             SETBIT(classbits, 0xa0); /* NSBP */  
 #ifndef COMPILE_PCRE8  
             xclass = TRUE;  
             *class_uchardata++ = XCL_SINGLE;  
             *class_uchardata++ = 0x1680;  
             *class_uchardata++ = XCL_SINGLE;  
             *class_uchardata++ = 0x180e;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x2000;  
             *class_uchardata++ = 0x200a;  
             *class_uchardata++ = XCL_SINGLE;  
             *class_uchardata++ = 0x202f;  
             *class_uchardata++ = XCL_SINGLE;  
             *class_uchardata++ = 0x205f;  
             *class_uchardata++ = XCL_SINGLE;  
             *class_uchardata++ = 0x3000;  
 #elif defined SUPPORT_UTF  
             if (utf)  
               {  
               xclass = TRUE;  
               *class_uchardata++ = XCL_SINGLE;  
               class_uchardata += PRIV(ord2utf)(0x1680, class_uchardata);  
               *class_uchardata++ = XCL_SINGLE;  
               class_uchardata += PRIV(ord2utf)(0x180e, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x2000, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x200a, class_uchardata);  
               *class_uchardata++ = XCL_SINGLE;  
               class_uchardata += PRIV(ord2utf)(0x202f, class_uchardata);  
               *class_uchardata++ = XCL_SINGLE;  
               class_uchardata += PRIV(ord2utf)(0x205f, class_uchardata);  
               *class_uchardata++ = XCL_SINGLE;  
               class_uchardata += PRIV(ord2utf)(0x3000, class_uchardata);  
               }  
 #endif  
4318              continue;              continue;
4319    
4320              case ESC_H:              case ESC_H:
4321              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4322                {                cd, PRIV(hspace_list));
               int x = 0xff;  
               switch (c)  
                 {  
                 case 0x09/8: x ^= 1 << (0x09%8); break;  
                 case 0x20/8: x ^= 1 << (0x20%8); break;  
                 case 0xa0/8: x ^= 1 << (0xa0%8); break;  
                 default: break;  
                 }  
               classbits[c] |= x;  
               }  
 #ifndef COMPILE_PCRE8  
             xclass = TRUE;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x0100;  
             *class_uchardata++ = 0x167f;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x1681;  
             *class_uchardata++ = 0x180d;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x180f;  
             *class_uchardata++ = 0x1fff;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x200b;  
             *class_uchardata++ = 0x202e;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x2030;  
             *class_uchardata++ = 0x205e;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x2060;  
             *class_uchardata++ = 0x2fff;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x3001;  
 #ifdef SUPPORT_UTF  
             if (utf)  
               class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);  
             else  
 #endif  
               *class_uchardata++ = 0xffff;  
 #elif defined SUPPORT_UTF  
             if (utf)  
               {  
               xclass = TRUE;  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x0100, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x167f, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x1681, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x180d, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x180f, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x1fff, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x200b, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x202e, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x2030, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x205e, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x2060, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x2fff, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x3001, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);  
               }  
 #endif  
4323              continue;              continue;
4324    
4325              case ESC_v:              case ESC_v:
4326              SETBIT(classbits, 0x0a); /* LF */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4327              SETBIT(classbits, 0x0b); /* VT */                PRIV(vspace_list), NOTACHAR);
             SETBIT(classbits, 0x0c); /* FF */  
             SETBIT(classbits, 0x0d); /* CR */  
             SETBIT(classbits, 0x85); /* NEL */  
 #ifndef COMPILE_PCRE8  
             xclass = TRUE;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x2028;  
             *class_uchardata++ = 0x2029;  
 #elif defined SUPPORT_UTF  
             if (utf)  
               {  
               xclass = TRUE;  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x2028, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x2029, class_uchardata);  
               }  
 #endif  
4328              continue;              continue;
4329    
4330              case ESC_V:              case ESC_V:
4331              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4332                {                cd, PRIV(vspace_list));
               int x = 0xff;  
               switch (c)  
                 {  
                 case 0x0a/8: x ^= 1 << (0x0a%8);  
                              x ^= 1 << (0x0b%8);  
                              x ^= 1 << (0x0c%8);  
                              x ^= 1 << (0x0d%8);  
                              break;  
                 case 0x85/8: x ^= 1 << (0x85%8); break;  
                 default: break;  
                 }  
               classbits[c] |= x;  
               }  
   
 #ifndef COMPILE_PCRE8  
             xclass = TRUE;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x0100;  
             *class_uchardata++ = 0x2027;  
             *class_uchardata++ = XCL_RANGE;  
             *class_uchardata++ = 0x202a;  
 #ifdef SUPPORT_UTF  
             if (utf)  
               class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);  
             else  
 #endif  
               *class_uchardata++ = 0xffff;  
 #elif defined SUPPORT_UTF  
             if (utf)  
               {  
               xclass = TRUE;  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x0100, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x2027, class_uchardata);  
               *class_uchardata++ = XCL_RANGE;  
               class_uchardata += PRIV(ord2utf)(0x202a, class_uchardata);  
               class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);  
               }  
 #endif  
4333              continue;              continue;
4334    
4335  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 4242  for (;; ptr++) Line 4340  for (;; ptr++)
4340                int pdata;                int pdata;
4341                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
4342                if (ptype < 0) goto FAILED;                if (ptype < 0) goto FAILED;
               xclass = TRUE;  
4343                *class_uchardata++ = ((-c == ESC_p) != negated)?                *class_uchardata++ = ((-c == ESC_p) != negated)?
4344                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
4345                *class_uchardata++ = ptype;                *class_uchardata++ = ptype;
# Line 4262  for (;; ptr++) Line 4359  for (;; ptr++)
4359                goto FAILED;                goto FAILED;
4360                }                }
4361              class_has_8bitchar--;    /* Undo the speculative increase. */              class_has_8bitchar--;    /* Undo the speculative increase. */
4362              class_single_char -= 2;  /* Undo the speculative increase. */              class_one_char -= 2;     /* Undo the speculative increase. */
4363              c = *ptr;                /* Get the final character and fall through */              c = *ptr;                /* Get the final character and fall through */
4364              break;              break;
4365              }              }
4366            }            }
4367    
4368          /* Fall through if we have a single character (c >= 0). This may be          /* Fall through if the escape just defined a single character (c >= 0).
4369          greater than 256. */          This may be greater than 256. */
4370    
4371          }   /* End of backslash handling */          }   /* End of backslash handling */
4372    
4373        /* A single character may be followed by '-' to form a range. However,        /* A character may be followed by '-' to form a range. However, Perl does
4374        Perl does not permit ']' to be the end of the range. A '-' character        not permit ']' to be the end of the range. A '-' character at the end is
4375        at the end is treated as a literal. Perl ignores orphaned \E sequences        treated as a literal. Perl ignores orphaned \E sequences entirely. The
4376        entirely. The code for handling \Q and \E is messy. */        code for handling \Q and \E is messy. */
4377    
4378        CHECK_RANGE:        CHECK_RANGE:
4379        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 4284  for (;; ptr++) Line 4381  for (;; ptr++)
4381          inescq = FALSE;          inescq = FALSE;
4382          ptr += 2;          ptr += 2;
4383          }          }
   
4384        oldptr = ptr;        oldptr = ptr;
4385    
4386        /* Remember \r or \n */        /* Remember if \r or \n were explicitly used */
4387    
4388        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4389    
# Line 4310  for (;; ptr++) Line 4406  for (;; ptr++)
4406            inescq = TRUE;            inescq = TRUE;
4407            break;            break;
4408            }            }
4409    
4410            /* Minus (hyphen) at the end of a class is treated as a literal, so put
4411            back the pointer and jump to handle the character that preceded it. */
4412    
4413          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4414            {            {
4415            ptr = oldptr;            ptr = oldptr;
4416            goto LONE_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4417            }            }
4418    
4419            /* Otherwise, we have a potential range; pick up the next character */
4420    
4421  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4422          if (utf)          if (utf)
# Line 4335  for (;; ptr++) Line 4436  for (;; ptr++)
4436            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
4437            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
4438    
4439            /* \b is backspace; any other special means the '-' was literal */            /* \b is backspace; any other special means the '-' was literal. */
4440    
4441            if (d < 0)            if (d < 0)
4442              {              {
4443              if (d == -ESC_b) d = CHAR_BS; else              if (d == -ESC_b) d = CHAR_BS; else
4444                {                {
4445                ptr = oldptr;                ptr = oldptr;
4446                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4447                }                }
4448              }              }
4449            }            }
4450    
4451          /* Check that the two values are in the correct order. Optimize          /* Check that the two values are in the correct order. Optimize
4452          one-character ranges */          one-character ranges. */
4453    
4454          if (d < c)          if (d < c)
4455            {            {
4456            *errorcodeptr = ERR8;            *errorcodeptr = ERR8;
4457            goto FAILED;            goto FAILED;
4458            }            }
4459            if (d == c) goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4460    
4461          if (d == c) goto LONE_SINGLE_CHARACTER;  /* A few lines below */          /* We have found a character range, so single character optimizations
4462            cannot be done anymore. Any value greater than 1 indicates that there
4463            is more than one character. */
4464    
4465            class_one_char = 2;
4466    
4467          /* Remember \r or \n */          /* Remember an explicit \r or \n, and add the range to the class. */
4468    
4469          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4470    
4471          /* Since we found a character range, single character optimizations          class_has_8bitchar +=
4472          cannot be done anymore. */            add_to_class(classbits, &class_uchardata, options, cd, c, d);
4473          class_single_char = 2;  
   
         /* In UTF-8 mode, if the upper limit is > 255, or > 127 for caseless  
         matching, we have to use an XCLASS with extra data items. Caseless  
         matching for characters > 127 is available only if UCP support is  
         available. */  
   
 #if defined SUPPORT_UTF && !(defined COMPILE_PCRE8)  
         if ((d > 255) || (utf && ((options & PCRE_CASELESS) != 0 && d > 127)))  
 #elif defined  SUPPORT_UTF  
         if (utf && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127)))  
 #elif !(defined COMPILE_PCRE8)  
         if (d > 255)  
 #endif  
 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)  
           {  
           xclass = TRUE;  
   
           /* With UCP support, we can find the other case equivalents of  
           the relevant characters. There may be several ranges. Optimize how  
           they fit with the basic range. */  
   
 #ifdef SUPPORT_UCP  
 #ifndef COMPILE_PCRE8  
           if (utf && (options & PCRE_CASELESS) != 0)  
 #else  
           if ((options & PCRE_CASELESS) != 0)  
 #endif  
             {  
             unsigned int occ, ocd;  
             unsigned int cc = c;  
             unsigned int origd = d;  
             while (get_othercase_range(&cc, origd, &occ, &ocd))  
               {  
               if (occ >= (unsigned int)c &&  
                   ocd <= (unsigned int)d)  
                 continue;                          /* Skip embedded ranges */  
   
               if (occ < (unsigned int)c  &&  
                   ocd >= (unsigned int)c - 1)      /* Extend the basic range */  
                 {                                  /* if there is overlap,   */  
                 c = occ;                           /* noting that if occ < c */  
                 continue;                          /* we can't have ocd > d  */  
                 }                                  /* because a subrange is  */  
               if (ocd > (unsigned int)d &&  
                   occ <= (unsigned int)d + 1)      /* always shorter than    */  
                 {                                  /* the basic range.       */  
                 d = ocd;  
                 continue;  
                 }  
   
               if (occ == ocd)  
                 {  
                 *class_uchardata++ = XCL_SINGLE;  
                 }  
               else  
                 {  
                 *class_uchardata++ = XCL_RANGE;  
                 class_uchardata += PRIV(ord2utf)(occ, class_uchardata);  
                 }  
               class_uchardata += PRIV(ord2utf)(ocd, class_uchardata);  
               }  
             }  
 #endif  /* SUPPORT_UCP */  
   
           /* Now record the original range, possibly modified for UCP caseless  
           overlapping ranges. */  
   
           *class_uchardata++ = XCL_RANGE;  
 #ifdef SUPPORT_UTF  
 #ifndef COMPILE_PCRE8  
           if (utf)  
             {  
             class_uchardata += PRIV(ord2utf)(c, class_uchardata);  
             class_uchardata += PRIV(ord2utf)(d, class_uchardata);  
             }  
           else  
             {  
             *class_uchardata++ = c;  
             *class_uchardata++ = d;  
             }  
 #else  
           class_uchardata += PRIV(ord2utf)(c, class_uchardata);  
           class_uchardata += PRIV(ord2utf)(d, class_uchardata);  
 #endif  
 #else /* SUPPORT_UTF */  
           *class_uchardata++ = c;  
           *class_uchardata++ = d;  
 #endif /* SUPPORT_UTF */  
   
           /* With UCP support, we are done. Without UCP support, there is no  
           caseless matching for UTF characters > 127; we can use the bit map  
           for the smaller ones. As for 16 bit characters without UTF, we  
           can still use  */  
   
 #ifdef SUPPORT_UCP  
 #ifndef COMPILE_PCRE8  
           if (utf)  
 #endif  
             continue;    /* With next character in the class */  
 #endif  /* SUPPORT_UCP */  
   
 #if defined SUPPORT_UTF && !defined(SUPPORT_UCP) && !(defined COMPILE_PCRE8)  
           if (utf)  
             {  
             if ((options & PCRE_CASELESS) == 0 || c > 127) continue;  
             /* Adjust upper limit and fall through to set up the map */  
             d = 127;  
             }  
           else  
             {  
             if (c > 255) continue;  
             /* Adjust upper limit and fall through to set up the map */  
             d = 255;  
             }  
 #elif defined SUPPORT_UTF && !defined(SUPPORT_UCP)  
           if ((options & PCRE_CASELESS) == 0 || c > 127) continue;  
           /* Adjust upper limit and fall through to set up the map */  
           d = 127;  
 #else  
           if (c > 255) continue;  
           /* Adjust upper limit and fall through to set up the map */  
           d = 255;  
 #endif  /* SUPPORT_UTF && !SUPPORT_UCP && !COMPILE_PCRE8 */  
           }  
 #endif  /* SUPPORT_UTF || !COMPILE_PCRE8 */  
   
         /* We use the bit map for 8 bit mode, or when the characters fall  
         partially or entirely to [0-255] ([0-127] for UCP) ranges. */  
   
         class_has_8bitchar = 1;  
   
         /* We can save a bit of time by skipping this in the pre-compile. */  
   
         if (lengthptr == NULL) for (; c <= d; c++)  
           {  
           classbits[c/8] |= (1 << (c&7));  
           if ((options & PCRE_CASELESS) != 0)  
             {  
             int uc = cd->fcc[c]; /* flip case */  
             classbits[uc/8] |= (1 << (uc&7));  
             }  
           }  
   
4474          continue;   /* Go get the next char in the class */          continue;   /* Go get the next char in the class */
4475          }          }
4476    
4477        /* Handle a lone single character - we can get here for a normal        /* Handle a single character - we can get here for a normal non-escape
4478        non-escape char, or after \ that introduces a single character or for an        char, or after \ that introduces a single character or for an apparent
4479        apparent range that isn't. */        range that isn't. Only the value 1 matters for class_one_char, so don't
4480          increase it if it is already 2 or more ... just in case there's a class
4481        LONE_SINGLE_CHARACTER:        with a zillion characters in it. */
4482    
4483          CLASS_SINGLE_CHARACTER:
4484          if (class_one_char < 2) class_one_char++;
4485    
4486          /* If class_one_char is 1, we have the first single character in the
4487          class, and there have been no prior ranges, or XCLASS items generated by
4488          escapes. If this is the final character in the class, we can optimize by
4489          turning the item into a 1-character OP_CHAR[I] if it's positive, or
4490          OP_NOT[I] if it's negative. In the positive case, it can cause firstchar
4491          to be set. Otherwise, there can be no first char if this item is first,
4492          whatever repeat count may follow. In the case of reqchar, save the
4493          previous value for reinstating. */
4494    
4495        /* Only the value of 1 matters for class_single_char. */        if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
   
       if (class_single_char < 2) class_single_char++;  
   
       /* If class_charcount is 1, we saw precisely one character. As long as  
       there was no use of \p or \P, in other words, no use of any XCLASS  
       features, we can optimize.  
   
       The optimization throws away the bit map. We turn the item into a  
       1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.  
       In the positive case, it can cause firstchar to be set. Otherwise, there  
       can be no first char if this item is first, whatever repeat count may  
       follow. In the case of reqchar, save the previous value for reinstating. */  
   
       if (class_single_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)  
4496          {          {
4497          ptr++;          ptr++;
4498          zeroreqchar = reqchar;          zeroreqchar = reqchar;
4499    
4500          if (negate_class)          if (negate_class)
4501            {            {
4502    #ifdef SUPPORT_UCP
4503              int d;
4504    #endif
4505            if (firstchar == REQ_UNSET) firstchar = REQ_NONE;            if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4506            zerofirstchar = firstchar;            zerofirstchar = firstchar;
4507            *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;  
4508              /* For caseless UTF-8 mode when UCP support is available, check
4509              whether this character has more than one other case. If so, generate
4510              a special OP_NOTPROP item instead of OP_NOTI. */
4511    
4512    #ifdef SUPPORT_UCP
4513              if (utf && (options & PCRE_CASELESS) != 0 &&
4514                  (d = UCD_CASESET(c)) != 0)
4515                {
4516                *code++ = OP_NOTPROP;
4517                *code++ = PT_CLIST;
4518                *code++ = d;
4519                }
4520              else
4521    #endif
4522              /* Char has only one other case, or UCP not available */
4523    
4524                {
4525                *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4526  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4527            if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)              if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4528              code += PRIV(ord2utf)(c, code);                code += PRIV(ord2utf)(c, code);
4529            else              else
4530  #endif  #endif
4531              *code++ = c;                *code++ = c;
4532            goto NOT_CHAR;              }
4533    
4534              /* We are finished with this character class */
4535    
4536              goto END_CLASS;
4537            }            }
4538    
4539          /* For a single, positive character, get the value into mcbuffer, and          /* For a single, positive character, get the value into mcbuffer, and
# Line 4564  for (;; ptr++) Line 4550  for (;; ptr++)
4550            }            }
4551          goto ONE_CHAR;          goto ONE_CHAR;
4552          }       /* End of 1-char optimization */          }       /* End of 1-char optimization */
4553    
4554        /* Handle a character that cannot go in the bit map. */        /* There is more than one character in the class, or an XCLASS item
4555          has been generated. Add this character to the class. */
4556  #if defined SUPPORT_UTF && !(defined COMPILE_PCRE8)  
4557        if ((c > 255) || (utf && ((options & PCRE_CASELESS) != 0 && c > 127)))        class_has_8bitchar +=
4558  #elif defined SUPPORT_UTF          add_to_class(classbits, &class_uchardata, options, cd, c, c);
       if (utf && (c > 255 || ((options & PCRE_CASELESS) != 0 && c > 127)))  
 #elif !(defined COMPILE_PCRE8)  
       if (c > 255)  
 #endif  
   
 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)  
         {  
         xclass = TRUE;  
         *class_uchardata++ = XCL_SINGLE;  
 #ifdef SUPPORT_UTF  
 #ifndef COMPILE_PCRE8  
         /* In non 8 bit mode, we can get here even if we are not in UTF mode. */  
         if (!utf)  
           *class_uchardata++ = c;  
         else  
 #endif  
           class_uchardata += PRIV(ord2utf)(c, class_uchardata);  
 #else /* SUPPORT_UTF */  
         *class_uchardata++ = c;  
 #endif /* SUPPORT_UTF */  
   
 #ifdef SUPPORT_UCP  
 #ifdef COMPILE_PCRE8  
         if ((options & PCRE_CASELESS) != 0)  
 #else  
         /* In non 8 bit mode, we can get here even if we are not in UTF mode. */  
         if (utf && (options & PCRE_CASELESS) != 0)  
 #endif  
           {  
           unsigned int othercase;  
           if ((int)(othercase = UCD_OTHERCASE(c)) != c)  
             {  
             *class_uchardata++ = XCL_SINGLE;  
             class_uchardata += PRIV(ord2utf)(othercase, class_uchardata);  
             }  
           }  
 #endif  /* SUPPORT_UCP */  
   
         }  
       else  
 #endif  /* SUPPORT_UTF || COMPILE_PCRE16 */  
   
       /* Handle a single-byte character */  
         {  
         class_has_8bitchar = 1;  
         classbits[c/8] |= (1 << (c&7));  
         if ((options & PCRE_CASELESS) != 0)  
           {  
           c = cd->fcc[c]; /* flip case */  
           classbits[c/8] |= (1 << (c&7));  
           }  
         }  
4559        }        }
4560    
4561      /* Loop until ']' reached. This "while" is the end of the "do" far above.      /* Loop until ']' reached. This "while" is the end of the "do" far above.
# Line 4641  for (;; ptr++) Line 4575  for (;; ptr++)
4575        goto FAILED;        goto FAILED;
4576        }        }
4577    
4578        /* We will need an XCLASS if data has been placed in class_uchardata. In
4579        the second phase this is a sufficient test. However, in the pre-compile
4580        phase, class_uchardata gets emptied to prevent workspace overflow, so it
4581        only if the very last character in the class needs XCLASS will it contain
4582        anything at this point. For this reason, xclass gets set TRUE above when
4583        uchar_classdata is emptied, and that's why this code is the way it is here
4584        instead of just doing a test on class_uchardata below. */
4585    
4586    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4587        if (class_uchardata > class_uchardata_base) xclass = TRUE;
4588    #endif
4589    
4590      /* If this is the first thing in the branch, there can be no first char      /* If this is the first thing in the branch, there can be no first char
4591      setting, whatever the repeat count. Any reqchar setting must remain      setting, whatever the repeat count. Any reqchar setting must remain
4592      unchanged after any kind of repeat. */      unchanged after any kind of repeat. */
# Line 4703  for (;; ptr++) Line 4649  for (;; ptr++)
4649        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
4650        }        }
4651      code += 32 / sizeof(pcre_uchar);      code += 32 / sizeof(pcre_uchar);
4652      NOT_CHAR:  
4653        END_CLASS:
4654      break;      break;
4655    
4656    
# Line 4813  for (;; ptr++) Line 4760  for (;; ptr++)
4760      if (*previous == OP_CHAR || *previous == OP_CHARI      if (*previous == OP_CHAR || *previous == OP_CHARI
4761          || *previous == OP_NOT || *previous == OP_NOTI)          || *previous == OP_NOT || *previous == OP_NOTI)
4762        {        {
4763        switch (*previous)        switch (*previous)
4764          {          {
4765          default: /* Make compiler happy. */          default: /* Make compiler happy. */
4766          case OP_CHAR:  op_type = OP_STAR - OP_STAR; break;          case OP_CHAR:  op_type = OP_STAR - OP_STAR; break;
# Line 5593  for (;; ptr++) Line 5540  for (;; ptr++)
5540        ptr++;        ptr++;
5541        while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;        while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;
5542        namelen = (int)(ptr - name);        namelen = (int)(ptr - name);
5543    
5544        /* It appears that Perl allows any characters whatsoever, other than        /* It appears that Perl allows any characters whatsoever, other than
5545        a closing parenthesis, to appear in arguments, so we no longer insist on        a closing parenthesis, to appear in arguments, so we no longer insist on
5546        letters, digits, and underscores. */        letters, digits, and underscores. */
# Line 5607  for (;; ptr++) Line 5554  for (;; ptr++)
5554            {            {
5555            *errorcodeptr = ERR75;            *errorcodeptr = ERR75;
5556            goto FAILED;            goto FAILED;
5557            }            }
5558          }          }
5559    
5560        if (*ptr != CHAR_RIGHT_PARENTHESIS)        if (*ptr != CHAR_RIGHT_PARENTHESIS)
# Line 5623  for (;; ptr++) Line 5570  for (;; ptr++)
5570          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
5571              STRNCMP_UC_C8(name, vn, namelen) == 0)              STRNCMP_UC_C8(name, vn, namelen) == 0)
5572            {            {
5573              int setverb;
5574    
5575            /* Check for open captures before ACCEPT and convert it to            /* Check for open captures before ACCEPT and convert it to
5576            ASSERT_ACCEPT if in an assertion. */            ASSERT_ACCEPT if in an assertion. */
5577    
# Line 5640  for (;; ptr++) Line 5589  for (;; ptr++)
5589                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
5590                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
5591                }                }
5592              *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;              setverb = *code++ =
5593                  (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5594    
5595              /* Do not set firstchar after *ACCEPT */              /* Do not set firstchar after *ACCEPT */
5596              if (firstchar == REQ_UNSET) firstchar = REQ_NONE;              if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
# Line 5655  for (;; ptr++) Line 5605  for (;; ptr++)
5605                *errorcodeptr = ERR66;                *errorcodeptr = ERR66;
5606                goto FAILED;                goto FAILED;
5607                }                }
5608              *code = verbs[i].op;              setverb = *code++ = verbs[i].op;
             if (*code++ == OP_THEN) cd->external_flags |= PCRE_HASTHEN;  
5609              }              }
5610    
5611            else            else
# Line 5666  for (;; ptr++) Line 5615  for (;; ptr++)
5615                *errorcodeptr = ERR59;                *errorcodeptr = ERR59;
5616                goto FAILED;                goto FAILED;
5617                }                }
5618              *code = verbs[i].op_arg;              setverb = *code++ = verbs[i].op_arg;
             if (*code++ == OP_THEN_ARG) cd->external_flags |= PCRE_HASTHEN;  
5619              *code++ = arglen;              *code++ = arglen;
5620              memcpy(code, arg, IN_UCHARS(arglen));              memcpy(code, arg, IN_UCHARS(arglen));
5621              code += arglen;              code += arglen;
5622              *code++ = 0;              *code++ = 0;
5623              }              }
5624    
5625              switch (setverb)
5626                {
5627                case OP_THEN:
5628                case OP_THEN_ARG:
5629                cd->external_flags |= PCRE_HASTHEN;
5630                break;
5631    
5632                case OP_PRUNE:
5633                case OP_PRUNE_ARG:
5634                case OP_SKIP:
5635                case OP_SKIP_ARG:
5636                cd->had_pruneorskip = TRUE;
5637                break;
5638                }
5639    
5640            break;  /* Found verb, exit loop */            break;  /* Found verb, exit loop */
5641            }            }
5642    
# Line 6859  for (;; ptr++) Line 6822  for (;; ptr++)
6822        /* For the rest (including \X when Unicode properties are supported), we        /* For the rest (including \X when Unicode properties are supported), we
6823        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
6824        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
6825        Unicode property tests. Note that \b and \B do a one-character        Unicode property tests. Note that \b and \B do a one-character
6826        lookbehind. */        lookbehind. */
6827    
6828        else        else
6829          {          {
6830          if ((-c == ESC_b || -c == ESC_B) && cd->max_lookbehind == 0)          if ((-c == ESC_b || -c == ESC_B) && cd->max_lookbehind == 0)
6831            cd->max_lookbehind = 1;            cd->max_lookbehind = 1;
6832  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
6833          if (-c >= ESC_DU && -c <= ESC_wu)          if (-c >= ESC_DU && -c <= ESC_wu)
6834            {            {
# Line 6922  for (;; ptr++) Line 6885  for (;; ptr++)
6885    
6886      ONE_CHAR:      ONE_CHAR:
6887      previous = code;      previous = code;
6888    
6889        /* For caseless UTF-8 mode when UCP support is available, check whether
6890        this character has more than one other case. If so, generate a special
6891        OP_PROP item instead of OP_CHARI. */
6892    
6893    #ifdef SUPPORT_UCP
6894        if (utf && (options & PCRE_CASELESS) != 0)
6895          {
6896          GETCHAR(c, mcbuffer);
6897          if ((c = UCD_CASESET(c)) != 0)
6898            {
6899            *code++ = OP_PROP;
6900            *code++ = PT_CLIST;
6901            *code++ = c;
6902            if (firstchar == REQ_UNSET) firstchar = zerofirstchar = REQ_NONE;
6903            break;
6904            }
6905          }
6906    #endif
6907    
6908        /* Caseful matches, or not one of the multicase characters. */
6909    
6910      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR;      *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR;
6911      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];      for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];
6912    
# Line 7173  for (;;) Line 7158  for (;;)
7158          *ptrptr = ptr;          *ptrptr = ptr;
7159          return FALSE;          return FALSE;
7160          }          }
7161        else        else
7162          {          {
7163          if (fixed_length > cd->max_lookbehind)          if (fixed_length > cd->max_lookbehind)
7164            cd->max_lookbehind = fixed_length;            cd->max_lookbehind = fixed_length;
7165          PUT(reverse_count, 0, fixed_length);          PUT(reverse_count, 0, fixed_length);
7166          }          }
7167        }        }
7168      }      }
# Line 7310  and the highest back reference was great Line 7295  and the highest back reference was great
7295  However, by keeping a bitmap of the first 31 back references, we can catch some  However, by keeping a bitmap of the first 31 back references, we can catch some
7296  of the more common cases more precisely.  of the more common cases more precisely.
7297    
7298    ... A second exception is when the .* appears inside an atomic group, because
7299    this prevents the number of characters it matches from being adjusted.
7300    
7301  Arguments:  Arguments:
7302    code           points to start of expression (the bracket)    code           points to start of expression (the bracket)
7303    bracket_map    a bitmap of which brackets we are inside while testing; this    bracket_map    a bitmap of which brackets we are inside while testing; this
7304                    handles up to substring 31; after that we just have to take                    handles up to substring 31; after that we just have to take
7305                    the less precise approach                    the less precise approach
7306    backref_map    the back reference bitmap    cd             points to the compile data block
7307      atomcount      atomic group level
7308    
7309  Returns:     TRUE or FALSE  Returns:     TRUE or FALSE
7310  */  */
7311    
7312  static BOOL  static BOOL
7313  is_anchored(register const pcre_uchar *code, unsigned int bracket_map,  is_anchored(register const pcre_uchar *code, unsigned int bracket_map,
7314    unsigned int backref_map)    compile_data *cd, int atomcount)
7315  {  {
7316  do {  do {
7317     const pcre_uchar *scode = first_significant_code(     const pcre_uchar *scode = first_significant_code(
# Line 7334  do { Line 7323  do {
7323     if (op == OP_BRA  || op == OP_BRAPOS ||     if (op == OP_BRA  || op == OP_BRAPOS ||
7324         op == OP_SBRA || op == OP_SBRAPOS)         op == OP_SBRA || op == OP_SBRAPOS)
7325       {       {
7326       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE;
7327       }       }
7328    
7329     /* Capturing brackets */     /* Capturing brackets */
# Line 7344  do { Line 7333  do {
7333       {       {
7334       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
7335       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
7336       if (!is_anchored(scode, new_map, backref_map)) return FALSE;       if (!is_anchored(scode, new_map, cd, atomcount)) return FALSE;
7337         }
7338    
7339       /* Positive forward assertions and conditions */
7340    
7341       else if (op == OP_ASSERT || op == OP_COND)
7342         {
7343         if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE;
7344       }       }
7345    
7346     /* Other brackets */     /* Atomic groups */
7347    
7348     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_ONCE_NC ||     else if (op == OP_ONCE || op == OP_ONCE_NC)
             op == OP_COND)  
7349       {       {
7350       if (!is_anchored(scode, bracket_map, backref_map)) return FALSE;       if (!is_anchored(scode, bracket_map, cd, atomcount + 1))
7351           return FALSE;
7352       }       }
7353    
7354     /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and     /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and
7355     it isn't in brackets that are or may be referenced. */     it isn't in brackets that are or may be referenced or inside an atomic
7356       group. */
7357    
7358     else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR ||     else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR ||
7359               op == OP_TYPEPOSSTAR))               op == OP_TYPEPOSSTAR))
7360       {       {
7361       if (scode[1] != OP_ALLANY || (bracket_map & backref_map) != 0)       if (scode[1] != OP_ALLANY || (bracket_map & cd->backref_map) != 0 ||
7362             atomcount > 0 || cd->had_pruneorskip)
7363         return FALSE;         return FALSE;
7364       }       }
7365    
7366     /* Check for explicit anchoring */     /* Check for explicit anchoring */
7367    
7368     else if (op != OP_SOD && op != OP_SOM && op != OP_CIRC) return FALSE;     else if (op != OP_SOD && op != OP_SOM && op != OP_CIRC) return FALSE;
7369    
7370     code += GET(code, 1);     code += GET(code, 1);
7371     }     }
7372  while (*code == OP_ALT);   /* Loop for each alternative */  while (*code == OP_ALT);   /* Loop for each alternative */
# Line 7385  return TRUE; Line 7384  return TRUE;
7384  matching and for non-DOTALL patterns that start with .* (which must start at  matching and for non-DOTALL patterns that start with .* (which must start at
7385  the beginning or after \n). As in the case of is_anchored() (see above), we  the beginning or after \n). As in the case of is_anchored() (see above), we
7386  have to take account of back references to capturing brackets that contain .*  have to take account of back references to capturing brackets that contain .*
7387  because in that case we can't make the assumption.  because in that case we can't make the assumption. Also, the appearance of .*
7388    inside atomic brackets or in a pattern that contains *PRUNE or *SKIP does not
7389    count, because once again the assumption no longer holds.
7390    
7391  Arguments:  Arguments:
7392    code           points to start of expression (the bracket)    code           points to start of expression (the bracket)
7393    bracket_map    a bitmap of which brackets we are inside while testing; this    bracket_map    a bitmap of which brackets we are inside while testing; this
7394                    handles up to substring 31; after that we just have to take                    handles up to substring 31; after that we just have to take
7395                    the less precise approach                    the less precise approach
7396    backref_map    the back reference bitmap    cd             points to the compile data
7397      atomcount      atomic group level
7398    
7399  Returns:         TRUE or FALSE  Returns:         TRUE or FALSE
7400  */  */
7401    
7402  static BOOL  static BOOL
7403  is_startline(const pcre_uchar *code, unsigned int bracket_map,  is_startline(const pcre_uchar *code, unsigned int bracket_map,
7404    unsigned int backref_map)    compile_data *cd, int atomcount)
7405  {  {
7406  do {  do {
7407     const pcre_uchar *scode = first_significant_code(     const pcre_uchar *scode = first_significant_code(
# Line 7425  do { Line 7427  do {
7427         return FALSE;         return FALSE;
7428    
7429         default:     /* Assertion */         default:     /* Assertion */
7430         if (!is_startline(scode, bracket_map, backref_map)) return FALSE;         if (!is_startline(scode, bracket_map, cd, atomcount)) return FALSE;
7431         do scode += GET(scode, 1); while (*scode == OP_ALT);         do scode += GET(scode, 1); while (*scode == OP_ALT);
7432         scode += 1 + LINK_SIZE;         scode += 1 + LINK_SIZE;
7433         break;         break;
# Line 7439  do { Line 7441  do {
7441     if (op == OP_BRA  || op == OP_BRAPOS ||     if (op == OP_BRA  || op == OP_BRAPOS ||
7442         op == OP_SBRA || op == OP_SBRAPOS)         op == OP_SBRA || op == OP_SBRAPOS)
7443       {       {
7444       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;       if (!is_startline(scode, bracket_map, cd, atomcount)) return FALSE;
7445       }       }
7446    
7447     /* Capturing brackets */     /* Capturing brackets */
# Line 7449  do { Line 7451  do {
7451       {       {
7452       int n = GET2(scode, 1+LINK_SIZE);       int n = GET2(scode, 1+LINK_SIZE);
7453       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);       int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
7454       if (!is_startline(scode, new_map, backref_map)) return FALSE;       if (!is_startline(scode, new_map, cd, atomcount)) return FALSE;
7455       }       }
7456    
7457     /* Other brackets */     /* Positive forward assertions */
7458    
7459     else if (op == OP_ASSERT || op == OP_ONCE || op == OP_ONCE_NC)     else if (op == OP_ASSERT)
7460       {       {
7461       if (!is_startline(scode, bracket_map, backref_map)) return FALSE;       if (!is_startline(scode, bracket_map, cd, atomcount)) return FALSE;
7462       }       }
7463    
7464       /* Atomic brackets */
7465    
7466     /* .* means "start at start or after \n" if it isn't in brackets that     else if (op == OP_ONCE || op == OP_ONCE_NC)
7467     may be referenced. */       {
7468         if (!is_startline(scode, bracket_map, cd, atomcount + 1)) return FALSE;
7469         }
7470    
7471       /* .* means "start at start or after \n" if it isn't in atomic brackets or
7472       brackets that may be referenced, as long as the pattern does not contain
7473       *PRUNE or *SKIP, because these break the feature. Consider, for example,
7474       /.*?a(*PRUNE)b/ with the subject "aab", which matches "ab", i.e. not at the
7475       start of a line. */
7476    
7477     else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR || op == OP_TYPEPOSSTAR)     else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR || op == OP_TYPEPOSSTAR)
7478       {       {
7479       if (scode[1] != OP_ANY || (bracket_map & backref_map) != 0) return FALSE;       if (scode[1] != OP_ANY || (bracket_map & cd->backref_map) != 0 ||
7480             atomcount > 0 || cd->had_pruneorskip)
7481           return FALSE;
7482       }       }
7483    
7484     /* Check for explicit circumflex */     /* Check for explicit circumflex; anything else gives a FALSE result. Note
7485       in particular that this includes atomic brackets OP_ONCE and OP_ONCE_NC
7486       because the number of characters matched by .* cannot be adjusted inside
7487       them. */
7488    
7489     else if (op != OP_CIRC && op != OP_CIRCM) return FALSE;     else if (op != OP_CIRC && op != OP_CIRCM) return FALSE;
7490    
# Line 7926  cd->start_code = codestart; Line 7943  cd->start_code = codestart;
7943  cd->hwm = (pcre_uchar *)(cd->start_workspace);  cd->hwm = (pcre_uchar *)(cd->start_workspace);
7944  cd->req_varyopt = 0;  cd->req_varyopt = 0;
7945  cd->had_accept = FALSE;  cd->had_accept = FALSE;
7946    cd->had_pruneorskip = FALSE;
7947  cd->check_lookbehind = FALSE;  cd->check_lookbehind = FALSE;
7948  cd->open_caps = NULL;  cd->open_caps = NULL;
7949    
# Line 8049  if (errorcode != 0) Line 8067  if (errorcode != 0)
8067    }    }
8068    
8069  /* If the anchored option was not passed, set the flag if we can determine that  /* If the anchored option was not passed, set the flag if we can determine that
8070  the pattern is anchored by virtue of ^ characters or \A or anything else (such  the pattern is anchored by virtue of ^ characters or \A or anything else, such
8071  as starting with .* when DOTALL is set).  as starting with non-atomic .* when DOTALL is set and there are no occurrences
8072    of *PRUNE or *SKIP.
8073    
8074  Otherwise, if we know what the first byte has to be, save it, because that  Otherwise, if we know what the first byte has to be, save it, because that
8075  speeds up unanchored matches no end. If not, see if we can set the  speeds up unanchored matches no end. If not, see if we can set the
8076  PCRE_STARTLINE flag. This is helpful for multiline matches when all branches  PCRE_STARTLINE flag. This is helpful for multiline matches when all branches
8077  start with ^. and also when all branches start with .* for non-DOTALL matches.  start with ^. and also when all branches start with non-atomic .* for
8078  */  non-DOTALL matches when *PRUNE and SKIP are not present. */
8079    
8080  if ((re->options & PCRE_ANCHORED) == 0)  if ((re->options & PCRE_ANCHORED) == 0)
8081    {    {
8082    if (is_anchored(codestart, 0, cd->backref_map))    if (is_anchored(codestart, 0, cd, 0)) re->options |= PCRE_ANCHORED;
     re->options |= PCRE_ANCHORED;  
8083    else    else
8084      {      {
8085      if (firstchar < 0)      if (firstchar < 0)
# Line 8098  if ((re->options & PCRE_ANCHORED) == 0) Line 8116  if ((re->options & PCRE_ANCHORED) == 0)
8116    
8117        re->flags |= PCRE_FIRSTSET;        re->flags |= PCRE_FIRSTSET;
8118        }        }
8119      else if (is_startline(codestart, 0, cd->backref_map))  
8120        re->flags |= PCRE_STARTLINE;      else if (is_startline(codestart, 0, cd, 0)) re->flags |= PCRE_STARTLINE;
8121      }      }
8122    }    }
8123    

Legend:
Removed from v.964  
changed lines
  Added in v.1048

  ViewVC Help
Powered by ViewVC 1.1.5