Commit 025f5654 authored by Geoff Simmons's avatar Geoff Simmons

compute the length of the subject string only once in match()

parent 54295ce5
...@@ -151,7 +151,7 @@ VCL_BOOL ...@@ -151,7 +151,7 @@ VCL_BOOL
vmod_regex_match(const struct vrt_ctx *ctx, struct vmod_re2_regex *re, vmod_regex_match(const struct vrt_ctx *ctx, struct vmod_re2_regex *re,
VCL_STRING subject) VCL_STRING subject)
{ {
int match = 0, ngroups = 0; int match = 0, ngroups = 0, len;
const char *err; const char *err;
char *text = (void *) subject; char *text = (void *) subject;
void *group = NULL; void *group = NULL;
...@@ -165,9 +165,10 @@ vmod_regex_match(const struct vrt_ctx *ctx, struct vmod_re2_regex *re, ...@@ -165,9 +165,10 @@ vmod_regex_match(const struct vrt_ctx *ctx, struct vmod_re2_regex *re,
AZ(pthread_setspecific(re->matchk, match_failed)); AZ(pthread_setspecific(re->matchk, match_failed));
len = strlen(subject);
if (! re->never_capture) { if (! re->never_capture) {
ngroups = re->ngroups + 1; ngroups = re->ngroups + 1;
if ((text = WS_Copy(ctx->ws, subject, -1)) == NULL) { if ((text = WS_Copy(ctx->ws, subject, len + 1)) == NULL) {
VERR(ctx, ERR_PREFIX "insufficient workspace to copy " VERR(ctx, ERR_PREFIX "insufficient workspace to copy "
"subject", re->vcl_name, subject); "subject", re->vcl_name, subject);
return 0; return 0;
...@@ -181,7 +182,7 @@ vmod_regex_match(const struct vrt_ctx *ctx, struct vmod_re2_regex *re, ...@@ -181,7 +182,7 @@ vmod_regex_match(const struct vrt_ctx *ctx, struct vmod_re2_regex *re,
} }
} }
if ((err = vre2_match(re->vre2, text, &match, ngroups, group)) if ((err = vre2_match(re->vre2, text, len, &match, ngroups, group))
!= NULL) { != NULL) {
VERR(ctx, ERR_PREFIX "%s", re->vcl_name, subject, err); VERR(ctx, ERR_PREFIX "%s", re->vcl_name, subject, err);
WS_Reset(ctx->ws, text); WS_Reset(ctx->ws, text);
......
...@@ -56,10 +56,10 @@ vre2::~vre2() { ...@@ -56,10 +56,10 @@ vre2::~vre2() {
} }
inline bool inline bool
vre2::match(const char *subject, const int ngroups, StringPiece* groups) const vre2::match(const char *subject, const size_t len, const int ngroups,
StringPiece* groups) const
{ {
return re_->Match(subject, 0, strlen(subject), RE2::UNANCHORED, return re_->Match(subject, 0, len, RE2::UNANCHORED, groups, ngroups);
groups, ngroups);
} }
inline const int inline const int
...@@ -106,12 +106,12 @@ vre2_matchsz(void) ...@@ -106,12 +106,12 @@ vre2_matchsz(void)
} }
const char * const char *
vre2_match(vre2 *vre2, const char * const subject, int * const match, vre2_match(vre2 *vre2, const char * const subject, const size_t len,
const int ngroups, void * const group) int * const match, const int ngroups, void * const group)
{ {
try { try {
StringPiece* g = reinterpret_cast<StringPiece *>(group); StringPiece* g = reinterpret_cast<StringPiece *>(group);
*match = vre2->match(subject, ngroups, g); *match = vre2->match(subject, len, ngroups, g);
return NULL; return NULL;
} }
CATCHALL CATCHALL
......
...@@ -43,7 +43,8 @@ private: ...@@ -43,7 +43,8 @@ private:
public: public:
vre2(const char *pattern, RE2::Options * const opt); vre2(const char *pattern, RE2::Options * const opt);
virtual ~vre2(); virtual ~vre2();
bool match(const char *subject, int ngroups, StringPiece* groups) const; bool match(const char *subject, size_t len, int ngroups,
StringPiece* groups) const;
const int ngroups() const; const int ngroups() const;
}; };
#else #else
...@@ -65,7 +66,7 @@ extern "C" { ...@@ -65,7 +66,7 @@ extern "C" {
const size_t vre2_matchsz(void); const size_t vre2_matchsz(void);
const char *vre2_ngroups(vre2 *vre2, int * const ngroups); const char *vre2_ngroups(vre2 *vre2, int * const ngroups);
const char *vre2_match(vre2 *vre2, const char * const subject, const char *vre2_match(vre2 *vre2, const char * const subject,
int * const match, int ngroups, size_t len, int * const match, int ngroups,
void * const group); void * const group);
const char *vre2_capture(void *group, int refnum, const char *vre2_capture(void *group, int refnum,
const char ** const capture, int * const len); const char ** const capture, int * const len);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment