< prev index next >

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

Print this page
rev 49110 : 8198955: String#repeat loop optimization
Reviewed-by: igerasim, redestad


2988         if (count == 1) {
2989             return this;
2990         }
2991         final int len = value.length;
2992         if (len == 0 || count == 0) {
2993             return "";
2994         }
2995         if (len == 1) {
2996             final byte[] single = new byte[count];
2997             Arrays.fill(single, value[0]);
2998             return new String(single, coder);
2999         }
3000         if (Integer.MAX_VALUE / count < len) {
3001             throw new OutOfMemoryError("Repeating " + len + " bytes String " + count +
3002                     " times will produce a String exceeding maximum size.");
3003         }
3004         final int limit = len * count;
3005         final byte[] multiple = new byte[limit];
3006         System.arraycopy(value, 0, multiple, 0, len);
3007         int copied = len;
3008         for (int next = copied << 1; next < limit && 0 < next; next = next << 1) {
3009             System.arraycopy(multiple, 0, multiple, copied, copied);
3010             copied = next;
3011         }
3012         System.arraycopy(multiple, 0, multiple, copied, limit - copied);
3013         return new String(multiple, coder);
3014     }
3015 
3016     ////////////////////////////////////////////////////////////////
3017 
3018     /**
3019      * Copy character bytes from this string into dst starting at dstBegin.
3020      * This method doesn't perform any range checking.
3021      *
3022      * Invoker guarantees: dst is in UTF16 (inflate itself for asb), if two
3023      * coders are different, and dst is big enough (range check)
3024      *
3025      * @param dstBegin  the char index, not offset of byte[]
3026      * @param coder     the coder of dst[]
3027      */
3028     void getBytes(byte dst[], int dstBegin, byte coder) {
3029         if (coder() == coder) {
3030             System.arraycopy(value, 0, dst, dstBegin << coder, value.length);




2988         if (count == 1) {
2989             return this;
2990         }
2991         final int len = value.length;
2992         if (len == 0 || count == 0) {
2993             return "";
2994         }
2995         if (len == 1) {
2996             final byte[] single = new byte[count];
2997             Arrays.fill(single, value[0]);
2998             return new String(single, coder);
2999         }
3000         if (Integer.MAX_VALUE / count < len) {
3001             throw new OutOfMemoryError("Repeating " + len + " bytes String " + count +
3002                     " times will produce a String exceeding maximum size.");
3003         }
3004         final int limit = len * count;
3005         final byte[] multiple = new byte[limit];
3006         System.arraycopy(value, 0, multiple, 0, len);
3007         int copied = len;
3008         for (; copied < limit - copied; copied <<= 1) {
3009             System.arraycopy(multiple, 0, multiple, copied, copied);

3010         }
3011         System.arraycopy(multiple, 0, multiple, copied, limit - copied);
3012         return new String(multiple, coder);
3013     }
3014 
3015     ////////////////////////////////////////////////////////////////
3016 
3017     /**
3018      * Copy character bytes from this string into dst starting at dstBegin.
3019      * This method doesn't perform any range checking.
3020      *
3021      * Invoker guarantees: dst is in UTF16 (inflate itself for asb), if two
3022      * coders are different, and dst is big enough (range check)
3023      *
3024      * @param dstBegin  the char index, not offset of byte[]
3025      * @param coder     the coder of dst[]
3026      */
3027     void getBytes(byte dst[], int dstBegin, byte coder) {
3028         if (coder() == coder) {
3029             System.arraycopy(value, 0, dst, dstBegin << coder, value.length);


< prev index next >