Skip to content

Commit c505116

Browse files
peffgitster
authored andcommitted
strbuf: add strbuf_add*_urlencode
This just follows the rfc3986 rules for percent-encoding url data into a strbuf. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6320358 commit c505116

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

strbuf.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,40 @@ int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
397397

398398
return len;
399399
}
400+
401+
static int is_rfc3986_reserved(char ch)
402+
{
403+
switch (ch) {
404+
case '!': case '*': case '\'': case '(': case ')': case ';':
405+
case ':': case '@': case '&': case '=': case '+': case '$':
406+
case ',': case '/': case '?': case '#': case '[': case ']':
407+
return 1;
408+
}
409+
return 0;
410+
}
411+
412+
static int is_rfc3986_unreserved(char ch)
413+
{
414+
return isalnum(ch) ||
415+
ch == '-' || ch == '_' || ch == '.' || ch == '~';
416+
}
417+
418+
void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
419+
int reserved)
420+
{
421+
strbuf_grow(sb, len);
422+
while (len--) {
423+
char ch = *s++;
424+
if (is_rfc3986_unreserved(ch) ||
425+
(!reserved && is_rfc3986_reserved(ch)))
426+
strbuf_addch(sb, ch);
427+
else
428+
strbuf_addf(sb, "%%%02x", ch);
429+
}
430+
}
431+
432+
void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
433+
int reserved)
434+
{
435+
strbuf_add_urlencode(sb, s, strlen(s), reserved);
436+
}

strbuf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,9 @@ extern int launch_editor(const char *path, struct strbuf *buffer, const char *co
115115
extern int strbuf_branchname(struct strbuf *sb, const char *name);
116116
extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
117117

118+
extern void strbuf_add_urlencode(struct strbuf *, const char *, size_t,
119+
int reserved);
120+
extern void strbuf_addstr_urlencode(struct strbuf *, const char *,
121+
int reserved);
122+
118123
#endif /* STRBUF_H */

0 commit comments

Comments
 (0)