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
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);
}
+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)
{
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;
}