Commit 4d9878cb by Geoff Simmons

Add the info_str() method.

parent 645789a2
Pipeline #266 skipped
......@@ -112,6 +112,15 @@ regex.info_int
INT regex.info_int(ENUM {BACKREFMAX,CAPTURECOUNT,JITSIZE,MATCHLIMIT,MAXLOOKBEHIND,MINLENGTH,RECURSIONLIMIT,SIZE})
.. _func_regex.info_str:
regex.info_str
--------------
::
STRING regex.info_str(ENUM {BSR,FIRSTCODEUNIT,FIRSTCODEUNITS,LASTCODEUNIT,NEWLINE}, STRING sep=" ")
.. _func_match:
match
......
......@@ -25,6 +25,8 @@
*
*/
#include <stdio.h>
#include "vmod_pcre2.h"
#include "vcc_if.h"
......@@ -217,3 +219,111 @@ vmod_regex_info_int(VRT_CTX, struct vmod_pcre2_regex *regex, VCL_ENUM ints)
AZ(ret);
return where;
}
VCL_STRING
vmod_regex_info_str(VRT_CTX, struct vmod_pcre2_regex *regex, VCL_ENUM strs,
VCL_STRING sep)
{
uint32_t what, where;
int ret;
char *str;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(regex, VMOD_PCRE2_REGEX_MAGIC);
if (sep == NULL) {
VERR(ctx, "sep is undefined in %s.info_str()", regex->vcl_name);
return NULL;
}
if (strcmp(strs, "BSR") == 0)
what = PCRE2_INFO_BSR;
else if (strcmp(strs, "FIRSTCODEUNIT") == 0)
what = PCRE2_INFO_FIRSTCODEUNIT;
else if (strcmp(strs, "FIRSTCODEUNITS") == 0)
what = PCRE2_INFO_FIRSTBITMAP;
else if (strcmp(strs, "LASTCODEUNIT") == 0)
what = PCRE2_INFO_LASTCODEUNIT;
else if (strcmp(strs, "NEWLINE") == 0)
what = PCRE2_INFO_NEWLINE;
else
WRONG("illegal enum in info_str");
if (what == PCRE2_INFO_FIRSTBITMAP) {
unsigned bytes;
int ctr, len;
const uint8_t *map;
char *p;
ret = pcre2_pattern_info(regex->code, what, &map);
AZ(ret);
if (map == NULL)
return "";
str = WS_Front(ctx->ws);
bytes = WS_Reserve(ctx->ws, 0);
if (bytes == 0)
goto nomem;
ctr = bytes;
len = strlen(sep);
p = str;
for (int i = 1; i < 256; i++)
if (map[i >> 3] & (1 << (i & 7))) {
if (ctr - len - 2 < 0)
goto nomem;
sprintf(p, "%c%s", (uint8_t)i, sep);
p += len + 1;
ctr -= len + 1;
}
assert(ctr >= 0);
assert((unsigned)ctr < bytes);
*(p - len) = '\0';
WS_Release(ctx->ws, strlen(str) + 1);
return (VCL_STRING)str;
nomem:
VERRNOMEM(ctx, "allocating space for first code units in "
"%s.info_str()", regex->vcl_name);
WS_Release(ctx->ws, 0);
return NULL;
}
ret = pcre2_pattern_info(regex->code, what, &where);
AZ(ret);
if (what == PCRE2_INFO_FIRSTCODEUNIT
|| what == PCRE2_INFO_LASTCODEUNIT) {
if (where == 0)
return "";
if ((str = WS_Printf(ctx->ws, "%c", where)) == NULL) {
VERRNOMEM(ctx, "allocating space for return string in "
"%s.info_str()", regex->vcl_name);
return NULL;
}
return (VCL_STRING)str;
}
if (what == PCRE2_INFO_BSR)
switch(where) {
case PCRE2_BSR_UNICODE:
return("UNICODE");
case PCRE2_BSR_ANYCRLF:
return ("ANYCRLF");
default:
WRONG("unexpected value returned for BSR");
}
assert(what == PCRE2_INFO_NEWLINE);
switch(where) {
case PCRE2_NEWLINE_CR:
return("CR");
case PCRE2_NEWLINE_LF:
return ("LF");
case PCRE2_NEWLINE_CRLF:
return ("CRLF");
case PCRE2_NEWLINE_ANY:
return ("ANY");
case PCRE2_NEWLINE_ANYCRLF:
return ("ANYCRLF");
default:
WRONG("unexpected value returned for BSR");
}
}
......@@ -66,6 +66,9 @@ $Method BOOL .info_bool(ENUM {ALLOW_EMPTY_CLASS, ANCHORED, ALT_BSUX,
$Method INT .info_int(ENUM {BACKREFMAX, CAPTURECOUNT, JITSIZE, MATCHLIMIT,
MAXLOOKBEHIND, MINLENGTH, RECURSIONLIMIT, SIZE})
$Method STRING .info_str(ENUM {BSR, FIRSTCODEUNIT, FIRSTCODEUNITS, LASTCODEUNIT,
NEWLINE}, STRING sep=" ")
$Function BOOL match(PRIV_CALL, PRIV_TASK, STRING pattern, STRING subject,
BOOL allow_empty_class=0, BOOL anchored=0,
ENUM {ANYCRLF, UNICODE} bsr=0, BOOL alt_bsux=0,
......
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