1 /* Extended regular expression matching and search library.
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 static reg_errcode_t match_ctx_init (re_match_context_t *cache, int eflags,
21 int n) internal_function;
22 static void match_ctx_clean (re_match_context_t *mctx) internal_function;
23 static void match_ctx_free (re_match_context_t *cache) internal_function;
24 static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, int node,
25 int str_idx, int from, int to)
27 static int search_cur_bkref_entry (re_match_context_t *mctx, int str_idx)
29 static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, int node,
30 int str_idx) internal_function;
31 static re_sub_match_last_t * match_ctx_add_sublast (re_sub_match_top_t *subtop,
32 int node, int str_idx)
34 static void sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
35 re_dfastate_t **limited_sts, int last_node,
38 static reg_errcode_t re_search_internal (const regex_t *preg,
39 const char *string, int length,
40 int start, int range, int stop,
41 size_t nmatch, regmatch_t pmatch[],
42 int eflags) internal_function;
43 static int re_search_2_stub (struct re_pattern_buffer *bufp,
44 const char *string1, int length1,
45 const char *string2, int length2,
46 int start, int range, struct re_registers *regs,
47 int stop, int ret_len) internal_function;
48 static int re_search_stub (struct re_pattern_buffer *bufp,
49 const char *string, int length, int start,
50 int range, int stop, struct re_registers *regs,
51 int ret_len) internal_function;
52 static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch,
53 int nregs, int regs_allocated) internal_function;
54 static inline re_dfastate_t *acquire_init_state_context
55 (reg_errcode_t *err, const re_match_context_t *mctx, int idx)
56 __attribute ((always_inline)) internal_function;
57 static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx)
59 static int check_matching (re_match_context_t *mctx, int fl_longest_match,
62 static int check_halt_node_context (const re_dfa_t *dfa, int node,
63 unsigned int context) internal_function;
64 static int check_halt_state_context (const re_match_context_t *mctx,
65 const re_dfastate_t *state, int idx)
67 static void update_regs (re_dfa_t *dfa, regmatch_t *pmatch,
68 regmatch_t *prev_idx_match, int cur_node,
69 int cur_idx, int nmatch) internal_function;
70 static int proceed_next_node (const re_match_context_t *mctx,
71 int nregs, regmatch_t *regs,
72 int *pidx, int node, re_node_set *eps_via_nodes,
73 struct re_fail_stack_t *fs) internal_function;
74 static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
75 int str_idx, int dest_node, int nregs,
77 re_node_set *eps_via_nodes) internal_function;
78 static int pop_fail_stack (struct re_fail_stack_t *fs, int *pidx, int nregs,
79 regmatch_t *regs, re_node_set *eps_via_nodes) internal_function;
80 static reg_errcode_t set_regs (const regex_t *preg,
81 const re_match_context_t *mctx,
82 size_t nmatch, regmatch_t *pmatch,
83 int fl_backtrack) internal_function;
84 static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs) internal_function;
87 static int sift_states_iter_mb (const re_match_context_t *mctx,
88 re_sift_context_t *sctx,
89 int node_idx, int str_idx, int max_str_idx) internal_function;
90 #endif /* RE_ENABLE_I18N */
91 static reg_errcode_t sift_states_backward (re_match_context_t *mctx,
92 re_sift_context_t *sctx) internal_function;
93 static reg_errcode_t build_sifted_states (re_match_context_t *mctx,
94 re_sift_context_t *sctx, int str_idx,
95 re_node_set *cur_dest) internal_function;
96 static reg_errcode_t update_cur_sifted_state (re_match_context_t *mctx,
97 re_sift_context_t *sctx,
99 re_node_set *dest_nodes) internal_function;
100 static reg_errcode_t add_epsilon_src_nodes (re_dfa_t *dfa,
101 re_node_set *dest_nodes,
102 const re_node_set *candidates) internal_function;
103 static reg_errcode_t sub_epsilon_src_nodes (re_dfa_t *dfa, int node,
104 re_node_set *dest_nodes,
105 const re_node_set *and_nodes) internal_function;
106 static int check_dst_limits (re_match_context_t *mctx, re_node_set *limits,
107 int dst_node, int dst_idx, int src_node,
108 int src_idx) internal_function;
109 static int check_dst_limits_calc_pos_1 (re_match_context_t *mctx,
110 int boundaries, int subexp_idx,
111 int from_node, int bkref_idx) internal_function;
112 static int check_dst_limits_calc_pos (re_match_context_t *mctx,
113 int limit, int subexp_idx,
114 int node, int str_idx,
115 int bkref_idx) internal_function;
116 static reg_errcode_t check_subexp_limits (re_dfa_t *dfa,
117 re_node_set *dest_nodes,
118 const re_node_set *candidates,
120 struct re_backref_cache_entry *bkref_ents,
121 int str_idx) internal_function;
122 static reg_errcode_t sift_states_bkref (re_match_context_t *mctx,
123 re_sift_context_t *sctx,
124 int str_idx, const re_node_set *candidates) internal_function;
125 static reg_errcode_t clean_state_log_if_needed (re_match_context_t *mctx,
126 int next_state_log_idx) internal_function;
127 static reg_errcode_t merge_state_array (re_dfa_t *dfa, re_dfastate_t **dst,
128 re_dfastate_t **src, int num) internal_function;
129 static re_dfastate_t *find_recover_state (reg_errcode_t *err,
130 re_match_context_t *mctx) internal_function;
131 static re_dfastate_t *transit_state (reg_errcode_t *err,
132 re_match_context_t *mctx,
133 re_dfastate_t *state) internal_function;
134 static re_dfastate_t *merge_state_with_log (reg_errcode_t *err,
135 re_match_context_t *mctx,
136 re_dfastate_t *next_state) internal_function;
137 static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx,
138 re_node_set *cur_nodes,
139 int str_idx) internal_function;
141 static re_dfastate_t *transit_state_sb (reg_errcode_t *err,
142 re_match_context_t *mctx,
143 re_dfastate_t *pstate) internal_function;
145 #ifdef RE_ENABLE_I18N
146 static reg_errcode_t transit_state_mb (re_match_context_t *mctx,
147 re_dfastate_t *pstate) internal_function;
148 #endif /* RE_ENABLE_I18N */
149 static reg_errcode_t transit_state_bkref (re_match_context_t *mctx,
150 const re_node_set *nodes) internal_function;
151 static reg_errcode_t get_subexp (re_match_context_t *mctx,
152 int bkref_node, int bkref_str_idx) internal_function;
153 static reg_errcode_t get_subexp_sub (re_match_context_t *mctx,
154 const re_sub_match_top_t *sub_top,
155 re_sub_match_last_t *sub_last,
156 int bkref_node, int bkref_str) internal_function;
157 static int find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
158 int subexp_idx, int type) internal_function;
159 static reg_errcode_t check_arrival (re_match_context_t *mctx,
160 state_array_t *path, int top_node,
161 int top_str, int last_node, int last_str,
162 int type) internal_function;
163 static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx,
165 re_node_set *cur_nodes,
166 re_node_set *next_nodes) internal_function;
167 static reg_errcode_t check_arrival_expand_ecl (re_dfa_t *dfa,
168 re_node_set *cur_nodes,
169 int ex_subexp, int type) internal_function;
170 static reg_errcode_t check_arrival_expand_ecl_sub (re_dfa_t *dfa,
171 re_node_set *dst_nodes,
172 int target, int ex_subexp,
173 int type) internal_function;
174 static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx,
175 re_node_set *cur_nodes, int cur_str,
176 int subexp_num, int type) internal_function;
177 static int build_trtable (re_dfa_t *dfa,
178 re_dfastate_t *state) internal_function;
179 #ifdef RE_ENABLE_I18N
180 static int check_node_accept_bytes (re_dfa_t *dfa, int node_idx,
181 const re_string_t *input, int idx) internal_function;
183 static unsigned int find_collation_sequence_value (const unsigned char *mbs,
184 size_t name_len) internal_function;
186 #endif /* RE_ENABLE_I18N */
187 static int group_nodes_into_DFAstates (re_dfa_t *dfa,
188 const re_dfastate_t *state,
189 re_node_set *states_node,
190 bitset *states_ch) internal_function;
191 static int check_node_accept (const re_match_context_t *mctx,
192 const re_token_t *node, int idx) internal_function;
193 static reg_errcode_t extend_buffers (re_match_context_t *mctx) internal_function;
195 /* Entry point for POSIX code. */
197 /* regexec searches for a given pattern, specified by PREG, in the
200 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
201 `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
202 least NMATCH elements, and we set them to the offsets of the
203 corresponding matched substrings.
205 EFLAGS specifies `execution flags' which affect matching: if
206 REG_NOTBOL is set, then ^ does not match at the beginning of the
207 string; if REG_NOTEOL is set, then $ does not match at the end.
209 We return 0 if we find a match and REG_NOMATCH if not. */
212 regexec (preg, string, nmatch, pmatch, eflags)
213 const regex_t *__restrict preg;
214 const char *__restrict string;
221 re_dfa_t *dfa = (re_dfa_t *)preg->buffer;
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 __libc_lock_lock (dfa->lock);
239 err = re_search_internal (preg, string, length, start, length - start,
240 length, 0, NULL, eflags);
242 err = re_search_internal (preg, string, length, start, length - start,
243 length, nmatch, pmatch, eflags);
244 __libc_lock_unlock (dfa->lock);
245 return err != REG_NOERROR;
249 # include <shlib-compat.h>
250 versioned_symbol (libc, __regexec, regexec, GLIBC_2_3_4);
252 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
253 __typeof__ (__regexec) __compat_regexec;
256 attribute_compat_text_section
257 __compat_regexec (const regex_t *__restrict preg,
258 const char *__restrict string, size_t nmatch,
259 regmatch_t pmatch[], int eflags)
261 return regexec (preg, string, nmatch, pmatch,
262 eflags & (REG_NOTBOL | REG_NOTEOL));
264 compat_symbol (libc, __compat_regexec, regexec, GLIBC_2_0);
268 /* Entry points for GNU code. */
270 /* re_match, re_search, re_match_2, re_search_2
272 The former two functions operate on STRING with length LENGTH,
273 while the later two operate on concatenation of STRING1 and STRING2
274 with lengths LENGTH1 and LENGTH2, respectively.
276 re_match() matches the compiled pattern in BUFP against the string,
277 starting at index START.
279 re_search() first tries matching at index START, then it tries to match
280 starting from index START + 1, and so on. The last start position tried
281 is START + RANGE. (Thus RANGE = 0 forces re_search to operate the same
284 The parameter STOP of re_{match,search}_2 specifies that no match exceeding
285 the first STOP characters of the concatenation of the strings should be
288 If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match
289 and all groups is stroed in REGS. (For the "_2" variants, the offsets are
290 computed relative to the concatenation, not relative to the individual
293 On success, re_match* functions return the length of the match, re_search*
294 return the position of the start of the match. Return value -1 means no
295 match was found and -2 indicates an internal error. */
298 re_match (bufp, string, length, start, regs)
299 struct re_pattern_buffer *bufp;
302 struct re_registers *regs;
304 return re_search_stub (bufp, string, length, start, 0, length, regs, 1);
307 weak_alias (__re_match, re_match)
311 re_search (bufp, string, length, start, range, regs)
312 struct re_pattern_buffer *bufp;
314 int length, start, range;
315 struct re_registers *regs;
317 return re_search_stub (bufp, string, length, start, range, length, regs, 0);
320 weak_alias (__re_search, re_search)
324 re_match_2 (bufp, string1, length1, string2, length2, start, regs, stop)
325 struct re_pattern_buffer *bufp;
326 const char *string1, *string2;
327 int length1, length2, start, stop;
328 struct re_registers *regs;
330 return re_search_2_stub (bufp, string1, length1, string2, length2,
331 start, 0, regs, stop, 1);
334 weak_alias (__re_match_2, re_match_2)
338 re_search_2 (bufp, string1, length1, string2, length2, start, range, regs, stop)
339 struct re_pattern_buffer *bufp;
340 const char *string1, *string2;
341 int length1, length2, start, range, stop;
342 struct re_registers *regs;
344 return re_search_2_stub (bufp, string1, length1, string2, length2,
345 start, range, regs, stop, 0);
348 weak_alias (__re_search_2, re_search_2)
352 re_search_2_stub (bufp, string1, length1, string2, length2, start, range, regs,
354 struct re_pattern_buffer *bufp;
355 const char *string1, *string2;
356 int length1, length2, start, range, stop, ret_len;
357 struct re_registers *regs;
361 int len = length1 + length2;
364 if (BE (length1 < 0 || length2 < 0 || stop < 0, 0))
367 /* Concatenate the strings. */
371 char *s = re_malloc (char, len);
373 if (BE (s == NULL, 0))
375 memcpy (s, string1, length1);
376 memcpy (s + length1, string2, length2);
385 rval = re_search_stub (bufp, str, len, start, range, stop, regs,
388 re_free ((char *) str);
392 /* The parameters have the same meaning as those of re_search.
393 Additional parameters:
394 If RET_LEN is nonzero the length of the match is returned (re_match style);
395 otherwise the position of the match is returned. */
398 re_search_stub (bufp, string, length, start, range, stop, regs, ret_len)
399 struct re_pattern_buffer *bufp;
401 int length, start, range, stop, ret_len;
402 struct re_registers *regs;
404 reg_errcode_t result;
408 re_dfa_t *dfa = (re_dfa_t *)bufp->buffer;
410 /* Check for out-of-range. */
411 if (BE (start < 0 || start > length, 0))
413 if (BE (start + range > length, 0))
414 range = length - start;
415 else if (BE (start + range < 0, 0))
418 __libc_lock_lock (dfa->lock);
420 eflags |= (bufp->not_bol) ? REG_NOTBOL : 0;
421 eflags |= (bufp->not_eol) ? REG_NOTEOL : 0;
423 /* Compile fastmap if we haven't yet. */
424 if (range > 0 && bufp->fastmap != NULL && !bufp->fastmap_accurate)
425 re_compile_fastmap (bufp);
427 if (BE (bufp->no_sub, 0))
430 /* We need at least 1 register. */
433 else if (BE (bufp->regs_allocated == REGS_FIXED &&
434 regs->num_regs < bufp->re_nsub + 1, 0))
436 nregs = regs->num_regs;
437 if (BE (nregs < 1, 0))
439 /* Nothing can be copied to regs. */
445 nregs = bufp->re_nsub + 1;
446 pmatch = re_malloc (regmatch_t, nregs);
447 if (BE (pmatch == NULL, 0))
453 result = re_search_internal (bufp, string, length, start, range, stop,
454 nregs, pmatch, eflags);
458 /* I hope we needn't fill ther regs with -1's when no match was found. */
459 if (result != REG_NOERROR)
461 else if (regs != NULL)
463 /* If caller wants register contents data back, copy them. */
464 bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs,
465 bufp->regs_allocated);
466 if (BE (bufp->regs_allocated == REGS_UNALLOCATED, 0))
470 if (BE (rval == 0, 1))
474 assert (pmatch[0].rm_so == start);
475 rval = pmatch[0].rm_eo - start;
478 rval = pmatch[0].rm_so;
482 __libc_lock_unlock (dfa->lock);
487 re_copy_regs (regs, pmatch, nregs, regs_allocated)
488 struct re_registers *regs;
490 int nregs, regs_allocated;
492 int rval = REGS_REALLOCATE;
494 int need_regs = nregs + 1;
495 /* We need one extra element beyond `num_regs' for the `-1' marker GNU code
498 /* Have the register data arrays been allocated? */
499 if (regs_allocated == REGS_UNALLOCATED)
500 { /* No. So allocate them with malloc. */
501 regs->start = re_malloc (regoff_t, need_regs);
502 regs->end = re_malloc (regoff_t, need_regs);
503 if (BE (regs->start == NULL, 0) || BE (regs->end == NULL, 0))
504 return REGS_UNALLOCATED;
505 regs->num_regs = need_regs;
507 else if (regs_allocated == REGS_REALLOCATE)
508 { /* Yes. If we need more elements than were already
509 allocated, reallocate them. If we need fewer, just
511 if (BE (need_regs > regs->num_regs, 0))
513 regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs);
514 regoff_t *new_end = re_realloc (regs->end, regoff_t, need_regs);
515 if (BE (new_start == NULL, 0) || BE (new_end == NULL, 0))
516 return REGS_UNALLOCATED;
517 regs->start = new_start;
519 regs->num_regs = need_regs;
524 assert (regs_allocated == REGS_FIXED);
525 /* This function may not be called with REGS_FIXED and nregs too big. */
526 assert (regs->num_regs >= nregs);
531 for (i = 0; i < nregs; ++i)
533 regs->start[i] = pmatch[i].rm_so;
534 regs->end[i] = pmatch[i].rm_eo;
536 for ( ; i < regs->num_regs; ++i)
537 regs->start[i] = regs->end[i] = -1;
542 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
543 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
544 this memory for recording register information. STARTS and ENDS
545 must be allocated using the malloc library routine, and must each
546 be at least NUM_REGS * sizeof (regoff_t) bytes long.
548 If NUM_REGS == 0, then subsequent matches should allocate their own
551 Unless this function is called, the first search or match using
552 PATTERN_BUFFER will allocate its own register data, without
553 freeing the old data. */
556 re_set_registers (bufp, regs, num_regs, starts, ends)
557 struct re_pattern_buffer *bufp;
558 struct re_registers *regs;
560 regoff_t *starts, *ends;
564 bufp->regs_allocated = REGS_REALLOCATE;
565 regs->num_regs = num_regs;
566 regs->start = starts;
571 bufp->regs_allocated = REGS_UNALLOCATED;
573 regs->start = regs->end = (regoff_t *) 0;
577 weak_alias (__re_set_registers, re_set_registers)
580 /* Entry points compatible with 4.2 BSD regex library. We don't define
581 them unless specifically requested. */
583 #if defined _REGEX_RE_COMP || defined _LIBC
591 return 0 == regexec (&re_comp_buf, s, 0, NULL, 0);
593 #endif /* _REGEX_RE_COMP */
595 /* Internal entry point. */
597 /* Searches for a compiled pattern PREG in the string STRING, whose
598 length is LENGTH. NMATCH, PMATCH, and EFLAGS have the same
599 mingings with regexec. START, and RANGE have the same meanings
601 Return REG_NOERROR if we find a match, and REG_NOMATCH if not,
602 otherwise return the error code.
603 Note: We assume front end functions already check ranges.
604 (START + RANGE >= 0 && START + RANGE <= LENGTH) */
607 re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
611 int length, start, range, stop, eflags;
616 re_dfa_t *dfa = (re_dfa_t *)preg->buffer;
617 int left_lim, right_lim, incr;
618 int fl_longest_match, match_first, match_kind, match_last = -1;
621 #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
622 re_match_context_t mctx = { .dfa = dfa };
624 re_match_context_t mctx;
626 char *fastmap = (preg->fastmap != NULL && preg->fastmap_accurate
627 && range && !preg->can_be_null) ? preg->fastmap : NULL;
628 unsigned RE_TRANSLATE_TYPE t = (unsigned RE_TRANSLATE_TYPE) preg->translate;
630 #if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L))
631 memset (&mctx, '\0', sizeof (re_match_context_t));
635 extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0;
636 nmatch -= extra_nmatch;
638 /* Check if the DFA haven't been compiled. */
639 if (BE (preg->used == 0 || dfa->init_state == NULL
640 || dfa->init_state_word == NULL || dfa->init_state_nl == NULL
641 || dfa->init_state_begbuf == NULL, 0))
645 /* We assume front-end functions already check them. */
646 assert (start + range >= 0 && start + range <= length);
649 /* If initial states with non-begbuf contexts have no elements,
650 the regex must be anchored. If preg->newline_anchor is set,
651 we'll never use init_state_nl, so do not check it. */
652 if (dfa->init_state->nodes.nelem == 0
653 && dfa->init_state_word->nodes.nelem == 0
654 && (dfa->init_state_nl->nodes.nelem == 0
655 || !preg->newline_anchor))
657 if (start != 0 && start + range != 0)
662 /* We must check the longest matching, if nmatch > 0. */
663 fl_longest_match = (nmatch != 0 || dfa->nbackref);
665 err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1,
666 preg->translate, preg->syntax & RE_ICASE, dfa);
667 if (BE (err != REG_NOERROR, 0))
669 mctx.input.stop = stop;
670 mctx.input.raw_stop = stop;
671 mctx.input.newline_anchor = preg->newline_anchor;
673 err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2);
674 if (BE (err != REG_NOERROR, 0))
677 /* We will log all the DFA states through which the dfa pass,
678 if nmatch > 1, or this dfa has "multibyte node", which is a
679 back-reference or a node which can accept multibyte character or
680 multi character collating element. */
681 if (nmatch > 1 || dfa->has_mb_node)
683 mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1);
684 if (BE (mctx.state_log == NULL, 0))
691 mctx.state_log = NULL;
694 mctx.input.tip_context = (eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
695 : CONTEXT_NEWLINE | CONTEXT_BEGBUF;
697 /* Check incrementally whether of not the input string match. */
698 incr = (range < 0) ? -1 : 1;
699 left_lim = (range < 0) ? start + range : start;
700 right_lim = (range < 0) ? start : start + range;
701 sb = dfa->mb_cur_max == 1;
704 ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0)
705 | (range >= 0 ? 2 : 0)
706 | (t != NULL ? 1 : 0))
709 for (;; match_first += incr)
712 if (match_first < left_lim || right_lim < match_first)
715 /* Advance as rapidly as possible through the string, until we
716 find a plausible place to start matching. This may be done
717 with varying efficiency, so there are various possibilities:
718 only the most common of them are specialized, in order to
719 save on code size. We use a switch statement for speed. */
727 /* Fastmap with single-byte translation, match forward. */
728 while (BE (match_first < right_lim, 1)
729 && !fastmap[t[(unsigned char) string[match_first]]])
731 goto forward_match_found_start_or_reached_end;
734 /* Fastmap without translation, match forward. */
735 while (BE (match_first < right_lim, 1)
736 && !fastmap[(unsigned char) string[match_first]])
739 forward_match_found_start_or_reached_end:
740 if (BE (match_first == right_lim, 0))
742 ch = match_first >= length
743 ? 0 : (unsigned char) string[match_first];
744 if (!fastmap[t ? t[ch] : ch])
751 /* Fastmap without multi-byte translation, match backwards. */
752 while (match_first >= left_lim)
754 ch = match_first >= length
755 ? 0 : (unsigned char) string[match_first];
756 if (fastmap[t ? t[ch] : ch])
760 if (match_first < left_lim)
765 /* In this case, we can't determine easily the current byte,
766 since it might be a component byte of a multibyte
767 character. Then we use the constructed buffer instead. */
770 /* If MATCH_FIRST is out of the valid range, reconstruct the
772 unsigned int offset = match_first - mctx.input.raw_mbs_idx;
773 if (BE (offset >= (unsigned int) mctx.input.valid_raw_len, 0))
775 err = re_string_reconstruct (&mctx.input, match_first,
777 if (BE (err != REG_NOERROR, 0))
780 offset = match_first - mctx.input.raw_mbs_idx;
782 /* If MATCH_FIRST is out of the buffer, leave it as '\0'.
783 Note that MATCH_FIRST must not be smaller than 0. */
784 ch = (match_first >= length
785 ? 0 : re_string_byte_at (&mctx.input, offset));
789 if (match_first < left_lim || match_first > right_lim)
798 /* Reconstruct the buffers so that the matcher can assume that
799 the matching starts from the beginning of the buffer. */
800 err = re_string_reconstruct (&mctx.input, match_first, eflags);
801 if (BE (err != REG_NOERROR, 0))
804 #ifdef RE_ENABLE_I18N
805 /* Don't consider this char as a possible match start if it part,
806 yet isn't the head, of a multibyte character. */
807 if (!sb && !re_string_first_byte (&mctx.input, 0))
811 /* It seems to be appropriate one, then use the matcher. */
812 /* We assume that the matching starts from 0. */
813 mctx.state_log_top = mctx.nbkref_ents = mctx.max_mb_elem_len = 0;
814 match_last = check_matching (&mctx, fl_longest_match,
815 range >= 0 ? &match_first : NULL);
816 if (match_last != -1)
818 if (BE (match_last == -2, 0))
825 mctx.match_last = match_last;
826 if ((!preg->no_sub && nmatch > 1) || dfa->nbackref)
828 re_dfastate_t *pstate = mctx.state_log[match_last];
829 mctx.last_node = check_halt_state_context (&mctx, pstate,
832 if ((!preg->no_sub && nmatch > 1 && dfa->has_plural_match)
835 err = prune_impossible_nodes (&mctx);
836 if (err == REG_NOERROR)
838 if (BE (err != REG_NOMATCH, 0))
843 break; /* We found a match. */
847 match_ctx_clean (&mctx);
851 assert (match_last != -1);
852 assert (err == REG_NOERROR);
855 /* Set pmatch[] if we need. */
860 /* Initialize registers. */
861 for (reg_idx = 1; reg_idx < nmatch; ++reg_idx)
862 pmatch[reg_idx].rm_so = pmatch[reg_idx].rm_eo = -1;
864 /* Set the points where matching start/end. */
866 pmatch[0].rm_eo = mctx.match_last;
868 if (!preg->no_sub && nmatch > 1)
870 err = set_regs (preg, &mctx, nmatch, pmatch,
871 dfa->has_plural_match && dfa->nbackref > 0);
872 if (BE (err != REG_NOERROR, 0))
876 /* At last, add the offset to the each registers, since we slided
877 the buffers so that we could assume that the matching starts
879 for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
880 if (pmatch[reg_idx].rm_so != -1)
882 #ifdef RE_ENABLE_I18N
883 if (BE (mctx.input.offsets_needed != 0, 0))
885 if (pmatch[reg_idx].rm_so == mctx.input.valid_len)
886 pmatch[reg_idx].rm_so += mctx.input.valid_raw_len - mctx.input.valid_len;
888 pmatch[reg_idx].rm_so = mctx.input.offsets[pmatch[reg_idx].rm_so];
889 if (pmatch[reg_idx].rm_eo == mctx.input.valid_len)
890 pmatch[reg_idx].rm_eo += mctx.input.valid_raw_len - mctx.input.valid_len;
892 pmatch[reg_idx].rm_eo = mctx.input.offsets[pmatch[reg_idx].rm_eo];
895 assert (mctx.input.offsets_needed == 0);
897 pmatch[reg_idx].rm_so += match_first;
898 pmatch[reg_idx].rm_eo += match_first;
900 for (reg_idx = 0; reg_idx < extra_nmatch; ++reg_idx)
902 pmatch[nmatch + reg_idx].rm_so = -1;
903 pmatch[nmatch + reg_idx].rm_eo = -1;
907 for (reg_idx = 0; reg_idx + 1 < nmatch; reg_idx++)
908 if (dfa->subexp_map[reg_idx] != reg_idx)
910 pmatch[reg_idx + 1].rm_so
911 = pmatch[dfa->subexp_map[reg_idx] + 1].rm_so;
912 pmatch[reg_idx + 1].rm_eo
913 = pmatch[dfa->subexp_map[reg_idx] + 1].rm_eo;
918 re_free (mctx.state_log);
920 match_ctx_free (&mctx);
921 re_string_destruct (&mctx.input);
926 prune_impossible_nodes (mctx)
927 re_match_context_t *mctx;
929 re_dfa_t *const dfa = mctx->dfa;
930 int halt_node, match_last;
932 re_dfastate_t **sifted_states;
933 re_dfastate_t **lim_states = NULL;
934 re_sift_context_t sctx;
936 assert (mctx->state_log != NULL);
938 match_last = mctx->match_last;
939 halt_node = mctx->last_node;
940 sifted_states = re_malloc (re_dfastate_t *, match_last + 1);
941 if (BE (sifted_states == NULL, 0))
948 lim_states = re_malloc (re_dfastate_t *, match_last + 1);
949 if (BE (lim_states == NULL, 0))
956 memset (lim_states, '\0',
957 sizeof (re_dfastate_t *) * (match_last + 1));
958 sift_ctx_init (&sctx, sifted_states, lim_states, halt_node,
960 ret = sift_states_backward (mctx, &sctx);
961 re_node_set_free (&sctx.limits);
962 if (BE (ret != REG_NOERROR, 0))
964 if (sifted_states[0] != NULL || lim_states[0] != NULL)
974 } while (mctx->state_log[match_last] == NULL
975 || !mctx->state_log[match_last]->halt);
976 halt_node = check_halt_state_context (mctx,
977 mctx->state_log[match_last],
980 ret = merge_state_array (dfa, sifted_states, lim_states,
982 re_free (lim_states);
984 if (BE (ret != REG_NOERROR, 0))
989 sift_ctx_init (&sctx, sifted_states, lim_states, halt_node, match_last);
990 ret = sift_states_backward (mctx, &sctx);
991 re_node_set_free (&sctx.limits);
992 if (BE (ret != REG_NOERROR, 0))
995 re_free (mctx->state_log);
996 mctx->state_log = sifted_states;
997 sifted_states = NULL;
998 mctx->last_node = halt_node;
999 mctx->match_last = match_last;
1002 re_free (sifted_states);
1003 re_free (lim_states);
1007 /* Acquire an initial state and return it.
1008 We must select appropriate initial state depending on the context,
1009 since initial states may have constraints like "\<", "^", etc.. */
1011 static inline re_dfastate_t *
1012 acquire_init_state_context (err, mctx, idx)
1014 const re_match_context_t *mctx;
1017 re_dfa_t *const dfa = mctx->dfa;
1018 if (dfa->init_state->has_constraint)
1020 unsigned int context;
1021 context = re_string_context_at (&mctx->input, idx - 1, mctx->eflags);
1022 if (IS_WORD_CONTEXT (context))
1023 return dfa->init_state_word;
1024 else if (IS_ORDINARY_CONTEXT (context))
1025 return dfa->init_state;
1026 else if (IS_BEGBUF_CONTEXT (context) && IS_NEWLINE_CONTEXT (context))
1027 return dfa->init_state_begbuf;
1028 else if (IS_NEWLINE_CONTEXT (context))
1029 return dfa->init_state_nl;
1030 else if (IS_BEGBUF_CONTEXT (context))
1032 /* It is relatively rare case, then calculate on demand. */
1033 return re_acquire_state_context (err, dfa,
1034 dfa->init_state->entrance_nodes,
1038 /* Must not happen? */
1039 return dfa->init_state;
1042 return dfa->init_state;
1045 /* Check whether the regular expression match input string INPUT or not,
1046 and return the index where the matching end, return -1 if not match,
1047 or return -2 in case of an error.
1048 FL_LONGEST_MATCH means we want the POSIX longest matching.
1049 If P_MATCH_FIRST is not NULL, and the match fails, it is set to the
1050 next place where we may want to try matching.
1051 Note that the matcher assume that the maching starts from the current
1052 index of the buffer. */
1055 check_matching (mctx, fl_longest_match, p_match_first)
1056 re_match_context_t *mctx;
1057 int fl_longest_match;
1060 re_dfa_t *const dfa = mctx->dfa;
1063 int match_last = -1;
1064 int cur_str_idx = re_string_cur_idx (&mctx->input);
1065 re_dfastate_t *cur_state;
1066 int at_init_state = p_match_first != NULL;
1067 int next_start_idx = cur_str_idx;
1070 cur_state = acquire_init_state_context (&err, mctx, cur_str_idx);
1071 /* An initial state must not be NULL (invalid). */
1072 if (BE (cur_state == NULL, 0))
1074 assert (err == REG_ESPACE);
1078 if (mctx->state_log != NULL)
1080 mctx->state_log[cur_str_idx] = cur_state;
1082 /* Check OP_OPEN_SUBEXP in the initial state in case that we use them
1083 later. E.g. Processing back references. */
1084 if (BE (dfa->nbackref, 0))
1087 err = check_subexp_matching_top (mctx, &cur_state->nodes, 0);
1088 if (BE (err != REG_NOERROR, 0))
1091 if (cur_state->has_backref)
1093 err = transit_state_bkref (mctx, &cur_state->nodes);
1094 if (BE (err != REG_NOERROR, 0))
1100 /* If the RE accepts NULL string. */
1101 if (BE (cur_state->halt, 0))
1103 if (!cur_state->has_constraint
1104 || check_halt_state_context (mctx, cur_state, cur_str_idx))
1106 if (!fl_longest_match)
1110 match_last = cur_str_idx;
1116 while (!re_string_eoi (&mctx->input))
1118 re_dfastate_t *old_state = cur_state;
1119 int next_char_idx = re_string_cur_idx (&mctx->input) + 1;
1121 if (BE (next_char_idx >= mctx->input.bufs_len, 0)
1122 || (BE (next_char_idx >= mctx->input.valid_len, 0)
1123 && mctx->input.valid_len < mctx->input.len))
1125 err = extend_buffers (mctx);
1126 if (BE (err != REG_NOERROR, 0))
1128 assert (err == REG_ESPACE);
1133 cur_state = transit_state (&err, mctx, cur_state);
1134 if (mctx->state_log != NULL)
1135 cur_state = merge_state_with_log (&err, mctx, cur_state);
1137 if (cur_state == NULL)
1139 /* Reached the invalid state or an error. Try to recover a valid
1140 state using the state log, if available and if we have not
1141 already found a valid (even if not the longest) match. */
1142 if (BE (err != REG_NOERROR, 0))
1145 if (mctx->state_log == NULL
1146 || (match && !fl_longest_match)
1147 || (cur_state = find_recover_state (&err, mctx)) == NULL)
1151 if (BE (at_init_state, 0))
1153 if (old_state == cur_state)
1154 next_start_idx = next_char_idx;
1159 if (cur_state->halt)
1161 /* Reached a halt state.
1162 Check the halt state can satisfy the current context. */
1163 if (!cur_state->has_constraint
1164 || check_halt_state_context (mctx, cur_state,
1165 re_string_cur_idx (&mctx->input)))
1167 /* We found an appropriate halt state. */
1168 match_last = re_string_cur_idx (&mctx->input);
1171 /* We found a match, do not modify match_first below. */
1172 p_match_first = NULL;
1173 if (!fl_longest_match)
1180 *p_match_first += next_start_idx;
1185 /* Check NODE match the current context. */
1187 static int check_halt_node_context (dfa, node, context)
1188 const re_dfa_t *dfa;
1190 unsigned int context;
1192 re_token_type_t type = dfa->nodes[node].type;
1193 unsigned int constraint = dfa->nodes[node].constraint;
1194 if (type != END_OF_RE)
1198 if (NOT_SATISFY_NEXT_CONSTRAINT (constraint, context))
1203 /* Check the halt state STATE match the current context.
1204 Return 0 if not match, if the node, STATE has, is a halt node and
1205 match the context, return the node. */
1208 check_halt_state_context (mctx, state, idx)
1209 const re_match_context_t *mctx;
1210 const re_dfastate_t *state;
1214 unsigned int context;
1216 assert (state->halt);
1218 context = re_string_context_at (&mctx->input, idx, mctx->eflags);
1219 for (i = 0; i < state->nodes.nelem; ++i)
1220 if (check_halt_node_context (mctx->dfa, state->nodes.elems[i], context))
1221 return state->nodes.elems[i];
1225 /* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA
1226 corresponding to the DFA).
1227 Return the destination node, and update EPS_VIA_NODES, return -1 in case
1231 proceed_next_node (mctx, nregs, regs, pidx, node, eps_via_nodes, fs)
1232 const re_match_context_t *mctx;
1234 int nregs, *pidx, node;
1235 re_node_set *eps_via_nodes;
1236 struct re_fail_stack_t *fs;
1238 re_dfa_t *const dfa = mctx->dfa;
1239 int i, err, dest_node;
1241 if (IS_EPSILON_NODE (dfa->nodes[node].type))
1243 re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes;
1244 re_node_set *edests = &dfa->edests[node];
1246 err = re_node_set_insert (eps_via_nodes, node);
1247 if (BE (err < 0, 0))
1249 /* Pick up a valid destination, or return -1 if none is found. */
1250 for (dest_node = -1, i = 0; i < edests->nelem; ++i)
1252 int candidate = edests->elems[i];
1253 if (!re_node_set_contains (cur_nodes, candidate))
1255 if (dest_node == -1)
1256 dest_node = candidate;
1260 /* In order to avoid infinite loop like "(a*)*", return the second
1261 epsilon-transition if the first was already considered. */
1262 if (re_node_set_contains (eps_via_nodes, dest_node))
1265 /* Otherwise, push the second epsilon-transition on the fail stack. */
1267 && push_fail_stack (fs, *pidx, candidate, nregs, regs,
1271 /* We know we are going to exit. */
1280 re_token_type_t type = dfa->nodes[node].type;
1282 #ifdef RE_ENABLE_I18N
1283 if (dfa->nodes[node].accept_mb)
1284 naccepted = check_node_accept_bytes (dfa, node, &mctx->input, *pidx);
1286 #endif /* RE_ENABLE_I18N */
1287 if (type == OP_BACK_REF)
1289 int subexp_idx = dfa->nodes[node].opr.idx + 1;
1290 naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so;
1293 if (regs[subexp_idx].rm_so == -1 || regs[subexp_idx].rm_eo == -1)
1297 char *buf = (char *) re_string_get_buffer (&mctx->input);
1298 if (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx,
1306 err = re_node_set_insert (eps_via_nodes, node);
1307 if (BE (err < 0, 0))
1309 dest_node = dfa->edests[node].elems[0];
1310 if (re_node_set_contains (&mctx->state_log[*pidx]->nodes,
1317 || check_node_accept (mctx, dfa->nodes + node, *pidx))
1319 dest_node = dfa->nexts[node];
1320 *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted;
1321 if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL
1322 || !re_node_set_contains (&mctx->state_log[*pidx]->nodes,
1325 re_node_set_empty (eps_via_nodes);
1332 static reg_errcode_t
1333 push_fail_stack (fs, str_idx, dest_node, nregs, regs, eps_via_nodes)
1334 struct re_fail_stack_t *fs;
1335 int str_idx, dest_node, nregs;
1337 re_node_set *eps_via_nodes;
1340 int num = fs->num++;
1341 if (fs->num == fs->alloc)
1343 struct re_fail_stack_ent_t *new_array;
1344 new_array = realloc (fs->stack, (sizeof (struct re_fail_stack_ent_t)
1346 if (new_array == NULL)
1349 fs->stack = new_array;
1351 fs->stack[num].idx = str_idx;
1352 fs->stack[num].node = dest_node;
1353 fs->stack[num].regs = re_malloc (regmatch_t, nregs);
1354 if (fs->stack[num].regs == NULL)
1356 memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs);
1357 err = re_node_set_init_copy (&fs->stack[num].eps_via_nodes, eps_via_nodes);
1362 pop_fail_stack (fs, pidx, nregs, regs, eps_via_nodes)
1363 struct re_fail_stack_t *fs;
1366 re_node_set *eps_via_nodes;
1368 int num = --fs->num;
1370 *pidx = fs->stack[num].idx;
1371 memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs);
1372 re_node_set_free (eps_via_nodes);
1373 re_free (fs->stack[num].regs);
1374 *eps_via_nodes = fs->stack[num].eps_via_nodes;
1375 return fs->stack[num].node;
1378 /* Set the positions where the subexpressions are starts/ends to registers
1380 Note: We assume that pmatch[0] is already set, and
1381 pmatch[i].rm_so == pmatch[i].rm_eo == -1 for 0 < i < nmatch. */
1383 static reg_errcode_t
1384 set_regs (preg, mctx, nmatch, pmatch, fl_backtrack)
1385 const regex_t *preg;
1386 const re_match_context_t *mctx;
1391 re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
1393 re_node_set eps_via_nodes;
1394 struct re_fail_stack_t *fs;
1395 struct re_fail_stack_t fs_body = { 0, 2, NULL };
1396 regmatch_t *prev_idx_match;
1399 assert (nmatch > 1);
1400 assert (mctx->state_log != NULL);
1405 fs->stack = re_malloc (struct re_fail_stack_ent_t, fs->alloc);
1406 if (fs->stack == NULL)
1412 cur_node = dfa->init_node;
1413 re_node_set_init_empty (&eps_via_nodes);
1415 prev_idx_match = (regmatch_t *) alloca (sizeof (regmatch_t) * nmatch);
1416 memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
1418 for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;)
1420 update_regs (dfa, pmatch, prev_idx_match, cur_node, idx, nmatch);
1422 if (idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
1427 for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
1428 if (pmatch[reg_idx].rm_so > -1 && pmatch[reg_idx].rm_eo == -1)
1430 if (reg_idx == nmatch)
1432 re_node_set_free (&eps_via_nodes);
1433 return free_fail_stack_return (fs);
1435 cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
1440 re_node_set_free (&eps_via_nodes);
1445 /* Proceed to next node. */
1446 cur_node = proceed_next_node (mctx, nmatch, pmatch, &idx, cur_node,
1447 &eps_via_nodes, fs);
1449 if (BE (cur_node < 0, 0))
1451 if (BE (cur_node == -2, 0))
1453 re_node_set_free (&eps_via_nodes);
1454 free_fail_stack_return (fs);
1458 cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
1462 re_node_set_free (&eps_via_nodes);
1467 re_node_set_free (&eps_via_nodes);
1468 return free_fail_stack_return (fs);
1471 static reg_errcode_t
1472 free_fail_stack_return (fs)
1473 struct re_fail_stack_t *fs;
1478 for (fs_idx = 0; fs_idx < fs->num; ++fs_idx)
1480 re_node_set_free (&fs->stack[fs_idx].eps_via_nodes);
1481 re_free (fs->stack[fs_idx].regs);
1483 re_free (fs->stack);
1489 update_regs (dfa, pmatch, prev_idx_match, cur_node, cur_idx, nmatch)
1491 regmatch_t *pmatch, *prev_idx_match;
1492 int cur_node, cur_idx, nmatch;
1494 int type = dfa->nodes[cur_node].type;
1495 if (type == OP_OPEN_SUBEXP)
1497 int reg_num = dfa->nodes[cur_node].opr.idx + 1;
1499 /* We are at the first node of this sub expression. */
1500 if (reg_num < nmatch)
1502 pmatch[reg_num].rm_so = cur_idx;
1503 pmatch[reg_num].rm_eo = -1;
1506 else if (type == OP_CLOSE_SUBEXP)
1508 int reg_num = dfa->nodes[cur_node].opr.idx + 1;
1509 if (reg_num < nmatch)
1511 /* We are at the last node of this sub expression. */
1512 if (pmatch[reg_num].rm_so < cur_idx)
1514 pmatch[reg_num].rm_eo = cur_idx;
1515 /* This is a non-empty match or we are not inside an optional
1516 subexpression. Accept this right away. */
1517 memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
1521 if (dfa->nodes[cur_node].opt_subexp
1522 && prev_idx_match[reg_num].rm_so != -1)
1523 /* We transited through an empty match for an optional
1524 subexpression, like (a?)*, and this is not the subexp's
1525 first match. Copy back the old content of the registers
1526 so that matches of an inner subexpression are undone as
1527 well, like in ((a?))*. */
1528 memcpy (pmatch, prev_idx_match, sizeof (regmatch_t) * nmatch);
1530 /* We completed a subexpression, but it may be part of
1531 an optional one, so do not update PREV_IDX_MATCH. */
1532 pmatch[reg_num].rm_eo = cur_idx;
1538 /* This function checks the STATE_LOG from the SCTX->last_str_idx to 0
1539 and sift the nodes in each states according to the following rules.
1540 Updated state_log will be wrote to STATE_LOG.
1542 Rules: We throw away the Node `a' in the STATE_LOG[STR_IDX] if...
1543 1. When STR_IDX == MATCH_LAST(the last index in the state_log):
1544 If `a' isn't the LAST_NODE and `a' can't epsilon transit to
1545 the LAST_NODE, we throw away the node `a'.
1546 2. When 0 <= STR_IDX < MATCH_LAST and `a' accepts
1547 string `s' and transit to `b':
1548 i. If 'b' isn't in the STATE_LOG[STR_IDX+strlen('s')], we throw
1550 ii. If 'b' is in the STATE_LOG[STR_IDX+strlen('s')] but 'b' is
1551 thrown away, we throw away the node `a'.
1552 3. When 0 <= STR_IDX < MATCH_LAST and 'a' epsilon transit to 'b':
1553 i. If 'b' isn't in the STATE_LOG[STR_IDX], we throw away the
1555 ii. If 'b' is in the STATE_LOG[STR_IDX] but 'b' is thrown away,
1556 we throw away the node `a'. */
1558 #define STATE_NODE_CONTAINS(state,node) \
1559 ((state) != NULL && re_node_set_contains (&(state)->nodes, node))
1561 static reg_errcode_t
1562 sift_states_backward (mctx, sctx)
1563 re_match_context_t *mctx;
1564 re_sift_context_t *sctx;
1568 int str_idx = sctx->last_str_idx;
1569 re_node_set cur_dest;
1572 assert (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL);
1575 /* Build sifted state_log[str_idx]. It has the nodes which can epsilon
1576 transit to the last_node and the last_node itself. */
1577 err = re_node_set_init_1 (&cur_dest, sctx->last_node);
1578 if (BE (err != REG_NOERROR, 0))
1580 err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
1581 if (BE (err != REG_NOERROR, 0))
1584 /* Then check each states in the state_log. */
1587 /* Update counters. */
1588 null_cnt = (sctx->sifted_states[str_idx] == NULL) ? null_cnt + 1 : 0;
1589 if (null_cnt > mctx->max_mb_elem_len)
1591 memset (sctx->sifted_states, '\0',
1592 sizeof (re_dfastate_t *) * str_idx);
1593 re_node_set_free (&cur_dest);
1596 re_node_set_empty (&cur_dest);
1599 if (mctx->state_log[str_idx])
1601 err = build_sifted_states (mctx, sctx, str_idx, &cur_dest);
1602 if (BE (err != REG_NOERROR, 0))
1606 /* Add all the nodes which satisfy the following conditions:
1607 - It can epsilon transit to a node in CUR_DEST.
1609 And update state_log. */
1610 err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
1611 if (BE (err != REG_NOERROR, 0))
1616 re_node_set_free (&cur_dest);
1620 static reg_errcode_t
1621 build_sifted_states (mctx, sctx, str_idx, cur_dest)
1622 re_match_context_t *mctx;
1623 re_sift_context_t *sctx;
1625 re_node_set *cur_dest;
1627 re_dfa_t *const dfa = mctx->dfa;
1628 re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
1631 /* Then build the next sifted state.
1632 We build the next sifted state on `cur_dest', and update
1633 `sifted_states[str_idx]' with `cur_dest'.
1635 `cur_dest' is the sifted state from `state_log[str_idx + 1]'.
1636 `cur_src' points the node_set of the old `state_log[str_idx]'
1637 (with the epsilon nodes pre-filtered out). */
1638 for (i = 0; i < cur_src->nelem; i++)
1640 int prev_node = cur_src->elems[i];
1645 re_token_type_t type = dfa->nodes[prev_node].type;
1646 assert (!IS_EPSILON_NODE (type));
1648 #ifdef RE_ENABLE_I18N
1649 /* If the node may accept `multi byte'. */
1650 if (dfa->nodes[prev_node].accept_mb)
1651 naccepted = sift_states_iter_mb (mctx, sctx, prev_node,
1652 str_idx, sctx->last_str_idx);
1653 #endif /* RE_ENABLE_I18N */
1655 /* We don't check backreferences here.
1656 See update_cur_sifted_state(). */
1658 && check_node_accept (mctx, dfa->nodes + prev_node, str_idx)
1659 && STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + 1],
1660 dfa->nexts[prev_node]))
1666 if (sctx->limits.nelem)
1668 int to_idx = str_idx + naccepted;
1669 if (check_dst_limits (mctx, &sctx->limits,
1670 dfa->nexts[prev_node], to_idx,
1671 prev_node, str_idx))
1674 ret = re_node_set_insert (cur_dest, prev_node);
1675 if (BE (ret == -1, 0))
1682 /* Helper functions. */
1684 static reg_errcode_t
1685 clean_state_log_if_needed (mctx, next_state_log_idx)
1686 re_match_context_t *mctx;
1687 int next_state_log_idx;
1689 int top = mctx->state_log_top;
1691 if (next_state_log_idx >= mctx->input.bufs_len
1692 || (next_state_log_idx >= mctx->input.valid_len
1693 && mctx->input.valid_len < mctx->input.len))
1696 err = extend_buffers (mctx);
1697 if (BE (err != REG_NOERROR, 0))
1701 if (top < next_state_log_idx)
1703 memset (mctx->state_log + top + 1, '\0',
1704 sizeof (re_dfastate_t *) * (next_state_log_idx - top));
1705 mctx->state_log_top = next_state_log_idx;
1710 static reg_errcode_t
1711 merge_state_array (dfa, dst, src, num)
1713 re_dfastate_t **dst;
1714 re_dfastate_t **src;
1719 for (st_idx = 0; st_idx < num; ++st_idx)
1721 if (dst[st_idx] == NULL)
1722 dst[st_idx] = src[st_idx];
1723 else if (src[st_idx] != NULL)
1725 re_node_set merged_set;
1726 err = re_node_set_init_union (&merged_set, &dst[st_idx]->nodes,
1727 &src[st_idx]->nodes);
1728 if (BE (err != REG_NOERROR, 0))
1730 dst[st_idx] = re_acquire_state (&err, dfa, &merged_set);
1731 re_node_set_free (&merged_set);
1732 if (BE (err != REG_NOERROR, 0))
1739 static reg_errcode_t
1740 update_cur_sifted_state (mctx, sctx, str_idx, dest_nodes)
1741 re_match_context_t *mctx;
1742 re_sift_context_t *sctx;
1744 re_node_set *dest_nodes;
1746 re_dfa_t *const dfa = mctx->dfa;
1748 const re_node_set *candidates;
1749 candidates = ((mctx->state_log[str_idx] == NULL) ? NULL
1750 : &mctx->state_log[str_idx]->nodes);
1752 if (dest_nodes->nelem == 0)
1753 sctx->sifted_states[str_idx] = NULL;
1758 /* At first, add the nodes which can epsilon transit to a node in
1760 err = add_epsilon_src_nodes (dfa, dest_nodes, candidates);
1761 if (BE (err != REG_NOERROR, 0))
1764 /* Then, check the limitations in the current sift_context. */
1765 if (sctx->limits.nelem)
1767 err = check_subexp_limits (dfa, dest_nodes, candidates, &sctx->limits,
1768 mctx->bkref_ents, str_idx);
1769 if (BE (err != REG_NOERROR, 0))
1774 sctx->sifted_states[str_idx] = re_acquire_state (&err, dfa, dest_nodes);
1775 if (BE (err != REG_NOERROR, 0))
1779 if (candidates && mctx->state_log[str_idx]->has_backref)
1781 err = sift_states_bkref (mctx, sctx, str_idx, candidates);
1782 if (BE (err != REG_NOERROR, 0))
1788 static reg_errcode_t
1789 add_epsilon_src_nodes (dfa, dest_nodes, candidates)
1791 re_node_set *dest_nodes;
1792 const re_node_set *candidates;
1794 reg_errcode_t err = REG_NOERROR;
1797 re_dfastate_t *state = re_acquire_state (&err, dfa, dest_nodes);
1798 if (BE (err != REG_NOERROR, 0))
1801 if (!state->inveclosure.alloc)
1803 err = re_node_set_alloc (&state->inveclosure, dest_nodes->nelem);
1804 if (BE (err != REG_NOERROR, 0))
1806 for (i = 0; i < dest_nodes->nelem; i++)
1807 re_node_set_merge (&state->inveclosure,
1808 dfa->inveclosures + dest_nodes->elems[i]);
1810 return re_node_set_add_intersect (dest_nodes, candidates,
1811 &state->inveclosure);
1814 static reg_errcode_t
1815 sub_epsilon_src_nodes (dfa, node, dest_nodes, candidates)
1818 re_node_set *dest_nodes;
1819 const re_node_set *candidates;
1823 re_node_set *inv_eclosure = dfa->inveclosures + node;
1824 re_node_set except_nodes;
1825 re_node_set_init_empty (&except_nodes);
1826 for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
1828 int cur_node = inv_eclosure->elems[ecl_idx];
1829 if (cur_node == node)
1831 if (IS_EPSILON_NODE (dfa->nodes[cur_node].type))
1833 int edst1 = dfa->edests[cur_node].elems[0];
1834 int edst2 = ((dfa->edests[cur_node].nelem > 1)
1835 ? dfa->edests[cur_node].elems[1] : -1);
1836 if ((!re_node_set_contains (inv_eclosure, edst1)
1837 && re_node_set_contains (dest_nodes, edst1))
1839 && !re_node_set_contains (inv_eclosure, edst2)
1840 && re_node_set_contains (dest_nodes, edst2)))
1842 err = re_node_set_add_intersect (&except_nodes, candidates,
1843 dfa->inveclosures + cur_node);
1844 if (BE (err != REG_NOERROR, 0))
1846 re_node_set_free (&except_nodes);
1852 for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
1854 int cur_node = inv_eclosure->elems[ecl_idx];
1855 if (!re_node_set_contains (&except_nodes, cur_node))
1857 int idx = re_node_set_contains (dest_nodes, cur_node) - 1;
1858 re_node_set_remove_at (dest_nodes, idx);
1861 re_node_set_free (&except_nodes);
1866 check_dst_limits (mctx, limits, dst_node, dst_idx, src_node, src_idx)
1867 re_match_context_t *mctx;
1868 re_node_set *limits;
1869 int dst_node, dst_idx, src_node, src_idx;
1871 re_dfa_t *const dfa = mctx->dfa;
1872 int lim_idx, src_pos, dst_pos;
1874 int dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
1875 int src_bkref_idx = search_cur_bkref_entry (mctx, src_idx);
1876 for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
1879 struct re_backref_cache_entry *ent;
1880 ent = mctx->bkref_ents + limits->elems[lim_idx];
1881 subexp_idx = dfa->nodes[ent->node].opr.idx;
1883 dst_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx],
1884 subexp_idx, dst_node, dst_idx,
1886 src_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx],
1887 subexp_idx, src_node, src_idx,
1891 <src> <dst> ( <subexp> )
1892 ( <subexp> ) <src> <dst>
1893 ( <subexp1> <src> <subexp2> <dst> <subexp3> ) */
1894 if (src_pos == dst_pos)
1895 continue; /* This is unrelated limitation. */
1903 check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx, from_node, bkref_idx)
1904 re_match_context_t *mctx;
1905 int boundaries, subexp_idx, from_node, bkref_idx;
1907 re_dfa_t *const dfa = mctx->dfa;
1908 re_node_set *eclosures = dfa->eclosures + from_node;
1911 /* Else, we are on the boundary: examine the nodes on the epsilon
1913 for (node_idx = 0; node_idx < eclosures->nelem; ++node_idx)
1915 int node = eclosures->elems[node_idx];
1916 switch (dfa->nodes[node].type)
1919 if (bkref_idx != -1)
1921 struct re_backref_cache_entry *ent = mctx->bkref_ents + bkref_idx;
1926 if (ent->node != node)
1929 if (subexp_idx <= 8 * sizeof (ent->eps_reachable_subexps_map)
1930 && !(ent->eps_reachable_subexps_map & (1 << subexp_idx)))
1933 /* Recurse trying to reach the OP_OPEN_SUBEXP and
1934 OP_CLOSE_SUBEXP cases below. But, if the
1935 destination node is the same node as the source
1936 node, don't recurse because it would cause an
1937 infinite loop: a regex that exhibits this behavior
1939 dst = dfa->edests[node].elems[0];
1940 if (dst == from_node)
1944 else /* if (boundaries & 2) */
1949 check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx,
1951 if (cpos == -1 /* && (boundaries & 1) */)
1953 if (cpos == 0 && (boundaries & 2))
1956 ent->eps_reachable_subexps_map &= ~(1 << subexp_idx);
1958 while (ent++->more);
1962 case OP_OPEN_SUBEXP:
1963 if ((boundaries & 1) && subexp_idx == dfa->nodes[node].opr.idx)
1967 case OP_CLOSE_SUBEXP:
1968 if ((boundaries & 2) && subexp_idx == dfa->nodes[node].opr.idx)
1977 return (boundaries & 2) ? 1 : 0;
1981 check_dst_limits_calc_pos (mctx, limit, subexp_idx, from_node, str_idx, bkref_idx)
1982 re_match_context_t *mctx;
1983 int limit, subexp_idx, from_node, str_idx, bkref_idx;
1985 struct re_backref_cache_entry *lim = mctx->bkref_ents + limit;
1988 /* If we are outside the range of the subexpression, return -1 or 1. */
1989 if (str_idx < lim->subexp_from)
1992 if (lim->subexp_to < str_idx)
1995 /* If we are within the subexpression, return 0. */
1996 boundaries = (str_idx == lim->subexp_from);
1997 boundaries |= (str_idx == lim->subexp_to) << 1;
1998 if (boundaries == 0)
2001 /* Else, examine epsilon closure. */
2002 return check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx,
2003 from_node, bkref_idx);
2006 /* Check the limitations of sub expressions LIMITS, and remove the nodes
2007 which are against limitations from DEST_NODES. */
2009 static reg_errcode_t
2010 check_subexp_limits (dfa, dest_nodes, candidates, limits, bkref_ents, str_idx)
2012 re_node_set *dest_nodes;
2013 const re_node_set *candidates;
2014 re_node_set *limits;
2015 struct re_backref_cache_entry *bkref_ents;
2019 int node_idx, lim_idx;
2021 for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
2024 struct re_backref_cache_entry *ent;
2025 ent = bkref_ents + limits->elems[lim_idx];
2027 if (str_idx <= ent->subexp_from || ent->str_idx < str_idx)
2028 continue; /* This is unrelated limitation. */
2030 subexp_idx = dfa->nodes[ent->node].opr.idx;
2031 if (ent->subexp_to == str_idx)
2035 for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
2037 int node = dest_nodes->elems[node_idx];
2038 re_token_type_t type = dfa->nodes[node].type;
2039 if (type == OP_OPEN_SUBEXP
2040 && subexp_idx == dfa->nodes[node].opr.idx)
2042 else if (type == OP_CLOSE_SUBEXP
2043 && subexp_idx == dfa->nodes[node].opr.idx)
2047 /* Check the limitation of the open subexpression. */
2048 /* Note that (ent->subexp_to = str_idx != ent->subexp_from). */
2051 err = sub_epsilon_src_nodes (dfa, ops_node, dest_nodes,
2053 if (BE (err != REG_NOERROR, 0))
2057 /* Check the limitation of the close subexpression. */
2059 for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
2061 int node = dest_nodes->elems[node_idx];
2062 if (!re_node_set_contains (dfa->inveclosures + node,
2064 && !re_node_set_contains (dfa->eclosures + node,
2067 /* It is against this limitation.
2068 Remove it form the current sifted state. */
2069 err = sub_epsilon_src_nodes (dfa, node, dest_nodes,
2071 if (BE (err != REG_NOERROR, 0))
2077 else /* (ent->subexp_to != str_idx) */
2079 for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
2081 int node = dest_nodes->elems[node_idx];
2082 re_token_type_t type = dfa->nodes[node].type;
2083 if (type == OP_CLOSE_SUBEXP || type == OP_OPEN_SUBEXP)
2085 if (subexp_idx != dfa->nodes[node].opr.idx)
2087 /* It is against this limitation.
2088 Remove it form the current sifted state. */
2089 err = sub_epsilon_src_nodes (dfa, node, dest_nodes,
2091 if (BE (err != REG_NOERROR, 0))
2100 static reg_errcode_t
2101 sift_states_bkref (mctx, sctx, str_idx, candidates)
2102 re_match_context_t *mctx;
2103 re_sift_context_t *sctx;
2105 const re_node_set *candidates;
2107 re_dfa_t *const dfa = mctx->dfa;
2110 re_sift_context_t local_sctx;
2111 int first_idx = search_cur_bkref_entry (mctx, str_idx);
2113 if (first_idx == -1)
2116 local_sctx.sifted_states = NULL; /* Mark that it hasn't been initialized. */
2118 for (node_idx = 0; node_idx < candidates->nelem; ++node_idx)
2121 re_token_type_t type;
2122 struct re_backref_cache_entry *entry;
2123 node = candidates->elems[node_idx];
2124 type = dfa->nodes[node].type;
2125 /* Avoid infinite loop for the REs like "()\1+". */
2126 if (node == sctx->last_node && str_idx == sctx->last_str_idx)
2128 if (type != OP_BACK_REF)
2131 entry = mctx->bkref_ents + first_idx;
2132 enabled_idx = first_idx;
2135 int subexp_len, to_idx, dst_node;
2136 re_dfastate_t *cur_state;
2138 if (entry->node != node)
2140 subexp_len = entry->subexp_to - entry->subexp_from;
2141 to_idx = str_idx + subexp_len;
2142 dst_node = (subexp_len ? dfa->nexts[node]
2143 : dfa->edests[node].elems[0]);
2145 if (to_idx > sctx->last_str_idx
2146 || sctx->sifted_states[to_idx] == NULL
2147 || !STATE_NODE_CONTAINS (sctx->sifted_states[to_idx], dst_node)
2148 || check_dst_limits (mctx, &sctx->limits, node,
2149 str_idx, dst_node, to_idx))
2152 if (local_sctx.sifted_states == NULL)
2155 err = re_node_set_init_copy (&local_sctx.limits, &sctx->limits);
2156 if (BE (err != REG_NOERROR, 0))
2159 local_sctx.last_node = node;
2160 local_sctx.last_str_idx = str_idx;
2161 err = re_node_set_insert (&local_sctx.limits, enabled_idx);
2162 if (BE (err < 0, 0))
2167 cur_state = local_sctx.sifted_states[str_idx];
2168 err = sift_states_backward (mctx, &local_sctx);
2169 if (BE (err != REG_NOERROR, 0))
2171 if (sctx->limited_states != NULL)
2173 err = merge_state_array (dfa, sctx->limited_states,
2174 local_sctx.sifted_states,
2176 if (BE (err != REG_NOERROR, 0))
2179 local_sctx.sifted_states[str_idx] = cur_state;
2180 re_node_set_remove (&local_sctx.limits, enabled_idx);
2182 /* mctx->bkref_ents may have changed, reload the pointer. */
2183 entry = mctx->bkref_ents + enabled_idx;
2185 while (enabled_idx++, entry++->more);
2189 if (local_sctx.sifted_states != NULL)
2191 re_node_set_free (&local_sctx.limits);
2198 #ifdef RE_ENABLE_I18N
2200 sift_states_iter_mb (mctx, sctx, node_idx, str_idx, max_str_idx)
2201 const re_match_context_t *mctx;
2202 re_sift_context_t *sctx;
2203 int node_idx, str_idx, max_str_idx;
2205 re_dfa_t *const dfa = mctx->dfa;
2207 /* Check the node can accept `multi byte'. */
2208 naccepted = check_node_accept_bytes (dfa, node_idx, &mctx->input, str_idx);
2209 if (naccepted > 0 && str_idx + naccepted <= max_str_idx &&
2210 !STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + naccepted],
2211 dfa->nexts[node_idx]))
2212 /* The node can't accept the `multi byte', or the
2213 destination was already thrown away, then the node
2214 could't accept the current input `multi byte'. */
2216 /* Otherwise, it is sure that the node could accept
2217 `naccepted' bytes input. */
2220 #endif /* RE_ENABLE_I18N */
2223 /* Functions for state transition. */
2225 /* Return the next state to which the current state STATE will transit by
2226 accepting the current input byte, and update STATE_LOG if necessary.
2227 If STATE can accept a multibyte char/collating element/back reference
2228 update the destination of STATE_LOG. */
2230 static re_dfastate_t *
2231 transit_state (err, mctx, state)
2233 re_match_context_t *mctx;
2234 re_dfastate_t *state;
2236 re_dfastate_t **trtable;
2239 #ifdef RE_ENABLE_I18N
2240 /* If the current state can accept multibyte. */
2241 if (BE (state->accept_mb, 0))
2243 *err = transit_state_mb (mctx, state);
2244 if (BE (*err != REG_NOERROR, 0))
2247 #endif /* RE_ENABLE_I18N */
2249 /* Then decide the next state with the single byte. */
2252 /* don't use transition table */
2253 return transit_state_sb (err, mctx, state);
2256 /* Use transition table */
2257 ch = re_string_fetch_byte (&mctx->input);
2260 trtable = state->trtable;
2261 if (BE (trtable != NULL, 1))
2264 trtable = state->word_trtable;
2265 if (BE (trtable != NULL, 1))
2267 unsigned int context;
2269 = re_string_context_at (&mctx->input,
2270 re_string_cur_idx (&mctx->input) - 1,
2272 if (IS_WORD_CONTEXT (context))
2273 return trtable[ch + SBC_MAX];
2278 if (!build_trtable (mctx->dfa, state))
2284 /* Retry, we now have a transition table. */
2288 /* Update the state_log if we need */
2290 merge_state_with_log (err, mctx, next_state)
2292 re_match_context_t *mctx;
2293 re_dfastate_t *next_state;
2295 re_dfa_t *const dfa = mctx->dfa;
2296 int cur_idx = re_string_cur_idx (&mctx->input);
2298 if (cur_idx > mctx->state_log_top)
2300 mctx->state_log[cur_idx] = next_state;
2301 mctx->state_log_top = cur_idx;
2303 else if (mctx->state_log[cur_idx] == 0)
2305 mctx->state_log[cur_idx] = next_state;
2309 re_dfastate_t *pstate;
2310 unsigned int context;
2311 re_node_set next_nodes, *log_nodes, *table_nodes = NULL;
2312 /* If (state_log[cur_idx] != 0), it implies that cur_idx is
2313 the destination of a multibyte char/collating element/
2314 back reference. Then the next state is the union set of
2315 these destinations and the results of the transition table. */
2316 pstate = mctx->state_log[cur_idx];
2317 log_nodes = pstate->entrance_nodes;
2318 if (next_state != NULL)
2320 table_nodes = next_state->entrance_nodes;
2321 *err = re_node_set_init_union (&next_nodes, table_nodes,
2323 if (BE (*err != REG_NOERROR, 0))
2327 next_nodes = *log_nodes;
2328 /* Note: We already add the nodes of the initial state,
2329 then we don't need to add them here. */
2331 context = re_string_context_at (&mctx->input,
2332 re_string_cur_idx (&mctx->input) - 1,
2334 next_state = mctx->state_log[cur_idx]
2335 = re_acquire_state_context (err, dfa, &next_nodes, context);
2336 /* We don't need to check errors here, since the return value of
2337 this function is next_state and ERR is already set. */
2339 if (table_nodes != NULL)
2340 re_node_set_free (&next_nodes);
2343 if (BE (dfa->nbackref, 0) && next_state != NULL)
2345 /* Check OP_OPEN_SUBEXP in the current state in case that we use them
2346 later. We must check them here, since the back references in the
2347 next state might use them. */
2348 *err = check_subexp_matching_top (mctx, &next_state->nodes,
2350 if (BE (*err != REG_NOERROR, 0))
2353 /* If the next state has back references. */
2354 if (next_state->has_backref)
2356 *err = transit_state_bkref (mctx, &next_state->nodes);
2357 if (BE (*err != REG_NOERROR, 0))
2359 next_state = mctx->state_log[cur_idx];
2366 /* Skip bytes in the input that correspond to part of a
2367 multi-byte match, then look in the log for a state
2368 from which to restart matching. */
2370 find_recover_state (err, mctx)
2372 re_match_context_t *mctx;
2374 re_dfastate_t *cur_state = NULL;
2377 int max = mctx->state_log_top;
2378 int cur_str_idx = re_string_cur_idx (&mctx->input);
2382 if (++cur_str_idx > max)
2384 re_string_skip_bytes (&mctx->input, 1);
2386 while (mctx->state_log[cur_str_idx] == NULL);
2388 cur_state = merge_state_with_log (err, mctx, NULL);
2390 while (err == REG_NOERROR && cur_state == NULL);
2394 /* Helper functions for transit_state. */
2396 /* From the node set CUR_NODES, pick up the nodes whose types are
2397 OP_OPEN_SUBEXP and which have corresponding back references in the regular
2398 expression. And register them to use them later for evaluating the
2399 correspoding back references. */
2401 static reg_errcode_t
2402 check_subexp_matching_top (mctx, cur_nodes, str_idx)
2403 re_match_context_t *mctx;
2404 re_node_set *cur_nodes;
2407 re_dfa_t *const dfa = mctx->dfa;
2411 /* TODO: This isn't efficient.
2412 Because there might be more than one nodes whose types are
2413 OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all
2416 for (node_idx = 0; node_idx < cur_nodes->nelem; ++node_idx)
2418 int node = cur_nodes->elems[node_idx];
2419 if (dfa->nodes[node].type == OP_OPEN_SUBEXP
2420 && dfa->nodes[node].opr.idx < (8 * sizeof (dfa->used_bkref_map))
2421 && dfa->used_bkref_map & (1 << dfa->nodes[node].opr.idx))
2423 err = match_ctx_add_subtop (mctx, node, str_idx);
2424 if (BE (err != REG_NOERROR, 0))
2432 /* Return the next state to which the current state STATE will transit by
2433 accepting the current input byte. */
2435 static re_dfastate_t *
2436 transit_state_sb (err, mctx, state)
2438 re_match_context_t *mctx;
2439 re_dfastate_t *state;
2441 re_dfa_t *const dfa = mctx->dfa;
2442 re_node_set next_nodes;
2443 re_dfastate_t *next_state;
2444 int node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
2445 unsigned int context;
2447 *err = re_node_set_alloc (&next_nodes, state->nodes.nelem + 1);
2448 if (BE (*err != REG_NOERROR, 0))
2450 for (node_cnt = 0; node_cnt < state->nodes.nelem; ++node_cnt)
2452 int cur_node = state->nodes.elems[node_cnt];
2453 if (check_node_accept (mctx, dfa->nodes + cur_node, cur_str_idx))
2455 *err = re_node_set_merge (&next_nodes,
2456 dfa->eclosures + dfa->nexts[cur_node]);
2457 if (BE (*err != REG_NOERROR, 0))
2459 re_node_set_free (&next_nodes);
2464 context = re_string_context_at (&mctx->input, cur_str_idx, mctx->eflags);
2465 next_state = re_acquire_state_context (err, dfa, &next_nodes, context);
2466 /* We don't need to check errors here, since the return value of
2467 this function is next_state and ERR is already set. */
2469 re_node_set_free (&next_nodes);
2470 re_string_skip_bytes (&mctx->input, 1);
2475 #ifdef RE_ENABLE_I18N
2476 static reg_errcode_t
2477 transit_state_mb (mctx, pstate)
2478 re_match_context_t *mctx;
2479 re_dfastate_t *pstate;
2481 re_dfa_t *const dfa = mctx->dfa;
2485 for (i = 0; i < pstate->nodes.nelem; ++i)
2487 re_node_set dest_nodes, *new_nodes;
2488 int cur_node_idx = pstate->nodes.elems[i];
2489 int naccepted, dest_idx;
2490 unsigned int context;
2491 re_dfastate_t *dest_state;
2493 if (!dfa->nodes[cur_node_idx].accept_mb)
2496 if (dfa->nodes[cur_node_idx].constraint)
2498 context = re_string_context_at (&mctx->input,
2499 re_string_cur_idx (&mctx->input),
2501 if (NOT_SATISFY_NEXT_CONSTRAINT (dfa->nodes[cur_node_idx].constraint,
2506 /* How many bytes the node can accept? */
2507 naccepted = check_node_accept_bytes (dfa, cur_node_idx, &mctx->input,
2508 re_string_cur_idx (&mctx->input));
2512 /* The node can accepts `naccepted' bytes. */
2513 dest_idx = re_string_cur_idx (&mctx->input) + naccepted;
2514 mctx->max_mb_elem_len = ((mctx->max_mb_elem_len < naccepted) ? naccepted
2515 : mctx->max_mb_elem_len);
2516 err = clean_state_log_if_needed (mctx, dest_idx);
2517 if (BE (err != REG_NOERROR, 0))
2520 assert (dfa->nexts[cur_node_idx] != -1);
2522 new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx];
2524 dest_state = mctx->state_log[dest_idx];
2525 if (dest_state == NULL)
2526 dest_nodes = *new_nodes;
2529 err = re_node_set_init_union (&dest_nodes,
2530 dest_state->entrance_nodes, new_nodes);
2531 if (BE (err != REG_NOERROR, 0))
2534 context = re_string_context_at (&mctx->input, dest_idx - 1, mctx->eflags);
2535 mctx->state_log[dest_idx]
2536 = re_acquire_state_context (&err, dfa, &dest_nodes, context);
2537 if (dest_state != NULL)
2538 re_node_set_free (&dest_nodes);
2539 if (BE (mctx->state_log[dest_idx] == NULL && err != REG_NOERROR, 0))
2544 #endif /* RE_ENABLE_I18N */
2546 static reg_errcode_t
2547 transit_state_bkref (mctx, nodes)
2548 re_match_context_t *mctx;
2549 const re_node_set *nodes;
2551 re_dfa_t *const dfa = mctx->dfa;
2554 int cur_str_idx = re_string_cur_idx (&mctx->input);
2556 for (i = 0; i < nodes->nelem; ++i)
2558 int dest_str_idx, prev_nelem, bkc_idx;
2559 int node_idx = nodes->elems[i];
2560 unsigned int context;
2561 const re_token_t *node = dfa->nodes + node_idx;
2562 re_node_set *new_dest_nodes;
2564 /* Check whether `node' is a backreference or not. */
2565 if (node->type != OP_BACK_REF)
2568 if (node->constraint)
2570 context = re_string_context_at (&mctx->input, cur_str_idx,
2572 if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context))
2576 /* `node' is a backreference.
2577 Check the substring which the substring matched. */
2578 bkc_idx = mctx->nbkref_ents;
2579 err = get_subexp (mctx, node_idx, cur_str_idx);
2580 if (BE (err != REG_NOERROR, 0))
2583 /* And add the epsilon closures (which is `new_dest_nodes') of
2584 the backreference to appropriate state_log. */
2586 assert (dfa->nexts[node_idx] != -1);
2588 for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx)
2591 re_dfastate_t *dest_state;
2592 struct re_backref_cache_entry *bkref_ent;
2593 bkref_ent = mctx->bkref_ents + bkc_idx;
2594 if (bkref_ent->node != node_idx || bkref_ent->str_idx != cur_str_idx)
2596 subexp_len = bkref_ent->subexp_to - bkref_ent->subexp_from;
2597 new_dest_nodes = (subexp_len == 0
2598 ? dfa->eclosures + dfa->edests[node_idx].elems[0]
2599 : dfa->eclosures + dfa->nexts[node_idx]);
2600 dest_str_idx = (cur_str_idx + bkref_ent->subexp_to
2601 - bkref_ent->subexp_from);
2602 context = re_string_context_at (&mctx->input, dest_str_idx - 1,
2604 dest_state = mctx->state_log[dest_str_idx];
2605 prev_nelem = ((mctx->state_log[cur_str_idx] == NULL) ? 0
2606 : mctx->state_log[cur_str_idx]->nodes.nelem);
2607 /* Add `new_dest_node' to state_log. */
2608 if (dest_state == NULL)
2610 mctx->state_log[dest_str_idx]
2611 = re_acquire_state_context (&err, dfa, new_dest_nodes,
2613 if (BE (mctx->state_log[dest_str_idx] == NULL
2614 && err != REG_NOERROR, 0))
2619 re_node_set dest_nodes;
2620 err = re_node_set_init_union (&dest_nodes,
2621 dest_state->entrance_nodes,
2623 if (BE (err != REG_NOERROR, 0))
2625 re_node_set_free (&dest_nodes);
2628 mctx->state_log[dest_str_idx]
2629 = re_acquire_state_context (&err, dfa, &dest_nodes, context);
2630 re_node_set_free (&dest_nodes);
2631 if (BE (mctx->state_log[dest_str_idx] == NULL
2632 && err != REG_NOERROR, 0))
2635 /* We need to check recursively if the backreference can epsilon
2638 && mctx->state_log[cur_str_idx]->nodes.nelem > prev_nelem)
2640 err = check_subexp_matching_top (mctx, new_dest_nodes,
2642 if (BE (err != REG_NOERROR, 0))
2644 err = transit_state_bkref (mctx, new_dest_nodes);
2645 if (BE (err != REG_NOERROR, 0))
2655 /* Enumerate all the candidates which the backreference BKREF_NODE can match
2656 at BKREF_STR_IDX, and register them by match_ctx_add_entry().
2657 Note that we might collect inappropriate candidates here.
2658 However, the cost of checking them strictly here is too high, then we
2659 delay these checking for prune_impossible_nodes(). */
2661 static reg_errcode_t
2662 get_subexp (mctx, bkref_node, bkref_str_idx)
2663 re_match_context_t *mctx;
2664 int bkref_node, bkref_str_idx;
2666 re_dfa_t *const dfa = mctx->dfa;
2667 int subexp_num, sub_top_idx;
2668 const char *buf = (const char *) re_string_get_buffer (&mctx->input);
2669 /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX. */
2670 int cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
2671 if (cache_idx != -1)
2673 const struct re_backref_cache_entry *entry = mctx->bkref_ents + cache_idx;
2675 if (entry->node == bkref_node)
2676 return REG_NOERROR; /* We already checked it. */
2677 while (entry++->more);
2680 subexp_num = dfa->nodes[bkref_node].opr.idx;
2682 /* For each sub expression */
2683 for (sub_top_idx = 0; sub_top_idx < mctx->nsub_tops; ++sub_top_idx)
2686 re_sub_match_top_t *sub_top = mctx->sub_tops[sub_top_idx];
2687 re_sub_match_last_t *sub_last;
2688 int sub_last_idx, sl_str, bkref_str_off;
2690 if (dfa->nodes[sub_top->node].opr.idx != subexp_num)
2691 continue; /* It isn't related. */
2693 sl_str = sub_top->str_idx;
2694 bkref_str_off = bkref_str_idx;
2695 /* At first, check the last node of sub expressions we already
2697 for (sub_last_idx = 0; sub_last_idx < sub_top->nlasts; ++sub_last_idx)
2700 sub_last = sub_top->lasts[sub_last_idx];
2701 sl_str_diff = sub_last->str_idx - sl_str;
2702 /* The matched string by the sub expression match with the substring
2703 at the back reference? */
2704 if (sl_str_diff > 0)
2706 if (BE (bkref_str_off + sl_str_diff > mctx->input.valid_len, 0))
2708 /* Not enough chars for a successful match. */
2709 if (bkref_str_off + sl_str_diff > mctx->input.len)
2712 err = clean_state_log_if_needed (mctx,
2715 if (BE (err != REG_NOERROR, 0))
2717 buf = (const char *) re_string_get_buffer (&mctx->input);
2719 if (memcmp (buf + bkref_str_off, buf + sl_str, sl_str_diff) != 0)
2720 break; /* We don't need to search this sub expression any more. */
2722 bkref_str_off += sl_str_diff;
2723 sl_str += sl_str_diff;
2724 err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node,
2727 /* Reload buf, since the preceding call might have reallocated
2729 buf = (const char *) re_string_get_buffer (&mctx->input);
2731 if (err == REG_NOMATCH)
2733 if (BE (err != REG_NOERROR, 0))
2737 if (sub_last_idx < sub_top->nlasts)
2739 if (sub_last_idx > 0)
2741 /* Then, search for the other last nodes of the sub expression. */
2742 for (; sl_str <= bkref_str_idx; ++sl_str)
2744 int cls_node, sl_str_off;
2745 const re_node_set *nodes;
2746 sl_str_off = sl_str - sub_top->str_idx;
2747 /* The matched string by the sub expression match with the substring
2748 at the back reference? */
2751 if (BE (bkref_str_off >= mctx->input.valid_len, 0))
2753 /* If we are at the end of the input, we cannot match. */
2754 if (bkref_str_off >= mctx->input.len)
2757 err = extend_buffers (mctx);
2758 if (BE (err != REG_NOERROR, 0))
2761 buf = (const char *) re_string_get_buffer (&mctx->input);
2763 if (buf [bkref_str_off++] != buf[sl_str - 1])
2764 break; /* We don't need to search this sub expression
2767 if (mctx->state_log[sl_str] == NULL)
2769 /* Does this state have a ')' of the sub expression? */
2770 nodes = &mctx->state_log[sl_str]->nodes;
2771 cls_node = find_subexp_node (dfa, nodes, subexp_num, OP_CLOSE_SUBEXP);
2774 if (sub_top->path == NULL)
2776 sub_top->path = calloc (sizeof (state_array_t),
2777 sl_str - sub_top->str_idx + 1);
2778 if (sub_top->path == NULL)
2781 /* Can the OP_OPEN_SUBEXP node arrive the OP_CLOSE_SUBEXP node
2782 in the current context? */
2783 err = check_arrival (mctx, sub_top->path, sub_top->node,
2784 sub_top->str_idx, cls_node, sl_str, OP_CLOSE_SUBEXP);
2785 if (err == REG_NOMATCH)
2787 if (BE (err != REG_NOERROR, 0))
2789 sub_last = match_ctx_add_sublast (sub_top, cls_node, sl_str);
2790 if (BE (sub_last == NULL, 0))
2792 err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node,
2794 if (err == REG_NOMATCH)
2801 /* Helper functions for get_subexp(). */
2803 /* Check SUB_LAST can arrive to the back reference BKREF_NODE at BKREF_STR.
2804 If it can arrive, register the sub expression expressed with SUB_TOP
2807 static reg_errcode_t
2808 get_subexp_sub (mctx, sub_top, sub_last, bkref_node, bkref_str)
2809 re_match_context_t *mctx;
2810 const re_sub_match_top_t *sub_top;
2811 re_sub_match_last_t *sub_last;
2812 int bkref_node, bkref_str;
2816 /* Can the subexpression arrive the back reference? */
2817 err = check_arrival (mctx, &sub_last->path, sub_last->node,
2818 sub_last->str_idx, bkref_node, bkref_str, OP_OPEN_SUBEXP);
2819 if (err != REG_NOERROR)
2821 err = match_ctx_add_entry (mctx, bkref_node, bkref_str, sub_top->str_idx,
2823 if (BE (err != REG_NOERROR, 0))
2825 to_idx = bkref_str + sub_last->str_idx - sub_top->str_idx;
2826 return clean_state_log_if_needed (mctx, to_idx);
2829 /* Find the first node which is '(' or ')' and whose index is SUBEXP_IDX.
2830 Search '(' if FL_OPEN, or search ')' otherwise.
2831 TODO: This function isn't efficient...
2832 Because there might be more than one nodes whose types are
2833 OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all
2838 find_subexp_node (dfa, nodes, subexp_idx, type)
2839 const re_dfa_t *dfa;
2840 const re_node_set *nodes;
2841 int subexp_idx, type;
2844 for (cls_idx = 0; cls_idx < nodes->nelem; ++cls_idx)
2846 int cls_node = nodes->elems[cls_idx];
2847 const re_token_t *node = dfa->nodes + cls_node;
2848 if (node->type == type
2849 && node->opr.idx == subexp_idx)
2855 /* Check whether the node TOP_NODE at TOP_STR can arrive to the node
2856 LAST_NODE at LAST_STR. We record the path onto PATH since it will be
2858 Return REG_NOERROR if it can arrive, or REG_NOMATCH otherwise. */
2860 static reg_errcode_t
2861 check_arrival (mctx, path, top_node, top_str, last_node, last_str,
2863 re_match_context_t *mctx;
2864 state_array_t *path;
2865 int top_node, top_str, last_node, last_str, type;
2867 re_dfa_t *const dfa = mctx->dfa;
2869 int subexp_num, backup_cur_idx, str_idx, null_cnt;
2870 re_dfastate_t *cur_state = NULL;
2871 re_node_set *cur_nodes, next_nodes;
2872 re_dfastate_t **backup_state_log;
2873 unsigned int context;
2875 subexp_num = dfa->nodes[top_node].opr.idx;
2876 /* Extend the buffer if we need. */
2877 if (BE (path->alloc < last_str + mctx->max_mb_elem_len + 1, 0))
2879 re_dfastate_t **new_array;
2880 int old_alloc = path->alloc;
2881 path->alloc += last_str + mctx->max_mb_elem_len + 1;
2882 new_array = re_realloc (path->array, re_dfastate_t *, path->alloc);
2883 if (new_array == NULL)
2885 path->alloc = old_alloc;
2888 path->array = new_array;
2889 memset (new_array + old_alloc, '\0',
2890 sizeof (re_dfastate_t *) * (path->alloc - old_alloc));
2893 str_idx = path->next_idx == 0 ? top_str : path->next_idx;
2895 /* Temporary modify MCTX. */
2896 backup_state_log = mctx->state_log;
2897 backup_cur_idx = mctx->input.cur_idx;
2898 mctx->state_log = path->array;
2899 mctx->input.cur_idx = str_idx;
2901 /* Setup initial node set. */
2902 context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags);
2903 if (str_idx == top_str)
2905 err = re_node_set_init_1 (&next_nodes, top_node);
2906 if (BE (err != REG_NOERROR, 0))
2908 err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type);
2909 if (BE (err != REG_NOERROR, 0))
2911 re_node_set_free (&next_nodes);
2917 cur_state = mctx->state_log[str_idx];
2918 if (cur_state && cur_state->has_backref)
2920 err = re_node_set_init_copy (&next_nodes, &cur_state->nodes);
2921 if (BE ( err != REG_NOERROR, 0))
2925 re_node_set_init_empty (&next_nodes);
2927 if (str_idx == top_str || (cur_state && cur_state->has_backref))
2929 if (next_nodes.nelem)
2931 err = expand_bkref_cache (mctx, &next_nodes, str_idx,
2933 if (BE ( err != REG_NOERROR, 0))
2935 re_node_set_free (&next_nodes);
2939 cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context);
2940 if (BE (cur_state == NULL && err != REG_NOERROR, 0))
2942 re_node_set_free (&next_nodes);
2945 mctx->state_log[str_idx] = cur_state;
2948 for (null_cnt = 0; str_idx < last_str && null_cnt <= mctx->max_mb_elem_len;)
2950 re_node_set_empty (&next_nodes);
2951 if (mctx->state_log[str_idx + 1])
2953 err = re_node_set_merge (&next_nodes,
2954 &mctx->state_log[str_idx + 1]->nodes);
2955 if (BE (err != REG_NOERROR, 0))
2957 re_node_set_free (&next_nodes);
2963 err = check_arrival_add_next_nodes (mctx, str_idx,
2964 &cur_state->non_eps_nodes, &next_nodes);
2965 if (BE (err != REG_NOERROR, 0))
2967 re_node_set_free (&next_nodes);
2972 if (next_nodes.nelem)
2974 err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type);
2975 if (BE (err != REG_NOERROR, 0))
2977 re_node_set_free (&next_nodes);
2980 err = expand_bkref_cache (mctx, &next_nodes, str_idx,
2982 if (BE ( err != REG_NOERROR, 0))
2984 re_node_set_free (&next_nodes);
2988 context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags);
2989 cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context);
2990 if (BE (cur_state == NULL && err != REG_NOERROR, 0))
2992 re_node_set_free (&next_nodes);
2995 mctx->state_log[str_idx] = cur_state;
2996 null_cnt = cur_state == NULL ? null_cnt + 1 : 0;
2998 re_node_set_free (&next_nodes);
2999 cur_nodes = (mctx->state_log[last_str] == NULL ? NULL
3000 : &mctx->state_log[last_str]->nodes);
3001 path->next_idx = str_idx;
3004 mctx->state_log = backup_state_log;
3005 mctx->input.cur_idx = backup_cur_idx;
3007 /* Then check the current node set has the node LAST_NODE. */
3008 if (cur_nodes != NULL && re_node_set_contains (cur_nodes, last_node))
3014 /* Helper functions for check_arrival. */
3016 /* Calculate the destination nodes of CUR_NODES at STR_IDX, and append them
3018 TODO: This function is similar to the functions transit_state*(),
3019 however this function has many additional works.
3020 Can't we unify them? */
3022 static reg_errcode_t
3023 check_arrival_add_next_nodes (mctx, str_idx, cur_nodes, next_nodes)
3024 re_match_context_t *mctx;
3026 re_node_set *cur_nodes, *next_nodes;
3028 re_dfa_t *const dfa = mctx->dfa;
3032 re_node_set union_set;
3033 re_node_set_init_empty (&union_set);
3034 for (cur_idx = 0; cur_idx < cur_nodes->nelem; ++cur_idx)
3037 int cur_node = cur_nodes->elems[cur_idx];
3039 re_token_type_t type = dfa->nodes[cur_node].type;
3040 assert (!IS_EPSILON_NODE (type));
3042 #ifdef RE_ENABLE_I18N
3043 /* If the node may accept `multi byte'. */
3044 if (dfa->nodes[cur_node].accept_mb)
3046 naccepted = check_node_accept_bytes (dfa, cur_node, &mctx->input,
3050 re_dfastate_t *dest_state;
3051 int next_node = dfa->nexts[cur_node];
3052 int next_idx = str_idx + naccepted;
3053 dest_state = mctx->state_log[next_idx];
3054 re_node_set_empty (&union_set);
3057 err = re_node_set_merge (&union_set, &dest_state->nodes);
3058 if (BE (err != REG_NOERROR, 0))
3060 re_node_set_free (&union_set);
3064 result = re_node_set_insert (&union_set, next_node);
3065 if (BE (result < 0, 0))
3067 re_node_set_free (&union_set);
3070 mctx->state_log[next_idx] = re_acquire_state (&err, dfa,
3072 if (BE (mctx->state_log[next_idx] == NULL
3073 && err != REG_NOERROR, 0))
3075 re_node_set_free (&union_set);
3080 #endif /* RE_ENABLE_I18N */
3082 || check_node_accept (mctx, dfa->nodes + cur_node, str_idx))
3084 result = re_node_set_insert (next_nodes, dfa->nexts[cur_node]);
3085 if (BE (result < 0, 0))
3087 re_node_set_free (&union_set);
3092 re_node_set_free (&union_set);
3096 /* For all the nodes in CUR_NODES, add the epsilon closures of them to
3097 CUR_NODES, however exclude the nodes which are:
3098 - inside the sub expression whose number is EX_SUBEXP, if FL_OPEN.
3099 - out of the sub expression whose number is EX_SUBEXP, if !FL_OPEN.
3102 static reg_errcode_t
3103 check_arrival_expand_ecl (dfa, cur_nodes, ex_subexp, type)
3105 re_node_set *cur_nodes;
3106 int ex_subexp, type;
3109 int idx, outside_node;
3110 re_node_set new_nodes;
3112 assert (cur_nodes->nelem);
3114 err = re_node_set_alloc (&new_nodes, cur_nodes->nelem);
3115 if (BE (err != REG_NOERROR, 0))
3117 /* Create a new node set NEW_NODES with the nodes which are epsilon
3118 closures of the node in CUR_NODES. */
3120 for (idx = 0; idx < cur_nodes->nelem; ++idx)
3122 int cur_node = cur_nodes->elems[idx];
3123 re_node_set *eclosure = dfa->eclosures + cur_node;
3124 outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type);
3125 if (outside_node == -1)
3127 /* There are no problematic nodes, just merge them. */
3128 err = re_node_set_merge (&new_nodes, eclosure);
3129 if (BE (err != REG_NOERROR, 0))
3131 re_node_set_free (&new_nodes);
3137 /* There are problematic nodes, re-calculate incrementally. */
3138 err = check_arrival_expand_ecl_sub (dfa, &new_nodes, cur_node,
3140 if (BE (err != REG_NOERROR, 0))
3142 re_node_set_free (&new_nodes);
3147 re_node_set_free (cur_nodes);
3148 *cur_nodes = new_nodes;
3152 /* Helper function for check_arrival_expand_ecl.
3153 Check incrementally the epsilon closure of TARGET, and if it isn't
3154 problematic append it to DST_NODES. */
3156 static reg_errcode_t
3157 check_arrival_expand_ecl_sub (dfa, dst_nodes, target, ex_subexp, type)
3159 int target, ex_subexp, type;
3160 re_node_set *dst_nodes;
3163 for (cur_node = target; !re_node_set_contains (dst_nodes, cur_node);)
3167 if (dfa->nodes[cur_node].type == type
3168 && dfa->nodes[cur_node].opr.idx == ex_subexp)
3170 if (type == OP_CLOSE_SUBEXP)
3172 err = re_node_set_insert (dst_nodes, cur_node);
3173 if (BE (err == -1, 0))
3178 err = re_node_set_insert (dst_nodes, cur_node);
3179 if (BE (err == -1, 0))
3181 if (dfa->edests[cur_node].nelem == 0)
3183 if (dfa->edests[cur_node].nelem == 2)
3185 err = check_arrival_expand_ecl_sub (dfa, dst_nodes,
3186 dfa->edests[cur_node].elems[1],
3188 if (BE (err != REG_NOERROR, 0))
3191 cur_node = dfa->edests[cur_node].elems[0];
3197 /* For all the back references in the current state, calculate the
3198 destination of the back references by the appropriate entry
3199 in MCTX->BKREF_ENTS. */
3201 static reg_errcode_t
3202 expand_bkref_cache (mctx, cur_nodes, cur_str, subexp_num,
3204 re_match_context_t *mctx;
3205 int cur_str, subexp_num, type;
3206 re_node_set *cur_nodes;
3208 re_dfa_t *const dfa = mctx->dfa;
3210 int cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
3211 struct re_backref_cache_entry *ent;
3213 if (cache_idx_start == -1)
3217 ent = mctx->bkref_ents + cache_idx_start;
3220 int to_idx, next_node;
3222 /* Is this entry ENT is appropriate? */
3223 if (!re_node_set_contains (cur_nodes, ent->node))
3226 to_idx = cur_str + ent->subexp_to - ent->subexp_from;
3227 /* Calculate the destination of the back reference, and append it
3228 to MCTX->STATE_LOG. */
3229 if (to_idx == cur_str)
3231 /* The backreference did epsilon transit, we must re-check all the
3232 node in the current state. */
3233 re_node_set new_dests;
3234 reg_errcode_t err2, err3;
3235 next_node = dfa->edests[ent->node].elems[0];
3236 if (re_node_set_contains (cur_nodes, next_node))
3238 err = re_node_set_init_1 (&new_dests, next_node);
3239 err2 = check_arrival_expand_ecl (dfa, &new_dests, subexp_num, type);
3240 err3 = re_node_set_merge (cur_nodes, &new_dests);
3241 re_node_set_free (&new_dests);
3242 if (BE (err != REG_NOERROR || err2 != REG_NOERROR
3243 || err3 != REG_NOERROR, 0))
3245 err = (err != REG_NOERROR ? err
3246 : (err2 != REG_NOERROR ? err2 : err3));
3249 /* TODO: It is still inefficient... */
3254 re_node_set union_set;
3255 next_node = dfa->nexts[ent->node];
3256 if (mctx->state_log[to_idx])
3259 if (re_node_set_contains (&mctx->state_log[to_idx]->nodes,
3262 err = re_node_set_init_copy (&union_set,
3263 &mctx->state_log[to_idx]->nodes);
3264 ret = re_node_set_insert (&union_set, next_node);
3265 if (BE (err != REG_NOERROR || ret < 0, 0))
3267 re_node_set_free (&union_set);
3268 err = err != REG_NOERROR ? err : REG_ESPACE;
3274 err = re_node_set_init_1 (&union_set, next_node);
3275 if (BE (err != REG_NOERROR, 0))
3278 mctx->state_log[to_idx] = re_acquire_state (&err, dfa, &union_set);
3279 re_node_set_free (&union_set);
3280 if (BE (mctx->state_log[to_idx] == NULL
3281 && err != REG_NOERROR, 0))
3285 while (ent++->more);
3289 /* Build transition table for the state.
3290 Return 1 if succeeded, otherwise return NULL. */
3293 build_trtable (dfa, state)
3295 re_dfastate_t *state;
3298 int i, j, ch, need_word_trtable = 0;
3299 unsigned int elem, mask;
3300 int dests_node_malloced = 0, dest_states_malloced = 0;
3301 int ndests; /* Number of the destination states from `state'. */
3302 re_dfastate_t **trtable;
3303 re_dfastate_t **dest_states = NULL, **dest_states_word, **dest_states_nl;
3304 re_node_set follows, *dests_node;
3308 /* We build DFA states which corresponds to the destination nodes
3309 from `state'. `dests_node[i]' represents the nodes which i-th
3310 destination state contains, and `dests_ch[i]' represents the
3311 characters which i-th destination state accepts. */
3313 if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX))
3314 dests_node = (re_node_set *)
3315 alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX);
3319 dests_node = (re_node_set *)
3320 malloc ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX);
3321 if (BE (dests_node == NULL, 0))
3323 dests_node_malloced = 1;
3325 dests_ch = (bitset *) (dests_node + SBC_MAX);
3327 /* Initialize transiton table. */
3328 state->word_trtable = state->trtable = NULL;
3330 /* At first, group all nodes belonging to `state' into several
3332 ndests = group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch);
3333 if (BE (ndests <= 0, 0))
3335 if (dests_node_malloced)
3337 /* Return 0 in case of an error, 1 otherwise. */
3340 state->trtable = (re_dfastate_t **)
3341 calloc (sizeof (re_dfastate_t *), SBC_MAX);
3347 err = re_node_set_alloc (&follows, ndests + 1);
3348 if (BE (err != REG_NOERROR, 0))
3352 if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX
3353 + ndests * 3 * sizeof (re_dfastate_t *)))
3354 dest_states = (re_dfastate_t **)
3355 alloca (ndests * 3 * sizeof (re_dfastate_t *));
3359 dest_states = (re_dfastate_t **)
3360 malloc (ndests * 3 * sizeof (re_dfastate_t *));
3361 if (BE (dest_states == NULL, 0))
3364 if (dest_states_malloced)
3366 re_node_set_free (&follows);
3367 for (i = 0; i < ndests; ++i)
3368 re_node_set_free (dests_node + i);
3369 if (dests_node_malloced)
3373 dest_states_malloced = 1;
3375 dest_states_word = dest_states + ndests;
3376 dest_states_nl = dest_states_word + ndests;
3377 bitset_empty (acceptable);
3379 /* Then build the states for all destinations. */
3380 for (i = 0; i < ndests; ++i)
3383 re_node_set_empty (&follows);
3384 /* Merge the follows of this destination states. */
3385 for (j = 0; j < dests_node[i].nelem; ++j)
3387 next_node = dfa->nexts[dests_node[i].elems[j]];
3388 if (next_node != -1)
3390 err = re_node_set_merge (&follows, dfa->eclosures + next_node);
3391 if (BE (err != REG_NOERROR, 0))
3395 dest_states[i] = re_acquire_state_context (&err, dfa, &follows, 0);
3396 if (BE (dest_states[i] == NULL && err != REG_NOERROR, 0))
3398 /* If the new state has context constraint,
3399 build appropriate states for these contexts. */
3400 if (dest_states[i]->has_constraint)
3402 dest_states_word[i] = re_acquire_state_context (&err, dfa, &follows,
3404 if (BE (dest_states_word[i] == NULL && err != REG_NOERROR, 0))
3407 if (dest_states[i] != dest_states_word[i] && dfa->mb_cur_max > 1)
3408 need_word_trtable = 1;
3410 dest_states_nl[i] = re_acquire_state_context (&err, dfa, &follows,
3412 if (BE (dest_states_nl[i] == NULL && err != REG_NOERROR, 0))
3417 dest_states_word[i] = dest_states[i];
3418 dest_states_nl[i] = dest_states[i];
3420 bitset_merge (acceptable, dests_ch[i]);
3423 if (!BE (need_word_trtable, 0))
3425 /* We don't care about whether the following character is a word
3426 character, or we are in a single-byte character set so we can
3427 discern by looking at the character code: allocate a
3428 256-entry transition table. */
3429 trtable = state->trtable =
3430 (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX);
3431 if (BE (trtable == NULL, 0))
3434 /* For all characters ch...: */
3435 for (i = 0; i < BITSET_UINTS; ++i)
3436 for (ch = i * UINT_BITS, elem = acceptable[i], mask = 1;
3438 mask <<= 1, elem >>= 1, ++ch)
3439 if (BE (elem & 1, 0))
3441 /* There must be exactly one destination which accepts
3442 character ch. See group_nodes_into_DFAstates. */
3443 for (j = 0; (dests_ch[j][i] & mask) == 0; ++j)
3446 /* j-th destination accepts the word character ch. */
3447 if (dfa->word_char[i] & mask)
3448 trtable[ch] = dest_states_word[j];
3450 trtable[ch] = dest_states[j];
3455 /* We care about whether the following character is a word
3456 character, and we are in a multi-byte character set: discern
3457 by looking at the character code: build two 256-entry
3458 transition tables, one starting at trtable[0] and one
3459 starting at trtable[SBC_MAX]. */
3460 trtable = state->word_trtable =
3461 (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX);
3462 if (BE (trtable == NULL, 0))
3465 /* For all characters ch...: */
3466 for (i = 0; i < BITSET_UINTS; ++i)
3467 for (ch = i * UINT_BITS, elem = acceptable[i], mask = 1;
3469 mask <<= 1, elem >>= 1, ++ch)
3470 if (BE (elem & 1, 0))
3472 /* There must be exactly one destination which accepts
3473 character ch. See group_nodes_into_DFAstates. */
3474 for (j = 0; (dests_ch[j][i] & mask) == 0; ++j)
3477 /* j-th destination accepts the word character ch. */
3478 trtable[ch] = dest_states[j];
3479 trtable[ch + SBC_MAX] = dest_states_word[j];
3484 if (bitset_contain (acceptable, NEWLINE_CHAR))
3486 /* The current state accepts newline character. */
3487 for (j = 0; j < ndests; ++j)
3488 if (bitset_contain (dests_ch[j], NEWLINE_CHAR))
3490 /* k-th destination accepts newline character. */
3491 trtable[NEWLINE_CHAR] = dest_states_nl[j];
3492 if (need_word_trtable)
3493 trtable[NEWLINE_CHAR + SBC_MAX] = dest_states_nl[j];
3494 /* There must be only one destination which accepts
3495 newline. See group_nodes_into_DFAstates. */
3500 if (dest_states_malloced)
3503 re_node_set_free (&follows);
3504 for (i = 0; i < ndests; ++i)
3505 re_node_set_free (dests_node + i);
3507 if (dests_node_malloced)
3513 /* Group all nodes belonging to STATE into several destinations.
3514 Then for all destinations, set the nodes belonging to the destination
3515 to DESTS_NODE[i] and set the characters accepted by the destination
3516 to DEST_CH[i]. This function return the number of destinations. */
3519 group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch)
3521 const re_dfastate_t *state;
3522 re_node_set *dests_node;
3528 int ndests; /* Number of the destinations from `state'. */
3529 bitset accepts; /* Characters a node can accept. */
3530 const re_node_set *cur_nodes = &state->nodes;
3531 bitset_empty (accepts);
3534 /* For all the nodes belonging to `state', */
3535 for (i = 0; i < cur_nodes->nelem; ++i)
3537 re_token_t *node = &dfa->nodes[cur_nodes->elems[i]];
3538 re_token_type_t type = node->type;
3539 unsigned int constraint = node->constraint;
3541 /* Enumerate all single byte character this node can accept. */
3542 if (type == CHARACTER)
3543 bitset_set (accepts, node->opr.c);
3544 else if (type == SIMPLE_BRACKET)
3546 bitset_merge (accepts, node->opr.sbcset);
3548 else if (type == OP_PERIOD)
3550 #ifdef RE_ENABLE_I18N
3551 if (dfa->mb_cur_max > 1)
3552 bitset_merge (accepts, dfa->sb_char);
3555 bitset_set_all (accepts);
3556 if (!(dfa->syntax & RE_DOT_NEWLINE))
3557 bitset_clear (accepts, '\n');
3558 if (dfa->syntax & RE_DOT_NOT_NULL)
3559 bitset_clear (accepts, '\0');
3561 #ifdef RE_ENABLE_I18N
3562 else if (type == OP_UTF8_PERIOD)
3564 memset (accepts, 255, sizeof (unsigned int) * BITSET_UINTS / 2);
3565 if (!(dfa->syntax & RE_DOT_NEWLINE))
3566 bitset_clear (accepts, '\n');
3567 if (dfa->syntax & RE_DOT_NOT_NULL)
3568 bitset_clear (accepts, '\0');
3574 /* Check the `accepts' and sift the characters which are not
3575 match it the context. */
3578 if (constraint & NEXT_NEWLINE_CONSTRAINT)
3580 int accepts_newline = bitset_contain (accepts, NEWLINE_CHAR);
3581 bitset_empty (accepts);
3582 if (accepts_newline)
3583 bitset_set (accepts, NEWLINE_CHAR);
3587 if (constraint & NEXT_ENDBUF_CONSTRAINT)
3589 bitset_empty (accepts);
3593 if (constraint & NEXT_WORD_CONSTRAINT)
3595 unsigned int any_set = 0;
3596 if (type == CHARACTER && !node->word_char)
3598 bitset_empty (accepts);
3601 #ifdef RE_ENABLE_I18N
3602 if (dfa->mb_cur_max > 1)
3603 for (j = 0; j < BITSET_UINTS; ++j)
3604 any_set |= (accepts[j] &= (dfa->word_char[j] | ~dfa->sb_char[j]));
3607 for (j = 0; j < BITSET_UINTS; ++j)
3608 any_set |= (accepts[j] &= dfa->word_char[j]);
3612 if (constraint & NEXT_NOTWORD_CONSTRAINT)
3614 unsigned int any_set = 0;
3615 if (type == CHARACTER && node->word_char)
3617 bitset_empty (accepts);
3620 #ifdef RE_ENABLE_I18N
3621 if (dfa->mb_cur_max > 1)
3622 for (j = 0; j < BITSET_UINTS; ++j)
3623 any_set |= (accepts[j] &= ~(dfa->word_char[j] & dfa->sb_char[j]));
3626 for (j = 0; j < BITSET_UINTS; ++j)
3627 any_set |= (accepts[j] &= ~dfa->word_char[j]);
3633 /* Then divide `accepts' into DFA states, or create a new
3634 state. Above, we make sure that accepts is not empty. */
3635 for (j = 0; j < ndests; ++j)
3637 bitset intersec; /* Intersection sets, see below. */
3639 /* Flags, see below. */
3640 int has_intersec, not_subset, not_consumed;
3642 /* Optimization, skip if this state doesn't accept the character. */
3643 if (type == CHARACTER && !bitset_contain (dests_ch[j], node->opr.c))
3646 /* Enumerate the intersection set of this state and `accepts'. */
3648 for (k = 0; k < BITSET_UINTS; ++k)
3649 has_intersec |= intersec[k] = accepts[k] & dests_ch[j][k];
3650 /* And skip if the intersection set is empty. */
3654 /* Then check if this state is a subset of `accepts'. */
3655 not_subset = not_consumed = 0;
3656 for (k = 0; k < BITSET_UINTS; ++k)
3658 not_subset |= remains[k] = ~accepts[k] & dests_ch[j][k];
3659 not_consumed |= accepts[k] = accepts[k] & ~dests_ch[j][k];
3662 /* If this state isn't a subset of `accepts', create a
3663 new group state, which has the `remains'. */
3666 bitset_copy (dests_ch[ndests], remains);
3667 bitset_copy (dests_ch[j], intersec);
3668 err = re_node_set_init_copy (dests_node + ndests, &dests_node[j]);
3669 if (BE (err != REG_NOERROR, 0))
3674 /* Put the position in the current group. */
3675 result = re_node_set_insert (&dests_node[j], cur_nodes->elems[i]);
3676 if (BE (result < 0, 0))
3679 /* If all characters are consumed, go to next node. */
3683 /* Some characters remain, create a new group. */
3686 bitset_copy (dests_ch[ndests], accepts);
3687 err = re_node_set_init_1 (dests_node + ndests, cur_nodes->elems[i]);
3688 if (BE (err != REG_NOERROR, 0))
3691 bitset_empty (accepts);
3696 for (j = 0; j < ndests; ++j)
3697 re_node_set_free (dests_node + j);
3701 #ifdef RE_ENABLE_I18N
3702 /* Check how many bytes the node `dfa->nodes[node_idx]' accepts.
3703 Return the number of the bytes the node accepts.
3704 STR_IDX is the current index of the input string.
3706 This function handles the nodes which can accept one character, or
3707 one collating element like '.', '[a-z]', opposite to the other nodes
3708 can only accept one byte. */
3711 check_node_accept_bytes (dfa, node_idx, input, str_idx)
3713 int node_idx, str_idx;
3714 const re_string_t *input;
3716 const re_token_t *node = dfa->nodes + node_idx;
3717 int char_len, elem_len;
3720 if (BE (node->type == OP_UTF8_PERIOD, 0))
3722 unsigned char c = re_string_byte_at (input, str_idx), d;
3723 if (BE (c < 0xc2, 1))
3726 if (str_idx + 2 > input->len)
3729 d = re_string_byte_at (input, str_idx + 1);
3731 return (d < 0x80 || d > 0xbf) ? 0 : 2;
3735 if (c == 0xe0 && d < 0xa0)
3741 if (c == 0xf0 && d < 0x90)
3747 if (c == 0xf8 && d < 0x88)
3753 if (c == 0xfc && d < 0x84)
3759 if (str_idx + char_len > input->len)
3762 for (i = 1; i < char_len; ++i)
3764 d = re_string_byte_at (input, str_idx + i);
3765 if (d < 0x80 || d > 0xbf)
3771 char_len = re_string_char_size_at (input, str_idx);
3772 if (node->type == OP_PERIOD)
3776 /* FIXME: I don't think this if is needed, as both '\n'
3777 and '\0' are char_len == 1. */
3778 /* '.' accepts any one character except the following two cases. */
3779 if ((!(dfa->syntax & RE_DOT_NEWLINE) &&
3780 re_string_byte_at (input, str_idx) == '\n') ||
3781 ((dfa->syntax & RE_DOT_NOT_NULL) &&
3782 re_string_byte_at (input, str_idx) == '\0'))
3787 elem_len = re_string_elem_size_at (input, str_idx);
3788 if ((elem_len <= 1 && char_len <= 1) || char_len == 0)
3791 if (node->type == COMPLEX_BRACKET)
3793 const re_charset_t *cset = node->opr.mbcset;
3795 const unsigned char *pin
3796 = ((const unsigned char *) re_string_get_buffer (input) + str_idx);
3801 wchar_t wc = ((cset->nranges || cset->nchar_classes || cset->nmbchars)
3802 ? re_string_wchar_at (input, str_idx) : 0);
3804 /* match with multibyte character? */
3805 for (i = 0; i < cset->nmbchars; ++i)
3806 if (wc == cset->mbchars[i])
3808 match_len = char_len;
3809 goto check_node_accept_bytes_match;
3811 /* match with character_class? */
3812 for (i = 0; i < cset->nchar_classes; ++i)
3814 wctype_t wt = cset->char_classes[i];
3815 if (__iswctype (wc, wt))
3817 match_len = char_len;
3818 goto check_node_accept_bytes_match;
3823 nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3826 unsigned int in_collseq = 0;
3827 const int32_t *table, *indirect;
3828 const unsigned char *weights, *extra;
3829 const char *collseqwc;
3831 /* This #include defines a local function! */
3832 # include <locale/weight.h>
3834 /* match with collating_symbol? */
3835 if (cset->ncoll_syms)
3836 extra = (const unsigned char *)
3837 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
3838 for (i = 0; i < cset->ncoll_syms; ++i)
3840 const unsigned char *coll_sym = extra + cset->coll_syms[i];
3841 /* Compare the length of input collating element and
3842 the length of current collating element. */
3843 if (*coll_sym != elem_len)
3845 /* Compare each bytes. */
3846 for (j = 0; j < *coll_sym; j++)
3847 if (pin[j] != coll_sym[1 + j])
3851 /* Match if every bytes is equal. */
3853 goto check_node_accept_bytes_match;
3859 if (elem_len <= char_len)
3861 collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
3862 in_collseq = __collseq_table_lookup (collseqwc, wc);
3865 in_collseq = find_collation_sequence_value (pin, elem_len);
3867 /* match with range expression? */
3868 for (i = 0; i < cset->nranges; ++i)
3869 if (cset->range_starts[i] <= in_collseq
3870 && in_collseq <= cset->range_ends[i])
3872 match_len = elem_len;
3873 goto check_node_accept_bytes_match;
3876 /* match with equivalence_class? */
3877 if (cset->nequiv_classes)
3879 const unsigned char *cp = pin;
3880 table = (const int32_t *)
3881 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
3882 weights = (const unsigned char *)
3883 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
3884 extra = (const unsigned char *)
3885 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
3886 indirect = (const int32_t *)
3887 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
3888 idx = findidx (&cp);
3890 for (i = 0; i < cset->nequiv_classes; ++i)
3892 int32_t equiv_class_idx = cset->equiv_classes[i];
3893 size_t weight_len = weights[idx];
3894 if (weight_len == weights[equiv_class_idx])
3897 while (cnt <= weight_len
3898 && (weights[equiv_class_idx + 1 + cnt]
3899 == weights[idx + 1 + cnt]))
3901 if (cnt > weight_len)
3903 match_len = elem_len;
3904 goto check_node_accept_bytes_match;
3913 /* match with range expression? */
3915 wchar_t cmp_buf[] = {L'\0', L'\0', wc, L'\0', L'\0', L'\0'};
3917 wchar_t cmp_buf[] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
3920 for (i = 0; i < cset->nranges; ++i)
3922 cmp_buf[0] = cset->range_starts[i];
3923 cmp_buf[4] = cset->range_ends[i];
3924 if (wcscoll (cmp_buf, cmp_buf + 2) <= 0
3925 && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0)
3927 match_len = char_len;
3928 goto check_node_accept_bytes_match;
3932 check_node_accept_bytes_match:
3933 if (!cset->non_match)
3940 return (elem_len > char_len) ? elem_len : char_len;
3948 find_collation_sequence_value (mbs, mbs_len)
3949 const unsigned char *mbs;
3952 uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3957 /* No valid character. Match it as a single byte character. */
3958 const unsigned char *collseq = (const unsigned char *)
3959 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
3960 return collseq[mbs[0]];
3967 const unsigned char *extra = (const unsigned char *)
3968 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
3969 int32_t extrasize = (const unsigned char *)
3970 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB + 1) - extra;
3972 for (idx = 0; idx < extrasize;)
3974 int mbs_cnt, found = 0;
3975 int32_t elem_mbs_len;
3976 /* Skip the name of collating element name. */
3977 idx = idx + extra[idx] + 1;
3978 elem_mbs_len = extra[idx++];
3979 if (mbs_len == elem_mbs_len)
3981 for (mbs_cnt = 0; mbs_cnt < elem_mbs_len; ++mbs_cnt)
3982 if (extra[idx + mbs_cnt] != mbs[mbs_cnt])
3984 if (mbs_cnt == elem_mbs_len)
3985 /* Found the entry. */
3988 /* Skip the byte sequence of the collating element. */
3989 idx += elem_mbs_len;
3990 /* Adjust for the alignment. */
3991 idx = (idx + 3) & ~3;
3992 /* Skip the collation sequence value. */
3993 idx += sizeof (uint32_t);
3994 /* Skip the wide char sequence of the collating element. */
3995 idx = idx + sizeof (uint32_t) * (extra[idx] + 1);
3996 /* If we found the entry, return the sequence value. */
3998 return *(uint32_t *) (extra + idx);
3999 /* Skip the collation sequence value. */
4000 idx += sizeof (uint32_t);
4006 #endif /* RE_ENABLE_I18N */
4008 /* Check whether the node accepts the byte which is IDX-th
4009 byte of the INPUT. */
4012 check_node_accept (mctx, node, idx)
4013 const re_match_context_t *mctx;
4014 const re_token_t *node;
4018 ch = re_string_byte_at (&mctx->input, idx);
4022 if (node->opr.c != ch)
4026 case SIMPLE_BRACKET:
4027 if (!bitset_contain (node->opr.sbcset, ch))
4031 #ifdef RE_ENABLE_I18N
4032 case OP_UTF8_PERIOD:
4038 if ((ch == '\n' && !(mctx->dfa->syntax & RE_DOT_NEWLINE))
4039 || (ch == '\0' && (mctx->dfa->syntax & RE_DOT_NOT_NULL)))
4047 if (node->constraint)
4049 /* The node has constraints. Check whether the current context
4050 satisfies the constraints. */
4051 unsigned int context = re_string_context_at (&mctx->input, idx,
4053 if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context))
4060 /* Extend the buffers, if the buffers have run out. */
4062 static reg_errcode_t
4063 extend_buffers (mctx)
4064 re_match_context_t *mctx;
4067 re_string_t *pstr = &mctx->input;
4069 /* Double the lengthes of the buffers. */
4070 ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2);
4071 if (BE (ret != REG_NOERROR, 0))
4074 if (mctx->state_log != NULL)
4076 /* And double the length of state_log. */
4077 /* XXX We have no indication of the size of this buffer. If this
4078 allocation fail we have no indication that the state_log array
4079 does not have the right size. */
4080 re_dfastate_t **new_array = re_realloc (mctx->state_log, re_dfastate_t *,
4081 pstr->bufs_len + 1);
4082 if (BE (new_array == NULL, 0))
4084 mctx->state_log = new_array;
4087 /* Then reconstruct the buffers. */
4090 #ifdef RE_ENABLE_I18N
4091 if (pstr->mb_cur_max > 1)
4093 ret = build_wcs_upper_buffer (pstr);
4094 if (BE (ret != REG_NOERROR, 0))
4098 #endif /* RE_ENABLE_I18N */
4099 build_upper_buffer (pstr);
4103 #ifdef RE_ENABLE_I18N
4104 if (pstr->mb_cur_max > 1)
4105 build_wcs_buffer (pstr);
4107 #endif /* RE_ENABLE_I18N */
4109 if (pstr->trans != NULL)
4110 re_string_translate_buffer (pstr);
4117 /* Functions for matching context. */
4119 /* Initialize MCTX. */
4121 static reg_errcode_t
4122 match_ctx_init (mctx, eflags, n)
4123 re_match_context_t *mctx;
4126 mctx->eflags = eflags;
4127 mctx->match_last = -1;
4130 mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n);
4131 mctx->sub_tops = re_malloc (re_sub_match_top_t *, n);
4132 if (BE (mctx->bkref_ents == NULL || mctx->sub_tops == NULL, 0))
4135 /* Already zero-ed by the caller.
4137 mctx->bkref_ents = NULL;
4138 mctx->nbkref_ents = 0;
4139 mctx->nsub_tops = 0; */
4140 mctx->abkref_ents = n;
4141 mctx->max_mb_elem_len = 1;
4142 mctx->asub_tops = n;
4146 /* Clean the entries which depend on the current input in MCTX.
4147 This function must be invoked when the matcher changes the start index
4148 of the input, or changes the input string. */
4151 match_ctx_clean (mctx)
4152 re_match_context_t *mctx;
4155 for (st_idx = 0; st_idx < mctx->nsub_tops; ++st_idx)
4158 re_sub_match_top_t *top = mctx->sub_tops[st_idx];
4159 for (sl_idx = 0; sl_idx < top->nlasts; ++sl_idx)
4161 re_sub_match_last_t *last = top->lasts[sl_idx];
4162 re_free (last->path.array);
4165 re_free (top->lasts);
4168 re_free (top->path->array);
4169 re_free (top->path);
4174 mctx->nsub_tops = 0;
4175 mctx->nbkref_ents = 0;
4178 /* Free all the memory associated with MCTX. */
4181 match_ctx_free (mctx)
4182 re_match_context_t *mctx;
4184 /* First, free all the memory associated with MCTX->SUB_TOPS. */
4185 match_ctx_clean (mctx);
4186 re_free (mctx->sub_tops);
4187 re_free (mctx->bkref_ents);
4190 /* Add a new backreference entry to MCTX.
4191 Note that we assume that caller never call this function with duplicate
4192 entry, and call with STR_IDX which isn't smaller than any existing entry.
4195 static reg_errcode_t
4196 match_ctx_add_entry (mctx, node, str_idx, from, to)
4197 re_match_context_t *mctx;
4198 int node, str_idx, from, to;
4200 if (mctx->nbkref_ents >= mctx->abkref_ents)
4202 struct re_backref_cache_entry* new_entry;
4203 new_entry = re_realloc (mctx->bkref_ents, struct re_backref_cache_entry,
4204 mctx->abkref_ents * 2);
4205 if (BE (new_entry == NULL, 0))
4207 re_free (mctx->bkref_ents);
4210 mctx->bkref_ents = new_entry;
4211 memset (mctx->bkref_ents + mctx->nbkref_ents, '\0',
4212 sizeof (struct re_backref_cache_entry) * mctx->abkref_ents);
4213 mctx->abkref_ents *= 2;
4215 if (mctx->nbkref_ents > 0
4216 && mctx->bkref_ents[mctx->nbkref_ents - 1].str_idx == str_idx)
4217 mctx->bkref_ents[mctx->nbkref_ents - 1].more = 1;
4219 mctx->bkref_ents[mctx->nbkref_ents].node = node;
4220 mctx->bkref_ents[mctx->nbkref_ents].str_idx = str_idx;
4221 mctx->bkref_ents[mctx->nbkref_ents].subexp_from = from;
4222 mctx->bkref_ents[mctx->nbkref_ents].subexp_to = to;
4224 /* This is a cache that saves negative results of check_dst_limits_calc_pos.
4225 If bit N is clear, means that this entry won't epsilon-transition to
4226 an OP_OPEN_SUBEXP or OP_CLOSE_SUBEXP for the N+1-th subexpression. If
4227 it is set, check_dst_limits_calc_pos_1 will recurse and try to find one
4230 A backreference does not epsilon-transition unless it is empty, so set
4231 to all zeros if FROM != TO. */
4232 mctx->bkref_ents[mctx->nbkref_ents].eps_reachable_subexps_map
4233 = (from == to ? ~0 : 0);
4235 mctx->bkref_ents[mctx->nbkref_ents++].more = 0;
4236 if (mctx->max_mb_elem_len < to - from)
4237 mctx->max_mb_elem_len = to - from;
4241 /* Search for the first entry which has the same str_idx, or -1 if none is
4242 found. Note that MCTX->BKREF_ENTS is already sorted by MCTX->STR_IDX. */
4245 search_cur_bkref_entry (mctx, str_idx)
4246 re_match_context_t *mctx;
4249 int left, right, mid, last;
4250 last = right = mctx->nbkref_ents;
4251 for (left = 0; left < right;)
4253 mid = (left + right) / 2;
4254 if (mctx->bkref_ents[mid].str_idx < str_idx)
4259 if (left < last && mctx->bkref_ents[left].str_idx == str_idx)
4265 /* Register the node NODE, whose type is OP_OPEN_SUBEXP, and which matches
4268 static reg_errcode_t
4269 match_ctx_add_subtop (mctx, node, str_idx)
4270 re_match_context_t *mctx;
4274 assert (mctx->sub_tops != NULL);
4275 assert (mctx->asub_tops > 0);
4277 if (BE (mctx->nsub_tops == mctx->asub_tops, 0))
4279 int new_asub_tops = mctx->asub_tops * 2;
4280 re_sub_match_top_t **new_array = re_realloc (mctx->sub_tops,
4281 re_sub_match_top_t *,
4283 if (BE (new_array == NULL, 0))
4285 mctx->sub_tops = new_array;
4286 mctx->asub_tops = new_asub_tops;
4288 mctx->sub_tops[mctx->nsub_tops] = calloc (1, sizeof (re_sub_match_top_t));
4289 if (BE (mctx->sub_tops[mctx->nsub_tops] == NULL, 0))
4291 mctx->sub_tops[mctx->nsub_tops]->node = node;
4292 mctx->sub_tops[mctx->nsub_tops++]->str_idx = str_idx;
4296 /* Register the node NODE, whose type is OP_CLOSE_SUBEXP, and which matches
4297 at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP. */
4299 static re_sub_match_last_t *
4300 match_ctx_add_sublast (subtop, node, str_idx)
4301 re_sub_match_top_t *subtop;
4304 re_sub_match_last_t *new_entry;
4305 if (BE (subtop->nlasts == subtop->alasts, 0))
4307 int new_alasts = 2 * subtop->alasts + 1;
4308 re_sub_match_last_t **new_array = re_realloc (subtop->lasts,
4309 re_sub_match_last_t *,
4311 if (BE (new_array == NULL, 0))
4313 subtop->lasts = new_array;
4314 subtop->alasts = new_alasts;
4316 new_entry = calloc (1, sizeof (re_sub_match_last_t));
4317 if (BE (new_entry != NULL, 1))
4319 subtop->lasts[subtop->nlasts] = new_entry;
4320 new_entry->node = node;
4321 new_entry->str_idx = str_idx;
4328 sift_ctx_init (sctx, sifted_sts, limited_sts, last_node, last_str_idx)
4329 re_sift_context_t *sctx;
4330 re_dfastate_t **sifted_sts, **limited_sts;
4331 int last_node, last_str_idx;
4333 sctx->sifted_states = sifted_sts;
4334 sctx->limited_states = limited_sts;
4335 sctx->last_node = last_node;
4336 sctx->last_str_idx = last_str_idx;
4337 re_node_set_init_empty (&sctx->limits);