< 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


 613             if (ch != ' ' && ch != '\t' && !CharacterDataLatin1.instance.isWhitespace(ch)) {
 614                 break;
 615             }
 616             right--;
 617         }
 618         return right;
 619     }
 620 
 621     public static String strip(byte[] value) {
 622         int left = indexOfNonWhitespace(value);
 623         if (left == value.length) {
 624             return "";
 625         }
 626         int right = lastIndexOfNonWhitespace(value);
 627         boolean ifChanged = (left > 0) || (right < value.length);
 628         return ifChanged ? newString(value, left, right - left) : null;
 629     }
 630 
 631     public static String stripLeading(byte[] value) {
 632         int left = indexOfNonWhitespace(value);
 633         if (left == value.length) {
 634             return "";
 635         }
 636         return (left != 0) ? newString(value, left, value.length - left) : null;
 637     }
 638 
 639     public static String stripTrailing(byte[] value) {
 640         int right = lastIndexOfNonWhitespace(value);
 641         if (right == 0) {
 642             return "";
 643         }
 644         return (right != value.length) ? newString(value, 0, right) : null;
 645     }
 646 
 647     private final static class LinesSpliterator implements Spliterator<String> {
 648         private byte[] value;
 649         private int index;        // current index, modified on advance/split
 650         private final int fence;  // one past last index
 651 
 652         private LinesSpliterator(byte[] value, int start, int length) {
 653             this.value = value;
 654             this.index = start;
 655             this.fence = start + length;
 656         }
 657 
 658         private int indexOfLineSeparator(int start) {
 659             for (int current = start; current < fence; current++) {
 660                 char ch = getChar(value, current);
 661                 if (ch == '\n' || ch == '\r') {
 662                     return current;
 663                 }


 747         return (char)(val[index] & 0xff);
 748     }
 749 
 750     public static byte[] toBytes(int[] val, int off, int len) {
 751         byte[] ret = new byte[len];
 752         for (int i = 0; i < len; i++) {
 753             int cp = val[off++];
 754             if (!canEncode(cp)) {
 755                 return null;
 756             }
 757             ret[i] = (byte)cp;
 758         }
 759         return ret;
 760     }
 761 
 762     public static byte[] toBytes(char c) {
 763         return new byte[] { (byte)c };
 764     }
 765 
 766     public static String newString(byte[] val, int index, int len) {



 767         return new String(Arrays.copyOfRange(val, index, index + len),
 768                           LATIN1);
 769     }
 770 
 771     public static void fillNull(byte[] val, int index, int end) {
 772         Arrays.fill(val, index, end, (byte)0);
 773     }
 774 
 775     // inflatedCopy byte[] -> char[]
 776     @HotSpotIntrinsicCandidate
 777     public static void inflate(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
 778         for (int i = 0; i < len; i++) {
 779             dst[dstOff++] = (char)(src[srcOff++] & 0xff);
 780         }
 781     }
 782 
 783     // inflatedCopy byte[] -> byte[]
 784     @HotSpotIntrinsicCandidate
 785     public static void inflate(byte[] src, int srcOff, byte[] dst, int dstOff, int len) {
 786         StringUTF16.inflate(src, srcOff, dst, dstOff, len);




 613             if (ch != ' ' && ch != '\t' && !CharacterDataLatin1.instance.isWhitespace(ch)) {
 614                 break;
 615             }
 616             right--;
 617         }
 618         return right;
 619     }
 620 
 621     public static String strip(byte[] value) {
 622         int left = indexOfNonWhitespace(value);
 623         if (left == value.length) {
 624             return "";
 625         }
 626         int right = lastIndexOfNonWhitespace(value);
 627         boolean ifChanged = (left > 0) || (right < value.length);
 628         return ifChanged ? newString(value, left, right - left) : null;
 629     }
 630 
 631     public static String stripLeading(byte[] value) {
 632         int left = indexOfNonWhitespace(value);



 633         return (left != 0) ? newString(value, left, value.length - left) : null;
 634     }
 635 
 636     public static String stripTrailing(byte[] value) {
 637         int right = lastIndexOfNonWhitespace(value);



 638         return (right != value.length) ? newString(value, 0, right) : null;
 639     }
 640 
 641     private final static class LinesSpliterator implements Spliterator<String> {
 642         private byte[] value;
 643         private int index;        // current index, modified on advance/split
 644         private final int fence;  // one past last index
 645 
 646         private LinesSpliterator(byte[] value, int start, int length) {
 647             this.value = value;
 648             this.index = start;
 649             this.fence = start + length;
 650         }
 651 
 652         private int indexOfLineSeparator(int start) {
 653             for (int current = start; current < fence; current++) {
 654                 char ch = getChar(value, current);
 655                 if (ch == '\n' || ch == '\r') {
 656                     return current;
 657                 }


 741         return (char)(val[index] & 0xff);
 742     }
 743 
 744     public static byte[] toBytes(int[] val, int off, int len) {
 745         byte[] ret = new byte[len];
 746         for (int i = 0; i < len; i++) {
 747             int cp = val[off++];
 748             if (!canEncode(cp)) {
 749                 return null;
 750             }
 751             ret[i] = (byte)cp;
 752         }
 753         return ret;
 754     }
 755 
 756     public static byte[] toBytes(char c) {
 757         return new byte[] { (byte)c };
 758     }
 759 
 760     public static String newString(byte[] val, int index, int len) {
 761         if (len == 0) {
 762             return "";
 763         }
 764         return new String(Arrays.copyOfRange(val, index, index + len),
 765                           LATIN1);
 766     }
 767 
 768     public static void fillNull(byte[] val, int index, int end) {
 769         Arrays.fill(val, index, end, (byte)0);
 770     }
 771 
 772     // inflatedCopy byte[] -> char[]
 773     @HotSpotIntrinsicCandidate
 774     public static void inflate(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
 775         for (int i = 0; i < len; i++) {
 776             dst[dstOff++] = (char)(src[srcOff++] & 0xff);
 777         }
 778     }
 779 
 780     // inflatedCopy byte[] -> byte[]
 781     @HotSpotIntrinsicCandidate
 782     public static void inflate(byte[] src, int srcOff, byte[] dst, int dstOff, int len) {
 783         StringUTF16.inflate(src, srcOff, dst, dstOff, len);


< prev index next >