Changeset 437
- Timestamp:
- 11/22/08 00:44:27 (3 years ago)
- Location:
- microregex/trunk
- Files:
-
- 5 edited
-
ChangeLog (modified) (1 diff)
-
microregex.h (modified) (2 diffs)
-
microregex_internal.h (modified) (4 diffs)
-
microregex_matcher.c (modified) (22 diffs)
-
microregex_parser.c (modified) (32 diffs)
Legend:
- Unmodified
- Added
- Removed
-
microregex/trunk/ChangeLog
r436 r437 1 11/22/2008: 2 * Applied patch (ticket #68) by anonymous contributor to fix leaks. 3 1 4 11/15/2008: 2 5 * Fixed double free issue by adding reference counting. (ticket #66) -
microregex/trunk/microregex.h
r362 r437 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 /** -
microregex/trunk/microregex_internal.h
r436 r437 57 57 58 58 struct microregex_nfa_t *next1, *next2; 59 microregex_nfa_t prevL, nextL; 59 60 }; 60 61 … … 64 65 struct microregex_t 65 66 { 66 c har *regex;67 const char *regex; 67 68 int paren_count; 68 69 int max_paren_count; … … 70 71 int case_insensitive; 71 72 microregex_nfa_t nfa; 73 microregex_nfa_t nlist; 72 74 }; 73 75 … … 87 89 int loop_count; 88 90 struct microregex_state_t *prev, *next; 91 struct microregex_state_t *prevL, *nextL; /* allocation list, used to free all re's */ 89 92 }; 90 93 -
microregex/trunk/microregex_matcher.c
r411 r437 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 } -
microregex/trunk/microregex_parser.c
r436 r437 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 { 41 40 if (!*nfa) 42 41 { 43 end->ref++;44 42 *nfa = end; 45 return ;46 } 47 43 return 1; 44 } 45 int added = 0; 48 46 if ((*nfa)->state_type != PASS && (*nfa)->state_type != REGEX_END && (*nfa)->next1 != end) 49 microregex_nfa_append(&(*nfa)->next1, end);47 added = microregex_nfa_append(&(*nfa)->next1, end); 50 48 if ((*nfa)->state_type == ALTERATION || (*nfa)->state_type == COUNTED_LOOP) 51 if ((*nfa)->next2 != end) 52 microregex_nfa_append(&(*nfa)->next2, end); 53 } 54 55 static microregex_nfa_t 56 microregex_nfa_backtrack_check(int backtrack) 57 { 58 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); 59 75 ret->state_type = BACKTRACK_CHECK; 60 76 ret->character_class = NULL; 61 77 ret->invert = 0; 62 ret->ref = 0;63 78 ret->loop_to = backtrack; 64 79 ret->next1 = NULL; … … 68 83 69 84 static microregex_nfa_t 70 microregex_nfa_alteration(microregex_nfa_t next1, microregex_nfa_t next2) 71 { 72 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); 73 89 ret->state_type = ALTERATION; 74 90 ret->character_class = NULL; 75 91 ret->invert = 0; 76 ret->ref = 0;77 92 ret->next1 = next1; 78 93 ret->next2 = next2; … … 81 96 82 97 static microregex_nfa_t 83 microregex_nfa_assert_beginning() 84 { 85 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); 86 102 ret->state_type = ASSERT_BEGINNING; 87 103 ret->character_class = NULL; 88 104 ret->invert = 0; 89 ret-> ref = 0;90 ret->next 1= NULL;91 ret ->next2 = NULL;92 return ret; 93 } 94 95 static microregex_nfa_t 96 microregex_nfa_assert_end() 97 { 98 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t));105 ret->next1 = NULL; 106 ret->next2 = NULL; 107 return ret; 108 } 109 110 static 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); 99 115 ret->state_type = ASSERT_END; 100 116 ret->character_class = NULL; 101 117 ret->invert = 0; 102 ret-> ref = 0;103 ret->next 1= NULL;104 ret ->next2 = NULL;105 return ret; 106 } 107 108 static microregex_nfa_t 109 microregex_nfa_counted_begin() 110 { 111 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t));118 ret->next1 = NULL; 119 ret->next2 = NULL; 120 return ret; 121 } 122 123 static 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); 112 128 ret->state_type = COUNTED_BEGIN; 113 129 ret->character_class = NULL; 114 130 ret->invert = 0; 115 ret-> ref = 0;116 ret->next 1= NULL;117 ret ->next2 = NULL;118 return ret; 119 } 120 121 static microregex_nfa_t 122 microregex_nfa_counted_loop(int from, int to) 123 { 124 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t));131 ret->next1 = NULL; 132 ret->next2 = NULL; 133 return ret; 134 } 135 136 static 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); 125 141 ret->state_type = COUNTED_LOOP; 126 142 ret->character_class = NULL; 127 143 ret->invert = 0; 128 ret->ref = 0;129 144 ret->loop_from = from; 130 145 ret->loop_to = to; … … 135 150 136 151 static microregex_nfa_t 137 microregex_nfa_counted_end() 138 { 139 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); 140 156 ret->state_type = COUNTED_END; 141 157 ret->character_class = NULL; 142 158 ret->invert = 0; 143 ret-> ref = 0;144 ret->next 1= NULL;145 ret ->next2 = NULL;146 return ret; 147 } 148 149 static microregex_nfa_t 150 microregex_nfa_push_submatch() 151 { 152 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t));159 ret->next1 = NULL; 160 ret->next2 = NULL; 161 return ret; 162 } 163 164 static 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); 153 169 ret->state_type = PUSH_SUBMATCH; 154 170 ret->character_class = NULL; 155 171 ret->invert = 0; 156 ret-> ref = 0;157 ret->next 1= NULL;158 ret ->next2 = NULL;159 return ret; 160 } 161 162 static microregex_nfa_t 163 microregex_nfa_pop_submatch() 164 { 165 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t));172 ret->next1 = NULL; 173 ret->next2 = NULL; 174 return ret; 175 } 176 177 static 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); 166 182 ret->state_type = POP_SUBMATCH; 167 183 ret->character_class = NULL; 168 184 ret->invert = 0; 169 ret-> ref = 0;170 ret->next 1= NULL;171 ret ->next2 = NULL;172 return ret; 173 } 174 175 static microregex_nfa_t 176 microregex_nfa_end() 177 { 178 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t));185 ret->next1 = NULL; 186 ret->next2 = NULL; 187 return ret; 188 } 189 190 static 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); 179 195 ret->state_type = REGEX_END; 180 196 ret->character_class = NULL; 181 197 ret->invert = 0; 182 ret-> ref = 0;183 ret->next 1= NULL;184 ret ->next2 = NULL;185 return ret; 186 } 187 188 static microregex_nfa_t 189 microregex_nfa_pass() 190 { 191 microregex_nfa_t ret = (microregex_nfa_t)malloc(sizeof(struct microregex_nfa_t));198 ret->next1 = NULL; 199 ret->next2 = NULL; 200 return ret; 201 } 202 203 static 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); 192 208 ret->state_type = PASS; 193 209 ret->character_class = NULL; 194 210 ret->invert = 0; 195 ret->ref = 0; 196 ret->next1 = NULL; 197 ret->next2 = NULL; 198 return ret; 199 } 200 201 static microregex_nfa_t 202 microregex_nfa_oneplus(microregex_nfa_t one, int greedy) 203 { 204 microregex_nfa_t pass = microregex_nfa_pass(); 211 ret->next1 = NULL; 212 ret->next2 = NULL; 213 return ret; 214 } 215 216 static microregex_nfa_t 217 microregex_nfa_oneplus(microregex_nfa_t one, int greedy, microregex_nfa_t *lst) 218 { 219 microregex_nfa_t pass = microregex_nfa_pass(lst); 205 220 microregex_nfa_t ret = 206 221 microregex_nfa_alteration( 207 222 greedy ? pass : NULL, 208 223 greedy ? NULL : pass 209 );224 , lst); 210 225 microregex_nfa_append(&one, ret); 211 226 pass->next1 = one; … … 214 229 215 230 static microregex_nfa_t 216 microregex_nfa_zeroplus(microregex_nfa_t one, int greedy )231 microregex_nfa_zeroplus(microregex_nfa_t one, int greedy, microregex_nfa_t *lst) 217 232 { 218 233 microregex_nfa_t ret = … … 220 235 greedy ? one : NULL, 221 236 greedy ? NULL : one 222 );223 microregex_nfa_t pass = microregex_nfa_pass( );237 , lst); 238 microregex_nfa_t pass = microregex_nfa_pass(lst); 224 239 microregex_nfa_append(&one, pass); 225 240 pass->next1 = ret; … … 228 243 229 244 static microregex_nfa_t 230 microregex_nfa_zeroone(microregex_t regex, microregex_nfa_t one, int greedy, int p )231 { 232 microregex_nfa_t pass = microregex_nfa_pass( );233 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); 234 249 235 250 microregex_nfa_t parens = NULL; 236 251 int i; 237 252 for (i = p; i < regex->max_paren_count; i++) { 238 microregex_nfa_append(&parens, microregex_nfa_push_submatch( ));253 microregex_nfa_append(&parens, microregex_nfa_push_submatch(lst)); 239 254 } 240 255 for (i = regex->max_paren_count; i > p; i--) { 241 microregex_nfa_append(&parens, microregex_nfa_pop_submatch( ));256 microregex_nfa_append(&parens, microregex_nfa_pop_submatch(lst)); 242 257 } 243 258 microregex_nfa_append(&parens, ce); … … 247 262 greedy ? one : parens, 248 263 greedy ? parens : one 249 );264 , lst); 250 265 microregex_nfa_append(&one, pass); 251 266 pass->next1 = ce; … … 253 268 } 254 269 255 static c har *256 microregex_nfa_charliteral_process(c har *regex, char *buf)270 static const char * 271 microregex_nfa_charliteral_process(const char *regex, char *buf) 257 272 { 258 273 int i, j; … … 308 323 } 309 324 case '0': 310 { 311 buf[0] = (char)strtol(regex, ®ex, 8); 325 { char *tt; 326 buf[0] = (char)strtol(regex, &tt, 8); 327 regex = tt; 312 328 break; 313 329 } 314 330 case 'x': 315 { 316 buf[0] = (char)strtol(regex, ®ex, 16); 331 { char *tt; 332 buf[0] = (char)strtol(regex, &tt, 16); 333 regex = tt; 317 334 break; 318 335 } … … 399 416 400 417 static microregex_nfa_t 401 microregex_nfa_charliteral(char *regex, char **end_location) 402 { 403 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); 404 422 char tmp_buf[256]; 405 423 … … 407 425 ret->character_class = NULL; 408 426 ret->invert = 0; 409 ret->ref = 0;410 427 ret->next1 = NULL; 411 428 ret->next2 = NULL; … … 417 434 if (*regex == '[') 418 435 { 419 c har *tmp = regex + 1;436 const char *tmp = regex + 1; 420 437 if (*tmp == '^') 421 438 { … … 503 520 504 521 ret->state_type = COUNTED_BEGIN; 505 microregex_nfa_t bt = microregex_nfa_backtrack_check(backtrack );522 microregex_nfa_t bt = microregex_nfa_backtrack_check(backtrack, lst); 506 523 ret->next1 = bt; 507 524 } … … 529 546 */ 530 547 microregex_t 531 microregex_create(c har *regex)548 microregex_create(const char *regex) 532 549 { 533 550 microregex_t ret = (microregex_t)malloc(sizeof(struct microregex_t)); … … 536 553 ret->regex = regex; 537 554 ret->err_string = NULL; 538 ret->nfa = microregex_parse_regex(ret, regex, NULL, 0); 539 microregex_nfa_append(&ret->nfa, microregex_nfa_end()); 540 541 return ret; 542 } 543 544 /** 545 * Destroy regular expression object. 546 * @param regex_obj The regular expression object to destroy. 547 */ 548 void 549 microregex_destroy_nfa(microregex_nfa_t *regex_obj) 550 { 551 if (regex_obj == NULL || (*regex_obj) == NULL) return; 552 553 if ((*regex_obj)->state_type != PASS) 554 { 555 microregex_destroy_nfa(&(*regex_obj)->next1); 556 microregex_destroy_nfa(&(*regex_obj)->next2); 557 } 558 559 (*regex_obj)->ref--; 560 if ((*regex_obj)->ref <= 0) 561 { 562 free((*regex_obj)->character_class); 563 free(*regex_obj); 564 } 565 *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; 566 560 } 567 561 … … 576 570 577 571 if ((*regex_obj)->err_string) free((*regex_obj)->err_string); 578 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 579 581 free(*regex_obj); 580 582 *regex_obj = NULL; … … 599 601 * @return The created NFA object. 600 602 */ 601 microregex_nfa_t602 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) 603 605 { 604 606 int cur_paren = regex_obj->paren_count; … … 624 626 microregex_nfa_append( 625 627 &prev, 626 microregex_nfa_push_submatch( )628 microregex_nfa_push_submatch(lst) 627 629 ); 628 630 microregex_nfa_append( … … 632 634 regex + 1, 633 635 &end_location, 634 paren_start + 1 636 paren_start + 1, 637 lst 635 638 ) 636 639 ); … … 645 648 microregex_nfa_append( 646 649 &prev, 647 microregex_nfa_pop_submatch( )650 microregex_nfa_pop_submatch(lst) 648 651 ); 649 652 } … … 666 669 regex + 1, 667 670 &end_location, 668 paren_start 669 ) 671 paren_start, 672 lst 673 ), lst 670 674 ); 671 675 … … 683 687 microregex_nfa_oneplus( 684 688 prev, 685 greedy 689 greedy, 690 lst 686 691 ); 687 692 … … 697 702 microregex_nfa_zeroplus( 698 703 prev, 699 greedy 704 greedy, 705 lst 700 706 ); 701 707 … … 713 719 prev, 714 720 greedy, 715 cur_paren 721 cur_paren, 722 lst 716 723 ); 717 724 … … 722 729 { 723 730 /* Counted repetition */ 724 char *cur_pos = regex; 725 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; 726 734 int to; 727 735 if (!*regex || (*regex != ',' && *regex != '}')) … … 732 740 else if (*regex == ',') 733 741 { 734 to = strtol(regex + 1, ®ex, 10); 742 to = strtol(regex + 1, &tt, 10); 743 regex = tt; 735 744 if (*regex != '}') 736 745 { … … 748 757 } 749 758 750 microregex_nfa_t tmpnfa = microregex_nfa_counted_begin( );751 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); 752 761 greedy = (*(++regex) != '?'); 753 microregex_nfa_t tmppass = microregex_nfa_pass( );762 microregex_nfa_t tmppass = microregex_nfa_pass(lst); 754 763 tmppass->next1 = counted_loop; 755 764 756 counted_loop->next1 = greedy ? prev : microregex_nfa_counted_end( );757 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; 758 767 microregex_nfa_append( 759 768 &prev, … … 772 781 microregex_nfa_append( 773 782 &nfa, 774 microregex_nfa_assert_beginning( )783 microregex_nfa_assert_beginning(lst) 775 784 ); 776 785 break; … … 781 790 microregex_nfa_append( 782 791 &nfa, 783 microregex_nfa_assert_end( )792 microregex_nfa_assert_end(lst) 784 793 ); 785 794 break; … … 792 801 prev = microregex_nfa_charliteral( 793 802 regex, 794 &end_location 803 (const char **)(&end_location), 804 lst 795 805 ); 796 797 806 regex = end_location; 798 807 } … … 803 812 804 813 if (prev) microregex_nfa_append(&nfa, prev); 805 if (end_char) *end_char = regex;814 if (end_char) *end_char = (char *)regex; 806 815 return nfa; 807 816 }
Note: See TracChangeset
for help on using the changeset viewer.
