提交 ad47d45e 编写于 作者: R Rich Felker

upgrade to latest upstream TRE regex code (0.8.0)

the main practical results of this change are
1. the regex code is no longer subject to LGPL; it's now 2-clause BSD
2. most (all?) popular nonstandard regex extensions are supported

I hesitate to call this a "sync" since both the old and new code are
heavily modified. in one sense, the old code was "more severely"
modified, in that it was actively hostile to non-strictly-conforming
expressions. on the other hand, the new code has eliminated the
useless translation of the entire regex string to wchar_t prior to
compiling, and now only converts multibyte character literals as
needed.

in the future i may use this modified TRE as a basis for writing the
long-planned new regex engine that will avoid multibyte-to-wide
character conversion entirely by compiling multibyte bracket
expressions specific to UTF-8.
上级 baa43bca
此差异已折叠。
/*
regerror.c - POSIX regerror() implementation for TRE.
Copyright (c) 2001-2006 Ville Laurikari <vl@iki.fi>.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h> #include <string.h>
#include <regex.h> #include <regex.h>
#include <stdio.h>
/* Error message strings for error codes listed in `regex.h'. This list /* Error message strings for error codes listed in `regex.h'. This list
needs to be in sync with the codes listed there, naturally. */ needs to be in sync with the codes listed there, naturally. */
...@@ -28,7 +8,7 @@ ...@@ -28,7 +8,7 @@
/* Converted to single string by Rich Felker to remove the need for /* Converted to single string by Rich Felker to remove the need for
* data relocations at runtime, 27 Feb 2006. */ * data relocations at runtime, 27 Feb 2006. */
static const char tre_error_messages[] = { static const char messages[] = {
"No error\0" "No error\0"
"No match\0" "No match\0"
"Invalid regexp\0" "Invalid regexp\0"
...@@ -43,33 +23,13 @@ static const char tre_error_messages[] = { ...@@ -43,33 +23,13 @@ static const char tre_error_messages[] = {
"Invalid character range\0" "Invalid character range\0"
"Out of memory\0" "Out of memory\0"
"XXX\0" "XXX\0"
"\0Unknown error"
}; };
size_t size_t regerror(int e, const regex_t *preg, char *buf, size_t size)
regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
{ {
const char *err; const char *s;
size_t err_len; for (s=messages; e && *s; e--, e+=strlen(s)+1);
if (!*s) s++;
if (errcode >= 0 && errcode <= REG_BADRPT) return 1+snprintf(buf, size, "%s", s);
for (err=tre_error_messages; errcode; errcode--, err+=strlen(err)+1);
else
err = "Unknown error";
err_len = strlen(err) + 1;
if (errbuf_size > 0 && errbuf != NULL)
{
if (err_len > errbuf_size)
{
memcpy(errbuf, err, errbuf_size - 1);
errbuf[errbuf_size - 1] = '\0';
}
else
{
strcpy(errbuf, err);
}
}
return err_len;
} }
/* EOF */
此差异已折叠。
/* /*
tre-mem.c - TRE memory allocator tre-mem.c - TRE memory allocator
Copyright (c) 2001-2006 Ville Laurikari <vl@iki.fi> Copyright (c) 2001-2009 Ville Laurikari <vl@iki.fi>
All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public Redistribution and use in source and binary forms, with or without
License as published by the Free Software Foundation; either modification, are permitted provided that the following conditions
version 2.1 of the License, or (at your option) any later version. are met:
This library is distributed in the hope that it will be useful, 1. Redistributions of source code must retain the above copyright
but WITHOUT ANY WARRANTY; without even the implied warranty of notice, this list of conditions and the following disclaimer.
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
You should have received a copy of the GNU Lesser General Public documentation and/or other materials provided with the distribution.
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
...@@ -31,6 +41,12 @@ ...@@ -31,6 +41,12 @@
#include "tre.h" #include "tre.h"
/*
This memory allocator is for allocating small memory blocks efficiently
in terms of memory overhead and execution speed. The allocated blocks
cannot be freed individually, only all at once. There can be multiple
allocators, though.
*/
/* Returns a new memory allocator or NULL if out of memory. */ /* Returns a new memory allocator or NULL if out of memory. */
tre_mem_t tre_mem_t
...@@ -77,24 +93,9 @@ tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block, ...@@ -77,24 +93,9 @@ tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
if (mem->failed) if (mem->failed)
{ {
DPRINT(("tre_mem_alloc: oops, called after failure?!\n"));
return NULL; return NULL;
} }
#ifdef MALLOC_DEBUGGING
if (!provided)
{
ptr = xmalloc(1);
if (ptr == NULL)
{
DPRINT(("tre_mem_alloc: xmalloc forced failure\n"));
mem->failed = 1;
return NULL;
}
xfree(ptr);
}
#endif /* MALLOC_DEBUGGING */
if (mem->n < size) if (mem->n < size)
{ {
/* We need more memory than is available in the current block. /* We need more memory than is available in the current block.
...@@ -102,10 +103,8 @@ tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block, ...@@ -102,10 +103,8 @@ tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
tre_list_t *l; tre_list_t *l;
if (provided) if (provided)
{ {
DPRINT(("tre_mem_alloc: using provided block\n"));
if (provided_block == NULL) if (provided_block == NULL)
{ {
DPRINT(("tre_mem_alloc: provided block was NULL\n"));
mem->failed = 1; mem->failed = 1;
return NULL; return NULL;
} }
...@@ -119,8 +118,6 @@ tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block, ...@@ -119,8 +118,6 @@ tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
block_size = size * 8; block_size = size * 8;
else else
block_size = TRE_MEM_BLOCK_SIZE; block_size = TRE_MEM_BLOCK_SIZE;
DPRINT(("tre_mem_alloc: allocating new %d byte block\n",
block_size));
l = xmalloc(sizeof(*l)); l = xmalloc(sizeof(*l));
if (l == NULL) if (l == NULL)
{ {
...@@ -159,5 +156,3 @@ tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block, ...@@ -159,5 +156,3 @@ tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
return ptr; return ptr;
} }
/* EOF */
/* /*
tre-internal.h - TRE internal definitions tre-internal.h - TRE internal definitions
Copyright (c) 2001-2006 Ville Laurikari <vl@iki.fi>. Copyright (c) 2001-2009 Ville Laurikari <vl@iki.fi>
All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public Redistribution and use in source and binary forms, with or without
License as published by the Free Software Foundation; either modification, are permitted provided that the following conditions
version 2.1 of the License, or (at your option) any later version. are met:
This library is distributed in the hope that it will be useful, 1. Redistributions of source code must retain the above copyright
but WITHOUT ANY WARRANTY; without even the implied warranty of notice, this list of conditions and the following disclaimer.
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
You should have received a copy of the GNU Lesser General Public documentation and/or other materials provided with the distribution.
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
...@@ -23,12 +33,7 @@ ...@@ -23,12 +33,7 @@
#include <wchar.h> #include <wchar.h>
#include <wctype.h> #include <wctype.h>
#define TRE_MULTIBYTE 1
#undef TRE_MBSTATE #undef TRE_MBSTATE
#define TRE_WCHAR 1
#define TRE_USE_SYSTEM_WCTYPE 1
#define HAVE_WCSTOMBS 1
#define TRE_MB_CUR_MAX MB_CUR_MAX
#define NDEBUG #define NDEBUG
...@@ -37,33 +42,16 @@ typedef int reg_errcode_t; ...@@ -37,33 +42,16 @@ typedef int reg_errcode_t;
typedef wchar_t tre_char_t; typedef wchar_t tre_char_t;
#ifdef TRE_DEBUG
#include <stdio.h>
#define DPRINT(msg) do {printf msg; fflush(stdout);} while(0)
#else /* !TRE_DEBUG */
#define DPRINT(msg) do { } while(0) #define DPRINT(msg) do { } while(0)
#endif /* !TRE_DEBUG */
#define elementsof(x) ( sizeof(x) / sizeof(x[0]) ) #define elementsof(x) ( sizeof(x) / sizeof(x[0]) )
#if 1
int __mbtowc(wchar_t *, const char *);
#define tre_mbrtowc(pwc, s, n, ps) (__mbtowc((pwc), (s)))
#else
#define tre_mbrtowc(pwc, s, n, ps) (mbtowc((pwc), (s), (n))) #define tre_mbrtowc(pwc, s, n, ps) (mbtowc((pwc), (s), (n)))
#endif
/* Wide characters. */ /* Wide characters. */
typedef wint_t tre_cint_t; typedef wint_t tre_cint_t;
#define TRE_CHAR_MAX WCHAR_MAX #define TRE_CHAR_MAX WCHAR_MAX
#ifdef TRE_MULTIBYTE
#define TRE_MB_CUR_MAX MB_CUR_MAX
#else /* !TRE_MULTIBYTE */
#define TRE_MB_CUR_MAX 1
#endif /* !TRE_MULTIBYTE */
#define tre_isalnum iswalnum #define tre_isalnum iswalnum
#define tre_isalpha iswalpha #define tre_isalpha iswalpha
#define tre_isblank iswblank #define tre_isblank iswblank
...@@ -98,9 +86,6 @@ typedef wctype_t tre_ctype_t; ...@@ -98,9 +86,6 @@ typedef wctype_t tre_ctype_t;
#define MAX(a, b) (((a) >= (b)) ? (a) : (b)) #define MAX(a, b) (((a) >= (b)) ? (a) : (b))
#define MIN(a, b) (((a) <= (b)) ? (a) : (b)) #define MIN(a, b) (((a) <= (b)) ? (a) : (b))
/* Define STRF to the correct printf formatter for strings. */
#define STRF "ls"
/* TNFA transition type. A TNFA state is an array of transitions, /* TNFA transition type. A TNFA state is an array of transitions,
the terminator is a transition with NULL `state'. */ the terminator is a transition with NULL `state'. */
typedef struct tnfa_transition tre_tnfa_transition_t; typedef struct tnfa_transition tre_tnfa_transition_t;
...@@ -170,42 +155,21 @@ struct tnfa { ...@@ -170,42 +155,21 @@ struct tnfa {
tre_tnfa_transition_t *initial; tre_tnfa_transition_t *initial;
tre_tnfa_transition_t *final; tre_tnfa_transition_t *final;
tre_submatch_data_t *submatch_data; tre_submatch_data_t *submatch_data;
char *firstpos_chars;
int first_char;
unsigned int num_submatches; unsigned int num_submatches;
tre_tag_direction_t *tag_directions; tre_tag_direction_t *tag_directions;
int *minimal_tags;
int num_tags; int num_tags;
int num_minimals;
int end_tag; int end_tag;
int num_states; int num_states;
int cflags; int cflags;
int have_backrefs; int have_backrefs;
int have_approx;
int params_depth;
}; };
#if 0
static int
tre_compile(regex_t *preg, const tre_char_t *regex, size_t n, int cflags);
static void
tre_free(regex_t *preg);
static void
tre_fill_pmatch(size_t nmatch, regmatch_t pmatch[], int cflags,
const tre_tnfa_t *tnfa, int *tags, int match_eo);
static reg_errcode_t
tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string, int len,
tre_str_type_t type, int *match_tags, int eflags,
int *match_end_ofs);
static reg_errcode_t
tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string, int len,
tre_str_type_t type, int *match_tags, int eflags,
int *match_end_ofs);
static reg_errcode_t
tre_tnfa_run_backtrack(const tre_tnfa_t *tnfa, const void *string,
int len, tre_str_type_t type, int *match_tags,
int eflags, int *match_end_ofs);
#endif
/* from tre-mem.h: */ /* from tre-mem.h: */
#define TRE_MEM_BLOCK_SIZE 1024 #define TRE_MEM_BLOCK_SIZE 1024
...@@ -266,4 +230,3 @@ void tre_mem_destroy(tre_mem_t mem); ...@@ -266,4 +230,3 @@ void tre_mem_destroy(tre_mem_t mem);
#define xfree free #define xfree free
#define xrealloc realloc #define xrealloc realloc
/* EOF */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册