darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1 | /* This is JavaScriptCore's variant of the PCRE library. While this library |
| 2 | started out as a copy of PCRE, many of the features of PCRE have been |
| 3 | removed. This library now supports only the regular expression features |
| 4 | required by the JavaScript language specification, and has only the functions |
| 5 | needed by JavaScriptCore and the rest of WebKit. |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 6 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 7 | Originally written by Philip Hazel |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 8 | Copyright (c) 1997-2006 University of Cambridge |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 9 | Copyright (C) 2002, 2004, 2006, 2007 Apple Inc. All rights reserved. |
| 10 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 11 | ----------------------------------------------------------------------------- |
| 12 | Redistribution and use in source and binary forms, with or without |
| 13 | modification, are permitted provided that the following conditions are met: |
| 14 | |
| 15 | * Redistributions of source code must retain the above copyright notice, |
| 16 | this list of conditions and the following disclaimer. |
| 17 | |
| 18 | * Redistributions in binary form must reproduce the above copyright |
| 19 | notice, this list of conditions and the following disclaimer in the |
| 20 | documentation and/or other materials provided with the distribution. |
| 21 | |
| 22 | * Neither the name of the University of Cambridge nor the names of its |
| 23 | contributors may be used to endorse or promote products derived from |
| 24 | this software without specific prior written permission. |
| 25 | |
| 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 27 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 28 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 29 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 30 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 31 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 32 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 33 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 34 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 35 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 | POSSIBILITY OF SUCH DAMAGE. |
| 37 | ----------------------------------------------------------------------------- |
| 38 | */ |
| 39 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 40 | /* This module contains jsRegExpExecute(), the externally visible function |
| 41 | that does pattern matching using an NFA algorithm, following the rules from |
| 42 | the JavaScript specification. There are also some supporting functions. */ |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 43 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 44 | #include "pcre_internal.h" |
| 45 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 46 | #include <wtf/ASCIICType.h> |
| 47 | #include <wtf/Vector.h> |
| 48 | |
| 49 | using namespace WTF; |
| 50 | |
| 51 | #ifdef __GNUC__ |
| 52 | #define USE_COMPUTED_GOTO_FOR_MATCH_RECURSION |
| 53 | //#define USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP |
| 54 | #endif |
| 55 | |
darin | b847b44 | 2006-10-27 16:48:28 +0000 | [diff] [blame] | 56 | /* Avoid warnings on Windows. */ |
| 57 | #undef min |
| 58 | #undef max |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 59 | |
| 60 | /* Structure for building a chain of data that actually lives on the |
| 61 | stack, for holding the values of the subject pointer at the start of each |
| 62 | subpattern, so as to detect when an empty string has been matched by a |
| 63 | subpattern - to break infinite loops. When NO_RECURSE is set, these blocks |
| 64 | are on the heap, not on the stack. */ |
| 65 | |
| 66 | typedef struct eptrblock { |
| 67 | struct eptrblock *epb_prev; |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 68 | USPTR epb_saved_eptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 69 | } eptrblock; |
| 70 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 71 | /* Structure for remembering the local variables in a private frame */ |
| 72 | |
| 73 | typedef struct matchframe { |
| 74 | /* Where to jump back to */ |
| 75 | #ifndef USE_COMPUTED_GOTO_FOR_MATCH_RECURSION |
| 76 | int where; |
| 77 | #else |
| 78 | void *where; |
| 79 | #endif |
| 80 | |
| 81 | struct matchframe *prevframe; |
| 82 | |
| 83 | /* Function arguments that may change */ |
| 84 | |
| 85 | const pcre_uchar *eptr; |
| 86 | const uschar *ecode; |
| 87 | int offset_top; |
| 88 | eptrblock *eptrb; |
| 89 | |
| 90 | /* Function local variables */ |
| 91 | |
| 92 | const uschar *data; |
| 93 | const uschar *next; |
| 94 | const pcre_uchar *pp; |
| 95 | const uschar *prev; |
| 96 | const pcre_uchar *saved_eptr; |
| 97 | |
| 98 | int repeat_othercase; |
| 99 | |
| 100 | int ctype; |
| 101 | int fc; |
| 102 | int fi; |
| 103 | int length; |
| 104 | int max; |
| 105 | int number; |
| 106 | int offset; |
| 107 | int save_offset1, save_offset2, save_offset3; |
| 108 | |
| 109 | eptrblock newptrb; |
| 110 | } matchframe; |
| 111 | |
| 112 | /* Structure for passing "static" information around between the functions |
| 113 | doing traditional NFA matching, so that they are thread-safe. */ |
| 114 | |
| 115 | typedef struct match_data { |
| 116 | unsigned long int match_call_count; /* As it says */ |
| 117 | int *offset_vector; /* Offset vector */ |
| 118 | int offset_end; /* One past the end */ |
| 119 | int offset_max; /* The maximum usable for return data */ |
| 120 | const uschar *lcc; /* Points to lower casing table */ |
| 121 | const uschar *ctypes; /* Points to table of type maps */ |
| 122 | BOOL offset_overflow; /* Set if too many extractions */ |
| 123 | USPTR start_subject; /* Start of the subject string */ |
| 124 | USPTR end_subject; /* End of the subject string */ |
| 125 | USPTR end_match_ptr; /* Subject position at end match */ |
| 126 | int end_offset_top; /* Highwater mark at end of match */ |
| 127 | BOOL multiline; |
| 128 | BOOL caseless; |
| 129 | } match_data; |
| 130 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 131 | #define match_isgroup TRUE /* Set if start of bracketed group */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 132 | |
| 133 | /* Non-error returns from the match() function. Error returns are externally |
| 134 | defined PCRE_ERROR_xxx codes, which are all negative. */ |
| 135 | |
| 136 | #define MATCH_MATCH 1 |
| 137 | #define MATCH_NOMATCH 0 |
| 138 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 139 | /* Min and max values for the common repeats; for the maxima, 0 => infinity */ |
| 140 | |
| 141 | static const char rep_min[] = { 0, 0, 1, 1, 0, 0 }; |
| 142 | static const char rep_max[] = { 0, 0, 0, 0, 1, 1 }; |
| 143 | |
| 144 | |
| 145 | |
| 146 | #ifdef DEBUG |
| 147 | /************************************************* |
| 148 | * Debugging function to print chars * |
| 149 | *************************************************/ |
| 150 | |
| 151 | /* Print a sequence of chars in printable format, stopping at the end of the |
| 152 | subject if the requested. |
| 153 | |
| 154 | Arguments: |
| 155 | p points to characters |
| 156 | length number to print |
| 157 | is_subject TRUE if printing from within md->start_subject |
| 158 | md pointer to matching data block, if is_subject is TRUE |
| 159 | |
| 160 | Returns: nothing |
| 161 | */ |
| 162 | |
| 163 | static void |
| 164 | pchars(const pcre_uchar *p, int length, BOOL is_subject, match_data *md) |
| 165 | { |
| 166 | int c; |
| 167 | if (is_subject && length > md->end_subject - p) length = md->end_subject - p; |
| 168 | while (length-- > 0) |
| 169 | if (isprint(c = *(p++))) printf("%c", c); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 170 | else if (c < 256) printf("\\x%02x", c); |
| 171 | else printf("\\x{%x}", c); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 172 | } |
| 173 | #endif |
| 174 | |
| 175 | |
| 176 | |
| 177 | /************************************************* |
| 178 | * Match a back-reference * |
| 179 | *************************************************/ |
| 180 | |
| 181 | /* If a back reference hasn't been set, the length that is passed is greater |
| 182 | than the number of characters left in the string, so the match fails. |
| 183 | |
| 184 | Arguments: |
| 185 | offset index into the offset vector |
| 186 | eptr points into the subject |
| 187 | length length to be matched |
| 188 | md points to match data block |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 189 | |
| 190 | Returns: TRUE if matched |
| 191 | */ |
| 192 | |
| 193 | static BOOL |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 194 | match_ref(int offset, register USPTR eptr, int length, match_data *md) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 195 | { |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 196 | USPTR p = md->start_subject + md->offset_vector[offset]; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 197 | |
| 198 | #ifdef DEBUG |
| 199 | if (eptr >= md->end_subject) |
| 200 | printf("matching subject <null>"); |
| 201 | else |
| 202 | { |
| 203 | printf("matching subject "); |
| 204 | pchars(eptr, length, TRUE, md); |
| 205 | } |
| 206 | printf(" against backref "); |
| 207 | pchars(p, length, FALSE, md); |
| 208 | printf("\n"); |
| 209 | #endif |
| 210 | |
| 211 | /* Always fail if not enough characters left */ |
| 212 | |
| 213 | if (length > md->end_subject - eptr) return FALSE; |
| 214 | |
| 215 | /* Separate the caselesss case for speed */ |
| 216 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 217 | if (md->caseless) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 218 | { |
| 219 | while (length-- > 0) |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 220 | { |
| 221 | pcre_uchar c = *p++; |
| 222 | int othercase = _pcre_ucp_othercase(c); |
| 223 | pcre_uchar d = *eptr++; |
| 224 | if (c != d && othercase != d) return FALSE; |
| 225 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 226 | } |
| 227 | else |
| 228 | { while (length-- > 0) if (*p++ != *eptr++) return FALSE; } |
| 229 | |
| 230 | return TRUE; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | /*************************************************************************** |
| 236 | **************************************************************************** |
| 237 | RECURSION IN THE match() FUNCTION |
| 238 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 239 | The original match() function was highly recursive. The current version |
| 240 | still has the remnants of the original in that recursive processing of the |
| 241 | regular expression is triggered by invoking a macro named RMATCH. This is |
| 242 | no longer really much like a recursive call to match() itself. |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 243 | **************************************************************************** |
| 244 | ***************************************************************************/ |
| 245 | |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 246 | /* These versions of the macros use the stack, as normal. There are debugging |
| 247 | versions and production versions. */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 248 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 249 | #ifndef USE_COMPUTED_GOTO_FOR_MATCH_RECURSION |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 250 | |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 251 | /* Use numbered labels and switch statement at the bottom of the match function. */ |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 252 | |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 253 | #define RMATCH_WHERE(num) num |
| 254 | #define RRETURN_LABEL RRETURN_SWITCH |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 255 | |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 256 | #else |
| 257 | |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 258 | /* Use GCC's computed goto extension. */ |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 259 | |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 260 | /* For one test case this is more than 40% faster than the switch statement. |
| 261 | We could avoid the use of the num argument entirely by using local labels, |
| 262 | but using it for the GCC case as well as the non-GCC case allows us to share |
| 263 | a bit more code and notice if we use conflicting numbers.*/ |
| 264 | |
| 265 | #define RMATCH_WHERE(num) &&RRETURN_##num |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 266 | #define RRETURN_LABEL *frame->where |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 267 | |
| 268 | #endif |
| 269 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 270 | #define RMATCH(num, ra, rb, rc)\ |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 271 | {\ |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 272 | if (frame >= stackframes && frame + 1 < stackframesend)\ |
| 273 | newframe = frame + 1;\ |
| 274 | else\ |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 275 | newframe = new matchframe;\ |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 276 | newframe->eptr = frame->eptr;\ |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 277 | newframe->ecode = (ra);\ |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 278 | newframe->offset_top = frame->offset_top;\ |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 279 | newframe->eptrb = (rb);\ |
| 280 | is_group_start = (rc);\ |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 281 | ++rdepth;\ |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 282 | newframe->prevframe = frame;\ |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 283 | frame = newframe;\ |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 284 | frame->where = RMATCH_WHERE(num);\ |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 285 | DPRINTF(("restarting from line %d\n", __LINE__));\ |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 286 | goto RECURSE;\ |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 287 | RRETURN_##num:\ |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 288 | newframe = frame;\ |
| 289 | frame = frame->prevframe;\ |
| 290 | if (!(newframe >= stackframes && newframe < stackframesend))\ |
| 291 | delete newframe;\ |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 292 | --rdepth;\ |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 293 | DPRINTF(("did a goto back to line %d\n", __LINE__));\ |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 294 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 295 | |
| 296 | #define RRETURN goto RRETURN_LABEL |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 297 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 298 | #define RRETURN_NO_MATCH \ |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 299 | {\ |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 300 | is_match = FALSE;\ |
| 301 | RRETURN;\ |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 302 | } |
| 303 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 304 | #define RRETURN_ERROR(error) \ |
| 305 | { \ |
| 306 | i = (error); \ |
| 307 | goto RETURN_ERROR; \ |
| 308 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 309 | |
| 310 | /************************************************* |
| 311 | * Match from current position * |
| 312 | *************************************************/ |
| 313 | |
| 314 | /* On entry ecode points to the first opcode, and eptr to the first character |
| 315 | in the subject string, while eptrb holds the value of eptr at the start of the |
| 316 | last bracketed group - used for breaking infinite loops matching zero-length |
| 317 | strings. This function is called recursively in many circumstances. Whenever it |
| 318 | returns a negative (error) response, the outer incarnation must also return the |
| 319 | same response. |
| 320 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 321 | Arguments: |
| 322 | eptr pointer in subject |
| 323 | ecode position in code |
| 324 | offset_top current top pointer |
| 325 | md pointer to "static" info for the match |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 326 | |
| 327 | Returns: MATCH_MATCH if matched ) these values are >= 0 |
| 328 | MATCH_NOMATCH if failed to match ) |
| 329 | a negative PCRE_ERROR_xxx value if aborted by an error condition |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 330 | (e.g. stopped by repeated call or recursion limit) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 331 | */ |
| 332 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 333 | static int match(USPTR eptr, const uschar *ecode, int offset_top, match_data *md) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 334 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 335 | register int is_match = FALSE; |
| 336 | register int i; |
| 337 | register int c; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 338 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 339 | unsigned rdepth = 0; |
| 340 | |
| 341 | BOOL cur_is_word; |
| 342 | BOOL prev_is_word; |
| 343 | BOOL is_group_start = TRUE; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 344 | int min; |
| 345 | BOOL minimize = FALSE; /* Initialization not really needed, but some compilers think so. */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 346 | |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 347 | /* The value 16 here is large enough that most regular expressions don't require |
| 348 | any calls to pcre_stack_malloc, yet the amount of stack used for the array is |
| 349 | modest enough that we don't run out of stack. */ |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 350 | matchframe stackframes[16]; |
| 351 | matchframe *stackframesend = stackframes + sizeof(stackframes) / sizeof(stackframes[0]); |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 352 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 353 | matchframe *frame = stackframes; |
| 354 | matchframe *newframe; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 355 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 356 | /* The opcode jump table. */ |
| 357 | #ifdef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP |
| 358 | #define EMIT_JUMP_TABLE_ENTRY(opcode) &&LABEL_OP_##opcode, |
| 359 | static void* opcode_jump_table[256] = { FOR_EACH_OPCODE(EMIT_JUMP_TABLE_ENTRY) }; |
| 360 | #undef EMIT_JUMP_TABLE_ENTRY |
| 361 | #endif |
| 362 | |
| 363 | /* One-time setup of the opcode jump table. */ |
| 364 | #ifdef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP |
| 365 | i = 255; |
| 366 | while (!opcode_jump_table[i]) |
| 367 | opcode_jump_table[i--] = &&CAPTURING_BRACKET; |
| 368 | #endif |
| 369 | |
| 370 | #ifdef USE_COMPUTED_GOTO_FOR_MATCH_RECURSION |
| 371 | frame->where = &&RETURN; |
| 372 | #else |
| 373 | frame->where = 0; |
| 374 | #endif |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 375 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 376 | frame->eptr = eptr; |
| 377 | frame->ecode = ecode; |
| 378 | frame->offset_top = offset_top; |
| 379 | frame->eptrb = NULL; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 380 | |
| 381 | /* This is where control jumps back to to effect "recursion" */ |
| 382 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 383 | RECURSE: |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 384 | |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 385 | /* OK, now we can get on with the real code of the function. Recursive calls |
| 386 | are specified by the macro RMATCH and RRETURN is used to return. When |
| 387 | NO_RECURSE is *not* defined, these just turn into a recursive call to match() |
| 388 | and a "return", respectively (possibly with some debugging if DEBUG is |
| 389 | defined). However, RMATCH isn't like a function call because it's quite a |
| 390 | complicated macro. It has to be used in one particular way. This shouldn't, |
| 391 | however, impact performance when true recursion is being used. */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 392 | |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 393 | /* First check that we haven't called match() too many times, or that we |
| 394 | haven't exceeded the recursive call limit. */ |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 395 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 396 | if (md->match_call_count++ >= MATCH_LIMIT) RRETURN_ERROR(JSRegExpErrorMatchLimit); |
| 397 | if (rdepth >= MATCH_LIMIT_RECURSION) RRETURN_ERROR(JSRegExpErrorRecursionLimit); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 398 | |
| 399 | /* At the start of a bracketed group, add the current subject pointer to the |
| 400 | stack of such pointers, to be re-instated at the end of the group when we hit |
| 401 | the closing ket. When match() is called in other circumstances, we don't add to |
| 402 | this stack. */ |
| 403 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 404 | if (is_group_start) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 405 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 406 | frame->newptrb.epb_prev = frame->eptrb; |
| 407 | frame->newptrb.epb_saved_eptr = frame->eptr; |
| 408 | frame->eptrb = &frame->newptrb; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* Now start processing the operations. */ |
| 412 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 413 | #ifndef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 414 | for (;;) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 415 | #endif |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 416 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 417 | |
| 418 | #ifdef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP |
| 419 | #define BEGIN_OPCODE(opcode) LABEL_OP_##opcode |
| 420 | #define NEXT_OPCODE goto *opcode_jump_table[*frame->ecode] |
| 421 | #else |
| 422 | #define BEGIN_OPCODE(opcode) case OP_##opcode |
| 423 | #define NEXT_OPCODE continue |
| 424 | #endif |
| 425 | |
| 426 | #ifdef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP |
| 427 | NEXT_OPCODE; |
| 428 | #else |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 429 | switch (*frame->ecode) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 430 | #endif |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 431 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 432 | /* Non-capturing bracket: optimized */ |
| 433 | |
| 434 | BEGIN_OPCODE(BRA): |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 435 | NON_CAPTURING_BRACKET: |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 436 | DPRINTF(("start bracket 0\n")); |
| 437 | do |
| 438 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 439 | RMATCH(2, frame->ecode + 1 + LINK_SIZE, frame->eptrb, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 440 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 441 | frame->ecode += GET(frame->ecode, 1); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 442 | } |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 443 | while (*frame->ecode == OP_ALT); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 444 | DPRINTF(("bracket 0 failed\n")); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 445 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 446 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 447 | /* End of the pattern. */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 448 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 449 | BEGIN_OPCODE(END): |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 450 | md->end_match_ptr = frame->eptr; /* Record where we ended */ |
| 451 | md->end_offset_top = frame->offset_top; /* and how many extracts were taken */ |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 452 | is_match = TRUE; |
| 453 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 454 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 455 | /* Assertion brackets. Check the alternative branches in turn - the |
| 456 | matching won't pass the KET for an assertion. If any one branch matches, |
| 457 | the assertion is true. Lookbehind assertions have an OP_REVERSE item at the |
| 458 | start of each branch to move the current point backwards, so the code at |
| 459 | this level is identical to the lookahead case. */ |
| 460 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 461 | BEGIN_OPCODE(ASSERT): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 462 | do |
| 463 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 464 | RMATCH(6, frame->ecode + 1 + LINK_SIZE, NULL, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 465 | if (is_match) break; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 466 | frame->ecode += GET(frame->ecode, 1); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 467 | } |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 468 | while (*frame->ecode == OP_ALT); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 469 | if (*frame->ecode == OP_KET) RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 470 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 471 | /* Continue from after the assertion, updating the offsets high water |
| 472 | mark, since extracts may have been taken during the assertion. */ |
| 473 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 474 | do frame->ecode += GET(frame->ecode,1); while (*frame->ecode == OP_ALT); |
| 475 | frame->ecode += 1 + LINK_SIZE; |
| 476 | frame->offset_top = md->end_offset_top; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 477 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 478 | |
| 479 | /* Negative assertion: all branches must fail to match */ |
| 480 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 481 | BEGIN_OPCODE(ASSERT_NOT): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 482 | do |
| 483 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 484 | RMATCH(7, frame->ecode + 1 + LINK_SIZE, NULL, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 485 | if (is_match) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 486 | frame->ecode += GET(frame->ecode,1); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 487 | } |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 488 | while (*frame->ecode == OP_ALT); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 489 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 490 | frame->ecode += 1 + LINK_SIZE; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 491 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 492 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 493 | /* "Once" brackets are like assertion brackets except that after a match, |
| 494 | the point in the subject string is not moved back. Thus there can never be |
| 495 | a move back into the brackets. Friedl calls these "atomic" subpatterns. |
| 496 | Check the alternative branches in turn - the matching won't pass the KET |
| 497 | for this kind of subpattern. If any one branch matches, we carry on as at |
| 498 | the end of a normal bracket, leaving the subject pointer. */ |
| 499 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 500 | BEGIN_OPCODE(ONCE): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 501 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 502 | frame->prev = frame->ecode; |
| 503 | frame->saved_eptr = frame->eptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 504 | |
| 505 | do |
| 506 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 507 | RMATCH(9, frame->ecode + 1 + LINK_SIZE, frame->eptrb, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 508 | if (is_match) break; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 509 | frame->ecode += GET(frame->ecode,1); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 510 | } |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 511 | while (*frame->ecode == OP_ALT); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 512 | |
| 513 | /* If hit the end of the group (which could be repeated), fail */ |
| 514 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 515 | if (*frame->ecode != OP_ONCE && *frame->ecode != OP_ALT) RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 516 | |
| 517 | /* Continue as from after the assertion, updating the offsets high water |
| 518 | mark, since extracts may have been taken. */ |
| 519 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 520 | do frame->ecode += GET(frame->ecode,1); while (*frame->ecode == OP_ALT); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 521 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 522 | frame->offset_top = md->end_offset_top; |
| 523 | frame->eptr = md->end_match_ptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 524 | |
| 525 | /* For a non-repeating ket, just continue at this level. This also |
| 526 | happens for a repeating ket if no characters were matched in the group. |
| 527 | This is the forcible breaking of infinite loops as implemented in Perl |
| 528 | 5.005. If there is an options reset, it will get obeyed in the normal |
| 529 | course of events. */ |
| 530 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 531 | if (*frame->ecode == OP_KET || frame->eptr == frame->saved_eptr) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 532 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 533 | frame->ecode += 1+LINK_SIZE; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 534 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* The repeating kets try the rest of the pattern or restart from the |
| 538 | preceding bracket, in the appropriate order. We need to reset any options |
| 539 | that changed within the bracket before re-running it, so check the next |
| 540 | opcode. */ |
| 541 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 542 | if (*frame->ecode == OP_KETRMIN) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 543 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 544 | RMATCH(10, frame->ecode + 1 + LINK_SIZE, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 545 | if (is_match) RRETURN; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 546 | RMATCH(11, frame->prev, frame->eptrb, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 547 | if (is_match) RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 548 | } |
| 549 | else /* OP_KETRMAX */ |
| 550 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 551 | RMATCH(12, frame->prev, frame->eptrb, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 552 | if (is_match) RRETURN; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 553 | RMATCH(13, frame->ecode + 1+LINK_SIZE, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 554 | if (is_match) RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 557 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 558 | |
| 559 | /* An alternation is the end of a branch; scan along to find the end of the |
| 560 | bracketed group and go to there. */ |
| 561 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 562 | BEGIN_OPCODE(ALT): |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 563 | do frame->ecode += GET(frame->ecode,1); while (*frame->ecode == OP_ALT); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 564 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 565 | |
| 566 | /* BRAZERO and BRAMINZERO occur just before a bracket group, indicating |
| 567 | that it may occur zero times. It may repeat infinitely, or not at all - |
| 568 | i.e. it could be ()* or ()? in the pattern. Brackets with fixed upper |
| 569 | repeat limits are compiled as a number of copies, with the optional ones |
| 570 | preceded by BRAZERO or BRAMINZERO. */ |
| 571 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 572 | BEGIN_OPCODE(BRAZERO): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 573 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 574 | frame->next = frame->ecode+1; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 575 | RMATCH(14, frame->next, frame->eptrb, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 576 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 577 | do frame->next += GET(frame->next,1); while (*frame->next == OP_ALT); |
| 578 | frame->ecode = frame->next + 1+LINK_SIZE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 579 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 580 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 581 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 582 | BEGIN_OPCODE(BRAMINZERO): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 583 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 584 | frame->next = frame->ecode+1; |
| 585 | do frame->next += GET(frame->next,1); while (*frame->next == OP_ALT); |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 586 | RMATCH(15, frame->next + 1+LINK_SIZE, frame->eptrb, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 587 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 588 | frame->ecode++; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 589 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 590 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 591 | |
| 592 | /* End of a group, repeated or non-repeating. If we are at the end of |
| 593 | an assertion "group", stop matching and return MATCH_MATCH, but record the |
| 594 | current high water mark for use by positive assertions. Do this also |
| 595 | for the "once" (not-backup up) groups. */ |
| 596 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 597 | BEGIN_OPCODE(KET): |
| 598 | BEGIN_OPCODE(KETRMIN): |
| 599 | BEGIN_OPCODE(KETRMAX): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 600 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 601 | frame->prev = frame->ecode - GET(frame->ecode, 1); |
| 602 | frame->saved_eptr = frame->eptrb->epb_saved_eptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 603 | |
| 604 | /* Back up the stack of bracket start pointers. */ |
| 605 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 606 | frame->eptrb = frame->eptrb->epb_prev; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 607 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 608 | if (*frame->prev == OP_ASSERT || *frame->prev == OP_ASSERT_NOT || *frame->prev == OP_ONCE) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 609 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 610 | md->end_match_ptr = frame->eptr; /* For ONCE */ |
| 611 | md->end_offset_top = frame->offset_top; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 612 | is_match = TRUE; |
| 613 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* In all other cases except a conditional group we have to check the |
| 617 | group number back at the start and if necessary complete handling an |
| 618 | extraction by setting the offsets and bumping the high water mark. */ |
| 619 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 620 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 621 | frame->number = *frame->prev - OP_BRA; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 622 | |
| 623 | /* For extended extraction brackets (large number), we have to fish out |
| 624 | the number from a dummy opcode at the start. */ |
| 625 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 626 | if (frame->number > EXTRACT_BASIC_MAX) frame->number = GET2(frame->prev, 2+LINK_SIZE); |
| 627 | frame->offset = frame->number << 1; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 628 | |
| 629 | #ifdef DEBUG |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 630 | printf("end bracket %d", frame->number); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 631 | printf("\n"); |
| 632 | #endif |
| 633 | |
| 634 | /* Test for a numbered group. This includes groups called as a result |
| 635 | of recursion. Note that whole-pattern recursion is coded as a recurse |
| 636 | into group 0, so it won't be picked up here. Instead, we catch it when |
| 637 | the OP_END is reached. */ |
| 638 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 639 | if (frame->number > 0) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 640 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 641 | if (frame->offset >= md->offset_max) md->offset_overflow = TRUE; else |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 642 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 643 | md->offset_vector[frame->offset] = |
| 644 | md->offset_vector[md->offset_end - frame->number]; |
| 645 | md->offset_vector[frame->offset+1] = frame->eptr - md->start_subject; |
| 646 | if (frame->offset_top <= frame->offset) frame->offset_top = frame->offset + 2; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 647 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 651 | /* For a non-repeating ket, just continue at this level. This also |
| 652 | happens for a repeating ket if no characters were matched in the group. |
| 653 | This is the forcible breaking of infinite loops as implemented in Perl |
| 654 | 5.005. If there is an options reset, it will get obeyed in the normal |
| 655 | course of events. */ |
| 656 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 657 | if (*frame->ecode == OP_KET || frame->eptr == frame->saved_eptr) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 658 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 659 | frame->ecode += 1 + LINK_SIZE; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 660 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /* The repeating kets try the rest of the pattern or restart from the |
| 664 | preceding bracket, in the appropriate order. */ |
| 665 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 666 | if (*frame->ecode == OP_KETRMIN) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 667 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 668 | RMATCH(16, frame->ecode + 1+LINK_SIZE, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 669 | if (is_match) RRETURN; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 670 | RMATCH(17, frame->prev, frame->eptrb, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 671 | if (is_match) RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 672 | } |
| 673 | else /* OP_KETRMAX */ |
| 674 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 675 | RMATCH(18, frame->prev, frame->eptrb, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 676 | if (is_match) RRETURN; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 677 | RMATCH(19, frame->ecode + 1+LINK_SIZE, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 678 | if (is_match) RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 681 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 682 | |
| 683 | /* Start of subject unless notbol, or after internal newline if multiline */ |
| 684 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 685 | BEGIN_OPCODE(CIRC): |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 686 | if (md->multiline) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 687 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 688 | if (frame->eptr != md->start_subject && !IS_NEWLINE(frame->eptr[-1])) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 689 | RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 690 | frame->ecode++; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 691 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 692 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 693 | if (frame->eptr != md->start_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 694 | frame->ecode++; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 695 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 696 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 697 | /* Assert before internal newline if multiline, or before a terminating |
| 698 | newline unless endonly is set, else end of subject unless noteol is set. */ |
| 699 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 700 | BEGIN_OPCODE(DOLL): |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 701 | if (md->multiline) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 702 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 703 | if (frame->eptr < md->end_subject) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 704 | { if (!IS_NEWLINE(*frame->eptr)) RRETURN_NO_MATCH; } |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 705 | frame->ecode++; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 706 | } |
| 707 | else |
| 708 | { |
darin@apple.com | bb14d63 | 2007-11-04 07:54:56 +0000 | [diff] [blame] | 709 | if (frame->eptr < md->end_subject - 1 || |
| 710 | (frame->eptr == md->end_subject - 1 && !IS_NEWLINE(*frame->eptr))) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 711 | RRETURN_NO_MATCH; |
darin@apple.com | bb14d63 | 2007-11-04 07:54:56 +0000 | [diff] [blame] | 712 | frame->ecode++; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 713 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 714 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 715 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 716 | /* Word boundary assertions */ |
| 717 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 718 | BEGIN_OPCODE(NOT_WORD_BOUNDARY): |
| 719 | BEGIN_OPCODE(WORD_BOUNDARY): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 720 | { |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 721 | /* Find out if the previous and current characters are "word" characters. |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 722 | It takes a bit more work in UTF-8 mode. Characters > 128 are assumed to |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 723 | be "non-word" characters. */ |
| 724 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 725 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 726 | if (frame->eptr == md->start_subject) prev_is_word = FALSE; else |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 727 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 728 | const pcre_uchar *lastptr = frame->eptr - 1; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 729 | while(ISMIDCHAR(*lastptr)) lastptr--; |
| 730 | GETCHAR(c, lastptr); |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 731 | prev_is_word = c < 128 && (md->ctypes[c] & ctype_word) != 0; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 732 | } |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 733 | if (frame->eptr >= md->end_subject) cur_is_word = FALSE; else |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 734 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 735 | GETCHAR(c, frame->eptr); |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 736 | cur_is_word = c < 128 && (md->ctypes[c] & ctype_word) != 0; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 739 | |
| 740 | /* Now see if the situation is what we want */ |
| 741 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 742 | if ((*frame->ecode++ == OP_WORD_BOUNDARY)? |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 743 | cur_is_word == prev_is_word : cur_is_word != prev_is_word) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 744 | RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 745 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 746 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 747 | |
| 748 | /* Match a single character type; inline for speed */ |
| 749 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 750 | BEGIN_OPCODE(ANY): |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 751 | if (frame->eptr < md->end_subject && IS_NEWLINE(*frame->eptr)) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 752 | RRETURN_NO_MATCH; |
| 753 | if (frame->eptr++ >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 754 | while (frame->eptr < md->end_subject && ISMIDCHAR(*frame->eptr)) frame->eptr++; |
| 755 | frame->ecode++; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 756 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 757 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 758 | BEGIN_OPCODE(NOT_DIGIT): |
| 759 | if (frame->eptr >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 760 | GETCHARINCTEST(c, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 761 | if (isASCIIDigit(c)) |
| 762 | RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 763 | frame->ecode++; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 764 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 765 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 766 | BEGIN_OPCODE(DIGIT): |
| 767 | if (frame->eptr >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 768 | GETCHARINCTEST(c, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 769 | if (!isASCIIDigit(c)) |
| 770 | RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 771 | frame->ecode++; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 772 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 773 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 774 | BEGIN_OPCODE(NOT_WHITESPACE): |
| 775 | if (frame->eptr >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 776 | GETCHARINCTEST(c, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 777 | if (c < 128 && (md->ctypes[c] & ctype_space) != 0) |
| 778 | RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 779 | frame->ecode++; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 780 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 781 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 782 | BEGIN_OPCODE(WHITESPACE): |
| 783 | if (frame->eptr >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 784 | GETCHARINCTEST(c, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 785 | if (c >= 128 || (md->ctypes[c] & ctype_space) == 0) |
| 786 | RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 787 | frame->ecode++; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 788 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 789 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 790 | BEGIN_OPCODE(NOT_WORDCHAR): |
| 791 | if (frame->eptr >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 792 | GETCHARINCTEST(c, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 793 | if (c < 128 && (md->ctypes[c] & ctype_word) != 0) |
| 794 | RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 795 | frame->ecode++; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 796 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 797 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 798 | BEGIN_OPCODE(WORDCHAR): |
| 799 | if (frame->eptr >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 800 | GETCHARINCTEST(c, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 801 | if (c >= 128 || (md->ctypes[c] & ctype_word) == 0) |
| 802 | RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 803 | frame->ecode++; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 804 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 805 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 806 | /* Match a back reference, possibly repeatedly. Look past the end of the |
| 807 | item to see if there is repeat information following. The code is similar |
| 808 | to that for character classes, but repeated for efficiency. Then obey |
| 809 | similar code to character type repeats - written out again for speed. |
| 810 | However, if the referenced string is the empty string, always treat |
| 811 | it as matched, any number of times (otherwise there could be infinite |
| 812 | loops). */ |
| 813 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 814 | BEGIN_OPCODE(REF): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 815 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 816 | frame->offset = GET2(frame->ecode, 1) << 1; /* Doubled ref number */ |
| 817 | frame->ecode += 3; /* Advance past item */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 818 | |
| 819 | /* If the reference is unset, set the length to be longer than the amount |
| 820 | of subject left; this ensures that every attempt at a match fails. We |
| 821 | can't just fail here, because of the possibility of quantifiers with zero |
| 822 | minima. */ |
| 823 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 824 | frame->length = (frame->offset >= frame->offset_top || md->offset_vector[frame->offset] < 0)? |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 825 | 0 : |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 826 | md->offset_vector[frame->offset+1] - md->offset_vector[frame->offset]; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 827 | |
| 828 | /* Set up for repetition, or handle the non-repeated case */ |
| 829 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 830 | switch (*frame->ecode) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 831 | { |
| 832 | case OP_CRSTAR: |
| 833 | case OP_CRMINSTAR: |
| 834 | case OP_CRPLUS: |
| 835 | case OP_CRMINPLUS: |
| 836 | case OP_CRQUERY: |
| 837 | case OP_CRMINQUERY: |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 838 | c = *frame->ecode++ - OP_CRSTAR; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 839 | minimize = (c & 1) != 0; |
| 840 | min = rep_min[c]; /* Pick up values from tables; */ |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 841 | frame->max = rep_max[c]; /* zero for max => infinity */ |
| 842 | if (frame->max == 0) frame->max = INT_MAX; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 843 | break; |
| 844 | |
| 845 | case OP_CRRANGE: |
| 846 | case OP_CRMINRANGE: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 847 | minimize = (*frame->ecode == OP_CRMINRANGE); |
| 848 | min = GET2(frame->ecode, 1); |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 849 | frame->max = GET2(frame->ecode, 3); |
| 850 | if (frame->max == 0) frame->max = INT_MAX; |
| 851 | frame->ecode += 5; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 852 | break; |
| 853 | |
| 854 | default: /* No repeat follows */ |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 855 | if (!match_ref(frame->offset, frame->eptr, frame->length, md)) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 856 | frame->eptr += frame->length; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 857 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /* If the length of the reference is zero, just continue with the |
| 861 | main loop. */ |
| 862 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 863 | if (frame->length == 0) |
| 864 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 865 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 866 | /* First, ensure the minimum number of matches are present. */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 867 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 868 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 869 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 870 | if (!match_ref(frame->offset, frame->eptr, frame->length, md)) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 871 | frame->eptr += frame->length; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /* If min = max, continue at the same level without recursion. |
| 875 | They are not both allowed to be zero. */ |
| 876 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 877 | if (min == frame->max) |
| 878 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 879 | |
| 880 | /* If minimizing, keep trying and advancing the pointer */ |
| 881 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 882 | if (minimize) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 883 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 884 | for (frame->fi = min;; frame->fi++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 885 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 886 | RMATCH(20, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 887 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 888 | if (frame->fi >= frame->max || !match_ref(frame->offset, frame->eptr, frame->length, md)) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 889 | RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 890 | frame->eptr += frame->length; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 891 | } |
| 892 | /* Control never gets here */ |
| 893 | } |
| 894 | |
| 895 | /* If maximizing, find the longest string and work backwards */ |
| 896 | |
| 897 | else |
| 898 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 899 | frame->pp = frame->eptr; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 900 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 901 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 902 | if (!match_ref(frame->offset, frame->eptr, frame->length, md)) break; |
| 903 | frame->eptr += frame->length; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 904 | } |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 905 | while (frame->eptr >= frame->pp) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 906 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 907 | RMATCH(21, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 908 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 909 | frame->eptr -= frame->length; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 910 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 911 | RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | /* Control never gets here */ |
| 915 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 916 | /* Match a bit-mapped character class, possibly repeatedly. This op code is |
| 917 | used when all the characters in the class have values in the range 0-255, |
| 918 | and either the matching is caseful, or the characters are in the range |
| 919 | 0-127 when UTF-8 processing is enabled. The only difference between |
| 920 | OP_CLASS and OP_NCLASS occurs when a data character outside the range is |
| 921 | encountered. |
| 922 | |
| 923 | First, look past the end of the item to see if there is repeat information |
| 924 | following. Then obey similar code to character type repeats - written out |
| 925 | again for speed. */ |
| 926 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 927 | BEGIN_OPCODE(NCLASS): |
| 928 | BEGIN_OPCODE(CLASS): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 929 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 930 | frame->data = frame->ecode + 1; /* Save for matching */ |
| 931 | frame->ecode += 33; /* Advance past the item */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 932 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 933 | switch (*frame->ecode) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 934 | { |
| 935 | case OP_CRSTAR: |
| 936 | case OP_CRMINSTAR: |
| 937 | case OP_CRPLUS: |
| 938 | case OP_CRMINPLUS: |
| 939 | case OP_CRQUERY: |
| 940 | case OP_CRMINQUERY: |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 941 | c = *frame->ecode++ - OP_CRSTAR; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 942 | minimize = (c & 1) != 0; |
| 943 | min = rep_min[c]; /* Pick up values from tables; */ |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 944 | frame->max = rep_max[c]; /* zero for max => infinity */ |
| 945 | if (frame->max == 0) frame->max = INT_MAX; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 946 | break; |
| 947 | |
| 948 | case OP_CRRANGE: |
| 949 | case OP_CRMINRANGE: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 950 | minimize = (*frame->ecode == OP_CRMINRANGE); |
| 951 | min = GET2(frame->ecode, 1); |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 952 | frame->max = GET2(frame->ecode, 3); |
| 953 | if (frame->max == 0) frame->max = INT_MAX; |
| 954 | frame->ecode += 5; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 955 | break; |
| 956 | |
| 957 | default: /* No repeat follows */ |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 958 | min = frame->max = 1; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 959 | break; |
| 960 | } |
| 961 | |
| 962 | /* First, ensure the minimum number of matches are present. */ |
| 963 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 964 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 965 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 966 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 967 | if (frame->eptr >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 968 | GETCHARINC(c, frame->eptr); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 969 | if (c > 255) |
| 970 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 971 | if (frame->data[-1] == OP_CLASS) RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 972 | } |
| 973 | else |
| 974 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 975 | if ((frame->data[c/8] & (1 << (c&7))) == 0) RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 979 | |
| 980 | /* If max == min we can continue with the main loop without the |
| 981 | need to recurse. */ |
| 982 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 983 | if (min == frame->max) |
| 984 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 985 | |
| 986 | /* If minimizing, keep testing the rest of the expression and advancing |
| 987 | the pointer while it matches the class. */ |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 988 | if (minimize) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 989 | { |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 990 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 991 | for (frame->fi = min;; frame->fi++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 992 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 993 | RMATCH(22, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 994 | if (is_match) RRETURN; |
| 995 | if (frame->fi >= frame->max || frame->eptr >= md->end_subject) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 996 | GETCHARINC(c, frame->eptr); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 997 | if (c > 255) |
| 998 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 999 | if (frame->data[-1] == OP_CLASS) RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1000 | } |
| 1001 | else |
| 1002 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1003 | if ((frame->data[c/8] & (1 << (c&7))) == 0) RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1007 | /* Control never gets here */ |
| 1008 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1009 | /* If maximizing, find the longest possible run, then work backwards. */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1010 | else |
| 1011 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1012 | frame->pp = frame->eptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1013 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1014 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1015 | { |
| 1016 | int len = 1; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1017 | if (frame->eptr >= md->end_subject) break; |
| 1018 | GETCHARLEN(c, frame->eptr, len); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1019 | if (c > 255) |
| 1020 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1021 | if (frame->data[-1] == OP_CLASS) break; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1022 | } |
| 1023 | else |
| 1024 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1025 | if ((frame->data[c/8] & (1 << (c&7))) == 0) break; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1026 | } |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1027 | frame->eptr += len; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1028 | } |
| 1029 | for (;;) |
| 1030 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1031 | RMATCH(24, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1032 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1033 | if (frame->eptr-- == frame->pp) break; /* Stop if tried at original pos */ |
| 1034 | BACKCHAR(frame->eptr); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1035 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1036 | |
| 1037 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | /* Control never gets here */ |
| 1041 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1042 | /* Match an extended character class. This opcode is encountered only |
| 1043 | in UTF-8 mode, because that's the only time it is compiled. */ |
| 1044 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1045 | BEGIN_OPCODE(XCLASS): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1046 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1047 | frame->data = frame->ecode + 1 + LINK_SIZE; /* Save for matching */ |
| 1048 | frame->ecode += GET(frame->ecode, 1); /* Advance past the item */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1049 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1050 | switch (*frame->ecode) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1051 | { |
| 1052 | case OP_CRSTAR: |
| 1053 | case OP_CRMINSTAR: |
| 1054 | case OP_CRPLUS: |
| 1055 | case OP_CRMINPLUS: |
| 1056 | case OP_CRQUERY: |
| 1057 | case OP_CRMINQUERY: |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1058 | c = *frame->ecode++ - OP_CRSTAR; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1059 | minimize = (c & 1) != 0; |
| 1060 | min = rep_min[c]; /* Pick up values from tables; */ |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1061 | frame->max = rep_max[c]; /* zero for max => infinity */ |
| 1062 | if (frame->max == 0) frame->max = INT_MAX; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | |
| 1065 | case OP_CRRANGE: |
| 1066 | case OP_CRMINRANGE: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1067 | minimize = (*frame->ecode == OP_CRMINRANGE); |
| 1068 | min = GET2(frame->ecode, 1); |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1069 | frame->max = GET2(frame->ecode, 3); |
| 1070 | if (frame->max == 0) frame->max = INT_MAX; |
| 1071 | frame->ecode += 5; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1072 | break; |
| 1073 | |
| 1074 | default: /* No repeat follows */ |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1075 | min = frame->max = 1; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /* First, ensure the minimum number of matches are present. */ |
| 1079 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1080 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1081 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1082 | if (frame->eptr >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1083 | GETCHARINC(c, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1084 | if (!_pcre_xclass(c, frame->data)) RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /* If max == min we can continue with the main loop without the |
| 1088 | need to recurse. */ |
| 1089 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1090 | if (min == frame->max) |
| 1091 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1092 | |
| 1093 | /* If minimizing, keep testing the rest of the expression and advancing |
| 1094 | the pointer while it matches the class. */ |
| 1095 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1096 | if (minimize) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1097 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1098 | for (frame->fi = min;; frame->fi++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1099 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1100 | RMATCH(26, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1101 | if (is_match) RRETURN; |
| 1102 | if (frame->fi >= frame->max || frame->eptr >= md->end_subject) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1103 | GETCHARINC(c, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1104 | if (!_pcre_xclass(c, frame->data)) RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1105 | } |
| 1106 | /* Control never gets here */ |
| 1107 | } |
| 1108 | |
| 1109 | /* If maximizing, find the longest possible run, then work backwards. */ |
| 1110 | |
| 1111 | else |
| 1112 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1113 | frame->pp = frame->eptr; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1114 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1115 | { |
| 1116 | int len = 1; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1117 | if (frame->eptr >= md->end_subject) break; |
| 1118 | GETCHARLEN(c, frame->eptr, len); |
| 1119 | if (!_pcre_xclass(c, frame->data)) break; |
| 1120 | frame->eptr += len; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1121 | } |
| 1122 | for(;;) |
| 1123 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1124 | RMATCH(27, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1125 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1126 | if (frame->eptr-- == frame->pp) break; /* Stop if tried at original pos */ |
| 1127 | BACKCHAR(frame->eptr) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1128 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1129 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | /* Control never gets here */ |
| 1133 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1134 | |
| 1135 | /* Match a single character, casefully */ |
| 1136 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1137 | BEGIN_OPCODE(CHAR): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1138 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1139 | frame->length = 1; |
| 1140 | frame->ecode++; |
| 1141 | GETUTF8CHARLEN(frame->fc, frame->ecode, frame->length); |
eseidel | 67d65af | 2005-09-29 22:05:12 +0000 | [diff] [blame] | 1142 | { |
darin | a8702f5 | 2006-01-13 09:32:51 +0000 | [diff] [blame] | 1143 | int dc; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1144 | frame->ecode += frame->length; |
| 1145 | switch (md->end_subject - frame->eptr) |
hyatt | 6c974dd | 2006-01-06 22:43:44 +0000 | [diff] [blame] | 1146 | { |
| 1147 | case 0: |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1148 | RRETURN_NO_MATCH; |
hyatt | 6c974dd | 2006-01-06 22:43:44 +0000 | [diff] [blame] | 1149 | case 1: |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1150 | dc = *frame->eptr++; |
hyatt | 6c974dd | 2006-01-06 22:43:44 +0000 | [diff] [blame] | 1151 | if (IS_LEADING_SURROGATE(dc)) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1152 | RRETURN_NO_MATCH; |
hyatt | 6c974dd | 2006-01-06 22:43:44 +0000 | [diff] [blame] | 1153 | break; |
| 1154 | default: |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1155 | GETCHARINC(dc, frame->eptr); |
hyatt | 6c974dd | 2006-01-06 22:43:44 +0000 | [diff] [blame] | 1156 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1157 | if (frame->fc != dc) RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1158 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1159 | } |
| 1160 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1161 | |
| 1162 | /* Match a single character, caselessly */ |
| 1163 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1164 | BEGIN_OPCODE(CHARNC): |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1165 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1166 | frame->length = 1; |
| 1167 | frame->ecode++; |
| 1168 | GETUTF8CHARLEN(frame->fc, frame->ecode, frame->length); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1169 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1170 | if (md->end_subject - frame->eptr == 0) RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1171 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1172 | { |
| 1173 | int dc; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1174 | if (md->end_subject - frame->eptr == 1) { |
| 1175 | dc = *frame->eptr++; |
eseidel | 67d65af | 2005-09-29 22:05:12 +0000 | [diff] [blame] | 1176 | if (IS_LEADING_SURROGATE(dc)) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1177 | RRETURN_NO_MATCH; |
eseidel | 67d65af | 2005-09-29 22:05:12 +0000 | [diff] [blame] | 1178 | } else |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1179 | GETCHARINC(dc, frame->eptr); |
| 1180 | frame->ecode += frame->length; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1181 | |
| 1182 | /* If we have Unicode property support, we can use it to test the other |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 1183 | case of the character, if there is one. */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1184 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1185 | if (frame->fc != dc) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1186 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1187 | if (dc != _pcre_ucp_othercase(frame->fc)) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1188 | RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1192 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1193 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1194 | /* Match a single ASCII character. */ |
| 1195 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1196 | BEGIN_OPCODE(ASCII_CHAR): |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1197 | if (md->end_subject == frame->eptr) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1198 | RRETURN_NO_MATCH; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1199 | if (*frame->eptr != frame->ecode[1]) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1200 | RRETURN_NO_MATCH; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1201 | ++frame->eptr; |
| 1202 | frame->ecode += 2; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1203 | NEXT_OPCODE; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1204 | |
| 1205 | /* Match one of two cases of an ASCII character. */ |
| 1206 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1207 | BEGIN_OPCODE(ASCII_LETTER_NC): |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1208 | if (md->end_subject == frame->eptr) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1209 | RRETURN_NO_MATCH; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1210 | if ((*frame->eptr | 0x20) != frame->ecode[1]) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1211 | RRETURN_NO_MATCH; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1212 | ++frame->eptr; |
| 1213 | frame->ecode += 2; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1214 | NEXT_OPCODE; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1215 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1216 | /* Match a single character repeatedly; different opcodes share code. */ |
| 1217 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1218 | BEGIN_OPCODE(EXACT): |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1219 | min = frame->max = GET2(frame->ecode, 1); |
| 1220 | minimize = FALSE; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1221 | frame->ecode += 3; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1222 | goto REPEATCHAR; |
| 1223 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1224 | BEGIN_OPCODE(UPTO): |
| 1225 | BEGIN_OPCODE(MINUPTO): |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1226 | min = 0; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1227 | frame->max = GET2(frame->ecode, 1); |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1228 | minimize = *frame->ecode == OP_MINUPTO; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1229 | frame->ecode += 3; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1230 | goto REPEATCHAR; |
| 1231 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1232 | BEGIN_OPCODE(STAR): |
| 1233 | BEGIN_OPCODE(MINSTAR): |
| 1234 | BEGIN_OPCODE(PLUS): |
| 1235 | BEGIN_OPCODE(MINPLUS): |
| 1236 | BEGIN_OPCODE(QUERY): |
| 1237 | BEGIN_OPCODE(MINQUERY): |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1238 | c = *frame->ecode++ - OP_STAR; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1239 | minimize = (c & 1) != 0; |
| 1240 | min = rep_min[c]; /* Pick up values from tables; */ |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1241 | frame->max = rep_max[c]; /* zero for max => infinity */ |
| 1242 | if (frame->max == 0) frame->max = INT_MAX; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1243 | |
| 1244 | /* Common code for all repeated single-character matches. We can give |
| 1245 | up quickly if there are fewer than the minimum number of characters left in |
| 1246 | the subject. */ |
| 1247 | |
| 1248 | REPEATCHAR: |
hyatt | 6c974dd | 2006-01-06 22:43:44 +0000 | [diff] [blame] | 1249 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1250 | frame->length = 1; |
| 1251 | GETUTF8CHARLEN(frame->fc, frame->ecode, frame->length); |
darin | a8702f5 | 2006-01-13 09:32:51 +0000 | [diff] [blame] | 1252 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1253 | if (min * (frame->fc > 0xFFFF ? 2 : 1) > md->end_subject - frame->eptr) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1254 | frame->ecode += frame->length; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1255 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1256 | if (frame->fc <= 0xFFFF) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1257 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1258 | int othercase = md->caseless ? _pcre_ucp_othercase(frame->fc) : -1; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1259 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1260 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1261 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1262 | if (*frame->eptr != frame->fc && *frame->eptr != othercase) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1263 | ++frame->eptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1266 | if (min == frame->max) |
| 1267 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1268 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1269 | if (minimize) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1270 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1271 | frame->repeat_othercase = othercase; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1272 | for (frame->fi = min;; frame->fi++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1273 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1274 | RMATCH(28, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1275 | if (is_match) RRETURN; |
| 1276 | if (frame->fi >= frame->max || frame->eptr >= md->end_subject) RRETURN; |
| 1277 | if (*frame->eptr != frame->fc && *frame->eptr != frame->repeat_othercase) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1278 | ++frame->eptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1279 | } |
| 1280 | /* Control never gets here */ |
| 1281 | } |
| 1282 | else |
| 1283 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1284 | frame->pp = frame->eptr; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1285 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1286 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1287 | if (frame->eptr >= md->end_subject) break; |
| 1288 | if (*frame->eptr != frame->fc && *frame->eptr != othercase) break; |
| 1289 | ++frame->eptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1290 | } |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1291 | while (frame->eptr >= frame->pp) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1292 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1293 | RMATCH(29, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1294 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1295 | --frame->eptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1296 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1297 | RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1298 | } |
| 1299 | /* Control never gets here */ |
| 1300 | } |
| 1301 | else |
| 1302 | { |
| 1303 | /* No case on surrogate pairs, so no need to bother with "othercase". */ |
| 1304 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1305 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1306 | { |
| 1307 | int nc; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1308 | GETCHAR(nc, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1309 | if (nc != frame->fc) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1310 | frame->eptr += 2; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1313 | if (min == frame->max) |
| 1314 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1315 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1316 | if (minimize) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1317 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1318 | for (frame->fi = min;; frame->fi++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1319 | { |
| 1320 | int nc; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1321 | RMATCH(30, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1322 | if (is_match) RRETURN; |
| 1323 | if (frame->fi >= frame->max || frame->eptr >= md->end_subject) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1324 | GETCHAR(nc, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1325 | if (*frame->eptr != frame->fc) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1326 | frame->eptr += 2; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1327 | } |
| 1328 | /* Control never gets here */ |
| 1329 | } |
| 1330 | else |
| 1331 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1332 | frame->pp = frame->eptr; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1333 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1334 | { |
| 1335 | int nc; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1336 | if (frame->eptr > md->end_subject - 2) break; |
| 1337 | GETCHAR(nc, frame->eptr); |
| 1338 | if (*frame->eptr != frame->fc) break; |
| 1339 | frame->eptr += 2; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1340 | } |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1341 | while (frame->eptr >= frame->pp) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1342 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1343 | RMATCH(31, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1344 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1345 | frame->eptr -= 2; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1346 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1347 | RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1348 | } |
| 1349 | /* Control never gets here */ |
| 1350 | } |
| 1351 | /* Control never gets here */ |
darin | a8702f5 | 2006-01-13 09:32:51 +0000 | [diff] [blame] | 1352 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1353 | |
| 1354 | /* Match a negated single one-byte character. The character we are |
| 1355 | checking can be multibyte. */ |
| 1356 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1357 | BEGIN_OPCODE(NOT): |
| 1358 | if (frame->eptr >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1359 | frame->ecode++; |
| 1360 | GETCHARINCTEST(c, frame->eptr); |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1361 | if (md->caseless) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1362 | { |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1363 | if (c < 128) |
| 1364 | c = md->lcc[c]; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1365 | if (md->lcc[*frame->ecode++] == c) RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1366 | } |
| 1367 | else |
| 1368 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1369 | if (*frame->ecode++ == c) RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1370 | } |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1371 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1372 | |
| 1373 | /* Match a negated single one-byte character repeatedly. This is almost a |
| 1374 | repeat of the code for a repeated single character, but I haven't found a |
| 1375 | nice way of commoning these up that doesn't require a test of the |
| 1376 | positive/negative option for each character match. Maybe that wouldn't add |
| 1377 | very much to the time taken, but character matching *is* what this is all |
| 1378 | about... */ |
| 1379 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1380 | BEGIN_OPCODE(NOTEXACT): |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1381 | min = frame->max = GET2(frame->ecode, 1); |
| 1382 | minimize = FALSE; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1383 | frame->ecode += 3; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1384 | goto REPEATNOTCHAR; |
| 1385 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1386 | BEGIN_OPCODE(NOTUPTO): |
| 1387 | BEGIN_OPCODE(NOTMINUPTO): |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1388 | min = 0; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1389 | frame->max = GET2(frame->ecode, 1); |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1390 | minimize = *frame->ecode == OP_NOTMINUPTO; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1391 | frame->ecode += 3; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1392 | goto REPEATNOTCHAR; |
| 1393 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1394 | BEGIN_OPCODE(NOTSTAR): |
| 1395 | BEGIN_OPCODE(NOTMINSTAR): |
| 1396 | BEGIN_OPCODE(NOTPLUS): |
| 1397 | BEGIN_OPCODE(NOTMINPLUS): |
| 1398 | BEGIN_OPCODE(NOTQUERY): |
| 1399 | BEGIN_OPCODE(NOTMINQUERY): |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1400 | c = *frame->ecode++ - OP_NOTSTAR; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1401 | minimize = (c & 1) != 0; |
| 1402 | min = rep_min[c]; /* Pick up values from tables; */ |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1403 | frame->max = rep_max[c]; /* zero for max => infinity */ |
| 1404 | if (frame->max == 0) frame->max = INT_MAX; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1405 | |
| 1406 | /* Common code for all repeated single-byte matches. We can give up quickly |
| 1407 | if there are fewer than the minimum number of bytes left in the |
| 1408 | subject. */ |
| 1409 | |
| 1410 | REPEATNOTCHAR: |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1411 | if (min > md->end_subject - frame->eptr) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1412 | frame->fc = *frame->ecode++; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1413 | |
| 1414 | /* The code is duplicated for the caseless and caseful cases, for speed, |
| 1415 | since matching characters is likely to be quite common. First, ensure the |
| 1416 | minimum number of matches are present. If min = max, continue at the same |
| 1417 | level without recursing. Otherwise, if minimizing, keep trying the rest of |
| 1418 | the expression and advancing one matching character if failing, up to the |
| 1419 | maximum. Alternatively, if maximizing, find the maximum number of |
| 1420 | characters and work backwards. */ |
| 1421 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1422 | DPRINTF(("negative matching %c{%d,%d}\n", frame->fc, min, frame->max)); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1423 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1424 | if (md->caseless) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1425 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1426 | if (frame->fc < 128) |
| 1427 | frame->fc = md->lcc[frame->fc]; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1428 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1429 | { |
| 1430 | register int d; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1431 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1432 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1433 | GETCHARINC(d, frame->eptr); |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1434 | if (d < 128) d = md->lcc[d]; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1435 | if (frame->fc == d) RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1436 | } |
| 1437 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1438 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1439 | if (min == frame->max) |
| 1440 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1441 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1442 | if (minimize) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1443 | { |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1444 | { |
| 1445 | register int d; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1446 | for (frame->fi = min;; frame->fi++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1447 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1448 | RMATCH(38, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1449 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1450 | GETCHARINC(d, frame->eptr); |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1451 | if (d < 128) d = md->lcc[d]; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1452 | if (frame->fi >= frame->max || frame->eptr >= md->end_subject || frame->fc == d) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1453 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1454 | } |
| 1455 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1456 | /* Control never gets here */ |
| 1457 | } |
| 1458 | |
| 1459 | /* Maximize case */ |
| 1460 | |
| 1461 | else |
| 1462 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1463 | frame->pp = frame->eptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1464 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1465 | { |
| 1466 | register int d; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1467 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1468 | { |
| 1469 | int len = 1; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1470 | if (frame->eptr >= md->end_subject) break; |
| 1471 | GETCHARLEN(d, frame->eptr, len); |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1472 | if (d < 128) d = md->lcc[d]; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1473 | if (frame->fc == d) break; |
| 1474 | frame->eptr += len; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1475 | } |
| 1476 | for(;;) |
| 1477 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1478 | RMATCH(40, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1479 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1480 | if (frame->eptr-- == frame->pp) break; /* Stop if tried at original pos */ |
| 1481 | BACKCHAR(frame->eptr); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1482 | } |
| 1483 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1484 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1485 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1486 | } |
| 1487 | /* Control never gets here */ |
| 1488 | } |
| 1489 | |
| 1490 | /* Caseful comparisons */ |
| 1491 | |
| 1492 | else |
| 1493 | { |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1494 | { |
| 1495 | register int d; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1496 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1497 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1498 | GETCHARINC(d, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1499 | if (frame->fc == d) RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1500 | } |
| 1501 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1502 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1503 | if (min == frame->max) |
| 1504 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1505 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1506 | if (minimize) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1507 | { |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1508 | { |
| 1509 | register int d; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1510 | for (frame->fi = min;; frame->fi++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1511 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1512 | RMATCH(42, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1513 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1514 | GETCHARINC(d, frame->eptr); |
| 1515 | if (frame->fi >= frame->max || frame->eptr >= md->end_subject || frame->fc == d) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1516 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1519 | /* Control never gets here */ |
| 1520 | } |
| 1521 | |
| 1522 | /* Maximize case */ |
| 1523 | |
| 1524 | else |
| 1525 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1526 | frame->pp = frame->eptr; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1527 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1528 | { |
| 1529 | register int d; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1530 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1531 | { |
| 1532 | int len = 1; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1533 | if (frame->eptr >= md->end_subject) break; |
| 1534 | GETCHARLEN(d, frame->eptr, len); |
| 1535 | if (frame->fc == d) break; |
| 1536 | frame->eptr += len; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1537 | } |
| 1538 | for(;;) |
| 1539 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1540 | RMATCH(44, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1541 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1542 | if (frame->eptr-- == frame->pp) break; /* Stop if tried at original pos */ |
| 1543 | BACKCHAR(frame->eptr); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1544 | } |
| 1545 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1546 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1547 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | /* Control never gets here */ |
| 1551 | |
| 1552 | /* Match a single character type repeatedly; several different opcodes |
| 1553 | share code. This is very similar to the code for single characters, but we |
| 1554 | repeat it in the interests of efficiency. */ |
| 1555 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1556 | BEGIN_OPCODE(TYPEEXACT): |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1557 | min = frame->max = GET2(frame->ecode, 1); |
| 1558 | minimize = TRUE; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1559 | frame->ecode += 3; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1560 | goto REPEATTYPE; |
| 1561 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1562 | BEGIN_OPCODE(TYPEUPTO): |
| 1563 | BEGIN_OPCODE(TYPEMINUPTO): |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1564 | min = 0; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1565 | frame->max = GET2(frame->ecode, 1); |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1566 | minimize = *frame->ecode == OP_TYPEMINUPTO; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1567 | frame->ecode += 3; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1568 | goto REPEATTYPE; |
| 1569 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1570 | BEGIN_OPCODE(TYPESTAR): |
| 1571 | BEGIN_OPCODE(TYPEMINSTAR): |
| 1572 | BEGIN_OPCODE(TYPEPLUS): |
| 1573 | BEGIN_OPCODE(TYPEMINPLUS): |
| 1574 | BEGIN_OPCODE(TYPEQUERY): |
| 1575 | BEGIN_OPCODE(TYPEMINQUERY): |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1576 | c = *frame->ecode++ - OP_TYPESTAR; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1577 | minimize = (c & 1) != 0; |
| 1578 | min = rep_min[c]; /* Pick up values from tables; */ |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1579 | frame->max = rep_max[c]; /* zero for max => infinity */ |
| 1580 | if (frame->max == 0) frame->max = INT_MAX; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1581 | |
| 1582 | /* Common code for all repeated single character type matches. Note that |
| 1583 | in UTF-8 mode, '.' matches a character of any length, but for the other |
| 1584 | character types, the valid characters are all one-byte long. */ |
| 1585 | |
| 1586 | REPEATTYPE: |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1587 | frame->ctype = *frame->ecode++; /* Code for the character type */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1588 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1589 | /* First, ensure the minimum number of matches are present. Use inline |
| 1590 | code for maximizing the speed, and do the type test once at the start |
| 1591 | (i.e. keep it out of the loop). Also we can test that there are at least |
| 1592 | the minimum number of bytes before we start. This isn't as effective in |
| 1593 | UTF-8 mode, but it does no harm. Separate the UTF-8 code completely as that |
| 1594 | is tidier. Also separate the UCP code, which can be the same for both UTF-8 |
| 1595 | and single-bytes. */ |
| 1596 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1597 | if (min > md->end_subject - frame->eptr) RRETURN_NO_MATCH; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1598 | if (min > 0) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1599 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1600 | switch(frame->ctype) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1601 | { |
| 1602 | case OP_ANY: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1603 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1604 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1605 | if (frame->eptr >= md->end_subject || IS_NEWLINE(*frame->eptr)) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1606 | RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1607 | ++frame->eptr; |
| 1608 | while (frame->eptr < md->end_subject && ISMIDCHAR(*frame->eptr)) frame->eptr++; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1609 | } |
| 1610 | break; |
| 1611 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1612 | case OP_NOT_DIGIT: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1613 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1614 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1615 | if (frame->eptr >= md->end_subject) RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1616 | GETCHARINC(c, frame->eptr); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1617 | if (isASCIIDigit(c)) |
| 1618 | RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1619 | } |
| 1620 | break; |
| 1621 | |
| 1622 | case OP_DIGIT: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1623 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1624 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1625 | if (frame->eptr >= md->end_subject || !isASCIIDigit(*frame->eptr++)) |
| 1626 | RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1627 | /* No need to skip more bytes - we know it's a 1-byte character */ |
| 1628 | } |
| 1629 | break; |
| 1630 | |
| 1631 | case OP_NOT_WHITESPACE: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1632 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1633 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1634 | if (frame->eptr >= md->end_subject || |
| 1635 | (*frame->eptr < 128 && (md->ctypes[*frame->eptr] & ctype_space) != 0)) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1636 | RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1637 | while (++frame->eptr < md->end_subject && ISMIDCHAR(*frame->eptr)); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1638 | } |
| 1639 | break; |
| 1640 | |
| 1641 | case OP_WHITESPACE: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1642 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1643 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1644 | if (frame->eptr >= md->end_subject || |
| 1645 | *frame->eptr >= 128 || (md->ctypes[*frame->eptr++] & ctype_space) == 0) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1646 | RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1647 | /* No need to skip more bytes - we know it's a 1-byte character */ |
| 1648 | } |
| 1649 | break; |
| 1650 | |
| 1651 | case OP_NOT_WORDCHAR: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1652 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1653 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1654 | if (frame->eptr >= md->end_subject || |
| 1655 | (*frame->eptr < 128 && (md->ctypes[*frame->eptr] & ctype_word) != 0)) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1656 | RRETURN_NO_MATCH; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1657 | while (++frame->eptr < md->end_subject && ISMIDCHAR(*frame->eptr)); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1658 | } |
| 1659 | break; |
| 1660 | |
| 1661 | case OP_WORDCHAR: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1662 | for (i = 1; i <= min; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1663 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1664 | if (frame->eptr >= md->end_subject || |
| 1665 | *frame->eptr >= 128 || (md->ctypes[*frame->eptr++] & ctype_word) == 0) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1666 | RRETURN_NO_MATCH; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1667 | /* No need to skip more bytes - we know it's a 1-byte character */ |
| 1668 | } |
| 1669 | break; |
| 1670 | |
| 1671 | default: |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1672 | ASSERT_NOT_REACHED(); |
| 1673 | RRETURN_ERROR(JSRegExpErrorInternal); |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1674 | } /* End switch(frame->ctype) */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | /* If min = max, continue at the same level without recursing */ |
| 1678 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1679 | if (min == frame->max) |
| 1680 | NEXT_OPCODE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1681 | |
| 1682 | /* If minimizing, we have to test the rest of the pattern before each |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1683 | subsequent match. */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1684 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1685 | if (minimize) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1686 | { |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1687 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1688 | for (frame->fi = min;; frame->fi++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1689 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1690 | RMATCH(48, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1691 | if (is_match) RRETURN; |
| 1692 | if (frame->fi >= frame->max || frame->eptr >= md->end_subject) RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1693 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1694 | GETCHARINC(c, frame->eptr); |
| 1695 | switch(frame->ctype) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1696 | { |
| 1697 | case OP_ANY: |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1698 | if (IS_NEWLINE(c)) RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1699 | break; |
| 1700 | |
| 1701 | case OP_NOT_DIGIT: |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1702 | if (isASCIIDigit(c)) |
| 1703 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1704 | break; |
| 1705 | |
| 1706 | case OP_DIGIT: |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1707 | if (!isASCIIDigit(c)) |
| 1708 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1709 | break; |
| 1710 | |
| 1711 | case OP_NOT_WHITESPACE: |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1712 | if (c < 128 && (md->ctypes[c] & ctype_space) != 0) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1713 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1714 | break; |
| 1715 | |
| 1716 | case OP_WHITESPACE: |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1717 | if (c >= 128 || (md->ctypes[c] & ctype_space) == 0) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1718 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1719 | break; |
| 1720 | |
| 1721 | case OP_NOT_WORDCHAR: |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1722 | if (c < 128 && (md->ctypes[c] & ctype_word) != 0) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1723 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1724 | break; |
| 1725 | |
| 1726 | case OP_WORDCHAR: |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1727 | if (c >= 128 || (md->ctypes[c] & ctype_word) == 0) |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1728 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1729 | break; |
| 1730 | |
| 1731 | default: |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1732 | ASSERT_NOT_REACHED(); |
| 1733 | RRETURN_ERROR(JSRegExpErrorInternal); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1734 | } |
| 1735 | } |
| 1736 | } |
| 1737 | /* Control never gets here */ |
| 1738 | } |
| 1739 | |
| 1740 | /* If maximizing it is worth using inline code for speed, doing the type |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1741 | test once at the start (i.e. keep it out of the loop). */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1742 | |
| 1743 | else |
| 1744 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1745 | frame->pp = frame->eptr; /* Remember where we started */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1746 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1747 | switch(frame->ctype) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1748 | { |
| 1749 | case OP_ANY: |
| 1750 | |
| 1751 | /* Special code is required for UTF8, but when the maximum is unlimited |
| 1752 | we don't need it, so we repeat the non-UTF8 code. This is probably |
| 1753 | worth it, because .* is quite a common idiom. */ |
| 1754 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1755 | if (frame->max < INT_MAX) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1756 | { |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1757 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1758 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1759 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1760 | if (frame->eptr >= md->end_subject || IS_NEWLINE(*frame->eptr)) break; |
| 1761 | frame->eptr++; |
| 1762 | while (frame->eptr < md->end_subject && (*frame->eptr & 0xc0) == 0x80) frame->eptr++; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1763 | } |
| 1764 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | /* Handle unlimited UTF-8 repeat */ |
| 1768 | |
| 1769 | else |
| 1770 | { |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1771 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1772 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1773 | { |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1774 | if (frame->eptr >= md->end_subject || IS_NEWLINE(*frame->eptr)) break; |
| 1775 | frame->eptr++; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1776 | } |
| 1777 | break; |
| 1778 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1779 | } |
| 1780 | break; |
| 1781 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1782 | case OP_NOT_DIGIT: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1783 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1784 | { |
| 1785 | int len = 1; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1786 | if (frame->eptr >= md->end_subject) break; |
| 1787 | GETCHARLEN(c, frame->eptr, len); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1788 | if (isASCIIDigit(c)) break; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1789 | frame->eptr+= len; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1790 | } |
| 1791 | break; |
| 1792 | |
| 1793 | case OP_DIGIT: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1794 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1795 | { |
| 1796 | int len = 1; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1797 | if (frame->eptr >= md->end_subject) break; |
| 1798 | GETCHARLEN(c, frame->eptr, len); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1799 | if (!isASCIIDigit(c)) break; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1800 | frame->eptr+= len; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1801 | } |
| 1802 | break; |
| 1803 | |
| 1804 | case OP_NOT_WHITESPACE: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1805 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1806 | { |
| 1807 | int len = 1; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1808 | if (frame->eptr >= md->end_subject) break; |
| 1809 | GETCHARLEN(c, frame->eptr, len); |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1810 | if (c < 128 && (md->ctypes[c] & ctype_space) != 0) break; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1811 | frame->eptr+= len; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1812 | } |
| 1813 | break; |
| 1814 | |
| 1815 | case OP_WHITESPACE: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1816 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1817 | { |
| 1818 | int len = 1; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1819 | if (frame->eptr >= md->end_subject) break; |
| 1820 | GETCHARLEN(c, frame->eptr, len); |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1821 | if (c >= 128 ||(md->ctypes[c] & ctype_space) == 0) break; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1822 | frame->eptr+= len; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1823 | } |
| 1824 | break; |
| 1825 | |
| 1826 | case OP_NOT_WORDCHAR: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1827 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1828 | { |
| 1829 | int len = 1; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1830 | if (frame->eptr >= md->end_subject) break; |
| 1831 | GETCHARLEN(c, frame->eptr, len); |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1832 | if (c < 128 && (md->ctypes[c] & ctype_word) != 0) break; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1833 | frame->eptr+= len; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1834 | } |
| 1835 | break; |
| 1836 | |
| 1837 | case OP_WORDCHAR: |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1838 | for (i = min; i < frame->max; i++) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1839 | { |
| 1840 | int len = 1; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1841 | if (frame->eptr >= md->end_subject) break; |
| 1842 | GETCHARLEN(c, frame->eptr, len); |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 1843 | if (c >= 128 || (md->ctypes[c] & ctype_word) == 0) break; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1844 | frame->eptr+= len; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1845 | } |
| 1846 | break; |
| 1847 | |
| 1848 | default: |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1849 | ASSERT_NOT_REACHED(); |
| 1850 | RRETURN_ERROR(JSRegExpErrorInternal); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1853 | /* frame->eptr is now past the end of the maximum run */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1854 | |
| 1855 | for(;;) |
| 1856 | { |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1857 | RMATCH(52, frame->ecode, frame->eptrb, 0); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1858 | if (is_match) RRETURN; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1859 | if (frame->eptr-- == frame->pp) break; /* Stop if tried at original pos */ |
| 1860 | BACKCHAR(frame->eptr); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1861 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1862 | |
| 1863 | /* Get here if we can't make it match with any permitted repetitions */ |
| 1864 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1865 | RRETURN; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1866 | } |
| 1867 | /* Control never gets here */ |
| 1868 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1869 | BEGIN_OPCODE(BRANUMBER): |
| 1870 | BEGIN_OPCODE(CRMINPLUS): |
| 1871 | BEGIN_OPCODE(CRMINQUERY): |
| 1872 | BEGIN_OPCODE(CRMINRANGE): |
| 1873 | BEGIN_OPCODE(CRMINSTAR): |
| 1874 | BEGIN_OPCODE(CRPLUS): |
| 1875 | BEGIN_OPCODE(CRQUERY): |
| 1876 | BEGIN_OPCODE(CRRANGE): |
| 1877 | BEGIN_OPCODE(CRSTAR): |
| 1878 | ASSERT_NOT_REACHED(); |
| 1879 | RRETURN_ERROR(JSRegExpErrorInternal); |
| 1880 | |
| 1881 | #ifdef USE_COMPUTED_GOTO_FOR_MATCH_OPCODE_LOOP |
| 1882 | CAPTURING_BRACKET: |
| 1883 | #else |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1884 | default: |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1885 | #endif |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1886 | /* Opening capturing bracket. If there is space in the offset vector, save |
| 1887 | the current subject position in the working slot at the top of the vector. We |
| 1888 | mustn't change the current values of the data slot, because they may be set |
| 1889 | from a previous iteration of this group, and be referred to by a reference |
| 1890 | inside the group. |
| 1891 | |
| 1892 | If the bracket fails to match, we need to restore this value and also the |
| 1893 | values of the final offsets, in case they were set by a previous iteration of |
| 1894 | the same bracket. |
| 1895 | |
| 1896 | If there isn't enough space in the offset vector, treat this as if it were a |
| 1897 | non-capturing bracket. Don't worry about setting the flag for the error case |
| 1898 | here; that is handled in the code for KET. */ |
| 1899 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1900 | ASSERT(*frame->ecode > OP_BRA); |
| 1901 | |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1902 | frame->number = *frame->ecode - OP_BRA; |
| 1903 | |
| 1904 | /* For extended extraction brackets (large number), we have to fish out the |
| 1905 | number from a dummy opcode at the start. */ |
| 1906 | |
| 1907 | if (frame->number > EXTRACT_BASIC_MAX) |
| 1908 | frame->number = GET2(frame->ecode, 2+LINK_SIZE); |
| 1909 | frame->offset = frame->number << 1; |
| 1910 | |
| 1911 | #ifdef DEBUG |
| 1912 | printf("start bracket %d subject=", frame->number); |
| 1913 | pchars(frame->eptr, 16, TRUE, md); |
| 1914 | printf("\n"); |
| 1915 | #endif |
| 1916 | |
| 1917 | if (frame->offset < md->offset_max) |
| 1918 | { |
| 1919 | frame->save_offset1 = md->offset_vector[frame->offset]; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1920 | frame->save_offset2 = md->offset_vector[frame->offset + 1]; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1921 | frame->save_offset3 = md->offset_vector[md->offset_end - frame->number]; |
| 1922 | |
| 1923 | DPRINTF(("saving %d %d %d\n", frame->save_offset1, frame->save_offset2, frame->save_offset3)); |
| 1924 | md->offset_vector[md->offset_end - frame->number] = frame->eptr - md->start_subject; |
| 1925 | |
| 1926 | do |
| 1927 | { |
| 1928 | RMATCH(1, frame->ecode + 1 + LINK_SIZE, frame->eptrb, match_isgroup); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1929 | if (is_match) RRETURN; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1930 | frame->ecode += GET(frame->ecode, 1); |
| 1931 | } |
| 1932 | while (*frame->ecode == OP_ALT); |
| 1933 | |
| 1934 | DPRINTF(("bracket %d failed\n", frame->number)); |
| 1935 | |
| 1936 | md->offset_vector[frame->offset] = frame->save_offset1; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1937 | md->offset_vector[frame->offset + 1] = frame->save_offset2; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1938 | md->offset_vector[md->offset_end - frame->number] = frame->save_offset3; |
| 1939 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1940 | RRETURN; |
darin@apple.com | af5544c | 2007-11-04 08:28:22 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | /* Insufficient room for saving captured contents */ |
| 1944 | |
| 1945 | goto NON_CAPTURING_BRACKET; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | /* Do not stick any code in here without much thought; it is assumed |
| 1949 | that "continue" in the code above comes out to here to repeat the main |
| 1950 | loop. */ |
| 1951 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1952 | } /* End of main loop */ |
| 1953 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 1954 | /* Control never reaches here */ |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1955 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1956 | #ifndef USE_COMPUTED_GOTO_FOR_MATCH_RECURSION |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1957 | |
| 1958 | RRETURN_SWITCH: |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 1959 | switch (frame->where) |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1960 | { |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1961 | case 0: goto RETURN; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1962 | case 1: goto RRETURN_1; |
| 1963 | case 2: goto RRETURN_2; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1964 | case 6: goto RRETURN_6; |
| 1965 | case 7: goto RRETURN_7; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1966 | case 9: goto RRETURN_9; |
| 1967 | case 10: goto RRETURN_10; |
| 1968 | case 11: goto RRETURN_11; |
| 1969 | case 12: goto RRETURN_12; |
| 1970 | case 13: goto RRETURN_13; |
| 1971 | case 14: goto RRETURN_14; |
| 1972 | case 15: goto RRETURN_15; |
| 1973 | case 16: goto RRETURN_16; |
| 1974 | case 17: goto RRETURN_17; |
| 1975 | case 18: goto RRETURN_18; |
| 1976 | case 19: goto RRETURN_19; |
| 1977 | case 20: goto RRETURN_20; |
| 1978 | case 21: goto RRETURN_21; |
| 1979 | case 22: goto RRETURN_22; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1980 | case 24: goto RRETURN_24; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1981 | case 26: goto RRETURN_26; |
| 1982 | case 27: goto RRETURN_27; |
| 1983 | case 28: goto RRETURN_28; |
| 1984 | case 29: goto RRETURN_29; |
| 1985 | case 30: goto RRETURN_30; |
| 1986 | case 31: goto RRETURN_31; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1987 | case 38: goto RRETURN_38; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1988 | case 40: goto RRETURN_40; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1989 | case 42: goto RRETURN_42; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1990 | case 44: goto RRETURN_44; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1991 | case 48: goto RRETURN_48; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1992 | case 52: goto RRETURN_52; |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1995 | abort(); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 1996 | RRETURN_ERROR(JSRegExpErrorInternal); |
darin | ed76fb5 | 2007-02-06 21:55:25 +0000 | [diff] [blame] | 1997 | |
| 1998 | #endif |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 1999 | |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 2000 | RETURN: |
| 2001 | return is_match ? MATCH_MATCH : MATCH_NOMATCH; |
| 2002 | |
| 2003 | RETURN_ERROR: |
| 2004 | while (!(frame >= stackframes && frame < stackframesend)) { |
| 2005 | newframe = frame->prevframe; |
| 2006 | delete frame; |
| 2007 | frame = newframe; |
| 2008 | } |
| 2009 | return i; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2013 | /************************************************* |
| 2014 | * Execute a Regular Expression * |
| 2015 | *************************************************/ |
| 2016 | |
| 2017 | /* This function applies a compiled re to a subject string and picks out |
| 2018 | portions of the string if it matches. Two elements in the vector are set for |
| 2019 | each substring: the offsets to the start and end of the substring. |
| 2020 | |
| 2021 | Arguments: |
| 2022 | argument_re points to the compiled expression |
| 2023 | extra_data points to extra data or is NULL |
| 2024 | subject points to the subject string |
| 2025 | length length of subject string (may contain binary zeros) |
| 2026 | start_offset where to start in the subject string |
| 2027 | options option bits |
| 2028 | offsets points to a vector of ints to be filled in with offsets |
| 2029 | offsetcount the number of elements in the vector |
| 2030 | |
| 2031 | Returns: > 0 => success; value is the number of elements filled in |
| 2032 | = 0 => success, but offsets is not big enough |
| 2033 | -1 => failed to match |
| 2034 | < -1 => some kind of unexpected problem |
| 2035 | */ |
| 2036 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 2037 | int |
| 2038 | jsRegExpExecute(const pcre *argument_re, |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 2039 | const UChar* subject, int length, int start_offset, int *offsets, |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2040 | int offsetcount) |
| 2041 | { |
| 2042 | int rc, resetcount, ocount; |
| 2043 | int first_byte = -1; |
| 2044 | int req_byte = -1; |
| 2045 | int req_byte2 = -1; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2046 | BOOL using_temporary_offsets = FALSE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2047 | BOOL first_byte_caseless = FALSE; |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 2048 | BOOL startline; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2049 | BOOL req_byte_caseless = FALSE; |
| 2050 | match_data match_block; |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 2051 | USPTR start_match = (USPTR)subject + start_offset; |
| 2052 | USPTR end_subject; |
| 2053 | USPTR req_byte_ptr = start_match - 1; |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 2054 | const uschar *start_code; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2055 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2056 | const real_pcre *external_re = (const real_pcre *)argument_re; |
| 2057 | const real_pcre *re = external_re; |
| 2058 | |
| 2059 | /* Plausibility checks */ |
| 2060 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 2061 | ASSERT(re); |
| 2062 | ASSERT(subject); |
| 2063 | ASSERT(offsetcount >= 0); |
| 2064 | ASSERT(offsets || offsetcount == 0); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2065 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2066 | /* Set up other data */ |
| 2067 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2068 | startline = (re->options & PCRE_STARTLINE) != 0; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2069 | |
| 2070 | /* The code starts after the real_pcre block and the capture name table. */ |
| 2071 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 2072 | start_code = (const uschar *)(external_re + 1); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2073 | |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 2074 | match_block.start_subject = (USPTR)subject; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2075 | match_block.end_subject = match_block.start_subject + length; |
| 2076 | end_subject = match_block.end_subject; |
| 2077 | |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 2078 | match_block.lcc = _pcre_default_tables + lcc_offset; |
| 2079 | match_block.ctypes = _pcre_default_tables + ctypes_offset; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2080 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 2081 | match_block.multiline = (re->options & PCRE_MULTILINE) != 0; |
| 2082 | match_block.caseless = (re->options & PCRE_CASELESS) != 0; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2083 | |
| 2084 | /* If the expression has got more back references than the offsets supplied can |
| 2085 | hold, we get a temporary chunk of working store to use during the matching. |
| 2086 | Otherwise, we can use the vector supplied, rounding down its size to a multiple |
| 2087 | of 3. */ |
| 2088 | |
| 2089 | ocount = offsetcount - (offsetcount % 3); |
| 2090 | |
| 2091 | if (re->top_backref > 0 && re->top_backref >= ocount/3) |
| 2092 | { |
| 2093 | ocount = re->top_backref * 3 + 3; |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 2094 | match_block.offset_vector = new int[ocount]; |
| 2095 | if (match_block.offset_vector == NULL) return JSRegExpErrorNoMemory; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2096 | using_temporary_offsets = TRUE; |
| 2097 | DPRINTF(("Got memory to hold back references\n")); |
| 2098 | } |
| 2099 | else match_block.offset_vector = offsets; |
| 2100 | |
| 2101 | match_block.offset_end = ocount; |
| 2102 | match_block.offset_max = (2*ocount)/3; |
| 2103 | match_block.offset_overflow = FALSE; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2104 | |
| 2105 | /* Compute the minimum number of offsets that we need to reset each time. Doing |
| 2106 | this makes a huge difference to execution time when there aren't many brackets |
| 2107 | in the pattern. */ |
| 2108 | |
| 2109 | resetcount = 2 + re->top_bracket * 2; |
| 2110 | if (resetcount > offsetcount) resetcount = ocount; |
| 2111 | |
| 2112 | /* Reset the working variable associated with each extraction. These should |
| 2113 | never be used unless previously set, but they get saved and restored, and so we |
| 2114 | initialize them to avoid reading uninitialized locations. */ |
| 2115 | |
| 2116 | if (match_block.offset_vector != NULL) |
| 2117 | { |
| 2118 | register int *iptr = match_block.offset_vector + ocount; |
| 2119 | register int *iend = iptr - resetcount/2 + 1; |
| 2120 | while (--iptr >= iend) *iptr = -1; |
| 2121 | } |
| 2122 | |
| 2123 | /* Set up the first character to match, if available. The first_byte value is |
| 2124 | never set for an anchored regular expression, but the anchoring may be forced |
| 2125 | at run time, so we have to test for anchoring. The first char may be unset for |
| 2126 | an unanchored pattern, of course. If there's no first char and the pattern was |
| 2127 | studied, there may be a bitmap of possible first characters. */ |
| 2128 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2129 | if ((re->options & PCRE_FIRSTSET) != 0) |
| 2130 | { |
| 2131 | first_byte = re->first_byte & 255; |
| 2132 | if ((first_byte_caseless = ((re->first_byte & REQ_CASELESS) != 0)) == TRUE) |
| 2133 | first_byte = match_block.lcc[first_byte]; |
| 2134 | } |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2135 | |
| 2136 | /* For anchored or unanchored matches, there may be a "last known required |
| 2137 | character" set. */ |
| 2138 | |
| 2139 | if ((re->options & PCRE_REQCHSET) != 0) |
| 2140 | { |
| 2141 | req_byte = re->req_byte & 255; |
| 2142 | req_byte_caseless = (re->req_byte & REQ_CASELESS) != 0; |
darin@apple.com | 7ecf0c3 | 2007-11-04 06:18:31 +0000 | [diff] [blame] | 2143 | req_byte2 = (_pcre_default_tables + fcc_offset)[req_byte]; /* case flipped */ |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | /* Loop for handling unanchored repeated matching attempts; for anchored regexs |
| 2147 | the loop runs just once. */ |
| 2148 | |
| 2149 | do |
| 2150 | { |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 2151 | USPTR save_end_subject = end_subject; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2152 | |
| 2153 | /* Reset the maximum number of extractions we might see. */ |
| 2154 | |
| 2155 | if (match_block.offset_vector != NULL) |
| 2156 | { |
| 2157 | register int *iptr = match_block.offset_vector; |
| 2158 | register int *iend = iptr + resetcount; |
| 2159 | while (iptr < iend) *iptr++ = -1; |
| 2160 | } |
| 2161 | |
| 2162 | /* Advance to a unique first char if possible. If firstline is TRUE, the |
| 2163 | start of the match is constrained to the first line of a multiline string. |
| 2164 | Implement this by temporarily adjusting end_subject so that we stop scanning |
| 2165 | at a newline. If the match fails at the newline, later code breaks this loop. |
| 2166 | */ |
| 2167 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2168 | /* Now test for a unique first byte */ |
| 2169 | |
| 2170 | if (first_byte >= 0) |
| 2171 | { |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 2172 | pcre_uchar first_char = first_byte; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2173 | if (first_byte_caseless) |
| 2174 | while (start_match < end_subject) |
| 2175 | { |
| 2176 | int sm = *start_match; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2177 | if (sm > 127) |
| 2178 | break; |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 2179 | if (match_block.lcc[sm] == first_char) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2180 | break; |
| 2181 | start_match++; |
| 2182 | } |
| 2183 | else |
darin | ce72b7a | 2007-02-06 19:42:35 +0000 | [diff] [blame] | 2184 | while (start_match < end_subject && *start_match != first_char) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2185 | start_match++; |
| 2186 | } |
| 2187 | |
| 2188 | /* Or to just after \n for a multiline match if possible */ |
| 2189 | |
| 2190 | else if (startline) |
| 2191 | { |
| 2192 | if (start_match > match_block.start_subject + start_offset) |
| 2193 | { |
darin@apple.com | 3fbfe08 | 2007-11-03 16:28:51 +0000 | [diff] [blame] | 2194 | while (start_match < end_subject && !IS_NEWLINE(start_match[-1])) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2195 | start_match++; |
| 2196 | } |
| 2197 | } |
| 2198 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2199 | /* Restore fudged end_subject */ |
| 2200 | |
| 2201 | end_subject = save_end_subject; |
| 2202 | |
| 2203 | #ifdef DEBUG /* Sigh. Some compilers never learn. */ |
| 2204 | printf(">>>> Match against: "); |
| 2205 | pchars(start_match, end_subject - start_match, TRUE, &match_block); |
| 2206 | printf("\n"); |
| 2207 | #endif |
| 2208 | |
| 2209 | /* If req_byte is set, we know that that character must appear in the subject |
| 2210 | for the match to succeed. If the first character is set, req_byte must be |
| 2211 | later in the subject; otherwise the test starts at the match point. This |
| 2212 | optimization can save a huge amount of backtracking in patterns with nested |
| 2213 | unlimited repeats that aren't going to match. Writing separate code for |
| 2214 | cased/caseless versions makes it go faster, as does using an autoincrement |
| 2215 | and backing off on a match. |
| 2216 | |
| 2217 | HOWEVER: when the subject string is very, very long, searching to its end can |
| 2218 | take a long time, and give bad performance on quite ordinary patterns. This |
| 2219 | showed up when somebody was matching /^C/ on a 32-megabyte string... so we |
| 2220 | don't do this when the string is sufficiently long. |
| 2221 | |
| 2222 | ALSO: this processing is disabled when partial matching is requested. |
| 2223 | */ |
| 2224 | |
| 2225 | if (req_byte >= 0 && |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 2226 | end_subject - start_match < REQ_BYTE_MAX) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2227 | { |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 2228 | register USPTR p = start_match + ((first_byte >= 0)? 1 : 0); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2229 | |
| 2230 | /* We don't need to repeat the search if we haven't yet reached the |
| 2231 | place we found it at last time. */ |
| 2232 | |
| 2233 | if (p > req_byte_ptr) |
| 2234 | { |
| 2235 | if (req_byte_caseless) |
| 2236 | { |
| 2237 | while (p < end_subject) |
| 2238 | { |
| 2239 | register int pp = *p++; |
| 2240 | if (pp == req_byte || pp == req_byte2) { p--; break; } |
| 2241 | } |
| 2242 | } |
| 2243 | else |
| 2244 | { |
| 2245 | while (p < end_subject) |
| 2246 | { |
| 2247 | if (*p++ == req_byte) { p--; break; } |
| 2248 | } |
| 2249 | } |
| 2250 | |
| 2251 | /* If we can't find the required character, break the matching loop */ |
| 2252 | |
| 2253 | if (p >= end_subject) break; |
| 2254 | |
| 2255 | /* If we have found the required character, save the point where we |
| 2256 | found it, so that we don't search again next time round the loop if |
| 2257 | the start hasn't passed this character yet. */ |
| 2258 | |
| 2259 | req_byte_ptr = p; |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | /* When a match occurs, substrings will be set for all internal extractions; |
| 2264 | we just need to set up the whole thing as substring 0 before returning. If |
| 2265 | there were too many extractions, set the return code to zero. In the case |
| 2266 | where we had to get some local store to hold offsets for backreferences, copy |
| 2267 | those back references that we can. In this case there need not be overflow |
| 2268 | if certain parts of the pattern were not used. */ |
| 2269 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2270 | match_block.match_call_count = 0; |
| 2271 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 2272 | rc = match(start_match, start_code, 2, &match_block); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2273 | |
| 2274 | /* When the result is no match, if the subject's first character was a |
| 2275 | newline and the PCRE_FIRSTLINE option is set, break (which will return |
| 2276 | PCRE_ERROR_NOMATCH). The option requests that a match occur before the first |
| 2277 | newline in the subject. Otherwise, advance the pointer to the next character |
| 2278 | and continue - but the continuation will actually happen only when the |
| 2279 | pattern is not anchored. */ |
| 2280 | |
| 2281 | if (rc == MATCH_NOMATCH) |
| 2282 | { |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2283 | start_match++; |
darin | 496882e | 2006-07-15 15:30:03 +0000 | [diff] [blame] | 2284 | while(start_match < end_subject && ISMIDCHAR(*start_match)) |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2285 | start_match++; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2286 | continue; |
| 2287 | } |
| 2288 | |
| 2289 | if (rc != MATCH_MATCH) |
| 2290 | { |
| 2291 | DPRINTF((">>>> error: returning %d\n", rc)); |
| 2292 | return rc; |
| 2293 | } |
| 2294 | |
| 2295 | /* We have a match! Copy the offset information from temporary store if |
| 2296 | necessary */ |
| 2297 | |
| 2298 | if (using_temporary_offsets) |
| 2299 | { |
| 2300 | if (offsetcount >= 4) |
| 2301 | { |
| 2302 | memcpy(offsets + 2, match_block.offset_vector + 2, |
| 2303 | (offsetcount - 2) * sizeof(int)); |
| 2304 | DPRINTF(("Copied offsets from temporary memory\n")); |
| 2305 | } |
| 2306 | if (match_block.end_offset_top > offsetcount) |
| 2307 | match_block.offset_overflow = TRUE; |
| 2308 | |
| 2309 | DPRINTF(("Freeing temporary memory\n")); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 2310 | delete [] match_block.offset_vector; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | rc = match_block.offset_overflow? 0 : match_block.end_offset_top/2; |
| 2314 | |
| 2315 | if (offsetcount < 2) rc = 0; else |
| 2316 | { |
darin | ae790da | 2007-10-17 05:38:39 +0000 | [diff] [blame] | 2317 | offsets[0] = start_match - match_block.start_subject; |
| 2318 | offsets[1] = match_block.end_match_ptr - match_block.start_subject; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | DPRINTF((">>>> returning %d\n", rc)); |
| 2322 | return rc; |
| 2323 | } |
| 2324 | |
| 2325 | /* This "while" is the end of the "do" above */ |
| 2326 | |
darin@apple.com | a7c3b87 | 2007-11-04 05:22:44 +0000 | [diff] [blame] | 2327 | while (start_match <= end_subject); |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2328 | |
| 2329 | if (using_temporary_offsets) |
| 2330 | { |
| 2331 | DPRINTF(("Freeing temporary memory\n")); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 2332 | delete [] match_block.offset_vector; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2335 | DPRINTF((">>>> returning PCRE_ERROR_NOMATCH\n")); |
darin@apple.com | ee752e7 | 2007-11-11 18:56:13 +0000 | [diff] [blame^] | 2336 | return JSRegExpErrorNoMatch; |
darin | d7737ab | 2005-09-09 00:51:07 +0000 | [diff] [blame] | 2337 | } |