49 #ifndef PLATFORM_UNALIGNED_WORD_ACCESS
55 str_dup(UChar* s, UChar*
end)
57 ptrdiff_t len = end - s;
74 c = *a; *a = *b; *b =
c;
79 size_t len = sn->
end - sn->
s;
81 sn->
end = sn->
s + len;
88 size_t len = sn->
end - sn->
s;
90 sn->
end = sn->
s + len;
109 if (m == 0)
return 0;
122 if (bs[i] != 0)
return 0;
129 onig_is_prelude(
void)
159 buf->
alloc = (
unsigned int )size;
165 #ifdef USE_SUBEXP_CALL
194 size = uslist->
alloc * 2;
216 #ifdef USE_COMBINATION_EXPLOSION_CHECK
218 add_state_check_num(
regex_t* reg,
int num)
319 #define IS_NEED_STR_LEN_OP_EXACT(op) \
320 ((op) == OP_EXACTN || (op) == OP_EXACTMB2N ||\
321 (op) == OP_EXACTMB3N || (op) == OP_EXACTMBN || (op) == OP_EXACTN_IC)
374 if (empty_info != 0) {
385 if (empty_info != 0) {
399 #ifdef USE_SUBEXP_CALL
420 for (i = 0; i < n; i++) {
429 regex_t* reg ARG_UNUSED,
int ignore_case)
440 len += mb_len * (int )str_len;
469 int rlen, r, len, prev_len, slen, ambig;
475 if (sn->
end <= sn->
s)
486 for (; p < sn->
end; ) {
488 if (len == prev_len) {
508 if (sn->
end <= sn->
s)
517 int r, len, prev_len, slen, ambig;
523 if (sn->
end <= sn->
s)
530 prev_len =
enclen(enc, p, end);
535 len =
enclen(enc, p, end);
536 if (len == prev_len) {
556 if (sn->
end <= sn->
s)
565 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
605 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
661 #define REPEAT_RANGE_ALLOC 4
735 #define QUANTIFIER_EXPAND_LIMIT_SIZE 50
736 #define CKN_ON (ckn > 0)
738 #ifdef USE_COMBINATION_EXPLOSION_CHECK
743 int len, mod_tlen, cklen;
749 if (tlen < 0)
return tlen;
757 if (qn->
greedy && infinite) {
770 if (infinite && qn->
lower <= 1) {
788 else if (qn->
upper == 0) {
795 if (qn->
lower == 0) {
797 len = SIZE_OP_STATE_CHECK_PUSH + tlen;
814 len += SIZE_OP_STATE_CHECK;
829 if (tlen < 0)
return tlen;
843 r = add_state_check_num(reg, ckn);
862 r = add_state_check_num(reg, ckn);
873 if (infinite && qn->
lower <= 1) {
875 if (qn->
lower == 1) {
884 r = add_state_check_num(reg, ckn);
899 if (qn->
lower == 0) {
908 r = add_state_check_num(reg, ckn);
911 -(mod_tlen + (
int )SIZE_OP_STATE_CHECK_PUSH_OR_JUMP));
917 else if (qn->
upper == 0) {
927 if (qn->
lower == 0) {
931 r = add_state_check_num(reg, ckn);
947 r = add_state_check_num(reg, ckn);
966 r = add_state_check_num(reg, ckn);
982 if (tlen < 0)
return tlen;
986 if (qn->
greedy && infinite) {
1005 len = tlen * qn->
lower;
1022 else if (!infinite && qn->
greedy &&
1025 len = tlen * qn->
lower;
1047 if (tlen < 0)
return tlen;
1068 if (empty_info != 0)
1137 else if (!infinite && qn->
greedy &&
1145 for (i = 0; i < n; i++) {
1177 if (tlen < 0)
return tlen;
1224 if (tlen < 0)
return tlen;
1229 switch (node->
type) {
1231 #ifdef USE_SUBEXP_CALL
1259 if (tlen < 0)
return tlen;
1261 len = tlen * qn->
lower
1275 if (tlen < 0)
return tlen;
1280 if (tlen < 0)
return tlen;
1307 switch (node->
type) {
1309 #ifdef USE_SUBEXP_CALL
1339 #ifdef USE_SUBEXP_CALL
1372 if (len < 0)
return len;
1403 if (len < 0)
return len;
1407 if (len2 < 0)
return len2;
1441 if (tlen < 0)
return tlen;
1444 switch (node->
type) {
1471 switch (node->
type) {
1490 #ifdef USE_WORD_BEGIN_END
1512 if (len < 0)
return len;
1577 if (r < 0)
return r;
1616 #ifdef USE_BACKREF_WITH_LEVEL
1633 #ifdef USE_SUBEXP_CALL
1662 int n,
type, len, pos, r = 0;
1682 pos = reg->
used + len;
1716 switch (
NCTYPE(node)->ctype) {
1718 if (
NCTYPE(node)->ascii_range != 0) {
1746 #ifdef USE_BACKREF_WITH_LEVEL
1755 goto add_bacref_mems;
1790 #ifdef USE_BACKREF_WITH_LEVEL
1796 for (i = br->
back_num - 1; i >= 0; i--) {
1804 #ifdef USE_SUBEXP_CALL
1824 fprintf(stderr,
"compile_tree: undefined node type %d\n",
NTYPE(node));
1832 #ifdef USE_NAMED_GROUP
1838 Node* node = *plink;
1840 switch (
NTYPE(node)) {
1850 Node** ptarget = &(
NQTFR(node)->target);
1851 Node* old = *ptarget;
1905 int i, pos, n, old_num;
1918 for (i = 0, pos = 0; i < old_num; i++) {
1935 switch (
NTYPE(node)) {
1979 switch (
NTYPE(node)) {
2008 int r,
i, pos, counter;
2014 for (i = 1; i <= env->
num_mem; i++) {
2019 if (r != 0)
return r;
2022 if (r != 0)
return r;
2024 for (i = 1, pos = 1; i <= env->
num_mem; i++) {
2025 if (map[i].new_val > 0) {
2046 #ifdef USE_SUBEXP_CALL
2054 for (i = 0; i < uslist->
num; i++) {
2066 #ifdef USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT
2072 switch (
NTYPE(node)) {
2084 #ifdef USE_SUBEXP_CALL
2097 if (qn->
upper != 0) {
2143 switch (
NTYPE(node)) {
2156 for (i = 1; i < br->
back_num; i++) {
2160 if (*min > tmin) *min = tmin;
2165 #ifdef USE_SUBEXP_CALL
2180 if (r == 0) *min += tmin;
2192 if (y == node) *min = tmin;
2193 else if (*min > tmin) *min = tmin;
2201 *min = sn->
end - sn->
s;
2218 if (qn->
lower > 0) {
2231 #ifdef USE_SUBEXP_CALL
2267 switch (
NTYPE(node)) {
2279 if (r == 0 && *max < tmax) *max = tmax;
2286 *max = sn->
end - sn->
s;
2310 for (i = 0; i < br->
back_num; i++) {
2314 if (*max < tmax) *max = tmax;
2319 #ifdef USE_SUBEXP_CALL
2332 if (qn->
upper != 0) {
2334 if (r == 0 && *max != 0) {
2349 #ifdef USE_SUBEXP_CALL
2378 #define GET_CHAR_LEN_VARLEN -1
2379 #define GET_CHAR_LEN_TOP_ALT_VARLEN -2
2390 switch (
NTYPE(node)) {
2429 while (s < sn->
end) {
2449 #ifdef USE_SUBEXP_CALL
2472 #ifdef USE_SUBEXP_CALL
2541 tmp = x; x = y; y = tmp;
2561 switch (
NCTYPE(y)->ctype) {
2563 if (
NCTYPE(y)->not == 0) {
2567 if (
NCTYPE(y)->ascii_range) {
2582 if (
NCTYPE(y)->ascii_range)
2646 switch (
NCTYPE(y)->ctype) {
2648 if (
NCTYPE(y)->ascii_range) {
2652 return !(
NCTYPE(y)->not);
2658 return !(
NCTYPE(y)->not);
2687 for (i = 0, p = ys->
s, q = xs->
s; (
OnigDistance )i < len; i++, p++, q++) {
2688 if (*p != *q)
return 1;
2712 switch (
NTYPE(node)) {
2716 #ifdef USE_SUBEXP_CALL
2736 if (sn->
end <= sn->
s)
2751 if (qn->
lower > 0) {
2821 if ((en->
type & enclose_mask) == 0)
2830 if ((type & anchor_mask) == 0)
2835 type_mask, enclose_mask, anchor_mask);
2844 #ifdef USE_SUBEXP_CALL
2846 #define RECURSION_EXIST 1
2847 #define RECURSION_INFINITE 2
2870 if (ret != 0)
return ret;
2871 if (min != 0) head = 0;
2892 if (
NQTFR(node)->lower == 0) r = 0;
2993 switch (
NTYPE(node)) {
3047 #define FOUND_CALLED_NODE 1
3061 else if (ret < 0)
return ret;
3068 if (
NQTFR(node)->upper == 0) {
3070 NQTFR(node)->is_refered = 1;
3148 #ifdef USE_NAMED_GROUP
3161 #ifdef USE_NAMED_GROUP
3174 #ifdef USE_NAMED_GROUP
3175 #ifdef USE_PERL_SUBEXP_CALL
3237 int anc_type = an->
type;
3250 NCAR(np) = insert_node;
3293 #ifdef USE_QTFR_PEEK_NEXT
3301 if (qn->
lower <= 1) {
3319 #ifndef ONIG_DONT_OPTIMIZE
3357 UChar *sbuf, *ebuf, *sp;
3363 sbuf_size = (end - sn->
s) * 2;
3366 ebuf = sbuf + sbuf_size;
3372 for (i = 0; i < len; i++) {
3380 sp = sbuf + sbuf_size;
3382 ebuf = sbuf + sbuf_size;
3423 UChar *
p,
int slen, UChar *
end,
3426 int r,
i, j, len, varlen, varclen;
3427 Node *anode, *var_anode, *snode, *xnode, *an;
3434 for (i = 0; i < item_num; i++) {
3435 if (items[i].byte_len != slen) {
3439 if (items[i].code_len != 1) {
3449 if (
IS_NULL(xnode))
goto mem_err;
3450 NCAR(var_anode) = xnode;
3453 if (
IS_NULL(anode))
goto mem_err;
3454 NCAR(xnode) = anode;
3462 if (
IS_NULL(snode))
goto mem_err;
3464 NCAR(anode) = snode;
3466 for (i = 0; i < item_num; i++) {
3468 if (
IS_NULL(snode))
goto mem_err;
3470 for (j = 0; j < items[
i].
code_len; j++) {
3478 if (r != 0)
goto mem_err2;
3486 if (items[i].byte_len != slen) {
3516 NCDR(var_anode) = an;
3526 if (varclen && !varlen)
3542 #define THRESHOLD_CASE_FOLD_ALT_FOR_EXPANSION 8
3544 int r, n, len, alt_num;
3547 Node *top_root, *root, *snode, *prev_node;
3555 if (start >= end)
return 0;
3558 top_root = root = prev_node = snode =
NULL_NODE;
3582 if (
IS_NULL(snode))
goto mem_err;
3592 if (r != 0)
goto err;
3607 if (r < 0)
goto mem_err;
3608 if (r > 0) varlen = 1;
3611 top_root = prev_node;
3620 root =
NCAR(prev_node);
3641 if (r != 0)
goto mem_err;
3664 top_root = (
IS_NOT_NULL(top_root) ? top_root : prev_node);
3689 #ifdef USE_COMBINATION_EXPLOSION_CHECK
3691 #define CEC_THRES_NUM_BIG_REPEAT 512
3692 #define CEC_INFINITE_NUM 0x7fffffff
3694 #define CEC_IN_INFINITE_REPEAT (1<<0)
3695 #define CEC_IN_FINITE_REPEAT (1<<1)
3696 #define CEC_CONT_BIG_REPEAT (1<<2)
3699 setup_comb_exp_check(
Node* node,
int state,
ScanEnv* env)
3710 r = setup_comb_exp_check(
NCAR(node), r, env);
3720 ret = setup_comb_exp_check(
NCAR(node), state, env);
3728 int child_state = state;
3735 if (qn->
upper > 1) {
3737 child_state |= CEC_IN_FINITE_REPEAT;
3750 child_state = state;
3759 if (state & CEC_IN_FINITE_REPEAT) {
3760 qn->comb_exp_check_num = -1;
3764 var_num = CEC_INFINITE_NUM;
3765 child_state |= CEC_IN_INFINITE_REPEAT;
3771 if (var_num >= CEC_THRES_NUM_BIG_REPEAT)
3772 add_state |= CEC_CONT_BIG_REPEAT;
3774 if (((state & CEC_IN_INFINITE_REPEAT) != 0 && var_num != 0) ||
3775 ((state & CEC_CONT_BIG_REPEAT) != 0 &&
3776 var_num >= CEC_THRES_NUM_BIG_REPEAT)) {
3777 if (qn->comb_exp_check_num == 0) {
3778 env->num_comb_exp_check++;
3779 qn->comb_exp_check_num = env->num_comb_exp_check;
3780 if (env->curr_max_regnum > env->comb_exp_max_regnum)
3781 env->comb_exp_max_regnum = env->curr_max_regnum;
3786 r = setup_comb_exp_check(target, child_state, env);
3798 if (env->curr_max_regnum < en->
regnum)
3799 env->curr_max_regnum = en->
regnum;
3801 r = setup_comb_exp_check(en->
target, state, env);
3806 r = setup_comb_exp_check(en->
target, state, env);
3812 #ifdef USE_SUBEXP_CALL
3815 env->has_recursion = 1;
3817 r = setup_comb_exp_check(
NCALL(node)->target, state, env);
3829 #define IN_ALT (1<<0)
3830 #define IN_NOT (1<<1)
3831 #define IN_REPEAT (1<<2)
3832 #define IN_VAR_REPEAT (1<<3)
3833 #define IN_ROOT (1<<4)
3848 int in_root = state &
IN_ROOT;
3857 int prev_in_root = 0;
3865 prev_in_root = state &
IN_ROOT;
3890 #ifdef USE_SUBEXP_CALL
3902 for (i = 0; i < br->
back_num; i++) {
3906 #ifdef USE_BACKREF_WITH_LEVEL
3931 #ifdef USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT
3940 if (r == 0 && d == 0) {
3959 #define EXPAND_STRING_MAX_LENGTH 100
3961 if (qn->
lower > 1) {
4005 #ifdef USE_OP_PUSH_OR_JUMP_EXACT
4062 #ifdef USE_NAMED_GROUP
4089 #define ALLOWED_TYPE_IN_LB \
4090 ( BIT_NT_LIST | BIT_NT_ALT | BIT_NT_STR | BIT_NT_CCLASS | BIT_NT_CTYPE | \
4091 BIT_NT_CANY | BIT_NT_ANCHOR | BIT_NT_ENCLOSE | BIT_NT_QTFR | BIT_NT_CALL )
4093 #define ALLOWED_ENCLOSE_IN_LB ( ENCLOSE_MEMORY )
4094 #define ALLOWED_ENCLOSE_IN_LB_NOT 0
4096 #define ALLOWED_ANCHOR_IN_LB \
4097 ( ANCHOR_LOOK_BEHIND | ANCHOR_LOOK_BEHIND_NOT | ANCHOR_BEGIN_LINE | \
4098 ANCHOR_END_LINE | ANCHOR_BEGIN_BUF | ANCHOR_BEGIN_POSITION | ANCHOR_KEEP | \
4099 ANCHOR_WORD_BOUND | ANCHOR_NOT_WORD_BOUND | \
4100 ANCHOR_WORD_BEGIN | ANCHOR_WORD_END )
4101 #define ALLOWED_ANCHOR_IN_LB_NOT \
4102 ( ANCHOR_LOOK_BEHIND | ANCHOR_LOOK_BEHIND_NOT | ANCHOR_BEGIN_LINE | \
4103 ANCHOR_END_LINE | ANCHOR_BEGIN_BUF | ANCHOR_BEGIN_POSITION | ANCHOR_KEEP | \
4104 ANCHOR_WORD_BOUND | ANCHOR_NOT_WORD_BOUND | \
4105 ANCHOR_WORD_BEGIN | ANCHOR_WORD_END )
4111 if (r < 0)
return r;
4114 if (r != 0)
return r;
4124 if (r < 0)
return r;
4127 if (r != 0)
return r;
4143 #ifndef USE_SUNDAY_QUICK_SEARCH
4147 UChar skip[],
int** int_skip,
int ignore_case)
4150 int clen, flen, n, j, k;
4156 if (len < ONIG_CHAR_TABLE_SIZE) {
4160 for (i = 0; i < len - 1; i += clen) {
4165 clen =
enclen(enc, p, end);
4167 for (j = 0; j < n; j++) {
4168 if ((items[j].code_len != 1) || (items[j].byte_len != clen))
4174 for (j = 0; j < clen; j++) {
4175 skip[s[i + j]] = (
UChar )(len - 1 - i - j);
4176 for (k = 0; k < n; k++) {
4177 skip[buf[k][j]] = (
UChar )(len - 1 - i - j);
4184 *int_skip = (
int* )
xmalloc(
sizeof(
int) * ONIG_CHAR_TABLE_SIZE);
4185 if (
IS_NULL(*int_skip))
return ONIGERR_MEMORY;
4187 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++) (*int_skip)[
i] = (int )len;
4190 for (i = 0; i < len - 1; i += clen) {
4195 clen =
enclen(enc, p, end);
4197 for (j = 0; j < n; j++) {
4198 if ((items[j].code_len != 1) || (items[j].byte_len != clen))
4204 for (j = 0; j < clen; j++) {
4205 (*int_skip)[s[i + j]] = (int )(len - 1 - i - j);
4206 for (k = 0; k < n; k++) {
4207 (*int_skip)[buf[k][j]] = (int )(len - 1 - i - j);
4220 UChar skip[],
int** int_skip,
int ignore_case)
4223 int clen, flen, n, j, k;
4229 if (len < ONIG_CHAR_TABLE_SIZE) {
4230 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++) skip[i] = (
UChar )(len + 1);
4233 for (i = 0; i < len; i += clen) {
4238 clen =
enclen(enc, p, end);
4240 for (j = 0; j < n; j++) {
4241 if ((items[j].code_len != 1) || (items[j].byte_len != clen))
4247 for (j = 0; j < clen; j++) {
4248 skip[s[i + j]] = (
UChar )(len - i - j);
4249 for (k = 0; k < n; k++) {
4250 skip[buf[k][j]] = (
UChar )(len - i - j);
4257 *int_skip = (
int* )
xmalloc(
sizeof(
int) * ONIG_CHAR_TABLE_SIZE);
4260 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++) (*int_skip)[
i] = (int )(len + 1);
4263 for (i = 0; i < len; i += clen) {
4268 clen =
enclen(enc, p, end);
4270 for (j = 0; j < n; j++) {
4271 if ((items[j].code_len != 1) || (items[j].byte_len != clen))
4277 for (j = 0; j < clen; j++) {
4278 (*int_skip)[s[i + j]] = (int )(len - i - j);
4279 for (k = 0; k < n; k++) {
4280 (*int_skip)[buf[k][j]] = (int )(len - i - j);
4289 #define OPT_EXACT_MAXLEN 24
4342 static const short int ByteValTable[] = {
4343 5, 1, 1, 1, 1, 1, 1, 1, 1, 10, 10, 1, 1, 10, 1, 1,
4344 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
4345 12, 4, 7, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
4346 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5,
4347 5, 6, 6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
4348 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 5, 5, 5,
4349 5, 6, 6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
4350 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 1
4353 if (i < (
int )(
sizeof(ByteValTable)/
sizeof(ByteValTable[0]))) {
4357 return (
int )ByteValTable[
i];
4367 static const short int dist_vals[] = {
4368 1000, 500, 333, 250, 200, 167, 143, 125, 111, 100,
4369 91, 83, 77, 71, 67, 63, 59, 56, 53, 50,
4370 48, 45, 43, 42, 40, 38, 37, 36, 34, 33,
4371 32, 31, 30, 29, 29, 28, 27, 26, 26, 25,
4372 24, 24, 23, 23, 22, 22, 21, 21, 20, 20,
4373 20, 19, 19, 19, 18, 18, 18, 17, 17, 17,
4374 16, 16, 16, 16, 15, 15, 15, 15, 14, 14,
4375 14, 14, 14, 14, 13, 13, 13, 13, 13, 13,
4376 12, 12, 12, 12, 12, 12, 11, 11, 11, 11,
4377 11, 11, 11, 11, 11, 10, 10, 10, 10, 10
4385 if (d <
sizeof(dist_vals)/
sizeof(dist_vals[0]))
4387 return (
int )dist_vals[d];
4395 if (v2 <= 0)
return -1;
4396 if (v1 <= 0)
return 1;
4401 if (v2 > v1)
return 1;
4402 if (v2 < v1)
return -1;
4404 if (d2->
min < d1->
min)
return 1;
4405 if (d2->
min > d1->
min)
return -1;
4485 if (left_len == 0) {
4490 if (right_len == 0) {
4576 for (i = to->
len; p < end; ) {
4577 len =
enclen(enc, p, end);
4579 for (j = 0; j < len && p < end; j++)
4599 len =
enclen(enc, p, end);
4601 for (j = 0; j < len && p < end; j++)
4613 if (add->
len == 0 || to->
len == 0) {
4623 for (i = 0; i < to->
len && i < add->
len; ) {
4624 if (to->
s[i] != add->
s[i])
break;
4627 for (j = 1; j < len; j++) {
4628 if (to->
s[i+j] != add->
s[i+j])
break;
4634 if (! add->
reach_end || i < add->len || i < to->len) {
4662 else if (v1 <= 2 && v2 <= 2) {
4667 if (now->
len > 1) v1 += 5;
4668 if (alt->
len > 1) v2 += 5;
4684 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4685 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4686 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4687 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4688 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4689 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4690 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4691 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4692 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4693 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4694 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4695 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4696 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4697 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4698 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4699 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4715 if (map->
map[c] == 0) {
4733 if (n < 0)
return n;
4735 for (i = 0; i < n; i++) {
4746 const int z = 1<<15;
4751 if (now->
value == 0) {
4756 v1 = z / now->
value;
4757 v2 = z / alt->
value;
4765 #define COMP_EM_BASE 20
4768 if (m->
value <= 0)
return -1;
4790 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++) {
4830 int exb_reach, exm_reach;
4858 else if (exm_reach) {
4899 #define MAX_NODE_OPT_INFO_REF_COUNT 5
5024 switch (
NCTYPE(node)->ctype) {
5026 if (
NCTYPE(node)->not != 0) {
5034 for (i = 0; i < maxcode; i++) {
5059 switch (
NANCHOR(node)->type) {
5078 else if (nopt.
exm.
len > 0)
5112 for (i = 1; i < br->
back_num; i++) {
5117 if (min > tmin) min = tmin;
5118 if (max < tmax) max = tmax;
5124 #ifdef USE_SUBEXP_CALL
5158 if (qn->
lower > 0) {
5162 for (i = 2; i <= qn->
lower &&
5166 if (i < qn->lower) {
5207 #ifdef USE_SUBEXP_CALL
5240 if (!onig_is_prelude()) fprintf(stderr,
"optimize_node_left: undefined node type %d\n",
5256 if (e->
len == 0)
return 0;
5267 if (e->
len >= 3 || (e->
len >= 2 && allow_reverse)) {
5271 reg->
optimize = (allow_reverse != 0
5283 if (e->
len >= 3 || (e->
len >= 2 && allow_reverse)) {
5288 reg->
optimize = (allow_reverse != 0
5311 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++)
5331 static void print_optimize_info(
FILE* f,
regex_t* reg);
5384 #if defined(ONIG_DEBUG_COMPILE) || defined(ONIG_DEBUG_MATCH)
5385 if (!onig_is_prelude()) print_optimize_info(stderr, reg);
5409 const UChar *s,
const UChar *end)
5411 fprintf(fp,
"\nPATTERN: /");
5421 fprintf(fp,
" 0x%04x ", (
int )code);
5424 fputc((
int )code, fp);
5427 p +=
enclen(enc, p, end);
5432 fputc((
int )*s, fp);
5437 fprintf(fp,
"/ (%s)\n", enc->
name);
5443 if (a == ONIG_INFINITE_DISTANCE)
5446 fprintf(f,
"(%"PRIuSIZE
")", a);
5450 if (b == ONIG_INFINITE_DISTANCE)
5453 fprintf(f,
"(%"PRIuSIZE
")", b);
5457 print_anchor(
FILE* f,
int anchor)
5464 fprintf(f,
"begin-buf");
5468 if (q) fprintf(f,
", ");
5470 fprintf(f,
"begin-line");
5473 if (q) fprintf(f,
", ");
5475 fprintf(f,
"begin-pos");
5478 if (q) fprintf(f,
", ");
5480 fprintf(f,
"end-buf");
5483 if (q) fprintf(f,
", ");
5485 fprintf(f,
"semi-end-buf");
5488 if (q) fprintf(f,
", ");
5490 fprintf(f,
"end-line");
5493 if (q) fprintf(f,
", ");
5495 fprintf(f,
"anychar-star");
5498 if (q) fprintf(f,
", ");
5499 fprintf(f,
"anychar-star-ml");
5508 static const char* on[] = {
"NONE",
"EXACT",
"EXACT_BM",
"EXACT_BM_NOT_REV",
5510 "EXACT_BM_IC",
"EXACT_BM_NOT_REV_IC" };
5512 fprintf(f,
"optimize: %s\n", on[reg->
optimize]);
5513 fprintf(f,
" anchor: "); print_anchor(f, reg->
anchor);
5519 fprintf(f,
" sub anchor: "); print_anchor(f, reg->
sub_anchor);
5526 fprintf(f,
"exact: [");
5527 for (p = reg->
exact; p < reg->exact_end; p++) {
5535 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++)
5536 if (reg->
map[i]) n++;
5538 fprintf(f,
"map: n=%d\n", n);
5542 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++) {
5543 if (reg->
map[i] != 0) {
5544 if (c > 0) fputs(
", ", f);
5550 fprintf(f,
"%d", i);
5571 #ifdef USE_NAMED_GROUP
5604 size_t size =
sizeof(*regs);
5610 #define REGEX_TRANSFER(to,from) do {\
5611 (to)->state = ONIG_STATE_MODIFY;\
5612 onig_free_body(to);\
5613 xmemcpy(to, from, sizeof(regex_t));\
5625 #define REGEX_CHAIN_HEAD(reg) do {\
5626 while (IS_NOT_NULL((reg)->chain)) {\
5627 (reg) = (reg)->chain;\
5659 static void print_compiled_byte_code_list
P_((
FILE* f,
regex_t* reg));
5661 #ifdef ONIG_DEBUG_PARSE_TREE
5662 static void print_tree
P_((
FILE* f,
Node* node));
5667 OnigErrorInfo* einfo,
const char *sourcefile,
int sourceline)
5669 #define COMPILE_INIT_SIZE 20
5675 #ifdef USE_SUBEXP_CALL
5686 if (!onig_is_prelude()) print_enc_string(stderr, reg->
enc, pattern, pattern_end);
5689 if (reg->
alloc == 0) {
5690 init_size = (pattern_end - pattern) * 2;
5693 if (r != 0)
goto end;
5703 #ifdef USE_COMBINATION_EXPLOSION_CHECK
5708 if (r != 0)
goto err;
5710 #ifdef ONIG_DEBUG_PARSE_TREE
5712 fprintf(stderr,
"ORIGINAL PARSE TREE:\n");
5713 if (!onig_is_prelude()) {
5714 print_tree(stderr, root);
5719 #ifdef USE_NAMED_GROUP
5729 if (r != 0)
goto err;
5733 #ifdef USE_SUBEXP_CALL
5736 if (r != 0)
goto err;
5739 if (r != 0)
goto err_unset;
5741 if (r < 0)
goto err_unset;
5743 if (r != 0)
goto err_unset;
5752 if (r != 0)
goto err_unset;
5754 #ifdef ONIG_DEBUG_PARSE_TREE
5755 if (!onig_is_prelude()) print_tree(stderr, root);
5768 #ifdef USE_COMBINATION_EXPLOSION_CHECK
5774 setup_comb_exp_check(root, 0, &scan_env);
5775 #ifdef USE_SUBEXP_CALL
5776 if (scan_env.has_recursion != 0) {
5777 scan_env.num_comb_exp_check = 0;
5781 if (scan_env.comb_exp_max_regnum > 0) {
5783 for (i = 1; i <= scan_env.comb_exp_max_regnum; i++) {
5785 scan_env.num_comb_exp_check = 0;
5796 #ifndef ONIG_DONT_OPTIMIZE
5798 if (r != 0)
goto err_unset;
5809 #ifdef USE_SUBEXP_CALL
5826 #ifdef USE_SUBEXP_CALL
5833 #ifdef ONIG_DEBUG_COMPILE
5834 #ifdef USE_NAMED_GROUP
5835 if (!onig_is_prelude()) onig_print_names(stderr, reg);
5837 if (!onig_is_prelude()) print_compiled_byte_code_list(stderr, reg);
5845 #ifdef USE_SUBEXP_CALL
5853 einfo->
enc = scan_env.
enc;
5865 #ifdef USE_RECOMPILE_API
5867 onig_recompile(
regex_t* reg,
const UChar* pattern,
const UChar* pattern_end,
5874 r =
onig_new(&new_reg, pattern, pattern_end, option, enc, syntax, einfo);
5918 (reg)->syntax = syntax;
5919 (reg)->optimize = 0;
5921 (reg)->int_map = (
int* )
NULL;
5922 (reg)->int_map_backward = (
int* )
NULL;
5928 (reg)->name_table = (
void* )
NULL;
5930 (reg)->case_fold_flag = case_fold_flag;
5974 if (onig_inited != 0)
5985 #ifdef ONIG_DEBUG_STATISTICS
5986 onig_statistics_init();
5999 #ifdef ONIG_DEBUG_STATISTICS
6000 if (!onig_is_prelude()) onig_print_statistics(stderr);
6003 #ifdef USE_SHARED_CCLASS_TABLE
6007 #ifdef USE_PARSE_TREE_NODE_RECYCLE
6028 for (low = 0, high = n; low < high; ) {
6029 x = (low + high) >> 1;
6030 if (code > data[x * 2 + 1])
6036 return ((low < n && code >= data[low * 2]) ? 1 : 0);
6044 if (elen > 1 || (code >= SINGLE_BYTE_SIZE)) {
6080 #define ARG_SPECIAL -1
6082 #define ARG_RELADDR 1
6083 #define ARG_ABSADDR 2
6084 #define ARG_LENGTH 3
6085 #define ARG_MEMNUM 4
6086 #define ARG_OPTION 5
6087 #define ARG_STATE_CHECK 6
6089 OnigOpInfoType OnigOpInfo[] = {
6091 {
OP_END,
"end", ARG_NON },
6155 {
OP_JUMP,
"jump", ARG_RELADDR },
6156 {
OP_PUSH,
"push", ARG_RELADDR },
6157 {
OP_POP,
"pop", ARG_NON },
6179 {
OP_CALL,
"call", ARG_ABSADDR },
6187 "state-check-anychar-ml*", ARG_STATE_CHECK },
6196 for (i = 0; OnigOpInfo[
i].opcode >= 0; i++) {
6197 if (opcode == OnigOpInfo[i].opcode)
6198 return OnigOpInfo[
i].name;
6204 op2arg_type(
int opcode)
6208 for (i = 0; OnigOpInfo[
i].opcode >= 0; i++) {
6209 if (opcode == OnigOpInfo[i].opcode)
6210 return OnigOpInfo[
i].arg_type;
6216 Indent(
FILE* f,
int indent)
6219 for (i = 0; i < indent; i++)
putc(
' ', f);
6223 p_string(
FILE* f,
int len, UChar* s)
6226 while (len-- > 0) { fputc(*s++, f); }
6232 int x = len * mb_len;
6234 fprintf(f,
":%d:", len);
6235 while (x-- > 0) { fputc(*s++, f); }
6250 fprintf(f,
"[%s", op2name(*bp));
6251 arg_type = op2arg_type(*bp);
6252 if (arg_type != ARG_SPECIAL) {
6259 fprintf(f,
":(%d)", addr);
6263 fprintf(f,
":(%d)", addr);
6267 fprintf(f,
":%d", len);
6272 fprintf(f,
":%d", mem);
6278 fprintf(f,
":%d", option);
6282 case ARG_STATE_CHECK:
6285 fprintf(f,
":%d", scn);
6294 p_string(f, 1, bp++);
break;
6296 p_string(f, 2, bp); bp += 2;
break;
6298 p_string(f, 3, bp); bp += 3;
break;
6300 p_string(f, 4, bp); bp += 4;
break;
6302 p_string(f, 5, bp); bp += 5;
break;
6305 p_len_string(f, len, 1, bp);
6310 p_string(f, 2, bp); bp += 2;
break;
6312 p_string(f, 4, bp); bp += 4;
break;
6314 p_string(f, 6, bp); bp += 6;
break;
6317 p_len_string(f, len, 2, bp);
6322 p_len_string(f, len, 3, bp);
6331 fprintf(f,
":%d:%d:", mb_len, len);
6333 while (n-- > 0) { fputc(*bp++, f); }
6338 len =
enclen(enc, bp, bpend);
6339 p_string(f, len, bp);
6344 p_len_string(f, len, 1, bp);
6351 fprintf(f,
":%d", n);
6357 fprintf(f,
":%d", n);
6364 #ifndef PLATFORM_UNALIGNED_WORD_ACCESS
6369 fprintf(f,
":%d:%d", (
int )code, len);
6378 #ifndef PLATFORM_UNALIGNED_WORD_ACCESS
6383 fprintf(f,
":%d:%d:%d", n, (
int )code, len);
6391 n = bitset_on_num(cc->
bs);
6392 fprintf(f,
":%"PRIuPTR
":%d", (
uintptr_t)cc, n);
6399 fprintf(f,
":%d", mem);
6406 for (i = 0; i < len; i++) {
6408 if (i > 0) fputs(
", ", f);
6409 fprintf(f,
"%d", mem);
6419 fprintf(f,
":%d", option);
6421 fprintf(f,
":%d", level);
6425 for (i = 0; i < len; i++) {
6427 if (i > 0) fputs(
", ", f);
6428 fprintf(f,
"%d", mem);
6440 fprintf(f,
":%d:%d", mem, addr);
6448 fprintf(f,
":(%d)", addr);
6455 fprintf(f,
":%d", len);
6461 fprintf(f,
":%d:(%d)", len, addr);
6470 fprintf(f,
":%d:(%d)", scn, addr);
6476 fprintf(f,
":%d:(%d)", mem, addr);
6480 fprintf(stderr,
"onig_print_compiled_byte_code: undefined code %d\n",
6485 if (nextp) *nextp =
bp;
6489 print_compiled_byte_code_list(
FILE* f,
regex_t* reg)
6495 fprintf(f,
"code length: %d", reg->
used);
6501 fprintf(f,
"\n%ld:", bp - reg->
p);
6503 fprintf(f,
" %ld:", bp - reg->
p);
6511 print_indent_tree(
FILE* f,
Node* node,
int indent)
6513 int i,
type, container_p = 0;
6519 fprintf(f,
"ERROR: null node!!!\n");
6528 fprintf(f,
"<list:%"PRIxPTR
">\n", (
intptr_t)node);
6530 fprintf(f,
"<alt:%"PRIxPTR
">\n", (
intptr_t)node);
6532 print_indent_tree(f,
NCAR(node), indent + add);
6534 if (
NTYPE(node) != type) {
6535 fprintf(f,
"ERROR: list/alt right is not a cons. %d\n",
NTYPE(node));
6538 print_indent_tree(f,
NCAR(node), indent + add);
6543 fprintf(f,
"<string%s:%"PRIxPTR
">",
6545 for (p =
NSTR(node)->s; p <
NSTR(node)->end; p++) {
6546 if (*p >= 0x20 && *p < 0x7f)
6549 fprintf(f,
" 0x%02x", *p);
6555 fprintf(f,
"<cclass:%"PRIxPTR
">", (
intptr_t)node);
6559 for (i = 0; i < (int )bbuf->
used; i++) {
6560 if (i > 0) fprintf(f,
",");
6561 fprintf(f,
"%0x", bbuf->
p[i]);
6567 fprintf(f,
"<ctype:%"PRIxPTR
"> ", (
intptr_t)node);
6568 switch (
NCTYPE(node)->ctype) {
6570 if (
NCTYPE(node)->not != 0)
6571 fputs(
"not word", f);
6577 fprintf(f,
"ERROR: undefined ctype.\n");
6583 fprintf(f,
"<anychar:%"PRIxPTR
">", (
intptr_t)node);
6587 fprintf(f,
"<anchor:%"PRIxPTR
"> ", (
intptr_t)node);
6588 switch (
NANCHOR(node)->type) {
6589 case ANCHOR_BEGIN_BUF: fputs(
"begin buf", f);
break;
6590 case ANCHOR_END_BUF: fputs(
"end buf", f);
break;
6591 case ANCHOR_BEGIN_LINE: fputs(
"begin line", f);
break;
6592 case ANCHOR_END_LINE: fputs(
"end line", f);
break;
6593 case ANCHOR_SEMI_END_BUF: fputs(
"semi end buf", f);
break;
6594 case ANCHOR_BEGIN_POSITION: fputs(
"begin position", f);
break;
6595 case ANCHOR_ANYCHAR_STAR: fputs(
"begin position/line", f);
break;
6599 #ifdef USE_WORD_BEGIN_END
6610 fprintf(f,
"ERROR: undefined anchor type.\n");
6620 fprintf(f,
"<backref:%"PRIxPTR
">", (
intptr_t)node);
6621 for (i = 0; i < br->
back_num; i++) {
6622 if (i > 0) fputs(
", ", f);
6623 fprintf(f,
"%d", p[i]);
6628 #ifdef USE_SUBEXP_CALL
6632 fprintf(f,
"<call:%"PRIxPTR
">", (
intptr_t)node);
6639 fprintf(f,
"<quantifier:%"PRIxPTR
">{%d,%d}%s\n", (
intptr_t)node,
6641 (
NQTFR(node)->greedy ?
"" :
"?"));
6642 print_indent_tree(f,
NQTFR(node)->target, indent + add);
6646 fprintf(f,
"<enclose:%"PRIxPTR
"> ", (
intptr_t)node);
6649 fprintf(f,
"option:%d\n",
NENCLOSE(node)->option);
6652 fprintf(f,
"memory:%d",
NENCLOSE(node)->regnum);
6655 fprintf(f,
"stop-bt");
6658 fprintf(f,
"condition:%d",
NENCLOSE(node)->regnum);
6665 print_indent_tree(f,
NENCLOSE(node)->target, indent + add);
6669 fprintf(f,
"print_indent_tree: undefined node type %d\n",
NTYPE(node));
6677 if (container_p) print_indent_tree(f,
NANCHOR(node)->target, indent + add);
6683 #ifdef ONIG_DEBUG_PARSE_TREE
6687 print_indent_tree(f, node, 0);