Commit 9c1a7cdb by Geoff Simmons

Consolidate code for setting call-scoped match options in the match and

sub methods.
parent eb5f62d3
Pipeline #257 skipped
......@@ -131,9 +131,14 @@
VCL_BOOL notempty, VCL_BOOL notempty_atstart, \
VCL_BOOL no_jit, VCL_BOOL no_utf_check
#define MATCH_SUB_FLAGS_PARAMS \
anchored, notbol, noteol, notempty, notempty_atstart, no_jit, \
no_utf_check, suball, sub_extended, unknown_unset, \
unset_empty \
#define MATCH_FLAGS_PARAMS \
anchored, notbol, noteol, notempty, notempty_atstart, no_jit, \
no_utf_check
no_utf_check, 0, 0, 0, 0
#define SUB_OPTS \
VCL_BOOL suball, VCL_BOOL sub_extended, VCL_BOOL unknown_unset, \
......@@ -409,6 +414,61 @@ get_match_context(VRT_CTX, MATCH_CTX_OPTS, const char * restrict const context,
return mctx;
}
static inline void
set_opt(uint32_t *options, VCL_BOOL vmod_opt, uint32_t pcre2_opt)
{
if (vmod_opt)
*options |= pcre2_opt;
}
static inline void
set_match_flags(uint32_t *options, MATCH_FLAGS, SUB_OPTS)
{
set_opt(options, anchored, PCRE2_ANCHORED);
set_opt(options, notbol, PCRE2_NOTBOL);
set_opt(options, noteol, PCRE2_NOTEOL);
set_opt(options, notempty, PCRE2_NOTEMPTY);
set_opt(options, notempty_atstart, PCRE2_NOTEMPTY_ATSTART);
set_opt(options, no_jit, PCRE2_NO_JIT);
set_opt(options, no_utf_check, PCRE2_NO_UTF_CHECK);
set_opt(options, suball, PCRE2_SUBSTITUTE_GLOBAL);
set_opt(options, sub_extended, PCRE2_SUBSTITUTE_EXTENDED);
set_opt(options, unknown_unset, PCRE2_SUBSTITUTE_UNKNOWN_UNSET);
set_opt(options, unset_empty, PCRE2_SUBSTITUTE_UNSET_EMPTY);
}
static inline struct match_call *
get_match_opts(VRT_CTX, struct vmod_priv *priv, MATCH_CTX_OPTS, MATCH_FLAGS,
SUB_OPTS, const char *context, const char *caller)
{
struct match_call *match_opts;
pcre2_match_context *mctx;
if (priv->priv != NULL) {
CAST_OBJ(match_opts, priv->priv, VMOD_PCRE2_MATCH_CALL_MAGIC);
return match_opts;
}
if ((mctx = get_match_context(ctx, MATCH_CTX_PARAMS, context, caller))
== NULL)
return NULL;
ALLOC_OBJ(match_opts, VMOD_PCRE2_MATCH_CALL_MAGIC);
if (match_opts == NULL) {
VERRNOMEM(ctx, "allocating call-scoped match options in %s%s",
context, caller);
return NULL;
}
match_opts->mctx = mctx;
set_match_flags(&match_opts->match_options, MATCH_SUB_FLAGS_PARAMS);
priv->priv = match_opts;
priv->free = match_call_free;
priv->len = sizeof(*match_opts);
return match_opts;
}
static inline struct task *
get_task(VRT_CTX, struct vmod_priv *priv_task,
const char * const restrict context,
......@@ -455,13 +515,6 @@ get_task(VRT_CTX, struct vmod_priv *priv_task,
}
static inline void
set_opt(uint32_t *options, VCL_BOOL vmod_opt, uint32_t pcre2_opt)
{
if (vmod_opt)
*options |= pcre2_opt;
}
static inline void
set_compile_flags(uint32_t *options, COMPILE_FLAGS)
{
/* XXX check illegal combinations such as never_ucp && ucp ... ? */
......@@ -492,18 +545,6 @@ set_compile_flags(uint32_t *options, COMPILE_FLAGS)
set_opt(options, utf, PCRE2_UTF);
}
static inline void
set_match_flags(uint32_t *options, MATCH_FLAGS)
{
set_opt(options, anchored, PCRE2_ANCHORED);
set_opt(options, notbol, PCRE2_NOTBOL);
set_opt(options, noteol, PCRE2_NOTEOL);
set_opt(options, notempty, PCRE2_NOTEMPTY);
set_opt(options, notempty_atstart, PCRE2_NOTEMPTY_ATSTART);
set_opt(options, no_jit, PCRE2_NO_JIT);
set_opt(options, no_utf_check, PCRE2_NO_UTF_CHECK);
}
static pcre2_code *
compile(VRT_CTX, pcre2_compile_context * restrict const cctx,
VCL_STRING const restrict pattern, uint32_t options, int do_jit,
......@@ -733,25 +774,11 @@ vmod_regex_match(VRT_CTX, struct vmod_pcre2_regex *regex,
/* The match context and options are PRIV_CALL-scoped. */
/* XXX param to decide whether these should be saved for the call */
if (priv_call->priv == NULL) {
pcre2_match_context *mctx;
if ((mctx = get_match_context(ctx, MATCH_CTX_PARAMS,
regex->vcl_name, ".match()"))
== NULL)
return 0;
ALLOC_OBJ(match_opts, VMOD_PCRE2_MATCH_CALL_MAGIC);
AN(match_opts);
match_opts->mctx = mctx;
priv_call->priv = match_opts;
priv_call->free = match_call_free;
set_match_flags(&match_opts->match_options, MATCH_FLAGS_PARAMS);
}
else
CAST_OBJ(match_opts, priv_call->priv,
VMOD_PCRE2_MATCH_CALL_MAGIC);
match_opts = get_match_opts(ctx, priv_call, MATCH_CTX_PARAMS,
MATCH_FLAGS_PARAMS, regex->vcl_name,
".match()");
if (match_opts == NULL)
return 0;
/* The match data block is task-scoped for this object only. */
obj_task = VRT_priv_task(ctx, regex);
......@@ -836,33 +863,11 @@ vmod_regex_sub(VRT_CTX, struct vmod_pcre2_regex *regex,
return NULL;
}
if (priv_call->priv == NULL) {
pcre2_match_context *mctx;
if ((mctx = get_match_context(ctx, MATCH_CTX_PARAMS,
regex->vcl_name, ".sub()"))
== NULL)
return NULL;
ALLOC_OBJ(match_opts, VMOD_PCRE2_MATCH_CALL_MAGIC);
AN(match_opts);
match_opts->mctx = mctx;
priv_call->priv = match_opts;
priv_call->free = match_call_free;
set_match_flags(&match_opts->match_options, MATCH_FLAGS_PARAMS);
set_opt(&match_opts->match_options, suball,
PCRE2_SUBSTITUTE_GLOBAL);
set_opt(&match_opts->match_options, sub_extended,
PCRE2_SUBSTITUTE_EXTENDED);
set_opt(&match_opts->match_options, unknown_unset,
PCRE2_SUBSTITUTE_UNKNOWN_UNSET);
set_opt(&match_opts->match_options, unset_empty,
PCRE2_SUBSTITUTE_UNSET_EMPTY);
}
else
CAST_OBJ(match_opts, priv_call->priv,
VMOD_PCRE2_MATCH_CALL_MAGIC);
match_opts = get_match_opts(ctx, priv_call, MATCH_CTX_PARAMS,
MATCH_SUB_FLAGS_PARAMS, regex->vcl_name,
".match()");
if (match_opts == NULL)
return 0;
/* XXX mdata in PRIV_CALL? */
if ((match_task = get_task(ctx, priv_task, regex->vcl_name, ".sub()"))
......
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