Changeset 439
- Timestamp:
- 11/22/08 00:48:47 (3 years ago)
- Location:
- interpreter/trunk/regex
- Files:
-
- 4 edited
-
microregex.h (modified) (2 diffs)
-
microregex_internal.h (modified) (4 diffs)
-
microregex_matcher.c (modified) (22 diffs)
-
microregex_parser.c (modified) (40 diffs)
Legend:
- Unmodified
- Added
- Removed
-
interpreter/trunk/regex/microregex.h
r364 r439 54 54 * @return The created regular expression object. 55 55 */ 56 microregex_t microregex_create(c har *regex);56 microregex_t microregex_create(const char *regex); 57 57 58 58 /** … … 91 91 */ 92 92 microregex_state_t 93 microregex_match(microregex_t regex_obj, c har *str, int start);93 microregex_match(microregex_t regex_obj, const char *str, int start); 94 94 95 95 /** -
interpreter/trunk/regex/microregex_internal.h
r429 r439 54 54 char *character_class; 55 55 int invert, loop_from, loop_to; 56 int ref; 56 57 57 58 struct microregex_nfa_t *next1, *next2; 59 microregex_nfa_t prevL, nextL; 58 60 }; 59 61 … … 63 65 struct microregex_t 64 66 { 65 c har *regex;67 const char *regex; 66 68 int paren_count; 67 69 int max_paren_count; … … 69 71 int case_insensitive; 70 72 microregex_nfa_t nfa; 73 microregex_nfa_t nlist; 71 74 }; 72 75 … … 86 89 int loop_count; 87 90 struct microregex_state_t *prev, *next; 91 struct microregex_state_t *prevL, *nextL; /* allocation list, used to free all re's */ 88 92 }; 89 93 -
interpreter/trunk/regex/microregex_matcher.c
r412 r439 33 33 #include "microregex_internal.h" 34 34 35 typedef struct tsmstList tMstList; 36 37 struct tsmstList { 38 microregex_state_t *state; 39 tMstList *prev; 40 }; 41 42 35 43 static microregex_state_t 36 microregex_state_new( )44 microregex_state_new(microregex_state_t *lst) 37 45 { 38 46 microregex_state_t ret = malloc(sizeof(struct microregex_state_t)); … … 50 58 ret->match_end = 0; 51 59 ret->insensitive = 0; 60 if (*lst) { 61 ret->prevL = (*lst); 62 ret->nextL = (*lst)->nextL; 63 if (ret->nextL) ret->nextL->prevL = ret; 64 (*lst)->nextL = ret; 65 } else { 66 ret->prevL = ret->nextL = NULL; 67 *lst = ret; 68 } 52 69 return ret; 53 70 } 54 71 55 72 static microregex_state_t 56 microregex_state_clone (microregex_state_tst)73 microregex_state_clone_ex(microregex_state_t st, int addToList) 57 74 { 58 75 int i; … … 74 91 ret->loop_count = st->loop_count; 75 92 ret->insensitive = st->insensitive; 93 if (addToList) { 94 ret->prevL = st; 95 ret->nextL = st->nextL; 96 if (ret->nextL) ret->nextL->prevL = ret; 97 st->nextL = ret; 98 } else ret->prevL = ret->nextL = NULL; 76 99 return ret; 77 100 } 78 101 102 static microregex_state_t 103 microregex_state_clone(microregex_state_t st) 104 { 105 return microregex_state_clone_ex(st, 1); 106 } 107 79 108 void 80 microregex_state_destroy(microregex_state_t *st) 81 { 109 microregex_state_destroy_ex(microregex_state_t *st, int removeFromList) 110 { 111 if (!st || !*st) return; 82 112 int i; 113 if (removeFromList) { 114 if ((*st)->nextL) (*st)->nextL->prevL = (*st)->prevL; 115 if ((*st)->prevL) (*st)->prevL->nextL = (*st)->nextL; 116 } 83 117 free((*st)->se_stack); 84 118 for (i = 0; i < (*st)->num_ses; i++) … … 92 126 93 127 128 static void microregex_state_destroy_list (microregex_state_t st) { 129 if (!st) return; 130 while (st->prevL) st = st->prevL; 131 while (st) { 132 microregex_state_t p = st->nextL; 133 microregex_state_destroy_ex(&st, 0); 134 st = p; 135 } 136 } 137 138 void microregex_state_destroy (microregex_state_t *st) { 139 if (!st || !*st) return; 140 microregex_state_destroy_list(*st); 141 *st = NULL; 142 } 143 144 145 94 146 /****************************************************************************** 95 147 * State machine. … … 97 149 static void 98 150 microregex_regex_push_submatch(microregex_state_t *st_lst, microregex_state_t cur, 99 c har *str,char *c);151 const char *str, const char *c); 100 152 static void 101 153 microregex_regex_pop_submatch(microregex_state_t *st_lst, microregex_state_t cur, 102 c har *str,char *c);154 const char *str, const char *c); 103 155 static void 104 156 microregex_regex_character_class(microregex_state_t *st_lst, microregex_state_t cur, 105 c har *str,char *c);157 const char *str, const char *c); 106 158 static void 107 159 microregex_regex_alteration(microregex_state_t *st_lst, microregex_state_t cur, 108 c har *str,char *c);160 const char *str, const char *c); 109 161 static void 110 162 microregex_regex_assert_beginning(microregex_state_t *st_lst, microregex_state_t cur, 111 c har *str,char *c);163 const char *str, const char *c); 112 164 static void 113 165 microregex_regex_assert_end(microregex_state_t *st_lst, microregex_state_t cur, 114 c har *str,char *c);166 const char *str, const char *c); 115 167 static void 116 168 microregex_regex_counted_loop(microregex_state_t *st_lst, microregex_state_t cur, 117 c har *str,char *c);169 const char *str, const char *c); 118 170 static void 119 171 microregex_regex_counted_end(microregex_state_t *st_lst, microregex_state_t cur, 120 c har *str,char *c);172 const char *str, const char *c); 121 173 static void 122 174 microregex_regex_counted_begin(microregex_state_t *st_lst, microregex_state_t cur, 123 c har *str,char *c);175 const char *str, const char *c); 124 176 static void 125 177 microregex_regex_pass(microregex_state_t *st_lst, microregex_state_t cur, 126 c har *str,char *c);178 const char *str, const char *c); 127 179 static void 128 180 microregex_regex_word_boundary(microregex_state_t *st_lst, microregex_state_t cur, 129 c har *str,char *c);181 const char *str, const char *c); 130 182 static void 131 183 microregex_regex_backtrack_check(microregex_state_t *st_lst, microregex_state_t cur, 132 c har *str,char *c);184 const char *str, const char *c); 133 185 typedef void 134 (*state_machine_func_t)(microregex_state_t *, microregex_state_t, c har *,char *);186 (*state_machine_func_t)(microregex_state_t *, microregex_state_t, const char *, const char *); 135 187 static state_machine_func_t state_functions[] = { 136 188 microregex_regex_push_submatch, … … 201 253 static void 202 254 microregex_regex_push_submatch(microregex_state_t *st_lst, microregex_state_t cur, 203 c har *str,char *c)255 const char *str, const char *c) 204 256 { 205 257 (void)st_lst; … … 225 277 static void 226 278 microregex_regex_pop_submatch(microregex_state_t *st_lst, microregex_state_t cur, 227 c har *str,char *c)279 const char *str, const char *c) 228 280 { 229 281 (void)st_lst; … … 243 295 static void 244 296 microregex_regex_pass(microregex_state_t *st_lst, microregex_state_t cur, 245 c har *str,char *c)297 const char *str, const char *c) 246 298 { 247 299 (void)st_lst; … … 258 310 static void 259 311 microregex_regex_backtrack_check(microregex_state_t *st_lst, microregex_state_t cur, 260 c har *str,char *c)312 const char *str, const char *c) 261 313 { 262 314 char *curstr = cur->ses[cur->state->loop_to]; … … 281 333 static void 282 334 microregex_regex_alteration(microregex_state_t *st_lst, microregex_state_t cur, 283 c har *str,char *c)335 const char *str, const char *c) 284 336 { 285 337 (void)st_lst; … … 303 355 static void 304 356 microregex_regex_counted_loop(microregex_state_t *st_lst, microregex_state_t cur, 305 c har *str,char *c)357 const char *str, const char *c) 306 358 { 307 359 (void)st_lst; … … 334 386 static void 335 387 microregex_regex_counted_end(microregex_state_t *st_lst, microregex_state_t cur, 336 c har *str,char *c)388 const char *str, const char *c) 337 389 { 338 390 (void)st_lst; … … 350 402 static void 351 403 microregex_regex_counted_begin(microregex_state_t *st_lst, microregex_state_t cur, 352 c har *str,char *c)404 const char *str, const char *c) 353 405 { 354 406 (void)st_lst; … … 367 419 static void 368 420 microregex_regex_assert_end(microregex_state_t *st_lst, microregex_state_t cur, 369 c har *str,char *c)421 const char *str, const char *c) 370 422 { 371 423 (void)st_lst; … … 382 434 static void 383 435 microregex_regex_assert_beginning(microregex_state_t *st_lst, microregex_state_t cur, 384 c har *str,char *c)436 const char *str, const char *c) 385 437 { 386 438 (void)st_lst; … … 396 448 static void 397 449 microregex_regex_character_class(microregex_state_t *st_lst, microregex_state_t cur, 398 c har *str,char *c)450 const char *str, const char *c) 399 451 { 400 452 (void)st_lst; … … 446 498 static void 447 499 microregex_regex_word_boundary(microregex_state_t *st_lst, microregex_state_t cur, 448 c har *str,char *c)500 const char *str, const char *c) 449 501 { 450 502 int left = 0, right = 0; … … 463 515 else 464 516 { 465 if ( left = !(isalnum(*c) || (*c == '_')))517 if ((left = !(isalnum(*c) || (*c == '_')))) 466 518 { 467 519 right = isalnum(*(c+1)) || (*(c+1) == '_'); … … 482 534 483 535 microregex_state_t 484 microregex_match(microregex_t regex_obj, c har *str, int start)536 microregex_match(microregex_t regex_obj, const char *str, int start) 485 537 { 486 538 microregex_state_t st_lst = NULL; 487 microregex_state_t begin = microregex_state_new(); 539 microregex_state_t flist = NULL; 540 microregex_state_t begin = microregex_state_new(&flist); 488 541 begin->state = regex_obj->nfa; 489 542 begin->insensitive = regex_obj->case_insensitive; 490 543 begin->match_begin = begin->match_end = start; 491 microregex_nfa_addstateforward(&st_lst, microregex_state_new( ));544 microregex_nfa_addstateforward(&st_lst, microregex_state_new(&flist)); 492 545 microregex_nfa_addstateforward(&st_lst, begin); 493 546 microregex_state_t cur = begin, ret = NULL; 494 c har *curstr = str + start;547 const char *curstr = str + start; 495 548 496 549 while(*curstr) … … 517 570 if (*(curstr + 1)) 518 571 { 519 microregex_state_t newbegin = microregex_state_new( );572 microregex_state_t newbegin = microregex_state_new(&flist); 520 573 newbegin->state = regex_obj->nfa; 521 574 newbegin->insensitive = regex_obj->case_insensitive; … … 542 595 else 543 596 { 544 ret = microregex_state_clone(cur); 545 while(st_lst != NULL) 546 { 547 cur = st_lst; 548 microregex_nfa_remove(&st_lst); 549 microregex_state_destroy(&cur); 550 } 597 ret = microregex_state_clone_ex(cur, 0); 598 microregex_state_destroy_list(flist); 551 599 return ret; 552 600 } … … 554 602 cur = next; 555 603 } 556 557 /* Find longest leftmost match. */ 558 #if 0 559 int matchstart = strlen(str + start); 560 int matchlength = 0; 561 #endif 562 563 while(st_lst != NULL) 564 { 565 cur = st_lst; 566 567 /* This might be necessary, but I don't think it is. 568 * If there's a bug that requires the following code to fix, I'll remove 569 * the #ifdef. */ 570 #if 0 571 if (cur->state && cur->state->state_type == REGEX_END) 572 { 573 if (matchstart > cur->match_begin || 574 (matchstart == cur->match_begin && matchlength < strlen(cur->ses[0])) 575 ) 576 { 577 if (ret) microregex_state_destroy(&ret); 578 ret = microregex_state_clone(cur); 579 matchstart = cur->match_begin; 580 matchlength = strlen(cur->ses[0]); 581 } 582 } 583 #endif 584 585 microregex_nfa_remove(&st_lst); 586 microregex_state_destroy(&cur); 587 } 604 microregex_state_destroy_list(flist); 588 605 return ret; 589 606 } -
interpreter/trunk/regex/microregex_parser.c
r429 r439 33 33 #include "microregex_internal.h" 34 34 35 microregex_nfa_t microregex_parse_regex(microregex_t, char *, char **, int); 36 void microregex_destroy_nfa(microregex_nfa_t *); 37 38 static void 35 static microregex_nfa_t microregex_parse_regex(microregex_t, const char *, char **, int, microregex_nfa_t *); 36 37 static int 39 38 microregex_nfa_append(microregex_nfa_t *nfa, microregex_nfa_t end) 40 39 { … … 42 41 { 43 42 *nfa = end; 44 return ;45 } 46 43 return 1; 44 } 45 int added = 0; 47 46 if ((*nfa)->state_type != PASS && (*nfa)->state_type != REGEX_END && (*nfa)->next1 != end) 48 microregex_nfa_append(&(*nfa)->next1, end);47 added = microregex_nfa_append(&(*nfa)->next1, end); 49 48 if ((*nfa)->state_type == ALTERATION || (*nfa)->state_type == COUNTED_LOOP) 50 if ((*nfa)->next2 != end) 51 microregex_nfa_append(&(*nfa)->next2, end); 52 } 53 54 static microregex_nfa_t 55 microregex_nfa_backtrack_check(int backtrack) 56 { 57 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 49 if ((*nfa)->next2 != end) { 50 int added2 = microregex_nfa_append(&(*nfa)->next2, end); 51 added |= added2; 52 } 53 return added; 54 } 55 56 57 static void microregex_nfa_tolist (microregex_nfa_t ret, microregex_nfa_t *lst) { 58 if (*lst) { 59 ret->prevL = (*lst); 60 ret->nextL = (*lst)->nextL; 61 if (ret->nextL) ret->nextL->prevL = ret; 62 (*lst)->nextL = ret; 63 } else { 64 ret->prevL = ret->nextL = NULL; 65 *lst = ret; 66 } 67 } 68 69 70 static microregex_nfa_t 71 microregex_nfa_backtrack_check(int backtrack, microregex_nfa_t *lst) 72 { 73 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 74 microregex_nfa_tolist(ret, lst); 58 75 ret->state_type = BACKTRACK_CHECK; 59 76 ret->character_class = NULL; … … 66 83 67 84 static microregex_nfa_t 68 microregex_nfa_alteration(microregex_nfa_t next1, microregex_nfa_t next2) 69 { 70 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 85 microregex_nfa_alteration(microregex_nfa_t next1, microregex_nfa_t next2, microregex_nfa_t *lst) 86 { 87 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 88 microregex_nfa_tolist(ret, lst); 71 89 ret->state_type = ALTERATION; 72 90 ret->character_class = NULL; … … 78 96 79 97 static microregex_nfa_t 80 microregex_nfa_assert_beginning() 81 { 82 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 98 microregex_nfa_assert_beginning(microregex_nfa_t *lst) 99 { 100 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 101 microregex_nfa_tolist(ret, lst); 83 102 ret->state_type = ASSERT_BEGINNING; 84 103 ret->character_class = NULL; … … 90 109 91 110 static microregex_nfa_t 92 microregex_nfa_assert_end() 93 { 94 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 111 microregex_nfa_assert_end(microregex_nfa_t *lst) 112 { 113 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 114 microregex_nfa_tolist(ret, lst); 95 115 ret->state_type = ASSERT_END; 96 116 ret->character_class = NULL; … … 102 122 103 123 static microregex_nfa_t 104 microregex_nfa_counted_begin() 105 { 106 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 124 microregex_nfa_counted_begin(microregex_nfa_t *lst) 125 { 126 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 127 microregex_nfa_tolist(ret, lst); 107 128 ret->state_type = COUNTED_BEGIN; 108 129 ret->character_class = NULL; … … 114 135 115 136 static microregex_nfa_t 116 microregex_nfa_counted_loop(int from, int to) 117 { 118 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 137 microregex_nfa_counted_loop(int from, int to, microregex_nfa_t *lst) 138 { 139 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 140 microregex_nfa_tolist(ret, lst); 119 141 ret->state_type = COUNTED_LOOP; 120 142 ret->character_class = NULL; … … 128 150 129 151 static microregex_nfa_t 130 microregex_nfa_counted_end() 131 { 132 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 152 microregex_nfa_counted_end(microregex_nfa_t *lst) 153 { 154 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 155 microregex_nfa_tolist(ret, lst); 133 156 ret->state_type = COUNTED_END; 134 157 ret->character_class = NULL; … … 140 163 141 164 static microregex_nfa_t 142 microregex_nfa_push_submatch() 143 { 144 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 165 microregex_nfa_push_submatch(microregex_nfa_t *lst) 166 { 167 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 168 microregex_nfa_tolist(ret, lst); 145 169 ret->state_type = PUSH_SUBMATCH; 146 170 ret->character_class = NULL; … … 152 176 153 177 static microregex_nfa_t 154 microregex_nfa_pop_submatch() 155 { 156 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 178 microregex_nfa_pop_submatch(microregex_nfa_t *lst) 179 { 180 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 181 microregex_nfa_tolist(ret, lst); 157 182 ret->state_type = POP_SUBMATCH; 158 183 ret->character_class = NULL; … … 164 189 165 190 static microregex_nfa_t 166 microregex_nfa_end() 167 { 168 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 191 microregex_nfa_end(microregex_nfa_t *lst) 192 { 193 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 194 microregex_nfa_tolist(ret, lst); 169 195 ret->state_type = REGEX_END; 170 196 ret->character_class = NULL; … … 176 202 177 203 static microregex_nfa_t 178 microregex_nfa_pass() 179 { 180 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 204 microregex_nfa_pass(microregex_nfa_t *lst) 205 { 206 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 207 microregex_nfa_tolist(ret, lst); 181 208 ret->state_type = PASS; 182 209 ret->character_class = NULL; … … 188 215 189 216 static microregex_nfa_t 190 microregex_nfa_oneplus(microregex_nfa_t one, int greedy )191 { 192 microregex_nfa_t pass = microregex_nfa_pass( );217 microregex_nfa_oneplus(microregex_nfa_t one, int greedy, microregex_nfa_t *lst) 218 { 219 microregex_nfa_t pass = microregex_nfa_pass(lst); 193 220 microregex_nfa_t ret = 194 221 microregex_nfa_alteration( 195 222 greedy ? pass : NULL, 196 223 greedy ? NULL : pass 197 );224 , lst); 198 225 microregex_nfa_append(&one, ret); 199 226 pass->next1 = one; … … 202 229 203 230 static microregex_nfa_t 204 microregex_nfa_zeroplus(microregex_nfa_t one, int greedy )231 microregex_nfa_zeroplus(microregex_nfa_t one, int greedy, microregex_nfa_t *lst) 205 232 { 206 233 microregex_nfa_t ret = … … 208 235 greedy ? one : NULL, 209 236 greedy ? NULL : one 210 );211 microregex_nfa_t pass = microregex_nfa_pass( );237 , lst); 238 microregex_nfa_t pass = microregex_nfa_pass(lst); 212 239 microregex_nfa_append(&one, pass); 213 240 pass->next1 = ret; … … 216 243 217 244 static microregex_nfa_t 218 microregex_nfa_zeroone(microregex_t regex, microregex_nfa_t one, int greedy, int p )219 { 220 microregex_nfa_t pass = microregex_nfa_pass( );221 microregex_nfa_t ce = microregex_nfa_counted_end( );245 microregex_nfa_zeroone(microregex_t regex, microregex_nfa_t one, int greedy, int p, microregex_nfa_t *lst) 246 { 247 microregex_nfa_t pass = microregex_nfa_pass(lst); 248 microregex_nfa_t ce = microregex_nfa_counted_end(lst); 222 249 223 250 microregex_nfa_t parens = NULL; 224 251 int i; 225 252 for (i = p; i < regex->max_paren_count; i++) { 226 microregex_nfa_append(&parens, microregex_nfa_push_submatch( ));253 microregex_nfa_append(&parens, microregex_nfa_push_submatch(lst)); 227 254 } 228 255 for (i = regex->max_paren_count; i > p; i--) { 229 microregex_nfa_append(&parens, microregex_nfa_pop_submatch( ));256 microregex_nfa_append(&parens, microregex_nfa_pop_submatch(lst)); 230 257 } 231 258 microregex_nfa_append(&parens, ce); … … 235 262 greedy ? one : parens, 236 263 greedy ? parens : one 237 );264 , lst); 238 265 microregex_nfa_append(&one, pass); 239 266 pass->next1 = ce; … … 241 268 } 242 269 243 static c har *244 microregex_nfa_charliteral_process(c har *regex, char *buf)270 static const char * 271 microregex_nfa_charliteral_process(const char *regex, char *buf) 245 272 { 246 273 int i, j; … … 296 323 } 297 324 case '0': 298 { 299 buf[0] = (char)strtol(regex, ®ex, 8); 325 { char *tt; 326 buf[0] = (char)strtol(regex, &tt, 8); 327 regex = tt; 300 328 break; 301 329 } 302 330 case 'x': 303 { 304 buf[0] = (char)strtol(regex, ®ex, 16); 331 { char *tt; 332 buf[0] = (char)strtol(regex, &tt, 16); 333 regex = tt; 305 334 break; 306 335 } … … 387 416 388 417 static microregex_nfa_t 389 microregex_nfa_charliteral(char *regex, char **end_location) 390 { 391 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 418 microregex_nfa_charliteral(const char *regex, const char **end_location, microregex_nfa_t *lst) 419 { 420 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t)); 421 microregex_nfa_tolist(ret, lst); 392 422 char tmp_buf[256]; 393 423 … … 404 434 if (*regex == '[') 405 435 { 406 c har *tmp = regex + 1;436 const char *tmp = regex + 1; 407 437 if (*tmp == '^') 408 438 { … … 490 520 491 521 ret->state_type = COUNTED_BEGIN; 492 microregex_nfa_t bt = microregex_nfa_backtrack_check(backtrack );522 microregex_nfa_t bt = microregex_nfa_backtrack_check(backtrack, lst); 493 523 ret->next1 = bt; 494 524 } … … 516 546 */ 517 547 microregex_t 518 microregex_create(c har *regex)548 microregex_create(const char *regex) 519 549 { 520 550 microregex_t ret = (microregex_t)malloc(sizeof(struct microregex_t)); … … 523 553 ret->regex = regex; 524 554 ret->err_string = NULL; 525 ret->nfa = microregex_parse_regex(ret, regex, NULL, 0); 526 microregex_nfa_append(&ret->nfa, microregex_nfa_end()); 527 528 return ret; 529 } 530 531 /** 532 * Destroy regular expression object. 533 * @param regex_obj The regular expression object to destroy. 534 */ 535 void 536 microregex_destroy_nfa(microregex_nfa_t *regex_obj) 537 { 538 if (regex_obj == NULL || (*regex_obj) == NULL) return; 539 540 if ((*regex_obj)->state_type != PASS) 541 { 542 microregex_destroy_nfa(&(*regex_obj)->next1); 543 microregex_destroy_nfa(&(*regex_obj)->next2); 544 } 545 546 free((*regex_obj)->character_class); 547 free(*regex_obj); 548 *regex_obj = NULL; 555 ret->nlist = NULL; 556 ret->nfa = microregex_parse_regex(ret, regex, NULL, 0, &ret->nlist); 557 microregex_nfa_append(&ret->nfa, microregex_nfa_end(&ret->nlist)); 558 559 return ret; 549 560 } 550 561 … … 559 570 560 571 if ((*regex_obj)->err_string) free((*regex_obj)->err_string); 561 if ((*regex_obj)->nfa) microregex_destroy_nfa(&(*regex_obj)->nfa); 572 573 microregex_nfa_t lst = (*regex_obj)->nlist, nn; 574 while (lst) { 575 nn = lst->nextL; 576 free(lst->character_class); 577 free(lst); 578 lst = nn; 579 } 580 562 581 free(*regex_obj); 563 582 *regex_obj = NULL; … … 582 601 * @return The created NFA object. 583 602 */ 584 microregex_nfa_t585 microregex_parse_regex(microregex_t regex_obj, c har *regex, char **end_char, int paren_start)603 static microregex_nfa_t 604 microregex_parse_regex(microregex_t regex_obj, const char *regex, char **end_char, int paren_start, microregex_nfa_t *lst) 586 605 { 587 606 int cur_paren = regex_obj->paren_count; … … 607 626 microregex_nfa_append( 608 627 &prev, 609 microregex_nfa_push_submatch( )628 microregex_nfa_push_submatch(lst) 610 629 ); 611 630 microregex_nfa_append( … … 615 634 regex + 1, 616 635 &end_location, 617 paren_start + 1 636 paren_start + 1, 637 lst 618 638 ) 619 639 ); … … 628 648 microregex_nfa_append( 629 649 &prev, 630 microregex_nfa_pop_submatch( )650 microregex_nfa_pop_submatch(lst) 631 651 ); 632 652 } … … 649 669 regex + 1, 650 670 &end_location, 651 paren_start 652 ) 671 paren_start, 672 lst 673 ), lst 653 674 ); 654 675 … … 666 687 microregex_nfa_oneplus( 667 688 prev, 668 greedy 689 greedy, 690 lst 669 691 ); 670 692 … … 680 702 microregex_nfa_zeroplus( 681 703 prev, 682 greedy 704 greedy, 705 lst 683 706 ); 684 707 … … 696 719 prev, 697 720 greedy, 698 cur_paren 721 cur_paren, 722 lst 699 723 ); 700 724 … … 705 729 { 706 730 /* Counted repetition */ 707 char *cur_pos = regex; 708 int from = strtol(regex + 1, ®ex, 10); 731 const char *cur_pos = regex; char *tt; 732 int from = strtol(regex + 1, &tt, 10); 733 regex = tt; 709 734 int to; 710 735 if (!*regex || (*regex != ',' && *regex != '}')) … … 715 740 else if (*regex == ',') 716 741 { 717 to = strtol(regex + 1, ®ex, 10); 742 to = strtol(regex + 1, &tt, 10); 743 regex = tt; 718 744 if (*regex != '}') 719 745 { … … 731 757 } 732 758 733 microregex_nfa_t tmpnfa = microregex_nfa_counted_begin( );734 microregex_nfa_t counted_loop = microregex_nfa_counted_loop(from, to );759 microregex_nfa_t tmpnfa = microregex_nfa_counted_begin(lst); 760 microregex_nfa_t counted_loop = microregex_nfa_counted_loop(from, to, lst); 735 761 greedy = (*(++regex) != '?'); 736 microregex_nfa_t tmppass = microregex_nfa_pass( );762 microregex_nfa_t tmppass = microregex_nfa_pass(lst); 737 763 tmppass->next1 = counted_loop; 738 764 739 counted_loop->next1 = greedy ? prev : microregex_nfa_counted_end( );740 counted_loop->next2 = greedy ? microregex_nfa_counted_end( ) : prev;765 counted_loop->next1 = greedy ? prev : microregex_nfa_counted_end(lst); 766 counted_loop->next2 = greedy ? microregex_nfa_counted_end(lst) : prev; 741 767 microregex_nfa_append( 742 768 &prev, … … 755 781 microregex_nfa_append( 756 782 &nfa, 757 microregex_nfa_assert_beginning( )783 microregex_nfa_assert_beginning(lst) 758 784 ); 759 785 break; … … 764 790 microregex_nfa_append( 765 791 &nfa, 766 microregex_nfa_assert_end( )792 microregex_nfa_assert_end(lst) 767 793 ); 768 794 break; … … 775 801 prev = microregex_nfa_charliteral( 776 802 regex, 777 &end_location 803 (const char **)(&end_location), 804 lst 778 805 ); 779 780 806 regex = end_location; 781 807 } … … 786 812 787 813 if (prev) microregex_nfa_append(&nfa, prev); 788 if (end_char) *end_char = regex;814 if (end_char) *end_char = (char *)regex; 789 815 return nfa; 790 816 }
Note: See TracChangeset
for help on using the changeset viewer.
