< prev index next >

src/java.base/share/classes/java/lang/StringLatin1.java

Print this page
rev 58157 : 8240094: Optimize empty substring handling
Reviewed-by: redestad, igerasim, jlaskey
Contributed-by: sergei.tsypanov@yandex.ru

@@ -628,21 +628,15 @@
         return ifChanged ? newString(value, left, right - left) : null;
     }
 
     public static String stripLeading(byte[] value) {
         int left = indexOfNonWhitespace(value);
-        if (left == value.length) {
-            return "";
-        }
         return (left != 0) ? newString(value, left, value.length - left) : null;
     }
 
     public static String stripTrailing(byte[] value) {
         int right = lastIndexOfNonWhitespace(value);
-        if (right == 0) {
-            return "";
-        }
         return (right != value.length) ? newString(value, 0, right) : null;
     }
 
     private final static class LinesSpliterator implements Spliterator<String> {
         private byte[] value;

@@ -762,10 +756,13 @@
     public static byte[] toBytes(char c) {
         return new byte[] { (byte)c };
     }
 
     public static String newString(byte[] val, int index, int len) {
+        if (len == 0) {
+            return "";
+        }
         return new String(Arrays.copyOfRange(val, index, index + len),
                           LATIN1);
     }
 
     public static void fillNull(byte[] val, int index, int end) {
< prev index next >