| 1 | /* |
| 2 | * Copyright (c) 1998, 2012 Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | vmregex.h - regular expression parser for T3 |
| 10 | Function |
| 11 | |
| 12 | Notes |
| 13 | Adapted from the TADS 2 regular expression parser. This version |
| 14 | uses UTF-8 strings rather than simple single-byte character strings, |
| 15 | and is organized into a C++ class. |
| 16 | Modified |
| 17 | 04/11/99 CNebel - Fix warnings. |
| 18 | 10/07/98 MJRoberts - Creation |
| 19 | */ |
| 20 | |
| 21 | #ifndef VMREGEX_H |
| 22 | #define VMREGEX_H |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include "t3std.h" |
| 26 | #include "vmuni.h" |
| 27 | #include "utf8.h" |
| 28 | #include "vmerr.h" |
| 29 | #include "vmerrnum.h" |
| 30 | |
| 31 | |
| 32 | /* state ID */ |
| 33 | typedef int re_state_id; |
| 34 | |
| 35 | /* invalid state ID - used to mark null machines */ |
| 36 | #define RE_STATE_INVALID ((re_state_id)-1) |
| 37 | |
| 38 | /* first valid state ID */ |
| 39 | #define RE_STATE_FIRST_VALID ((re_state_id)0) |
| 40 | |
| 41 | |
| 42 | /* ------------------------------------------------------------------------ */ |
| 43 | /* |
| 44 | * forward declarations |
| 45 | */ |
| 46 | typedef struct regex_scan_frame regex_scan_frame; |
| 47 | |
| 48 | /* ------------------------------------------------------------------------ */ |
| 49 | /* |
| 50 | * Group register structure. Each register keeps track of the starting |
| 51 | * and ending offset of the group's text within the original search |
| 52 | * string. |
| 53 | */ |
| 54 | struct re_group_register |
| 55 | { |
| 56 | int start_ofs; |
| 57 | int end_ofs; |
| 58 | }; |
| 59 | |
| 60 | /* maximum number of group registers we keep */ |
| 61 | #define RE_GROUP_REG_CNT 10 |
| 62 | |
| 63 | /* maximum group nesting depth */ |
| 64 | #define RE_GROUP_NESTING_MAX 20 |
| 65 | |
| 66 | /* |
| 67 | * the maximum number of separate loop variables we need is the same as |
| 68 | * the group nesting level, since we only need one loop variable per |
| 69 | * nested group |
| 70 | */ |
| 71 | #define RE_LOOP_VARS_MAX RE_GROUP_NESTING_MAX |
| 72 | |
| 73 | |
| 74 | |
| 75 | /* ------------------------------------------------------------------------ */ |
| 76 | /* |
| 77 | * Recognizer types. |
| 78 | */ |
| 79 | |
| 80 | enum re_recog_type |
| 81 | { |
| 82 | /* invalid/uninitialized */ |
| 83 | RE_INVALID, |
| 84 | |
| 85 | /* literal (single) character recognizer */ |
| 86 | RE_LITERAL, |
| 87 | |
| 88 | /* literal string recognizer */ |
| 89 | RE_LITSTR, |
| 90 | |
| 91 | /* aliased literal string (points to string allocated in another state) */ |
| 92 | RE_LITSTRA, |
| 93 | |
| 94 | /* "epsilon" recognizer - match without consuming anything */ |
| 95 | RE_EPSILON, |
| 96 | |
| 97 | /* wildcard character */ |
| 98 | RE_WILDCARD, |
| 99 | |
| 100 | /* beginning and end of text */ |
| 101 | RE_TEXT_BEGIN, |
| 102 | RE_TEXT_END, |
| 103 | |
| 104 | /* |
| 105 | * same position as parent lookback assertion - this is used for |
| 106 | * lookback assertions to assert that the assertion ends at the point |
| 107 | * where the assertion occurs in the match string |
| 108 | */ |
| 109 | RE_LOOKBACK_POS, |
| 110 | |
| 111 | /* start and end of a word */ |
| 112 | RE_WORD_BEGIN, |
| 113 | RE_WORD_END, |
| 114 | |
| 115 | /* word-char and non-word-char */ |
| 116 | RE_WORD_CHAR, |
| 117 | RE_NON_WORD_CHAR, |
| 118 | |
| 119 | /* word-boundary and non-word-boundary */ |
| 120 | RE_WORD_BOUNDARY, |
| 121 | RE_NON_WORD_BOUNDARY, |
| 122 | |
| 123 | /* a character range/exclusion range */ |
| 124 | RE_RANGE, |
| 125 | RE_RANGE_EXCL, |
| 126 | |
| 127 | /* group entry/exit transition */ |
| 128 | RE_GROUP_ENTER, |
| 129 | RE_GROUP_EXIT, |
| 130 | |
| 131 | /* |
| 132 | * group matcher - the character code has the group number (0 for |
| 133 | * group 0, etc) rather than a literal to match |
| 134 | */ |
| 135 | RE_GROUP_MATCH, |
| 136 | |
| 137 | /* any alphabetic character */ |
| 138 | RE_ALPHA, |
| 139 | |
| 140 | /* any digit */ |
| 141 | RE_DIGIT, |
| 142 | RE_NON_DIGIT, |
| 143 | |
| 144 | /* any upper-case alphabetic */ |
| 145 | RE_UPPER, |
| 146 | |
| 147 | /* any lower-case alphabetic */ |
| 148 | RE_LOWER, |
| 149 | |
| 150 | /* any alphanumeric */ |
| 151 | RE_ALPHANUM, |
| 152 | |
| 153 | /* space character */ |
| 154 | RE_SPACE, |
| 155 | RE_NON_SPACE, |
| 156 | |
| 157 | /* punctuation character */ |
| 158 | RE_PUNCT, |
| 159 | |
| 160 | /* newline character */ |
| 161 | RE_NEWLINE, |
| 162 | |
| 163 | /* null character (used in range recognizers) */ |
| 164 | RE_NULLCHAR, |
| 165 | |
| 166 | /* positive look-ahead assertion */ |
| 167 | RE_ASSERT_POS, |
| 168 | |
| 169 | /* negative look-ahead assertion */ |
| 170 | RE_ASSERT_NEG, |
| 171 | |
| 172 | /* positive look-back assertion */ |
| 173 | RE_ASSERT_BACKPOS, |
| 174 | |
| 175 | /* negative look-back assertion */ |
| 176 | RE_ASSERT_BACKNEG, |
| 177 | |
| 178 | /* loop entry: zero the associated loop variable */ |
| 179 | RE_ZERO_VAR, |
| 180 | |
| 181 | /* loop branch: inspect loop criteria and branch accordingly */ |
| 182 | RE_LOOP_BRANCH, |
| 183 | |
| 184 | /* vertical whitespace */ |
| 185 | RE_VSPACE, |
| 186 | RE_NON_VSPACE |
| 187 | }; |
| 188 | |
| 189 | |
| 190 | /* ------------------------------------------------------------------------ */ |
| 191 | /* |
| 192 | * Denormalized state transition tuple. Each tuple represents the |
| 193 | * complete set of transitions out of a particular state. A particular |
| 194 | * state can have one character transition, or two epsilon transitions. |
| 195 | * Note that we don't need to store the state ID of the tuple itself in |
| 196 | * the tuple, because the state ID is the index of the tuple in an array |
| 197 | * of state tuples. |
| 198 | */ |
| 199 | struct re_tuple |
| 200 | { |
| 201 | /* recognizer type */ |
| 202 | re_recog_type typ; |
| 203 | |
| 204 | /* the character we must match to transition to the target state */ |
| 205 | union |
| 206 | { |
| 207 | /* |
| 208 | * if this is a character transition, this is the character (used |
| 209 | * as the character literal in RE_LITERAL, and as the group ID in |
| 210 | * RE_GROUP_MATCH and in RE_EPSILON nodes with the group flag set) |
| 211 | */ |
| 212 | wchar_t ch; |
| 213 | |
| 214 | /* |
| 215 | * If this is a character string transition, this is the string; |
| 216 | * it's stored as a null-terminated string of wchar_t's, allocated |
| 217 | * with new wchar_t[]. 'src' is the original state number for an |
| 218 | * aliased string that points to another state's original. |
| 219 | */ |
| 220 | struct |
| 221 | { |
| 222 | wchar_t *str; |
| 223 | re_state_id src; |
| 224 | } str; |
| 225 | |
| 226 | /* |
| 227 | * if this has a sub-machine, this is the start and end info (used |
| 228 | * for the assertion entry states: RE_ASSERT_POS, RE_ASSERT_NEG, |
| 229 | * RE_ASSERT_BACKPOS, RE_ASSERT_BACKNEG) |
| 230 | */ |
| 231 | struct |
| 232 | { |
| 233 | /* sub-machine start/end states */ |
| 234 | re_state_id init; |
| 235 | re_state_id final; |
| 236 | |
| 237 | /* for look-back assertions, the match length range */ |
| 238 | int minlen; |
| 239 | int maxlen; |
| 240 | } sub; |
| 241 | |
| 242 | /* |
| 243 | * if this is a loop, the loop parameters (used for RE_ZERO_VAR, |
| 244 | * RE_LOOP_BRANCH) |
| 245 | */ |
| 246 | struct |
| 247 | { |
| 248 | int loop_min; |
| 249 | int loop_max; |
| 250 | int loop_var; |
| 251 | } loop; |
| 252 | |
| 253 | /* |
| 254 | * Character range match table - this is used if the recognizer |
| 255 | * type is RE_RANGE or RE_RANGE_EXCL; for other recognizer types, |
| 256 | * this is not used. |
| 257 | * |
| 258 | * If used, this is an array of pairs of characters. In each pair, |
| 259 | * the first is the low end of the range, and the second is the |
| 260 | * high end of the range, both ends inclusive. A single character |
| 261 | * takes up two entries, both identical, to specify a range of only |
| 262 | * one character. |
| 263 | * |
| 264 | * If the first character is '\0', then neither wchar_t is a |
| 265 | * character in the ordinary sense described above. Instead, the |
| 266 | * second wchar_t is actually one of the recognizer type codes |
| 267 | * (re_recog_type) for a character class (RE_ALPHA, RE_DIGIT, etc). |
| 268 | * The pair in this case is to be taken to match (or exclude) the |
| 269 | * entire class. |
| 270 | * |
| 271 | * To represent a match for '\0', use '\0' for the first wchar_t |
| 272 | * and RE_NULLCHAR for the second wchar_t. Note that the special |
| 273 | * meaning of '\0' in the first character of a pair makes it |
| 274 | * impossible to represent a range including a null byte with a |
| 275 | * single pair; instead, representing a range like [\000-\017] |
| 276 | * requires two pairs: the first pair is ('\0', RE_NULLCHAR), and |
| 277 | * the second pair is ('\001', '\017'). |
| 278 | */ |
| 279 | struct |
| 280 | { |
| 281 | wchar_t *char_range; |
| 282 | size_t char_range_cnt; |
| 283 | } range; |
| 284 | |
| 285 | } info; |
| 286 | |
| 287 | /* the target states */ |
| 288 | re_state_id next_state_1; |
| 289 | re_state_id next_state_2; |
| 290 | |
| 291 | /* flags */ |
| 292 | unsigned char flags; |
| 293 | }; |
| 294 | |
| 295 | |
| 296 | /* |
| 297 | * Tuple flags |
| 298 | */ |
| 299 | |
| 300 | /* this state is being tested for a cycle */ |
| 301 | #define RE_STATE_CYCLE_TEST 0x08 |
| 302 | |
| 303 | /* |
| 304 | * for branching states: take the shortest, rather than longest, branch |
| 305 | * when both branches are successful |
| 306 | */ |
| 307 | #define RE_STATE_SHORTEST 0x10 |
| 308 | |
| 309 | |
| 310 | /* ------------------------------------------------------------------------ */ |
| 311 | /* |
| 312 | * A "machine" description. A machines is fully described by its initial |
| 313 | * and final state ID's. |
| 314 | */ |
| 315 | struct re_machine |
| 316 | { |
| 317 | /* the machine's initial state */ |
| 318 | re_state_id init; |
| 319 | |
| 320 | /* the machine's final state */ |
| 321 | re_state_id final; |
| 322 | }; |
| 323 | |
| 324 | |
| 325 | /* ------------------------------------------------------------------------ */ |
| 326 | /* |
| 327 | * Compiled pattern description. This is not a complete compiled pattern, |
| 328 | * since the tuple array is separate; this is just a description of the |
| 329 | * compiled pattern that can be combined with the tuple array to form a |
| 330 | * full compiled pattern. |
| 331 | */ |
| 332 | struct re_compiled_pattern_base |
| 333 | { |
| 334 | /* the pattern's machine description */ |
| 335 | re_machine machine; |
| 336 | |
| 337 | /* the number of tuples in the tuple array */ |
| 338 | re_state_id tuple_cnt; |
| 339 | |
| 340 | /* number of capturing groups in the expression */ |
| 341 | int group_cnt; |
| 342 | |
| 343 | /* maximum number of looping variables in the expression */ |
| 344 | int loop_var_cnt; |
| 345 | |
| 346 | /* |
| 347 | * <Case> or <NoCase> mode. If this flag is clear, the search is not |
| 348 | * case-sensitive, so alphabetic characters in the pattern are matched |
| 349 | * without regard to case. |
| 350 | */ |
| 351 | unsigned int case_sensitive : 1; |
| 352 | |
| 353 | /* is the case sensitivity explicit or defaulted? */ |
| 354 | unsigned int case_sensitivity_specified : 1; |
| 355 | |
| 356 | /* |
| 357 | * <MIN> or <MAX> match mode -- if this flag is set, we match the |
| 358 | * longest string in case of ambiguity; otherwise we match the |
| 359 | * shortest. |
| 360 | */ |
| 361 | unsigned int longest_match : 1; |
| 362 | |
| 363 | /* |
| 364 | * <FirstEnd> or <FirstBeg> match mode -- if this flag is set, we |
| 365 | * match (in a search) the string that starts first in case of |
| 366 | * ambiguity; otherwise, we match the string that ends first |
| 367 | */ |
| 368 | unsigned int first_begin : 1; |
| 369 | }; |
| 370 | |
| 371 | /* |
| 372 | * Compiled pattern object. This is a pattern compiled and saved for use |
| 373 | * in searches and matches. This is a compiled pattern description |
| 374 | * coupled with its tuple array, which in combination provide a complete |
| 375 | * compiled pattern. |
| 376 | */ |
| 377 | struct re_compiled_pattern: re_compiled_pattern_base |
| 378 | { |
| 379 | /* |
| 380 | * the tuple array (the structure is overallocated to make room for |
| 381 | * tuple_cnt entries in this array) |
| 382 | */ |
| 383 | re_tuple tuples[1]; |
| 384 | }; |
| 385 | |
| 386 | /* ------------------------------------------------------------------------ */ |
| 387 | /* |
| 388 | * Status codes |
| 389 | */ |
| 390 | typedef enum |
| 391 | { |
| 392 | /* success */ |
| 393 | RE_STATUS_SUCCESS = 0, |
| 394 | |
| 395 | /* compilation error - group nesting too deep */ |
| 396 | RE_STATUS_GROUP_NESTING_TOO_DEEP |
| 397 | |
| 398 | } re_status_t; |
| 399 | |
| 400 | |
| 401 | /* ------------------------------------------------------------------------ */ |
| 402 | /* |
| 403 | * Regular expression compilation context structure. This tracks the |
| 404 | * state of the compilation and stores the resources associated with the |
| 405 | * compiled expression. |
| 406 | */ |
| 407 | class CRegexParser |
| 408 | { |
| 409 | friend class CRegexSearcher; |
| 410 | friend class CRegexSearcherSimple; |
| 411 | |
| 412 | public: |
| 413 | /* initialize */ |
| 414 | CRegexParser(); |
| 415 | |
| 416 | /* delete */ |
| 417 | ~CRegexParser(); |
| 418 | |
| 419 | /* |
| 420 | * Compile an expression and create a compiled pattern object, filling |
| 421 | * in *pattern with a pointer to the newly-allocated pattern object. |
| 422 | * The caller is responsible for freeing the pattern by calling |
| 423 | * free_pattern(pattern). |
| 424 | */ |
| 425 | re_status_t compile_pattern(const char *expr_str, size_t exprlen, |
| 426 | re_compiled_pattern **pattern); |
| 427 | |
| 428 | /* free a pattern previously created with compile_pattern() */ |
| 429 | static void free_pattern(re_compiled_pattern *pattern); |
| 430 | |
| 431 | protected: |
| 432 | /* reset the parser */ |
| 433 | void reset(); |
| 434 | |
| 435 | /* allocate a new state ID */ |
| 436 | re_state_id alloc_state(); |
| 437 | |
| 438 | /* set a transition from a state to a given destination state */ |
| 439 | void set_trans(re_state_id id, re_state_id dest_id, |
| 440 | re_recog_type typ, wchar_t ch); |
| 441 | |
| 442 | /* initialize a new machine, setting up the initial and final state */ |
| 443 | void init_machine(struct re_machine *machine); |
| 444 | |
| 445 | /* build a character recognizer */ |
| 446 | void build_char(struct re_machine *machine, wchar_t ch); |
| 447 | |
| 448 | /* build a special recognizer */ |
| 449 | void build_special(struct re_machine *machine, |
| 450 | re_recog_type typ, wchar_t ch); |
| 451 | |
| 452 | /* build a character range recognizer */ |
| 453 | void build_char_range(struct re_machine *machine, int exclusion); |
| 454 | |
| 455 | /* build a group recognizer */ |
| 456 | void build_group_matcher(struct re_machine *machine, int group_num); |
| 457 | |
| 458 | /* build a concatenation recognizer */ |
| 459 | void build_concat(struct re_machine *new_machine, |
| 460 | struct re_machine *lhs, struct re_machine *rhs); |
| 461 | |
| 462 | /* build a group machine */ |
| 463 | void build_group(struct re_machine *new_machine, |
| 464 | struct re_machine *sub_machine, int group_id); |
| 465 | |
| 466 | /* build a positive or negative assertion machine */ |
| 467 | void build_assert(struct re_machine *new_machine, |
| 468 | struct re_machine *sub_machine, |
| 469 | int is_negative, int is_lookback); |
| 470 | |
| 471 | /* build an alternation recognizer */ |
| 472 | void build_alter(struct re_machine *new_machine, |
| 473 | struct re_machine *lhs, struct re_machine *rhs); |
| 474 | |
| 475 | /* build a closure recognizer */ |
| 476 | void build_closure(struct re_machine *new_machine, |
| 477 | struct re_machine *sub, wchar_t specifier, |
| 478 | int shortest); |
| 479 | |
| 480 | /* build an interval matcher */ |
| 481 | void build_interval(struct re_machine *new_machine, |
| 482 | struct re_machine *sub, int min_val, int max_val, |
| 483 | int var_id, int shortest); |
| 484 | |
| 485 | /* build a null machine */ |
| 486 | void build_null_machine(struct re_machine *machine); |
| 487 | |
| 488 | /* determine if a machine is null */ |
| 489 | int is_machine_null(struct re_machine *machine); |
| 490 | |
| 491 | /* concate the second machine onto the first machine */ |
| 492 | void concat_onto(struct re_machine *dest, struct re_machine *rhs); |
| 493 | |
| 494 | /* alternate the second machine onto the first */ |
| 495 | void alternate_onto(struct re_machine *dest, struct re_machine *rhs); |
| 496 | |
| 497 | /* compile an expression */ |
| 498 | re_status_t compile(const char *expr_str, size_t exprlen, |
| 499 | re_compiled_pattern_base *pat); |
| 500 | |
| 501 | /* compile a character class or class range expression */ |
| 502 | int compile_char_class_expr(utf8_ptr *expr, size_t *exprchars, |
| 503 | re_machine *result_machine); |
| 504 | |
| 505 | /* parse an integer value */ |
| 506 | int parse_int(utf8_ptr *p, size_t *chars_rem); |
| 507 | |
| 508 | /* add a character to our range buffer */ |
| 509 | void add_range_char(wchar_t ch) { add_range_char(ch, ch); } |
| 510 | void add_range_char(wchar_t ch_lo, wchar_t ch_hi); |
| 511 | |
| 512 | /* add a character class to our range buffer */ |
| 513 | void add_range_class(re_recog_type cl); |
| 514 | |
| 515 | /* ensure space in the range buffer for another entry */ |
| 516 | void ensure_range_buf_space(); |
| 517 | |
| 518 | /* break any infinite loops in the machine */ |
| 519 | void break_loops(re_machine *machine); |
| 520 | |
| 521 | /* get the match length for a state */ |
| 522 | void get_match_length(re_state_id init, re_state_id final, |
| 523 | int *minlen, int *maxlen, |
| 524 | regex_scan_frame *stack); |
| 525 | |
| 526 | /* find an infinite loop back to the given state */ |
| 527 | int break_loops(re_state_id init, re_state_id final, |
| 528 | regex_scan_frame *stack); |
| 529 | |
| 530 | /* optimize away meaningless branch-to-branch transitions */ |
| 531 | void remove_branch_to_branch(re_machine *machine); |
| 532 | void optimize_transition(const re_machine *machine, re_state_id *trans); |
| 533 | |
| 534 | /* consolidate runs of characters into strings */ |
| 535 | void consolidate_strings(re_machine *machine); |
| 536 | |
| 537 | /* next available state ID */ |
| 538 | re_state_id next_state_; |
| 539 | |
| 540 | /* |
| 541 | * The array of transition tuples. We'll allocate this array and |
| 542 | * expand it as necessary. |
| 543 | */ |
| 544 | re_tuple *tuple_arr_; |
| 545 | |
| 546 | /* number of transition tuples allocated in the array */ |
| 547 | int tuples_alloc_; |
| 548 | |
| 549 | /* buffer for building range exprssions */ |
| 550 | wchar_t *range_buf_; |
| 551 | |
| 552 | /* current number of entries in range buffer */ |
| 553 | size_t range_buf_cnt_; |
| 554 | |
| 555 | /* maximum number of entries in range buffer */ |
| 556 | size_t range_buf_max_; |
| 557 | }; |
| 558 | |
| 559 | /* ------------------------------------------------------------------------ */ |
| 560 | /* |
| 561 | * Pattern recognizer state stack. Each time we need to process a |
| 562 | * sub-state (a two-way epsilon, or a nested assertion), we stack the |
| 563 | * current state so that we can backtrack when we're done with the |
| 564 | * sub-expression. The state we store consists of: |
| 565 | * |
| 566 | * - backtrack type - this is an arbitrary uchar identifier that the |
| 567 | * pattern matcher uses to identify where to go when we pop the state |
| 568 | * |
| 569 | * - the current state ID |
| 570 | * |
| 571 | * - the current offset in the string being matched |
| 572 | * |
| 573 | * - the state ID of the terminating state of the machine |
| 574 | * |
| 575 | * - saved group registers; we only store the ones we've actually |
| 576 | * modified, to avoid unnecessary copying |
| 577 | * |
| 578 | * - saved loop variables; we only store the ones we've actually modified, |
| 579 | * to avoid unnecessary copying |
| 580 | */ |
| 581 | |
| 582 | enum regex_frame_type |
| 583 | { |
| 584 | ST_EPS1 = 1, /* doing first branch of two-branch epsilon */ |
| 585 | ST_EPS2 = 2, /* doing second branch of two-branch epsilon */ |
| 586 | ST_ASSERT = 3 /* doing an assertion */ |
| 587 | }; |
| 588 | |
| 589 | /* the base structure for a stacked state */ |
| 590 | struct regex_stack_entry |
| 591 | { |
| 592 | /* the backtrack type identifier */ |
| 593 | regex_frame_type typ; |
| 594 | |
| 595 | /* the starting offset in the string */ |
| 596 | int start_ofs; |
| 597 | |
| 598 | /* the current offset in the string */ |
| 599 | int str_ofs; |
| 600 | |
| 601 | /* the length of the search portion of the string */ |
| 602 | int curlen; |
| 603 | |
| 604 | /* the pattern state */ |
| 605 | re_state_id state; |
| 606 | |
| 607 | /* the final state of the machine */ |
| 608 | re_state_id final; |
| 609 | |
| 610 | /* the return value for this state */ |
| 611 | int retval; |
| 612 | |
| 613 | /* |
| 614 | * The look-back iteration. For a look-back assertion, we have to |
| 615 | * check each starting point over the range of possible match lengths, |
| 616 | * until we've either found a success condition or exhausted the range |
| 617 | * of possible match lengths. This keeps track of where we are in the |
| 618 | * iteration. |
| 619 | */ |
| 620 | int iter; |
| 621 | |
| 622 | /* stack offset of previous frame */ |
| 623 | int prv_sp; |
| 624 | }; |
| 625 | |
| 626 | /* saved group/loop entry */ |
| 627 | struct regex_stack_var |
| 628 | { |
| 629 | /* |
| 630 | * The ID - this is in the range 0..RE_GROUP_REG_CNT-1 for group |
| 631 | * registers, RE_GROUP_REG_CNT..RE_GROUP_REG_CNT+RE_LOOP_VARS_MAX-1 |
| 632 | * for loop variables. In other words, a loop variable is identified |
| 633 | * by its loop variable number plus RE_GROUP_REG_CNT. The special ID |
| 634 | * value -1 indicates the integer 'retval' value (a saved return value |
| 635 | * for the stack state). |
| 636 | */ |
| 637 | int id; |
| 638 | |
| 639 | /* the value */ |
| 640 | union |
| 641 | { |
| 642 | re_group_register group; |
| 643 | short loopvar; |
| 644 | int retval; |
| 645 | } val; |
| 646 | }; |
| 647 | |
| 648 | /* state stack class */ |
| 649 | class CRegexStack |
| 650 | { |
| 651 | friend class reg_deltas; |
| 652 | |
| 653 | public: |
| 654 | CRegexStack() |
| 655 | { |
| 656 | /* allocate the initial state buffer */ |
| 657 | bufsiz_ = 8192; |
| 658 | buf_ = (char *)t3malloc(bufsiz_); |
| 659 | |
| 660 | /* we don't have anything on the stack yet */ |
| 661 | sp_ = -1; |
| 662 | used_ = 0; |
| 663 | } |
| 664 | |
| 665 | ~CRegexStack() |
| 666 | { |
| 667 | /* delete the stack buffer */ |
| 668 | t3free(buf_); |
| 669 | } |
| 670 | |
| 671 | /* reset the stack */ |
| 672 | void reset() |
| 673 | { |
| 674 | /* empty the stack */ |
| 675 | sp_ = -1; |
| 676 | used_ = 0; |
| 677 | } |
| 678 | |
| 679 | /* push a new state */ |
| 680 | void push(regex_frame_type typ, int start_ofs, int str_ofs, int curlen, |
| 681 | re_state_id state, re_state_id final, int iter) |
| 682 | { |
| 683 | /* |
| 684 | * Ensure we have enough space for the base state structure plus a |
| 685 | * full complement of group registers, loop variables, and return |
| 686 | * value. We might not actually need all of the registers and |
| 687 | * loop variables, so we won't commit all of this space yet, but |
| 688 | * check in advance to make sure we have it so that we don't have |
| 689 | * to check again when and if we get around to consuming |
| 690 | * group/loop slots. |
| 691 | */ |
| 692 | ensure_space(sizeof(regex_stack_entry) |
| 693 | + ((RE_GROUP_REG_CNT + RE_LOOP_VARS_MAX + 1) |
| 694 | *sizeof(regex_stack_var))); |
| 695 | |
| 696 | /* allocate the base stack frame */ |
| 697 | regex_stack_entry *fp = |
| 698 | (regex_stack_entry *)alloc_space(sizeof(regex_stack_entry)); |
| 699 | |
| 700 | /* set it up */ |
| 701 | fp->typ = typ; |
| 702 | fp->start_ofs = start_ofs; |
| 703 | fp->str_ofs = str_ofs; |
| 704 | fp->curlen = curlen; |
| 705 | fp->state = state; |
| 706 | fp->final = final; |
| 707 | fp->iter = iter; |
| 708 | |
| 709 | /* push it onto the stack */ |
| 710 | fp->prv_sp = sp_; |
| 711 | sp_ = (char *)fp - buf_; |
| 712 | } |
| 713 | |
| 714 | /* save a group register */ |
| 715 | void save_group_reg(int id, const re_group_register *regs) |
| 716 | { |
| 717 | /* allocate a new slot if needed and save the value */ |
| 718 | regex_stack_var *var; |
| 719 | if (sp_ != -1 && (var = new_reg_or_var(id)) != 0) |
| 720 | var->val.group = regs[id]; |
| 721 | } |
| 722 | |
| 723 | /* save a loop variable */ |
| 724 | void save_loop_var(int id, const short *loop_vars) |
| 725 | { |
| 726 | /* |
| 727 | * allocate a new slot if needed and save the value; note that |
| 728 | * loop variables are identified by the loop variable ID plus the |
| 729 | * base index RE_GROUP_REG_CNT |
| 730 | */ |
| 731 | regex_stack_var *var; |
| 732 | if (sp_ != -1 && (var = new_reg_or_var(id + RE_GROUP_REG_CNT)) != 0) |
| 733 | var->val.loopvar = loop_vars[id]; |
| 734 | } |
| 735 | |
| 736 | /* propagate a group register or loop variable from another frame */ |
| 737 | void propagate(const regex_stack_var *src) |
| 738 | { |
| 739 | regex_stack_var *dst; |
| 740 | if (sp_ != -1 && (dst = new_reg_or_var(src->id)) != 0) |
| 741 | memcpy(dst, src, sizeof(*dst)); |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * get the type of the state at top of stack; if there is no state, |
| 746 | * returns -1 |
| 747 | */ |
| 748 | int get_top_type() |
| 749 | { |
| 750 | /* |
| 751 | * if there's nothing on the stack, so indicate, otherwise get the |
| 752 | * type from the top stack element |
| 753 | */ |
| 754 | if (sp_ == -1) |
| 755 | return -1; |
| 756 | else |
| 757 | return ((regex_stack_entry *)(buf_ + sp_))->typ; |
| 758 | } |
| 759 | |
| 760 | /* get the stack frame at the given depth (0 is top of stack) */ |
| 761 | regex_stack_entry *get_frame(int depth) |
| 762 | { |
| 763 | /* traverse the given number of frames from the top of the stack */ |
| 764 | regex_stack_entry *fp; |
| 765 | for (fp = (regex_stack_entry *)(buf_ + sp_) ; |
| 766 | depth != 0 && fp != 0 ; --depth, fp = get_parent_frame(fp)) ; |
| 767 | |
| 768 | /* return the frame pointer */ |
| 769 | return fp; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * Get the parent lookback assertion match position. This scans up the |
| 774 | * stack for the nearest assertion frame, and returns its match |
| 775 | * position. |
| 776 | */ |
| 777 | int get_lookback_pos() |
| 778 | { |
| 779 | /* scan up the stack for a lookback frame */ |
| 780 | for (regex_stack_entry *fp = (regex_stack_entry *)(buf_ + sp_) ; |
| 781 | fp != 0 ; fp = get_parent_frame(fp)) |
| 782 | { |
| 783 | /* if this is an assert frame, return the position */ |
| 784 | if (fp->typ == ST_ASSERT) |
| 785 | return fp->str_ofs; |
| 786 | } |
| 787 | |
| 788 | /* didn't find it */ |
| 789 | return -1; |
| 790 | } |
| 791 | |
| 792 | /* get the parent stack entry */ |
| 793 | regex_stack_entry *get_parent_frame(regex_stack_entry *fp) |
| 794 | { |
| 795 | if (fp->prv_sp < 0) |
| 796 | return 0; |
| 797 | else |
| 798 | return (regex_stack_entry *)(buf_ + fp->prv_sp); |
| 799 | } |
| 800 | |
| 801 | /* pop a state */ |
| 802 | void pop(int *start_ofs, int *str_ofs, size_t *curlen, |
| 803 | re_state_id *state, re_state_id *final, |
| 804 | re_group_register *regs, short *loop_vars, int *iter) |
| 805 | { |
| 806 | /* get the stack pointer */ |
| 807 | regex_stack_entry *fp = (regex_stack_entry *)(buf_ + sp_); |
| 808 | |
| 809 | /* restore the string offset and state ID */ |
| 810 | *start_ofs = fp->start_ofs; |
| 811 | *str_ofs = fp->str_ofs; |
| 812 | *curlen = fp->curlen; |
| 813 | *state = fp->state; |
| 814 | *final = fp->final; |
| 815 | *iter = fp->iter; |
| 816 | |
| 817 | /* run through the saved registers/variables in the state */ |
| 818 | for (regex_stack_var *var = (regex_stack_var *)(fp + 1) ; |
| 819 | var < (regex_stack_var *)(buf_ + used_) ; ++var) |
| 820 | { |
| 821 | /* sense the type */ |
| 822 | if (var->id < RE_GROUP_REG_CNT) |
| 823 | { |
| 824 | /* it's a group register */ |
| 825 | regs[var->id] = var->val.group; |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | /* it's a loop variable */ |
| 830 | loop_vars[var->id - RE_GROUP_REG_CNT] = var->val.loopvar; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | /* we're done with the stop stack frame, so discard it */ |
| 835 | discard(); |
| 836 | } |
| 837 | |
| 838 | /* discard the top stack state */ |
| 839 | void discard() |
| 840 | { |
| 841 | /* get the stack pointer */ |
| 842 | regex_stack_entry *fp = (regex_stack_entry *)(buf_ + sp_); |
| 843 | |
| 844 | /* unwind the stack */ |
| 845 | used_ = (size_t)sp_; |
| 846 | sp_ = fp->prv_sp; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Swap the current state with the state at the top of the stack, and |
| 851 | * push a second copy of the restored state. This is used to traverse |
| 852 | * the second branch of a two-branch epsilon: we first have to save the |
| 853 | * results of the first branch, including the return value and its |
| 854 | * registers, and we then have to restore the initial register/loop |
| 855 | * state as it was before the first branch. |
| 856 | * |
| 857 | * We save the final state and restore the initial state by swapping |
| 858 | * the group registers in the saved state with those in the current |
| 859 | * state. This brings back the initial conditions to the current |
| 860 | * machine state, while saving everything that's changed in the current |
| 861 | * machine state in the stack frame. We'll likewise swap the machine |
| 862 | * state and string offset. Later, this same final machine state can |
| 863 | * be restored by first restoring the machine state to the initial |
| 864 | * state, then popping this frame. |
| 865 | * |
| 866 | * On return, the stack frame that was active on entry will be set to |
| 867 | * contain the current machine state, and the current machine state |
| 868 | * will be replaced with what was in that stack frame. In addition, |
| 869 | * we'll have pushed a new stack frame for the new current machine |
| 870 | * state. |
| 871 | */ |
| 872 | void swap_and_push(int retval, regex_frame_type typ, |
| 873 | int *start_ofs, int *str_ofs, size_t *curlen, |
| 874 | re_state_id *state, re_state_id *final, |
| 875 | re_group_register *regs, short *loop_vars) |
| 876 | { |
| 877 | /* swap the current state wtih the top of stack */ |
| 878 | swap(start_ofs, str_ofs, curlen, state, final, regs, loop_vars); |
| 879 | |
| 880 | /* save the return value from the outgoing state */ |
| 881 | regex_stack_entry *fp = (regex_stack_entry *)(buf_ + sp_); |
| 882 | fp->retval = retval; |
| 883 | |
| 884 | /* push a copy of the restored state */ |
| 885 | push(typ, *start_ofs, *str_ofs, *curlen, *state, *final, 0); |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Swap the current active state with the top of stack, then pop the |
| 890 | * frame. |
| 891 | */ |
| 892 | void swap_and_pop(int *start_ofs, int *str_ofs, size_t *curlen, |
| 893 | re_state_id *state, re_state_id *final, |
| 894 | re_group_register *regs, short *loop_vars, int *iter) |
| 895 | { |
| 896 | /* swap the current state wtih the top of stack */ |
| 897 | swap(start_ofs, str_ofs, curlen, state, final, regs, loop_vars); |
| 898 | |
| 899 | /* save the return value from the outgoing state */ |
| 900 | regex_stack_entry *fp = (regex_stack_entry *)(buf_ + sp_); |
| 901 | *iter = fp->iter; |
| 902 | |
| 903 | /* discard the frame */ |
| 904 | discard(); |
| 905 | } |
| 906 | |
| 907 | /* |
| 908 | * Swap the current state with the top stack frame |
| 909 | */ |
| 910 | void swap(int *start_ofs, int *str_ofs, size_t *curlen, |
| 911 | re_state_id *state, re_state_id *final, |
| 912 | re_group_register *regs, short *loop_vars) |
| 913 | { |
| 914 | /* get the stack pointer */ |
| 915 | regex_stack_entry *fp = (regex_stack_entry *)(buf_ + sp_); |
| 916 | |
| 917 | /* swap the string offset */ |
| 918 | int tmp_ofs = *str_ofs; |
| 919 | *str_ofs = fp->str_ofs; |
| 920 | fp->str_ofs = tmp_ofs; |
| 921 | |
| 922 | /* swap the starting offset */ |
| 923 | tmp_ofs = *start_ofs; |
| 924 | *start_ofs = fp->start_ofs; |
| 925 | fp->start_ofs = tmp_ofs; |
| 926 | |
| 927 | /* swap the search length */ |
| 928 | size_t tmp_len = *curlen; |
| 929 | *curlen = fp->curlen; |
| 930 | fp->curlen = tmp_len; |
| 931 | |
| 932 | /* swap the current machine state */ |
| 933 | re_state_id tmp_id = *state; |
| 934 | *state = fp->state; |
| 935 | fp->state = tmp_id; |
| 936 | |
| 937 | /* swap the final machine state */ |
| 938 | tmp_id = *final; |
| 939 | *final = fp->final; |
| 940 | fp->final = tmp_id; |
| 941 | |
| 942 | /* swap all group and loop registers with the current state */ |
| 943 | for (regex_stack_var *var = (regex_stack_var *)(fp + 1) ; |
| 944 | var < (regex_stack_var *)(buf_ + used_) ; ++var) |
| 945 | { |
| 946 | /* sense the type */ |
| 947 | if (var->id < RE_GROUP_REG_CNT) |
| 948 | { |
| 949 | re_group_register tmp; |
| 950 | |
| 951 | /* it's a group register */ |
| 952 | tmp = regs[var->id]; |
| 953 | regs[var->id] = var->val.group; |
| 954 | var->val.group = tmp; |
| 955 | } |
| 956 | else |
| 957 | { |
| 958 | short tmp; |
| 959 | |
| 960 | /* it's a loop variable */ |
| 961 | tmp = loop_vars[var->id - RE_GROUP_REG_CNT]; |
| 962 | loop_vars[var->id - RE_GROUP_REG_CNT] = var->val.loopvar; |
| 963 | var->val.loopvar = tmp; |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | protected: |
| 969 | /* |
| 970 | * allocate a new register or group variable in the stack frame; if we |
| 971 | * find an existing copy of the same variable, we'll return null to |
| 972 | * indicate that we don't have to save it again |
| 973 | */ |
| 974 | regex_stack_var *new_reg_or_var(int id) |
| 975 | { |
| 976 | /* get the stack pointer */ |
| 977 | regex_stack_entry *fp = (regex_stack_entry *)(buf_ + sp_); |
| 978 | |
| 979 | /* scan the frame for a register/variable with this ID */ |
| 980 | regex_stack_var *var; |
| 981 | for (var = (regex_stack_var *)(fp + 1) ; |
| 982 | var < (regex_stack_var *)(buf_ + used_) ; ++var) |
| 983 | { |
| 984 | /* if this is the one, we don't need to save it again */ |
| 985 | if (var->id == id) |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | /* we didn't find it, so return a new entry with the given ID */ |
| 990 | var = (regex_stack_var *)alloc_space(sizeof(regex_stack_var)); |
| 991 | var->id = id; |
| 992 | return var; |
| 993 | } |
| 994 | |
| 995 | /* ensure space in our stack buffer */ |
| 996 | void ensure_space(size_t siz) |
| 997 | { |
| 998 | /* if it's within range, we're fine */ |
| 999 | if (used_ + siz <= bufsiz_) |
| 1000 | return; |
| 1001 | |
| 1002 | /* expand */ |
| 1003 | bufsiz_ += 8192; |
| 1004 | |
| 1005 | /* if it's too large, throw an error */ |
| 1006 | if (bufsiz_ > OSMALMAX) |
| 1007 | err_throw(VMERR_OUT_OF_MEMORY); |
| 1008 | |
| 1009 | /* reallocate at the new size */ |
| 1010 | buf_ = (char *)t3realloc(buf_, bufsiz_); |
| 1011 | |
| 1012 | /* make sure we're not out of memory */ |
| 1013 | if (buf_ == 0) |
| 1014 | err_throw(VMERR_OUT_OF_MEMORY); |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * allocate space - the caller must have already checked that space is |
| 1019 | * available |
| 1020 | */ |
| 1021 | char *alloc_space(size_t siz) |
| 1022 | { |
| 1023 | /* figure out where the new object goes */ |
| 1024 | char *ret = buf_ + used_; |
| 1025 | |
| 1026 | /* consume the space */ |
| 1027 | used_ += siz; |
| 1028 | |
| 1029 | /* return the allocated space */ |
| 1030 | return ret; |
| 1031 | } |
| 1032 | |
| 1033 | /* the stack buffer */ |
| 1034 | char *buf_; |
| 1035 | size_t bufsiz_; |
| 1036 | |
| 1037 | /* offset of current stack frame */ |
| 1038 | int sp_; |
| 1039 | |
| 1040 | /* number of bytes used so far */ |
| 1041 | size_t used_; |
| 1042 | }; |
| 1043 | |
| 1044 | /* ------------------------------------------------------------------------ */ |
| 1045 | /* |
| 1046 | * Regular Expression Searcher/Matcher. This object encapsulates the |
| 1047 | * group registers associated with a search. |
| 1048 | */ |
| 1049 | class CRegexSearcher |
| 1050 | { |
| 1051 | public: |
| 1052 | CRegexSearcher(); |
| 1053 | ~CRegexSearcher(); |
| 1054 | |
| 1055 | /* |
| 1056 | * Search for a compiled pattern. Returns the byte offset of the |
| 1057 | * match, or -1 if no match was found. *result_len is filled in with |
| 1058 | * the byte length of the match if we found one. Note that the |
| 1059 | * returned index and result_len values are byte lengths, not |
| 1060 | * character lengths. |
| 1061 | * |
| 1062 | * The caller is responsible for providing a set of group registers, |
| 1063 | * which must be an array of registers of size RE_GROUP_REG_CNT. The |
| 1064 | * caller also must save the original search string if it will be |
| 1065 | * necessary to extract substrings based on the group registers. |
| 1066 | */ |
| 1067 | int search_for_pattern(const re_compiled_pattern *pattern, |
| 1068 | const char *entirestr, |
| 1069 | const char *searchstr, size_t searchlen, |
| 1070 | int *result_len, re_group_register *regs); |
| 1071 | |
| 1072 | /* |
| 1073 | * Search backwards for a compiled pattern. This is similar to |
| 1074 | * search_for_pattern(), but searches backwards from the starting |
| 1075 | * position. The match must end BEFORE the character at the starting |
| 1076 | * position; the match can't overlap or include that character. |
| 1077 | * |
| 1078 | * On success, the return value is non-negative, and gives the number |
| 1079 | * of bytes before the starting position where the match begins. To |
| 1080 | * get the starting byte pointer, subtract the return value from the |
| 1081 | * 'searchstr' pointer. On failure, returns -1. |
| 1082 | */ |
| 1083 | int search_back_for_pattern(const re_compiled_pattern *pattern, |
| 1084 | const char *entirestr, |
| 1085 | const char *searchstr, size_t searchlen, |
| 1086 | int *result_len, re_group_register *regs); |
| 1087 | |
| 1088 | /* |
| 1089 | * Check for a match to a previously compiled expression. Returns the |
| 1090 | * length of the match if we found a match, -1 if we found no match. |
| 1091 | * This is not a search function; we merely match the leading |
| 1092 | * substring of the given string to the given pattern. Note that the |
| 1093 | * returned length is a byte length, not a character length. |
| 1094 | * |
| 1095 | * The caller is responsible for providing a set of group registers, |
| 1096 | * which must be an array of registers of size RE_GROUP_REG_CNT. The |
| 1097 | * caller also must save the original search string if it will be |
| 1098 | * necessary to extract substrings based on the group registers. |
| 1099 | */ |
| 1100 | int match_pattern(const re_compiled_pattern *pattern, |
| 1101 | const char *entirestr, |
| 1102 | const char *searchstr, size_t searchlen, |
| 1103 | re_group_register *regs); |
| 1104 | |
| 1105 | /* |
| 1106 | * Get/set the default case sensititivy for searching and matching. |
| 1107 | * This controls the case sensitivity for patterns that don't include |
| 1108 | * explicit <case> or <nocase> flags. Explicit flags in the pattern |
| 1109 | * override this default. |
| 1110 | */ |
| 1111 | int get_default_case_sensitive() const { return default_case_sensitive_; } |
| 1112 | void set_default_case_sensitive(int f) { default_case_sensitive_ = f; } |
| 1113 | |
| 1114 | protected: |
| 1115 | /* match a string to a compiled expression */ |
| 1116 | int match(const char *entire_str, size_t entire_len, |
| 1117 | const char *str, size_t origlen, |
| 1118 | const re_compiled_pattern_base *pattern, |
| 1119 | const re_tuple *tuple_arr, |
| 1120 | const struct re_machine *machine, |
| 1121 | re_group_register *regs, short *loop_vars); |
| 1122 | |
| 1123 | /* search for a regular expression within a string */ |
| 1124 | int search(const char *entire_str, |
| 1125 | const char *str, size_t len, |
| 1126 | const re_compiled_pattern_base *pattern, |
| 1127 | const re_tuple *tuple_arr, |
| 1128 | const struct re_machine *machine, |
| 1129 | re_group_register *regs, int *result_len); |
| 1130 | |
| 1131 | /* search backwards for a regular expression within a string */ |
| 1132 | int search_back(const char *entire_str, |
| 1133 | const char *str, size_t len, |
| 1134 | const re_compiled_pattern_base *pattern, |
| 1135 | const re_tuple *tuple_arr, |
| 1136 | const struct re_machine *machine, |
| 1137 | re_group_register *regs, int *result_len); |
| 1138 | |
| 1139 | /* clear a set of group registers */ |
| 1140 | void clear_group_regs(re_group_register *regs) |
| 1141 | { |
| 1142 | /* set the start and end offsets for all registers to -1 */ |
| 1143 | int i; |
| 1144 | re_group_register *r; |
| 1145 | for (r = regs, i = 0 ; i < RE_GROUP_REG_CNT ; ++i, ++r) |
| 1146 | r->start_ofs = r->end_ofs = -1; |
| 1147 | } |
| 1148 | |
| 1149 | /* |
| 1150 | * Determine if a character is part of a word. We consider letters |
| 1151 | * and numbers to be word characters. |
| 1152 | */ |
| 1153 | static int is_word_char(wchar_t c) |
| 1154 | { |
| 1155 | return (t3_is_alpha(c) || t3_is_digit(c)); |
| 1156 | } |
| 1157 | |
| 1158 | /* match state stack */ |
| 1159 | CRegexStack stack_; |
| 1160 | |
| 1161 | /* default case sensitivity, for patterns that don't specify it */ |
| 1162 | unsigned int default_case_sensitive_ : 1; |
| 1163 | }; |
| 1164 | |
| 1165 | /* |
| 1166 | * Simplified Searcher - this class provides some high-level methods that |
| 1167 | * simplify one-off searches that combine compilation and searching into |
| 1168 | * one step. |
| 1169 | */ |
| 1170 | class CRegexSearcherSimple: public CRegexSearcher |
| 1171 | { |
| 1172 | public: |
| 1173 | CRegexSearcherSimple(class CRegexParser *parser) |
| 1174 | { |
| 1175 | /* remember my parser */ |
| 1176 | parser_ = parser; |
| 1177 | } |
| 1178 | |
| 1179 | ~CRegexSearcherSimple() |
| 1180 | { |
| 1181 | } |
| 1182 | |
| 1183 | /* clear out our group registers */ |
| 1184 | void clear_group_regs() |
| 1185 | { |
| 1186 | /* clear the last match register */ |
| 1187 | match_.start_ofs = match_.end_ofs = -1; |
| 1188 | |
| 1189 | /* clear the actual group registers */ |
| 1190 | CRegexSearcher::clear_group_regs(regs_); |
| 1191 | } |
| 1192 | |
| 1193 | /* search for a pattern, using our internal group registers */ |
| 1194 | int search_for_pattern(const re_compiled_pattern *pattern, |
| 1195 | const char *entirestr, |
| 1196 | const char *searchstr, size_t searchlen, |
| 1197 | int *result_len) |
| 1198 | { |
| 1199 | /* remember the group count from the compiled pattern */ |
| 1200 | group_cnt_ = pattern->group_cnt; |
| 1201 | |
| 1202 | /* clear the group registers */ |
| 1203 | clear_group_regs(); |
| 1204 | |
| 1205 | /* search for the compiled pattern using our group register */ |
| 1206 | int m = CRegexSearcher::search_for_pattern( |
| 1207 | pattern, entirestr, searchstr, searchlen, result_len, regs_); |
| 1208 | |
| 1209 | /* save the match information on success */ |
| 1210 | if (m >= 0) |
| 1211 | { |
| 1212 | match_.start_ofs = m; |
| 1213 | match_.end_ofs = m + *result_len; |
| 1214 | } |
| 1215 | |
| 1216 | /* return the result */ |
| 1217 | return m; |
| 1218 | } |
| 1219 | |
| 1220 | /* search backwards for a pattern, using internal group registers */ |
| 1221 | int search_back_for_pattern(const re_compiled_pattern *pattern, |
| 1222 | const char *entirestr, |
| 1223 | const char *searchstr, size_t searchlen, |
| 1224 | int *result_len) |
| 1225 | { |
| 1226 | /* remember the group count from the compiled pattern */ |
| 1227 | group_cnt_ = pattern->group_cnt; |
| 1228 | |
| 1229 | /* clear the group registers */ |
| 1230 | clear_group_regs(); |
| 1231 | |
| 1232 | /* search for the compiled pattern using our group register */ |
| 1233 | int m = CRegexSearcher::search_back_for_pattern( |
| 1234 | pattern, entirestr, searchstr, searchlen, result_len, regs_); |
| 1235 | |
| 1236 | /* save the match information on success */ |
| 1237 | if (m >= 0) |
| 1238 | { |
| 1239 | match_.start_ofs = searchstr - entirestr - m; |
| 1240 | match_.end_ofs = match_.start_ofs + *result_len; |
| 1241 | } |
| 1242 | |
| 1243 | /* return the result */ |
| 1244 | return m; |
| 1245 | } |
| 1246 | |
| 1247 | /* match a pattern, using our internal group registers */ |
| 1248 | int match_pattern(const re_compiled_pattern *pattern, |
| 1249 | const char *entirestr, |
| 1250 | const char *searchstr, size_t searchlen) |
| 1251 | { |
| 1252 | /* remember the group count from the compiled pattern */ |
| 1253 | group_cnt_ = pattern->group_cnt; |
| 1254 | |
| 1255 | /* clear the group registers */ |
| 1256 | clear_group_regs(); |
| 1257 | |
| 1258 | /* search for the compiled pattern using our group register */ |
| 1259 | int m = CRegexSearcher::match_pattern( |
| 1260 | pattern, entirestr, searchstr, searchlen, regs_); |
| 1261 | |
| 1262 | /* save the match information on success */ |
| 1263 | if (m >= 0) |
| 1264 | { |
| 1265 | match_.start_ofs = searchstr - entirestr; |
| 1266 | match_.end_ofs = match_.start_ofs + m; |
| 1267 | } |
| 1268 | |
| 1269 | /* return the result */ |
| 1270 | return m; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | * Compile an expression and search for a match within the given |
| 1275 | * string. Returns the byte offset of the match, or -1 if no match |
| 1276 | * was found. *result_len is filled in with the byte length of the |
| 1277 | * match if we found one. Note that the returned index and result_len |
| 1278 | * values are byte lengths, not character lengths. |
| 1279 | */ |
| 1280 | int compile_and_search(const char *pattern, size_t patlen, |
| 1281 | const char *entirestr, |
| 1282 | const char *searchstr, size_t searchlen, |
| 1283 | int *result_len); |
| 1284 | |
| 1285 | /* compile and search backwards */ |
| 1286 | int compile_and_search_back(const char *pattern, size_t patlen, |
| 1287 | const char *entirestr, |
| 1288 | const char *searchstr, size_t searchlen, |
| 1289 | int *result_len); |
| 1290 | |
| 1291 | /* |
| 1292 | * Compile an expression and check for a match. Returns the byte |
| 1293 | * length of the match if we found a match, -1 if we found no match. |
| 1294 | * This is not a search function; we merely match the leading |
| 1295 | * substring of the given string to the given pattern. Note that the |
| 1296 | * returned length is a byte length, not a character length. |
| 1297 | */ |
| 1298 | int compile_and_match(const char *pattern, size_t patlen, |
| 1299 | const char *entirestr, |
| 1300 | const char *searchstr, size_t searchlen); |
| 1301 | |
| 1302 | /* |
| 1303 | * Get a group register. 0 refers to the first group; groups are |
| 1304 | * numbered in left-to-right order by their opening parenthesis. |
| 1305 | */ |
| 1306 | const re_group_register *get_group_reg(int i) const { return ®s_[i]; } |
| 1307 | |
| 1308 | /* get the last match */ |
| 1309 | const re_group_register *get_last_match() const { return &match_; } |
| 1310 | |
| 1311 | /* get the number of groups in the last pattern we searched */ |
| 1312 | int get_group_cnt() const { return group_cnt_; } |
| 1313 | |
| 1314 | /* copy group registers from another search */ |
| 1315 | void copy_group_regs(CRegexSearcherSimple *s) |
| 1316 | { |
| 1317 | memcpy(regs_, s->regs_, sizeof(regs_)); |
| 1318 | group_cnt_ = s->group_cnt_; |
| 1319 | } |
| 1320 | |
| 1321 | protected: |
| 1322 | /* group registers */ |
| 1323 | re_group_register regs_[RE_GROUP_REG_CNT]; |
| 1324 | |
| 1325 | /* last match location */ |
| 1326 | re_group_register match_; |
| 1327 | |
| 1328 | /* number of groups in last pattern we searched */ |
| 1329 | int group_cnt_; |
| 1330 | |
| 1331 | /* my regular expression parser */ |
| 1332 | class CRegexParser *parser_; |
| 1333 | }; |
| 1334 | |
| 1335 | #endif /* VMREGEX_H */ |
| 1336 |