Changeset 3058


Ignore:
Timestamp:
Apr 8, 2007, 8:51:40 PM (18 years ago)
Author:
bird
Message:

Merged in regex from glibc 2.5.

Location:
branches/libc-0.6/src/emx
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/libc-0.6/src/emx/include/regex.h

    r2240 r3058  
    11/* Definitions for data structures and routines for the regular
    22   expression library.
    3    Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003
     3   Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003,2005,2006
    44   Free Software Foundation, Inc.
    55   This file is part of the GNU C Library.
     
    2828#ifdef __cplusplus
    2929extern "C" {
    30 #endif
    31 
    32 /* POSIX says that <sys/types.h> must be included (by the caller) before
    33    <regex.h>.  */
    34 
    35 #if !defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE && defined VMS
    36 /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
    37    should be there.  */
    38 # include <stddef.h>
    3930#endif
    4031
     
    209200       | RE_CONTEXT_INVALID_OPS ))
    210201
    211 #define RE_SYNTAX_POSIX_AWK                                             \
     202#define RE_SYNTAX_POSIX_AWK                                             \
    212203  (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS              \
    213204   | RE_INTERVALS           | RE_NO_GNU_OPS)
     
    350341
    351342#ifndef RE_TRANSLATE_TYPE
    352 # define RE_TRANSLATE_TYPE char *
     343# define RE_TRANSLATE_TYPE unsigned char *
    353344#endif
    354345
    355346struct re_pattern_buffer
    356347{
    357 /* [[[begin pattern_buffer]]] */
    358         /* Space that holds the compiled pattern.  It is declared as
    359           `unsigned char *' because its elements are
    360            sometimes used as array indexes.  */
     348  /* Space that holds the compiled pattern.  It is declared as
     349     `unsigned char *' because its elements are sometimes used as
     350     array indexes.  */
    361351  unsigned char *buffer;
    362352
    363         /* Number of bytes to which `buffer' points.  */
     353  /* Number of bytes to which `buffer' points.  */
    364354  unsigned long int allocated;
    365355
    366         /* Number of bytes actually used in `buffer'.  */
     356  /* Number of bytes actually used in `buffer'.  */
    367357  unsigned long int used;
    368358
    369         /* Syntax setting with which the pattern was compiled.  */
     359  /* Syntax setting with which the pattern was compiled.  */
    370360  reg_syntax_t syntax;
    371361
    372         /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
    373            the fastmap, if there is one, to skip over impossible
    374            starting points for matches.  */
     362  /* Pointer to a fastmap, if any, otherwise zero.  re_search uses the
     363     fastmap, if there is one, to skip over impossible starting points
     364     for matches.  */
    375365  char *fastmap;
    376366
    377         /* Either a translate table to apply to all characters before
    378            comparing them, or zero for no translation.  The translation
    379            is applied to a pattern when it is compiled and to a string
    380            when it is matched.  */
     367  /* Either a translate table to apply to all characters before
     368     comparing them, or zero for no translation.  The translation is
     369     applied to a pattern when it is compiled and to a string when it
     370     is matched.  */
    381371  RE_TRANSLATE_TYPE translate;
    382372
    383         /* Number of subexpressions found by the compiler.  */
     373  /* Number of subexpressions found by the compiler.  */
    384374  size_t re_nsub;
    385375
    386         /* Zero if this pattern cannot match the empty string, one else.
    387            Well, in truth it's used only in `re_search_2', to see
    388            whether or not we should use the fastmap, so we don't set
    389            this absolutely perfectly; see `re_compile_fastmap' (the
    390            `duplicate' case).  */
     376  /* Zero if this pattern cannot match the empty string, one else.
     377     Well, in truth it's used only in `re_search_2', to see whether or
     378     not we should use the fastmap, so we don't set this absolutely
     379     perfectly; see `re_compile_fastmap' (the `duplicate' case).  */
    391380  unsigned can_be_null : 1;
    392381
    393         /* If REGS_UNALLOCATED, allocate space in the `regs' structure
    394              for `max (RE_NREGS, re_nsub + 1)' groups.
    395            If REGS_REALLOCATE, reallocate space if necessary.
    396            If REGS_FIXED, use what's there.  */
     382  /* If REGS_UNALLOCATED, allocate space in the `regs' structure
     383     for `max (RE_NREGS, re_nsub + 1)' groups.
     384     If REGS_REALLOCATE, reallocate space if necessary.
     385     If REGS_FIXED, use what's there.  */
    397386#define REGS_UNALLOCATED 0
    398387#define REGS_REALLOCATE 1
     
    400389  unsigned regs_allocated : 2;
    401390
    402         /* Set to zero when `regex_compile' compiles a pattern; set to one
    403            by `re_compile_fastmap' if it updates the fastmap.  */
     391  /* Set to zero when `regex_compile' compiles a pattern; set to one
     392     by `re_compile_fastmap' if it updates the fastmap.  */
    404393  unsigned fastmap_accurate : 1;
    405394
    406         /* If set, `re_match_2' does not return information about
    407            subexpressions.  */
     395  /* If set, `re_match_2' does not return information about
     396     subexpressions.  */
    408397  unsigned no_sub : 1;
    409398
    410         /* If set, a beginning-of-line anchor doesn't match at the
    411            beginning of the string.  */
     399  /* If set, a beginning-of-line anchor doesn't match at the beginning
     400     of the string.  */
    412401  unsigned not_bol : 1;
    413402
    414         /* Similarly for an end-of-line anchor.  */
     403  /* Similarly for an end-of-line anchor.  */
    415404  unsigned not_eol : 1;
    416405
    417         /* If true, an anchor at a newline matches.  */
     406  /* If true, an anchor at a newline matches.  */
    418407  unsigned newline_anchor : 1;
    419 
    420 /* [[[end pattern_buffer]]] */
    421408};
    422409
     
    458445/* Declarations for routines.  */
    459446
    460 /* To avoid duplicating every routine declaration -- once with a
    461    prototype (if we are ANSI), and once without (if we aren't) -- we
    462    use the following macro to declare argument types.  This
    463    unfortunately clutters up the declarations a bit, but I think it's
    464    worth it.  */
    465 
    466 #if __STDC__
    467 
    468 # define _RE_ARGS(args) args
    469 
    470 #else /* not __STDC__ */
    471 
    472 # define _RE_ARGS(args) ()
    473 #error "asdf"
    474 
    475 #endif /* not __STDC__ */
    476 
    477447/* Sets the current default syntax to SYNTAX, and return the old syntax.
    478448   You can also simply assign to the `re_syntax_options' variable.  */
    479 extern reg_syntax_t re_set_syntax _RE_ARGS ((reg_syntax_t syntax));
     449extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
    480450
    481451/* Compile the regular expression PATTERN, with length LENGTH
    482452   and syntax given by the global `re_syntax_options', into the buffer
    483453   BUFFER.  Return NULL if successful, and an error string if not.  */
    484 extern const char *re_compile_pattern
    485   _RE_ARGS ((const char *pattern, size_t length,
    486              struct re_pattern_buffer *buffer));
     454extern const char *re_compile_pattern (const char *__pattern, size_t __length,
     455                                       struct re_pattern_buffer *__buffer);
    487456
    488457
     
    490459   accelerate searches.  Return 0 if successful and -2 if was an
    491460   internal error.  */
    492 extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer));
     461extern int re_compile_fastmap (struct re_pattern_buffer *__buffer);
    493462
    494463
     
    498467   match, or -2 for an internal error.  Also return register
    499468   information in REGS (if REGS and BUFFER->no_sub are nonzero).  */
    500 extern int re_search
    501   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
    502             int length, int start, int range, struct re_registers *regs));
     469extern int re_search (struct re_pattern_buffer *__buffer, const char *__string,
     470                      int __length, int __start, int __range,
     471                      struct re_registers *__regs);
    503472
    504473
    505474/* Like `re_search', but search in the concatenation of STRING1 and
    506475   STRING2.  Also, stop searching at index START + STOP.  */
    507 extern int re_search_2
    508   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
    509              int length1, const char *string2, int length2,
    510              int start, int range, struct re_registers *regs, int stop));
     476extern int re_search_2 (struct re_pattern_buffer *__buffer,
     477                        const char *__string1, int __length1,
     478                        const char *__string2, int __length2, int __start,
     479                        int __range, struct re_registers *__regs, int __stop);
    511480
    512481
    513482/* Like `re_search', but return how many characters in STRING the regexp
    514483   in BUFFER matched, starting at position START.  */
    515 extern int re_match
    516   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
    517              int length, int start, struct re_registers *regs));
     484extern int re_match (struct re_pattern_buffer *__buffer, const char *__string,
     485                     int __length, int __start, struct re_registers *__regs);
    518486
    519487
    520488/* Relates to `re_match' as `re_search_2' relates to `re_search'.  */
    521 extern int re_match_2
    522   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
    523              int length1, const char *string2, int length2,
    524              int start, struct re_registers *regs, int stop));
     489extern int re_match_2 (struct re_pattern_buffer *__buffer,
     490                       const char *__string1, int __length1,
     491                       const char *__string2, int __length2, int __start,
     492                       struct re_registers *__regs, int __stop);
    525493
    526494
     
    537505   PATTERN_BUFFER will allocate its own register data, without
    538506   freeing the old data.  */
    539 extern void re_set_registers
    540   _RE_ARGS ((struct re_pattern_buffer *buffer, struct re_registers *regs,
    541              unsigned num_regs, regoff_t *starts, regoff_t *ends));
     507extern void re_set_registers (struct re_pattern_buffer *__buffer,
     508                              struct re_registers *__regs,
     509                              unsigned int __num_regs,
     510                              regoff_t *__starts, regoff_t *__ends);
    542511
    543512#if defined _REGEX_RE_COMP || defined _LIBC
    544513# ifndef _CRAY
    545514/* 4.2 bsd compatibility.  */
    546 extern char *re_comp _RE_ARGS ((const char *));
    547 extern int re_exec _RE_ARGS ((const char *));
     515extern char *re_comp (const char *);
     516extern int re_exec (const char *);
    548517# endif
    549518#endif
     
    562531/* gcc 3.1 and up support the [restrict] syntax.  */
    563532#ifndef __restrict_arr
    564 # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
     533# if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) \
     534     && !defined __GNUG__
    565535#  define __restrict_arr __restrict
    566536# else
     
    570540
    571541/* POSIX compatibility.  */
    572 extern int regcomp _RE_ARGS ((regex_t *__restrict __preg,
    573                               const char *__restrict __pattern,
    574                               int __cflags));
    575 
    576 extern int regexec _RE_ARGS ((const regex_t *__restrict __preg,
    577                               const char *__restrict __string, size_t __nmatch,
    578                               regmatch_t __pmatch[__restrict_arr],
    579                               int __eflags));
    580 
    581 extern size_t regerror _RE_ARGS ((int __errcode, const regex_t *__preg,
    582                                   char *__errbuf, size_t __errbuf_size));
    583 
    584 extern void regfree _RE_ARGS ((regex_t *__preg));
     542extern int regcomp (regex_t *__restrict __preg,
     543                    const char *__restrict __pattern,
     544                    int __cflags);
     545
     546extern int regexec (const regex_t *__restrict __preg,
     547                    const char *__restrict __string, size_t __nmatch,
     548                    regmatch_t __pmatch[__restrict_arr],
     549                    int __eflags);
     550
     551extern size_t regerror (int __errcode, const regex_t *__restrict __preg,
     552                        char *__restrict __errbuf, size_t __errbuf_size);
     553
     554extern void regfree (regex_t *__preg);
    585555
    586556
     
    590560
    591561#endif /* regex.h */
    592 
    593 
    594 /*
    595 Local variables:
    596 make-backup-files: t
    597 version-control: t
    598 trim-versions-without-asking: nil
    599 End:
    600  */
  • branches/libc-0.6/src/emx/src/lib/lgpl/include/config.h

    r2259 r3058  
    5252#define HAVE___ARGZ_STRINGIFY 1
    5353#define HAVE___ARGZ_NEXT 1
     54#define HAVE_STDINT_H 1
    5455#define HAVE_STDINT_H_WITH_UINTMAX 1
    5556#define HAVE_STRCASECMP 1
     
    6162#define HAVE_STDLIB_H 1
    6263#define HAVE_STRING_H 1
     64#define HAVE_LANGINFO_H 1
    6365#define HAVE_LANGINFO_CODESET 1
    6466#define HAVE_SETLOCALE 1
     
    7375#define HAVE_WCSCOLL 0
    7476#define HAVE_DECL_GETC_UNLOCKED 1
     77#define HAVE_STDBOOL_H 1
    7578
    7679#define STRERROR_R_CHAR_P 0
  • branches/libc-0.6/src/emx/src/lib/lgpl/posix/regcomp.c

    r2260 r3058  
    11/* Extended regular expression matching and search library.
    2    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
     2   Copyright (C) 2002,2003,2004,2005,2006 Free Software Foundation, Inc.
    33   This file is part of the GNU C Library.
    44   Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
     
    2020
    2121static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
    22                                           int length, reg_syntax_t syntax);
     22                                          size_t length, reg_syntax_t syntax);
    2323static void re_compile_fastmap_iter (regex_t *bufp,
    2424                                     const re_dfastate_t *init_state,
    2525                                     char *fastmap);
    26 static reg_errcode_t init_dfa (re_dfa_t *dfa, int pat_len);
    27 static void init_word_char (re_dfa_t *dfa);
     26static reg_errcode_t init_dfa (re_dfa_t *dfa, size_t pat_len);
    2827#ifdef RE_ENABLE_I18N
    2928static void free_charset (re_charset_t *cset);
     
    3534#endif
    3635static reg_errcode_t analyze (regex_t *preg);
    37 static reg_errcode_t create_initial_state (re_dfa_t *dfa);
    3836static reg_errcode_t preorder (bin_tree_t *root,
    3937                               reg_errcode_t (fn (void *, bin_tree_t *)),
     
    4947static reg_errcode_t calc_next (void *extra, bin_tree_t *node);
    5048static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
    51 static reg_errcode_t duplicate_node_closure (re_dfa_t *dfa, int top_org_node,
    52                                              int top_clone_node, int root_node,
    53                                              unsigned int constraint);
    54 static reg_errcode_t duplicate_node (int *new_idx, re_dfa_t *dfa, int org_idx,
    55                                      unsigned int constraint);
    56 static int search_duplicated_node (re_dfa_t *dfa, int org_node,
     49static int duplicate_node (re_dfa_t *dfa, int org_idx, unsigned int constraint);
     50static int search_duplicated_node (const re_dfa_t *dfa, int org_node,
    5751                                   unsigned int constraint);
    5852static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
     
    6256static int fetch_number (re_string_t *input, re_token_t *token,
    6357                         reg_syntax_t syntax);
    64 static void fetch_token (re_token_t *result, re_string_t *input,
    65                          reg_syntax_t syntax);
    6658static int peek_token (re_token_t *token, re_string_t *input,
    67                         reg_syntax_t syntax);
    68 static int peek_token_bracket (re_token_t *token, re_string_t *input,
    69                                reg_syntax_t syntax);
     59                        reg_syntax_t syntax) internal_function;
    7060static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
    7161                          reg_syntax_t syntax, reg_errcode_t *err);
     
    9787                                          re_string_t *regexp,
    9888                                          re_token_t *token);
    99 #ifndef _LIBC
    100 # ifdef RE_ENABLE_I18N
    101 static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
    102                                       re_charset_t *mbcset, int *range_alloc,
    103                                       bracket_elem_t *start_elem,
    104                                       bracket_elem_t *end_elem);
    105 static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
    106                                              re_charset_t *mbcset,
    107                                              int *coll_sym_alloc,
    108                                              const unsigned char *name);
    109 # else /* not RE_ENABLE_I18N */
    110 static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
    111                                       bracket_elem_t *start_elem,
    112                                       bracket_elem_t *end_elem);
    113 static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
    114                                              const unsigned char *name);
    115 # endif /* not RE_ENABLE_I18N */
    116 #endif /* not _LIBC */
    117 #ifdef RE_ENABLE_I18N
    118 static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
     89#ifdef RE_ENABLE_I18N
     90static reg_errcode_t build_equiv_class (bitset_t sbcset,
    11991                                        re_charset_t *mbcset,
    12092                                        int *equiv_class_alloc,
    12193                                        const unsigned char *name);
    122 static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans,
    123                                       re_bitset_ptr_t sbcset,
     94static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
     95                                      bitset_t sbcset,
    12496                                      re_charset_t *mbcset,
    12597                                      int *char_class_alloc,
     
    12799                                      reg_syntax_t syntax);
    128100#else  /* not RE_ENABLE_I18N */
    129 static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
     101static reg_errcode_t build_equiv_class (bitset_t sbcset,
    130102                                        const unsigned char *name);
    131 static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans,
    132                                       re_bitset_ptr_t sbcset,
     103static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
     104                                      bitset_t sbcset,
    133105                                      const unsigned char *class_name,
    134106                                      reg_syntax_t syntax);
    135107#endif /* not RE_ENABLE_I18N */
    136108static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
    137                                        unsigned RE_TRANSLATE_TYPE trans,
     109                                       RE_TRANSLATE_TYPE trans,
    138110                                       const unsigned char *class_name,
    139111                                       const unsigned char *extra,
     
    330302
    331303static void
    332 re_compile_fastmap_iter (bufp, init_state, fastmap)
    333      regex_t *bufp;
    334      const re_dfastate_t *init_state;
    335      char *fastmap;
     304re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state,
     305                         char *fastmap)
    336306{
    337307  re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
     
    359329                     && dfa->nodes[node].mb_partial)
    360330                *p++ = dfa->nodes[node].opr.c;
    361               memset (&state, 0, sizeof (state));
     331              memset (&state, '\0', sizeof (state));
    362332              if (mbrtowc (&wc, (const char *) buf, p - buf,
    363333                           &state) == p - buf
     
    370340      else if (type == SIMPLE_BRACKET)
    371341        {
    372           int i, j, ch;
    373           for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
    374             for (j = 0; j < UINT_BITS; ++j, ++ch)
    375               if (dfa->nodes[node].opr.sbcset[i] & (1 << j))
    376                 re_set_fastmap (fastmap, icase, ch);
     342          int i, ch;
     343          for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
     344            {
     345              int j;
     346              bitset_word_t w = dfa->nodes[node].opr.sbcset[i];
     347              for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
     348                if (w & ((bitset_word_t) 1 << j))
     349                  re_set_fastmap (fastmap, icase, ch);
     350            }
    377351        }
    378352#ifdef RE_ENABLE_I18N
     
    393367                          'b' since 'b' is the only collation element
    394368                          which starts from 'b'.  */
    395                   int j, ch;
    396369                  const int32_t *table = (const int32_t *)
    397370                    _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
    398                   for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
    399                     for (j = 0; j < UINT_BITS; ++j, ++ch)
    400                       if (table[ch] < 0)
    401                         re_set_fastmap (fastmap, icase, ch);
     371                  for (i = 0; i < SBC_MAX; ++i)
     372                    if (table[i] < 0)
     373                      re_set_fastmap (fastmap, icase, i);
    402374                }
    403375# else
     
    419391                  if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
    420392                      != (size_t) -1)
    421                   re_set_fastmap (fastmap, 0, *(unsigned char *) buf);
     393                    re_set_fastmap (fastmap, 0, *(unsigned char *) buf);
    422394                }
    423395            }
     
    540512_STD(regerror) (errcode, preg, errbuf, errbuf_size)
    541513    int errcode;
    542     const regex_t *preg;
    543     char *errbuf;
     514    const regex_t *__restrict preg;
     515    char *__restrict errbuf;
    544516    size_t errbuf_size;
    545517{
     
    587559   it the same all the time.  UTF-8 is the preferred encoding so this is
    588560   a worthwhile optimization.  */
    589 static const bitset utf8_sb_map =
     561static const bitset_t utf8_sb_map =
    590562{
    591563  /* Set the first 128 bits.  */
    592 # if UINT_MAX == 0xffffffff
    593   0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
    594 # else
    595 #  error "Add case for new unsigned int size"
    596 # endif
     564  [0 ... 0x80 / BITSET_WORD_BITS - 1] = BITSET_WORD_MAX
    597565};
    598566#endif
     
    745713
    746714static reg_errcode_t
    747 re_compile_internal (preg, pattern, length, syntax)
    748      regex_t *preg;
    749      const char * pattern;
    750      int length;
    751      reg_syntax_t syntax;
     715re_compile_internal (regex_t *preg, const char * pattern, size_t length,
     716                     reg_syntax_t syntax)
    752717{
    753718  reg_errcode_t err = REG_NOERROR;
     
    780745  preg->used = sizeof (re_dfa_t);
    781746
    782   /* __libc_lock_init (dfa->lock); - why init before memsetting the whole thing? */
    783 
    784747  err = init_dfa (dfa, length);
    785748  if (BE (err != REG_NOERROR, 0))
     
    790753      return err;
    791754    }
    792   __libc_lock_init (dfa->lock); /* bird */
    793 
    794755#ifdef DEBUG
     756  /* Note: length+1 will not overflow since it is checked in init_dfa.  */
    795757  dfa->re_str = re_malloc (char, length + 1);
    796758  strncpy (dfa->re_str, pattern, length + 1);
    797759#endif
     760
     761  __libc_lock_init (dfa->lock);
    798762
    799763  err = re_string_construct (&regexp, pattern, length, preg->translate,
     
    848812
    849813static reg_errcode_t
    850 init_dfa (dfa, pat_len)
    851      re_dfa_t *dfa;
    852      int pat_len;
    853 {
    854   int table_size;
     814init_dfa (re_dfa_t *dfa, size_t pat_len)
     815{
     816  unsigned int table_size;
    855817#ifndef _LIBC
    856818  char *codeset_name;
     
    862824  dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
    863825
     826  /* Avoid overflows.  */
     827  if (pat_len == SIZE_MAX)
     828    return REG_ESPACE;
     829
    864830  dfa->nodes_alloc = pat_len + 1;
    865831  dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
    866832
    867   dfa->states_alloc = pat_len + 1;
    868 
    869833  /*  table_size = 2 ^ ceil(log pat_len) */
    870   for (table_size = 1; table_size > 0; table_size <<= 1)
     834  for (table_size = 1; ; table_size <<= 1)
    871835    if (table_size > pat_len)
    872836      break;
     
    915879          int i, j, ch;
    916880
    917           dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset), 1);
     881          dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
    918882          if (BE (dfa->sb_char == NULL, 0))
    919883            return REG_ESPACE;
    920884
    921           /* Clear all bits by, then set those corresponding to single
    922              byte chars.  */
    923           bitset_empty (dfa->sb_char);
    924 
    925           for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
    926             for (j = 0; j < UINT_BITS; ++j, ++ch)
     885          /* Set the bits corresponding to single byte chars.  */
     886          for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
     887            for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
    927888              {
    928889                wint_t wch = __btowc (ch);
    929890                if (wch != WEOF)
    930                   dfa->sb_char[i] |= 1 << j;
    931 # if !defined (_LIBC) && !defined (__INNOTEK_LIBC__)
    932                 if (isascii (ch) && wch != (wchar_t) ch)
     891                  dfa->sb_char[i] |= (bitset_word_t) 1 << j;
     892# if !defined (_LIBC)
     893                if (isascii (ch) && wch != ch)
    933894                  dfa->map_notascii = 1;
    934895# endif
     
    948909
    949910static void
    950 init_word_char (dfa)
    951      re_dfa_t *dfa;
     911internal_function
     912init_word_char (re_dfa_t *dfa)
    952913{
    953914  int i, j, ch;
    954915  dfa->word_ops_used = 1;
    955   for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
    956     for (j = 0; j < UINT_BITS; ++j, ++ch)
     916  for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
     917    for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
    957918      if (isalnum (ch) || ch == '_')
    958         dfa->word_char[i] |= 1 << j;
     919        dfa->word_char[i] |= (bitset_word_t) 1 << j;
    959920}
    960921
     
    962923
    963924static void
    964 free_workarea_compile (preg)
    965      regex_t *preg;
     925free_workarea_compile (regex_t *preg)
    966926{
    967927  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     
    982942
    983943static reg_errcode_t
    984 create_initial_state (dfa)
    985      re_dfa_t *dfa;
     944create_initial_state (re_dfa_t *dfa)
    986945{
    987946  int first, i;
     
    10661025
    10671026static void
    1068 optimize_utf8 (dfa)
    1069      re_dfa_t *dfa;
     1027optimize_utf8 (re_dfa_t *dfa)
    10701028{
    10711029  int node, i, mb_chars = 0, has_period = 0;
     
    11041062        return;
    11051063      case SIMPLE_BRACKET:
    1106         /* Just double check.  */
    1107         for (i = 0x80 / UINT_BITS; i < BITSET_UINTS; ++i)
     1064        /* Just double check.  The non-ASCII range starts at 0x80.  */
     1065        assert (0x80 % BITSET_WORD_BITS == 0);
     1066        for (i = 0x80 / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
    11081067          if (dfa->nodes[node].opr.sbcset[i])
    11091068            return;
     
    11351094
    11361095static reg_errcode_t
    1137 analyze (preg)
    1138      regex_t *preg;
     1096analyze (regex_t *preg)
    11391097{
    11401098  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     
    11991157   some hairy code in these two functions.  */
    12001158static reg_errcode_t
    1201 postorder (root, fn, extra)
    1202      bin_tree_t *root;
    1203      reg_errcode_t (fn (void *, bin_tree_t *));
    1204      void *extra;
     1159postorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
     1160           void *extra)
    12051161{
    12061162  bin_tree_t *node, *prev;
     
    12331189
    12341190static reg_errcode_t
    1235 preorder (root, fn, extra)
    1236      bin_tree_t *root;
    1237      reg_errcode_t (fn (void *, bin_tree_t *));
    1238      void *extra;
     1191preorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
     1192          void *extra)
    12391193{
    12401194  bin_tree_t *node;
     
    12681222   backreferences as well.  Requires a preorder visit.  */
    12691223static reg_errcode_t
    1270 optimize_subexps (extra, node)
    1271      void *extra;
    1272      bin_tree_t *node;
     1224optimize_subexps (void *extra, bin_tree_t *node)
    12731225{
    12741226  re_dfa_t *dfa = (re_dfa_t *) extra;
     
    12911243
    12921244      dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
    1293       if (other_idx < 8 * sizeof (dfa->used_bkref_map))
    1294         dfa->used_bkref_map &= ~(1 << other_idx);
     1245      if (other_idx < BITSET_WORD_BITS)
     1246          dfa->used_bkref_map &= ~((bitset_word_t) 1 << other_idx);
    12951247    }
    12961248
     
    13011253   of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP.  */
    13021254static reg_errcode_t
    1303 lower_subexps (extra, node)
    1304      void *extra;
    1305      bin_tree_t *node;
     1255lower_subexps (void *extra, bin_tree_t *node)
    13061256{
    13071257  regex_t *preg = (regex_t *) extra;
     
    13251275
    13261276static bin_tree_t *
    1327 lower_subexp (err, preg, node)
    1328      reg_errcode_t *err;
    1329      regex_t *preg;
    1330      bin_tree_t *node;
     1277lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
    13311278{
    13321279  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     
    13401287         this case is the sed "script" /\(\)/x.  */
    13411288      && node->left != NULL
    1342       && (node->token.opr.idx >= 8 * sizeof (dfa->used_bkref_map)
    1343           || !(dfa->used_bkref_map & (1 << node->token.opr.idx))))
     1289      && (node->token.opr.idx >= BITSET_WORD_BITS
     1290          || !(dfa->used_bkref_map
     1291               & ((bitset_word_t) 1 << node->token.opr.idx))))
    13441292    return node->left;
    13451293
     
    13641312   nodes.  Requires a postorder visit.  */
    13651313static reg_errcode_t
    1366 calc_first (extra, node)
    1367      void *extra;
    1368      bin_tree_t *node;
     1314calc_first (void *extra, bin_tree_t *node)
    13691315{
    13701316  re_dfa_t *dfa = (re_dfa_t *) extra;
     
    13861332/* Pass 2: compute NEXT on the tree.  Preorder visit.  */
    13871333static reg_errcode_t
    1388 calc_next (extra, node)
    1389      void *extra;
    1390      bin_tree_t *node;
     1334calc_next (void *extra, bin_tree_t *node)
    13911335{
    13921336  switch (node->token.type)
     
    14111355/* Pass 3: link all DFA nodes to their NEXT node (any order will do).  */
    14121356static reg_errcode_t
    1413 link_nfa_nodes (extra, node)
    1414      void *extra;
    1415      bin_tree_t *node;
     1357link_nfa_nodes (void *extra, bin_tree_t *node)
    14161358{
    14171359  re_dfa_t *dfa = (re_dfa_t *) extra;
     
    14731415
    14741416static reg_errcode_t
    1475 duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
    1476                         init_constraint)
    1477      re_dfa_t *dfa;
    1478      int top_org_node, top_clone_node, root_node;
    1479      unsigned int init_constraint;
    1480 {
    1481   reg_errcode_t err;
     1417internal_function
     1418duplicate_node_closure (re_dfa_t *dfa, int top_org_node, int top_clone_node,
     1419                        int root_node, unsigned int init_constraint)
     1420{
    14821421  int org_node, clone_node, ret;
    14831422  unsigned int constraint = init_constraint;
     
    14931432          org_dest = dfa->nexts[org_node];
    14941433          re_node_set_empty (dfa->edests + clone_node);
    1495           err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
    1496           if (BE (err != REG_NOERROR, 0))
    1497             return err;
     1434          clone_dest = duplicate_node (dfa, org_dest, constraint);
     1435          if (BE (clone_dest == -1, 0))
     1436            return REG_ESPACE;
    14981437          dfa->nexts[clone_node] = dfa->nexts[org_node];
    14991438          ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
     
    15311470              constraint |= dfa->nodes[org_node].opr.ctx_type;
    15321471            }
    1533           err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
    1534           if (BE (err != REG_NOERROR, 0))
    1535             return err;
     1472          clone_dest = duplicate_node (dfa, org_dest, constraint);
     1473          if (BE (clone_dest == -1, 0))
     1474            return REG_ESPACE;
    15361475          ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
    15371476          if (BE (ret < 0, 0))
     
    15491488            {
    15501489              /* There are no such a duplicated node, create a new one.  */
    1551               err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
    1552               if (BE (err != REG_NOERROR, 0))
    1553                 return err;
     1490              reg_errcode_t err;
     1491              clone_dest = duplicate_node (dfa, org_dest, constraint);
     1492              if (BE (clone_dest == -1, 0))
     1493                return REG_ESPACE;
    15541494              ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
    15551495              if (BE (ret < 0, 0))
     
    15701510
    15711511          org_dest = dfa->edests[org_node].elems[1];
    1572           err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
    1573           if (BE (err != REG_NOERROR, 0))
    1574             return err;
     1512          clone_dest = duplicate_node (dfa, org_dest, constraint);
     1513          if (BE (clone_dest == -1, 0))
     1514            return REG_ESPACE;
    15751515          ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
    15761516          if (BE (ret < 0, 0))
     
    15871527
    15881528static int
    1589 search_duplicated_node (dfa, org_node, constraint)
    1590      re_dfa_t *dfa;
    1591      int org_node;
    1592      unsigned int constraint;
     1529search_duplicated_node (const re_dfa_t *dfa, int org_node,
     1530                        unsigned int constraint)
    15931531{
    15941532  int idx;
     
    16031541
    16041542/* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
    1605    The new index will be stored in NEW_IDX and return REG_NOERROR if succeeded,
    1606    otherwise return the error code.  */
     1543   Return the index of the new node, or -1 if insufficient storage is
     1544   available.  */
     1545
     1546static int
     1547duplicate_node (re_dfa_t *dfa, int org_idx, unsigned int constraint)
     1548{
     1549  int dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
     1550  if (BE (dup_idx != -1, 1))
     1551    {
     1552      dfa->nodes[dup_idx].constraint = constraint;
     1553      if (dfa->nodes[org_idx].type == ANCHOR)
     1554        dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
     1555      dfa->nodes[dup_idx].duplicated = 1;
     1556
     1557      /* Store the index of the original node.  */
     1558      dfa->org_indices[dup_idx] = org_idx;
     1559    }
     1560  return dup_idx;
     1561}
    16071562
    16081563static reg_errcode_t
    1609 duplicate_node (new_idx, dfa, org_idx, constraint)
    1610      re_dfa_t *dfa;
    1611      int *new_idx, org_idx;
    1612      unsigned int constraint;
    1613 {
    1614   int dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
    1615   if (BE (dup_idx == -1, 0))
    1616     return REG_ESPACE;
    1617   dfa->nodes[dup_idx].constraint = constraint;
    1618   if (dfa->nodes[org_idx].type == ANCHOR)
    1619     dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
    1620   dfa->nodes[dup_idx].duplicated = 1;
    1621 
    1622   /* Store the index of the original node.  */
    1623   dfa->org_indices[dup_idx] = org_idx;
    1624   *new_idx = dup_idx;
    1625   return REG_NOERROR;
    1626 }
    1627 
    1628 static reg_errcode_t
    1629 calc_inveclosure (dfa)
    1630      re_dfa_t *dfa;
     1564calc_inveclosure (re_dfa_t *dfa)
    16311565{
    16321566  int src, idx, ret;
     
    16511585
    16521586static reg_errcode_t
    1653 calc_eclosure (dfa)
    1654      re_dfa_t *dfa;
     1587calc_eclosure (re_dfa_t *dfa)
    16551588{
    16561589  int node_idx, incomplete;
     
    16961629
    16971630static reg_errcode_t
    1698 calc_eclosure_iter (new_set, dfa, node, root)
    1699      re_node_set *new_set;
    1700      re_dfa_t *dfa;
    1701      int node, root;
     1631calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, int node, int root)
    17021632{
    17031633  reg_errcode_t err;
     
    17221652      && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
    17231653    {
    1724       int org_node, cur_node;
    1725       org_node = cur_node = node;
    17261654      err = duplicate_node_closure (dfa, node, node, node, constraint);
    17271655      if (BE (err != REG_NOERROR, 0))
     
    17801708
    17811709static void
    1782 fetch_token (result, input, syntax)
    1783      re_token_t *result;
    1784      re_string_t *input;
    1785      reg_syntax_t syntax;
     1710internal_function
     1711fetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax)
    17861712{
    17871713  re_string_skip_bytes (input, peek_token (result, input, syntax));
     
    17921718
    17931719static int
    1794 peek_token (token, input, syntax)
    1795      re_token_t *token;
    1796      re_string_t *input;
    1797      reg_syntax_t syntax;
     1720internal_function
     1721peek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
    17981722{
    17991723  unsigned char c;
     
    20331957
    20341958static int
    2035 peek_token_bracket (token, input, syntax)
    2036      re_token_t *token;
    2037      re_string_t *input;
    2038      reg_syntax_t syntax;
     1959internal_function
     1960peek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
    20391961{
    20401962  unsigned char c;
     
    21332055
    21342056static bin_tree_t *
    2135 parse (regexp, preg, syntax, err)
    2136      re_string_t *regexp;
    2137      regex_t *preg;
    2138      reg_syntax_t syntax;
    2139      reg_errcode_t *err;
     2057parse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax,
     2058       reg_errcode_t *err)
    21402059{
    21412060  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     
    21702089
    21712090static bin_tree_t *
    2172 parse_reg_exp (regexp, preg, token, syntax, nest, err)
    2173      re_string_t *regexp;
    2174      regex_t *preg;
    2175      re_token_t *token;
    2176      reg_syntax_t syntax;
    2177      int nest;
    2178      reg_errcode_t *err;
     2091parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
     2092               reg_syntax_t syntax, int nest, reg_errcode_t *err)
    21792093{
    21802094  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     
    22162130
    22172131static bin_tree_t *
    2218 parse_branch (regexp, preg, token, syntax, nest, err)
    2219      re_string_t *regexp;
    2220      regex_t *preg;
    2221      re_token_t *token;
    2222      reg_syntax_t syntax;
    2223      int nest;
    2224      reg_errcode_t *err;
     2132parse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token,
     2133              reg_syntax_t syntax, int nest, reg_errcode_t *err)
    22252134{
    22262135  bin_tree_t *tree, *exp;
     
    22612170
    22622171static bin_tree_t *
    2263 parse_expression (regexp, preg, token, syntax, nest, err)
    2264      re_string_t *regexp;
    2265      regex_t *preg;
    2266      re_token_t *token;
    2267      reg_syntax_t syntax;
    2268      int nest;
    2269      reg_errcode_t *err;
     2172parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
     2173                  reg_syntax_t syntax, int nest, reg_errcode_t *err)
    22702174{
    22712175  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     
    24822386
    24832387static bin_tree_t *
    2484 parse_sub_exp (regexp, preg, token, syntax, nest, err)
    2485      re_string_t *regexp;
    2486      regex_t *preg;
    2487      re_token_t *token;
    2488      reg_syntax_t syntax;
    2489      int nest;
    2490      reg_errcode_t *err;
     2388parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
     2389               reg_syntax_t syntax, int nest, reg_errcode_t *err)
    24912390{
    24922391  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     
    25082407        return NULL;
    25092408    }
    2510   dfa->completed_bkref_map |= 1 << cur_nsub;
     2409
     2410  if (cur_nsub <= '9' - '1')
     2411    dfa->completed_bkref_map |= 1 << cur_nsub;
    25112412
    25122413  tree = create_tree (dfa, tree, NULL, SUBEXP);
     
    25232424
    25242425static bin_tree_t *
    2525 parse_dup_op (elem, regexp, dfa, token, syntax, err)
    2526      bin_tree_t *elem;
    2527      re_string_t *regexp;
    2528      re_dfa_t *dfa;
    2529      re_token_t *token;
    2530      reg_syntax_t syntax;
    2531      reg_errcode_t *err;
     2426parse_dup_op (bin_tree_t *elem, re_string_t *regexp, re_dfa_t *dfa,
     2427              re_token_t *token, reg_syntax_t syntax, reg_errcode_t *err)
    25322428{
    25332429  bin_tree_t *tree = NULL, *old_tree = NULL;
     
    26682564
    26692565static reg_errcode_t
     2566internal_function
    26702567# ifdef RE_ENABLE_I18N
    2671 build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
    2672      re_charset_t *mbcset;
    2673      int *range_alloc;
     2568build_range_exp (bitset_t sbcset, re_charset_t *mbcset, int *range_alloc,
     2569                 bracket_elem_t *start_elem, bracket_elem_t *end_elem, reg_syntax_t syntax) /* bird: syntax */
    26742570# else /* not RE_ENABLE_I18N */
    2675 build_range_exp (sbcset, start_elem, end_elem)
     2571build_range_exp (bitset_t sbcset, bracket_elem_t *start_elem,
     2572                 bracket_elem_t *end_elem, reg_syntax_t syntax)                             /* bird: syntax */
    26762573# endif /* not RE_ENABLE_I18N */
    2677      re_bitset_ptr_t sbcset;
    2678      bracket_elem_t *start_elem, *end_elem;
    26792574{
    26802575  unsigned int start_ch, end_ch;
     
    26952590# ifdef RE_ENABLE_I18N
    26962591  {
    2697     wint_t wc, start_wc, end_wc;
     2592    wchar_t wc;
     2593    wint_t start_wc;
     2594    wint_t end_wc;
    26982595    wchar_t cmp_buf[6] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
    26992596
     
    27122609    cmp_buf[0] = start_wc;
    27132610    cmp_buf[4] = end_wc;
    2714     if (wcscoll (cmp_buf, cmp_buf + 4) > 0)
     2611    if ((syntax & RE_NO_EMPTY_RANGES) && wcscoll (cmp_buf, cmp_buf + 4) > 0) /* bird: RE_NO_EMPTY_RANGES */
    27152612      return REG_ERANGE;
    27162613
     
    27692666                 : 0));
    27702667    if (start_ch > end_ch)
    2771       return REG_ERANGE;
     2668      return (syntax & RE_NO_EMPTY_RANGES) ? REG_ERANGE : REG_NOERROR; /* bird: added RE_NO_EMPTY_RANGES check. */
     2669
    27722670    /* Build the table for single byte characters.  */
    27732671    for (ch = 0; ch < SBC_MAX; ++ch)
     
    27882686
    27892687static reg_errcode_t
     2688internal_function
    27902689# ifdef RE_ENABLE_I18N
    2791 build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
    2792      re_charset_t *mbcset;
    2793      int *coll_sym_alloc;
     2690build_collating_symbol (bitset_t sbcset, re_charset_t *mbcset,
     2691                        int *coll_sym_alloc, const unsigned char *name)
    27942692# else /* not RE_ENABLE_I18N */
    2795 build_collating_symbol (sbcset, name)
     2693build_collating_symbol (bitset_t sbcset, const unsigned char *name)
    27962694# endif /* not RE_ENABLE_I18N */
    2797      re_bitset_ptr_t sbcset;
    2798      const unsigned char *name;
    27992695{
    28002696  size_t name_len = strlen ((const char *) name);
     
    28132709
    28142710static bin_tree_t *
    2815 parse_bracket_exp (regexp, dfa, token, syntax, err)
    2816      re_string_t *regexp;
    2817      re_dfa_t *dfa;
    2818      re_token_t *token;
    2819      reg_syntax_t syntax;
    2820      reg_errcode_t *err;
     2711parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
     2712                   reg_syntax_t syntax, reg_errcode_t *err)
    28212713{
    28222714#ifdef _LIBC
     
    28402732      int32_t hash = elem_hash ((const char *) name, name_len);
    28412733      int32_t elem = hash % table_size;
    2842       int32_t second = hash % (table_size - 2);
    2843       while (symb_table[2 * elem] != 0)
    2844         {
    2845           /* First compare the hashing value.  */
    2846           if (symb_table[2 * elem] == hash
    2847               /* Compare the length of the name.  */
    2848               && name_len == extra[symb_table[2 * elem + 1]]
    2849               /* Compare the name.  */
    2850               && memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
    2851                          name_len) == 0)
     2734      if (symb_table[2 * elem] != 0)
     2735        {
     2736          int32_t second = hash % (table_size - 2) + 1;
     2737
     2738          do
    28522739            {
    2853               /* Yep, this is the entry.  */
    2854               break;
     2740              /* First compare the hashing value.  */
     2741              if (symb_table[2 * elem] == hash
     2742                  /* Compare the length of the name.  */
     2743                  && name_len == extra[symb_table[2 * elem + 1]]
     2744                  /* Compare the name.  */
     2745                  && memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
     2746                             name_len) == 0)
     2747                {
     2748                  /* Yep, this is the entry.  */
     2749                  break;
     2750                }
     2751
     2752              /* Next entry.  */
     2753              elem += second;
    28552754            }
    2856 
    2857           /* Next entry.  */
    2858           elem += second;
     2755          while (symb_table[2 * elem] != 0);
    28592756        }
    28602757      return elem;
     
    29382835         re_charset_t *mbcset;
    29392836         int *range_alloc;
    2940          re_bitset_ptr_t sbcset;
     2837         bitset_t sbcset;
    29412838         bracket_elem_t *start_elem, *end_elem;
    29422839    {
     
    30212918         re_charset_t *mbcset;
    30222919         int *coll_sym_alloc;
    3023          re_bitset_ptr_t sbcset;
     2920         bitset_t sbcset;
    30242921         const unsigned char *name;
    30252922    {
     
    30982995      if (MB_CUR_MAX > 1)
    30992996      */
    3100         collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
     2997      collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
    31012998      table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB);
    31022999      symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE,
     
    31063003    }
    31073004#endif
    3108   sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
     3005  sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
    31093006#ifdef RE_ENABLE_I18N
    31103007  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
     
    32173114          *err = build_range_exp (sbcset,
    32183115                                  dfa->mb_cur_max > 1 ? mbcset : NULL,
    3219                                   &range_alloc, &start_elem, &end_elem);
     3116                                  &range_alloc, &start_elem, &end_elem, syntax);
    32203117# else
    3221           *err = build_range_exp (sbcset, &start_elem, &end_elem);
     3118          *err = build_range_exp (sbcset, &start_elem, &end_elem, syntax);
    32223119# endif
    32233120#endif /* RE_ENABLE_I18N */
     
    33163213      if (BE (mbc_tree == NULL, 0))
    33173214        goto parse_bracket_exp_espace;
    3318       for (sbc_idx = 0; sbc_idx < BITSET_UINTS; ++sbc_idx)
     3215      for (sbc_idx = 0; sbc_idx < BITSET_WORDS; ++sbc_idx)
    33193216        if (sbcset[sbc_idx])
    33203217          break;
    33213218      /* If there are no bits set in sbcset, there is no point
    33223219         of having both SIMPLE_BRACKET and COMPLEX_BRACKET.  */
    3323       if (sbc_idx < BITSET_UINTS)
     3220      if (sbc_idx < BITSET_WORDS)
    33243221        {
    33253222          /* Build a tree for simple bracket.  */
     
    33693266
    33703267static reg_errcode_t
    3371 parse_bracket_element (elem, regexp, token, token_len, dfa, syntax,
    3372                        accept_hyphen)
    3373      bracket_elem_t *elem;
    3374      re_string_t *regexp;
    3375      re_token_t *token;
    3376      int token_len;
    3377      re_dfa_t *dfa;
    3378      reg_syntax_t syntax;
    3379      int accept_hyphen;
     3268parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
     3269                       re_token_t *token, int token_len, re_dfa_t *dfa,
     3270                       reg_syntax_t syntax, int accept_hyphen)
    33803271{
    33813272#ifdef RE_ENABLE_I18N
     
    34153306
    34163307static reg_errcode_t
    3417 parse_bracket_symbol (elem, regexp, token)
    3418      bracket_elem_t *elem;
    3419      re_string_t *regexp;
    3420      re_token_t *token;
     3308parse_bracket_symbol (bracket_elem_t *elem, re_string_t *regexp,
     3309                      re_token_t *token)
    34213310{
    34223311  unsigned char ch, delim = token->opr.c;
     
    34653354static reg_errcode_t
    34663355#ifdef RE_ENABLE_I18N
    3467 build_equiv_class (sbcset, mbcset, equiv_class_alloc, name)
    3468      re_charset_t *mbcset;
    3469      int *equiv_class_alloc;
     3356build_equiv_class (bitset_t sbcset, re_charset_t *mbcset,
     3357                   int *equiv_class_alloc, const unsigned char *name)
    34703358#else /* not RE_ENABLE_I18N */
    3471 build_equiv_class (sbcset, name)
     3359build_equiv_class (bitset_t sbcset, const unsigned char *name)
    34723360#endif /* not RE_ENABLE_I18N */
    3473      re_bitset_ptr_t sbcset;
    3474      const unsigned char *name;
    3475 {
    3476 #if defined _LIBC
     3361{
     3362#ifdef _LIBC
    34773363  uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
    34783364  if (nrules != 0)
     
    35603446static reg_errcode_t
    35613447#ifdef RE_ENABLE_I18N
    3562 build_charclass (trans, sbcset, mbcset, char_class_alloc, class_name, syntax)
    3563      re_charset_t *mbcset;
    3564      int *char_class_alloc;
     3448build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
     3449                 re_charset_t *mbcset, int *char_class_alloc,
     3450                 const unsigned char *class_name, reg_syntax_t syntax)
    35653451#else /* not RE_ENABLE_I18N */
    3566 build_charclass (trans, sbcset, class_name, syntax)
     3452build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
     3453                 const unsigned char *class_name, reg_syntax_t syntax)
    35673454#endif /* not RE_ENABLE_I18N */
    3568      unsigned RE_TRANSLATE_TYPE trans;
    3569      re_bitset_ptr_t sbcset;
    3570      const unsigned char *class_name;
    3571      reg_syntax_t syntax;
    35723455{
    35733456  int i;
     
    35993482
    36003483#define BUILD_CHARCLASS_LOOP(ctype_func)        \
    3601     for (i = 0; i < SBC_MAX; ++i)               \
     3484  do {                                          \
     3485    if (BE (trans != NULL, 0))                  \
    36023486      {                                         \
    3603         if (ctype_func (i))                     \
    3604           {                                     \
    3605             int ch = trans ? trans[i] : i;      \
    3606             bitset_set (sbcset, ch);            \
    3607           }                                     \
    3608       }
     3487        for (i = 0; i < SBC_MAX; ++i)           \
     3488          if (ctype_func (i))                   \
     3489            bitset_set (sbcset, trans[i]);      \
     3490      }                                         \
     3491    else                                        \
     3492      {                                         \
     3493        for (i = 0; i < SBC_MAX; ++i)           \
     3494          if (ctype_func (i))                   \
     3495            bitset_set (sbcset, i);             \
     3496      }                                         \
     3497  } while (0)
    36093498
    36103499  if (strcmp (name, "alnum") == 0)
    3611     BUILD_CHARCLASS_LOOP (isalnum)
     3500    BUILD_CHARCLASS_LOOP (isalnum);
    36123501  else if (strcmp (name, "cntrl") == 0)
    3613     BUILD_CHARCLASS_LOOP (iscntrl)
     3502    BUILD_CHARCLASS_LOOP (iscntrl);
    36143503  else if (strcmp (name, "lower") == 0)
    3615     BUILD_CHARCLASS_LOOP (islower)
     3504    BUILD_CHARCLASS_LOOP (islower);
    36163505  else if (strcmp (name, "space") == 0)
    3617     BUILD_CHARCLASS_LOOP (isspace)
     3506    BUILD_CHARCLASS_LOOP (isspace);
    36183507  else if (strcmp (name, "alpha") == 0)
    3619     BUILD_CHARCLASS_LOOP (isalpha)
     3508    BUILD_CHARCLASS_LOOP (isalpha);
    36203509  else if (strcmp (name, "digit") == 0)
    3621     BUILD_CHARCLASS_LOOP (isdigit)
     3510    BUILD_CHARCLASS_LOOP (isdigit);
    36223511  else if (strcmp (name, "print") == 0)
    3623     BUILD_CHARCLASS_LOOP (isprint)
     3512    BUILD_CHARCLASS_LOOP (isprint);
    36243513  else if (strcmp (name, "upper") == 0)
    3625     BUILD_CHARCLASS_LOOP (isupper)
     3514    BUILD_CHARCLASS_LOOP (isupper);
    36263515  else if (strcmp (name, "blank") == 0)
    3627     BUILD_CHARCLASS_LOOP (isblank)
     3516    BUILD_CHARCLASS_LOOP (isblank);
    36283517  else if (strcmp (name, "graph") == 0)
    3629     BUILD_CHARCLASS_LOOP (isgraph)
     3518    BUILD_CHARCLASS_LOOP (isgraph);
    36303519  else if (strcmp (name, "punct") == 0)
    3631     BUILD_CHARCLASS_LOOP (ispunct)
     3520    BUILD_CHARCLASS_LOOP (ispunct);
    36323521  else if (strcmp (name, "xdigit") == 0)
    3633     BUILD_CHARCLASS_LOOP (isxdigit)
     3522    BUILD_CHARCLASS_LOOP (isxdigit);
    36343523  else
    36353524    return REG_ECTYPE;
     
    36393528
    36403529static bin_tree_t *
    3641 build_charclass_op (dfa, trans, class_name, extra, non_match, err)
    3642      re_dfa_t *dfa;
    3643      unsigned RE_TRANSLATE_TYPE trans;
    3644      const unsigned char *class_name;
    3645      const unsigned char *extra;
    3646      int non_match;
    3647      reg_errcode_t *err;
     3530build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,
     3531                    const unsigned char *class_name,
     3532                    const unsigned char *extra, int non_match,
     3533                    reg_errcode_t *err)
    36483534{
    36493535  re_bitset_ptr_t sbcset;
     
    36563542  bin_tree_t *tree;
    36573543
    3658   sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
     3544  sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
    36593545#ifdef RE_ENABLE_I18N
    36603546  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
     
    37593645
    37603646static int
    3761 fetch_number (input, token, syntax)
    3762      re_string_t *input;
    3763      re_token_t *token;
    3764      reg_syntax_t syntax;
     3647fetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax)
    37653648{
    37663649  int num = -1;
     
    38043687
    38053688static bin_tree_t *
    3806 create_tree (dfa, left, right, type)
    3807      re_dfa_t *dfa;
    3808      bin_tree_t *left;
    3809      bin_tree_t *right;
    3810      re_token_type_t type;
     3689create_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
     3690             re_token_type_t type)
    38113691{
    38123692  re_token_t t;
     
    38163696
    38173697static bin_tree_t *
    3818 create_token_tree (dfa, left, right, token)
    3819      re_dfa_t *dfa;
    3820      bin_tree_t *left;
    3821      bin_tree_t *right;
    3822      const re_token_t *token;
     3698create_token_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
     3699                   const re_token_t *token)
    38233700{
    38243701  bin_tree_t *tree;
     
    38563733
    38573734static reg_errcode_t
    3858 mark_opt_subexp (extra, node)
    3859      void *extra;
    3860      bin_tree_t *node;
     3735mark_opt_subexp (void *extra, bin_tree_t *node)
    38613736{
    38623737  int idx = (int) (long) extra;
     
    38983773
    38993774static bin_tree_t *
    3900 duplicate_tree (root, dfa)
    3901      const bin_tree_t *root;
    3902      re_dfa_t *dfa;
     3775duplicate_tree (const bin_tree_t *root, re_dfa_t *dfa)
    39033776{
    39043777  const bin_tree_t *node;
  • branches/libc-0.6/src/emx/src/lib/lgpl/posix/regex.c

    r2240 r3058  
    11/* Extended regular expression matching and search library.
    2    Copyright (C) 2002, 2003 Free Software Foundation, Inc.
     2   Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
    33   This file is part of the GNU C Library.
    44   Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
     
    2323#endif
    2424
    25 #define RE_ENABLE_I18N 1
    26 
    27 /* BSD 4.2, not delcared in the header */
    28 char * re_comp (const char *);
    29 int re_exec (const char *);
    30 
    31 
    32 #ifdef _AIX
    33 #pragma alloca
    34 #else
    35 # ifndef allocax           /* predefined by HP cc +Olibcalls */
    36 #  ifdef __GNUC__
    37 #   define alloca(size) __builtin_alloca (size)
    38 #  else
    39 #   if HAVE_ALLOCA_H
    40 #    include <alloca.h>
    41 #   else
    42 #    ifdef __hpux
    43         void *alloca ();
    44 #    else
    45 #     if !defined __OS2__ && !defined WIN32
    46         char *alloca ();
    47 #     else
    48 #      include <malloc.h>       /* OS/2 defines alloca in here */
    49 #     endif
    50 #    endif
    51 #   endif
    52 #  endif
    53 # endif
     25/* Make sure noone compiles this code with a C++ compiler.  */
     26#ifdef __cplusplus
     27# error "This is C code, use a C compiler"
    5428#endif
    5529
     
    7953#endif
    8054
    81 /* POSIX says that <sys/types.h> must be included (by the caller) before
    82    <regex.h>.  */
    83 #include <sys/types.h>
    84 
    8555/* On some systems, limits.h sets RE_DUP_MAX to a lower value than
    8656   GNU regex allows.  Include it before <regex.h>, which correctly
     
    9666
    9767/* Binary backward compatibility.  */
    98 #if defined _LIBC && _LIBC
     68#if _LIBC
    9969# include <shlib-compat.h>
    10070# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3)
  • branches/libc-0.6/src/emx/src/lib/lgpl/posix/regex_internal.c

    r2240 r3058  
    11/* Extended regular expression matching and search library.
    2    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
     2   Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
    33   This file is part of the GNU C Library.
    44   Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
     
    2323                                        RE_TRANSLATE_TYPE trans, int icase,
    2424                                        const re_dfa_t *dfa) internal_function;
    25 #ifdef RE_ENABLE_I18N
    26 static int re_string_skip_chars (re_string_t *pstr, int new_raw_idx,
    27                                  wint_t *last_wc) internal_function;
    28 #endif /* RE_ENABLE_I18N */
    29 static reg_errcode_t register_state (re_dfa_t *dfa, re_dfastate_t *newstate,
    30                                      unsigned int hash) internal_function;
    31 static re_dfastate_t *create_ci_newstate (re_dfa_t *dfa,
     25static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
    3226                                          const re_node_set *nodes,
    3327                                          unsigned int hash) internal_function;
    34 static re_dfastate_t *create_cd_newstate (re_dfa_t *dfa,
     28static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
    3529                                          const re_node_set *nodes,
    3630                                          unsigned int context,
    3731                                          unsigned int hash) internal_function;
    38 static unsigned int inline calc_state_hash (const re_node_set *nodes,
    39                                             unsigned int context) internal_function;
    4032
    4133
     
    4638
    4739static reg_errcode_t
    48 re_string_allocate (pstr, str, len, init_len, trans, icase, dfa)
    49      re_string_t *pstr;
    50      const char *str;
    51      int len, init_len, icase;
    52      RE_TRANSLATE_TYPE trans;
    53      const re_dfa_t *dfa;
     40internal_function
     41re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len,
     42                    RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
    5443{
    5544  reg_errcode_t ret;
     
    7766
    7867static reg_errcode_t
    79 re_string_construct (pstr, str, len, trans, icase, dfa)
    80      re_string_t *pstr;
    81      const char *str;
    82      int len, icase;
    83      RE_TRANSLATE_TYPE trans;
    84      const re_dfa_t *dfa;
     68internal_function
     69re_string_construct (re_string_t *pstr, const char *str, int len,
     70                     RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
    8571{
    8672  reg_errcode_t ret;
     
    143129
    144130static reg_errcode_t
    145 re_string_realloc_buffers (pstr, new_buf_len)
    146      re_string_t *pstr;
    147      int new_buf_len;
     131internal_function
     132re_string_realloc_buffers (re_string_t *pstr, int new_buf_len)
    148133{
    149134#ifdef RE_ENABLE_I18N
    150135  if (pstr->mb_cur_max > 1)
    151136    {
    152       wint_t *new_array = re_realloc (pstr->wcs, wint_t, new_buf_len);
    153       if (BE (new_array == NULL, 0))
     137      wint_t *new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len);
     138      if (BE (new_wcs == NULL, 0))
    154139        return REG_ESPACE;
    155       pstr->wcs = new_array;
     140      pstr->wcs = new_wcs;
    156141      if (pstr->offsets != NULL)
    157142        {
    158           int *new_array = re_realloc (pstr->offsets, int, new_buf_len);
    159           if (BE (new_array == NULL, 0))
     143          int *new_offsets = re_realloc (pstr->offsets, int, new_buf_len);
     144          if (BE (new_offsets == NULL, 0))
    160145            return REG_ESPACE;
    161           pstr->offsets = new_array;
     146          pstr->offsets = new_offsets;
    162147        }
    163148    }
     
    165150  if (pstr->mbs_allocated)
    166151    {
    167       unsigned char *new_array = re_realloc (pstr->mbs, unsigned char,
    168                                              new_buf_len);
    169       if (BE (new_array == NULL, 0))
     152      unsigned char *new_mbs = re_realloc (pstr->mbs, unsigned char,
     153                                           new_buf_len);
     154      if (BE (new_mbs == NULL, 0))
    170155        return REG_ESPACE;
    171       pstr->mbs = new_array;
     156      pstr->mbs = new_mbs;
    172157    }
    173158  pstr->bufs_len = new_buf_len;
     
    177162
    178163static void
    179 re_string_construct_common (str, len, pstr, trans, icase, dfa)
    180      const char *str;
    181      int len;
    182      re_string_t *pstr;
    183      RE_TRANSLATE_TYPE trans;
    184      int icase;
    185      const re_dfa_t *dfa;
     164internal_function
     165re_string_construct_common (const char *str, int len, re_string_t *pstr,
     166                            RE_TRANSLATE_TYPE trans, int icase,
     167                            const re_dfa_t *dfa)
    186168{
    187169  pstr->raw_mbs = (const unsigned char *) str;
    188170  pstr->len = len;
    189171  pstr->raw_len = len;
    190   pstr->trans = (unsigned RE_TRANSLATE_TYPE) trans;
     172  pstr->trans = trans;
    191173  pstr->icase = icase ? 1 : 0;
    192174  pstr->mbs_allocated = (trans != NULL || icase);
     
    212194
    213195static void
    214 build_wcs_buffer (pstr)
    215      re_string_t *pstr;
     196internal_function
     197build_wcs_buffer (re_string_t *pstr)
    216198{
    217199#ifdef _LIBC
     
    279261   but for REG_ICASE.  */
    280262
    281 static int
    282 build_wcs_upper_buffer (pstr)
    283      re_string_t *pstr;
     263static reg_errcode_t
     264internal_function
     265build_wcs_upper_buffer (re_string_t *pstr)
    284266{
    285267  mbstate_t prev_st;
     
    496478
    497479static int
    498 re_string_skip_chars (pstr, new_raw_idx, last_wc)
    499      re_string_t *pstr;
    500      int new_raw_idx;
    501      wint_t *last_wc;
     480internal_function
     481re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
    502482{
    503483  mbstate_t prev_st;
    504484  int rawbuf_idx;
    505485  size_t mbclen;
    506   wchar_t wc = 0;
     486  wchar_t wc = WEOF;
    507487
    508488  /* Skip the characters which are not necessary to check.  */
     
    517497      if (BE (mbclen == (size_t) -2 || mbclen == (size_t) -1 || mbclen == 0, 0))
    518498        {
    519           /* We treat these cases as a singlebyte character.  */
     499          /* We treat these cases as a single byte character.  */
     500          if (mbclen == 0 || remain_len == 0)
     501            wc = L'\0';
     502          else
     503            wc = *(unsigned char *) (pstr->raw_mbs + rawbuf_idx);
    520504          mbclen = 1;
    521505          pstr->cur_state = prev_st;
     
    533517
    534518static void
    535 build_upper_buffer (pstr)
    536      re_string_t *pstr;
     519internal_function
     520build_upper_buffer (re_string_t *pstr)
    537521{
    538522  int char_idx, end_idx;
     
    556540
    557541static void
    558 re_string_translate_buffer (pstr)
    559      re_string_t *pstr;
     542internal_function
     543re_string_translate_buffer (re_string_t *pstr)
    560544{
    561545  int buf_idx, end_idx;
     
    577561
    578562static reg_errcode_t
    579 re_string_reconstruct (pstr, idx, eflags)
    580      re_string_t *pstr;
    581      int idx, eflags;
     563internal_function
     564re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
    582565{
    583566  int offset = idx - pstr->raw_mbs_idx;
     
    604587  if (BE (offset != 0, 1))
    605588    {
    606       /* Are the characters which are already checked remain?  */
    607       if (BE (offset < pstr->valid_raw_len, 1)
    608 #ifdef RE_ENABLE_I18N
    609           /* Handling this would enlarge the code too much.
    610              Accept a slowdown in that case.  */
    611           && pstr->offsets_needed == 0
     589      /* Should the already checked characters be kept?  */
     590      if (BE (offset < pstr->valid_raw_len, 1))
     591        {
     592          /* Yes, move them to the front of the buffer.  */
     593#ifdef RE_ENABLE_I18N
     594          if (BE (pstr->offsets_needed, 0))
     595            {
     596              int low = 0, high = pstr->valid_len, mid;
     597              do
     598                {
     599                  mid = (high + low) / 2;
     600                  if (pstr->offsets[mid] > offset)
     601                    high = mid;
     602                  else if (pstr->offsets[mid] < offset)
     603                    low = mid + 1;
     604                  else
     605                    break;
     606                }
     607              while (low < high);
     608              if (pstr->offsets[mid] < offset)
     609                ++mid;
     610              pstr->tip_context = re_string_context_at (pstr, mid - 1,
     611                                                        eflags);
     612              /* This can be quite complicated, so handle specially
     613                 only the common and easy case where the character with
     614                 different length representation of lower and upper
     615                 case is present at or after offset.  */
     616              if (pstr->valid_len > offset
     617                  && mid == offset && pstr->offsets[mid] == offset)
     618                {
     619                  memmove (pstr->wcs, pstr->wcs + offset,
     620                           (pstr->valid_len - offset) * sizeof (wint_t));
     621                  memmove (pstr->mbs, pstr->mbs + offset, pstr->valid_len - offset);
     622                  pstr->valid_len -= offset;
     623                  pstr->valid_raw_len -= offset;
     624                  for (low = 0; low < pstr->valid_len; low++)
     625                    pstr->offsets[low] = pstr->offsets[low + offset] - offset;
     626                }
     627              else
     628                {
     629                  /* Otherwise, just find out how long the partial multibyte
     630                     character at offset is and fill it with WEOF/255.  */
     631                  pstr->len = pstr->raw_len - idx + offset;
     632                  pstr->stop = pstr->raw_stop - idx + offset;
     633                  pstr->offsets_needed = 0;
     634                  while (mid > 0 && pstr->offsets[mid - 1] == offset)
     635                    --mid;
     636                  while (mid < pstr->valid_len)
     637                    if (pstr->wcs[mid] != WEOF)
     638                      break;
     639                    else
     640                      ++mid;
     641                  if (mid == pstr->valid_len)
     642                    pstr->valid_len = 0;
     643                  else
     644                    {
     645                      pstr->valid_len = pstr->offsets[mid] - offset;
     646                      if (pstr->valid_len)
     647                        {
     648                          for (low = 0; low < pstr->valid_len; ++low)
     649                            pstr->wcs[low] = WEOF;
     650                          memset (pstr->mbs, 255, pstr->valid_len);
     651                        }
     652                    }
     653                  pstr->valid_raw_len = pstr->valid_len;
     654                }
     655            }
     656          else
    612657#endif
    613          )
    614         {
    615           /* Yes, move them to the front of the buffer.  */
    616           pstr->tip_context = re_string_context_at (pstr, offset - 1, eflags);
    617 #ifdef RE_ENABLE_I18N
    618           if (pstr->mb_cur_max > 1)
    619             memmove (pstr->wcs, pstr->wcs + offset,
    620                      (pstr->valid_len - offset) * sizeof (wint_t));
     658            {
     659              pstr->tip_context = re_string_context_at (pstr, offset - 1,
     660                                                        eflags);
     661#ifdef RE_ENABLE_I18N
     662              if (pstr->mb_cur_max > 1)
     663                memmove (pstr->wcs, pstr->wcs + offset,
     664                         (pstr->valid_len - offset) * sizeof (wint_t));
    621665#endif /* RE_ENABLE_I18N */
    622           if (BE (pstr->mbs_allocated, 0))
    623             memmove (pstr->mbs, pstr->mbs + offset,
    624                     pstr->valid_len - offset);
    625           pstr->valid_len -= offset;
    626           pstr->valid_raw_len -= offset;
    627 #ifdef DEBUG //bird
    628           assert (pstr->valid_len > 0);
     666              if (BE (pstr->mbs_allocated, 0))
     667                memmove (pstr->mbs, pstr->mbs + offset,
     668                        pstr->valid_len - offset);
     669              pstr->valid_len -= offset;
     670              pstr->valid_raw_len -= offset;
     671#if DEBUG
     672              assert (pstr->valid_len > 0);
    629673#endif
     674            }
    630675        }
    631676      else
    632677        {
    633678          /* No, skip all characters until IDX.  */
     679          int prev_valid_len = pstr->valid_len;
     680
    634681#ifdef RE_ENABLE_I18N
    635682          if (BE (pstr->offsets_needed, 0))
     
    641688#endif
    642689          pstr->valid_len = 0;
    643           pstr->valid_raw_len = 0;
    644690#ifdef RE_ENABLE_I18N
    645691          if (pstr->mb_cur_max > 1)
     
    656702                  raw = pstr->raw_mbs + pstr->raw_mbs_idx;
    657703                  end = raw + (offset - pstr->mb_cur_max);
    658                   for (p = raw + offset - 1; p >= end; --p)
    659                     if ((*p & 0xc0) != 0x80)
    660                       {
    661                         mbstate_t cur_state;
    662                         wchar_t wc2;
    663                         int mlen = raw + pstr->len - p;
    664                         unsigned char buf[6];
    665 
    666                         q = p;
    667                         if (BE (pstr->trans != NULL, 0))
    668                           {
    669                             int i = mlen < 6 ? mlen : 6;
    670                             while (--i >= 0)
    671                               buf[i] = pstr->trans[p[i]];
    672                             q = buf;
    673                           }
    674                         /* XXX Don't use mbrtowc, we know which conversion
    675                            to use (UTF-8 -> UCS4).  */
    676                         memset (&cur_state, 0, sizeof (cur_state));
    677                         mlen = (mbrtowc (&wc2, (const char *) p, mlen,
    678                                          &cur_state)
    679                                 - (raw + offset - p));
    680                         if (mlen >= 0)
    681                           {
    682                             memset (&pstr->cur_state, '\0',
    683                                     sizeof (mbstate_t));
    684                             pstr->valid_len = mlen;
    685                             wc = wc2;
    686                           }
    687                         break;
    688                       }
     704                  if (end < pstr->raw_mbs)
     705                    end = pstr->raw_mbs;
     706                  p = raw + offset - 1;
     707#ifdef _LIBC
     708                  /* We know the wchar_t encoding is UCS4, so for the simple
     709                     case, ASCII characters, skip the conversion step.  */
     710                  if (isascii (*p) && BE (pstr->trans == NULL, 1))
     711                    {
     712                      memset (&pstr->cur_state, '\0', sizeof (mbstate_t));
     713                      /* pstr->valid_len = 0; */
     714                      wc = (wchar_t) *p;
     715                    }
     716                  else
     717#endif
     718                    for (; p >= end; --p)
     719                      if ((*p & 0xc0) != 0x80)
     720                        {
     721                          mbstate_t cur_state;
     722                          wchar_t wc2;
     723                          int mlen = raw + pstr->len - p;
     724                          unsigned char buf[6];
     725                          size_t mbclen;
     726
     727                          q = p;
     728                          if (BE (pstr->trans != NULL, 0))
     729                            {
     730                              int i = mlen < 6 ? mlen : 6;
     731                              while (--i >= 0)
     732                                buf[i] = pstr->trans[p[i]];
     733                              q = buf;
     734                            }
     735                          /* XXX Don't use mbrtowc, we know which conversion
     736                             to use (UTF-8 -> UCS4).  */
     737                          memset (&cur_state, 0, sizeof (cur_state));
     738                          mbclen = mbrtowc (&wc2, (const char *) p, mlen,
     739                                            &cur_state);
     740                          if (raw + offset - p <= mbclen
     741                              && mbclen < (size_t) -2)
     742                            {
     743                              memset (&pstr->cur_state, '\0',
     744                                      sizeof (mbstate_t));
     745                              pstr->valid_len = mbclen - (raw + offset - p);
     746                              wc = wc2;
     747                            }
     748                          break;
     749                        }
    689750                }
    690751
    691752              if (wc == WEOF)
    692753                pstr->valid_len = re_string_skip_chars (pstr, idx, &wc) - idx;
     754              if (wc == WEOF)
     755                pstr->tip_context
     756                  = re_string_context_at (pstr, prev_valid_len - 1, eflags);
     757              else
     758                pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
     759                                      && IS_WIDE_WORD_CHAR (wc))
     760                                     ? CONTEXT_WORD
     761                                     : ((IS_WIDE_NEWLINE (wc)
     762                                         && pstr->newline_anchor)
     763                                        ? CONTEXT_NEWLINE : 0));
    693764              if (BE (pstr->valid_len, 0))
    694765                {
     
    699770                }
    700771              pstr->valid_raw_len = pstr->valid_len;
    701               pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
    702                                     && IS_WIDE_WORD_CHAR (wc))
    703                                    ? CONTEXT_WORD
    704                                    : ((IS_WIDE_NEWLINE (wc)
    705                                        && pstr->newline_anchor)
    706                                       ? CONTEXT_NEWLINE : 0));
    707772            }
    708773          else
     
    710775            {
    711776              int c = pstr->raw_mbs[pstr->raw_mbs_idx + offset - 1];
     777              pstr->valid_raw_len = 0;
    712778              if (pstr->trans)
    713779                c = pstr->trans[c];
     
    731797      if (pstr->icase)
    732798        {
    733           int ret = build_wcs_upper_buffer (pstr);
     799          reg_errcode_t ret = build_wcs_upper_buffer (pstr);
    734800          if (BE (ret != REG_NOERROR, 0))
    735801            return ret;
     
    740806  else
    741807#endif /* RE_ENABLE_I18N */
    742   if (BE (pstr->mbs_allocated, 0))
    743     {
    744       if (pstr->icase)
    745         build_upper_buffer (pstr);
    746       else if (pstr->trans != NULL)
    747         re_string_translate_buffer (pstr);
    748     }
    749   else
    750     pstr->valid_len = pstr->len;
     808    if (BE (pstr->mbs_allocated, 0))
     809      {
     810        if (pstr->icase)
     811          build_upper_buffer (pstr);
     812        else if (pstr->trans != NULL)
     813          re_string_translate_buffer (pstr);
     814      }
     815    else
     816      pstr->valid_len = pstr->len;
    751817
    752818  pstr->cur_idx = 0;
     
    755821
    756822static unsigned char
    757 re_string_peek_byte_case (pstr, idx)
    758      const re_string_t *pstr;
    759      int idx;
     823internal_function __attribute ((pure))
     824re_string_peek_byte_case (const re_string_t *pstr, int idx)
    760825{
    761826  int ch, off;
     
    792857
    793858static unsigned char
    794 re_string_fetch_byte_case (pstr)
    795      re_string_t *pstr;
     859internal_function __attribute ((pure))
     860re_string_fetch_byte_case (re_string_t *pstr)
    796861{
    797862  if (BE (!pstr->mbs_allocated, 1))
     
    829894
    830895static void
    831 re_string_destruct (pstr)
    832      re_string_t *pstr;
     896internal_function
     897re_string_destruct (re_string_t *pstr)
    833898{
    834899#ifdef RE_ENABLE_I18N
     
    843908
    844909static unsigned int
    845 re_string_context_at (input, idx, eflags)
    846      const re_string_t *input;
    847      int idx, eflags;
     910internal_function
     911re_string_context_at (const re_string_t *input, int idx, int eflags)
    848912{
    849913  int c;
     
    890954
    891955static reg_errcode_t
    892 re_node_set_alloc (set, size)
    893      re_node_set *set;
    894      int size;
     956internal_function
     957re_node_set_alloc (re_node_set *set, int size)
    895958{
    896959  set->alloc = size;
     
    903966
    904967static reg_errcode_t
    905 re_node_set_init_1 (set, elem)
    906      re_node_set *set;
    907      int elem;
     968internal_function
     969re_node_set_init_1 (re_node_set *set, int elem)
    908970{
    909971  set->alloc = 1;
     
    920982
    921983static reg_errcode_t
    922 re_node_set_init_2 (set, elem1, elem2)
    923      re_node_set *set;
    924      int elem1, elem2;
     984internal_function
     985re_node_set_init_2 (re_node_set *set, int elem1, int elem2)
    925986{
    926987  set->alloc = 2;
     
    9511012
    9521013static reg_errcode_t
    953 re_node_set_init_copy (dest, src)
    954      re_node_set *dest;
    955      const re_node_set *src;
     1014internal_function
     1015re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
    9561016{
    9571017  dest->nelem = src->nelem;
     
    9771037
    9781038static reg_errcode_t
    979 re_node_set_add_intersect (dest, src1, src2)
    980      re_node_set *dest;
    981      const re_node_set *src1, *src2;
     1039internal_function
     1040re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
     1041                           const re_node_set *src2)
    9821042{
    9831043  int i1, i2, is, id, delta, sbase;
     
    10681128
    10691129static reg_errcode_t
    1070 re_node_set_init_union (dest, src1, src2)
    1071      re_node_set *dest;
    1072      const re_node_set *src1, *src2;
     1130internal_function
     1131re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
     1132                        const re_node_set *src2)
    10731133{
    10741134  int i1, i2, id;
     
    11211181
    11221182static reg_errcode_t
    1123 re_node_set_merge (dest, src)
    1124      re_node_set *dest;
    1125      const re_node_set *src;
     1183internal_function
     1184re_node_set_merge (re_node_set *dest, const re_node_set *src)
    11261185{
    11271186  int is, id, sbase, delta;
     
    12051264
    12061265static int
    1207 re_node_set_insert (set, elem)
    1208      re_node_set *set;
    1209      int elem;
     1266internal_function
     1267re_node_set_insert (re_node_set *set, int elem)
    12101268{
    12111269  int idx;
     
    12301288  if (set->alloc == set->nelem)
    12311289    {
    1232       int *new_array;
     1290      int *new_elems;
    12331291      set->alloc = set->alloc * 2;
    1234       new_array = re_realloc (set->elems, int, set->alloc);
    1235       if (BE (new_array == NULL, 0))
     1292      new_elems = re_realloc (set->elems, int, set->alloc);
     1293      if (BE (new_elems == NULL, 0))
    12361294        return -1;
    1237       set->elems = new_array;
     1295      set->elems = new_elems;
    12381296    }
    12391297
     
    12631321
    12641322static int
    1265 re_node_set_insert_last (set, elem)
    1266      re_node_set *set;
    1267      int elem;
     1323internal_function
     1324re_node_set_insert_last (re_node_set *set, int elem)
    12681325{
    12691326  /* Realloc if we need.  */
    12701327  if (set->alloc == set->nelem)
    12711328    {
    1272       int *new_array;
     1329      int *new_elems;
    12731330      set->alloc = (set->alloc + 1) * 2;
    1274       new_array = re_realloc (set->elems, int, set->alloc);
    1275       if (BE (new_array == NULL, 0))
     1331      new_elems = re_realloc (set->elems, int, set->alloc);
     1332      if (BE (new_elems == NULL, 0))
    12761333        return -1;
    1277       set->elems = new_array;
     1334      set->elems = new_elems;
    12781335    }
    12791336
     
    12871344
    12881345static int
    1289 re_node_set_compare (set1, set2)
    1290      const re_node_set *set1, *set2;
     1346internal_function __attribute ((pure))
     1347re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
    12911348{
    12921349  int i;
     
    13021359
    13031360static int
    1304 re_node_set_contains (set, elem)
    1305      const re_node_set *set;
    1306      int elem;
     1361internal_function __attribute ((pure))
     1362re_node_set_contains (const re_node_set *set, int elem)
    13071363{
    13081364  unsigned int idx, right, mid;
     
    13251381
    13261382static void
    1327 re_node_set_remove_at (set, idx)
    1328      re_node_set *set;
    1329      int idx;
     1383internal_function
     1384re_node_set_remove_at (re_node_set *set, int idx)
    13301385{
    13311386  if (idx < 0 || idx >= set->nelem)
     
    13421397
    13431398static int
    1344 re_dfa_add_node (dfa, token)
    1345      re_dfa_t *dfa;
    1346      re_token_t token;
    1347 {
    1348 #ifdef RE_ENABLE_I18N //bird
     1399internal_function
     1400re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
     1401{
    13491402  int type = token.type;
    1350 #endif
    13511403  if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
    13521404    {
    1353       int new_nodes_alloc = dfa->nodes_alloc * 2;
     1405      size_t new_nodes_alloc = dfa->nodes_alloc * 2;
    13541406      int *new_nexts, *new_indices;
    13551407      re_node_set *new_edests, *new_eclosures;
    1356 
    1357       re_token_t *new_array = re_realloc (dfa->nodes, re_token_t,
    1358                                           new_nodes_alloc);
    1359       if (BE (new_array == NULL, 0))
     1408      re_token_t *new_nodes;
     1409
     1410      /* Avoid overflows.  */
     1411      if (BE (new_nodes_alloc < dfa->nodes_alloc, 0))
    13601412        return -1;
    1361       dfa->nodes = new_array;
     1413
     1414      new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc);
     1415      if (BE (new_nodes == NULL, 0))
     1416        return -1;
     1417      dfa->nodes = new_nodes;
    13621418      new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc);
    13631419      new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc);
     
    13851441}
    13861442
    1387 static unsigned int inline
    1388 calc_state_hash (nodes, context)
    1389      const re_node_set *nodes;
    1390      unsigned int context;
     1443static inline unsigned int
     1444internal_function
     1445calc_state_hash (const re_node_set *nodes, unsigned int context)
    13911446{
    13921447  unsigned int hash = nodes->nelem + context;
     
    14061461           optimization.  */
    14071462
    1408 static re_dfastate_t*
    1409 re_acquire_state (err, dfa, nodes)
    1410      reg_errcode_t *err;
    1411      re_dfa_t *dfa;
    1412      const re_node_set *nodes;
     1463static re_dfastate_t *
     1464internal_function
     1465re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
     1466                  const re_node_set *nodes)
    14131467{
    14141468  unsigned int hash;
     
    14351489  /* There are no appropriate state in the dfa, create the new one.  */
    14361490  new_state = create_ci_newstate (dfa, nodes, hash);
    1437   if (BE (new_state != NULL, 1))
    1438     return new_state;
    1439   else
    1440     {
    1441       *err = REG_ESPACE;
    1442       return NULL;
    1443     }
     1491  if (BE (new_state == NULL, 0))
     1492    *err = REG_ESPACE;
     1493
     1494  return new_state;
    14441495}
    14451496
     
    14541505           optimization.  */
    14551506
    1456 static re_dfastate_t*
    1457 re_acquire_state_context (err, dfa, nodes, context)
    1458      reg_errcode_t *err;
    1459      re_dfa_t *dfa;
    1460      const re_node_set *nodes;
    1461      unsigned int context;
     1507static re_dfastate_t *
     1508internal_function
     1509re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
     1510                          const re_node_set *nodes, unsigned int context)
    14621511{
    14631512  unsigned int hash;
     
    14831532  /* There are no appropriate state in `dfa', create the new one.  */
    14841533  new_state = create_cd_newstate (dfa, nodes, context, hash);
    1485   if (BE (new_state != NULL, 1))
    1486     return new_state;
    1487   else
    1488     {
    1489       *err = REG_ESPACE;
    1490       return NULL;
    1491     }
     1534  if (BE (new_state == NULL, 0))
     1535    *err = REG_ESPACE;
     1536
     1537  return new_state;
    14921538}
    14931539
     
    14971543
    14981544static reg_errcode_t
    1499 register_state (dfa, newstate, hash)
    1500      re_dfa_t *dfa;
    1501      re_dfastate_t *newstate;
    1502      unsigned int hash;
     1545register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
     1546                unsigned int hash)
    15031547{
    15041548  struct re_state_table_entry *spot;
     
    15321576}
    15331577
     1578static void
     1579free_state (re_dfastate_t *state)
     1580{
     1581  re_node_set_free (&state->non_eps_nodes);
     1582  re_node_set_free (&state->inveclosure);
     1583  if (state->entrance_nodes != &state->nodes)
     1584    {
     1585      re_node_set_free (state->entrance_nodes);
     1586      re_free (state->entrance_nodes);
     1587    }
     1588  re_node_set_free (&state->nodes);
     1589  re_free (state->word_trtable);
     1590  re_free (state->trtable);
     1591  re_free (state);
     1592}
     1593
    15341594/* Create the new state which is independ of contexts.
    15351595   Return the new state if succeeded, otherwise return NULL.  */
    15361596
    15371597static re_dfastate_t *
    1538 create_ci_newstate (dfa, nodes, hash)
    1539      re_dfa_t *dfa;
    1540      const re_node_set *nodes;
    1541      unsigned int hash;
     1598internal_function
     1599create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
     1600                    unsigned int hash)
    15421601{
    15431602  int i;
     
    15871646
    15881647static re_dfastate_t *
    1589 create_cd_newstate (dfa, nodes, context, hash)
    1590      re_dfa_t *dfa;
    1591      const re_node_set *nodes;
    1592      unsigned int context, hash;
     1648internal_function
     1649create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
     1650                    unsigned int context, unsigned int hash)
    15931651{
    15941652  int i, nctx_nodes = 0;
     
    16611719  return  newstate;
    16621720}
    1663 
    1664 static void
    1665 free_state (state)
    1666      re_dfastate_t *state;
    1667 {
    1668   re_node_set_free (&state->non_eps_nodes);
    1669   re_node_set_free (&state->inveclosure);
    1670   if (state->entrance_nodes != &state->nodes)
    1671     {
    1672       re_node_set_free (state->entrance_nodes);
    1673       re_free (state->entrance_nodes);
    1674     }
    1675   re_node_set_free (&state->nodes);
    1676   re_free (state->word_trtable);
    1677   re_free (state->trtable);
    1678   re_free (state);
    1679 }
  • branches/libc-0.6/src/emx/src/lib/lgpl/posix/regex_internal.h

    r2240 r3058  
    4040# include <wctype.h>
    4141#endif /* HAVE_WCTYPE_H || _LIBC */
     42#if defined HAVE_STDBOOL_H || defined _LIBC
     43# include <stdbool.h>
     44#endif /* HAVE_STDBOOL_H || _LIBC */
     45#if defined HAVE_STDINT_H || defined _LIBC
     46# include <stdint.h>
     47#endif /* HAVE_STDINT_H || _LIBC */
    4248#if defined _LIBC || defined __INNOTEK_LIBC__
    4349# include <bits/libc-lock.h>
     
    8187#endif
    8288
     89/* For loser systems without the definition.  */
     90#ifndef SIZE_MAX
     91# define SIZE_MAX ((size_t) -1)
     92#endif
     93
    8394#ifndef __INNOTEK_LIBC__
    8495#if (defined MB_CUR_MAX && HAVE_LOCALE_H && HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_WCRTOMB && HAVE_MBRTOWC && HAVE_WCSCOLL) || _LIBC
    8596# define RE_ENABLE_I18N
    8697#endif
    87 #endif
     98#else
     99#endif
     100#define RE_ENABLE_I18N
    88101
    89102#if __GNUC__ >= 3
     
    94107#endif
    95108
    96 /* Number of bits in a byte.  */
    97 #define BYTE_BITS 8
    98109/* Number of single byte character.  */
    99110#define SBC_MAX 256
     
    125136extern const size_t __re_error_msgid_idx[] attribute_hidden;
    126137
    127 /* Number of bits in an unsinged int.  */
    128 #define UINT_BITS (sizeof (unsigned int) * BYTE_BITS)
    129 /* Number of unsigned int in an bit_set.  */
    130 #define BITSET_UINTS ((SBC_MAX + UINT_BITS - 1) / UINT_BITS)
    131 typedef unsigned int bitset[BITSET_UINTS];
    132 typedef unsigned int *re_bitset_ptr_t;
    133 typedef const unsigned int *re_const_bitset_ptr_t;
    134 
    135 #define bitset_set(set,i) (set[i / UINT_BITS] |= 1 << i % UINT_BITS)
    136 #define bitset_clear(set,i) (set[i / UINT_BITS] &= ~(1 << i % UINT_BITS))
    137 #define bitset_contain(set,i) (set[i / UINT_BITS] & (1 << i % UINT_BITS))
    138 #define bitset_empty(set) memset (set, 0, sizeof (unsigned int) * BITSET_UINTS)
    139 #define bitset_set_all(set) \
    140   memset (set, 255, sizeof (unsigned int) * BITSET_UINTS)
    141 #define bitset_copy(dest,src) \
    142   memcpy (dest, src, sizeof (unsigned int) * BITSET_UINTS)
    143 static inline void bitset_not (bitset set);
    144 static inline void bitset_merge (bitset dest, const bitset src);
    145 static inline void bitset_not_merge (bitset dest, const bitset src);
    146 static inline void bitset_mask (bitset dest, const bitset src);
     138/* An integer used to represent a set of bits.  It must be unsigned,
     139   and must be at least as wide as unsigned int.  */
     140typedef unsigned long int bitset_word_t;
     141/* All bits set in a bitset_word_t.  */
     142#define BITSET_WORD_MAX ULONG_MAX
     143/* Number of bits in a bitset_word_t.  */
     144#define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
     145/* Number of bitset_word_t in a bit_set.  */
     146#define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
     147typedef bitset_word_t bitset_t[BITSET_WORDS];
     148typedef bitset_word_t *re_bitset_ptr_t;
     149typedef const bitset_word_t *re_const_bitset_ptr_t;
     150
     151#define bitset_set(set,i) \
     152  (set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
     153#define bitset_clear(set,i) \
     154  (set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
     155#define bitset_contain(set,i) \
     156  (set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
     157#define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
     158#define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
     159#define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
    147160
    148161#define PREV_WORD_CONSTRAINT 0x0001
     
    350363  unsigned int tip_context;
    351364  /* The translation passed as a part of an argument of re_compile_pattern.  */
    352   unsigned RE_TRANSLATE_TYPE trans;
     365  RE_TRANSLATE_TYPE trans;
    353366  /* Copy of re_dfa_t's word_char.  */
    354367  re_const_bitset_ptr_t word_char;
     
    377390#endif
    378391
    379 #ifndef RE_NO_INTERNAL_PROTOTYPES
    380 static reg_errcode_t re_string_allocate (re_string_t *pstr, const char *str,
    381                                          int len, int init_len,
    382                                          RE_TRANSLATE_TYPE trans, int icase,
    383                                          const re_dfa_t *dfa)
    384      internal_function;
    385 static reg_errcode_t re_string_construct (re_string_t *pstr, const char *str,
    386                                           int len, RE_TRANSLATE_TYPE trans,
    387                                           int icase, const re_dfa_t *dfa)
    388      internal_function;
    389 static reg_errcode_t re_string_reconstruct (re_string_t *pstr, int idx,
    390                                             int eflags) internal_function;
    391392static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
    392393                                                int new_buf_len)
    393394     internal_function;
    394 # ifdef RE_ENABLE_I18N
     395#ifdef RE_ENABLE_I18N
    395396static void build_wcs_buffer (re_string_t *pstr) internal_function;
    396397static int build_wcs_upper_buffer (re_string_t *pstr) internal_function;
    397 # endif /* RE_ENABLE_I18N */
     398#endif /* RE_ENABLE_I18N */
    398399static void build_upper_buffer (re_string_t *pstr) internal_function;
    399400static void re_string_translate_buffer (re_string_t *pstr) internal_function;
    400 static void re_string_destruct (re_string_t *pstr) internal_function;
    401 # ifdef RE_ENABLE_I18N
    402 static int re_string_elem_size_at (const re_string_t *pstr, int idx)
    403      internal_function __attribute ((pure));
    404 static inline int re_string_char_size_at (const re_string_t *pstr, int idx)
    405      internal_function __attribute ((pure));
    406 static inline wint_t re_string_wchar_at (const re_string_t *pstr, int idx)
    407      internal_function __attribute ((pure));
    408 # endif /* RE_ENABLE_I18N */
    409401static unsigned int re_string_context_at (const re_string_t *input, int idx,
    410402                                          int eflags)
    411403     internal_function __attribute ((pure));
    412 static unsigned char re_string_peek_byte_case (const re_string_t *pstr,
    413                                                int idx)
    414      internal_function __attribute ((pure));
    415 static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
    416      internal_function __attribute ((pure));
    417 #endif
    418404#define re_string_peek_byte(pstr, offset) \
    419405  ((pstr)->mbs[(pstr)->cur_idx + offset])
     
    433419#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
    434420
     421#include <alloca.h>
     422
     423#ifndef _LIBC
     424# if HAVE_ALLOCA
     425/* The OS usually guarantees only one guard page at the bottom of the stack,
     426   and a page size can be as small as 4096 bytes.  So we cannot safely
     427   allocate anything larger than 4096 bytes.  Also care for the possibility
     428   of a few compiler-allocated temporary stack slots.  */
     429#  define __libc_use_alloca(n) ((n) < 4032)
     430# else
     431/* alloca is implemented with malloc, so just use malloc.  */
     432#  define __libc_use_alloca(n) 0
     433# endif
     434#endif
     435
    435436#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
    436437#define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
     
    544545  int str_idx;
    545546  int node;
    546   int next_last_offset;
    547547  state_array_t *path;
    548548  int alasts; /* Allocation size of LASTS.  */
     
    567567  re_string_t input;
    568568#if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
    569   re_dfa_t *const dfa;
     569  const re_dfa_t *const dfa;
    570570#else
    571   re_dfa_t *dfa;
     571  const re_dfa_t *dfa;
    572572#endif
    573573  /* EFLAGS of the argument of regexec.  */
     
    616616{
    617617  re_token_t *nodes;
    618   int nodes_alloc;
    619   int nodes_len;
     618  size_t nodes_alloc;
     619  size_t nodes_len;
    620620  int *nexts;
    621621  int *org_indices;
     
    635635  /* number of subexpressions `re_nsub' is in regex_t.  */
    636636  unsigned int state_hash_mask;
    637   int states_alloc;
    638637  int init_node;
    639638  int nbackref; /* The number of backreference in this dfa.  */
    640639
    641640  /* Bitmap expressing which backreference is used.  */
    642   unsigned int used_bkref_map;
    643   unsigned int completed_bkref_map;
     641  bitset_word_t used_bkref_map;
     642  bitset_word_t completed_bkref_map;
    644643
    645644  unsigned int has_plural_match : 1;
     
    652651  unsigned int word_ops_used : 1;
    653652  int mb_cur_max;
    654   bitset word_char;
     653  bitset_t word_char;
    655654  reg_syntax_t syntax;
    656655  int *subexp_map;
     
    661660};
    662661
    663 #ifndef RE_NO_INTERNAL_PROTOTYPES
    664 static reg_errcode_t re_node_set_alloc (re_node_set *set, int size) internal_function;
    665 static reg_errcode_t re_node_set_init_1 (re_node_set *set, int elem) internal_function;
    666 static reg_errcode_t re_node_set_init_2 (re_node_set *set, int elem1,
    667                                          int elem2) internal_function;
    668662#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
    669 static reg_errcode_t re_node_set_init_copy (re_node_set *dest,
    670                                             const re_node_set *src) internal_function;
    671 static reg_errcode_t re_node_set_add_intersect (re_node_set *dest,
    672                                                 const re_node_set *src1,
    673                                                 const re_node_set *src2) internal_function;
    674 static reg_errcode_t re_node_set_init_union (re_node_set *dest,
    675                                              const re_node_set *src1,
    676                                              const re_node_set *src2) internal_function;
    677 static reg_errcode_t re_node_set_merge (re_node_set *dest,
    678                                         const re_node_set *src) internal_function;
    679 static int re_node_set_insert (re_node_set *set, int elem) internal_function;
    680 static int re_node_set_insert_last (re_node_set *set,
    681                                     int elem) internal_function;
    682 static int re_node_set_compare (const re_node_set *set1,
    683                                 const re_node_set *set2)
    684      internal_function __attribute ((pure));
    685 static int re_node_set_contains (const re_node_set *set, int elem)
    686      internal_function __attribute ((pure));
    687 static void re_node_set_remove_at (re_node_set *set, int idx) internal_function;
    688663#define re_node_set_remove(set,id) \
    689664  (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
    690665#define re_node_set_empty(p) ((p)->nelem = 0)
    691666#define re_node_set_free(set) re_free ((set)->elems)
    692 static int re_dfa_add_node (re_dfa_t *dfa, re_token_t token) internal_function;
    693 static re_dfastate_t *re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa,
    694                                         const re_node_set *nodes) internal_function;
    695 static re_dfastate_t *re_acquire_state_context (reg_errcode_t *err,
    696                                                 re_dfa_t *dfa,
    697                                                 const re_node_set *nodes,
    698                                                 unsigned int context) internal_function;
    699 static void free_state (re_dfastate_t *state) internal_function;
    700 #endif
    701667
    702668
     
    725691/* Inline functions for bitset operation.  */
    726692static inline void
    727 bitset_not (bitset set)
     693bitset_not (bitset_t set)
    728694{
    729695  int bitset_i;
    730   for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
     696  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
    731697    set[bitset_i] = ~set[bitset_i];
    732698}
    733699
    734700static inline void
    735 bitset_merge (bitset dest, const bitset src)
     701bitset_merge (bitset_t dest, const bitset_t src)
    736702{
    737703  int bitset_i;
    738   for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
     704  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
    739705    dest[bitset_i] |= src[bitset_i];
    740706}
    741707
    742708static inline void
    743 bitset_not_merge (bitset dest, const bitset src)
    744 {
    745   int i;
    746   for (i = 0; i < BITSET_UINTS; ++i)
    747     dest[i] |= ~src[i];
    748 }
    749 
    750 static inline void
    751 bitset_mask (bitset dest, const bitset src)
     709bitset_mask (bitset_t dest, const bitset_t src)
    752710{
    753711  int bitset_i;
    754   for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
     712  for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i)
    755713    dest[bitset_i] &= src[bitset_i];
    756714}
    757715
    758 #if defined RE_ENABLE_I18N && !defined RE_NO_INTERNAL_PROTOTYPES
     716#ifdef RE_ENABLE_I18N
    759717/* Inline functions for re_string.  */
    760718static inline int
    761 internal_function
     719internal_function __attribute ((pure))
    762720re_string_char_size_at (const re_string_t *pstr, int idx)
    763721{
     
    772730
    773731static inline wint_t
    774 internal_function
     732internal_function __attribute ((pure))
    775733re_string_wchar_at (const re_string_t *pstr, int idx)
    776734{
     
    781739
    782740static int
    783 internal_function
     741internal_function __attribute ((pure))
    784742re_string_elem_size_at (const re_string_t *pstr, int idx)
    785743{
    786 #ifdef _LIBC
     744# ifdef _LIBC
    787745  const unsigned char *p, *extra;
    788746  const int32_t *table, *indirect;
    789747  int32_t tmp;
    790 # include <locale/weight.h>
     748#  include <locale/weight.h>
    791749  uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
    792750
     
    803761    }
    804762  else
    805 #endif /* _LIBC */
     763# endif /* _LIBC */
    806764    return 1;
    807765}
  • branches/libc-0.6/src/emx/src/lib/lgpl/posix/regexec.c

    r2240 r3058  
    2626                                          int str_idx, int from, int to)
    2727     internal_function;
    28 static int search_cur_bkref_entry (re_match_context_t *mctx, int str_idx)
     28static int search_cur_bkref_entry (const re_match_context_t *mctx, int str_idx)
    2929     internal_function;
    3030static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, int node,
     
    5353static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch,
    5454                              int nregs, int regs_allocated) internal_function;
    55 static inline re_dfastate_t *acquire_init_state_context
    56      (reg_errcode_t *err, const re_match_context_t *mctx, int idx)
    57      __attribute ((always_inline)) internal_function;
    5855static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx)
    5956     internal_function;
    6057static int check_matching (re_match_context_t *mctx, int fl_longest_match,
    61                            int *p_match_first)
    62      internal_function;
    63 static int check_halt_node_context (const re_dfa_t *dfa, int node,
    64                                     unsigned int context) internal_function;
     58                           int *p_match_first) internal_function;
    6559static int check_halt_state_context (const re_match_context_t *mctx,
    6660                                     const re_dfastate_t *state, int idx)
    6761     internal_function;
    68 static void update_regs (re_dfa_t *dfa, regmatch_t *pmatch,
     62static void update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
    6963                         regmatch_t *prev_idx_match, int cur_node,
    7064                         int cur_idx, int nmatch) internal_function;
    71 static int proceed_next_node (const re_match_context_t *mctx,
    72                               int nregs, regmatch_t *regs,
    73                               int *pidx, int node, re_node_set *eps_via_nodes,
    74                               struct re_fail_stack_t *fs) internal_function;
    7565static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
    7666                                      int str_idx, int dest_node, int nregs,
    7767                                      regmatch_t *regs,
    78                                       re_node_set *eps_via_nodes) internal_function;
    79 static int pop_fail_stack (struct re_fail_stack_t *fs, int *pidx, int nregs,
    80                            regmatch_t *regs, re_node_set *eps_via_nodes) internal_function;
     68                                      re_node_set *eps_via_nodes)
     69     internal_function;
    8170static reg_errcode_t set_regs (const regex_t *preg,
    8271                               const re_match_context_t *mctx,
    8372                               size_t nmatch, regmatch_t *pmatch,
    8473                               int fl_backtrack) internal_function;
    85 static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs) internal_function;
     74static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs)
     75     internal_function;
    8676
    8777#ifdef RE_ENABLE_I18N
    8878static int sift_states_iter_mb (const re_match_context_t *mctx,
    8979                                re_sift_context_t *sctx,
    90                                 int node_idx, int str_idx, int max_str_idx) internal_function;
     80                                int node_idx, int str_idx, int max_str_idx)
     81     internal_function;
    9182#endif /* RE_ENABLE_I18N */
    92 static reg_errcode_t sift_states_backward (re_match_context_t *mctx,
    93                                            re_sift_context_t *sctx) internal_function;
    94 static reg_errcode_t build_sifted_states (re_match_context_t *mctx,
     83static reg_errcode_t sift_states_backward (const re_match_context_t *mctx,
     84                                           re_sift_context_t *sctx)
     85     internal_function;
     86static reg_errcode_t build_sifted_states (const re_match_context_t *mctx,
    9587                                          re_sift_context_t *sctx, int str_idx,
    96                                           re_node_set *cur_dest) internal_function;
    97 static reg_errcode_t update_cur_sifted_state (re_match_context_t *mctx,
     88                                          re_node_set *cur_dest)
     89     internal_function;
     90static reg_errcode_t update_cur_sifted_state (const re_match_context_t *mctx,
    9891                                              re_sift_context_t *sctx,
    9992                                              int str_idx,
    100                                               re_node_set *dest_nodes) internal_function;
    101 static reg_errcode_t add_epsilon_src_nodes (re_dfa_t *dfa,
     93                                              re_node_set *dest_nodes)
     94     internal_function;
     95static reg_errcode_t add_epsilon_src_nodes (const re_dfa_t *dfa,
    10296                                            re_node_set *dest_nodes,
    103                                             const re_node_set *candidates) internal_function;
    104 static reg_errcode_t sub_epsilon_src_nodes (re_dfa_t *dfa, int node,
    105                                             re_node_set *dest_nodes,
    106                                             const re_node_set *and_nodes) internal_function;
    107 static int check_dst_limits (re_match_context_t *mctx, re_node_set *limits,
     97                                            const re_node_set *candidates)
     98     internal_function;
     99static int check_dst_limits (const re_match_context_t *mctx,
     100                             re_node_set *limits,
    108101                             int dst_node, int dst_idx, int src_node,
    109102                             int src_idx) internal_function;
    110 static int check_dst_limits_calc_pos_1 (re_match_context_t *mctx,
     103static int check_dst_limits_calc_pos_1 (const re_match_context_t *mctx,
    111104                                        int boundaries, int subexp_idx,
    112                                         int from_node, int bkref_idx) internal_function;
    113 static int check_dst_limits_calc_pos (re_match_context_t *mctx,
     105                                        int from_node, int bkref_idx)
     106     internal_function;
     107static int check_dst_limits_calc_pos (const re_match_context_t *mctx,
    114108                                      int limit, int subexp_idx,
    115109                                      int node, int str_idx,
    116110                                      int bkref_idx) internal_function;
    117 static reg_errcode_t check_subexp_limits (re_dfa_t *dfa,
     111static reg_errcode_t check_subexp_limits (const re_dfa_t *dfa,
    118112                                          re_node_set *dest_nodes,
    119113                                          const re_node_set *candidates,
     
    121115                                          struct re_backref_cache_entry *bkref_ents,
    122116                                          int str_idx) internal_function;
    123 static reg_errcode_t sift_states_bkref (re_match_context_t *mctx,
     117static reg_errcode_t sift_states_bkref (const re_match_context_t *mctx,
    124118                                        re_sift_context_t *sctx,
    125                                         int str_idx, const re_node_set *candidates) internal_function;
    126 static reg_errcode_t clean_state_log_if_needed (re_match_context_t *mctx,
    127                                                 int next_state_log_idx) internal_function;
    128 static reg_errcode_t merge_state_array (re_dfa_t *dfa, re_dfastate_t **dst,
    129                                         re_dfastate_t **src, int num) internal_function;
     119                                        int str_idx, const re_node_set *candidates)
     120     internal_function;
     121static reg_errcode_t merge_state_array (const re_dfa_t *dfa,
     122                                        re_dfastate_t **dst,
     123                                        re_dfastate_t **src, int num)
     124     internal_function;
    130125static re_dfastate_t *find_recover_state (reg_errcode_t *err,
    131126                                         re_match_context_t *mctx) internal_function;
     
    135130static re_dfastate_t *merge_state_with_log (reg_errcode_t *err,
    136131                                            re_match_context_t *mctx,
    137                                             re_dfastate_t *next_state) internal_function;
     132                                            re_dfastate_t *next_state)
     133     internal_function;
    138134static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx,
    139135                                                re_node_set *cur_nodes,
     
    142138static re_dfastate_t *transit_state_sb (reg_errcode_t *err,
    143139                                        re_match_context_t *mctx,
    144                                         re_dfastate_t *pstate) internal_function;
     140                                        re_dfastate_t *pstate)
     141     internal_function;
    145142#endif
    146143#ifdef RE_ENABLE_I18N
    147144static reg_errcode_t transit_state_mb (re_match_context_t *mctx,
    148                                        re_dfastate_t *pstate) internal_function;
     145                                       re_dfastate_t *pstate)
     146     internal_function;
    149147#endif /* RE_ENABLE_I18N */
    150148static reg_errcode_t transit_state_bkref (re_match_context_t *mctx,
    151                                           const re_node_set *nodes) internal_function;
     149                                          const re_node_set *nodes)
     150     internal_function;
    152151static reg_errcode_t get_subexp (re_match_context_t *mctx,
    153                                  int bkref_node, int bkref_str_idx) internal_function;
     152                                 int bkref_node, int bkref_str_idx)
     153     internal_function;
    154154static reg_errcode_t get_subexp_sub (re_match_context_t *mctx,
    155155                                     const re_sub_match_top_t *sub_top,
    156156                                     re_sub_match_last_t *sub_last,
    157                                      int bkref_node, int bkref_str) internal_function;
     157                                     int bkref_node, int bkref_str)
     158     internal_function;
    158159static int find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
    159160                             int subexp_idx, int type) internal_function;
     
    165166                                                   int str_idx,
    166167                                                   re_node_set *cur_nodes,
    167                                                    re_node_set *next_nodes) internal_function;
    168 static reg_errcode_t check_arrival_expand_ecl (re_dfa_t *dfa,
     168                                                   re_node_set *next_nodes)
     169     internal_function;
     170static reg_errcode_t check_arrival_expand_ecl (const re_dfa_t *dfa,
    169171                                               re_node_set *cur_nodes,
    170                                                int ex_subexp, int type) internal_function;
    171 static reg_errcode_t check_arrival_expand_ecl_sub (re_dfa_t *dfa,
     172                                               int ex_subexp, int type)
     173     internal_function;
     174static reg_errcode_t check_arrival_expand_ecl_sub (const re_dfa_t *dfa,
    172175                                                   re_node_set *dst_nodes,
    173176                                                   int target, int ex_subexp,
     
    175178static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx,
    176179                                         re_node_set *cur_nodes, int cur_str,
    177                                          int subexp_num, int type) internal_function;
    178 static int build_trtable (re_dfa_t *dfa,
     180                                         int subexp_num, int type)
     181     internal_function;
     182static int build_trtable (const re_dfa_t *dfa,
    179183                          re_dfastate_t *state) internal_function;
    180184#ifdef RE_ENABLE_I18N
    181 static int check_node_accept_bytes (re_dfa_t *dfa, int node_idx,
    182                                     const re_string_t *input, int idx) internal_function;
     185static int check_node_accept_bytes (const re_dfa_t *dfa, int node_idx,
     186                                    const re_string_t *input, int idx)
     187     internal_function;
    183188# ifdef _LIBC
    184189static unsigned int find_collation_sequence_value (const unsigned char *mbs,
    185                                                    size_t name_len) internal_function;
     190                                                   size_t name_len)
     191     internal_function;
    186192# endif /* _LIBC */
    187193#endif /* RE_ENABLE_I18N */
    188 static int group_nodes_into_DFAstates (re_dfa_t *dfa,
     194static int group_nodes_into_DFAstates (const re_dfa_t *dfa,
    189195                                       const re_dfastate_t *state,
    190196                                       re_node_set *states_node,
    191                                        bitset *states_ch) internal_function;
     197                                       bitset_t *states_ch) internal_function;
    192198static int check_node_accept (const re_match_context_t *mctx,
    193                               const re_token_t *node, int idx) internal_function;
    194 static reg_errcode_t extend_buffers (re_match_context_t *mctx) internal_function;
     199                              const re_token_t *node, int idx)
     200     internal_function;
     201static reg_errcode_t extend_buffers (re_match_context_t *mctx)
     202     internal_function;
    195203
    196204
     
    221229  reg_errcode_t err;
    222230  int start, length;
    223   re_dfa_t *dfa = (re_dfa_t *)preg->buffer;
     231  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
    224232
    225233  if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND))
     
    375383        if (BE (s == NULL, 0))
    376384          return -2;
     385#ifdef _LIBC
     386        memcpy (__mempcpy (s, string1, length1), string2, length2);
     387#else
    377388        memcpy (s, string1, length1);
    378389        memcpy (s + length1, string2, length2);
     390#endif
    379391        str = s;
    380392        free_str = 1;
     
    408420  int nregs, rval;
    409421  int eflags = 0;
    410   re_dfa_t *dfa = (re_dfa_t *)bufp->buffer;
     422  re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
    411423
    412424  /* Check for out-of-range.  */
     
    618630{
    619631  reg_errcode_t err;
    620   re_dfa_t *dfa = (re_dfa_t *)preg->buffer;
     632  const re_dfa_t *dfa = (const re_dfa_t *) preg->buffer;
    621633  int left_lim, right_lim, incr;
    622634  int fl_longest_match, match_first, match_kind, match_last = -1;
     
    630642  char *fastmap = (preg->fastmap != NULL && preg->fastmap_accurate
    631643                   && range && !preg->can_be_null) ? preg->fastmap : NULL;
    632   unsigned RE_TRANSLATE_TYPE t = (unsigned RE_TRANSLATE_TYPE) preg->translate;
     644  RE_TRANSLATE_TYPE t = preg->translate;
    633645
    634646#if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L))
     
    887899            if (BE (mctx.input.offsets_needed != 0, 0))
    888900              {
    889                 if (pmatch[reg_idx].rm_so == mctx.input.valid_len)
    890                   pmatch[reg_idx].rm_so += mctx.input.valid_raw_len - mctx.input.valid_len;
    891                 else
    892                   pmatch[reg_idx].rm_so = mctx.input.offsets[pmatch[reg_idx].rm_so];
    893                 if (pmatch[reg_idx].rm_eo == mctx.input.valid_len)
    894                   pmatch[reg_idx].rm_eo += mctx.input.valid_raw_len - mctx.input.valid_len;
    895                 else
    896                   pmatch[reg_idx].rm_eo = mctx.input.offsets[pmatch[reg_idx].rm_eo];
     901                pmatch[reg_idx].rm_so =
     902                  (pmatch[reg_idx].rm_so == mctx.input.valid_len
     903                   ? mctx.input.valid_raw_len
     904                   : mctx.input.offsets[pmatch[reg_idx].rm_so]);
     905                pmatch[reg_idx].rm_eo =
     906                  (pmatch[reg_idx].rm_eo == mctx.input.valid_len
     907                   ? mctx.input.valid_raw_len
     908                   : mctx.input.offsets[pmatch[reg_idx].rm_eo]);
    897909              }
    898910#else
     
    931943     re_match_context_t *mctx;
    932944{
    933   re_dfa_t *const dfa = mctx->dfa;
     945  const re_dfa_t *const dfa = mctx->dfa;
    934946  int halt_node, match_last;
    935947  reg_errcode_t ret;
     
    10141026
    10151027static inline re_dfastate_t *
    1016 acquire_init_state_context (err, mctx, idx)
    1017      reg_errcode_t *err;
    1018      const re_match_context_t *mctx;
    1019      int idx;
    1020 {
    1021   re_dfa_t *const dfa = mctx->dfa;
     1028__attribute ((always_inline)) internal_function
     1029acquire_init_state_context (reg_errcode_t *err, const re_match_context_t *mctx,
     1030                            int idx)
     1031{
     1032  const re_dfa_t *const dfa = mctx->dfa;
    10221033  if (dfa->init_state->has_constraint)
    10231034    {
     
    10571068
    10581069static int
    1059 check_matching (mctx, fl_longest_match, p_match_first)
    1060     re_match_context_t *mctx;
    1061     int fl_longest_match;
    1062     int *p_match_first;
    1063 {
    1064   re_dfa_t *const dfa = mctx->dfa;
     1070internal_function
     1071check_matching (re_match_context_t *mctx, int fl_longest_match,
     1072                int *p_match_first)
     1073{
     1074  const re_dfa_t *const dfa = mctx->dfa;
    10651075  reg_errcode_t err;
    10661076  int match = 0;
     
    11891199/* Check NODE match the current context.  */
    11901200
    1191 static int check_halt_node_context (dfa, node, context)
    1192     const re_dfa_t *dfa;
    1193     int node;
    1194     unsigned int context;
     1201static int
     1202internal_function
     1203check_halt_node_context (const re_dfa_t *dfa, int node, unsigned int context)
    11951204{
    11961205  re_token_type_t type = dfa->nodes[node].type;
     
    12101219
    12111220static int
    1212 check_halt_state_context (mctx, state, idx)
    1213     const re_match_context_t *mctx;
    1214     const re_dfastate_t *state;
    1215     int idx;
     1221internal_function
     1222check_halt_state_context (const re_match_context_t *mctx,
     1223                          const re_dfastate_t *state, int idx)
    12161224{
    12171225  int i;
     
    12331241
    12341242static int
    1235 proceed_next_node (mctx, nregs, regs, pidx, node, eps_via_nodes, fs)
    1236     const re_match_context_t *mctx;
    1237     regmatch_t *regs;
    1238     int nregs, *pidx, node;
    1239     re_node_set *eps_via_nodes;
    1240     struct re_fail_stack_t *fs;
    1241 {
    1242   re_dfa_t *const dfa = mctx->dfa;
    1243   int i, err, dest_node;
    1244   dest_node = -1;
     1243internal_function
     1244proceed_next_node (const re_match_context_t *mctx, int nregs, regmatch_t *regs,
     1245                   int *pidx, int node, re_node_set *eps_via_nodes,
     1246                   struct re_fail_stack_t *fs)
     1247{
     1248  const re_dfa_t *const dfa = mctx->dfa;
     1249  int i, err;
    12451250  if (IS_EPSILON_NODE (dfa->nodes[node].type))
    12461251    {
     
    13081313          if (naccepted == 0)
    13091314            {
     1315              int dest_node;
    13101316              err = re_node_set_insert (eps_via_nodes, node);
    13111317              if (BE (err < 0, 0))
     
    13211327          || check_node_accept (mctx, dfa->nodes + node, *pidx))
    13221328        {
    1323           dest_node = dfa->nexts[node];
     1329          int dest_node = dfa->nexts[node];
    13241330          *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted;
    13251331          if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL
     
    13351341
    13361342static reg_errcode_t
    1337 push_fail_stack (fs, str_idx, dest_node, nregs, regs, eps_via_nodes)
    1338      struct re_fail_stack_t *fs;
    1339      int str_idx, dest_node, nregs;
    1340      regmatch_t *regs;
    1341      re_node_set *eps_via_nodes;
     1343internal_function
     1344push_fail_stack (struct re_fail_stack_t *fs, int str_idx, int dest_node,
     1345                 int nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
    13421346{
    13431347  reg_errcode_t err;
     
    13641368
    13651369static int
    1366 pop_fail_stack (fs, pidx, nregs, regs, eps_via_nodes)
    1367      struct re_fail_stack_t *fs;
    1368      int *pidx, nregs;
    1369      regmatch_t *regs;
    1370      re_node_set *eps_via_nodes;
     1370internal_function
     1371pop_fail_stack (struct re_fail_stack_t *fs, int *pidx, int nregs,
     1372                regmatch_t *regs, re_node_set *eps_via_nodes)
    13711373{
    13721374  int num = --fs->num;
     
    13861388
    13871389static reg_errcode_t
    1388 set_regs (preg, mctx, nmatch, pmatch, fl_backtrack)
    1389      const regex_t *preg;
    1390      const re_match_context_t *mctx;
    1391      size_t nmatch;
    1392      regmatch_t *pmatch;
    1393      int fl_backtrack;
    1394 {
    1395   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     1390internal_function
     1391set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
     1392          regmatch_t *pmatch, int fl_backtrack)
     1393{
     1394  const re_dfa_t *dfa = (const re_dfa_t *) preg->buffer;
    13961395  int idx, cur_node;
    13971396  re_node_set eps_via_nodes;
     
    13991398  struct re_fail_stack_t fs_body = { 0, 2, NULL };
    14001399  regmatch_t *prev_idx_match;
     1400  int prev_idx_match_malloced = 0;
    14011401
    14021402#ifdef DEBUG
     
    14171417  re_node_set_init_empty (&eps_via_nodes);
    14181418
    1419   prev_idx_match = (regmatch_t *) alloca (sizeof (regmatch_t) * nmatch);
     1419  if (__libc_use_alloca (nmatch * sizeof (regmatch_t)))
     1420    prev_idx_match = (regmatch_t *) alloca (nmatch * sizeof (regmatch_t));
     1421  else
     1422    {
     1423      prev_idx_match = re_malloc (regmatch_t, nmatch);
     1424      if (prev_idx_match == NULL)
     1425        {
     1426          free_fail_stack_return (fs);
     1427          return REG_ESPACE;
     1428        }
     1429      prev_idx_match_malloced = 1;
     1430    }
    14201431  memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
    14211432
     
    14351446                {
    14361447                  re_node_set_free (&eps_via_nodes);
     1448                  if (prev_idx_match_malloced)
     1449                    re_free (prev_idx_match);
    14371450                  return free_fail_stack_return (fs);
    14381451                }
     
    14431456            {
    14441457              re_node_set_free (&eps_via_nodes);
     1458              if (prev_idx_match_malloced)
     1459                re_free (prev_idx_match);
    14451460              return REG_NOERROR;
    14461461            }
     
    14561471            {
    14571472              re_node_set_free (&eps_via_nodes);
     1473              if (prev_idx_match_malloced)
     1474                re_free (prev_idx_match);
    14581475              free_fail_stack_return (fs);
    14591476              return REG_ESPACE;
     
    14651482            {
    14661483              re_node_set_free (&eps_via_nodes);
     1484              if (prev_idx_match_malloced)
     1485                re_free (prev_idx_match);
    14671486              return REG_NOMATCH;
    14681487            }
     
    14701489    }
    14711490  re_node_set_free (&eps_via_nodes);
     1491  if (prev_idx_match_malloced)
     1492    re_free (prev_idx_match);
    14721493  return free_fail_stack_return (fs);
    14731494}
    14741495
    14751496static reg_errcode_t
    1476 free_fail_stack_return (fs)
    1477      struct re_fail_stack_t *fs;
     1497internal_function
     1498free_fail_stack_return (struct re_fail_stack_t *fs)
    14781499{
    14791500  if (fs)
     
    14911512
    14921513static void
    1493 update_regs (dfa, pmatch, prev_idx_match, cur_node, cur_idx, nmatch)
    1494      re_dfa_t *dfa;
    1495      regmatch_t *pmatch, *prev_idx_match;
    1496      int cur_node, cur_idx, nmatch;
     1514internal_function
     1515update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
     1516             regmatch_t *prev_idx_match, int cur_node, int cur_idx, int nmatch)
    14971517{
    14981518  int type = dfa->nodes[cur_node].type;
     
    15641584
    15651585static reg_errcode_t
    1566 sift_states_backward (mctx, sctx)
    1567      re_match_context_t *mctx;
    1568      re_sift_context_t *sctx;
     1586internal_function
     1587sift_states_backward (const re_match_context_t *mctx, re_sift_context_t *sctx)
    15691588{
    15701589  reg_errcode_t err;
     
    16231642
    16241643static reg_errcode_t
    1625 build_sifted_states (mctx, sctx, str_idx, cur_dest)
    1626      re_match_context_t *mctx;
    1627      re_sift_context_t *sctx;
    1628      int str_idx;
    1629      re_node_set *cur_dest;
    1630 {
    1631   re_dfa_t *const dfa = mctx->dfa;
    1632   re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
     1644internal_function
     1645build_sifted_states (const re_match_context_t *mctx, re_sift_context_t *sctx,
     1646                     int str_idx, re_node_set *cur_dest)
     1647{
     1648  const re_dfa_t *const dfa = mctx->dfa;
     1649  const re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
    16331650  int i;
    16341651
     
    16871704
    16881705static reg_errcode_t
    1689 clean_state_log_if_needed (mctx, next_state_log_idx)
    1690     re_match_context_t *mctx;
    1691     int next_state_log_idx;
     1706internal_function
     1707clean_state_log_if_needed (re_match_context_t *mctx, int next_state_log_idx)
    16921708{
    16931709  int top = mctx->state_log_top;
     
    17131729
    17141730static reg_errcode_t
    1715 merge_state_array (dfa, dst, src, num)
    1716      re_dfa_t *dfa;
    1717      re_dfastate_t **dst;
    1718      re_dfastate_t **src;
    1719      int num;
     1731internal_function
     1732merge_state_array (const re_dfa_t *dfa, re_dfastate_t **dst,
     1733                   re_dfastate_t **src, int num)
    17201734{
    17211735  int st_idx;
     
    17421756
    17431757static reg_errcode_t
    1744 update_cur_sifted_state (mctx, sctx, str_idx, dest_nodes)
    1745      re_match_context_t *mctx;
    1746      re_sift_context_t *sctx;
    1747      int str_idx;
    1748      re_node_set *dest_nodes;
    1749 {
    1750   re_dfa_t *const dfa = mctx->dfa;
    1751   reg_errcode_t err;
     1758internal_function
     1759update_cur_sifted_state (const re_match_context_t *mctx,
     1760                         re_sift_context_t *sctx, int str_idx,
     1761                         re_node_set *dest_nodes)
     1762{
     1763  const re_dfa_t *const dfa = mctx->dfa;
     1764  reg_errcode_t err = REG_NOERROR;
    17521765  const re_node_set *candidates;
    17531766  candidates = ((mctx->state_log[str_idx] == NULL) ? NULL
     
    17911804
    17921805static reg_errcode_t
    1793 add_epsilon_src_nodes (dfa, dest_nodes, candidates)
    1794      re_dfa_t *dfa;
    1795      re_node_set *dest_nodes;
    1796      const re_node_set *candidates;
     1806internal_function
     1807add_epsilon_src_nodes (const re_dfa_t *dfa, re_node_set *dest_nodes,
     1808                       const re_node_set *candidates)
    17971809{
    17981810  reg_errcode_t err = REG_NOERROR;
     
    18171829
    18181830static reg_errcode_t
    1819 sub_epsilon_src_nodes (dfa, node, dest_nodes, candidates)
    1820      re_dfa_t *dfa;
    1821      int node;
    1822      re_node_set *dest_nodes;
    1823      const re_node_set *candidates;
     1831internal_function
     1832sub_epsilon_src_nodes (const re_dfa_t *dfa, int node, re_node_set *dest_nodes,
     1833                       const re_node_set *candidates)
    18241834{
    18251835    int ecl_idx;
     
    18681878
    18691879static int
    1870 check_dst_limits (mctx, limits, dst_node, dst_idx, src_node, src_idx)
    1871      re_match_context_t *mctx;
    1872      re_node_set *limits;
    1873      int dst_node, dst_idx, src_node, src_idx;
    1874 {
    1875   re_dfa_t *const dfa = mctx->dfa;
     1880internal_function
     1881check_dst_limits (const re_match_context_t *mctx, re_node_set *limits,
     1882                  int dst_node, int dst_idx, int src_node, int src_idx)
     1883{
     1884  const re_dfa_t *const dfa = mctx->dfa;
    18761885  int lim_idx, src_pos, dst_pos;
    18771886
     
    19051914
    19061915static int
    1907 check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx, from_node, bkref_idx)
    1908      re_match_context_t *mctx;
    1909      int boundaries, subexp_idx, from_node, bkref_idx;
    1910 {
    1911   re_dfa_t *const dfa = mctx->dfa;
    1912   re_node_set *eclosures = dfa->eclosures + from_node;
     1916internal_function
     1917check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, int boundaries,
     1918                             int subexp_idx, int from_node, int bkref_idx)
     1919{
     1920  const re_dfa_t *const dfa = mctx->dfa;
     1921  const re_node_set *eclosures = dfa->eclosures + from_node;
    19131922  int node_idx;
    19141923
     
    19311940                    continue;
    19321941
    1933                   if (subexp_idx <= 8 * sizeof (ent->eps_reachable_subexps_map)
    1934                       && !(ent->eps_reachable_subexps_map & (1 << subexp_idx)))
     1942                  if (subexp_idx < BITSET_WORD_BITS
     1943                      && !(ent->eps_reachable_subexps_map
     1944                           & ((bitset_word_t) 1 << subexp_idx)))
    19351945                    continue;
    19361946
     
    19581968                    return 0;
    19591969
    1960                   ent->eps_reachable_subexps_map &= ~(1 << subexp_idx);
     1970                  if (subexp_idx < BITSET_WORD_BITS)
     1971                    ent->eps_reachable_subexps_map
     1972                      &= ~((bitset_word_t) 1 << subexp_idx);
    19611973                }
    19621974              while (ent++->more);
     
    19831995
    19841996static int
    1985 check_dst_limits_calc_pos (mctx, limit, subexp_idx, from_node, str_idx, bkref_idx)
    1986      re_match_context_t *mctx;
    1987      int limit, subexp_idx, from_node, str_idx, bkref_idx;
     1997internal_function
     1998check_dst_limits_calc_pos (const re_match_context_t *mctx, int limit,
     1999                           int subexp_idx, int from_node, int str_idx,
     2000                           int bkref_idx)
    19882001{
    19892002  struct re_backref_cache_entry *lim = mctx->bkref_ents + limit;
     
    20122025
    20132026static reg_errcode_t
    2014 check_subexp_limits (dfa, dest_nodes, candidates, limits, bkref_ents, str_idx)
    2015      re_dfa_t *dfa;
    2016      re_node_set *dest_nodes;
    2017      const re_node_set *candidates;
    2018      re_node_set *limits;
    2019      struct re_backref_cache_entry *bkref_ents;
    2020      int str_idx;
     2027internal_function
     2028check_subexp_limits (const re_dfa_t *dfa, re_node_set *dest_nodes,
     2029                     const re_node_set *candidates, re_node_set *limits,
     2030                     struct re_backref_cache_entry *bkref_ents, int str_idx)
    20212031{
    20222032  reg_errcode_t err;
     
    21032113
    21042114static reg_errcode_t
    2105 sift_states_bkref (mctx, sctx, str_idx, candidates)
    2106      re_match_context_t *mctx;
    2107      re_sift_context_t *sctx;
    2108      int str_idx;
    2109      const re_node_set *candidates;
    2110 {
    2111   re_dfa_t *const dfa = mctx->dfa;
     2115internal_function
     2116sift_states_bkref (const re_match_context_t *mctx, re_sift_context_t *sctx,
     2117                   int str_idx, const re_node_set *candidates)
     2118{
     2119  const re_dfa_t *const dfa = mctx->dfa;
    21122120  reg_errcode_t err;
    21132121  int node_idx, node;
     
    21372145      do
    21382146        {
    2139           int subexp_len, to_idx, dst_node;
     2147          int subexp_len;
     2148          int to_idx;
     2149          int dst_node;
     2150          int ret;
    21402151          re_dfastate_t *cur_state;
    21412152
     
    21632174          local_sctx.last_node = node;
    21642175          local_sctx.last_str_idx = str_idx;
    2165           err = re_node_set_insert (&local_sctx.limits, enabled_idx);
    2166           if (BE (err < 0, 0))
     2176          ret = re_node_set_insert (&local_sctx.limits, enabled_idx);
     2177          if (BE (ret < 0, 0))
    21672178            {
    21682179              err = REG_ESPACE;
     
    22022213#ifdef RE_ENABLE_I18N
    22032214static int
    2204 sift_states_iter_mb (mctx, sctx, node_idx, str_idx, max_str_idx)
    2205     const re_match_context_t *mctx;
    2206     re_sift_context_t *sctx;
    2207     int node_idx, str_idx, max_str_idx;
    2208 {
    2209   re_dfa_t *const dfa = mctx->dfa;
     2215internal_function
     2216sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx,
     2217                     int node_idx, int str_idx, int max_str_idx)
     2218{
     2219  const re_dfa_t *const dfa = mctx->dfa;
    22102220  int naccepted;
    22112221  /* Check the node can accept `multi byte'.  */
     
    22342244
    22352245static re_dfastate_t *
    2236 transit_state (err, mctx, state)
    2237      reg_errcode_t *err;
    2238      re_match_context_t *mctx;
    2239      re_dfastate_t *state;
     2246internal_function
     2247transit_state (reg_errcode_t *err, re_match_context_t *mctx,
     2248               re_dfastate_t *state)
    22402249{
    22412250  re_dfastate_t **trtable;
     
    22922301
    22932302/* Update the state_log if we need */
    2294 static re_dfastate_t *
    2295 merge_state_with_log (err, mctx, next_state)
    2296      reg_errcode_t *err;
    2297      re_match_context_t *mctx;
    2298      re_dfastate_t *next_state;
    2299 {
    2300   re_dfa_t *const dfa = mctx->dfa;
     2303re_dfastate_t *
     2304internal_function
     2305merge_state_with_log (reg_errcode_t *err, re_match_context_t *mctx,
     2306                      re_dfastate_t *next_state)
     2307{
     2308  const re_dfa_t *const dfa = mctx->dfa;
    23012309  int cur_idx = re_string_cur_idx (&mctx->input);
    23022310
     
    23732381   from which to restart matching.  */
    23742382re_dfastate_t *
    2375 find_recover_state (err, mctx)
    2376      reg_errcode_t *err;
    2377      re_match_context_t *mctx;
    2378 {
    2379   re_dfastate_t *cur_state = NULL;
     2383internal_function
     2384find_recover_state (reg_errcode_t *err, re_match_context_t *mctx)
     2385{
     2386  re_dfastate_t *cur_state;
    23802387  do
    23812388    {
     
    23932400      cur_state = merge_state_with_log (err, mctx, NULL);
    23942401    }
    2395   while (err == REG_NOERROR && cur_state == NULL);
     2402  while (*err == REG_NOERROR && cur_state == NULL);
    23962403  return cur_state;
    23972404}
     
    24052412
    24062413static reg_errcode_t
    2407 check_subexp_matching_top (mctx, cur_nodes, str_idx)
    2408      re_match_context_t *mctx;
    2409      re_node_set *cur_nodes;
    2410      int str_idx;
    2411 {
    2412   re_dfa_t *const dfa = mctx->dfa;
     2414internal_function
     2415check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes,
     2416                           int str_idx)
     2417{
     2418  const re_dfa_t *const dfa = mctx->dfa;
    24132419  int node_idx;
    24142420  reg_errcode_t err;
     
    24232429      int node = cur_nodes->elems[node_idx];
    24242430      if (dfa->nodes[node].type == OP_OPEN_SUBEXP
    2425           && dfa->nodes[node].opr.idx < (8 * sizeof (dfa->used_bkref_map))
    2426           && dfa->used_bkref_map & (1 << dfa->nodes[node].opr.idx))
     2431          && dfa->nodes[node].opr.idx < BITSET_WORD_BITS
     2432          && (dfa->used_bkref_map
     2433              & ((bitset_word_t) 1 << dfa->nodes[node].opr.idx)))
    24272434        {
    24282435          err = match_ctx_add_subtop (mctx, node, str_idx);
     
    24392446
    24402447static re_dfastate_t *
    2441 transit_state_sb (err, mctx, state)
    2442      reg_errcode_t *err;
    2443      re_match_context_t *mctx;
    2444      re_dfastate_t *state;
    2445 {
    2446   re_dfa_t *const dfa = mctx->dfa;
     2448transit_state_sb (reg_errcode_t *err, re_match_context_t *mctx,
     2449                  re_dfastate_t *state)
     2450{
     2451  const re_dfa_t *const dfa = mctx->dfa;
    24472452  re_node_set next_nodes;
    24482453  re_dfastate_t *next_state;
     
    24802485#ifdef RE_ENABLE_I18N
    24812486static reg_errcode_t
    2482 transit_state_mb (mctx, pstate)
    2483     re_match_context_t *mctx;
    2484     re_dfastate_t *pstate;
    2485 {
    2486   re_dfa_t *const dfa = mctx->dfa;
     2487internal_function
     2488transit_state_mb (re_match_context_t *mctx, re_dfastate_t *pstate)
     2489{
     2490  const re_dfa_t *const dfa = mctx->dfa;
    24872491  reg_errcode_t err;
    24882492  int i;
     
    25372541            return err;
    25382542        }
    2539       context = re_string_context_at (&mctx->input, dest_idx - 1, mctx->eflags);
     2543      context = re_string_context_at (&mctx->input, dest_idx - 1,
     2544                                      mctx->eflags);
    25402545      mctx->state_log[dest_idx]
    25412546        = re_acquire_state_context (&err, dfa, &dest_nodes, context);
     
    25502555
    25512556static reg_errcode_t
    2552 transit_state_bkref (mctx, nodes)
    2553     re_match_context_t *mctx;
    2554     const re_node_set *nodes;
    2555 {
    2556   re_dfa_t *const dfa = mctx->dfa;
     2557internal_function
     2558transit_state_bkref (re_match_context_t *mctx, const re_node_set *nodes)
     2559{
     2560  const re_dfa_t *const dfa = mctx->dfa;
    25572561  reg_errcode_t err;
    25582562  int i;
     
    26652669
    26662670static reg_errcode_t
    2667 get_subexp (mctx, bkref_node, bkref_str_idx)
    2668      re_match_context_t *mctx;
    2669      int bkref_node, bkref_str_idx;
    2670 {
    2671   re_dfa_t *const dfa = mctx->dfa;
     2671internal_function
     2672get_subexp (re_match_context_t *mctx, int bkref_node, int bkref_str_idx)
     2673{
     2674  const re_dfa_t *const dfa = mctx->dfa;
    26722675  int subexp_num, sub_top_idx;
    26732676  const char *buf = (const char *) re_string_get_buffer (&mctx->input);
     
    26762679  if (cache_idx != -1)
    26772680    {
    2678       const struct re_backref_cache_entry *entry = mctx->bkref_ents + cache_idx;
     2681      const struct re_backref_cache_entry *entry
     2682        = mctx->bkref_ents + cache_idx;
    26792683      do
    26802684        if (entry->node == bkref_node)
     
    27232727                }
    27242728              if (memcmp (buf + bkref_str_off, buf + sl_str, sl_str_diff) != 0)
    2725                 break; /* We don't need to search this sub expression any more.  */
     2729                /* We don't need to search this sub expression any more.  */
     2730                break;
    27262731            }
    27272732          bkref_str_off += sl_str_diff;
     
    27742779          /* Does this state have a ')' of the sub expression?  */
    27752780          nodes = &mctx->state_log[sl_str]->nodes;
    2776           cls_node = find_subexp_node (dfa, nodes, subexp_num, OP_CLOSE_SUBEXP);
     2781          cls_node = find_subexp_node (dfa, nodes, subexp_num,
     2782                                       OP_CLOSE_SUBEXP);
    27772783          if (cls_node == -1)
    27782784            continue; /* No.  */
     
    27872793             in the current context?  */
    27882794          err = check_arrival (mctx, sub_top->path, sub_top->node,
    2789                                sub_top->str_idx, cls_node, sl_str, OP_CLOSE_SUBEXP);
     2795                               sub_top->str_idx, cls_node, sl_str,
     2796                               OP_CLOSE_SUBEXP);
    27902797          if (err == REG_NOMATCH)
    27912798              continue;
     
    28112818
    28122819static reg_errcode_t
    2813 get_subexp_sub (mctx, sub_top, sub_last, bkref_node, bkref_str)
    2814      re_match_context_t *mctx;
    2815      const re_sub_match_top_t *sub_top;
    2816      re_sub_match_last_t *sub_last;
    2817      int bkref_node, bkref_str;
     2820internal_function
     2821get_subexp_sub (re_match_context_t *mctx, const re_sub_match_top_t *sub_top,
     2822                re_sub_match_last_t *sub_last, int bkref_node, int bkref_str)
    28182823{
    28192824  reg_errcode_t err;
     
    28212826  /* Can the subexpression arrive the back reference?  */
    28222827  err = check_arrival (mctx, &sub_last->path, sub_last->node,
    2823                        sub_last->str_idx, bkref_node, bkref_str, OP_OPEN_SUBEXP);
     2828                       sub_last->str_idx, bkref_node, bkref_str,
     2829                       OP_OPEN_SUBEXP);
    28242830  if (err != REG_NOERROR)
    28252831    return err;
     
    28412847
    28422848static int
    2843 find_subexp_node (dfa, nodes, subexp_idx, type)
    2844      const re_dfa_t *dfa;
    2845      const re_node_set *nodes;
    2846      int subexp_idx, type;
     2849internal_function
     2850find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
     2851                  int subexp_idx, int type)
    28472852{
    28482853  int cls_idx;
     
    28642869
    28652870static reg_errcode_t
    2866 check_arrival (mctx, path, top_node, top_str, last_node, last_str,
    2867                type)
    2868      re_match_context_t *mctx;
    2869      state_array_t *path;
    2870      int top_node, top_str, last_node, last_str, type;
    2871 {
    2872   re_dfa_t *const dfa = mctx->dfa;
    2873   reg_errcode_t err;
     2871internal_function
     2872check_arrival (re_match_context_t *mctx, state_array_t *path, int top_node,
     2873               int top_str, int last_node, int last_str, int type)
     2874{
     2875  const re_dfa_t *const dfa = mctx->dfa;
     2876  reg_errcode_t err = REG_NOERROR;
    28742877  int subexp_num, backup_cur_idx, str_idx, null_cnt;
    28752878  re_dfastate_t *cur_state = NULL;
     
    28862889      path->alloc += last_str + mctx->max_mb_elem_len + 1;
    28872890      new_array = re_realloc (path->array, re_dfastate_t *, path->alloc);
    2888       if (new_array == NULL)
     2891      if (BE (new_array == NULL, 0))
    28892892        {
    28902893          path->alloc = old_alloc;
     
    28962899    }
    28972900
    2898   str_idx = path->next_idx == 0 ? top_str : path->next_idx;
     2901  str_idx = path->next_idx ?: top_str;
    28992902
    29002903  /* Temporary modify MCTX.  */
     
    29242927        {
    29252928          err = re_node_set_init_copy (&next_nodes, &cur_state->nodes);
    2926           if (BE ( err != REG_NOERROR, 0))
     2929          if (BE (err != REG_NOERROR, 0))
    29272930            return err;
    29282931        }
     
    29362939          err = expand_bkref_cache (mctx, &next_nodes, str_idx,
    29372940                                    subexp_num, type);
    2938           if (BE ( err != REG_NOERROR, 0))
     2941          if (BE (err != REG_NOERROR, 0))
    29392942            {
    29402943              re_node_set_free (&next_nodes);
     
    29672970        {
    29682971          err = check_arrival_add_next_nodes (mctx, str_idx,
    2969                                               &cur_state->non_eps_nodes, &next_nodes);
     2972                                              &cur_state->non_eps_nodes,
     2973                                              &next_nodes);
    29702974          if (BE (err != REG_NOERROR, 0))
    29712975            {
     
    29852989          err = expand_bkref_cache (mctx, &next_nodes, str_idx,
    29862990                                    subexp_num, type);
    2987           if (BE ( err != REG_NOERROR, 0))
     2991          if (BE (err != REG_NOERROR, 0))
    29882992            {
    29892993              re_node_set_free (&next_nodes);
     
    30263030
    30273031static reg_errcode_t
    3028 check_arrival_add_next_nodes (mctx, str_idx, cur_nodes, next_nodes)
    3029      re_match_context_t *mctx;
    3030      int str_idx;
    3031      re_node_set *cur_nodes, *next_nodes;
    3032 {
    3033   re_dfa_t *const dfa = mctx->dfa;
     3032internal_function
     3033check_arrival_add_next_nodes (re_match_context_t *mctx, int str_idx,
     3034                              re_node_set *cur_nodes, re_node_set *next_nodes)
     3035{
     3036  const re_dfa_t *const dfa = mctx->dfa;
    30343037  int result;
    30353038  int cur_idx;
    3036 #ifdef RE_ENABLE_I18N
    3037   reg_errcode_t err;
    3038 #endif
     3039  reg_errcode_t err = REG_NOERROR;
    30393040  re_node_set union_set;
    30403041  re_node_set_init_empty (&union_set);
     
    31083109
    31093110static reg_errcode_t
    3110 check_arrival_expand_ecl (dfa, cur_nodes, ex_subexp, type)
    3111      re_dfa_t *dfa;
    3112      re_node_set *cur_nodes;
    3113      int ex_subexp, type;
     3111internal_function
     3112check_arrival_expand_ecl (const re_dfa_t *dfa, re_node_set *cur_nodes,
     3113                          int ex_subexp, int type)
    31143114{
    31153115  reg_errcode_t err;
     
    31283128    {
    31293129      int cur_node = cur_nodes->elems[idx];
    3130       re_node_set *eclosure = dfa->eclosures + cur_node;
     3130      const re_node_set *eclosure = dfa->eclosures + cur_node;
    31313131      outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type);
    31323132      if (outside_node == -1)
     
    31623162
    31633163static reg_errcode_t
    3164 check_arrival_expand_ecl_sub (dfa, dst_nodes, target, ex_subexp, type)
    3165      re_dfa_t *dfa;
    3166      int target, ex_subexp, type;
    3167      re_node_set *dst_nodes;
     3164internal_function
     3165check_arrival_expand_ecl_sub (const re_dfa_t *dfa, re_node_set *dst_nodes,
     3166                              int target, int ex_subexp, int type)
    31683167{
    31693168  int cur_node;
     
    32073206
    32083207static reg_errcode_t
    3209 expand_bkref_cache (mctx, cur_nodes, cur_str, subexp_num,
    3210                     type)
    3211      re_match_context_t *mctx;
    3212      int cur_str, subexp_num, type;
    3213      re_node_set *cur_nodes;
    3214 {
    3215   re_dfa_t *const dfa = mctx->dfa;
     3208internal_function
     3209expand_bkref_cache (re_match_context_t *mctx, re_node_set *cur_nodes,
     3210                    int cur_str, int subexp_num, int type)
     3211{
     3212  const re_dfa_t *const dfa = mctx->dfa;
    32163213  reg_errcode_t err;
    32173214  int cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
     
    32983295
    32993296static int
    3300 build_trtable (dfa, state)
    3301     re_dfa_t *dfa;
    3302     re_dfastate_t *state;
     3297internal_function
     3298build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)
    33033299{
    33043300  reg_errcode_t err;
    33053301  int i, j, ch, need_word_trtable = 0;
    3306   unsigned int elem, mask;
    3307   int dests_node_malloced = 0, dest_states_malloced = 0;
     3302  bitset_word_t elem, mask;
     3303  bool dests_node_malloced = false;
     3304  bool dest_states_malloced = false;
    33083305  int ndests; /* Number of the destination states from `state'.  */
    33093306  re_dfastate_t **trtable;
    33103307  re_dfastate_t **dest_states = NULL, **dest_states_word, **dest_states_nl;
    33113308  re_node_set follows, *dests_node;
    3312   bitset *dests_ch;
    3313   bitset acceptable;
     3309  bitset_t *dests_ch;
     3310  bitset_t acceptable;
     3311
     3312  struct dests_alloc
     3313  {
     3314    re_node_set dests_node[SBC_MAX];
     3315    bitset_t dests_ch[SBC_MAX];
     3316  } *dests_alloc;
    33143317
    33153318  /* We build DFA states which corresponds to the destination nodes
     
    33173320     destination state contains, and `dests_ch[i]' represents the
    33183321     characters which i-th destination state accepts.  */
    3319 #ifdef _LIBC
    3320   if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX))
    3321     dests_node = (re_node_set *)
    3322       alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX);
     3322  if (__libc_use_alloca (sizeof (struct dests_alloc)))
     3323    dests_alloc = (struct dests_alloc *) alloca (sizeof (struct dests_alloc));
    33233324  else
    3324 #endif
    3325     {
    3326       dests_node = (re_node_set *)
    3327         malloc ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX);
    3328       if (BE (dests_node == NULL, 0))
     3325    {
     3326      dests_alloc = re_malloc (struct dests_alloc, 1);
     3327      if (BE (dests_alloc == NULL, 0))
    33293328        return 0;
    3330       dests_node_malloced = 1;
    3331     }
    3332   dests_ch = (bitset *) (dests_node + SBC_MAX);
     3329      dests_node_malloced = true;
     3330    }
     3331  dests_node = dests_alloc->dests_node;
     3332  dests_ch = dests_alloc->dests_ch;
    33333333
    33343334  /* Initialize transiton table.  */
     
    33413341    {
    33423342      if (dests_node_malloced)
    3343         free (dests_node);
     3343        free (dests_alloc);
    33443344      /* Return 0 in case of an error, 1 otherwise.  */
    33453345      if (ndests == 0)
     
    33563356    goto out_free;
    33573357
    3358 #ifdef _LIBC
    3359   if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX
     3358  if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX
    33603359                         + ndests * 3 * sizeof (re_dfastate_t *)))
    33613360    dest_states = (re_dfastate_t **)
    33623361      alloca (ndests * 3 * sizeof (re_dfastate_t *));
    33633362  else
    3364 #endif
    33653363    {
    33663364      dest_states = (re_dfastate_t **)
     
    33753373            re_node_set_free (dests_node + i);
    33763374          if (dests_node_malloced)
    3377             free (dests_node);
     3375            free (dests_alloc);
    33783376          return 0;
    33793377        }
    3380       dest_states_malloced = 1;
     3378      dest_states_malloced = true;
    33813379    }
    33823380  dest_states_word = dest_states + ndests;
     
    34403438
    34413439      /* For all characters ch...:  */
    3442       for (i = 0; i < BITSET_UINTS; ++i)
    3443         for (ch = i * UINT_BITS, elem = acceptable[i], mask = 1;
     3440      for (i = 0; i < BITSET_WORDS; ++i)
     3441        for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1;
    34443442             elem;
    34453443             mask <<= 1, elem >>= 1, ++ch)
     
    34713469
    34723470      /* For all characters ch...:  */
    3473       for (i = 0; i < BITSET_UINTS; ++i)
    3474         for (ch = i * UINT_BITS, elem = acceptable[i], mask = 1;
     3471      for (i = 0; i < BITSET_WORDS; ++i)
     3472        for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1;
    34753473             elem;
    34763474             mask <<= 1, elem >>= 1, ++ch)
     
    35133511
    35143512  if (dests_node_malloced)
    3515     free (dests_node);
     3513    free (dests_alloc);
    35163514
    35173515  return 1;
     
    35243522
    35253523static int
    3526 group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch)
    3527     re_dfa_t *dfa;
    3528     const re_dfastate_t *state;
    3529     re_node_set *dests_node;
    3530     bitset *dests_ch;
     3524internal_function
     3525group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state,
     3526                            re_node_set *dests_node, bitset_t *dests_ch)
    35313527{
    35323528  reg_errcode_t err;
     
    35343530  int i, j, k;
    35353531  int ndests; /* Number of the destinations from `state'.  */
    3536   bitset accepts; /* Characters a node can accept.  */
     3532  bitset_t accepts; /* Characters a node can accept.  */
    35373533  const re_node_set *cur_nodes = &state->nodes;
    35383534  bitset_empty (accepts);
     
    35693565      else if (type == OP_UTF8_PERIOD)
    35703566        {
    3571           memset (accepts, 255, sizeof (unsigned int) * BITSET_UINTS / 2);
     3567          memset (accepts, '\xff', sizeof (bitset_t) / 2);
    35723568          if (!(dfa->syntax & RE_DOT_NEWLINE))
    35733569            bitset_clear (accepts, '\n');
     
    35853581          if (constraint & NEXT_NEWLINE_CONSTRAINT)
    35863582            {
    3587               int accepts_newline = bitset_contain (accepts, NEWLINE_CHAR);
     3583              bool accepts_newline = bitset_contain (accepts, NEWLINE_CHAR);
    35883584              bitset_empty (accepts);
    35893585              if (accepts_newline)
     
    36003596          if (constraint & NEXT_WORD_CONSTRAINT)
    36013597            {
    3602               unsigned int any_set = 0;
     3598              bitset_word_t any_set = 0;
    36033599              if (type == CHARACTER && !node->word_char)
    36043600                {
     
    36083604#ifdef RE_ENABLE_I18N
    36093605              if (dfa->mb_cur_max > 1)
    3610                 for (j = 0; j < BITSET_UINTS; ++j)
     3606                for (j = 0; j < BITSET_WORDS; ++j)
    36113607                  any_set |= (accepts[j] &= (dfa->word_char[j] | ~dfa->sb_char[j]));
    36123608              else
    36133609#endif
    3614                 for (j = 0; j < BITSET_UINTS; ++j)
     3610                for (j = 0; j < BITSET_WORDS; ++j)
    36153611                  any_set |= (accepts[j] &= dfa->word_char[j]);
    36163612              if (!any_set)
     
    36193615          if (constraint & NEXT_NOTWORD_CONSTRAINT)
    36203616            {
    3621               unsigned int any_set = 0;
     3617              bitset_word_t any_set = 0;
    36223618              if (type == CHARACTER && node->word_char)
    36233619                {
     
    36273623#ifdef RE_ENABLE_I18N
    36283624              if (dfa->mb_cur_max > 1)
    3629                 for (j = 0; j < BITSET_UINTS; ++j)
     3625                for (j = 0; j < BITSET_WORDS; ++j)
    36303626                  any_set |= (accepts[j] &= ~(dfa->word_char[j] & dfa->sb_char[j]));
    36313627              else
    36323628#endif
    3633                 for (j = 0; j < BITSET_UINTS; ++j)
     3629                for (j = 0; j < BITSET_WORDS; ++j)
    36343630                  any_set |= (accepts[j] &= ~dfa->word_char[j]);
    36353631              if (!any_set)
     
    36423638      for (j = 0; j < ndests; ++j)
    36433639        {
    3644           bitset intersec; /* Intersection sets, see below.  */
    3645           bitset remains;
     3640          bitset_t intersec; /* Intersection sets, see below.  */
     3641          bitset_t remains;
    36463642          /* Flags, see below.  */
    3647           int has_intersec, not_subset, not_consumed;
     3643          bitset_word_t has_intersec, not_subset, not_consumed;
    36483644
    36493645          /* Optimization, skip if this state doesn't accept the character.  */
     
    36533649          /* Enumerate the intersection set of this state and `accepts'.  */
    36543650          has_intersec = 0;
    3655           for (k = 0; k < BITSET_UINTS; ++k)
     3651          for (k = 0; k < BITSET_WORDS; ++k)
    36563652            has_intersec |= intersec[k] = accepts[k] & dests_ch[j][k];
    36573653          /* And skip if the intersection set is empty.  */
     
    36613657          /* Then check if this state is a subset of `accepts'.  */
    36623658          not_subset = not_consumed = 0;
    3663           for (k = 0; k < BITSET_UINTS; ++k)
     3659          for (k = 0; k < BITSET_WORDS; ++k)
    36643660            {
    36653661              not_subset |= remains[k] = ~accepts[k] & dests_ch[j][k];
     
    37163712
    37173713static int
    3718 check_node_accept_bytes (dfa, node_idx, input, str_idx)
    3719     re_dfa_t *dfa;
    3720     int node_idx, str_idx;
    3721     const re_string_t *input;
     3714internal_function
     3715check_node_accept_bytes (const re_dfa_t *dfa, int node_idx,
     3716                         const re_string_t *input, int str_idx)
    37223717{
    37233718  const re_token_t *node = dfa->nodes + node_idx;
     
    39533948# ifdef _LIBC
    39543949static unsigned int
    3955 find_collation_sequence_value (mbs, mbs_len)
    3956     const unsigned char *mbs;
    3957     size_t mbs_len;
     3950internal_function
     3951find_collation_sequence_value (const unsigned char *mbs, size_t mbs_len)
    39583952{
    39593953  uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
     
    40174011
    40184012static int
    4019 check_node_accept (mctx, node, idx)
    4020     const re_match_context_t *mctx;
    4021     const re_token_t *node;
    4022     int idx;
     4013internal_function
     4014check_node_accept (const re_match_context_t *mctx, const re_token_t *node,
     4015                   int idx)
    40234016{
    40244017  unsigned char ch;
     
    40684061
    40694062static reg_errcode_t
    4070 extend_buffers (mctx)
    4071      re_match_context_t *mctx;
     4063internal_function
     4064extend_buffers (re_match_context_t *mctx)
    40724065{
    40734066  reg_errcode_t ret;
     
    41284121
    41294122static reg_errcode_t
    4130 match_ctx_init (mctx, eflags, n)
    4131     re_match_context_t *mctx;
    4132     int eflags, n;
     4123internal_function
     4124match_ctx_init (re_match_context_t *mctx, int eflags, int n)
    41334125{
    41344126  mctx->eflags = eflags;
     
    41574149
    41584150static void
    4159 match_ctx_clean (mctx)
    4160     re_match_context_t *mctx;
     4151internal_function
     4152match_ctx_clean (re_match_context_t *mctx)
    41614153{
    41624154  int st_idx;
     
    41874179
    41884180static void
    4189 match_ctx_free (mctx)
    4190     re_match_context_t *mctx;
     4181internal_function
     4182match_ctx_free (re_match_context_t *mctx)
    41914183{
    41924184  /* First, free all the memory associated with MCTX->SUB_TOPS.  */
     
    42024194
    42034195static reg_errcode_t
    4204 match_ctx_add_entry (mctx, node, str_idx, from, to)
    4205      re_match_context_t *mctx;
    4206      int node, str_idx, from, to;
     4196internal_function
     4197match_ctx_add_entry (re_match_context_t *mctx, int node, int str_idx, int from,
     4198                     int to)
    42074199{
    42084200  if (mctx->nbkref_ents >= mctx->abkref_ents)
     
    42514243
    42524244static int
    4253 search_cur_bkref_entry (mctx, str_idx)
    4254      re_match_context_t *mctx;
    4255      int str_idx;
     4245internal_function
     4246search_cur_bkref_entry (const re_match_context_t *mctx, int str_idx)
    42564247{
    42574248  int left, right, mid, last;
     
    42754266
    42764267static reg_errcode_t
    4277 match_ctx_add_subtop (mctx, node, str_idx)
    4278      re_match_context_t *mctx;
    4279      int node, str_idx;
     4268internal_function
     4269match_ctx_add_subtop (re_match_context_t *mctx, int node, int str_idx)
    42804270{
    42814271#ifdef DEBUG
     
    43064296
    43074297static re_sub_match_last_t *
    4308 match_ctx_add_sublast (subtop, node, str_idx)
    4309      re_sub_match_top_t *subtop;
    4310      int node, str_idx;
     4298internal_function
     4299match_ctx_add_sublast (re_sub_match_top_t *subtop, int node, int str_idx)
    43114300{
    43124301  re_sub_match_last_t *new_entry;
     
    43344323
    43354324static void
    4336 sift_ctx_init (sctx, sifted_sts, limited_sts, last_node, last_str_idx)
    4337     re_sift_context_t *sctx;
    4338     re_dfastate_t **sifted_sts, **limited_sts;
    4339     int last_node, last_str_idx;
     4325internal_function
     4326sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
     4327               re_dfastate_t **limited_sts, int last_node, int last_str_idx)
    43404328{
    43414329  sctx->sifted_states = sifted_sts;
Note: See TracChangeset for help on using the changeset viewer.