From 5963c0367f00df0e5eeb761f1ef77a33c8f54c40 Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sun, 25 Nov 2012 12:08:34 +0100 Subject: Add new function strbuf_add_xml_quoted() Substantially the same code is present in http-push.c and imap-send.c, so make a library function out of it. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- strbuf.c | 26 ++++++++++++++++++++++++++ strbuf.h | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/strbuf.c b/strbuf.c index 0510f76c24..41828f05d8 100644 --- a/strbuf.c +++ b/strbuf.c @@ -428,6 +428,32 @@ void strbuf_add_lines(struct strbuf *out, const char *prefix, strbuf_complete_line(out); } +void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s) +{ + while (*s) { + size_t len = strcspn(s, "\"<>&"); + strbuf_add(buf, s, len); + s += len; + switch (*s) { + case '"': + strbuf_addstr(buf, """); + break; + case '<': + strbuf_addstr(buf, "<"); + break; + case '>': + strbuf_addstr(buf, ">"); + break; + case '&': + strbuf_addstr(buf, "&"); + break; + case 0: + return; + } + s++; + } +} + static int is_rfc3986_reserved(char ch) { switch (ch) { diff --git a/strbuf.h b/strbuf.h index be941ee481..65aa2a032f 100644 --- a/strbuf.h +++ b/strbuf.h @@ -102,6 +102,12 @@ extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap); extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size); +/* + * Append s to sb, with the characters '<', '>', '&' and '"' converted + * into XML entities. + */ +extern void strbuf_addstr_xml_quoted(struct strbuf *sb, const char *s); + static inline void strbuf_complete_line(struct strbuf *sb) { if (sb->len && sb->buf[sb->len - 1] != '\n') -- cgit v1.2.1