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

Diff of /code/trunk/pcre_study.c

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

revision 342 by ph10, Sun Apr 20 17:10:13 2008 UTC revision 550 by ph10, Sun Oct 10 16:24:11 2010 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2008 University of Cambridge             Copyright (c) 1997-2010 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 48  supporting functions. */ Line 48  supporting functions. */
48    
49  #include "pcre_internal.h"  #include "pcre_internal.h"
50    
51    #define SET_BIT(c) start_bits[c/8] |= (1 << (c&7))
52    
53  /* Returns from set_start_bits() */  /* Returns from set_start_bits() */
54    
55  enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE };  enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE };
56    
57    
58    
59    /*************************************************
60    *   Find the minimum subject length for a group  *
61    *************************************************/
62    
63    /* Scan a parenthesized group and compute the minimum length of subject that
64    is needed to match it. This is a lower bound; it does not mean there is a
65    string of that length that matches. In UTF8 mode, the result is in characters
66    rather than bytes.
67    
68    Arguments:
69      code       pointer to start of group (the bracket)
70      startcode  pointer to start of the whole pattern
71      options    the compiling options
72    
73    Returns:   the minimum length
74               -1 if \C was encountered
75               -2 internal error (missing capturing bracket)
76    */
77    
78    static int
79    find_minlength(const uschar *code, const uschar *startcode, int options)
80    {
81    int length = -1;
82    BOOL utf8 = (options & PCRE_UTF8) != 0;
83    BOOL had_recurse = FALSE;
84    register int branchlength = 0;
85    register uschar *cc = (uschar *)code + 1 + LINK_SIZE;
86    
87    if (*code == OP_CBRA || *code == OP_SCBRA) cc += 2;
88    
89    /* Scan along the opcodes for this branch. If we get to the end of the
90    branch, check the length against that of the other branches. */
91    
92    for (;;)
93      {
94      int d, min;
95      uschar *cs, *ce;
96      register int op = *cc;
97    
98      switch (op)
99        {
100        case OP_COND:
101        case OP_SCOND:
102    
103        /* If there is only one branch in a condition, the implied branch has zero
104        length, so we don't add anything. This covers the DEFINE "condition"
105        automatically. */
106    
107        cs = cc + GET(cc, 1);
108        if (*cs != OP_ALT)
109          {
110          cc = cs + 1 + LINK_SIZE;
111          break;
112          }
113    
114        /* Otherwise we can fall through and treat it the same as any other
115        subpattern. */
116    
117        case OP_CBRA:
118        case OP_SCBRA:
119        case OP_BRA:
120        case OP_SBRA:
121        case OP_ONCE:
122        d = find_minlength(cc, startcode, options);
123        if (d < 0) return d;
124        branchlength += d;
125        do cc += GET(cc, 1); while (*cc == OP_ALT);
126        cc += 1 + LINK_SIZE;
127        break;
128    
129        /* Reached end of a branch; if it's a ket it is the end of a nested
130        call. If it's ALT it is an alternation in a nested call. If it is
131        END it's the end of the outer call. All can be handled by the same code. */
132    
133        case OP_ALT:
134        case OP_KET:
135        case OP_KETRMAX:
136        case OP_KETRMIN:
137        case OP_END:
138        if (length < 0 || (!had_recurse && branchlength < length))
139          length = branchlength;
140        if (*cc != OP_ALT) return length;
141        cc += 1 + LINK_SIZE;
142        branchlength = 0;
143        had_recurse = FALSE;
144        break;
145    
146        /* Skip over assertive subpatterns */
147    
148        case OP_ASSERT:
149        case OP_ASSERT_NOT:
150        case OP_ASSERTBACK:
151        case OP_ASSERTBACK_NOT:
152        do cc += GET(cc, 1); while (*cc == OP_ALT);
153        /* Fall through */
154    
155        /* Skip over things that don't match chars */
156    
157        case OP_REVERSE:
158        case OP_CREF:
159        case OP_NCREF:
160        case OP_RREF:
161        case OP_NRREF:
162        case OP_DEF:
163        case OP_OPT:
164        case OP_CALLOUT:
165        case OP_SOD:
166        case OP_SOM:
167        case OP_EOD:
168        case OP_EODN:
169        case OP_CIRC:
170        case OP_DOLL:
171        case OP_NOT_WORD_BOUNDARY:
172        case OP_WORD_BOUNDARY:
173        cc += _pcre_OP_lengths[*cc];
174        break;
175    
176        /* Skip over a subpattern that has a {0} or {0,x} quantifier */
177    
178        case OP_BRAZERO:
179        case OP_BRAMINZERO:
180        case OP_SKIPZERO:
181        cc += _pcre_OP_lengths[*cc];
182        do cc += GET(cc, 1); while (*cc == OP_ALT);
183        cc += 1 + LINK_SIZE;
184        break;
185    
186        /* Handle literal characters and + repetitions */
187    
188        case OP_CHAR:
189        case OP_CHARNC:
190        case OP_NOT:
191        case OP_PLUS:
192        case OP_MINPLUS:
193        case OP_POSPLUS:
194        case OP_NOTPLUS:
195        case OP_NOTMINPLUS:
196        case OP_NOTPOSPLUS:
197        branchlength++;
198        cc += 2;
199    #ifdef SUPPORT_UTF8
200        if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
201    #endif
202        break;
203    
204        case OP_TYPEPLUS:
205        case OP_TYPEMINPLUS:
206        case OP_TYPEPOSPLUS:
207        branchlength++;
208        cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
209        break;
210    
211        /* Handle exact repetitions. The count is already in characters, but we
212        need to skip over a multibyte character in UTF8 mode.  */
213    
214        case OP_EXACT:
215        case OP_NOTEXACT:
216        branchlength += GET2(cc,1);
217        cc += 4;
218    #ifdef SUPPORT_UTF8
219        if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
220    #endif
221        break;
222    
223        case OP_TYPEEXACT:
224        branchlength += GET2(cc,1);
225        cc += (cc[3] == OP_PROP || cc[3] == OP_NOTPROP)? 6 : 4;
226        break;
227    
228        /* Handle single-char non-literal matchers */
229    
230        case OP_PROP:
231        case OP_NOTPROP:
232        cc += 2;
233        /* Fall through */
234    
235        case OP_NOT_DIGIT:
236        case OP_DIGIT:
237        case OP_NOT_WHITESPACE:
238        case OP_WHITESPACE:
239        case OP_NOT_WORDCHAR:
240        case OP_WORDCHAR:
241        case OP_ANY:
242        case OP_ALLANY:
243        case OP_EXTUNI:
244        case OP_HSPACE:
245        case OP_NOT_HSPACE:
246        case OP_VSPACE:
247        case OP_NOT_VSPACE:
248        branchlength++;
249        cc++;
250        break;
251    
252        /* "Any newline" might match two characters */
253    
254        case OP_ANYNL:
255        branchlength += 2;
256        cc++;
257        break;
258    
259        /* The single-byte matcher means we can't proceed in UTF-8 mode */
260    
261        case OP_ANYBYTE:
262    #ifdef SUPPORT_UTF8
263        if (utf8) return -1;
264    #endif
265        branchlength++;
266        cc++;
267        break;
268    
269        /* For repeated character types, we have to test for \p and \P, which have
270        an extra two bytes of parameters. */
271    
272        case OP_TYPESTAR:
273        case OP_TYPEMINSTAR:
274        case OP_TYPEQUERY:
275        case OP_TYPEMINQUERY:
276        case OP_TYPEPOSSTAR:
277        case OP_TYPEPOSQUERY:
278        if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
279        cc += _pcre_OP_lengths[op];
280        break;
281    
282        case OP_TYPEUPTO:
283        case OP_TYPEMINUPTO:
284        case OP_TYPEPOSUPTO:
285        if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;
286        cc += _pcre_OP_lengths[op];
287        break;
288    
289        /* Check a class for variable quantification */
290    
291    #ifdef SUPPORT_UTF8
292        case OP_XCLASS:
293        cc += GET(cc, 1) - 33;
294        /* Fall through */
295    #endif
296    
297        case OP_CLASS:
298        case OP_NCLASS:
299        cc += 33;
300    
301        switch (*cc)
302          {
303          case OP_CRPLUS:
304          case OP_CRMINPLUS:
305          branchlength++;
306          /* Fall through */
307    
308          case OP_CRSTAR:
309          case OP_CRMINSTAR:
310          case OP_CRQUERY:
311          case OP_CRMINQUERY:
312          cc++;
313          break;
314    
315          case OP_CRRANGE:
316          case OP_CRMINRANGE:
317          branchlength += GET2(cc,1);
318          cc += 5;
319          break;
320    
321          default:
322          branchlength++;
323          break;
324          }
325        break;
326    
327        /* Backreferences and subroutine calls are treated in the same way: we find
328        the minimum length for the subpattern. A recursion, however, causes an
329        a flag to be set that causes the length of this branch to be ignored. The
330        logic is that a recursion can only make sense if there is another
331        alternation that stops the recursing. That will provide the minimum length
332        (when no recursion happens). A backreference within the group that it is
333        referencing behaves in the same way.
334    
335        If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
336        matches an empty string (by default it causes a matching failure), so in
337        that case we must set the minimum length to zero. */
338    
339        case OP_REF:
340        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
341          {
342          ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
343          if (cs == NULL) return -2;
344          do ce += GET(ce, 1); while (*ce == OP_ALT);
345          if (cc > cs && cc < ce)
346            {
347            d = 0;
348            had_recurse = TRUE;
349            }
350          else d = find_minlength(cs, startcode, options);
351          }
352        else d = 0;
353        cc += 3;
354    
355        /* Handle repeated back references */
356    
357        switch (*cc)
358          {
359          case OP_CRSTAR:
360          case OP_CRMINSTAR:
361          case OP_CRQUERY:
362          case OP_CRMINQUERY:
363          min = 0;
364          cc++;
365          break;
366    
367          case OP_CRRANGE:
368          case OP_CRMINRANGE:
369          min = GET2(cc, 1);
370          cc += 5;
371          break;
372    
373          default:
374          min = 1;
375          break;
376          }
377    
378        branchlength += min * d;
379        break;
380    
381        case OP_RECURSE:
382        cs = ce = (uschar *)startcode + GET(cc, 1);
383        if (cs == NULL) return -2;
384        do ce += GET(ce, 1); while (*ce == OP_ALT);
385        if (cc > cs && cc < ce)
386          had_recurse = TRUE;
387        else
388          branchlength += find_minlength(cs, startcode, options);
389        cc += 1 + LINK_SIZE;
390        break;
391    
392        /* Anything else does not or need not match a character. We can get the
393        item's length from the table, but for those that can match zero occurrences
394        of a character, we must take special action for UTF-8 characters. */
395    
396        case OP_UPTO:
397        case OP_NOTUPTO:
398        case OP_MINUPTO:
399        case OP_NOTMINUPTO:
400        case OP_POSUPTO:
401        case OP_STAR:
402        case OP_MINSTAR:
403        case OP_NOTMINSTAR:
404        case OP_POSSTAR:
405        case OP_NOTPOSSTAR:
406        case OP_QUERY:
407        case OP_MINQUERY:
408        case OP_NOTMINQUERY:
409        case OP_POSQUERY:
410        case OP_NOTPOSQUERY:
411        cc += _pcre_OP_lengths[op];
412    #ifdef SUPPORT_UTF8
413        if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
414    #endif
415        break;
416    
417        /* Skip these, but we need to add in the name length. */
418    
419        case OP_MARK:
420        case OP_PRUNE_ARG:
421        case OP_SKIP_ARG:
422        cc += _pcre_OP_lengths[op] + cc[1];
423        break;
424    
425        case OP_THEN_ARG:
426        cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE];
427        break;
428    
429        /* For the record, these are the opcodes that are matched by "default":
430        OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,
431        OP_THEN. */
432    
433        default:
434        cc += _pcre_OP_lengths[op];
435        break;
436        }
437      }
438    /* Control never gets here */
439    }
440    
441    
442    
443  /*************************************************  /*************************************************
444  *      Set a bit and maybe its alternate case    *  *      Set a bit and maybe its alternate case    *
445  *************************************************/  *************************************************/
446    
447  /* Given a character, set its bit in the table, and also the bit for the other  /* Given a character, set its first byte's bit in the table, and also the
448  version of a letter if we are caseless.  corresponding bit for the other version of a letter if we are caseless. In
449    UTF-8 mode, for characters greater than 127, we can only do the caseless thing
450    when Unicode property support is available.
451    
452  Arguments:  Arguments:
453    start_bits    points to the bit map    start_bits    points to the bit map
454    c             is the character    p             points to the character
455    caseless      the caseless flag    caseless      the caseless flag
456    cd            the block with char table pointers    cd            the block with char table pointers
457      utf8          TRUE for UTF-8 mode
458    
459  Returns:        nothing  Returns:        pointer after the character
460    */
461    
462    static const uschar *
463    set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless,
464      compile_data *cd, BOOL utf8)
465    {
466    unsigned int c = *p;
467    
468    SET_BIT(c);
469    
470    #ifdef SUPPORT_UTF8
471    if (utf8 && c > 127)
472      {
473      GETCHARINC(c, p);
474    #ifdef SUPPORT_UCP
475      if (caseless)
476        {
477        uschar buff[8];
478        c = UCD_OTHERCASE(c);
479        (void)_pcre_ord2utf8(c, buff);
480        SET_BIT(buff[0]);
481        }
482    #endif
483      return p;
484      }
485    #endif
486    
487    /* Not UTF-8 mode, or character is less than 127. */
488    
489    if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
490    return p + 1;
491    }
492    
493    
494    
495    /*************************************************
496    *     Set bits for a positive character type     *
497    *************************************************/
498    
499    /* This function sets starting bits for a character type. In UTF-8 mode, we can
500    only do a direct setting for bytes less than 128, as otherwise there can be
501    confusion with bytes in the middle of UTF-8 characters. In a "traditional"
502    environment, the tables will only recognize ASCII characters anyway, but in at
503    least one Windows environment, some higher bytes bits were set in the tables.
504    So we deal with that case by considering the UTF-8 encoding.
505    
506    Arguments:
507      start_bits     the starting bitmap
508      cbit type      the type of character wanted
509      table_limit    32 for non-UTF-8; 16 for UTF-8
510      cd             the block with char table pointers
511    
512    Returns:         nothing
513    */
514    
515    static void
516    set_type_bits(uschar *start_bits, int cbit_type, int table_limit,
517      compile_data *cd)
518    {
519    register int c;
520    for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];
521    if (table_limit == 32) return;
522    for (c = 128; c < 256; c++)
523      {
524      if ((cd->cbits[c/8] & (1 << (c&7))) != 0)
525        {
526        uschar buff[8];
527        (void)_pcre_ord2utf8(c, buff);
528        SET_BIT(buff[0]);
529        }
530      }
531    }
532    
533    
534    /*************************************************
535    *     Set bits for a negative character type     *
536    *************************************************/
537    
538    /* This function sets starting bits for a negative character type such as \D.
539    In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
540    otherwise there can be confusion with bytes in the middle of UTF-8 characters.
541    Unlike in the positive case, where we can set appropriate starting bits for
542    specific high-valued UTF-8 characters, in this case we have to set the bits for
543    all high-valued characters. The lowest is 0xc2, but we overkill by starting at
544    0xc0 (192) for simplicity.
545    
546    Arguments:
547      start_bits     the starting bitmap
548      cbit type      the type of character wanted
549      table_limit    32 for non-UTF-8; 16 for UTF-8
550      cd             the block with char table pointers
551    
552    Returns:         nothing
553  */  */
554    
555  static void  static void
556  set_bit(uschar *start_bits, unsigned int c, BOOL caseless, compile_data *cd)  set_nottype_bits(uschar *start_bits, int cbit_type, int table_limit,
557      compile_data *cd)
558  {  {
559  start_bits[c/8] |= (1 << (c&7));  register int c;
560  if (caseless && (cd->ctypes[c] & ctype_letter) != 0)  for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];
561    start_bits[cd->fcc[c]/8] |= (1 << (cd->fcc[c]&7));  if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;
562  }  }
563    
564    
# Line 110  set_start_bits(const uschar *code, uscha Line 593  set_start_bits(const uschar *code, uscha
593  {  {
594  register int c;  register int c;
595  int yield = SSB_DONE;  int yield = SSB_DONE;
596    int table_limit = utf8? 16:32;
597    
598  #if 0  #if 0
599  /* ========================================================================= */  /* ========================================================================= */
# Line 220  do Line 704  do
704        /* SKIPZERO skips the bracket. */        /* SKIPZERO skips the bracket. */
705    
706        case OP_SKIPZERO:        case OP_SKIPZERO:
707          tcode++;
708        do tcode += GET(tcode,1); while (*tcode == OP_ALT);        do tcode += GET(tcode,1); while (*tcode == OP_ALT);
709        tcode += 1 + LINK_SIZE;        tcode += 1 + LINK_SIZE;
710        break;        break;
# Line 232  do Line 717  do
717        case OP_QUERY:        case OP_QUERY:
718        case OP_MINQUERY:        case OP_MINQUERY:
719        case OP_POSQUERY:        case OP_POSQUERY:
720        set_bit(start_bits, tcode[1], caseless, cd);        tcode = set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);
       tcode += 2;  
 #ifdef SUPPORT_UTF8  
       if (utf8 && tcode[-1] >= 0xc0)  
         tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];  
 #endif  
721        break;        break;
722    
723        /* Single-char upto sets the bit and tries the next */        /* Single-char upto sets the bit and tries the next */
# Line 245  do Line 725  do
725        case OP_UPTO:        case OP_UPTO:
726        case OP_MINUPTO:        case OP_MINUPTO:
727        case OP_POSUPTO:        case OP_POSUPTO:
728        set_bit(start_bits, tcode[3], caseless, cd);        tcode = set_table_bit(start_bits, tcode + 3, caseless, cd, utf8);
       tcode += 4;  
 #ifdef SUPPORT_UTF8  
       if (utf8 && tcode[-1] >= 0xc0)  
         tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];  
 #endif  
729        break;        break;
730    
731        /* At least one single char sets the bit and stops */        /* At least one single char sets the bit and stops */
# Line 263  do Line 738  do
738        case OP_PLUS:        case OP_PLUS:
739        case OP_MINPLUS:        case OP_MINPLUS:
740        case OP_POSPLUS:        case OP_POSPLUS:
741        set_bit(start_bits, tcode[1], caseless, cd);        (void)set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);
742          try_next = FALSE;
743          break;
744    
745          /* Special spacing and line-terminating items. These recognize specific
746          lists of characters. The difference between VSPACE and ANYNL is that the
747          latter can match the two-character CRLF sequence, but that is not
748          relevant for finding the first character, so their code here is
749          identical. */
750    
751          case OP_HSPACE:
752          SET_BIT(0x09);
753          SET_BIT(0x20);
754          if (utf8)
755            {
756            SET_BIT(0xC2);  /* For U+00A0 */
757            SET_BIT(0xE1);  /* For U+1680, U+180E */
758            SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
759            SET_BIT(0xE3);  /* For U+3000 */
760            }
761          else SET_BIT(0xA0);
762          try_next = FALSE;
763          break;
764    
765          case OP_ANYNL:
766          case OP_VSPACE:
767          SET_BIT(0x0A);
768          SET_BIT(0x0B);
769          SET_BIT(0x0C);
770          SET_BIT(0x0D);
771          if (utf8)
772            {
773            SET_BIT(0xC2);  /* For U+0085 */
774            SET_BIT(0xE2);  /* For U+2028, U+2029 */
775            }
776          else SET_BIT(0x85);
777        try_next = FALSE;        try_next = FALSE;
778        break;        break;
779    
780        /* Single character type sets the bits and stops */        /* Single character types set the bits and stop. Note that if PCRE_UCP
781          is set, we do not see these op codes because \d etc are converted to
782          properties. Therefore, these apply in the case when only characters less
783          than 256 are recognized to match the types. */
784    
785        case OP_NOT_DIGIT:        case OP_NOT_DIGIT:
786        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
         start_bits[c] |= ~cd->cbits[c+cbit_digit];  
787        try_next = FALSE;        try_next = FALSE;
788        break;        break;
789    
790        case OP_DIGIT:        case OP_DIGIT:
791        for (c = 0; c < 32; c++)        set_type_bits(start_bits, cbit_digit, table_limit, cd);
         start_bits[c] |= cd->cbits[c+cbit_digit];  
792        try_next = FALSE;        try_next = FALSE;
793        break;        break;
794    
795        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
796        discard it. */        ensure it is set as not whitespace. */
797    
798        case OP_NOT_WHITESPACE:        case OP_NOT_WHITESPACE:
799        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_space, table_limit, cd);
800          {        start_bits[1] |= 0x08;
         int d = cd->cbits[c+cbit_space];  
         if (c == 1) d &= ~0x08;  
         start_bits[c] |= ~d;  
         }  
801        try_next = FALSE;        try_next = FALSE;
802        break;        break;
803    
804        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
805        discard it. */        not set it from the table. */
806    
807        case OP_WHITESPACE:        case OP_WHITESPACE:
808        for (c = 0; c < 32; c++)        c = start_bits[1];    /* Save in case it was already set */
809          {        set_type_bits(start_bits, cbit_space, table_limit, cd);
810          int d = cd->cbits[c+cbit_space];        start_bits[1] = (start_bits[1] & ~0x08) | c;
         if (c == 1) d &= ~0x08;  
         start_bits[c] |= d;  
         }  
811        try_next = FALSE;        try_next = FALSE;
812        break;        break;
813    
814        case OP_NOT_WORDCHAR:        case OP_NOT_WORDCHAR:
815        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_word, table_limit, cd);
         start_bits[c] |= ~cd->cbits[c+cbit_word];  
816        try_next = FALSE;        try_next = FALSE;
817        break;        break;
818    
819        case OP_WORDCHAR:        case OP_WORDCHAR:
820        for (c = 0; c < 32; c++)        set_type_bits(start_bits, cbit_word, table_limit, cd);
         start_bits[c] |= cd->cbits[c+cbit_word];  
821        try_next = FALSE;        try_next = FALSE;
822        break;        break;
823    
# Line 324  do Line 826  do
826    
827        case OP_TYPEPLUS:        case OP_TYPEPLUS:
828        case OP_TYPEMINPLUS:        case OP_TYPEMINPLUS:
829          case OP_TYPEPOSPLUS:
830        tcode++;        tcode++;
831        break;        break;
832    
# Line 347  do Line 850  do
850        case OP_TYPEPOSQUERY:        case OP_TYPEPOSQUERY:
851        switch(tcode[1])        switch(tcode[1])
852          {          {
853            default:
854          case OP_ANY:          case OP_ANY:
855          case OP_ALLANY:          case OP_ALLANY:
856          return SSB_FAIL;          return SSB_FAIL;
857    
858            case OP_HSPACE:
859            SET_BIT(0x09);
860            SET_BIT(0x20);
861            if (utf8)
862              {
863              SET_BIT(0xC2);  /* For U+00A0 */
864              SET_BIT(0xE1);  /* For U+1680, U+180E */
865              SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
866              SET_BIT(0xE3);  /* For U+3000 */
867              }
868            else SET_BIT(0xA0);
869            break;
870    
871            case OP_ANYNL:
872            case OP_VSPACE:
873            SET_BIT(0x0A);
874            SET_BIT(0x0B);
875            SET_BIT(0x0C);
876            SET_BIT(0x0D);
877            if (utf8)
878              {
879              SET_BIT(0xC2);  /* For U+0085 */
880              SET_BIT(0xE2);  /* For U+2028, U+2029 */
881              }
882            else SET_BIT(0x85);
883            break;
884    
885          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
886          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
           start_bits[c] |= ~cd->cbits[c+cbit_digit];  
887          break;          break;
888    
889          case OP_DIGIT:          case OP_DIGIT:
890          for (c = 0; c < 32; c++)          set_type_bits(start_bits, cbit_digit, table_limit, cd);
           start_bits[c] |= cd->cbits[c+cbit_digit];  
891          break;          break;
892    
893          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we have to
894          discard it. */          ensure it gets set as not whitespace. */
895    
896          case OP_NOT_WHITESPACE:          case OP_NOT_WHITESPACE:
897          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_space, table_limit, cd);
898            {          start_bits[1] |= 0x08;
           int d = cd->cbits[c+cbit_space];  
           if (c == 1) d &= ~0x08;  
           start_bits[c] |= ~d;  
           }  
899          break;          break;
900    
901          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we have to
902          discard it. */          avoid setting it. */
903    
904          case OP_WHITESPACE:          case OP_WHITESPACE:
905          for (c = 0; c < 32; c++)          c = start_bits[1];    /* Save in case it was already set */
906            {          set_type_bits(start_bits, cbit_space, table_limit, cd);
907            int d = cd->cbits[c+cbit_space];          start_bits[1] = (start_bits[1] & ~0x08) | c;
           if (c == 1) d &= ~0x08;  
           start_bits[c] |= d;  
           }  
908          break;          break;
909    
910          case OP_NOT_WORDCHAR:          case OP_NOT_WORDCHAR:
911          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_word, table_limit, cd);
           start_bits[c] |= ~cd->cbits[c+cbit_word];  
912          break;          break;
913    
914          case OP_WORDCHAR:          case OP_WORDCHAR:
915          for (c = 0; c < 32; c++)          set_type_bits(start_bits, cbit_word, table_limit, cd);
           start_bits[c] |= cd->cbits[c+cbit_word];  
916          break;          break;
917          }          }
918    
# Line 499  Arguments: Line 1019  Arguments:
1019              set NULL unless error              set NULL unless error
1020    
1021  Returns:    pointer to a pcre_extra block, with study_data filled in and the  Returns:    pointer to a pcre_extra block, with study_data filled in and the
1022                appropriate flag set;                appropriate flags set;
1023              NULL on error or if no optimization possible              NULL on error or if no optimization possible
1024  */  */
1025    
1026  PCRE_EXP_DEFN pcre_extra *  PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
1027  pcre_study(const pcre *external_re, int options, const char **errorptr)  pcre_study(const pcre *external_re, int options, const char **errorptr)
1028  {  {
1029    int min;
1030    BOOL bits_set = FALSE;
1031  uschar start_bits[32];  uschar start_bits[32];
1032  pcre_extra *extra;  pcre_extra *extra;
1033  pcre_study_data *study;  pcre_study_data *study;
# Line 532  code = (uschar *)re + re->name_table_off Line 1054  code = (uschar *)re + re->name_table_off
1054    (re->name_count * re->name_entry_size);    (re->name_count * re->name_entry_size);
1055    
1056  /* For an anchored pattern, or an unanchored pattern that has a first char, or  /* For an anchored pattern, or an unanchored pattern that has a first char, or
1057  a multiline pattern that matches only at "line starts", no further processing  a multiline pattern that matches only at "line starts", there is no point in
1058  at present. */  seeking a list of starting bytes. */
1059    
1060  if ((re->options & PCRE_ANCHORED) != 0 ||  if ((re->options & PCRE_ANCHORED) == 0 &&
1061      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) != 0)      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1062    return NULL;    {
1063      /* Set the character tables in the block that is passed around */
1064    
1065  /* Set the character tables in the block that is passed around */    tables = re->tables;
1066      if (tables == NULL)
1067        (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
1068        (void *)(&tables));
1069    
1070      compile_block.lcc = tables + lcc_offset;
1071      compile_block.fcc = tables + fcc_offset;
1072      compile_block.cbits = tables + cbits_offset;
1073      compile_block.ctypes = tables + ctypes_offset;
1074    
1075      /* See if we can find a fixed set of initial characters for the pattern. */
1076    
1077      memset(start_bits, 0, 32 * sizeof(uschar));
1078      bits_set = set_start_bits(code, start_bits,
1079        (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,
1080        &compile_block) == SSB_DONE;
1081      }
1082    
1083    /* Find the minimum length of subject string. */
1084    
1085    min = find_minlength(code, code, re->options);
1086    
1087  tables = re->tables;  /* Return NULL if no optimization is possible. */
1088  if (tables == NULL)  
1089    (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,  if (!bits_set && min < 0) return NULL;
   (void *)(&tables));  
   
 compile_block.lcc = tables + lcc_offset;  
 compile_block.fcc = tables + fcc_offset;  
 compile_block.cbits = tables + cbits_offset;  
 compile_block.ctypes = tables + ctypes_offset;  
   
 /* See if we can find a fixed set of initial characters for the pattern. */  
   
 memset(start_bits, 0, 32 * sizeof(uschar));  
 if (set_start_bits(code, start_bits, (re->options & PCRE_CASELESS) != 0,  
   (re->options & PCRE_UTF8) != 0, &compile_block) != SSB_DONE) return NULL;  
1090    
1091  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in
1092  the latter, which is pointed to by the former, which may also get additional  the latter, which is pointed to by the former, which may also get additional
# Line 578  extra->flags = PCRE_EXTRA_STUDY_DATA; Line 1109  extra->flags = PCRE_EXTRA_STUDY_DATA;
1109  extra->study_data = study;  extra->study_data = study;
1110    
1111  study->size = sizeof(pcre_study_data);  study->size = sizeof(pcre_study_data);
1112  study->options = PCRE_STUDY_MAPPED;  study->flags = 0;
1113  memcpy(study->start_bits, start_bits, sizeof(start_bits));  
1114    if (bits_set)
1115      {
1116      study->flags |= PCRE_STUDY_MAPPED;
1117      memcpy(study->start_bits, start_bits, sizeof(start_bits));
1118      }
1119    
1120    if (min >= 0)
1121      {
1122      study->flags |= PCRE_STUDY_MINLEN;
1123      study->minlength = min;
1124      }
1125    
1126  return extra;  return extra;
1127  }  }

Legend:
Removed from v.342  
changed lines
  Added in v.550

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12