Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
libvmod-re2
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
uplex-varnish
libvmod-re2
Commits
ff44820e
Commit
ff44820e
authored
Jul 17, 2018
by
Geoff Simmons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add the .cost() method.
parent
0b0b2cc8
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
104 additions
and
0 deletions
+104
-0
README.rst
README.rst
+18
-0
cost.vtc
src/tests/cost.vtc
+37
-0
vmod_re2.c
src/vmod_re2.c
+17
-0
vmod_re2.vcc
src/vmod_re2.vcc
+14
-0
vre2.cpp
src/vre2/vre2.cpp
+16
-0
vre2.h
src/vre2/vre2.h
+2
-0
No files found.
README.rst
View file @
ff44820e
...
@@ -41,6 +41,7 @@ SYNOPSIS
...
@@ -41,6 +41,7 @@ SYNOPSIS
STRING <obj>.sub(STRING text, STRING rewrite)
STRING <obj>.sub(STRING text, STRING rewrite)
STRING <obj>.suball(STRING text, STRING rewrite)
STRING <obj>.suball(STRING text, STRING rewrite)
STRING <obj>.extract(STRING text, STRING rewrite)
STRING <obj>.extract(STRING text, STRING rewrite)
INT <obj>.cost()
# regex function interface
# regex function interface
BOOL re2.match(STRING pattern, STRING subject [, <regex options>])
BOOL re2.match(STRING pattern, STRING subject [, <regex options>])
...
@@ -610,6 +611,23 @@ Example::
...
@@ -610,6 +611,23 @@ Example::
set resp.http.X-UUCP = email.extract("boris@kremvax.ru", "\2!\1");
set resp.http.X-UUCP = email.extract("boris@kremvax.ru", "\2!\1");
}
}
.. _func_regex.cost:
INT xregex.cost()
-----------------
Return a numeric measurement > 0 for this regex object from the RE2
library. According to the RE2 documentation:
... a very approximate measure of a regexp's "cost". Larger numbers
are more expensive than smaller numbers.
The absolute numeric values are opaque and not relevant, but they are
meaningful relative to one another -- more complex regexen have a
higher cost than less complex regexen. This may be useful during
development and optimization of regular expressions.
regex functional interface
regex functional interface
==========================
==========================
...
...
src/tests/cost.vtc
0 → 100644
View file @
ff44820e
# looks like -*- vcl -*-
varnishtest "cost() function"
varnish v1 -vcl {
import ${vmod_re2};
backend b { .host = "${bad_ip}"; }
sub vcl_init {
# Tests from re2 testing/re2_test.cc
new simple = re2.regex("simple regexp");
new medium = re2.regex("medium.*regexp");
new complex = re2.regex("complex.{1,128}regexp");
}
sub vcl_recv {
return(synth(200));
}
sub vcl_synth {
set resp.http.C-Simple = simple.cost();
set resp.http.C-Medium = medium.cost();
set resp.http.C-Complex = complex.cost();
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.C-Simple ~ {^\d+$}
expect resp.http.C-Medium ~ {^\d+$}
expect resp.http.C-Complex ~ {^\d+$}
expect resp.http.C-Simple > 0
expect resp.http.C-Medium > resp.http.C-Simple
expect resp.http.C-Complex > resp.http.C-Medium
} -run
src/vmod_re2.c
View file @
ff44820e
...
@@ -468,6 +468,23 @@ vmod_regex_extract(VRT_CTX, struct vmod_re2_regex *re, VCL_STRING text,
...
@@ -468,6 +468,23 @@ vmod_regex_extract(VRT_CTX, struct vmod_re2_regex *re, VCL_STRING text,
return
rewrite_method
(
ctx
,
EXTRACT
,
re
,
text
,
rewrite
,
fallback
);
return
rewrite_method
(
ctx
,
EXTRACT
,
re
,
text
,
rewrite
,
fallback
);
}
}
VCL_INT
vmod_regex_cost
(
VRT_CTX
,
struct
vmod_re2_regex
*
re
)
{
int
cost
;
const
char
*
err
;
CHECK_OBJ_NOTNULL
(
ctx
,
VRT_CTX_MAGIC
);
CHECK_OBJ_NOTNULL
(
re
,
VMOD_RE2_REGEX_MAGIC
);
if
((
err
=
vre2_cost
(
re
->
vre2
,
&
cost
))
!=
NULL
)
{
VERR
(
ctx
,
"%s.cost(): Cannot retrieve cost: %s"
,
re
->
vcl_name
,
err
);
return
(
-
1
);
}
return
cost
;
}
/* Regex function interface */
/* Regex function interface */
#define ERR_PREFIX "re2.match(pattern=\"%.40s\", text=\"%.40s\"): "
#define ERR_PREFIX "re2.match(pattern=\"%.40s\", text=\"%.40s\"): "
...
...
src/vmod_re2.vcc
View file @
ff44820e
...
@@ -28,6 +28,7 @@ SYNOPSIS
...
@@ -28,6 +28,7 @@ SYNOPSIS
STRING <obj>.sub(STRING text, STRING rewrite)
STRING <obj>.sub(STRING text, STRING rewrite)
STRING <obj>.suball(STRING text, STRING rewrite)
STRING <obj>.suball(STRING text, STRING rewrite)
STRING <obj>.extract(STRING text, STRING rewrite)
STRING <obj>.extract(STRING text, STRING rewrite)
INT <obj>.cost()
# regex function interface
# regex function interface
BOOL re2.match(STRING pattern, STRING subject [, <regex options>])
BOOL re2.match(STRING pattern, STRING subject [, <regex options>])
...
@@ -522,6 +523,19 @@ Example::
...
@@ -522,6 +523,19 @@ Example::
set resp.http.X-UUCP = email.extract("boris@kremvax.ru", "\2!\1");
set resp.http.X-UUCP = email.extract("boris@kremvax.ru", "\2!\1");
}
}
$Method INT .cost()
Return a numeric measurement > 0 for this regex object from the RE2
library. According to the RE2 documentation:
... a very approximate measure of a regexp's "cost". Larger numbers
are more expensive than smaller numbers.
The absolute numeric values are opaque and not relevant, but they are
meaningful relative to one another -- more complex regexen have a
higher cost than less complex regexen. This may be useful during
development and optimization of regular expressions.
regex functional interface
regex functional interface
==========================
==========================
...
...
src/vre2/vre2.cpp
View file @
ff44820e
...
@@ -99,6 +99,12 @@ vre2::extract(string *out, const char * const text, const char * const rewrite)
...
@@ -99,6 +99,12 @@ vre2::extract(string *out, const char * const text, const char * const rewrite)
return
RE2
::
Extract
(
text
,
*
re_
,
rewrite
,
out
);
return
RE2
::
Extract
(
text
,
*
re_
,
rewrite
,
out
);
}
}
inline
int
vre2
::
size
()
const
{
return
re_
->
ProgramSize
();
}
const
char
*
const
char
*
vre2_init
(
vre2
**
vre2p
,
const
char
*
pattern
,
unsigned
utf8
,
vre2_init
(
vre2
**
vre2p
,
const
char
*
pattern
,
unsigned
utf8
,
unsigned
posix_syntax
,
unsigned
longest_match
,
long
max_mem
,
unsigned
posix_syntax
,
unsigned
longest_match
,
long
max_mem
,
...
@@ -236,6 +242,16 @@ vre2_quotemeta(const char * const unquoted, char * const dest,
...
@@ -236,6 +242,16 @@ vre2_quotemeta(const char * const unquoted, char * const dest,
CATCHALL
CATCHALL
}
}
const
char
*
vre2_cost
(
vre2
*
vre2
,
int
*
cost
)
{
try
{
*
cost
=
vre2
->
size
();
return
NULL
;
}
CATCHALL
}
const
char
*
const
char
*
vre2_fini
(
vre2
**
vre2
)
vre2_fini
(
vre2
**
vre2
)
{
{
...
...
src/vre2/vre2.h
View file @
ff44820e
...
@@ -54,6 +54,7 @@ public:
...
@@ -54,6 +54,7 @@ public:
bool
global_replace
(
string
*
text
,
const
char
*
const
rewrite
)
const
;
bool
global_replace
(
string
*
text
,
const
char
*
const
rewrite
)
const
;
bool
extract
(
string
*
out
,
const
char
*
const
text
,
bool
extract
(
string
*
out
,
const
char
*
const
text
,
const
char
*
const
rewrite
)
const
;
const
char
*
const
rewrite
)
const
;
int
size
()
const
;
};
};
#else
#else
typedef
struct
vre2
vre2
;
typedef
struct
vre2
vre2
;
...
@@ -91,6 +92,7 @@ extern "C" {
...
@@ -91,6 +92,7 @@ extern "C" {
const
char
*
const
rewrite
,
const
char
*
const
rewrite
,
char
*
const
dest
,
const
size_t
bytes
,
char
*
const
dest
,
const
size_t
bytes
,
int
*
const
match
,
size_t
*
const
len
);
int
*
const
match
,
size_t
*
const
len
);
const
char
*
vre2_cost
(
vre2
*
vre2
,
int
*
cost
);
const
char
*
vre2_quotemeta
(
const
char
*
const
unquoted
,
const
char
*
vre2_quotemeta
(
const
char
*
const
unquoted
,
char
*
const
dest
,
const
size_t
bytes
,
char
*
const
dest
,
const
size_t
bytes
,
size_t
*
const
len
);
size_t
*
const
len
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment