mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 05:35:25 +00:00
util: introduce virBufferEscapeRegex
Add a helper to escape all possible meta-characters used for POSIX extended regular expressions. Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
parent
f15f155403
commit
0918b84968
@ -1312,6 +1312,7 @@ virBufferCurrentContent;
|
||||
virBufferError;
|
||||
virBufferEscape;
|
||||
virBufferEscapeN;
|
||||
virBufferEscapeRegex;
|
||||
virBufferEscapeSexpr;
|
||||
virBufferEscapeShell;
|
||||
virBufferEscapeString;
|
||||
|
@ -555,6 +555,25 @@ virBufferEscapeSexpr(virBufferPtr buf,
|
||||
virBufferEscape(buf, '\\', "\\'", format, str);
|
||||
}
|
||||
|
||||
/**
|
||||
* virBufferEscapeRegex:
|
||||
* @buf: the buffer to append to
|
||||
* @format: a printf like format string but with only one %s parameter
|
||||
* @str: the string argument which needs to be escaped
|
||||
*
|
||||
* Do a formatted print with a single string to a buffer. The @str is
|
||||
* escaped to avoid using POSIX extended regular expression meta-characters.
|
||||
* Escaping is not applied to characters specified in @format. Auto
|
||||
* indentation may be applied.
|
||||
*/
|
||||
void
|
||||
virBufferEscapeRegex(virBufferPtr buf,
|
||||
const char *format,
|
||||
const char *str)
|
||||
{
|
||||
virBufferEscape(buf, '\\', "^$.|?*+()[]{}\\", format, str);
|
||||
}
|
||||
|
||||
/**
|
||||
* virBufferEscape:
|
||||
* @buf: the buffer to append to
|
||||
|
@ -88,6 +88,9 @@ void virBufferEscapeString(virBufferPtr buf, const char *format,
|
||||
const char *str);
|
||||
void virBufferEscapeSexpr(virBufferPtr buf, const char *format,
|
||||
const char *str);
|
||||
void virBufferEscapeRegex(virBufferPtr buf,
|
||||
const char *format,
|
||||
const char *str);
|
||||
void virBufferEscapeShell(virBufferPtr buf, const char *str);
|
||||
void virBufferURIEncodeString(virBufferPtr buf, const char *str);
|
||||
|
||||
|
@ -404,6 +404,35 @@ testBufEscapeN(const void *opaque)
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
testBufEscapeRegex(const void *opaque)
|
||||
{
|
||||
const struct testBufAddStrData *data = opaque;
|
||||
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||
char *actual;
|
||||
int ret = -1;
|
||||
|
||||
virBufferEscapeRegex(&buf, "%s", data->data);
|
||||
|
||||
if (!(actual = virBufferContentAndReset(&buf))) {
|
||||
VIR_TEST_DEBUG("testBufEscapeN: buf is empty");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (STRNEQ_NULLABLE(actual, data->expect)) {
|
||||
VIR_TEST_DEBUG("testBufEscapeN: Strings don't match:\n");
|
||||
virTestDifference(stderr, data->expect, actual);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
cleanup:
|
||||
VIR_FREE(actual);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
testBufSetIndent(const void *opaque ATTRIBUTE_UNUSED)
|
||||
{
|
||||
@ -492,6 +521,17 @@ mymain(void)
|
||||
DO_TEST_ESCAPEN("equal=escape", "equal\\=escape");
|
||||
DO_TEST_ESCAPEN("comma,equal=escape", "comma,,equal\\=escape");
|
||||
|
||||
#define DO_TEST_ESCAPE_REGEX(data, expect) \
|
||||
do { \
|
||||
struct testBufAddStrData info = { data, expect }; \
|
||||
if (virTestRun("Buf: EscapeRegex", testBufEscapeRegex, &info) < 0) \
|
||||
ret = -1; \
|
||||
} while (0)
|
||||
|
||||
DO_TEST_ESCAPE_REGEX("noescape", "noescape");
|
||||
DO_TEST_ESCAPE_REGEX("^$.|?*+()[]{}\\",
|
||||
"\\^\\$\\.\\|\\?\\*\\+\\(\\)\\[\\]\\{\\}\\\\");
|
||||
|
||||
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user