1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-21 23:16:28 +02:00

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>
This commit is contained in:
Jeff King 2011-12-10 05:34:20 -05:00 committed by Junio C Hamano
parent 6320358e31
commit c505116b91
2 changed files with 42 additions and 0 deletions

View File

@ -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);
}

View File

@ -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 */