]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
escape: use table lookup when adding %-codes to output
authorDaniel Stenberg <daniel@haxx.se>
Tue, 31 Jan 2023 11:34:08 +0000 (12:34 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Tue, 31 Jan 2023 21:42:21 +0000 (22:42 +0100)
On my dev host, this code runs 7.8 times faster.

Closes #10377

lib/escape.c

index 93db06cbd822e914944d753457bc264198312947..56aa2b39888866b17e8335fe78d0b1c2de1ebc60 100644 (file)
@@ -97,7 +97,7 @@ char *curl_easy_escape(struct Curl_easy *data, const char *string,
     return strdup("");
 
   while(length--) {
-    unsigned char in = *string; /* we need to treat the characters unsigned */
+    unsigned char in = *string++; /* treat the characters unsigned */
 
     if(Curl_isunreserved(in)) {
       /* append this */
@@ -106,10 +106,13 @@ char *curl_easy_escape(struct Curl_easy *data, const char *string,
     }
     else {
       /* encode it */
-      if(Curl_dyn_addf(&d, "%%%02X", in))
+      const char hex[] = "0123456789ABCDEF";
+      char out[3]={'%'};
+      out[1] = hex[in>>4];
+      out[2] = hex[in & 0xf];
+      if(Curl_dyn_addn(&d, out, 3))
         return NULL;
     }
-    string++;
   }
 
   return Curl_dyn_ptr(&d);