1 /* Extended regular expression matching and search library.
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 static reg_errcode_t
match_ctx_init (re_match_context_t
*cache
, int eflags
,
22 int n
) internal_function
;
23 static void match_ctx_clean (re_match_context_t
*mctx
) internal_function
;
24 static void match_ctx_free (re_match_context_t
*cache
) internal_function
;
25 static reg_errcode_t
match_ctx_add_entry (re_match_context_t
*cache
, int node
,
26 int str_idx
, int from
, int to
)
28 static int search_cur_bkref_entry (re_match_context_t
*mctx
, int str_idx
)
30 static reg_errcode_t
match_ctx_add_subtop (re_match_context_t
*mctx
, int node
,
31 int str_idx
) internal_function
;
32 static re_sub_match_last_t
* match_ctx_add_sublast (re_sub_match_top_t
*subtop
,
33 int node
, int str_idx
)
35 static void sift_ctx_init (re_sift_context_t
*sctx
, re_dfastate_t
**sifted_sts
,
36 re_dfastate_t
**limited_sts
, int last_node
,
39 static reg_errcode_t
re_search_internal (const regex_t
*preg
,
40 const char *string
, int length
,
41 int start
, int range
, int stop
,
42 size_t nmatch
, regmatch_t pmatch
[],
43 int eflags
) internal_function
;
44 static int re_search_2_stub (struct re_pattern_buffer
*bufp
,
45 const char *string1
, int length1
,
46 const char *string2
, int length2
,
47 int start
, int range
, struct re_registers
*regs
,
48 int stop
, int ret_len
) internal_function
;
49 static int re_search_stub (struct re_pattern_buffer
*bufp
,
50 const char *string
, int length
, int start
,
51 int range
, int stop
, struct re_registers
*regs
,
52 int ret_len
) internal_function
;
53 static unsigned re_copy_regs (struct re_registers
*regs
, regmatch_t
*pmatch
,
54 int nregs
, int regs_allocated
) internal_function
;
55 static inline re_dfastate_t
*acquire_init_state_context
56 (reg_errcode_t
*err
, const re_match_context_t
*mctx
, int idx
)
57 __attribute ((always_inline
)) internal_function
;
58 static reg_errcode_t
prune_impossible_nodes (re_match_context_t
*mctx
)
60 static int check_matching (re_match_context_t
*mctx
, int fl_longest_match
,
63 static int check_halt_node_context (const re_dfa_t
*dfa
, int node
,
64 unsigned int context
) internal_function
;
65 static int check_halt_state_context (const re_match_context_t
*mctx
,
66 const re_dfastate_t
*state
, int idx
)
68 static void update_regs (re_dfa_t
*dfa
, regmatch_t
*pmatch
,
69 regmatch_t
*prev_idx_match
, int cur_node
,
70 int cur_idx
, int nmatch
) internal_function
;
71 static int proceed_next_node (const re_match_context_t
*mctx
,
72 int nregs
, regmatch_t
*regs
,
73 int *pidx
, int node
, re_node_set
*eps_via_nodes
,
74 struct re_fail_stack_t
*fs
) internal_function
;
75 static reg_errcode_t
push_fail_stack (struct re_fail_stack_t
*fs
,
76 int str_idx
, int dest_node
, int nregs
,
78 re_node_set
*eps_via_nodes
) internal_function
;
79 static int pop_fail_stack (struct re_fail_stack_t
*fs
, int *pidx
, int nregs
,
80 regmatch_t
*regs
, re_node_set
*eps_via_nodes
) internal_function
;
81 static reg_errcode_t
set_regs (const regex_t
*preg
,
82 const re_match_context_t
*mctx
,
83 size_t nmatch
, regmatch_t
*pmatch
,
84 int fl_backtrack
) internal_function
;
85 static reg_errcode_t
free_fail_stack_return (struct re_fail_stack_t
*fs
) internal_function
;
88 static int sift_states_iter_mb (const re_match_context_t
*mctx
,
89 re_sift_context_t
*sctx
,
90 int node_idx
, int str_idx
, int max_str_idx
) internal_function
;
91 #endif /* RE_ENABLE_I18N */
92 static reg_errcode_t
sift_states_backward (re_match_context_t
*mctx
,
93 re_sift_context_t
*sctx
) internal_function
;
94 static reg_errcode_t
build_sifted_states (re_match_context_t
*mctx
,
95 re_sift_context_t
*sctx
, int str_idx
,
96 re_node_set
*cur_dest
) internal_function
;
97 static reg_errcode_t
update_cur_sifted_state (re_match_context_t
*mctx
,
98 re_sift_context_t
*sctx
,
100 re_node_set
*dest_nodes
) internal_function
;
101 static reg_errcode_t
add_epsilon_src_nodes (re_dfa_t
*dfa
,
102 re_node_set
*dest_nodes
,
103 const re_node_set
*candidates
) internal_function
;
104 static reg_errcode_t
sub_epsilon_src_nodes (re_dfa_t
*dfa
, int node
,
105 re_node_set
*dest_nodes
,
106 const re_node_set
*and_nodes
) internal_function
;
107 static int check_dst_limits (re_match_context_t
*mctx
, re_node_set
*limits
,
108 int dst_node
, int dst_idx
, int src_node
,
109 int src_idx
) internal_function
;
110 static int check_dst_limits_calc_pos_1 (re_match_context_t
*mctx
,
111 int boundaries
, int subexp_idx
,
112 int from_node
, int bkref_idx
) internal_function
;
113 static int check_dst_limits_calc_pos (re_match_context_t
*mctx
,
114 int limit
, int subexp_idx
,
115 int node
, int str_idx
,
116 int bkref_idx
) internal_function
;
117 static reg_errcode_t
check_subexp_limits (re_dfa_t
*dfa
,
118 re_node_set
*dest_nodes
,
119 const re_node_set
*candidates
,
121 struct re_backref_cache_entry
*bkref_ents
,
122 int str_idx
) internal_function
;
123 static reg_errcode_t
sift_states_bkref (re_match_context_t
*mctx
,
124 re_sift_context_t
*sctx
,
125 int str_idx
, const re_node_set
*candidates
) internal_function
;
126 static reg_errcode_t
clean_state_log_if_needed (re_match_context_t
*mctx
,
127 int next_state_log_idx
) internal_function
;
128 static reg_errcode_t
merge_state_array (re_dfa_t
*dfa
, re_dfastate_t
**dst
,
129 re_dfastate_t
**src
, int num
) internal_function
;
130 static re_dfastate_t
*find_recover_state (reg_errcode_t
*err
,
131 re_match_context_t
*mctx
) internal_function
;
132 static re_dfastate_t
*transit_state (reg_errcode_t
*err
,
133 re_match_context_t
*mctx
,
134 re_dfastate_t
*state
) internal_function
;
135 static re_dfastate_t
*merge_state_with_log (reg_errcode_t
*err
,
136 re_match_context_t
*mctx
,
137 re_dfastate_t
*next_state
) internal_function
;
138 static reg_errcode_t
check_subexp_matching_top (re_match_context_t
*mctx
,
139 re_node_set
*cur_nodes
,
140 int str_idx
) internal_function
;
142 static re_dfastate_t
*transit_state_sb (reg_errcode_t
*err
,
143 re_match_context_t
*mctx
,
144 re_dfastate_t
*pstate
) internal_function
;
146 #ifdef RE_ENABLE_I18N
147 static reg_errcode_t
transit_state_mb (re_match_context_t
*mctx
,
148 re_dfastate_t
*pstate
) internal_function
;
149 #endif /* RE_ENABLE_I18N */
150 static reg_errcode_t
transit_state_bkref (re_match_context_t
*mctx
,
151 const re_node_set
*nodes
) internal_function
;
152 static reg_errcode_t
get_subexp (re_match_context_t
*mctx
,
153 int bkref_node
, int bkref_str_idx
) internal_function
;
154 static reg_errcode_t
get_subexp_sub (re_match_context_t
*mctx
,
155 const re_sub_match_top_t
*sub_top
,
156 re_sub_match_last_t
*sub_last
,
157 int bkref_node
, int bkref_str
) internal_function
;
158 static int find_subexp_node (const re_dfa_t
*dfa
, const re_node_set
*nodes
,
159 int subexp_idx
, int type
) internal_function
;
160 static reg_errcode_t
check_arrival (re_match_context_t
*mctx
,
161 state_array_t
*path
, int top_node
,
162 int top_str
, int last_node
, int last_str
,
163 int type
) internal_function
;
164 static reg_errcode_t
check_arrival_add_next_nodes (re_match_context_t
*mctx
,
166 re_node_set
*cur_nodes
,
167 re_node_set
*next_nodes
) internal_function
;
168 static reg_errcode_t
check_arrival_expand_ecl (re_dfa_t
*dfa
,
169 re_node_set
*cur_nodes
,
170 int ex_subexp
, int type
) internal_function
;
171 static reg_errcode_t
check_arrival_expand_ecl_sub (re_dfa_t
*dfa
,
172 re_node_set
*dst_nodes
,
173 int target
, int ex_subexp
,
174 int type
) internal_function
;
175 static reg_errcode_t
expand_bkref_cache (re_match_context_t
*mctx
,
176 re_node_set
*cur_nodes
, int cur_str
,
177 int subexp_num
, int type
) internal_function
;
178 static int build_trtable (re_dfa_t
*dfa
,
179 re_dfastate_t
*state
) internal_function
;
180 #ifdef RE_ENABLE_I18N
181 static int check_node_accept_bytes (re_dfa_t
*dfa
, int node_idx
,
182 const re_string_t
*input
, int idx
) internal_function
;
184 static unsigned int find_collation_sequence_value (const unsigned char *mbs
,
185 size_t name_len
) internal_function
;
187 #endif /* RE_ENABLE_I18N */
188 static int group_nodes_into_DFAstates (re_dfa_t
*dfa
,
189 const re_dfastate_t
*state
,
190 re_node_set
*states_node
,
191 bitset
*states_ch
) internal_function
;
192 static int check_node_accept (const re_match_context_t
*mctx
,
193 const re_token_t
*node
, int idx
) internal_function
;
194 static reg_errcode_t
extend_buffers (re_match_context_t
*mctx
) internal_function
;
196 /* Entry point for POSIX code. */
198 /* regexec searches for a given pattern, specified by PREG, in the
201 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
202 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
203 least NMATCH elements, and we set them to the offsets of the
204 corresponding matched substrings.
206 EFLAGS specifies `execution flags' which affect matching: if
207 REG_NOTBOL is set, then ^ does not match at the beginning of the
208 string; if REG_NOTEOL is set, then $ does not match at the end.
210 We return 0 if we find a match and REG_NOMATCH if not. */
213 regexec (preg
, string
, nmatch
, pmatch
, eflags
)
214 const regex_t
*__restrict preg
;
215 const char *__restrict string
;
223 if (eflags
& ~(REG_NOTBOL
| REG_NOTEOL
| REG_STARTEND
))
226 if (eflags
& REG_STARTEND
)
228 start
= pmatch
[0].rm_so
;
229 length
= pmatch
[0].rm_eo
;
234 length
= strlen (string
);
237 err
= re_search_internal (preg
, string
, length
, start
, length
- start
,
238 length
, 0, NULL
, eflags
);
240 err
= re_search_internal (preg
, string
, length
, start
, length
- start
,
241 length
, nmatch
, pmatch
, eflags
);
242 return err
!= REG_NOERROR
;
246 # include <shlib-compat.h>
247 versioned_symbol (libc
, __regexec
, regexec
, GLIBC_2_3_4
);
249 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
250 __typeof__ (__regexec
) __compat_regexec
;
253 attribute_compat_text_section
254 __compat_regexec (const regex_t
*__restrict preg
,
255 const char *__restrict string
, size_t nmatch
,
256 regmatch_t pmatch
[], int eflags
)
258 return regexec (preg
, string
, nmatch
, pmatch
,
259 eflags
& (REG_NOTBOL
| REG_NOTEOL
));
261 compat_symbol (libc
, __compat_regexec
, regexec
, GLIBC_2_0
);
265 /* Entry points for GNU code. */
267 /* re_match, re_search, re_match_2, re_search_2
269 The former two functions operate on STRING with length LENGTH,
270 while the later two operate on concatenation of STRING1 and STRING2
271 with lengths LENGTH1 and LENGTH2, respectively.
273 re_match() matches the compiled pattern in BUFP against the string,
274 starting at index START.
276 re_search() first tries matching at index START, then it tries to match
277 starting from index START + 1, and so on. The last start position tried
278 is START + RANGE. (Thus RANGE = 0 forces re_search to operate the same
281 The parameter STOP of re_{match,search}_2 specifies that no match exceeding
282 the first STOP characters of the concatenation of the strings should be
285 If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match
286 and all groups is stroed in REGS. (For the "_2" variants, the offsets are
287 computed relative to the concatenation, not relative to the individual
290 On success, re_match* functions return the length of the match, re_search*
291 return the position of the start of the match. Return value -1 means no
292 match was found and -2 indicates an internal error. */
295 re_match (bufp
, string
, length
, start
, regs
)
296 struct re_pattern_buffer
*bufp
;
299 struct re_registers
*regs
;
301 return re_search_stub (bufp
, string
, length
, start
, 0, length
, regs
, 1);
304 weak_alias (__re_match
, re_match
)
308 re_search (bufp
, string
, length
, start
, range
, regs
)
309 struct re_pattern_buffer
*bufp
;
311 int length
, start
, range
;
312 struct re_registers
*regs
;
314 return re_search_stub (bufp
, string
, length
, start
, range
, length
, regs
, 0);
317 weak_alias (__re_search
, re_search
)
321 re_match_2 (bufp
, string1
, length1
, string2
, length2
, start
, regs
, stop
)
322 struct re_pattern_buffer
*bufp
;
323 const char *string1
, *string2
;
324 int length1
, length2
, start
, stop
;
325 struct re_registers
*regs
;
327 return re_search_2_stub (bufp
, string1
, length1
, string2
, length2
,
328 start
, 0, regs
, stop
, 1);
331 weak_alias (__re_match_2
, re_match_2
)
335 re_search_2 (bufp
, string1
, length1
, string2
, length2
, start
, range
, regs
, stop
)
336 struct re_pattern_buffer
*bufp
;
337 const char *string1
, *string2
;
338 int length1
, length2
, start
, range
, stop
;
339 struct re_registers
*regs
;
341 return re_search_2_stub (bufp
, string1
, length1
, string2
, length2
,
342 start
, range
, regs
, stop
, 0);
345 weak_alias (__re_search_2
, re_search_2
)
349 re_search_2_stub (bufp
, string1
, length1
, string2
, length2
, start
, range
, regs
,
351 struct re_pattern_buffer
*bufp
;
352 const char *string1
, *string2
;
353 int length1
, length2
, start
, range
, stop
, ret_len
;
354 struct re_registers
*regs
;
358 int len
= length1
+ length2
;
361 if (BE (length1
< 0 || length2
< 0 || stop
< 0, 0))
364 /* Concatenate the strings. */
368 char *s
= re_malloc (char, len
);
370 if (BE (s
== NULL
, 0))
372 memcpy (s
, string1
, length1
);
373 memcpy (s
+ length1
, string2
, length2
);
382 rval
= re_search_stub (bufp
, str
, len
, start
, range
, stop
, regs
,
385 re_free ((char *) str
);
389 /* The parameters have the same meaning as those of re_search.
390 Additional parameters:
391 If RET_LEN is nonzero the length of the match is returned (re_match style);
392 otherwise the position of the match is returned. */
395 re_search_stub (bufp
, string
, length
, start
, range
, stop
, regs
, ret_len
)
396 struct re_pattern_buffer
*bufp
;
398 int length
, start
, range
, stop
, ret_len
;
399 struct re_registers
*regs
;
401 reg_errcode_t result
;
406 /* Check for out-of-range. */
407 if (BE (start
< 0 || start
> length
, 0))
409 if (BE (start
+ range
> length
, 0))
410 range
= length
- start
;
411 else if (BE (start
+ range
< 0, 0))
414 eflags
|= (bufp
->not_bol
) ? REG_NOTBOL
: 0;
415 eflags
|= (bufp
->not_eol
) ? REG_NOTEOL
: 0;
417 /* Compile fastmap if we haven't yet. */
418 if (range
> 0 && bufp
->fastmap
!= NULL
&& !bufp
->fastmap_accurate
)
419 re_compile_fastmap (bufp
);
421 if (BE (bufp
->no_sub
, 0))
424 /* We need at least 1 register. */
427 else if (BE (bufp
->regs_allocated
== REGS_FIXED
&&
428 regs
->num_regs
< bufp
->re_nsub
+ 1, 0))
430 nregs
= regs
->num_regs
;
431 if (BE (nregs
< 1, 0))
433 /* Nothing can be copied to regs. */
439 nregs
= bufp
->re_nsub
+ 1;
440 pmatch
= re_malloc (regmatch_t
, nregs
);
441 if (BE (pmatch
== NULL
, 0))
444 result
= re_search_internal (bufp
, string
, length
, start
, range
, stop
,
445 nregs
, pmatch
, eflags
);
449 /* I hope we needn't fill ther regs with -1's when no match was found. */
450 if (result
!= REG_NOERROR
)
452 else if (regs
!= NULL
)
454 /* If caller wants register contents data back, copy them. */
455 bufp
->regs_allocated
= re_copy_regs (regs
, pmatch
, nregs
,
456 bufp
->regs_allocated
);
457 if (BE (bufp
->regs_allocated
== REGS_UNALLOCATED
, 0))
461 if (BE (rval
== 0, 1))
465 assert (pmatch
[0].rm_so
== start
);
466 rval
= pmatch
[0].rm_eo
- start
;
469 rval
= pmatch
[0].rm_so
;
476 re_copy_regs (regs
, pmatch
, nregs
, regs_allocated
)
477 struct re_registers
*regs
;
479 int nregs
, regs_allocated
;
481 int rval
= REGS_REALLOCATE
;
483 int need_regs
= nregs
+ 1;
484 /* We need one extra element beyond `num_regs' for the `-1' marker GNU code
487 /* Have the register data arrays been allocated? */
488 if (regs_allocated
== REGS_UNALLOCATED
)
489 { /* No. So allocate them with malloc. */
490 regs
->start
= re_malloc (regoff_t
, need_regs
);
491 regs
->end
= re_malloc (regoff_t
, need_regs
);
492 if (BE (regs
->start
== NULL
, 0) || BE (regs
->end
== NULL
, 0))
493 return REGS_UNALLOCATED
;
494 regs
->num_regs
= need_regs
;
496 else if (regs_allocated
== REGS_REALLOCATE
)
497 { /* Yes. If we need more elements than were already
498 allocated, reallocate them. If we need fewer, just
500 if (BE (need_regs
> regs
->num_regs
, 0))
502 regoff_t
*new_start
= re_realloc (regs
->start
, regoff_t
, need_regs
);
503 regoff_t
*new_end
= re_realloc (regs
->end
, regoff_t
, need_regs
);
504 if (BE (new_start
== NULL
, 0) || BE (new_end
== NULL
, 0))
505 return REGS_UNALLOCATED
;
506 regs
->start
= new_start
;
508 regs
->num_regs
= need_regs
;
513 assert (regs_allocated
== REGS_FIXED
);
514 /* This function may not be called with REGS_FIXED and nregs too big. */
515 assert (regs
->num_regs
>= nregs
);
520 for (i
= 0; i
< nregs
; ++i
)
522 regs
->start
[i
] = pmatch
[i
].rm_so
;
523 regs
->end
[i
] = pmatch
[i
].rm_eo
;
525 for ( ; i
< regs
->num_regs
; ++i
)
526 regs
->start
[i
] = regs
->end
[i
] = -1;
531 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
532 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
533 this memory for recording register information. STARTS and ENDS
534 must be allocated using the malloc library routine, and must each
535 be at least NUM_REGS * sizeof (regoff_t) bytes long.
537 If NUM_REGS == 0, then subsequent matches should allocate their own
540 Unless this function is called, the first search or match using
541 PATTERN_BUFFER will allocate its own register data, without
542 freeing the old data. */
545 re_set_registers (bufp
, regs
, num_regs
, starts
, ends
)
546 struct re_pattern_buffer
*bufp
;
547 struct re_registers
*regs
;
549 regoff_t
*starts
, *ends
;
553 bufp
->regs_allocated
= REGS_REALLOCATE
;
554 regs
->num_regs
= num_regs
;
555 regs
->start
= starts
;
560 bufp
->regs_allocated
= REGS_UNALLOCATED
;
562 regs
->start
= regs
->end
= (regoff_t
*) 0;
566 weak_alias (__re_set_registers
, re_set_registers
)
569 /* Entry points compatible with 4.2 BSD regex library. We don't define
570 them unless specifically requested. */
572 #if defined _REGEX_RE_COMP || defined _LIBC
580 return 0 == regexec (&re_comp_buf
, s
, 0, NULL
, 0);
582 #endif /* _REGEX_RE_COMP */
584 /* Internal entry point. */
586 /* Searches for a compiled pattern PREG in the string STRING, whose
587 length is LENGTH. NMATCH, PMATCH, and EFLAGS have the same
588 mingings with regexec. START, and RANGE have the same meanings
590 Return REG_NOERROR if we find a match, and REG_NOMATCH if not,
591 otherwise return the error code.
592 Note: We assume front end functions already check ranges.
593 (START + RANGE >= 0 && START + RANGE <= LENGTH) */
596 re_search_internal (preg
, string
, length
, start
, range
, stop
, nmatch
, pmatch
,
600 int length
, start
, range
, stop
, eflags
;
605 re_dfa_t
*dfa
= (re_dfa_t
*)preg
->buffer
;
606 int left_lim
, right_lim
, incr
;
607 int fl_longest_match
, match_first
, match_kind
, match_last
= -1;
609 #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
610 re_match_context_t mctx
= { .dfa
= dfa
};
612 re_match_context_t mctx
;
614 char *fastmap
= (preg
->fastmap
!= NULL
&& preg
->fastmap_accurate
615 && range
&& !preg
->can_be_null
) ? preg
->fastmap
: NULL
;
616 unsigned RE_TRANSLATE_TYPE t
= (unsigned RE_TRANSLATE_TYPE
) preg
->translate
;
618 #if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L))
619 memset (&mctx
, '\0', sizeof (re_match_context_t
));
623 /* Check if the DFA haven't been compiled. */
624 if (BE (preg
->used
== 0 || dfa
->init_state
== NULL
625 || dfa
->init_state_word
== NULL
|| dfa
->init_state_nl
== NULL
626 || dfa
->init_state_begbuf
== NULL
, 0))
630 /* We assume front-end functions already check them. */
631 assert (start
+ range
>= 0 && start
+ range
<= length
);
634 /* If initial states with non-begbuf contexts have no elements,
635 the regex must be anchored. If preg->newline_anchor is set,
636 we'll never use init_state_nl, so do not check it. */
637 if (dfa
->init_state
->nodes
.nelem
== 0
638 && dfa
->init_state_word
->nodes
.nelem
== 0
639 && (dfa
->init_state_nl
->nodes
.nelem
== 0
640 || !preg
->newline_anchor
))
642 if (start
!= 0 && start
+ range
!= 0)
647 /* We must check the longest matching, if nmatch > 0. */
648 fl_longest_match
= (nmatch
!= 0 || dfa
->nbackref
);
650 err
= re_string_allocate (&mctx
.input
, string
, length
, dfa
->nodes_len
+ 1,
651 preg
->translate
, preg
->syntax
& RE_ICASE
, dfa
);
652 if (BE (err
!= REG_NOERROR
, 0))
654 mctx
.input
.stop
= stop
;
655 mctx
.input
.raw_stop
= stop
;
656 mctx
.input
.newline_anchor
= preg
->newline_anchor
;
658 err
= match_ctx_init (&mctx
, eflags
, dfa
->nbackref
* 2);
659 if (BE (err
!= REG_NOERROR
, 0))
662 /* We will log all the DFA states through which the dfa pass,
663 if nmatch > 1, or this dfa has "multibyte node", which is a
664 back-reference or a node which can accept multibyte character or
665 multi character collating element. */
666 if (nmatch
> 1 || dfa
->has_mb_node
)
668 mctx
.state_log
= re_malloc (re_dfastate_t
*, mctx
.input
.bufs_len
+ 1);
669 if (BE (mctx
.state_log
== NULL
, 0))
676 mctx
.state_log
= NULL
;
679 mctx
.input
.tip_context
= (eflags
& REG_NOTBOL
) ? CONTEXT_BEGBUF
680 : CONTEXT_NEWLINE
| CONTEXT_BEGBUF
;
682 /* Check incrementally whether of not the input string match. */
683 incr
= (range
< 0) ? -1 : 1;
684 left_lim
= (range
< 0) ? start
+ range
: start
;
685 right_lim
= (range
< 0) ? start
: start
+ range
;
686 sb
= dfa
->mb_cur_max
== 1;
689 ? ((sb
|| !(preg
->syntax
& RE_ICASE
|| t
) ? 4 : 0)
690 | (range
>= 0 ? 2 : 0)
691 | (t
!= NULL
? 1 : 0))
694 for (;; match_first
+= incr
)
697 if (match_first
< left_lim
|| right_lim
< match_first
)
700 /* Advance as rapidly as possible through the string, until we
701 find a plausible place to start matching. This may be done
702 with varying efficiency, so there are various possibilities:
703 only the most common of them are specialized, in order to
704 save on code size. We use a switch statement for speed. */
712 /* Fastmap with single-byte translation, match forward. */
713 while (BE (match_first
< right_lim
, 1)
714 && !fastmap
[t
[(unsigned char) string
[match_first
]]])
716 goto forward_match_found_start_or_reached_end
;
719 /* Fastmap without translation, match forward. */
720 while (BE (match_first
< right_lim
, 1)
721 && !fastmap
[(unsigned char) string
[match_first
]])
724 forward_match_found_start_or_reached_end
:
725 if (BE (match_first
== right_lim
, 0))
727 ch
= match_first
>= length
728 ? 0 : (unsigned char) string
[match_first
];
729 if (!fastmap
[t
? t
[ch
] : ch
])
736 /* Fastmap without multi-byte translation, match backwards. */
737 while (match_first
>= left_lim
)
739 ch
= match_first
>= length
740 ? 0 : (unsigned char) string
[match_first
];
741 if (fastmap
[t
? t
[ch
] : ch
])
745 if (match_first
< left_lim
)
750 /* In this case, we can't determine easily the current byte,
751 since it might be a component byte of a multibyte
752 character. Then we use the constructed buffer instead. */
755 /* If MATCH_FIRST is out of the valid range, reconstruct the
757 unsigned int offset
= match_first
- mctx
.input
.raw_mbs_idx
;
758 if (BE (offset
>= (unsigned int) mctx
.input
.valid_raw_len
, 0))
760 err
= re_string_reconstruct (&mctx
.input
, match_first
,
762 if (BE (err
!= REG_NOERROR
, 0))
765 offset
= match_first
- mctx
.input
.raw_mbs_idx
;
767 /* If MATCH_FIRST is out of the buffer, leave it as '\0'.
768 Note that MATCH_FIRST must not be smaller than 0. */
769 ch
= (match_first
>= length
770 ? 0 : re_string_byte_at (&mctx
.input
, offset
));
774 if (match_first
< left_lim
|| match_first
> right_lim
)
783 /* Reconstruct the buffers so that the matcher can assume that
784 the matching starts from the beginning of the buffer. */
785 err
= re_string_reconstruct (&mctx
.input
, match_first
, eflags
);
786 if (BE (err
!= REG_NOERROR
, 0))
789 #ifdef RE_ENABLE_I18N
790 /* Don't consider this char as a possible match start if it part,
791 yet isn't the head, of a multibyte character. */
792 if (!sb
&& !re_string_first_byte (&mctx
.input
, 0))
796 /* It seems to be appropriate one, then use the matcher. */
797 /* We assume that the matching starts from 0. */
798 mctx
.state_log_top
= mctx
.nbkref_ents
= mctx
.max_mb_elem_len
= 0;
799 match_last
= check_matching (&mctx
, fl_longest_match
,
800 range
>= 0 ? &match_first
: NULL
);
801 if (match_last
!= -1)
803 if (BE (match_last
== -2, 0))
810 mctx
.match_last
= match_last
;
811 if ((!preg
->no_sub
&& nmatch
> 1) || dfa
->nbackref
)
813 re_dfastate_t
*pstate
= mctx
.state_log
[match_last
];
814 mctx
.last_node
= check_halt_state_context (&mctx
, pstate
,
817 if ((!preg
->no_sub
&& nmatch
> 1 && dfa
->has_plural_match
)
820 err
= prune_impossible_nodes (&mctx
);
821 if (err
== REG_NOERROR
)
823 if (BE (err
!= REG_NOMATCH
, 0))
828 break; /* We found a match. */
832 match_ctx_clean (&mctx
);
836 assert (match_last
!= -1);
837 assert (err
== REG_NOERROR
);
840 /* Set pmatch[] if we need. */
845 /* Initialize registers. */
846 for (reg_idx
= 1; reg_idx
< nmatch
; ++reg_idx
)
847 pmatch
[reg_idx
].rm_so
= pmatch
[reg_idx
].rm_eo
= -1;
849 /* Set the points where matching start/end. */
851 pmatch
[0].rm_eo
= mctx
.match_last
;
853 if (!preg
->no_sub
&& nmatch
> 1)
855 err
= set_regs (preg
, &mctx
, nmatch
, pmatch
,
856 dfa
->has_plural_match
&& dfa
->nbackref
> 0);
857 if (BE (err
!= REG_NOERROR
, 0))
861 /* At last, add the offset to the each registers, since we slided
862 the buffers so that we could assume that the matching starts
864 for (reg_idx
= 0; reg_idx
< nmatch
; ++reg_idx
)
865 if (pmatch
[reg_idx
].rm_so
!= -1)
867 #ifdef RE_ENABLE_I18N
868 if (BE (mctx
.input
.offsets_needed
!= 0, 0))
870 if (pmatch
[reg_idx
].rm_so
== mctx
.input
.valid_len
)
871 pmatch
[reg_idx
].rm_so
+= mctx
.input
.valid_raw_len
- mctx
.input
.valid_len
;
873 pmatch
[reg_idx
].rm_so
= mctx
.input
.offsets
[pmatch
[reg_idx
].rm_so
];
874 if (pmatch
[reg_idx
].rm_eo
== mctx
.input
.valid_len
)
875 pmatch
[reg_idx
].rm_eo
+= mctx
.input
.valid_raw_len
- mctx
.input
.valid_len
;
877 pmatch
[reg_idx
].rm_eo
= mctx
.input
.offsets
[pmatch
[reg_idx
].rm_eo
];
880 assert (mctx
.input
.offsets_needed
== 0);
882 pmatch
[reg_idx
].rm_so
+= match_first
;
883 pmatch
[reg_idx
].rm_eo
+= match_first
;
888 reg_idx
+ 1 < nmatch
&& reg_idx
< preg
->re_nsub
;
890 if (dfa
->subexp_map
[reg_idx
] != reg_idx
)
892 pmatch
[reg_idx
+ 1].rm_so
893 = pmatch
[dfa
->subexp_map
[reg_idx
] + 1].rm_so
;
894 pmatch
[reg_idx
+ 1].rm_eo
895 = pmatch
[dfa
->subexp_map
[reg_idx
] + 1].rm_eo
;
900 re_free (mctx
.state_log
);
902 match_ctx_free (&mctx
);
903 re_string_destruct (&mctx
.input
);
908 prune_impossible_nodes (mctx
)
909 re_match_context_t
*mctx
;
911 re_dfa_t
*const dfa
= mctx
->dfa
;
912 int halt_node
, match_last
;
914 re_dfastate_t
**sifted_states
;
915 re_dfastate_t
**lim_states
= NULL
;
916 re_sift_context_t sctx
;
918 assert (mctx
->state_log
!= NULL
);
920 match_last
= mctx
->match_last
;
921 halt_node
= mctx
->last_node
;
922 sifted_states
= re_malloc (re_dfastate_t
*, match_last
+ 1);
923 if (BE (sifted_states
== NULL
, 0))
930 lim_states
= re_malloc (re_dfastate_t
*, match_last
+ 1);
931 if (BE (lim_states
== NULL
, 0))
938 memset (lim_states
, '\0',
939 sizeof (re_dfastate_t
*) * (match_last
+ 1));
940 sift_ctx_init (&sctx
, sifted_states
, lim_states
, halt_node
,
942 ret
= sift_states_backward (mctx
, &sctx
);
943 re_node_set_free (&sctx
.limits
);
944 if (BE (ret
!= REG_NOERROR
, 0))
946 if (sifted_states
[0] != NULL
|| lim_states
[0] != NULL
)
956 } while (mctx
->state_log
[match_last
] == NULL
957 || !mctx
->state_log
[match_last
]->halt
);
958 halt_node
= check_halt_state_context (mctx
,
959 mctx
->state_log
[match_last
],
962 ret
= merge_state_array (dfa
, sifted_states
, lim_states
,
964 re_free (lim_states
);
966 if (BE (ret
!= REG_NOERROR
, 0))
971 sift_ctx_init (&sctx
, sifted_states
, lim_states
, halt_node
, match_last
);
972 ret
= sift_states_backward (mctx
, &sctx
);
973 re_node_set_free (&sctx
.limits
);
974 if (BE (ret
!= REG_NOERROR
, 0))
977 re_free (mctx
->state_log
);
978 mctx
->state_log
= sifted_states
;
979 sifted_states
= NULL
;
980 mctx
->last_node
= halt_node
;
981 mctx
->match_last
= match_last
;
984 re_free (sifted_states
);
985 re_free (lim_states
);
989 /* Acquire an initial state and return it.
990 We must select appropriate initial state depending on the context,
991 since initial states may have constraints like "\<", "^", etc.. */
993 static inline re_dfastate_t
*
994 acquire_init_state_context (err
, mctx
, idx
)
996 const re_match_context_t
*mctx
;
999 re_dfa_t
*const dfa
= mctx
->dfa
;
1000 if (dfa
->init_state
->has_constraint
)
1002 unsigned int context
;
1003 context
= re_string_context_at (&mctx
->input
, idx
- 1, mctx
->eflags
);
1004 if (IS_WORD_CONTEXT (context
))
1005 return dfa
->init_state_word
;
1006 else if (IS_ORDINARY_CONTEXT (context
))
1007 return dfa
->init_state
;
1008 else if (IS_BEGBUF_CONTEXT (context
) && IS_NEWLINE_CONTEXT (context
))
1009 return dfa
->init_state_begbuf
;
1010 else if (IS_NEWLINE_CONTEXT (context
))
1011 return dfa
->init_state_nl
;
1012 else if (IS_BEGBUF_CONTEXT (context
))
1014 /* It is relatively rare case, then calculate on demand. */
1015 return re_acquire_state_context (err
, dfa
,
1016 dfa
->init_state
->entrance_nodes
,
1020 /* Must not happen? */
1021 return dfa
->init_state
;
1024 return dfa
->init_state
;
1027 /* Check whether the regular expression match input string INPUT or not,
1028 and return the index where the matching end, return -1 if not match,
1029 or return -2 in case of an error.
1030 FL_LONGEST_MATCH means we want the POSIX longest matching.
1031 If P_MATCH_FIRST is not NULL, and the match fails, it is set to the
1032 next place where we may want to try matching.
1033 Note that the matcher assume that the maching starts from the current
1034 index of the buffer. */
1037 check_matching (mctx
, fl_longest_match
, p_match_first
)
1038 re_match_context_t
*mctx
;
1039 int fl_longest_match
;
1042 re_dfa_t
*const dfa
= mctx
->dfa
;
1045 int match_last
= -1;
1046 int cur_str_idx
= re_string_cur_idx (&mctx
->input
);
1047 re_dfastate_t
*cur_state
;
1048 int at_init_state
= p_match_first
!= NULL
;
1049 int next_start_idx
= cur_str_idx
;
1052 cur_state
= acquire_init_state_context (&err
, mctx
, cur_str_idx
);
1053 /* An initial state must not be NULL (invalid). */
1054 if (BE (cur_state
== NULL
, 0))
1056 assert (err
== REG_ESPACE
);
1060 if (mctx
->state_log
!= NULL
)
1062 mctx
->state_log
[cur_str_idx
] = cur_state
;
1064 /* Check OP_OPEN_SUBEXP in the initial state in case that we use them
1065 later. E.g. Processing back references. */
1066 if (BE (dfa
->nbackref
, 0))
1069 err
= check_subexp_matching_top (mctx
, &cur_state
->nodes
, 0);
1070 if (BE (err
!= REG_NOERROR
, 0))
1073 if (cur_state
->has_backref
)
1075 err
= transit_state_bkref (mctx
, &cur_state
->nodes
);
1076 if (BE (err
!= REG_NOERROR
, 0))
1082 /* If the RE accepts NULL string. */
1083 if (BE (cur_state
->halt
, 0))
1085 if (!cur_state
->has_constraint
1086 || check_halt_state_context (mctx
, cur_state
, cur_str_idx
))
1088 if (!fl_longest_match
)
1092 match_last
= cur_str_idx
;
1098 while (!re_string_eoi (&mctx
->input
))
1100 re_dfastate_t
*old_state
= cur_state
;
1101 int next_char_idx
= re_string_cur_idx (&mctx
->input
) + 1;
1103 if (BE (next_char_idx
>= mctx
->input
.bufs_len
, 0)
1104 || (BE (next_char_idx
>= mctx
->input
.valid_len
, 0)
1105 && mctx
->input
.valid_len
< mctx
->input
.len
))
1107 err
= extend_buffers (mctx
);
1108 if (BE (err
!= REG_NOERROR
, 0))
1110 assert (err
== REG_ESPACE
);
1115 cur_state
= transit_state (&err
, mctx
, cur_state
);
1116 if (mctx
->state_log
!= NULL
)
1117 cur_state
= merge_state_with_log (&err
, mctx
, cur_state
);
1119 if (cur_state
== NULL
)
1121 /* Reached the invalid state or an error. Try to recover a valid
1122 state using the state log, if available and if we have not
1123 already found a valid (even if not the longest) match. */
1124 if (BE (err
!= REG_NOERROR
, 0))
1127 if (mctx
->state_log
== NULL
1128 || (match
&& !fl_longest_match
)
1129 || (cur_state
= find_recover_state (&err
, mctx
)) == NULL
)
1133 if (BE (at_init_state
, 0))
1135 if (old_state
== cur_state
)
1136 next_start_idx
= next_char_idx
;
1141 if (cur_state
->halt
)
1143 /* Reached a halt state.
1144 Check the halt state can satisfy the current context. */
1145 if (!cur_state
->has_constraint
1146 || check_halt_state_context (mctx
, cur_state
,
1147 re_string_cur_idx (&mctx
->input
)))
1149 /* We found an appropriate halt state. */
1150 match_last
= re_string_cur_idx (&mctx
->input
);
1153 /* We found a match, do not modify match_first below. */
1154 p_match_first
= NULL
;
1155 if (!fl_longest_match
)
1162 *p_match_first
+= next_start_idx
;
1167 /* Check NODE match the current context. */
1169 static int check_halt_node_context (dfa
, node
, context
)
1170 const re_dfa_t
*dfa
;
1172 unsigned int context
;
1174 re_token_type_t type
= dfa
->nodes
[node
].type
;
1175 unsigned int constraint
= dfa
->nodes
[node
].constraint
;
1176 if (type
!= END_OF_RE
)
1180 if (NOT_SATISFY_NEXT_CONSTRAINT (constraint
, context
))
1185 /* Check the halt state STATE match the current context.
1186 Return 0 if not match, if the node, STATE has, is a halt node and
1187 match the context, return the node. */
1190 check_halt_state_context (mctx
, state
, idx
)
1191 const re_match_context_t
*mctx
;
1192 const re_dfastate_t
*state
;
1196 unsigned int context
;
1198 assert (state
->halt
);
1200 context
= re_string_context_at (&mctx
->input
, idx
, mctx
->eflags
);
1201 for (i
= 0; i
< state
->nodes
.nelem
; ++i
)
1202 if (check_halt_node_context (mctx
->dfa
, state
->nodes
.elems
[i
], context
))
1203 return state
->nodes
.elems
[i
];
1207 /* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA
1208 corresponding to the DFA).
1209 Return the destination node, and update EPS_VIA_NODES, return -1 in case
1213 proceed_next_node (mctx
, nregs
, regs
, pidx
, node
, eps_via_nodes
, fs
)
1214 const re_match_context_t
*mctx
;
1216 int nregs
, *pidx
, node
;
1217 re_node_set
*eps_via_nodes
;
1218 struct re_fail_stack_t
*fs
;
1220 re_dfa_t
*const dfa
= mctx
->dfa
;
1221 int i
, err
, dest_node
;
1223 if (IS_EPSILON_NODE (dfa
->nodes
[node
].type
))
1225 re_node_set
*cur_nodes
= &mctx
->state_log
[*pidx
]->nodes
;
1226 re_node_set
*edests
= &dfa
->edests
[node
];
1228 err
= re_node_set_insert (eps_via_nodes
, node
);
1229 if (BE (err
< 0, 0))
1231 /* Pick up a valid destination, or return -1 if none is found. */
1232 for (dest_node
= -1, i
= 0; i
< edests
->nelem
; ++i
)
1234 int candidate
= edests
->elems
[i
];
1235 if (!re_node_set_contains (cur_nodes
, candidate
))
1237 if (dest_node
== -1)
1238 dest_node
= candidate
;
1242 /* In order to avoid infinite loop like "(a*)*", return the second
1243 epsilon-transition if the first was already considered. */
1244 if (re_node_set_contains (eps_via_nodes
, dest_node
))
1247 /* Otherwise, push the second epsilon-transition on the fail stack. */
1249 && push_fail_stack (fs
, *pidx
, candidate
, nregs
, regs
,
1253 /* We know we are going to exit. */
1262 re_token_type_t type
= dfa
->nodes
[node
].type
;
1264 #ifdef RE_ENABLE_I18N
1265 if (ACCEPT_MB_NODE (type
))
1266 naccepted
= check_node_accept_bytes (dfa
, node
, &mctx
->input
, *pidx
);
1268 #endif /* RE_ENABLE_I18N */
1269 if (type
== OP_BACK_REF
)
1271 int subexp_idx
= dfa
->nodes
[node
].opr
.idx
+ 1;
1272 naccepted
= regs
[subexp_idx
].rm_eo
- regs
[subexp_idx
].rm_so
;
1275 if (regs
[subexp_idx
].rm_so
== -1 || regs
[subexp_idx
].rm_eo
== -1)
1279 char *buf
= (char *) re_string_get_buffer (&mctx
->input
);
1280 if (memcmp (buf
+ regs
[subexp_idx
].rm_so
, buf
+ *pidx
,
1288 err
= re_node_set_insert (eps_via_nodes
, node
);
1289 if (BE (err
< 0, 0))
1291 dest_node
= dfa
->edests
[node
].elems
[0];
1292 if (re_node_set_contains (&mctx
->state_log
[*pidx
]->nodes
,
1299 || check_node_accept (mctx
, dfa
->nodes
+ node
, *pidx
))
1301 dest_node
= dfa
->nexts
[node
];
1302 *pidx
= (naccepted
== 0) ? *pidx
+ 1 : *pidx
+ naccepted
;
1303 if (fs
&& (*pidx
> mctx
->match_last
|| mctx
->state_log
[*pidx
] == NULL
1304 || !re_node_set_contains (&mctx
->state_log
[*pidx
]->nodes
,
1307 re_node_set_empty (eps_via_nodes
);
1314 static reg_errcode_t
1315 push_fail_stack (fs
, str_idx
, dest_node
, nregs
, regs
, eps_via_nodes
)
1316 struct re_fail_stack_t
*fs
;
1317 int str_idx
, dest_node
, nregs
;
1319 re_node_set
*eps_via_nodes
;
1322 int num
= fs
->num
++;
1323 if (fs
->num
== fs
->alloc
)
1325 struct re_fail_stack_ent_t
*new_array
;
1326 new_array
= realloc (fs
->stack
, (sizeof (struct re_fail_stack_ent_t
)
1328 if (new_array
== NULL
)
1331 fs
->stack
= new_array
;
1333 fs
->stack
[num
].idx
= str_idx
;
1334 fs
->stack
[num
].node
= dest_node
;
1335 fs
->stack
[num
].regs
= re_malloc (regmatch_t
, nregs
);
1336 if (fs
->stack
[num
].regs
== NULL
)
1338 memcpy (fs
->stack
[num
].regs
, regs
, sizeof (regmatch_t
) * nregs
);
1339 err
= re_node_set_init_copy (&fs
->stack
[num
].eps_via_nodes
, eps_via_nodes
);
1344 pop_fail_stack (fs
, pidx
, nregs
, regs
, eps_via_nodes
)
1345 struct re_fail_stack_t
*fs
;
1348 re_node_set
*eps_via_nodes
;
1350 int num
= --fs
->num
;
1352 *pidx
= fs
->stack
[num
].idx
;
1353 memcpy (regs
, fs
->stack
[num
].regs
, sizeof (regmatch_t
) * nregs
);
1354 re_node_set_free (eps_via_nodes
);
1355 re_free (fs
->stack
[num
].regs
);
1356 *eps_via_nodes
= fs
->stack
[num
].eps_via_nodes
;
1357 return fs
->stack
[num
].node
;
1360 /* Set the positions where the subexpressions are starts/ends to registers
1362 Note: We assume that pmatch[0] is already set, and
1363 pmatch[i].rm_so == pmatch[i].rm_eo == -1 for 0 < i < nmatch. */
1365 static reg_errcode_t
1366 set_regs (preg
, mctx
, nmatch
, pmatch
, fl_backtrack
)
1367 const regex_t
*preg
;
1368 const re_match_context_t
*mctx
;
1373 re_dfa_t
*dfa
= (re_dfa_t
*) preg
->buffer
;
1374 int idx
, cur_node
, real_nmatch
;
1375 re_node_set eps_via_nodes
;
1376 struct re_fail_stack_t
*fs
;
1377 struct re_fail_stack_t fs_body
= { 0, 2, NULL
};
1378 regmatch_t
*prev_idx_match
;
1381 assert (nmatch
> 1);
1382 assert (mctx
->state_log
!= NULL
);
1387 fs
->stack
= re_malloc (struct re_fail_stack_ent_t
, fs
->alloc
);
1388 if (fs
->stack
== NULL
)
1394 cur_node
= dfa
->init_node
;
1395 real_nmatch
= (nmatch
<= preg
->re_nsub
) ? nmatch
: preg
->re_nsub
+ 1;
1396 re_node_set_init_empty (&eps_via_nodes
);
1398 prev_idx_match
= (regmatch_t
*) alloca (sizeof (regmatch_t
) * real_nmatch
);
1399 memcpy (prev_idx_match
, pmatch
, sizeof (regmatch_t
) * real_nmatch
);
1401 for (idx
= pmatch
[0].rm_so
; idx
<= pmatch
[0].rm_eo
;)
1403 update_regs (dfa
, pmatch
, prev_idx_match
, cur_node
, idx
, real_nmatch
);
1405 if (idx
== pmatch
[0].rm_eo
&& cur_node
== mctx
->last_node
)
1410 for (reg_idx
= 0; reg_idx
< nmatch
; ++reg_idx
)
1411 if (pmatch
[reg_idx
].rm_so
> -1 && pmatch
[reg_idx
].rm_eo
== -1)
1413 if (reg_idx
== nmatch
)
1415 re_node_set_free (&eps_via_nodes
);
1416 return free_fail_stack_return (fs
);
1418 cur_node
= pop_fail_stack (fs
, &idx
, nmatch
, pmatch
,
1423 re_node_set_free (&eps_via_nodes
);
1428 /* Proceed to next node. */
1429 cur_node
= proceed_next_node (mctx
, nmatch
, pmatch
, &idx
, cur_node
,
1430 &eps_via_nodes
, fs
);
1432 if (BE (cur_node
< 0, 0))
1434 if (BE (cur_node
== -2, 0))
1436 re_node_set_free (&eps_via_nodes
);
1437 free_fail_stack_return (fs
);
1441 cur_node
= pop_fail_stack (fs
, &idx
, nmatch
, pmatch
,
1445 re_node_set_free (&eps_via_nodes
);
1450 re_node_set_free (&eps_via_nodes
);
1451 return free_fail_stack_return (fs
);
1454 static reg_errcode_t
1455 free_fail_stack_return (fs
)
1456 struct re_fail_stack_t
*fs
;
1461 for (fs_idx
= 0; fs_idx
< fs
->num
; ++fs_idx
)
1463 re_node_set_free (&fs
->stack
[fs_idx
].eps_via_nodes
);
1464 re_free (fs
->stack
[fs_idx
].regs
);
1466 re_free (fs
->stack
);
1472 update_regs (dfa
, pmatch
, prev_idx_match
, cur_node
, cur_idx
, nmatch
)
1474 regmatch_t
*pmatch
, *prev_idx_match
;
1475 int cur_node
, cur_idx
, nmatch
;
1477 int type
= dfa
->nodes
[cur_node
].type
;
1478 if (type
== OP_OPEN_SUBEXP
)
1480 int reg_num
= dfa
->nodes
[cur_node
].opr
.idx
+ 1;
1482 /* We are at the first node of this sub expression. */
1483 if (reg_num
< nmatch
)
1485 pmatch
[reg_num
].rm_so
= cur_idx
;
1486 pmatch
[reg_num
].rm_eo
= -1;
1489 else if (type
== OP_CLOSE_SUBEXP
)
1491 int reg_num
= dfa
->nodes
[cur_node
].opr
.idx
+ 1;
1492 if (reg_num
< nmatch
)
1494 /* We are at the last node of this sub expression. */
1495 if (pmatch
[reg_num
].rm_so
< cur_idx
)
1497 pmatch
[reg_num
].rm_eo
= cur_idx
;
1498 /* This is a non-empty match or we are not inside an optional
1499 subexpression. Accept this right away. */
1500 memcpy (prev_idx_match
, pmatch
, sizeof (regmatch_t
) * nmatch
);
1504 if (dfa
->nodes
[cur_node
].opt_subexp
1505 && prev_idx_match
[reg_num
].rm_so
!= -1)
1506 /* We transited through an empty match for an optional
1507 subexpression, like (a?)*, and this is not the subexp's
1508 first match. Copy back the old content of the registers
1509 so that matches of an inner subexpression are undone as
1510 well, like in ((a?))*. */
1511 memcpy (pmatch
, prev_idx_match
, sizeof (regmatch_t
) * nmatch
);
1513 /* We completed a subexpression, but it may be part of
1514 an optional one, so do not update PREV_IDX_MATCH. */
1515 pmatch
[reg_num
].rm_eo
= cur_idx
;
1521 /* This function checks the STATE_LOG from the SCTX->last_str_idx to 0
1522 and sift the nodes in each states according to the following rules.
1523 Updated state_log will be wrote to STATE_LOG.
1525 Rules: We throw away the Node `a' in the STATE_LOG[STR_IDX] if...
1526 1. When STR_IDX == MATCH_LAST(the last index in the state_log):
1527 If `a' isn't the LAST_NODE and `a' can't epsilon transit to
1528 the LAST_NODE, we throw away the node `a'.
1529 2. When 0 <= STR_IDX < MATCH_LAST and `a' accepts
1530 string `s' and transit to `b':
1531 i. If 'b' isn't in the STATE_LOG[STR_IDX+strlen('s')], we throw
1533 ii. If 'b' is in the STATE_LOG[STR_IDX+strlen('s')] but 'b' is
1534 thrown away, we throw away the node `a'.
1535 3. When 0 <= STR_IDX < MATCH_LAST and 'a' epsilon transit to 'b':
1536 i. If 'b' isn't in the STATE_LOG[STR_IDX], we throw away the
1538 ii. If 'b' is in the STATE_LOG[STR_IDX] but 'b' is thrown away,
1539 we throw away the node `a'. */
1541 #define STATE_NODE_CONTAINS(state,node) \
1542 ((state) != NULL && re_node_set_contains (&(state)->nodes, node))
1544 static reg_errcode_t
1545 sift_states_backward (mctx
, sctx
)
1546 re_match_context_t
*mctx
;
1547 re_sift_context_t
*sctx
;
1551 int str_idx
= sctx
->last_str_idx
;
1552 re_node_set cur_dest
;
1555 assert (mctx
->state_log
!= NULL
&& mctx
->state_log
[str_idx
] != NULL
);
1558 /* Build sifted state_log[str_idx]. It has the nodes which can epsilon
1559 transit to the last_node and the last_node itself. */
1560 err
= re_node_set_init_1 (&cur_dest
, sctx
->last_node
);
1561 if (BE (err
!= REG_NOERROR
, 0))
1563 err
= update_cur_sifted_state (mctx
, sctx
, str_idx
, &cur_dest
);
1564 if (BE (err
!= REG_NOERROR
, 0))
1567 /* Then check each states in the state_log. */
1570 /* Update counters. */
1571 null_cnt
= (sctx
->sifted_states
[str_idx
] == NULL
) ? null_cnt
+ 1 : 0;
1572 if (null_cnt
> mctx
->max_mb_elem_len
)
1574 memset (sctx
->sifted_states
, '\0',
1575 sizeof (re_dfastate_t
*) * str_idx
);
1576 re_node_set_free (&cur_dest
);
1579 re_node_set_empty (&cur_dest
);
1582 if (mctx
->state_log
[str_idx
])
1584 err
= build_sifted_states (mctx
, sctx
, str_idx
, &cur_dest
);
1585 if (BE (err
!= REG_NOERROR
, 0))
1589 /* Add all the nodes which satisfy the following conditions:
1590 - It can epsilon transit to a node in CUR_DEST.
1592 And update state_log. */
1593 err
= update_cur_sifted_state (mctx
, sctx
, str_idx
, &cur_dest
);
1594 if (BE (err
!= REG_NOERROR
, 0))
1599 re_node_set_free (&cur_dest
);
1603 static reg_errcode_t
1604 build_sifted_states (mctx
, sctx
, str_idx
, cur_dest
)
1605 re_match_context_t
*mctx
;
1606 re_sift_context_t
*sctx
;
1608 re_node_set
*cur_dest
;
1610 re_dfa_t
*const dfa
= mctx
->dfa
;
1611 re_node_set
*cur_src
= &mctx
->state_log
[str_idx
]->non_eps_nodes
;
1614 /* Then build the next sifted state.
1615 We build the next sifted state on `cur_dest', and update
1616 `sifted_states[str_idx]' with `cur_dest'.
1618 `cur_dest' is the sifted state from `state_log[str_idx + 1]'.
1619 `cur_src' points the node_set of the old `state_log[str_idx]'
1620 (with the epsilon nodes pre-filtered out). */
1621 for (i
= 0; i
< cur_src
->nelem
; i
++)
1623 int prev_node
= cur_src
->elems
[i
];
1627 #if defined DEBUG || defined RE_ENABLE_I18N
1628 re_token_type_t type
= dfa
->nodes
[prev_node
].type
;
1631 assert (!IS_EPSILON_NODE (type
));
1633 #ifdef RE_ENABLE_I18N
1634 /* If the node may accept `multi byte'. */
1635 if (ACCEPT_MB_NODE (type
))
1636 naccepted
= sift_states_iter_mb (mctx
, sctx
, prev_node
,
1637 str_idx
, sctx
->last_str_idx
);
1638 #endif /* RE_ENABLE_I18N */
1640 /* We don't check backreferences here.
1641 See update_cur_sifted_state(). */
1643 && check_node_accept (mctx
, dfa
->nodes
+ prev_node
, str_idx
)
1644 && STATE_NODE_CONTAINS (sctx
->sifted_states
[str_idx
+ 1],
1645 dfa
->nexts
[prev_node
]))
1651 if (sctx
->limits
.nelem
)
1653 int to_idx
= str_idx
+ naccepted
;
1654 if (check_dst_limits (mctx
, &sctx
->limits
,
1655 dfa
->nexts
[prev_node
], to_idx
,
1656 prev_node
, str_idx
))
1659 ret
= re_node_set_insert (cur_dest
, prev_node
);
1660 if (BE (ret
== -1, 0))
1667 /* Helper functions. */
1669 static reg_errcode_t
1670 clean_state_log_if_needed (mctx
, next_state_log_idx
)
1671 re_match_context_t
*mctx
;
1672 int next_state_log_idx
;
1674 int top
= mctx
->state_log_top
;
1676 if (next_state_log_idx
>= mctx
->input
.bufs_len
1677 || (next_state_log_idx
>= mctx
->input
.valid_len
1678 && mctx
->input
.valid_len
< mctx
->input
.len
))
1681 err
= extend_buffers (mctx
);
1682 if (BE (err
!= REG_NOERROR
, 0))
1686 if (top
< next_state_log_idx
)
1688 memset (mctx
->state_log
+ top
+ 1, '\0',
1689 sizeof (re_dfastate_t
*) * (next_state_log_idx
- top
));
1690 mctx
->state_log_top
= next_state_log_idx
;
1695 static reg_errcode_t
1696 merge_state_array (dfa
, dst
, src
, num
)
1698 re_dfastate_t
**dst
;
1699 re_dfastate_t
**src
;
1704 for (st_idx
= 0; st_idx
< num
; ++st_idx
)
1706 if (dst
[st_idx
] == NULL
)
1707 dst
[st_idx
] = src
[st_idx
];
1708 else if (src
[st_idx
] != NULL
)
1710 re_node_set merged_set
;
1711 err
= re_node_set_init_union (&merged_set
, &dst
[st_idx
]->nodes
,
1712 &src
[st_idx
]->nodes
);
1713 if (BE (err
!= REG_NOERROR
, 0))
1715 dst
[st_idx
] = re_acquire_state (&err
, dfa
, &merged_set
);
1716 re_node_set_free (&merged_set
);
1717 if (BE (err
!= REG_NOERROR
, 0))
1724 static reg_errcode_t
1725 update_cur_sifted_state (mctx
, sctx
, str_idx
, dest_nodes
)
1726 re_match_context_t
*mctx
;
1727 re_sift_context_t
*sctx
;
1729 re_node_set
*dest_nodes
;
1731 re_dfa_t
*const dfa
= mctx
->dfa
;
1733 const re_node_set
*candidates
;
1734 candidates
= ((mctx
->state_log
[str_idx
] == NULL
) ? NULL
1735 : &mctx
->state_log
[str_idx
]->nodes
);
1737 if (dest_nodes
->nelem
== 0)
1738 sctx
->sifted_states
[str_idx
] = NULL
;
1743 /* At first, add the nodes which can epsilon transit to a node in
1745 err
= add_epsilon_src_nodes (dfa
, dest_nodes
, candidates
);
1746 if (BE (err
!= REG_NOERROR
, 0))
1749 /* Then, check the limitations in the current sift_context. */
1750 if (sctx
->limits
.nelem
)
1752 err
= check_subexp_limits (dfa
, dest_nodes
, candidates
, &sctx
->limits
,
1753 mctx
->bkref_ents
, str_idx
);
1754 if (BE (err
!= REG_NOERROR
, 0))
1759 sctx
->sifted_states
[str_idx
] = re_acquire_state (&err
, dfa
, dest_nodes
);
1760 if (BE (err
!= REG_NOERROR
, 0))
1764 if (candidates
&& mctx
->state_log
[str_idx
]->has_backref
)
1766 err
= sift_states_bkref (mctx
, sctx
, str_idx
, candidates
);
1767 if (BE (err
!= REG_NOERROR
, 0))
1773 static reg_errcode_t
1774 add_epsilon_src_nodes (dfa
, dest_nodes
, candidates
)
1776 re_node_set
*dest_nodes
;
1777 const re_node_set
*candidates
;
1779 reg_errcode_t err
= REG_NOERROR
;
1782 re_dfastate_t
*state
= re_acquire_state (&err
, dfa
, dest_nodes
);
1783 if (BE (err
!= REG_NOERROR
, 0))
1786 if (!state
->inveclosure
.alloc
)
1788 err
= re_node_set_alloc (&state
->inveclosure
, dest_nodes
->nelem
);
1789 if (BE (err
!= REG_NOERROR
, 0))
1791 for (i
= 0; i
< dest_nodes
->nelem
; i
++)
1792 re_node_set_merge (&state
->inveclosure
,
1793 dfa
->inveclosures
+ dest_nodes
->elems
[i
]);
1795 return re_node_set_add_intersect (dest_nodes
, candidates
,
1796 &state
->inveclosure
);
1799 static reg_errcode_t
1800 sub_epsilon_src_nodes (dfa
, node
, dest_nodes
, candidates
)
1803 re_node_set
*dest_nodes
;
1804 const re_node_set
*candidates
;
1808 re_node_set
*inv_eclosure
= dfa
->inveclosures
+ node
;
1809 re_node_set except_nodes
;
1810 re_node_set_init_empty (&except_nodes
);
1811 for (ecl_idx
= 0; ecl_idx
< inv_eclosure
->nelem
; ++ecl_idx
)
1813 int cur_node
= inv_eclosure
->elems
[ecl_idx
];
1814 if (cur_node
== node
)
1816 if (IS_EPSILON_NODE (dfa
->nodes
[cur_node
].type
))
1818 int edst1
= dfa
->edests
[cur_node
].elems
[0];
1819 int edst2
= ((dfa
->edests
[cur_node
].nelem
> 1)
1820 ? dfa
->edests
[cur_node
].elems
[1] : -1);
1821 if ((!re_node_set_contains (inv_eclosure
, edst1
)
1822 && re_node_set_contains (dest_nodes
, edst1
))
1824 && !re_node_set_contains (inv_eclosure
, edst2
)
1825 && re_node_set_contains (dest_nodes
, edst2
)))
1827 err
= re_node_set_add_intersect (&except_nodes
, candidates
,
1828 dfa
->inveclosures
+ cur_node
);
1829 if (BE (err
!= REG_NOERROR
, 0))
1831 re_node_set_free (&except_nodes
);
1837 for (ecl_idx
= 0; ecl_idx
< inv_eclosure
->nelem
; ++ecl_idx
)
1839 int cur_node
= inv_eclosure
->elems
[ecl_idx
];
1840 if (!re_node_set_contains (&except_nodes
, cur_node
))
1842 int idx
= re_node_set_contains (dest_nodes
, cur_node
) - 1;
1843 re_node_set_remove_at (dest_nodes
, idx
);
1846 re_node_set_free (&except_nodes
);
1851 check_dst_limits (mctx
, limits
, dst_node
, dst_idx
, src_node
, src_idx
)
1852 re_match_context_t
*mctx
;
1853 re_node_set
*limits
;
1854 int dst_node
, dst_idx
, src_node
, src_idx
;
1856 re_dfa_t
*const dfa
= mctx
->dfa
;
1857 int lim_idx
, src_pos
, dst_pos
;
1859 int dst_bkref_idx
= search_cur_bkref_entry (mctx
, dst_idx
);
1860 int src_bkref_idx
= search_cur_bkref_entry (mctx
, src_idx
);
1861 for (lim_idx
= 0; lim_idx
< limits
->nelem
; ++lim_idx
)
1864 struct re_backref_cache_entry
*ent
;
1865 ent
= mctx
->bkref_ents
+ limits
->elems
[lim_idx
];
1866 subexp_idx
= dfa
->nodes
[ent
->node
].opr
.idx
;
1868 dst_pos
= check_dst_limits_calc_pos (mctx
, limits
->elems
[lim_idx
],
1869 subexp_idx
, dst_node
, dst_idx
,
1871 src_pos
= check_dst_limits_calc_pos (mctx
, limits
->elems
[lim_idx
],
1872 subexp_idx
, src_node
, src_idx
,
1876 <src> <dst> ( <subexp> )
1877 ( <subexp> ) <src> <dst>
1878 ( <subexp1> <src> <subexp2> <dst> <subexp3> ) */
1879 if (src_pos
== dst_pos
)
1880 continue; /* This is unrelated limitation. */
1888 check_dst_limits_calc_pos_1 (mctx
, boundaries
, subexp_idx
, from_node
, bkref_idx
)
1889 re_match_context_t
*mctx
;
1890 int boundaries
, subexp_idx
, from_node
, bkref_idx
;
1892 re_dfa_t
*const dfa
= mctx
->dfa
;
1893 re_node_set
*eclosures
= dfa
->eclosures
+ from_node
;
1896 /* Else, we are on the boundary: examine the nodes on the epsilon
1898 for (node_idx
= 0; node_idx
< eclosures
->nelem
; ++node_idx
)
1900 int node
= eclosures
->elems
[node_idx
];
1901 switch (dfa
->nodes
[node
].type
)
1904 if (bkref_idx
!= -1)
1906 struct re_backref_cache_entry
*ent
= mctx
->bkref_ents
+ bkref_idx
;
1911 if (ent
->node
!= node
)
1914 if (subexp_idx
<= 8 * sizeof (ent
->eps_reachable_subexps_map
)
1915 && !(ent
->eps_reachable_subexps_map
& (1 << subexp_idx
)))
1918 /* Recurse trying to reach the OP_OPEN_SUBEXP and
1919 OP_CLOSE_SUBEXP cases below. But, if the
1920 destination node is the same node as the source
1921 node, don't recurse because it would cause an
1922 infinite loop: a regex that exhibits this behavior
1924 dst
= dfa
->edests
[node
].elems
[0];
1925 if (dst
== from_node
)
1929 else /* if (boundaries & 2) */
1934 check_dst_limits_calc_pos_1 (mctx
, boundaries
, subexp_idx
,
1936 if (cpos
== -1 /* && (boundaries & 1) */)
1938 if (cpos
== 0 && (boundaries
& 2))
1941 ent
->eps_reachable_subexps_map
&= ~(1 << subexp_idx
);
1943 while (ent
++->more
);
1947 case OP_OPEN_SUBEXP
:
1948 if ((boundaries
& 1) && subexp_idx
== dfa
->nodes
[node
].opr
.idx
)
1952 case OP_CLOSE_SUBEXP
:
1953 if ((boundaries
& 2) && subexp_idx
== dfa
->nodes
[node
].opr
.idx
)
1962 return (boundaries
& 2) ? 1 : 0;
1966 check_dst_limits_calc_pos (mctx
, limit
, subexp_idx
, from_node
, str_idx
, bkref_idx
)
1967 re_match_context_t
*mctx
;
1968 int limit
, subexp_idx
, from_node
, str_idx
, bkref_idx
;
1970 struct re_backref_cache_entry
*lim
= mctx
->bkref_ents
+ limit
;
1973 /* If we are outside the range of the subexpression, return -1 or 1. */
1974 if (str_idx
< lim
->subexp_from
)
1977 if (lim
->subexp_to
< str_idx
)
1980 /* If we are within the subexpression, return 0. */
1981 boundaries
= (str_idx
== lim
->subexp_from
);
1982 boundaries
|= (str_idx
== lim
->subexp_to
) << 1;
1983 if (boundaries
== 0)
1986 /* Else, examine epsilon closure. */
1987 return check_dst_limits_calc_pos_1 (mctx
, boundaries
, subexp_idx
,
1988 from_node
, bkref_idx
);
1991 /* Check the limitations of sub expressions LIMITS, and remove the nodes
1992 which are against limitations from DEST_NODES. */
1994 static reg_errcode_t
1995 check_subexp_limits (dfa
, dest_nodes
, candidates
, limits
, bkref_ents
, str_idx
)
1997 re_node_set
*dest_nodes
;
1998 const re_node_set
*candidates
;
1999 re_node_set
*limits
;
2000 struct re_backref_cache_entry
*bkref_ents
;
2004 int node_idx
, lim_idx
;
2006 for (lim_idx
= 0; lim_idx
< limits
->nelem
; ++lim_idx
)
2009 struct re_backref_cache_entry
*ent
;
2010 ent
= bkref_ents
+ limits
->elems
[lim_idx
];
2012 if (str_idx
<= ent
->subexp_from
|| ent
->str_idx
< str_idx
)
2013 continue; /* This is unrelated limitation. */
2015 subexp_idx
= dfa
->nodes
[ent
->node
].opr
.idx
;
2016 if (ent
->subexp_to
== str_idx
)
2020 for (node_idx
= 0; node_idx
< dest_nodes
->nelem
; ++node_idx
)
2022 int node
= dest_nodes
->elems
[node_idx
];
2023 re_token_type_t type
= dfa
->nodes
[node
].type
;
2024 if (type
== OP_OPEN_SUBEXP
2025 && subexp_idx
== dfa
->nodes
[node
].opr
.idx
)
2027 else if (type
== OP_CLOSE_SUBEXP
2028 && subexp_idx
== dfa
->nodes
[node
].opr
.idx
)
2032 /* Check the limitation of the open subexpression. */
2033 /* Note that (ent->subexp_to = str_idx != ent->subexp_from). */
2036 err
= sub_epsilon_src_nodes (dfa
, ops_node
, dest_nodes
,
2038 if (BE (err
!= REG_NOERROR
, 0))
2042 /* Check the limitation of the close subexpression. */
2044 for (node_idx
= 0; node_idx
< dest_nodes
->nelem
; ++node_idx
)
2046 int node
= dest_nodes
->elems
[node_idx
];
2047 if (!re_node_set_contains (dfa
->inveclosures
+ node
,
2049 && !re_node_set_contains (dfa
->eclosures
+ node
,
2052 /* It is against this limitation.
2053 Remove it form the current sifted state. */
2054 err
= sub_epsilon_src_nodes (dfa
, node
, dest_nodes
,
2056 if (BE (err
!= REG_NOERROR
, 0))
2062 else /* (ent->subexp_to != str_idx) */
2064 for (node_idx
= 0; node_idx
< dest_nodes
->nelem
; ++node_idx
)
2066 int node
= dest_nodes
->elems
[node_idx
];
2067 re_token_type_t type
= dfa
->nodes
[node
].type
;
2068 if (type
== OP_CLOSE_SUBEXP
|| type
== OP_OPEN_SUBEXP
)
2070 if (subexp_idx
!= dfa
->nodes
[node
].opr
.idx
)
2072 /* It is against this limitation.
2073 Remove it form the current sifted state. */
2074 err
= sub_epsilon_src_nodes (dfa
, node
, dest_nodes
,
2076 if (BE (err
!= REG_NOERROR
, 0))
2085 static reg_errcode_t
2086 sift_states_bkref (mctx
, sctx
, str_idx
, candidates
)
2087 re_match_context_t
*mctx
;
2088 re_sift_context_t
*sctx
;
2090 const re_node_set
*candidates
;
2092 re_dfa_t
*const dfa
= mctx
->dfa
;
2095 re_sift_context_t local_sctx
;
2096 int first_idx
= search_cur_bkref_entry (mctx
, str_idx
);
2098 if (first_idx
== -1)
2101 local_sctx
.sifted_states
= NULL
; /* Mark that it hasn't been initialized. */
2103 for (node_idx
= 0; node_idx
< candidates
->nelem
; ++node_idx
)
2106 re_token_type_t type
;
2107 struct re_backref_cache_entry
*entry
;
2108 node
= candidates
->elems
[node_idx
];
2109 type
= dfa
->nodes
[node
].type
;
2110 /* Avoid infinite loop for the REs like "()\1+". */
2111 if (node
== sctx
->last_node
&& str_idx
== sctx
->last_str_idx
)
2113 if (type
!= OP_BACK_REF
)
2116 entry
= mctx
->bkref_ents
+ first_idx
;
2117 enabled_idx
= first_idx
;
2120 int subexp_len
, to_idx
, dst_node
;
2121 re_dfastate_t
*cur_state
;
2123 if (entry
->node
!= node
)
2125 subexp_len
= entry
->subexp_to
- entry
->subexp_from
;
2126 to_idx
= str_idx
+ subexp_len
;
2127 dst_node
= (subexp_len
? dfa
->nexts
[node
]
2128 : dfa
->edests
[node
].elems
[0]);
2130 if (to_idx
> sctx
->last_str_idx
2131 || sctx
->sifted_states
[to_idx
] == NULL
2132 || !STATE_NODE_CONTAINS (sctx
->sifted_states
[to_idx
], dst_node
)
2133 || check_dst_limits (mctx
, &sctx
->limits
, node
,
2134 str_idx
, dst_node
, to_idx
))
2137 if (local_sctx
.sifted_states
== NULL
)
2140 err
= re_node_set_init_copy (&local_sctx
.limits
, &sctx
->limits
);
2141 if (BE (err
!= REG_NOERROR
, 0))
2144 local_sctx
.last_node
= node
;
2145 local_sctx
.last_str_idx
= str_idx
;
2146 err
= re_node_set_insert (&local_sctx
.limits
, enabled_idx
);
2147 if (BE (err
< 0, 0))
2152 cur_state
= local_sctx
.sifted_states
[str_idx
];
2153 err
= sift_states_backward (mctx
, &local_sctx
);
2154 if (BE (err
!= REG_NOERROR
, 0))
2156 if (sctx
->limited_states
!= NULL
)
2158 err
= merge_state_array (dfa
, sctx
->limited_states
,
2159 local_sctx
.sifted_states
,
2161 if (BE (err
!= REG_NOERROR
, 0))
2164 local_sctx
.sifted_states
[str_idx
] = cur_state
;
2165 re_node_set_remove (&local_sctx
.limits
, enabled_idx
);
2167 /* mctx->bkref_ents may have changed, reload the pointer. */
2168 entry
= mctx
->bkref_ents
+ enabled_idx
;
2170 while (enabled_idx
++, entry
++->more
);
2174 if (local_sctx
.sifted_states
!= NULL
)
2176 re_node_set_free (&local_sctx
.limits
);
2183 #ifdef RE_ENABLE_I18N
2185 sift_states_iter_mb (mctx
, sctx
, node_idx
, str_idx
, max_str_idx
)
2186 const re_match_context_t
*mctx
;
2187 re_sift_context_t
*sctx
;
2188 int node_idx
, str_idx
, max_str_idx
;
2190 re_dfa_t
*const dfa
= mctx
->dfa
;
2192 /* Check the node can accept `multi byte'. */
2193 naccepted
= check_node_accept_bytes (dfa
, node_idx
, &mctx
->input
, str_idx
);
2194 if (naccepted
> 0 && str_idx
+ naccepted
<= max_str_idx
&&
2195 !STATE_NODE_CONTAINS (sctx
->sifted_states
[str_idx
+ naccepted
],
2196 dfa
->nexts
[node_idx
]))
2197 /* The node can't accept the `multi byte', or the
2198 destination was already thrown away, then the node
2199 could't accept the current input `multi byte'. */
2201 /* Otherwise, it is sure that the node could accept
2202 `naccepted' bytes input. */
2205 #endif /* RE_ENABLE_I18N */
2208 /* Functions for state transition. */
2210 /* Return the next state to which the current state STATE will transit by
2211 accepting the current input byte, and update STATE_LOG if necessary.
2212 If STATE can accept a multibyte char/collating element/back reference
2213 update the destination of STATE_LOG. */
2215 static re_dfastate_t
*
2216 transit_state (err
, mctx
, state
)
2218 re_match_context_t
*mctx
;
2219 re_dfastate_t
*state
;
2221 re_dfastate_t
**trtable
;
2224 #ifdef RE_ENABLE_I18N
2225 /* If the current state can accept multibyte. */
2226 if (BE (state
->accept_mb
, 0))
2228 *err
= transit_state_mb (mctx
, state
);
2229 if (BE (*err
!= REG_NOERROR
, 0))
2232 #endif /* RE_ENABLE_I18N */
2234 /* Then decide the next state with the single byte. */
2237 /* don't use transition table */
2238 return transit_state_sb (err
, mctx
, state
);
2241 /* Use transition table */
2242 ch
= re_string_fetch_byte (&mctx
->input
);
2245 trtable
= state
->trtable
;
2246 if (BE (trtable
!= NULL
, 1))
2249 trtable
= state
->word_trtable
;
2250 if (BE (trtable
!= NULL
, 1))
2252 unsigned int context
;
2254 = re_string_context_at (&mctx
->input
,
2255 re_string_cur_idx (&mctx
->input
) - 1,
2257 if (IS_WORD_CONTEXT (context
))
2258 return trtable
[ch
+ SBC_MAX
];
2263 if (!build_trtable (mctx
->dfa
, state
))
2269 /* Retry, we now have a transition table. */
2273 /* Update the state_log if we need */
2275 merge_state_with_log (err
, mctx
, next_state
)
2277 re_match_context_t
*mctx
;
2278 re_dfastate_t
*next_state
;
2280 re_dfa_t
*const dfa
= mctx
->dfa
;
2281 int cur_idx
= re_string_cur_idx (&mctx
->input
);
2283 if (cur_idx
> mctx
->state_log_top
)
2285 mctx
->state_log
[cur_idx
] = next_state
;
2286 mctx
->state_log_top
= cur_idx
;
2288 else if (mctx
->state_log
[cur_idx
] == 0)
2290 mctx
->state_log
[cur_idx
] = next_state
;
2294 re_dfastate_t
*pstate
;
2295 unsigned int context
;
2296 re_node_set next_nodes
, *log_nodes
, *table_nodes
= NULL
;
2297 /* If (state_log[cur_idx] != 0), it implies that cur_idx is
2298 the destination of a multibyte char/collating element/
2299 back reference. Then the next state is the union set of
2300 these destinations and the results of the transition table. */
2301 pstate
= mctx
->state_log
[cur_idx
];
2302 log_nodes
= pstate
->entrance_nodes
;
2303 if (next_state
!= NULL
)
2305 table_nodes
= next_state
->entrance_nodes
;
2306 *err
= re_node_set_init_union (&next_nodes
, table_nodes
,
2308 if (BE (*err
!= REG_NOERROR
, 0))
2312 next_nodes
= *log_nodes
;
2313 /* Note: We already add the nodes of the initial state,
2314 then we don't need to add them here. */
2316 context
= re_string_context_at (&mctx
->input
,
2317 re_string_cur_idx (&mctx
->input
) - 1,
2319 next_state
= mctx
->state_log
[cur_idx
]
2320 = re_acquire_state_context (err
, dfa
, &next_nodes
, context
);
2321 /* We don't need to check errors here, since the return value of
2322 this function is next_state and ERR is already set. */
2324 if (table_nodes
!= NULL
)
2325 re_node_set_free (&next_nodes
);
2328 if (BE (dfa
->nbackref
, 0) && next_state
!= NULL
)
2330 /* Check OP_OPEN_SUBEXP in the current state in case that we use them
2331 later. We must check them here, since the back references in the
2332 next state might use them. */
2333 *err
= check_subexp_matching_top (mctx
, &next_state
->nodes
,
2335 if (BE (*err
!= REG_NOERROR
, 0))
2338 /* If the next state has back references. */
2339 if (next_state
->has_backref
)
2341 *err
= transit_state_bkref (mctx
, &next_state
->nodes
);
2342 if (BE (*err
!= REG_NOERROR
, 0))
2344 next_state
= mctx
->state_log
[cur_idx
];
2351 /* Skip bytes in the input that correspond to part of a
2352 multi-byte match, then look in the log for a state
2353 from which to restart matching. */
2355 find_recover_state (err
, mctx
)
2357 re_match_context_t
*mctx
;
2359 re_dfastate_t
*cur_state
= NULL
;
2362 int max
= mctx
->state_log_top
;
2363 int cur_str_idx
= re_string_cur_idx (&mctx
->input
);
2367 if (++cur_str_idx
> max
)
2369 re_string_skip_bytes (&mctx
->input
, 1);
2371 while (mctx
->state_log
[cur_str_idx
] == NULL
);
2373 cur_state
= merge_state_with_log (err
, mctx
, NULL
);
2375 while (err
== REG_NOERROR
&& cur_state
== NULL
);
2379 /* Helper functions for transit_state. */
2381 /* From the node set CUR_NODES, pick up the nodes whose types are
2382 OP_OPEN_SUBEXP and which have corresponding back references in the regular
2383 expression. And register them to use them later for evaluating the
2384 correspoding back references. */
2386 static reg_errcode_t
2387 check_subexp_matching_top (mctx
, cur_nodes
, str_idx
)
2388 re_match_context_t
*mctx
;
2389 re_node_set
*cur_nodes
;
2392 re_dfa_t
*const dfa
= mctx
->dfa
;
2396 /* TODO: This isn't efficient.
2397 Because there might be more than one nodes whose types are
2398 OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all
2401 for (node_idx
= 0; node_idx
< cur_nodes
->nelem
; ++node_idx
)
2403 int node
= cur_nodes
->elems
[node_idx
];
2404 if (dfa
->nodes
[node
].type
== OP_OPEN_SUBEXP
2405 && dfa
->nodes
[node
].opr
.idx
< (8 * sizeof (dfa
->used_bkref_map
))
2406 && dfa
->used_bkref_map
& (1 << dfa
->nodes
[node
].opr
.idx
))
2408 err
= match_ctx_add_subtop (mctx
, node
, str_idx
);
2409 if (BE (err
!= REG_NOERROR
, 0))
2417 /* Return the next state to which the current state STATE will transit by
2418 accepting the current input byte. */
2420 static re_dfastate_t
*
2421 transit_state_sb (err
, mctx
, state
)
2423 re_match_context_t
*mctx
;
2424 re_dfastate_t
*state
;
2426 re_dfa_t
*const dfa
= mctx
->dfa
;
2427 re_node_set next_nodes
;
2428 re_dfastate_t
*next_state
;
2429 int node_cnt
, cur_str_idx
= re_string_cur_idx (&mctx
->input
);
2430 unsigned int context
;
2432 *err
= re_node_set_alloc (&next_nodes
, state
->nodes
.nelem
+ 1);
2433 if (BE (*err
!= REG_NOERROR
, 0))
2435 for (node_cnt
= 0; node_cnt
< state
->nodes
.nelem
; ++node_cnt
)
2437 int cur_node
= state
->nodes
.elems
[node_cnt
];
2438 if (check_node_accept (mctx
, dfa
->nodes
+ cur_node
, cur_str_idx
))
2440 *err
= re_node_set_merge (&next_nodes
,
2441 dfa
->eclosures
+ dfa
->nexts
[cur_node
]);
2442 if (BE (*err
!= REG_NOERROR
, 0))
2444 re_node_set_free (&next_nodes
);
2449 context
= re_string_context_at (&mctx
->input
, cur_str_idx
, mctx
->eflags
);
2450 next_state
= re_acquire_state_context (err
, dfa
, &next_nodes
, context
);
2451 /* We don't need to check errors here, since the return value of
2452 this function is next_state and ERR is already set. */
2454 re_node_set_free (&next_nodes
);
2455 re_string_skip_bytes (&mctx
->input
, 1);
2460 #ifdef RE_ENABLE_I18N
2461 static reg_errcode_t
2462 transit_state_mb (mctx
, pstate
)
2463 re_match_context_t
*mctx
;
2464 re_dfastate_t
*pstate
;
2466 re_dfa_t
*const dfa
= mctx
->dfa
;
2470 for (i
= 0; i
< pstate
->nodes
.nelem
; ++i
)
2472 re_node_set dest_nodes
, *new_nodes
;
2473 int cur_node_idx
= pstate
->nodes
.elems
[i
];
2474 int naccepted
= 0, dest_idx
;
2475 unsigned int context
;
2476 re_dfastate_t
*dest_state
;
2478 if (dfa
->nodes
[cur_node_idx
].constraint
)
2480 context
= re_string_context_at (&mctx
->input
,
2481 re_string_cur_idx (&mctx
->input
),
2483 if (NOT_SATISFY_NEXT_CONSTRAINT (dfa
->nodes
[cur_node_idx
].constraint
,
2488 /* How many bytes the node can accept? */
2489 if (ACCEPT_MB_NODE (dfa
->nodes
[cur_node_idx
].type
))
2490 naccepted
= check_node_accept_bytes (dfa
, cur_node_idx
, &mctx
->input
,
2491 re_string_cur_idx (&mctx
->input
));
2495 /* The node can accepts `naccepted' bytes. */
2496 dest_idx
= re_string_cur_idx (&mctx
->input
) + naccepted
;
2497 mctx
->max_mb_elem_len
= ((mctx
->max_mb_elem_len
< naccepted
) ? naccepted
2498 : mctx
->max_mb_elem_len
);
2499 err
= clean_state_log_if_needed (mctx
, dest_idx
);
2500 if (BE (err
!= REG_NOERROR
, 0))
2503 assert (dfa
->nexts
[cur_node_idx
] != -1);
2505 /* `cur_node_idx' may point the entity of the OP_CONTEXT_NODE,
2506 then we use pstate->nodes.elems[i] instead. */
2507 new_nodes
= dfa
->eclosures
+ dfa
->nexts
[pstate
->nodes
.elems
[i
]];
2509 dest_state
= mctx
->state_log
[dest_idx
];
2510 if (dest_state
== NULL
)
2511 dest_nodes
= *new_nodes
;
2514 err
= re_node_set_init_union (&dest_nodes
,
2515 dest_state
->entrance_nodes
, new_nodes
);
2516 if (BE (err
!= REG_NOERROR
, 0))
2519 context
= re_string_context_at (&mctx
->input
, dest_idx
- 1, mctx
->eflags
);
2520 mctx
->state_log
[dest_idx
]
2521 = re_acquire_state_context (&err
, dfa
, &dest_nodes
, context
);
2522 if (dest_state
!= NULL
)
2523 re_node_set_free (&dest_nodes
);
2524 if (BE (mctx
->state_log
[dest_idx
] == NULL
&& err
!= REG_NOERROR
, 0))
2529 #endif /* RE_ENABLE_I18N */
2531 static reg_errcode_t
2532 transit_state_bkref (mctx
, nodes
)
2533 re_match_context_t
*mctx
;
2534 const re_node_set
*nodes
;
2536 re_dfa_t
*const dfa
= mctx
->dfa
;
2539 int cur_str_idx
= re_string_cur_idx (&mctx
->input
);
2541 for (i
= 0; i
< nodes
->nelem
; ++i
)
2543 int dest_str_idx
, prev_nelem
, bkc_idx
;
2544 int node_idx
= nodes
->elems
[i
];
2545 unsigned int context
;
2546 const re_token_t
*node
= dfa
->nodes
+ node_idx
;
2547 re_node_set
*new_dest_nodes
;
2549 /* Check whether `node' is a backreference or not. */
2550 if (node
->type
!= OP_BACK_REF
)
2553 if (node
->constraint
)
2555 context
= re_string_context_at (&mctx
->input
, cur_str_idx
,
2557 if (NOT_SATISFY_NEXT_CONSTRAINT (node
->constraint
, context
))
2561 /* `node' is a backreference.
2562 Check the substring which the substring matched. */
2563 bkc_idx
= mctx
->nbkref_ents
;
2564 err
= get_subexp (mctx
, node_idx
, cur_str_idx
);
2565 if (BE (err
!= REG_NOERROR
, 0))
2568 /* And add the epsilon closures (which is `new_dest_nodes') of
2569 the backreference to appropriate state_log. */
2571 assert (dfa
->nexts
[node_idx
] != -1);
2573 for (; bkc_idx
< mctx
->nbkref_ents
; ++bkc_idx
)
2576 re_dfastate_t
*dest_state
;
2577 struct re_backref_cache_entry
*bkref_ent
;
2578 bkref_ent
= mctx
->bkref_ents
+ bkc_idx
;
2579 if (bkref_ent
->node
!= node_idx
|| bkref_ent
->str_idx
!= cur_str_idx
)
2581 subexp_len
= bkref_ent
->subexp_to
- bkref_ent
->subexp_from
;
2582 new_dest_nodes
= (subexp_len
== 0
2583 ? dfa
->eclosures
+ dfa
->edests
[node_idx
].elems
[0]
2584 : dfa
->eclosures
+ dfa
->nexts
[node_idx
]);
2585 dest_str_idx
= (cur_str_idx
+ bkref_ent
->subexp_to
2586 - bkref_ent
->subexp_from
);
2587 context
= re_string_context_at (&mctx
->input
, dest_str_idx
- 1,
2589 dest_state
= mctx
->state_log
[dest_str_idx
];
2590 prev_nelem
= ((mctx
->state_log
[cur_str_idx
] == NULL
) ? 0
2591 : mctx
->state_log
[cur_str_idx
]->nodes
.nelem
);
2592 /* Add `new_dest_node' to state_log. */
2593 if (dest_state
== NULL
)
2595 mctx
->state_log
[dest_str_idx
]
2596 = re_acquire_state_context (&err
, dfa
, new_dest_nodes
,
2598 if (BE (mctx
->state_log
[dest_str_idx
] == NULL
2599 && err
!= REG_NOERROR
, 0))
2604 re_node_set dest_nodes
;
2605 err
= re_node_set_init_union (&dest_nodes
,
2606 dest_state
->entrance_nodes
,
2608 if (BE (err
!= REG_NOERROR
, 0))
2610 re_node_set_free (&dest_nodes
);
2613 mctx
->state_log
[dest_str_idx
]
2614 = re_acquire_state_context (&err
, dfa
, &dest_nodes
, context
);
2615 re_node_set_free (&dest_nodes
);
2616 if (BE (mctx
->state_log
[dest_str_idx
] == NULL
2617 && err
!= REG_NOERROR
, 0))
2620 /* We need to check recursively if the backreference can epsilon
2623 && mctx
->state_log
[cur_str_idx
]->nodes
.nelem
> prev_nelem
)
2625 err
= check_subexp_matching_top (mctx
, new_dest_nodes
,
2627 if (BE (err
!= REG_NOERROR
, 0))
2629 err
= transit_state_bkref (mctx
, new_dest_nodes
);
2630 if (BE (err
!= REG_NOERROR
, 0))
2640 /* Enumerate all the candidates which the backreference BKREF_NODE can match
2641 at BKREF_STR_IDX, and register them by match_ctx_add_entry().
2642 Note that we might collect inappropriate candidates here.
2643 However, the cost of checking them strictly here is too high, then we
2644 delay these checking for prune_impossible_nodes(). */
2646 static reg_errcode_t
2647 get_subexp (mctx
, bkref_node
, bkref_str_idx
)
2648 re_match_context_t
*mctx
;
2649 int bkref_node
, bkref_str_idx
;
2651 re_dfa_t
*const dfa
= mctx
->dfa
;
2652 int subexp_num
, sub_top_idx
;
2653 const char *buf
= (const char *) re_string_get_buffer (&mctx
->input
);
2654 /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX. */
2655 int cache_idx
= search_cur_bkref_entry (mctx
, bkref_str_idx
);
2656 if (cache_idx
!= -1)
2658 const struct re_backref_cache_entry
*entry
= mctx
->bkref_ents
+ cache_idx
;
2660 if (entry
->node
== bkref_node
)
2661 return REG_NOERROR
; /* We already checked it. */
2662 while (entry
++->more
);
2665 subexp_num
= dfa
->nodes
[bkref_node
].opr
.idx
;
2667 /* For each sub expression */
2668 for (sub_top_idx
= 0; sub_top_idx
< mctx
->nsub_tops
; ++sub_top_idx
)
2671 re_sub_match_top_t
*sub_top
= mctx
->sub_tops
[sub_top_idx
];
2672 re_sub_match_last_t
*sub_last
;
2673 int sub_last_idx
, sl_str
, bkref_str_off
;
2675 if (dfa
->nodes
[sub_top
->node
].opr
.idx
!= subexp_num
)
2676 continue; /* It isn't related. */
2678 sl_str
= sub_top
->str_idx
;
2679 bkref_str_off
= bkref_str_idx
;
2680 /* At first, check the last node of sub expressions we already
2682 for (sub_last_idx
= 0; sub_last_idx
< sub_top
->nlasts
; ++sub_last_idx
)
2685 sub_last
= sub_top
->lasts
[sub_last_idx
];
2686 sl_str_diff
= sub_last
->str_idx
- sl_str
;
2687 /* The matched string by the sub expression match with the substring
2688 at the back reference? */
2689 if (sl_str_diff
> 0)
2691 if (BE (bkref_str_off
+ sl_str_diff
> mctx
->input
.valid_len
, 0))
2693 /* Not enough chars for a successful match. */
2694 if (bkref_str_off
+ sl_str_diff
> mctx
->input
.len
)
2697 err
= clean_state_log_if_needed (mctx
,
2700 if (BE (err
!= REG_NOERROR
, 0))
2702 buf
= (const char *) re_string_get_buffer (&mctx
->input
);
2704 if (memcmp (buf
+ bkref_str_off
, buf
+ sl_str
, sl_str_diff
) != 0)
2705 break; /* We don't need to search this sub expression any more. */
2707 bkref_str_off
+= sl_str_diff
;
2708 sl_str
+= sl_str_diff
;
2709 err
= get_subexp_sub (mctx
, sub_top
, sub_last
, bkref_node
,
2712 /* Reload buf, since the preceding call might have reallocated
2714 buf
= (const char *) re_string_get_buffer (&mctx
->input
);
2716 if (err
== REG_NOMATCH
)
2718 if (BE (err
!= REG_NOERROR
, 0))
2722 if (sub_last_idx
< sub_top
->nlasts
)
2724 if (sub_last_idx
> 0)
2726 /* Then, search for the other last nodes of the sub expression. */
2727 for (; sl_str
<= bkref_str_idx
; ++sl_str
)
2729 int cls_node
, sl_str_off
;
2730 const re_node_set
*nodes
;
2731 sl_str_off
= sl_str
- sub_top
->str_idx
;
2732 /* The matched string by the sub expression match with the substring
2733 at the back reference? */
2736 if (BE (bkref_str_off
>= mctx
->input
.valid_len
, 0))
2738 /* If we are at the end of the input, we cannot match. */
2739 if (bkref_str_off
>= mctx
->input
.len
)
2742 err
= extend_buffers (mctx
);
2743 if (BE (err
!= REG_NOERROR
, 0))
2746 buf
= (const char *) re_string_get_buffer (&mctx
->input
);
2748 if (buf
[bkref_str_off
++] != buf
[sl_str
- 1])
2749 break; /* We don't need to search this sub expression
2752 if (mctx
->state_log
[sl_str
] == NULL
)
2754 /* Does this state have a ')' of the sub expression? */
2755 nodes
= &mctx
->state_log
[sl_str
]->nodes
;
2756 cls_node
= find_subexp_node (dfa
, nodes
, subexp_num
, OP_CLOSE_SUBEXP
);
2759 if (sub_top
->path
== NULL
)
2761 sub_top
->path
= calloc (sizeof (state_array_t
),
2762 sl_str
- sub_top
->str_idx
+ 1);
2763 if (sub_top
->path
== NULL
)
2766 /* Can the OP_OPEN_SUBEXP node arrive the OP_CLOSE_SUBEXP node
2767 in the current context? */
2768 err
= check_arrival (mctx
, sub_top
->path
, sub_top
->node
,
2769 sub_top
->str_idx
, cls_node
, sl_str
, OP_CLOSE_SUBEXP
);
2770 if (err
== REG_NOMATCH
)
2772 if (BE (err
!= REG_NOERROR
, 0))
2774 sub_last
= match_ctx_add_sublast (sub_top
, cls_node
, sl_str
);
2775 if (BE (sub_last
== NULL
, 0))
2777 err
= get_subexp_sub (mctx
, sub_top
, sub_last
, bkref_node
,
2779 if (err
== REG_NOMATCH
)
2786 /* Helper functions for get_subexp(). */
2788 /* Check SUB_LAST can arrive to the back reference BKREF_NODE at BKREF_STR.
2789 If it can arrive, register the sub expression expressed with SUB_TOP
2792 static reg_errcode_t
2793 get_subexp_sub (mctx
, sub_top
, sub_last
, bkref_node
, bkref_str
)
2794 re_match_context_t
*mctx
;
2795 const re_sub_match_top_t
*sub_top
;
2796 re_sub_match_last_t
*sub_last
;
2797 int bkref_node
, bkref_str
;
2801 /* Can the subexpression arrive the back reference? */
2802 err
= check_arrival (mctx
, &sub_last
->path
, sub_last
->node
,
2803 sub_last
->str_idx
, bkref_node
, bkref_str
, OP_OPEN_SUBEXP
);
2804 if (err
!= REG_NOERROR
)
2806 err
= match_ctx_add_entry (mctx
, bkref_node
, bkref_str
, sub_top
->str_idx
,
2808 if (BE (err
!= REG_NOERROR
, 0))
2810 to_idx
= bkref_str
+ sub_last
->str_idx
- sub_top
->str_idx
;
2811 return clean_state_log_if_needed (mctx
, to_idx
);
2814 /* Find the first node which is '(' or ')' and whose index is SUBEXP_IDX.
2815 Search '(' if FL_OPEN, or search ')' otherwise.
2816 TODO: This function isn't efficient...
2817 Because there might be more than one nodes whose types are
2818 OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all
2823 find_subexp_node (dfa
, nodes
, subexp_idx
, type
)
2824 const re_dfa_t
*dfa
;
2825 const re_node_set
*nodes
;
2826 int subexp_idx
, type
;
2829 for (cls_idx
= 0; cls_idx
< nodes
->nelem
; ++cls_idx
)
2831 int cls_node
= nodes
->elems
[cls_idx
];
2832 const re_token_t
*node
= dfa
->nodes
+ cls_node
;
2833 if (node
->type
== type
2834 && node
->opr
.idx
== subexp_idx
)
2840 /* Check whether the node TOP_NODE at TOP_STR can arrive to the node
2841 LAST_NODE at LAST_STR. We record the path onto PATH since it will be
2843 Return REG_NOERROR if it can arrive, or REG_NOMATCH otherwise. */
2845 static reg_errcode_t
2846 check_arrival (mctx
, path
, top_node
, top_str
, last_node
, last_str
,
2848 re_match_context_t
*mctx
;
2849 state_array_t
*path
;
2850 int top_node
, top_str
, last_node
, last_str
, type
;
2852 re_dfa_t
*const dfa
= mctx
->dfa
;
2854 int subexp_num
, backup_cur_idx
, str_idx
, null_cnt
;
2855 re_dfastate_t
*cur_state
= NULL
;
2856 re_node_set
*cur_nodes
, next_nodes
;
2857 re_dfastate_t
**backup_state_log
;
2858 unsigned int context
;
2860 subexp_num
= dfa
->nodes
[top_node
].opr
.idx
;
2861 /* Extend the buffer if we need. */
2862 if (BE (path
->alloc
< last_str
+ mctx
->max_mb_elem_len
+ 1, 0))
2864 re_dfastate_t
**new_array
;
2865 int old_alloc
= path
->alloc
;
2866 path
->alloc
+= last_str
+ mctx
->max_mb_elem_len
+ 1;
2867 new_array
= re_realloc (path
->array
, re_dfastate_t
*, path
->alloc
);
2868 if (new_array
== NULL
)
2870 path
->alloc
= old_alloc
;
2873 path
->array
= new_array
;
2874 memset (new_array
+ old_alloc
, '\0',
2875 sizeof (re_dfastate_t
*) * (path
->alloc
- old_alloc
));
2878 str_idx
= path
->next_idx
== 0 ? top_str
: path
->next_idx
;
2880 /* Temporary modify MCTX. */
2881 backup_state_log
= mctx
->state_log
;
2882 backup_cur_idx
= mctx
->input
.cur_idx
;
2883 mctx
->state_log
= path
->array
;
2884 mctx
->input
.cur_idx
= str_idx
;
2886 /* Setup initial node set. */
2887 context
= re_string_context_at (&mctx
->input
, str_idx
- 1, mctx
->eflags
);
2888 if (str_idx
== top_str
)
2890 err
= re_node_set_init_1 (&next_nodes
, top_node
);
2891 if (BE (err
!= REG_NOERROR
, 0))
2893 err
= check_arrival_expand_ecl (dfa
, &next_nodes
, subexp_num
, type
);
2894 if (BE (err
!= REG_NOERROR
, 0))
2896 re_node_set_free (&next_nodes
);
2902 cur_state
= mctx
->state_log
[str_idx
];
2903 if (cur_state
&& cur_state
->has_backref
)
2905 err
= re_node_set_init_copy (&next_nodes
, &cur_state
->nodes
);
2906 if (BE ( err
!= REG_NOERROR
, 0))
2910 re_node_set_init_empty (&next_nodes
);
2912 if (str_idx
== top_str
|| (cur_state
&& cur_state
->has_backref
))
2914 if (next_nodes
.nelem
)
2916 err
= expand_bkref_cache (mctx
, &next_nodes
, str_idx
,
2918 if (BE ( err
!= REG_NOERROR
, 0))
2920 re_node_set_free (&next_nodes
);
2924 cur_state
= re_acquire_state_context (&err
, dfa
, &next_nodes
, context
);
2925 if (BE (cur_state
== NULL
&& err
!= REG_NOERROR
, 0))
2927 re_node_set_free (&next_nodes
);
2930 mctx
->state_log
[str_idx
] = cur_state
;
2933 for (null_cnt
= 0; str_idx
< last_str
&& null_cnt
<= mctx
->max_mb_elem_len
;)
2935 re_node_set_empty (&next_nodes
);
2936 if (mctx
->state_log
[str_idx
+ 1])
2938 err
= re_node_set_merge (&next_nodes
,
2939 &mctx
->state_log
[str_idx
+ 1]->nodes
);
2940 if (BE (err
!= REG_NOERROR
, 0))
2942 re_node_set_free (&next_nodes
);
2948 err
= check_arrival_add_next_nodes (mctx
, str_idx
,
2949 &cur_state
->non_eps_nodes
, &next_nodes
);
2950 if (BE (err
!= REG_NOERROR
, 0))
2952 re_node_set_free (&next_nodes
);
2957 if (next_nodes
.nelem
)
2959 err
= check_arrival_expand_ecl (dfa
, &next_nodes
, subexp_num
, type
);
2960 if (BE (err
!= REG_NOERROR
, 0))
2962 re_node_set_free (&next_nodes
);
2965 err
= expand_bkref_cache (mctx
, &next_nodes
, str_idx
,
2967 if (BE ( err
!= REG_NOERROR
, 0))
2969 re_node_set_free (&next_nodes
);
2973 context
= re_string_context_at (&mctx
->input
, str_idx
- 1, mctx
->eflags
);
2974 cur_state
= re_acquire_state_context (&err
, dfa
, &next_nodes
, context
);
2975 if (BE (cur_state
== NULL
&& err
!= REG_NOERROR
, 0))
2977 re_node_set_free (&next_nodes
);
2980 mctx
->state_log
[str_idx
] = cur_state
;
2981 null_cnt
= cur_state
== NULL
? null_cnt
+ 1 : 0;
2983 re_node_set_free (&next_nodes
);
2984 cur_nodes
= (mctx
->state_log
[last_str
] == NULL
? NULL
2985 : &mctx
->state_log
[last_str
]->nodes
);
2986 path
->next_idx
= str_idx
;
2989 mctx
->state_log
= backup_state_log
;
2990 mctx
->input
.cur_idx
= backup_cur_idx
;
2992 /* Then check the current node set has the node LAST_NODE. */
2993 if (cur_nodes
!= NULL
&& re_node_set_contains (cur_nodes
, last_node
))
2999 /* Helper functions for check_arrival. */
3001 /* Calculate the destination nodes of CUR_NODES at STR_IDX, and append them
3003 TODO: This function is similar to the functions transit_state*(),
3004 however this function has many additional works.
3005 Can't we unify them? */
3007 static reg_errcode_t
3008 check_arrival_add_next_nodes (mctx
, str_idx
, cur_nodes
, next_nodes
)
3009 re_match_context_t
*mctx
;
3011 re_node_set
*cur_nodes
, *next_nodes
;
3013 re_dfa_t
*const dfa
= mctx
->dfa
;
3017 re_node_set union_set
;
3018 re_node_set_init_empty (&union_set
);
3019 for (cur_idx
= 0; cur_idx
< cur_nodes
->nelem
; ++cur_idx
)
3022 int cur_node
= cur_nodes
->elems
[cur_idx
];
3023 #if defined DEBUG || defined RE_ENABLE_I18N
3024 re_token_type_t type
= dfa
->nodes
[cur_node
].type
;
3027 assert (!IS_EPSILON_NODE (type
));
3029 #ifdef RE_ENABLE_I18N
3030 /* If the node may accept `multi byte'. */
3031 if (ACCEPT_MB_NODE (type
))
3033 naccepted
= check_node_accept_bytes (dfa
, cur_node
, &mctx
->input
,
3037 re_dfastate_t
*dest_state
;
3038 int next_node
= dfa
->nexts
[cur_node
];
3039 int next_idx
= str_idx
+ naccepted
;
3040 dest_state
= mctx
->state_log
[next_idx
];
3041 re_node_set_empty (&union_set
);
3044 err
= re_node_set_merge (&union_set
, &dest_state
->nodes
);
3045 if (BE (err
!= REG_NOERROR
, 0))
3047 re_node_set_free (&union_set
);
3051 result
= re_node_set_insert (&union_set
, next_node
);
3052 if (BE (result
< 0, 0))
3054 re_node_set_free (&union_set
);
3057 mctx
->state_log
[next_idx
] = re_acquire_state (&err
, dfa
,
3059 if (BE (mctx
->state_log
[next_idx
] == NULL
3060 && err
!= REG_NOERROR
, 0))
3062 re_node_set_free (&union_set
);
3067 #endif /* RE_ENABLE_I18N */
3069 || check_node_accept (mctx
, dfa
->nodes
+ cur_node
, str_idx
))
3071 result
= re_node_set_insert (next_nodes
, dfa
->nexts
[cur_node
]);
3072 if (BE (result
< 0, 0))
3074 re_node_set_free (&union_set
);
3079 re_node_set_free (&union_set
);
3083 /* For all the nodes in CUR_NODES, add the epsilon closures of them to
3084 CUR_NODES, however exclude the nodes which are:
3085 - inside the sub expression whose number is EX_SUBEXP, if FL_OPEN.
3086 - out of the sub expression whose number is EX_SUBEXP, if !FL_OPEN.
3089 static reg_errcode_t
3090 check_arrival_expand_ecl (dfa
, cur_nodes
, ex_subexp
, type
)
3092 re_node_set
*cur_nodes
;
3093 int ex_subexp
, type
;
3096 int idx
, outside_node
;
3097 re_node_set new_nodes
;
3099 assert (cur_nodes
->nelem
);
3101 err
= re_node_set_alloc (&new_nodes
, cur_nodes
->nelem
);
3102 if (BE (err
!= REG_NOERROR
, 0))
3104 /* Create a new node set NEW_NODES with the nodes which are epsilon
3105 closures of the node in CUR_NODES. */
3107 for (idx
= 0; idx
< cur_nodes
->nelem
; ++idx
)
3109 int cur_node
= cur_nodes
->elems
[idx
];
3110 re_node_set
*eclosure
= dfa
->eclosures
+ cur_node
;
3111 outside_node
= find_subexp_node (dfa
, eclosure
, ex_subexp
, type
);
3112 if (outside_node
== -1)
3114 /* There are no problematic nodes, just merge them. */
3115 err
= re_node_set_merge (&new_nodes
, eclosure
);
3116 if (BE (err
!= REG_NOERROR
, 0))
3118 re_node_set_free (&new_nodes
);
3124 /* There are problematic nodes, re-calculate incrementally. */
3125 err
= check_arrival_expand_ecl_sub (dfa
, &new_nodes
, cur_node
,
3127 if (BE (err
!= REG_NOERROR
, 0))
3129 re_node_set_free (&new_nodes
);
3134 re_node_set_free (cur_nodes
);
3135 *cur_nodes
= new_nodes
;
3139 /* Helper function for check_arrival_expand_ecl.
3140 Check incrementally the epsilon closure of TARGET, and if it isn't
3141 problematic append it to DST_NODES. */
3143 static reg_errcode_t
3144 check_arrival_expand_ecl_sub (dfa
, dst_nodes
, target
, ex_subexp
, type
)
3146 int target
, ex_subexp
, type
;
3147 re_node_set
*dst_nodes
;
3150 for (cur_node
= target
; !re_node_set_contains (dst_nodes
, cur_node
);)
3154 if (dfa
->nodes
[cur_node
].type
== type
3155 && dfa
->nodes
[cur_node
].opr
.idx
== ex_subexp
)
3157 if (type
== OP_CLOSE_SUBEXP
)
3159 err
= re_node_set_insert (dst_nodes
, cur_node
);
3160 if (BE (err
== -1, 0))
3165 err
= re_node_set_insert (dst_nodes
, cur_node
);
3166 if (BE (err
== -1, 0))
3168 if (dfa
->edests
[cur_node
].nelem
== 0)
3170 if (dfa
->edests
[cur_node
].nelem
== 2)
3172 err
= check_arrival_expand_ecl_sub (dfa
, dst_nodes
,
3173 dfa
->edests
[cur_node
].elems
[1],
3175 if (BE (err
!= REG_NOERROR
, 0))
3178 cur_node
= dfa
->edests
[cur_node
].elems
[0];
3184 /* For all the back references in the current state, calculate the
3185 destination of the back references by the appropriate entry
3186 in MCTX->BKREF_ENTS. */
3188 static reg_errcode_t
3189 expand_bkref_cache (mctx
, cur_nodes
, cur_str
, subexp_num
,
3191 re_match_context_t
*mctx
;
3192 int cur_str
, subexp_num
, type
;
3193 re_node_set
*cur_nodes
;
3195 re_dfa_t
*const dfa
= mctx
->dfa
;
3197 int cache_idx_start
= search_cur_bkref_entry (mctx
, cur_str
);
3198 struct re_backref_cache_entry
*ent
;
3200 if (cache_idx_start
== -1)
3204 ent
= mctx
->bkref_ents
+ cache_idx_start
;
3207 int to_idx
, next_node
;
3209 /* Is this entry ENT is appropriate? */
3210 if (!re_node_set_contains (cur_nodes
, ent
->node
))
3213 to_idx
= cur_str
+ ent
->subexp_to
- ent
->subexp_from
;
3214 /* Calculate the destination of the back reference, and append it
3215 to MCTX->STATE_LOG. */
3216 if (to_idx
== cur_str
)
3218 /* The backreference did epsilon transit, we must re-check all the
3219 node in the current state. */
3220 re_node_set new_dests
;
3221 reg_errcode_t err2
, err3
;
3222 next_node
= dfa
->edests
[ent
->node
].elems
[0];
3223 if (re_node_set_contains (cur_nodes
, next_node
))
3225 err
= re_node_set_init_1 (&new_dests
, next_node
);
3226 err2
= check_arrival_expand_ecl (dfa
, &new_dests
, subexp_num
, type
);
3227 err3
= re_node_set_merge (cur_nodes
, &new_dests
);
3228 re_node_set_free (&new_dests
);
3229 if (BE (err
!= REG_NOERROR
|| err2
!= REG_NOERROR
3230 || err3
!= REG_NOERROR
, 0))
3232 err
= (err
!= REG_NOERROR
? err
3233 : (err2
!= REG_NOERROR
? err2
: err3
));
3236 /* TODO: It is still inefficient... */
3241 re_node_set union_set
;
3242 next_node
= dfa
->nexts
[ent
->node
];
3243 if (mctx
->state_log
[to_idx
])
3246 if (re_node_set_contains (&mctx
->state_log
[to_idx
]->nodes
,
3249 err
= re_node_set_init_copy (&union_set
,
3250 &mctx
->state_log
[to_idx
]->nodes
);
3251 ret
= re_node_set_insert (&union_set
, next_node
);
3252 if (BE (err
!= REG_NOERROR
|| ret
< 0, 0))
3254 re_node_set_free (&union_set
);
3255 err
= err
!= REG_NOERROR
? err
: REG_ESPACE
;
3261 err
= re_node_set_init_1 (&union_set
, next_node
);
3262 if (BE (err
!= REG_NOERROR
, 0))
3265 mctx
->state_log
[to_idx
] = re_acquire_state (&err
, dfa
, &union_set
);
3266 re_node_set_free (&union_set
);
3267 if (BE (mctx
->state_log
[to_idx
] == NULL
3268 && err
!= REG_NOERROR
, 0))
3272 while (ent
++->more
);
3276 /* Build transition table for the state.
3277 Return 1 if succeeded, otherwise return NULL. */
3280 build_trtable (dfa
, state
)
3282 re_dfastate_t
*state
;
3285 int i
, j
, ch
, need_word_trtable
= 0;
3286 unsigned int elem
, mask
;
3287 int dests_node_malloced
= 0, dest_states_malloced
= 0;
3288 int ndests
; /* Number of the destination states from `state'. */
3289 re_dfastate_t
**trtable
;
3290 re_dfastate_t
**dest_states
= NULL
, **dest_states_word
, **dest_states_nl
;
3291 re_node_set follows
, *dests_node
;
3295 /* We build DFA states which corresponds to the destination nodes
3296 from `state'. `dests_node[i]' represents the nodes which i-th
3297 destination state contains, and `dests_ch[i]' represents the
3298 characters which i-th destination state accepts. */
3300 if (__libc_use_alloca ((sizeof (re_node_set
) + sizeof (bitset
)) * SBC_MAX
))
3301 dests_node
= (re_node_set
*)
3302 alloca ((sizeof (re_node_set
) + sizeof (bitset
)) * SBC_MAX
);
3306 dests_node
= (re_node_set
*)
3307 malloc ((sizeof (re_node_set
) + sizeof (bitset
)) * SBC_MAX
);
3308 if (BE (dests_node
== NULL
, 0))
3310 dests_node_malloced
= 1;
3312 dests_ch
= (bitset
*) (dests_node
+ SBC_MAX
);
3314 /* Initialize transiton table. */
3315 state
->word_trtable
= state
->trtable
= NULL
;
3317 /* At first, group all nodes belonging to `state' into several
3319 ndests
= group_nodes_into_DFAstates (dfa
, state
, dests_node
, dests_ch
);
3320 if (BE (ndests
<= 0, 0))
3322 if (dests_node_malloced
)
3324 /* Return 0 in case of an error, 1 otherwise. */
3327 state
->trtable
= (re_dfastate_t
**)
3328 calloc (sizeof (re_dfastate_t
*), SBC_MAX
);
3334 err
= re_node_set_alloc (&follows
, ndests
+ 1);
3335 if (BE (err
!= REG_NOERROR
, 0))
3339 if (__libc_use_alloca ((sizeof (re_node_set
) + sizeof (bitset
)) * SBC_MAX
3340 + ndests
* 3 * sizeof (re_dfastate_t
*)))
3341 dest_states
= (re_dfastate_t
**)
3342 alloca (ndests
* 3 * sizeof (re_dfastate_t
*));
3346 dest_states
= (re_dfastate_t
**)
3347 malloc (ndests
* 3 * sizeof (re_dfastate_t
*));
3348 if (BE (dest_states
== NULL
, 0))
3351 if (dest_states_malloced
)
3353 re_node_set_free (&follows
);
3354 for (i
= 0; i
< ndests
; ++i
)
3355 re_node_set_free (dests_node
+ i
);
3356 if (dests_node_malloced
)
3360 dest_states_malloced
= 1;
3362 dest_states_word
= dest_states
+ ndests
;
3363 dest_states_nl
= dest_states_word
+ ndests
;
3364 bitset_empty (acceptable
);
3366 /* Then build the states for all destinations. */
3367 for (i
= 0; i
< ndests
; ++i
)
3370 re_node_set_empty (&follows
);
3371 /* Merge the follows of this destination states. */
3372 for (j
= 0; j
< dests_node
[i
].nelem
; ++j
)
3374 next_node
= dfa
->nexts
[dests_node
[i
].elems
[j
]];
3375 if (next_node
!= -1)
3377 err
= re_node_set_merge (&follows
, dfa
->eclosures
+ next_node
);
3378 if (BE (err
!= REG_NOERROR
, 0))
3382 dest_states
[i
] = re_acquire_state_context (&err
, dfa
, &follows
, 0);
3383 if (BE (dest_states
[i
] == NULL
&& err
!= REG_NOERROR
, 0))
3385 /* If the new state has context constraint,
3386 build appropriate states for these contexts. */
3387 if (dest_states
[i
]->has_constraint
)
3389 dest_states_word
[i
] = re_acquire_state_context (&err
, dfa
, &follows
,
3391 if (BE (dest_states_word
[i
] == NULL
&& err
!= REG_NOERROR
, 0))
3394 if (dest_states
[i
] != dest_states_word
[i
] && dfa
->mb_cur_max
> 1)
3395 need_word_trtable
= 1;
3397 dest_states_nl
[i
] = re_acquire_state_context (&err
, dfa
, &follows
,
3399 if (BE (dest_states_nl
[i
] == NULL
&& err
!= REG_NOERROR
, 0))
3404 dest_states_word
[i
] = dest_states
[i
];
3405 dest_states_nl
[i
] = dest_states
[i
];
3407 bitset_merge (acceptable
, dests_ch
[i
]);
3410 if (!BE (need_word_trtable
, 0))
3412 /* We don't care about whether the following character is a word
3413 character, or we are in a single-byte character set so we can
3414 discern by looking at the character code: allocate a
3415 256-entry transition table. */
3416 trtable
= state
->trtable
=
3417 (re_dfastate_t
**) calloc (sizeof (re_dfastate_t
*), SBC_MAX
);
3418 if (BE (trtable
== NULL
, 0))
3421 /* For all characters ch...: */
3422 for (i
= 0; i
< BITSET_UINTS
; ++i
)
3423 for (ch
= i
* UINT_BITS
, elem
= acceptable
[i
], mask
= 1;
3425 mask
<<= 1, elem
>>= 1, ++ch
)
3426 if (BE (elem
& 1, 0))
3428 /* There must be exactly one destination which accepts
3429 character ch. See group_nodes_into_DFAstates. */
3430 for (j
= 0; (dests_ch
[j
][i
] & mask
) == 0; ++j
)
3433 /* j-th destination accepts the word character ch. */
3434 if (dfa
->word_char
[i
] & mask
)
3435 trtable
[ch
] = dest_states_word
[j
];
3437 trtable
[ch
] = dest_states
[j
];
3442 /* We care about whether the following character is a word
3443 character, and we are in a multi-byte character set: discern
3444 by looking at the character code: build two 256-entry
3445 transition tables, one starting at trtable[0] and one
3446 starting at trtable[SBC_MAX]. */
3447 trtable
= state
->word_trtable
=
3448 (re_dfastate_t
**) calloc (sizeof (re_dfastate_t
*), 2 * SBC_MAX
);
3449 if (BE (trtable
== NULL
, 0))
3452 /* For all characters ch...: */
3453 for (i
= 0; i
< BITSET_UINTS
; ++i
)
3454 for (ch
= i
* UINT_BITS
, elem
= acceptable
[i
], mask
= 1;
3456 mask
<<= 1, elem
>>= 1, ++ch
)
3457 if (BE (elem
& 1, 0))
3459 /* There must be exactly one destination which accepts
3460 character ch. See group_nodes_into_DFAstates. */
3461 for (j
= 0; (dests_ch
[j
][i
] & mask
) == 0; ++j
)
3464 /* j-th destination accepts the word character ch. */
3465 trtable
[ch
] = dest_states
[j
];
3466 trtable
[ch
+ SBC_MAX
] = dest_states_word
[j
];
3471 if (bitset_contain (acceptable
, NEWLINE_CHAR
))
3473 /* The current state accepts newline character. */
3474 for (j
= 0; j
< ndests
; ++j
)
3475 if (bitset_contain (dests_ch
[j
], NEWLINE_CHAR
))
3477 /* k-th destination accepts newline character. */
3478 trtable
[NEWLINE_CHAR
] = dest_states_nl
[j
];
3479 if (need_word_trtable
)
3480 trtable
[NEWLINE_CHAR
+ SBC_MAX
] = dest_states_nl
[j
];
3481 /* There must be only one destination which accepts
3482 newline. See group_nodes_into_DFAstates. */
3487 if (dest_states_malloced
)
3490 re_node_set_free (&follows
);
3491 for (i
= 0; i
< ndests
; ++i
)
3492 re_node_set_free (dests_node
+ i
);
3494 if (dests_node_malloced
)
3500 /* Group all nodes belonging to STATE into several destinations.
3501 Then for all destinations, set the nodes belonging to the destination
3502 to DESTS_NODE[i] and set the characters accepted by the destination
3503 to DEST_CH[i]. This function return the number of destinations. */
3506 group_nodes_into_DFAstates (dfa
, state
, dests_node
, dests_ch
)
3508 const re_dfastate_t
*state
;
3509 re_node_set
*dests_node
;
3515 int ndests
; /* Number of the destinations from `state'. */
3516 bitset accepts
; /* Characters a node can accept. */
3517 const re_node_set
*cur_nodes
= &state
->nodes
;
3518 bitset_empty (accepts
);
3521 /* For all the nodes belonging to `state', */
3522 for (i
= 0; i
< cur_nodes
->nelem
; ++i
)
3524 re_token_t
*node
= &dfa
->nodes
[cur_nodes
->elems
[i
]];
3525 re_token_type_t type
= node
->type
;
3526 unsigned int constraint
= node
->constraint
;
3528 /* Enumerate all single byte character this node can accept. */
3529 if (type
== CHARACTER
)
3530 bitset_set (accepts
, node
->opr
.c
);
3531 else if (type
== SIMPLE_BRACKET
)
3533 bitset_merge (accepts
, node
->opr
.sbcset
);
3535 else if (type
== OP_PERIOD
)
3537 #ifdef RE_ENABLE_I18N
3538 if (dfa
->mb_cur_max
> 1)
3539 bitset_merge (accepts
, dfa
->sb_char
);
3542 bitset_set_all (accepts
);
3543 if (!(dfa
->syntax
& RE_DOT_NEWLINE
))
3544 bitset_clear (accepts
, '\n');
3545 if (dfa
->syntax
& RE_DOT_NOT_NULL
)
3546 bitset_clear (accepts
, '\0');
3548 #ifdef RE_ENABLE_I18N
3549 else if (type
== OP_UTF8_PERIOD
)
3551 memset (accepts
, 255, sizeof (unsigned int) * BITSET_UINTS
/ 2);
3552 if (!(dfa
->syntax
& RE_DOT_NEWLINE
))
3553 bitset_clear (accepts
, '\n');
3554 if (dfa
->syntax
& RE_DOT_NOT_NULL
)
3555 bitset_clear (accepts
, '\0');
3561 /* Check the `accepts' and sift the characters which are not
3562 match it the context. */
3565 if (constraint
& NEXT_NEWLINE_CONSTRAINT
)
3567 int accepts_newline
= bitset_contain (accepts
, NEWLINE_CHAR
);
3568 bitset_empty (accepts
);
3569 if (accepts_newline
)
3570 bitset_set (accepts
, NEWLINE_CHAR
);
3574 if (constraint
& NEXT_ENDBUF_CONSTRAINT
)
3576 bitset_empty (accepts
);
3580 if (constraint
& NEXT_WORD_CONSTRAINT
)
3582 unsigned int any_set
= 0;
3583 if (type
== CHARACTER
&& !node
->word_char
)
3585 bitset_empty (accepts
);
3588 #ifdef RE_ENABLE_I18N
3589 if (dfa
->mb_cur_max
> 1)
3590 for (j
= 0; j
< BITSET_UINTS
; ++j
)
3591 any_set
|= (accepts
[j
] &= (dfa
->word_char
[j
] | ~dfa
->sb_char
[j
]));
3594 for (j
= 0; j
< BITSET_UINTS
; ++j
)
3595 any_set
|= (accepts
[j
] &= dfa
->word_char
[j
]);
3599 if (constraint
& NEXT_NOTWORD_CONSTRAINT
)
3601 unsigned int any_set
= 0;
3602 if (type
== CHARACTER
&& node
->word_char
)
3604 bitset_empty (accepts
);
3607 #ifdef RE_ENABLE_I18N
3608 if (dfa
->mb_cur_max
> 1)
3609 for (j
= 0; j
< BITSET_UINTS
; ++j
)
3610 any_set
|= (accepts
[j
] &= ~(dfa
->word_char
[j
] & dfa
->sb_char
[j
]));
3613 for (j
= 0; j
< BITSET_UINTS
; ++j
)
3614 any_set
|= (accepts
[j
] &= ~dfa
->word_char
[j
]);
3620 /* Then divide `accepts' into DFA states, or create a new
3621 state. Above, we make sure that accepts is not empty. */
3622 for (j
= 0; j
< ndests
; ++j
)
3624 bitset intersec
; /* Intersection sets, see below. */
3626 /* Flags, see below. */
3627 int has_intersec
, not_subset
, not_consumed
;
3629 /* Optimization, skip if this state doesn't accept the character. */
3630 if (type
== CHARACTER
&& !bitset_contain (dests_ch
[j
], node
->opr
.c
))
3633 /* Enumerate the intersection set of this state and `accepts'. */
3635 for (k
= 0; k
< BITSET_UINTS
; ++k
)
3636 has_intersec
|= intersec
[k
] = accepts
[k
] & dests_ch
[j
][k
];
3637 /* And skip if the intersection set is empty. */
3641 /* Then check if this state is a subset of `accepts'. */
3642 not_subset
= not_consumed
= 0;
3643 for (k
= 0; k
< BITSET_UINTS
; ++k
)
3645 not_subset
|= remains
[k
] = ~accepts
[k
] & dests_ch
[j
][k
];
3646 not_consumed
|= accepts
[k
] = accepts
[k
] & ~dests_ch
[j
][k
];
3649 /* If this state isn't a subset of `accepts', create a
3650 new group state, which has the `remains'. */
3653 bitset_copy (dests_ch
[ndests
], remains
);
3654 bitset_copy (dests_ch
[j
], intersec
);
3655 err
= re_node_set_init_copy (dests_node
+ ndests
, &dests_node
[j
]);
3656 if (BE (err
!= REG_NOERROR
, 0))
3661 /* Put the position in the current group. */
3662 result
= re_node_set_insert (&dests_node
[j
], cur_nodes
->elems
[i
]);
3663 if (BE (result
< 0, 0))
3666 /* If all characters are consumed, go to next node. */
3670 /* Some characters remain, create a new group. */
3673 bitset_copy (dests_ch
[ndests
], accepts
);
3674 err
= re_node_set_init_1 (dests_node
+ ndests
, cur_nodes
->elems
[i
]);
3675 if (BE (err
!= REG_NOERROR
, 0))
3678 bitset_empty (accepts
);
3683 for (j
= 0; j
< ndests
; ++j
)
3684 re_node_set_free (dests_node
+ j
);
3688 #ifdef RE_ENABLE_I18N
3689 /* Check how many bytes the node `dfa->nodes[node_idx]' accepts.
3690 Return the number of the bytes the node accepts.
3691 STR_IDX is the current index of the input string.
3693 This function handles the nodes which can accept one character, or
3694 one collating element like '.', '[a-z]', opposite to the other nodes
3695 can only accept one byte. */
3698 check_node_accept_bytes (dfa
, node_idx
, input
, str_idx
)
3700 int node_idx
, str_idx
;
3701 const re_string_t
*input
;
3703 const re_token_t
*node
= dfa
->nodes
+ node_idx
;
3704 int char_len
, elem_len
;
3707 if (BE (node
->type
== OP_UTF8_PERIOD
, 0))
3709 unsigned char c
= re_string_byte_at (input
, str_idx
), d
;
3710 if (BE (c
< 0xc2, 1))
3713 if (str_idx
+ 2 > input
->len
)
3716 d
= re_string_byte_at (input
, str_idx
+ 1);
3718 return (d
< 0x80 || d
> 0xbf) ? 0 : 2;
3722 if (c
== 0xe0 && d
< 0xa0)
3728 if (c
== 0xf0 && d
< 0x90)
3734 if (c
== 0xf8 && d
< 0x88)
3740 if (c
== 0xfc && d
< 0x84)
3746 if (str_idx
+ char_len
> input
->len
)
3749 for (i
= 1; i
< char_len
; ++i
)
3751 d
= re_string_byte_at (input
, str_idx
+ i
);
3752 if (d
< 0x80 || d
> 0xbf)
3758 char_len
= re_string_char_size_at (input
, str_idx
);
3759 if (node
->type
== OP_PERIOD
)
3763 /* FIXME: I don't think this if is needed, as both '\n'
3764 and '\0' are char_len == 1. */
3765 /* '.' accepts any one character except the following two cases. */
3766 if ((!(dfa
->syntax
& RE_DOT_NEWLINE
) &&
3767 re_string_byte_at (input
, str_idx
) == '\n') ||
3768 ((dfa
->syntax
& RE_DOT_NOT_NULL
) &&
3769 re_string_byte_at (input
, str_idx
) == '\0'))
3774 elem_len
= re_string_elem_size_at (input
, str_idx
);
3775 if ((elem_len
<= 1 && char_len
<= 1) || char_len
== 0)
3778 if (node
->type
== COMPLEX_BRACKET
)
3780 const re_charset_t
*cset
= node
->opr
.mbcset
;
3782 const unsigned char *pin
= ((char *) re_string_get_buffer (input
)
3788 wchar_t wc
= ((cset
->nranges
|| cset
->nchar_classes
|| cset
->nmbchars
)
3789 ? re_string_wchar_at (input
, str_idx
) : 0);
3791 /* match with multibyte character? */
3792 for (i
= 0; i
< cset
->nmbchars
; ++i
)
3793 if (wc
== cset
->mbchars
[i
])
3795 match_len
= char_len
;
3796 goto check_node_accept_bytes_match
;
3798 /* match with character_class? */
3799 for (i
= 0; i
< cset
->nchar_classes
; ++i
)
3801 wctype_t wt
= cset
->char_classes
[i
];
3802 if (__iswctype (wc
, wt
))
3804 match_len
= char_len
;
3805 goto check_node_accept_bytes_match
;
3810 nrules
= _NL_CURRENT_WORD (LC_COLLATE
, _NL_COLLATE_NRULES
);
3813 unsigned int in_collseq
= 0;
3814 const int32_t *table
, *indirect
;
3815 const unsigned char *weights
, *extra
;
3816 const char *collseqwc
;
3818 /* This #include defines a local function! */
3819 # include <locale/weight.h>
3821 /* match with collating_symbol? */
3822 if (cset
->ncoll_syms
)
3823 extra
= (const unsigned char *)
3824 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_SYMB_EXTRAMB
);
3825 for (i
= 0; i
< cset
->ncoll_syms
; ++i
)
3827 const unsigned char *coll_sym
= extra
+ cset
->coll_syms
[i
];
3828 /* Compare the length of input collating element and
3829 the length of current collating element. */
3830 if (*coll_sym
!= elem_len
)
3832 /* Compare each bytes. */
3833 for (j
= 0; j
< *coll_sym
; j
++)
3834 if (pin
[j
] != coll_sym
[1 + j
])
3838 /* Match if every bytes is equal. */
3840 goto check_node_accept_bytes_match
;
3846 if (elem_len
<= char_len
)
3848 collseqwc
= _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_COLLSEQWC
);
3849 in_collseq
= __collseq_table_lookup (collseqwc
, wc
);
3852 in_collseq
= find_collation_sequence_value (pin
, elem_len
);
3854 /* match with range expression? */
3855 for (i
= 0; i
< cset
->nranges
; ++i
)
3856 if (cset
->range_starts
[i
] <= in_collseq
3857 && in_collseq
<= cset
->range_ends
[i
])
3859 match_len
= elem_len
;
3860 goto check_node_accept_bytes_match
;
3863 /* match with equivalence_class? */
3864 if (cset
->nequiv_classes
)
3866 const unsigned char *cp
= pin
;
3867 table
= (const int32_t *)
3868 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_TABLEMB
);
3869 weights
= (const unsigned char *)
3870 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_WEIGHTMB
);
3871 extra
= (const unsigned char *)
3872 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_EXTRAMB
);
3873 indirect
= (const int32_t *)
3874 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_INDIRECTMB
);
3875 idx
= findidx (&cp
);
3877 for (i
= 0; i
< cset
->nequiv_classes
; ++i
)
3879 int32_t equiv_class_idx
= cset
->equiv_classes
[i
];
3880 size_t weight_len
= weights
[idx
];
3881 if (weight_len
== weights
[equiv_class_idx
])
3884 while (cnt
<= weight_len
3885 && (weights
[equiv_class_idx
+ 1 + cnt
]
3886 == weights
[idx
+ 1 + cnt
]))
3888 if (cnt
> weight_len
)
3890 match_len
= elem_len
;
3891 goto check_node_accept_bytes_match
;
3900 /* match with range expression? */
3902 wchar_t cmp_buf
[] = {L
'\0', L
'\0', wc
, L
'\0', L
'\0', L
'\0'};
3904 wchar_t cmp_buf
[] = {L
'\0', L
'\0', L
'\0', L
'\0', L
'\0', L
'\0'};
3907 for (i
= 0; i
< cset
->nranges
; ++i
)
3909 cmp_buf
[0] = cset
->range_starts
[i
];
3910 cmp_buf
[4] = cset
->range_ends
[i
];
3911 if (wcscoll (cmp_buf
, cmp_buf
+ 2) <= 0
3912 && wcscoll (cmp_buf
+ 2, cmp_buf
+ 4) <= 0)
3914 match_len
= char_len
;
3915 goto check_node_accept_bytes_match
;
3919 check_node_accept_bytes_match
:
3920 if (!cset
->non_match
)
3927 return (elem_len
> char_len
) ? elem_len
: char_len
;
3935 find_collation_sequence_value (mbs
, mbs_len
)
3936 const unsigned char *mbs
;
3939 uint32_t nrules
= _NL_CURRENT_WORD (LC_COLLATE
, _NL_COLLATE_NRULES
);
3944 /* No valid character. Match it as a single byte character. */
3945 const unsigned char *collseq
= (const unsigned char *)
3946 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_COLLSEQMB
);
3947 return collseq
[mbs
[0]];
3954 const unsigned char *extra
= (const unsigned char *)
3955 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_SYMB_EXTRAMB
);
3956 int32_t extrasize
= (const unsigned char *)
3957 _NL_CURRENT (LC_COLLATE
, _NL_COLLATE_SYMB_EXTRAMB
+ 1) - extra
;
3959 for (idx
= 0; idx
< extrasize
;)
3961 int mbs_cnt
, found
= 0;
3962 int32_t elem_mbs_len
;
3963 /* Skip the name of collating element name. */
3964 idx
= idx
+ extra
[idx
] + 1;
3965 elem_mbs_len
= extra
[idx
++];
3966 if (mbs_len
== elem_mbs_len
)
3968 for (mbs_cnt
= 0; mbs_cnt
< elem_mbs_len
; ++mbs_cnt
)
3969 if (extra
[idx
+ mbs_cnt
] != mbs
[mbs_cnt
])
3971 if (mbs_cnt
== elem_mbs_len
)
3972 /* Found the entry. */
3975 /* Skip the byte sequence of the collating element. */
3976 idx
+= elem_mbs_len
;
3977 /* Adjust for the alignment. */
3978 idx
= (idx
+ 3) & ~3;
3979 /* Skip the collation sequence value. */
3980 idx
+= sizeof (uint32_t);
3981 /* Skip the wide char sequence of the collating element. */
3982 idx
= idx
+ sizeof (uint32_t) * (extra
[idx
] + 1);
3983 /* If we found the entry, return the sequence value. */
3985 return *(uint32_t *) (extra
+ idx
);
3986 /* Skip the collation sequence value. */
3987 idx
+= sizeof (uint32_t);
3993 #endif /* RE_ENABLE_I18N */
3995 /* Check whether the node accepts the byte which is IDX-th
3996 byte of the INPUT. */
3999 check_node_accept (mctx
, node
, idx
)
4000 const re_match_context_t
*mctx
;
4001 const re_token_t
*node
;
4005 ch
= re_string_byte_at (&mctx
->input
, idx
);
4009 if (node
->opr
.c
!= ch
)
4013 case SIMPLE_BRACKET
:
4014 if (!bitset_contain (node
->opr
.sbcset
, ch
))
4018 #ifdef RE_ENABLE_I18N
4019 case OP_UTF8_PERIOD
:
4025 if ((ch
== '\n' && !(mctx
->dfa
->syntax
& RE_DOT_NEWLINE
))
4026 || (ch
== '\0' && (mctx
->dfa
->syntax
& RE_DOT_NOT_NULL
)))
4034 if (node
->constraint
)
4036 /* The node has constraints. Check whether the current context
4037 satisfies the constraints. */
4038 unsigned int context
= re_string_context_at (&mctx
->input
, idx
,
4040 if (NOT_SATISFY_NEXT_CONSTRAINT (node
->constraint
, context
))
4047 /* Extend the buffers, if the buffers have run out. */
4049 static reg_errcode_t
4050 extend_buffers (mctx
)
4051 re_match_context_t
*mctx
;
4054 re_string_t
*pstr
= &mctx
->input
;
4056 /* Double the lengthes of the buffers. */
4057 ret
= re_string_realloc_buffers (pstr
, pstr
->bufs_len
* 2);
4058 if (BE (ret
!= REG_NOERROR
, 0))
4061 if (mctx
->state_log
!= NULL
)
4063 /* And double the length of state_log. */
4064 /* XXX We have no indication of the size of this buffer. If this
4065 allocation fail we have no indication that the state_log array
4066 does not have the right size. */
4067 re_dfastate_t
**new_array
= re_realloc (mctx
->state_log
, re_dfastate_t
*,
4068 pstr
->bufs_len
+ 1);
4069 if (BE (new_array
== NULL
, 0))
4071 mctx
->state_log
= new_array
;
4074 /* Then reconstruct the buffers. */
4077 #ifdef RE_ENABLE_I18N
4078 if (pstr
->mb_cur_max
> 1)
4080 ret
= build_wcs_upper_buffer (pstr
);
4081 if (BE (ret
!= REG_NOERROR
, 0))
4085 #endif /* RE_ENABLE_I18N */
4086 build_upper_buffer (pstr
);
4090 #ifdef RE_ENABLE_I18N
4091 if (pstr
->mb_cur_max
> 1)
4092 build_wcs_buffer (pstr
);
4094 #endif /* RE_ENABLE_I18N */
4096 if (pstr
->trans
!= NULL
)
4097 re_string_translate_buffer (pstr
);
4104 /* Functions for matching context. */
4106 /* Initialize MCTX. */
4108 static reg_errcode_t
4109 match_ctx_init (mctx
, eflags
, n
)
4110 re_match_context_t
*mctx
;
4113 mctx
->eflags
= eflags
;
4114 mctx
->match_last
= -1;
4117 mctx
->bkref_ents
= re_malloc (struct re_backref_cache_entry
, n
);
4118 mctx
->sub_tops
= re_malloc (re_sub_match_top_t
*, n
);
4119 if (BE (mctx
->bkref_ents
== NULL
|| mctx
->sub_tops
== NULL
, 0))
4122 /* Already zero-ed by the caller.
4124 mctx->bkref_ents = NULL;
4125 mctx->nbkref_ents = 0;
4126 mctx->nsub_tops = 0; */
4127 mctx
->abkref_ents
= n
;
4128 mctx
->max_mb_elem_len
= 1;
4129 mctx
->asub_tops
= n
;
4133 /* Clean the entries which depend on the current input in MCTX.
4134 This function must be invoked when the matcher changes the start index
4135 of the input, or changes the input string. */
4138 match_ctx_clean (mctx
)
4139 re_match_context_t
*mctx
;
4142 for (st_idx
= 0; st_idx
< mctx
->nsub_tops
; ++st_idx
)
4145 re_sub_match_top_t
*top
= mctx
->sub_tops
[st_idx
];
4146 for (sl_idx
= 0; sl_idx
< top
->nlasts
; ++sl_idx
)
4148 re_sub_match_last_t
*last
= top
->lasts
[sl_idx
];
4149 re_free (last
->path
.array
);
4152 re_free (top
->lasts
);
4155 re_free (top
->path
->array
);
4156 re_free (top
->path
);
4161 mctx
->nsub_tops
= 0;
4162 mctx
->nbkref_ents
= 0;
4165 /* Free all the memory associated with MCTX. */
4168 match_ctx_free (mctx
)
4169 re_match_context_t
*mctx
;
4171 /* First, free all the memory associated with MCTX->SUB_TOPS. */
4172 match_ctx_clean (mctx
);
4173 re_free (mctx
->sub_tops
);
4174 re_free (mctx
->bkref_ents
);
4177 /* Add a new backreference entry to MCTX.
4178 Note that we assume that caller never call this function with duplicate
4179 entry, and call with STR_IDX which isn't smaller than any existing entry.
4182 static reg_errcode_t
4183 match_ctx_add_entry (mctx
, node
, str_idx
, from
, to
)
4184 re_match_context_t
*mctx
;
4185 int node
, str_idx
, from
, to
;
4187 if (mctx
->nbkref_ents
>= mctx
->abkref_ents
)
4189 struct re_backref_cache_entry
* new_entry
;
4190 new_entry
= re_realloc (mctx
->bkref_ents
, struct re_backref_cache_entry
,
4191 mctx
->abkref_ents
* 2);
4192 if (BE (new_entry
== NULL
, 0))
4194 re_free (mctx
->bkref_ents
);
4197 mctx
->bkref_ents
= new_entry
;
4198 memset (mctx
->bkref_ents
+ mctx
->nbkref_ents
, '\0',
4199 sizeof (struct re_backref_cache_entry
) * mctx
->abkref_ents
);
4200 mctx
->abkref_ents
*= 2;
4202 if (mctx
->nbkref_ents
> 0
4203 && mctx
->bkref_ents
[mctx
->nbkref_ents
- 1].str_idx
== str_idx
)
4204 mctx
->bkref_ents
[mctx
->nbkref_ents
- 1].more
= 1;
4206 mctx
->bkref_ents
[mctx
->nbkref_ents
].node
= node
;
4207 mctx
->bkref_ents
[mctx
->nbkref_ents
].str_idx
= str_idx
;
4208 mctx
->bkref_ents
[mctx
->nbkref_ents
].subexp_from
= from
;
4209 mctx
->bkref_ents
[mctx
->nbkref_ents
].subexp_to
= to
;
4211 /* This is a cache that saves negative results of check_dst_limits_calc_pos.
4212 If bit N is clear, means that this entry won't epsilon-transition to
4213 an OP_OPEN_SUBEXP or OP_CLOSE_SUBEXP for the N+1-th subexpression. If
4214 it is set, check_dst_limits_calc_pos_1 will recurse and try to find one
4217 A backreference does not epsilon-transition unless it is empty, so set
4218 to all zeros if FROM != TO. */
4219 mctx
->bkref_ents
[mctx
->nbkref_ents
].eps_reachable_subexps_map
4220 = (from
== to
? ~0 : 0);
4222 mctx
->bkref_ents
[mctx
->nbkref_ents
++].more
= 0;
4223 if (mctx
->max_mb_elem_len
< to
- from
)
4224 mctx
->max_mb_elem_len
= to
- from
;
4228 /* Search for the first entry which has the same str_idx, or -1 if none is
4229 found. Note that MCTX->BKREF_ENTS is already sorted by MCTX->STR_IDX. */
4232 search_cur_bkref_entry (mctx
, str_idx
)
4233 re_match_context_t
*mctx
;
4236 int left
, right
, mid
, last
;
4237 last
= right
= mctx
->nbkref_ents
;
4238 for (left
= 0; left
< right
;)
4240 mid
= (left
+ right
) / 2;
4241 if (mctx
->bkref_ents
[mid
].str_idx
< str_idx
)
4246 if (left
< last
&& mctx
->bkref_ents
[left
].str_idx
== str_idx
)
4252 /* Register the node NODE, whose type is OP_OPEN_SUBEXP, and which matches
4255 static reg_errcode_t
4256 match_ctx_add_subtop (mctx
, node
, str_idx
)
4257 re_match_context_t
*mctx
;
4261 assert (mctx
->sub_tops
!= NULL
);
4262 assert (mctx
->asub_tops
> 0);
4264 if (BE (mctx
->nsub_tops
== mctx
->asub_tops
, 0))
4266 int new_asub_tops
= mctx
->asub_tops
* 2;
4267 re_sub_match_top_t
**new_array
= re_realloc (mctx
->sub_tops
,
4268 re_sub_match_top_t
*,
4270 if (BE (new_array
== NULL
, 0))
4272 mctx
->sub_tops
= new_array
;
4273 mctx
->asub_tops
= new_asub_tops
;
4275 mctx
->sub_tops
[mctx
->nsub_tops
] = calloc (1, sizeof (re_sub_match_top_t
));
4276 if (BE (mctx
->sub_tops
[mctx
->nsub_tops
] == NULL
, 0))
4278 mctx
->sub_tops
[mctx
->nsub_tops
]->node
= node
;
4279 mctx
->sub_tops
[mctx
->nsub_tops
++]->str_idx
= str_idx
;
4283 /* Register the node NODE, whose type is OP_CLOSE_SUBEXP, and which matches
4284 at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP. */
4286 static re_sub_match_last_t
*
4287 match_ctx_add_sublast (subtop
, node
, str_idx
)
4288 re_sub_match_top_t
*subtop
;
4291 re_sub_match_last_t
*new_entry
;
4292 if (BE (subtop
->nlasts
== subtop
->alasts
, 0))
4294 int new_alasts
= 2 * subtop
->alasts
+ 1;
4295 re_sub_match_last_t
**new_array
= re_realloc (subtop
->lasts
,
4296 re_sub_match_last_t
*,
4298 if (BE (new_array
== NULL
, 0))
4300 subtop
->lasts
= new_array
;
4301 subtop
->alasts
= new_alasts
;
4303 new_entry
= calloc (1, sizeof (re_sub_match_last_t
));
4304 if (BE (new_entry
!= NULL
, 1))
4306 subtop
->lasts
[subtop
->nlasts
] = new_entry
;
4307 new_entry
->node
= node
;
4308 new_entry
->str_idx
= str_idx
;
4315 sift_ctx_init (sctx
, sifted_sts
, limited_sts
, last_node
, last_str_idx
)
4316 re_sift_context_t
*sctx
;
4317 re_dfastate_t
**sifted_sts
, **limited_sts
;
4318 int last_node
, last_str_idx
;
4320 sctx
->sifted_states
= sifted_sts
;
4321 sctx
->limited_states
= limited_sts
;
4322 sctx
->last_node
= last_node
;
4323 sctx
->last_str_idx
= last_str_idx
;
4324 re_node_set_init_empty (&sctx
->limits
);