aboutsummaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
authorMichael Haggerty <mhagger@alum.mit.edu>2012-11-25 12:08:34 +0100
committerJunio C Hamano <gitster@pobox.com>2012-11-26 13:30:08 -0800
commit5963c0367f00df0e5eeb761f1ef77a33c8f54c40 (patch)
tree3868310a0904f0371f649f2ce5490f2be9250003 /strbuf.c
parentd1eded46fa5ed350ff52654103623090101b3ba6 (diff)
downloadgit-5963c0367f00df0e5eeb761f1ef77a33c8f54c40.tar.gz
git-5963c0367f00df0e5eeb761f1ef77a33c8f54c40.tar.xz
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>
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/strbuf.c b/strbuf.c
index 0510f76c2..41828f05d 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, "&quot;");
+ break;
+ case '<':
+ strbuf_addstr(buf, "&lt;");
+ break;
+ case '>':
+ strbuf_addstr(buf, "&gt;");
+ break;
+ case '&':
+ strbuf_addstr(buf, "&amp;");
+ break;
+ case 0:
+ return;
+ }
+ s++;
+ }
+}
+
static int is_rfc3986_reserved(char ch)
{
switch (ch) {