VirtualBox

source: kBuild/trunk/src/grep/lib/regex_internal.h@ 3598

Last change on this file since 3598 was 3532, checked in by bird, 3 years ago

grep: Initial windows config and adjustments.

  • Property svn:eol-style set to native
File size: 23.1 KB
Line 
1/* Extended regular expression matching and search library.
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Isamu Hasegawa <[email protected]>.
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 <https://www.gnu.org/licenses/>. */
19
20#ifndef _REGEX_INTERNAL_H
21#define _REGEX_INTERNAL_H 1
22
23#include <ctype.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#if !defined(KMK_GREP) || !defined(_MSC_VER)
29#include <langinfo.h>
30#endif
31#include <locale.h>
32#include <wchar.h>
33#include <wctype.h>
34#include <stdbool.h>
35#include <stdint.h>
36
37#ifndef _LIBC
38# include <dynarray.h>
39#endif
40
41#include <intprops.h>
42#include <verify.h>
43
44#if defined DEBUG && DEBUG != 0
45# include <assert.h>
46# define DEBUG_ASSERT(x) assert (x)
47#else
48# define DEBUG_ASSERT(x) assume (x)
49#endif
50
51#ifdef _LIBC
52# include <libc-lock.h>
53# define lock_define(name) __libc_lock_define (, name)
54# define lock_init(lock) (__libc_lock_init (lock), 0)
55# define lock_fini(lock) ((void) 0)
56# define lock_lock(lock) __libc_lock_lock (lock)
57# define lock_unlock(lock) __libc_lock_unlock (lock)
58#elif defined GNULIB_LOCK && !defined GNULIB_REGEX_SINGLE_THREAD
59# include "glthread/lock.h"
60# define lock_define(name) gl_lock_define (, name)
61# define lock_init(lock) glthread_lock_init (&(lock))
62# define lock_fini(lock) glthread_lock_destroy (&(lock))
63# define lock_lock(lock) glthread_lock_lock (&(lock))
64# define lock_unlock(lock) glthread_lock_unlock (&(lock))
65#elif defined GNULIB_PTHREAD && !defined GNULIB_REGEX_SINGLE_THREAD
66# include <pthread.h>
67# define lock_define(name) pthread_mutex_t name;
68# define lock_init(lock) pthread_mutex_init (&(lock), 0)
69# define lock_fini(lock) pthread_mutex_destroy (&(lock))
70# define lock_lock(lock) pthread_mutex_lock (&(lock))
71# define lock_unlock(lock) pthread_mutex_unlock (&(lock))
72#else
73# define lock_define(name)
74# define lock_init(lock) 0
75# define lock_fini(lock) ((void) 0)
76 /* The 'dfa' avoids an "unused variable 'dfa'" warning from GCC. */
77# define lock_lock(lock) ((void) dfa)
78# define lock_unlock(lock) ((void) 0)
79#endif
80
81/* In case that the system doesn't have isblank(). */
82#if !defined _LIBC && ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
83# define isblank(ch) ((ch) == ' ' || (ch) == '\t')
84#endif
85
86/* regex code assumes isascii has its usual numeric meaning,
87 even if the portable character set uses EBCDIC encoding,
88 and even if wint_t is wider than int. */
89#ifndef _LIBC
90# undef isascii
91# define isascii(c) (((c) & ~0x7f) == 0)
92#endif
93
94#ifdef _LIBC
95# ifndef _RE_DEFINE_LOCALE_FUNCTIONS
96# define _RE_DEFINE_LOCALE_FUNCTIONS 1
97# include <locale/localeinfo.h>
98# include <locale/coll-lookup.h>
99# endif
100#endif
101
102/* This is for other GNU distributions with internationalized messages. */
103#if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
104# include <libintl.h>
105# ifdef _LIBC
106# undef gettext
107# define gettext(msgid) \
108 __dcgettext (_libc_intl_domainname, msgid, LC_MESSAGES)
109# endif
110#else
111# undef gettext
112# define gettext(msgid) (msgid)
113#endif
114
115#ifndef gettext_noop
116/* This define is so xgettext can find the internationalizable
117 strings. */
118# define gettext_noop(String) String
119#endif
120
121#if (defined MB_CUR_MAX && HAVE_WCTYPE_H && HAVE_ISWCTYPE) || _LIBC
122# define RE_ENABLE_I18N
123#endif
124
125/* Number of ASCII characters. */
126#define ASCII_CHARS 0x80
127
128/* Number of single byte characters. */
129#define SBC_MAX (UCHAR_MAX + 1)
130
131#define COLL_ELEM_LEN_MAX 8
132
133/* The character which represents newline. */
134#define NEWLINE_CHAR '\n'
135#define WIDE_NEWLINE_CHAR L'\n'
136
137/* Rename to standard API for using out of glibc. */
138#ifndef _LIBC
139# undef __wctype
140# undef __iswalnum
141# undef __iswctype
142# undef __towlower
143# undef __towupper
144# define __wctype wctype
145# define __iswalnum iswalnum
146# define __iswctype iswctype
147# define __towlower towlower
148# define __towupper towupper
149# define __btowc btowc
150# define __mbrtowc mbrtowc
151# define __wcrtomb wcrtomb
152# define __regfree regfree
153#endif /* not _LIBC */
154
155#ifndef SSIZE_MAX
156# define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
157#endif
158#ifndef ULONG_WIDTH
159# define ULONG_WIDTH REGEX_UINTEGER_WIDTH (ULONG_MAX)
160/* The number of usable bits in an unsigned integer type with maximum
161 value MAX, as an int expression suitable in #if. Cover all known
162 practical hosts. This implementation exploits the fact that MAX is
163 1 less than a power of 2, and merely counts the number of 1 bits in
164 MAX; "COBn" means "count the number of 1 bits in the low-order n bits". */
165# define REGEX_UINTEGER_WIDTH(max) REGEX_COB128 (max)
166# define REGEX_COB128(n) (REGEX_COB64 ((n) >> 31 >> 31 >> 2) + REGEX_COB64 (n))
167# define REGEX_COB64(n) (REGEX_COB32 ((n) >> 31 >> 1) + REGEX_COB32 (n))
168# define REGEX_COB32(n) (REGEX_COB16 ((n) >> 16) + REGEX_COB16 (n))
169# define REGEX_COB16(n) (REGEX_COB8 ((n) >> 8) + REGEX_COB8 (n))
170# define REGEX_COB8(n) (REGEX_COB4 ((n) >> 4) + REGEX_COB4 (n))
171# define REGEX_COB4(n) (!!((n) & 8) + !!((n) & 4) + !!((n) & 2) + ((n) & 1))
172# if ULONG_MAX / 2 + 1 != 1ul << (ULONG_WIDTH - 1)
173# error "ULONG_MAX out of range"
174# endif
175#endif
176
177/* The type of indexes into strings. This is signed, not size_t,
178 since the API requires indexes to fit in regoff_t anyway, and using
179 signed integers makes the code a bit smaller and presumably faster.
180 The traditional GNU regex implementation uses int for indexes.
181 The POSIX-compatible implementation uses a possibly-wider type.
182 The name 'Idx' is three letters to minimize the hassle of
183 reindenting a lot of regex code that formerly used 'int'. */
184typedef regoff_t Idx;
185#ifdef _REGEX_LARGE_OFFSETS
186# define IDX_MAX SSIZE_MAX
187#else
188# define IDX_MAX INT_MAX
189#endif
190
191/* A hash value, suitable for computing hash tables. */
192typedef __re_size_t re_hashval_t;
193
194/* An integer used to represent a set of bits. It must be unsigned,
195 and must be at least as wide as unsigned int. */
196typedef unsigned long int bitset_word_t;
197/* All bits set in a bitset_word_t. */
198#define BITSET_WORD_MAX ULONG_MAX
199/* Number of bits in a bitset_word_t. */
200#define BITSET_WORD_BITS ULONG_WIDTH
201
202/* Number of bitset_word_t values in a bitset_t. */
203#define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
204
205typedef bitset_word_t bitset_t[BITSET_WORDS];
206typedef bitset_word_t *re_bitset_ptr_t;
207typedef const bitset_word_t *re_const_bitset_ptr_t;
208
209#define PREV_WORD_CONSTRAINT 0x0001
210#define PREV_NOTWORD_CONSTRAINT 0x0002
211#define NEXT_WORD_CONSTRAINT 0x0004
212#define NEXT_NOTWORD_CONSTRAINT 0x0008
213#define PREV_NEWLINE_CONSTRAINT 0x0010
214#define NEXT_NEWLINE_CONSTRAINT 0x0020
215#define PREV_BEGBUF_CONSTRAINT 0x0040
216#define NEXT_ENDBUF_CONSTRAINT 0x0080
217#define WORD_DELIM_CONSTRAINT 0x0100
218#define NOT_WORD_DELIM_CONSTRAINT 0x0200
219
220typedef enum
221{
222 INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
223 WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
224 WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
225 INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
226 LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
227 LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
228 BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
229 BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
230 WORD_DELIM = WORD_DELIM_CONSTRAINT,
231 NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
232} re_context_type;
233
234typedef struct
235{
236 Idx alloc;
237 Idx nelem;
238 Idx *elems;
239} re_node_set;
240
241typedef enum
242{
243 NON_TYPE = 0,
244
245 /* Node type, These are used by token, node, tree. */
246 CHARACTER = 1,
247 END_OF_RE = 2,
248 SIMPLE_BRACKET = 3,
249 OP_BACK_REF = 4,
250 OP_PERIOD = 5,
251#ifdef RE_ENABLE_I18N
252 COMPLEX_BRACKET = 6,
253 OP_UTF8_PERIOD = 7,
254#endif /* RE_ENABLE_I18N */
255
256 /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
257 when the debugger shows values of this enum type. */
258#define EPSILON_BIT 8
259 OP_OPEN_SUBEXP = EPSILON_BIT | 0,
260 OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
261 OP_ALT = EPSILON_BIT | 2,
262 OP_DUP_ASTERISK = EPSILON_BIT | 3,
263 ANCHOR = EPSILON_BIT | 4,
264
265 /* Tree type, these are used only by tree. */
266 CONCAT = 16,
267 SUBEXP = 17,
268
269 /* Token type, these are used only by token. */
270 OP_DUP_PLUS = 18,
271 OP_DUP_QUESTION,
272 OP_OPEN_BRACKET,
273 OP_CLOSE_BRACKET,
274 OP_CHARSET_RANGE,
275 OP_OPEN_DUP_NUM,
276 OP_CLOSE_DUP_NUM,
277 OP_NON_MATCH_LIST,
278 OP_OPEN_COLL_ELEM,
279 OP_CLOSE_COLL_ELEM,
280 OP_OPEN_EQUIV_CLASS,
281 OP_CLOSE_EQUIV_CLASS,
282 OP_OPEN_CHAR_CLASS,
283 OP_CLOSE_CHAR_CLASS,
284 OP_WORD,
285 OP_NOTWORD,
286 OP_SPACE,
287 OP_NOTSPACE,
288 BACK_SLASH
289
290} re_token_type_t;
291
292#ifdef RE_ENABLE_I18N
293typedef struct
294{
295 /* Multibyte characters. */
296 wchar_t *mbchars;
297
298 /* Collating symbols. */
299# ifdef _LIBC
300 int32_t *coll_syms;
301# endif
302
303 /* Equivalence classes. */
304# ifdef _LIBC
305 int32_t *equiv_classes;
306# endif
307
308 /* Range expressions. */
309# ifdef _LIBC
310 uint32_t *range_starts;
311 uint32_t *range_ends;
312# else /* not _LIBC */
313 wchar_t *range_starts;
314 wchar_t *range_ends;
315# endif /* not _LIBC */
316
317 /* Character classes. */
318 wctype_t *char_classes;
319
320 /* If this character set is the non-matching list. */
321 unsigned int non_match : 1;
322
323 /* # of multibyte characters. */
324 Idx nmbchars;
325
326 /* # of collating symbols. */
327 Idx ncoll_syms;
328
329 /* # of equivalence classes. */
330 Idx nequiv_classes;
331
332 /* # of range expressions. */
333 Idx nranges;
334
335 /* # of character classes. */
336 Idx nchar_classes;
337} re_charset_t;
338#endif /* RE_ENABLE_I18N */
339
340typedef struct
341{
342 union
343 {
344 unsigned char c; /* for CHARACTER */
345 re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
346#ifdef RE_ENABLE_I18N
347 re_charset_t *mbcset; /* for COMPLEX_BRACKET */
348#endif /* RE_ENABLE_I18N */
349 Idx idx; /* for BACK_REF */
350 re_context_type ctx_type; /* for ANCHOR */
351 } opr;
352#if (__GNUC__ >= 2 || defined __clang__) && !defined __STRICT_ANSI__
353 re_token_type_t type : 8;
354#else
355 re_token_type_t type;
356#endif
357 unsigned int constraint : 10; /* context constraint */
358 unsigned int duplicated : 1;
359 unsigned int opt_subexp : 1;
360#ifdef RE_ENABLE_I18N
361 unsigned int accept_mb : 1;
362 /* These 2 bits can be moved into the union if needed (e.g. if running out
363 of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
364 unsigned int mb_partial : 1;
365#endif
366 unsigned int word_char : 1;
367} re_token_t;
368
369#define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
370
371struct re_string_t
372{
373 /* Indicate the raw buffer which is the original string passed as an
374 argument of regexec(), re_search(), etc.. */
375 const unsigned char *raw_mbs;
376 /* Store the multibyte string. In case of "case insensitive mode" like
377 REG_ICASE, upper cases of the string are stored, otherwise MBS points
378 the same address that RAW_MBS points. */
379 unsigned char *mbs;
380#ifdef RE_ENABLE_I18N
381 /* Store the wide character string which is corresponding to MBS. */
382 wint_t *wcs;
383 Idx *offsets;
384 mbstate_t cur_state;
385#endif
386 /* Index in RAW_MBS. Each character mbs[i] corresponds to
387 raw_mbs[raw_mbs_idx + i]. */
388 Idx raw_mbs_idx;
389 /* The length of the valid characters in the buffers. */
390 Idx valid_len;
391 /* The corresponding number of bytes in raw_mbs array. */
392 Idx valid_raw_len;
393 /* The length of the buffers MBS and WCS. */
394 Idx bufs_len;
395 /* The index in MBS, which is updated by re_string_fetch_byte. */
396 Idx cur_idx;
397 /* length of RAW_MBS array. */
398 Idx raw_len;
399 /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
400 Idx len;
401 /* End of the buffer may be shorter than its length in the cases such
402 as re_match_2, re_search_2. Then, we use STOP for end of the buffer
403 instead of LEN. */
404 Idx raw_stop;
405 /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
406 Idx stop;
407
408 /* The context of mbs[0]. We store the context independently, since
409 the context of mbs[0] may be different from raw_mbs[0], which is
410 the beginning of the input string. */
411 unsigned int tip_context;
412 /* The translation passed as a part of an argument of re_compile_pattern. */
413 RE_TRANSLATE_TYPE trans;
414 /* Copy of re_dfa_t's word_char. */
415 re_const_bitset_ptr_t word_char;
416 /* true if REG_ICASE. */
417 unsigned char icase;
418 unsigned char is_utf8;
419 unsigned char map_notascii;
420 unsigned char mbs_allocated;
421 unsigned char offsets_needed;
422 unsigned char newline_anchor;
423 unsigned char word_ops_used;
424 int mb_cur_max;
425};
426typedef struct re_string_t re_string_t;
427
428
429struct re_dfa_t;
430typedef struct re_dfa_t re_dfa_t;
431
432#ifndef _LIBC
433# define IS_IN(libc) false
434#endif
435
436#define re_string_peek_byte(pstr, offset) \
437 ((pstr)->mbs[(pstr)->cur_idx + offset])
438#define re_string_fetch_byte(pstr) \
439 ((pstr)->mbs[(pstr)->cur_idx++])
440#define re_string_first_byte(pstr, idx) \
441 ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
442#define re_string_is_single_byte_char(pstr, idx) \
443 ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
444 || (pstr)->wcs[(idx) + 1] != WEOF))
445#define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
446#define re_string_cur_idx(pstr) ((pstr)->cur_idx)
447#define re_string_get_buffer(pstr) ((pstr)->mbs)
448#define re_string_length(pstr) ((pstr)->len)
449#define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
450#define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
451#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
452
453#ifdef _LIBC
454# define MALLOC_0_IS_NONNULL 1
455#elif !defined MALLOC_0_IS_NONNULL
456# define MALLOC_0_IS_NONNULL 0
457#endif
458
459#ifndef MAX
460# define MAX(a,b) ((a) < (b) ? (b) : (a))
461#endif
462#ifndef MIN
463# define MIN(a,b) ((a) < (b) ? (a) : (b))
464#endif
465
466#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
467#define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
468#define re_free(p) free (p)
469
470struct bin_tree_t
471{
472 struct bin_tree_t *parent;
473 struct bin_tree_t *left;
474 struct bin_tree_t *right;
475 struct bin_tree_t *first;
476 struct bin_tree_t *next;
477
478 re_token_t token;
479
480 /* 'node_idx' is the index in dfa->nodes, if 'type' == 0.
481 Otherwise 'type' indicate the type of this node. */
482 Idx node_idx;
483};
484typedef struct bin_tree_t bin_tree_t;
485
486#define BIN_TREE_STORAGE_SIZE \
487 ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
488
489struct bin_tree_storage_t
490{
491 struct bin_tree_storage_t *next;
492 bin_tree_t data[BIN_TREE_STORAGE_SIZE];
493};
494typedef struct bin_tree_storage_t bin_tree_storage_t;
495
496#define CONTEXT_WORD 1
497#define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
498#define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
499#define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
500
501#define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
502#define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
503#define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
504#define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
505#define IS_ORDINARY_CONTEXT(c) ((c) == 0)
506
507#define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
508#define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
509#define IS_WIDE_WORD_CHAR(ch) (__iswalnum (ch) || (ch) == L'_')
510#define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
511
512#define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
513 ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
514 || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
515 || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
516 || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
517
518#define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
519 ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
520 || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
521 || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
522 || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
523
524struct re_dfastate_t
525{
526 re_hashval_t hash;
527 re_node_set nodes;
528 re_node_set non_eps_nodes;
529 re_node_set inveclosure;
530 re_node_set *entrance_nodes;
531 struct re_dfastate_t **trtable, **word_trtable;
532 unsigned int context : 4;
533 unsigned int halt : 1;
534 /* If this state can accept "multi byte".
535 Note that we refer to multibyte characters, and multi character
536 collating elements as "multi byte". */
537 unsigned int accept_mb : 1;
538 /* If this state has backreference node(s). */
539 unsigned int has_backref : 1;
540 unsigned int has_constraint : 1;
541};
542typedef struct re_dfastate_t re_dfastate_t;
543
544struct re_state_table_entry
545{
546 Idx num;
547 Idx alloc;
548 re_dfastate_t **array;
549};
550
551/* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
552
553typedef struct
554{
555 Idx next_idx;
556 Idx alloc;
557 re_dfastate_t **array;
558} state_array_t;
559
560/* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
561
562typedef struct
563{
564 Idx node;
565 Idx str_idx; /* The position NODE match at. */
566 state_array_t path;
567} re_sub_match_last_t;
568
569/* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
570 And information about the node, whose type is OP_CLOSE_SUBEXP,
571 corresponding to NODE is stored in LASTS. */
572
573typedef struct
574{
575 Idx str_idx;
576 Idx node;
577 state_array_t *path;
578 Idx alasts; /* Allocation size of LASTS. */
579 Idx nlasts; /* The number of LASTS. */
580 re_sub_match_last_t **lasts;
581} re_sub_match_top_t;
582
583struct re_backref_cache_entry
584{
585 Idx node;
586 Idx str_idx;
587 Idx subexp_from;
588 Idx subexp_to;
589 bitset_word_t eps_reachable_subexps_map;
590 char more;
591};
592
593typedef struct
594{
595 /* The string object corresponding to the input string. */
596 re_string_t input;
597 const re_dfa_t *const dfa;
598 /* EFLAGS of the argument of regexec. */
599 int eflags;
600 /* Where the matching ends. */
601 Idx match_last;
602 Idx last_node;
603 /* The state log used by the matcher. */
604 re_dfastate_t **state_log;
605 Idx state_log_top;
606 /* Back reference cache. */
607 Idx nbkref_ents;
608 Idx abkref_ents;
609 struct re_backref_cache_entry *bkref_ents;
610 int max_mb_elem_len;
611 Idx nsub_tops;
612 Idx asub_tops;
613 re_sub_match_top_t **sub_tops;
614} re_match_context_t;
615
616typedef struct
617{
618 re_dfastate_t **sifted_states;
619 re_dfastate_t **limited_states;
620 Idx last_node;
621 Idx last_str_idx;
622 re_node_set limits;
623} re_sift_context_t;
624
625struct re_fail_stack_ent_t
626{
627 Idx idx;
628 Idx node;
629 regmatch_t *regs;
630 re_node_set eps_via_nodes;
631};
632
633struct re_fail_stack_t
634{
635 Idx num;
636 Idx alloc;
637 struct re_fail_stack_ent_t *stack;
638};
639
640struct re_dfa_t
641{
642 re_token_t *nodes;
643 size_t nodes_alloc;
644 size_t nodes_len;
645 Idx *nexts;
646 Idx *org_indices;
647 re_node_set *edests;
648 re_node_set *eclosures;
649 re_node_set *inveclosures;
650 struct re_state_table_entry *state_table;
651 re_dfastate_t *init_state;
652 re_dfastate_t *init_state_word;
653 re_dfastate_t *init_state_nl;
654 re_dfastate_t *init_state_begbuf;
655 bin_tree_t *str_tree;
656 bin_tree_storage_t *str_tree_storage;
657 re_bitset_ptr_t sb_char;
658 int str_tree_storage_idx;
659
660 /* number of subexpressions 're_nsub' is in regex_t. */
661 re_hashval_t state_hash_mask;
662 Idx init_node;
663 Idx nbackref; /* The number of backreference in this dfa. */
664
665 /* Bitmap expressing which backreference is used. */
666 bitset_word_t used_bkref_map;
667 bitset_word_t completed_bkref_map;
668
669 unsigned int has_plural_match : 1;
670 /* If this dfa has "multibyte node", which is a backreference or
671 a node which can accept multibyte character or multi character
672 collating element. */
673 unsigned int has_mb_node : 1;
674 unsigned int is_utf8 : 1;
675 unsigned int map_notascii : 1;
676 unsigned int word_ops_used : 1;
677 int mb_cur_max;
678 bitset_t word_char;
679 reg_syntax_t syntax;
680 Idx *subexp_map;
681#ifdef DEBUG
682 char* re_str;
683#endif
684 lock_define (lock)
685};
686
687#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
688#define re_node_set_remove(set,id) \
689 (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
690#define re_node_set_empty(p) ((p)->nelem = 0)
691#define re_node_set_free(set) re_free ((set)->elems)
692
693
694
695typedef enum
696{
697 SB_CHAR,
698 MB_CHAR,
699 EQUIV_CLASS,
700 COLL_SYM,
701 CHAR_CLASS
702} bracket_elem_type;
703
704typedef struct
705{
706 bracket_elem_type type;
707 union
708 {
709 unsigned char ch;
710 unsigned char *name;
711 wchar_t wch;
712 } opr;
713} bracket_elem_t;
714
715
716/* Functions for bitset_t operation. */
717
718static inline void
719bitset_set (bitset_t set, Idx i)
720{
721 set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS;
722}
723
724static inline void
725bitset_clear (bitset_t set, Idx i)
726{
727 set[i / BITSET_WORD_BITS] &= ~ ((bitset_word_t) 1 << i % BITSET_WORD_BITS);
728}
729
730static inline bool
731bitset_contain (const bitset_t set, Idx i)
732{
733 return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
734}
735
736static inline void
737bitset_empty (bitset_t set)
738{
739 memset (set, '\0', sizeof (bitset_t));
740}
741
742static inline void
743bitset_set_all (bitset_t set)
744{
745 memset (set, -1, sizeof (bitset_word_t) * (SBC_MAX / BITSET_WORD_BITS));
746 if (SBC_MAX % BITSET_WORD_BITS != 0)
747 set[BITSET_WORDS - 1] =
748 ((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
749}
750
751static inline void
752bitset_copy (bitset_t dest, const bitset_t src)
753{
754 memcpy (dest, src, sizeof (bitset_t));
755}
756
757static inline void
758bitset_not (bitset_t set)
759{
760 int bitset_i;
761 for (bitset_i = 0; bitset_i < SBC_MAX / BITSET_WORD_BITS; ++bitset_i)
762 set[bitset_i] = ~set[bitset_i];
763 if (SBC_MAX % BITSET_WORD_BITS != 0)
764 set[BITSET_WORDS - 1] =
765 ((((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
766 & ~set[BITSET_WORDS - 1]);
767}
768
769static inline void
770bitset_merge (bitset_t dest, const bitset_t src)
771{
772 int bitset_i;
773 for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
774 dest[bitset_i] |= src[bitset_i];
775}
776
777static inline void
778bitset_mask (bitset_t dest, const bitset_t src)
779{
780 int bitset_i;
781 for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
782 dest[bitset_i] &= src[bitset_i];
783}
784
785#ifdef RE_ENABLE_I18N
786/* Functions for re_string. */
787static int
788__attribute__ ((pure, unused))
789re_string_char_size_at (const re_string_t *pstr, Idx idx)
790{
791 int byte_idx;
792 if (pstr->mb_cur_max == 1)
793 return 1;
794 for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
795 if (pstr->wcs[idx + byte_idx] != WEOF)
796 break;
797 return byte_idx;
798}
799
800static wint_t
801__attribute__ ((pure, unused))
802re_string_wchar_at (const re_string_t *pstr, Idx idx)
803{
804 if (pstr->mb_cur_max == 1)
805 return (wint_t) pstr->mbs[idx];
806 return (wint_t) pstr->wcs[idx];
807}
808
809# ifdef _LIBC
810# include <locale/weight.h>
811# endif
812
813static int
814__attribute__ ((pure, unused))
815re_string_elem_size_at (const re_string_t *pstr, Idx idx)
816{
817# ifdef _LIBC
818 const unsigned char *p, *extra;
819 const int32_t *table, *indirect;
820 uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
821
822 if (nrules != 0)
823 {
824 table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
825 extra = (const unsigned char *)
826 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
827 indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
828 _NL_COLLATE_INDIRECTMB);
829 p = pstr->mbs + idx;
830 findidx (table, indirect, extra, &p, pstr->len - idx);
831 return p - pstr->mbs - idx;
832 }
833 else
834# endif /* _LIBC */
835 return 1;
836}
837#endif /* RE_ENABLE_I18N */
838
839#ifdef _LIBC
840# if __GNUC__ >= 7
841# define FALLTHROUGH __attribute__ ((__fallthrough__))
842# else
843# define FALLTHROUGH ((void) 0)
844# endif
845#else
846# include "attribute.h"
847#endif
848
849#endif /* _REGEX_INTERNAL_H */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette