| 1 |
#ifdef RCSID |
| 2 |
static char RCSid[] = |
| 3 |
"$Header: d:/cvsroot/tads/TADS2/regex.c,v 1.3 1999/07/11 00:46:34 MJRoberts Exp $"; |
| 4 |
#endif |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
#include <ctype.h> |
| 65 |
#include <stdio.h> |
| 66 |
#include <stdlib.h> |
| 67 |
#include <string.h> |
| 68 |
|
| 69 |
#include "std.h" |
| 70 |
#include "mch.h" |
| 71 |
#include "ler.h" |
| 72 |
#include "regex.h" |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#define RE_EPSILON '\001' |
| 98 |
|
| 99 |
|
| 100 |
#define RE_WILDCARD '\002' |
| 101 |
|
| 102 |
|
| 103 |
#define RE_TEXT_BEGIN '\003' |
| 104 |
#define RE_TEXT_END '\004' |
| 105 |
|
| 106 |
|
| 107 |
#define RE_WORD_BEGIN '\005' |
| 108 |
#define RE_WORD_END '\006' |
| 109 |
|
| 110 |
|
| 111 |
#define RE_WORD_CHAR '\007' |
| 112 |
#define RE_NON_WORD_CHAR '\010' |
| 113 |
|
| 114 |
|
| 115 |
#define RE_WORD_BOUNDARY '\011' |
| 116 |
#define RE_NON_WORD_BOUNDARY '\012' |
| 117 |
|
| 118 |
|
| 119 |
#define RE_RANGE '\013' |
| 120 |
#define RE_RANGE_EXCL '\014' |
| 121 |
|
| 122 |
|
| 123 |
#define RE_GROUP_MATCH_0 '\015' |
| 124 |
#define RE_GROUP_MATCH_9 (RE_GROUP_MATCH_0 + 9) |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
typedef struct |
| 133 |
{ |
| 134 |
|
| 135 |
re_state_id init; |
| 136 |
|
| 137 |
|
| 138 |
re_state_id final; |
| 139 |
} re_machine; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
void re_init(re_context *ctx, errcxdef *errctx) |
| 148 |
{ |
| 149 |
|
| 150 |
ctx->errctx = errctx; |
| 151 |
|
| 152 |
|
| 153 |
ctx->tuple_arr = 0; |
| 154 |
ctx->tuples_alloc = 0; |
| 155 |
|
| 156 |
|
| 157 |
ctx->next_state = RE_STATE_FIRST_VALID; |
| 158 |
|
| 159 |
|
| 160 |
ctx->cur_group = 0; |
| 161 |
|
| 162 |
|
| 163 |
ctx->strbuf = 0; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
static void re_reset(re_context *ctx) |
| 171 |
{ |
| 172 |
int i; |
| 173 |
|
| 174 |
|
| 175 |
for (i = 0 ; i < ctx->next_state ; ++i) |
| 176 |
{ |
| 177 |
if (ctx->tuple_arr[i].char_range != 0) |
| 178 |
{ |
| 179 |
mchfre(ctx->tuple_arr[i].char_range); |
| 180 |
ctx->tuple_arr[i].char_range = 0; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
ctx->next_state = RE_STATE_FIRST_VALID; |
| 186 |
|
| 187 |
|
| 188 |
ctx->cur_group = 0; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
void re_delete(re_context *ctx) |
| 197 |
{ |
| 198 |
|
| 199 |
re_reset(ctx); |
| 200 |
|
| 201 |
|
| 202 |
if (ctx->tuple_arr != 0) |
| 203 |
{ |
| 204 |
mchfre(ctx->tuple_arr); |
| 205 |
ctx->tuple_arr = 0; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
if (ctx->strbuf != 0) |
| 210 |
{ |
| 211 |
mchfre(ctx->strbuf); |
| 212 |
ctx->strbuf = 0; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
static re_state_id re_alloc_state(re_context *ctx) |
| 221 |
{ |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if (ctx->next_state >= ctx->tuples_alloc) |
| 226 |
{ |
| 227 |
uint new_alloc; |
| 228 |
|
| 229 |
|
| 230 |
new_alloc = ctx->tuples_alloc + 100; |
| 231 |
|
| 232 |
|
| 233 |
if (ctx->tuples_alloc == 0) |
| 234 |
{ |
| 235 |
|
| 236 |
ctx->tuple_arr = |
| 237 |
(re_tuple *)mchalo(ctx->errctx, |
| 238 |
(new_alloc * sizeof(re_tuple)), |
| 239 |
"regex"); |
| 240 |
} |
| 241 |
else |
| 242 |
{ |
| 243 |
re_tuple *ptr; |
| 244 |
|
| 245 |
|
| 246 |
ptr = (re_tuple *)mchalo(ctx->errctx, |
| 247 |
(new_alloc * sizeof(re_tuple)), |
| 248 |
"regex"); |
| 249 |
|
| 250 |
|
| 251 |
memcpy(ptr, ctx->tuple_arr, ctx->tuples_alloc * sizeof(re_tuple)); |
| 252 |
|
| 253 |
|
| 254 |
mchfre(ctx->tuple_arr); |
| 255 |
|
| 256 |
|
| 257 |
ctx->tuple_arr = ptr; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
ctx->tuples_alloc = new_alloc; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
ctx->tuple_arr[ctx->next_state].next_state_1 = RE_STATE_INVALID; |
| 266 |
ctx->tuple_arr[ctx->next_state].next_state_2 = RE_STATE_INVALID; |
| 267 |
ctx->tuple_arr[ctx->next_state].ch = RE_EPSILON; |
| 268 |
ctx->tuple_arr[ctx->next_state].flags = 0; |
| 269 |
ctx->tuple_arr[ctx->next_state].char_range = 0; |
| 270 |
|
| 271 |
|
| 272 |
return ctx->next_state++; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
static void re_set_trans(re_context *ctx, |
| 281 |
re_state_id id, re_state_id dest_id, char ch) |
| 282 |
{ |
| 283 |
re_tuple *tuple; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
tuple = &ctx->tuple_arr[id]; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
if (tuple->next_state_1 == RE_STATE_INVALID) |
| 302 |
{ |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if (!(tuple->flags & (RE_STATE_GROUP_BEGIN | RE_STATE_GROUP_END))) |
| 309 |
tuple->ch = ch; |
| 310 |
|
| 311 |
|
| 312 |
tuple->next_state_1 = dest_id; |
| 313 |
} |
| 314 |
else |
| 315 |
{ |
| 316 |
|
| 317 |
tuple->next_state_2 = dest_id; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
static void re_init_machine(re_context *ctx, re_machine *machine) |
| 326 |
{ |
| 327 |
machine->init = re_alloc_state(ctx); |
| 328 |
machine->final = re_alloc_state(ctx); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
static void re_build_char(re_context *ctx, re_machine *machine, char ch) |
| 335 |
{ |
| 336 |
|
| 337 |
re_init_machine(ctx, machine); |
| 338 |
|
| 339 |
|
| 340 |
re_set_trans(ctx, machine->init, machine->final, ch); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
static void re_build_char_range(re_context *ctx, re_machine *machine, |
| 348 |
unsigned char *range, int exclusion) |
| 349 |
{ |
| 350 |
unsigned char *range_copy; |
| 351 |
|
| 352 |
|
| 353 |
re_init_machine(ctx, machine); |
| 354 |
|
| 355 |
|
| 356 |
re_set_trans(ctx, machine->init, machine->final, |
| 357 |
(char)(exclusion ? RE_RANGE_EXCL : RE_RANGE)); |
| 358 |
|
| 359 |
|
| 360 |
range_copy = (unsigned char *)mchalo(ctx->errctx, 32, "regex range"); |
| 361 |
|
| 362 |
|
| 363 |
memcpy(range_copy, range, 32); |
| 364 |
|
| 365 |
|
| 366 |
ctx->tuple_arr[machine->init].char_range = range_copy; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
static void re_build_group_matcher(re_context *ctx, |
| 376 |
re_machine *machine, int group_num) |
| 377 |
{ |
| 378 |
|
| 379 |
re_init_machine(ctx, machine); |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
re_set_trans(ctx, machine->init, machine->final, |
| 387 |
(char)(group_num + RE_GROUP_MATCH_0)); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
static void re_build_concat(re_context *ctx, re_machine *new_machine, |
| 395 |
re_machine *lhs, re_machine *rhs) |
| 396 |
{ |
| 397 |
|
| 398 |
re_init_machine(ctx, new_machine); |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
re_set_trans(ctx, new_machine->init, lhs->init, RE_EPSILON); |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
re_set_trans(ctx, lhs->final, rhs->init, RE_EPSILON); |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
re_set_trans(ctx, rhs->final, new_machine->final, RE_EPSILON); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
static void re_build_group(re_context *ctx, re_machine *new_machine, |
| 425 |
re_machine *sub_machine, int group_id) |
| 426 |
{ |
| 427 |
|
| 428 |
re_init_machine(ctx, new_machine); |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
re_set_trans(ctx, new_machine->init, sub_machine->init, RE_EPSILON); |
| 436 |
re_set_trans(ctx, sub_machine->final, new_machine->final, RE_EPSILON); |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
ctx->tuple_arr[new_machine->init].flags |= RE_STATE_GROUP_BEGIN; |
| 443 |
ctx->tuple_arr[new_machine->final].flags |= RE_STATE_GROUP_END; |
| 444 |
|
| 445 |
|
| 446 |
ctx->tuple_arr[new_machine->init].ch = group_id; |
| 447 |
ctx->tuple_arr[new_machine->final].ch = group_id; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
static void re_build_alter(re_context *ctx, re_machine *new_machine, |
| 454 |
re_machine *lhs, re_machine *rhs) |
| 455 |
{ |
| 456 |
|
| 457 |
re_init_machine(ctx, new_machine); |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
re_set_trans(ctx, new_machine->init, lhs->init, RE_EPSILON); |
| 464 |
re_set_trans(ctx, new_machine->init, rhs->init, RE_EPSILON); |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
re_set_trans(ctx, lhs->final, new_machine->final, RE_EPSILON); |
| 471 |
re_set_trans(ctx, rhs->final, new_machine->final, RE_EPSILON); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
static void re_build_closure(re_context *ctx, |
| 478 |
re_machine *new_machine, re_machine *sub, |
| 479 |
char specifier) |
| 480 |
{ |
| 481 |
|
| 482 |
re_init_machine(ctx, new_machine); |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
re_set_trans(ctx, new_machine->init, sub->init, RE_EPSILON); |
| 490 |
re_set_trans(ctx, sub->final, new_machine->final, RE_EPSILON); |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
if (specifier != '?') |
| 500 |
re_set_trans(ctx, sub->final, sub->init, RE_EPSILON); |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
if (specifier != '+') |
| 510 |
re_set_trans(ctx, new_machine->init, new_machine->final, RE_EPSILON); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
static void re_build_null_machine(re_context *ctx, re_machine *machine) |
| 517 |
{ |
| 518 |
machine->init = machine->final = RE_STATE_INVALID; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
static int re_is_machine_null(re_context *ctx, re_machine *machine) |
| 526 |
{ |
| 527 |
return (machine->init == RE_STATE_INVALID); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
static void re_concat_onto(re_context *ctx, |
| 539 |
re_machine *dest, re_machine *rhs) |
| 540 |
{ |
| 541 |
|
| 542 |
if (re_is_machine_null(ctx, dest)) |
| 543 |
{ |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
*dest = *rhs; |
| 549 |
} |
| 550 |
else |
| 551 |
{ |
| 552 |
re_machine new_machine; |
| 553 |
|
| 554 |
|
| 555 |
re_build_concat(ctx, &new_machine, dest, rhs); |
| 556 |
|
| 557 |
|
| 558 |
*dest = new_machine; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
static void re_alternate_onto(re_context *ctx, |
| 570 |
re_machine *dest, re_machine *rhs) |
| 571 |
{ |
| 572 |
|
| 573 |
if (re_is_machine_null(ctx, dest)) |
| 574 |
{ |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
*dest = *rhs; |
| 580 |
} |
| 581 |
else |
| 582 |
{ |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
if (!re_is_machine_null(ctx, rhs)) |
| 588 |
{ |
| 589 |
re_machine new_machine; |
| 590 |
|
| 591 |
|
| 592 |
re_build_alter(ctx, &new_machine, dest, rhs); |
| 593 |
|
| 594 |
|
| 595 |
*dest = new_machine; |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
#define re_set_bit(set, bit) \ |
| 605 |
(((unsigned char *)(set))[(bit) >> 3] |= (1 << ((bit) & 7))) |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
#define re_is_bit_set(set, bit) \ |
| 611 |
((((unsigned char *)(set))[(bit) >> 3] & (1 << ((bit) & 7))) != 0) |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
static re_status_t re_compile(re_context *ctx, |
| 618 |
const char *expr, size_t exprlen, |
| 619 |
re_machine *result_machine) |
| 620 |
{ |
| 621 |
re_machine cur_machine; |
| 622 |
re_machine alter_machine; |
| 623 |
re_machine new_machine; |
| 624 |
size_t group_stack_level; |
| 625 |
struct |
| 626 |
{ |
| 627 |
re_machine old_cur; |
| 628 |
re_machine old_alter; |
| 629 |
int group_id; |
| 630 |
} group_stack[50]; |
| 631 |
|
| 632 |
|
| 633 |
re_reset(ctx); |
| 634 |
|
| 635 |
|
| 636 |
re_build_null_machine(ctx, &cur_machine); |
| 637 |
re_build_null_machine(ctx, &alter_machine); |
| 638 |
|
| 639 |
|
| 640 |
group_stack_level = 0; |
| 641 |
|
| 642 |
|
| 643 |
for ( ; exprlen != 0 ; ++expr, --exprlen) |
| 644 |
{ |
| 645 |
switch(*expr) |
| 646 |
{ |
| 647 |
case '^': |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
if (!re_is_machine_null(ctx, &cur_machine)) |
| 655 |
goto normal_char; |
| 656 |
|
| 657 |
|
| 658 |
re_build_char(ctx, &new_machine, RE_TEXT_BEGIN); |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
re_concat_onto(ctx, &cur_machine, &new_machine); |
| 665 |
break; |
| 666 |
|
| 667 |
case '$': |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
if (exprlen > 1 |
| 674 |
&& (*(expr+1) != ')' && *(expr+1) != '|')) |
| 675 |
goto normal_char; |
| 676 |
|
| 677 |
|
| 678 |
re_build_char(ctx, &new_machine, RE_TEXT_END); |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
re_concat_onto(ctx, &cur_machine, &new_machine); |
| 685 |
break; |
| 686 |
|
| 687 |
case '(': |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
if (group_stack_level |
| 694 |
> sizeof(group_stack)/sizeof(group_stack[0])) |
| 695 |
{ |
| 696 |
|
| 697 |
return RE_STATUS_GROUP_NESTING_TOO_DEEP; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
group_stack[group_stack_level].old_cur = cur_machine; |
| 702 |
group_stack[group_stack_level].old_alter = alter_machine; |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
group_stack[group_stack_level].group_id = ctx->cur_group; |
| 713 |
|
| 714 |
|
| 715 |
ctx->cur_group++; |
| 716 |
|
| 717 |
|
| 718 |
++group_stack_level; |
| 719 |
|
| 720 |
|
| 721 |
re_build_null_machine(ctx, &cur_machine); |
| 722 |
re_build_null_machine(ctx, &alter_machine); |
| 723 |
break; |
| 724 |
|
| 725 |
case ')': |
| 726 |
|
| 727 |
if (group_stack_level == 0) |
| 728 |
break; |
| 729 |
|
| 730 |
|
| 731 |
--group_stack_level; |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
re_alternate_onto(ctx, &alter_machine, &cur_machine); |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
re_build_group(ctx, &new_machine, &alter_machine, |
| 752 |
group_stack[group_stack_level].group_id); |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
cur_machine = group_stack[group_stack_level].old_cur; |
| 759 |
alter_machine = group_stack[group_stack_level].old_alter; |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
goto apply_postfix; |
| 766 |
|
| 767 |
case '|': |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
re_alternate_onto(ctx, &alter_machine, &cur_machine); |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
re_build_null_machine(ctx, &cur_machine); |
| 783 |
break; |
| 784 |
|
| 785 |
case '%': |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
++expr; |
| 791 |
--exprlen; |
| 792 |
|
| 793 |
|
| 794 |
if (exprlen == 0) |
| 795 |
{ |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
--expr; |
| 802 |
++exprlen; |
| 803 |
break; |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
switch(*expr) |
| 808 |
{ |
| 809 |
case '1': |
| 810 |
case '2': |
| 811 |
case '3': |
| 812 |
case '4': |
| 813 |
case '5': |
| 814 |
case '6': |
| 815 |
case '7': |
| 816 |
case '8': |
| 817 |
case '9': |
| 818 |
|
| 819 |
re_build_group_matcher(ctx, &new_machine, (int)(*expr - '1')); |
| 820 |
|
| 821 |
|
| 822 |
goto apply_postfix; |
| 823 |
|
| 824 |
case '<': |
| 825 |
|
| 826 |
re_build_char(ctx, &new_machine, RE_WORD_BEGIN); |
| 827 |
|
| 828 |
|
| 829 |
re_concat_onto(ctx, &cur_machine, &new_machine); |
| 830 |
break; |
| 831 |
|
| 832 |
case '>': |
| 833 |
|
| 834 |
re_build_char(ctx, &new_machine, RE_WORD_END); |
| 835 |
|
| 836 |
|
| 837 |
re_concat_onto(ctx, &cur_machine, &new_machine); |
| 838 |
break; |
| 839 |
|
| 840 |
case 'w': |
| 841 |
|
| 842 |
re_build_char(ctx, &new_machine, RE_WORD_CHAR); |
| 843 |
goto apply_postfix; |
| 844 |
|
| 845 |
case 'W': |
| 846 |
|
| 847 |
re_build_char(ctx, &new_machine, RE_NON_WORD_CHAR); |
| 848 |
goto apply_postfix; |
| 849 |
|
| 850 |
case 'b': |
| 851 |
|
| 852 |
re_build_char(ctx, &new_machine, RE_WORD_BOUNDARY); |
| 853 |
|
| 854 |
|
| 855 |
re_concat_onto(ctx, &cur_machine, &new_machine); |
| 856 |
break; |
| 857 |
|
| 858 |
case 'B': |
| 859 |
|
| 860 |
re_build_char(ctx, &new_machine, RE_NON_WORD_BOUNDARY); |
| 861 |
|
| 862 |
|
| 863 |
re_concat_onto(ctx, &cur_machine, &new_machine); |
| 864 |
break; |
| 865 |
|
| 866 |
default: |
| 867 |
|
| 868 |
re_build_char(ctx, &new_machine, *expr); |
| 869 |
|
| 870 |
|
| 871 |
goto apply_postfix; |
| 872 |
} |
| 873 |
break; |
| 874 |
|
| 875 |
case '.': |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
re_build_char(ctx, &new_machine, RE_WILDCARD); |
| 882 |
goto apply_postfix; |
| 883 |
break; |
| 884 |
|
| 885 |
case '[': |
| 886 |
|
| 887 |
{ |
| 888 |
int is_exclusive = FALSE; |
| 889 |
unsigned char set[32]; |
| 890 |
|
| 891 |
|
| 892 |
memset(set, 0, sizeof(set)); |
| 893 |
|
| 894 |
|
| 895 |
++expr; |
| 896 |
--exprlen; |
| 897 |
|
| 898 |
|
| 899 |
if (exprlen != 0 && *expr == '^') |
| 900 |
{ |
| 901 |
|
| 902 |
++expr; |
| 903 |
--exprlen; |
| 904 |
|
| 905 |
|
| 906 |
is_exclusive = TRUE; |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
if (exprlen != 0 && *expr == ']') |
| 914 |
{ |
| 915 |
re_set_bit(set, (int)']'); |
| 916 |
++expr; |
| 917 |
--exprlen; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
if (exprlen != 0 && *expr == '-') |
| 925 |
{ |
| 926 |
re_set_bit(set, (int)'-'); |
| 927 |
++expr; |
| 928 |
--exprlen; |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
while (exprlen != 0 && *expr != ']') |
| 933 |
{ |
| 934 |
int ch; |
| 935 |
|
| 936 |
|
| 937 |
ch = (int)(unsigned char)*expr; |
| 938 |
|
| 939 |
|
| 940 |
re_set_bit(set, ch); |
| 941 |
|
| 942 |
|
| 943 |
++expr; |
| 944 |
--exprlen; |
| 945 |
|
| 946 |
|
| 947 |
if (exprlen != 0 && *expr == '-') |
| 948 |
{ |
| 949 |
int ch2; |
| 950 |
|
| 951 |
|
| 952 |
++expr; |
| 953 |
--exprlen; |
| 954 |
if (exprlen != 0) |
| 955 |
{ |
| 956 |
|
| 957 |
ch2 = (int)(unsigned char)*expr; |
| 958 |
|
| 959 |
|
| 960 |
++expr; |
| 961 |
--exprlen; |
| 962 |
|
| 963 |
|
| 964 |
if (ch > ch2) |
| 965 |
{ |
| 966 |
int tmp = ch; |
| 967 |
ch = ch2; |
| 968 |
ch2 = tmp; |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
for ( ; ch <= ch2 ; ++ch) |
| 973 |
re_set_bit(set, ch); |
| 974 |
} |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
re_build_char_range(ctx, &new_machine, set, is_exclusive); |
| 980 |
|
| 981 |
|
| 982 |
goto apply_postfix; |
| 983 |
} |
| 984 |
break; |
| 985 |
|
| 986 |
default: |
| 987 |
normal_char: |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
re_build_char(ctx, &new_machine, *expr); |
| 994 |
|
| 995 |
apply_postfix: |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
if (exprlen > 1) |
| 1003 |
{ |
| 1004 |
switch(*(expr+1)) |
| 1005 |
{ |
| 1006 |
case '*': |
| 1007 |
case '+': |
| 1008 |
case '?': |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
{ |
| 1014 |
re_machine closure_machine; |
| 1015 |
|
| 1016 |
|
| 1017 |
++expr; |
| 1018 |
--exprlen; |
| 1019 |
|
| 1020 |
|
| 1021 |
re_build_closure(ctx, &closure_machine, |
| 1022 |
&new_machine, *expr); |
| 1023 |
|
| 1024 |
|
| 1025 |
new_machine = closure_machine; |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
while (exprlen > 1 && (*(expr+1) == '?' |
| 1032 |
|| *(expr+1) == '+' |
| 1033 |
|| *(expr+1) == '*')) |
| 1034 |
{ |
| 1035 |
++expr; |
| 1036 |
--exprlen; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
break; |
| 1040 |
|
| 1041 |
default: |
| 1042 |
|
| 1043 |
break; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
re_concat_onto(ctx, &cur_machine, &new_machine); |
| 1052 |
break; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
re_alternate_onto(ctx, &alter_machine, &cur_machine); |
| 1058 |
|
| 1059 |
|
| 1060 |
*result_machine = alter_machine; |
| 1061 |
|
| 1062 |
|
| 1063 |
return RE_STATUS_SUCCESS; |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
static void re_note_group(re_context *ctx, re_group_register *regs, |
| 1076 |
re_state_id id, const char *p) |
| 1077 |
{ |
| 1078 |
int group_index; |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
if (id == RE_STATE_INVALID |
| 1085 |
|| !(ctx->tuple_arr[id].flags |
| 1086 |
& (RE_STATE_GROUP_BEGIN | RE_STATE_GROUP_END)) |
| 1087 |
|| (group_index = (int)ctx->tuple_arr[id].ch) >= RE_GROUP_REG_CNT) |
| 1088 |
return; |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
if ((ctx->tuple_arr[id].flags & RE_STATE_GROUP_BEGIN) != 0) |
| 1094 |
regs[group_index].start_ofs = p; |
| 1095 |
else |
| 1096 |
regs[group_index].end_ofs = p; |
| 1097 |
} |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
static int re_is_word_char(char c) |
| 1104 |
{ |
| 1105 |
return (isalpha((unsigned char)c) || isdigit((unsigned char)c)); |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
static int re_match(re_context *ctx, const char *entire_str, |
| 1113 |
const char *str, size_t origlen, |
| 1114 |
const re_machine *machine, re_group_register *regs) |
| 1115 |
{ |
| 1116 |
re_state_id cur_state; |
| 1117 |
const char *p; |
| 1118 |
size_t curlen; |
| 1119 |
|
| 1120 |
|
| 1121 |
cur_state = machine->init; |
| 1122 |
|
| 1123 |
|
| 1124 |
p = str; |
| 1125 |
curlen = origlen; |
| 1126 |
|
| 1127 |
|
| 1128 |
re_note_group(ctx, regs, cur_state, p); |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
if (cur_state == machine->final) |
| 1135 |
{ |
| 1136 |
|
| 1137 |
return 0; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
for (;;) |
| 1142 |
{ |
| 1143 |
re_tuple *tuple; |
| 1144 |
|
| 1145 |
|
| 1146 |
tuple = &ctx->tuple_arr[cur_state]; |
| 1147 |
|
| 1148 |
|
| 1149 |
re_note_group(ctx, regs, cur_state, p); |
| 1150 |
|
| 1151 |
|
| 1152 |
if (!(tuple->flags & (RE_STATE_GROUP_BEGIN | RE_STATE_GROUP_END)) |
| 1153 |
&& tuple->ch != RE_EPSILON) |
| 1154 |
{ |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
switch(tuple->ch) |
| 1161 |
{ |
| 1162 |
case RE_GROUP_MATCH_0: |
| 1163 |
case RE_GROUP_MATCH_0 + 1: |
| 1164 |
case RE_GROUP_MATCH_0 + 2: |
| 1165 |
case RE_GROUP_MATCH_0 + 3: |
| 1166 |
case RE_GROUP_MATCH_0 + 4: |
| 1167 |
case RE_GROUP_MATCH_0 + 5: |
| 1168 |
case RE_GROUP_MATCH_0 + 6: |
| 1169 |
case RE_GROUP_MATCH_0 + 7: |
| 1170 |
case RE_GROUP_MATCH_0 + 8: |
| 1171 |
case RE_GROUP_MATCH_0 + 9: |
| 1172 |
{ |
| 1173 |
int group_num; |
| 1174 |
re_group_register *group_reg; |
| 1175 |
size_t reg_len; |
| 1176 |
|
| 1177 |
|
| 1178 |
group_num = tuple->ch - RE_GROUP_MATCH_0; |
| 1179 |
group_reg = ®s[group_num]; |
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
if (group_reg->start_ofs == 0 || group_reg->end_ofs == 0) |
| 1186 |
return -1; |
| 1187 |
|
| 1188 |
|
| 1189 |
reg_len = group_reg->end_ofs - group_reg->start_ofs; |
| 1190 |
|
| 1191 |
|
| 1192 |
if (curlen < reg_len) |
| 1193 |
return -1; |
| 1194 |
|
| 1195 |
|
| 1196 |
if (memcmp(p, group_reg->start_ofs, reg_len) != 0) |
| 1197 |
return -1; |
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
p += reg_len; |
| 1204 |
curlen -= reg_len; |
| 1205 |
} |
| 1206 |
break; |
| 1207 |
|
| 1208 |
case RE_TEXT_BEGIN: |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
if (p != entire_str) |
| 1215 |
return -1; |
| 1216 |
break; |
| 1217 |
|
| 1218 |
case RE_TEXT_END: |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
if (curlen != 0) |
| 1225 |
return -1; |
| 1226 |
break; |
| 1227 |
|
| 1228 |
case RE_WORD_BEGIN: |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
if (p != entire_str && re_is_word_char(*(p-1))) |
| 1234 |
return -1; |
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
if (curlen == 0 || !re_is_word_char(*p)) |
| 1242 |
return -1; |
| 1243 |
break; |
| 1244 |
|
| 1245 |
case RE_WORD_END: |
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
if (curlen != 0 && re_is_word_char(*p)) |
| 1251 |
return -1; |
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
if (p == entire_str || !re_is_word_char(*(p-1))) |
| 1259 |
return -1; |
| 1260 |
break; |
| 1261 |
|
| 1262 |
case RE_WORD_CHAR: |
| 1263 |
|
| 1264 |
if (curlen == 0 || !re_is_word_char(*p)) |
| 1265 |
return -1; |
| 1266 |
|
| 1267 |
|
| 1268 |
++p; |
| 1269 |
--curlen; |
| 1270 |
break; |
| 1271 |
|
| 1272 |
case RE_NON_WORD_CHAR: |
| 1273 |
|
| 1274 |
if (curlen == 0 || re_is_word_char(*p)) |
| 1275 |
return -1; |
| 1276 |
|
| 1277 |
|
| 1278 |
++p; |
| 1279 |
--curlen; |
| 1280 |
break; |
| 1281 |
|
| 1282 |
case RE_WORD_BOUNDARY: |
| 1283 |
case RE_NON_WORD_BOUNDARY: |
| 1284 |
{ |
| 1285 |
int prev_is_word; |
| 1286 |
int next_is_word; |
| 1287 |
int boundary; |
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
prev_is_word = (p != entire_str |
| 1296 |
&& re_is_word_char(*(p-1))); |
| 1297 |
|
| 1298 |
|
| 1299 |
next_is_word = (curlen != 0 |
| 1300 |
&& re_is_word_char(*p)); |
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
boundary = ((prev_is_word != 0) ^ (next_is_word != 0)); |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
if ((tuple->ch == RE_WORD_BOUNDARY && !boundary) |
| 1313 |
|| (tuple->ch == RE_NON_WORD_BOUNDARY && boundary)) |
| 1314 |
return -1; |
| 1315 |
} |
| 1316 |
break; |
| 1317 |
|
| 1318 |
case RE_WILDCARD: |
| 1319 |
|
| 1320 |
if (curlen == 0) |
| 1321 |
return -1; |
| 1322 |
|
| 1323 |
|
| 1324 |
++p; |
| 1325 |
--curlen; |
| 1326 |
break; |
| 1327 |
|
| 1328 |
case RE_RANGE: |
| 1329 |
case RE_RANGE_EXCL: |
| 1330 |
{ |
| 1331 |
int match; |
| 1332 |
|
| 1333 |
|
| 1334 |
if (curlen == 0) |
| 1335 |
return -1; |
| 1336 |
|
| 1337 |
|
| 1338 |
match = re_is_bit_set(tuple->char_range, |
| 1339 |
(int)(unsigned char)*p); |
| 1340 |
|
| 1341 |
|
| 1342 |
if ((tuple->ch == RE_RANGE && !match) |
| 1343 |
|| (tuple->ch == RE_RANGE_EXCL && match)) |
| 1344 |
return -1; |
| 1345 |
|
| 1346 |
|
| 1347 |
++p; |
| 1348 |
--curlen; |
| 1349 |
} |
| 1350 |
break; |
| 1351 |
|
| 1352 |
default: |
| 1353 |
|
| 1354 |
if (curlen == 0 || tuple->ch != *p) |
| 1355 |
return -1; |
| 1356 |
|
| 1357 |
|
| 1358 |
++p; |
| 1359 |
--curlen; |
| 1360 |
break; |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
cur_state = tuple->next_state_1; |
| 1368 |
} |
| 1369 |
else if (tuple->next_state_2 == RE_STATE_INVALID) |
| 1370 |
{ |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
cur_state = tuple->next_state_1; |
| 1376 |
} |
| 1377 |
else |
| 1378 |
{ |
| 1379 |
re_machine sub_machine; |
| 1380 |
re_group_register regs1[RE_GROUP_REG_CNT]; |
| 1381 |
re_group_register regs2[RE_GROUP_REG_CNT]; |
| 1382 |
int ret1; |
| 1383 |
int ret2; |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
memcpy(regs1, regs, sizeof(regs1)); |
| 1393 |
sub_machine.init = tuple->next_state_1; |
| 1394 |
sub_machine.final = machine->final; |
| 1395 |
ret1 = re_match(ctx, entire_str, p, curlen, &sub_machine, regs1); |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
memcpy(regs2, regs, sizeof(regs2)); |
| 1401 |
sub_machine.init = tuple->next_state_2; |
| 1402 |
sub_machine.final = machine->final; |
| 1403 |
ret2 = re_match(ctx, entire_str, p, curlen, &sub_machine, regs2); |
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
if (ret1 < 0 && ret2 < 0) |
| 1412 |
{ |
| 1413 |
|
| 1414 |
return -1; |
| 1415 |
} |
| 1416 |
else if (ret1 > ret2) |
| 1417 |
{ |
| 1418 |
|
| 1419 |
memcpy(regs, regs1, sizeof(regs1)); |
| 1420 |
return ret1 + (p - str); |
| 1421 |
} |
| 1422 |
else |
| 1423 |
{ |
| 1424 |
|
| 1425 |
memcpy(regs, regs2, sizeof(regs2)); |
| 1426 |
return ret2 + (p - str); |
| 1427 |
} |
| 1428 |
} |
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
if (cur_state == machine->final) |
| 1434 |
{ |
| 1435 |
|
| 1436 |
re_note_group(ctx, regs, cur_state, p); |
| 1437 |
|
| 1438 |
|
| 1439 |
return p - str; |
| 1440 |
} |
| 1441 |
} |
| 1442 |
} |
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
static int re_search(re_context *ctx, const char *str, size_t len, |
| 1452 |
const re_machine *machine, re_group_register *regs, |
| 1453 |
int *result_len) |
| 1454 |
{ |
| 1455 |
int ofs; |
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
for (ofs = 0 ; ofs < (int)len ; ++ofs) |
| 1463 |
{ |
| 1464 |
int matchlen; |
| 1465 |
|
| 1466 |
|
| 1467 |
matchlen = re_match(ctx, str, str + ofs, len - ofs, |
| 1468 |
machine, regs); |
| 1469 |
if (matchlen >= 0) |
| 1470 |
{ |
| 1471 |
|
| 1472 |
*result_len = matchlen; |
| 1473 |
return ofs; |
| 1474 |
} |
| 1475 |
} |
| 1476 |
|
| 1477 |
|
| 1478 |
return -1; |
| 1479 |
} |
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
static void re_save_search_str(re_context *ctx, const char *str, size_t len) |
| 1486 |
{ |
| 1487 |
|
| 1488 |
if (len == 0) |
| 1489 |
{ |
| 1490 |
|
| 1491 |
ctx->curlen = 0; |
| 1492 |
return; |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
if (ctx->strbuf == 0 || ctx->strbufsiz < len) |
| 1497 |
{ |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
if (ctx->strbuf != 0) |
| 1504 |
mchfre(ctx->strbuf); |
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
ctx->strbufsiz = ((len + 255) & ~255); |
| 1512 |
|
| 1513 |
|
| 1514 |
ctx->strbuf = (char *)mchalo(ctx->errctx, ctx->strbufsiz, |
| 1515 |
"regex str"); |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
memcpy(ctx->strbuf, str, len); |
| 1520 |
|
| 1521 |
|
| 1522 |
ctx->curlen = len; |
| 1523 |
} |
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
int re_compile_and_search(re_context *ctx, |
| 1531 |
const char *pattern, size_t patlen, |
| 1532 |
const char *searchstr, size_t searchlen, |
| 1533 |
int *result_len) |
| 1534 |
{ |
| 1535 |
re_machine machine; |
| 1536 |
|
| 1537 |
|
| 1538 |
if (re_compile(ctx, pattern, patlen, &machine) != RE_STATUS_SUCCESS) |
| 1539 |
return -1; |
| 1540 |
|
| 1541 |
|
| 1542 |
re_save_search_str(ctx, searchstr, searchlen); |
| 1543 |
|
| 1544 |
|
| 1545 |
memset(ctx->regs, 0, sizeof(ctx->regs)); |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
return re_search(ctx, ctx->strbuf, ctx->curlen, &machine, |
| 1553 |
ctx->regs, result_len); |
| 1554 |
} |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
int re_compile_and_match(re_context *ctx, |
| 1564 |
const char *pattern, size_t patlen, |
| 1565 |
const char *searchstr, size_t searchlen) |
| 1566 |
{ |
| 1567 |
re_machine machine; |
| 1568 |
|
| 1569 |
|
| 1570 |
if (re_compile(ctx, pattern, patlen, &machine) != RE_STATUS_SUCCESS) |
| 1571 |
return FALSE; |
| 1572 |
|
| 1573 |
|
| 1574 |
re_save_search_str(ctx, searchstr, searchlen); |
| 1575 |
|
| 1576 |
|
| 1577 |
memset(ctx->regs, 0, sizeof(ctx->regs)); |
| 1578 |
|
| 1579 |
|
| 1580 |
return re_match(ctx, ctx->strbuf, ctx->strbuf, ctx->curlen, |
| 1581 |
&machine, ctx->regs); |
| 1582 |
} |
| 1583 |
|