Commit 3d9d5c5b authored by Geoff Simmons's avatar Geoff Simmons

Reduce code duplication for set.string() and .backend().

parent b0bf452d
Pipeline #336 skipped
...@@ -810,76 +810,58 @@ vmod_set_nmatches(VRT_CTX, struct vmod_re2_set *set) ...@@ -810,76 +810,58 @@ vmod_set_nmatches(VRT_CTX, struct vmod_re2_set *set)
return task->nmatches; return task->nmatches;
} }
VCL_STRING static int
vmod_set_string(VRT_CTX, struct vmod_re2_set *set, VCL_INT n) get_match_idx(VRT_CTX, struct vmod_re2_set *set, VCL_INT n, const char *method)
{ {
struct task_set_match *task; struct task_set_match *task;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(set, VMOD_RE2_SET_MAGIC); CHECK_OBJ_NOTNULL(set, VMOD_RE2_SET_MAGIC);
if (n > set->npatterns) { if (n > set->npatterns) {
VERR(ctx, "%s.string(%lld): set has %d patterns", set->vcl_name, VERR(ctx, "%s.%s(%lld): set has %d patterns", set->vcl_name,
n, set->npatterns); method, n, set->npatterns);
return NULL; return -1;
} }
if (n > 0) if (n > 0)
return set->string[n - 1]; return n - 1;
if ((task = get_task_data(ctx, set)) == NULL) { if ((task = get_task_data(ctx, set)) == NULL) {
VERR(ctx, "%s.string() called without prior match", VERR(ctx, "%s.%s() called without prior match", set->vcl_name,
set->vcl_name); method);
return NULL; return -1;
} }
if (task->nmatches == 0) { if (task->nmatches == 0) {
VERR(ctx, "%s.string(%lld): previous match was unsuccessful", VERR(ctx, "%s.%s(%lld): previous match was unsuccessful",
set->vcl_name, n); set->vcl_name, method, n);
return NULL; return -1;
} }
if (task->nmatches > 1) { if (task->nmatches > 1) {
VERR(ctx, "%s.string(%lld): %d successful matches", VERR(ctx, "%s.%s(%lld): %d successful matches", set->vcl_name,
set->vcl_name, n, task->nmatches); method, n, task->nmatches);
return NULL; return -1;
} }
WS_Assert_Allocated(ctx->ws, task->matches, WS_Assert_Allocated(ctx->ws, task->matches,
task->nmatches * sizeof(int)); task->nmatches * sizeof(int));
return set->string[task->matches[0]]; return task->matches[0];
} }
VCL_BACKEND VCL_STRING
vmod_set_backend(VRT_CTX, struct vmod_re2_set *set, VCL_INT n) vmod_set_string(VRT_CTX, struct vmod_re2_set *set, VCL_INT n)
{ {
struct task_set_match *task; int idx = get_match_idx(ctx, set, n, "string");
if (idx < 0)
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(set, VMOD_RE2_SET_MAGIC);
if (n > set->npatterns) {
VERR(ctx, "%s.backend(%lld): set has %d patterns",
set->vcl_name, n, set->npatterns);
return NULL;
}
if (n > 0)
return set->backend[n - 1];
if ((task = get_task_data(ctx, set)) == NULL) {
VERR(ctx, "%s.backend() called without prior match",
set->vcl_name);
return NULL; return NULL;
} return set->string[idx];
}
if (task->nmatches == 0) { VCL_BACKEND
VERR(ctx, "%s.backend(%lld): previous match was unsuccessful", vmod_set_backend(VRT_CTX, struct vmod_re2_set *set, VCL_INT n)
set->vcl_name, n); {
return NULL; int idx = get_match_idx(ctx, set, n, "backend");
} if (idx < 0)
if (task->nmatches > 1) {
VERR(ctx, "%s.backend(%lld): %d successful matches",
set->vcl_name, n, task->nmatches);
return NULL; return NULL;
} return set->backend[idx];
WS_Assert_Allocated(ctx->ws, task->matches,
task->nmatches * sizeof(int));
return set->backend[task->matches[0]];
} }
/* Regex function interface */ /* Regex function interface */
......
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