Browse Source

Optimize loop in URL decoding #110235 (#110237)

when the target character is detected, the loop ends
---------
Co-authored-by: shane-zfx <zffxxx7@gmail.com>
404 1 year ago
parent
commit
765c2f1875

+ 7 - 0
docs/changelog/110237.yaml

@@ -0,0 +1,7 @@
+pr: 110237
+summary: Optimize the loop processing of URL decoding
+area: Infra/REST API
+type: enhancement
+issues:
+  - 110235
+

+ 8 - 8
server/src/main/java/org/elasticsearch/rest/RestUtils.java

@@ -146,17 +146,17 @@ public class RestUtils {
     }
 
     private static boolean decodingNeeded(String s, int size, boolean plusAsSpace) {
-        boolean decodingNeeded = false;
-        for (int i = 0; i < size; i++) {
+        if (Strings.isEmpty(s)) {
+            return false;
+        }
+        int num = Math.min(s.length(), size);
+        for (int i = 0; i < num; i++) {
             final char c = s.charAt(i);
-            if (c == '%') {
-                i++;  // We can skip at least one char, e.g. `%%'.
-                decodingNeeded = true;
-            } else if (plusAsSpace && c == '+') {
-                decodingNeeded = true;
+            if (c == '%' || (plusAsSpace && c == '+')) {
+                return true;
             }
         }
-        return decodingNeeded;
+        return false;
     }
 
     @SuppressWarnings("fallthrough")