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)) {
1984 switch (
NTYPE(node)) {
2013 int r, i, pos, counter;
2019 for (i = 1; i <= env->
num_mem; i++) {
2024 if (r != 0)
return r;
2027 if (r != 0)
return r;
2029 for (i = 1, pos = 1; i <= env->
num_mem; i++) {
2030 if (map[i].new_val > 0) {
2051 #ifdef USE_SUBEXP_CALL
2059 for (i = 0; i < uslist->
num; i++) {
2071 #ifdef USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT
2077 switch (
NTYPE(node)) {
2089 #ifdef USE_SUBEXP_CALL
2102 if (qn->
upper != 0) {
2148 switch (
NTYPE(node)) {
2161 for (i = 1; i < br->
back_num; i++) {
2165 if (*min > tmin) *min = tmin;
2170 #ifdef USE_SUBEXP_CALL
2185 if (r == 0) *min += tmin;
2197 if (y == node) *min = tmin;
2198 else if (*min > tmin) *min = tmin;
2206 *min = sn->
end - sn->
s;
2223 if (qn->
lower > 0) {
2236 #ifdef USE_SUBEXP_CALL
2272 switch (
NTYPE(node)) {
2284 if (r == 0 && *max < tmax) *max = tmax;
2291 *max = sn->
end - sn->
s;
2315 for (i = 0; i < br->
back_num; i++) {
2319 if (*max < tmax) *max = tmax;
2324 #ifdef USE_SUBEXP_CALL
2337 if (qn->
upper != 0) {
2339 if (r == 0 && *max != 0) {
2354 #ifdef USE_SUBEXP_CALL
2383 #define GET_CHAR_LEN_VARLEN -1
2384 #define GET_CHAR_LEN_TOP_ALT_VARLEN -2
2395 switch (
NTYPE(node)) {
2434 while (s < sn->
end) {
2454 #ifdef USE_SUBEXP_CALL
2477 #ifdef USE_SUBEXP_CALL
2546 tmp = x; x = y; y = tmp;
2566 switch (
NCTYPE(y)->ctype) {
2568 if (
NCTYPE(y)->not == 0) {
2572 if (
NCTYPE(y)->ascii_range) {
2587 if (
NCTYPE(y)->ascii_range)
2651 switch (
NCTYPE(y)->ctype) {
2653 if (
NCTYPE(y)->ascii_range) {
2657 return !(
NCTYPE(y)->not);
2663 return !(
NCTYPE(y)->not);
2692 for (i = 0, p = ys->
s, q = xs->
s; (
OnigDistance )i < len; i++, p++, q++) {
2693 if (*p != *q)
return 1;
2717 switch (
NTYPE(node)) {
2721 #ifdef USE_SUBEXP_CALL
2741 if (sn->
end <= sn->
s)
2756 if (qn->
lower > 0) {
2826 if ((en->
type & enclose_mask) == 0)
2835 if ((type & anchor_mask) == 0)
2840 type_mask, enclose_mask, anchor_mask);
2849 #ifdef USE_SUBEXP_CALL
2851 #define RECURSION_EXIST 1
2852 #define RECURSION_INFINITE 2
2875 if (ret != 0)
return ret;
2876 if (min != 0) head = 0;
2897 if (
NQTFR(node)->lower == 0) r = 0;
2998 switch (
NTYPE(node)) {
3052 #define FOUND_CALLED_NODE 1
3066 else if (ret < 0)
return ret;
3073 if (
NQTFR(node)->upper == 0) {
3075 NQTFR(node)->is_refered = 1;
3153 #ifdef USE_NAMED_GROUP
3166 #ifdef USE_NAMED_GROUP
3179 #ifdef USE_NAMED_GROUP
3180 #ifdef USE_PERL_SUBEXP_CALL
3242 int anc_type = an->
type;
3255 NCAR(np) = insert_node;
3298 #ifdef USE_QTFR_PEEK_NEXT
3306 if (qn->
lower <= 1) {
3324 #ifndef ONIG_DONT_OPTIMIZE
3362 UChar *sbuf, *ebuf, *sp;
3368 sbuf_size = (end - sn->
s) * 2;
3371 ebuf = sbuf + sbuf_size;
3377 for (i = 0; i < len; i++) {
3385 sp = sbuf + sbuf_size;
3387 ebuf = sbuf + sbuf_size;
3428 UChar *p,
int slen, UChar *
end,
3431 int r, i, j, len, varlen, varclen;
3432 Node *anode, *var_anode, *snode, *xnode, *an;
3439 for (i = 0; i < item_num; i++) {
3440 if (items[i].byte_len != slen) {
3444 if (items[i].code_len != 1) {
3454 if (
IS_NULL(xnode))
goto mem_err;
3455 NCAR(var_anode) = xnode;
3458 if (
IS_NULL(anode))
goto mem_err;
3459 NCAR(xnode) = anode;
3467 if (
IS_NULL(snode))
goto mem_err;
3469 NCAR(anode) = snode;
3471 for (i = 0; i < item_num; i++) {
3473 if (
IS_NULL(snode))
goto mem_err;
3475 for (j = 0; j < items[i].
code_len; j++) {
3483 if (r != 0)
goto mem_err2;
3491 if (items[i].byte_len != slen) {
3521 NCDR(var_anode) = an;
3531 if (varclen && !varlen)
3547 #define THRESHOLD_CASE_FOLD_ALT_FOR_EXPANSION 8
3549 int r, n, len, alt_num;
3552 Node *top_root, *root, *snode, *prev_node;
3560 if (start >= end)
return 0;
3563 top_root = root = prev_node = snode =
NULL_NODE;
3587 if (
IS_NULL(snode))
goto mem_err;
3597 if (r != 0)
goto err;
3612 if (r < 0)
goto mem_err;
3613 if (r > 0) varlen = 1;
3616 top_root = prev_node;
3625 root =
NCAR(prev_node);
3646 if (r != 0)
goto mem_err;
3669 top_root = (
IS_NOT_NULL(top_root) ? top_root : prev_node);
3694 #ifdef USE_COMBINATION_EXPLOSION_CHECK
3696 #define CEC_THRES_NUM_BIG_REPEAT 512
3697 #define CEC_INFINITE_NUM 0x7fffffff
3699 #define CEC_IN_INFINITE_REPEAT (1<<0)
3700 #define CEC_IN_FINITE_REPEAT (1<<1)
3701 #define CEC_CONT_BIG_REPEAT (1<<2)
3704 setup_comb_exp_check(
Node* node,
int state,
ScanEnv* env)
3715 r = setup_comb_exp_check(
NCAR(node), r, env);
3725 ret = setup_comb_exp_check(
NCAR(node), state, env);
3733 int child_state = state;
3740 if (qn->
upper > 1) {
3742 child_state |= CEC_IN_FINITE_REPEAT;
3755 child_state = state;
3764 if (state & CEC_IN_FINITE_REPEAT) {
3765 qn->comb_exp_check_num = -1;
3769 var_num = CEC_INFINITE_NUM;
3770 child_state |= CEC_IN_INFINITE_REPEAT;
3776 if (var_num >= CEC_THRES_NUM_BIG_REPEAT)
3777 add_state |= CEC_CONT_BIG_REPEAT;
3779 if (((state & CEC_IN_INFINITE_REPEAT) != 0 && var_num != 0) ||
3780 ((state & CEC_CONT_BIG_REPEAT) != 0 &&
3781 var_num >= CEC_THRES_NUM_BIG_REPEAT)) {
3782 if (qn->comb_exp_check_num == 0) {
3783 env->num_comb_exp_check++;
3784 qn->comb_exp_check_num = env->num_comb_exp_check;
3785 if (env->curr_max_regnum > env->comb_exp_max_regnum)
3786 env->comb_exp_max_regnum = env->curr_max_regnum;
3791 r = setup_comb_exp_check(target, child_state, env);
3803 if (env->curr_max_regnum < en->
regnum)
3804 env->curr_max_regnum = en->
regnum;
3806 r = setup_comb_exp_check(en->
target, state, env);
3811 r = setup_comb_exp_check(en->
target, state, env);
3817 #ifdef USE_SUBEXP_CALL
3820 env->has_recursion = 1;
3822 r = setup_comb_exp_check(
NCALL(node)->target, state, env);
3834 #define IN_ALT (1<<0)
3835 #define IN_NOT (1<<1)
3836 #define IN_REPEAT (1<<2)
3837 #define IN_VAR_REPEAT (1<<3)
3838 #define IN_ROOT (1<<4)
3853 int in_root = state &
IN_ROOT;
3862 int prev_in_root = 0;
3870 prev_in_root = state &
IN_ROOT;
3895 #ifdef USE_SUBEXP_CALL
3907 for (i = 0; i < br->
back_num; i++) {
3911 #ifdef USE_BACKREF_WITH_LEVEL
3936 #ifdef USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT
3945 if (r == 0 && d == 0) {
3964 #define EXPAND_STRING_MAX_LENGTH 100
3966 if (qn->
lower > 1) {
3967 int i, n = qn->
lower;
4010 #ifdef USE_OP_PUSH_OR_JUMP_EXACT
4067 #ifdef USE_NAMED_GROUP
4094 #define ALLOWED_TYPE_IN_LB \
4095 ( BIT_NT_LIST | BIT_NT_ALT | BIT_NT_STR | BIT_NT_CCLASS | BIT_NT_CTYPE | \
4096 BIT_NT_CANY | BIT_NT_ANCHOR | BIT_NT_ENCLOSE | BIT_NT_QTFR | BIT_NT_CALL )
4098 #define ALLOWED_ENCLOSE_IN_LB ( ENCLOSE_MEMORY | ENCLOSE_OPTION )
4099 #define ALLOWED_ENCLOSE_IN_LB_NOT ENCLOSE_OPTION
4101 #define ALLOWED_ANCHOR_IN_LB \
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 )
4106 #define ALLOWED_ANCHOR_IN_LB_NOT \
4107 ( ANCHOR_LOOK_BEHIND | ANCHOR_LOOK_BEHIND_NOT | ANCHOR_BEGIN_LINE | \
4108 ANCHOR_END_LINE | ANCHOR_BEGIN_BUF | ANCHOR_BEGIN_POSITION | ANCHOR_KEEP | \
4109 ANCHOR_WORD_BOUND | ANCHOR_NOT_WORD_BOUND | \
4110 ANCHOR_WORD_BEGIN | ANCHOR_WORD_END )
4116 if (r < 0)
return r;
4119 if (r != 0)
return r;
4129 if (r < 0)
return r;
4132 if (r != 0)
return r;
4148 #ifndef USE_SUNDAY_QUICK_SEARCH
4152 UChar skip[],
int** int_skip,
int ignore_case)
4155 int clen, flen, n, j, k;
4161 if (len < ONIG_CHAR_TABLE_SIZE) {
4165 for (i = 0; i < len - 1; i += clen) {
4170 clen =
enclen(enc, p, end);
4172 for (j = 0; j < n; j++) {
4173 if ((items[j].code_len != 1) || (items[j].byte_len != clen))
4179 for (j = 0; j < clen; j++) {
4180 skip[s[i + j]] = (
UChar )(len - 1 - i - j);
4181 for (k = 0; k < n; k++) {
4182 skip[buf[k][j]] = (
UChar )(len - 1 - i - j);
4189 *int_skip = (
int* )
xmalloc(
sizeof(
int) * ONIG_CHAR_TABLE_SIZE);
4190 if (
IS_NULL(*int_skip))
return ONIGERR_MEMORY;
4192 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++) (*int_skip)[i] = (int )len;
4195 for (i = 0; i < len - 1; i += clen) {
4200 clen =
enclen(enc, p, end);
4202 for (j = 0; j < n; j++) {
4203 if ((items[j].code_len != 1) || (items[j].byte_len != clen))
4209 for (j = 0; j < clen; j++) {
4210 (*int_skip)[s[i + j]] = (int )(len - 1 - i - j);
4211 for (k = 0; k < n; k++) {
4212 (*int_skip)[buf[k][j]] = (int )(len - 1 - i - j);
4225 UChar skip[],
int** int_skip,
int ignore_case)
4228 int clen, flen, n, j, k;
4234 if (len < ONIG_CHAR_TABLE_SIZE) {
4235 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++) skip[i] = (
UChar )(len + 1);
4238 for (i = 0; i < len; i += clen) {
4243 clen =
enclen(enc, p, end);
4245 for (j = 0; j < n; j++) {
4246 if ((items[j].code_len != 1) || (items[j].byte_len != clen))
4252 for (j = 0; j < clen; j++) {
4253 skip[s[i + j]] = (
UChar )(len - i - j);
4254 for (k = 0; k < n; k++) {
4255 skip[buf[k][j]] = (
UChar )(len - i - j);
4262 *int_skip = (
int* )
xmalloc(
sizeof(
int) * ONIG_CHAR_TABLE_SIZE);
4265 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++) (*int_skip)[i] = (int )(len + 1);
4268 for (i = 0; i < len; i += clen) {
4273 clen =
enclen(enc, p, end);
4275 for (j = 0; j < n; j++) {
4276 if ((items[j].code_len != 1) || (items[j].byte_len != clen))
4282 for (j = 0; j < clen; j++) {
4283 (*int_skip)[s[i + j]] = (int )(len - i - j);
4284 for (k = 0; k < n; k++) {
4285 (*int_skip)[buf[k][j]] = (int )(len - i - j);
4294 #define OPT_EXACT_MAXLEN 24
4347 static const short int ByteValTable[] = {
4348 5, 1, 1, 1, 1, 1, 1, 1, 1, 10, 10, 1, 1, 10, 1, 1,
4349 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
4350 12, 4, 7, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
4351 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5,
4352 5, 6, 6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
4353 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 5, 5, 5,
4354 5, 6, 6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
4355 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 1
4358 if (i < (
int )(
sizeof(ByteValTable)/
sizeof(ByteValTable[0]))) {
4362 return (
int )ByteValTable[i];
4372 static const short int dist_vals[] = {
4373 1000, 500, 333, 250, 200, 167, 143, 125, 111, 100,
4374 91, 83, 77, 71, 67, 63, 59, 56, 53, 50,
4375 48, 45, 43, 42, 40, 38, 37, 36, 34, 33,
4376 32, 31, 30, 29, 29, 28, 27, 26, 26, 25,
4377 24, 24, 23, 23, 22, 22, 21, 21, 20, 20,
4378 20, 19, 19, 19, 18, 18, 18, 17, 17, 17,
4379 16, 16, 16, 16, 15, 15, 15, 15, 14, 14,
4380 14, 14, 14, 14, 13, 13, 13, 13, 13, 13,
4381 12, 12, 12, 12, 12, 12, 11, 11, 11, 11,
4382 11, 11, 11, 11, 11, 10, 10, 10, 10, 10
4390 if (d <
sizeof(dist_vals)/
sizeof(dist_vals[0]))
4392 return (
int )dist_vals[d];
4400 if (v2 <= 0)
return -1;
4401 if (v1 <= 0)
return 1;
4406 if (v2 > v1)
return 1;
4407 if (v2 < v1)
return -1;
4409 if (d2->
min < d1->
min)
return 1;
4410 if (d2->
min > d1->
min)
return -1;
4490 if (left_len == 0) {
4495 if (right_len == 0) {
4581 for (i = to->
len; p < end; ) {
4582 len =
enclen(enc, p, end);
4584 for (j = 0; j < len && p < end; j++)
4604 len =
enclen(enc, p, end);
4606 for (j = 0; j < len && p < end; j++)
4618 if (add->
len == 0 || to->
len == 0) {
4628 for (i = 0; i < to->
len && i < add->
len; ) {
4629 if (to->
s[i] != add->
s[i])
break;
4632 for (j = 1; j < len; j++) {
4633 if (to->
s[i+j] != add->
s[i+j])
break;
4639 if (! add->
reach_end || i < add->len || i < to->len) {
4667 else if (v1 <= 2 && v2 <= 2) {
4672 if (now->
len > 1) v1 += 5;
4673 if (alt->
len > 1) v2 += 5;
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,
4700 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4701 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4702 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4703 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4704 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4720 if (map->
map[c] == 0) {
4738 if (n < 0)
return n;
4740 for (i = 0; i < n; i++) {
4751 const int z = 1<<15;
4756 if (now->
value == 0) {
4761 v1 = z / now->
value;
4762 v2 = z / alt->
value;
4770 #define COMP_EM_BASE 20
4773 if (m->
value <= 0)
return -1;
4795 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++) {
4835 int exb_reach, exm_reach;
4863 else if (exm_reach) {
4904 #define MAX_NODE_OPT_INFO_REF_COUNT 5
5029 switch (
NCTYPE(node)->ctype) {
5031 if (
NCTYPE(node)->not != 0) {
5039 for (i = 0; i < maxcode; i++) {
5064 switch (
NANCHOR(node)->type) {
5083 else if (nopt.
exm.
len > 0)
5117 for (i = 1; i < br->
back_num; i++) {
5122 if (min > tmin) min = tmin;
5123 if (max < tmax) max = tmax;
5129 #ifdef USE_SUBEXP_CALL
5163 if (qn->
lower > 0) {
5167 for (i = 2; i <= qn->
lower &&
5171 if (i < qn->lower) {
5212 #ifdef USE_SUBEXP_CALL
5245 if (!onig_is_prelude()) fprintf(stderr,
"optimize_node_left: undefined node type %d\n",
5261 if (e->
len == 0)
return 0;
5272 if (e->
len >= 3 || (e->
len >= 2 && allow_reverse)) {
5276 reg->
optimize = (allow_reverse != 0
5288 if (e->
len >= 3 || (e->
len >= 2 && allow_reverse)) {
5293 reg->
optimize = (allow_reverse != 0
5316 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++)
5336 static void print_optimize_info(
FILE* f,
regex_t* reg);
5389 #if defined(ONIG_DEBUG_COMPILE) || defined(ONIG_DEBUG_MATCH)
5390 if (!onig_is_prelude()) print_optimize_info(stderr, reg);
5414 const UChar *s,
const UChar *end)
5416 fprintf(fp,
"\nPATTERN: /");
5426 fprintf(fp,
" 0x%04x ", (
int )code);
5429 fputc((
int )code, fp);
5432 p +=
enclen(enc, p, end);
5437 fputc((
int )*s, fp);
5442 fprintf(fp,
"/ (%s)\n", enc->
name);
5448 if (a == ONIG_INFINITE_DISTANCE)
5451 fprintf(f,
"(%"PRIuSIZE
")", a);
5455 if (b == ONIG_INFINITE_DISTANCE)
5458 fprintf(f,
"(%"PRIuSIZE
")", b);
5462 print_anchor(
FILE* f,
int anchor)
5469 fprintf(f,
"begin-buf");
5473 if (q) fprintf(f,
", ");
5475 fprintf(f,
"begin-line");
5478 if (q) fprintf(f,
", ");
5480 fprintf(f,
"begin-pos");
5483 if (q) fprintf(f,
", ");
5485 fprintf(f,
"end-buf");
5488 if (q) fprintf(f,
", ");
5490 fprintf(f,
"semi-end-buf");
5493 if (q) fprintf(f,
", ");
5495 fprintf(f,
"end-line");
5498 if (q) fprintf(f,
", ");
5500 fprintf(f,
"anychar-star");
5503 if (q) fprintf(f,
", ");
5504 fprintf(f,
"anychar-star-ml");
5513 static const char* on[] = {
"NONE",
"EXACT",
"EXACT_BM",
"EXACT_BM_NOT_REV",
5515 "EXACT_BM_IC",
"EXACT_BM_NOT_REV_IC" };
5517 fprintf(f,
"optimize: %s\n", on[reg->
optimize]);
5518 fprintf(f,
" anchor: "); print_anchor(f, reg->
anchor);
5524 fprintf(f,
" sub anchor: "); print_anchor(f, reg->
sub_anchor);
5531 fprintf(f,
"exact: [");
5532 for (p = reg->
exact; p < reg->exact_end; p++) {
5540 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++)
5541 if (reg->
map[i]) n++;
5543 fprintf(f,
"map: n=%d\n", n);
5547 for (i = 0; i < ONIG_CHAR_TABLE_SIZE; i++) {
5548 if (reg->
map[i] != 0) {
5549 if (c > 0) fputs(
", ", f);
5555 fprintf(f,
"%d", i);
5576 #ifdef USE_NAMED_GROUP
5609 size_t size =
sizeof(*regs);
5615 #define REGEX_TRANSFER(to,from) do {\
5616 (to)->state = ONIG_STATE_MODIFY;\
5617 onig_free_body(to);\
5618 xmemcpy(to, from, sizeof(regex_t));\
5630 #define REGEX_CHAIN_HEAD(reg) do {\
5631 while (IS_NOT_NULL((reg)->chain)) {\
5632 (reg) = (reg)->chain;\
5664 static void print_compiled_byte_code_list
P_((
FILE* f,
regex_t* reg));
5666 #ifdef ONIG_DEBUG_PARSE_TREE
5667 static void print_tree
P_((
FILE* f,
Node* node));
5672 OnigErrorInfo* einfo,
const char *sourcefile,
int sourceline)
5674 #define COMPILE_INIT_SIZE 20
5680 #ifdef USE_SUBEXP_CALL
5691 if (!onig_is_prelude()) print_enc_string(stderr, reg->
enc, pattern, pattern_end);
5694 if (reg->
alloc == 0) {
5695 init_size = (pattern_end - pattern) * 2;
5698 if (r != 0)
goto end;
5708 #ifdef USE_COMBINATION_EXPLOSION_CHECK
5713 if (r != 0)
goto err;
5715 #ifdef ONIG_DEBUG_PARSE_TREE
5717 fprintf(stderr,
"ORIGINAL PARSE TREE:\n");
5718 if (!onig_is_prelude()) {
5719 print_tree(stderr, root);
5724 #ifdef USE_NAMED_GROUP
5734 if (r != 0)
goto err;
5738 #ifdef USE_SUBEXP_CALL
5741 if (r != 0)
goto err;
5744 if (r != 0)
goto err_unset;
5746 if (r < 0)
goto err_unset;
5748 if (r != 0)
goto err_unset;
5757 if (r != 0)
goto err_unset;
5759 #ifdef ONIG_DEBUG_PARSE_TREE
5760 if (!onig_is_prelude()) print_tree(stderr, root);
5773 #ifdef USE_COMBINATION_EXPLOSION_CHECK
5779 setup_comb_exp_check(root, 0, &scan_env);
5780 #ifdef USE_SUBEXP_CALL
5781 if (scan_env.has_recursion != 0) {
5782 scan_env.num_comb_exp_check = 0;
5786 if (scan_env.comb_exp_max_regnum > 0) {
5788 for (i = 1; i <= scan_env.comb_exp_max_regnum; i++) {
5790 scan_env.num_comb_exp_check = 0;
5801 #ifndef ONIG_DONT_OPTIMIZE
5803 if (r != 0)
goto err_unset;
5814 #ifdef USE_SUBEXP_CALL
5831 #ifdef USE_SUBEXP_CALL
5838 #ifdef ONIG_DEBUG_COMPILE
5839 #ifdef USE_NAMED_GROUP
5840 if (!onig_is_prelude()) onig_print_names(stderr, reg);
5842 if (!onig_is_prelude()) print_compiled_byte_code_list(stderr, reg);
5850 #ifdef USE_SUBEXP_CALL
5858 einfo->
enc = scan_env.
enc;
5870 #ifdef USE_RECOMPILE_API
5872 onig_recompile(
regex_t* reg,
const UChar* pattern,
const UChar* pattern_end,
5879 r =
onig_new(&new_reg, pattern, pattern_end, option, enc, syntax, einfo);
5923 (reg)->syntax = syntax;
5924 (reg)->optimize = 0;
5926 (reg)->int_map = (
int* )
NULL;
5927 (reg)->int_map_backward = (
int* )
NULL;
5933 (reg)->name_table = (
void* )
NULL;
5935 (reg)->case_fold_flag = case_fold_flag;
5979 if (onig_inited != 0)
5990 #ifdef ONIG_DEBUG_STATISTICS
5991 onig_statistics_init();
6004 #ifdef ONIG_DEBUG_STATISTICS
6005 if (!onig_is_prelude()) onig_print_statistics(stderr);
6008 #ifdef USE_SHARED_CCLASS_TABLE
6012 #ifdef USE_PARSE_TREE_NODE_RECYCLE
6033 for (low = 0, high = n; low < high; ) {
6034 x = (low + high) >> 1;
6035 if (code > data[x * 2 + 1])
6041 return ((low < n && code >= data[low * 2]) ? 1 : 0);
6049 if (elen > 1 || (code >= SINGLE_BYTE_SIZE)) {
6085 #define ARG_SPECIAL -1
6087 #define ARG_RELADDR 1
6088 #define ARG_ABSADDR 2
6089 #define ARG_LENGTH 3
6090 #define ARG_MEMNUM 4
6091 #define ARG_OPTION 5
6092 #define ARG_STATE_CHECK 6
6094 OnigOpInfoType OnigOpInfo[] = {
6096 {
OP_END,
"end", ARG_NON },
6160 {
OP_JUMP,
"jump", ARG_RELADDR },
6161 {
OP_PUSH,
"push", ARG_RELADDR },
6162 {
OP_POP,
"pop", ARG_NON },
6184 {
OP_CALL,
"call", ARG_ABSADDR },
6192 "state-check-anychar-ml*", ARG_STATE_CHECK },
6201 for (i = 0; OnigOpInfo[i].opcode >= 0; i++) {
6202 if (opcode == OnigOpInfo[i].opcode)
6203 return OnigOpInfo[i].name;
6209 op2arg_type(
int opcode)
6213 for (i = 0; OnigOpInfo[i].opcode >= 0; i++) {
6214 if (opcode == OnigOpInfo[i].opcode)
6215 return OnigOpInfo[i].arg_type;
6221 Indent(
FILE* f,
int indent)
6224 for (i = 0; i < indent; i++)
putc(
' ', f);
6228 p_string(
FILE* f,
int len, UChar* s)
6231 while (len-- > 0) { fputc(*s++, f); }
6237 int x = len * mb_len;
6239 fprintf(f,
":%d:", len);
6240 while (x-- > 0) { fputc(*s++, f); }
6255 fprintf(f,
"[%s", op2name(*bp));
6256 arg_type = op2arg_type(*bp);
6257 if (arg_type != ARG_SPECIAL) {
6264 fprintf(f,
":(%d)", addr);
6268 fprintf(f,
":(%d)", addr);
6272 fprintf(f,
":%d", len);
6277 fprintf(f,
":%d", mem);
6283 fprintf(f,
":%d", option);
6287 case ARG_STATE_CHECK:
6290 fprintf(f,
":%d", scn);
6299 p_string(f, 1, bp++);
break;
6301 p_string(f, 2, bp); bp += 2;
break;
6303 p_string(f, 3, bp); bp += 3;
break;
6305 p_string(f, 4, bp); bp += 4;
break;
6307 p_string(f, 5, bp); bp += 5;
break;
6310 p_len_string(f, len, 1, bp);
6315 p_string(f, 2, bp); bp += 2;
break;
6317 p_string(f, 4, bp); bp += 4;
break;
6319 p_string(f, 6, bp); bp += 6;
break;
6322 p_len_string(f, len, 2, bp);
6327 p_len_string(f, len, 3, bp);
6336 fprintf(f,
":%d:%d:", mb_len, len);
6338 while (n-- > 0) { fputc(*bp++, f); }
6343 len =
enclen(enc, bp, bpend);
6344 p_string(f, len, bp);
6349 p_len_string(f, len, 1, bp);
6356 fprintf(f,
":%d", n);
6362 fprintf(f,
":%d", n);
6369 #ifndef PLATFORM_UNALIGNED_WORD_ACCESS
6374 fprintf(f,
":%d:%d", (
int )code, len);
6383 #ifndef PLATFORM_UNALIGNED_WORD_ACCESS
6388 fprintf(f,
":%d:%d:%d", n, (
int )code, len);
6396 n = bitset_on_num(cc->
bs);
6397 fprintf(f,
":%"PRIuPTR
":%d", (
uintptr_t)cc, n);
6404 fprintf(f,
":%d", mem);
6411 for (i = 0; i < len; i++) {
6413 if (i > 0) fputs(
", ", f);
6414 fprintf(f,
"%d", mem);
6424 fprintf(f,
":%d", option);
6426 fprintf(f,
":%d", level);
6430 for (i = 0; i < len; i++) {
6432 if (i > 0) fputs(
", ", f);
6433 fprintf(f,
"%d", mem);
6445 fprintf(f,
":%d:%d", mem, addr);
6453 fprintf(f,
":(%d)", addr);
6460 fprintf(f,
":%d", len);
6466 fprintf(f,
":%d:(%d)", len, addr);
6475 fprintf(f,
":%d:(%d)", scn, addr);
6481 fprintf(f,
":%d:(%d)", mem, addr);
6485 fprintf(stderr,
"onig_print_compiled_byte_code: undefined code %d\n",
6490 if (nextp) *nextp =
bp;
6494 print_compiled_byte_code_list(
FILE* f,
regex_t* reg)
6500 fprintf(f,
"code length: %d", reg->
used);
6506 fprintf(f,
"\n%ld:", bp - reg->
p);
6508 fprintf(f,
" %ld:", bp - reg->
p);
6516 print_indent_tree(
FILE* f,
Node* node,
int indent)
6518 int i,
type, container_p = 0;
6524 fprintf(f,
"ERROR: null node!!!\n");
6533 fprintf(f,
"<list:%"PRIxPTR
">\n", (
intptr_t)node);
6535 fprintf(f,
"<alt:%"PRIxPTR
">\n", (
intptr_t)node);
6537 print_indent_tree(f,
NCAR(node), indent + add);
6539 if (
NTYPE(node) != type) {
6540 fprintf(f,
"ERROR: list/alt right is not a cons. %d\n",
NTYPE(node));
6543 print_indent_tree(f,
NCAR(node), indent + add);
6548 fprintf(f,
"<string%s:%"PRIxPTR
">",
6550 for (p =
NSTR(node)->s; p <
NSTR(node)->end; p++) {
6551 if (*p >= 0x20 && *p < 0x7f)
6554 fprintf(f,
" 0x%02x", *p);
6560 fprintf(f,
"<cclass:%"PRIxPTR
">", (
intptr_t)node);
6564 for (i = 0; i < (int )bbuf->
used; i++) {
6565 if (i > 0) fprintf(f,
",");
6566 fprintf(f,
"%0x", bbuf->
p[i]);
6572 fprintf(f,
"<ctype:%"PRIxPTR
"> ", (
intptr_t)node);
6573 switch (
NCTYPE(node)->ctype) {
6575 if (
NCTYPE(node)->not != 0)
6576 fputs(
"not word", f);
6582 fprintf(f,
"ERROR: undefined ctype.\n");
6588 fprintf(f,
"<anychar:%"PRIxPTR
">", (
intptr_t)node);
6592 fprintf(f,
"<anchor:%"PRIxPTR
"> ", (
intptr_t)node);
6593 switch (
NANCHOR(node)->type) {
6594 case ANCHOR_BEGIN_BUF: fputs(
"begin buf", f);
break;
6595 case ANCHOR_END_BUF: fputs(
"end buf", f);
break;
6596 case ANCHOR_BEGIN_LINE: fputs(
"begin line", f);
break;
6597 case ANCHOR_END_LINE: fputs(
"end line", f);
break;
6598 case ANCHOR_SEMI_END_BUF: fputs(
"semi end buf", f);
break;
6599 case ANCHOR_BEGIN_POSITION: fputs(
"begin position", f);
break;
6600 case ANCHOR_ANYCHAR_STAR: fputs(
"begin position/line", f);
break;
6604 #ifdef USE_WORD_BEGIN_END
6615 fprintf(f,
"ERROR: undefined anchor type.\n");
6625 fprintf(f,
"<backref:%"PRIxPTR
">", (
intptr_t)node);
6626 for (i = 0; i < br->
back_num; i++) {
6627 if (i > 0) fputs(
", ", f);
6628 fprintf(f,
"%d", p[i]);
6633 #ifdef USE_SUBEXP_CALL
6637 fprintf(f,
"<call:%"PRIxPTR
">", (
intptr_t)node);
6644 fprintf(f,
"<quantifier:%"PRIxPTR
">{%d,%d}%s\n", (
intptr_t)node,
6646 (
NQTFR(node)->greedy ?
"" :
"?"));
6647 print_indent_tree(f,
NQTFR(node)->target, indent + add);
6651 fprintf(f,
"<enclose:%"PRIxPTR
"> ", (
intptr_t)node);
6654 fprintf(f,
"option:%d",
NENCLOSE(node)->option);
6657 fprintf(f,
"memory:%d",
NENCLOSE(node)->regnum);
6660 fprintf(f,
"stop-bt");
6663 fprintf(f,
"condition:%d",
NENCLOSE(node)->regnum);
6670 print_indent_tree(f,
NENCLOSE(node)->target, indent + add);
6674 fprintf(f,
"print_indent_tree: undefined node type %d\n",
NTYPE(node));
6682 if (container_p) print_indent_tree(f,
NANCHOR(node)->target, indent + add);
6688 #ifdef ONIG_DEBUG_PARSE_TREE
6692 print_indent_tree(f, node, 0);