1 /* Extended regular expression matching and search library.
2 Copyright (C) 2002-2016 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 <http://www.gnu.org/licenses/>. */
20 #ifndef _REGEX_INTERNAL_H
21 #define _REGEX_INTERNAL_H 1
37 # include <libc-lock.h>
38 # define lock_define(name) __libc_lock_define (, name)
39 # define lock_init(lock) (__libc_lock_init (lock), 0)
40 # define lock_fini(lock) 0
41 # define lock_lock(lock) __libc_lock_lock (lock)
42 # define lock_unlock(lock) __libc_lock_unlock (lock)
43 #elif defined GNULIB_LOCK && !defined USE_UNLOCKED_IO
44 # include "glthread/lock.h"
45 /* Use gl_lock_define if empty macro arguments are known to work.
46 Otherwise, fall back on less-portable substitutes. */
47 # if ((defined __GNUC__ && !defined __STRICT_ANSI__) \
48 || (defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__))
49 # define lock_define(name) gl_lock_define (, name)
50 # elif USE_POSIX_THREADS
51 # define lock_define(name) pthread_mutex_t name;
52 # elif USE_PTH_THREADS
53 # define lock_define(name) pth_mutex_t name;
54 # elif USE_SOLARIS_THREADS
55 # define lock_define(name) mutex_t name;
56 # elif USE_WINDOWS_THREADS
57 # define lock_define(name) gl_lock_t name;
59 # define 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 USE_UNLOCKED_IO
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))
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)
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')
87 # ifndef _RE_DEFINE_LOCALE_FUNCTIONS
88 # define _RE_DEFINE_LOCALE_FUNCTIONS 1
89 # include <locale/localeinfo.h>
90 # include <locale/coll-lookup.h>
94 /* This is for other GNU distributions with internationalized messages. */
95 #if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
99 # define gettext(msgid) \
100 __dcgettext (_libc_intl_domainname, msgid, LC_MESSAGES)
103 # define gettext(msgid) (msgid)
107 /* This define is so xgettext can find the internationalizable
109 # define gettext_noop(String) String
112 #if (defined MB_CUR_MAX && HAVE_WCTYPE_H && HAVE_ISWCTYPE) || _LIBC
113 # define RE_ENABLE_I18N
117 # define BE(expr, val) __builtin_expect (expr, val)
119 # define BE(expr, val) (expr)
122 /* Number of ASCII characters. */
123 #define ASCII_CHARS 0x80
125 /* Number of single byte characters. */
126 #define SBC_MAX (UCHAR_MAX + 1)
128 #define COLL_ELEM_LEN_MAX 8
130 /* The character which represents newline. */
131 #define NEWLINE_CHAR '\n'
132 #define WIDE_NEWLINE_CHAR L'\n'
134 /* Rename to standard API for using out of glibc. */
138 # define __wctype wctype
139 # define __iswalnum iswalnum
140 # define __iswctype iswctype
141 # define __towlower towlower
142 # define __towupper towupper
143 # define __btowc btowc
144 # define __mbrtowc mbrtowc
145 # define __wcrtomb wcrtomb
146 # define __regfree regfree
147 # define attribute_hidden
148 #endif /* not _LIBC */
150 #if __GNUC__ < 3 + (__GNUC_MINOR__ < 1)
151 # define __attribute__(arg)
155 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
158 /* The type of indexes into strings. This is signed, not size_t,
159 since the API requires indexes to fit in regoff_t anyway, and using
160 signed integers makes the code a bit smaller and presumably faster.
161 The traditional GNU regex implementation uses int for indexes.
162 The POSIX-compatible implementation uses a possibly-wider type.
163 The name 'Idx' is three letters to minimize the hassle of
164 reindenting a lot of regex code that formerly used 'int'. */
165 typedef regoff_t Idx;
166 #ifdef _REGEX_LARGE_OFFSETS
167 # define IDX_MAX SSIZE_MAX
169 # define IDX_MAX INT_MAX
172 /* A hash value, suitable for computing hash tables. */
173 typedef __re_size_t re_hashval_t;
175 /* An integer used to represent a set of bits. It must be unsigned,
176 and must be at least as wide as unsigned int. */
177 typedef unsigned long int bitset_word_t;
178 /* All bits set in a bitset_word_t. */
179 #define BITSET_WORD_MAX ULONG_MAX
181 /* Number of bits in a bitset_word_t. For portability to hosts with
182 padding bits, do not use '(sizeof (bitset_word_t) * CHAR_BIT)';
183 instead, deduce it directly from BITSET_WORD_MAX. Avoid
184 greater-than-32-bit integers and unconditional shifts by more than
185 31 bits, as they're not portable. */
186 #if BITSET_WORD_MAX == 0xffffffffUL
187 # define BITSET_WORD_BITS 32
188 #elif BITSET_WORD_MAX >> 31 >> 4 == 1
189 # define BITSET_WORD_BITS 36
190 #elif BITSET_WORD_MAX >> 31 >> 16 == 1
191 # define BITSET_WORD_BITS 48
192 #elif BITSET_WORD_MAX >> 31 >> 28 == 1
193 # define BITSET_WORD_BITS 60
194 #elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1
195 # define BITSET_WORD_BITS 64
196 #elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1
197 # define BITSET_WORD_BITS 72
198 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1
199 # define BITSET_WORD_BITS 128
200 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1
201 # define BITSET_WORD_BITS 256
202 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1
203 # define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */
204 # if BITSET_WORD_BITS <= SBC_MAX
205 # error "Invalid SBC_MAX"
208 # error "Add case for new bitset_word_t size"
211 /* Number of bitset_word_t values in a bitset_t. */
212 #define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
214 typedef bitset_word_t bitset_t[BITSET_WORDS];
215 typedef bitset_word_t *re_bitset_ptr_t;
216 typedef const bitset_word_t *re_const_bitset_ptr_t;
218 #define PREV_WORD_CONSTRAINT 0x0001
219 #define PREV_NOTWORD_CONSTRAINT 0x0002
220 #define NEXT_WORD_CONSTRAINT 0x0004
221 #define NEXT_NOTWORD_CONSTRAINT 0x0008
222 #define PREV_NEWLINE_CONSTRAINT 0x0010
223 #define NEXT_NEWLINE_CONSTRAINT 0x0020
224 #define PREV_BEGBUF_CONSTRAINT 0x0040
225 #define NEXT_ENDBUF_CONSTRAINT 0x0080
226 #define WORD_DELIM_CONSTRAINT 0x0100
227 #define NOT_WORD_DELIM_CONSTRAINT 0x0200
231 INSIDE_WORD = PREV_WORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
232 WORD_FIRST = PREV_NOTWORD_CONSTRAINT | NEXT_WORD_CONSTRAINT,
233 WORD_LAST = PREV_WORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
234 INSIDE_NOTWORD = PREV_NOTWORD_CONSTRAINT | NEXT_NOTWORD_CONSTRAINT,
235 LINE_FIRST = PREV_NEWLINE_CONSTRAINT,
236 LINE_LAST = NEXT_NEWLINE_CONSTRAINT,
237 BUF_FIRST = PREV_BEGBUF_CONSTRAINT,
238 BUF_LAST = NEXT_ENDBUF_CONSTRAINT,
239 WORD_DELIM = WORD_DELIM_CONSTRAINT,
240 NOT_WORD_DELIM = NOT_WORD_DELIM_CONSTRAINT
254 /* Node type, These are used by token, node, tree. */
260 #ifdef RE_ENABLE_I18N
263 #endif /* RE_ENABLE_I18N */
265 /* We define EPSILON_BIT as a macro so that OP_OPEN_SUBEXP is used
266 when the debugger shows values of this enum type. */
267 #define EPSILON_BIT 8
268 OP_OPEN_SUBEXP = EPSILON_BIT | 0,
269 OP_CLOSE_SUBEXP = EPSILON_BIT | 1,
270 OP_ALT = EPSILON_BIT | 2,
271 OP_DUP_ASTERISK = EPSILON_BIT | 3,
272 ANCHOR = EPSILON_BIT | 4,
274 /* Tree type, these are used only by tree. */
278 /* Token type, these are used only by token. */
290 OP_CLOSE_EQUIV_CLASS,
301 #ifdef RE_ENABLE_I18N
304 /* Multibyte characters. */
307 /* Collating symbols. */
312 /* Equivalence classes. */
314 int32_t *equiv_classes;
317 /* Range expressions. */
319 uint32_t *range_starts;
320 uint32_t *range_ends;
321 # else /* not _LIBC */
322 wchar_t *range_starts;
324 # endif /* not _LIBC */
326 /* Character classes. */
327 wctype_t *char_classes;
329 /* If this character set is the non-matching list. */
330 unsigned int non_match : 1;
332 /* # of multibyte characters. */
335 /* # of collating symbols. */
338 /* # of equivalence classes. */
341 /* # of range expressions. */
344 /* # of character classes. */
347 #endif /* RE_ENABLE_I18N */
353 unsigned char c; /* for CHARACTER */
354 re_bitset_ptr_t sbcset; /* for SIMPLE_BRACKET */
355 #ifdef RE_ENABLE_I18N
356 re_charset_t *mbcset; /* for COMPLEX_BRACKET */
357 #endif /* RE_ENABLE_I18N */
358 Idx idx; /* for BACK_REF */
359 re_context_type ctx_type; /* for ANCHOR */
361 #if __GNUC__ >= 2 && !defined __STRICT_ANSI__
362 re_token_type_t type : 8;
364 re_token_type_t type;
366 unsigned int constraint : 10; /* context constraint */
367 unsigned int duplicated : 1;
368 unsigned int opt_subexp : 1;
369 #ifdef RE_ENABLE_I18N
370 unsigned int accept_mb : 1;
371 /* These 2 bits can be moved into the union if needed (e.g. if running out
372 of bits; move opr.c to opr.c.c and move the flags to opr.c.flags). */
373 unsigned int mb_partial : 1;
375 unsigned int word_char : 1;
378 #define IS_EPSILON_NODE(type) ((type) & EPSILON_BIT)
382 /* Indicate the raw buffer which is the original string passed as an
383 argument of regexec(), re_search(), etc.. */
384 const unsigned char *raw_mbs;
385 /* Store the multibyte string. In case of "case insensitive mode" like
386 REG_ICASE, upper cases of the string are stored, otherwise MBS points
387 the same address that RAW_MBS points. */
389 #ifdef RE_ENABLE_I18N
390 /* Store the wide character string which is corresponding to MBS. */
395 /* Index in RAW_MBS. Each character mbs[i] corresponds to
396 raw_mbs[raw_mbs_idx + i]. */
398 /* The length of the valid characters in the buffers. */
400 /* The corresponding number of bytes in raw_mbs array. */
402 /* The length of the buffers MBS and WCS. */
404 /* The index in MBS, which is updated by re_string_fetch_byte. */
406 /* length of RAW_MBS array. */
408 /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
410 /* End of the buffer may be shorter than its length in the cases such
411 as re_match_2, re_search_2. Then, we use STOP for end of the buffer
414 /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
417 /* The context of mbs[0]. We store the context independently, since
418 the context of mbs[0] may be different from raw_mbs[0], which is
419 the beginning of the input string. */
420 unsigned int tip_context;
421 /* The translation passed as a part of an argument of re_compile_pattern. */
422 RE_TRANSLATE_TYPE trans;
423 /* Copy of re_dfa_t's word_char. */
424 re_const_bitset_ptr_t word_char;
425 /* true if REG_ICASE. */
427 unsigned char is_utf8;
428 unsigned char map_notascii;
429 unsigned char mbs_allocated;
430 unsigned char offsets_needed;
431 unsigned char newline_anchor;
432 unsigned char word_ops_used;
435 typedef struct re_string_t re_string_t;
439 typedef struct re_dfa_t re_dfa_t;
442 # define internal_function
443 # define IS_IN(libc) false
446 static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
449 #ifdef RE_ENABLE_I18N
450 static void build_wcs_buffer (re_string_t *pstr) internal_function;
451 static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
453 #endif /* RE_ENABLE_I18N */
454 static void build_upper_buffer (re_string_t *pstr) internal_function;
455 static void re_string_translate_buffer (re_string_t *pstr) internal_function;
456 static unsigned int re_string_context_at (const re_string_t *input, Idx idx,
458 internal_function __attribute__ ((pure));
460 #define re_string_peek_byte(pstr, offset) \
461 ((pstr)->mbs[(pstr)->cur_idx + offset])
462 #define re_string_fetch_byte(pstr) \
463 ((pstr)->mbs[(pstr)->cur_idx++])
464 #define re_string_first_byte(pstr, idx) \
465 ((idx) == (pstr)->valid_len || (pstr)->wcs[idx] != WEOF)
466 #define re_string_is_single_byte_char(pstr, idx) \
467 ((pstr)->wcs[idx] != WEOF && ((pstr)->valid_len == (idx) + 1 \
468 || (pstr)->wcs[(idx) + 1] != WEOF))
469 #define re_string_eoi(pstr) ((pstr)->stop <= (pstr)->cur_idx)
470 #define re_string_cur_idx(pstr) ((pstr)->cur_idx)
471 #define re_string_get_buffer(pstr) ((pstr)->mbs)
472 #define re_string_length(pstr) ((pstr)->len)
473 #define re_string_byte_at(pstr,idx) ((pstr)->mbs[idx])
474 #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
475 #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
477 #if defined _LIBC || HAVE_ALLOCA
483 /* The OS usually guarantees only one guard page at the bottom of the stack,
484 and a page size can be as small as 4096 bytes. So we cannot safely
485 allocate anything larger than 4096 bytes. Also care for the possibility
486 of a few compiler-allocated temporary stack slots. */
487 # define __libc_use_alloca(n) ((n) < 4032)
489 /* alloca is implemented with malloc, so just use malloc. */
490 # define __libc_use_alloca(n) 0
492 # define alloca(n) malloc (n)
497 # define MALLOC_0_IS_NONNULL 1
498 #elif !defined MALLOC_0_IS_NONNULL
499 # define MALLOC_0_IS_NONNULL 0
503 # define MAX(a,b) ((a) < (b) ? (b) : (a))
506 # define MIN(a,b) ((a) < (b) ? (a) : (b))
509 #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
510 #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
511 #define re_free(p) free (p)
515 struct bin_tree_t *parent;
516 struct bin_tree_t *left;
517 struct bin_tree_t *right;
518 struct bin_tree_t *first;
519 struct bin_tree_t *next;
523 /* 'node_idx' is the index in dfa->nodes, if 'type' == 0.
524 Otherwise 'type' indicate the type of this node. */
527 typedef struct bin_tree_t bin_tree_t;
529 #define BIN_TREE_STORAGE_SIZE \
530 ((1024 - sizeof (void *)) / sizeof (bin_tree_t))
532 struct bin_tree_storage_t
534 struct bin_tree_storage_t *next;
535 bin_tree_t data[BIN_TREE_STORAGE_SIZE];
537 typedef struct bin_tree_storage_t bin_tree_storage_t;
539 #define CONTEXT_WORD 1
540 #define CONTEXT_NEWLINE (CONTEXT_WORD << 1)
541 #define CONTEXT_BEGBUF (CONTEXT_NEWLINE << 1)
542 #define CONTEXT_ENDBUF (CONTEXT_BEGBUF << 1)
544 #define IS_WORD_CONTEXT(c) ((c) & CONTEXT_WORD)
545 #define IS_NEWLINE_CONTEXT(c) ((c) & CONTEXT_NEWLINE)
546 #define IS_BEGBUF_CONTEXT(c) ((c) & CONTEXT_BEGBUF)
547 #define IS_ENDBUF_CONTEXT(c) ((c) & CONTEXT_ENDBUF)
548 #define IS_ORDINARY_CONTEXT(c) ((c) == 0)
550 #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
551 #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
552 #define IS_WIDE_WORD_CHAR(ch) (__iswalnum (ch) || (ch) == L'_')
553 #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
555 #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
556 ((((constraint) & PREV_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
557 || ((constraint & PREV_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
558 || ((constraint & PREV_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context))\
559 || ((constraint & PREV_BEGBUF_CONSTRAINT) && !IS_BEGBUF_CONTEXT (context)))
561 #define NOT_SATISFY_NEXT_CONSTRAINT(constraint,context) \
562 ((((constraint) & NEXT_WORD_CONSTRAINT) && !IS_WORD_CONTEXT (context)) \
563 || (((constraint) & NEXT_NOTWORD_CONSTRAINT) && IS_WORD_CONTEXT (context)) \
564 || (((constraint) & NEXT_NEWLINE_CONSTRAINT) && !IS_NEWLINE_CONTEXT (context)) \
565 || (((constraint) & NEXT_ENDBUF_CONSTRAINT) && !IS_ENDBUF_CONTEXT (context)))
571 re_node_set non_eps_nodes;
572 re_node_set inveclosure;
573 re_node_set *entrance_nodes;
574 struct re_dfastate_t **trtable, **word_trtable;
575 unsigned int context : 4;
576 unsigned int halt : 1;
577 /* If this state can accept "multi byte".
578 Note that we refer to multibyte characters, and multi character
579 collating elements as "multi byte". */
580 unsigned int accept_mb : 1;
581 /* If this state has backreference node(s). */
582 unsigned int has_backref : 1;
583 unsigned int has_constraint : 1;
585 typedef struct re_dfastate_t re_dfastate_t;
587 struct re_state_table_entry
591 re_dfastate_t **array;
594 /* Array type used in re_sub_match_last_t and re_sub_match_top_t. */
600 re_dfastate_t **array;
603 /* Store information about the node NODE whose type is OP_CLOSE_SUBEXP. */
608 Idx str_idx; /* The position NODE match at. */
610 } re_sub_match_last_t;
612 /* Store information about the node NODE whose type is OP_OPEN_SUBEXP.
613 And information about the node, whose type is OP_CLOSE_SUBEXP,
614 corresponding to NODE is stored in LASTS. */
621 Idx alasts; /* Allocation size of LASTS. */
622 Idx nlasts; /* The number of LASTS. */
623 re_sub_match_last_t **lasts;
624 } re_sub_match_top_t;
626 struct re_backref_cache_entry
634 unsigned short int eps_reachable_subexps_map;
639 /* The string object corresponding to the input string. */
641 #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
642 const re_dfa_t *const dfa;
646 /* EFLAGS of the argument of regexec. */
648 /* Where the matching ends. */
651 /* The state log used by the matcher. */
652 re_dfastate_t **state_log;
654 /* Back reference cache. */
657 struct re_backref_cache_entry *bkref_ents;
661 re_sub_match_top_t **sub_tops;
662 } re_match_context_t;
666 re_dfastate_t **sifted_states;
667 re_dfastate_t **limited_states;
673 struct re_fail_stack_ent_t
678 re_node_set eps_via_nodes;
681 struct re_fail_stack_t
685 struct re_fail_stack_ent_t *stack;
696 re_node_set *eclosures;
697 re_node_set *inveclosures;
698 struct re_state_table_entry *state_table;
699 re_dfastate_t *init_state;
700 re_dfastate_t *init_state_word;
701 re_dfastate_t *init_state_nl;
702 re_dfastate_t *init_state_begbuf;
703 bin_tree_t *str_tree;
704 bin_tree_storage_t *str_tree_storage;
705 re_bitset_ptr_t sb_char;
706 int str_tree_storage_idx;
708 /* number of subexpressions 're_nsub' is in regex_t. */
709 re_hashval_t state_hash_mask;
711 Idx nbackref; /* The number of backreference in this dfa. */
713 /* Bitmap expressing which backreference is used. */
714 bitset_word_t used_bkref_map;
715 bitset_word_t completed_bkref_map;
717 unsigned int has_plural_match : 1;
718 /* If this dfa has "multibyte node", which is a backreference or
719 a node which can accept multibyte character or multi character
720 collating element. */
721 unsigned int has_mb_node : 1;
722 unsigned int is_utf8 : 1;
723 unsigned int map_notascii : 1;
724 unsigned int word_ops_used : 1;
735 #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
736 #define re_node_set_remove(set,id) \
737 (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
738 #define re_node_set_empty(p) ((p)->nelem = 0)
739 #define re_node_set_free(set) re_free ((set)->elems)
753 bracket_elem_type type;
763 /* Functions for bitset_t operation. */
766 bitset_set (bitset_t set, Idx i)
768 set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS;
772 bitset_clear (bitset_t set, Idx i)
774 set[i / BITSET_WORD_BITS] &= ~ ((bitset_word_t) 1 << i % BITSET_WORD_BITS);
778 bitset_contain (const bitset_t set, Idx i)
780 return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
784 bitset_empty (bitset_t set)
786 memset (set, '\0', sizeof (bitset_t));
790 bitset_set_all (bitset_t set)
792 memset (set, -1, sizeof (bitset_word_t) * (SBC_MAX / BITSET_WORD_BITS));
793 if (SBC_MAX % BITSET_WORD_BITS != 0)
794 set[BITSET_WORDS - 1] =
795 ((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
799 bitset_copy (bitset_t dest, const bitset_t src)
801 memcpy (dest, src, sizeof (bitset_t));
804 static void __attribute__ ((unused))
805 bitset_not (bitset_t set)
808 for (bitset_i = 0; bitset_i < SBC_MAX / BITSET_WORD_BITS; ++bitset_i)
809 set[bitset_i] = ~set[bitset_i];
810 if (SBC_MAX % BITSET_WORD_BITS != 0)
811 set[BITSET_WORDS - 1] =
812 ((((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
813 & ~set[BITSET_WORDS - 1]);
816 static void __attribute__ ((unused))
817 bitset_merge (bitset_t dest, const bitset_t src)
820 for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
821 dest[bitset_i] |= src[bitset_i];
824 static void __attribute__ ((unused))
825 bitset_mask (bitset_t dest, const bitset_t src)
828 for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
829 dest[bitset_i] &= src[bitset_i];
832 #ifdef RE_ENABLE_I18N
833 /* Functions for re_string. */
835 internal_function __attribute__ ((pure, unused))
836 re_string_char_size_at (const re_string_t *pstr, Idx idx)
839 if (pstr->mb_cur_max == 1)
841 for (byte_idx = 1; idx + byte_idx < pstr->valid_len; ++byte_idx)
842 if (pstr->wcs[idx + byte_idx] != WEOF)
848 internal_function __attribute__ ((pure, unused))
849 re_string_wchar_at (const re_string_t *pstr, Idx idx)
851 if (pstr->mb_cur_max == 1)
852 return (wint_t) pstr->mbs[idx];
853 return (wint_t) pstr->wcs[idx];
857 # include <locale/weight.h>
861 internal_function __attribute__ ((pure, unused))
862 re_string_elem_size_at (const re_string_t *pstr, Idx idx)
865 const unsigned char *p, *extra;
866 const int32_t *table, *indirect;
867 uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
871 table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
872 extra = (const unsigned char *)
873 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
874 indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
875 _NL_COLLATE_INDIRECTMB);
877 findidx (table, indirect, extra, &p, pstr->len - idx);
878 return p - pstr->mbs - idx;
884 #endif /* RE_ENABLE_I18N */
886 #ifndef __GNUC_PREREQ
887 # if defined __GNUC__ && defined __GNUC_MINOR__
888 # define __GNUC_PREREQ(maj, min) \
889 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
891 # define __GNUC_PREREQ(maj, min) 0
895 #if __GNUC_PREREQ (3,4)
896 # undef __attribute_warn_unused_result__
897 # define __attribute_warn_unused_result__ \
898 __attribute__ ((__warn_unused_result__))
900 # define __attribute_warn_unused_result__ /* empty */
903 #endif /* _REGEX_INTERNAL_H */