Commit 47ac7eca authored by Geoff Simmons's avatar Geoff Simmons

Add the set.which() method.

parent 2cc6a0a9
Pipeline #347 skipped
......@@ -975,6 +975,15 @@ Example::
+ " patterns from the set");
}
.. _func_set.which:
set.which
---------
::
INT set.which(ENUM {FIRST,LAST} select=0)
.. _func_set.string:
set.string
......
# looks like -*- vcl -*-
varnishtest "set .string() and .backend() methods"
varnishtest "set .which(), .string() and .backend() methods"
varnish v1 -vcl {
import ${vmod_re2};
......@@ -25,22 +25,29 @@ varnish v1 -vcl {
set resp.http.s-str-1 = s.string(1);
set resp.http.s-str-2 = s.string(2);
set resp.http.s-before-match = s.string();
set resp.http.s-which-b4-match = s.which();
set resp.http.s-foo-match = s.match("foo");
set resp.http.s-foo-n = s.nmatches();
set resp.http.s-foo-which = s.which();
set resp.http.s-foo-str = s.string();
set resp.http.s-bar-match = s.match("bar");
set resp.http.s-bar-n = s.nmatches();
set resp.http.s-bar-which = s.which();
set resp.http.s-bar-str = s.string();
set resp.http.s-bar-str-0 = s.string(0);
set resp.http.s-bar-str-1 = s.string(-1);
set resp.http.s-fail-match = s.match("fail");
set resp.http.s-fail-n = s.nmatches();
set resp.http.s-fail-which = s.which();
set resp.http.s-fail-str = s.string();
set resp.http.s-many-match = s.match("foobar");
set resp.http.s-many-n = s.nmatches();
set resp.http.s-many-str = s.string();
set resp.http.s-many-first = s.string(select=FIRST);
set resp.http.s-many-last = s.string(select=LAST);
set resp.http.s-which-many = s.which();
set resp.http.s-which-first = s.which(select=FIRST);
set resp.http.s-which-last = s.which(select=LAST);
set resp.http.s-outofrange = s.string(3);
set resp.http.n-str-1 = n.string(1);
......@@ -60,22 +67,29 @@ client c1 {
expect resp.http.s-str-1 == "baz"
expect resp.http.s-str-2 == "quux"
expect resp.http.s-before-match == ""
expect resp.http.s-which-b4-match == "0"
expect resp.http.s-foo-match == "true"
expect resp.http.s-foo-n == 1
expect resp.http.s-foo-which == 1
expect resp.http.s-foo-str == "baz"
expect resp.http.s-bar-match == "true"
expect resp.http.s-bar-n == 1
expect resp.http.s-bar-which == 2
expect resp.http.s-bar-str == "quux"
expect resp.http.s-bar-str-0 == resp.http.s-bar-str
expect resp.http.s-bar-str-1 == resp.http.s-bar-str
expect resp.http.s-fail-match == "false"
expect resp.http.s-fail-n == 0
expect resp.http.s-fail-which == 0
expect resp.http.s-fail-str == ""
expect resp.http.s-many-match == "true"
expect resp.http.s-many-n == 2
expect resp.http.s-many-str == ""
expect resp.http.s-many-first == "baz"
expect resp.http.s-many-last == "quux"
expect resp.http.s-which-many == 0
expect resp.http.s-which-first == 1
expect resp.http.s-which-last == 2
expect resp.http.s-outofrange == ""
expect resp.http.n-str-1 == ""
expect resp.http.n-str-2 == ""
......@@ -89,8 +103,11 @@ logexpect l1 -v v1 -d 1 -g vxid -q "VCL_Error" {
expect 0 * Begin req
expect * = VCL_Error "^vmod re2 error: s.string.. called without prior match$"
expect * = VCL_Error "^vmod re2 error: s.which.. called without prior match$"
expect * = VCL_Error "^vmod re2 error: s.which.0.: previous match was unsuccessful$"
expect * = VCL_Error "^vmod re2 error: s.string.0.: previous match was unsuccessful$"
expect * = VCL_Error "^vmod re2 error: s.string.0.: 2 successful matches$"
expect * = VCL_Error "^vmod re2 error: s.which.0.: 2 successful matches$"
expect * = VCL_Error "^vmod re2 error: s.string.3.: set has 2 patterns$"
expect * = VCL_Error "^vmod re2 error: n.string.1.: No strings were set for n$"
expect * = VCL_Error "^vmod re2 error: n.string.2.: No strings were set for n$"
......
......@@ -860,6 +860,14 @@ get_match_idx(VRT_CTX, struct vmod_re2_set *set, VCL_INT n, VCL_ENUM selects,
return task->matches[idx];
}
VCL_INT
vmod_set_which(VRT_CTX, struct vmod_re2_set *set, VCL_ENUM selects)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(set, VMOD_RE2_SET_MAGIC);
return get_match_idx(ctx, set, 0, selects, "which") + 1;
}
VCL_STRING
vmod_set_string(VRT_CTX, struct vmod_re2_set *set, VCL_INT n, VCL_ENUM selects)
{
......
......@@ -850,6 +850,8 @@ Example::
+ " patterns from the set");
}
$Method INT .which(ENUM {FIRST, LAST} select=0)
$Method STRING .string(INT n=0, ENUM {FIRST, LAST} select=0)
Returns the string associated with the `nth` pattern added to the set,
......
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