< prev index next >

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

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

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

@@ -1130,10 +1124,13 @@
             putChar(val, index++, str[off++]);
         }
     }
 
     public static String newString(byte[] val, int index, int len) {
+        if (len == 0) {
+            return "";
+        }
         if (String.COMPACT_STRINGS) {
             byte[] buf = compress(val, index, len);
             if (buf != null) {
                 return new String(buf, LATIN1);
             }
< prev index next >