1/* Extended regular expression matching and search library.
2   Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4   Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
5
6   The GNU C Library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10
11   The GNU C Library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with the GNU C Library; if not, write to the Free
18   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   02111-1307 USA.  */
20
21#ifndef _REGEX_INTERNAL_H
22#define _REGEX_INTERNAL_H 1
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include <assert.h>
29#include <ctype.h>
30#if 0
31/* Don't include this here. On some systems it sets RE_DUP_MAX to a
32 * lower value than GNU regex allows.  Instead, include it in
33 * regex.c, before include of <regex.h>, which correctly
34 * #undefs RE_DUP_MAX and sets it to the right value.
35 */
36#include <limits.h>
37#endif
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41
42#if defined HAVE_LOCALE_H || defined _LIBC
43# include <locale.h>
44#endif
45#if defined HAVE_WCHAR_H || defined _LIBC
46# include <wchar.h>
47#endif /* HAVE_WCHAR_H || _LIBC */
48#if defined HAVE_WCTYPE_H || defined _LIBC
49# include <wctype.h>
50#endif /* HAVE_WCTYPE_H || _LIBC */
51
52/* In case that the system doesn't have isblank().  */
53#if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank
54# define isblank(ch) ((ch) == ' ' || (ch) == '\t')
55#endif
56
57#ifdef _LIBC
58# ifndef _RE_DEFINE_LOCALE_FUNCTIONS
59#  define _RE_DEFINE_LOCALE_FUNCTIONS 1
60#   include <locale/localeinfo.h>
61#   include <locale/elem-hash.h>
62#   include <locale/coll-lookup.h>
63# endif
64#endif
65
66/* This is for other GNU distributions with internationalized messages.  */
67#if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
68# include <libintl.h>
69# ifdef _LIBC
70#  undef gettext
71#  define gettext(msgid) \
72  INTUSE(__dcgettext) (INTUSE(_libc_intl_domainname), msgid, LC_MESSAGES)
73# endif
74#else
75# define gettext(msgid) (msgid)
76#endif
77
78#ifndef gettext_noop
79/* This define is so xgettext can find the internationalizable
80   strings.  */
81# define gettext_noop(String) String
82#endif
83
84#if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL) || _LIBC
85# define RE_ENABLE_I18N
86#endif
87
88#if __GNUC__ >= 3
89# define BE(expr, val) __builtin_expect (expr, val)
90#else
91# define BE(expr, val) (expr)
92# define inline
93#endif
94
95/* Number of bits in a byte.  */
96#define BYTE_BITS 8
97/* Number of single byte character.  */
98#define SBC_MAX 256
99
100#define COLL_ELEM_LEN_MAX 8
101
102/* The character which represents newline.  */
103#define NEWLINE_CHAR '\n'
104#define WIDE_NEWLINE_CHAR L'\n'
105
106/* Rename to standard API for using out of glibc.  */
107#ifndef _LIBC
108# define __wctype wctype
109# define __iswctype iswctype
110# define __btowc btowc
111# define __wcrtomb wcrtomb
112# define attribute_hidden
113# define __thread
114#endif /* not _LIBC */
115
116#if _LIBC || __GNUC__ >= 3
117# define BE(expr, val) __builtin_expect (expr, val)
118#else
119# define BE(expr, val) (expr)
120# define inline
121#endif
122
123#ifdef RE_ENABLE_I18N
124__thread int re_mb_cur_max = 1;
125#endif
126
127extern const char __re_error_msgid[] attribute_hidden;
128extern const size_t __re_error_msgid_idx[] attribute_hidden;
129
130/* Number of bits in an unsinged int.  */
131#define UINT_BITS (sizeof (unsigned int) * BYTE_BITS)
132/* Number of unsigned int in an bit_set.  */
133#define BITSET_UINTS ((SBC_MAX + UINT_BITS - 1) / UINT_BITS)
134typedef unsigned int bitset[BITSET_UINTS];
135typedef unsigned int *re_bitset_ptr_t;
136
137#define bitset_set(set,i) (set[i / UINT_BITS] |= 1UL << i % UINT_BITS)
138#define bitset_clear(set,i) (set[i / UINT_BITS] &= ~(1UL << i % UINT_BITS))
139#define bitset_contain(set,i) (set[i / UINT_BITS] & (1UL << i % UINT_BITS))
140#define bitset_empty(set) memset (set, 0, sizeof (unsigned int) * BITSET_UINTS)
141#define bitset_set_all(set) \
142  memset (set, 255, sizeof (unsigned int) * BITSET_UINTS)
143#define bitset_copy(dest,src) \
144  memcpy (dest, src, sizeof (unsigned int) * BITSET_UINTS)
145static inline void bitset_not _RE_ARGS((bitset set));
146static inline void bitset_merge _RE_ARGS((bitset dest, const bitset src));
147static inline void bitset_not_merge _RE_ARGS((bitset dest, const bitset src));
148
149#define PREV_WORD_CONSTRAINT 0x0001
150#define PREV_NOTWORD_CONSTRAINT 0x0002
151#define NEXT_WORD_CONSTRAINT 0x0004
152#define NEXT_NOTWORD_CONSTRAINT 0x0008
153#define PREV_NEWLINE_CONSTRAINT 0x0010
154#define NEXT_NEWLINE_CONSTRAINT 0x0020
155#define PREV_BEGBUF_CONSTRAINT 0x0040
156#define NEXT_ENDBUF_CONSTRAINT 0x0080
157#define DUMMY_CONSTRAINT 0x0100
158
159typedef enum
160{
161  INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
162  WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
163  WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
164  LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
165  LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
166  BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
167  BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
168  WORD_DELIM = DUMMY_CONSTRAINT
169} re_context_type;
170
171typedef struct
172{
173  int alloc;
174  int nelem;
175  int *elems;
176} re_node_set;
177
178typedef enum
179{
180  NON_TYPE = 0,
181
182  /* Token type, these are used only by token.  */
183  OP_OPEN_BRACKET,
184  OP_CLOSE_BRACKET,
185  OP_CHARSET_RANGE,
186  OP_OPEN_DUP_NUM,
187  OP_CLOSE_DUP_NUM,
188  OP_NON_MATCH_LIST,
189  OP_OPEN_COLL_ELEM,
190  OP_CLOSE_COLL_ELEM,
191  OP_OPEN_EQUIV_CLASS,
192  OP_CLOSE_EQUIV_CLASS,
193  OP_OPEN_CHAR_CLASS,
194  OP_CLOSE_CHAR_CLASS,
195  OP_WORD,
196  OP_NOTWORD,
197  BACK_SLASH,
198
199  /* Tree type, these are used only by tree. */
200  CONCAT,
201  ALT,
202  SUBEXP,
203  SIMPLE_BRACKET,
204#ifdef RE_ENABLE_I18N
205  COMPLEX_BRACKET,
206#endif /* RE_ENABLE_I18N */
207
208  /* Node type, These are used by token, node, tree.  */
209  OP_OPEN_SUBEXP,
210  OP_CLOSE_SUBEXP,
211  OP_PERIOD,
212  CHARACTER,
213  END_OF_RE,
214  OP_ALT,
215  OP_DUP_ASTERISK,
216  OP_DUP_PLUS,
217  OP_DUP_QUESTION,
218  OP_BACK_REF,
219  ANCHOR,
220
221  /* Dummy marker.  */
222  END_OF_RE_TOKEN_T
223} re_token_type_t;
224
225#ifdef RE_ENABLE_I18N
226typedef struct
227{
228  /* Multibyte characters.  */
229  wchar_t *mbchars;
230
231  /* Collating symbols.  */
232# ifdef _LIBC
233  int32_t *coll_syms;
234# endif
235
236  /* Equivalence classes. */
237# ifdef _LIBC
238  int32_t *equiv_classes;
239# endif
240
241  /* Range expressions. */
242# ifdef _LIBC
243  uint32_t *range_starts;
244  uint32_t *range_ends;
245# else /* not _LIBC */
246  wchar_t *range_starts;
247  wchar_t *range_ends;
248# endif /* not _LIBC */
249
250  /* Character classes. */
251  wctype_t *char_classes;
252
253  /* If this character set is the non-matching list.  */
254  unsigned int non_match : 1;
255
256  /* # of multibyte characters.  */
257  int nmbchars;
258
259  /* # of collating symbols.  */
260  int ncoll_syms;
261
262  /* # of equivalence classes. */
263  int nequiv_classes;
264
265  /* # of range expressions. */
266  int nranges;
267
268  /* # of character classes. */
269  int nchar_classes;
270} re_charset_t;
271#endif /* RE_ENABLE_I18N */
272
273typedef struct
274{
275  union
276  {
277    unsigned char c;		/* for CHARACTER */
278    re_bitset_ptr_t sbcset;	/* for SIMPLE_BRACKET */
279#ifdef RE_ENABLE_I18N
280    re_charset_t *mbcset;	/* for COMPLEX_BRACKET */
281#endif /* RE_ENABLE_I18N */
282    int idx;			/* for BACK_REF */
283    re_context_type ctx_type;	/* for ANCHOR */
284  } opr;
285#if __GNUC__ >= 2
286  re_token_type_t type : 8;
287#else
288  re_token_type_t type;
289#endif
290  unsigned int constraint : 10;	/* context constraint */
291  unsigned int duplicated : 1;
292#ifdef RE_ENABLE_I18N
293  unsigned int mb_partial : 1;
294#endif
295} re_token_t;
296
297#define IS_EPSILON_NODE(type) \
298  ((type) == OP_ALT || (type) == OP_DUP_ASTERISK || (type) == OP_DUP_PLUS \
299   || (type) == OP_DUP_QUESTION || (type) == ANCHOR \
300   || (type) == OP_OPEN_SUBEXP || (type) == OP_CLOSE_SUBEXP)
301
302#define ACCEPT_MB_NODE(type) \
303  ((type) == COMPLEX_BRACKET || (type) == OP_PERIOD)
304
305struct re_string_t
306{
307  /* Indicate the raw buffer which is the original string passed as an
308     argument of regexec(), re_search(), etc..  */
309  const unsigned char *raw_mbs;
310  /* Store the multibyte string.  In case of "case insensitive mode" like
311     REG_ICASE, upper cases of the string are stored, otherwise MBS points
312     the same address that RAW_MBS points.  */
313  unsigned char *mbs;
314  /* Store the case sensitive multibyte string.  In case of
315     "case insensitive mode", the original string are stored,
316     otherwise MBS_CASE points the same address that MBS points.  */
317  unsigned char *mbs_case;
318#ifdef RE_ENABLE_I18N
319  /* Store the wide character string which is corresponding to MBS.  */
320  wint_t *wcs;
321  mbstate_t cur_state;
322#endif
323  /* Index in RAW_MBS.  Each character mbs[i] corresponds to
324     raw_mbs[raw_mbs_idx + i].  */
325  int raw_mbs_idx;
326  /* The length of the valid characters in the buffers.  */
327  int valid_len;
328  /* The length of the buffers MBS, MBS_CASE, and WCS.  */
329  int bufs_len;
330  /* The index in MBS, which is updated by re_string_fetch_byte.  */
331  int cur_idx;
332  /* This is length_of_RAW_MBS - RAW_MBS_IDX.  */
333  int len;
334  /* End of the buffer may be shorter than its length in the cases such
335     as re_match_2, re_search_2.  Then, we use STOP for end of the buffer
336     instead of LEN.  */
337  int stop;
338
339  /* The context of mbs[0].  We store the context independently, since
340     the context of mbs[0] may be different from raw_mbs[0], which is
341     the beginning of the input string.  */
342  unsigned int tip_context;
343  /* The translation passed as a part of an argument of re_compile_pattern.  */
344  RE_TRANSLATE_TYPE trans;
345  /* 1 if REG_ICASE.  */
346  unsigned int icase : 1;
347};
348typedef struct re_string_t re_string_t;
349/* In case of REG_ICASE, we allocate the buffer dynamically for mbs.  */
350#define MBS_ALLOCATED(pstr) (pstr->icase)
351/* In case that we need translation, we allocate the buffer dynamically
352   for mbs_case.  Note that mbs == mbs_case if not REG_ICASE.  */
353#define MBS_CASE_ALLOCATED(pstr) (pstr->trans != NULL)
354
355static reg_errcode_t re_string_allocate _RE_ARGS((re_string_t *pstr, const char *str,
356					 int len, int init_len,
357                                  RE_TRANSLATE_TYPE trans, int icase));
358static reg_errcode_t re_string_construct _RE_ARGS((re_string_t *pstr, const char *str, int len,
359                                   RE_TRANSLATE_TYPE trans, int icase));
360static reg_errcode_t re_string_reconstruct _RE_ARGS((re_string_t *pstr, int idx,
361                                     int eflags, int newline));
362static reg_errcode_t re_string_realloc_buffers _RE_ARGS((re_string_t *pstr, int new_buf_len));
363#ifdef RE_ENABLE_I18N
364static void build_wcs_buffer _RE_ARGS((re_string_t *pstr));
365static void build_wcs_upper_buffer _RE_ARGS((re_string_t *pstr));
366#endif /* RE_ENABLE_I18N */
367static void build_upper_buffer _RE_ARGS((re_string_t *pstr));
368static void re_string_translate_buffer _RE_ARGS((re_string_t *pstr));
369static void re_string_destruct _RE_ARGS((re_string_t *pstr));
370#ifdef RE_ENABLE_I18N
371static int re_string_elem_size_at _RE_ARGS((const re_string_t *pstr, int idx));
372static inline int re_string_char_size_at _RE_ARGS((const re_string_t *pstr, int idx));
373static inline wint_t re_string_wchar_at _RE_ARGS((const re_string_t *pstr, int idx));
374#endif /* RE_ENABLE_I18N */
375static unsigned int re_string_context_at _RE_ARGS((const re_string_t *input, int idx,
376                                   int eflags, int newline_anchor));
377#define re_string_peek_byte(pstr, offset) \
378  ((pstr)->mbs[(pstr)->cur_idx + offset])
379#define re_string_peek_byte_case(pstr, offset) \
380  ((pstr)->mbs_case[(pstr)->cur_idx + offset])
381#define re_string_fetch_byte(pstr) \
382  ((pstr)->mbs[(pstr)->cur_idx++])
383#define re_string_fetch_byte_case(pstr) \
384  ((pstr)->mbs_case[(pstr)->cur_idx++])
385#define re_string_first_byte(pstr, idx) \
386  ((idx) == (pstr)->len || (pstr)->wcs[idx] != WEOF)
387#define re_string_is_single_byte_char(pstr, idx) \
388  ((pstr)->wcs[idx] != WEOF && ((pstr)->len == (idx) \
389				|| (pstr)->wcs[(idx) + 1] != WEOF))
390#define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
391#define re_string_cur_idx(pstr) ((pstr)->cur_idx)
392#define re_string_get_buffer(pstr) ((pstr)->mbs)
393#define re_string_length(pstr) ((pstr)->len)
394#define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
395#define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
396#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
397
398#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
399#define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
400#define re_free(p) free (p)
401
402struct bin_tree_t
403{
404  struct bin_tree_t *parent;
405  struct bin_tree_t *left;
406  struct bin_tree_t *right;
407
408  /* `node_idx' is the index in dfa->nodes, if `type' == 0.
409     Otherwise `type' indicate the type of this node.  */
410  re_token_type_t type;
411  int node_idx;
412
413  int first;
414  int next;
415  re_node_set eclosure;
416};
417typedef struct bin_tree_t bin_tree_t;
418
419
420#define CONTEXT_WORD 1
421#define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
422#define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
423#define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
424
425#define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
426#define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
427#define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
428#define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
429#define IS_ORDINARY_CONTEXT(c) ((c) == 0)
430
431#define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
432#define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
433#define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
434#define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
435
436#define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
437 ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
438  || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
439  || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
440  || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
441
442#define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
443 ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
444  || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
445  || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
446  || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
447
448struct re_dfastate_t
449{
450  unsigned int hash;
451  re_node_set nodes;
452  re_node_set *entrance_nodes;
453  struct re_dfastate_t **trtable;
454  struct re_dfastate_t **trtable_search;
455  /* If this state is a special state.
456     A state is a special state if the state is the halt state, or
457     a anchor.  */
458  unsigned int context : 2;
459  unsigned int halt : 1;
460  /* If this state can accept `multi byte'.
461     Note that we refer to multibyte characters, and multi character
462     collating elements as `multi byte'.  */
463  unsigned int accept_mb : 1;
464  /* If this state has backreference node(s).  */
465  unsigned int has_backref : 1;
466  unsigned int has_constraint : 1;
467};
468typedef struct re_dfastate_t re_dfastate_t;
469
470typedef struct
471{
472  /* start <= node < end  */
473  int start;
474  int end;
475} re_subexp_t;
476
477struct re_state_table_entry
478{
479  int num;
480  int alloc;
481  re_dfastate_t **array;
482};
483
484/* Array type used in re_sub_match_last_t and re_sub_match_top_t.  */
485
486typedef struct
487{
488  int next_idx;
489  int alloc;
490  re_dfastate_t **array;
491} state_array_t;
492
493/* Store information about the node NODE whose type is OP_CLOSE_SUBEXP.  */
494
495typedef struct
496{
497  int node;
498  int str_idx; /* The position NODE match at.  */
499  state_array_t path;
500} re_sub_match_last_t;
501
502/* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
503   And information about the node, whose type is OP_CLOSE_SUBEXP,
504   corresponding to NODE is stored in LASTS.  */
505
506typedef struct
507{
508  int str_idx;
509  int node;
510  int next_last_offset;
511  state_array_t *path;
512  int alasts; /* Allocation size of LASTS.  */
513  int nlasts; /* The number of LASTS.  */
514  re_sub_match_last_t **lasts;
515} re_sub_match_top_t;
516
517struct re_backref_cache_entry
518{
519  int node;
520  int str_idx;
521  int subexp_from;
522  int subexp_to;
523  int flag;
524};
525
526typedef struct
527{
528  /* EFLAGS of the argument of regexec.  */
529  int eflags;
530  /* Where the matching ends.  */
531  int match_last;
532  int last_node;
533  /* The string object corresponding to the input string.  */
534  re_string_t *input;
535  /* The state log used by the matcher.  */
536  re_dfastate_t **state_log;
537  int state_log_top;
538  /* Back reference cache.  */
539  int nbkref_ents;
540  int abkref_ents;
541  struct re_backref_cache_entry *bkref_ents;
542  int max_mb_elem_len;
543  int nsub_tops;
544  int asub_tops;
545  re_sub_match_top_t **sub_tops;
546} re_match_context_t;
547
548typedef struct
549{
550  int cur_bkref;
551  int cls_subexp_idx;
552
553  re_dfastate_t **sifted_states;
554  re_dfastate_t **limited_states;
555
556  re_node_set limits;
557
558  int last_node;
559  int last_str_idx;
560  int check_subexp;
561} re_sift_context_t;
562
563struct re_fail_stack_ent_t
564{
565  int idx;
566  int node;
567  regmatch_t *regs;
568  re_node_set eps_via_nodes;
569};
570
571struct re_fail_stack_t
572{
573  int num;
574  int alloc;
575  struct re_fail_stack_ent_t *stack;
576};
577
578struct re_dfa_t
579{
580  re_bitset_ptr_t word_char;
581
582  /* number of subexpressions `re_nsub' is in regex_t.  */
583  int subexps_alloc;
584  re_subexp_t *subexps;
585
586  re_token_t *nodes;
587  int nodes_alloc;
588  int nodes_len;
589  bin_tree_t *str_tree;
590  int *nexts;
591  int *org_indices;
592  re_node_set *edests;
593  re_node_set *eclosures;
594  re_node_set *inveclosures;
595  struct re_state_table_entry *state_table;
596  unsigned int state_hash_mask;
597  re_dfastate_t *init_state;
598  re_dfastate_t *init_state_word;
599  re_dfastate_t *init_state_nl;
600  re_dfastate_t *init_state_begbuf;
601  int states_alloc;
602  int init_node;
603  int nbackref; /* The number of backreference in this dfa.  */
604  /* Bitmap expressing which backreference is used.  */
605  unsigned int used_bkref_map;
606#ifdef DEBUG
607  char* re_str;
608#endif
609  unsigned int has_plural_match : 1;
610  /* If this dfa has "multibyte node", which is a backreference or
611     a node which can accept multibyte character or multi character
612     collating element.  */
613  unsigned int has_mb_node : 1;
614};
615typedef struct re_dfa_t re_dfa_t;
616
617static reg_errcode_t re_node_set_alloc _RE_ARGS((re_node_set *set, int size));
618static reg_errcode_t re_node_set_init_1 _RE_ARGS((re_node_set *set, int elem));
619static reg_errcode_t re_node_set_init_2 _RE_ARGS((re_node_set *set, int elem1, int elem2));
620#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
621static reg_errcode_t re_node_set_init_copy _RE_ARGS((re_node_set *dest,
622                                            const re_node_set *src));
623static reg_errcode_t re_node_set_add_intersect _RE_ARGS((re_node_set *dest,
624						const re_node_set *src1,
625                                         const re_node_set *src2));
626static reg_errcode_t re_node_set_init_union _RE_ARGS((re_node_set *dest,
627					     const re_node_set *src1,
628                                      const re_node_set *src2));
629static reg_errcode_t re_node_set_merge _RE_ARGS((re_node_set *dest, const re_node_set *src));
630static int re_node_set_insert _RE_ARGS((re_node_set *set, int elem));
631static int re_node_set_compare _RE_ARGS((const re_node_set *set1, const re_node_set *set2));
632static int re_node_set_contains _RE_ARGS((const re_node_set *set, int elem));
633static void re_node_set_remove_at _RE_ARGS((re_node_set *set, int idx));
634#define re_node_set_remove(set,id) \
635  (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
636#define re_node_set_empty(p) ((p)->nelem = 0)
637#define re_node_set_free(set) re_free ((set)->elems)
638static int re_dfa_add_node _RE_ARGS((re_dfa_t *dfa, re_token_t token, int mode));
639static re_dfastate_t *re_acquire_state _RE_ARGS((reg_errcode_t *err, re_dfa_t *dfa,
640                                 const re_node_set *nodes));
641static re_dfastate_t *re_acquire_state_context _RE_ARGS((reg_errcode_t *err, re_dfa_t *dfa,
642						const re_node_set *nodes,
643                                         unsigned int context));
644static void free_state _RE_ARGS((re_dfastate_t *state));
645
646
647typedef enum
648{
649  SB_CHAR,
650  MB_CHAR,
651  EQUIV_CLASS,
652  COLL_SYM,
653  CHAR_CLASS
654} bracket_elem_type;
655
656typedef struct
657{
658  bracket_elem_type type;
659  union
660  {
661    unsigned char ch;
662    unsigned char *name;
663    wchar_t wch;
664  } opr;
665} bracket_elem_t;
666
667
668/* Inline functions for bitset operation.  */
669static inline void
670bitset_not (set)
671     bitset set;
672{
673  int bitset_i;
674  for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
675    set[bitset_i] = ~set[bitset_i];
676}
677
678static inline void
679bitset_merge (dest, src)
680     bitset dest;
681     const bitset src;
682{
683  int bitset_i;
684  for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
685    dest[bitset_i] |= src[bitset_i];
686}
687
688static inline void
689bitset_not_merge (dest, src)
690     bitset dest;
691     const bitset src;
692{
693  int i;
694  for (i = 0; i < BITSET_UINTS; ++i)
695    dest[i] |= ~src[i];
696}
697
698#ifdef RE_ENABLE_I18N
699/* Inline functions for re_string.  */
700static inline int
701re_string_char_size_at (pstr, idx)
702     const re_string_t *pstr;
703     int idx;
704{
705  int byte_idx;
706  if (re_mb_cur_max == 1)
707    return 1;
708  for (byte_idx = 1; idx + byte_idx < pstr->len; ++byte_idx)
709    if (pstr->wcs[idx + byte_idx] != WEOF)
710      break;
711  return byte_idx;
712}
713
714static inline wint_t
715re_string_wchar_at (pstr, idx)
716     const re_string_t *pstr;
717     int idx;
718{
719  if (re_mb_cur_max == 1)
720    return (wint_t) pstr->mbs[idx];
721  return (wint_t) pstr->wcs[idx];
722}
723
724static int
725re_string_elem_size_at (pstr, idx)
726     const re_string_t *pstr;
727     int idx;
728{
729#ifdef _LIBC
730  const unsigned char *p, *extra;
731  const int32_t *table, *indirect;
732  int32_t tmp;
733# include <locale/weight.h>
734  uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
735
736  if (nrules != 0)
737    {
738      table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
739      extra = (const unsigned char *)
740	_NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
741      indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
742						_NL_COLLATE_INDIRECTMB);
743      p = pstr->mbs + idx;
744      tmp = findidx (&p);
745      return p - pstr->mbs - idx;
746    }
747  else
748#endif /* _LIBC */
749    return 1;
750}
751#endif /* RE_ENABLE_I18N */
752
753#endif /*  _REGEX_INTERNAL_H */
754