ywkaras commented on a change in pull request #7519:
URL: https://github.com/apache/trafficserver/pull/7519#discussion_r574894028



##########
File path: plugins/experimental/cookie_remap/cookie_remap.cc
##########
@@ -172,13 +240,25 @@ dec_to_hex(type _num, char *hdigits)
 void
 urlencode(std::string &str)
 {
-  size_t pos = 0;
-  for (; pos < str.length(); pos++) {
-    if (!isalnum(str[pos])) {
-      char dec[2];
-      dec_to_hex(str[pos], dec);
-      std::string replacement = "%" + std::string(dec, 2);
-      str.replace(pos, 1, replacement);
+  auto orig = str.size();
+  auto enc  = orig;
+  for (auto c : str) {
+    if (!isalnum(c)) {
+      enc += 2;
+    }
+  }
+  if (enc == orig) {
+    // No changes needed.
+    return;
+  }
+  str.resize(enc);
+  while (orig--) {
+    if (!isalnum(str[orig])) {
+      enc -= 3;
+      dec_to_hex(str[orig], &(str[enc + 1]));
+      str[enc] = '%';
+    } else {
+      str[--enc] = str[orig];

Review comment:
       I optimized this while I was in here, because it's potentially high 
frequency code.  Not related to the main purpose of the PR.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to