Use common bits/shm.h for more architectures.
[glibc.git] / posix / regex_internal.h
blobc7880667dd02d797c8f6076aca0177d24e279d79
1 /* Extended regular expression matching and search library.
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #ifndef _REGEX_INTERNAL_H
21 #define _REGEX_INTERNAL_H 1
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include <langinfo.h>
30 #include <locale.h>
31 #include <wchar.h>
32 #include <wctype.h>
33 #include <stdbool.h>
34 #include <stdint.h>
36 /* Properties of integers. Although Gnulib has intprops.h, glibc does
37 without for now. */
38 #ifndef _LIBC
39 # include "intprops.h"
40 #else
41 /* True if the real type T is signed. */
42 # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
44 /* True if adding the nonnegative Idx values A and B would overflow.
45 If false, set *R to A + B. A, B, and R may be evaluated more than
46 once, or zero times. Although this is not a full implementation of
47 Gnulib INT_ADD_WRAPV, it is good enough for glibc regex code.
48 FIXME: This implementation is a fragile stopgap, and this file would
49 be simpler and more robust if intprops.h were migrated into glibc. */
50 # define INT_ADD_WRAPV(a, b, r) \
51 (IDX_MAX - (a) < (b) ? true : (*(r) = (a) + (b), false))
52 #endif
54 #ifdef _LIBC
55 # include <libc-lock.h>
56 # define lock_define(name) __libc_lock_define (, name)
57 # define lock_init(lock) (__libc_lock_init (lock), 0)
58 # define lock_fini(lock) ((void) 0)
59 # define lock_lock(lock) __libc_lock_lock (lock)
60 # define lock_unlock(lock) __libc_lock_unlock (lock)
61 #elif defined GNULIB_LOCK && !defined USE_UNLOCKED_IO
62 # include "glthread/lock.h"
63 /* Use gl_lock_define if empty macro arguments are known to work.
64 Otherwise, fall back on less-portable substitutes. */
65 # if ((defined __GNUC__ && !defined __STRICT_ANSI__) \
66 || (defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__))
67 # define lock_define(name) gl_lock_define (, name)
68 # elif USE_POSIX_THREADS
69 # define lock_define(name) pthread_mutex_t name;
70 # elif USE_PTH_THREADS
71 # define lock_define(name) pth_mutex_t name;
72 # elif USE_SOLARIS_THREADS
73 # define lock_define(name) mutex_t name;
74 # elif USE_WINDOWS_THREADS
75 # define lock_define(name) gl_lock_t name;
76 # else
77 # define lock_define(name)
78 # endif
79 # define lock_init(lock) glthread_lock_init (&(lock))
80 # define lock_fini(lock) glthread_lock_destroy (&(lock))
81 # define lock_lock(lock) glthread_lock_lock (&(lock))
82 # define lock_unlock(lock) glthread_lock_unlock (&(lock))
83 #elif defined GNULIB_PTHREAD && !defined USE_UNLOCKED_IO
84 # include <pthread.h>
85 # define lock_define(name) pthread_mutex_t name;
86 # define lock_init(lock) pthread_mutex_init (&(lock), 0)
87 # define lock_fini(lock) pthread_mutex_destroy (&(lock))
88 # define lock_lock(lock) pthread_mutex_lock (&(lock))
89 # define lock_unlock(lock) pthread_mutex_unlock (&(lock))
90 #else
91 # define lock_define(name)
92 # define lock_init(lock) 0
93 # define lock_fini(lock) ((void) 0)
94 /* The 'dfa' avoids an "unused variable 'dfa'" warning from GCC. */
95 # define lock_lock(lock) ((void) dfa)
96 # define lock_unlock(lock) ((void) 0)
97 #endif
99 /* In case that the system doesn't have isblank(). */
100 #if !defined _LIBC && ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
101 # define isblank(ch) ((ch) == ' ' || (ch) == '\t')
102 #endif
104 #ifdef _LIBC
105 # ifndef _RE_DEFINE_LOCALE_FUNCTIONS
106 # define _RE_DEFINE_LOCALE_FUNCTIONS 1
107 # include <locale/localeinfo.h>
108 # include <locale/coll-lookup.h>
109 # endif
110 #endif
112 /* This is for other GNU distributions with internationalized messages. */
113 #if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
114 # include <libintl.h>
115 # ifdef _LIBC
116 # undef gettext
117 # define gettext(msgid) \
118 __dcgettext (_libc_intl_domainname, msgid, LC_MESSAGES)
119 # endif
120 #else
121 # undef gettext
122 # define gettext(msgid) (msgid)
123 #endif
125 #ifndef gettext_noop
126 /* This define is so xgettext can find the internationalizable
127 strings. */
128 # define gettext_noop(String) String
129 #endif
131 #if (defined MB_CUR_MAX && HAVE_WCTYPE_H && HAVE_ISWCTYPE) || _LIBC
132 # define RE_ENABLE_I18N
133 #endif
135 #define BE(expr, val) __builtin_expect (expr, val)
137 /* Number of ASCII characters. */
138 #define ASCII_CHARS 0x80
140 /* Number of single byte characters. */
141 #define SBC_MAX (UCHAR_MAX + 1)
143 #define COLL_ELEM_LEN_MAX 8
145 /* The character which represents newline. */
146 #define NEWLINE_CHAR '\n'
147 #define WIDE_NEWLINE_CHAR L'\n'
149 /* Rename to standard API for using out of glibc. */
150 #ifndef _LIBC
151 # undef __wctype
152 # undef __iswalnum
153 # undef __iswctype
154 # undef __towlower
155 # undef __towupper
156 # define __wctype wctype
157 # define __iswalnum iswalnum
158 # define __iswctype iswctype
159 # define __towlower towlower
160 # define __towupper towupper
161 # define __btowc btowc
162 # define __mbrtowc mbrtowc
163 # define __wcrtomb wcrtomb
164 # define __regfree regfree
165 # define attribute_hidden
166 #endif /* not _LIBC */
168 #if __GNUC__ < 3 + (__GNUC_MINOR__ < 1)
169 # define __attribute__(arg)
170 #endif
172 #ifndef SSIZE_MAX
173 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
174 #endif
176 /* The type of indexes into strings. This is signed, not size_t,
177 since the API requires indexes to fit in regoff_t anyway, and using
178 signed integers makes the code a bit smaller and presumably faster.
179 The traditional GNU regex implementation uses int for indexes.
180 The POSIX-compatible implementation uses a possibly-wider type.
181 The name 'Idx' is three letters to minimize the hassle of
182 reindenting a lot of regex code that formerly used 'int'. */
183 typedef regoff_t Idx;
184 #ifdef _REGEX_LARGE_OFFSETS
185 # define IDX_MAX SSIZE_MAX
186 #else
187 # define IDX_MAX INT_MAX
188 #endif
190 /* A hash value, suitable for computing hash tables. */
191 typedef __re_size_t re_hashval_t;
193 /* An integer used to represent a set of bits. It must be unsigned,
194 and must be at least as wide as unsigned int. */
195 typedef unsigned long int bitset_word_t;
196 /* All bits set in a bitset_word_t. */
197 #define BITSET_WORD_MAX ULONG_MAX
199 /* Number of bits in a bitset_word_t. For portability to hosts with
200 padding bits, do not use '(sizeof (bitset_word_t) * CHAR_BIT)';
201 instead, deduce it directly from BITSET_WORD_MAX. Avoid
202 greater-than-32-bit integers and unconditional shifts by more than
203 31 bits, as they're not portable. */
204 #if BITSET_WORD_MAX == 0xffffffffUL
205 # define BITSET_WORD_BITS 32
206 #elif BITSET_WORD_MAX >> 31 >> 4 == 1
207 # define BITSET_WORD_BITS 36
208 #elif BITSET_WORD_MAX >> 31 >> 16 == 1
209 # define BITSET_WORD_BITS 48
210 #elif BITSET_WORD_MAX >> 31 >> 28 == 1
211 # define BITSET_WORD_BITS 60
212 #elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1
213 # define BITSET_WORD_BITS 64
214 #elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1
215 # define BITSET_WORD_BITS 72
216 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1
217 # define BITSET_WORD_BITS 128
218 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1
219 # define BITSET_WORD_BITS 256
220 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1
221 # define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */
222 # if BITSET_WORD_BITS <= SBC_MAX
223 # error "Invalid SBC_MAX"
224 # endif
225 #else
226 # error "Add case for new bitset_word_t size"
227 #endif
229 /* Number of bitset_word_t values in a bitset_t. */
230 #define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
232 typedef bitset_word_t bitset_t[BITSET_WORDS];
233 typedef bitset_word_t *re_bitset_ptr_t;
234 typedef const bitset_word_t *re_const_bitset_ptr_t;
236 #define PREV_WORD_CONSTRAINT 0x0001
237 #define PREV_NOTWORD_CONSTRAINT 0x0002
238 #define NEXT_WORD_CONSTRAINT 0x0004
239 #define NEXT_NOTWORD_CONSTRAINT 0x0008
240 #define PREV_NEWLINE_CONSTRAINT 0x0010
241 #define NEXT_NEWLINE_CONSTRAINT 0x0020
242 #define PREV_BEGBUF_CONSTRAINT 0x0040
243 #define NEXT_ENDBUF_CONSTRAINT 0x0080
244 #define WORD_DELIM_CONSTRAINT 0x0100
245 #define NOT_WORD_DELIM_CONSTRAINT 0x0200
247 typedef enum
249 INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
250 WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
251 WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
252 INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
253 LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
254 LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
255 BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
256 BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
257 WORD_DELIM = WORD_DELIM_CONSTRAINT,
258 NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
259 } re_context_type;
261 typedef struct
263 Idx alloc;
264 Idx nelem;
265 Idx *elems;
266 } re_node_set;
268 typedef enum
270 NON_TYPE = 0,
272 /* Node type, These are used by token, node, tree. */
273 CHARACTER = 1,
274 END_OF_RE = 2,
275 SIMPLE_BRACKET = 3,
276 OP_BACK_REF = 4,
277 OP_PERIOD = 5,
278 #ifdef RE_ENABLE_I18N
279 COMPLEX_BRACKET = 6,
280 OP_UTF8_PERIOD = 7,
281 #endif /* RE_ENABLE_I18N */
283 /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
284 when the debugger shows values of this enum type. */
285 #define EPSILON_BIT 8
286 OP_OPEN_SUBEXP = EPSILON_BIT | 0,
287 OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
288 OP_ALT = EPSILON_BIT | 2,
289 OP_DUP_ASTERISK = EPSILON_BIT | 3,
290 ANCHOR = EPSILON_BIT | 4,
292 /* Tree type, these are used only by tree. */
293 CONCAT = 16,
294 SUBEXP = 17,
296 /* Token type, these are used only by token. */
297 OP_DUP_PLUS = 18,
298 OP_DUP_QUESTION,
299 OP_OPEN_BRACKET,
300 OP_CLOSE_BRACKET,
301 OP_CHARSET_RANGE,
302 OP_OPEN_DUP_NUM,
303 OP_CLOSE_DUP_NUM,
304 OP_NON_MATCH_LIST,
305 OP_OPEN_COLL_ELEM,
306 OP_CLOSE_COLL_ELEM,
307 OP_OPEN_EQUIV_CLASS,
308 OP_CLOSE_EQUIV_CLASS,
309 OP_OPEN_CHAR_CLASS,
310 OP_CLOSE_CHAR_CLASS,
311 OP_WORD,
312 OP_NOTWORD,
313 OP_SPACE,
314 OP_NOTSPACE,
315 BACK_SLASH
317 } re_token_type_t;
319 #ifdef RE_ENABLE_I18N
320 typedef struct
322 /* Multibyte characters. */
323 wchar_t *mbchars;
325 /* Collating symbols. */
326 # ifdef _LIBC
327 int32_t *coll_syms;
328 # endif
330 /* Equivalence classes. */
331 # ifdef _LIBC
332 int32_t *equiv_classes;
333 # endif
335 /* Range expressions. */
336 # ifdef _LIBC
337 uint32_t *range_starts;
338 uint32_t *range_ends;
339 # else /* not _LIBC */
340 wchar_t *range_starts;
341 wchar_t *range_ends;
342 # endif /* not _LIBC */
344 /* Character classes. */
345 wctype_t *char_classes;
347 /* If this character set is the non-matching list. */
348 unsigned int non_match : 1;
350 /* # of multibyte characters. */
351 Idx nmbchars;
353 /* # of collating symbols. */
354 Idx ncoll_syms;
356 /* # of equivalence classes. */
357 Idx nequiv_classes;
359 /* # of range expressions. */
360 Idx nranges;
362 /* # of character classes. */
363 Idx nchar_classes;
364 } re_charset_t;
365 #endif /* RE_ENABLE_I18N */
367 typedef struct
369 union
371 unsigned char c; /* for CHARACTER */
372 re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
373 #ifdef RE_ENABLE_I18N
374 re_charset_t *mbcset; /* for COMPLEX_BRACKET */
375 #endif /* RE_ENABLE_I18N */
376 Idx idx; /* for BACK_REF */
377 re_context_type ctx_type; /* for ANCHOR */
378 } opr;
379 #if __GNUC__ >= 2 && !defined __STRICT_ANSI__
380 re_token_type_t type : 8;
381 #else
382 re_token_type_t type;
383 #endif
384 unsigned int constraint : 10; /* context constraint */
385 unsigned int duplicated : 1;
386 unsigned int opt_subexp : 1;
387 #ifdef RE_ENABLE_I18N
388 unsigned int accept_mb : 1;
389 /* These 2 bits can be moved into the union if needed (e.g. if running out
390 of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
391 unsigned int mb_partial : 1;
392 #endif
393 unsigned int word_char : 1;
394 } re_token_t;
396 #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
398 struct re_string_t
400 /* Indicate the raw buffer which is the original string passed as an
401 argument of regexec(), re_search(), etc.. */
402 const unsigned char *raw_mbs;
403 /* Store the multibyte string. In case of "case insensitive mode" like
404 REG_ICASE, upper cases of the string are stored, otherwise MBS points
405 the same address that RAW_MBS points. */
406 unsigned char *mbs;
407 #ifdef RE_ENABLE_I18N
408 /* Store the wide character string which is corresponding to MBS. */
409 wint_t *wcs;
410 Idx *offsets;
411 mbstate_t cur_state;
412 #endif
413 /* Index in RAW_MBS. Each character mbs[i] corresponds to
414 raw_mbs[raw_mbs_idx + i]. */
415 Idx raw_mbs_idx;
416 /* The length of the valid characters in the buffers. */
417 Idx valid_len;
418 /* The corresponding number of bytes in raw_mbs array. */
419 Idx valid_raw_len;
420 /* The length of the buffers MBS and WCS. */
421 Idx bufs_len;
422 /* The index in MBS, which is updated by re_string_fetch_byte. */
423 Idx cur_idx;
424 /* length of RAW_MBS array. */
425 Idx raw_len;
426 /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
427 Idx len;
428 /* End of the buffer may be shorter than its length in the cases such
429 as re_match_2, re_search_2. Then, we use STOP for end of the buffer
430 instead of LEN. */
431 Idx raw_stop;
432 /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
433 Idx stop;
435 /* The context of mbs[0]. We store the context independently, since
436 the context of mbs[0] may be different from raw_mbs[0], which is
437 the beginning of the input string. */
438 unsigned int tip_context;
439 /* The translation passed as a part of an argument of re_compile_pattern. */
440 RE_TRANSLATE_TYPE trans;
441 /* Copy of re_dfa_t's word_char. */
442 re_const_bitset_ptr_t word_char;
443 /* true if REG_ICASE. */
444 unsigned char icase;
445 unsigned char is_utf8;
446 unsigned char map_notascii;
447 unsigned char mbs_allocated;
448 unsigned char offsets_needed;
449 unsigned char newline_anchor;
450 unsigned char word_ops_used;
451 int mb_cur_max;
453 typedef struct re_string_t re_string_t;
456 struct re_dfa_t;
457 typedef struct re_dfa_t re_dfa_t;
459 #ifndef _LIBC
460 # define IS_IN(libc) false
461 #endif
463 #define re_string_peek_byte(pstr, offset) \
464 ((pstr)->mbs[(pstr)->cur_idx + offset])
465 #define re_string_fetch_byte(pstr) \
466 ((pstr)->mbs[(pstr)->cur_idx++])
467 #define re_string_first_byte(pstr, idx) \
468 ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
469 #define re_string_is_single_byte_char(pstr, idx) \
470 ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
471 || (pstr)->wcs[(idx) + 1] != WEOF))
472 #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
473 #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
474 #define re_string_get_buffer(pstr) ((pstr)->mbs)
475 #define re_string_length(pstr) ((pstr)->len)
476 #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
477 #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
478 #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
480 #if defined _LIBC || HAVE_ALLOCA
481 # include <alloca.h>
482 #endif
484 #ifndef _LIBC
485 # if HAVE_ALLOCA
486 /* The OS usually guarantees only one guard page at the bottom of the stack,
487 and a page size can be as small as 4096 bytes. So we cannot safely
488 allocate anything larger than 4096 bytes. Also care for the possibility
489 of a few compiler-allocated temporary stack slots. */
490 # define __libc_use_alloca(n) ((n) < 4032)
491 # else
492 /* alloca is implemented with malloc, so just use malloc. */
493 # define __libc_use_alloca(n) 0
494 # undef alloca
495 # define alloca(n) malloc (n)
496 # endif
497 #endif
499 #ifdef _LIBC
500 # define MALLOC_0_IS_NONNULL 1
501 #elif !defined MALLOC_0_IS_NONNULL
502 # define MALLOC_0_IS_NONNULL 0
503 #endif
505 #ifndef MAX
506 # define MAX(a,b) ((a) < (b) ? (b) : (a))
507 #endif
508 #ifndef MIN
509 # define MIN(a,b) ((a) < (b) ? (a) : (b))
510 #endif
512 #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
513 #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
514 #define re_free(p) free (p)
516 struct bin_tree_t
518 struct bin_tree_t *parent;
519 struct bin_tree_t *left;
520 struct bin_tree_t *right;
521 struct bin_tree_t *first;
522 struct bin_tree_t *next;
524 re_token_t token;
526 /* 'node_idx' is the index in dfa->nodes, if 'type' == 0.
527 Otherwise 'type' indicate the type of this node. */
528 Idx node_idx;
530 typedef struct bin_tree_t bin_tree_t;
532 #define BIN_TREE_STORAGE_SIZE \
533 ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
535 struct bin_tree_storage_t
537 struct bin_tree_storage_t *next;
538 bin_tree_t data[BIN_TREE_STORAGE_SIZE];
540 typedef struct bin_tree_storage_t bin_tree_storage_t;
542 #define CONTEXT_WORD 1
543 #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
544 #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
545 #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
547 #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
548 #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
549 #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
550 #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
551 #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
553 #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
554 #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
555 #define IS_WIDE_WORD_CHAR(ch) (__iswalnum (ch) || (ch) == L'_')
556 #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
558 #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
559 ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
560 || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
561 || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
562 || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
564 #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
565 ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
566 || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
567 || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
568 || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
570 struct re_dfastate_t
572 re_hashval_t hash;
573 re_node_set nodes;
574 re_node_set non_eps_nodes;
575 re_node_set inveclosure;
576 re_node_set *entrance_nodes;
577 struct re_dfastate_t **trtable, **word_trtable;
578 unsigned int context : 4;
579 unsigned int halt : 1;
580 /* If this state can accept "multi byte".
581 Note that we refer to multibyte characters, and multi character
582 collating elements as "multi byte". */
583 unsigned int accept_mb : 1;
584 /* If this state has backreference node(s). */
585 unsigned int has_backref : 1;
586 unsigned int has_constraint : 1;
588 typedef struct re_dfastate_t re_dfastate_t;
590 struct re_state_table_entry
592 Idx num;
593 Idx alloc;
594 re_dfastate_t **array;
597 /* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
599 typedef struct
601 Idx next_idx;
602 Idx alloc;
603 re_dfastate_t **array;
604 } state_array_t;
606 /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
608 typedef struct
610 Idx node;
611 Idx str_idx; /* The position NODE match at. */
612 state_array_t path;
613 } re_sub_match_last_t;
615 /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
616 And information about the node, whose type is OP_CLOSE_SUBEXP,
617 corresponding to NODE is stored in LASTS. */
619 typedef struct
621 Idx str_idx;
622 Idx node;
623 state_array_t *path;
624 Idx alasts; /* Allocation size of LASTS. */
625 Idx nlasts; /* The number of LASTS. */
626 re_sub_match_last_t **lasts;
627 } re_sub_match_top_t;
629 struct re_backref_cache_entry
631 Idx node;
632 Idx str_idx;
633 Idx subexp_from;
634 Idx subexp_to;
635 char more;
636 char unused;
637 unsigned short int eps_reachable_subexps_map;
640 typedef struct
642 /* The string object corresponding to the input string. */
643 re_string_t input;
644 #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
645 const re_dfa_t *const dfa;
646 #else
647 const re_dfa_t *dfa;
648 #endif
649 /* EFLAGS of the argument of regexec. */
650 int eflags;
651 /* Where the matching ends. */
652 Idx match_last;
653 Idx last_node;
654 /* The state log used by the matcher. */
655 re_dfastate_t **state_log;
656 Idx state_log_top;
657 /* Back reference cache. */
658 Idx nbkref_ents;
659 Idx abkref_ents;
660 struct re_backref_cache_entry *bkref_ents;
661 int max_mb_elem_len;
662 Idx nsub_tops;
663 Idx asub_tops;
664 re_sub_match_top_t **sub_tops;
665 } re_match_context_t;
667 typedef struct
669 re_dfastate_t **sifted_states;
670 re_dfastate_t **limited_states;
671 Idx last_node;
672 Idx last_str_idx;
673 re_node_set limits;
674 } re_sift_context_t;
676 struct re_fail_stack_ent_t
678 Idx idx;
679 Idx node;
680 regmatch_t *regs;
681 re_node_set eps_via_nodes;
684 struct re_fail_stack_t
686 Idx num;
687 Idx alloc;
688 struct re_fail_stack_ent_t *stack;
691 struct re_dfa_t
693 re_token_t *nodes;
694 size_t nodes_alloc;
695 size_t nodes_len;
696 Idx *nexts;
697 Idx *org_indices;
698 re_node_set *edests;
699 re_node_set *eclosures;
700 re_node_set *inveclosures;
701 struct re_state_table_entry *state_table;
702 re_dfastate_t *init_state;
703 re_dfastate_t *init_state_word;
704 re_dfastate_t *init_state_nl;
705 re_dfastate_t *init_state_begbuf;
706 bin_tree_t *str_tree;
707 bin_tree_storage_t *str_tree_storage;
708 re_bitset_ptr_t sb_char;
709 int str_tree_storage_idx;
711 /* number of subexpressions 're_nsub' is in regex_t. */
712 re_hashval_t state_hash_mask;
713 Idx init_node;
714 Idx nbackref; /* The number of backreference in this dfa. */
716 /* Bitmap expressing which backreference is used. */
717 bitset_word_t used_bkref_map;
718 bitset_word_t completed_bkref_map;
720 unsigned int has_plural_match : 1;
721 /* If this dfa has "multibyte node", which is a backreference or
722 a node which can accept multibyte character or multi character
723 collating element. */
724 unsigned int has_mb_node : 1;
725 unsigned int is_utf8 : 1;
726 unsigned int map_notascii : 1;
727 unsigned int word_ops_used : 1;
728 int mb_cur_max;
729 bitset_t word_char;
730 reg_syntax_t syntax;
731 Idx *subexp_map;
732 #ifdef DEBUG
733 char* re_str;
734 #endif
735 lock_define (lock)
738 #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
739 #define re_node_set_remove(set,id) \
740 (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
741 #define re_node_set_empty(p) ((p)->nelem = 0)
742 #define re_node_set_free(set) re_free ((set)->elems)
745 typedef enum
747 SB_CHAR,
748 MB_CHAR,
749 EQUIV_CLASS,
750 COLL_SYM,
751 CHAR_CLASS
752 } bracket_elem_type;
754 typedef struct
756 bracket_elem_type type;
757 union
759 unsigned char ch;
760 unsigned char *name;
761 wchar_t wch;
762 } opr;
763 } bracket_elem_t;
766 /* Functions for bitset_t operation. */
768 static inline void
769 bitset_set (bitset_t set, Idx i)
771 set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS;
774 static inline void
775 bitset_clear (bitset_t set, Idx i)
777 set[i / BITSET_WORD_BITS] &= ~ ((bitset_word_t) 1 << i % BITSET_WORD_BITS);
780 static inline bool
781 bitset_contain (const bitset_t set, Idx i)
783 return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
786 static inline void
787 bitset_empty (bitset_t set)
789 memset (set, '\0', sizeof (bitset_t));
792 static inline void
793 bitset_set_all (bitset_t set)
795 memset (set, -1, sizeof (bitset_word_t) * (SBC_MAX / BITSET_WORD_BITS));
796 if (SBC_MAX % BITSET_WORD_BITS != 0)
797 set[BITSET_WORDS - 1] =
798 ((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
801 static inline void
802 bitset_copy (bitset_t dest, const bitset_t src)
804 memcpy (dest, src, sizeof (bitset_t));
807 static inline void
808 bitset_not (bitset_t set)
810 int bitset_i;
811 for (bitset_i = 0; bitset_i < SBC_MAX / BITSET_WORD_BITS; ++bitset_i)
812 set[bitset_i] = ~set[bitset_i];
813 if (SBC_MAX % BITSET_WORD_BITS != 0)
814 set[BITSET_WORDS - 1] =
815 ((((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
816 & ~set[BITSET_WORDS - 1]);
819 static inline void
820 bitset_merge (bitset_t dest, const bitset_t src)
822 int bitset_i;
823 for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
824 dest[bitset_i] |= src[bitset_i];
827 static inline void
828 bitset_mask (bitset_t dest, const bitset_t src)
830 int bitset_i;
831 for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
832 dest[bitset_i] &= src[bitset_i];
835 #ifdef RE_ENABLE_I18N
836 /* Functions for re_string. */
837 static int
838 __attribute__ ((pure, unused))
839 re_string_char_size_at (const re_string_t *pstr, Idx idx)
841 int byte_idx;
842 if (pstr->mb_cur_max == 1)
843 return 1;
844 for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
845 if (pstr->wcs[idx + byte_idx] != WEOF)
846 break;
847 return byte_idx;
850 static wint_t
851 __attribute__ ((pure, unused))
852 re_string_wchar_at (const re_string_t *pstr, Idx idx)
854 if (pstr->mb_cur_max == 1)
855 return (wint_t) pstr->mbs[idx];
856 return (wint_t) pstr->wcs[idx];
859 # ifdef _LIBC
860 # include <locale/weight.h>
861 # endif
863 static int
864 __attribute__ ((pure, unused))
865 re_string_elem_size_at (const re_string_t *pstr, Idx idx)
867 # ifdef _LIBC
868 const unsigned char *p, *extra;
869 const int32_t *table, *indirect;
870 uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
872 if (nrules != 0)
874 table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
875 extra = (const unsigned char *)
876 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
877 indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
878 _NL_COLLATE_INDIRECTMB);
879 p = pstr->mbs + idx;
880 findidx (table, indirect, extra, &p, pstr->len - idx);
881 return p - pstr->mbs - idx;
883 else
884 # endif /* _LIBC */
885 return 1;
887 #endif /* RE_ENABLE_I18N */
889 #ifndef __GNUC_PREREQ
890 # if defined __GNUC__ && defined __GNUC_MINOR__
891 # define __GNUC_PREREQ(maj, min) \
892 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
893 # else
894 # define __GNUC_PREREQ(maj, min) 0
895 # endif
896 #endif
898 #if __GNUC_PREREQ (3,4)
899 # undef __attribute_warn_unused_result__
900 # define __attribute_warn_unused_result__ \
901 __attribute__ ((__warn_unused_result__))
902 #else
903 # define __attribute_warn_unused_result__ /* empty */
904 #endif
906 #ifndef FALLTHROUGH
907 # if __GNUC__ < 7
908 # define FALLTHROUGH ((void) 0)
909 # else
910 # define FALLTHROUGH __attribute__ ((__fallthrough__))
911 # endif
912 #endif
914 #endif /* _REGEX_INTERNAL_H */