Skip to contentSkip to conversation

http: use strbuf API in quote_ref_url

PATCH In review Tay Ray Chuan 2 participants 2 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 http: use strbuf API in quote_ref_url

In addition, ''quote_ref_url'' inserts a slash between the base URL and remote ref path only if needed. Previously, this insertion wasn't contingent on the lack of a separating slash.

---
 http.c |   29 ++++++++++-------------------
 1 files changed, 10 insertions(+), 19 deletions(-)
http.c +10 -19
index cdedeb6..9de4130 100644
@@ -577,31 +577,22 @@ static inline int hex(int v)
 
 static char *quote_ref_url(const char *base, const char *ref)
 {
+	struct strbuf buf = STRBUF_INIT;
 	const char *cp;
-	char *dp, *qref;
-	int len, baselen, ch;
+	int ch;
+
+	strbuf_addstr(&buf, base);
+	if (strcmp(base+strlen(base)-1, "/") && strcmp(ref, "/"))
+		strbuf_addstr(&buf, "/");
 
-	baselen = strlen(base);
-	len = baselen + 2; /* '/' after base and terminating NUL */
-	for (cp = ref; (ch = *cp) != 0; cp++, len++)
-		if (needs_quote(ch))
-			len += 2; /* extra two hex plus replacement % */
-	qref = xmalloc(len);
-	memcpy(qref, base, baselen);
-	dp = qref + baselen;
-	*(dp++) = '/';
 	for (cp = ref; (ch = *cp) != 0; cp++) {
-		if (needs_quote(ch)) {
-			*dp++ = '%';
-			*dp++ = hex((ch >> 4) & 0xF);
-			*dp++ = hex(ch & 0xF);
-		}
+		if (needs_quote(ch))
+			strbuf_addf(&buf, "%%%02x", ch);
 		else
-			*dp++ = ch;
+			strbuf_addch(&buf, *cp);
 	}
-	*dp = 0;
 
-	return qref;
+	return strbuf_detach(&buf, NULL);
 }
 
 int http_fetch_ref(const char *base, struct ref *ref)
Signature · 1 line
1.6.2.rc1
#1 lore raw

Hi,

Quoted 7 lines · Tay Ray Chuan

In addition, ''quote_ref_url'' inserts a slash between the base URL and remote ref path only if needed. Previously, this insertion wasn't contingent on the lack of a separating slash.

---

I would prefer to give my ACK explicitely... :-)

Quoted 10 lines
 http.c |   29 ++++++++++-------------------
 1 files changed, 10 insertions(+), 19 deletions(-)
http.c +0 -0
index cdedeb6..9de4130 100644
@@ -577,31 +577,22 @@ static inline int hex(int v)
 
 static char *quote_ref_url(const char *base, const char *ref)

http.c static inline int hex(int v) · line 586

Quoted 10 lines
http.c +6 -2
 {
+	struct strbuf buf = STRBUF_INIT;
 	const char *cp;
-	char *dp, *qref;
-	int len, baselen, ch;
+	int ch;
+
+	strbuf_addstr(&buf, base);
+	if (strcmp(base+strlen(base)-1, "/") && strcmp(ref, "/"))
+		strbuf_addstr(&buf, "/");

I would not have scratched my head that much if it read like this:

	if (buf.len && buf.buf[buf.len - 1] != '/' && *ref != '/')
		strbuf_addch(&buf, '/');

http.c static inline int hex(int v) · line 593

Quoted 12 lines
http.c +3 -6
 	for (cp = ref; (ch = *cp) != 0; cp++) {
-		if (needs_quote(ch)) {
-			*dp++ = '%';
-			*dp++ = hex((ch >> 4) & 0xF);
-			*dp++ = hex(ch & 0xF);
-		}
+		if (needs_quote(ch))
+			strbuf_addf(&buf, "%%%02x", ch);
 		else
-			*dp++ = ch;
+			strbuf_addch(&buf, *cp);
 	}

Seems as if you could remove even the curly brackets here.

Other than that, it indeed looks like an ACK from me...

Ciao,
Dscho