X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=strbuf.c;h=ff0b96b4162bd92162a7eb05eee5be7a5ec2b6ba;hb=08265798e1ff6abc1b0aaff31c1471f83bd51425;hp=a849705197a6ad3d0d2ab9de22b269de6b4fc2b1;hpb=b2dd02112052aff875e8dc7d12b9915c91feafe2;p=git.git diff --git a/strbuf.c b/strbuf.c index a84970519..ff0b96b41 100644 --- a/strbuf.c +++ b/strbuf.c @@ -411,3 +411,40 @@ void strbuf_add_lines(struct strbuf *out, const char *prefix, } strbuf_complete_line(out); } + +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); +}