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
Nils Goroll
libvmod-re2
Commits
2101e6d4
Commit
2101e6d4
authored
Mar 26, 2016
by
Geoff Simmons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add the regex.sub() method
parent
2f698cdf
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
140 additions
and
0 deletions
+140
-0
README.rst
README.rst
+9
-0
sub.vtc
src/tests/sub.vtc
+58
-0
vmod_re2.c
src/vmod_re2.c
+39
-0
vmod_re2.vcc
src/vmod_re2.vcc
+3
-0
vre2.cpp
src/vre2/vre2.cpp
+26
-0
vre2.h
src/vre2/vre2.h
+5
-0
No files found.
README.rst
View file @
2101e6d4
...
...
@@ -34,6 +34,7 @@ CONTENTS
* STRING regex.backref(INT, STRING)
* BOOL regex.match(STRING)
* STRING regex.namedref(STRING, STRING)
* STRING regex.sub(STRING, STRING, STRING)
* Object set
* VOID set.add(STRING)
* VOID set.compile()
...
...
@@ -70,6 +71,14 @@ STRING regex.namedref(STRING, STRING)
Prototype
STRING regex.namedref(STRING name, STRING fallback)
.. _func_regex.sub:
STRING regex.sub(STRING, STRING, STRING)
----------------------------------------
Prototype
STRING regex.sub(STRING text, STRING rewrite, STRING fallback)
.. _obj_set:
Object set
...
...
src/tests/sub.vtc
0 → 100644
View file @
2101e6d4
# looks like -*- vcl -*-
varnishtest "sub() method"
# Tests from re2 testing/re2_test.cc
# regex object
varnish v1 -vcl {
import re2 from "${vmod_topbuild}/src/.libs/libvmod_re2.so";
backend be { .host = "${bad_ip}"; }
sub vcl_init {
new piglatin = re2.regex("(qu|[b-df-hj-np-tv-z]*)([a-z]+)");
new word = re2.regex("\w+");
new start = re2.regex("^");
new end = re2.regex("$");
new b = re2.regex("b");
new bplus = re2.regex("b+");
new bstar = re2.regex("b*");
}
sub vcl_recv {
return(synth(200));
}
sub vcl_synth {
set resp.http.pangram
= piglatin.sub("the quick brown fox jumps over the lazy dogs.",
"\2\1ay");
set resp.http.nospam = word.sub("abcd.efghi@google.com",
"\0-NOSPAM");
set resp.http.startfoo = start.sub("foo", "(START)");
set resp.http.start = start.sub("", "(START)");
set resp.http.end = end.sub("", "(END)");
set resp.http.ab = b.sub("ababababab", "bb");
set resp.http.bb = b.sub("bbbbbb", "bb");
set resp.http.bplus = bplus.sub("bbbbbb", "bb");
set resp.http.bstar1 = bstar.sub("bbbbbb", "bb");
set resp.http.bstar2 = bstar.sub("aaaaa", "bb");
}
} -start
client c1 {
txreq
rxresp
expect resp.http.pangram == "ethay quick brown fox jumps over the lazy dogs."
expect resp.http.nospam == "abcd-NOSPAM.efghi@google.com"
expect resp.http.startfoo == "(START)foo"
expect resp.http.start == "(START)"
expect resp.http.end == "(END)"
expect resp.http.ab == "abbabababab"
expect resp.http.bb == "bbbbbbb"
expect resp.http.bplus == "bb"
expect resp.http.bstar1 == "bb"
expect resp.http.bstar2 == "bbaaaaa"
} -run
src/vmod_re2.c
View file @
2101e6d4
...
...
@@ -381,6 +381,45 @@ vmod_regex_namedref(VRT_CTX, struct vmod_re2_regex *re, VCL_STRING name,
#undef ERR_PREFIX
#define ERR_PREFIX "%s.sub(text=\"%s\", rewrite=\"%s\", fallback=\"%s\"): "
VCL_STRING
vmod_regex_sub
(
VRT_CTX
,
struct
vmod_re2_regex
*
re
,
VCL_STRING
text
,
VCL_STRING
rewrite
,
VCL_STRING
fallback
)
{
int
match
=
0
;
size_t
bytes
,
len
;
char
*
ret
;
const
char
*
err
;
CHECK_OBJ_NOTNULL
(
ctx
,
VRT_CTX_MAGIC
);
CHECK_OBJ_NOTNULL
(
re
,
VMOD_RE2_REGEX_MAGIC
);
if
(
text
==
NULL
)
text
=
""
;
if
(
rewrite
==
NULL
)
rewrite
=
""
;
if
(
fallback
==
NULL
)
fallback
=
""
;
ret
=
WS_Snapshot
(
ctx
->
ws
);
bytes
=
WS_Reserve
(
ctx
->
ws
,
0
);
if
((
err
=
vre2_replace
(
re
->
vre2
,
text
,
rewrite
,
ret
,
bytes
,
&
match
,
&
len
))
!=
NULL
)
{
VERR
(
ctx
,
ERR_PREFIX
"%s"
,
re
->
vcl_name
,
text
,
rewrite
,
fallback
,
err
);
WS_Release
(
ctx
->
ws
,
0
);
return
fallback
;
}
if
(
!
match
)
{
WS_Release
(
ctx
->
ws
,
0
);
return
fallback
;
}
WS_Release
(
ctx
->
ws
,
len
);
return
ret
;
}
#undef ERR_PREFIX
/* Object set */
VCL_VOID
...
...
src/vmod_re2.vcc
View file @
2101e6d4
...
...
@@ -24,6 +24,9 @@ $Method STRING .backref(INT ref, STRING fallback = "**BACKREF METHOD FAILED**")
$Method STRING .namedref(STRING name,
STRING fallback = "**NAMEDREF METHOD FAILED**")
$Method STRING .sub(STRING text, STRING rewrite,
STRING fallback = "**SUB METHOD FAILED**")
$Object 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,
...
...
src/vre2/vre2.cpp
View file @
2101e6d4
...
...
@@ -81,6 +81,12 @@ vre2::get_group(const char * const name) const
}
}
inline
bool
vre2
::
replace
(
string
*
text
,
const
char
*
const
rewrite
)
const
{
return
RE2
::
Replace
(
text
,
*
re_
,
rewrite
);
}
const
char
*
vre2_init
(
vre2
**
vre2p
,
const
char
*
pattern
,
unsigned
utf8
,
unsigned
posix_syntax
,
unsigned
longest_match
,
long
max_mem
,
...
...
@@ -164,6 +170,26 @@ vre2_get_group(vre2 *vre2, const char * const name, int * const refnum)
CATCHALL
}
const
char
*
vre2_replace
(
vre2
*
vre2
,
const
char
*
const
text
,
const
char
*
const
rewrite
,
char
*
const
dest
,
const
size_t
bytes
,
int
*
const
match
,
size_t
*
const
len
)
{
try
{
string
t
(
text
);
*
match
=
vre2
->
replace
(
&
t
,
rewrite
);
if
(
!*
match
)
return
NULL
;
if
(
t
.
size
()
>
bytes
+
1
)
throw
runtime_error
(
"insufficient workspace"
);
*
len
=
t
.
size
()
+
1
;
t
.
copy
(
dest
,
*
len
);
dest
[
*
len
]
=
'\0'
;
return
NULL
;
}
CATCHALL
}
const
char
*
vre2_fini
(
vre2
**
vre2
)
{
...
...
src/vre2/vre2.h
View file @
2101e6d4
...
...
@@ -48,6 +48,7 @@ public:
StringPiece
*
groups
)
const
;
const
int
ngroups
()
const
;
int
get_group
(
const
char
*
const
name
)
const
;
bool
replace
(
string
*
text
,
const
char
*
const
rewrite
)
const
;
};
#else
typedef
struct
vre2
vre2
;
...
...
@@ -74,6 +75,10 @@ extern "C" {
const
char
**
const
capture
,
int
*
const
len
);
const
char
*
vre2_get_group
(
vre2
*
vre2
,
const
char
*
const
name
,
int
*
const
refnum
);
const
char
*
vre2_replace
(
vre2
*
vre2
,
const
char
*
const
text
,
const
char
*
const
rewrite
,
char
*
const
dest
,
const
size_t
bytes
,
int
*
const
match
,
size_t
*
const
len
);
#ifdef __cplusplus
}
...
...
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