< prev index next >

src/java.base/share/classes/java/util/regex/Pattern.java

Print this page
rev 54772 : [mq]: 8223593-Refactor-code-for-reallocating-storage

@@ -41,10 +41,11 @@
 import java.util.Spliterators;
 import java.util.function.Predicate;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 
+import jdk.internal.util.ArraysSupport;
 
 /**
  * A compiled representation of a regular expression.
  *
  * <p> A regular expression, specified as a string, must first be compiled into

@@ -2313,17 +2314,18 @@
         } else {
             return newSlice(buffer, first, hasSupplementary);
         }
     }
 
-    private void append(int ch, int len) {
-        if (len >= buffer.length) {
-            int[] tmp = new int[len+len];
-            System.arraycopy(buffer, 0, tmp, 0, len);
-            buffer = tmp;
+    private void append(int ch, int index) {
+        int oldCapacity = buffer.length;
+        if (index - oldCapacity >= 0) {
+            int newCapacity = ArraysSupport.newCapacity(oldCapacity,
+                    1 + index - oldCapacity, oldCapacity);
+            buffer = Arrays.copyOf(buffer, newCapacity);
         }
-        buffer[len] = ch;
+        buffer[index] = ch;
     }
 
     /**
      * Parses a backref greedily, taking as many numbers as it
      * can. The first digit is always treated as a backref, but
< prev index next >