summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 6320358)
raw | patch | inline | side by side (parent: 6320358)
author | Jeff King <peff@peff.net> | |
Sat, 10 Dec 2011 10:34:20 +0000 (05:34 -0500) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Tue, 13 Dec 2011 00:08:27 +0000 (16:08 -0800) |
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>
url data into a strbuf.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
strbuf.c | patch | blob | history | |
strbuf.h | patch | blob | history |
diff --git a/strbuf.c b/strbuf.c
index 3ad2cc00160fbf24e1e4904bb37ce44e8c414ce5..60e5e598dd4e49e8b2fd37b0bbb5040639b4411d 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
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);
+}
diff --git a/strbuf.h b/strbuf.h
index 46a33f8c46985c4377071011d6ea48d6d3fe5331..cecd48c45adaaaf55e7139cf534c97ded1073c48 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -115,4 +115,9 @@ extern int launch_editor(const char *path, struct strbuf *buffer, const char *co
extern int strbuf_branchname(struct strbuf *sb, const char *name);
extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
+extern void strbuf_add_urlencode(struct strbuf *, const char *, size_t,
+ int reserved);
+extern void strbuf_addstr_urlencode(struct strbuf *, const char *,
+ int reserved);
+
#endif /* STRBUF_H */