@@ -1216,6 +1216,7 @@ compile_branch(int options, int *brackets, uschar **codeptr,
12161216int repeat_type , op_type ;
12171217int repeat_min , repeat_max ;
12181218int bravalue , length ;
1219+ int greedy_default , greedy_non_default ;
12191220register int c ;
12201221register uschar * code = * codeptr ;
12211222const uschar * ptr = * ptrptr ;
@@ -1224,6 +1225,11 @@ uschar *previous = NULL;
12241225uschar class [32 ];
12251226uschar * class_flag ; /* Pointer to the single-byte flag for OP_CLASS_L */
12261227
1228+ /* Set up the default and non-default settings for greediness */
1229+
1230+ greedy_default = ((options & PCRE_UNGREEDY ) != 0 );
1231+ greedy_non_default = greedy_default ^ 1 ;
1232+
12271233/* Switch on next character until the end of the branch */
12281234
12291235for (;; ptr ++ )
@@ -1536,10 +1542,13 @@ for (;; ptr++)
15361542 goto FAILED ;
15371543 }
15381544
1539- /* If the next character is '?' this is a minimizing repeat. Advance to the
1545+ /* If the next character is '?' this is a minimizing repeat, by default,
1546+ but if PCRE_UNGREEDY is set, it works the other way round. Advance to the
15401547 next character. */
15411548
1542- if (ptr [1 ] == '?' ) { repeat_type = 1 ; ptr ++ ; } else repeat_type = 0 ;
1549+ if (ptr [1 ] == '?' )
1550+ { repeat_type = greedy_non_default ; ptr ++ ; }
1551+ else repeat_type = greedy_default ;
15431552
15441553 /* If the maximum is zero then the minimum must also be zero; Perl allows
15451554 this case, so we do too - by simply omitting the item altogether. */
@@ -1628,14 +1637,20 @@ for (;; ptr++)
16281637 /* If the mininum is 1 and the previous item was a character string,
16291638 we either have to put back the item that got cancelled if the string
16301639 length was 1, or add the character back onto the end of a longer
1631- string. For a character type nothing need be done; it will just get put
1632- back naturally. */
1640+ string. For a character type nothing need be done; it will just get
1641+ put back naturally. Note that the final character is always going to
1642+ get added below. */
16331643
16341644 else if (* previous == OP_CHARS )
16351645 {
16361646 if (code == previous ) code += 2 ; else previous [1 ]++ ;
16371647 }
16381648
1649+ /* For a single negated character we also have to put back the
1650+ item that got cancelled. */
1651+
1652+ else if (* previous == OP_NOT ) code ++ ;
1653+
16391654 /* If the maximum is unlimited, insert an OP_STAR. */
16401655
16411656 if (repeat_max < 0 )
@@ -2484,7 +2499,7 @@ while ((c = *(++ptr)) != 0)
24842499 ptr += 2 ;
24852500 break ;
24862501 }
2487- /* Else fall thourh */
2502+ /* Else fall through */
24882503
24892504 /* Else loop setting valid options until ) is met. Anything else is an
24902505 error. */
@@ -2725,14 +2740,15 @@ printf("Length = %d top_bracket = %d top_backref=%d\n",
27252740
27262741if (re -> options != 0 )
27272742 {
2728- printf ("%s%s%s%s%s%s%s\n" ,
2743+ printf ("%s%s%s%s%s%s%s%s \n" ,
27292744 ((re -> options & PCRE_ANCHORED ) != 0 )? "anchored " : "" ,
27302745 ((re -> options & PCRE_CASELESS ) != 0 )? "caseless " : "" ,
27312746 ((re -> options & PCRE_EXTENDED ) != 0 )? "extended " : "" ,
27322747 ((re -> options & PCRE_MULTILINE ) != 0 )? "multiline " : "" ,
27332748 ((re -> options & PCRE_DOTALL ) != 0 )? "dotall " : "" ,
27342749 ((re -> options & PCRE_DOLLAR_ENDONLY ) != 0 )? "endonly " : "" ,
2735- ((re -> options & PCRE_EXTRA ) != 0 )? "extra " : "" );
2750+ ((re -> options & PCRE_EXTRA ) != 0 )? "extra " : "" ,
2751+ ((re -> options & PCRE_UNGREEDY ) != 0 )? "ungreedy " : "" );
27362752 }
27372753
27382754if ((re -> options & PCRE_FIRSTSET ) != 0 )
@@ -3070,7 +3086,7 @@ static int grow_stack(match_data *md)
30703086 if (md -> offset_top == NULL || md -> eptr == NULL || md -> ecode == NULL ||
30713087 md -> off_num == NULL || md -> r1 == NULL || md -> r2 == NULL )
30723088 {
3073- PyErr_SetString ( PyExc_MemoryError , "Can't increase failure stack for re operation" );
3089+ PyErr_NoMemory ( );
30743090 longjmp (md -> error_env , 1 );
30753091 }
30763092 return 0 ;
0 commit comments