1 /**********************************************************************
2 regexec.c - Oniguruma (regular expression library)
3 **********************************************************************/
5 * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* #define USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE */
34 #ifdef USE_CRNL_AS_LINE_TERMINATOR
35 #define ONIGENC_IS_MBC_CRNL(enc,p,end) \
36 (ONIGENC_MBC_TO_CODE(enc,p,end) == 13 && \
37 ONIGENC_IS_MBC_NEWLINE(enc,(p+enclen(enc,p)),end))
40 #ifdef USE_CAPTURE_HISTORY
41 static void history_tree_free(OnigCaptureTreeNode
* node
);
44 history_tree_clear(OnigCaptureTreeNode
* node
)
48 if (IS_NOT_NULL(node
)) {
49 for (i
= 0; i
< node
->num_childs
; i
++) {
50 if (IS_NOT_NULL(node
->childs
[i
])) {
51 history_tree_free(node
->childs
[i
]);
54 for (i
= 0; i
< node
->allocated
; i
++) {
55 node
->childs
[i
] = (OnigCaptureTreeNode
* )0;
58 node
->beg
= ONIG_REGION_NOTPOS
;
59 node
->end
= ONIG_REGION_NOTPOS
;
65 history_tree_free(OnigCaptureTreeNode
* node
)
67 history_tree_clear(node
);
72 history_root_free(OnigRegion
* r
)
74 if (IS_NOT_NULL(r
->history_root
)) {
75 history_tree_free(r
->history_root
);
76 r
->history_root
= (OnigCaptureTreeNode
* )0;
80 static OnigCaptureTreeNode
*
81 history_node_new(void)
83 OnigCaptureTreeNode
* node
;
85 node
= (OnigCaptureTreeNode
* )xmalloc(sizeof(OnigCaptureTreeNode
));
86 CHECK_NULL_RETURN(node
);
87 node
->childs
= (OnigCaptureTreeNode
** )0;
91 node
->beg
= ONIG_REGION_NOTPOS
;
92 node
->end
= ONIG_REGION_NOTPOS
;
98 history_tree_add_child(OnigCaptureTreeNode
* parent
, OnigCaptureTreeNode
* child
)
100 #define HISTORY_TREE_INIT_ALLOC_SIZE 8
102 if (parent
->num_childs
>= parent
->allocated
) {
105 if (IS_NULL(parent
->childs
)) {
106 n
= HISTORY_TREE_INIT_ALLOC_SIZE
;
108 (OnigCaptureTreeNode
** )xmalloc(sizeof(OnigCaptureTreeNode
*) * n
);
111 n
= parent
->allocated
* 2;
113 (OnigCaptureTreeNode
** )xrealloc(parent
->childs
,
114 sizeof(OnigCaptureTreeNode
*) * n
);
116 CHECK_NULL_RETURN_MEMERR(parent
->childs
);
117 for (i
= parent
->allocated
; i
< n
; i
++) {
118 parent
->childs
[i
] = (OnigCaptureTreeNode
* )0;
120 parent
->allocated
= n
;
123 parent
->childs
[parent
->num_childs
] = child
;
124 parent
->num_childs
++;
128 static OnigCaptureTreeNode
*
129 history_tree_clone(OnigCaptureTreeNode
* node
)
132 OnigCaptureTreeNode
*clone
, *child
;
134 clone
= history_node_new();
135 CHECK_NULL_RETURN(clone
);
137 clone
->beg
= node
->beg
;
138 clone
->end
= node
->end
;
139 for (i
= 0; i
< node
->num_childs
; i
++) {
140 child
= history_tree_clone(node
->childs
[i
]);
141 if (IS_NULL(child
)) {
142 history_tree_free(clone
);
143 return (OnigCaptureTreeNode
* )0;
145 history_tree_add_child(clone
, child
);
151 extern OnigCaptureTreeNode
*
152 onig_get_capture_tree(OnigRegion
* region
)
154 return region
->history_root
;
156 #endif /* USE_CAPTURE_HISTORY */
159 onig_region_clear(OnigRegion
* region
)
163 for (i
= 0; i
< region
->num_regs
; i
++) {
164 region
->beg
[i
] = region
->end
[i
] = ONIG_REGION_NOTPOS
;
166 #ifdef USE_CAPTURE_HISTORY
167 history_root_free(region
);
172 onig_region_resize(OnigRegion
* region
, int n
)
174 region
->num_regs
= n
;
176 if (n
< ONIG_NREGION
)
179 if (region
->allocated
== 0) {
180 region
->beg
= (int* )xmalloc(n
* sizeof(int));
181 if (region
->beg
== 0)
182 return ONIGERR_MEMORY
;
184 region
->end
= (int* )xmalloc(n
* sizeof(int));
185 if (region
->end
== 0) {
187 return ONIGERR_MEMORY
;
190 region
->allocated
= n
;
192 else if (region
->allocated
< n
) {
195 region
->allocated
= 0;
196 tmp
= (int* )xrealloc(region
->beg
, n
* sizeof(int));
200 return ONIGERR_MEMORY
;
203 tmp
= (int* )xrealloc(region
->end
, n
* sizeof(int));
206 return ONIGERR_MEMORY
;
210 if (region
->beg
== 0 || region
->end
== 0)
211 return ONIGERR_MEMORY
;
213 region
->allocated
= n
;
220 onig_region_resize_clear(OnigRegion
* region
, int n
)
224 r
= onig_region_resize(region
, n
);
225 if (r
!= 0) return r
;
226 onig_region_clear(region
);
231 onig_region_set(OnigRegion
* region
, int at
, int beg
, int end
)
233 if (at
< 0) return ONIGERR_INVALID_ARGUMENT
;
235 if (at
>= region
->allocated
) {
236 int r
= onig_region_resize(region
, at
+ 1);
240 region
->beg
[at
] = beg
;
241 region
->end
[at
] = end
;
246 onig_region_init(OnigRegion
* region
)
248 region
->num_regs
= 0;
249 region
->allocated
= 0;
250 region
->beg
= (int* )0;
251 region
->end
= (int* )0;
252 region
->history_root
= (OnigCaptureTreeNode
* )0;
256 onig_region_new(void)
260 r
= (OnigRegion
* )xmalloc(sizeof(OnigRegion
));
267 onig_region_free(OnigRegion
* r
, int free_self
)
270 if (r
->allocated
> 0) {
271 if (r
->beg
) xfree(r
->beg
);
272 if (r
->end
) xfree(r
->end
);
275 #ifdef USE_CAPTURE_HISTORY
276 history_root_free(r
);
278 if (free_self
) xfree(r
);
283 onig_region_copy(OnigRegion
* to
, OnigRegion
* from
)
285 #define RREGC_SIZE (sizeof(int) * from->num_regs)
288 if (to
== from
) return;
290 onig_region_resize(to
, from
->num_regs
);
291 for (i
= 0; i
< from
->num_regs
; i
++) {
292 to
->beg
[i
] = from
->beg
[i
];
293 to
->end
[i
] = from
->end
[i
];
295 to
->num_regs
= from
->num_regs
;
297 #ifdef USE_CAPTURE_HISTORY
298 history_root_free(to
);
300 if (IS_NOT_NULL(from
->history_root
)) {
301 to
->history_root
= history_tree_clone(from
->history_root
);
308 #define INVALID_STACK_INDEX -1
311 /* used by normal-POP */
312 #define STK_ALT 0x0001
313 #define STK_LOOK_BEHIND_NOT 0x0002
314 #define STK_POS_NOT 0x0003
315 /* handled by normal-POP */
316 #define STK_MEM_START 0x0100
317 #define STK_MEM_END 0x8200
318 #define STK_REPEAT_INC 0x0300
319 #define STK_STATE_CHECK_MARK 0x1000
320 /* avoided by normal-POP */
321 #define STK_NULL_CHECK_START 0x3000
322 #define STK_NULL_CHECK_END 0x5000 /* for recursive call */
323 #define STK_MEM_END_MARK 0x8400
324 #define STK_POS 0x0500 /* used when POP-POS */
325 #define STK_STOP_BT 0x0600 /* mark for "(?>...)" */
326 #define STK_REPEAT 0x0700
327 #define STK_CALL_FRAME 0x0800
328 #define STK_RETURN 0x0900
329 #define STK_VOID 0x0a00 /* for fill a blank */
331 /* stack type check mask */
332 #define STK_MASK_POP_USED 0x00ff
333 #define STK_MASK_TO_VOID_TARGET 0x10ff
334 #define STK_MASK_MEM_END_OR_MARK 0x8000 /* MEM_END or MEM_END_MARK */
336 #ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
337 #define MATCH_ARG_INIT(msa, arg_option, arg_region, arg_start) do {\
338 (msa).stack_p = (void* )0;\
339 (msa).options = (arg_option);\
340 (msa).region = (arg_region);\
341 (msa).start = (arg_start);\
342 (msa).best_len = ONIG_MISMATCH;\
345 #define MATCH_ARG_INIT(msa, arg_option, arg_region, arg_start) do {\
346 (msa).stack_p = (void* )0;\
347 (msa).options = (arg_option);\
348 (msa).region = (arg_region);\
349 (msa).start = (arg_start);\
353 #ifdef USE_COMBINATION_EXPLOSION_CHECK
355 #define STATE_CHECK_BUFF_MALLOC_THRESHOLD_SIZE 16
357 #define STATE_CHECK_BUFF_INIT(msa, str_len, offset, state_num) do { \
358 if ((state_num) > 0 && str_len >= STATE_CHECK_STRING_THRESHOLD_LEN) {\
359 unsigned int size = (unsigned int )(((str_len) + 1) * (state_num) + 7) >> 3;\
360 offset = ((offset) * (state_num)) >> 3;\
361 if (size > 0 && offset < size && size < STATE_CHECK_BUFF_MAX_SIZE) {\
362 if (size >= STATE_CHECK_BUFF_MALLOC_THRESHOLD_SIZE) {\
363 (msa).state_check_buff = (void* )xmalloc(size);\
364 CHECK_NULL_RETURN_MEMERR((msa).state_check_buff);\
367 (msa).state_check_buff = (void* )xalloca(size);\
368 xmemset(((char* )((msa).state_check_buff)+(offset)), 0, \
369 (size_t )(size - (offset))); \
370 (msa).state_check_buff_size = size;\
373 (msa).state_check_buff = (void* )0;\
374 (msa).state_check_buff_size = 0;\
378 (msa).state_check_buff = (void* )0;\
379 (msa).state_check_buff_size = 0;\
383 #define MATCH_ARG_FREE(msa) do {\
384 if ((msa).stack_p) xfree((msa).stack_p);\
385 if ((msa).state_check_buff_size >= STATE_CHECK_BUFF_MALLOC_THRESHOLD_SIZE) { \
386 if ((msa).state_check_buff) xfree((msa).state_check_buff);\
390 #define MATCH_ARG_FREE(msa) if ((msa).stack_p) xfree((msa).stack_p)
395 #define STACK_INIT(alloc_addr, ptr_num, stack_num) do {\
397 alloc_addr = (char* )xalloca(sizeof(char*) * (ptr_num));\
398 stk_alloc = (OnigStackType* )(msa->stack_p);\
399 stk_base = stk_alloc;\
401 stk_end = stk_base + msa->stack_n;\
404 alloc_addr = (char* )xalloca(sizeof(char*) * (ptr_num)\
405 + sizeof(OnigStackType) * (stack_num));\
406 stk_alloc = (OnigStackType* )(alloc_addr + sizeof(char*) * (ptr_num));\
407 stk_base = stk_alloc;\
409 stk_end = stk_base + (stack_num);\
413 #define STACK_SAVE do{\
414 if (stk_base != stk_alloc) {\
415 msa->stack_p = stk_base;\
416 msa->stack_n = stk_end - stk_base;\
420 static unsigned int MatchStackLimitSize
= DEFAULT_MATCH_STACK_LIMIT_SIZE
;
423 onig_get_match_stack_limit_size(void)
425 return MatchStackLimitSize
;
429 onig_set_match_stack_limit_size(unsigned int size
)
431 MatchStackLimitSize
= size
;
436 stack_double(OnigStackType
** arg_stk_base
, OnigStackType
** arg_stk_end
,
437 OnigStackType
** arg_stk
, OnigStackType
* stk_alloc
, OnigMatchArg
* msa
)
440 OnigStackType
*x
, *stk_base
, *stk_end
, *stk
;
442 stk_base
= *arg_stk_base
;
443 stk_end
= *arg_stk_end
;
446 n
= stk_end
- stk_base
;
447 if (stk_base
== stk_alloc
&& IS_NULL(msa
->stack_p
)) {
448 x
= (OnigStackType
* )xmalloc(sizeof(OnigStackType
) * n
* 2);
451 return ONIGERR_MEMORY
;
453 xmemcpy(x
, stk_base
, n
* sizeof(OnigStackType
));
457 unsigned int limit_size
= MatchStackLimitSize
;
459 if (limit_size
!= 0 && n
> limit_size
) {
460 if ((unsigned int )(stk_end
- stk_base
) == limit_size
)
461 return ONIGERR_MATCH_STACK_LIMIT_OVER
;
465 x
= (OnigStackType
* )xrealloc(stk_base
, sizeof(OnigStackType
) * n
);
468 return ONIGERR_MEMORY
;
471 *arg_stk
= x
+ (stk
- stk_base
);
473 *arg_stk_end
= x
+ n
;
477 #define STACK_ENSURE(n) do {\
478 if (stk_end - stk < (n)) {\
479 int r = stack_double(&stk_base, &stk_end, &stk, stk_alloc, msa);\
480 if (r != 0) { STACK_SAVE; return r; } \
484 #define STACK_AT(index) (stk_base + (index))
485 #define GET_STACK_INDEX(stk) ((stk) - stk_base)
487 #define STACK_PUSH_TYPE(stack_type) do {\
489 stk->type = (stack_type);\
493 #define IS_TO_VOID_TARGET(stk) (((stk)->type & STK_MASK_TO_VOID_TARGET) != 0)
495 #ifdef USE_COMBINATION_EXPLOSION_CHECK
496 #define STATE_CHECK_POS(s,snum) \
497 (((s) - str) * num_comb_exp_check + ((snum) - 1))
498 #define STATE_CHECK_VAL(v,snum) do {\
499 if (state_check_buff != NULL) {\
500 int x = STATE_CHECK_POS(s,snum);\
501 (v) = state_check_buff[x/8] & (1<<(x%8));\
507 #define ELSE_IF_STATE_CHECK_MARK(stk) \
508 else if ((stk)->type == STK_STATE_CHECK_MARK) { \
509 int x = STATE_CHECK_POS(stk->u.state.pstr, stk->u.state.state_check);\
510 state_check_buff[x/8] |= (1<<(x%8)); \
513 #define STACK_PUSH(stack_type,pat,s,sprev) do {\
515 stk->type = (stack_type);\
516 stk->u.state.pcode = (pat);\
517 stk->u.state.pstr = (s);\
518 stk->u.state.pstr_prev = (sprev);\
519 stk->u.state.state_check = 0;\
523 #define STACK_PUSH_ENSURED(stack_type,pat) do {\
524 stk->type = (stack_type);\
525 stk->u.state.pcode = (pat);\
526 stk->u.state.state_check = 0;\
530 #define STACK_PUSH_ALT_WITH_STATE_CHECK(pat,s,sprev,snum) do {\
532 stk->type = STK_ALT;\
533 stk->u.state.pcode = (pat);\
534 stk->u.state.pstr = (s);\
535 stk->u.state.pstr_prev = (sprev);\
536 stk->u.state.state_check = ((state_check_buff != NULL) ? (snum) : 0);\
540 #define STACK_PUSH_STATE_CHECK(s,snum) do {\
541 if (state_check_buff != NULL) {\
543 stk->type = STK_STATE_CHECK_MARK;\
544 stk->u.state.pstr = (s);\
545 stk->u.state.state_check = (snum);\
550 #else /* USE_COMBINATION_EXPLOSION_CHECK */
552 #define ELSE_IF_STATE_CHECK_MARK(stk)
554 #define STACK_PUSH(stack_type,pat,s,sprev) do {\
556 stk->type = (stack_type);\
557 stk->u.state.pcode = (pat);\
558 stk->u.state.pstr = (s);\
559 stk->u.state.pstr_prev = (sprev);\
563 #define STACK_PUSH_ENSURED(stack_type,pat) do {\
564 stk->type = (stack_type);\
565 stk->u.state.pcode = (pat);\
568 #endif /* USE_COMBINATION_EXPLOSION_CHECK */
570 #define STACK_PUSH_ALT(pat,s,sprev) STACK_PUSH(STK_ALT,pat,s,sprev)
571 #define STACK_PUSH_POS(s,sprev) STACK_PUSH(STK_POS,NULL_UCHARP,s,sprev)
572 #define STACK_PUSH_POS_NOT(pat,s,sprev) STACK_PUSH(STK_POS_NOT,pat,s,sprev)
573 #define STACK_PUSH_STOP_BT STACK_PUSH_TYPE(STK_STOP_BT)
574 #define STACK_PUSH_LOOK_BEHIND_NOT(pat,s,sprev) \
575 STACK_PUSH(STK_LOOK_BEHIND_NOT,pat,s,sprev)
577 #define STACK_PUSH_REPEAT(id, pat) do {\
579 stk->type = STK_REPEAT;\
580 stk->u.repeat.num = (id);\
581 stk->u.repeat.pcode = (pat);\
582 stk->u.repeat.count = 0;\
586 #define STACK_PUSH_REPEAT_INC(sindex) do {\
588 stk->type = STK_REPEAT_INC;\
589 stk->u.repeat_inc.si = (sindex);\
593 #define STACK_PUSH_MEM_START(mnum, s) do {\
595 stk->type = STK_MEM_START;\
596 stk->u.mem.num = (mnum);\
597 stk->u.mem.pstr = (s);\
598 stk->u.mem.start = mem_start_stk[mnum];\
599 stk->u.mem.end = mem_end_stk[mnum];\
600 mem_start_stk[mnum] = GET_STACK_INDEX(stk);\
601 mem_end_stk[mnum] = INVALID_STACK_INDEX;\
605 #define STACK_PUSH_MEM_END(mnum, s) do {\
607 stk->type = STK_MEM_END;\
608 stk->u.mem.num = (mnum);\
609 stk->u.mem.pstr = (s);\
610 stk->u.mem.start = mem_start_stk[mnum];\
611 stk->u.mem.end = mem_end_stk[mnum];\
612 mem_end_stk[mnum] = GET_STACK_INDEX(stk);\
616 #define STACK_PUSH_MEM_END_MARK(mnum) do {\
618 stk->type = STK_MEM_END_MARK;\
619 stk->u.mem.num = (mnum);\
623 #define STACK_GET_MEM_START(mnum, k) do {\
626 while (k > stk_base) {\
628 if ((k->type & STK_MASK_MEM_END_OR_MARK) != 0 \
629 && k->u.mem.num == (mnum)) {\
632 else if (k->type == STK_MEM_START && k->u.mem.num == (mnum)) {\
633 if (level == 0) break;\
639 #define STACK_GET_MEM_RANGE(k, mnum, start, end) do {\
642 if (k->type == STK_MEM_START && k->u.mem.num == (mnum)) {\
643 if (level == 0) (start) = k->u.mem.pstr;\
646 else if (k->type == STK_MEM_END && k->u.mem.num == (mnum)) {\
649 (end) = k->u.mem.pstr;\
657 #define STACK_PUSH_NULL_CHECK_START(cnum, s) do {\
659 stk->type = STK_NULL_CHECK_START;\
660 stk->u.null_check.num = (cnum);\
661 stk->u.null_check.pstr = (s);\
665 #define STACK_PUSH_NULL_CHECK_END(cnum) do {\
667 stk->type = STK_NULL_CHECK_END;\
668 stk->u.null_check.num = (cnum);\
672 #define STACK_PUSH_CALL_FRAME(pat) do {\
674 stk->type = STK_CALL_FRAME;\
675 stk->u.call_frame.ret_addr = (pat);\
679 #define STACK_PUSH_RETURN do {\
681 stk->type = STK_RETURN;\
687 #define STACK_BASE_CHECK(p, at) \
688 if ((p) < stk_base) {\
689 fprintf(stderr, "at %s\n", at);\
693 #define STACK_BASE_CHECK(p, at)
696 #define STACK_POP_ONE do {\
698 STACK_BASE_CHECK(stk, "STACK_POP_ONE"); \
701 #define STACK_POP do {\
702 switch (pop_level) {\
703 case STACK_POP_LEVEL_FREE:\
706 STACK_BASE_CHECK(stk, "STACK_POP"); \
707 if ((stk->type & STK_MASK_POP_USED) != 0) break;\
708 ELSE_IF_STATE_CHECK_MARK(stk);\
711 case STACK_POP_LEVEL_MEM_START:\
714 STACK_BASE_CHECK(stk, "STACK_POP 2"); \
715 if ((stk->type & STK_MASK_POP_USED) != 0) break;\
716 else if (stk->type == STK_MEM_START) {\
717 mem_start_stk[stk->u.mem.num] = stk->u.mem.start;\
718 mem_end_stk[stk->u.mem.num] = stk->u.mem.end;\
720 ELSE_IF_STATE_CHECK_MARK(stk);\
726 STACK_BASE_CHECK(stk, "STACK_POP 3"); \
727 if ((stk->type & STK_MASK_POP_USED) != 0) break;\
728 else if (stk->type == STK_MEM_START) {\
729 mem_start_stk[stk->u.mem.num] = stk->u.mem.start;\
730 mem_end_stk[stk->u.mem.num] = stk->u.mem.end;\
732 else if (stk->type == STK_REPEAT_INC) {\
733 STACK_AT(stk->u.repeat_inc.si)->u.repeat.count--;\
735 else if (stk->type == STK_MEM_END) {\
736 mem_start_stk[stk->u.mem.num] = stk->u.mem.start;\
737 mem_end_stk[stk->u.mem.num] = stk->u.mem.end;\
739 ELSE_IF_STATE_CHECK_MARK(stk);\
745 #define STACK_POP_TIL_POS_NOT do {\
748 STACK_BASE_CHECK(stk, "STACK_POP_TIL_POS_NOT"); \
749 if (stk->type == STK_POS_NOT) break;\
750 else if (stk->type == STK_MEM_START) {\
751 mem_start_stk[stk->u.mem.num] = stk->u.mem.start;\
752 mem_end_stk[stk->u.mem.num] = stk->u.mem.end;\
754 else if (stk->type == STK_REPEAT_INC) {\
755 STACK_AT(stk->u.repeat_inc.si)->u.repeat.count--;\
757 else if (stk->type == STK_MEM_END) {\
758 mem_start_stk[stk->u.mem.num] = stk->u.mem.start;\
759 mem_end_stk[stk->u.mem.num] = stk->u.mem.end;\
761 ELSE_IF_STATE_CHECK_MARK(stk);\
765 #define STACK_POP_TIL_LOOK_BEHIND_NOT do {\
768 STACK_BASE_CHECK(stk, "STACK_POP_TIL_LOOK_BEHIND_NOT"); \
769 if (stk->type == STK_LOOK_BEHIND_NOT) break;\
770 else if (stk->type == STK_MEM_START) {\
771 mem_start_stk[stk->u.mem.num] = stk->u.mem.start;\
772 mem_end_stk[stk->u.mem.num] = stk->u.mem.end;\
774 else if (stk->type == STK_REPEAT_INC) {\
775 STACK_AT(stk->u.repeat_inc.si)->u.repeat.count--;\
777 else if (stk->type == STK_MEM_END) {\
778 mem_start_stk[stk->u.mem.num] = stk->u.mem.start;\
779 mem_end_stk[stk->u.mem.num] = stk->u.mem.end;\
781 ELSE_IF_STATE_CHECK_MARK(stk);\
785 #define STACK_POS_END(k) do {\
789 STACK_BASE_CHECK(k, "STACK_POS_END"); \
790 if (IS_TO_VOID_TARGET(k)) {\
793 else if (k->type == STK_POS) {\
800 #define STACK_STOP_BT_END do {\
801 OnigStackType *k = stk;\
804 STACK_BASE_CHECK(k, "STACK_STOP_BT_END"); \
805 if (IS_TO_VOID_TARGET(k)) {\
808 else if (k->type == STK_STOP_BT) {\
815 #define STACK_NULL_CHECK(isnull,id,s) do {\
816 OnigStackType* k = stk;\
819 STACK_BASE_CHECK(k, "STACK_NULL_CHECK"); \
820 if (k->type == STK_NULL_CHECK_START) {\
821 if (k->u.null_check.num == (id)) {\
822 (isnull) = (k->u.null_check.pstr == (s));\
829 #define STACK_NULL_CHECK_REC(isnull,id,s) do {\
831 OnigStackType* k = stk;\
834 STACK_BASE_CHECK(k, "STACK_NULL_CHECK_REC"); \
835 if (k->type == STK_NULL_CHECK_START) {\
836 if (k->u.null_check.num == (id)) {\
838 (isnull) = (k->u.null_check.pstr == (s));\
844 else if (k->type == STK_NULL_CHECK_END) {\
850 #define STACK_NULL_CHECK_MEMST(isnull,id,s,reg) do {\
851 OnigStackType* k = stk;\
854 STACK_BASE_CHECK(k, "STACK_NULL_CHECK_MEMST"); \
855 if (k->type == STK_NULL_CHECK_START) {\
856 if (k->u.null_check.num == (id)) {\
857 if (k->u.null_check.pstr != (s)) {\
865 if (k->type == STK_MEM_START) {\
866 if (k->u.mem.end == INVALID_STACK_INDEX) {\
867 (isnull) = 0; break;\
869 if (BIT_STATUS_AT(reg->bt_mem_end, k->u.mem.num))\
870 endp = STACK_AT(k->u.mem.end)->u.mem.pstr;\
872 endp = (UChar* )k->u.mem.end;\
873 if (STACK_AT(k->u.mem.start)->u.mem.pstr != endp) {\
874 (isnull) = 0; break;\
876 else if (endp != s) {\
877 (isnull) = -1; /* empty, but position changed */ \
889 #define STACK_NULL_CHECK_MEMST_REC(isnull,id,s,reg) do {\
891 OnigStackType* k = stk;\
894 STACK_BASE_CHECK(k, "STACK_NULL_CHECK_MEMST_REC"); \
895 if (k->type == STK_NULL_CHECK_START) {\
896 if (k->u.null_check.num == (id)) {\
898 if (k->u.null_check.pstr != (s)) {\
906 if (k->type == STK_MEM_START) {\
907 if (k->u.mem.end == INVALID_STACK_INDEX) {\
908 (isnull) = 0; break;\
910 if (BIT_STATUS_AT(reg->bt_mem_end, k->u.mem.num))\
911 endp = STACK_AT(k->u.mem.end)->u.mem.pstr;\
913 endp = (UChar* )k->u.mem.end;\
914 if (STACK_AT(k->u.mem.start)->u.mem.pstr != endp) {\
915 (isnull) = 0; break;\
917 else if (endp != s) {\
918 (isnull) = -1; /* empty, but position changed */ \
931 else if (k->type == STK_NULL_CHECK_END) {\
932 if (k->u.null_check.num == (id)) level++;\
937 #define STACK_GET_REPEAT(id, k) do {\
942 STACK_BASE_CHECK(k, "STACK_GET_REPEAT"); \
943 if (k->type == STK_REPEAT) {\
945 if (k->u.repeat.num == (id)) {\
950 else if (k->type == STK_CALL_FRAME) level--;\
951 else if (k->type == STK_RETURN) level++;\
955 #define STACK_RETURN(addr) do {\
957 OnigStackType* k = stk;\
960 STACK_BASE_CHECK(k, "STACK_RETURN"); \
961 if (k->type == STK_CALL_FRAME) {\
963 (addr) = k->u.call_frame.ret_addr;\
968 else if (k->type == STK_RETURN)\
974 #define STRING_CMP(s1,s2,len) do {\
976 if (*s1++ != *s2++) goto fail;\
980 #define STRING_CMP_IC(case_fold_flag,s1,ps2,len) do {\
981 if (string_cmp_ic(encode, case_fold_flag, s1, ps2, len) == 0) \
985 static int string_cmp_ic(OnigEncoding enc
, int case_fold_flag
,
986 UChar
* s1
, UChar
** ps2
, int mblen
)
988 UChar buf1
[ONIGENC_MBC_CASE_FOLD_MAXLEN
];
989 UChar buf2
[ONIGENC_MBC_CASE_FOLD_MAXLEN
];
990 UChar
*p1
, *p2
, *end1
, *s2
, *end2
;
997 len1
= ONIGENC_MBC_CASE_FOLD(enc
, case_fold_flag
, &s1
, end1
, buf1
);
998 len2
= ONIGENC_MBC_CASE_FOLD(enc
, case_fold_flag
, &s2
, end2
, buf2
);
999 if (len1
!= len2
) return 0;
1002 while (len1
-- > 0) {
1003 if (*p1
!= *p2
) return 0;
1013 #define STRING_CMP_VALUE(s1,s2,len,is_fail) do {\
1015 while (len-- > 0) {\
1016 if (*s1++ != *s2++) {\
1017 is_fail = 1; break;\
1022 #define STRING_CMP_VALUE_IC(case_fold_flag,s1,ps2,len,is_fail) do {\
1023 if (string_cmp_ic(encode, case_fold_flag, s1, ps2, len) == 0) \
1030 #define IS_EMPTY_STR (str == end)
1031 #define ON_STR_BEGIN(s) ((s) == str)
1032 #define ON_STR_END(s) ((s) == end)
1033 #ifdef USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE
1034 #define DATA_ENSURE_CHECK1 (s < right_range)
1035 #define DATA_ENSURE_CHECK(n) (s + (n) <= right_range)
1036 #define DATA_ENSURE(n) if (s + (n) > right_range) goto fail
1038 #define DATA_ENSURE_CHECK1 (s < end)
1039 #define DATA_ENSURE_CHECK(n) (s + (n) <= end)
1040 #define DATA_ENSURE(n) if (s + (n) > end) goto fail
1041 #endif /* USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE */
1044 #ifdef USE_CAPTURE_HISTORY
1046 make_capture_history_tree(OnigCaptureTreeNode
* node
, OnigStackType
** kp
,
1047 OnigStackType
* stk_top
, UChar
* str
, regex_t
* reg
)
1050 OnigCaptureTreeNode
* child
;
1051 OnigStackType
* k
= *kp
;
1053 while (k
< stk_top
) {
1054 if (k
->type
== STK_MEM_START
) {
1056 if (n
<= ONIG_MAX_CAPTURE_HISTORY_GROUP
&&
1057 BIT_STATUS_AT(reg
->capture_history
, n
) != 0) {
1058 child
= history_node_new();
1059 CHECK_NULL_RETURN_MEMERR(child
);
1061 child
->beg
= (int )(k
->u
.mem
.pstr
- str
);
1062 r
= history_tree_add_child(node
, child
);
1063 if (r
!= 0) return r
;
1065 r
= make_capture_history_tree(child
, kp
, stk_top
, str
, reg
);
1066 if (r
!= 0) return r
;
1069 child
->end
= (int )(k
->u
.mem
.pstr
- str
);
1072 else if (k
->type
== STK_MEM_END
) {
1073 if (k
->u
.mem
.num
== node
->group
) {
1074 node
->end
= (int )(k
->u
.mem
.pstr
- str
);
1082 return 1; /* 1: root node ending. */
1086 #ifdef USE_BACKREF_WITH_LEVEL
1087 static int mem_is_in_memp(int mem
, int num
, UChar
* memp
)
1092 for (i
= 0; i
< num
; i
++) {
1093 GET_MEMNUM_INC(m
, memp
);
1094 if (mem
== (int )m
) return 1;
1099 static int backref_match_at_nested_level(regex_t
* reg
1100 , OnigStackType
* top
, OnigStackType
* stk_base
1101 , int ignore_case
, int case_fold_flag
1102 , int nest
, int mem_num
, UChar
* memp
, UChar
** s
, const UChar
* send
)
1104 UChar
*ss
, *p
, *pstart
, *pend
= NULL_UCHARP
;
1111 while (k
>= stk_base
) {
1112 if (k
->type
== STK_CALL_FRAME
) {
1115 else if (k
->type
== STK_RETURN
) {
1118 else if (level
== nest
) {
1119 if (k
->type
== STK_MEM_START
) {
1120 if (mem_is_in_memp(k
->u
.mem
.num
, mem_num
, memp
)) {
1121 pstart
= k
->u
.mem
.pstr
;
1122 if (pend
!= NULL_UCHARP
) {
1123 if (pend
- pstart
> send
- *s
) return 0; /* or goto next_mem; */
1127 if (ignore_case
!= 0) {
1128 if (string_cmp_ic(reg
->enc
, case_fold_flag
,
1129 pstart
, &ss
, (int )(pend
- pstart
)) == 0)
1130 return 0; /* or goto next_mem; */
1134 if (*p
++ != *ss
++) return 0; /* or goto next_mem; */
1143 else if (k
->type
== STK_MEM_END
) {
1144 if (mem_is_in_memp(k
->u
.mem
.num
, mem_num
, memp
)) {
1145 pend
= k
->u
.mem
.pstr
;
1154 #endif /* USE_BACKREF_WITH_LEVEL */
1157 #ifdef ONIG_DEBUG_STATISTICS
1159 #define USE_TIMEOFDAY
1161 #ifdef USE_TIMEOFDAY
1162 #ifdef HAVE_SYS_TIME_H
1163 #include <sys/time.h>
1165 #ifdef HAVE_UNISTD_H
1168 static struct timeval ts
, te
;
1169 #define GETTIME(t) gettimeofday(&(t), (struct timezone* )0)
1170 #define TIMEDIFF(te,ts) (((te).tv_usec - (ts).tv_usec) + \
1171 (((te).tv_sec - (ts).tv_sec)*1000000))
1173 #ifdef HAVE_SYS_TIMES_H
1174 #include <sys/times.h>
1176 static struct tms ts
, te
;
1177 #define GETTIME(t) times(&(t))
1178 #define TIMEDIFF(te,ts) ((te).tms_utime - (ts).tms_utime)
1181 static int OpCounter
[256];
1182 static int OpPrevCounter
[256];
1183 static unsigned long OpTime
[256];
1184 static int OpCurr
= OP_FINISH
;
1185 static int OpPrevTarget
= OP_FAIL
;
1186 static int MaxStackDepth
= 0;
1188 #define MOP_IN(opcode) do {\
1189 if (opcode == OpPrevTarget) OpPrevCounter[OpCurr]++;\
1191 OpCounter[opcode]++;\
1195 #define MOP_OUT do {\
1197 OpTime[OpCurr] += TIMEDIFF(te, ts);\
1201 onig_statistics_init(void)
1204 for (i
= 0; i
< 256; i
++) {
1205 OpCounter
[i
] = OpPrevCounter
[i
] = 0; OpTime
[i
] = 0;
1211 onig_print_statistics(FILE* f
)
1214 fprintf(f
, " count prev time\n");
1215 for (i
= 0; OnigOpInfo
[i
].opcode
>= 0; i
++) {
1216 fprintf(f
, "%8d: %8d: %10ld: %s\n",
1217 OpCounter
[i
], OpPrevCounter
[i
], OpTime
[i
], OnigOpInfo
[i
].name
);
1219 fprintf(f
, "\nmax stack depth: %d\n", MaxStackDepth
);
1222 #define STACK_INC do {\
1224 if (stk - stk_base > MaxStackDepth) \
1225 MaxStackDepth = stk - stk_base;\
1229 #define STACK_INC stk++
1231 #define MOP_IN(opcode)
1236 /* matching region of POSIX API */
1237 typedef int regoff_t
;
1244 /* match data(str - end) from position (sstart). */
1245 /* if sstart == str then set sprev to NULL. */
1247 match_at(regex_t
* reg
, const UChar
* str
, const UChar
* end
,
1248 #ifdef USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE
1249 const UChar
* right_range
,
1251 const UChar
* sstart
, UChar
* sprev
, OnigMatchArg
* msa
)
1253 static const UChar FinishCode
[] = { OP_FINISH
};
1255 int i
, n
, num_mem
, best_len
, pop_level
;
1256 LengthType tlen
, tlen2
;
1259 OnigOptionType option
= reg
->options
;
1260 OnigEncoding encode
= reg
->enc
;
1261 OnigCaseFoldType case_fold_flag
= reg
->case_fold_flag
;
1262 UChar
*s
, *q
, *sbegin
;
1265 OnigStackType
*stk_alloc
, *stk_base
, *stk
, *stk_end
;
1266 OnigStackType
*stkp
; /* used as any purpose. */
1268 OnigStackIndex
*repeat_stk
;
1269 OnigStackIndex
*mem_start_stk
, *mem_end_stk
;
1270 #ifdef USE_COMBINATION_EXPLOSION_CHECK
1272 unsigned char* state_check_buff
= msa
->state_check_buff
;
1273 int num_comb_exp_check
= reg
->num_comb_exp_check
;
1275 n
= reg
->num_repeat
+ reg
->num_mem
* 2;
1277 STACK_INIT(alloca_base
, n
, INIT_MATCH_STACK_SIZE
);
1278 pop_level
= reg
->stack_pop_level
;
1279 num_mem
= reg
->num_mem
;
1280 repeat_stk
= (OnigStackIndex
* )alloca_base
;
1282 mem_start_stk
= (OnigStackIndex
* )(repeat_stk
+ reg
->num_repeat
);
1283 mem_end_stk
= mem_start_stk
+ num_mem
;
1284 mem_start_stk
--; /* for index start from 1,
1285 mem_start_stk[1]..mem_start_stk[num_mem] */
1286 mem_end_stk
--; /* for index start from 1,
1287 mem_end_stk[1]..mem_end_stk[num_mem] */
1288 for (i
= 1; i
<= num_mem
; i
++) {
1289 mem_start_stk
[i
] = mem_end_stk
[i
] = INVALID_STACK_INDEX
;
1292 #ifdef ONIG_DEBUG_MATCH
1293 fprintf(stderr
, "match_at: str: %d, end: %d, start: %d, sprev: %d\n",
1294 (int )str
, (int )end
, (int )sstart
, (int )sprev
);
1295 fprintf(stderr
, "size: %d, start offset: %d\n",
1296 (int )(end
- str
), (int )(sstart
- str
));
1299 STACK_PUSH_ENSURED(STK_ALT
, (UChar
*)FinishCode
); /* bottom stack */
1300 best_len
= ONIG_MISMATCH
;
1301 s
= (UChar
* )sstart
;
1303 #ifdef ONIG_DEBUG_MATCH
1305 UChar
*q
, *bp
, buf
[50];
1307 fprintf(stderr
, "%4d> \"", (int )(s
- str
));
1309 for (i
= 0, q
= s
; i
< 7 && q
< end
; i
++) {
1310 len
= enclen(encode
, q
);
1311 while (len
-- > 0) *bp
++ = *q
++;
1313 if (q
< end
) { xmemcpy(bp
, "...\"", 4); bp
+= 4; }
1314 else { xmemcpy(bp
, "\"", 1); bp
+= 1; }
1316 fputs((char* )buf
, stderr
);
1317 for (i
= 0; i
< 20 - (bp
- buf
); i
++) fputc(' ', stderr
);
1318 onig_print_compiled_byte_code(stderr
, p
, NULL
, encode
);
1319 fprintf(stderr
, "\n");
1325 case OP_END
: MOP_IN(OP_END
);
1329 #ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
1330 if (IS_FIND_LONGEST(option
)) {
1331 if (n
> msa
->best_len
) {
1333 msa
->best_s
= (UChar
* )sstart
;
1340 region
= msa
->region
;
1342 #ifdef USE_POSIX_API_REGION_OPTION
1343 if (IS_POSIX_REGION(msa
->options
)) {
1344 posix_regmatch_t
* rmt
= (posix_regmatch_t
* )region
;
1346 rmt
[0].rm_so
= sstart
- str
;
1347 rmt
[0].rm_eo
= s
- str
;
1348 for (i
= 1; i
<= num_mem
; i
++) {
1349 if (mem_end_stk
[i
] != INVALID_STACK_INDEX
) {
1350 if (BIT_STATUS_AT(reg
->bt_mem_start
, i
))
1351 rmt
[i
].rm_so
= STACK_AT(mem_start_stk
[i
])->u
.mem
.pstr
- str
;
1353 rmt
[i
].rm_so
= (UChar
* )((void* )(mem_start_stk
[i
])) - str
;
1355 rmt
[i
].rm_eo
= (BIT_STATUS_AT(reg
->bt_mem_end
, i
)
1356 ? STACK_AT(mem_end_stk
[i
])->u
.mem
.pstr
1357 : (UChar
* )((void* )mem_end_stk
[i
])) - str
;
1360 rmt
[i
].rm_so
= rmt
[i
].rm_eo
= ONIG_REGION_NOTPOS
;
1365 #endif /* USE_POSIX_API_REGION_OPTION */
1366 region
->beg
[0] = sstart
- str
;
1367 region
->end
[0] = s
- str
;
1368 for (i
= 1; i
<= num_mem
; i
++) {
1369 if (mem_end_stk
[i
] != INVALID_STACK_INDEX
) {
1370 if (BIT_STATUS_AT(reg
->bt_mem_start
, i
))
1371 region
->beg
[i
] = STACK_AT(mem_start_stk
[i
])->u
.mem
.pstr
- str
;
1373 region
->beg
[i
] = (UChar
* )((void* )mem_start_stk
[i
]) - str
;
1375 region
->end
[i
] = (BIT_STATUS_AT(reg
->bt_mem_end
, i
)
1376 ? STACK_AT(mem_end_stk
[i
])->u
.mem
.pstr
1377 : (UChar
* )((void* )mem_end_stk
[i
])) - str
;
1380 region
->beg
[i
] = region
->end
[i
] = ONIG_REGION_NOTPOS
;
1384 #ifdef USE_CAPTURE_HISTORY
1385 if (reg
->capture_history
!= 0) {
1387 OnigCaptureTreeNode
* node
;
1389 if (IS_NULL(region
->history_root
)) {
1390 region
->history_root
= node
= history_node_new();
1391 CHECK_NULL_RETURN_MEMERR(node
);
1394 node
= region
->history_root
;
1395 history_tree_clear(node
);
1399 node
->beg
= sstart
- str
;
1400 node
->end
= s
- str
;
1403 r
= make_capture_history_tree(region
->history_root
, &stkp
,
1404 stk
, (UChar
* )str
, reg
);
1406 best_len
= r
; /* error code */
1410 #endif /* USE_CAPTURE_HISTORY */
1411 #ifdef USE_POSIX_API_REGION_OPTION
1412 } /* else IS_POSIX_REGION() */
1415 } /* n > best_len */
1417 #ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
1422 if (IS_FIND_CONDITION(option
)) {
1423 if (IS_FIND_NOT_EMPTY(option
) && s
== sstart
) {
1424 best_len
= ONIG_MISMATCH
;
1425 goto fail
; /* for retry */
1427 if (IS_FIND_LONGEST(option
) && DATA_ENSURE_CHECK1
) {
1428 goto fail
; /* for retry */
1432 /* default behavior: return first-matching result. */
1436 case OP_EXACT1
: MOP_IN(OP_EXACT1
);
1439 if (*p
!= *s
) goto fail
;
1442 if (*p
!= *s
++) goto fail
;
1448 case OP_EXACT1_IC
: MOP_IN(OP_EXACT1_IC
);
1451 UChar
*q
, lowbuf
[ONIGENC_MBC_CASE_FOLD_MAXLEN
];
1454 len
= ONIGENC_MBC_CASE_FOLD(encode
,
1455 /* DISABLE_CASE_FOLD_MULTI_CHAR(case_fold_flag), */
1470 case OP_EXACT2
: MOP_IN(OP_EXACT2
);
1472 if (*p
!= *s
) goto fail
;
1474 if (*p
!= *s
) goto fail
;
1481 case OP_EXACT3
: MOP_IN(OP_EXACT3
);
1483 if (*p
!= *s
) goto fail
;
1485 if (*p
!= *s
) goto fail
;
1487 if (*p
!= *s
) goto fail
;
1494 case OP_EXACT4
: MOP_IN(OP_EXACT4
);
1496 if (*p
!= *s
) goto fail
;
1498 if (*p
!= *s
) goto fail
;
1500 if (*p
!= *s
) goto fail
;
1502 if (*p
!= *s
) goto fail
;
1509 case OP_EXACT5
: MOP_IN(OP_EXACT5
);
1511 if (*p
!= *s
) goto fail
;
1513 if (*p
!= *s
) goto fail
;
1515 if (*p
!= *s
) goto fail
;
1517 if (*p
!= *s
) goto fail
;
1519 if (*p
!= *s
) goto fail
;
1526 case OP_EXACTN
: MOP_IN(OP_EXACTN
);
1527 GET_LENGTH_INC(tlen
, p
);
1529 while (tlen
-- > 0) {
1530 if (*p
++ != *s
++) goto fail
;
1537 case OP_EXACTN_IC
: MOP_IN(OP_EXACTN_IC
);
1540 UChar
*q
, *endp
, lowbuf
[ONIGENC_MBC_CASE_FOLD_MAXLEN
];
1542 GET_LENGTH_INC(tlen
, p
);
1548 len
= ONIGENC_MBC_CASE_FOLD(encode
,
1549 /* DISABLE_CASE_FOLD_MULTI_CHAR(case_fold_flag), */
1555 if (*p
!= *q
) goto fail
;
1565 case OP_EXACTMB2N1
: MOP_IN(OP_EXACTMB2N1
);
1567 if (*p
!= *s
) goto fail
;
1569 if (*p
!= *s
) goto fail
;
1574 case OP_EXACTMB2N2
: MOP_IN(OP_EXACTMB2N2
);
1576 if (*p
!= *s
) goto fail
;
1578 if (*p
!= *s
) goto fail
;
1581 if (*p
!= *s
) goto fail
;
1583 if (*p
!= *s
) goto fail
;
1589 case OP_EXACTMB2N3
: MOP_IN(OP_EXACTMB2N3
);
1591 if (*p
!= *s
) goto fail
;
1593 if (*p
!= *s
) goto fail
;
1595 if (*p
!= *s
) goto fail
;
1597 if (*p
!= *s
) goto fail
;
1600 if (*p
!= *s
) goto fail
;
1602 if (*p
!= *s
) goto fail
;
1608 case OP_EXACTMB2N
: MOP_IN(OP_EXACTMB2N
);
1609 GET_LENGTH_INC(tlen
, p
);
1610 DATA_ENSURE(tlen
* 2);
1611 while (tlen
-- > 0) {
1612 if (*p
!= *s
) goto fail
;
1614 if (*p
!= *s
) goto fail
;
1622 case OP_EXACTMB3N
: MOP_IN(OP_EXACTMB3N
);
1623 GET_LENGTH_INC(tlen
, p
);
1624 DATA_ENSURE(tlen
* 3);
1625 while (tlen
-- > 0) {
1626 if (*p
!= *s
) goto fail
;
1628 if (*p
!= *s
) goto fail
;
1630 if (*p
!= *s
) goto fail
;
1638 case OP_EXACTMBN
: MOP_IN(OP_EXACTMBN
);
1639 GET_LENGTH_INC(tlen
, p
); /* mb-len */
1640 GET_LENGTH_INC(tlen2
, p
); /* string len */
1643 while (tlen2
-- > 0) {
1644 if (*p
!= *s
) goto fail
;
1652 case OP_CCLASS
: MOP_IN(OP_CCLASS
);
1654 if (BITSET_AT(((BitSetRef
)p
), *s
) == 0) goto fail
;
1656 s
+= enclen(encode
, s
, end
); /* OP_CCLASS can match mb-code. \D, \S */
1660 case OP_CCLASS_MB
: MOP_IN(OP_CCLASS_MB
);
1661 if (! ONIGENC_IS_MBC_HEAD(encode
, s
, end
)) goto fail
;
1664 GET_LENGTH_INC(tlen
, p
);
1671 mb_len
= enclen(encode
, s
, end
);
1672 DATA_ENSURE(mb_len
);
1675 code
= ONIGENC_MBC_TO_CODE(encode
, ss
, s
);
1677 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
1678 if (! onig_is_in_code_range(p
, code
)) goto fail
;
1682 if (! onig_is_in_code_range(q
, code
)) goto fail
;
1689 case OP_CCLASS_MIX
: MOP_IN(OP_CCLASS_MIX
);
1691 if (ONIGENC_IS_MBC_HEAD(encode
, s
, end
)) {
1696 if (BITSET_AT(((BitSetRef
)p
), *s
) == 0)
1700 GET_LENGTH_INC(tlen
, p
);
1707 case OP_CCLASS_NOT
: MOP_IN(OP_CCLASS_NOT
);
1709 if (BITSET_AT(((BitSetRef
)p
), *s
) != 0) goto fail
;
1711 s
+= enclen(encode
, s
, end
);
1715 case OP_CCLASS_MB_NOT
: MOP_IN(OP_CCLASS_MB_NOT
);
1717 if (! ONIGENC_IS_MBC_HEAD(encode
, s
, end
)) {
1719 GET_LENGTH_INC(tlen
, p
);
1721 goto cc_mb_not_success
;
1725 GET_LENGTH_INC(tlen
, p
);
1729 int mb_len
= enclen(encode
, s
, end
);
1731 if (! DATA_ENSURE_CHECK(mb_len
)) {
1735 goto cc_mb_not_success
;
1740 code
= ONIGENC_MBC_TO_CODE(encode
, ss
, s
);
1742 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
1743 if (onig_is_in_code_range(p
, code
)) goto fail
;
1747 if (onig_is_in_code_range(q
, code
)) goto fail
;
1756 case OP_CCLASS_MIX_NOT
: MOP_IN(OP_CCLASS_MIX_NOT
);
1758 if (ONIGENC_IS_MBC_HEAD(encode
, s
, end
)) {
1763 if (BITSET_AT(((BitSetRef
)p
), *s
) != 0)
1767 GET_LENGTH_INC(tlen
, p
);
1774 case OP_CCLASS_NODE
: MOP_IN(OP_CCLASS_NODE
);
1782 GET_POINTER_INC(node
, p
);
1783 mb_len
= enclen(encode
, s
, end
);
1787 code
= ONIGENC_MBC_TO_CODE(encode
, ss
, s
);
1788 if (onig_is_code_in_cc_len(mb_len
, code
, node
) == 0) goto fail
;
1793 case OP_ANYCHAR
: MOP_IN(OP_ANYCHAR
);
1795 n
= enclen(encode
, s
, end
);
1797 if (ONIGENC_IS_MBC_NEWLINE(encode
, s
, end
)) goto fail
;
1802 case OP_ANYCHAR_ML
: MOP_IN(OP_ANYCHAR_ML
);
1804 n
= enclen(encode
, s
, end
);
1810 case OP_ANYCHAR_STAR
: MOP_IN(OP_ANYCHAR_STAR
);
1811 while (DATA_ENSURE_CHECK1
) {
1812 STACK_PUSH_ALT(p
, s
, sprev
);
1813 n
= enclen(encode
, s
, end
);
1815 if (ONIGENC_IS_MBC_NEWLINE(encode
, s
, end
)) goto fail
;
1822 case OP_ANYCHAR_ML_STAR
: MOP_IN(OP_ANYCHAR_ML_STAR
);
1823 while (DATA_ENSURE_CHECK1
) {
1824 STACK_PUSH_ALT(p
, s
, sprev
);
1825 n
= enclen(encode
, s
, end
);
1839 case OP_ANYCHAR_STAR_PEEK_NEXT
: MOP_IN(OP_ANYCHAR_STAR_PEEK_NEXT
);
1840 while (DATA_ENSURE_CHECK1
) {
1842 STACK_PUSH_ALT(p
+ 1, s
, sprev
);
1844 n
= enclen(encode
, s
, end
);
1846 if (ONIGENC_IS_MBC_NEWLINE(encode
, s
, end
)) goto fail
;
1854 case OP_ANYCHAR_ML_STAR_PEEK_NEXT
:MOP_IN(OP_ANYCHAR_ML_STAR_PEEK_NEXT
);
1855 while (DATA_ENSURE_CHECK1
) {
1857 STACK_PUSH_ALT(p
+ 1, s
, sprev
);
1859 n
= enclen(encode
, s
, end
);
1874 #ifdef USE_COMBINATION_EXPLOSION_CHECK
1875 case OP_STATE_CHECK_ANYCHAR_STAR
: MOP_IN(OP_STATE_CHECK_ANYCHAR_STAR
);
1876 GET_STATE_CHECK_NUM_INC(mem
, p
);
1877 while (DATA_ENSURE_CHECK1
) {
1878 STATE_CHECK_VAL(scv
, mem
);
1881 STACK_PUSH_ALT_WITH_STATE_CHECK(p
, s
, sprev
, mem
);
1882 n
= enclen(encode
, s
);
1884 if (ONIGENC_IS_MBC_NEWLINE(encode
, s
, end
)) goto fail
;
1891 case OP_STATE_CHECK_ANYCHAR_ML_STAR
:
1892 MOP_IN(OP_STATE_CHECK_ANYCHAR_ML_STAR
);
1894 GET_STATE_CHECK_NUM_INC(mem
, p
);
1895 while (DATA_ENSURE_CHECK1
) {
1896 STATE_CHECK_VAL(scv
, mem
);
1899 STACK_PUSH_ALT_WITH_STATE_CHECK(p
, s
, sprev
, mem
);
1900 n
= enclen(encode
, s
);
1913 #endif /* USE_COMBINATION_EXPLOSION_CHECK */
1915 case OP_WORD
: MOP_IN(OP_WORD
);
1917 if (! ONIGENC_IS_MBC_WORD(encode
, s
, end
))
1920 s
+= enclen(encode
, s
, end
);
1924 case OP_NOT_WORD
: MOP_IN(OP_NOT_WORD
);
1926 if (ONIGENC_IS_MBC_WORD(encode
, s
, end
))
1929 s
+= enclen(encode
, s
, end
);
1933 case OP_WORD_BOUND
: MOP_IN(OP_WORD_BOUND
);
1934 if (ON_STR_BEGIN(s
)) {
1936 if (! ONIGENC_IS_MBC_WORD(encode
, s
, end
))
1939 else if (ON_STR_END(s
)) {
1940 if (! ONIGENC_IS_MBC_WORD(encode
, sprev
, end
))
1944 if (ONIGENC_IS_MBC_WORD(encode
, s
, end
)
1945 == ONIGENC_IS_MBC_WORD(encode
, sprev
, end
))
1952 case OP_NOT_WORD_BOUND
: MOP_IN(OP_NOT_WORD_BOUND
);
1953 if (ON_STR_BEGIN(s
)) {
1954 if (DATA_ENSURE_CHECK1
&& ONIGENC_IS_MBC_WORD(encode
, s
, end
))
1957 else if (ON_STR_END(s
)) {
1958 if (ONIGENC_IS_MBC_WORD(encode
, sprev
, end
))
1962 if (ONIGENC_IS_MBC_WORD(encode
, s
, end
)
1963 != ONIGENC_IS_MBC_WORD(encode
, sprev
, end
))
1970 #ifdef USE_WORD_BEGIN_END
1971 case OP_WORD_BEGIN
: MOP_IN(OP_WORD_BEGIN
);
1972 if (DATA_ENSURE_CHECK1
&& ONIGENC_IS_MBC_WORD(encode
, s
, end
)) {
1973 if (ON_STR_BEGIN(s
) || !ONIGENC_IS_MBC_WORD(encode
, sprev
, end
)) {
1981 case OP_WORD_END
: MOP_IN(OP_WORD_END
);
1982 if (!ON_STR_BEGIN(s
) && ONIGENC_IS_MBC_WORD(encode
, sprev
, end
)) {
1983 if (ON_STR_END(s
) || !ONIGENC_IS_MBC_WORD(encode
, s
, end
)) {
1992 case OP_BEGIN_BUF
: MOP_IN(OP_BEGIN_BUF
);
1993 if (! ON_STR_BEGIN(s
)) goto fail
;
1999 case OP_END_BUF
: MOP_IN(OP_END_BUF
);
2000 if (! ON_STR_END(s
)) goto fail
;
2006 case OP_BEGIN_LINE
: MOP_IN(OP_BEGIN_LINE
);
2007 if (ON_STR_BEGIN(s
)) {
2008 if (IS_NOTBOL(msa
->options
)) goto fail
;
2012 else if (ONIGENC_IS_MBC_NEWLINE(encode
, sprev
, end
) && !ON_STR_END(s
)) {
2019 case OP_END_LINE
: MOP_IN(OP_END_LINE
);
2020 if (ON_STR_END(s
)) {
2021 #ifndef USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE
2022 if (IS_EMPTY_STR
|| !ONIGENC_IS_MBC_NEWLINE(encode
, sprev
, end
)) {
2024 if (IS_NOTEOL(msa
->options
)) goto fail
;
2027 #ifndef USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE
2031 else if (ONIGENC_IS_MBC_NEWLINE(encode
, s
, end
)) {
2035 #ifdef USE_CRNL_AS_LINE_TERMINATOR
2036 else if (ONIGENC_IS_MBC_CRNL(encode
, s
, end
)) {
2044 case OP_SEMI_END_BUF
: MOP_IN(OP_SEMI_END_BUF
);
2045 if (ON_STR_END(s
)) {
2046 #ifndef USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE
2047 if (IS_EMPTY_STR
|| !ONIGENC_IS_MBC_NEWLINE(encode
, sprev
, end
)) {
2049 if (IS_NOTEOL(msa
->options
)) goto fail
;
2052 #ifndef USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE
2056 else if (ONIGENC_IS_MBC_NEWLINE(encode
, s
, end
) &&
2057 ON_STR_END(s
+ enclen(encode
, s
, end
))) {
2061 #ifdef USE_CRNL_AS_LINE_TERMINATOR
2062 else if (ONIGENC_IS_MBC_CRNL(encode
, s
, end
)) {
2063 UChar
* ss
= s
+ enclen(encode
, s
);
2064 ss
+= enclen(encode
, ss
);
2065 if (ON_STR_END(ss
)) {
2074 case OP_BEGIN_POSITION
: MOP_IN(OP_BEGIN_POSITION
);
2075 if (s
!= msa
->start
)
2082 case OP_MEMORY_START_PUSH
: MOP_IN(OP_MEMORY_START_PUSH
);
2083 GET_MEMNUM_INC(mem
, p
);
2084 STACK_PUSH_MEM_START(mem
, s
);
2089 case OP_MEMORY_START
: MOP_IN(OP_MEMORY_START
);
2090 GET_MEMNUM_INC(mem
, p
);
2091 mem_start_stk
[mem
] = (OnigStackIndex
)((void* )s
);
2096 case OP_MEMORY_END_PUSH
: MOP_IN(OP_MEMORY_END_PUSH
);
2097 GET_MEMNUM_INC(mem
, p
);
2098 STACK_PUSH_MEM_END(mem
, s
);
2103 case OP_MEMORY_END
: MOP_IN(OP_MEMORY_END
);
2104 GET_MEMNUM_INC(mem
, p
);
2105 mem_end_stk
[mem
] = (OnigStackIndex
)((void* )s
);
2110 #ifdef USE_SUBEXP_CALL
2111 case OP_MEMORY_END_PUSH_REC
: MOP_IN(OP_MEMORY_END_PUSH_REC
);
2112 GET_MEMNUM_INC(mem
, p
);
2113 STACK_GET_MEM_START(mem
, stkp
); /* should be before push mem-end. */
2114 STACK_PUSH_MEM_END(mem
, s
);
2115 mem_start_stk
[mem
] = GET_STACK_INDEX(stkp
);
2120 case OP_MEMORY_END_REC
: MOP_IN(OP_MEMORY_END_REC
);
2121 GET_MEMNUM_INC(mem
, p
);
2122 mem_end_stk
[mem
] = (OnigStackIndex
)((void* )s
);
2123 STACK_GET_MEM_START(mem
, stkp
);
2125 if (BIT_STATUS_AT(reg
->bt_mem_start
, mem
))
2126 mem_start_stk
[mem
] = GET_STACK_INDEX(stkp
);
2128 mem_start_stk
[mem
] = (OnigStackIndex
)((void* )stkp
->u
.mem
.pstr
);
2130 STACK_PUSH_MEM_END_MARK(mem
);
2136 case OP_BACKREF1
: MOP_IN(OP_BACKREF1
);
2141 case OP_BACKREF2
: MOP_IN(OP_BACKREF2
);
2146 case OP_BACKREFN
: MOP_IN(OP_BACKREFN
);
2147 GET_MEMNUM_INC(mem
, p
);
2151 UChar
*pstart
, *pend
;
2153 /* if you want to remove following line,
2154 you should check in parse and compile time. */
2155 if (mem
> num_mem
) goto fail
;
2156 if (mem_end_stk
[mem
] == INVALID_STACK_INDEX
) goto fail
;
2157 if (mem_start_stk
[mem
] == INVALID_STACK_INDEX
) goto fail
;
2159 if (BIT_STATUS_AT(reg
->bt_mem_start
, mem
))
2160 pstart
= STACK_AT(mem_start_stk
[mem
])->u
.mem
.pstr
;
2162 pstart
= (UChar
* )((void* )mem_start_stk
[mem
]);
2164 pend
= (BIT_STATUS_AT(reg
->bt_mem_end
, mem
)
2165 ? STACK_AT(mem_end_stk
[mem
])->u
.mem
.pstr
2166 : (UChar
* )((void* )mem_end_stk
[mem
]));
2170 STRING_CMP(pstart
, s
, n
);
2171 while (sprev
+ (len
= enclen(encode
, sprev
, end
)) < s
)
2179 case OP_BACKREFN_IC
: MOP_IN(OP_BACKREFN_IC
);
2180 GET_MEMNUM_INC(mem
, p
);
2183 UChar
*pstart
, *pend
;
2185 /* if you want to remove following line,
2186 you should check in parse and compile time. */
2187 if (mem
> num_mem
) goto fail
;
2188 if (mem_end_stk
[mem
] == INVALID_STACK_INDEX
) goto fail
;
2189 if (mem_start_stk
[mem
] == INVALID_STACK_INDEX
) goto fail
;
2191 if (BIT_STATUS_AT(reg
->bt_mem_start
, mem
))
2192 pstart
= STACK_AT(mem_start_stk
[mem
])->u
.mem
.pstr
;
2194 pstart
= (UChar
* )((void* )mem_start_stk
[mem
]);
2196 pend
= (BIT_STATUS_AT(reg
->bt_mem_end
, mem
)
2197 ? STACK_AT(mem_end_stk
[mem
])->u
.mem
.pstr
2198 : (UChar
* )((void* )mem_end_stk
[mem
]));
2202 STRING_CMP_IC(case_fold_flag
, pstart
, &s
, n
);
2203 while (sprev
+ (len
= enclen(encode
, sprev
, end
)) < s
)
2211 case OP_BACKREF_MULTI
: MOP_IN(OP_BACKREF_MULTI
);
2214 UChar
*pstart
, *pend
, *swork
;
2216 GET_LENGTH_INC(tlen
, p
);
2217 for (i
= 0; i
< tlen
; i
++) {
2218 GET_MEMNUM_INC(mem
, p
);
2220 if (mem_end_stk
[mem
] == INVALID_STACK_INDEX
) continue;
2221 if (mem_start_stk
[mem
] == INVALID_STACK_INDEX
) continue;
2223 if (BIT_STATUS_AT(reg
->bt_mem_start
, mem
))
2224 pstart
= STACK_AT(mem_start_stk
[mem
])->u
.mem
.pstr
;
2226 pstart
= (UChar
* )((void* )mem_start_stk
[mem
]);
2228 pend
= (BIT_STATUS_AT(reg
->bt_mem_end
, mem
)
2229 ? STACK_AT(mem_end_stk
[mem
])->u
.mem
.pstr
2230 : (UChar
* )((void* )mem_end_stk
[mem
]));
2235 STRING_CMP_VALUE(pstart
, swork
, n
, is_fail
);
2236 if (is_fail
) continue;
2238 while (sprev
+ (len
= enclen(encode
, sprev
, end
)) < s
)
2241 p
+= (SIZE_MEMNUM
* (tlen
- i
- 1));
2242 break; /* success */
2244 if (i
== tlen
) goto fail
;
2250 case OP_BACKREF_MULTI_IC
: MOP_IN(OP_BACKREF_MULTI_IC
);
2253 UChar
*pstart
, *pend
, *swork
;
2255 GET_LENGTH_INC(tlen
, p
);
2256 for (i
= 0; i
< tlen
; i
++) {
2257 GET_MEMNUM_INC(mem
, p
);
2259 if (mem_end_stk
[mem
] == INVALID_STACK_INDEX
) continue;
2260 if (mem_start_stk
[mem
] == INVALID_STACK_INDEX
) continue;
2262 if (BIT_STATUS_AT(reg
->bt_mem_start
, mem
))
2263 pstart
= STACK_AT(mem_start_stk
[mem
])->u
.mem
.pstr
;
2265 pstart
= (UChar
* )((void* )mem_start_stk
[mem
]);
2267 pend
= (BIT_STATUS_AT(reg
->bt_mem_end
, mem
)
2268 ? STACK_AT(mem_end_stk
[mem
])->u
.mem
.pstr
2269 : (UChar
* )((void* )mem_end_stk
[mem
]));
2274 STRING_CMP_VALUE_IC(case_fold_flag
, pstart
, &swork
, n
, is_fail
);
2275 if (is_fail
) continue;
2277 while (sprev
+ (len
= enclen(encode
, sprev
, end
)) < s
)
2280 p
+= (SIZE_MEMNUM
* (tlen
- i
- 1));
2281 break; /* success */
2283 if (i
== tlen
) goto fail
;
2289 #ifdef USE_BACKREF_WITH_LEVEL
2290 case OP_BACKREF_WITH_LEVEL
:
2296 GET_OPTION_INC(ic
, p
);
2297 GET_LENGTH_INC(level
, p
);
2298 GET_LENGTH_INC(tlen
, p
);
2301 if (backref_match_at_nested_level(reg
, stk
, stk_base
, ic
2302 , case_fold_flag
, (int )level
, (int )tlen
, p
, &s
, end
)) {
2303 while (sprev
+ (len
= enclen(encode
, sprev
, end
)) < s
)
2306 p
+= (SIZE_MEMNUM
* tlen
);
2318 #if 0 /* no need: IS_DYNAMIC_OPTION() == 0 */
2319 case OP_SET_OPTION_PUSH
: MOP_IN(OP_SET_OPTION_PUSH
);
2320 GET_OPTION_INC(option
, p
);
2321 STACK_PUSH_ALT(p
, s
, sprev
);
2322 p
+= SIZE_OP_SET_OPTION
+ SIZE_OP_FAIL
;
2327 case OP_SET_OPTION
: MOP_IN(OP_SET_OPTION
);
2328 GET_OPTION_INC(option
, p
);
2334 case OP_NULL_CHECK_START
: MOP_IN(OP_NULL_CHECK_START
);
2335 GET_MEMNUM_INC(mem
, p
); /* mem: null check id */
2336 STACK_PUSH_NULL_CHECK_START(mem
, s
);
2341 case OP_NULL_CHECK_END
: MOP_IN(OP_NULL_CHECK_END
);
2345 GET_MEMNUM_INC(mem
, p
); /* mem: null check id */
2346 STACK_NULL_CHECK(isnull
, mem
, s
);
2348 #ifdef ONIG_DEBUG_MATCH
2349 fprintf(stderr
, "NULL_CHECK_END: skip id:%d, s:%d\n",
2350 (int )mem
, (int )s
);
2353 /* empty loop founded, skip next instruction */
2360 case OP_REPEAT_INC_NG
:
2361 case OP_REPEAT_INC_SG
:
2362 case OP_REPEAT_INC_NG_SG
:
2366 goto unexpected_bytecode_error
;
2375 #ifdef USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT
2376 case OP_NULL_CHECK_END_MEMST
: MOP_IN(OP_NULL_CHECK_END_MEMST
);
2380 GET_MEMNUM_INC(mem
, p
); /* mem: null check id */
2381 STACK_NULL_CHECK_MEMST(isnull
, mem
, s
, reg
);
2383 #ifdef ONIG_DEBUG_MATCH
2384 fprintf(stderr
, "NULL_CHECK_END_MEMST: skip id:%d, s:%d\n",
2385 (int )mem
, (int )s
);
2387 if (isnull
== -1) goto fail
;
2388 goto null_check_found
;
2396 #ifdef USE_SUBEXP_CALL
2397 case OP_NULL_CHECK_END_MEMST_PUSH
:
2398 MOP_IN(OP_NULL_CHECK_END_MEMST_PUSH
);
2402 GET_MEMNUM_INC(mem
, p
); /* mem: null check id */
2403 #ifdef USE_MONOMANIAC_CHECK_CAPTURES_IN_ENDLESS_REPEAT
2404 STACK_NULL_CHECK_MEMST_REC(isnull
, mem
, s
, reg
);
2406 STACK_NULL_CHECK_REC(isnull
, mem
, s
);
2409 #ifdef ONIG_DEBUG_MATCH
2410 fprintf(stderr
, "NULL_CHECK_END_MEMST_PUSH: skip id:%d, s:%d\n",
2411 (int )mem
, (int )s
);
2413 if (isnull
== -1) goto fail
;
2414 goto null_check_found
;
2417 STACK_PUSH_NULL_CHECK_END(mem
);
2425 case OP_JUMP
: MOP_IN(OP_JUMP
);
2426 GET_RELADDR_INC(addr
, p
);
2429 CHECK_INTERRUPT_IN_MATCH_AT
;
2433 case OP_PUSH
: MOP_IN(OP_PUSH
);
2434 GET_RELADDR_INC(addr
, p
);
2435 STACK_PUSH_ALT(p
+ addr
, s
, sprev
);
2440 #ifdef USE_COMBINATION_EXPLOSION_CHECK
2441 case OP_STATE_CHECK_PUSH
: MOP_IN(OP_STATE_CHECK_PUSH
);
2442 GET_STATE_CHECK_NUM_INC(mem
, p
);
2443 STATE_CHECK_VAL(scv
, mem
);
2446 GET_RELADDR_INC(addr
, p
);
2447 STACK_PUSH_ALT_WITH_STATE_CHECK(p
+ addr
, s
, sprev
, mem
);
2452 case OP_STATE_CHECK_PUSH_OR_JUMP
: MOP_IN(OP_STATE_CHECK_PUSH_OR_JUMP
);
2453 GET_STATE_CHECK_NUM_INC(mem
, p
);
2454 GET_RELADDR_INC(addr
, p
);
2455 STATE_CHECK_VAL(scv
, mem
);
2460 STACK_PUSH_ALT_WITH_STATE_CHECK(p
+ addr
, s
, sprev
, mem
);
2466 case OP_STATE_CHECK
: MOP_IN(OP_STATE_CHECK
);
2467 GET_STATE_CHECK_NUM_INC(mem
, p
);
2468 STATE_CHECK_VAL(scv
, mem
);
2471 STACK_PUSH_STATE_CHECK(s
, mem
);
2475 #endif /* USE_COMBINATION_EXPLOSION_CHECK */
2477 case OP_POP
: MOP_IN(OP_POP
);
2483 case OP_PUSH_OR_JUMP_EXACT1
: MOP_IN(OP_PUSH_OR_JUMP_EXACT1
);
2484 GET_RELADDR_INC(addr
, p
);
2485 if (*p
== *s
&& DATA_ENSURE_CHECK1
) {
2487 STACK_PUSH_ALT(p
+ addr
, s
, sprev
);
2496 case OP_PUSH_IF_PEEK_NEXT
: MOP_IN(OP_PUSH_IF_PEEK_NEXT
);
2497 GET_RELADDR_INC(addr
, p
);
2500 STACK_PUSH_ALT(p
+ addr
, s
, sprev
);
2509 case OP_REPEAT
: MOP_IN(OP_REPEAT
);
2511 GET_MEMNUM_INC(mem
, p
); /* mem: OP_REPEAT ID */
2512 GET_RELADDR_INC(addr
, p
);
2515 repeat_stk
[mem
] = GET_STACK_INDEX(stk
);
2516 STACK_PUSH_REPEAT(mem
, p
);
2518 if (reg
->repeat_range
[mem
].lower
== 0) {
2519 STACK_PUSH_ALT(p
+ addr
, s
, sprev
);
2526 case OP_REPEAT_NG
: MOP_IN(OP_REPEAT_NG
);
2528 GET_MEMNUM_INC(mem
, p
); /* mem: OP_REPEAT ID */
2529 GET_RELADDR_INC(addr
, p
);
2532 repeat_stk
[mem
] = GET_STACK_INDEX(stk
);
2533 STACK_PUSH_REPEAT(mem
, p
);
2535 if (reg
->repeat_range
[mem
].lower
== 0) {
2536 STACK_PUSH_ALT(p
, s
, sprev
);
2544 case OP_REPEAT_INC
: MOP_IN(OP_REPEAT_INC
);
2545 GET_MEMNUM_INC(mem
, p
); /* mem: OP_REPEAT ID */
2546 si
= repeat_stk
[mem
];
2547 stkp
= STACK_AT(si
);
2550 stkp
->u
.repeat
.count
++;
2551 if (stkp
->u
.repeat
.count
>= reg
->repeat_range
[mem
].upper
) {
2552 /* end of repeat. Nothing to do. */
2554 else if (stkp
->u
.repeat
.count
>= reg
->repeat_range
[mem
].lower
) {
2555 STACK_PUSH_ALT(p
, s
, sprev
);
2556 p
= STACK_AT(si
)->u
.repeat
.pcode
; /* Don't use stkp after PUSH. */
2559 p
= stkp
->u
.repeat
.pcode
;
2561 STACK_PUSH_REPEAT_INC(si
);
2563 CHECK_INTERRUPT_IN_MATCH_AT
;
2567 case OP_REPEAT_INC_SG
: MOP_IN(OP_REPEAT_INC_SG
);
2568 GET_MEMNUM_INC(mem
, p
); /* mem: OP_REPEAT ID */
2569 STACK_GET_REPEAT(mem
, stkp
);
2570 si
= GET_STACK_INDEX(stkp
);
2574 case OP_REPEAT_INC_NG
: MOP_IN(OP_REPEAT_INC_NG
);
2575 GET_MEMNUM_INC(mem
, p
); /* mem: OP_REPEAT ID */
2576 si
= repeat_stk
[mem
];
2577 stkp
= STACK_AT(si
);
2580 stkp
->u
.repeat
.count
++;
2581 if (stkp
->u
.repeat
.count
< reg
->repeat_range
[mem
].upper
) {
2582 if (stkp
->u
.repeat
.count
>= reg
->repeat_range
[mem
].lower
) {
2583 UChar
* pcode
= stkp
->u
.repeat
.pcode
;
2585 STACK_PUSH_REPEAT_INC(si
);
2586 STACK_PUSH_ALT(pcode
, s
, sprev
);
2589 p
= stkp
->u
.repeat
.pcode
;
2590 STACK_PUSH_REPEAT_INC(si
);
2593 else if (stkp
->u
.repeat
.count
== reg
->repeat_range
[mem
].upper
) {
2594 STACK_PUSH_REPEAT_INC(si
);
2597 CHECK_INTERRUPT_IN_MATCH_AT
;
2601 case OP_REPEAT_INC_NG_SG
: MOP_IN(OP_REPEAT_INC_NG_SG
);
2602 GET_MEMNUM_INC(mem
, p
); /* mem: OP_REPEAT ID */
2603 STACK_GET_REPEAT(mem
, stkp
);
2604 si
= GET_STACK_INDEX(stkp
);
2608 case OP_PUSH_POS
: MOP_IN(OP_PUSH_POS
);
2609 STACK_PUSH_POS(s
, sprev
);
2614 case OP_POP_POS
: MOP_IN(OP_POP_POS
);
2616 STACK_POS_END(stkp
);
2617 s
= stkp
->u
.state
.pstr
;
2618 sprev
= stkp
->u
.state
.pstr_prev
;
2624 case OP_PUSH_POS_NOT
: MOP_IN(OP_PUSH_POS_NOT
);
2625 GET_RELADDR_INC(addr
, p
);
2626 STACK_PUSH_POS_NOT(p
+ addr
, s
, sprev
);
2631 case OP_FAIL_POS
: MOP_IN(OP_FAIL_POS
);
2632 STACK_POP_TIL_POS_NOT
;
2636 case OP_PUSH_STOP_BT
: MOP_IN(OP_PUSH_STOP_BT
);
2642 case OP_POP_STOP_BT
: MOP_IN(OP_POP_STOP_BT
);
2648 case OP_LOOK_BEHIND
: MOP_IN(OP_LOOK_BEHIND
);
2649 GET_LENGTH_INC(tlen
, p
);
2650 s
= (UChar
* )ONIGENC_STEP_BACK(encode
, str
, s
, (int )tlen
);
2651 if (IS_NULL(s
)) goto fail
;
2652 sprev
= (UChar
* )onigenc_get_prev_char_head(encode
, str
, s
);
2657 case OP_PUSH_LOOK_BEHIND_NOT
: MOP_IN(OP_PUSH_LOOK_BEHIND_NOT
);
2658 GET_RELADDR_INC(addr
, p
);
2659 GET_LENGTH_INC(tlen
, p
);
2660 q
= (UChar
* )ONIGENC_STEP_BACK(encode
, str
, s
, (int )tlen
);
2662 /* too short case -> success. ex. /(?<!XXX)a/.match("a")
2663 If you want to change to fail, replace following line. */
2668 STACK_PUSH_LOOK_BEHIND_NOT(p
+ addr
, s
, sprev
);
2670 sprev
= (UChar
* )onigenc_get_prev_char_head(encode
, str
, s
);
2676 case OP_FAIL_LOOK_BEHIND_NOT
: MOP_IN(OP_FAIL_LOOK_BEHIND_NOT
);
2677 STACK_POP_TIL_LOOK_BEHIND_NOT
;
2681 #ifdef USE_SUBEXP_CALL
2682 case OP_CALL
: MOP_IN(OP_CALL
);
2683 GET_ABSADDR_INC(addr
, p
);
2684 STACK_PUSH_CALL_FRAME(p
);
2690 case OP_RETURN
: MOP_IN(OP_RETURN
);
2705 case OP_FAIL
: MOP_IN(OP_FAIL
);
2707 p
= stk
->u
.state
.pcode
;
2708 s
= stk
->u
.state
.pstr
;
2709 sprev
= stk
->u
.state
.pstr_prev
;
2711 #ifdef USE_COMBINATION_EXPLOSION_CHECK
2712 if (stk
->u
.state
.state_check
!= 0) {
2713 stk
->type
= STK_STATE_CHECK_MARK
;
2723 goto bytecode_error
;
2725 } /* end of switch */
2727 } /* end of while(1) */
2736 return ONIGERR_STACK_BUG
;
2741 return ONIGERR_UNDEFINED_BYTECODE
;
2743 unexpected_bytecode_error
:
2745 return ONIGERR_UNEXPECTED_BYTECODE
;
2750 slow_search(OnigEncoding enc
, UChar
* target
, UChar
* target_end
,
2751 const UChar
* text
, const UChar
* text_end
, UChar
* text_range
)
2753 UChar
*t
, *p
, *s
, *end
;
2755 end
= (UChar
* )text_end
;
2756 end
-= target_end
- target
- 1;
2757 if (end
> text_range
)
2762 if (enc
->max_enc_len
== enc
->min_enc_len
) {
2763 int n
= enc
->max_enc_len
;
2766 if (*s
== *target
) {
2769 if (target_end
== t
|| memcmp(t
, p
, target_end
- t
) == 0)
2774 return (UChar
*)NULL
;
2777 if (*s
== *target
) {
2780 if (target_end
== t
|| memcmp(t
, p
, target_end
- t
) == 0)
2783 s
+= enclen(enc
, s
, end
);
2786 return (UChar
* )NULL
;
2790 str_lower_case_match(OnigEncoding enc
, int case_fold_flag
,
2791 const UChar
* t
, const UChar
* tend
,
2792 const UChar
* p
, const UChar
* end
)
2795 UChar
*q
, lowbuf
[ONIGENC_MBC_CASE_FOLD_MAXLEN
];
2798 lowlen
= ONIGENC_MBC_CASE_FOLD(enc
, case_fold_flag
, &p
, end
, lowbuf
);
2800 while (lowlen
> 0) {
2801 if (*t
++ != *q
++) return 0;
2810 slow_search_ic(OnigEncoding enc
, int case_fold_flag
,
2811 UChar
* target
, UChar
* target_end
,
2812 const UChar
* text
, const UChar
* text_end
, UChar
* text_range
)
2816 end
= (UChar
* )text_end
;
2817 end
-= target_end
- target
- 1;
2818 if (end
> text_range
)
2824 if (str_lower_case_match(enc
, case_fold_flag
, target
, target_end
,
2828 s
+= enclen(enc
, s
, text_end
);
2831 return (UChar
* )NULL
;
2835 slow_search_backward(OnigEncoding enc
, UChar
* target
, UChar
* target_end
,
2836 const UChar
* text
, const UChar
* adjust_text
,
2837 const UChar
* text_end
, const UChar
* text_start
)
2841 s
= (UChar
* )text_end
;
2842 s
-= (target_end
- target
);
2844 s
= (UChar
* )text_start
;
2846 s
= ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc
, adjust_text
, s
);
2849 if (*s
== *target
) {
2852 while (t
< target_end
) {
2857 if (t
== target_end
)
2860 s
= (UChar
* )onigenc_get_prev_char_head(enc
, adjust_text
, s
);
2863 return (UChar
* )NULL
;
2867 slow_search_backward_ic(OnigEncoding enc
, int case_fold_flag
,
2868 UChar
* target
, UChar
* target_end
,
2869 const UChar
* text
, const UChar
* adjust_text
,
2870 const UChar
* text_end
, const UChar
* text_start
)
2874 s
= (UChar
* )text_end
;
2875 s
-= (target_end
- target
);
2877 s
= (UChar
* )text_start
;
2879 s
= ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc
, adjust_text
, s
);
2882 if (str_lower_case_match(enc
, case_fold_flag
,
2883 target
, target_end
, s
, text_end
))
2886 s
= (UChar
* )onigenc_get_prev_char_head(enc
, adjust_text
, s
);
2889 return (UChar
* )NULL
;
2893 bm_search_notrev(regex_t
* reg
, const UChar
* target
, const UChar
* target_end
,
2894 const UChar
* text
, const UChar
* text_end
,
2895 const UChar
* text_range
)
2897 const UChar
*s
, *se
, *t
, *p
, *end
;
2901 #ifdef ONIG_DEBUG_SEARCH
2902 fprintf(stderr
, "bm_search_notrev: text: %d, text_end: %d, text_range: %d\n",
2903 (int )text
, (int )text_end
, (int )text_range
);
2906 tail
= target_end
- 1;
2907 tlen1
= tail
- target
;
2909 if (end
+ tlen1
> text_end
)
2910 end
= text_end
- tlen1
;
2914 if (IS_NULL(reg
->int_map
)) {
2918 while (t
>= target
&& *p
== *t
) {
2921 if (t
< target
) return (UChar
* )s
;
2923 skip
= reg
->map
[*se
];
2926 s
+= enclen(reg
->enc
, s
, end
);
2927 } while ((s
- t
) < skip
&& s
< end
);
2934 while (t
>= target
&& *p
== *t
) {
2937 if (t
< target
) return (UChar
* )s
;
2939 skip
= reg
->int_map
[*se
];
2942 s
+= enclen(reg
->enc
, s
, end
);
2943 } while ((s
- t
) < skip
&& s
< end
);
2947 return (UChar
* )NULL
;
2951 bm_search(regex_t
* reg
, const UChar
* target
, const UChar
* target_end
,
2952 const UChar
* text
, const UChar
* text_end
, const UChar
* text_range
)
2954 const UChar
*s
, *t
, *p
, *end
;
2957 end
= text_range
+ (target_end
- target
) - 1;
2961 tail
= target_end
- 1;
2962 s
= text
+ (target_end
- target
) - 1;
2963 if (IS_NULL(reg
->int_map
)) {
2967 while (t
>= target
&& *p
== *t
) {
2970 if (t
< target
) return (UChar
* )(p
+ 1);
2974 else { /* see int_map[] */
2978 while (t
>= target
&& *p
== *t
) {
2981 if (t
< target
) return (UChar
* )(p
+ 1);
2982 s
+= reg
->int_map
[*s
];
2985 return (UChar
* )NULL
;
2989 set_bm_backward_skip(UChar
* s
, UChar
* end
, OnigEncoding enc ARG_UNUSED
,
2995 if (IS_NULL(*skip
)) {
2996 *skip
= (int* )xmalloc(sizeof(int) * ONIG_CHAR_TABLE_SIZE
);
2997 if (IS_NULL(*skip
)) return ONIGERR_MEMORY
;
3001 for (i
= 0; i
< ONIG_CHAR_TABLE_SIZE
; i
++)
3004 for (i
= len
- 1; i
> 0; i
--)
3011 bm_search_backward(regex_t
* reg
, const UChar
* target
, const UChar
* target_end
,
3012 const UChar
* text
, const UChar
* adjust_text
,
3013 const UChar
* text_end
, const UChar
* text_start
)
3015 const UChar
*s
, *t
, *p
;
3017 s
= text_end
- (target_end
- target
);
3021 s
= ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg
->enc
, adjust_text
, s
);
3026 while (t
< target_end
&& *p
== *t
) {
3029 if (t
== target_end
)
3032 s
-= reg
->int_map_backward
[*s
];
3033 s
= ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg
->enc
, adjust_text
, s
);
3036 return (UChar
* )NULL
;
3040 map_search(OnigEncoding enc
, UChar map
[],
3041 const UChar
* text
, const UChar
* text_range
)
3043 const UChar
*s
= text
;
3045 while (s
< text_range
) {
3046 if (map
[*s
]) return (UChar
* )s
;
3048 s
+= enclen(enc
, s
, text_range
);
3050 return (UChar
* )NULL
;
3054 map_search_backward(OnigEncoding enc
, UChar map
[],
3055 const UChar
* text
, const UChar
* adjust_text
,
3056 const UChar
* text_start
)
3058 const UChar
*s
= text_start
;
3061 if (map
[*s
]) return (UChar
* )s
;
3063 s
= onigenc_get_prev_char_head(enc
, adjust_text
, s
);
3065 return (UChar
* )NULL
;
3069 onig_match(regex_t
* reg
, const UChar
* str
, const UChar
* end
, const UChar
* at
, OnigRegion
* region
,
3070 OnigOptionType option
)
3076 #if defined(USE_RECOMPILE_API) && defined(USE_MULTI_THREAD_SYSTEM)
3078 THREAD_ATOMIC_START
;
3079 if (ONIG_STATE(reg
) >= ONIG_STATE_NORMAL
) {
3080 ONIG_STATE_INC(reg
);
3081 if (IS_NOT_NULL(reg
->chain
) && ONIG_STATE(reg
) == ONIG_STATE_NORMAL
) {
3082 onig_chain_reduce(reg
);
3083 ONIG_STATE_INC(reg
);
3091 while (ONIG_STATE(reg
) < ONIG_STATE_NORMAL
) {
3092 if (++n
> THREAD_PASS_LIMIT_COUNT
)
3093 return ONIGERR_OVER_THREAD_PASS_LIMIT_COUNT
;
3099 #endif /* USE_RECOMPILE_API && USE_MULTI_THREAD_SYSTEM */
3101 MATCH_ARG_INIT(msa
, option
, region
, at
);
3102 #ifdef USE_COMBINATION_EXPLOSION_CHECK
3104 int offset
= at
- str
;
3105 STATE_CHECK_BUFF_INIT(msa
, end
- str
, offset
, reg
->num_comb_exp_check
);
3110 #ifdef USE_POSIX_API_REGION_OPTION
3111 && !IS_POSIX_REGION(option
)
3114 r
= onig_region_resize_clear(region
, reg
->num_mem
+ 1);
3120 prev
= (UChar
* )onigenc_get_prev_char_head(reg
->enc
, str
, at
);
3121 r
= match_at(reg
, str
, end
,
3122 #ifdef USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE
3128 MATCH_ARG_FREE(msa
);
3129 ONIG_STATE_DEC_THREAD(reg
);
3134 forward_search_range(regex_t
* reg
, const UChar
* str
, const UChar
* end
, UChar
* s
,
3135 UChar
* range
, UChar
** low
, UChar
** high
, UChar
** low_prev
)
3137 UChar
*p
, *pprev
= (UChar
* )NULL
;
3139 #ifdef ONIG_DEBUG_SEARCH
3140 fprintf(stderr
, "forward_search_range: str: %d, end: %d, s: %d, range: %d\n",
3141 (int )str
, (int )end
, (int )s
, (int )range
);
3145 if (reg
->dmin
> 0) {
3146 if (ONIGENC_IS_SINGLEBYTE(reg
->enc
)) {
3150 UChar
*q
= p
+ reg
->dmin
;
3151 while (p
< q
) p
+= enclen(reg
->enc
, p
, end
);
3156 switch (reg
->optimize
) {
3157 case ONIG_OPTIMIZE_EXACT
:
3158 p
= slow_search(reg
->enc
, reg
->exact
, reg
->exact_end
, p
, end
, range
);
3160 case ONIG_OPTIMIZE_EXACT_IC
:
3161 p
= slow_search_ic(reg
->enc
, reg
->case_fold_flag
,
3162 reg
->exact
, reg
->exact_end
, p
, end
, range
);
3165 case ONIG_OPTIMIZE_EXACT_BM
:
3166 p
= bm_search(reg
, reg
->exact
, reg
->exact_end
, p
, end
, range
);
3169 case ONIG_OPTIMIZE_EXACT_BM_NOT_REV
:
3170 p
= bm_search_notrev(reg
, reg
->exact
, reg
->exact_end
, p
, end
, range
);
3173 case ONIG_OPTIMIZE_MAP
:
3174 p
= map_search(reg
->enc
, reg
->map
, p
, range
);
3178 if (p
&& p
< range
) {
3179 if (p
- reg
->dmin
< s
) {
3182 p
+= enclen(reg
->enc
, p
, end
);
3186 if (reg
->sub_anchor
) {
3189 switch (reg
->sub_anchor
) {
3190 case ANCHOR_BEGIN_LINE
:
3191 if (!ON_STR_BEGIN(p
)) {
3192 prev
= onigenc_get_prev_char_head(reg
->enc
,
3193 (pprev
? pprev
: str
), p
);
3194 if (!ONIGENC_IS_MBC_NEWLINE(reg
->enc
, prev
, end
))
3199 case ANCHOR_END_LINE
:
3200 if (ON_STR_END(p
)) {
3201 #ifndef USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE
3202 prev
= (UChar
* )onigenc_get_prev_char_head(reg
->enc
,
3203 (pprev
? pprev
: str
), p
);
3204 if (prev
&& ONIGENC_IS_MBC_NEWLINE(reg
->enc
, prev
, end
))
3208 else if (! ONIGENC_IS_MBC_NEWLINE(reg
->enc
, p
, end
)
3209 #ifdef USE_CRNL_AS_LINE_TERMINATOR
3210 && ! ONIGENC_IS_MBC_CRNL(reg
->enc
, p
, end
)
3218 if (reg
->dmax
== 0) {
3222 *low_prev
= onigenc_get_prev_char_head(reg
->enc
, s
, p
);
3224 *low_prev
= onigenc_get_prev_char_head(reg
->enc
,
3225 (pprev
? pprev
: str
), p
);
3229 if (reg
->dmax
!= ONIG_INFINITE_DISTANCE
) {
3230 *low
= p
- reg
->dmax
;
3232 *low
= onigenc_get_right_adjust_char_head_with_prev(reg
->enc
, s
,
3233 *low
, (const UChar
** )low_prev
);
3234 if (low_prev
&& IS_NULL(*low_prev
))
3235 *low_prev
= onigenc_get_prev_char_head(reg
->enc
,
3236 (pprev
? pprev
: s
), *low
);
3240 *low_prev
= onigenc_get_prev_char_head(reg
->enc
,
3241 (pprev
? pprev
: str
), *low
);
3245 /* no needs to adjust *high, *high is used as range check only */
3246 *high
= p
- reg
->dmin
;
3248 #ifdef ONIG_DEBUG_SEARCH
3250 "forward_search_range success: low: %d, high: %d, dmin: %d, dmax: %d\n",
3251 (int )(*low
- str
), (int )(*high
- str
), reg
->dmin
, reg
->dmax
);
3253 return 1; /* success */
3256 return 0; /* fail */
3259 static int set_bm_backward_skip
P_((UChar
* s
, UChar
* end
, OnigEncoding enc
,
3262 #define BM_BACKWARD_SEARCH_LENGTH_THRESHOLD 100
3265 backward_search_range(regex_t
* reg
, const UChar
* str
, const UChar
* end
,
3266 UChar
* s
, const UChar
* range
, UChar
* adjrange
,
3267 UChar
** low
, UChar
** high
)
3276 switch (reg
->optimize
) {
3277 case ONIG_OPTIMIZE_EXACT
:
3279 p
= slow_search_backward(reg
->enc
, reg
->exact
, reg
->exact_end
,
3280 range
, adjrange
, end
, p
);
3283 case ONIG_OPTIMIZE_EXACT_IC
:
3284 p
= slow_search_backward_ic(reg
->enc
, reg
->case_fold_flag
,
3285 reg
->exact
, reg
->exact_end
,
3286 range
, adjrange
, end
, p
);
3289 case ONIG_OPTIMIZE_EXACT_BM
:
3290 case ONIG_OPTIMIZE_EXACT_BM_NOT_REV
:
3291 if (IS_NULL(reg
->int_map_backward
)) {
3292 if (s
- range
< BM_BACKWARD_SEARCH_LENGTH_THRESHOLD
)
3295 r
= set_bm_backward_skip(reg
->exact
, reg
->exact_end
, reg
->enc
,
3296 &(reg
->int_map_backward
));
3299 p
= bm_search_backward(reg
, reg
->exact
, reg
->exact_end
, range
, adjrange
,
3303 case ONIG_OPTIMIZE_MAP
:
3304 p
= map_search_backward(reg
->enc
, reg
->map
, range
, adjrange
, p
);
3309 if (reg
->sub_anchor
) {
3312 switch (reg
->sub_anchor
) {
3313 case ANCHOR_BEGIN_LINE
:
3314 if (!ON_STR_BEGIN(p
)) {
3315 prev
= onigenc_get_prev_char_head(reg
->enc
, str
, p
);
3316 if (!ONIGENC_IS_MBC_NEWLINE(reg
->enc
, prev
, end
)) {
3323 case ANCHOR_END_LINE
:
3324 if (ON_STR_END(p
)) {
3325 #ifndef USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE
3326 prev
= onigenc_get_prev_char_head(reg
->enc
, adjrange
, p
);
3327 if (IS_NULL(prev
)) goto fail
;
3328 if (ONIGENC_IS_MBC_NEWLINE(reg
->enc
, prev
, end
)) {
3334 else if (! ONIGENC_IS_MBC_NEWLINE(reg
->enc
, p
, end
)
3335 #ifdef USE_CRNL_AS_LINE_TERMINATOR
3336 && ! ONIGENC_IS_MBC_CRNL(reg
->enc
, p
, end
)
3339 p
= onigenc_get_prev_char_head(reg
->enc
, adjrange
, p
);
3340 if (IS_NULL(p
)) goto fail
;
3347 /* no needs to adjust *high, *high is used as range check only */
3348 if (reg
->dmax
!= ONIG_INFINITE_DISTANCE
) {
3349 *low
= p
- reg
->dmax
;
3350 *high
= p
- reg
->dmin
;
3351 *high
= onigenc_get_right_adjust_char_head(reg
->enc
, adjrange
, *high
);
3354 #ifdef ONIG_DEBUG_SEARCH
3355 fprintf(stderr
, "backward_search_range: low: %d, high: %d\n",
3356 (int )(*low
- str
), (int )(*high
- str
));
3358 return 1; /* success */
3362 #ifdef ONIG_DEBUG_SEARCH
3363 fprintf(stderr
, "backward_search_range: fail.\n");
3365 return 0; /* fail */
3370 onig_search(regex_t
* reg
, const UChar
* str
, const UChar
* end
,
3371 const UChar
* start
, const UChar
* range
, OnigRegion
* region
, OnigOptionType option
)
3376 const UChar
*orig_start
= start
;
3377 #ifdef USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE
3378 const UChar
*orig_range
= range
;
3381 #if defined(USE_RECOMPILE_API) && defined(USE_MULTI_THREAD_SYSTEM)
3383 THREAD_ATOMIC_START
;
3384 if (ONIG_STATE(reg
) >= ONIG_STATE_NORMAL
) {
3385 ONIG_STATE_INC(reg
);
3386 if (IS_NOT_NULL(reg
->chain
) && ONIG_STATE(reg
) == ONIG_STATE_NORMAL
) {
3387 onig_chain_reduce(reg
);
3388 ONIG_STATE_INC(reg
);
3396 while (ONIG_STATE(reg
) < ONIG_STATE_NORMAL
) {
3397 if (++n
> THREAD_PASS_LIMIT_COUNT
)
3398 return ONIGERR_OVER_THREAD_PASS_LIMIT_COUNT
;
3404 #endif /* USE_RECOMPILE_API && USE_MULTI_THREAD_SYSTEM */
3406 #ifdef ONIG_DEBUG_SEARCH
3408 "onig_search (entry point): str: %d, end: %d, start: %d, range: %d\n",
3409 (int )str
, (int )(end
- str
), (int )(start
- str
), (int )(range
- str
));
3413 #ifdef USE_POSIX_API_REGION_OPTION
3414 && !IS_POSIX_REGION(option
)
3417 r
= onig_region_resize_clear(region
, reg
->num_mem
+ 1);
3418 if (r
) goto finish_no_msa
;
3421 if (start
> end
|| start
< str
) goto mismatch_no_msa
;
3424 #ifdef USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE
3425 #ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
3426 #define MATCH_AND_RETURN_CHECK(upper_range) \
3427 r = match_at(reg, str, end, (upper_range), s, prev, &msa); \
3428 if (r != ONIG_MISMATCH) {\
3430 if (! IS_FIND_LONGEST(reg->options)) {\
3434 else goto finish; /* error */ \
3437 #define MATCH_AND_RETURN_CHECK(upper_range) \
3438 r = match_at(reg, str, end, (upper_range), s, prev, &msa); \
3439 if (r != ONIG_MISMATCH) {\
3443 else goto finish; /* error */ \
3445 #endif /* USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE */
3447 #ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
3448 #define MATCH_AND_RETURN_CHECK(none) \
3449 r = match_at(reg, str, end, s, prev, &msa);\
3450 if (r != ONIG_MISMATCH) {\
3452 if (! IS_FIND_LONGEST(reg->options)) {\
3456 else goto finish; /* error */ \
3459 #define MATCH_AND_RETURN_CHECK(none) \
3460 r = match_at(reg, str, end, s, prev, &msa);\
3461 if (r != ONIG_MISMATCH) {\
3465 else goto finish; /* error */ \
3467 #endif /* USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE */
3468 #endif /* USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE */
3471 /* anchor optimize: resume search range */
3472 if (reg
->anchor
!= 0 && str
< end
) {
3473 UChar
*min_semi_end
, *max_semi_end
;
3475 if (reg
->anchor
& ANCHOR_BEGIN_POSITION
) {
3476 /* search start-position only */
3483 else if (reg
->anchor
& ANCHOR_BEGIN_BUF
) {
3484 /* search str-position only */
3485 if (range
> start
) {
3486 if (start
!= str
) goto mismatch_no_msa
;
3495 goto mismatch_no_msa
;
3498 else if (reg
->anchor
& ANCHOR_END_BUF
) {
3499 min_semi_end
= max_semi_end
= (UChar
* )end
;
3502 if ((OnigDistance
)(max_semi_end
- str
) < reg
->anchor_dmin
)
3503 goto mismatch_no_msa
;
3505 if (range
> start
) {
3506 if ((OnigDistance
)(min_semi_end
- start
) > reg
->anchor_dmax
) {
3507 start
= min_semi_end
- reg
->anchor_dmax
;
3509 start
= onigenc_get_right_adjust_char_head(reg
->enc
, str
, start
);
3510 else { /* match with empty at end */
3511 start
= onigenc_get_prev_char_head(reg
->enc
, str
, end
);
3514 if ((OnigDistance
)(max_semi_end
- (range
- 1)) < reg
->anchor_dmin
) {
3515 range
= max_semi_end
- reg
->anchor_dmin
+ 1;
3518 if (start
>= range
) goto mismatch_no_msa
;
3521 if ((OnigDistance
)(min_semi_end
- range
) > reg
->anchor_dmax
) {
3522 range
= min_semi_end
- reg
->anchor_dmax
;
3524 if ((OnigDistance
)(max_semi_end
- start
) < reg
->anchor_dmin
) {
3525 start
= max_semi_end
- reg
->anchor_dmin
;
3526 start
= ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg
->enc
, str
, start
);
3528 if (range
> start
) goto mismatch_no_msa
;
3531 else if (reg
->anchor
& ANCHOR_SEMI_END_BUF
) {
3532 UChar
* pre_end
= ONIGENC_STEP_BACK(reg
->enc
, str
, end
, 1);
3534 max_semi_end
= (UChar
* )end
;
3535 if (ONIGENC_IS_MBC_NEWLINE(reg
->enc
, pre_end
, end
)) {
3536 min_semi_end
= pre_end
;
3538 #ifdef USE_CRNL_AS_LINE_TERMINATOR
3539 pre_end
= ONIGENC_STEP_BACK(reg
->enc
, str
, pre_end
, 1);
3540 if (IS_NOT_NULL(pre_end
) &&
3541 ONIGENC_IS_MBC_CRNL(reg
->enc
, pre_end
, end
)) {
3542 min_semi_end
= pre_end
;
3545 if (min_semi_end
> str
&& start
<= min_semi_end
) {
3550 min_semi_end
= (UChar
* )end
;
3554 else if ((reg
->anchor
& ANCHOR_ANYCHAR_STAR_ML
)) {
3555 goto begin_position
;
3558 else if (str
== end
) { /* empty string */
3559 static const UChar address_for_empty_string
[] = "";
3561 #ifdef ONIG_DEBUG_SEARCH
3562 fprintf(stderr
, "onig_search: empty string.\n");
3565 if (reg
->threshold_len
== 0) {
3566 start
= end
= str
= address_for_empty_string
;
3568 prev
= (UChar
* )NULL
;
3570 MATCH_ARG_INIT(msa
, option
, region
, start
);
3571 #ifdef USE_COMBINATION_EXPLOSION_CHECK
3572 msa
.state_check_buff
= (void* )0;
3573 msa
.state_check_buff_size
= 0; /* NO NEED, for valgrind */
3575 MATCH_AND_RETURN_CHECK(end
);
3578 goto mismatch_no_msa
;
3581 #ifdef ONIG_DEBUG_SEARCH
3582 fprintf(stderr
, "onig_search(apply anchor): end: %d, start: %d, range: %d\n",
3583 (int )(end
- str
), (int )(start
- str
), (int )(range
- str
));
3586 MATCH_ARG_INIT(msa
, option
, region
, orig_start
);
3587 #ifdef USE_COMBINATION_EXPLOSION_CHECK
3589 int offset
= (MIN(start
, range
) - str
);
3590 STATE_CHECK_BUFF_INIT(msa
, end
- str
, offset
, reg
->num_comb_exp_check
);
3595 if (range
> start
) { /* forward search */
3597 prev
= onigenc_get_prev_char_head(reg
->enc
, str
, s
);
3599 prev
= (UChar
* )NULL
;
3601 if (reg
->optimize
!= ONIG_OPTIMIZE_NONE
) {
3602 UChar
*sch_range
, *low
, *high
, *low_prev
;
3604 sch_range
= (UChar
* )range
;
3605 if (reg
->dmax
!= 0) {
3606 if (reg
->dmax
== ONIG_INFINITE_DISTANCE
)
3607 sch_range
= (UChar
* )end
;
3609 sch_range
+= reg
->dmax
;
3610 if (sch_range
> end
) sch_range
= (UChar
* )end
;
3614 if ((end
- start
) < reg
->threshold_len
)
3617 if (reg
->dmax
!= ONIG_INFINITE_DISTANCE
) {
3619 if (! forward_search_range(reg
, str
, end
, s
, sch_range
,
3620 &low
, &high
, &low_prev
)) goto mismatch
;
3626 MATCH_AND_RETURN_CHECK(orig_range
);
3628 s
+= enclen(reg
->enc
, s
, end
);
3630 } while (s
< range
);
3633 else { /* check only. */
3634 if (! forward_search_range(reg
, str
, end
, s
, sch_range
,
3635 &low
, &high
, (UChar
** )NULL
)) goto mismatch
;
3637 if ((reg
->anchor
& ANCHOR_ANYCHAR_STAR
) != 0) {
3639 MATCH_AND_RETURN_CHECK(orig_range
);
3641 s
+= enclen(reg
->enc
, s
, end
);
3643 while (!ONIGENC_IS_MBC_NEWLINE(reg
->enc
, prev
, end
) && s
< range
) {
3645 s
+= enclen(reg
->enc
, s
, end
);
3647 } while (s
< range
);
3654 MATCH_AND_RETURN_CHECK(orig_range
);
3656 s
+= enclen(reg
->enc
, s
, end
);
3657 } while (s
< range
);
3659 if (s
== range
) { /* because empty match with /$/. */
3660 MATCH_AND_RETURN_CHECK(orig_range
);
3663 else { /* backward search */
3664 #ifdef USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE
3665 if (orig_start
< end
)
3666 orig_start
+= enclen(reg
->enc
, orig_start
, end
); /* is upper range */
3669 if (reg
->optimize
!= ONIG_OPTIMIZE_NONE
) {
3670 UChar
*low
, *high
, *adjrange
, *sch_start
;
3673 adjrange
= ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg
->enc
, str
, range
);
3675 adjrange
= (UChar
* )end
;
3677 if (reg
->dmax
!= ONIG_INFINITE_DISTANCE
&&
3678 (end
- range
) >= reg
->threshold_len
) {
3680 sch_start
= s
+ reg
->dmax
;
3681 if (sch_start
> end
) sch_start
= (UChar
* )end
;
3682 if (backward_search_range(reg
, str
, end
, sch_start
, range
, adjrange
,
3690 prev
= onigenc_get_prev_char_head(reg
->enc
, str
, s
);
3691 MATCH_AND_RETURN_CHECK(orig_start
);
3694 } while (s
>= range
);
3697 else { /* check only. */
3698 if ((end
- range
) < reg
->threshold_len
) goto mismatch
;
3701 if (reg
->dmax
!= 0) {
3702 if (reg
->dmax
== ONIG_INFINITE_DISTANCE
)
3703 sch_start
= (UChar
* )end
;
3705 sch_start
+= reg
->dmax
;
3706 if (sch_start
> end
) sch_start
= (UChar
* )end
;
3708 sch_start
= ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg
->enc
,
3712 if (backward_search_range(reg
, str
, end
, sch_start
, range
, adjrange
,
3713 &low
, &high
) <= 0) goto mismatch
;
3718 prev
= onigenc_get_prev_char_head(reg
->enc
, str
, s
);
3719 MATCH_AND_RETURN_CHECK(orig_start
);
3721 } while (s
>= range
);
3725 #ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
3726 if (IS_FIND_LONGEST(reg
->options
)) {
3727 if (msa
.best_len
>= 0) {
3736 MATCH_ARG_FREE(msa
);
3737 ONIG_STATE_DEC_THREAD(reg
);
3739 /* If result is mismatch and no FIND_NOT_EMPTY option,
3740 then the region is not setted in match_at(). */
3741 if (IS_FIND_NOT_EMPTY(reg
->options
) && region
3742 #ifdef USE_POSIX_API_REGION_OPTION
3743 && !IS_POSIX_REGION(option
)
3746 onig_region_clear(region
);
3750 if (r
!= ONIG_MISMATCH
)
3751 fprintf(stderr
, "onig_search: error %d\n", r
);
3758 ONIG_STATE_DEC_THREAD(reg
);
3760 if (r
!= ONIG_MISMATCH
)
3761 fprintf(stderr
, "onig_search: error %d\n", r
);
3766 ONIG_STATE_DEC_THREAD(reg
);
3767 MATCH_ARG_FREE(msa
);
3772 onig_get_encoding(regex_t
* reg
)
3777 extern OnigOptionType
3778 onig_get_options(regex_t
* reg
)
3780 return reg
->options
;
3783 extern OnigCaseFoldType
3784 onig_get_case_fold_flag(regex_t
* reg
)
3786 return reg
->case_fold_flag
;
3789 extern const OnigSyntaxType
*
3790 onig_get_syntax(regex_t
* reg
)
3796 onig_number_of_captures(regex_t
* reg
)
3798 return reg
->num_mem
;
3802 onig_number_of_capture_histories(regex_t
* reg
)
3804 #ifdef USE_CAPTURE_HISTORY
3808 for (i
= 0; i
<= ONIG_MAX_CAPTURE_HISTORY_GROUP
; i
++) {
3809 if (BIT_STATUS_AT(reg
->capture_history
, i
) != 0)
3819 onig_copy_encoding(OnigEncoding to
, OnigEncoding from
)