aboutsummaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2011-12-10 05:34:20 -0500
committerJunio C Hamano <gitster@pobox.com>2011-12-12 16:08:27 -0800
commitc505116b91d3c92f0c3066cb9806773d2df11088 (patch)
tree5a78822db20c9dbd81557fb376057eac1e1b288e /strbuf.c
parent6320358e31d42f25fa4ac5eb4e72574630de1692 (diff)
downloadgit-c505116b91d3c92f0c3066cb9806773d2df11088.tar.gz
git-c505116b91d3c92f0c3066cb9806773d2df11088.tar.xz
strbuf: add strbuf_add*_urlencode
This just follows the rfc3986 rules for percent-encoding url data into a strbuf. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/strbuf.c b/strbuf.c
index 3ad2cc001..60e5e598d 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -397,3 +397,40 @@ int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
return len;
}
+
+static int is_rfc3986_reserved(char ch)
+{
+ switch (ch) {
+ case '!': case '*': case '\'': case '(': case ')': case ';':
+ case ':': case '@': case '&': case '=': case '+': case '$':
+ case ',': case '/': case '?': case '#': case '[': case ']':
+ return 1;
+ }
+ return 0;
+}
+
+static int is_rfc3986_unreserved(char ch)
+{
+ return isalnum(ch) ||
+ ch == '-' || ch == '_' || ch == '.' || ch == '~';
+}
+
+void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
+ int reserved)
+{
+ strbuf_grow(sb, len);
+ while (len--) {
+ char ch = *s++;
+ if (is_rfc3986_unreserved(ch) ||
+ (!reserved && is_rfc3986_reserved(ch)))
+ strbuf_addch(sb, ch);
+ else
+ strbuf_addf(sb, "%%%02x", ch);
+ }
+}
+
+void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
+ int reserved)
+{
+ strbuf_add_urlencode(sb, s, strlen(s), reserved);
+}