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>.
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, see
18    <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef _REGEX_INTERNAL_H
21 #define _REGEX_INTERNAL_H 1
22 
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET
29 # include <langinfo.h>
30 #endif
31 #if defined HAVE_LOCALE_H
32 # include <locale.h>
33 #endif
34 #if defined HAVE_WCHAR_H
35 # include <wchar.h>
36 #endif
37 #if defined HAVE_WCTYPE_H
38 # include <wctype.h>
39 #endif
40 #if defined HAVE_STDBOOL_H
41 # include <stdbool.h>
42 #endif
43 #if defined HAVE_STDINT_H
44 # include <stdint.h>
45 #endif
46 
47 #ifdef __UCLIBC_HAS_THREADS__
48 #include <bits/libc-lock.h>
49 #else
50 #define __libc_lock_define(CLASS, NAME)
51 #define __libc_lock_init(NAME)   do { } while (0)
52 #define __libc_lock_lock(NAME)   do { } while (0)
53 #define __libc_lock_unlock(NAME) do { } while (0)
54 #endif
55 
56 #undef gettext
57 #undef gettext_noop
58 #define gettext(msgid)       (msgid)
59 #define gettext_noop(String) String
60 
61 #if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL)
62 # define RE_ENABLE_I18N
63 #endif
64 
65 #if __GNUC__ >= 3
66 /* uclibc: lean towards smaller size a bit:
67  * OFF: # define BE(expr, val) __builtin_expect (expr, val) */
68 # define BE(expr, val) (expr)
69 #else
70 # define BE(expr, val) (expr)
71 # define inline
72 #endif
73 
74 /* Number of single byte character.  */
75 #define SBC_MAX 256
76 
77 #define COLL_ELEM_LEN_MAX 8
78 
79 /* The character which represents newline.  */
80 #define NEWLINE_CHAR '\n'
81 #define WIDE_NEWLINE_CHAR L'\n'
82 
83 #ifdef __GNUC__
84 # define __attribute(arg) __attribute__ (arg)
85 #else
86 # define __attribute(arg)
87 #endif
88 
89 /* An integer used to represent a set of bits.  It must be unsigned,
90    and must be at least as wide as unsigned int.  */
91 typedef unsigned long int bitset_word_t;
92 /* All bits set in a bitset_word_t.  */
93 #define BITSET_WORD_MAX ULONG_MAX
94 /* Number of bits in a bitset_word_t.  */
95 #define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
96 /* Number of bitset_word_t in a bit_set.  */
97 #define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
98 typedef bitset_word_t bitset_t[BITSET_WORDS];
99 typedef bitset_word_t *re_bitset_ptr_t;
100 typedef const bitset_word_t *re_const_bitset_ptr_t;
101 
102 #define bitset_set(set,i) \
103   (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
104 #define bitset_clear(set,i) \
105   (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
106 #define bitset_contain(set,i) \
107   (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
108 #define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
109 #define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
110 #define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
111 
112 #define PREV_WORD_CONSTRAINT 0x0001
113 #define PREV_NOTWORD_CONSTRAINT 0x0002
114 #define NEXT_WORD_CONSTRAINT 0x0004
115 #define NEXT_NOTWORD_CONSTRAINT 0x0008
116 #define PREV_NEWLINE_CONSTRAINT 0x0010
117 #define NEXT_NEWLINE_CONSTRAINT 0x0020
118 #define PREV_BEGBUF_CONSTRAINT 0x0040
119 #define NEXT_ENDBUF_CONSTRAINT 0x0080
120 #define WORD_DELIM_CONSTRAINT 0x0100
121 #define NOT_WORD_DELIM_CONSTRAINT 0x0200
122 
123 typedef enum
124 {
125   INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
126   WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
127   WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
128   INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
129   LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
130   LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
131   BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
132   BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
133   WORD_DELIM = WORD_DELIM_CONSTRAINT,
134   NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
135 } re_context_type;
136 
137 typedef struct
138 {
139   int alloc;
140   int nelem;
141   int *elems;
142 } re_node_set;
143 
144 typedef enum
145 {
146   NON_TYPE = 0,
147 
148   /* Node type, These are used by token, node, tree.  */
149   CHARACTER = 1,
150   END_OF_RE = 2,
151   SIMPLE_BRACKET = 3,
152   OP_BACK_REF = 4,
153   OP_PERIOD = 5,
154 #ifdef RE_ENABLE_I18N
155   COMPLEX_BRACKET = 6,
156   OP_UTF8_PERIOD = 7,
157 #endif /* RE_ENABLE_I18N */
158 
159   /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
160      when the debugger shows values of this enum type.  */
161 #define EPSILON_BIT 8
162   OP_OPEN_SUBEXP = EPSILON_BIT | 0,
163   OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
164   OP_ALT = EPSILON_BIT | 2,
165   OP_DUP_ASTERISK = EPSILON_BIT | 3,
166   ANCHOR = EPSILON_BIT | 4,
167 
168   /* Tree type, these are used only by tree. */
169   CONCAT = 16,
170   SUBEXP = 17,
171 
172   /* Token type, these are used only by token.  */
173   OP_DUP_PLUS = 18,
174   OP_DUP_QUESTION,
175   OP_OPEN_BRACKET,
176   OP_CLOSE_BRACKET,
177   OP_CHARSET_RANGE,
178   OP_OPEN_DUP_NUM,
179   OP_CLOSE_DUP_NUM,
180   OP_NON_MATCH_LIST,
181   OP_OPEN_COLL_ELEM,
182   OP_CLOSE_COLL_ELEM,
183   OP_OPEN_EQUIV_CLASS,
184   OP_CLOSE_EQUIV_CLASS,
185   OP_OPEN_CHAR_CLASS,
186   OP_CLOSE_CHAR_CLASS,
187   OP_WORD,
188   OP_NOTWORD,
189   OP_SPACE,
190   OP_NOTSPACE,
191   BACK_SLASH
192 
193 } re_token_type_t;
194 
195 #ifdef RE_ENABLE_I18N
196 typedef struct
197 {
198   /* Multibyte characters.  */
199   wchar_t *mbchars;
200 
201   /* Collating symbols.  */
202 # if 0
203   int32_t *coll_syms;
204 # endif
205 
206   /* Equivalence classes. */
207 # if 0
208   int32_t *equiv_classes;
209 # endif
210 
211   /* Range expressions. */
212 # if 0
213   uint32_t *range_starts;
214   uint32_t *range_ends;
215 # else
216   wchar_t *range_starts;
217   wchar_t *range_ends;
218 # endif
219 
220   /* Character classes. */
221   wctype_t *char_classes;
222 
223   /* If this character set is the non-matching list.  */
224   unsigned int non_match : 1;
225 
226   /* # of multibyte characters.  */
227   int nmbchars;
228 
229   /* # of collating symbols.  */
230   int ncoll_syms;
231 
232   /* # of equivalence classes. */
233   int nequiv_classes;
234 
235   /* # of range expressions. */
236   int nranges;
237 
238   /* # of character classes. */
239   int nchar_classes;
240 } re_charset_t;
241 #endif /* RE_ENABLE_I18N */
242 
243 typedef struct
244 {
245   union
246   {
247     unsigned char c;		/* for CHARACTER */
248     re_bitset_ptr_t sbcset;	/* for SIMPLE_BRACKET */
249 #ifdef RE_ENABLE_I18N
250     re_charset_t *mbcset;	/* for COMPLEX_BRACKET */
251 #endif /* RE_ENABLE_I18N */
252     int idx;			/* for BACK_REF */
253     re_context_type ctx_type;	/* for ANCHOR */
254   } opr;
255 #if __GNUC__ >= 2
256   re_token_type_t type : 8;
257 #else
258   re_token_type_t type;
259 #endif
260   unsigned int constraint : 10;	/* context constraint */
261   unsigned int duplicated : 1;
262   unsigned int opt_subexp : 1;
263 #ifdef RE_ENABLE_I18N
264   unsigned int accept_mb : 1;
265   /* These 2 bits can be moved into the union if needed (e.g. if running out
266      of bits; move opr.c to opr.c.c and move the flags to opr.c.flags).  */
267   unsigned int mb_partial : 1;
268 #endif
269   unsigned int word_char : 1;
270 } re_token_t;
271 
272 #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
273 
274 struct re_string_t
275 {
276   /* Indicate the raw buffer which is the original string passed as an
277      argument of regexec(), re_search(), etc..  */
278   const unsigned char *raw_mbs;
279   /* Store the multibyte string.  In case of "case insensitive mode" like
280      REG_ICASE, upper cases of the string are stored, otherwise MBS points
281      the same address that RAW_MBS points.  */
282   unsigned char *mbs;
283 #ifdef RE_ENABLE_I18N
284   /* Store the wide character string which is corresponding to MBS.  */
285   wint_t *wcs;
286   int *offsets;
287   mbstate_t cur_state;
288 #endif
289   /* Index in RAW_MBS.  Each character mbs[i] corresponds to
290      raw_mbs[raw_mbs_idx + i].  */
291   int raw_mbs_idx;
292   /* The length of the valid characters in the buffers.  */
293   int valid_len;
294   /* The corresponding number of bytes in raw_mbs array.  */
295   int valid_raw_len;
296   /* The length of the buffers MBS and WCS.  */
297   int bufs_len;
298   /* The index in MBS, which is updated by re_string_fetch_byte.  */
299   int cur_idx;
300   /* length of RAW_MBS array.  */
301   int raw_len;
302   /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN.  */
303   int len;
304   /* End of the buffer may be shorter than its length in the cases such
305      as re_match_2, re_search_2.  Then, we use STOP for end of the buffer
306      instead of LEN.  */
307   int raw_stop;
308   /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS.  */
309   int stop;
310 
311   /* The context of mbs[0].  We store the context independently, since
312      the context of mbs[0] may be different from raw_mbs[0], which is
313      the beginning of the input string.  */
314   unsigned int tip_context;
315   /* The translation passed as a part of an argument of re_compile_pattern.  */
316   __RE_TRANSLATE_TYPE trans;
317   /* Copy of re_dfa_t's word_char.  */
318   re_const_bitset_ptr_t word_char;
319   /* 1 if REG_ICASE.  */
320   unsigned char icase;
321   unsigned char is_utf8;
322   unsigned char map_notascii;
323   unsigned char mbs_allocated;
324   unsigned char offsets_needed;
325   unsigned char newline_anchor;
326   unsigned char word_ops_used;
327   int mb_cur_max;
328 };
329 typedef struct re_string_t re_string_t;
330 
331 struct re_dfa_t;
332 typedef struct re_dfa_t re_dfa_t;
333 
334 static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
335 						int new_buf_len)
336      internal_function;
337 #ifdef RE_ENABLE_I18N
338 static void build_wcs_buffer (re_string_t *pstr) internal_function;
339 static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr) internal_function;
340 #endif /* RE_ENABLE_I18N */
341 static void build_upper_buffer (re_string_t *pstr) internal_function;
342 static void re_string_translate_buffer (re_string_t *pstr) internal_function;
343 static unsigned int re_string_context_at (const re_string_t *input, int idx,
344 					  int eflags)
345      internal_function __attribute ((pure));
346 #define re_string_peek_byte(pstr, offset) \
347   ((pstr)->mbs[(pstr)->cur_idx + offset])
348 #define re_string_fetch_byte(pstr) \
349   ((pstr)->mbs[(pstr)->cur_idx++])
350 #define re_string_first_byte(pstr, idx) \
351   ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
352 #define re_string_is_single_byte_char(pstr, idx) \
353   ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
354 				|| (pstr)->wcs[(idx) + 1] != WEOF))
355 #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
356 #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
357 #define re_string_get_buffer(pstr) ((pstr)->mbs)
358 #define re_string_length(pstr) ((pstr)->len)
359 #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
360 #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
361 #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
362 
363 #include <alloca.h>
364 
365 #if 1
366 # ifdef HAVE_ALLOCA
367 /* The OS usually guarantees only one guard page at the bottom of the stack,
368    and a page size can be as small as 4096 bytes.  So we cannot safely
369    allocate anything larger than 4096 bytes.  Also care for the possibility
370    of a few compiler-allocated temporary stack slots.  */
371 #  define __libc_use_alloca(n) ((n) < 4032)
372 # else
373 /* alloca is implemented with malloc, so just use malloc.  */
374 #  define __libc_use_alloca(n) 0
375 # endif
376 #endif
377 
378 #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
379 #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
380 #define re_free(p) free (p)
381 
382 struct bin_tree_t
383 {
384   struct bin_tree_t *parent;
385   struct bin_tree_t *left;
386   struct bin_tree_t *right;
387   struct bin_tree_t *first;
388   struct bin_tree_t *next;
389 
390   re_token_t token;
391 
392   /* `node_idx' is the index in dfa->nodes, if `type' == 0.
393      Otherwise `type' indicate the type of this node.  */
394   int node_idx;
395 };
396 typedef struct bin_tree_t bin_tree_t;
397 
398 #define BIN_TREE_STORAGE_SIZE \
399   ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
400 
401 struct bin_tree_storage_t
402 {
403   struct bin_tree_storage_t *next;
404   bin_tree_t data[BIN_TREE_STORAGE_SIZE];
405 };
406 typedef struct bin_tree_storage_t bin_tree_storage_t;
407 
408 #define CONTEXT_WORD 1
409 #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
410 #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
411 #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
412 
413 #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
414 #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
415 #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
416 #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
417 #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
418 
419 #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
420 #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
421 #define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
422 #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
423 
424 #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
425  ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
426   || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
427   || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
428   || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
429 
430 #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
431  ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
432   || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
433   || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
434   || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
435 
436 struct re_dfastate_t
437 {
438   unsigned int hash;
439   re_node_set nodes;
440   re_node_set non_eps_nodes;
441   re_node_set inveclosure;
442   re_node_set *entrance_nodes;
443   struct re_dfastate_t **trtable, **word_trtable;
444   unsigned int context : 4;
445   unsigned int halt : 1;
446   /* If this state can accept `multi byte'.
447      Note that we refer to multibyte characters, and multi character
448      collating elements as `multi byte'.  */
449   unsigned int accept_mb : 1;
450   /* If this state has backreference node(s).  */
451   unsigned int has_backref : 1;
452   unsigned int has_constraint : 1;
453 };
454 typedef struct re_dfastate_t re_dfastate_t;
455 
456 struct re_state_table_entry
457 {
458   int num;
459   int alloc;
460   re_dfastate_t **array;
461 };
462 
463 /* Array type used in re_sub_match_last_t and re_sub_match_top_t.  */
464 
465 typedef struct
466 {
467   int next_idx;
468   int alloc;
469   re_dfastate_t **array;
470 } state_array_t;
471 
472 /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP.  */
473 
474 typedef struct
475 {
476   int node;
477   int str_idx; /* The position NODE match at.  */
478   state_array_t path;
479 } re_sub_match_last_t;
480 
481 /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
482    And information about the node, whose type is OP_CLOSE_SUBEXP,
483    corresponding to NODE is stored in LASTS.  */
484 
485 typedef struct
486 {
487   int str_idx;
488   int node;
489   state_array_t *path;
490   int alasts; /* Allocation size of LASTS.  */
491   int nlasts; /* The number of LASTS.  */
492   re_sub_match_last_t **lasts;
493 } re_sub_match_top_t;
494 
495 struct re_backref_cache_entry
496 {
497   int node;
498   int str_idx;
499   int subexp_from;
500   int subexp_to;
501   char more;
502   char unused;
503   unsigned short int eps_reachable_subexps_map;
504 };
505 
506 typedef struct
507 {
508   /* The string object corresponding to the input string.  */
509   re_string_t input;
510   const re_dfa_t *dfa;
511   /* EFLAGS of the argument of regexec.  */
512   int eflags;
513   /* Where the matching ends.  */
514   int match_last;
515   int last_node;
516   /* The state log used by the matcher.  */
517   re_dfastate_t **state_log;
518   int state_log_top;
519   /* Back reference cache.  */
520   int nbkref_ents;
521   int abkref_ents;
522   struct re_backref_cache_entry *bkref_ents;
523   int max_mb_elem_len;
524   int nsub_tops;
525   int asub_tops;
526   re_sub_match_top_t **sub_tops;
527 } re_match_context_t;
528 
529 typedef struct
530 {
531   re_dfastate_t **sifted_states;
532   re_dfastate_t **limited_states;
533   int last_node;
534   int last_str_idx;
535   re_node_set limits;
536 } re_sift_context_t;
537 
538 struct re_fail_stack_ent_t
539 {
540   int idx;
541   int node;
542   regmatch_t *regs;
543   re_node_set eps_via_nodes;
544 };
545 
546 struct re_fail_stack_t
547 {
548   int num;
549   int alloc;
550   struct re_fail_stack_ent_t *stack;
551 };
552 
553 struct re_dfa_t
554 {
555   re_token_t *nodes;
556   size_t nodes_alloc;
557   size_t nodes_len;
558   int *nexts;
559   int *org_indices;
560   re_node_set *edests;
561   re_node_set *eclosures;
562   re_node_set *inveclosures;
563   struct re_state_table_entry *state_table;
564   re_dfastate_t *init_state;
565   re_dfastate_t *init_state_word;
566   re_dfastate_t *init_state_nl;
567   re_dfastate_t *init_state_begbuf;
568   bin_tree_t *str_tree;
569   bin_tree_storage_t *str_tree_storage;
570   re_bitset_ptr_t sb_char;
571   int str_tree_storage_idx;
572 
573   /* number of subexpressions `re_nsub' is in regex_t.  */
574   unsigned int state_hash_mask;
575   int init_node;
576   int nbackref; /* The number of backreference in this dfa.  */
577 
578   /* Bitmap expressing which backreference is used.  */
579   bitset_word_t used_bkref_map;
580   bitset_word_t completed_bkref_map;
581 
582   unsigned int has_plural_match : 1;
583   /* If this dfa has "multibyte node", which is a backreference or
584      a node which can accept multibyte character or multi character
585      collating element.  */
586   unsigned int has_mb_node : 1;
587   unsigned int is_utf8 : 1;
588   unsigned int map_notascii : 1;
589   unsigned int word_ops_used : 1;
590   int mb_cur_max;
591   bitset_t word_char;
592   reg_syntax_t syntax;
593   int *subexp_map;
594 #ifdef DEBUG
595   char* re_str;
596 #endif
597   __libc_lock_define (, lock)
598 };
599 
600 #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
601 #define re_node_set_remove(set,id) \
602   (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
603 #define re_node_set_empty(p) ((p)->nelem = 0)
604 #define re_node_set_free(set) re_free ((set)->elems)
605 
606 
607 typedef enum
608 {
609   SB_CHAR,
610   MB_CHAR,
611   EQUIV_CLASS,
612   COLL_SYM,
613   CHAR_CLASS
614 } bracket_elem_type;
615 
616 typedef struct
617 {
618   bracket_elem_type type;
619   union
620   {
621     unsigned char ch;
622     unsigned char *name;
623 #ifdef __UCLIBC_HAS_WCHAR__
624     wchar_t wch;
625 #endif
626   } opr;
627 } bracket_elem_t;
628 
629 
630 /* Inline functions for bitset operation.  */
631 static __inline__ void
bitset_not(bitset_t set)632 bitset_not (bitset_t set)
633 {
634   int bitset_i;
635   for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
636     set[bitset_i] = ~set[bitset_i];
637 }
638 
639 static __inline__ void
bitset_merge(bitset_t dest,const bitset_t src)640 bitset_merge (bitset_t dest, const bitset_t src)
641 {
642   int bitset_i;
643   for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
644     dest[bitset_i] |= src[bitset_i];
645 }
646 
647 static __inline__ void
bitset_mask(bitset_t dest,const bitset_t src)648 bitset_mask (bitset_t dest, const bitset_t src)
649 {
650   int bitset_i;
651   for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
652     dest[bitset_i] &= src[bitset_i];
653 }
654 
655 #ifdef RE_ENABLE_I18N
656 /* Inline functions for re_string.  */
657 static __inline__ int
internal_function(pure)658 internal_function __attribute ((pure))
659 re_string_char_size_at (const re_string_t *pstr, int idx)
660 {
661   int byte_idx;
662   if (pstr->mb_cur_max == 1)
663     return 1;
664   for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
665     if (pstr->wcs[idx + byte_idx] != WEOF)
666       break;
667   return byte_idx;
668 }
669 
670 static __inline__ wint_t
internal_function(pure)671 internal_function __attribute ((pure))
672 re_string_wchar_at (const re_string_t *pstr, int idx)
673 {
674   if (pstr->mb_cur_max == 1)
675     return (wint_t) pstr->mbs[idx];
676   return (wint_t) pstr->wcs[idx];
677 }
678 
679 static int
internal_function(pure)680 internal_function __attribute ((pure))
681 re_string_elem_size_at (const re_string_t *pstr, int idx)
682 {
683 # if 0
684   const unsigned char *p, *extra;
685   const int32_t *table, *indirect;
686   int32_t tmp;
687 #  include <locale/weight.h>
688   uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
689 
690   if (nrules != 0)
691     {
692       table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
693       extra = (const unsigned char *)
694 	_NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
695       indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
696 						_NL_COLLATE_INDIRECTMB);
697       p = pstr->mbs + idx;
698       tmp = findidx (&p);
699       return p - pstr->mbs - idx;
700     }
701 # endif
702   return 1;
703 }
704 #endif /* RE_ENABLE_I18N */
705 
706 #endif /*  _REGEX_INTERNAL_H */
707