Re: [libvirt] [PATCH 4/5] buf: implement generic virBufferEscape
On 10/11/2011 04:39 AM, Daniel P. Berrange wrote: On Mon, Sep 19, 2011 at 09:13:42PM -0700, Sage Weil wrote: Implement a generic helper to escape a given set of characters with a leading '\'. Generalizes virBufferEscapeSexpr(). Signed-off-by: Sage Weils...@newdream.net @@ -408,14 +428,13 @@ virBufferEscapeSexpr(const virBufferPtr buf, cur = str; out = escaped; while (*cur != 0) { -switch (*cur) { -case '\\': -case '\'': -*out++ = '\\'; -/* fallthrough */ -default: -*out++ = *cur; +for (p = toescape; *p; ++p) { +if (*cur == *p) { strchr is slightly more efficient than hand-rolling this loop. More importantly, you had a logic bug, where you replaced one hard-coded instance of \\', but not the other (the strcspn optimization). Also, I tend to use ' instead of \', although both work, since I favor concise code (I save my ramblings for my emails :). ACK, trivial isolated patch I added you to AUTHORS, squashed this in, then pushed. diff --git i/src/util/buf.c w/src/util/buf.c index 7d0d2d3..5627d8f 100644 --- i/src/util/buf.c +++ w/src/util/buf.c @@ -383,7 +383,7 @@ virBufferEscapeSexpr(const virBufferPtr buf, const char *format, const char *str) { -virBufferEscape(buf, \\\', format, str); +virBufferEscape(buf, \\', format, str); } /** @@ -414,7 +414,7 @@ virBufferEscape(const virBufferPtr buf, return; len = strlen(str); -if (strcspn(str, \\') == len) { +if (strcspn(str, toescape) == len) { virBufferAsprintf(buf, format, str); return; } @@ -428,12 +428,8 @@ virBufferEscape(const virBufferPtr buf, cur = str; out = escaped; while (*cur != 0) { -for (p = toescape; *p; ++p) { -if (*cur == *p) { -*out++ = '\\'; -break; -} -} +if (strchr(toescape, *cur)) +*out++ = '\\'; *out++ = *cur; cur++; } -- Eric Blake ebl...@redhat.com+1-801-349-2682 Libvirt virtualization library http://libvirt.org -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 4/5] buf: implement generic virBufferEscape
On Mon, Sep 19, 2011 at 09:13:42PM -0700, Sage Weil wrote: Implement a generic helper to escape a given set of characters with a leading '\'. Generalizes virBufferEscapeSexpr(). Signed-off-by: Sage Weil s...@newdream.net --- src/libvirt_private.syms |1 + src/util/buf.c | 33 ++--- src/util/buf.h |1 + 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 830222b..d230fab 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -25,6 +25,7 @@ virBufferAddChar; virBufferAsprintf; virBufferContentAndReset; virBufferError; +virBufferEscape; virBufferEscapeSexpr; virBufferEscapeString; virBufferFreeAndReset; diff --git a/src/util/buf.c b/src/util/buf.c index 5002486..7d0d2d3 100644 --- a/src/util/buf.c +++ b/src/util/buf.c @@ -383,9 +383,29 @@ virBufferEscapeSexpr(const virBufferPtr buf, const char *format, const char *str) { +virBufferEscape(buf, \\\', format, str); +} + +/** + * virBufferEscape: + * @buf: the buffer to dump + * @toescape: NULL-terminated list of characters to escape + * @format: a printf like format string but with only one %s parameter + * @str: the string argument which need to be escaped + * + * Do a formatted print with a single string to a buffer. Any characters + * in the provided list are escaped with a preceeding \. + */ +void +virBufferEscape(const virBufferPtr buf, +const char *toescape, +const char *format, +const char *str) +{ int len; char *escaped, *out; const char *cur; +const char *p; if ((format == NULL) || (buf == NULL) || (str == NULL)) return; @@ -408,14 +428,13 @@ virBufferEscapeSexpr(const virBufferPtr buf, cur = str; out = escaped; while (*cur != 0) { -switch (*cur) { -case '\\': -case '\'': -*out++ = '\\'; -/* fallthrough */ -default: -*out++ = *cur; +for (p = toescape; *p; ++p) { +if (*cur == *p) { +*out++ = '\\'; +break; +} } +*out++ = *cur; cur++; } *out = 0; diff --git a/src/util/buf.h b/src/util/buf.h index 06d01ba..e545ed9 100644 --- a/src/util/buf.h +++ b/src/util/buf.h @@ -50,6 +50,7 @@ void virBufferStrcat(const virBufferPtr buf, ...) ATTRIBUTE_SENTINEL; void virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str); void virBufferEscapeSexpr(const virBufferPtr buf, const char *format, const char *str); +void virBufferEscape(const virBufferPtr buf, const char *toescape, const char *format, const char *str); void virBufferURIEncodeString (const virBufferPtr buf, const char *str); # define virBufferAddLit(buf_, literal_string_) \ ACK, trivial isolated patch Daniel -- |: http://berrange.com -o-http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :| -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
[libvirt] [PATCH 4/5] buf: implement generic virBufferEscape
Implement a generic helper to escape a given set of characters with a leading '\'. Generalizes virBufferEscapeSexpr(). Signed-off-by: Sage Weil s...@newdream.net --- src/libvirt_private.syms |1 + src/util/buf.c | 33 ++--- src/util/buf.h |1 + 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 830222b..d230fab 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -25,6 +25,7 @@ virBufferAddChar; virBufferAsprintf; virBufferContentAndReset; virBufferError; +virBufferEscape; virBufferEscapeSexpr; virBufferEscapeString; virBufferFreeAndReset; diff --git a/src/util/buf.c b/src/util/buf.c index 5002486..7d0d2d3 100644 --- a/src/util/buf.c +++ b/src/util/buf.c @@ -383,9 +383,29 @@ virBufferEscapeSexpr(const virBufferPtr buf, const char *format, const char *str) { +virBufferEscape(buf, \\\', format, str); +} + +/** + * virBufferEscape: + * @buf: the buffer to dump + * @toescape: NULL-terminated list of characters to escape + * @format: a printf like format string but with only one %s parameter + * @str: the string argument which need to be escaped + * + * Do a formatted print with a single string to a buffer. Any characters + * in the provided list are escaped with a preceeding \. + */ +void +virBufferEscape(const virBufferPtr buf, +const char *toescape, +const char *format, +const char *str) +{ int len; char *escaped, *out; const char *cur; +const char *p; if ((format == NULL) || (buf == NULL) || (str == NULL)) return; @@ -408,14 +428,13 @@ virBufferEscapeSexpr(const virBufferPtr buf, cur = str; out = escaped; while (*cur != 0) { -switch (*cur) { -case '\\': -case '\'': -*out++ = '\\'; -/* fallthrough */ -default: -*out++ = *cur; +for (p = toescape; *p; ++p) { +if (*cur == *p) { +*out++ = '\\'; +break; +} } +*out++ = *cur; cur++; } *out = 0; diff --git a/src/util/buf.h b/src/util/buf.h index 06d01ba..e545ed9 100644 --- a/src/util/buf.h +++ b/src/util/buf.h @@ -50,6 +50,7 @@ void virBufferStrcat(const virBufferPtr buf, ...) ATTRIBUTE_SENTINEL; void virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str); void virBufferEscapeSexpr(const virBufferPtr buf, const char *format, const char *str); +void virBufferEscape(const virBufferPtr buf, const char *toescape, const char *format, const char *str); void virBufferURIEncodeString (const virBufferPtr buf, const char *str); # define virBufferAddLit(buf_, literal_string_) \ -- 1.7.4.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
[libvirt] [PATCH 4/5] buf: implement generic virBufferEscape
Implement a generic helper to escape a given set of characters with a leading '\'. Generalizes virBufferEscapeSexpr(). Signed-off-by: Sage Weil s...@newdream.net --- src/libvirt_private.syms |1 + src/util/buf.c | 33 ++--- src/util/buf.h |1 + 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 830222b..d230fab 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -25,6 +25,7 @@ virBufferAddChar; virBufferAsprintf; virBufferContentAndReset; virBufferError; +virBufferEscape; virBufferEscapeSexpr; virBufferEscapeString; virBufferFreeAndReset; diff --git a/src/util/buf.c b/src/util/buf.c index 5002486..7d0d2d3 100644 --- a/src/util/buf.c +++ b/src/util/buf.c @@ -383,9 +383,29 @@ virBufferEscapeSexpr(const virBufferPtr buf, const char *format, const char *str) { +virBufferEscape(buf, \\\', format, str); +} + +/** + * virBufferEscape: + * @buf: the buffer to dump + * @toescape: NULL-terminated list of characters to escape + * @format: a printf like format string but with only one %s parameter + * @str: the string argument which need to be escaped + * + * Do a formatted print with a single string to a buffer. Any characters + * in the provided list are escaped with a preceeding \. + */ +void +virBufferEscape(const virBufferPtr buf, +const char *toescape, +const char *format, +const char *str) +{ int len; char *escaped, *out; const char *cur; +const char *p; if ((format == NULL) || (buf == NULL) || (str == NULL)) return; @@ -408,14 +428,13 @@ virBufferEscapeSexpr(const virBufferPtr buf, cur = str; out = escaped; while (*cur != 0) { -switch (*cur) { -case '\\': -case '\'': -*out++ = '\\'; -/* fallthrough */ -default: -*out++ = *cur; +for (p = toescape; *p; ++p) { +if (*cur == *p) { +*out++ = '\\'; +break; +} } +*out++ = *cur; cur++; } *out = 0; diff --git a/src/util/buf.h b/src/util/buf.h index 06d01ba..e545ed9 100644 --- a/src/util/buf.h +++ b/src/util/buf.h @@ -50,6 +50,7 @@ void virBufferStrcat(const virBufferPtr buf, ...) ATTRIBUTE_SENTINEL; void virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str); void virBufferEscapeSexpr(const virBufferPtr buf, const char *format, const char *str); +void virBufferEscape(const virBufferPtr buf, const char *toescape, const char *format, const char *str); void virBufferURIEncodeString (const virBufferPtr buf, const char *str); # define virBufferAddLit(buf_, literal_string_) \ -- 1.7.4.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list