Commit c8c28b68 authored by Geoff Simmons's avatar Geoff Simmons

Update doc formatting for changes in the vmodtool since Varnish 5.0.

parent 507f934e
...@@ -198,38 +198,28 @@ turned off. ...@@ -198,38 +198,28 @@ turned off.
CONTENTS CONTENTS
======== ========
* STRING backref(PRIV_TASK, INT, STRING) * regex(STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL)
* STRING extract(STRING, STRING, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL)
* BOOL match(PRIV_TASK, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL) * BOOL match(PRIV_TASK, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL)
* STRING backref(PRIV_TASK, INT, STRING)
* STRING namedref(PRIV_TASK, STRING, STRING) * STRING namedref(PRIV_TASK, STRING, STRING)
* Object regex
* STRING regex.backref(INT, STRING)
* STRING regex.extract(STRING, STRING, STRING)
* BOOL regex.match(STRING)
* STRING regex.namedref(STRING, STRING)
* STRING regex.sub(STRING, STRING, STRING)
* STRING regex.suball(STRING, STRING, STRING)
* Object set
* VOID set.add(STRING)
* VOID set.compile()
* BOOL set.match(STRING)
* STRING sub(STRING, STRING, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL) * STRING sub(STRING, STRING, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL)
* STRING suball(STRING, STRING, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL) * STRING suball(STRING, STRING, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL)
* STRING extract(STRING, STRING, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL)
* set(ENUM {none,start,both}, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL)
* STRING version() * STRING version()
.. _obj_regex: .. _obj_regex:
Object regex regex
============ -----
::
Prototype new OBJ = regex(STRING pattern, BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0)
new OBJECT = re2.regex(STRING pattern [, <regex options>])
Description Create a regex object from ``pattern`` and the given options (or
Create a regex object from ``pattern`` and the given options (or option option defaults). If the pattern is invalid, then VCL will fail to
defaults). If the pattern is invalid, then VCL will fail to load and the VCC load and the VCC compiler will emit an error message.
compiler will emit an error message.
Example:: Example::
...@@ -244,15 +234,15 @@ Example:: ...@@ -244,15 +234,15 @@ Example::
.. _func_regex.match: .. _func_regex.match:
BOOL regex.match(STRING) regex.match
------------------------ -----------
::
Prototype
BOOL regex.match(STRING) BOOL regex.match(STRING)
Description Returns ``true`` if and only if the compiled regex matches the given
Returns ``true`` if and only if the compiled regex matches the given string; corresponds to VCL's infix operator ``~``.
string; corresponds to VCL's infix operator ``~``.
Example:: Example::
...@@ -262,48 +252,51 @@ Example:: ...@@ -262,48 +252,51 @@ Example::
.. _func_regex.backref: .. _func_regex.backref:
STRING regex.backref(INT, STRING) regex.backref
--------------------------------- -------------
Prototype ::
STRING regex.backref(INT ref, STRING fallback)
STRING regex.backref(INT ref, STRING fallback="**BACKREF METHOD FAILED**")
Description
Returns the `nth` captured subexpression from the most recent successful Returns the `nth` captured subexpression from the most recent
call of the ``.match()`` method for this object in the same client or backend, successful call of the ``.match()`` method for this object in the same
context, or a fallback string in case the capture fails. Backref 0 indicates client or backend, context, or a fallback string in case the capture
the entire matched string. Thus this function behaves like the ``\n`` in the fails. Backref 0 indicates the entire matched string. Thus this
native VCL functions ``regsub`` and ``regsuball``, and the ``$1``, ``$2`` ... function behaves like the ``\n`` in the native VCL functions
variables in Perl. ``regsub`` and ``regsuball``, and the ``$1``, ``$2`` ... variables in
Perl.
Since Varnish client and backend operations run in different threads,
``.backref()`` can only refer back to a ``.match()`` call in the same Since Varnish client and backend operations run in different threads,
thread. Thus a ``.backref()`` call in any of the ``vcl_backend_*`` ``.backref()`` can only refer back to a ``.match()`` call in the same
subroutines -- the backend context -- refers back to a previous ``.match()`` thread. Thus a ``.backref()`` call in any of the ``vcl_backend_*``
in any of those same subroutines; and a call in any of the other VCL subroutines -- the backend context -- refers back to a previous
subroutines -- the client context -- refers back to a ``.match()`` in the ``.match()`` in any of those same subroutines; and a call in any of
same client context. the other VCL subroutines -- the client context -- refers back to a
``.match()`` in the same client context.
After unsuccessful matches, the ``fallback`` string is returned for any call
to ``.backref()``. The default value of ``fallback`` is ``"**BACKREF METHOD After unsuccessful matches, the ``fallback`` string is returned for
FAILED**"``. ``.backref()`` always fails after a failed match, even if any call to ``.backref()``. The default value of ``fallback`` is
``.match()`` had been called successfully before the failure. ``"**BACKREF METHOD FAILED**"``. ``.backref()`` always fails after a
failed match, even if ``.match()`` had been called successfully before
``.backref()`` may also return ``fallback`` after a successful match, if the failure.
no captured group in the matching string corresponds to the backref number.
For example, when the pattern ``(a|(b))c`` matches the string ``ac``, there ``.backref()`` may also return ``fallback`` after a successful match,
is no backref 2, since nothing matches ``b`` in the string. if no captured group in the matching string corresponds to the backref
number. For example, when the pattern ``(a|(b))c`` matches the string
The VCL infix operators ``~`` and ``!~`` do not affect this method, nor do ``ac``, there is no backref 2, since nothing matches ``b`` in the
the functions ``regsub`` or ``regsuball``. Nor is it affected by the matches string.
performed by any other method or function in this VMOD (such as the ``sub()``,
``suball()`` or ``extract()`` methods or functions, or the ``set`` object's The VCL infix operators ``~`` and ``!~`` do not affect this method,
``.match()`` method). nor do the functions ``regsub`` or ``regsuball``. Nor is it affected
by the matches performed by any other method or function in this VMOD
``.backref()`` fails, returning ``fallback`` and writing an error (such as the ``sub()``, ``suball()`` or ``extract()`` methods or
message to the Varnish log with the ``VCL_Error`` tag, under the functions, or the ``set`` object's ``.match()`` method).
following conditions (even if a previous match was successful and a
substring could have been captured): ``.backref()`` fails, returning ``fallback`` and writing an error
message to the Varnish log with the ``VCL_Error`` tag, under the
following conditions (even if a previous match was successful and a
substring could have been captured):
* The ``fallback`` string is undefined, for example if set from an unset * The ``fallback`` string is undefined, for example if set from an unset
header variable. header variable.
...@@ -323,34 +316,38 @@ Example:: ...@@ -323,34 +316,38 @@ Example::
.. _func_regex.namedref: .. _func_regex.namedref:
STRING regex.namedref(STRING, STRING) regex.namedref
------------------------------------- --------------
::
Prototype STRING regex.namedref(STRING name, STRING fallback="**NAMEDREF METHOD FAILED**")
STRING regex.namedref(STRING name, STRING fallback)
Description Returns the captured subexpression designated by ``name`` from the
Returns the captured subexpression designated by ``name`` from the most most recent successful call to ``.match()`` in the current context
recent successful call to ``.match()`` in the current context (client or (client or backend), or ``fallback`` in case of failure.
backend), or ``fallback`` in case of failure.
Named capturing groups are written in RE2 as: ``(?P<name>re)``. (Note that Named capturing groups are written in RE2 as: ``(?P<name>re)``. (Note
this syntax with ``P``, inspired by Python, differs from the notation for that this syntax with ``P``, inspired by Python, differs from the
named capturing groups in PCRE.) Thus when ``(?P<foo>.+)bar$`` matches notation for named capturing groups in PCRE.) Thus when
``bazbar``, then ``.namedref("foo")`` returns ``baz``. ``(?P<foo>.+)bar$`` matches ``bazbar``, then ``.namedref("foo")``
returns ``baz``.
Note that a named capturing group can also be referenced as a numbered group. Note that a named capturing group can also be referenced as a numbered
So in the previous example, ``.backref(1)`` also returns ``baz``. group. So in the previous example, ``.backref(1)`` also returns
``baz``.
``fallback`` is returned when ``.namedref()`` is called after an ``fallback`` is returned when ``.namedref()`` is called after an
unsuccessful match. The default fallback is ``"**NAMEDREF METHOD FAILED**"``. unsuccessful match. The default fallback is ``"**NAMEDREF METHOD
FAILED**"``.
Like ``.backref()``, ``.namedref()`` is not affected by native VCL regex Like ``.backref()``, ``.namedref()`` is not affected by native VCL
operations, nor by any other matches performed by methods or functions of regex operations, nor by any other matches performed by methods or
the VMOD, except for a prior ``.match()`` for the same object. functions of the VMOD, except for a prior ``.match()`` for the same
object.
``.namedref()`` fails, returning ``fallback`` and logging a ``VCL_Error`` ``.namedref()`` fails, returning ``fallback`` and logging a
message, if: ``VCL_Error`` message, if:
* The ``fallback`` string is undefined. * The ``fallback`` string is undefined.
* ``name`` is undefined or the empty string. * ``name`` is undefined or the empty string.
...@@ -373,25 +370,25 @@ Example:: ...@@ -373,25 +370,25 @@ Example::
.. _func_regex.sub: .. _func_regex.sub:
STRING regex.sub(STRING, STRING, STRING) regex.sub
---------------------------------------- ---------
::
Prototype STRING regex.sub(STRING text, STRING rewrite, STRING fallback="**SUB METHOD FAILED**")
STRING regex.sub(STRING text, STRING rewrite, STRING fallback)
Description If the compiled pattern for this regex object matches ``text``, then
If the compiled pattern for this regex object matches ``text``, then return the result of replacing the first match in ``text`` with
return the result of replacing the first match in ``text`` with ``rewrite``. ``rewrite``. Within ``rewrite``, ``\1`` through ``\9`` can be used to
Within ``rewrite``, ``\1`` through ``\9`` can be used to insert the insert the the numbered capturing group from the pattern, and ``\0``
the numbered capturing group from the pattern, and ``\0`` to insert the to insert the entire matching text. This method corresponds to the VCL
entire matching text. This method corresponds to the VCL native function native function ``regsub()``.
``regsub()``.
``fallback`` is returned if the pattern does not match ``text``. The default ``fallback`` is returned if the pattern does not match ``text``. The
fallback is ``"**SUB METHOD FAILED**"``. default fallback is ``"**SUB METHOD FAILED**"``.
``.sub()`` fails, returning ``fallback`` and logging a ``VCL_Error`` message, ``.sub()`` fails, returning ``fallback`` and logging a ``VCL_Error``
if: message, if:
* Any of ``text``, ``rewrite`` or ``fallback`` are undefined. * Any of ``text``, ``rewrite`` or ``fallback`` are undefined.
* There is insufficient workspace for the rewritten string. * There is insufficient workspace for the rewritten string.
...@@ -410,22 +407,23 @@ Example:: ...@@ -410,22 +407,23 @@ Example::
.. _func_regex.suball: .. _func_regex.suball:
STRING regex.suball(STRING, STRING, STRING) regex.suball
------------------------------------------- ------------
Prototype ::
STRING regex.suball(STRING text, STRING rewrite, STRING fallback)
STRING regex.suball(STRING text, STRING rewrite, STRING fallback="**SUBALL METHOD FAILED**")
Description Like ``.sub()``, except that all successive non-overlapping matches in
Like ``.sub()``, except that all successive non-overlapping matches in ``text`` are replaced with ``rewrite``. This method corresponds to VCL
``text`` are replaced with ``rewrite``. This method corresponds to VCL native ``regsuball()``.
native ``regsuball()``.
The default fallback is ``"**SUBALL METHOD FAILED**"``. ``.suball()`` The default fallback is ``"**SUBALL METHOD FAILED**"``. ``.suball()``
fails under the same conditions as ``.sub()``. fails under the same conditions as ``.sub()``.
Since only non-overlapping matches are substituted, replacing ``"ana"`` Since only non-overlapping matches are substituted, replacing
within ``"banana"`` only results in one substitution, not two. ``"ana"`` within ``"banana"`` only results in one substitution, not
two.
Example:: Example::
...@@ -441,19 +439,19 @@ Example:: ...@@ -441,19 +439,19 @@ Example::
.. _func_regex.extract: .. _func_regex.extract:
STRING regex.extract(STRING, STRING, STRING) regex.extract
-------------------------------------------- -------------
::
Prototype STRING regex.extract(STRING text, STRING rewrite, STRING fallback="**EXTRACT METHOD FAILED**")
STRING regex.extract(STRING text, STRING rewrite, STRING fallback)
Description If the compiled pattern for this regex object matches ``text``, then
If the compiled pattern for this regex object matches ``text``, then return ``rewrite`` with substitutions from the matching portions of
return ``rewrite`` with substitutions from the matching portions of ``text``. Non-matching substrings of ``text`` are ignored.
``text``. Non-matching substrings of ``text`` are ignored.
The default fallback is ``"**EXTRACT METHOD FAILED**"``. Like ``.sub()`` The default fallback is ``"**EXTRACT METHOD FAILED**"``. Like
and ``.suball()``, ``.extract()`` fails if: ``.sub()`` and ``.suball()``, ``.extract()`` fails if:
* Any of ``text``, ``rewrite`` or ``fallback`` are undefined. * Any of ``text``, ``rewrite`` or ``fallback`` are undefined.
* There is insufficient workspace for the rewritten string. * There is insufficient workspace for the rewritten string.
...@@ -474,19 +472,19 @@ regex functional interface ...@@ -474,19 +472,19 @@ regex functional interface
.. _func_match: .. _func_match:
BOOL match(PRIV_TASK, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL) match
------------------------------------------------------------------------------------------------------------ -----
::
Prototype BOOL match(PRIV_TASK, STRING pattern, STRING subject, BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0)
BOOL match(PRIV_TASK, STRING pattern, STRING subject, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL never_capture, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line)
Description Like the ``regex.match()`` method, return ``true`` if ``pattern``
Like the ``regex.match()`` method, return ``true`` if ``pattern`` matches matches ``subject``, where ``pattern`` is compiled with the given
``subject``, where ``pattern`` is compiled with the given options (or default options (or default options) on each invocation.
options) on each invocation.
If ``pattern`` fails to compile, then an error message is logged with If ``pattern`` fails to compile, then an error message is logged with
the ``VCL_Error`` tag, and ``false`` is returned. the ``VCL_Error`` tag, and ``false`` is returned.
Example:: Example::
...@@ -497,26 +495,26 @@ Example:: ...@@ -497,26 +495,26 @@ Example::
.. _func_backref: .. _func_backref:
STRING backref(PRIV_TASK, INT, STRING) backref
-------------------------------------- -------
Prototype ::
STRING backref(PRIV_TASK, INT ref, STRING fallback)
STRING backref(PRIV_TASK, INT ref, STRING fallback="**BACKREF FUNCTION FAILED**")
Description Returns the `nth` captured subexpression from the most recent
Returns the `nth` captured subexpression from the most recent successful successful call of the ``match()`` function in the current client or
call of the ``match()`` function in the current client or backend context, backend context, or a fallback string if the capture fails. The
or a fallback string if the capture fails. The default ``fallback`` is default ``fallback`` is ``"**BACKREF FUNCTION FAILED**"``.
``"**BACKREF FUNCTION FAILED**"``.
Similarly to the ``regex.backref()`` method, ``fallback`` is returned Similarly to the ``regex.backref()`` method, ``fallback`` is returned
after any failed invocation of the ``match()`` function, or if there after any failed invocation of the ``match()`` function, or if there
is no captured group corresponding to the backref number. The function is no captured group corresponding to the backref number. The function
is not affected by native VCL regex operations, or any other method or is not affected by native VCL regex operations, or any other method or
function of the VMOD except for the ``match()`` function. function of the VMOD except for the ``match()`` function.
The function fails, returning ``fallback`` and logging a ``VCL_Error`` The function fails, returning ``fallback`` and logging a ``VCL_Error``
message, under the same conditions as the corresponding method: message, under the same conditions as the corresponding method:
* ``fallback`` is undefined. * ``fallback`` is undefined.
* ``never_capture`` was true in the previous invocation of the ``match()`` * ``never_capture`` was true in the previous invocation of the ``match()``
...@@ -536,23 +534,23 @@ Example:: ...@@ -536,23 +534,23 @@ Example::
.. _func_namedref: .. _func_namedref:
STRING namedref(PRIV_TASK, STRING, STRING) namedref
------------------------------------------ --------
Prototype ::
STRING namedref(PRIV_TASK, STRING name, STRING fallback)
Description STRING namedref(PRIV_TASK, STRING name, STRING fallback="**NAMEDREF FUNCTION FAILED**")
Returns the captured subexpression designated by ``name`` from the most
recent successful call to the ``match()`` function in the current context, or
``fallback`` in case of failure. The default fallback is ``"**NAMEDREF
FUNCTION FAILED**"``.
The function returns ``fallback`` when the previous invocation of the Returns the captured subexpression designated by ``name`` from the
``match()`` function failed, and is only affected by use of the ``match()`` most recent successful call to the ``match()`` function in the current
function. The function fails, returning ``fallback`` and logging a context, or ``fallback`` in case of failure. The default fallback is
``VCL_Error`` message, under the same conditions as the corresponding ``"**NAMEDREF FUNCTION FAILED**"``.
method:
The function returns ``fallback`` when the previous invocation of the
``match()`` function failed, and is only affected by use of the
``match()`` function. The function fails, returning ``fallback`` and
logging a ``VCL_Error`` message, under the same conditions as the
corresponding method:
* ``fallback`` is undefined. * ``fallback`` is undefined.
* ``name`` is undefined or the empty string. * ``name`` is undefined or the empty string.
...@@ -570,24 +568,24 @@ Example:: ...@@ -570,24 +568,24 @@ Example::
.. _func_sub: .. _func_sub:
STRING sub(STRING, STRING, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL) sub
----------------------------------------------------------------------------------------------------------------- ---
::
Prototype STRING sub(STRING pattern, STRING text, STRING rewrite, STRING fallback="**SUB FUNCTION FAILED**", BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0)
STRING sub(STRING pattern, STRING text, STRING rewrite, STRING fallback, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL never_capture, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line)
Description Compiles ``pattern`` with the given options, and if it matches
Compiles ``pattern`` with the given options, and if it matches ``text``, ``text``, then return the result of replacing the first match in
then return the result of replacing the first match in ``text`` with ``text`` with ``rewrite``. As with the ``regex.sub()`` method, ``\0``
``rewrite``. As with the ``regex.sub()`` method, ``\0`` through ``\9`` through ``\9`` may be used in ``rewrite`` to substitute captured
may be used in ``rewrite`` to substitute captured groups from the groups from the pattern.
pattern.
``fallback`` is returned if the pattern does not match ``text``. The default ``fallback`` is returned if the pattern does not match ``text``. The
fallback is ``"**SUB FUNCTION FAILED**"``. default fallback is ``"**SUB FUNCTION FAILED**"``.
``sub()`` fails, returning ``fallback`` and logging a ``VCL_Error`` message, ``sub()`` fails, returning ``fallback`` and logging a ``VCL_Error``
if: message, if:
* ``pattern`` cannot be compiled. * ``pattern`` cannot be compiled.
* Any of ``text``, ``rewrite`` or ``fallback`` are undefined. * Any of ``text``, ``rewrite`` or ``fallback`` are undefined.
...@@ -603,18 +601,18 @@ Example:: ...@@ -603,18 +601,18 @@ Example::
.. _func_suball: .. _func_suball:
STRING suball(STRING, STRING, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL) suball
-------------------------------------------------------------------------------------------------------------------- ------
Prototype ::
STRING suball(STRING pattern, STRING text, STRING rewrite, STRING fallback, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL never_capture, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line)
Description STRING suball(STRING pattern, STRING text, STRING rewrite, STRING fallback="**SUBALL FUNCTION FAILED**", BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0)
Like the ``sub()`` function, except that all successive non-overlapping
matches in ``text`` are replace with ``rewrite``.
The default fallback is ``"**SUBALL FUNCTION FAILED**"``. The ``suball()`` Like the ``sub()`` function, except that all successive
function fails under the same conditions as ``sub()``. non-overlapping matches in ``text`` are replace with ``rewrite``.
The default fallback is ``"**SUBALL FUNCTION FAILED**"``. The
``suball()`` function fails under the same conditions as ``sub()``.
Example:: Example::
...@@ -626,19 +624,20 @@ Example:: ...@@ -626,19 +624,20 @@ Example::
.. _func_extract: .. _func_extract:
STRING extract(STRING, STRING, STRING, STRING, BOOL, BOOL, BOOL, INT, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL, BOOL) extract
--------------------------------------------------------------------------------------------------------------------- -------
::
Prototype STRING extract(STRING pattern, STRING text, STRING rewrite, STRING fallback="**EXTRACT FUNCTION FAILED**", BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0)
STRING extract(STRING pattern, STRING text, STRING rewrite, STRING fallback, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL never_capture, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line)
Description Compiles ``pattern`` with the given options, and if it matches
Compiles ``pattern`` with the given options, and if it matches ``text``, ``text``, then return ``rewrite`` with substitutions from the matching
then return ``rewrite`` with substitutions from the matching portions of portions of ``text``, ignoring the non-matching portions.
``text``, ignoring the non-matching portions.
The default fallback is ``"**EXTRACT FUNCTION FAILED**"``. The ``extract()`` The default fallback is ``"**EXTRACT FUNCTION FAILED**"``. The
function fails under the same conditions as ``sub()`` and ``suball()``. ``extract()`` function fails under the same conditions as ``sub()``
and ``suball()``.
Example:: Example::
...@@ -649,34 +648,35 @@ Example:: ...@@ -649,34 +648,35 @@ Example::
.. _obj_set: .. _obj_set:
Object set set
========== ---
::
Prototype new OBJ = set(ENUM {none,start,both} anchor="none", BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0)
new OBJECT = re2.set([ENUM anchor] [, <regex options>])
Description Initialize a set object that represents several patterns combined by
Initialize a set object that represents several patterns combined by alternation -- ``|`` for "or".
alternation -- ``|`` for "or".
Optional parameters control the interpretation of the resulting composed Optional parameters control the interpretation of the resulting
pattern. The ``anchor`` parameter is an enum that can have the values composed pattern. The ``anchor`` parameter is an enum that can have
``none``, ``start`` or ``both``, where ``none`` is the default. ``start`` the values ``none``, ``start`` or ``both``, where ``none`` is the
means that each pattern is matched as if it begins with ``^`` for default. ``start`` means that each pattern is matched as if it begins
start-of-text, and ``both`` means that each pattern is anchored with both with ``^`` for start-of-text, and ``both`` means that each pattern is
``^`` at the beginning and ``$`` for end-of-text at the end. ``none`` means anchored with both ``^`` at the beginning and ``$`` for end-of-text at
that each pattern is interpreted as a partial match (although individual the end. ``none`` means that each pattern is interpreted as a partial
patterns within the set may have either of ``^`` of ``$``). match (although individual patterns within the set may have either of
``^`` of ``$``).
For example, if a set is initialized with ``anchor=both``, and the patterns For example, if a set is initialized with ``anchor=both``, and the
``foo`` and ``bar`` are added, then matches against the set match a string patterns ``foo`` and ``bar`` are added, then matches against the set
against ``^foo$|^bar$``, or equivalently ``^(foo|bar)$``. match a string against ``^foo$|^bar$``, or equivalently
``^(foo|bar)$``.
The usual regex options can be set, which then control matching against The usual regex options can be set, which then control matching
the resulting composed pattern. However, the ``never_capture`` option against the resulting composed pattern. However, the ``never_capture``
cannot be set, and is always implicitly true, since backrefs and option cannot be set, and is always implicitly true, since backrefs
namedrefs are not possible with sets. and namedrefs are not possible with sets.
Example:: Example::
...@@ -697,25 +697,25 @@ Example:: ...@@ -697,25 +697,25 @@ Example::
.. _func_set.add: .. _func_set.add:
VOID set.add(STRING) set.add
-------------------- -------
::
Prototype
VOID set.add(STRING) VOID set.add(STRING)
Description Add the given pattern to the set. If the pattern is invalid,
Add the given pattern to the set. If the pattern is invalid, ``.add()`` ``.add()`` fails, and the VCL will fail to load, with an error message
fails, and the VCL will fail to load, with an error message describing describing the problem.
the problem.
``.add()`` MUST be called in ``vcl_init``, and MAY NOT be called after ``.add()`` MUST be called in ``vcl_init``, and MAY NOT be called after
``.compile()``. If ``.add()`` is called in any other subroutine, an ``.compile()``. If ``.add()`` is called in any other subroutine, an
error message with ``VCL_Error`` is logged, and the call has no effect. error message with ``VCL_Error`` is logged, and the call has no
If it is called in ``vcl_init`` after ``.compile()``, then the VCL load effect. If it is called in ``vcl_init`` after ``.compile()``, then the
will fail with an error message. VCL load will fail with an error message.
In other words, add all patterns to the set in ``vcl_init``, and finally In other words, add all patterns to the set in ``vcl_init``, and
call ``.compile()`` when you're done. finally call ``.compile()`` when you're done.
Example:: Example::
...@@ -732,43 +732,44 @@ Example:: ...@@ -732,43 +732,44 @@ Example::
.. _func_set.compile: .. _func_set.compile:
VOID set.compile() set.compile
------------------ -----------
::
Prototype
VOID set.compile() VOID set.compile()
Description Compile the compound pattern represented by the set -- an alternation
Compile the compound pattern represented by the set -- an alternation of of all patterns added by ``.add()``.
all patterns added by ``.add()``.
``.compile()`` may fail if the ``max_mem`` setting is not large enough ``.compile()`` may fail if the ``max_mem`` setting is not large enough
for the composed pattern. In that case, the VCL load will fail with an for the composed pattern. In that case, the VCL load will fail with an
error message (then consider a larger value for ``max_mem`` in the set error message (then consider a larger value for ``max_mem`` in the set
constructor). constructor).
``.compile()`` MUST be called in ``vcl_init``, and MAY NOT be called ``.compile()`` MUST be called in ``vcl_init``, and MAY NOT be called
more than once for a set object. If it is called in any other subroutine, more than once for a set object. If it is called in any other
a ``VCL_Error`` message is logged, and the call has no effect. If it subroutine, a ``VCL_Error`` message is logged, and the call has no
is called a second time in ``vcl_init``, the VCL load will fail. effect. If it is called a second time in ``vcl_init``, the VCL load
will fail.
See above for examples. See above for examples.
.. _func_set.match: .. _func_set.match:
BOOL set.match(STRING) set.match
---------------------- ---------
::
Prototype
BOOL set.match(STRING) BOOL set.match(STRING)
Description Returns ``true`` if the given string matches the compound pattern
Returns ``true`` if the given string matches the compound pattern represented by the set, i.e. if it matches any of the patterns that
represented by the set, i.e. if it matches any of the patterns that were added to the set.
were added to the set.
``.match()`` MUST be called after ``.compile()``; otherwise the ``.match()`` MUST be called after ``.compile()``; otherwise the match
match always fails. always fails.
Example:: Example::
...@@ -778,14 +779,14 @@ Example:: ...@@ -778,14 +779,14 @@ Example::
.. _func_version: .. _func_version:
STRING version() version
---------------- -------
::
Prototype
STRING version() STRING version()
Description Return the version string for this VMOD.
Return the version string for this VMOD.
Example:: Example::
...@@ -912,3 +913,16 @@ project. See LICENSE for details. ...@@ -912,3 +913,16 @@ project. See LICENSE for details.
* Copyright (c) 2016 UPLEX Nils Goroll Systemoptimierung * Copyright (c) 2016 UPLEX Nils Goroll Systemoptimierung
COPYRIGHT
=========
::
Copyright (c) 2016 UPLEX Nils Goroll Systemoptimierung
All rights reserved
Author: Geoffrey Simmons <geoffrey.simmons@uplex.de>
See LICENSE
...@@ -186,13 +186,9 @@ $Object regex(STRING pattern, BOOL utf8=0, BOOL posix_syntax=0, ...@@ -186,13 +186,9 @@ $Object regex(STRING pattern, BOOL utf8=0, BOOL posix_syntax=0,
BOOL case_sensitive=1, BOOL perl_classes=0, BOOL case_sensitive=1, BOOL perl_classes=0,
BOOL word_boundary=0, BOOL one_line=0) BOOL word_boundary=0, BOOL one_line=0)
Prototype Create a regex object from ``pattern`` and the given options (or
new OBJECT = re2.regex(STRING pattern [, <regex options>]) option defaults). If the pattern is invalid, then VCL will fail to
load and the VCC compiler will emit an error message.
Description
Create a regex object from ``pattern`` and the given options (or option
defaults). If the pattern is invalid, then VCL will fail to load and the VCC
compiler will emit an error message.
Example:: Example::
...@@ -207,9 +203,8 @@ Example:: ...@@ -207,9 +203,8 @@ Example::
$Method BOOL .match(STRING) $Method BOOL .match(STRING)
Description Returns ``true`` if and only if the compiled regex matches the given
Returns ``true`` if and only if the compiled regex matches the given string; corresponds to VCL's infix operator ``~``.
string; corresponds to VCL's infix operator ``~``.
Example:: Example::
...@@ -219,42 +214,44 @@ Example:: ...@@ -219,42 +214,44 @@ Example::
$Method STRING .backref(INT ref, STRING fallback = "**BACKREF METHOD FAILED**") $Method STRING .backref(INT ref, STRING fallback = "**BACKREF METHOD FAILED**")
Description Returns the `nth` captured subexpression from the most recent
Returns the `nth` captured subexpression from the most recent successful successful call of the ``.match()`` method for this object in the same
call of the ``.match()`` method for this object in the same client or backend, client or backend, context, or a fallback string in case the capture
context, or a fallback string in case the capture fails. Backref 0 indicates fails. Backref 0 indicates the entire matched string. Thus this
the entire matched string. Thus this function behaves like the ``\n`` in the function behaves like the ``\n`` in the native VCL functions
native VCL functions ``regsub`` and ``regsuball``, and the ``$1``, ``$2`` ... ``regsub`` and ``regsuball``, and the ``$1``, ``$2`` ... variables in
variables in Perl. Perl.
Since Varnish client and backend operations run in different threads, Since Varnish client and backend operations run in different threads,
``.backref()`` can only refer back to a ``.match()`` call in the same ``.backref()`` can only refer back to a ``.match()`` call in the same
thread. Thus a ``.backref()`` call in any of the ``vcl_backend_*`` thread. Thus a ``.backref()`` call in any of the ``vcl_backend_*``
subroutines -- the backend context -- refers back to a previous ``.match()`` subroutines -- the backend context -- refers back to a previous
in any of those same subroutines; and a call in any of the other VCL ``.match()`` in any of those same subroutines; and a call in any of
subroutines -- the client context -- refers back to a ``.match()`` in the the other VCL subroutines -- the client context -- refers back to a
same client context. ``.match()`` in the same client context.
After unsuccessful matches, the ``fallback`` string is returned for any call After unsuccessful matches, the ``fallback`` string is returned for
to ``.backref()``. The default value of ``fallback`` is ``"**BACKREF METHOD any call to ``.backref()``. The default value of ``fallback`` is
FAILED**"``. ``.backref()`` always fails after a failed match, even if ``"**BACKREF METHOD FAILED**"``. ``.backref()`` always fails after a
``.match()`` had been called successfully before the failure. failed match, even if ``.match()`` had been called successfully before
the failure.
``.backref()`` may also return ``fallback`` after a successful match, if
no captured group in the matching string corresponds to the backref number. ``.backref()`` may also return ``fallback`` after a successful match,
For example, when the pattern ``(a|(b))c`` matches the string ``ac``, there if no captured group in the matching string corresponds to the backref
is no backref 2, since nothing matches ``b`` in the string. number. For example, when the pattern ``(a|(b))c`` matches the string
``ac``, there is no backref 2, since nothing matches ``b`` in the
The VCL infix operators ``~`` and ``!~`` do not affect this method, nor do string.
the functions ``regsub`` or ``regsuball``. Nor is it affected by the matches
performed by any other method or function in this VMOD (such as the ``sub()``, The VCL infix operators ``~`` and ``!~`` do not affect this method,
``suball()`` or ``extract()`` methods or functions, or the ``set`` object's nor do the functions ``regsub`` or ``regsuball``. Nor is it affected
``.match()`` method). by the matches performed by any other method or function in this VMOD
(such as the ``sub()``, ``suball()`` or ``extract()`` methods or
``.backref()`` fails, returning ``fallback`` and writing an error functions, or the ``set`` object's ``.match()`` method).
message to the Varnish log with the ``VCL_Error`` tag, under the
following conditions (even if a previous match was successful and a ``.backref()`` fails, returning ``fallback`` and writing an error
substring could have been captured): message to the Varnish log with the ``VCL_Error`` tag, under the
following conditions (even if a previous match was successful and a
substring could have been captured):
* The ``fallback`` string is undefined, for example if set from an unset * The ``fallback`` string is undefined, for example if set from an unset
header variable. header variable.
...@@ -275,28 +272,31 @@ Example:: ...@@ -275,28 +272,31 @@ Example::
$Method STRING .namedref(STRING name, $Method STRING .namedref(STRING name,
STRING fallback = "**NAMEDREF METHOD FAILED**") STRING fallback = "**NAMEDREF METHOD FAILED**")
Description Returns the captured subexpression designated by ``name`` from the
Returns the captured subexpression designated by ``name`` from the most most recent successful call to ``.match()`` in the current context
recent successful call to ``.match()`` in the current context (client or (client or backend), or ``fallback`` in case of failure.
backend), or ``fallback`` in case of failure.
Named capturing groups are written in RE2 as: ``(?P<name>re)``. (Note that Named capturing groups are written in RE2 as: ``(?P<name>re)``. (Note
this syntax with ``P``, inspired by Python, differs from the notation for that this syntax with ``P``, inspired by Python, differs from the
named capturing groups in PCRE.) Thus when ``(?P<foo>.+)bar$`` matches notation for named capturing groups in PCRE.) Thus when
``bazbar``, then ``.namedref("foo")`` returns ``baz``. ``(?P<foo>.+)bar$`` matches ``bazbar``, then ``.namedref("foo")``
returns ``baz``.
Note that a named capturing group can also be referenced as a numbered group. Note that a named capturing group can also be referenced as a numbered
So in the previous example, ``.backref(1)`` also returns ``baz``. group. So in the previous example, ``.backref(1)`` also returns
``baz``.
``fallback`` is returned when ``.namedref()`` is called after an ``fallback`` is returned when ``.namedref()`` is called after an
unsuccessful match. The default fallback is ``"**NAMEDREF METHOD FAILED**"``. unsuccessful match. The default fallback is ``"**NAMEDREF METHOD
FAILED**"``.
Like ``.backref()``, ``.namedref()`` is not affected by native VCL regex Like ``.backref()``, ``.namedref()`` is not affected by native VCL
operations, nor by any other matches performed by methods or functions of regex operations, nor by any other matches performed by methods or
the VMOD, except for a prior ``.match()`` for the same object. functions of the VMOD, except for a prior ``.match()`` for the same
object.
``.namedref()`` fails, returning ``fallback`` and logging a ``VCL_Error`` ``.namedref()`` fails, returning ``fallback`` and logging a
message, if: ``VCL_Error`` message, if:
* The ``fallback`` string is undefined. * The ``fallback`` string is undefined.
* ``name`` is undefined or the empty string. * ``name`` is undefined or the empty string.
...@@ -320,19 +320,18 @@ Example:: ...@@ -320,19 +320,18 @@ Example::
$Method STRING .sub(STRING text, STRING rewrite, $Method STRING .sub(STRING text, STRING rewrite,
STRING fallback = "**SUB METHOD FAILED**") STRING fallback = "**SUB METHOD FAILED**")
Description If the compiled pattern for this regex object matches ``text``, then
If the compiled pattern for this regex object matches ``text``, then return the result of replacing the first match in ``text`` with
return the result of replacing the first match in ``text`` with ``rewrite``. ``rewrite``. Within ``rewrite``, ``\1`` through ``\9`` can be used to
Within ``rewrite``, ``\1`` through ``\9`` can be used to insert the insert the the numbered capturing group from the pattern, and ``\0``
the numbered capturing group from the pattern, and ``\0`` to insert the to insert the entire matching text. This method corresponds to the VCL
entire matching text. This method corresponds to the VCL native function native function ``regsub()``.
``regsub()``.
``fallback`` is returned if the pattern does not match ``text``. The default ``fallback`` is returned if the pattern does not match ``text``. The
fallback is ``"**SUB METHOD FAILED**"``. default fallback is ``"**SUB METHOD FAILED**"``.
``.sub()`` fails, returning ``fallback`` and logging a ``VCL_Error`` message, ``.sub()`` fails, returning ``fallback`` and logging a ``VCL_Error``
if: message, if:
* Any of ``text``, ``rewrite`` or ``fallback`` are undefined. * Any of ``text``, ``rewrite`` or ``fallback`` are undefined.
* There is insufficient workspace for the rewritten string. * There is insufficient workspace for the rewritten string.
...@@ -352,16 +351,16 @@ Example:: ...@@ -352,16 +351,16 @@ Example::
$Method STRING .suball(STRING text, STRING rewrite, $Method STRING .suball(STRING text, STRING rewrite,
STRING fallback = "**SUBALL METHOD FAILED**") STRING fallback = "**SUBALL METHOD FAILED**")
Description Like ``.sub()``, except that all successive non-overlapping matches in
Like ``.sub()``, except that all successive non-overlapping matches in ``text`` are replaced with ``rewrite``. This method corresponds to VCL
``text`` are replaced with ``rewrite``. This method corresponds to VCL native ``regsuball()``.
native ``regsuball()``.
The default fallback is ``"**SUBALL METHOD FAILED**"``. ``.suball()`` The default fallback is ``"**SUBALL METHOD FAILED**"``. ``.suball()``
fails under the same conditions as ``.sub()``. fails under the same conditions as ``.sub()``.
Since only non-overlapping matches are substituted, replacing ``"ana"`` Since only non-overlapping matches are substituted, replacing
within ``"banana"`` only results in one substitution, not two. ``"ana"`` within ``"banana"`` only results in one substitution, not
two.
Example:: Example::
...@@ -378,13 +377,12 @@ Example:: ...@@ -378,13 +377,12 @@ Example::
$Method STRING .extract(STRING text, STRING rewrite, $Method STRING .extract(STRING text, STRING rewrite,
STRING fallback = "**EXTRACT METHOD FAILED**") STRING fallback = "**EXTRACT METHOD FAILED**")
Description If the compiled pattern for this regex object matches ``text``, then
If the compiled pattern for this regex object matches ``text``, then return ``rewrite`` with substitutions from the matching portions of
return ``rewrite`` with substitutions from the matching portions of ``text``. Non-matching substrings of ``text`` are ignored.
``text``. Non-matching substrings of ``text`` are ignored.
The default fallback is ``"**EXTRACT METHOD FAILED**"``. Like ``.sub()`` The default fallback is ``"**EXTRACT METHOD FAILED**"``. Like
and ``.suball()``, ``.extract()`` fails if: ``.sub()`` and ``.suball()``, ``.extract()`` fails if:
* Any of ``text``, ``rewrite`` or ``fallback`` are undefined. * Any of ``text``, ``rewrite`` or ``fallback`` are undefined.
* There is insufficient workspace for the rewritten string. * There is insufficient workspace for the rewritten string.
...@@ -410,13 +408,12 @@ $Function BOOL match(PRIV_TASK, STRING pattern, STRING subject, BOOL utf8=0, ...@@ -410,13 +408,12 @@ $Function BOOL match(PRIV_TASK, STRING pattern, STRING subject, BOOL utf8=0,
BOOL case_sensitive=1, BOOL perl_classes=0, BOOL case_sensitive=1, BOOL perl_classes=0,
BOOL word_boundary=0, BOOL one_line=0) BOOL word_boundary=0, BOOL one_line=0)
Description Like the ``regex.match()`` method, return ``true`` if ``pattern``
Like the ``regex.match()`` method, return ``true`` if ``pattern`` matches matches ``subject``, where ``pattern`` is compiled with the given
``subject``, where ``pattern`` is compiled with the given options (or default options (or default options) on each invocation.
options) on each invocation.
If ``pattern`` fails to compile, then an error message is logged with If ``pattern`` fails to compile, then an error message is logged with
the ``VCL_Error`` tag, and ``false`` is returned. the ``VCL_Error`` tag, and ``false`` is returned.
Example:: Example::
...@@ -428,20 +425,19 @@ Example:: ...@@ -428,20 +425,19 @@ Example::
$Function STRING backref(PRIV_TASK, INT ref, $Function STRING backref(PRIV_TASK, INT ref,
STRING fallback = "**BACKREF FUNCTION FAILED**") STRING fallback = "**BACKREF FUNCTION FAILED**")
Description Returns the `nth` captured subexpression from the most recent
Returns the `nth` captured subexpression from the most recent successful successful call of the ``match()`` function in the current client or
call of the ``match()`` function in the current client or backend context, backend context, or a fallback string if the capture fails. The
or a fallback string if the capture fails. The default ``fallback`` is default ``fallback`` is ``"**BACKREF FUNCTION FAILED**"``.
``"**BACKREF FUNCTION FAILED**"``.
Similarly to the ``regex.backref()`` method, ``fallback`` is returned Similarly to the ``regex.backref()`` method, ``fallback`` is returned
after any failed invocation of the ``match()`` function, or if there after any failed invocation of the ``match()`` function, or if there
is no captured group corresponding to the backref number. The function is no captured group corresponding to the backref number. The function
is not affected by native VCL regex operations, or any other method or is not affected by native VCL regex operations, or any other method or
function of the VMOD except for the ``match()`` function. function of the VMOD except for the ``match()`` function.
The function fails, returning ``fallback`` and logging a ``VCL_Error`` The function fails, returning ``fallback`` and logging a ``VCL_Error``
message, under the same conditions as the corresponding method: message, under the same conditions as the corresponding method:
* ``fallback`` is undefined. * ``fallback`` is undefined.
* ``never_capture`` was true in the previous invocation of the ``match()`` * ``never_capture`` was true in the previous invocation of the ``match()``
...@@ -462,17 +458,16 @@ Example:: ...@@ -462,17 +458,16 @@ Example::
$Function STRING namedref(PRIV_TASK, STRING name, $Function STRING namedref(PRIV_TASK, STRING name,
STRING fallback = "**NAMEDREF FUNCTION FAILED**") STRING fallback = "**NAMEDREF FUNCTION FAILED**")
Description Returns the captured subexpression designated by ``name`` from the
Returns the captured subexpression designated by ``name`` from the most most recent successful call to the ``match()`` function in the current
recent successful call to the ``match()`` function in the current context, or context, or ``fallback`` in case of failure. The default fallback is
``fallback`` in case of failure. The default fallback is ``"**NAMEDREF ``"**NAMEDREF FUNCTION FAILED**"``.
FUNCTION FAILED**"``.
The function returns ``fallback`` when the previous invocation of the The function returns ``fallback`` when the previous invocation of the
``match()`` function failed, and is only affected by use of the ``match()`` ``match()`` function failed, and is only affected by use of the
function. The function fails, returning ``fallback`` and logging a ``match()`` function. The function fails, returning ``fallback`` and
``VCL_Error`` message, under the same conditions as the corresponding logging a ``VCL_Error`` message, under the same conditions as the
method: corresponding method:
* ``fallback`` is undefined. * ``fallback`` is undefined.
* ``name`` is undefined or the empty string. * ``name`` is undefined or the empty string.
...@@ -495,18 +490,17 @@ $Function STRING sub(STRING pattern, STRING text, STRING rewrite, ...@@ -495,18 +490,17 @@ $Function STRING sub(STRING pattern, STRING text, STRING rewrite,
BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1,
BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0) BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0)
Description Compiles ``pattern`` with the given options, and if it matches
Compiles ``pattern`` with the given options, and if it matches ``text``, ``text``, then return the result of replacing the first match in
then return the result of replacing the first match in ``text`` with ``text`` with ``rewrite``. As with the ``regex.sub()`` method, ``\0``
``rewrite``. As with the ``regex.sub()`` method, ``\0`` through ``\9`` through ``\9`` may be used in ``rewrite`` to substitute captured
may be used in ``rewrite`` to substitute captured groups from the groups from the pattern.
pattern.
``fallback`` is returned if the pattern does not match ``text``. The default ``fallback`` is returned if the pattern does not match ``text``. The
fallback is ``"**SUB FUNCTION FAILED**"``. default fallback is ``"**SUB FUNCTION FAILED**"``.
``sub()`` fails, returning ``fallback`` and logging a ``VCL_Error`` message, ``sub()`` fails, returning ``fallback`` and logging a ``VCL_Error``
if: message, if:
* ``pattern`` cannot be compiled. * ``pattern`` cannot be compiled.
* Any of ``text``, ``rewrite`` or ``fallback`` are undefined. * Any of ``text``, ``rewrite`` or ``fallback`` are undefined.
...@@ -528,12 +522,11 @@ $Function STRING suball(STRING pattern, STRING text, STRING rewrite, ...@@ -528,12 +522,11 @@ $Function STRING suball(STRING pattern, STRING text, STRING rewrite,
BOOL case_sensitive=1, BOOL perl_classes=0, BOOL case_sensitive=1, BOOL perl_classes=0,
BOOL word_boundary=0, BOOL one_line=0) BOOL word_boundary=0, BOOL one_line=0)
Description Like the ``sub()`` function, except that all successive
Like the ``sub()`` function, except that all successive non-overlapping non-overlapping matches in ``text`` are replace with ``rewrite``.
matches in ``text`` are replace with ``rewrite``.
The default fallback is ``"**SUBALL FUNCTION FAILED**"``. The ``suball()`` The default fallback is ``"**SUBALL FUNCTION FAILED**"``. The
function fails under the same conditions as ``sub()``. ``suball()`` function fails under the same conditions as ``sub()``.
Example:: Example::
...@@ -551,13 +544,13 @@ $Function STRING extract(STRING pattern, STRING text, STRING rewrite, ...@@ -551,13 +544,13 @@ $Function STRING extract(STRING pattern, STRING text, STRING rewrite,
BOOL case_sensitive=1, BOOL perl_classes=0, BOOL case_sensitive=1, BOOL perl_classes=0,
BOOL word_boundary=0, BOOL one_line=0) BOOL word_boundary=0, BOOL one_line=0)
Description Compiles ``pattern`` with the given options, and if it matches
Compiles ``pattern`` with the given options, and if it matches ``text``, ``text``, then return ``rewrite`` with substitutions from the matching
then return ``rewrite`` with substitutions from the matching portions of portions of ``text``, ignoring the non-matching portions.
``text``, ignoring the non-matching portions.
The default fallback is ``"**EXTRACT FUNCTION FAILED**"``. The ``extract()`` The default fallback is ``"**EXTRACT FUNCTION FAILED**"``. The
function fails under the same conditions as ``sub()`` and ``suball()``. ``extract()`` function fails under the same conditions as ``sub()``
and ``suball()``.
Example:: Example::
...@@ -572,30 +565,28 @@ $Object set(ENUM { none, start, both } anchor="none", BOOL utf8=0, ...@@ -572,30 +565,28 @@ $Object set(ENUM { none, start, both } anchor="none", BOOL utf8=0,
BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0,
BOOL one_line=0) BOOL one_line=0)
Prototype Initialize a set object that represents several patterns combined by
new OBJECT = re2.set([ENUM anchor] [, <regex options>]) alternation -- ``|`` for "or".
Description Optional parameters control the interpretation of the resulting
Initialize a set object that represents several patterns combined by composed pattern. The ``anchor`` parameter is an enum that can have
alternation -- ``|`` for "or". the values ``none``, ``start`` or ``both``, where ``none`` is the
default. ``start`` means that each pattern is matched as if it begins
Optional parameters control the interpretation of the resulting composed with ``^`` for start-of-text, and ``both`` means that each pattern is
pattern. The ``anchor`` parameter is an enum that can have the values anchored with both ``^`` at the beginning and ``$`` for end-of-text at
``none``, ``start`` or ``both``, where ``none`` is the default. ``start`` the end. ``none`` means that each pattern is interpreted as a partial
means that each pattern is matched as if it begins with ``^`` for match (although individual patterns within the set may have either of
start-of-text, and ``both`` means that each pattern is anchored with both ``^`` of ``$``).
``^`` at the beginning and ``$`` for end-of-text at the end. ``none`` means
that each pattern is interpreted as a partial match (although individual For example, if a set is initialized with ``anchor=both``, and the
patterns within the set may have either of ``^`` of ``$``). patterns ``foo`` and ``bar`` are added, then matches against the set
match a string against ``^foo$|^bar$``, or equivalently
For example, if a set is initialized with ``anchor=both``, and the patterns ``^(foo|bar)$``.
``foo`` and ``bar`` are added, then matches against the set match a string
against ``^foo$|^bar$``, or equivalently ``^(foo|bar)$``. The usual regex options can be set, which then control matching
against the resulting composed pattern. However, the ``never_capture``
The usual regex options can be set, which then control matching against option cannot be set, and is always implicitly true, since backrefs
the resulting composed pattern. However, the ``never_capture`` option and namedrefs are not possible with sets.
cannot be set, and is always implicitly true, since backrefs and
namedrefs are not possible with sets.
Example:: Example::
...@@ -616,19 +607,18 @@ Example:: ...@@ -616,19 +607,18 @@ Example::
$Method VOID .add(STRING) $Method VOID .add(STRING)
Description Add the given pattern to the set. If the pattern is invalid,
Add the given pattern to the set. If the pattern is invalid, ``.add()`` ``.add()`` fails, and the VCL will fail to load, with an error message
fails, and the VCL will fail to load, with an error message describing describing the problem.
the problem.
``.add()`` MUST be called in ``vcl_init``, and MAY NOT be called after ``.add()`` MUST be called in ``vcl_init``, and MAY NOT be called after
``.compile()``. If ``.add()`` is called in any other subroutine, an ``.compile()``. If ``.add()`` is called in any other subroutine, an
error message with ``VCL_Error`` is logged, and the call has no effect. error message with ``VCL_Error`` is logged, and the call has no
If it is called in ``vcl_init`` after ``.compile()``, then the VCL load effect. If it is called in ``vcl_init`` after ``.compile()``, then the
will fail with an error message. VCL load will fail with an error message.
In other words, add all patterns to the set in ``vcl_init``, and finally In other words, add all patterns to the set in ``vcl_init``, and
call ``.compile()`` when you're done. finally call ``.compile()`` when you're done.
Example:: Example::
...@@ -645,31 +635,30 @@ Example:: ...@@ -645,31 +635,30 @@ Example::
$Method VOID .compile() $Method VOID .compile()
Description Compile the compound pattern represented by the set -- an alternation
Compile the compound pattern represented by the set -- an alternation of of all patterns added by ``.add()``.
all patterns added by ``.add()``.
``.compile()`` may fail if the ``max_mem`` setting is not large enough ``.compile()`` may fail if the ``max_mem`` setting is not large enough
for the composed pattern. In that case, the VCL load will fail with an for the composed pattern. In that case, the VCL load will fail with an
error message (then consider a larger value for ``max_mem`` in the set error message (then consider a larger value for ``max_mem`` in the set
constructor). constructor).
``.compile()`` MUST be called in ``vcl_init``, and MAY NOT be called ``.compile()`` MUST be called in ``vcl_init``, and MAY NOT be called
more than once for a set object. If it is called in any other subroutine, more than once for a set object. If it is called in any other
a ``VCL_Error`` message is logged, and the call has no effect. If it subroutine, a ``VCL_Error`` message is logged, and the call has no
is called a second time in ``vcl_init``, the VCL load will fail. effect. If it is called a second time in ``vcl_init``, the VCL load
will fail.
See above for examples. See above for examples.
$Method BOOL .match(STRING) $Method BOOL .match(STRING)
Description Returns ``true`` if the given string matches the compound pattern
Returns ``true`` if the given string matches the compound pattern represented by the set, i.e. if it matches any of the patterns that
represented by the set, i.e. if it matches any of the patterns that were added to the set.
were added to the set.
``.match()`` MUST be called after ``.compile()``; otherwise the ``.match()`` MUST be called after ``.compile()``; otherwise the match
match always fails. always fails.
Example:: Example::
...@@ -679,8 +668,7 @@ Example:: ...@@ -679,8 +668,7 @@ Example::
$Function STRING version() $Function STRING version()
Description Return the version string for this VMOD.
Return the version string for this VMOD.
Example:: Example::
......
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