libroot: Synchronize glibc regex with upstream 2.28.

This commit is contained in:
Augustin Cavalier
2024-07-22 19:56:48 -04:00
parent d3f63a3e4c
commit bda66ab7c7
6 changed files with 1483 additions and 1313 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
SubDir HAIKU_TOP src system libroot posix glibc regex ; SubDir HAIKU_TOP src system libroot posix glibc regex ;
SubDirHdrs $(HAIKU_TOP) headers ; SubDirHdrs $(HAIKU_TOP) headers ;
SubDirCcFlags -D_REGEX_RE_COMP -D_DEFAULT_SOURCE -DHAVE_STDBOOL_H -DHAVE_STDINT_H ; SubDirCcFlags -D_REGEX_RE_COMP -D_DEFAULT_SOURCE ;
local architectureObject ; local architectureObject ;
for architectureObject in [ MultiArchSubDirSetup ] { for architectureObject in [ MultiArchSubDirSetup ] {
File diff suppressed because it is too large Load Diff
+14 -9
View File
@@ -1,5 +1,5 @@
/* Extended regular expression matching and search library. /* Extended regular expression matching and search library.
Copyright (C) 2002-2014 Free Software Foundation, Inc. Copyright (C) 2002-2018 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
@@ -15,14 +15,22 @@
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */ <https://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H #if !defined _LIBC && !defined __HAIKU__
#include "config.h" # include <config.h>
# if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__
# pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
# endif
# if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
# pragma GCC diagnostic ignored "-Wold-style-definition"
# pragma GCC diagnostic ignored "-Wtype-limits"
# endif
#endif #endif
/* Make sure noone compiles this code with a C++ compiler. */ /* Make sure no one compiles this code with a C++ compiler. */
#ifdef __cplusplus #if defined __cplusplus && defined _LIBC
# error "This is C code, use a C compiler" # error "This is C code, use a C compiler"
#endif #endif
@@ -56,9 +64,6 @@
#undefs RE_DUP_MAX and sets it to the right value. */ #undefs RE_DUP_MAX and sets it to the right value. */
#include <limits.h> #include <limits.h>
/* This header defines the MIN and MAX macros. */
#include <sys/param.h>
#include <regex.h> #include <regex.h>
#include "regex_internal.h" #include "regex_internal.h"
@@ -1,5 +1,5 @@
/* Extended regular expression matching and search library. /* Extended regular expression matching and search library.
Copyright (C) 2002-2014 Free Software Foundation, Inc. Copyright (C) 2002-2018 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
@@ -15,19 +15,29 @@
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */ <https://www.gnu.org/licenses/>. */
static void re_string_construct_common (const char *str, int len, static void re_string_construct_common (const char *str, Idx len,
re_string_t *pstr, re_string_t *pstr,
RE_TRANSLATE_TYPE trans, int icase, RE_TRANSLATE_TYPE trans, bool icase,
const re_dfa_t *dfa) internal_function; const re_dfa_t *dfa);
static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa, static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
const re_node_set *nodes, const re_node_set *nodes,
unsigned int hash) internal_function; re_hashval_t hash);
static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa, static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
const re_node_set *nodes, const re_node_set *nodes,
unsigned int context, unsigned int context,
unsigned int hash) internal_function; re_hashval_t hash);
static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
Idx new_buf_len);
#ifdef RE_ENABLE_I18N
static void build_wcs_buffer (re_string_t *pstr);
static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr);
#endif /* RE_ENABLE_I18N */
static void build_upper_buffer (re_string_t *pstr);
static void re_string_translate_buffer (re_string_t *pstr);
static unsigned int re_string_context_at (const re_string_t *input, Idx idx,
int eflags) __attribute__ ((pure));
/* Functions for string operation. */ /* Functions for string operation. */
@@ -35,12 +45,12 @@ static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
re_string_reconstruct before using the object. */ re_string_reconstruct before using the object. */
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len, re_string_allocate (re_string_t *pstr, const char *str, Idx len, Idx init_len,
RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa) RE_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
{ {
reg_errcode_t ret; reg_errcode_t ret;
int init_buf_len; Idx init_buf_len;
/* Ensure at least one character fits into the buffers. */ /* Ensure at least one character fits into the buffers. */
if (init_len < dfa->mb_cur_max) if (init_len < dfa->mb_cur_max)
@@ -63,9 +73,9 @@ re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len,
/* This function allocate the buffers, and initialize them. */ /* This function allocate the buffers, and initialize them. */
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_string_construct (re_string_t *pstr, const char *str, int len, re_string_construct (re_string_t *pstr, const char *str, Idx len,
RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa) RE_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
{ {
reg_errcode_t ret; reg_errcode_t ret;
memset (pstr, '\0', sizeof (re_string_t)); memset (pstr, '\0', sizeof (re_string_t));
@@ -126,8 +136,8 @@ re_string_construct (re_string_t *pstr, const char *str, int len,
/* Helper functions for re_string_allocate, and re_string_construct. */ /* Helper functions for re_string_allocate, and re_string_construct. */
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_string_realloc_buffers (re_string_t *pstr, int new_buf_len) re_string_realloc_buffers (re_string_t *pstr, Idx new_buf_len)
{ {
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
if (pstr->mb_cur_max > 1) if (pstr->mb_cur_max > 1)
@@ -135,8 +145,8 @@ re_string_realloc_buffers (re_string_t *pstr, int new_buf_len)
wint_t *new_wcs; wint_t *new_wcs;
/* Avoid overflow in realloc. */ /* Avoid overflow in realloc. */
const size_t max_object_size = MAX (sizeof (wint_t), sizeof (int)); const size_t max_object_size = MAX (sizeof (wint_t), sizeof (Idx));
if (BE (SIZE_MAX / max_object_size < new_buf_len, 0)) if (BE (MIN (IDX_MAX, SIZE_MAX / max_object_size) < new_buf_len, 0))
return REG_ESPACE; return REG_ESPACE;
new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len); new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len);
@@ -145,7 +155,7 @@ re_string_realloc_buffers (re_string_t *pstr, int new_buf_len)
pstr->wcs = new_wcs; pstr->wcs = new_wcs;
if (pstr->offsets != NULL) if (pstr->offsets != NULL)
{ {
int *new_offsets = re_realloc (pstr->offsets, int, new_buf_len); Idx *new_offsets = re_realloc (pstr->offsets, Idx, new_buf_len);
if (BE (new_offsets == NULL, 0)) if (BE (new_offsets == NULL, 0))
return REG_ESPACE; return REG_ESPACE;
pstr->offsets = new_offsets; pstr->offsets = new_offsets;
@@ -166,16 +176,15 @@ re_string_realloc_buffers (re_string_t *pstr, int new_buf_len)
static void static void
internal_function re_string_construct_common (const char *str, Idx len, re_string_t *pstr,
re_string_construct_common (const char *str, int len, re_string_t *pstr, RE_TRANSLATE_TYPE trans, bool icase,
RE_TRANSLATE_TYPE trans, int icase,
const re_dfa_t *dfa) const re_dfa_t *dfa)
{ {
pstr->raw_mbs = (const unsigned char *) str; pstr->raw_mbs = (const unsigned char *) str;
pstr->len = len; pstr->len = len;
pstr->raw_len = len; pstr->raw_len = len;
pstr->trans = trans; pstr->trans = trans;
pstr->icase = icase ? 1 : 0; pstr->icase = icase;
pstr->mbs_allocated = (trans != NULL || icase); pstr->mbs_allocated = (trans != NULL || icase);
pstr->mb_cur_max = dfa->mb_cur_max; pstr->mb_cur_max = dfa->mb_cur_max;
pstr->is_utf8 = dfa->is_utf8; pstr->is_utf8 = dfa->is_utf8;
@@ -198,7 +207,6 @@ re_string_construct_common (const char *str, int len, re_string_t *pstr,
built and starts from PSTR->VALID_LEN. */ built and starts from PSTR->VALID_LEN. */
static void static void
internal_function
build_wcs_buffer (re_string_t *pstr) build_wcs_buffer (re_string_t *pstr)
{ {
#ifdef _LIBC #ifdef _LIBC
@@ -208,7 +216,7 @@ build_wcs_buffer (re_string_t *pstr)
unsigned char buf[64]; unsigned char buf[64];
#endif #endif
mbstate_t prev_st; mbstate_t prev_st;
int byte_idx, end_idx, remain_len; Idx byte_idx, end_idx, remain_len;
size_t mbclen; size_t mbclen;
/* Build the buffers from pstr->valid_len to either pstr->len or /* Build the buffers from pstr->valid_len to either pstr->len or
@@ -267,11 +275,11 @@ build_wcs_buffer (re_string_t *pstr)
but for REG_ICASE. */ but for REG_ICASE. */
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
build_wcs_upper_buffer (re_string_t *pstr) build_wcs_upper_buffer (re_string_t *pstr)
{ {
mbstate_t prev_st; mbstate_t prev_st;
int src_idx, byte_idx, end_idx, remain_len; Idx src_idx, byte_idx, end_idx, remain_len;
size_t mbclen; size_t mbclen;
#ifdef _LIBC #ifdef _LIBC
char buf[MB_LEN_MAX]; char buf[MB_LEN_MAX];
@@ -309,15 +317,14 @@ build_wcs_upper_buffer (re_string_t *pstr)
mbclen = __mbrtowc (&wc, mbclen = __mbrtowc (&wc,
((const char *) pstr->raw_mbs + pstr->raw_mbs_idx ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
+ byte_idx), remain_len, &pstr->cur_state); + byte_idx), remain_len, &pstr->cur_state);
if (BE (mbclen + 2 > 2, 1)) if (BE (0 < mbclen && mbclen < (size_t) -2, 1))
{ {
wchar_t wcu = wc; wchar_t wcu = __towupper (wc);
if (iswlower (wc)) if (wcu != wc)
{ {
size_t mbcdlen; size_t mbcdlen;
wcu = towupper (wc); mbcdlen = __wcrtomb (buf, wcu, &prev_st);
mbcdlen = wcrtomb (buf, wcu, &prev_st);
if (BE (mbclen == mbcdlen, 1)) if (BE (mbclen == mbcdlen, 1))
memcpy (pstr->mbs + byte_idx, buf, mbclen); memcpy (pstr->mbs + byte_idx, buf, mbclen);
else else
@@ -379,15 +386,14 @@ build_wcs_upper_buffer (re_string_t *pstr)
else else
p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx; p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state); mbclen = __mbrtowc (&wc, p, remain_len, &pstr->cur_state);
if (BE (mbclen + 2 > 2, 1)) if (BE (0 < mbclen && mbclen < (size_t) -2, 1))
{ {
wchar_t wcu = wc; wchar_t wcu = __towupper (wc);
if (iswlower (wc)) if (wcu != wc)
{ {
size_t mbcdlen; size_t mbcdlen;
wcu = towupper (wc); mbcdlen = __wcrtomb ((char *) buf, wcu, &prev_st);
mbcdlen = wcrtomb ((char *) buf, wcu, &prev_st);
if (BE (mbclen == mbcdlen, 1)) if (BE (mbclen == mbcdlen, 1))
memcpy (pstr->mbs + byte_idx, buf, mbclen); memcpy (pstr->mbs + byte_idx, buf, mbclen);
else if (mbcdlen != (size_t) -1) else if (mbcdlen != (size_t) -1)
@@ -402,7 +408,7 @@ build_wcs_upper_buffer (re_string_t *pstr)
if (pstr->offsets == NULL) if (pstr->offsets == NULL)
{ {
pstr->offsets = re_malloc (int, pstr->bufs_len); pstr->offsets = re_malloc (Idx, pstr->bufs_len);
if (pstr->offsets == NULL) if (pstr->offsets == NULL)
return REG_ESPACE; return REG_ESPACE;
@@ -485,12 +491,11 @@ build_wcs_upper_buffer (re_string_t *pstr)
/* Skip characters until the index becomes greater than NEW_RAW_IDX. /* Skip characters until the index becomes greater than NEW_RAW_IDX.
Return the index. */ Return the index. */
static int static Idx
internal_function re_string_skip_chars (re_string_t *pstr, Idx new_raw_idx, wint_t *last_wc)
re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
{ {
mbstate_t prev_st; mbstate_t prev_st;
int rawbuf_idx; Idx rawbuf_idx;
size_t mbclen; size_t mbclen;
wint_t wc = WEOF; wint_t wc = WEOF;
@@ -499,11 +504,11 @@ re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
rawbuf_idx < new_raw_idx;) rawbuf_idx < new_raw_idx;)
{ {
wchar_t wc2; wchar_t wc2;
int remain_len = pstr->raw_len - rawbuf_idx; Idx remain_len = pstr->raw_len - rawbuf_idx;
prev_st = pstr->cur_state; prev_st = pstr->cur_state;
mbclen = __mbrtowc (&wc2, (const char *) pstr->raw_mbs + rawbuf_idx, mbclen = __mbrtowc (&wc2, (const char *) pstr->raw_mbs + rawbuf_idx,
remain_len, &pstr->cur_state); remain_len, &pstr->cur_state);
if (BE ((ssize_t) mbclen <= 0, 0)) if (BE (mbclen == (size_t) -2 || mbclen == (size_t) -1 || mbclen == 0, 0))
{ {
/* We treat these cases as a single byte character. */ /* We treat these cases as a single byte character. */
if (mbclen == 0 || remain_len == 0) if (mbclen == 0 || remain_len == 0)
@@ -514,7 +519,7 @@ re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
pstr->cur_state = prev_st; pstr->cur_state = prev_st;
} }
else else
wc = (wint_t) wc2; wc = wc2;
/* Then proceed the next character. */ /* Then proceed the next character. */
rawbuf_idx += mbclen; rawbuf_idx += mbclen;
} }
@@ -527,10 +532,9 @@ re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
This function is used in case of REG_ICASE. */ This function is used in case of REG_ICASE. */
static void static void
internal_function
build_upper_buffer (re_string_t *pstr) build_upper_buffer (re_string_t *pstr)
{ {
int char_idx, end_idx; Idx char_idx, end_idx;
end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len; end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx) for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
@@ -538,10 +542,7 @@ build_upper_buffer (re_string_t *pstr)
int ch = pstr->raw_mbs[pstr->raw_mbs_idx + char_idx]; int ch = pstr->raw_mbs[pstr->raw_mbs_idx + char_idx];
if (BE (pstr->trans != NULL, 0)) if (BE (pstr->trans != NULL, 0))
ch = pstr->trans[ch]; ch = pstr->trans[ch];
if (islower (ch)) pstr->mbs[char_idx] = toupper (ch);
pstr->mbs[char_idx] = toupper (ch);
else
pstr->mbs[char_idx] = ch;
} }
pstr->valid_len = char_idx; pstr->valid_len = char_idx;
pstr->valid_raw_len = char_idx; pstr->valid_raw_len = char_idx;
@@ -550,10 +551,9 @@ build_upper_buffer (re_string_t *pstr)
/* Apply TRANS to the buffer in PSTR. */ /* Apply TRANS to the buffer in PSTR. */
static void static void
internal_function
re_string_translate_buffer (re_string_t *pstr) re_string_translate_buffer (re_string_t *pstr)
{ {
int buf_idx, end_idx; Idx buf_idx, end_idx;
end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len; end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx) for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx)
@@ -571,11 +571,14 @@ re_string_translate_buffer (re_string_t *pstr)
convert to upper case in case of REG_ICASE, apply translation. */ convert to upper case in case of REG_ICASE, apply translation. */
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_string_reconstruct (re_string_t *pstr, int idx, int eflags) re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
{ {
int offset = idx - pstr->raw_mbs_idx; Idx offset;
if (BE (offset < 0, 0))
if (BE (pstr->raw_mbs_idx <= idx, 0))
offset = idx - pstr->raw_mbs_idx;
else
{ {
/* Reset buffer. */ /* Reset buffer. */
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
@@ -604,7 +607,7 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
if (BE (pstr->offsets_needed, 0)) if (BE (pstr->offsets_needed, 0))
{ {
int low = 0, high = pstr->valid_len, mid; Idx low = 0, high = pstr->valid_len, mid;
do do
{ {
mid = (high + low) / 2; mid = (high + low) / 2;
@@ -686,10 +689,10 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
} }
else else
{ {
/* No, skip all characters until IDX. */
int prev_valid_len = pstr->valid_len;
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
/* No, skip all characters until IDX. */
Idx prev_valid_len = pstr->valid_len;
if (BE (pstr->offsets_needed, 0)) if (BE (pstr->offsets_needed, 0))
{ {
pstr->len = pstr->raw_len - idx + offset; pstr->len = pstr->raw_len - idx + offset;
@@ -701,7 +704,7 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
if (pstr->mb_cur_max > 1) if (pstr->mb_cur_max > 1)
{ {
int wcs_idx; Idx wcs_idx;
wint_t wc = WEOF; wint_t wc = WEOF;
if (pstr->is_utf8) if (pstr->is_utf8)
@@ -731,7 +734,7 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
{ {
mbstate_t cur_state; mbstate_t cur_state;
wchar_t wc2; wchar_t wc2;
int mlen = raw + pstr->len - p; Idx mlen = raw + pstr->len - p;
unsigned char buf[6]; unsigned char buf[6];
size_t mbclen; size_t mbclen;
@@ -831,10 +834,11 @@ re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
} }
static unsigned char static unsigned char
internal_function __attribute ((pure)) __attribute__ ((pure))
re_string_peek_byte_case (const re_string_t *pstr, int idx) re_string_peek_byte_case (const re_string_t *pstr, Idx idx)
{ {
int ch, off; int ch;
Idx off;
/* Handle the common (easiest) cases first. */ /* Handle the common (easiest) cases first. */
if (BE (!pstr->mbs_allocated, 1)) if (BE (!pstr->mbs_allocated, 1))
@@ -867,7 +871,6 @@ re_string_peek_byte_case (const re_string_t *pstr, int idx)
} }
static unsigned char static unsigned char
internal_function
re_string_fetch_byte_case (re_string_t *pstr) re_string_fetch_byte_case (re_string_t *pstr)
{ {
if (BE (!pstr->mbs_allocated, 1)) if (BE (!pstr->mbs_allocated, 1))
@@ -876,7 +879,8 @@ re_string_fetch_byte_case (re_string_t *pstr)
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
if (pstr->offsets_needed) if (pstr->offsets_needed)
{ {
int off, ch; Idx off;
int ch;
/* For tr_TR.UTF-8 [[:islower:]] there is /* For tr_TR.UTF-8 [[:islower:]] there is
[[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip [[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip
@@ -904,7 +908,6 @@ re_string_fetch_byte_case (re_string_t *pstr)
} }
static void static void
internal_function
re_string_destruct (re_string_t *pstr) re_string_destruct (re_string_t *pstr)
{ {
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
@@ -918,8 +921,7 @@ re_string_destruct (re_string_t *pstr)
/* Return the context at IDX in INPUT. */ /* Return the context at IDX in INPUT. */
static unsigned int static unsigned int
internal_function re_string_context_at (const re_string_t *input, Idx idx, int eflags)
re_string_context_at (const re_string_t *input, int idx, int eflags)
{ {
int c; int c;
if (BE (idx < 0, 0)) if (BE (idx < 0, 0))
@@ -933,7 +935,7 @@ re_string_context_at (const re_string_t *input, int idx, int eflags)
if (input->mb_cur_max > 1) if (input->mb_cur_max > 1)
{ {
wint_t wc; wint_t wc;
int wc_idx = idx; Idx wc_idx = idx;
while(input->wcs[wc_idx] == WEOF) while(input->wcs[wc_idx] == WEOF)
{ {
#if defined DEBUG && DEBUG #if defined DEBUG && DEBUG
@@ -963,24 +965,24 @@ re_string_context_at (const re_string_t *input, int idx, int eflags)
/* Functions for set operation. */ /* Functions for set operation. */
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_node_set_alloc (re_node_set *set, int size) re_node_set_alloc (re_node_set *set, Idx size)
{ {
set->alloc = size; set->alloc = size;
set->nelem = 0; set->nelem = 0;
set->elems = re_malloc (int, size); set->elems = re_malloc (Idx, size);
if (BE (set->elems == NULL, 0)) if (BE (set->elems == NULL, 0) && (MALLOC_0_IS_NONNULL || size != 0))
return REG_ESPACE; return REG_ESPACE;
return REG_NOERROR; return REG_NOERROR;
} }
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_node_set_init_1 (re_node_set *set, int elem) re_node_set_init_1 (re_node_set *set, Idx elem)
{ {
set->alloc = 1; set->alloc = 1;
set->nelem = 1; set->nelem = 1;
set->elems = re_malloc (int, 1); set->elems = re_malloc (Idx, 1);
if (BE (set->elems == NULL, 0)) if (BE (set->elems == NULL, 0))
{ {
set->alloc = set->nelem = 0; set->alloc = set->nelem = 0;
@@ -991,11 +993,11 @@ re_node_set_init_1 (re_node_set *set, int elem)
} }
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_node_set_init_2 (re_node_set *set, int elem1, int elem2) re_node_set_init_2 (re_node_set *set, Idx elem1, Idx elem2)
{ {
set->alloc = 2; set->alloc = 2;
set->elems = re_malloc (int, 2); set->elems = re_malloc (Idx, 2);
if (BE (set->elems == NULL, 0)) if (BE (set->elems == NULL, 0))
return REG_ESPACE; return REG_ESPACE;
if (elem1 == elem2) if (elem1 == elem2)
@@ -1021,20 +1023,20 @@ re_node_set_init_2 (re_node_set *set, int elem1, int elem2)
} }
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_node_set_init_copy (re_node_set *dest, const re_node_set *src) re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
{ {
dest->nelem = src->nelem; dest->nelem = src->nelem;
if (src->nelem > 0) if (src->nelem > 0)
{ {
dest->alloc = dest->nelem; dest->alloc = dest->nelem;
dest->elems = re_malloc (int, dest->alloc); dest->elems = re_malloc (Idx, dest->alloc);
if (BE (dest->elems == NULL, 0)) if (BE (dest->elems == NULL, 0))
{ {
dest->alloc = dest->nelem = 0; dest->alloc = dest->nelem = 0;
return REG_ESPACE; return REG_ESPACE;
} }
memcpy (dest->elems, src->elems, src->nelem * sizeof (int)); memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx));
} }
else else
re_node_set_init_empty (dest); re_node_set_init_empty (dest);
@@ -1046,11 +1048,11 @@ re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
Note: We assume dest->elems is NULL, when dest->alloc is 0. */ Note: We assume dest->elems is NULL, when dest->alloc is 0. */
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1, re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
const re_node_set *src2) const re_node_set *src2)
{ {
int i1, i2, is, id, delta, sbase; Idx i1, i2, is, id, delta, sbase;
if (src1->nelem == 0 || src2->nelem == 0) if (src1->nelem == 0 || src2->nelem == 0)
return REG_NOERROR; return REG_NOERROR;
@@ -1058,8 +1060,8 @@ re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
conservative estimate. */ conservative estimate. */
if (src1->nelem + src2->nelem + dest->nelem > dest->alloc) if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
{ {
int new_alloc = src1->nelem + src2->nelem + dest->alloc; Idx new_alloc = src1->nelem + src2->nelem + dest->alloc;
int *new_elems = re_realloc (dest->elems, int, new_alloc); Idx *new_elems = re_realloc (dest->elems, Idx, new_alloc);
if (BE (new_elems == NULL, 0)) if (BE (new_elems == NULL, 0))
return REG_ESPACE; return REG_ESPACE;
dest->elems = new_elems; dest->elems = new_elems;
@@ -1081,7 +1083,7 @@ re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
--id; --id;
if (id < 0 || dest->elems[id] != src1->elems[i1]) if (id < 0 || dest->elems[id] != src1->elems[i1])
dest->elems[--sbase] = src1->elems[i1]; dest->elems[--sbase] = src1->elems[i1];
if (--i1 < 0 || --i2 < 0) if (--i1 < 0 || --i2 < 0)
break; break;
@@ -1128,7 +1130,7 @@ re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
} }
/* Copy remaining SRC elements. */ /* Copy remaining SRC elements. */
memcpy (dest->elems, dest->elems + sbase, delta * sizeof (int)); memcpy (dest->elems, dest->elems + sbase, delta * sizeof (Idx));
return REG_NOERROR; return REG_NOERROR;
} }
@@ -1137,15 +1139,15 @@ re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
DEST. Return value indicate the error code or REG_NOERROR if succeeded. */ DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_node_set_init_union (re_node_set *dest, const re_node_set *src1, re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
const re_node_set *src2) const re_node_set *src2)
{ {
int i1, i2, id; Idx i1, i2, id;
if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0) if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
{ {
dest->alloc = src1->nelem + src2->nelem; dest->alloc = src1->nelem + src2->nelem;
dest->elems = re_malloc (int, dest->alloc); dest->elems = re_malloc (Idx, dest->alloc);
if (BE (dest->elems == NULL, 0)) if (BE (dest->elems == NULL, 0))
return REG_ESPACE; return REG_ESPACE;
} }
@@ -1173,13 +1175,13 @@ re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
if (i1 < src1->nelem) if (i1 < src1->nelem)
{ {
memcpy (dest->elems + id, src1->elems + i1, memcpy (dest->elems + id, src1->elems + i1,
(src1->nelem - i1) * sizeof (int)); (src1->nelem - i1) * sizeof (Idx));
id += src1->nelem - i1; id += src1->nelem - i1;
} }
else if (i2 < src2->nelem) else if (i2 < src2->nelem)
{ {
memcpy (dest->elems + id, src2->elems + i2, memcpy (dest->elems + id, src2->elems + i2,
(src2->nelem - i2) * sizeof (int)); (src2->nelem - i2) * sizeof (Idx));
id += src2->nelem - i2; id += src2->nelem - i2;
} }
dest->nelem = id; dest->nelem = id;
@@ -1190,16 +1192,16 @@ re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
DEST. Return value indicate the error code or REG_NOERROR if succeeded. */ DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
static reg_errcode_t static reg_errcode_t
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_node_set_merge (re_node_set *dest, const re_node_set *src) re_node_set_merge (re_node_set *dest, const re_node_set *src)
{ {
int is, id, sbase, delta; Idx is, id, sbase, delta;
if (src == NULL || src->nelem == 0) if (src == NULL || src->nelem == 0)
return REG_NOERROR; return REG_NOERROR;
if (dest->alloc < 2 * src->nelem + dest->nelem) if (dest->alloc < 2 * src->nelem + dest->nelem)
{ {
int new_alloc = 2 * (src->nelem + dest->alloc); Idx new_alloc = 2 * (src->nelem + dest->alloc);
int *new_buffer = re_realloc (dest->elems, int, new_alloc); Idx *new_buffer = re_realloc (dest->elems, Idx, new_alloc);
if (BE (new_buffer == NULL, 0)) if (BE (new_buffer == NULL, 0))
return REG_ESPACE; return REG_ESPACE;
dest->elems = new_buffer; dest->elems = new_buffer;
@@ -1209,7 +1211,7 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
if (BE (dest->nelem == 0, 0)) if (BE (dest->nelem == 0, 0))
{ {
dest->nelem = src->nelem; dest->nelem = src->nelem;
memcpy (dest->elems, src->elems, src->nelem * sizeof (int)); memcpy (dest->elems, src->elems, src->nelem * sizeof (Idx));
return REG_NOERROR; return REG_NOERROR;
} }
@@ -1230,7 +1232,7 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
{ {
/* If DEST is exhausted, the remaining items of SRC must be unique. */ /* If DEST is exhausted, the remaining items of SRC must be unique. */
sbase -= is + 1; sbase -= is + 1;
memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int)); memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (Idx));
} }
id = dest->nelem - 1; id = dest->nelem - 1;
@@ -1259,7 +1261,7 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
{ {
/* Copy remaining SRC elements. */ /* Copy remaining SRC elements. */
memcpy (dest->elems, dest->elems + sbase, memcpy (dest->elems, dest->elems + sbase,
delta * sizeof (int)); delta * sizeof (Idx));
break; break;
} }
} }
@@ -1270,38 +1272,33 @@ re_node_set_merge (re_node_set *dest, const re_node_set *src)
/* Insert the new element ELEM to the re_node_set* SET. /* Insert the new element ELEM to the re_node_set* SET.
SET should not already have ELEM. SET should not already have ELEM.
return -1 if an error is occured, return 1 otherwise. */ Return true if successful. */
static int static bool
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_node_set_insert (re_node_set *set, int elem) re_node_set_insert (re_node_set *set, Idx elem)
{ {
int idx; Idx idx;
/* In case the set is empty. */ /* In case the set is empty. */
if (set->alloc == 0) if (set->alloc == 0)
{ return BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1);
if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1))
return 1;
else
return -1;
}
if (BE (set->nelem, 0) == 0) if (BE (set->nelem, 0) == 0)
{ {
/* We already guaranteed above that set->alloc != 0. */ /* We already guaranteed above that set->alloc != 0. */
set->elems[0] = elem; set->elems[0] = elem;
++set->nelem; ++set->nelem;
return 1; return true;
} }
/* Realloc if we need. */ /* Realloc if we need. */
if (set->alloc == set->nelem) if (set->alloc == set->nelem)
{ {
int *new_elems; Idx *new_elems;
set->alloc = set->alloc * 2; set->alloc = set->alloc * 2;
new_elems = re_realloc (set->elems, int, set->alloc); new_elems = re_realloc (set->elems, Idx, set->alloc);
if (BE (new_elems == NULL, 0)) if (BE (new_elems == NULL, 0))
return -1; return false;
set->elems = new_elems; set->elems = new_elems;
} }
@@ -1322,56 +1319,56 @@ re_node_set_insert (re_node_set *set, int elem)
/* Insert the new element. */ /* Insert the new element. */
set->elems[idx] = elem; set->elems[idx] = elem;
++set->nelem; ++set->nelem;
return 1; return true;
} }
/* Insert the new element ELEM to the re_node_set* SET. /* Insert the new element ELEM to the re_node_set* SET.
SET should not already have any element greater than or equal to ELEM. SET should not already have any element greater than or equal to ELEM.
Return -1 if an error is occured, return 1 otherwise. */ Return true if successful. */
static int static bool
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_node_set_insert_last (re_node_set *set, int elem) re_node_set_insert_last (re_node_set *set, Idx elem)
{ {
/* Realloc if we need. */ /* Realloc if we need. */
if (set->alloc == set->nelem) if (set->alloc == set->nelem)
{ {
int *new_elems; Idx *new_elems;
set->alloc = (set->alloc + 1) * 2; set->alloc = (set->alloc + 1) * 2;
new_elems = re_realloc (set->elems, int, set->alloc); new_elems = re_realloc (set->elems, Idx, set->alloc);
if (BE (new_elems == NULL, 0)) if (BE (new_elems == NULL, 0))
return -1; return false;
set->elems = new_elems; set->elems = new_elems;
} }
/* Insert the new element. */ /* Insert the new element. */
set->elems[set->nelem++] = elem; set->elems[set->nelem++] = elem;
return 1; return true;
} }
/* Compare two node sets SET1 and SET2. /* Compare two node sets SET1 and SET2.
return 1 if SET1 and SET2 are equivalent, return 0 otherwise. */ Return true if SET1 and SET2 are equivalent. */
static int static bool
internal_function __attribute ((pure)) __attribute__ ((pure))
re_node_set_compare (const re_node_set *set1, const re_node_set *set2) re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
{ {
int i; Idx i;
if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem) if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
return 0; return false;
for (i = set1->nelem ; --i >= 0 ; ) for (i = set1->nelem ; --i >= 0 ; )
if (set1->elems[i] != set2->elems[i]) if (set1->elems[i] != set2->elems[i])
return 0; return false;
return 1; return true;
} }
/* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */ /* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */
static int static Idx
internal_function __attribute ((pure)) __attribute__ ((pure))
re_node_set_contains (const re_node_set *set, int elem) re_node_set_contains (const re_node_set *set, Idx elem)
{ {
unsigned int idx, right, mid; __re_size_t idx, right, mid;
if (set->nelem <= 0) if (set->nelem <= 0)
return 0; return 0;
@@ -1390,8 +1387,7 @@ re_node_set_contains (const re_node_set *set, int elem)
} }
static void static void
internal_function re_node_set_remove_at (re_node_set *set, Idx idx)
re_node_set_remove_at (re_node_set *set, int idx)
{ {
if (idx < 0 || idx >= set->nelem) if (idx < 0 || idx >= set->nelem)
return; return;
@@ -1402,38 +1398,42 @@ re_node_set_remove_at (re_node_set *set, int idx)
/* Add the token TOKEN to dfa->nodes, and return the index of the token. /* Add the token TOKEN to dfa->nodes, and return the index of the token.
Or return -1, if an error will be occured. */ Or return -1 if an error occurred. */
static int static Idx
internal_function
re_dfa_add_node (re_dfa_t *dfa, re_token_t token) re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
{ {
int type = token.type;
if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0)) if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
{ {
size_t new_nodes_alloc = dfa->nodes_alloc * 2; size_t new_nodes_alloc = dfa->nodes_alloc * 2;
int *new_nexts, *new_indices; Idx *new_nexts, *new_indices;
re_node_set *new_edests, *new_eclosures; re_node_set *new_edests, *new_eclosures;
re_token_t *new_nodes; re_token_t *new_nodes;
/* Avoid overflows in realloc. */ /* Avoid overflows in realloc. */
const size_t max_object_size = MAX (sizeof (re_token_t), const size_t max_object_size = MAX (sizeof (re_token_t),
MAX (sizeof (re_node_set), MAX (sizeof (re_node_set),
sizeof (int))); sizeof (Idx)));
if (BE (SIZE_MAX / max_object_size < new_nodes_alloc, 0)) if (BE (MIN (IDX_MAX, SIZE_MAX / max_object_size) < new_nodes_alloc, 0))
return -1; return -1;
new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc); new_nodes = re_realloc (dfa->nodes, re_token_t, new_nodes_alloc);
if (BE (new_nodes == NULL, 0)) if (BE (new_nodes == NULL, 0))
return -1; return -1;
dfa->nodes = new_nodes; dfa->nodes = new_nodes;
new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc); new_nexts = re_realloc (dfa->nexts, Idx, new_nodes_alloc);
new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc); new_indices = re_realloc (dfa->org_indices, Idx, new_nodes_alloc);
new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc); new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc); new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
if (BE (new_nexts == NULL || new_indices == NULL if (BE (new_nexts == NULL || new_indices == NULL
|| new_edests == NULL || new_eclosures == NULL, 0)) || new_edests == NULL || new_eclosures == NULL, 0))
return -1; {
re_free (new_nexts);
re_free (new_indices);
re_free (new_edests);
re_free (new_eclosures);
return -1;
}
dfa->nexts = new_nexts; dfa->nexts = new_nexts;
dfa->org_indices = new_indices; dfa->org_indices = new_indices;
dfa->edests = new_edests; dfa->edests = new_edests;
@@ -1444,7 +1444,8 @@ re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
dfa->nodes[dfa->nodes_len].constraint = 0; dfa->nodes[dfa->nodes_len].constraint = 0;
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
dfa->nodes[dfa->nodes_len].accept_mb = dfa->nodes[dfa->nodes_len].accept_mb =
(type == OP_PERIOD && dfa->mb_cur_max > 1) || type == COMPLEX_BRACKET; ((token.type == OP_PERIOD && dfa->mb_cur_max > 1)
|| token.type == COMPLEX_BRACKET);
#endif #endif
dfa->nexts[dfa->nodes_len] = -1; dfa->nexts[dfa->nodes_len] = -1;
re_node_set_init_empty (dfa->edests + dfa->nodes_len); re_node_set_init_empty (dfa->edests + dfa->nodes_len);
@@ -1452,12 +1453,11 @@ re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
return dfa->nodes_len++; return dfa->nodes_len++;
} }
static inline unsigned int static re_hashval_t
internal_function
calc_state_hash (const re_node_set *nodes, unsigned int context) calc_state_hash (const re_node_set *nodes, unsigned int context)
{ {
unsigned int hash = nodes->nelem + context; re_hashval_t hash = nodes->nelem + context;
int i; Idx i;
for (i = 0 ; i < nodes->nelem ; i++) for (i = 0 ; i < nodes->nelem ; i++)
hash += nodes->elems[i]; hash += nodes->elems[i];
return hash; return hash;
@@ -1473,14 +1473,18 @@ calc_state_hash (const re_node_set *nodes, unsigned int context)
optimization. */ optimization. */
static re_dfastate_t * static re_dfastate_t *
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa, re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
const re_node_set *nodes) const re_node_set *nodes)
{ {
unsigned int hash; re_hashval_t hash;
re_dfastate_t *new_state; re_dfastate_t *new_state;
struct re_state_table_entry *spot; struct re_state_table_entry *spot;
int i; Idx i;
#if defined GCC_LINT || defined lint
/* Suppress bogus uninitialized-variable warnings. */
*err = REG_NOERROR;
#endif
if (BE (nodes->nelem == 0, 0)) if (BE (nodes->nelem == 0, 0))
{ {
*err = REG_NOERROR; *err = REG_NOERROR;
@@ -1517,14 +1521,18 @@ re_acquire_state (reg_errcode_t *err, const re_dfa_t *dfa,
optimization. */ optimization. */
static re_dfastate_t * static re_dfastate_t *
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa, re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
const re_node_set *nodes, unsigned int context) const re_node_set *nodes, unsigned int context)
{ {
unsigned int hash; re_hashval_t hash;
re_dfastate_t *new_state; re_dfastate_t *new_state;
struct re_state_table_entry *spot; struct re_state_table_entry *spot;
int i; Idx i;
#if defined GCC_LINT || defined lint
/* Suppress bogus uninitialized-variable warnings. */
*err = REG_NOERROR;
#endif
if (nodes->nelem == 0) if (nodes->nelem == 0)
{ {
*err = REG_NOERROR; *err = REG_NOERROR;
@@ -1541,7 +1549,7 @@ re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
&& re_node_set_compare (state->entrance_nodes, nodes)) && re_node_set_compare (state->entrance_nodes, nodes))
return state; return state;
} }
/* There are no appropriate state in `dfa', create the new one. */ /* There are no appropriate state in 'dfa', create the new one. */
new_state = create_cd_newstate (dfa, nodes, context, hash); new_state = create_cd_newstate (dfa, nodes, context, hash);
if (BE (new_state == NULL, 0)) if (BE (new_state == NULL, 0))
*err = REG_ESPACE; *err = REG_ESPACE;
@@ -1556,11 +1564,11 @@ re_acquire_state_context (reg_errcode_t *err, const re_dfa_t *dfa,
static reg_errcode_t static reg_errcode_t
__attribute_warn_unused_result__ __attribute_warn_unused_result__
register_state (const re_dfa_t *dfa, re_dfastate_t *newstate, register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
unsigned int hash) re_hashval_t hash)
{ {
struct re_state_table_entry *spot; struct re_state_table_entry *spot;
reg_errcode_t err; reg_errcode_t err;
int i; Idx i;
newstate->hash = hash; newstate->hash = hash;
err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem); err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
@@ -1568,16 +1576,16 @@ register_state (const re_dfa_t *dfa, re_dfastate_t *newstate,
return REG_ESPACE; return REG_ESPACE;
for (i = 0; i < newstate->nodes.nelem; i++) for (i = 0; i < newstate->nodes.nelem; i++)
{ {
int elem = newstate->nodes.elems[i]; Idx elem = newstate->nodes.elems[i];
if (!IS_EPSILON_NODE (dfa->nodes[elem].type)) if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
if (re_node_set_insert_last (&newstate->non_eps_nodes, elem) < 0) if (! re_node_set_insert_last (&newstate->non_eps_nodes, elem))
return REG_ESPACE; return REG_ESPACE;
} }
spot = dfa->state_table + (hash & dfa->state_hash_mask); spot = dfa->state_table + (hash & dfa->state_hash_mask);
if (BE (spot->alloc <= spot->num, 0)) if (BE (spot->alloc <= spot->num, 0))
{ {
int new_alloc = 2 * spot->num + 2; Idx new_alloc = 2 * spot->num + 2;
re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *, re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
new_alloc); new_alloc);
if (BE (new_array == NULL, 0)) if (BE (new_array == NULL, 0))
@@ -1605,15 +1613,15 @@ free_state (re_dfastate_t *state)
re_free (state); re_free (state);
} }
/* Create the new state which is independ of contexts. /* Create the new state which is independent of contexts.
Return the new state if succeeded, otherwise return NULL. */ Return the new state if succeeded, otherwise return NULL. */
static re_dfastate_t * static re_dfastate_t *
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes, create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
unsigned int hash) re_hashval_t hash)
{ {
int i; Idx i;
reg_errcode_t err; reg_errcode_t err;
re_dfastate_t *newstate; re_dfastate_t *newstate;
@@ -1659,11 +1667,11 @@ create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
Return the new state if succeeded, otherwise return NULL. */ Return the new state if succeeded, otherwise return NULL. */
static re_dfastate_t * static re_dfastate_t *
internal_function __attribute_warn_unused_result__ __attribute_warn_unused_result__
create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes, create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
unsigned int context, unsigned int hash) unsigned int context, re_hashval_t hash)
{ {
int i, nctx_nodes = 0; Idx i, nctx_nodes = 0;
reg_errcode_t err; reg_errcode_t err;
re_dfastate_t *newstate; re_dfastate_t *newstate;
@@ -1,5 +1,5 @@
/* Extended regular expression matching and search library. /* Extended regular expression matching and search library.
Copyright (C) 2002-2014 Free Software Foundation, Inc. Copyright (C) 2002-2018 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
@@ -15,7 +15,7 @@
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */ <https://www.gnu.org/licenses/>. */
#ifndef _REGEX_INTERNAL_H #ifndef _REGEX_INTERNAL_H
#define _REGEX_INTERNAL_H 1 #define _REGEX_INTERNAL_H 1
@@ -26,35 +26,83 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC #include <langinfo.h>
# include <langinfo.h> #include <locale.h>
#include <wchar.h>
#include <wctype.h>
#include <stdbool.h>
#include <stdint.h>
#ifdef __HAIKU__
typedef unsigned int __re_size_t;
#define _Restrict_
#endif #endif
#if defined HAVE_LOCALE_H || defined _LIBC
# include <locale.h> /* Properties of integers. Although Gnulib has intprops.h, glibc does
#endif without for now. */
#if defined HAVE_WCHAR_H || defined _LIBC #if !defined _LIBC && !defined __HAIKU__
# include <wchar.h> # include "intprops.h"
#endif /* HAVE_WCHAR_H || _LIBC */
#if defined HAVE_WCTYPE_H || defined _LIBC
# include <wctype.h>
#endif /* HAVE_WCTYPE_H || _LIBC */
#if defined HAVE_STDBOOL_H || defined _LIBC
# include <stdbool.h>
#endif /* HAVE_STDBOOL_H || _LIBC */
#if defined HAVE_STDINT_H || defined _LIBC
# include <stdint.h>
#endif /* HAVE_STDINT_H || _LIBC */
#if defined _LIBC
# include <bits/libc-lock.h>
#else #else
# define __libc_lock_define(CLASS,NAME) /* True if the real type T is signed. */
# define __libc_lock_init(NAME) do { } while (0) # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
# define __libc_lock_lock(NAME) do { } while (0)
# define __libc_lock_unlock(NAME) do { } while (0) /* True if adding the nonnegative Idx values A and B would overflow.
If false, set *R to A + B. A, B, and R may be evaluated more than
once, or zero times. Although this is not a full implementation of
Gnulib INT_ADD_WRAPV, it is good enough for glibc regex code.
FIXME: This implementation is a fragile stopgap, and this file would
be simpler and more robust if intprops.h were migrated into glibc. */
# define INT_ADD_WRAPV(a, b, r) \
(IDX_MAX - (a) < (b) ? true : (*(r) = (a) + (b), false))
#endif
#ifdef _LIBC
# include <libc-lock.h>
# define lock_define(name) __libc_lock_define (, name)
# define lock_init(lock) (__libc_lock_init (lock), 0)
# define lock_fini(lock) ((void) 0)
# define lock_lock(lock) __libc_lock_lock (lock)
# define lock_unlock(lock) __libc_lock_unlock (lock)
#elif defined GNULIB_LOCK && !defined USE_UNLOCKED_IO
# include "glthread/lock.h"
/* Use gl_lock_define if empty macro arguments are known to work.
Otherwise, fall back on less-portable substitutes. */
# if ((defined __GNUC__ && !defined __STRICT_ANSI__) \
|| (defined __STDC_VERSION__ && 199901L <= __STDC_VERSION__))
# define lock_define(name) gl_lock_define (, name)
# elif USE_POSIX_THREADS
# define lock_define(name) pthread_mutex_t name;
# elif USE_PTH_THREADS
# define lock_define(name) pth_mutex_t name;
# elif USE_SOLARIS_THREADS
# define lock_define(name) mutex_t name;
# elif USE_WINDOWS_THREADS
# define lock_define(name) gl_lock_t name;
# else
# define lock_define(name)
# endif
# define lock_init(lock) glthread_lock_init (&(lock))
# define lock_fini(lock) glthread_lock_destroy (&(lock))
# define lock_lock(lock) glthread_lock_lock (&(lock))
# define lock_unlock(lock) glthread_lock_unlock (&(lock))
#elif defined GNULIB_PTHREAD && !defined USE_UNLOCKED_IO
# include <pthread.h>
# define lock_define(name) pthread_mutex_t name;
# define lock_init(lock) pthread_mutex_init (&(lock), 0)
# define lock_fini(lock) pthread_mutex_destroy (&(lock))
# define lock_lock(lock) pthread_mutex_lock (&(lock))
# define lock_unlock(lock) pthread_mutex_unlock (&(lock))
#else
# define lock_define(name)
# define lock_init(lock) 0
# define lock_fini(lock) ((void) 0)
/* The 'dfa' avoids an "unused variable 'dfa'" warning from GCC. */
# define lock_lock(lock) ((void) dfa)
# define lock_unlock(lock) ((void) 0)
#endif #endif
/* In case that the system doesn't have isblank(). */ /* In case that the system doesn't have isblank(). */
#if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank #if !defined _LIBC && ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
# define isblank(ch) ((ch) == ' ' || (ch) == '\t') # define isblank(ch) ((ch) == ' ' || (ch) == '\t')
#endif #endif
@@ -62,7 +110,6 @@
# ifndef _RE_DEFINE_LOCALE_FUNCTIONS # ifndef _RE_DEFINE_LOCALE_FUNCTIONS
# define _RE_DEFINE_LOCALE_FUNCTIONS 1 # define _RE_DEFINE_LOCALE_FUNCTIONS 1
# include <locale/localeinfo.h> # include <locale/localeinfo.h>
# include <locale/elem-hash.h>
# include <locale/coll-lookup.h> # include <locale/coll-lookup.h>
# endif # endif
#endif #endif
@@ -76,6 +123,7 @@
__dcgettext (_libc_intl_domainname, msgid, LC_MESSAGES) __dcgettext (_libc_intl_domainname, msgid, LC_MESSAGES)
# endif # endif
#else #else
# undef gettext
# define gettext(msgid) (msgid) # define gettext(msgid) (msgid)
#endif #endif
@@ -85,11 +133,6 @@
# define gettext_noop(String) String # define gettext_noop(String) String
#endif #endif
/* For loser systems without the definition. */
#ifndef SIZE_MAX
# define SIZE_MAX ((size_t) -1)
#endif
#if (defined MB_CUR_MAX && HAVE_WCTYPE_H && HAVE_ISWCTYPE) || _LIBC #if (defined MB_CUR_MAX && HAVE_WCTYPE_H && HAVE_ISWCTYPE) || _LIBC
# define RE_ENABLE_I18N # define RE_ENABLE_I18N
#endif #endif
@@ -100,8 +143,11 @@
# define BE(expr, val) (expr) # define BE(expr, val) (expr)
#endif #endif
/* Number of single byte character. */ /* Number of ASCII characters. */
#define SBC_MAX 256 #define ASCII_CHARS 0x80
/* Number of single byte characters. */
#define SBC_MAX (UCHAR_MAX + 1)
#define COLL_ELEM_LEN_MAX 8 #define COLL_ELEM_LEN_MAX 8
@@ -111,11 +157,15 @@
/* Rename to standard API for using out of glibc. */ /* Rename to standard API for using out of glibc. */
#ifndef _LIBC #ifndef _LIBC
# undef __wctype
# undef __iswctype
# define __wctype wctype # define __wctype wctype
# define __iswalnum iswalnum
# define __iswctype iswctype # define __iswctype iswctype
# define __towlower towlower
# define __towupper towupper
# define __btowc btowc # define __btowc btowc
# define __mbrtowc mbrtowc # define __mbrtowc mbrtowc
# define __mempcpy mempcpy
# define __wcrtomb wcrtomb # define __wcrtomb wcrtomb
# define __regfree regfree # define __regfree regfree
# define attribute_hidden # define attribute_hidden
@@ -125,32 +175,70 @@
# define __attribute__(arg) # define __attribute__(arg)
#endif #endif
extern const char __re_error_msgid[] attribute_hidden; #ifndef SSIZE_MAX
extern const size_t __re_error_msgid_idx[] attribute_hidden; # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
#endif
/* The type of indexes into strings. This is signed, not size_t,
since the API requires indexes to fit in regoff_t anyway, and using
signed integers makes the code a bit smaller and presumably faster.
The traditional GNU regex implementation uses int for indexes.
The POSIX-compatible implementation uses a possibly-wider type.
The name 'Idx' is three letters to minimize the hassle of
reindenting a lot of regex code that formerly used 'int'. */
typedef regoff_t Idx;
#ifdef _REGEX_LARGE_OFFSETS
# define IDX_MAX SSIZE_MAX
#else
# define IDX_MAX INT_MAX
#endif
/* A hash value, suitable for computing hash tables. */
typedef __re_size_t re_hashval_t;
/* An integer used to represent a set of bits. It must be unsigned, /* An integer used to represent a set of bits. It must be unsigned,
and must be at least as wide as unsigned int. */ and must be at least as wide as unsigned int. */
typedef unsigned long int bitset_word_t; typedef unsigned long int bitset_word_t;
/* All bits set in a bitset_word_t. */ /* All bits set in a bitset_word_t. */
#define BITSET_WORD_MAX ULONG_MAX #define BITSET_WORD_MAX ULONG_MAX
/* Number of bits in a bitset_word_t. */
#define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT) /* Number of bits in a bitset_word_t. For portability to hosts with
/* Number of bitset_word_t in a bit_set. */ padding bits, do not use '(sizeof (bitset_word_t) * CHAR_BIT)';
#define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS) instead, deduce it directly from BITSET_WORD_MAX. Avoid
greater-than-32-bit integers and unconditional shifts by more than
31 bits, as they're not portable. */
#if BITSET_WORD_MAX == 0xffffffffUL
# define BITSET_WORD_BITS 32
#elif BITSET_WORD_MAX >> 31 >> 4 == 1
# define BITSET_WORD_BITS 36
#elif BITSET_WORD_MAX >> 31 >> 16 == 1
# define BITSET_WORD_BITS 48
#elif BITSET_WORD_MAX >> 31 >> 28 == 1
# define BITSET_WORD_BITS 60
#elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1
# define BITSET_WORD_BITS 64
#elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1
# define BITSET_WORD_BITS 72
#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1
# define BITSET_WORD_BITS 128
#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1
# define BITSET_WORD_BITS 256
#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1
# define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */
# if BITSET_WORD_BITS <= SBC_MAX
# error "Invalid SBC_MAX"
# endif
#else
# error "Add case for new bitset_word_t size"
#endif
/* Number of bitset_word_t values in a bitset_t. */
#define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
typedef bitset_word_t bitset_t[BITSET_WORDS]; typedef bitset_word_t bitset_t[BITSET_WORDS];
typedef bitset_word_t *re_bitset_ptr_t; typedef bitset_word_t *re_bitset_ptr_t;
typedef const bitset_word_t *re_const_bitset_ptr_t; typedef const bitset_word_t *re_const_bitset_ptr_t;
#define bitset_set(set,i) \
(set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS)
#define bitset_clear(set,i) \
(set[i / BITSET_WORD_BITS] &= ~((bitset_word_t) 1 << i % BITSET_WORD_BITS))
#define bitset_contain(set,i) \
(set[i / BITSET_WORD_BITS] & ((bitset_word_t) 1 << i % BITSET_WORD_BITS))
#define bitset_empty(set) memset (set, '\0', sizeof (bitset_t))
#define bitset_set_all(set) memset (set, '\xff', sizeof (bitset_t))
#define bitset_copy(dest,src) memcpy (dest, src, sizeof (bitset_t))
#define PREV_WORD_CONSTRAINT 0x0001 #define PREV_WORD_CONSTRAINT 0x0001
#define PREV_NOTWORD_CONSTRAINT 0x0002 #define PREV_NOTWORD_CONSTRAINT 0x0002
#define NEXT_WORD_CONSTRAINT 0x0004 #define NEXT_WORD_CONSTRAINT 0x0004
@@ -178,9 +266,9 @@ typedef enum
typedef struct typedef struct
{ {
int alloc; Idx alloc;
int nelem; Idx nelem;
int *elems; Idx *elems;
} re_node_set; } re_node_set;
typedef enum typedef enum
@@ -266,19 +354,19 @@ typedef struct
unsigned int non_match : 1; unsigned int non_match : 1;
/* # of multibyte characters. */ /* # of multibyte characters. */
int nmbchars; Idx nmbchars;
/* # of collating symbols. */ /* # of collating symbols. */
int ncoll_syms; Idx ncoll_syms;
/* # of equivalence classes. */ /* # of equivalence classes. */
int nequiv_classes; Idx nequiv_classes;
/* # of range expressions. */ /* # of range expressions. */
int nranges; Idx nranges;
/* # of character classes. */ /* # of character classes. */
int nchar_classes; Idx nchar_classes;
} re_charset_t; } re_charset_t;
#endif /* RE_ENABLE_I18N */ #endif /* RE_ENABLE_I18N */
@@ -291,10 +379,10 @@ typedef struct
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
re_charset_t *mbcset; /* for COMPLEX_BRACKET */ re_charset_t *mbcset; /* for COMPLEX_BRACKET */
#endif /* RE_ENABLE_I18N */ #endif /* RE_ENABLE_I18N */
int idx; /* for BACK_REF */ Idx idx; /* for BACK_REF */
re_context_type ctx_type; /* for ANCHOR */ re_context_type ctx_type; /* for ANCHOR */
} opr; } opr;
#if __GNUC__ >= 2 #if __GNUC__ >= 2 && !defined __STRICT_ANSI__
re_token_type_t type : 8; re_token_type_t type : 8;
#else #else
re_token_type_t type; re_token_type_t type;
@@ -325,30 +413,30 @@ struct re_string_t
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
/* Store the wide character string which is corresponding to MBS. */ /* Store the wide character string which is corresponding to MBS. */
wint_t *wcs; wint_t *wcs;
int *offsets; Idx *offsets;
mbstate_t cur_state; mbstate_t cur_state;
#endif #endif
/* Index in RAW_MBS. Each character mbs[i] corresponds to /* Index in RAW_MBS. Each character mbs[i] corresponds to
raw_mbs[raw_mbs_idx + i]. */ raw_mbs[raw_mbs_idx + i]. */
int raw_mbs_idx; Idx raw_mbs_idx;
/* The length of the valid characters in the buffers. */ /* The length of the valid characters in the buffers. */
int valid_len; Idx valid_len;
/* The corresponding number of bytes in raw_mbs array. */ /* The corresponding number of bytes in raw_mbs array. */
int valid_raw_len; Idx valid_raw_len;
/* The length of the buffers MBS and WCS. */ /* The length of the buffers MBS and WCS. */
int bufs_len; Idx bufs_len;
/* The index in MBS, which is updated by re_string_fetch_byte. */ /* The index in MBS, which is updated by re_string_fetch_byte. */
int cur_idx; Idx cur_idx;
/* length of RAW_MBS array. */ /* length of RAW_MBS array. */
int raw_len; Idx raw_len;
/* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */ /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
int len; Idx len;
/* End of the buffer may be shorter than its length in the cases such /* End of the buffer may be shorter than its length in the cases such
as re_match_2, re_search_2. Then, we use STOP for end of the buffer as re_match_2, re_search_2. Then, we use STOP for end of the buffer
instead of LEN. */ instead of LEN. */
int raw_stop; Idx raw_stop;
/* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */ /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
int stop; Idx stop;
/* The context of mbs[0]. We store the context independently, since /* The context of mbs[0]. We store the context independently, since
the context of mbs[0] may be different from raw_mbs[0], which is the context of mbs[0] may be different from raw_mbs[0], which is
@@ -358,7 +446,7 @@ struct re_string_t
RE_TRANSLATE_TYPE trans; RE_TRANSLATE_TYPE trans;
/* Copy of re_dfa_t's word_char. */ /* Copy of re_dfa_t's word_char. */
re_const_bitset_ptr_t word_char; re_const_bitset_ptr_t word_char;
/* 1 if REG_ICASE. */ /* true if REG_ICASE. */
unsigned char icase; unsigned char icase;
unsigned char is_utf8; unsigned char is_utf8;
unsigned char map_notascii; unsigned char map_notascii;
@@ -375,29 +463,9 @@ struct re_dfa_t;
typedef struct re_dfa_t re_dfa_t; typedef struct re_dfa_t re_dfa_t;
#ifndef _LIBC #ifndef _LIBC
# ifdef __i386__ # define IS_IN(libc) false
# define internal_function __attribute__ ((regparm (3), stdcall))
# else
# define internal_function
# endif
# define __attribute_warn_unused_result__
#endif #endif
#ifndef NOT_IN_libc
static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
int new_buf_len)
internal_function;
# ifdef RE_ENABLE_I18N
static void build_wcs_buffer (re_string_t *pstr) internal_function;
static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
internal_function;
# endif /* RE_ENABLE_I18N */
static void build_upper_buffer (re_string_t *pstr) internal_function;
static void re_string_translate_buffer (re_string_t *pstr) internal_function;
static unsigned int re_string_context_at (const re_string_t *input, int idx,
int eflags)
internal_function __attribute__ ((pure));
#endif
#define re_string_peek_byte(pstr, offset) \ #define re_string_peek_byte(pstr, offset) \
((pstr)->mbs[(pstr)->cur_idx + offset]) ((pstr)->mbs[(pstr)->cur_idx + offset])
#define re_string_fetch_byte(pstr) \ #define re_string_fetch_byte(pstr) \
@@ -415,7 +483,9 @@ static unsigned int re_string_context_at (const re_string_t *input, int idx,
#define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx)) #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx)) #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
#include <alloca.h> #if defined _LIBC || HAVE_ALLOCA
# include <alloca.h>
#endif
#ifndef _LIBC #ifndef _LIBC
# if HAVE_ALLOCA # if HAVE_ALLOCA
@@ -427,9 +497,24 @@ static unsigned int re_string_context_at (const re_string_t *input, int idx,
# else # else
/* alloca is implemented with malloc, so just use malloc. */ /* alloca is implemented with malloc, so just use malloc. */
# define __libc_use_alloca(n) 0 # define __libc_use_alloca(n) 0
# undef alloca
# define alloca(n) malloc (n)
# endif # endif
#endif #endif
#ifdef _LIBC
# define MALLOC_0_IS_NONNULL 1
#elif !defined MALLOC_0_IS_NONNULL
# define MALLOC_0_IS_NONNULL 0
#endif
#ifndef MAX
# define MAX(a,b) ((a) < (b) ? (b) : (a))
#endif
#ifndef MIN
# define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t))) #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
#define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t))) #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
#define re_free(p) free (p) #define re_free(p) free (p)
@@ -444,9 +529,9 @@ struct bin_tree_t
re_token_t token; re_token_t token;
/* `node_idx' is the index in dfa->nodes, if `type' == 0. /* 'node_idx' is the index in dfa->nodes, if 'type' == 0.
Otherwise `type' indicate the type of this node. */ Otherwise 'type' indicate the type of this node. */
int node_idx; Idx node_idx;
}; };
typedef struct bin_tree_t bin_tree_t; typedef struct bin_tree_t bin_tree_t;
@@ -473,7 +558,7 @@ typedef struct bin_tree_storage_t bin_tree_storage_t;
#define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_') #define IS_WORD_CHAR(ch) (isalnum (ch) || (ch) == '_')
#define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR) #define IS_NEWLINE(ch) ((ch) == NEWLINE_CHAR)
#define IS_WIDE_WORD_CHAR(ch) (iswalnum (ch) || (ch) == L'_') #define IS_WIDE_WORD_CHAR(ch) (__iswalnum (ch) || (ch) == L'_')
#define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR) #define IS_WIDE_NEWLINE(ch) ((ch) == WIDE_NEWLINE_CHAR)
#define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \ #define NOT_SATISFY_PREV_CONSTRAINT(constraint,context) \
@@ -490,7 +575,7 @@ typedef struct bin_tree_storage_t bin_tree_storage_t;
struct re_dfastate_t struct re_dfastate_t
{ {
unsigned int hash; re_hashval_t hash;
re_node_set nodes; re_node_set nodes;
re_node_set non_eps_nodes; re_node_set non_eps_nodes;
re_node_set inveclosure; re_node_set inveclosure;
@@ -498,9 +583,9 @@ struct re_dfastate_t
struct re_dfastate_t **trtable, **word_trtable; struct re_dfastate_t **trtable, **word_trtable;
unsigned int context : 4; unsigned int context : 4;
unsigned int halt : 1; unsigned int halt : 1;
/* If this state can accept `multi byte'. /* If this state can accept "multi byte".
Note that we refer to multibyte characters, and multi character Note that we refer to multibyte characters, and multi character
collating elements as `multi byte'. */ collating elements as "multi byte". */
unsigned int accept_mb : 1; unsigned int accept_mb : 1;
/* If this state has backreference node(s). */ /* If this state has backreference node(s). */
unsigned int has_backref : 1; unsigned int has_backref : 1;
@@ -510,8 +595,8 @@ typedef struct re_dfastate_t re_dfastate_t;
struct re_state_table_entry struct re_state_table_entry
{ {
int num; Idx num;
int alloc; Idx alloc;
re_dfastate_t **array; re_dfastate_t **array;
}; };
@@ -519,8 +604,8 @@ struct re_state_table_entry
typedef struct typedef struct
{ {
int next_idx; Idx next_idx;
int alloc; Idx alloc;
re_dfastate_t **array; re_dfastate_t **array;
} state_array_t; } state_array_t;
@@ -528,8 +613,8 @@ typedef struct
typedef struct typedef struct
{ {
int node; Idx node;
int str_idx; /* The position NODE match at. */ Idx str_idx; /* The position NODE match at. */
state_array_t path; state_array_t path;
} re_sub_match_last_t; } re_sub_match_last_t;
@@ -539,20 +624,20 @@ typedef struct
typedef struct typedef struct
{ {
int str_idx; Idx str_idx;
int node; Idx node;
state_array_t *path; state_array_t *path;
int alasts; /* Allocation size of LASTS. */ Idx alasts; /* Allocation size of LASTS. */
int nlasts; /* The number of LASTS. */ Idx nlasts; /* The number of LASTS. */
re_sub_match_last_t **lasts; re_sub_match_last_t **lasts;
} re_sub_match_top_t; } re_sub_match_top_t;
struct re_backref_cache_entry struct re_backref_cache_entry
{ {
int node; Idx node;
int str_idx; Idx str_idx;
int subexp_from; Idx subexp_from;
int subexp_to; Idx subexp_to;
char more; char more;
char unused; char unused;
unsigned short int eps_reachable_subexps_map; unsigned short int eps_reachable_subexps_map;
@@ -570,18 +655,18 @@ typedef struct
/* EFLAGS of the argument of regexec. */ /* EFLAGS of the argument of regexec. */
int eflags; int eflags;
/* Where the matching ends. */ /* Where the matching ends. */
int match_last; Idx match_last;
int last_node; Idx last_node;
/* The state log used by the matcher. */ /* The state log used by the matcher. */
re_dfastate_t **state_log; re_dfastate_t **state_log;
int state_log_top; Idx state_log_top;
/* Back reference cache. */ /* Back reference cache. */
int nbkref_ents; Idx nbkref_ents;
int abkref_ents; Idx abkref_ents;
struct re_backref_cache_entry *bkref_ents; struct re_backref_cache_entry *bkref_ents;
int max_mb_elem_len; int max_mb_elem_len;
int nsub_tops; Idx nsub_tops;
int asub_tops; Idx asub_tops;
re_sub_match_top_t **sub_tops; re_sub_match_top_t **sub_tops;
} re_match_context_t; } re_match_context_t;
@@ -589,23 +674,23 @@ typedef struct
{ {
re_dfastate_t **sifted_states; re_dfastate_t **sifted_states;
re_dfastate_t **limited_states; re_dfastate_t **limited_states;
int last_node; Idx last_node;
int last_str_idx; Idx last_str_idx;
re_node_set limits; re_node_set limits;
} re_sift_context_t; } re_sift_context_t;
struct re_fail_stack_ent_t struct re_fail_stack_ent_t
{ {
int idx; Idx idx;
int node; Idx node;
regmatch_t *regs; regmatch_t *regs;
re_node_set eps_via_nodes; re_node_set eps_via_nodes;
}; };
struct re_fail_stack_t struct re_fail_stack_t
{ {
int num; Idx num;
int alloc; Idx alloc;
struct re_fail_stack_ent_t *stack; struct re_fail_stack_ent_t *stack;
}; };
@@ -614,8 +699,8 @@ struct re_dfa_t
re_token_t *nodes; re_token_t *nodes;
size_t nodes_alloc; size_t nodes_alloc;
size_t nodes_len; size_t nodes_len;
int *nexts; Idx *nexts;
int *org_indices; Idx *org_indices;
re_node_set *edests; re_node_set *edests;
re_node_set *eclosures; re_node_set *eclosures;
re_node_set *inveclosures; re_node_set *inveclosures;
@@ -629,10 +714,10 @@ struct re_dfa_t
re_bitset_ptr_t sb_char; re_bitset_ptr_t sb_char;
int str_tree_storage_idx; int str_tree_storage_idx;
/* number of subexpressions `re_nsub' is in regex_t. */ /* number of subexpressions 're_nsub' is in regex_t. */
unsigned int state_hash_mask; re_hashval_t state_hash_mask;
int init_node; Idx init_node;
int nbackref; /* The number of backreference in this dfa. */ Idx nbackref; /* The number of backreference in this dfa. */
/* Bitmap expressing which backreference is used. */ /* Bitmap expressing which backreference is used. */
bitset_word_t used_bkref_map; bitset_word_t used_bkref_map;
@@ -649,11 +734,11 @@ struct re_dfa_t
int mb_cur_max; int mb_cur_max;
bitset_t word_char; bitset_t word_char;
reg_syntax_t syntax; reg_syntax_t syntax;
int *subexp_map; Idx *subexp_map;
#ifdef DEBUG #ifdef DEBUG
char* re_str; char* re_str;
#endif #endif
__libc_lock_define (, lock) lock_define (lock)
}; };
#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set)) #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
@@ -684,16 +769,60 @@ typedef struct
} bracket_elem_t; } bracket_elem_t;
/* Inline functions for bitset operation. */ /* Functions for bitset_t operation. */
static void __attribute__ ((unused))
static inline void
bitset_set (bitset_t set, Idx i)
{
set[i / BITSET_WORD_BITS] |= (bitset_word_t) 1 << i % BITSET_WORD_BITS;
}
static inline void
bitset_clear (bitset_t set, Idx i)
{
set[i / BITSET_WORD_BITS] &= ~ ((bitset_word_t) 1 << i % BITSET_WORD_BITS);
}
static inline bool
bitset_contain (const bitset_t set, Idx i)
{
return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
}
static inline void
bitset_empty (bitset_t set)
{
memset (set, '\0', sizeof (bitset_t));
}
static inline void
bitset_set_all (bitset_t set)
{
memset (set, -1, sizeof (bitset_word_t) * (SBC_MAX / BITSET_WORD_BITS));
if (SBC_MAX % BITSET_WORD_BITS != 0)
set[BITSET_WORDS - 1] =
((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
}
static inline void
bitset_copy (bitset_t dest, const bitset_t src)
{
memcpy (dest, src, sizeof (bitset_t));
}
static inline void
bitset_not (bitset_t set) bitset_not (bitset_t set)
{ {
int bitset_i; int bitset_i;
for (bitset_i = 0; bitset_i < BITSET_WORDS; ++bitset_i) for (bitset_i = 0; bitset_i < SBC_MAX / BITSET_WORD_BITS; ++bitset_i)
set[bitset_i] = ~set[bitset_i]; set[bitset_i] = ~set[bitset_i];
if (SBC_MAX % BITSET_WORD_BITS != 0)
set[BITSET_WORDS - 1] =
((((bitset_word_t) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
& ~set[BITSET_WORDS - 1]);
} }
static void __attribute__ ((unused)) static inline void
bitset_merge (bitset_t dest, const bitset_t src) bitset_merge (bitset_t dest, const bitset_t src)
{ {
int bitset_i; int bitset_i;
@@ -701,7 +830,7 @@ bitset_merge (bitset_t dest, const bitset_t src)
dest[bitset_i] |= src[bitset_i]; dest[bitset_i] |= src[bitset_i];
} }
static void __attribute__ ((unused)) static inline void
bitset_mask (bitset_t dest, const bitset_t src) bitset_mask (bitset_t dest, const bitset_t src)
{ {
int bitset_i; int bitset_i;
@@ -710,10 +839,10 @@ bitset_mask (bitset_t dest, const bitset_t src)
} }
#ifdef RE_ENABLE_I18N #ifdef RE_ENABLE_I18N
/* Inline functions for re_string. */ /* Functions for re_string. */
static int static int
internal_function __attribute__ ((pure, unused)) __attribute__ ((pure, unused))
re_string_char_size_at (const re_string_t *pstr, int idx) re_string_char_size_at (const re_string_t *pstr, Idx idx)
{ {
int byte_idx; int byte_idx;
if (pstr->mb_cur_max == 1) if (pstr->mb_cur_max == 1)
@@ -725,24 +854,23 @@ re_string_char_size_at (const re_string_t *pstr, int idx)
} }
static wint_t static wint_t
internal_function __attribute__ ((pure, unused)) __attribute__ ((pure, unused))
re_string_wchar_at (const re_string_t *pstr, int idx) re_string_wchar_at (const re_string_t *pstr, Idx idx)
{ {
if (pstr->mb_cur_max == 1) if (pstr->mb_cur_max == 1)
return (wint_t) pstr->mbs[idx]; return (wint_t) pstr->mbs[idx];
return (wint_t) pstr->wcs[idx]; return (wint_t) pstr->wcs[idx];
} }
# ifndef NOT_IN_libc # ifdef _LIBC
# ifdef _LIBC # include <locale/weight.h>
# include <locale/weight.h> # endif
# endif
static int static int
internal_function __attribute__ ((pure, unused)) __attribute__ ((pure, unused))
re_string_elem_size_at (const re_string_t *pstr, int idx) re_string_elem_size_at (const re_string_t *pstr, Idx idx)
{ {
# ifdef _LIBC # ifdef _LIBC
const unsigned char *p, *extra; const unsigned char *p, *extra;
const int32_t *table, *indirect; const int32_t *table, *indirect;
uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); uint_fast32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
@@ -759,10 +887,34 @@ re_string_elem_size_at (const re_string_t *pstr, int idx)
return p - pstr->mbs - idx; return p - pstr->mbs - idx;
} }
else else
# endif /* _LIBC */ # endif /* _LIBC */
return 1; return 1;
} }
# endif
#endif /* RE_ENABLE_I18N */ #endif /* RE_ENABLE_I18N */
#ifndef __GNUC_PREREQ
# if defined __GNUC__ && defined __GNUC_MINOR__
# define __GNUC_PREREQ(maj, min) \
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
# else
# define __GNUC_PREREQ(maj, min) 0
# endif
#endif
#if __GNUC_PREREQ (3,4)
# undef __attribute_warn_unused_result__
# define __attribute_warn_unused_result__ \
__attribute__ ((__warn_unused_result__))
#else
# define __attribute_warn_unused_result__ /* empty */
#endif
#ifndef FALLTHROUGH
# if __GNUC__ < 7
# define FALLTHROUGH ((void) 0)
# else
# define FALLTHROUGH __attribute__ ((__fallthrough__))
# endif
#endif
#endif /* _REGEX_INTERNAL_H */ #endif /* _REGEX_INTERNAL_H */
File diff suppressed because it is too large Load Diff