MINOR: regex: Use native PCRE API.
The pcreposix layer (in the pcre projetc) execute strlen to find thlength of the string. When we are using the function "regex_exex*2", the length is used to add a final \0, when pcreposix is executed a strlen is executed to compute the length. If we are using a native PCRE api, the length is provided as an argument, and these operations disappear. This is useful because PCRE regex are more used than POSIC regex.
This commit is contained in:
parent
c9c2daf283
commit
26202760a4
|
@ -35,14 +35,12 @@
|
|||
|
||||
struct my_regex {
|
||||
#ifdef USE_PCRE
|
||||
pcre *reg;
|
||||
pcre_extra *extra;
|
||||
#ifdef USE_PCRE_JIT
|
||||
#ifndef PCRE_CONFIG_JIT
|
||||
#error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT."
|
||||
#endif
|
||||
pcre *reg;
|
||||
pcre_extra *extra;
|
||||
#else /* no PCRE_JIT */
|
||||
regex_t regex;
|
||||
#endif
|
||||
#else /* no PCRE */
|
||||
regex_t regex;
|
||||
|
@ -87,7 +85,7 @@ const char *chain_regex(struct hdr_exp **head, struct my_regex *preg,
|
|||
/* If the function doesn't match, it returns false, else it returns true.
|
||||
*/
|
||||
static inline int regex_exec(const struct my_regex *preg, char *subject) {
|
||||
#ifdef USE_PCRE_JIT
|
||||
#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
|
||||
if (pcre_exec(preg->reg, preg->extra, subject, strlen(subject), 0, 0, NULL, 0) < 0)
|
||||
return 0;
|
||||
return 1;
|
||||
|
@ -107,7 +105,7 @@ static inline int regex_exec(const struct my_regex *preg, char *subject) {
|
|||
* If the function doesn't match, it returns false, else it returns true.
|
||||
*/
|
||||
static inline int regex_exec2(const struct my_regex *preg, char *subject, int length) {
|
||||
#ifdef USE_PCRE_JIT
|
||||
#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
|
||||
if (pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0) < 0)
|
||||
return 0;
|
||||
return 1;
|
||||
|
@ -129,9 +127,11 @@ int regex_exec_match2(const struct my_regex *preg, char *subject, int length,
|
|||
size_t nmatch, regmatch_t pmatch[]);
|
||||
|
||||
static inline void regex_free(struct my_regex *preg) {
|
||||
#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
|
||||
pcre_free(preg->reg);
|
||||
#ifdef USE_PCRE_JIT
|
||||
pcre_free_study(preg->extra);
|
||||
pcre_free(preg->reg);
|
||||
#endif
|
||||
#else
|
||||
regfree(&preg->regex);
|
||||
#endif
|
||||
|
|
10
src/regex.c
10
src/regex.c
|
@ -156,7 +156,7 @@ const char *chain_regex(struct hdr_exp **head, struct my_regex *preg,
|
|||
*/
|
||||
int regex_exec_match(const struct my_regex *preg, const char *subject,
|
||||
size_t nmatch, regmatch_t pmatch[]) {
|
||||
#ifdef USE_PCRE_JIT
|
||||
#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
|
||||
int ret;
|
||||
int matches[MAX_MATCH * 3];
|
||||
int enmatch;
|
||||
|
@ -216,7 +216,7 @@ int regex_exec_match(const struct my_regex *preg, const char *subject,
|
|||
*/
|
||||
int regex_exec_match2(const struct my_regex *preg, char *subject, int length,
|
||||
size_t nmatch, regmatch_t pmatch[]) {
|
||||
#ifdef USE_PCRE_JIT
|
||||
#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
|
||||
int ret;
|
||||
int matches[MAX_MATCH * 3];
|
||||
int enmatch;
|
||||
|
@ -272,7 +272,7 @@ int regex_exec_match2(const struct my_regex *preg, char *subject, int length,
|
|||
|
||||
int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err)
|
||||
{
|
||||
#ifdef USE_PCRE_JIT
|
||||
#if defined(USE_PCRE) || defined(USE_PCRE_JIT)
|
||||
int flags = 0;
|
||||
const char *error;
|
||||
int erroffset;
|
||||
|
@ -288,12 +288,16 @@ int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **
|
|||
return 0;
|
||||
}
|
||||
|
||||
#ifdef USE_PCRE_JIT
|
||||
regex->extra = pcre_study(regex->reg, PCRE_STUDY_JIT_COMPILE, &error);
|
||||
if (!regex->extra) {
|
||||
pcre_free(regex->reg);
|
||||
memprintf(err, "failed to compile regex '%s' (error=%s)", str, error);
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
regex->extra = NULL;
|
||||
#endif
|
||||
#else
|
||||
int flags = REG_EXTENDED;
|
||||
|
||||
|
|
Loading…
Reference in New Issue