summaryrefslogtreecommitdiff
path: root/lib/urlapi.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2023-02-01 00:17:28 +0100
committerDaniel Stenberg <daniel@haxx.se>2023-02-01 23:05:51 +0100
commit7305ca63e2b408a1fe9dc1c59f4e833bea942ee4 (patch)
tree5510b07ea8dc07dfc3d22f092f083b314f735b75 /lib/urlapi.c
parent804d5293f899705198c310c6f76c61a8859da8d7 (diff)
downloadcurl-7305ca63e2b408a1fe9dc1c59f4e833bea942ee4.tar.gz
urlapi: avoid Curl_dyn_addf() for hex outputs
Inspired by the recent fixes to escape.c, we should avoid calling Curl_dyn_addf() in loops, perhaps in particular when adding something so simple as %HH codes - for performance reasons. This change makes the same thing for the URL parser's two URL-encoding loops. Closes #10384
Diffstat (limited to 'lib/urlapi.c')
-rw-r--r--lib/urlapi.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/lib/urlapi.c b/lib/urlapi.c
index ef9ec4169..f22adc7af 100644
--- a/lib/urlapi.c
+++ b/lib/urlapi.c
@@ -117,14 +117,11 @@ static const char *find_host_sep(const char *url)
}
/*
- * Decide in an encoding-independent manner whether a character in a URL must
- * be escaped. This is used in urlencode_str().
+ * Decide whether a character in a URL must be escaped.
*/
-static bool urlchar_needs_escaping(int c)
-{
- return !(ISCNTRL(c) || ISSPACE(c) || ISGRAPH(c));
-}
+#define urlchar_needs_escaping(c) (!(ISCNTRL(c) || ISSPACE(c) || ISGRAPH(c)))
+static const char hexdigits[] = "0123456789abcdef";
/* urlencode_str() writes data into an output dynbuf and URL-encodes the
* spaces in the source URL accordingly.
*
@@ -168,7 +165,10 @@ static CURLUcode urlencode_str(struct dynbuf *o, const char *url,
left = FALSE;
if(urlchar_needs_escaping(*iptr)) {
- if(Curl_dyn_addf(o, "%%%02x", *iptr))
+ char out[3]={'%'};
+ out[1] = hexdigits[*iptr>>4];
+ out[2] = hexdigits[*iptr & 0xf];
+ if(Curl_dyn_addn(o, out, 3))
return CURLUE_OUT_OF_MEMORY;
}
else {
@@ -1836,7 +1836,10 @@ CURLUcode curl_url_set(CURLU *u, CURLUPart what,
return CURLUE_OUT_OF_MEMORY;
}
else {
- result = Curl_dyn_addf(&enc, "%%%02x", *i);
+ char out[3]={'%'};
+ out[1] = hexdigits[*i>>4];
+ out[2] = hexdigits[*i & 0xf];
+ result = Curl_dyn_addn(&enc, out, 3);
if(result)
return CURLUE_OUT_OF_MEMORY;
}