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 <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Nov 25, 2012 at 12:08 UTC
5963c0367f00df0e5eeb761f1ef77a33c8f54c40
2 files changed
+32
strbuf.c
+26
@@ -428,6 +428,32 @@ void strbuf_add_lines(struct strbuf *out, const char *prefix,
428
strbuf_complete_line(out);
429
}
430
431
+void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
432
+{
433
+ while (*s) {
434
+ size_t len = strcspn(s, "\"<>&");
435
+ strbuf_add(buf, s, len);
436
+ s += len;
437
+ switch (*s) {
438
+ case '"':
439
+ strbuf_addstr(buf, """);
440
+ break;
441
+ case '<':
442
+ strbuf_addstr(buf, "<");
443
+ break;
444
+ case '>':
445
+ strbuf_addstr(buf, ">");
446
+ break;
447
+ case '&':
448
+ strbuf_addstr(buf, "&");
449
+ break;
450
+ case 0:
451
+ return;
452
+ }
453
+ s++;
454
+ }
455
+}
456
+
457
static int is_rfc3986_reserved(char ch)
458
{
459
switch (ch) {
strbuf.h
+6
@@ -102,6 +102,12 @@ extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
102
103
extern void strbuf_add_lines(struct strbuf *sb, const char *prefix, const char *buf, size_t size);
104
105
+/*
106
+ * Append s to sb, with the characters '<', '>', '&' and '"' converted
107
+ * into XML entities.
108
+ */
109
+extern void strbuf_addstr_xml_quoted(struct strbuf *sb, const char *s);
110
+
111
static inline void strbuf_complete_line(struct strbuf *sb)
112
{
113
if (sb->len && sb->buf[sb->len - 1] != '\n')