xref: /netbsd-src/external/gpl2/xcvs/dist/lib/regex_internal.h (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1a7c91847Schristos /* Extended regular expression matching and search library.
2a7c91847Schristos    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3a7c91847Schristos    This file is part of the GNU C Library.
4a7c91847Schristos    Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
5a7c91847Schristos 
6a7c91847Schristos    This program is free software; you can redistribute it and/or modify
7a7c91847Schristos    it under the terms of the GNU General Public License as published by
8a7c91847Schristos    the Free Software Foundation; either version 2, or (at your option)
9a7c91847Schristos    any later version.
10a7c91847Schristos 
11a7c91847Schristos    This program is distributed in the hope that it will be useful,
12a7c91847Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13a7c91847Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14a7c91847Schristos    GNU General Public License for more details.
15a7c91847Schristos 
16a7c91847Schristos    You should have received a copy of the GNU General Public License along
17a7c91847Schristos    with this program; if not, write to the Free Software Foundation,
18a7c91847Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19a7c91847Schristos 
20a7c91847Schristos #ifndef _REGEX_INTERNAL_H
21a7c91847Schristos #define _REGEX_INTERNAL_H 1
22a7c91847Schristos 
23a7c91847Schristos #include <assert.h>
24a7c91847Schristos #include <ctype.h>
25a7c91847Schristos #include <stdbool.h>
26a7c91847Schristos #include <stdio.h>
27a7c91847Schristos #include <stdlib.h>
28a7c91847Schristos #include <string.h>
29a7c91847Schristos 
30a7c91847Schristos #if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
31a7c91847Schristos # include <langinfo.h>
32a7c91847Schristos #endif
33a7c91847Schristos #if defined HAVE_LOCALE_H || defined _LIBC
34a7c91847Schristos # include <locale.h>
35a7c91847Schristos #endif
36a7c91847Schristos #if defined HAVE_WCHAR_H || defined _LIBC
37a7c91847Schristos # include <wchar.h>
38a7c91847Schristos #endif /* HAVE_WCHAR_H || _LIBC */
39a7c91847Schristos #if defined HAVE_WCTYPE_H || defined _LIBC
40a7c91847Schristos # include <wctype.h>
41a7c91847Schristos #endif /* HAVE_WCTYPE_H || _LIBC */
42a7c91847Schristos #if defined _LIBC
43a7c91847Schristos # include <bits/libc-lock.h>
44a7c91847Schristos #else
45a7c91847Schristos # define __libc_lock_define(CLASS,NAME)
46a7c91847Schristos # define __libc_lock_init(NAME) do { } while (0)
47a7c91847Schristos # define __libc_lock_lock(NAME) do { } while (0)
48a7c91847Schristos # define __libc_lock_unlock(NAME) do { } while (0)
49a7c91847Schristos #endif
50a7c91847Schristos 
51a7c91847Schristos /* In case that the system doesn't have isblank().  */
52a7c91847Schristos #if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank
53a7c91847Schristos # define isblank(ch) ((ch) == ' ' || (ch) == '\t')
54a7c91847Schristos #endif
55a7c91847Schristos 
56a7c91847Schristos #ifdef _LIBC
57a7c91847Schristos # ifndef _RE_DEFINE_LOCALE_FUNCTIONS
58a7c91847Schristos #  define _RE_DEFINE_LOCALE_FUNCTIONS 1
59a7c91847Schristos #   include <locale/localeinfo.h>
60a7c91847Schristos #   include <locale/elem-hash.h>
61a7c91847Schristos #   include <locale/coll-lookup.h>
62a7c91847Schristos # endif
63a7c91847Schristos #endif
64a7c91847Schristos 
65a7c91847Schristos /* This is for other GNU distributions with internationalized messages.  */
66a7c91847Schristos #if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
67a7c91847Schristos # include <libintl.h>
68a7c91847Schristos # ifdef _LIBC
69a7c91847Schristos #  undef gettext
70a7c91847Schristos #  define gettext(msgid) \
71a7c91847Schristos   INTUSE(__dcgettext) (_libc_intl_domainname, msgid, LC_MESSAGES)
72a7c91847Schristos # endif
73a7c91847Schristos #else
74a7c91847Schristos # define gettext(msgid) (msgid)
75a7c91847Schristos #endif
76a7c91847Schristos 
77a7c91847Schristos #ifndef gettext_noop
78a7c91847Schristos /* This define is so xgettext can find the internationalizable
79a7c91847Schristos    strings.  */
80a7c91847Schristos # define gettext_noop(String) String
81a7c91847Schristos #endif
82a7c91847Schristos 
83a7c91847Schristos #if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL) || _LIBC
84a7c91847Schristos # define RE_ENABLE_I18N
85a7c91847Schristos #endif
86a7c91847Schristos 
87a7c91847Schristos #if __GNUC__ >= 3
88a7c91847Schristos # define BE(expr, val) __builtin_expect (expr, val)
89a7c91847Schristos #else
90a7c91847Schristos # define BE(expr, val) (expr)
91a7c91847Schristos #endif
92a7c91847Schristos 
93a7c91847Schristos /* Number of single byte character.  */
94a7c91847Schristos #define SBC_MAX 256
95a7c91847Schristos 
96a7c91847Schristos #define COLL_ELEM_LEN_MAX 8
97a7c91847Schristos 
98a7c91847Schristos /* The character which represents newline.  */
99a7c91847Schristos #define NEWLINE_CHAR '\n'
100a7c91847Schristos #define WIDE_NEWLINE_CHAR L'\n'
101a7c91847Schristos 
102a7c91847Schristos /* Rename to standard API for using out of glibc.  */
103a7c91847Schristos #ifndef _LIBC
104a7c91847Schristos # define __wctype wctype
105a7c91847Schristos # define __iswctype iswctype
106a7c91847Schristos # define __btowc btowc
107a7c91847Schristos # ifndef __mempcpy
108a7c91847Schristos #  define __mempcpy mempcpy
109a7c91847Schristos # endif
110a7c91847Schristos # define __wcrtomb wcrtomb
111a7c91847Schristos # define __regfree regfree
112a7c91847Schristos # define attribute_hidden
113a7c91847Schristos #endif /* not _LIBC */
114a7c91847Schristos 
115a7c91847Schristos #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
116a7c91847Schristos # define __attribute(arg) __attribute__ (arg)
117a7c91847Schristos #else
118a7c91847Schristos # define __attribute(arg)
119a7c91847Schristos #endif
120a7c91847Schristos 
121a7c91847Schristos extern const char __re_error_msgid[] attribute_hidden;
122a7c91847Schristos extern const size_t __re_error_msgid_idx[] attribute_hidden;
123a7c91847Schristos 
124a7c91847Schristos typedef __re_idx_t Idx;
125a7c91847Schristos 
126a7c91847Schristos /* Special return value for failure to match.  */
127a7c91847Schristos #define REG_MISSING ((Idx) -1)
128a7c91847Schristos 
129a7c91847Schristos /* Special return value for internal error.  */
130a7c91847Schristos #define REG_ERROR ((Idx) -2)
131a7c91847Schristos 
132a7c91847Schristos /* Test whether N is a valid index, and is not one of the above.  */
133a7c91847Schristos #ifdef _REGEX_LARGE_OFFSETS
134a7c91847Schristos # define REG_VALID_INDEX(n) ((Idx) (n) < REG_ERROR)
135a7c91847Schristos #else
136a7c91847Schristos # define REG_VALID_INDEX(n) (0 <= (n))
137a7c91847Schristos #endif
138a7c91847Schristos 
139a7c91847Schristos /* Test whether N is a valid nonzero index.  */
140a7c91847Schristos #ifdef _REGEX_LARGE_OFFSETS
141a7c91847Schristos # define REG_VALID_NONZERO_INDEX(n) ((Idx) ((n) - 1) < (Idx) (REG_ERROR - 1))
142a7c91847Schristos #else
143a7c91847Schristos # define REG_VALID_NONZERO_INDEX(n) (0 < (n))
144a7c91847Schristos #endif
145a7c91847Schristos 
146a7c91847Schristos /* A hash value, suitable for computing hash tables.  */
147a7c91847Schristos typedef __re_size_t re_hashval_t;
148a7c91847Schristos 
149a7c91847Schristos /* An integer used to represent a set of bits.  It must be unsigned,
150a7c91847Schristos    and must be at least as wide as unsigned int.  */
151a7c91847Schristos typedef unsigned long int bitset_word;
152a7c91847Schristos 
153a7c91847Schristos /* Maximum value of a bitset word.  It must be useful in preprocessor
154a7c91847Schristos    contexts, and must be consistent with bitset_word.  */
155a7c91847Schristos #define BITSET_WORD_MAX ULONG_MAX
156a7c91847Schristos 
157a7c91847Schristos /* Number of bits in a bitset word.  Avoid greater-than-32-bit
158a7c91847Schristos    integers and unconditional shifts by more than 31 bits, as they're
159a7c91847Schristos    not portable.  */
160a7c91847Schristos #if BITSET_WORD_MAX == 0xffffffff
161a7c91847Schristos # define BITSET_WORD_BITS 32
162a7c91847Schristos #elif BITSET_WORD_MAX >> 31 >> 5 == 1
163a7c91847Schristos # define BITSET_WORD_BITS 36
164a7c91847Schristos #elif BITSET_WORD_MAX >> 31 >> 16 == 1
165a7c91847Schristos # define BITSET_WORD_BITS 48
166a7c91847Schristos #elif BITSET_WORD_MAX >> 31 >> 28 == 1
167a7c91847Schristos # define BITSET_WORD_BITS 60
168a7c91847Schristos #elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1
169a7c91847Schristos # define BITSET_WORD_BITS 64
170a7c91847Schristos #elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1
171a7c91847Schristos # define BITSET_WORD_BITS 72
172a7c91847Schristos #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1
173a7c91847Schristos # define BITSET_WORD_BITS 128
174a7c91847Schristos #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1
175a7c91847Schristos # define BITSET_WORD_BITS 256
176a7c91847Schristos #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1
177a7c91847Schristos # define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */
178a7c91847Schristos # if BITSET_WORD_BITS <= SBC_MAX
179a7c91847Schristos #  error "Invalid SBC_MAX"
180a7c91847Schristos # endif
181a7c91847Schristos #else
182a7c91847Schristos # error "Add case for new bitset_word size"
183a7c91847Schristos #endif
184a7c91847Schristos 
185a7c91847Schristos /* Number of bitset words in a bitset.  */
186a7c91847Schristos #define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
187a7c91847Schristos 
188a7c91847Schristos typedef bitset_word bitset[BITSET_WORDS];
189a7c91847Schristos typedef bitset_word *re_bitset_ptr_t;
190a7c91847Schristos typedef const bitset_word *re_const_bitset_ptr_t;
191a7c91847Schristos 
192a7c91847Schristos #define PREV_WORD_CONSTRAINT 0x0001
193a7c91847Schristos #define PREV_NOTWORD_CONSTRAINT 0x0002
194a7c91847Schristos #define NEXT_WORD_CONSTRAINT 0x0004
195a7c91847Schristos #define NEXT_NOTWORD_CONSTRAINT 0x0008
196a7c91847Schristos #define PREV_NEWLINE_CONSTRAINT 0x0010
197a7c91847Schristos #define NEXT_NEWLINE_CONSTRAINT 0x0020
198a7c91847Schristos #define PREV_BEGBUF_CONSTRAINT 0x0040
199a7c91847Schristos #define NEXT_ENDBUF_CONSTRAINT 0x0080
200a7c91847Schristos #define WORD_DELIM_CONSTRAINT 0x0100
201a7c91847Schristos #define NOT_WORD_DELIM_CONSTRAINT 0x0200
202a7c91847Schristos 
203a7c91847Schristos typedef enum
204a7c91847Schristos {
205a7c91847Schristos   INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
206a7c91847Schristos   WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
207a7c91847Schristos   WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
208a7c91847Schristos   INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
209a7c91847Schristos   LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
210a7c91847Schristos   LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
211a7c91847Schristos   BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
212a7c91847Schristos   BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
213a7c91847Schristos   WORD_DELIM = WORD_DELIM_CONSTRAINT,
214a7c91847Schristos   NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
215a7c91847Schristos } re_context_type;
216a7c91847Schristos 
217a7c91847Schristos typedef struct
218a7c91847Schristos {
219a7c91847Schristos   Idx alloc;
220a7c91847Schristos   Idx nelem;
221a7c91847Schristos   Idx *elems;
222a7c91847Schristos } re_node_set;
223a7c91847Schristos 
224a7c91847Schristos typedef enum
225a7c91847Schristos {
226a7c91847Schristos   NON_TYPE = 0,
227a7c91847Schristos 
228a7c91847Schristos   /* Node type, These are used by token, node, tree.  */
229a7c91847Schristos   CHARACTER = 1,
230a7c91847Schristos   END_OF_RE = 2,
231a7c91847Schristos   SIMPLE_BRACKET = 3,
232a7c91847Schristos   OP_BACK_REF = 4,
233a7c91847Schristos   OP_PERIOD = 5,
234a7c91847Schristos #ifdef RE_ENABLE_I18N
235a7c91847Schristos   COMPLEX_BRACKET = 6,
236a7c91847Schristos   OP_UTF8_PERIOD = 7,
237a7c91847Schristos #endif /* RE_ENABLE_I18N */
238a7c91847Schristos 
239a7c91847Schristos   /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
240a7c91847Schristos      when the debugger shows values of this enum type.  */
241a7c91847Schristos #define EPSILON_BIT 8
242a7c91847Schristos   OP_OPEN_SUBEXP = EPSILON_BIT | 0,
243a7c91847Schristos   OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
244a7c91847Schristos   OP_ALT = EPSILON_BIT | 2,
245a7c91847Schristos   OP_DUP_ASTERISK = EPSILON_BIT | 3,
246a7c91847Schristos   ANCHOR = EPSILON_BIT | 4,
247a7c91847Schristos 
248a7c91847Schristos   /* Tree type, these are used only by tree. */
249a7c91847Schristos   CONCAT = 16,
250a7c91847Schristos   SUBEXP = 17,
251a7c91847Schristos 
252a7c91847Schristos   /* Token type, these are used only by token.  */
253a7c91847Schristos   OP_DUP_PLUS = 18,
254a7c91847Schristos   OP_DUP_QUESTION,
255a7c91847Schristos   OP_OPEN_BRACKET,
256a7c91847Schristos   OP_CLOSE_BRACKET,
257a7c91847Schristos   OP_CHARSET_RANGE,
258a7c91847Schristos   OP_OPEN_DUP_NUM,
259a7c91847Schristos   OP_CLOSE_DUP_NUM,
260a7c91847Schristos   OP_NON_MATCH_LIST,
261a7c91847Schristos   OP_OPEN_COLL_ELEM,
262a7c91847Schristos   OP_CLOSE_COLL_ELEM,
263a7c91847Schristos   OP_OPEN_EQUIV_CLASS,
264a7c91847Schristos   OP_CLOSE_EQUIV_CLASS,
265a7c91847Schristos   OP_OPEN_CHAR_CLASS,
266a7c91847Schristos   OP_CLOSE_CHAR_CLASS,
267a7c91847Schristos   OP_WORD,
268a7c91847Schristos   OP_NOTWORD,
269a7c91847Schristos   OP_SPACE,
270a7c91847Schristos   OP_NOTSPACE,
271a7c91847Schristos   BACK_SLASH
272a7c91847Schristos 
273a7c91847Schristos } re_token_type_t;
274a7c91847Schristos 
275a7c91847Schristos #ifdef RE_ENABLE_I18N
276a7c91847Schristos typedef struct
277a7c91847Schristos {
278a7c91847Schristos   /* Multibyte characters.  */
279a7c91847Schristos   wchar_t *mbchars;
280a7c91847Schristos 
281a7c91847Schristos   /* Collating symbols.  */
282a7c91847Schristos # ifdef _LIBC
283a7c91847Schristos   int32_t *coll_syms;
284a7c91847Schristos # endif
285a7c91847Schristos 
286a7c91847Schristos   /* Equivalence classes. */
287a7c91847Schristos # ifdef _LIBC
288a7c91847Schristos   int32_t *equiv_classes;
289a7c91847Schristos # endif
290a7c91847Schristos 
291a7c91847Schristos   /* Range expressions. */
292a7c91847Schristos # ifdef _LIBC
293a7c91847Schristos   uint32_t *range_starts;
294a7c91847Schristos   uint32_t *range_ends;
295a7c91847Schristos # else /* not _LIBC */
296a7c91847Schristos   wchar_t *range_starts;
297a7c91847Schristos   wchar_t *range_ends;
298a7c91847Schristos # endif /* not _LIBC */
299a7c91847Schristos 
300a7c91847Schristos   /* Character classes. */
301a7c91847Schristos   wctype_t *char_classes;
302a7c91847Schristos 
303a7c91847Schristos   /* If this character set is the non-matching list.  */
304a7c91847Schristos   unsigned int non_match : 1;
305a7c91847Schristos 
306a7c91847Schristos   /* # of multibyte characters.  */
307a7c91847Schristos   Idx nmbchars;
308a7c91847Schristos 
309a7c91847Schristos   /* # of collating symbols.  */
310a7c91847Schristos   Idx ncoll_syms;
311a7c91847Schristos 
312a7c91847Schristos   /* # of equivalence classes. */
313a7c91847Schristos   Idx nequiv_classes;
314a7c91847Schristos 
315a7c91847Schristos   /* # of range expressions. */
316a7c91847Schristos   Idx nranges;
317a7c91847Schristos 
318a7c91847Schristos   /* # of character classes. */
319a7c91847Schristos   Idx nchar_classes;
320a7c91847Schristos } re_charset_t;
321a7c91847Schristos #endif /* RE_ENABLE_I18N */
322a7c91847Schristos 
323a7c91847Schristos typedef struct
324a7c91847Schristos {
325a7c91847Schristos   union
326a7c91847Schristos   {
327a7c91847Schristos     unsigned char c;		/* for CHARACTER */
328a7c91847Schristos     re_bitset_ptr_t sbcset;	/* for SIMPLE_BRACKET */
329a7c91847Schristos #ifdef RE_ENABLE_I18N
330a7c91847Schristos     re_charset_t *mbcset;	/* for COMPLEX_BRACKET */
331a7c91847Schristos #endif /* RE_ENABLE_I18N */
332a7c91847Schristos     Idx idx;			/* for BACK_REF */
333a7c91847Schristos     re_context_type ctx_type;	/* for ANCHOR */
334a7c91847Schristos   } opr;
335a7c91847Schristos #if __GNUC__ >= 2
336a7c91847Schristos   re_token_type_t type : 8;
337a7c91847Schristos #else
338a7c91847Schristos   re_token_type_t type;
339a7c91847Schristos #endif
340a7c91847Schristos   unsigned int constraint : 10;	/* context constraint */
341a7c91847Schristos   unsigned int duplicated : 1;
342a7c91847Schristos   unsigned int opt_subexp : 1;
343a7c91847Schristos #ifdef RE_ENABLE_I18N
344a7c91847Schristos   unsigned int accept_mb : 1;
345a7c91847Schristos   /* These 2 bits can be moved into the union if needed (e.g. if running out
346a7c91847Schristos      of bits; move opr.c to opr.c.c and move the flags to opr.c.flags).  */
347a7c91847Schristos   unsigned int mb_partial : 1;
348a7c91847Schristos #endif
349a7c91847Schristos   unsigned int word_char : 1;
350a7c91847Schristos } re_token_t;
351a7c91847Schristos 
352a7c91847Schristos #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
353a7c91847Schristos 
354a7c91847Schristos struct re_string_t
355a7c91847Schristos {
356a7c91847Schristos   /* Indicate the raw buffer which is the original string passed as an
357a7c91847Schristos      argument of regexec(), re_search(), etc..  */
358a7c91847Schristos   const unsigned char *raw_mbs;
359a7c91847Schristos   /* Store the multibyte string.  In case of "case insensitive mode" like
360a7c91847Schristos      REG_ICASE, upper cases of the string are stored, otherwise MBS points
361a7c91847Schristos      the same address that RAW_MBS points.  */
362a7c91847Schristos   unsigned char *mbs;
363a7c91847Schristos #ifdef RE_ENABLE_I18N
364a7c91847Schristos   /* Store the wide character string which is corresponding to MBS.  */
365a7c91847Schristos   wint_t *wcs;
366a7c91847Schristos   Idx *offsets;
367a7c91847Schristos   mbstate_t cur_state;
368a7c91847Schristos #endif
369a7c91847Schristos   /* Index in RAW_MBS.  Each character mbs[i] corresponds to
370a7c91847Schristos      raw_mbs[raw_mbs_idx + i].  */
371a7c91847Schristos   Idx raw_mbs_idx;
372a7c91847Schristos   /* The length of the valid characters in the buffers.  */
373a7c91847Schristos   Idx valid_len;
374a7c91847Schristos   /* The corresponding number of bytes in raw_mbs array.  */
375a7c91847Schristos   Idx valid_raw_len;
376a7c91847Schristos   /* The length of the buffers MBS and WCS.  */
377a7c91847Schristos   Idx bufs_len;
378a7c91847Schristos   /* The index in MBS, which is updated by re_string_fetch_byte.  */
379a7c91847Schristos   Idx cur_idx;
380a7c91847Schristos   /* length of RAW_MBS array.  */
381a7c91847Schristos   Idx raw_len;
382a7c91847Schristos   /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN.  */
383a7c91847Schristos   Idx len;
384a7c91847Schristos   /* End of the buffer may be shorter than its length in the cases such
385a7c91847Schristos      as re_match_2, re_search_2.  Then, we use STOP for end of the buffer
386a7c91847Schristos      instead of LEN.  */
387a7c91847Schristos   Idx raw_stop;
388a7c91847Schristos   /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS.  */
389a7c91847Schristos   Idx stop;
390a7c91847Schristos 
391a7c91847Schristos   /* The context of mbs[0].  We store the context independently, since
392a7c91847Schristos      the context of mbs[0] may be different from raw_mbs[0], which is
393a7c91847Schristos      the beginning of the input string.  */
394a7c91847Schristos   unsigned int tip_context;
395a7c91847Schristos   /* The translation passed as a part of an argument of re_compile_pattern.  */
396a7c91847Schristos   unsigned REG_TRANSLATE_TYPE trans;
397a7c91847Schristos   /* Copy of re_dfa_t's word_char.  */
398a7c91847Schristos   re_const_bitset_ptr_t word_char;
399a7c91847Schristos   /* true if REG_ICASE.  */
400a7c91847Schristos   unsigned char icase;
401a7c91847Schristos   unsigned char is_utf8;
402a7c91847Schristos   unsigned char map_notascii;
403a7c91847Schristos   unsigned char mbs_allocated;
404a7c91847Schristos   unsigned char offsets_needed;
405a7c91847Schristos   unsigned char newline_anchor;
406a7c91847Schristos   unsigned char word_ops_used;
407a7c91847Schristos   int mb_cur_max;
408a7c91847Schristos };
409a7c91847Schristos typedef struct re_string_t re_string_t;
410a7c91847Schristos 
411a7c91847Schristos 
412a7c91847Schristos struct re_dfa_t;
413a7c91847Schristos typedef struct re_dfa_t re_dfa_t;
414a7c91847Schristos 
415a7c91847Schristos #ifndef _LIBC
416a7c91847Schristos # ifdef __i386__
417a7c91847Schristos #  define internal_function   __attribute ((regparm (3), stdcall))
418a7c91847Schristos # else
419a7c91847Schristos #  define internal_function
420a7c91847Schristos # endif
421a7c91847Schristos #endif
422a7c91847Schristos 
423a7c91847Schristos static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
424a7c91847Schristos 						Idx new_buf_len)
425a7c91847Schristos      internal_function;
426a7c91847Schristos #ifdef RE_ENABLE_I18N
427a7c91847Schristos static void build_wcs_buffer (re_string_t *pstr) internal_function;
428a7c91847Schristos static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
429a7c91847Schristos      internal_function;
430a7c91847Schristos #endif /* RE_ENABLE_I18N */
431a7c91847Schristos static void build_upper_buffer (re_string_t *pstr) internal_function;
432a7c91847Schristos static void re_string_translate_buffer (re_string_t *pstr) internal_function;
433a7c91847Schristos static unsigned int re_string_context_at (const re_string_t *input,
434a7c91847Schristos 					  Idx idx, int eflags)
435a7c91847Schristos      internal_function __attribute ((pure));
436a7c91847Schristos 
437a7c91847Schristos #define re_string_peek_byte(pstr, offset) \
438a7c91847Schristos   ((pstr)->mbs[(pstr)->cur_idx + offset])
439a7c91847Schristos #define re_string_fetch_byte(pstr) \
440a7c91847Schristos   ((pstr)->mbs[(pstr)->cur_idx++])
441a7c91847Schristos #define re_string_first_byte(pstr, idx) \
442a7c91847Schristos   ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
443a7c91847Schristos #define re_string_is_single_byte_char(pstr, idx) \
444a7c91847Schristos   ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
445a7c91847Schristos 				|| (pstr)->wcs[(idx) + 1] != WEOF))
446a7c91847Schristos #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
447a7c91847Schristos #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
448a7c91847Schristos #define re_string_get_buffer(pstr) ((pstr)->mbs)
449a7c91847Schristos #define re_string_length(pstr) ((pstr)->len)
450a7c91847Schristos #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
451a7c91847Schristos #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
452a7c91847Schristos #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
453a7c91847Schristos 
454*274254cdSchristos #ifndef __NetBSD__
455a7c91847Schristos #include <alloca.h>
456*274254cdSchristos #endif
457a7c91847Schristos 
458a7c91847Schristos #ifndef _LIBC
459a7c91847Schristos # if HAVE_ALLOCA
460a7c91847Schristos /* The OS usually guarantees only one guard page at the bottom of the stack,
461a7c91847Schristos    and a page size can be as small as 4096 bytes.  So we cannot safely
462a7c91847Schristos    allocate anything larger than 4096 bytes.  Also care for the possibility
463a7c91847Schristos    of a few compiler-allocated temporary stack slots.  */
464a7c91847Schristos #  define __libc_use_alloca(n) ((n) < 4032)
465a7c91847Schristos # else
466a7c91847Schristos /* alloca is implemented with malloc, so just use malloc.  */
467a7c91847Schristos #  define __libc_use_alloca(n) 0
468a7c91847Schristos # endif
469a7c91847Schristos #endif
470a7c91847Schristos 
471a7c91847Schristos #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
472a7c91847Schristos #define re_xmalloc(t,n) ((t *) re_xnmalloc (n, sizeof (t)))
473a7c91847Schristos #define re_calloc(t,n) ((t *) calloc (n, sizeof (t)))
474a7c91847Schristos #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
475a7c91847Schristos #define re_xrealloc(p,t,n) ((t *) re_xnrealloc (p, n, sizeof (t)))
476a7c91847Schristos #define re_x2realloc(p,t,pn) ((t *) re_x2nrealloc (p, pn, sizeof (t)))
477a7c91847Schristos #define re_free(p) free (p)
478a7c91847Schristos 
479a7c91847Schristos #ifndef SIZE_MAX
480a7c91847Schristos # define SIZE_MAX ((size_t) -1)
481a7c91847Schristos #endif
482a7c91847Schristos 
483a7c91847Schristos /* Return true if an array of N objects, each of size S, cannot exist
484a7c91847Schristos    due to size arithmetic overflow.  S must be nonzero.  */
485a7c91847Schristos static inline bool
re_alloc_oversized(size_t n,size_t s)486a7c91847Schristos re_alloc_oversized (size_t n, size_t s)
487a7c91847Schristos {
488a7c91847Schristos   return BE (SIZE_MAX / s < n, 0);
489a7c91847Schristos }
490a7c91847Schristos 
491a7c91847Schristos /* Return true if an array of (2 * N + 1) objects, each of size S,
492a7c91847Schristos    cannot exist due to size arithmetic overflow.  S must be nonzero.  */
493a7c91847Schristos static inline bool
re_x2alloc_oversized(size_t n,size_t s)494a7c91847Schristos re_x2alloc_oversized (size_t n, size_t s)
495a7c91847Schristos {
496a7c91847Schristos   return BE ((SIZE_MAX / s - 1) / 2 < n, 0);
497a7c91847Schristos }
498a7c91847Schristos 
499a7c91847Schristos /* Allocate an array of N objects, each with S bytes of memory,
500a7c91847Schristos    dynamically, with error checking.  S must be nonzero.  */
501a7c91847Schristos static inline void *
re_xnmalloc(size_t n,size_t s)502a7c91847Schristos re_xnmalloc (size_t n, size_t s)
503a7c91847Schristos {
504a7c91847Schristos   return re_alloc_oversized (n, s) ? NULL : malloc (n * s);
505a7c91847Schristos }
506a7c91847Schristos 
507a7c91847Schristos /* Change the size of an allocated block of memory P to an array of N
508a7c91847Schristos    objects each of S bytes, with error checking.  S must be nonzero.  */
509a7c91847Schristos static inline void *
re_xnrealloc(void * p,size_t n,size_t s)510a7c91847Schristos re_xnrealloc (void *p, size_t n, size_t s)
511a7c91847Schristos {
512a7c91847Schristos   return re_alloc_oversized (n, s) ? NULL : realloc (p, n * s);
513a7c91847Schristos }
514a7c91847Schristos 
515a7c91847Schristos /* Reallocate a block of memory P to an array of (2 * (*PN) + 1)
516a7c91847Schristos    objects each of S bytes, with error checking.  S must be nonzero.
517a7c91847Schristos    If the allocation is successful, set *PN to the new allocation
518a7c91847Schristos    count and return the resulting pointer.  Otherwise, return
519a7c91847Schristos    NULL.  */
520a7c91847Schristos static inline void *
re_x2nrealloc(void * p,size_t * pn,size_t s)521a7c91847Schristos re_x2nrealloc (void *p, size_t *pn, size_t s)
522a7c91847Schristos {
523a7c91847Schristos   if (re_x2alloc_oversized (*pn, s))
524a7c91847Schristos     return NULL;
525a7c91847Schristos   else
526a7c91847Schristos     {
527a7c91847Schristos       /* Add 1 in case *PN is zero.  */
528a7c91847Schristos       size_t n1 = 2 * *pn + 1;
529a7c91847Schristos       p = realloc (p, n1 * s);
530a7c91847Schristos       if (BE (p != NULL, 1))
531a7c91847Schristos 	*pn = n1;
532a7c91847Schristos       return p;
533a7c91847Schristos     }
534a7c91847Schristos }
535a7c91847Schristos 
536a7c91847Schristos struct bin_tree_t
537a7c91847Schristos {
538a7c91847Schristos   struct bin_tree_t *parent;
539a7c91847Schristos   struct bin_tree_t *left;
540a7c91847Schristos   struct bin_tree_t *right;
541a7c91847Schristos   struct bin_tree_t *first;
542a7c91847Schristos   struct bin_tree_t *next;
543a7c91847Schristos 
544a7c91847Schristos   re_token_t token;
545a7c91847Schristos 
546a7c91847Schristos   /* `node_idx' is the index in dfa->nodes, if `type' == 0.
547a7c91847Schristos      Otherwise `type' indicate the type of this node.  */
548a7c91847Schristos   Idx node_idx;
549a7c91847Schristos };
550a7c91847Schristos typedef struct bin_tree_t bin_tree_t;
551a7c91847Schristos 
552a7c91847Schristos #define BIN_TREE_STORAGE_SIZE \
553a7c91847Schristos   ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
554a7c91847Schristos 
555a7c91847Schristos struct bin_tree_storage_t
556a7c91847Schristos {
557a7c91847Schristos   struct bin_tree_storage_t *next;
558a7c91847Schristos   bin_tree_t data[BIN_TREE_STORAGE_SIZE];
559a7c91847Schristos };
560a7c91847Schristos typedef struct bin_tree_storage_t bin_tree_storage_t;
561a7c91847Schristos 
562a7c91847Schristos #define CONTEXT_WORD 1
563a7c91847Schristos #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
564a7c91847Schristos #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
565a7c91847Schristos #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
566a7c91847Schristos 
567a7c91847Schristos #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
568a7c91847Schristos #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
569a7c91847Schristos #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
570a7c91847Schristos #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
571a7c91847Schristos #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
572a7c91847Schristos 
573a7c91847Schristos #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
574a7c91847Schristos #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
575a7c91847Schristos #define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_')
576a7c91847Schristos #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
577a7c91847Schristos 
578a7c91847Schristos #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
579a7c91847Schristos  ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
580a7c91847Schristos   || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
581a7c91847Schristos   || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
582a7c91847Schristos   || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
583a7c91847Schristos 
584a7c91847Schristos #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
585a7c91847Schristos  ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
586a7c91847Schristos   || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
587a7c91847Schristos   || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
588a7c91847Schristos   || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
589a7c91847Schristos 
590a7c91847Schristos struct re_dfastate_t
591a7c91847Schristos {
592a7c91847Schristos   re_hashval_t hash;
593a7c91847Schristos   re_node_set nodes;
594a7c91847Schristos   re_node_set non_eps_nodes;
595a7c91847Schristos   re_node_set inveclosure;
596a7c91847Schristos   re_node_set *entrance_nodes;
597a7c91847Schristos   struct re_dfastate_t **trtable, **word_trtable;
598a7c91847Schristos   unsigned int context : 4;
599a7c91847Schristos   unsigned int halt : 1;
600a7c91847Schristos   /* If this state can accept `multi byte'.
601a7c91847Schristos      Note that we refer to multibyte characters, and multi character
602a7c91847Schristos      collating elements as `multi byte'.  */
603a7c91847Schristos   unsigned int accept_mb : 1;
604a7c91847Schristos   /* If this state has backreference node(s).  */
605a7c91847Schristos   unsigned int has_backref : 1;
606a7c91847Schristos   unsigned int has_constraint : 1;
607a7c91847Schristos };
608a7c91847Schristos typedef struct re_dfastate_t re_dfastate_t;
609a7c91847Schristos 
610a7c91847Schristos struct re_state_table_entry
611a7c91847Schristos {
612a7c91847Schristos   Idx num;
613a7c91847Schristos   Idx alloc;
614a7c91847Schristos   re_dfastate_t **array;
615a7c91847Schristos };
616a7c91847Schristos 
617a7c91847Schristos /* Array type used in re_sub_match_last_t and re_sub_match_top_t.  */
618a7c91847Schristos 
619a7c91847Schristos typedef struct
620a7c91847Schristos {
621a7c91847Schristos   Idx next_idx;
622a7c91847Schristos   Idx alloc;
623a7c91847Schristos   re_dfastate_t **array;
624a7c91847Schristos } state_array_t;
625a7c91847Schristos 
626a7c91847Schristos /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP.  */
627a7c91847Schristos 
628a7c91847Schristos typedef struct
629a7c91847Schristos {
630a7c91847Schristos   Idx node;
631a7c91847Schristos   Idx str_idx; /* The position NODE match at.  */
632a7c91847Schristos   state_array_t path;
633a7c91847Schristos } re_sub_match_last_t;
634a7c91847Schristos 
635a7c91847Schristos /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
636a7c91847Schristos    And information about the node, whose type is OP_CLOSE_SUBEXP,
637a7c91847Schristos    corresponding to NODE is stored in LASTS.  */
638a7c91847Schristos 
639a7c91847Schristos typedef struct
640a7c91847Schristos {
641a7c91847Schristos   Idx str_idx;
642a7c91847Schristos   Idx node;
643a7c91847Schristos   state_array_t *path;
644a7c91847Schristos   Idx alasts; /* Allocation size of LASTS.  */
645a7c91847Schristos   Idx nlasts; /* The number of LASTS.  */
646a7c91847Schristos   re_sub_match_last_t **lasts;
647a7c91847Schristos } re_sub_match_top_t;
648a7c91847Schristos 
649a7c91847Schristos struct re_backref_cache_entry
650a7c91847Schristos {
651a7c91847Schristos   Idx node;
652a7c91847Schristos   Idx str_idx;
653a7c91847Schristos   Idx subexp_from;
654a7c91847Schristos   Idx subexp_to;
655a7c91847Schristos   char more;
656a7c91847Schristos   char unused;
657a7c91847Schristos   unsigned short int eps_reachable_subexps_map;
658a7c91847Schristos };
659a7c91847Schristos 
660a7c91847Schristos typedef struct
661a7c91847Schristos {
662a7c91847Schristos   /* The string object corresponding to the input string.  */
663a7c91847Schristos   re_string_t input;
664a7c91847Schristos #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
665a7c91847Schristos   re_dfa_t *const dfa;
666a7c91847Schristos #else
667a7c91847Schristos   re_dfa_t *dfa;
668a7c91847Schristos #endif
669a7c91847Schristos   /* EFLAGS of the argument of regexec.  */
670a7c91847Schristos   int eflags;
671a7c91847Schristos   /* Where the matching ends.  */
672a7c91847Schristos   Idx match_last;
673a7c91847Schristos   Idx last_node;
674a7c91847Schristos   /* The state log used by the matcher.  */
675a7c91847Schristos   re_dfastate_t **state_log;
676a7c91847Schristos   Idx state_log_top;
677a7c91847Schristos   /* Back reference cache.  */
678a7c91847Schristos   Idx nbkref_ents;
679a7c91847Schristos   Idx abkref_ents;
680a7c91847Schristos   struct re_backref_cache_entry *bkref_ents;
681a7c91847Schristos   int max_mb_elem_len;
682a7c91847Schristos   Idx nsub_tops;
683a7c91847Schristos   Idx asub_tops;
684a7c91847Schristos   re_sub_match_top_t **sub_tops;
685a7c91847Schristos } re_match_context_t;
686a7c91847Schristos 
687a7c91847Schristos typedef struct
688a7c91847Schristos {
689a7c91847Schristos   re_dfastate_t **sifted_states;
690a7c91847Schristos   re_dfastate_t **limited_states;
691a7c91847Schristos   Idx last_node;
692a7c91847Schristos   Idx last_str_idx;
693a7c91847Schristos   re_node_set limits;
694a7c91847Schristos } re_sift_context_t;
695a7c91847Schristos 
696a7c91847Schristos struct re_fail_stack_ent_t
697a7c91847Schristos {
698a7c91847Schristos   Idx idx;
699a7c91847Schristos   Idx node;
700a7c91847Schristos   regmatch_t *regs;
701a7c91847Schristos   re_node_set eps_via_nodes;
702a7c91847Schristos };
703a7c91847Schristos 
704a7c91847Schristos struct re_fail_stack_t
705a7c91847Schristos {
706a7c91847Schristos   Idx num;
707a7c91847Schristos   Idx alloc;
708a7c91847Schristos   struct re_fail_stack_ent_t *stack;
709a7c91847Schristos };
710a7c91847Schristos 
711a7c91847Schristos struct re_dfa_t
712a7c91847Schristos {
713a7c91847Schristos   re_token_t *nodes;
714a7c91847Schristos   Idx nodes_alloc;
715a7c91847Schristos   Idx nodes_len;
716a7c91847Schristos   Idx *nexts;
717a7c91847Schristos   Idx *org_indices;
718a7c91847Schristos   re_node_set *edests;
719a7c91847Schristos   re_node_set *eclosures;
720a7c91847Schristos   re_node_set *inveclosures;
721a7c91847Schristos   struct re_state_table_entry *state_table;
722a7c91847Schristos   re_dfastate_t *init_state;
723a7c91847Schristos   re_dfastate_t *init_state_word;
724a7c91847Schristos   re_dfastate_t *init_state_nl;
725a7c91847Schristos   re_dfastate_t *init_state_begbuf;
726a7c91847Schristos   bin_tree_t *str_tree;
727a7c91847Schristos   bin_tree_storage_t *str_tree_storage;
728a7c91847Schristos   re_bitset_ptr_t sb_char;
729a7c91847Schristos   int str_tree_storage_idx;
730a7c91847Schristos 
731a7c91847Schristos   /* number of subexpressions `re_nsub' is in regex_t.  */
732a7c91847Schristos   re_hashval_t state_hash_mask;
733a7c91847Schristos   Idx init_node;
734a7c91847Schristos   Idx nbackref; /* The number of backreference in this dfa.  */
735a7c91847Schristos 
736a7c91847Schristos   /* Bitmap expressing which backreference is used.  */
737a7c91847Schristos   bitset_word used_bkref_map;
738a7c91847Schristos   bitset_word completed_bkref_map;
739a7c91847Schristos 
740a7c91847Schristos   unsigned int has_plural_match : 1;
741a7c91847Schristos   /* If this dfa has "multibyte node", which is a backreference or
742a7c91847Schristos      a node which can accept multibyte character or multi character
743a7c91847Schristos      collating element.  */
744a7c91847Schristos   unsigned int has_mb_node : 1;
745a7c91847Schristos   unsigned int is_utf8 : 1;
746a7c91847Schristos   unsigned int map_notascii : 1;
747a7c91847Schristos   unsigned int word_ops_used : 1;
748a7c91847Schristos   int mb_cur_max;
749a7c91847Schristos   bitset word_char;
750a7c91847Schristos   reg_syntax_t syntax;
751a7c91847Schristos   Idx *subexp_map;
752a7c91847Schristos #ifdef DEBUG
753a7c91847Schristos   char* re_str;
754a7c91847Schristos #endif
755a7c91847Schristos   __libc_lock_define (, lock)
756a7c91847Schristos };
757a7c91847Schristos 
758a7c91847Schristos #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
759a7c91847Schristos #define re_node_set_remove(set,id) \
760a7c91847Schristos   (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
761a7c91847Schristos #define re_node_set_empty(p) ((p)->nelem = 0)
762a7c91847Schristos #define re_node_set_free(set) re_free ((set)->elems)
763a7c91847Schristos 
764a7c91847Schristos static void free_state (re_dfastate_t *state) internal_function;
765a7c91847Schristos 
766a7c91847Schristos 
767a7c91847Schristos typedef enum
768a7c91847Schristos {
769a7c91847Schristos   SB_CHAR,
770a7c91847Schristos   MB_CHAR,
771a7c91847Schristos   EQUIV_CLASS,
772a7c91847Schristos   COLL_SYM,
773a7c91847Schristos   CHAR_CLASS
774a7c91847Schristos } bracket_elem_type;
775a7c91847Schristos 
776a7c91847Schristos typedef struct
777a7c91847Schristos {
778a7c91847Schristos   bracket_elem_type type;
779a7c91847Schristos   union
780a7c91847Schristos   {
781a7c91847Schristos     unsigned char ch;
782a7c91847Schristos     unsigned char *name;
783a7c91847Schristos     wchar_t wch;
784a7c91847Schristos   } opr;
785a7c91847Schristos } bracket_elem_t;
786a7c91847Schristos 
787a7c91847Schristos 
788a7c91847Schristos /* Inline functions for bitset operation.  */
789a7c91847Schristos 
790a7c91847Schristos static inline void
bitset_set(bitset set,Idx i)791a7c91847Schristos bitset_set (bitset set, Idx i)
792a7c91847Schristos {
793a7c91847Schristos   set[i / BITSET_WORD_BITS] |= (bitset_word) 1 << i % BITSET_WORD_BITS;
794a7c91847Schristos }
795a7c91847Schristos 
796a7c91847Schristos static inline void
bitset_clear(bitset set,Idx i)797a7c91847Schristos bitset_clear (bitset set, Idx i)
798a7c91847Schristos {
799a7c91847Schristos   set[i / BITSET_WORD_BITS] &= ~ ((bitset_word) 1 << i % BITSET_WORD_BITS);
800a7c91847Schristos }
801a7c91847Schristos 
802a7c91847Schristos static inline bool
bitset_contain(const bitset set,Idx i)803a7c91847Schristos bitset_contain (const bitset set, Idx i)
804a7c91847Schristos {
805a7c91847Schristos   return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
806a7c91847Schristos }
807a7c91847Schristos 
808a7c91847Schristos static inline void
bitset_empty(bitset set)809a7c91847Schristos bitset_empty (bitset set)
810a7c91847Schristos {
811a7c91847Schristos   memset (set, 0, sizeof (bitset));
812a7c91847Schristos }
813a7c91847Schristos 
814a7c91847Schristos static inline void
bitset_set_all(bitset set)815a7c91847Schristos bitset_set_all (bitset set)
816a7c91847Schristos {
817a7c91847Schristos   memset (set, -1, sizeof (bitset_word) * (SBC_MAX / BITSET_WORD_BITS));
818a7c91847Schristos   if (SBC_MAX % BITSET_WORD_BITS != 0)
819a7c91847Schristos     set[BITSET_WORDS - 1] =
820a7c91847Schristos       ((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
821a7c91847Schristos }
822a7c91847Schristos 
823a7c91847Schristos static inline void
bitset_copy(bitset dest,const bitset src)824a7c91847Schristos bitset_copy (bitset dest, const bitset src)
825a7c91847Schristos {
826a7c91847Schristos   memcpy (dest, src, sizeof (bitset));
827a7c91847Schristos }
828a7c91847Schristos 
829a7c91847Schristos static inline void
bitset_not(bitset set)830a7c91847Schristos bitset_not (bitset set)
831a7c91847Schristos {
832a7c91847Schristos   int i;
833a7c91847Schristos   for (i = 0; i < SBC_MAX / BITSET_WORD_BITS; ++i)
834a7c91847Schristos     set[i] = ~set[i];
835a7c91847Schristos   if (SBC_MAX % BITSET_WORD_BITS != 0)
836a7c91847Schristos     set[BITSET_WORDS - 1] =
837a7c91847Schristos       ((((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
838a7c91847Schristos        & ~set[BITSET_WORDS - 1]);
839a7c91847Schristos }
840a7c91847Schristos 
841a7c91847Schristos static inline void
bitset_merge(bitset dest,const bitset src)842a7c91847Schristos bitset_merge (bitset dest, const bitset src)
843a7c91847Schristos {
844a7c91847Schristos   int i;
845a7c91847Schristos   for (i = 0; i < BITSET_WORDS; ++i)
846a7c91847Schristos     dest[i] |= src[i];
847a7c91847Schristos }
848a7c91847Schristos 
849a7c91847Schristos static inline void
bitset_mask(bitset dest,const bitset src)850a7c91847Schristos bitset_mask (bitset dest, const bitset src)
851a7c91847Schristos {
852a7c91847Schristos   int i;
853a7c91847Schristos   for (i = 0; i < BITSET_WORDS; ++i)
854a7c91847Schristos     dest[i] &= src[i];
855a7c91847Schristos }
856a7c91847Schristos 
857a7c91847Schristos #if defined RE_ENABLE_I18N
858a7c91847Schristos /* Inline functions for re_string.  */
859a7c91847Schristos static inline int
internal_function(pure)860a7c91847Schristos internal_function __attribute ((pure))
861a7c91847Schristos re_string_char_size_at (const re_string_t *pstr, Idx idx)
862a7c91847Schristos {
863a7c91847Schristos   int byte_idx;
864a7c91847Schristos   if (pstr->mb_cur_max == 1)
865a7c91847Schristos     return 1;
866a7c91847Schristos   for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
867a7c91847Schristos     if (pstr->wcs[idx + byte_idx] != WEOF)
868a7c91847Schristos       break;
869a7c91847Schristos   return byte_idx;
870a7c91847Schristos }
871a7c91847Schristos 
872a7c91847Schristos static inline wint_t
internal_function(pure)873a7c91847Schristos internal_function __attribute ((pure))
874a7c91847Schristos re_string_wchar_at (const re_string_t *pstr, Idx idx)
875a7c91847Schristos {
876a7c91847Schristos   if (pstr->mb_cur_max == 1)
877a7c91847Schristos     return (wint_t) pstr->mbs[idx];
878a7c91847Schristos   return (wint_t) pstr->wcs[idx];
879a7c91847Schristos }
880a7c91847Schristos 
881a7c91847Schristos static int
internal_function(pure)882a7c91847Schristos internal_function __attribute ((pure))
883a7c91847Schristos re_string_elem_size_at (const re_string_t *pstr, Idx idx)
884a7c91847Schristos {
885a7c91847Schristos #ifdef _LIBC
886a7c91847Schristos   const unsigned char *p, *extra;
887a7c91847Schristos   const int32_t *table, *indirect;
888a7c91847Schristos   int32_t tmp;
889a7c91847Schristos # include <locale/weight.h>
890a7c91847Schristos   uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
891a7c91847Schristos 
892a7c91847Schristos   if (nrules != 0)
893a7c91847Schristos     {
894a7c91847Schristos       table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
895a7c91847Schristos       extra = (const unsigned char *)
896a7c91847Schristos 	_NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
897a7c91847Schristos       indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
898a7c91847Schristos 						_NL_COLLATE_INDIRECTMB);
899a7c91847Schristos       p = pstr->mbs + idx;
900a7c91847Schristos       tmp = findidx (&p);
901a7c91847Schristos       return p - pstr->mbs - idx;
902a7c91847Schristos     }
903a7c91847Schristos   else
904a7c91847Schristos #endif /* _LIBC */
905a7c91847Schristos     return 1;
906a7c91847Schristos }
907a7c91847Schristos #endif /* RE_ENABLE_I18N */
908a7c91847Schristos 
909a7c91847Schristos #endif /*  _REGEX_INTERNAL_H */
910