VirtualBox

Changeset 3613 in kBuild for trunk/src/sed/lib/regcomp.c


Ignore:
Timestamp:
Sep 19, 2024 12:34:43 AM (7 months ago)
Author:
bird
Message:

src/sed: Merged in changes between 4.1.5 and 4.9 from the vendor branch. (svn merge /vendor/sed/4.1.5 /vendor/sed/current .)

Location:
trunk/src/sed
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sed

  • trunk/src/sed/lib/regcomp.c

    r3550 r3613  
    11/* Extended regular expression matching and search library.
    2    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
     2   Copyright (C) 2002-2022 Free Software Foundation, Inc.
    33   This file is part of the GNU C Library.
    44   Contributed by Isamu Hasegawa <[email protected]>.
     
    1515
    1616   You should have received a copy of the GNU Lesser General Public
    17    License along with the GNU C Library; if not, write to the Free
    18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    19    02111-1307 USA.  */
     17   License along with the GNU C Library; if not, see
     18   <https://www.gnu.org/licenses/>.  */
     19
     20#ifdef _LIBC
     21# include <locale/weight.h>
     22#endif
    2023
    2124static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
     
    2528                                     char *fastmap);
    2629static reg_errcode_t init_dfa (re_dfa_t *dfa, size_t pat_len);
    27 #ifdef RE_ENABLE_I18N
    2830static void free_charset (re_charset_t *cset);
    29 #endif /* RE_ENABLE_I18N */
    3031static void free_workarea_compile (regex_t *preg);
    3132static reg_errcode_t create_initial_state (re_dfa_t *dfa);
    32 #ifdef RE_ENABLE_I18N
    3333static void optimize_utf8 (re_dfa_t *dfa);
    34 #endif
    3534static reg_errcode_t analyze (regex_t *preg);
    3635static reg_errcode_t preorder (bin_tree_t *root,
     
    4746static reg_errcode_t calc_next (void *extra, bin_tree_t *node);
    4847static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
    49 static int duplicate_node (re_dfa_t *dfa, int org_idx, unsigned int constraint);
    50 static int search_duplicated_node (const re_dfa_t *dfa, int org_node,
     48static Idx duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint);
     49static Idx search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
    5150                                   unsigned int constraint);
    5251static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
    5352static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
    54                                          int node, int root);
     53                                         Idx node, bool root);
    5554static reg_errcode_t calc_inveclosure (re_dfa_t *dfa);
    56 static int fetch_number (re_string_t *input, re_token_t *token,
     55static Idx fetch_number (re_string_t *input, re_token_t *token,
    5756                         reg_syntax_t syntax);
    5857static int peek_token (re_token_t *token, re_string_t *input,
    59                         reg_syntax_t syntax) internal_function;
     58                        reg_syntax_t syntax);
    6059static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
    6160                          reg_syntax_t syntax, reg_errcode_t *err);
    6261static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
    6362                                  re_token_t *token, reg_syntax_t syntax,
    64                                   int nest, reg_errcode_t *err);
     63                                  Idx nest, reg_errcode_t *err);
    6564static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
    6665                                 re_token_t *token, reg_syntax_t syntax,
    67                                  int nest, reg_errcode_t *err);
     66                                 Idx nest, reg_errcode_t *err);
    6867static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
    6968                                     re_token_t *token, reg_syntax_t syntax,
    70                                      int nest, reg_errcode_t *err);
     69                                     Idx nest, reg_errcode_t *err);
    7170static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
    7271                                  re_token_t *token, reg_syntax_t syntax,
    73                                   int nest, reg_errcode_t *err);
     72                                  Idx nest, reg_errcode_t *err);
    7473static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
    7574                                 re_dfa_t *dfa, re_token_t *token,
     
    8382                                            re_dfa_t *dfa,
    8483                                            reg_syntax_t syntax,
    85                                             int accept_hyphen);
     84                                            bool accept_hyphen);
    8685static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
    8786                                          re_string_t *regexp,
    8887                                          re_token_t *token);
    89 #ifdef RE_ENABLE_I18N
    9088static reg_errcode_t build_equiv_class (bitset_t sbcset,
    9189                                        re_charset_t *mbcset,
    92                                         int *equiv_class_alloc,
     90                                        Idx *equiv_class_alloc,
    9391                                        const unsigned char *name);
    9492static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
    9593                                      bitset_t sbcset,
    9694                                      re_charset_t *mbcset,
    97                                       int *char_class_alloc,
    98                                       const unsigned char *class_name,
     95                                      Idx *char_class_alloc,
     96                                      const char *class_name,
    9997                                      reg_syntax_t syntax);
    100 #else  /* not RE_ENABLE_I18N */
    101 static reg_errcode_t build_equiv_class (bitset_t sbcset,
    102                                         const unsigned char *name);
    103 static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
    104                                       bitset_t sbcset,
    105                                       const unsigned char *class_name,
    106                                       reg_syntax_t syntax);
    107 #endif /* not RE_ENABLE_I18N */
    10898static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
    10999                                       RE_TRANSLATE_TYPE trans,
    110                                        const unsigned char *class_name,
    111                                        const unsigned char *extra,
    112                                        int non_match, reg_errcode_t *err);
     100                                       const char *class_name,
     101                                       const char *extra,
     102                                       bool non_match, reg_errcode_t *err);
    113103static bin_tree_t *create_tree (re_dfa_t *dfa,
    114104                                bin_tree_t *left, bin_tree_t *right,
     
    128118   but why not be nice?  */
    129119
    130 const char __re_error_msgid[] attribute_hidden =
     120static const char __re_error_msgid[] =
    131121  {
    132122#define REG_NOERROR_IDX 0
     
    152142    "\0"
    153143#define REG_EBRACK_IDX  (REG_ESUBREG_IDX + sizeof "Invalid back reference")
    154     gettext_noop ("Unmatched [ or [^")  /* REG_EBRACK */
     144    gettext_noop ("Unmatched [, [^, [:, [., or [=")     /* REG_EBRACK */
    155145    "\0"
    156 #define REG_EPAREN_IDX  (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
     146#define REG_EPAREN_IDX  (REG_EBRACK_IDX + sizeof "Unmatched [, [^, [:, [., or [=")
    157147    gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
    158148    "\0"
     
    182172  };
    183173
    184 const size_t __re_error_msgid_idx[] attribute_hidden =
     174static const size_t __re_error_msgid_idx[] =
    185175  {
    186176    REG_NOERROR_IDX,
     
    210200   Returns 0 if the pattern was valid, otherwise an error string.
    211201
    212    Assumes the `allocated' (and perhaps `buffer') and `translate' fields
     202   Assumes the 'allocated' (and perhaps 'buffer') and 'translate' fields
    213203   are set in BUFP on entry.  */
    214204
    215205const char *
    216 re_compile_pattern (pattern, length, bufp)
    217     const char *pattern;
    218     size_t length;
    219     struct re_pattern_buffer *bufp;
     206re_compile_pattern (const char *pattern, size_t length,
     207                    struct re_pattern_buffer *bufp)
    220208{
    221209  reg_errcode_t ret;
     
    235223  return gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
    236224}
    237 #ifdef _LIBC
    238225weak_alias (__re_compile_pattern, re_compile_pattern)
    239 #endif
    240 
    241 /* Set by `re_set_syntax' to the current regexp syntax to recognize.  Can
     226
     227/* Set by 're_set_syntax' to the current regexp syntax to recognize.  Can
    242228   also be assigned to arbitrarily: each pattern buffer stores its own
    243229   syntax, so it can be changed between regex compilations.  */
     
    255241
    256242reg_syntax_t
    257 re_set_syntax (syntax)
    258     reg_syntax_t syntax;
     243re_set_syntax (reg_syntax_t syntax)
    259244{
    260245  reg_syntax_t ret = re_syntax_options;
     
    263248  return ret;
    264249}
    265 #ifdef _LIBC
    266250weak_alias (__re_set_syntax, re_set_syntax)
    267 #endif
    268251
    269252int
    270 re_compile_fastmap (bufp)
    271     struct re_pattern_buffer *bufp;
    272 {
    273   re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
     253re_compile_fastmap (struct re_pattern_buffer *bufp)
     254{
     255  re_dfa_t *dfa = bufp->buffer;
    274256  char *fastmap = bufp->fastmap;
    275257
     
    285267  return 0;
    286268}
    287 #ifdef _LIBC
    288269weak_alias (__re_compile_fastmap, re_compile_fastmap)
    289 #endif
    290 
    291 static inline void
    292 __attribute ((always_inline))
    293 re_set_fastmap (char *fastmap, int icase, int ch)
     270
     271static __always_inline void
     272re_set_fastmap (char *fastmap, bool icase, int ch)
    294273{
    295274  fastmap[ch] = 1;
     
    305284                         char *fastmap)
    306285{
    307   re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
    308   int node_cnt;
    309   int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
     286  re_dfa_t *dfa = bufp->buffer;
     287  Idx node_cnt;
     288  bool icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
    310289  for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
    311290    {
    312       int node = init_state->nodes.elems[node_cnt];
     291      Idx node = init_state->nodes.elems[node_cnt];
    313292      re_token_type_t type = dfa->nodes[node].type;
    314293
     
    316295        {
    317296          re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
    318 #ifdef RE_ENABLE_I18N
    319297          if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
    320298            {
    321               unsigned char *buf = alloca (dfa->mb_cur_max), *p;
     299              unsigned char buf[MB_LEN_MAX];
     300              unsigned char *p;
    322301              wchar_t wc;
    323302              mbstate_t state;
     
    330309                *p++ = dfa->nodes[node].opr.c;
    331310              memset (&state, '\0', sizeof (state));
    332               if (mbrtowc (&wc, (const char *) buf, p - buf,
    333                            &state) == p - buf
    334                   && (__wcrtomb ((char *) buf, towlower (wc), &state)
     311              if (__mbrtowc (&wc, (const char *) buf, p - buf,
     312                             &state) == p - buf
     313                  && (__wcrtomb ((char *) buf, __towlower (wc), &state)
    335314                      != (size_t) -1))
    336                 re_set_fastmap (fastmap, 0, buf[0]);
     315                re_set_fastmap (fastmap, false, buf[0]);
    337316            }
    338 #endif
    339317        }
    340318      else if (type == SIMPLE_BRACKET)
     
    350328            }
    351329        }
    352 #ifdef RE_ENABLE_I18N
    353330      else if (type == COMPLEX_BRACKET)
    354331        {
    355           int i;
    356332          re_charset_t *cset = dfa->nodes[node].opr.mbcset;
    357           if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes
    358               || cset->nranges || cset->nchar_classes)
    359             {
    360 # ifdef _LIBC
    361               if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0)
     333          Idx i;
     334
     335#ifdef _LIBC
     336          /* See if we have to try all bytes which start multiple collation
     337             elements.
     338             e.g. In da_DK, we want to catch 'a' since "aa" is a valid
     339                  collation element, and don't catch 'b' since 'b' is
     340                  the only collation element which starts from 'b' (and
     341                  it is caught by SIMPLE_BRACKET).  */
     342              if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0
     343                  && (cset->ncoll_syms || cset->nranges))
    362344                {
    363                   /* In this case we want to catch the bytes which are
    364                      the first byte of any collation elements.
    365                      e.g. In da_DK, we want to catch 'a' since "aa"
    366                           is a valid collation element, and don't catch
    367                           'b' since 'b' is the only collation element
    368                           which starts from 'b'.  */
    369345                  const int32_t *table = (const int32_t *)
    370346                    _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
     
    373349                      re_set_fastmap (fastmap, icase, i);
    374350                }
    375 # else
    376               if (dfa->mb_cur_max > 1)
    377                 for (i = 0; i < SBC_MAX; ++i)
    378                   if (__btowc (i) == WEOF)
    379                     re_set_fastmap (fastmap, icase, i);
    380 # endif /* not _LIBC */
     351#endif /* _LIBC */
     352
     353          /* See if we have to start the match at all multibyte characters,
     354             i.e. where we would not find an invalid sequence.  This only
     355             applies to multibyte character sets; for single byte character
     356             sets, the SIMPLE_BRACKET again suffices.  */
     357          if (dfa->mb_cur_max > 1
     358              && (cset->nchar_classes || cset->non_match || cset->nranges
     359#ifdef _LIBC
     360                  || cset->nequiv_classes
     361#endif /* _LIBC */
     362                 ))
     363            {
     364              unsigned char c = 0;
     365              do
     366                {
     367                  mbstate_t mbs;
     368                  memset (&mbs, 0, sizeof (mbs));
     369                  if (__mbrtowc (NULL, (char *) &c, 1, &mbs) == (size_t) -2)
     370                    re_set_fastmap (fastmap, false, (int) c);
     371                }
     372              while (++c != 0);
    381373            }
    382           for (i = 0; i < cset->nmbchars; ++i)
     374
     375          else
    383376            {
    384               char buf[256];
    385               mbstate_t state;
    386               memset (&state, '\0', sizeof (state));
    387               if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
    388                 re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
    389               if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
     377              /* ... Else catch all bytes which can start the mbchars.  */
     378              for (i = 0; i < cset->nmbchars; ++i)
    390379                {
    391                   if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
    392                       != (size_t) -1)
    393                     re_set_fastmap (fastmap, 0, *(unsigned char *) buf);
     380                  char buf[256];
     381                  mbstate_t state;
     382                  memset (&state, '\0', sizeof (state));
     383                  if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
     384                    re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
     385                  if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
     386                    {
     387                      if (__wcrtomb (buf, __towlower (cset->mbchars[i]), &state)
     388                          != (size_t) -1)
     389                        re_set_fastmap (fastmap, false, *(unsigned char *) buf);
     390                    }
    394391                }
    395392            }
    396393        }
    397 #endif /* RE_ENABLE_I18N */
    398       else if (type == OP_PERIOD
    399 #ifdef RE_ENABLE_I18N
    400                || type == OP_UTF8_PERIOD
    401 #endif /* RE_ENABLE_I18N */
    402                || type == END_OF_RE)
     394      else if (type == OP_PERIOD || type == OP_UTF8_PERIOD || type == END_OF_RE)
    403395        {
    404396          memset (fastmap, '\1', sizeof (char) * SBC_MAX);
     
    417409   since POSIX says we shouldn't.  Thus, we set
    418410
    419      `buffer' to the compiled pattern;
    420      `used' to the length of the compiled pattern;
    421      `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
     411     'buffer' to the compiled pattern;
     412     'used' to the length of the compiled pattern;
     413     'syntax' to RE_SYNTAX_POSIX_EXTENDED if the
    422414       REG_EXTENDED bit in CFLAGS is set; otherwise, to
    423415       RE_SYNTAX_POSIX_BASIC;
    424      `newline_anchor' to REG_NEWLINE being set in CFLAGS;
    425      `fastmap' to an allocated space for the fastmap;
    426      `fastmap_accurate' to zero;
    427      `re_nsub' to the number of subexpressions in PATTERN.
     416     'newline_anchor' to REG_NEWLINE being set in CFLAGS;
     417     'fastmap' to an allocated space for the fastmap;
     418     'fastmap_accurate' to zero;
     419     're_nsub' to the number of subexpressions in PATTERN.
    428420
    429421   PATTERN is the address of the pattern string.
     
    448440
    449441int
    450 regcomp (preg, pattern, cflags)
    451     regex_t *__restrict preg;
    452     const char *__restrict pattern;
    453     int cflags;
     442regcomp (regex_t *__restrict preg, const char *__restrict pattern, int cflags)
    454443{
    455444  reg_errcode_t ret;
     
    463452  /* Try to allocate space for the fastmap.  */
    464453  preg->fastmap = re_malloc (char, SBC_MAX);
    465   if (BE (preg->fastmap == NULL, 0))
     454  if (__glibc_unlikely (preg->fastmap == NULL))
    466455    return REG_ESPACE;
    467456
     
    489478
    490479  /* We have already checked preg->fastmap != NULL.  */
    491   if (BE (ret == REG_NOERROR, 1))
     480  if (__glibc_likely (ret == REG_NOERROR))
    492481    /* Compute the fastmap now, since regexec cannot modify the pattern
    493482       buffer.  This function never fails in this implementation.  */
     
    502491  return (int) ret;
    503492}
    504 #ifdef _LIBC
     493libc_hidden_def (__regcomp)
    505494weak_alias (__regcomp, regcomp)
    506 #endif
    507495
    508496/* Returns a message corresponding to an error code, ERRCODE, returned
     
    510498
    511499size_t
    512 regerror (errcode, preg, errbuf, errbuf_size)
    513     int errcode;
    514     const regex_t *__restrict preg;
    515     char *__restrict errbuf;
    516     size_t errbuf_size;
     500regerror (int errcode, const regex_t *__restrict preg, char *__restrict errbuf,
     501          size_t errbuf_size)
    517502{
    518503  const char *msg;
    519504  size_t msg_size;
    520 
    521   if (BE (errcode < 0
    522           || errcode >= (int) (sizeof (__re_error_msgid_idx)
    523                                / sizeof (__re_error_msgid_idx[0])), 0))
     505  int nerrcodes = sizeof __re_error_msgid_idx / sizeof __re_error_msgid_idx[0];
     506
     507  if (__glibc_unlikely (errcode < 0 || errcode >= nerrcodes))
    524508    /* Only error codes returned by the rest of the code should be passed
    525509       to this routine.  If we are given anything else, or if other regex
     
    532516  msg_size = strlen (msg) + 1; /* Includes the null.  */
    533517
    534   if (BE (errbuf_size != 0, 1))
    535     {
    536       if (BE (msg_size > errbuf_size, 0))
    537         {
    538 #if defined HAVE_MEMPCPY || defined _LIBC
    539           *((char *) __mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
    540 #else
    541           memcpy (errbuf, msg, errbuf_size - 1);
    542           errbuf[errbuf_size - 1] = 0;
    543 #endif
    544         }
    545       else
    546         memcpy (errbuf, msg, msg_size);
     518  if (__glibc_likely (errbuf_size != 0))
     519    {
     520      size_t cpy_size = msg_size;
     521      if (__glibc_unlikely (msg_size > errbuf_size))
     522        {
     523          cpy_size = errbuf_size - 1;
     524          errbuf[cpy_size] = '\0';
     525        }
     526      memcpy (errbuf, msg, cpy_size);
    547527    }
    548528
    549529  return msg_size;
    550530}
    551 #ifdef _LIBC
    552531weak_alias (__regerror, regerror)
    553 #endif
    554 
    555 
    556 #ifdef RE_ENABLE_I18N
     532
     533
    557534/* This static array is used for the map to single-byte characters when
    558535   UTF-8 is used.  Otherwise we would allocate memory just to initialize
     
    562539{
    563540    /* Set the first 128 bits.  */
    564 #ifdef _MSC_VER
    565   BITSET_WORD_MAX, BITSET_WORD_MAX, BITSET_WORD_MAX, BITSET_WORD_MAX, 0, 0, 0, 0
     541#if (defined __GNUC__ || __clang_major__ >= 4) && !defined __STRICT_ANSI__
     542  [0 ... 0x80 / BITSET_WORD_BITS - 1] = BITSET_WORD_MAX
    566543#else
    567   [0 ... 0x80 / BITSET_WORD_BITS - 1] = BITSET_WORD_MAX
     544# if 4 * BITSET_WORD_BITS < ASCII_CHARS
     545#  error "bitset_word_t is narrower than 32 bits"
     546# elif 3 * BITSET_WORD_BITS < ASCII_CHARS
     547  BITSET_WORD_MAX, BITSET_WORD_MAX, BITSET_WORD_MAX,
     548# elif 2 * BITSET_WORD_BITS < ASCII_CHARS
     549  BITSET_WORD_MAX, BITSET_WORD_MAX,
     550# elif 1 * BITSET_WORD_BITS < ASCII_CHARS
     551  BITSET_WORD_MAX,
     552# endif
     553  (BITSET_WORD_MAX
     554   >> (SBC_MAX % BITSET_WORD_BITS == 0
     555       ? 0
     556       : BITSET_WORD_BITS - SBC_MAX % BITSET_WORD_BITS))
    568557#endif
    569558};
    570 #endif
    571559
    572560
     
    574562free_dfa_content (re_dfa_t *dfa)
    575563{
    576   int i, j;
     564  Idx i, j;
    577565
    578566  if (dfa->nodes)
     
    603591            free_state (state);
    604592          }
    605         re_free (entry->array);
     593        re_free (entry->array);
    606594      }
    607595  re_free (dfa->state_table);
    608 #ifdef RE_ENABLE_I18N
    609596  if (dfa->sb_char != utf8_sb_map)
    610597    re_free (dfa->sb_char);
    611 #endif
    612598  re_free (dfa->subexp_map);
    613599#ifdef DEBUG
     
    622608
    623609void
    624 regfree (preg)
    625     regex_t *preg;
    626 {
    627   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
    628   if (BE (dfa != NULL, 1))
    629     free_dfa_content (dfa);
     610regfree (regex_t *preg)
     611{
     612  re_dfa_t *dfa = preg->buffer;
     613  if (__glibc_likely (dfa != NULL))
     614    {
     615      lock_fini (dfa->lock);
     616      free_dfa_content (dfa);
     617    }
    630618  preg->buffer = NULL;
    631619  preg->allocated = 0;
     
    637625  preg->translate = NULL;
    638626}
    639 #ifdef _LIBC
     627libc_hidden_def (__regfree)
    640628weak_alias (__regfree, regfree)
    641 #endif
    642629
    643630
     
    657644weak_function
    658645# endif
    659 re_comp (s)
    660      const char *s;
     646re_comp (const char *s)
    661647{
    662648  reg_errcode_t ret;
     
    681667  if (re_comp_buf.fastmap == NULL)
    682668    {
    683       re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
     669      re_comp_buf.fastmap = re_malloc (char, SBC_MAX);
    684670      if (re_comp_buf.fastmap == NULL)
    685671        return (char *) gettext (__re_error_msgid
     
    687673    }
    688674
    689   /* Since `re_exec' always passes NULL for the `regs' argument, we
     675  /* Since 're_exec' always passes NULL for the 'regs' argument, we
    690676     don't need to initialize the pattern buffer fields which affect it.  */
    691677
     
    698684    return NULL;
    699685
    700   /* Yes, we're discarding `const' here if !HAVE_LIBINTL.  */
     686  /* Yes, we're discarding 'const' here if !HAVE_LIBINTL.  */
    701687  return (char *) gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
    702688}
     
    734720
    735721  /* Initialize the dfa.  */
    736   dfa = (re_dfa_t *) preg->buffer;
    737   if (BE (preg->allocated < sizeof (re_dfa_t), 0))
     722  dfa = preg->buffer;
     723  if (__glibc_unlikely (preg->allocated < sizeof (re_dfa_t)))
    738724    {
    739725      /* If zero allocated, but buffer is non-null, try to realloc
     
    745731        return REG_ESPACE;
    746732      preg->allocated = sizeof (re_dfa_t);
    747       preg->buffer = (unsigned char *) dfa;
     733      preg->buffer = dfa;
    748734    }
    749735  preg->used = sizeof (re_dfa_t);
    750736
    751737  err = init_dfa (dfa, length);
    752   if (BE (err != REG_NOERROR, 0))
     738  if (__glibc_unlikely (err == REG_NOERROR && lock_init (dfa->lock) != 0))
     739    err = REG_ESPACE;
     740  if (__glibc_unlikely (err != REG_NOERROR))
    753741    {
    754742      free_dfa_content (dfa);
     
    763751#endif
    764752
    765   __libc_lock_init (dfa->lock);
    766 
    767753  err = re_string_construct (&regexp, pattern, length, preg->translate,
    768                              syntax & RE_ICASE, dfa);
    769   if (BE (err != REG_NOERROR, 0))
     754                             (syntax & RE_ICASE) != 0, dfa);
     755  if (__glibc_unlikely (err != REG_NOERROR))
    770756    {
    771757    re_compile_internal_free_return:
    772758      free_workarea_compile (preg);
    773759      re_string_destruct (&regexp);
     760      lock_fini (dfa->lock);
    774761      free_dfa_content (dfa);
    775762      preg->buffer = NULL;
     
    781768  preg->re_nsub = 0;
    782769  dfa->str_tree = parse (&regexp, preg, syntax, &err);
    783   if (BE (dfa->str_tree == NULL, 0))
     770  if (__glibc_unlikely (dfa->str_tree == NULL))
    784771    goto re_compile_internal_free_return;
    785772
    786773  /* Analyze the tree and create the nfa.  */
    787774  err = analyze (preg);
    788   if (BE (err != REG_NOERROR, 0))
     775  if (__glibc_unlikely (err != REG_NOERROR))
    789776    goto re_compile_internal_free_return;
    790777
    791 #ifdef RE_ENABLE_I18N
    792778  /* If possible, do searching in single byte encoding to speed things up.  */
    793779  if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL)
    794780    optimize_utf8 (dfa);
    795 #endif
    796781
    797782  /* Then create the initial state of the dfa.  */
     
    802787  re_string_destruct (&regexp);
    803788
    804   if (BE (err != REG_NOERROR, 0))
    805     {
     789  if (__glibc_unlikely (err != REG_NOERROR))
     790    {
     791      lock_fini (dfa->lock);
    806792      free_dfa_content (dfa);
    807793      preg->buffer = NULL;
     
    818804init_dfa (re_dfa_t *dfa, size_t pat_len)
    819805{
    820   unsigned int table_size;
     806  __re_size_t table_size;
    821807#ifndef _LIBC
    822   char *codeset_name;
     808  const char *codeset_name;
    823809#endif
     810  size_t max_i18n_object_size = MAX (sizeof (wchar_t), sizeof (wctype_t));
     811  size_t max_object_size =
     812    MAX (sizeof (struct re_state_table_entry),
     813         MAX (sizeof (re_token_t),
     814              MAX (sizeof (re_node_set),
     815                   MAX (sizeof (regmatch_t),
     816                        max_i18n_object_size))));
    824817
    825818  memset (dfa, '\0', sizeof (re_dfa_t));
     
    828821  dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
    829822
    830   /* Avoid overflows.  */
    831   if (pat_len == SIZE_MAX)
     823  /* Avoid overflows.  The extra "/ 2" is for the table_size doubling
     824     calculation below, and for similar doubling calculations
     825     elsewhere.  And it's <= rather than <, because some of the
     826     doubling calculations add 1 afterwards.  */
     827  if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / max_object_size) / 2
     828                        <= pat_len))
    832829    return REG_ESPACE;
    833830
     
    855852  if (get_crt_codepage() == MY_CP_UTF8)
    856853# else
    857 #  ifdef HAVE_LANGINFO_CODESET
    858854  codeset_name = nl_langinfo (CODESET);
    859 #  else
    860   codeset_name = getenv ("LC_ALL");
    861   if (codeset_name == NULL || codeset_name[0] == '\0')
    862     codeset_name = getenv ("LC_CTYPE");
    863   if (codeset_name == NULL || codeset_name[0] == '\0')
    864     codeset_name = getenv ("LANG");
    865   if (codeset_name == NULL)
    866     codeset_name = "";
    867   else if (strchr (codeset_name, '.') !=  NULL)
    868     codeset_name = strchr (codeset_name, '.') + 1;
    869 #  endif
    870 
    871   if (strcasecmp (codeset_name, "UTF-8") == 0
    872       || strcasecmp (codeset_name, "UTF8") == 0)
     855  if ((codeset_name[0] == 'U' || codeset_name[0] == 'u')
     856      && (codeset_name[1] == 'T' || codeset_name[1] == 't')
     857      && (codeset_name[2] == 'F' || codeset_name[2] == 'f')
     858      && strcmp (codeset_name + 3 + (codeset_name[3] == '-'), "8") == 0)
    873859# endif
    874860    dfa->is_utf8 = 1;
     
    879865#endif
    880866
    881 #ifdef RE_ENABLE_I18N
    882867  if (dfa->mb_cur_max > 1)
    883868    {
     
    889874
    890875          dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
    891           if (BE (dfa->sb_char == NULL, 0))
     876          if (__glibc_unlikely (dfa->sb_char == NULL))
    892877            return REG_ESPACE;
    893878
     
    899884                if (wch != WEOF)
    900885                  dfa->sb_char[i] |= (bitset_word_t) 1 << j;
    901 # ifndef _LIBC
     886#ifndef _LIBC
    902887                if (isascii (ch) && wch != ch)
    903888                  dfa->map_notascii = 1;
    904 # endif
     889#endif
    905890              }
    906891        }
    907892    }
    908 #endif
    909 
    910   if (BE (dfa->nodes == NULL || dfa->state_table == NULL, 0))
     893
     894  if (__glibc_unlikely (dfa->nodes == NULL || dfa->state_table == NULL))
    911895    return REG_ESPACE;
    912896  return REG_NOERROR;
     
    918902
    919903static void
    920 internal_function
    921904init_word_char (re_dfa_t *dfa)
    922905{
    923   int i, j, ch;
     906  int i = 0;
     907  int j;
     908  int ch = 0;
    924909  dfa->word_ops_used = 1;
    925   for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
     910  if (__glibc_likely (dfa->map_notascii == 0))
     911    {
     912      bitset_word_t bits0 = 0x00000000;
     913      bitset_word_t bits1 = 0x03ff0000;
     914      bitset_word_t bits2 = 0x87fffffe;
     915      bitset_word_t bits3 = 0x07fffffe;
     916      if (BITSET_WORD_BITS == 64)
     917        {
     918          /* Pacify gcc -Woverflow on 32-bit platformns.  */
     919          dfa->word_char[0] = bits1 << 31 << 1 | bits0;
     920          dfa->word_char[1] = bits3 << 31 << 1 | bits2;
     921          i = 2;
     922        }
     923      else if (BITSET_WORD_BITS == 32)
     924        {
     925          dfa->word_char[0] = bits0;
     926          dfa->word_char[1] = bits1;
     927          dfa->word_char[2] = bits2;
     928          dfa->word_char[3] = bits3;
     929          i = 4;
     930        }
     931      else
     932        goto general_case;
     933      ch = 128;
     934
     935      if (__glibc_likely (dfa->is_utf8))
     936        {
     937          memset (&dfa->word_char[i], '\0', (SBC_MAX - ch) / 8);
     938          return;
     939        }
     940    }
     941
     942 general_case:
     943  for (; i < BITSET_WORDS; ++i)
    926944    for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
    927945      if (isalnum (ch) || ch == '_')
     
    934952free_workarea_compile (regex_t *preg)
    935953{
    936   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     954  re_dfa_t *dfa = preg->buffer;
    937955  bin_tree_storage_t *storage, *next;
    938956  for (storage = dfa->str_tree_storage; storage; storage = next)
     
    953971create_initial_state (re_dfa_t *dfa)
    954972{
    955   int first, i;
     973  Idx first, i;
    956974  reg_errcode_t err;
    957975  re_node_set init_nodes;
     
    962980  dfa->init_node = first;
    963981  err = re_node_set_init_copy (&init_nodes, dfa->eclosures + first);
    964   if (BE (err != REG_NOERROR, 0))
     982  if (__glibc_unlikely (err != REG_NOERROR))
    965983    return err;
    966984
     
    972990    for (i = 0; i < init_nodes.nelem; ++i)
    973991      {
    974         int node_idx = init_nodes.elems[i];
     992        Idx node_idx = init_nodes.elems[i];
    975993        re_token_type_t type = dfa->nodes[node_idx].type;
    976994
    977         int clexp_idx;
     995        Idx clexp_idx;
    978996        if (type != OP_BACK_REF)
    979997          continue;
     
    9911009        if (type == OP_BACK_REF)
    9921010          {
    993             int dest_idx = dfa->edests[node_idx].elems[0];
     1011            Idx dest_idx = dfa->edests[node_idx].elems[0];
    9941012            if (!re_node_set_contains (&init_nodes, dest_idx))
    9951013              {
    996                 re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
     1014                reg_errcode_t merge_err
     1015                  = re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
     1016                if (merge_err != REG_NOERROR)
     1017                  return merge_err;
    9971018                i = 0;
    9981019              }
     
    10031024  dfa->init_state = re_acquire_state_context (&err, dfa, &init_nodes, 0);
    10041025  /* We don't check ERR here, since the initial state must not be NULL.  */
    1005   if (BE (dfa->init_state == NULL, 0))
     1026  if (__glibc_unlikely (dfa->init_state == NULL))
    10061027    return err;
    10071028  if (dfa->init_state->has_constraint)
     
    10151036                                                         CONTEXT_NEWLINE
    10161037                                                         | CONTEXT_BEGBUF);
    1017       if (BE (dfa->init_state_word == NULL || dfa->init_state_nl == NULL
    1018               || dfa->init_state_begbuf == NULL, 0))
     1038      if (__glibc_unlikely (dfa->init_state_word == NULL
     1039                            || dfa->init_state_nl == NULL
     1040                            || dfa->init_state_begbuf == NULL))
    10191041        return err;
    10201042    }
     
    10281050
    10291051
    1030 #ifdef RE_ENABLE_I18N
    10311052/* If it is possible to do searching in single byte encoding instead of UTF-8
    10321053   to speed things up, set dfa->mb_cur_max to 1, clear is_utf8 and change
     
    10361057optimize_utf8 (re_dfa_t *dfa)
    10371058{
    1038   int node, i, mb_chars = 0, has_period = 0;
     1059  Idx node;
     1060  int i;
     1061  bool mb_chars = false;
     1062  bool has_period = false;
    10391063
    10401064  for (node = 0; node < dfa->nodes_len; ++node)
     
    10421066      {
    10431067      case CHARACTER:
    1044         if (dfa->nodes[node].opr.c >= 0x80)
    1045           mb_chars = 1;
     1068        if (dfa->nodes[node].opr.c >= ASCII_CHARS)
     1069          mb_chars = true;
    10461070        break;
    10471071      case ANCHOR:
    1048         switch (dfa->nodes[node].opr.idx)
     1072        switch (dfa->nodes[node].opr.ctx_type)
    10491073          {
    10501074          case LINE_FIRST:
     
    10541078            break;
    10551079          default:
    1056             /* Word anchors etc. cannot be handled.  */
     1080            /* Word anchors etc. cannot be handled.  It's okay to test
     1081               opr.ctx_type since constraints (for all DFA nodes) are
     1082               created by ORing one or more opr.ctx_type values.  */
    10571083            return;
    10581084          }
    10591085        break;
    10601086      case OP_PERIOD:
    1061         has_period = 1;
    1062         break;
     1087        has_period = true;
     1088        break;
    10631089      case OP_BACK_REF:
    10641090      case OP_ALT:
     
    10711097        return;
    10721098      case SIMPLE_BRACKET:
    1073         /* Just double check.  The non-ASCII range starts at 0x80.  */
    1074         assert (0x80 % BITSET_WORD_BITS == 0);
    1075         for (i = 0x80 / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
    1076           if (dfa->nodes[node].opr.sbcset[i])
    1077             return;
     1099        /* Just double check.  */
     1100        {
     1101          int rshift = (ASCII_CHARS % BITSET_WORD_BITS == 0
     1102                        ? 0
     1103                        : BITSET_WORD_BITS - ASCII_CHARS % BITSET_WORD_BITS);
     1104          for (i = ASCII_CHARS / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
     1105            {
     1106              if (dfa->nodes[node].opr.sbcset[i] >> rshift != 0)
     1107                return;
     1108              rshift = 0;
     1109            }
     1110        }
    10781111        break;
    10791112      default:
     
    10851118      {
    10861119        if (dfa->nodes[node].type == CHARACTER
    1087             && dfa->nodes[node].opr.c >= 0x80)
     1120            && dfa->nodes[node].opr.c >= ASCII_CHARS)
    10881121          dfa->nodes[node].mb_partial = 0;
    10891122        else if (dfa->nodes[node].type == OP_PERIOD)
     
    10961129  dfa->has_mb_node = dfa->nbackref > 0 || has_period;
    10971130}
    1098 #endif
    10991131
    11001132
     
    11051137analyze (regex_t *preg)
    11061138{
    1107   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     1139  re_dfa_t *dfa = preg->buffer;
    11081140  reg_errcode_t ret;
    11091141
    11101142  /* Allocate arrays.  */
    1111   dfa->nexts = re_malloc (int, dfa->nodes_alloc);
    1112   dfa->org_indices = re_malloc (int, dfa->nodes_alloc);
     1143  dfa->nexts = re_malloc (Idx, dfa->nodes_alloc);
     1144  dfa->org_indices = re_malloc (Idx, dfa->nodes_alloc);
    11131145  dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
    11141146  dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
    1115   if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
    1116           || dfa->eclosures == NULL, 0))
     1147  if (__glibc_unlikely (dfa->nexts == NULL || dfa->org_indices == NULL
     1148                        || dfa->edests == NULL || dfa->eclosures == NULL))
    11171149    return REG_ESPACE;
    11181150
    1119   dfa->subexp_map = re_malloc (int, preg->re_nsub);
     1151  dfa->subexp_map = re_malloc (Idx, preg->re_nsub);
    11201152  if (dfa->subexp_map != NULL)
    11211153    {
    1122       int i;
     1154      Idx i;
    11231155      for (i = 0; i < preg->re_nsub; i++)
    11241156        dfa->subexp_map[i] = i;
     
    11291161      if (i == preg->re_nsub)
    11301162        {
    1131           free (dfa->subexp_map);
     1163          re_free (dfa->subexp_map);
    11321164          dfa->subexp_map = NULL;
    11331165        }
     
    11351167
    11361168  ret = postorder (dfa->str_tree, lower_subexps, preg);
    1137   if (BE (ret != REG_NOERROR, 0))
     1169  if (__glibc_unlikely (ret != REG_NOERROR))
    11381170    return ret;
    11391171  ret = postorder (dfa->str_tree, calc_first, dfa);
    1140   if (BE (ret != REG_NOERROR, 0))
     1172  if (__glibc_unlikely (ret != REG_NOERROR))
    11411173    return ret;
    11421174  preorder (dfa->str_tree, calc_next, dfa);
    11431175  ret = preorder (dfa->str_tree, link_nfa_nodes, dfa);
    1144   if (BE (ret != REG_NOERROR, 0))
     1176  if (__glibc_unlikely (ret != REG_NOERROR))
    11451177    return ret;
    11461178  ret = calc_eclosure (dfa);
    1147   if (BE (ret != REG_NOERROR, 0))
     1179  if (__glibc_unlikely (ret != REG_NOERROR))
    11481180    return ret;
    11491181
     
    11541186    {
    11551187      dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len);
    1156       if (BE (dfa->inveclosures == NULL, 0))
    1157         return REG_ESPACE;
     1188      if (__glibc_unlikely (dfa->inveclosures == NULL))
     1189        return REG_ESPACE;
    11581190      ret = calc_inveclosure (dfa);
    11591191    }
     
    11771209      while (node->left || node->right)
    11781210        if (node->left)
    1179           node = node->left;
    1180         else
    1181           node = node->right;
     1211          node = node->left;
     1212        else
     1213          node = node->right;
    11821214
    11831215      do
    11841216        {
    11851217          reg_errcode_t err = fn (extra, node);
    1186           if (BE (err != REG_NOERROR, 0))
     1218          if (__glibc_unlikely (err != REG_NOERROR))
    11871219            return err;
    1188           if (node->parent == NULL)
     1220          if (node->parent == NULL)
    11891221            return REG_NOERROR;
    11901222          prev = node;
     
    12061238    {
    12071239      reg_errcode_t err = fn (extra, node);
    1208       if (BE (err != REG_NOERROR, 0))
     1240      if (__glibc_unlikely (err != REG_NOERROR))
    12091241        return err;
    12101242
     
    12201252              node = node->parent;
    12211253              if (!node)
    1222                 return REG_NOERROR;
     1254                return REG_NOERROR;
    12231255            }
    12241256          node = node->right;
     
    12431275
    12441276  else if (node->token.type == SUBEXP
    1245            && node->left && node->left->token.type == SUBEXP)
    1246     {
    1247       int other_idx = node->left->token.opr.idx;
     1277           && node->left && node->left->token.type == SUBEXP)
     1278    {
     1279      Idx other_idx = node->left->token.opr.idx;
    12481280
    12491281      node->left = node->left->left;
    12501282      if (node->left)
    1251         node->left->parent = node;
     1283        node->left->parent = node;
    12521284
    12531285      dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
    12541286      if (other_idx < BITSET_WORD_BITS)
    1255           dfa->used_bkref_map &= ~((bitset_word_t) 1 << other_idx);
     1287        dfa->used_bkref_map &= ~((bitset_word_t) 1 << other_idx);
    12561288    }
    12571289
     
    12861318lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
    12871319{
    1288   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     1320  re_dfa_t *dfa = preg->buffer;
    12891321  bin_tree_t *body = node->left;
    12901322  bin_tree_t *op, *cls, *tree1, *tree;
     
    13071339  tree1 = body ? create_tree (dfa, body, cls, CONCAT) : cls;
    13081340  tree = create_tree (dfa, op, tree1, CONCAT);
    1309   if (BE (tree == NULL || tree1 == NULL || op == NULL || cls == NULL, 0))
     1341  if (__glibc_unlikely (tree == NULL || tree1 == NULL
     1342                        || op == NULL || cls == NULL))
    13101343    {
    13111344      *err = REG_ESPACE;
     
    13331366      node->first = node;
    13341367      node->node_idx = re_dfa_add_node (dfa, node->token);
    1335       if (BE (node->node_idx == -1, 0))
    1336         return REG_ESPACE;
     1368      if (__glibc_unlikely (node->node_idx == -1))
     1369        return REG_ESPACE;
     1370      if (node->token.type == ANCHOR)
     1371        dfa->nodes[node->node_idx].constraint = node->token.opr.ctx_type;
    13371372    }
    13381373  return REG_NOERROR;
     
    13561391        node->left->next = node->next;
    13571392      if (node->right)
    1358         node->right->next = node->next;
     1393        node->right->next = node->next;
    13591394      break;
    13601395    }
     
    13671402{
    13681403  re_dfa_t *dfa = (re_dfa_t *) extra;
    1369   int idx = node->node_idx;
     1404  Idx idx = node->node_idx;
    13701405  reg_errcode_t err = REG_NOERROR;
    13711406
     
    13761411
    13771412    case END_OF_RE:
    1378       assert (node->next == NULL);
     1413      DEBUG_ASSERT (node->next == NULL);
    13791414      break;
    13801415
     
    13821417    case OP_ALT:
    13831418      {
    1384         int left, right;
     1419        Idx left, right;
    13851420        dfa->has_plural_match = 1;
    13861421        if (node->left != NULL)
     
    13921427        else
    13931428          right = node->next->node_idx;
    1394         assert (left > -1);
    1395         assert (right > -1);
     1429        DEBUG_ASSERT (left > -1);
     1430        DEBUG_ASSERT (right > -1);
    13961431        err = re_node_set_init_2 (dfa->edests + idx, left, right);
    13971432      }
     
    14071442      dfa->nexts[idx] = node->next->node_idx;
    14081443      if (node->token.type == OP_BACK_REF)
    1409         re_node_set_init_1 (dfa->edests + idx, dfa->nexts[idx]);
     1444        err = re_node_set_init_1 (dfa->edests + idx, dfa->nexts[idx]);
    14101445      break;
    14111446
    14121447    default:
    1413       assert (!IS_EPSILON_NODE (node->token.type));
     1448      DEBUG_ASSERT (!IS_EPSILON_NODE (node->token.type));
    14141449      dfa->nexts[idx] = node->next->node_idx;
    14151450      break;
     
    14241459
    14251460static reg_errcode_t
    1426 internal_function
    1427 duplicate_node_closure (re_dfa_t *dfa, int top_org_node, int top_clone_node,
    1428                         int root_node, unsigned int init_constraint)
    1429 {
    1430   int org_node, clone_node, ret;
     1461duplicate_node_closure (re_dfa_t *dfa, Idx top_org_node, Idx top_clone_node,
     1462                        Idx root_node, unsigned int init_constraint)
     1463{
     1464  Idx org_node, clone_node;
     1465  bool ok;
    14311466  unsigned int constraint = init_constraint;
    14321467  for (org_node = top_org_node, clone_node = top_clone_node;;)
    14331468    {
    1434       int org_dest, clone_dest;
     1469      Idx org_dest, clone_dest;
    14351470      if (dfa->nodes[org_node].type == OP_BACK_REF)
    14361471        {
     
    14421477          re_node_set_empty (dfa->edests + clone_node);
    14431478          clone_dest = duplicate_node (dfa, org_dest, constraint);
    1444           if (BE (clone_dest == -1, 0))
     1479          if (__glibc_unlikely (clone_dest == -1))
    14451480            return REG_ESPACE;
    14461481          dfa->nexts[clone_node] = dfa->nexts[org_node];
    1447           ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
    1448           if (BE (ret < 0, 0))
     1482          ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
     1483          if (__glibc_unlikely (! ok))
    14491484            return REG_ESPACE;
    14501485        }
     
    14631498          org_dest = dfa->edests[org_node].elems[0];
    14641499          re_node_set_empty (dfa->edests + clone_node);
    1465           if (dfa->nodes[org_node].type == ANCHOR)
     1500          /* If the node is root_node itself, it means the epsilon closure
     1501             has a loop.  Then tie it to the destination of the root_node.  */
     1502          if (org_node == root_node && clone_node != org_node)
    14661503            {
    1467               /* In case of the node has another constraint, append it.  */
    1468               if (org_node == root_node && clone_node != org_node)
    1469                 {
    1470                   /* ...but if the node is root_node itself, it means the
    1471                      epsilon closure have a loop, then tie it to the
    1472                      destination of the root_node.  */
    1473                   ret = re_node_set_insert (dfa->edests + clone_node,
    1474                                             org_dest);
    1475                   if (BE (ret < 0, 0))
    1476                     return REG_ESPACE;
    1477                   break;
    1478                 }
    1479               constraint |= dfa->nodes[org_node].opr.ctx_type;
     1504              ok = re_node_set_insert (dfa->edests + clone_node, org_dest);
     1505              if (__glibc_unlikely (! ok))
     1506                return REG_ESPACE;
     1507              break;
    14801508            }
     1509          /* In case the node has another constraint, append it.  */
     1510          constraint |= dfa->nodes[org_node].constraint;
    14811511          clone_dest = duplicate_node (dfa, org_dest, constraint);
    1482           if (BE (clone_dest == -1, 0))
     1512          if (__glibc_unlikely (clone_dest == -1))
    14831513            return REG_ESPACE;
    1484           ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
    1485           if (BE (ret < 0, 0))
     1514          ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
     1515          if (__glibc_unlikely (! ok))
    14861516            return REG_ESPACE;
    14871517        }
     
    14961526          if (clone_dest == -1)
    14971527            {
    1498               /* There are no such a duplicated node, create a new one.  */
     1528              /* There is no such duplicated node, create a new one.  */
    14991529              reg_errcode_t err;
    15001530              clone_dest = duplicate_node (dfa, org_dest, constraint);
    1501               if (BE (clone_dest == -1, 0))
     1531              if (__glibc_unlikely (clone_dest == -1))
    15021532                return REG_ESPACE;
    1503               ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
    1504               if (BE (ret < 0, 0))
     1533              ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
     1534              if (__glibc_unlikely (! ok))
    15051535                return REG_ESPACE;
    15061536              err = duplicate_node_closure (dfa, org_dest, clone_dest,
    15071537                                            root_node, constraint);
    1508               if (BE (err != REG_NOERROR, 0))
     1538              if (__glibc_unlikely (err != REG_NOERROR))
    15091539                return err;
    15101540            }
    15111541          else
    15121542            {
    1513               /* There are a duplicated node which satisfy the constraint,
     1543              /* There is a duplicated node which satisfies the constraint,
    15141544                 use it to avoid infinite loop.  */
    1515               ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
    1516               if (BE (ret < 0, 0))
     1545              ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
     1546              if (__glibc_unlikely (! ok))
    15171547                return REG_ESPACE;
    15181548            }
     
    15201550          org_dest = dfa->edests[org_node].elems[1];
    15211551          clone_dest = duplicate_node (dfa, org_dest, constraint);
    1522           if (BE (clone_dest == -1, 0))
     1552          if (__glibc_unlikely (clone_dest == -1))
    15231553            return REG_ESPACE;
    1524           ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
    1525           if (BE (ret < 0, 0))
     1554          ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
     1555          if (__glibc_unlikely (! ok))
    15261556            return REG_ESPACE;
    15271557        }
     
    15351565   satisfies the constraint CONSTRAINT.  */
    15361566
    1537 static int
    1538 search_duplicated_node (const re_dfa_t *dfa, int org_node,
     1567static Idx
     1568search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
    15391569                        unsigned int constraint)
    15401570{
    1541   int idx;
     1571  Idx idx;
    15421572  for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
    15431573    {
     
    15531583   available.  */
    15541584
    1555 static int
    1556 duplicate_node (re_dfa_t *dfa, int org_idx, unsigned int constraint)
    1557 {
    1558   int dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
    1559   if (BE (dup_idx != -1, 1))
     1585static Idx
     1586duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint)
     1587{
     1588  Idx dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
     1589  if (__glibc_likely (dup_idx != -1))
    15601590    {
    15611591      dfa->nodes[dup_idx].constraint = constraint;
    1562       if (dfa->nodes[org_idx].type == ANCHOR)
    1563         dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
     1592      dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].constraint;
    15641593      dfa->nodes[dup_idx].duplicated = 1;
    15651594
     
    15731602calc_inveclosure (re_dfa_t *dfa)
    15741603{
    1575   int src, idx, ret;
     1604  Idx src, idx;
     1605  bool ok;
    15761606  for (idx = 0; idx < dfa->nodes_len; ++idx)
    15771607    re_node_set_init_empty (dfa->inveclosures + idx);
     
    15791609  for (src = 0; src < dfa->nodes_len; ++src)
    15801610    {
    1581       int *elems = dfa->eclosures[src].elems;
     1611      Idx *elems = dfa->eclosures[src].elems;
    15821612      for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
    15831613        {
    1584           ret = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
    1585           if (BE (ret == -1, 0))
     1614          ok = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
     1615          if (__glibc_unlikely (! ok))
    15861616            return REG_ESPACE;
    15871617        }
     
    15961626calc_eclosure (re_dfa_t *dfa)
    15971627{
    1598   int node_idx, incomplete;
    1599 #ifdef DEBUG
    1600   assert (dfa->nodes_len > 0);
    1601 #endif
    1602   incomplete = 0;
     1628  Idx node_idx;
     1629  bool incomplete;
     1630  DEBUG_ASSERT (dfa->nodes_len > 0);
     1631  incomplete = false;
    16031632  /* For each nodes, calculate epsilon closure.  */
    16041633  for (node_idx = 0; ; ++node_idx)
     
    16101639          if (!incomplete)
    16111640            break;
    1612           incomplete = 0;
     1641          incomplete = false;
    16131642          node_idx = 0;
    16141643        }
    16151644
    1616 #ifdef DEBUG
    1617       assert (dfa->eclosures[node_idx].nelem != -1);
    1618 #endif
     1645      DEBUG_ASSERT (dfa->eclosures[node_idx].nelem != -1);
    16191646
    16201647      /* If we have already calculated, skip it.  */
    16211648      if (dfa->eclosures[node_idx].nelem != 0)
    16221649        continue;
    1623       /* Calculate epsilon closure of `node_idx'.  */
    1624       err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1);
    1625       if (BE (err != REG_NOERROR, 0))
     1650      /* Calculate epsilon closure of 'node_idx'.  */
     1651      err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, true);
     1652      if (__glibc_unlikely (err != REG_NOERROR))
    16261653        return err;
    16271654
    16281655      if (dfa->eclosures[node_idx].nelem == 0)
    16291656        {
    1630           incomplete = 1;
     1657          incomplete = true;
    16311658          re_node_set_free (&eclosure_elem);
    16321659        }
     
    16381665
    16391666static reg_errcode_t
    1640 calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, int node, int root)
     1667calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, Idx node, bool root)
    16411668{
    16421669  reg_errcode_t err;
    1643   unsigned int constraint;
    1644   int i, incomplete;
     1670  Idx i;
    16451671  re_node_set eclosure;
    1646   incomplete = 0;
     1672  bool incomplete = false;
    16471673  err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
    1648   if (BE (err != REG_NOERROR, 0))
     1674  if (__glibc_unlikely (err != REG_NOERROR))
    16491675    return err;
     1676
     1677  /* An epsilon closure includes itself.  */
     1678  eclosure.elems[eclosure.nelem++] = node;
    16501679
    16511680  /* This indicates that we are calculating this node now.
     
    16531682  dfa->eclosures[node].nelem = -1;
    16541683
    1655   constraint = ((dfa->nodes[node].type == ANCHOR)
    1656                 ? dfa->nodes[node].opr.ctx_type : 0);
    1657   /* If the current node has constraints, duplicate all nodes.
    1658      Since they must inherit the constraints.  */
    1659   if (constraint
     1684  /* If the current node has constraints, duplicate all nodes
     1685     since they must inherit the constraints.  */
     1686  if (dfa->nodes[node].constraint
    16601687      && dfa->edests[node].nelem
    16611688      && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
    16621689    {
    1663       int org_node, cur_node;
    1664       org_node = cur_node = node;
    1665       err = duplicate_node_closure (dfa, node, node, node, constraint);
    1666       if (BE (err != REG_NOERROR, 0))
     1690      err = duplicate_node_closure (dfa, node, node, node,
     1691                                    dfa->nodes[node].constraint);
     1692      if (__glibc_unlikely (err != REG_NOERROR))
    16671693        return err;
    16681694    }
     
    16731699      {
    16741700        re_node_set eclosure_elem;
    1675         int edest = dfa->edests[node].elems[i];
    1676         /* If calculating the epsilon closure of `edest' is in progress,
     1701        Idx edest = dfa->edests[node].elems[i];
     1702        /* If calculating the epsilon closure of 'edest' is in progress,
    16771703           return intermediate result.  */
    16781704        if (dfa->eclosures[edest].nelem == -1)
    16791705          {
    1680             incomplete = 1;
     1706            incomplete = true;
    16811707            continue;
    16821708          }
    1683         /* If we haven't calculated the epsilon closure of `edest' yet,
     1709        /* If we haven't calculated the epsilon closure of 'edest' yet,
    16841710           calculate now. Otherwise use calculated epsilon closure.  */
    16851711        if (dfa->eclosures[edest].nelem == 0)
    16861712          {
    1687             err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0);
    1688             if (BE (err != REG_NOERROR, 0))
     1713            err = calc_eclosure_iter (&eclosure_elem, dfa, edest, false);
     1714            if (__glibc_unlikely (err != REG_NOERROR))
    16891715              return err;
    16901716          }
    16911717        else
    16921718          eclosure_elem = dfa->eclosures[edest];
    1693         /* Merge the epsilon closure of `edest'.  */
    1694         re_node_set_merge (&eclosure, &eclosure_elem);
    1695         /* If the epsilon closure of `edest' is incomplete,
     1719        /* Merge the epsilon closure of 'edest'.  */
     1720        err = re_node_set_merge (&eclosure, &eclosure_elem);
     1721        if (__glibc_unlikely (err != REG_NOERROR))
     1722          return err;
     1723        /* If the epsilon closure of 'edest' is incomplete,
    16961724           the epsilon closure of this node is also incomplete.  */
    16971725        if (dfa->eclosures[edest].nelem == 0)
    16981726          {
    1699             incomplete = 1;
     1727            incomplete = true;
    17001728            re_node_set_free (&eclosure_elem);
    17011729          }
    17021730      }
    17031731
    1704   /* Epsilon closures include itself.  */
    1705   re_node_set_insert (&eclosure, node);
    17061732  if (incomplete && !root)
    17071733    dfa->eclosures[node].nelem = 0;
     
    17191745
    17201746static void
    1721 internal_function
    17221747fetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax)
    17231748{
     
    17291754
    17301755static int
    1731 internal_function
    17321756peek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
    17331757{
     
    17441768
    17451769  token->word_char = 0;
    1746 #ifdef RE_ENABLE_I18N
    17471770  token->mb_partial = 0;
    1748   if (input->mb_cur_max > 1 &&
    1749       !re_string_first_byte (input, re_string_cur_idx (input)))
     1771  if (input->mb_cur_max > 1
     1772      && !re_string_first_byte (input, re_string_cur_idx (input)))
    17501773    {
    17511774      token->type = CHARACTER;
     
    17531776      return 1;
    17541777    }
    1755 #endif
    17561778  if (c == '\\')
    17571779    {
     
    17661788      token->opr.c = c2;
    17671789      token->type = CHARACTER;
    1768 #ifdef RE_ENABLE_I18N
    17691790      if (input->mb_cur_max > 1)
    17701791        {
     
    17741795        }
    17751796      else
    1776 #endif
    17771797        token->word_char = IS_WORD_CHAR (c2) != 0;
    17781798
     
    18801900
    18811901  token->type = CHARACTER;
    1882 #ifdef RE_ENABLE_I18N
    18831902  if (input->mb_cur_max > 1)
    18841903    {
     
    18871906    }
    18881907  else
    1889 #endif
    18901908    token->word_char = IS_WORD_CHAR (token->opr.c);
    18911909
     
    19341952      break;
    19351953    case '^':
    1936       if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) &&
    1937           re_string_cur_idx (input) != 0)
     1954      if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE))
     1955          && re_string_cur_idx (input) != 0)
    19381956        {
    19391957          char prev = re_string_peek_byte (input, -1);
     
    19451963      break;
    19461964    case '$':
    1947       if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
    1948           re_string_cur_idx (input) + 1 != re_string_length (input))
     1965      if (!(syntax & RE_CONTEXT_INDEP_ANCHORS)
     1966          && re_string_cur_idx (input) + 1 != re_string_length (input))
    19491967        {
    19501968          re_token_t next;
     
    19681986
    19691987static int
    1970 internal_function
    19711988peek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
    19721989{
     
    19801997  token->opr.c = c;
    19811998
    1982 #ifdef RE_ENABLE_I18N
    1983   if (input->mb_cur_max > 1 &&
    1984       !re_string_first_byte (input, re_string_cur_idx (input)))
     1999  if (input->mb_cur_max > 1
     2000      && !re_string_first_byte (input, re_string_cur_idx (input)))
    19852001    {
    19862002      token->type = CHARACTER;
    19872003      return 1;
    19882004    }
    1989 #endif /* RE_ENABLE_I18N */
    19902005
    19912006  if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS)
     
    20152030          token->type = OP_OPEN_COLL_ELEM;
    20162031          break;
     2032
    20172033        case '=':
    20182034          token->type = OP_OPEN_EQUIV_CLASS;
    20192035          break;
     2036
    20202037        case ':':
    20212038          if (syntax & RE_CHAR_CLASSES)
     
    20242041              break;
    20252042            }
    2026           /* else fall through.  */
     2043          FALLTHROUGH;
    20272044        default:
    20282045          token->type = CHARACTER;
     
    20352052  switch (c)
    20362053    {
    2037     case '-':
    2038       token->type = OP_CHARSET_RANGE;
    2039       break;
    20402054    case ']':
    20412055      token->type = OP_CLOSE_BRACKET;
     
    20442058      token->type = OP_NON_MATCH_LIST;
    20452059      break;
     2060    case '-':
     2061      /* In V7 Unix grep and Unix awk and mawk, [...---...]
     2062         (3 adjacent minus signs) stands for a single minus sign.
     2063         Support that without breaking anything else.  */
     2064      if (! (re_string_cur_idx (input) + 2 < re_string_length (input)
     2065             && re_string_peek_byte (input, 1) == '-'
     2066             && re_string_peek_byte (input, 2) == '-'))
     2067        {
     2068          token->type = OP_CHARSET_RANGE;
     2069          break;
     2070        }
     2071      re_string_skip_bytes (input, 2);
     2072      FALLTHROUGH;
    20462073    default:
    20472074      token->type = CHARACTER;
     
    20552082/* Entry point of the parser.
    20562083   Parse the regular expression REGEXP and return the structure tree.
    2057    If an error is occured, ERR is set by error code, and return NULL.
     2084   If an error occurs, ERR is set by error code, and return NULL.
    20582085   This function build the following tree, from regular expression <reg_exp>:
    20592086           CAT
     
    20692096       reg_errcode_t *err)
    20702097{
    2071   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     2098  re_dfa_t *dfa = preg->buffer;
    20722099  bin_tree_t *tree, *eor, *root;
    20732100  re_token_t current_token;
     
    20752102  fetch_token (&current_token, regexp, syntax | RE_CARET_ANCHORS_HERE);
    20762103  tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
    2077   if (BE (*err != REG_NOERROR && tree == NULL, 0))
     2104  if (__glibc_unlikely (*err != REG_NOERROR && tree == NULL))
    20782105    return NULL;
    20792106  eor = create_tree (dfa, NULL, NULL, END_OF_RE);
     
    20822109  else
    20832110    root = eor;
    2084   if (BE (eor == NULL || root == NULL, 0))
     2111  if (__glibc_unlikely (eor == NULL || root == NULL))
    20852112    {
    20862113      *err = REG_ESPACE;
     
    20972124   <branch1> <branch2>
    20982125
    2099    ALT means alternative, which represents the operator `|'.  */
     2126   ALT means alternative, which represents the operator '|'.  */
    21002127
    21012128static bin_tree_t *
    21022129parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
    2103                reg_syntax_t syntax, int nest, reg_errcode_t *err)
    2104 {
    2105   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     2130               reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
     2131{
     2132  re_dfa_t *dfa = preg->buffer;
    21062133  bin_tree_t *tree, *branch = NULL;
     2134  bitset_word_t initial_bkref_map = dfa->completed_bkref_map;
    21072135  tree = parse_branch (regexp, preg, token, syntax, nest, err);
    2108   if (BE (*err != REG_NOERROR && tree == NULL, 0))
     2136  if (__glibc_unlikely (*err != REG_NOERROR && tree == NULL))
    21092137    return NULL;
    21102138
     
    21152143          && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
    21162144        {
     2145          bitset_word_t accumulated_bkref_map = dfa->completed_bkref_map;
     2146          dfa->completed_bkref_map = initial_bkref_map;
    21172147          branch = parse_branch (regexp, preg, token, syntax, nest, err);
    2118           if (BE (*err != REG_NOERROR && branch == NULL, 0))
    2119             return NULL;
     2148          if (__glibc_unlikely (*err != REG_NOERROR && branch == NULL))
     2149            {
     2150              if (tree != NULL)
     2151                postorder (tree, free_tree, NULL);
     2152              return NULL;
     2153            }
     2154          dfa->completed_bkref_map |= accumulated_bkref_map;
    21202155        }
    21212156      else
    21222157        branch = NULL;
    21232158      tree = create_tree (dfa, tree, branch, OP_ALT);
    2124       if (BE (tree == NULL, 0))
     2159      if (__glibc_unlikely (tree == NULL))
    21252160        {
    21262161          *err = REG_ESPACE;
     
    21422177static bin_tree_t *
    21432178parse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token,
    2144               reg_syntax_t syntax, int nest, reg_errcode_t *err)
    2145 {
    2146   bin_tree_t *tree, *exp;
    2147   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     2179              reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
     2180{
     2181  bin_tree_t *tree, *expr;
     2182  re_dfa_t *dfa = preg->buffer;
    21482183  tree = parse_expression (regexp, preg, token, syntax, nest, err);
    2149   if (BE (*err != REG_NOERROR && tree == NULL, 0))
     2184  if (__glibc_unlikely (*err != REG_NOERROR && tree == NULL))
    21502185    return NULL;
    21512186
     
    21532188         && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
    21542189    {
    2155       exp = parse_expression (regexp, preg, token, syntax, nest, err);
    2156       if (BE (*err != REG_NOERROR && exp == NULL, 0))
    2157         {
     2190      expr = parse_expression (regexp, preg, token, syntax, nest, err);
     2191      if (__glibc_unlikely (*err != REG_NOERROR && expr == NULL))
     2192        {
     2193          if (tree != NULL)
     2194            postorder (tree, free_tree, NULL);
    21582195          return NULL;
    21592196        }
    2160       if (tree != NULL && exp != NULL)
    2161         {
    2162           tree = create_tree (dfa, tree, exp, CONCAT);
    2163           if (tree == NULL)
     2197      if (tree != NULL && expr != NULL)
     2198        {
     2199          bin_tree_t *newtree = create_tree (dfa, tree, expr, CONCAT);
     2200          if (newtree == NULL)
    21642201            {
     2202              postorder (expr, free_tree, NULL);
     2203              postorder (tree, free_tree, NULL);
    21652204              *err = REG_ESPACE;
    21662205              return NULL;
    21672206            }
     2207          tree = newtree;
    21682208        }
    21692209      else if (tree == NULL)
    2170         tree = exp;
    2171       /* Otherwise exp == NULL, we don't need to create new tree.  */
     2210        tree = expr;
     2211      /* Otherwise expr == NULL, we don't need to create new tree.  */
    21722212    }
    21732213  return tree;
     
    21822222static bin_tree_t *
    21832223parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
    2184                   reg_syntax_t syntax, int nest, reg_errcode_t *err)
    2185 {
    2186   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     2224                  reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
     2225{
     2226  re_dfa_t *dfa = preg->buffer;
    21872227  bin_tree_t *tree;
    21882228  switch (token->type)
     
    21902230    case CHARACTER:
    21912231      tree = create_token_tree (dfa, NULL, NULL, token);
    2192       if (BE (tree == NULL, 0))
     2232      if (__glibc_unlikely (tree == NULL))
    21932233        {
    21942234          *err = REG_ESPACE;
    21952235          return NULL;
    21962236        }
    2197 #ifdef RE_ENABLE_I18N
    21982237      if (dfa->mb_cur_max > 1)
    21992238        {
     
    22052244              mbc_remain = create_token_tree (dfa, NULL, NULL, token);
    22062245              tree = create_tree (dfa, tree, mbc_remain, CONCAT);
    2207               if (BE (mbc_remain == NULL || tree == NULL, 0))
     2246              if (__glibc_unlikely (mbc_remain == NULL || tree == NULL))
    22082247                {
    22092248                  *err = REG_ESPACE;
     
    22122251            }
    22132252        }
    2214 #endif
    22152253      break;
     2254
    22162255    case OP_OPEN_SUBEXP:
    22172256      tree = parse_sub_exp (regexp, preg, token, syntax, nest + 1, err);
    2218       if (BE (*err != REG_NOERROR && tree == NULL, 0))
     2257      if (__glibc_unlikely (*err != REG_NOERROR && tree == NULL))
    22192258        return NULL;
    22202259      break;
     2260
    22212261    case OP_OPEN_BRACKET:
    22222262      tree = parse_bracket_exp (regexp, dfa, token, syntax, err);
    2223       if (BE (*err != REG_NOERROR && tree == NULL, 0))
     2263      if (__glibc_unlikely (*err != REG_NOERROR && tree == NULL))
    22242264        return NULL;
    22252265      break;
     2266
    22262267    case OP_BACK_REF:
    2227       if (!BE (dfa->completed_bkref_map & (1 << token->opr.idx), 1))
     2268      if (!__glibc_likely (dfa->completed_bkref_map & (1 << token->opr.idx)))
    22282269        {
    22292270          *err = REG_ESUBREG;
     
    22322273      dfa->used_bkref_map |= 1 << token->opr.idx;
    22332274      tree = create_token_tree (dfa, NULL, NULL, token);
    2234       if (BE (tree == NULL, 0))
     2275      if (__glibc_unlikely (tree == NULL))
    22352276        {
    22362277          *err = REG_ESPACE;
     
    22402281      dfa->has_mb_node = 1;
    22412282      break;
     2283
    22422284    case OP_OPEN_DUP_NUM:
    22432285      if (syntax & RE_CONTEXT_INVALID_DUP)
     
    22462288          return NULL;
    22472289        }
    2248       /* FALLTHROUGH */
     2290      FALLTHROUGH;
    22492291    case OP_DUP_ASTERISK:
    22502292    case OP_DUP_PLUS:
     
    22602302          return parse_expression (regexp, preg, token, syntax, nest, err);
    22612303        }
    2262       /* else fall through  */
     2304      FALLTHROUGH;
    22632305    case OP_CLOSE_SUBEXP:
    2264       if ((token->type == OP_CLOSE_SUBEXP) &&
    2265           !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
     2306      if ((token->type == OP_CLOSE_SUBEXP)
     2307          && !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
    22662308        {
    22672309          *err = REG_ERPAREN;
    22682310          return NULL;
    22692311        }
    2270       /* else fall through  */
     2312      FALLTHROUGH;
    22712313    case OP_CLOSE_DUP_NUM:
    22722314      /* We treat it as a normal character.  */
     
    22772319         by peek_token.  */
    22782320      tree = create_token_tree (dfa, NULL, NULL, token);
    2279       if (BE (tree == NULL, 0))
     2321      if (__glibc_unlikely (tree == NULL))
    22802322        {
    22812323          *err = REG_ESPACE;
     
    22832325        }
    22842326      break;
     2327
    22852328    case ANCHOR:
    22862329      if ((token->opr.ctx_type
     
    22892332        init_word_char (dfa);
    22902333      if (token->opr.ctx_type == WORD_DELIM
    2291           || token->opr.ctx_type == NOT_WORD_DELIM)
     2334          || token->opr.ctx_type == NOT_WORD_DELIM)
    22922335        {
    22932336          bin_tree_t *tree_first, *tree_last;
     
    22972340              tree_first = create_token_tree (dfa, NULL, NULL, token);
    22982341              token->opr.ctx_type = WORD_LAST;
    2299             }
    2300           else
    2301             {
     2342            }
     2343          else
     2344            {
    23022345              token->opr.ctx_type = INSIDE_WORD;
    23032346              tree_first = create_token_tree (dfa, NULL, NULL, token);
    23042347              token->opr.ctx_type = INSIDE_NOTWORD;
    2305             }
     2348            }
    23062349          tree_last = create_token_tree (dfa, NULL, NULL, token);
    23072350          tree = create_tree (dfa, tree_first, tree_last, OP_ALT);
    2308           if (BE (tree_first == NULL || tree_last == NULL || tree == NULL, 0))
     2351          if (__glibc_unlikely (tree_first == NULL || tree_last == NULL
     2352                                || tree == NULL))
    23092353            {
    23102354              *err = REG_ESPACE;
     
    23152359        {
    23162360          tree = create_token_tree (dfa, NULL, NULL, token);
    2317           if (BE (tree == NULL, 0))
     2361          if (__glibc_unlikely (tree == NULL))
    23182362            {
    23192363              *err = REG_ESPACE;
     
    23272371      fetch_token (token, regexp, syntax);
    23282372      return tree;
     2373
    23292374    case OP_PERIOD:
    23302375      tree = create_token_tree (dfa, NULL, NULL, token);
    2331       if (BE (tree == NULL, 0))
     2376      if (__glibc_unlikely (tree == NULL))
    23322377        {
    23332378          *err = REG_ESPACE;
     
    23372382        dfa->has_mb_node = 1;
    23382383      break;
     2384
    23392385    case OP_WORD:
    23402386    case OP_NOTWORD:
    23412387      tree = build_charclass_op (dfa, regexp->trans,
    2342                                  (const unsigned char *) "alnum",
    2343                                  (const unsigned char *) "_",
     2388                                 "alnum",
     2389                                 "_",
    23442390                                 token->type == OP_NOTWORD, err);
    2345       if (BE (*err != REG_NOERROR && tree == NULL, 0))
     2391      if (__glibc_unlikely (*err != REG_NOERROR && tree == NULL))
    23462392        return NULL;
    23472393      break;
     2394
    23482395    case OP_SPACE:
    23492396    case OP_NOTSPACE:
    23502397      tree = build_charclass_op (dfa, regexp->trans,
    2351                                  (const unsigned char *) "space",
    2352                                  (const unsigned char *) "",
     2398                                 "space",
     2399                                 "",
    23532400                                 token->type == OP_NOTSPACE, err);
    2354       if (BE (*err != REG_NOERROR && tree == NULL, 0))
     2401      if (__glibc_unlikely (*err != REG_NOERROR && tree == NULL))
    23552402        return NULL;
    23562403      break;
     2404
    23572405    case OP_ALT:
    23582406    case END_OF_RE:
    23592407      return NULL;
     2408
    23602409    case BACK_SLASH:
    23612410      *err = REG_EESCAPE;
    23622411      return NULL;
     2412
    23632413    default:
    23642414      /* Must not happen?  */
    2365 #ifdef DEBUG
    2366       assert (0);
    2367 #endif
     2415      DEBUG_ASSERT (false);
    23682416      return NULL;
    23692417    }
     
    23732421         || token->type == OP_DUP_QUESTION || token->type == OP_OPEN_DUP_NUM)
    23742422    {
    2375       tree = parse_dup_op (tree, regexp, dfa, token, syntax, err);
    2376       if (BE (*err != REG_NOERROR && tree == NULL, 0))
    2377         return NULL;
     2423      bin_tree_t *dup_tree = parse_dup_op (tree, regexp, dfa, token,
     2424                                           syntax, err);
     2425      if (__glibc_unlikely (*err != REG_NOERROR && dup_tree == NULL))
     2426        {
     2427          if (tree != NULL)
     2428            postorder (tree, free_tree, NULL);
     2429          return NULL;
     2430        }
     2431      tree = dup_tree;
    23782432      /* In BRE consecutive duplications are not allowed.  */
    23792433      if ((syntax & RE_CONTEXT_INVALID_DUP)
     
    23812435              || token->type == OP_OPEN_DUP_NUM))
    23822436        {
     2437          if (tree != NULL)
     2438            postorder (tree, free_tree, NULL);
    23832439          *err = REG_BADRPT;
    23842440          return NULL;
     
    23982454static bin_tree_t *
    23992455parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
    2400                reg_syntax_t syntax, int nest, reg_errcode_t *err)
    2401 {
    2402   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     2456               reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
     2457{
     2458  re_dfa_t *dfa = preg->buffer;
    24032459  bin_tree_t *tree;
    24042460  size_t cur_nsub;
     
    24132469    {
    24142470      tree = parse_reg_exp (regexp, preg, token, syntax, nest, err);
    2415       if (BE (*err == REG_NOERROR && token->type != OP_CLOSE_SUBEXP, 0))
    2416         *err = REG_EPAREN;
    2417       if (BE (*err != REG_NOERROR, 0))
     2471      if (__glibc_unlikely (*err == REG_NOERROR
     2472                            && token->type != OP_CLOSE_SUBEXP))
     2473        {
     2474          if (tree != NULL)
     2475            postorder (tree, free_tree, NULL);
     2476          *err = REG_EPAREN;
     2477        }
     2478      if (__glibc_unlikely (*err != REG_NOERROR))
    24182479        return NULL;
    24192480    }
     
    24232484
    24242485  tree = create_tree (dfa, tree, NULL, SUBEXP);
    2425   if (BE (tree == NULL, 0))
     2486  if (__glibc_unlikely (tree == NULL))
    24262487    {
    24272488      *err = REG_ESPACE;
     
    24392500{
    24402501  bin_tree_t *tree = NULL, *old_tree = NULL;
    2441   int i, start, end, start_idx = re_string_cur_idx (regexp);
     2502  Idx i, start, end, start_idx = re_string_cur_idx (regexp);
    24422503  re_token_t start_token = *token;
    24432504
     
    24562517            }
    24572518        }
    2458       if (BE (start != -2, 1))
     2519      if (__glibc_likely (start != -2))
    24592520        {
    24602521          /* We treat "{n}" as "{n,n}".  */
     
    24632524                    ? fetch_number (regexp, token, syntax) : -2));
    24642525        }
    2465       if (BE (start == -2 || end == -2, 0))
     2526      if (__glibc_unlikely (start == -2 || end == -2))
    24662527        {
    24672528          /* Invalid sequence.  */
    2468           if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
     2529          if (__glibc_unlikely (!(syntax & RE_INVALID_INTERVAL_ORD)))
    24692530            {
    24702531              if (token->type == END_OF_RE)
     
    24852546        }
    24862547
    2487       if (BE (end != -1 && start > end, 0))
     2548      if (__glibc_unlikely ((end != -1 && start > end)
     2549                            || token->type != OP_CLOSE_DUP_NUM))
    24882550        {
    24892551          /* First number greater than second.  */
     
    24912553          return NULL;
    24922554        }
     2555
     2556      if (__glibc_unlikely (RE_DUP_MAX < (end == -1 ? start : end)))
     2557        {
     2558          *err = REG_ESIZE;
     2559          return NULL;
     2560        }
    24932561    }
    24942562  else
     
    25002568  fetch_token (token, regexp, syntax);
    25012569
    2502   if (BE (elem == NULL, 0))
     2570  if (__glibc_unlikely (elem == NULL))
    25032571    return NULL;
    2504   if (BE (start == 0 && end == 0, 0))
     2572  if (__glibc_unlikely (start == 0 && end == 0))
    25052573    {
    25062574      postorder (elem, free_tree, NULL);
     
    25092577
    25102578  /* Extract "<re>{n,m}" to "<re><re>...<re><re>{0,<m-n>}".  */
    2511   if (BE (start > 0, 0))
     2579  if (__glibc_unlikely (start > 0))
    25122580    {
    25132581      tree = elem;
     
    25162584          elem = duplicate_tree (elem, dfa);
    25172585          tree = create_tree (dfa, tree, elem, CONCAT);
    2518           if (BE (elem == NULL || tree == NULL, 0))
     2586          if (__glibc_unlikely (elem == NULL || tree == NULL))
    25192587            goto parse_dup_op_espace;
    25202588        }
     
    25252593      /* Duplicate ELEM before it is marked optional.  */
    25262594      elem = duplicate_tree (elem, dfa);
     2595      if (__glibc_unlikely (elem == NULL))
     2596        goto parse_dup_op_espace;
    25272597      old_tree = tree;
    25282598    }
     
    25312601
    25322602  if (elem->token.type == SUBEXP)
    2533     postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
    2534 
    2535   tree = create_tree (dfa, elem, NULL, (end == -1 ? OP_DUP_ASTERISK : OP_ALT));
    2536   if (BE (tree == NULL, 0))
     2603    {
     2604      uintptr_t subidx = elem->token.opr.idx;
     2605      postorder (elem, mark_opt_subexp, (void *) subidx);
     2606    }
     2607
     2608  tree = create_tree (dfa, elem, NULL,
     2609                      (end == -1 ? OP_DUP_ASTERISK : OP_ALT));
     2610  if (__glibc_unlikely (tree == NULL))
    25372611    goto parse_dup_op_espace;
    25382612
     
    25402614     to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?...  We have
    25412615     already created the start+1-th copy.  */
    2542   for (i = start + 2; i <= end; ++i)
    2543     {
    2544       elem = duplicate_tree (elem, dfa);
    2545       tree = create_tree (dfa, tree, elem, CONCAT);
    2546       if (BE (elem == NULL || tree == NULL, 0))
    2547         goto parse_dup_op_espace;
    2548 
    2549       tree = create_tree (dfa, tree, NULL, OP_ALT);
    2550       if (BE (tree == NULL, 0))
    2551         goto parse_dup_op_espace;
    2552     }
     2616  if (TYPE_SIGNED (Idx) || end != -1)
     2617    for (i = start + 2; i <= end; ++i)
     2618      {
     2619        elem = duplicate_tree (elem, dfa);
     2620        tree = create_tree (dfa, tree, elem, CONCAT);
     2621        if (__glibc_unlikely (elem == NULL || tree == NULL))
     2622          goto parse_dup_op_espace;
     2623
     2624        tree = create_tree (dfa, tree, NULL, OP_ALT);
     2625        if (__glibc_unlikely (tree == NULL))
     2626          goto parse_dup_op_espace;
     2627      }
    25532628
    25542629  if (old_tree)
     
    25672642
    25682643#ifndef _LIBC
    2569   /* Local function for parse_bracket_exp only used in case of NOT _LIBC.
    2570      Build the range expression which starts from START_ELEM, and ends
    2571      at END_ELEM.  The result are written to MBCSET and SBCSET.
    2572      RANGE_ALLOC is the allocated size of mbcset->range_starts, and
    2573      mbcset->range_ends, is a pointer argument sinse we may
    2574      update it.  */
     2644
     2645/* Convert the byte B to the corresponding wide character.  In a
     2646   unibyte locale, treat B as itself.  In a multibyte locale, return
     2647   WEOF if B is an encoding error.  */
     2648static wint_t
     2649parse_byte (unsigned char b, re_dfa_t const *dfa)
     2650{
     2651  return dfa->mb_cur_max > 1 ? __btowc (b) : b;
     2652}
     2653
     2654/* Local function for parse_bracket_exp used in _LIBC environment.
     2655   Build the range expression which starts from START_ELEM, and ends
     2656   at END_ELEM.  The result are written to MBCSET and SBCSET.
     2657   RANGE_ALLOC is the allocated size of mbcset->range_starts, and
     2658   mbcset->range_ends, is a pointer argument since we may
     2659   update it.  */
    25752660
    25762661static reg_errcode_t
    2577 internal_function
    2578 # ifdef RE_ENABLE_I18N
    2579 build_range_exp (bitset_t sbcset, re_charset_t *mbcset, int *range_alloc,
    2580                  bracket_elem_t *start_elem, bracket_elem_t *end_elem)
    2581 # else /* not RE_ENABLE_I18N */
    2582 build_range_exp (bitset_t sbcset, bracket_elem_t *start_elem,
    2583                  bracket_elem_t *end_elem)
    2584 # endif /* not RE_ENABLE_I18N */
    2585 {
    2586   unsigned int start_ch, end_ch;
     2662build_range_exp (bitset_t sbcset, re_charset_t *mbcset, Idx *range_alloc,
     2663                 bracket_elem_t *start_elem, bracket_elem_t *end_elem,
     2664                 re_dfa_t *dfa, reg_syntax_t syntax, uint_fast32_t nrules,
     2665                 const unsigned char *collseqmb, const char *collseqwc,
     2666                 int_fast32_t table_size, const void *symb_table,
     2667                 const unsigned char *extra)
     2668{
    25872669  /* Equivalence Classes and Character Classes can't be a range start/end.  */
    2588   if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
    2589           || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
    2590           0))
     2670  if (__glibc_unlikely (start_elem->type == EQUIV_CLASS
     2671                        || start_elem->type == CHAR_CLASS
     2672                        || end_elem->type == EQUIV_CLASS
     2673                        || end_elem->type == CHAR_CLASS))
    25912674    return REG_ERANGE;
    25922675
    25932676  /* We can handle no multi character collating elements without libc
    25942677     support.  */
    2595   if (BE ((start_elem->type == COLL_SYM
    2596           && strlen ((char *) start_elem->opr.name) > 1)
    2597           || (end_elem->type == COLL_SYM
    2598               && strlen ((char *) end_elem->opr.name) > 1), 0))
     2678  if (__glibc_unlikely ((start_elem->type == COLL_SYM
     2679                        && strlen ((char *) start_elem->opr.name) > 1)
     2680                        || (end_elem->type == COLL_SYM
     2681                            && strlen ((char *) end_elem->opr.name) > 1)))
    25992682    return REG_ECOLLATE;
    26002683
    2601 # ifdef RE_ENABLE_I18N
    2602   {
    2603     wchar_t wc;
    2604     wint_t start_wc;
    2605     wint_t end_wc;
    2606     wchar_t cmp_buf[6] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
    2607 
     2684  unsigned int
    26082685    start_ch = ((start_elem->type == SB_CHAR) ? start_elem->opr.ch
    26092686                : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
    2610                    : 0));
     2687                   : 0)),
    26112688    end_ch = ((end_elem->type == SB_CHAR) ? end_elem->opr.ch
    26122689              : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
    26132690                 : 0));
     2691  wint_t
    26142692    start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM)
    2615                 ? __btowc (start_ch) : start_elem->opr.wch);
     2693                ? parse_byte (start_ch, dfa) : start_elem->opr.wch),
    26162694    end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM)
    2617               ? __btowc (end_ch) : end_elem->opr.wch);
    2618     if (start_wc == WEOF || end_wc == WEOF)
    2619       return REG_ECOLLATE;
    2620     cmp_buf[0] = start_wc;
    2621     cmp_buf[4] = end_wc;
    2622     if (wcscoll (cmp_buf, cmp_buf + 4) > 0)
    2623       return REG_ERANGE;
    2624 
    2625     /* Got valid collation sequence values, add them as a new entry.
    2626        However, for !_LIBC we have no collation elements: if the
    2627        character set is single byte, the single byte character set
    2628        that we build below suffices.  parse_bracket_exp passes
    2629        no MBCSET if dfa->mb_cur_max == 1.  */
    2630     if (mbcset)
    2631       {
    2632         /* Check the space of the arrays.  */
    2633         if (BE (*range_alloc == mbcset->nranges, 0))
    2634           {
    2635             /* There is not enough space, need realloc.  */
    2636             wchar_t *new_array_start, *new_array_end;
    2637             int new_nranges;
    2638 
    2639             /* +1 in case of mbcset->nranges is 0.  */
    2640             new_nranges = 2 * mbcset->nranges + 1;
    2641             /* Use realloc since mbcset->range_starts and mbcset->range_ends
    2642                are NULL if *range_alloc == 0.  */
    2643             new_array_start = re_realloc (mbcset->range_starts, wchar_t,
    2644                                           new_nranges);
    2645             new_array_end = re_realloc (mbcset->range_ends, wchar_t,
    2646                                         new_nranges);
    2647 
    2648             if (BE (new_array_start == NULL || new_array_end == NULL, 0))
    2649               return REG_ESPACE;
    2650 
    2651             mbcset->range_starts = new_array_start;
    2652             mbcset->range_ends = new_array_end;
    2653             *range_alloc = new_nranges;
    2654           }
    2655 
    2656         mbcset->range_starts[mbcset->nranges] = start_wc;
    2657         mbcset->range_ends[mbcset->nranges++] = end_wc;
    2658       }
    2659 
    2660     /* Build the table for single byte characters.  */
    2661     for (wc = 0; wc < SBC_MAX; ++wc)
    2662       {
    2663         cmp_buf[2] = wc;
    2664         if (wcscoll (cmp_buf, cmp_buf + 2) <= 0
    2665             && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0)
    2666           bitset_set (sbcset, wc);
    2667       }
    2668   }
    2669 # else /* not RE_ENABLE_I18N */
    2670   {
    2671     unsigned int ch;
    2672     start_ch = ((start_elem->type == SB_CHAR ) ? start_elem->opr.ch
    2673                 : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
    2674                    : 0));
    2675     end_ch = ((end_elem->type == SB_CHAR ) ? end_elem->opr.ch
    2676               : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
    2677                  : 0));
    2678     if (start_ch > end_ch)
    2679       return REG_ERANGE;
    2680     /* Build the table for single byte characters.  */
    2681     for (ch = 0; ch < SBC_MAX; ++ch)
    2682       if (start_ch <= ch  && ch <= end_ch)
    2683         bitset_set (sbcset, ch);
    2684   }
    2685 # endif /* not RE_ENABLE_I18N */
     2695              ? parse_byte (end_ch, dfa) : end_elem->opr.wch);
     2696
     2697  if (start_wc == WEOF || end_wc == WEOF)
     2698    return REG_ECOLLATE;
     2699  else if (__glibc_unlikely ((syntax & RE_NO_EMPTY_RANGES)
     2700                             && start_wc > end_wc))
     2701    return REG_ERANGE;
     2702
     2703  /* Got valid collation sequence values, add them as a new entry.
     2704     However, for !_LIBC we have no collation elements: if the
     2705     character set is single byte, the single byte character set
     2706     that we build below suffices.  parse_bracket_exp passes
     2707     no MBCSET if dfa->mb_cur_max == 1.  */
     2708  if (dfa->mb_cur_max > 1)
     2709    {
     2710      /* Check the space of the arrays.  */
     2711      if (__glibc_unlikely (*range_alloc == mbcset->nranges))
     2712        {
     2713          /* There is not enough space, need realloc.  */
     2714          wchar_t *new_array_start, *new_array_end;
     2715          Idx new_nranges;
     2716
     2717          /* +1 in case of mbcset->nranges is 0.  */
     2718          new_nranges = 2 * mbcset->nranges + 1;
     2719          /* Use realloc since mbcset->range_starts and mbcset->range_ends
     2720             are NULL if *range_alloc == 0.  */
     2721          new_array_start = re_realloc (mbcset->range_starts, wchar_t,
     2722                                        new_nranges);
     2723          new_array_end = re_realloc (mbcset->range_ends, wchar_t,
     2724                                      new_nranges);
     2725
     2726          if (__glibc_unlikely (new_array_start == NULL
     2727                                || new_array_end == NULL))
     2728            {
     2729              re_free (new_array_start);
     2730              re_free (new_array_end);
     2731              return REG_ESPACE;
     2732            }
     2733
     2734          mbcset->range_starts = new_array_start;
     2735          mbcset->range_ends = new_array_end;
     2736          *range_alloc = new_nranges;
     2737        }
     2738
     2739      mbcset->range_starts[mbcset->nranges] = start_wc;
     2740      mbcset->range_ends[mbcset->nranges++] = end_wc;
     2741    }
     2742
     2743  /* Build the table for single byte characters.  */
     2744  for (wchar_t wc = 0; wc < SBC_MAX; ++wc)
     2745    {
     2746      if (start_wc <= wc && wc <= end_wc)
     2747        bitset_set (sbcset, wc);
     2748    }
     2749
    26862750  return REG_NOERROR;
    26872751}
     
    26892753
    26902754#ifndef _LIBC
    2691 /* Helper function for parse_bracket_exp only used in case of NOT _LIBC..
     2755/* Helper function for parse_bracket_exp only used in case of NOT _LIBC.
    26922756   Build the collating element which is represented by NAME.
    26932757   The result are written to MBCSET and SBCSET.
     
    26962760
    26972761static reg_errcode_t
    2698 internal_function
    2699 # ifdef RE_ENABLE_I18N
    27002762build_collating_symbol (bitset_t sbcset, re_charset_t *mbcset,
    2701                         int *coll_sym_alloc, const unsigned char *name)
    2702 # else /* not RE_ENABLE_I18N */
    2703 build_collating_symbol (bitset_t sbcset, const unsigned char *name)
    2704 # endif /* not RE_ENABLE_I18N */
     2763                        Idx *coll_sym_alloc, const unsigned char *name,
     2764                        uint_fast32_t nrules, int_fast32_t table_size,
     2765                        const void *symb_table, const unsigned char *extra)
    27052766{
    27062767  size_t name_len = strlen ((const char *) name);
    2707   if (BE (name_len != 1, 0))
     2768  if (__glibc_unlikely (name_len != 1))
    27082769    return REG_ECOLLATE;
    27092770  else
     
    27152776#endif /* not _LIBC */
    27162777
    2717 /* This function parse bracket expression like "[abc]", "[a-c]",
    2718    "[[.a-a.]]" etc.  */
    2719 
    2720 static bin_tree_t *
    2721 parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
    2722                    reg_syntax_t syntax, reg_errcode_t *err)
    2723 {
    27242778#ifdef _LIBC
    2725   const unsigned char *collseqmb;
    2726   const char *collseqwc;
    2727   uint32_t nrules;
    2728   int32_t table_size;
    2729   const int32_t *symb_table;
    2730   const unsigned char *extra;
    2731 
    2732   /* Local function for parse_bracket_exp used in _LIBC environement.
    2733      Seek the collating symbol entry correspondings to NAME.
    2734      Return the index of the symbol in the SYMB_TABLE.  */
    2735 
    2736   auto inline int32_t
    2737   __attribute ((always_inline))
    2738   seek_collating_symbol_entry (name, name_len)
    2739          const unsigned char *name;
    2740          size_t name_len;
    2741     {
    2742       int32_t hash = elem_hash ((const char *) name, name_len);
    2743       int32_t elem = hash % table_size;
    2744       if (symb_table[2 * elem] != 0)
    2745         {
    2746           int32_t second = hash % (table_size - 2) + 1;
    2747 
    2748           do
    2749             {
    2750               /* First compare the hashing value.  */
    2751               if (symb_table[2 * elem] == hash
    2752                   /* Compare the length of the name.  */
    2753                   && name_len == extra[symb_table[2 * elem + 1]]
    2754                   /* Compare the name.  */
    2755                   && memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
    2756                              name_len) == 0)
    2757                 {
    2758                   /* Yep, this is the entry.  */
    2759                   break;
    2760                 }
    2761 
    2762               /* Next entry.  */
    2763               elem += second;
    2764             }
    2765           while (symb_table[2 * elem] != 0);
    2766         }
    2767       return elem;
    2768     }
    2769 
    2770   /* Local function for parse_bracket_exp used in _LIBC environement.
    2771      Look up the collation sequence value of BR_ELEM.
    2772      Return the value if succeeded, UINT_MAX otherwise.  */
    2773 
    2774   auto inline unsigned int
    2775   __attribute ((always_inline))
    2776   lookup_collation_sequence_value (br_elem)
    2777          bracket_elem_t *br_elem;
    2778     {
    2779       if (br_elem->type == SB_CHAR)
    2780         {
    2781           /*
    2782           if (MB_CUR_MAX == 1)
    2783           */
    2784           if (nrules == 0)
    2785             return collseqmb[br_elem->opr.ch];
    2786           else
    2787             {
    2788               wint_t wc = __btowc (br_elem->opr.ch);
    2789               return __collseq_table_lookup (collseqwc, wc);
    2790             }
    2791         }
    2792       else if (br_elem->type == MB_CHAR)
    2793         {
    2794           return __collseq_table_lookup (collseqwc, br_elem->opr.wch);
    2795         }
    2796       else if (br_elem->type == COLL_SYM)
    2797         {
    2798           size_t sym_name_len = strlen ((char *) br_elem->opr.name);
    2799           if (nrules != 0)
    2800             {
    2801               int32_t elem, idx;
    2802               elem = seek_collating_symbol_entry (br_elem->opr.name,
    2803                                                   sym_name_len);
    2804               if (symb_table[2 * elem] != 0)
    2805                 {
    2806                   /* We found the entry.  */
    2807                   idx = symb_table[2 * elem + 1];
    2808                   /* Skip the name of collating element name.  */
    2809                   idx += 1 + extra[idx];
    2810                   /* Skip the byte sequence of the collating element.  */
    2811                   idx += 1 + extra[idx];
    2812                   /* Adjust for the alignment.  */
    2813                   idx = (idx + 3) & ~3;
    2814                   /* Skip the multibyte collation sequence value.  */
    2815                   idx += sizeof (unsigned int);
    2816                   /* Skip the wide char sequence of the collating element.  */
    2817                   idx += sizeof (unsigned int) *
    2818                     (1 + *(unsigned int *) (extra + idx));
    2819                   /* Return the collation sequence value.  */
    2820                   return *(unsigned int *) (extra + idx);
    2821                 }
    2822               else if (symb_table[2 * elem] == 0 && sym_name_len == 1)
    2823                 {
    2824                   /* No valid character.  Match it as a single byte
    2825                      character.  */
    2826                   return collseqmb[br_elem->opr.name[0]];
    2827                 }
    2828             }
    2829           else if (sym_name_len == 1)
    2830             return collseqmb[br_elem->opr.name[0]];
    2831         }
    2832       return UINT_MAX;
    2833     }
    2834 
    2835   /* Local function for parse_bracket_exp used in _LIBC environement.
    2836      Build the range expression which starts from START_ELEM, and ends
    2837      at END_ELEM.  The result are written to MBCSET and SBCSET.
    2838      RANGE_ALLOC is the allocated size of mbcset->range_starts, and
    2839      mbcset->range_ends, is a pointer argument sinse we may
    2840      update it.  */
    2841 
    2842   auto inline reg_errcode_t
    2843   __attribute ((always_inline))
    2844   build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
    2845          re_charset_t *mbcset;
    2846          int *range_alloc;
    2847          bitset_t sbcset;
    2848          bracket_elem_t *start_elem, *end_elem;
    2849     {
    2850       unsigned int ch;
    2851       uint32_t start_collseq;
    2852       uint32_t end_collseq;
    2853 
    2854       /* Equivalence Classes and Character Classes can't be a range
    2855          start/end.  */
    2856       if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
    2857               || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
    2858               0))
    2859         return REG_ERANGE;
    2860 
    2861       start_collseq = lookup_collation_sequence_value (start_elem);
    2862       end_collseq = lookup_collation_sequence_value (end_elem);
    2863       /* Check start/end collation sequence values.  */
    2864       if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
    2865         return REG_ECOLLATE;
    2866       if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
    2867         return REG_ERANGE;
    2868 
    2869       /* Got valid collation sequence values, add them as a new entry.
    2870          However, if we have no collation elements, and the character set
    2871          is single byte, the single byte character set that we
    2872          build below suffices. */
    2873       if (nrules > 0 || dfa->mb_cur_max > 1)
    2874         {
    2875           /* Check the space of the arrays.  */
    2876           if (BE (*range_alloc == mbcset->nranges, 0))
    2877             {
    2878               /* There is not enough space, need realloc.  */
    2879               uint32_t *new_array_start;
    2880               uint32_t *new_array_end;
    2881               int new_nranges;
    2882 
    2883               /* +1 in case of mbcset->nranges is 0.  */
    2884               new_nranges = 2 * mbcset->nranges + 1;
    2885               new_array_start = re_realloc (mbcset->range_starts, uint32_t,
    2886                                             new_nranges);
    2887               new_array_end = re_realloc (mbcset->range_ends, uint32_t,
    2888                                           new_nranges);
    2889 
    2890               if (BE (new_array_start == NULL || new_array_end == NULL, 0))
    2891                 return REG_ESPACE;
    2892 
    2893               mbcset->range_starts = new_array_start;
    2894               mbcset->range_ends = new_array_end;
    2895               *range_alloc = new_nranges;
    2896             }
    2897 
    2898           mbcset->range_starts[mbcset->nranges] = start_collseq;
    2899           mbcset->range_ends[mbcset->nranges++] = end_collseq;
    2900         }
    2901 
    2902       /* Build the table for single byte characters.  */
    2903       for (ch = 0; ch < SBC_MAX; ch++)
    2904         {
    2905           uint32_t ch_collseq;
    2906           /*
    2907           if (MB_CUR_MAX == 1)
    2908           */
    2909           if (nrules == 0)
    2910             ch_collseq = collseqmb[ch];
    2911           else
    2912             ch_collseq = __collseq_table_lookup (collseqwc, __btowc (ch));
    2913           if (start_collseq <= ch_collseq && ch_collseq <= end_collseq)
    2914             bitset_set (sbcset, ch);
    2915         }
    2916       return REG_NOERROR;
    2917     }
    2918 
    2919   /* Local function for parse_bracket_exp used in _LIBC environement.
    2920      Build the collating element which is represented by NAME.
    2921      The result are written to MBCSET and SBCSET.
    2922      COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
    2923      pointer argument sinse we may update it.  */
    2924 
    2925   auto inline reg_errcode_t
    2926   __attribute ((always_inline))
    2927   build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
    2928          re_charset_t *mbcset;
    2929          int *coll_sym_alloc;
    2930          bitset_t sbcset;
    2931          const unsigned char *name;
    2932     {
    2933       int32_t elem, idx;
    2934       size_t name_len = strlen ((const char *) name);
     2779/* Local function for parse_bracket_exp used in _LIBC environment.
     2780   Seek the collating symbol entry corresponding to NAME.
     2781   Return the index of the symbol in the SYMB_TABLE,
     2782   or -1 if not found.  */
     2783
     2784static __always_inline int32_t
     2785seek_collating_symbol_entry (const unsigned char *name, size_t name_len,
     2786                             const int32_t *symb_table,
     2787                             int_fast32_t table_size,
     2788                             const unsigned char *extra)
     2789{
     2790  int_fast32_t elem;
     2791
     2792  for (elem = 0; elem < table_size; elem++)
     2793    if (symb_table[2 * elem] != 0)
     2794      {
     2795        int32_t idx = symb_table[2 * elem + 1];
     2796        /* Skip the name of collating element name.  */
     2797        idx += 1 + extra[idx];
     2798        if (/* Compare the length of the name.  */
     2799            name_len == extra[idx]
     2800            /* Compare the name.  */
     2801            && memcmp (name, &extra[idx + 1], name_len) == 0)
     2802          /* Yep, this is the entry.  */
     2803          return elem;
     2804      }
     2805  return -1;
     2806}
     2807
     2808/* Local function for parse_bracket_exp used in _LIBC environment.
     2809   Look up the collation sequence value of BR_ELEM.
     2810   Return the value if succeeded, UINT_MAX otherwise.  */
     2811
     2812static __always_inline unsigned int
     2813lookup_collation_sequence_value (bracket_elem_t *br_elem, uint32_t nrules,
     2814                                 const unsigned char *collseqmb,
     2815                                 const char *collseqwc,
     2816                                 int_fast32_t table_size,
     2817                                 const int32_t *symb_table,
     2818                                 const unsigned char *extra)
     2819{
     2820  if (br_elem->type == SB_CHAR)
     2821    {
     2822      /* if (MB_CUR_MAX == 1) */
     2823      if (nrules == 0)
     2824        return collseqmb[br_elem->opr.ch];
     2825      else
     2826        {
     2827          wint_t wc = __btowc (br_elem->opr.ch);
     2828          return __collseq_table_lookup (collseqwc, wc);
     2829        }
     2830    }
     2831  else if (br_elem->type == MB_CHAR)
     2832    {
    29352833      if (nrules != 0)
    2936         {
    2937           elem = seek_collating_symbol_entry (name, name_len);
    2938           if (symb_table[2 * elem] != 0)
     2834        return __collseq_table_lookup (collseqwc, br_elem->opr.wch);
     2835    }
     2836  else if (br_elem->type == COLL_SYM)
     2837    {
     2838      size_t sym_name_len = strlen ((char *) br_elem->opr.name);
     2839      if (nrules != 0)
     2840        {
     2841          int32_t elem, idx;
     2842          elem = seek_collating_symbol_entry (br_elem->opr.name,
     2843                                              sym_name_len,
     2844                                              symb_table, table_size,
     2845                                              extra);
     2846          if (elem != -1)
    29392847            {
    29402848              /* We found the entry.  */
     
    29422850              /* Skip the name of collating element name.  */
    29432851              idx += 1 + extra[idx];
     2852              /* Skip the byte sequence of the collating element.  */
     2853              idx += 1 + extra[idx];
     2854              /* Adjust for the alignment.  */
     2855              idx = (idx + 3) & ~3;
     2856              /* Skip the multibyte collation sequence value.  */
     2857              idx += sizeof (unsigned int);
     2858              /* Skip the wide char sequence of the collating element.  */
     2859              idx += sizeof (unsigned int) *
     2860                (1 + *(unsigned int *) (extra + idx));
     2861              /* Return the collation sequence value.  */
     2862              return *(unsigned int *) (extra + idx);
    29442863            }
    2945           else if (symb_table[2 * elem] == 0 && name_len == 1)
     2864          else if (sym_name_len == 1)
    29462865            {
    2947               /* No valid character, treat it as a normal
     2866              /* No valid character.  Match it as a single byte
    29482867                 character.  */
    2949               bitset_set (sbcset, name[0]);
    2950               return REG_NOERROR;
     2868              return collseqmb[br_elem->opr.name[0]];
    29512869            }
    2952           else
    2953             return REG_ECOLLATE;
    2954 
    2955           /* Got valid collation sequence, add it as a new entry.  */
    2956           /* Check the space of the arrays.  */
    2957           if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
    2958             {
    2959               /* Not enough, realloc it.  */
    2960               /* +1 in case of mbcset->ncoll_syms is 0.  */
    2961               int new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
    2962               /* Use realloc since mbcset->coll_syms is NULL
    2963                  if *alloc == 0.  */
    2964               int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t,
    2965                                                    new_coll_sym_alloc);
    2966               if (BE (new_coll_syms == NULL, 0))
    2967                 return REG_ESPACE;
    2968               mbcset->coll_syms = new_coll_syms;
    2969               *coll_sym_alloc = new_coll_sym_alloc;
    2970             }
    2971           mbcset->coll_syms[mbcset->ncoll_syms++] = idx;
     2870        }
     2871      else if (sym_name_len == 1)
     2872        return collseqmb[br_elem->opr.name[0]];
     2873    }
     2874  return UINT_MAX;
     2875}
     2876
     2877/* Local function for parse_bracket_exp used in _LIBC environment.
     2878   Build the range expression which starts from START_ELEM, and ends
     2879   at END_ELEM.  The result are written to MBCSET and SBCSET.
     2880   RANGE_ALLOC is the allocated size of mbcset->range_starts, and
     2881   mbcset->range_ends, is a pointer argument since we may
     2882   update it.  */
     2883
     2884static __always_inline reg_errcode_t
     2885build_range_exp (bitset_t sbcset, re_charset_t *mbcset, Idx *range_alloc,
     2886                 bracket_elem_t *start_elem, bracket_elem_t *end_elem,
     2887                 re_dfa_t *dfa, reg_syntax_t syntax, uint32_t nrules,
     2888                 const unsigned char *collseqmb, const char *collseqwc,
     2889                 int_fast32_t table_size, const int32_t *symb_table,
     2890                 const unsigned char *extra)
     2891{
     2892  unsigned int ch;
     2893  uint32_t start_collseq;
     2894  uint32_t end_collseq;
     2895
     2896  /* Equivalence Classes and Character Classes can't be a range
     2897     start/end.  */
     2898  if (__glibc_unlikely (start_elem->type == EQUIV_CLASS
     2899                        || start_elem->type == CHAR_CLASS
     2900                        || end_elem->type == EQUIV_CLASS
     2901                        || end_elem->type == CHAR_CLASS))
     2902    return REG_ERANGE;
     2903
     2904  /* FIXME: Implement rational ranges here, too.  */
     2905  start_collseq = lookup_collation_sequence_value (start_elem, nrules, collseqmb, collseqwc,
     2906                                                   table_size, symb_table, extra);
     2907  end_collseq = lookup_collation_sequence_value (end_elem, nrules, collseqmb, collseqwc,
     2908                                                 table_size, symb_table, extra);
     2909  /* Check start/end collation sequence values.  */
     2910  if (__glibc_unlikely (start_collseq == UINT_MAX
     2911                        || end_collseq == UINT_MAX))
     2912    return REG_ECOLLATE;
     2913  if (__glibc_unlikely ((syntax & RE_NO_EMPTY_RANGES)
     2914                        && start_collseq > end_collseq))
     2915    return REG_ERANGE;
     2916
     2917  /* Got valid collation sequence values, add them as a new entry.
     2918     However, if we have no collation elements, and the character set
     2919     is single byte, the single byte character set that we
     2920     build below suffices. */
     2921  if (nrules > 0 || dfa->mb_cur_max > 1)
     2922    {
     2923      /* Check the space of the arrays.  */
     2924      if (__glibc_unlikely (*range_alloc == mbcset->nranges))
     2925        {
     2926          /* There is not enough space, need realloc.  */
     2927          uint32_t *new_array_start;
     2928          uint32_t *new_array_end;
     2929          int new_nranges;
     2930
     2931          /* +1 in case of mbcset->nranges is 0.  */
     2932          new_nranges = 2 * mbcset->nranges + 1;
     2933          new_array_start = re_realloc (mbcset->range_starts, uint32_t,
     2934                                        new_nranges);
     2935          new_array_end = re_realloc (mbcset->range_ends, uint32_t,
     2936                                      new_nranges);
     2937
     2938          if (__glibc_unlikely (new_array_start == NULL
     2939                                || new_array_end == NULL))
     2940            return REG_ESPACE;
     2941
     2942          mbcset->range_starts = new_array_start;
     2943          mbcset->range_ends = new_array_end;
     2944          *range_alloc = new_nranges;
     2945        }
     2946
     2947      mbcset->range_starts[mbcset->nranges] = start_collseq;
     2948      mbcset->range_ends[mbcset->nranges++] = end_collseq;
     2949    }
     2950
     2951  /* Build the table for single byte characters.  */
     2952  for (ch = 0; ch < SBC_MAX; ch++)
     2953    {
     2954      uint32_t ch_collseq;
     2955      /* if (MB_CUR_MAX == 1) */
     2956      if (nrules == 0)
     2957        ch_collseq = collseqmb[ch];
     2958      else
     2959        ch_collseq = __collseq_table_lookup (collseqwc, __btowc (ch));
     2960      if (start_collseq <= ch_collseq && ch_collseq <= end_collseq)
     2961        bitset_set (sbcset, ch);
     2962    }
     2963  return REG_NOERROR;
     2964}
     2965
     2966/* Local function for parse_bracket_exp used in _LIBC environment.
     2967   Build the collating element which is represented by NAME.
     2968   The result are written to MBCSET and SBCSET.
     2969   COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
     2970   pointer argument since we may update it.  */
     2971
     2972static __always_inline reg_errcode_t
     2973build_collating_symbol (bitset_t sbcset, re_charset_t *mbcset,
     2974                        Idx *coll_sym_alloc, const unsigned char *name,
     2975                        uint_fast32_t nrules, int_fast32_t table_size,
     2976                        const int32_t *symb_table, const unsigned char *extra)
     2977{
     2978  int32_t elem, idx;
     2979  size_t name_len = strlen ((const char *) name);
     2980  if (nrules != 0)
     2981    {
     2982      elem = seek_collating_symbol_entry (name, name_len, symb_table,
     2983                                          table_size, extra);
     2984      if (elem != -1)
     2985        {
     2986          /* We found the entry.  */
     2987          idx = symb_table[2 * elem + 1];
     2988          /* Skip the name of collating element name.  */
     2989          idx += 1 + extra[idx];
     2990        }
     2991      else if (name_len == 1)
     2992        {
     2993          /* No valid character, treat it as a normal
     2994             character.  */
     2995          bitset_set (sbcset, name[0]);
    29722996          return REG_NOERROR;
    29732997        }
    29742998      else
    2975         {
    2976           if (BE (name_len != 1, 0))
    2977             return REG_ECOLLATE;
    2978           else
    2979             {
    2980               bitset_set (sbcset, name[0]);
    2981               return REG_NOERROR;
    2982             }
    2983         }
    2984     }
    2985 #endif
     2999        return REG_ECOLLATE;
     3000
     3001      /* Got valid collation sequence, add it as a new entry.  */
     3002      /* Check the space of the arrays.  */
     3003      if (__glibc_unlikely (*coll_sym_alloc == mbcset->ncoll_syms))
     3004        {
     3005          /* Not enough, realloc it.  */
     3006          /* +1 in case of mbcset->ncoll_syms is 0.  */
     3007          int new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
     3008          /* Use realloc since mbcset->coll_syms is NULL
     3009             if *alloc == 0.  */
     3010          int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t,
     3011                                               new_coll_sym_alloc);
     3012          if (__glibc_unlikely (new_coll_syms == NULL))
     3013            return REG_ESPACE;
     3014          mbcset->coll_syms = new_coll_syms;
     3015          *coll_sym_alloc = new_coll_sym_alloc;
     3016        }
     3017      mbcset->coll_syms[mbcset->ncoll_syms++] = idx;
     3018      return REG_NOERROR;
     3019    }
     3020  else
     3021    {
     3022      if (__glibc_unlikely (name_len != 1))
     3023        return REG_ECOLLATE;
     3024      else
     3025        {
     3026          bitset_set (sbcset, name[0]);
     3027          return REG_NOERROR;
     3028        }
     3029    }
     3030}
     3031#endif /* _LIBC */
     3032
     3033/* This function parse bracket expression like "[abc]", "[a-c]",
     3034   "[[.a-a.]]" etc.  */
     3035
     3036static bin_tree_t *
     3037parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
     3038                   reg_syntax_t syntax, reg_errcode_t *err)
     3039{
     3040  const unsigned char *collseqmb = NULL;
     3041  const char *collseqwc = NULL;
     3042  uint_fast32_t nrules = 0;
     3043  int_fast32_t table_size = 0;
     3044  const void *symb_table = NULL;
     3045  const unsigned char *extra = NULL;
    29863046
    29873047  re_token_t br_token;
    29883048  re_bitset_ptr_t sbcset;
    2989 #ifdef RE_ENABLE_I18N
    29903049  re_charset_t *mbcset;
    2991   int coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
    2992   int equiv_class_alloc = 0, char_class_alloc = 0;
    2993 #endif /* not RE_ENABLE_I18N */
    2994   int non_match = 0;
     3050  Idx coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
     3051  Idx equiv_class_alloc = 0, char_class_alloc = 0;
     3052  bool non_match = false;
    29953053  bin_tree_t *work_tree;
    29963054  int token_len;
    2997   int first_round = 1;
     3055  bool first_round = true;
    29983056#ifdef _LIBC
    29993057  collseqmb = (const unsigned char *)
     
    30073065      collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
    30083066      table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB);
    3009       symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE,
    3010                                                   _NL_COLLATE_SYMB_TABLEMB);
     3067      symb_table = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_TABLEMB);
    30113068      extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
    30123069                                                   _NL_COLLATE_SYMB_EXTRAMB);
     
    30143071#endif
    30153072  sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
    3016 #ifdef RE_ENABLE_I18N
    30173073  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
    3018 #endif /* RE_ENABLE_I18N */
    3019 #ifdef RE_ENABLE_I18N
    3020   if (BE (sbcset == NULL || mbcset == NULL, 0))
    3021 #else
    3022   if (BE (sbcset == NULL, 0))
    3023 #endif /* RE_ENABLE_I18N */
    3024     {
     3074  if (__glibc_unlikely (sbcset == NULL || mbcset == NULL))
     3075    {
     3076      re_free (sbcset);
     3077      re_free (mbcset);
    30253078      *err = REG_ESPACE;
    30263079      return NULL;
     
    30283081
    30293082  token_len = peek_token_bracket (token, regexp, syntax);
    3030   if (BE (token->type == END_OF_RE, 0))
     3083  if (__glibc_unlikely (token->type == END_OF_RE))
    30313084    {
    30323085      *err = REG_BADPAT;
     
    30353088  if (token->type == OP_NON_MATCH_LIST)
    30363089    {
    3037 #ifdef RE_ENABLE_I18N
    30383090      mbcset->non_match = 1;
    3039 #endif /* not RE_ENABLE_I18N */
    3040       non_match = 1;
     3091      non_match = true;
    30413092      if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
    3042         bitset_set (sbcset, '\0');
     3093        bitset_set (sbcset, '\n');
    30433094      re_string_skip_bytes (regexp, token_len); /* Skip a token.  */
    30443095      token_len = peek_token_bracket (token, regexp, syntax);
    3045       if (BE (token->type == END_OF_RE, 0))
     3096      if (__glibc_unlikely (token->type == END_OF_RE))
    30463097        {
    30473098          *err = REG_BADPAT;
     
    30603111      unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
    30613112      reg_errcode_t ret;
    3062       int token_len2 = 0, is_range_exp = 0;
     3113      int token_len2 = 0;
     3114      bool is_range_exp = false;
    30633115      re_token_t token2;
    30643116
    30653117      start_elem.opr.name = start_name_buf;
     3118      start_elem.type = COLL_SYM;
    30663119      ret = parse_bracket_element (&start_elem, regexp, token, token_len, dfa,
    30673120                                   syntax, first_round);
    3068       if (BE (ret != REG_NOERROR, 0))
     3121      if (__glibc_unlikely (ret != REG_NOERROR))
    30693122        {
    30703123          *err = ret;
    30713124          goto parse_bracket_exp_free_return;
    30723125        }
    3073       first_round = 0;
     3126      first_round = false;
    30743127
    30753128      /* Get information about the next token.  We need it in any case.  */
     
    30793132      if (start_elem.type != CHAR_CLASS && start_elem.type != EQUIV_CLASS)
    30803133        {
    3081           if (BE (token->type == END_OF_RE, 0))
     3134          if (__glibc_unlikely (token->type == END_OF_RE))
    30823135            {
    30833136              *err = REG_EBRACK;
     
    30883141              re_string_skip_bytes (regexp, token_len); /* Skip '-'.  */
    30893142              token_len2 = peek_token_bracket (&token2, regexp, syntax);
    3090               if (BE (token2.type == END_OF_RE, 0))
     3143              if (__glibc_unlikely (token2.type == END_OF_RE))
    30913144                {
    30923145                  *err = REG_EBRACK;
     
    31003153                }
    31013154              else
    3102                 is_range_exp = 1;
     3155                is_range_exp = true;
    31033156            }
    31043157        }
    31053158
    3106       if (is_range_exp == 1)
     3159      if (is_range_exp == true)
    31073160        {
    31083161          end_elem.opr.name = end_name_buf;
     3162          end_elem.type = COLL_SYM;
    31093163          ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
    3110                                        dfa, syntax, 1);
    3111           if (BE (ret != REG_NOERROR, 0))
     3164                                       dfa, syntax, true);
     3165          if (__glibc_unlikely (ret != REG_NOERROR))
    31123166            {
    31133167              *err = ret;
     
    31173171          token_len = peek_token_bracket (token, regexp, syntax);
    31183172
    3119 #ifdef _LIBC
    31203173          *err = build_range_exp (sbcset, mbcset, &range_alloc,
    3121                                   &start_elem, &end_elem);
    3122 #else
    3123 # ifdef RE_ENABLE_I18N
    3124           *err = build_range_exp (sbcset,
    3125                                   dfa->mb_cur_max > 1 ? mbcset : NULL,
    3126                                   &range_alloc, &start_elem, &end_elem);
    3127 # else
    3128           *err = build_range_exp (sbcset, &start_elem, &end_elem);
    3129 # endif
    3130 #endif /* RE_ENABLE_I18N */
    3131           if (BE (*err != REG_NOERROR, 0))
     3174                                  &start_elem, &end_elem,
     3175                                  dfa, syntax, nrules, collseqmb, collseqwc,
     3176                                  table_size, symb_table, extra);
     3177          if (__glibc_unlikely (*err != REG_NOERROR))
    31323178            goto parse_bracket_exp_free_return;
    31333179        }
     
    31393185              bitset_set (sbcset, start_elem.opr.ch);
    31403186              break;
    3141 #ifdef RE_ENABLE_I18N
    31423187            case MB_CHAR:
    31433188              /* Check whether the array has enough space.  */
    3144               if (BE (mbchar_alloc == mbcset->nmbchars, 0))
     3189              if (__glibc_unlikely (mbchar_alloc == mbcset->nmbchars))
    31453190                {
    31463191                  wchar_t *new_mbchars;
     
    31513196                  new_mbchars = re_realloc (mbcset->mbchars, wchar_t,
    31523197                                            mbchar_alloc);
    3153                   if (BE (new_mbchars == NULL, 0))
     3198                  if (__glibc_unlikely (new_mbchars == NULL))
    31543199                    goto parse_bracket_exp_espace;
    31553200                  mbcset->mbchars = new_mbchars;
     
    31573202              mbcset->mbchars[mbcset->nmbchars++] = start_elem.opr.wch;
    31583203              break;
    3159 #endif /* RE_ENABLE_I18N */
    31603204            case EQUIV_CLASS:
    31613205              *err = build_equiv_class (sbcset,
    3162 #ifdef RE_ENABLE_I18N
    31633206                                        mbcset, &equiv_class_alloc,
    3164 #endif /* RE_ENABLE_I18N */
    31653207                                        start_elem.opr.name);
    3166               if (BE (*err != REG_NOERROR, 0))
     3208              if (__glibc_unlikely (*err != REG_NOERROR))
    31673209                goto parse_bracket_exp_free_return;
    31683210              break;
    31693211            case COLL_SYM:
    31703212              *err = build_collating_symbol (sbcset,
    3171 #ifdef RE_ENABLE_I18N
    31723213                                             mbcset, &coll_sym_alloc,
    3173 #endif /* RE_ENABLE_I18N */
    3174                                              start_elem.opr.name);
    3175               if (BE (*err != REG_NOERROR, 0))
     3214                                             start_elem.opr.name,
     3215                                             nrules, table_size, symb_table, extra);
     3216              if (__glibc_unlikely (*err != REG_NOERROR))
    31763217                goto parse_bracket_exp_free_return;
    31773218              break;
    31783219            case CHAR_CLASS:
    31793220              *err = build_charclass (regexp->trans, sbcset,
    3180 #ifdef RE_ENABLE_I18N
    31813221                                      mbcset, &char_class_alloc,
    3182 #endif /* RE_ENABLE_I18N */
    3183                                       start_elem.opr.name, syntax);
    3184               if (BE (*err != REG_NOERROR, 0))
     3222                                      (const char *) start_elem.opr.name,
     3223                                      syntax);
     3224              if (__glibc_unlikely (*err != REG_NOERROR))
    31853225               goto parse_bracket_exp_free_return;
    31863226              break;
    31873227            default:
    3188               assert (0);
     3228              DEBUG_ASSERT (false);
    31893229              break;
    31903230            }
    31913231        }
    3192       if (BE (token->type == END_OF_RE, 0))
     3232      if (__glibc_unlikely (token->type == END_OF_RE))
    31933233        {
    31943234          *err = REG_EBRACK;
     
    32053245    bitset_not (sbcset);
    32063246
    3207 #ifdef RE_ENABLE_I18N
    32083247  /* Ensure only single byte characters are set.  */
    32093248  if (dfa->mb_cur_max > 1)
     
    32213260      br_token.opr.mbcset = mbcset;
    32223261      mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
    3223       if (BE (mbc_tree == NULL, 0))
     3262      if (__glibc_unlikely (mbc_tree == NULL))
    32243263        goto parse_bracket_exp_espace;
    32253264      for (sbc_idx = 0; sbc_idx < BITSET_WORDS; ++sbc_idx)
     
    32303269      if (sbc_idx < BITSET_WORDS)
    32313270        {
    3232           /* Build a tree for simple bracket.  */
    3233           br_token.type = SIMPLE_BRACKET;
    3234           br_token.opr.sbcset = sbcset;
    3235           work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
    3236           if (BE (work_tree == NULL, 0))
    3237             goto parse_bracket_exp_espace;
    3238 
    3239           /* Then join them by ALT node.  */
    3240           work_tree = create_tree (dfa, work_tree, mbc_tree, OP_ALT);
    3241           if (BE (work_tree == NULL, 0))
    3242             goto parse_bracket_exp_espace;
     3271          /* Build a tree for simple bracket.  */
     3272          br_token.type = SIMPLE_BRACKET;
     3273          br_token.opr.sbcset = sbcset;
     3274          work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
     3275          if (__glibc_unlikely (work_tree == NULL))
     3276            goto parse_bracket_exp_espace;
     3277
     3278          /* Then join them by ALT node.  */
     3279          work_tree = create_tree (dfa, work_tree, mbc_tree, OP_ALT);
     3280          if (__glibc_unlikely (work_tree == NULL))
     3281            goto parse_bracket_exp_espace;
    32433282        }
    32443283      else
     
    32493288    }
    32503289  else
    3251 #endif /* not RE_ENABLE_I18N */
    3252     {
    3253 #ifdef RE_ENABLE_I18N
     3290    {
    32543291      free_charset (mbcset);
    3255 #endif
    32563292      /* Build a tree for simple bracket.  */
    32573293      br_token.type = SIMPLE_BRACKET;
    32583294      br_token.opr.sbcset = sbcset;
    32593295      work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
    3260       if (BE (work_tree == NULL, 0))
    3261         goto parse_bracket_exp_espace;
     3296      if (__glibc_unlikely (work_tree == NULL))
     3297        goto parse_bracket_exp_espace;
    32623298    }
    32633299  return work_tree;
     
    32673303 parse_bracket_exp_free_return:
    32683304  re_free (sbcset);
    3269 #ifdef RE_ENABLE_I18N
    32703305  free_charset (mbcset);
    3271 #endif /* RE_ENABLE_I18N */
    32723306  return NULL;
    32733307}
     
    32783312parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
    32793313                       re_token_t *token, int token_len, re_dfa_t *dfa,
    3280                        reg_syntax_t syntax, int accept_hyphen)
    3281 {
    3282 #ifdef RE_ENABLE_I18N
     3314                       reg_syntax_t syntax, bool accept_hyphen)
     3315{
    32833316  int cur_char_size;
    32843317  cur_char_size = re_string_char_size_at (regexp, re_string_cur_idx (regexp));
     
    32903323      return REG_NOERROR;
    32913324    }
    3292 #endif /* RE_ENABLE_I18N */
    32933325  re_string_skip_bytes (regexp, token_len); /* Skip a token.  */
    32943326  if (token->type == OP_OPEN_COLL_ELEM || token->type == OP_OPEN_CHAR_CLASS
    32953327      || token->type == OP_OPEN_EQUIV_CLASS)
    32963328    return parse_bracket_symbol (elem, regexp, token);
    3297   if (BE (token->type == OP_CHARSET_RANGE, 0) && !accept_hyphen)
     3329  if (__glibc_unlikely (token->type == OP_CHARSET_RANGE) && !accept_hyphen)
    32983330    {
    32993331      /* A '-' must only appear as anything but a range indicator before
     
    33603392     The result are written to MBCSET and SBCSET.
    33613393     EQUIV_CLASS_ALLOC is the allocated size of mbcset->equiv_classes,
    3362      is a pointer argument sinse we may update it.  */
     3394     is a pointer argument since we may update it.  */
    33633395
    33643396static reg_errcode_t
    3365 #ifdef RE_ENABLE_I18N
    33663397build_equiv_class (bitset_t sbcset, re_charset_t *mbcset,
    3367                    int *equiv_class_alloc, const unsigned char *name)
    3368 #else /* not RE_ENABLE_I18N */
    3369 build_equiv_class (bitset_t sbcset, const unsigned char *name)
    3370 #endif /* not RE_ENABLE_I18N */
     3398                   Idx *equiv_class_alloc, const unsigned char *name)
    33713399{
    33723400#ifdef _LIBC
     
    33803408      unsigned int ch;
    33813409      size_t len;
    3382       /* This #include defines a local function!  */
    3383 # include <locale/weight.h>
    33843410      /* Calculate the index for equivalence class.  */
    33853411      cp = name;
     
    33913417      indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
    33923418                                                _NL_COLLATE_INDIRECTMB);
    3393       idx1 = findidx (&cp);
    3394       if (BE (idx1 == 0 || cp < name + strlen ((const char *) name), 0))
     3419      idx1 = findidx (table, indirect, extra, &cp, -1);
     3420      if (__glibc_unlikely (idx1 == 0 || *cp != '\0'))
    33953421        /* This isn't a valid character.  */
    33963422        return REG_ECOLLATE;
    33973423
    3398       /* Build single byte matcing table for this equivalence class.  */
    3399       char_buf[1] = (unsigned char) '\0';
    3400       len = weights[idx1];
     3424      /* Build single byte matching table for this equivalence class.  */
     3425      len = weights[idx1 & 0xffffff];
    34013426      for (ch = 0; ch < SBC_MAX; ++ch)
    34023427        {
    34033428          char_buf[0] = ch;
    34043429          cp = char_buf;
    3405           idx2 = findidx (&cp);
     3430          idx2 = findidx (table, indirect, extra, &cp, 1);
    34063431/*
    34073432          idx2 = table[ch];
     
    34103435            /* This isn't a valid character.  */
    34113436            continue;
    3412           if (len == weights[idx2])
    3413             {
    3414               int cnt = 0;
    3415               while (cnt <= len &&
    3416                      weights[idx1 + 1 + cnt] == weights[idx2 + 1 + cnt])
    3417                 ++cnt;
    3418 
    3419               if (cnt > len)
    3420                 bitset_set (sbcset, ch);
    3421             }
     3437          /* Compare only if the length matches and the collation rule
     3438             index is the same.  */
     3439          if (len == weights[idx2 & 0xffffff] && (idx1 >> 24) == (idx2 >> 24)
     3440              && memcmp (weights + (idx1 & 0xffffff) + 1,
     3441                         weights + (idx2 & 0xffffff) + 1, len) == 0)
     3442            bitset_set (sbcset, ch);
    34223443        }
    34233444      /* Check whether the array has enough space.  */
    3424       if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
     3445      if (__glibc_unlikely (*equiv_class_alloc == mbcset->nequiv_classes))
    34253446        {
    34263447          /* Not enough, realloc it.  */
    34273448          /* +1 in case of mbcset->nequiv_classes is 0.  */
    3428           int new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
     3449          Idx new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
    34293450          /* Use realloc since the array is NULL if *alloc == 0.  */
    34303451          int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes,
    34313452                                                   int32_t,
    34323453                                                   new_equiv_class_alloc);
    3433           if (BE (new_equiv_classes == NULL, 0))
     3454          if (__glibc_unlikely (new_equiv_classes == NULL))
    34343455            return REG_ESPACE;
    34353456          mbcset->equiv_classes = new_equiv_classes;
     
    34413462#endif /* _LIBC */
    34423463    {
    3443       if (BE (strlen ((const char *) name) != 1, 0))
     3464      if (__glibc_unlikely (strlen ((const char *) name) != 1))
    34443465        return REG_ECOLLATE;
    34453466      bitset_set (sbcset, *name);
     
    34523473     The result are written to MBCSET and SBCSET.
    34533474     CHAR_CLASS_ALLOC is the allocated size of mbcset->char_classes,
    3454      is a pointer argument sinse we may update it.  */
     3475     is a pointer argument since we may update it.  */
    34553476
    34563477static reg_errcode_t
    3457 #ifdef RE_ENABLE_I18N
    34583478build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
    3459                  re_charset_t *mbcset, int *char_class_alloc,
    3460                  const unsigned char *class_name, reg_syntax_t syntax)
    3461 #else /* not RE_ENABLE_I18N */
    3462 build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
    3463                  const unsigned char *class_name, reg_syntax_t syntax)
    3464 #endif /* not RE_ENABLE_I18N */
     3479                 re_charset_t *mbcset, Idx *char_class_alloc,
     3480                 const char *class_name, reg_syntax_t syntax)
    34653481{
    34663482  int i;
    3467   const char *name = (const char *) class_name;
     3483  const char *name = class_name;
    34683484
    34693485  /* In case of REG_ICASE "upper" and "lower" match the both of
     
    34733489    name = "alpha";
    34743490
    3475 #ifdef RE_ENABLE_I18N
    34763491  /* Check the space of the arrays.  */
    3477   if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
     3492  if (__glibc_unlikely (*char_class_alloc == mbcset->nchar_classes))
    34783493    {
    34793494      /* Not enough, realloc it.  */
    34803495      /* +1 in case of mbcset->nchar_classes is 0.  */
    3481       int new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
     3496      Idx new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
    34823497      /* Use realloc since array is NULL if *alloc == 0.  */
    34833498      wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t,
    34843499                                               new_char_class_alloc);
    3485       if (BE (new_char_classes == NULL, 0))
     3500      if (__glibc_unlikely (new_char_classes == NULL))
    34863501        return REG_ESPACE;
    34873502      mbcset->char_classes = new_char_classes;
     
    34893504    }
    34903505  mbcset->char_classes[mbcset->nchar_classes++] = __wctype (name);
    3491 #endif /* RE_ENABLE_I18N */
    34923506
    34933507#define BUILD_CHARCLASS_LOOP(ctype_func)        \
    34943508  do {                                          \
    3495     if (BE (trans != NULL, 0))                  \
     3509    if (__glibc_unlikely (trans != NULL))                       \
    34963510      {                                         \
    34973511        for (i = 0; i < SBC_MAX; ++i)           \
    3498           if (ctype_func (i))                   \
     3512          if (ctype_func (i))                   \
    34993513            bitset_set (sbcset, trans[i]);      \
    35003514      }                                         \
     
    35023516      {                                         \
    35033517        for (i = 0; i < SBC_MAX; ++i)           \
    3504           if (ctype_func (i))                   \
     3518          if (ctype_func (i))                   \
    35053519            bitset_set (sbcset, i);             \
    35063520      }                                         \
     
    35393553static bin_tree_t *
    35403554build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,
    3541                     const unsigned char *class_name,
    3542                     const unsigned char *extra, int non_match,
     3555                    const char *class_name,
     3556                    const char *extra, bool non_match,
    35433557                    reg_errcode_t *err)
    35443558{
    35453559  re_bitset_ptr_t sbcset;
    3546 #ifdef RE_ENABLE_I18N
    35473560  re_charset_t *mbcset;
    3548   int alloc = 0;
    3549 #endif /* not RE_ENABLE_I18N */
     3561  Idx alloc = 0;
    35503562  reg_errcode_t ret;
    3551   re_token_t br_token;
    35523563  bin_tree_t *tree;
    35533564
    35543565  sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
    3555 #ifdef RE_ENABLE_I18N
    3556   mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
    3557 #endif /* RE_ENABLE_I18N */
    3558 
    3559 #ifdef RE_ENABLE_I18N
    3560   if (BE (sbcset == NULL || mbcset == NULL, 0))
    3561 #else /* not RE_ENABLE_I18N */
    3562   if (BE (sbcset == NULL, 0))
    3563 #endif /* not RE_ENABLE_I18N */
     3566  if (__glibc_unlikely (sbcset == NULL))
    35643567    {
    35653568      *err = REG_ESPACE;
    35663569      return NULL;
    35673570    }
    3568 
    3569   if (non_match)
    3570     {
    3571 #ifdef RE_ENABLE_I18N
    3572       /*
    3573       if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
    3574         bitset_set(cset->sbcset, '\0');
    3575       */
    3576       mbcset->non_match = 1;
    3577 #endif /* not RE_ENABLE_I18N */
    3578     }
     3571  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
     3572  if (__glibc_unlikely (mbcset == NULL))
     3573    {
     3574      re_free (sbcset);
     3575      *err = REG_ESPACE;
     3576      return NULL;
     3577    }
     3578  mbcset->non_match = non_match;
    35793579
    35803580  /* We don't care the syntax in this case.  */
    3581   ret = build_charclass (trans, sbcset,
    3582 #ifdef RE_ENABLE_I18N
    3583                          mbcset, &alloc,
    3584 #endif /* RE_ENABLE_I18N */
    3585                          class_name, 0);
    3586 
    3587   if (BE (ret != REG_NOERROR, 0))
     3581  ret = build_charclass (trans, sbcset, mbcset, &alloc, class_name, 0);
     3582
     3583  if (__glibc_unlikely (ret != REG_NOERROR))
    35883584    {
    35893585      re_free (sbcset);
    3590 #ifdef RE_ENABLE_I18N
    35913586      free_charset (mbcset);
    3592 #endif /* RE_ENABLE_I18N */
    35933587      *err = ret;
    35943588      return NULL;
     
    36023596    bitset_not (sbcset);
    36033597
    3604 #ifdef RE_ENABLE_I18N
    36053598  /* Ensure only single byte characters are set.  */
    36063599  if (dfa->mb_cur_max > 1)
    36073600    bitset_mask (sbcset, dfa->sb_char);
    3608 #endif
    36093601
    36103602  /* Build a tree for simple bracket.  */
    3611   br_token.type = SIMPLE_BRACKET;
    3612   br_token.opr.sbcset = sbcset;
     3603  re_token_t br_token = { .type = SIMPLE_BRACKET, .opr.sbcset = sbcset };
    36133604  tree = create_token_tree (dfa, NULL, NULL, &br_token);
    3614   if (BE (tree == NULL, 0))
     3605  if (__glibc_unlikely (tree == NULL))
    36153606    goto build_word_op_espace;
    36163607
    3617 #ifdef RE_ENABLE_I18N
    36183608  if (dfa->mb_cur_max > 1)
    36193609    {
     
    36243614      dfa->has_mb_node = 1;
    36253615      mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
    3626       if (BE (mbc_tree == NULL, 0))
     3616      if (__glibc_unlikely (mbc_tree == NULL))
    36273617        goto build_word_op_espace;
    36283618      /* Then join them by ALT node.  */
    36293619      tree = create_tree (dfa, tree, mbc_tree, OP_ALT);
    3630       if (BE (mbc_tree != NULL, 1))
     3620      if (__glibc_likely (mbc_tree != NULL))
    36313621        return tree;
    36323622    }
     
    36363626      return tree;
    36373627    }
    3638 #else /* not RE_ENABLE_I18N */
    3639   return tree;
    3640 #endif /* not RE_ENABLE_I18N */
    36413628
    36423629 build_word_op_espace:
    36433630  re_free (sbcset);
    3644 #ifdef RE_ENABLE_I18N
    36453631  free_charset (mbcset);
    3646 #endif /* RE_ENABLE_I18N */
    36473632  *err = REG_ESPACE;
    36483633  return NULL;
     
    36503635
    36513636/* This is intended for the expressions like "a{1,3}".
    3652    Fetch a number from `input', and return the number.
    3653    Return -1, if the number field is empty like "{,1}".
    3654    Return -2, If an error is occured.  */
    3655 
    3656 static int
     3637   Fetch a number from 'input', and return the number.
     3638   Return -1 if the number field is empty like "{,1}".
     3639   Return RE_DUP_MAX + 1 if the number field is too large.
     3640   Return -2 if an error occurred.  */
     3641
     3642static Idx
    36573643fetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax)
    36583644{
    3659   int num = -1;
     3645  Idx num = -1;
    36603646  unsigned char c;
    36613647  while (1)
     
    36633649      fetch_token (token, input, syntax);
    36643650      c = token->opr.c;
    3665       if (BE (token->type == END_OF_RE, 0))
     3651      if (__glibc_unlikely (token->type == END_OF_RE))
    36663652        return -2;
    36673653      if (token->type == OP_CLOSE_DUP_NUM || c == ',')
    36683654        break;
    36693655      num = ((token->type != CHARACTER || c < '0' || '9' < c || num == -2)
    3670              ? -2 : ((num == -1) ? c - '0' : num * 10 + c - '0'));
    3671       num = (num > RE_DUP_MAX) ? -2 : num;
     3656             ? -2
     3657             : num == -1
     3658             ? c - '0'
     3659             : MIN (RE_DUP_MAX + 1, num * 10 + c - '0'));
    36723660    }
    36733661  return num;
     
    36753663
    36763664
    3677 #ifdef RE_ENABLE_I18N
    36783665static void
    36793666free_charset (re_charset_t *cset)
    36803667{
    36813668  re_free (cset->mbchars);
    3682 # ifdef _LIBC
     3669#ifdef _LIBC
    36833670  re_free (cset->coll_syms);
    36843671  re_free (cset->equiv_classes);
     3672#endif
    36853673  re_free (cset->range_starts);
    36863674  re_free (cset->range_ends);
    3687 # endif
    36883675  re_free (cset->char_classes);
    36893676  re_free (cset);
    36903677}
    3691 #endif /* RE_ENABLE_I18N */
    36923678
    36933679
     
    37003686             re_token_type_t type)
    37013687{
    3702   re_token_t t;
    3703   t.type = type;
     3688  re_token_t t = { .type = type };
    37043689  return create_token_tree (dfa, left, right, &t);
    37053690}
     
    37103695{
    37113696  bin_tree_t *tree;
    3712   if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0))
     3697  if (__glibc_unlikely (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE))
    37133698    {
    37143699      bin_tree_storage_t *storage = re_malloc (bin_tree_storage_t, 1);
     
    37453730mark_opt_subexp (void *extra, bin_tree_t *node)
    37463731{
    3747   int idx = (int) (long) extra;
     3732  Idx idx = (uintptr_t) extra;
    37483733  if (node->token.type == SUBEXP && node->token.opr.idx == idx)
    37493734    node->token.opt_subexp = 1;
     
    37573742free_token (re_token_t *node)
    37583743{
    3759 #ifdef RE_ENABLE_I18N
    37603744  if (node->type == COMPLEX_BRACKET && node->duplicated == 0)
    37613745    free_charset (node->opr.mbcset);
    3762   else
    3763 #endif /* RE_ENABLE_I18N */
    3764     if (node->type == SIMPLE_BRACKET && node->duplicated == 0)
    3765       re_free (node->opr.sbcset);
     3746  else if (node->type == SIMPLE_BRACKET && node->duplicated == 0)
     3747    re_free (node->opr.sbcset);
    37663748}
    37673749
     
    38143796              dup_node = dup_node->parent;
    38153797              if (!node)
    3816                 return dup_root;
     3798                return dup_root;
    38173799            }
    38183800          node = node->right;
Note: See TracChangeset for help on using the changeset viewer.

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