16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.util.Arrays;
29 import java.util.Locale;
30 import java.util.Spliterator;
31 import java.util.function.Consumer;
32 import java.util.function.IntConsumer;
33 import java.util.stream.Stream;
34 import java.util.stream.StreamSupport;
35 import jdk.internal.HotSpotIntrinsicCandidate;
36 import jdk.internal.vm.annotation.ForceInline;
37 import jdk.internal.vm.annotation.DontInline;
38
39 import static java.lang.String.UTF16;
40 import static java.lang.String.LATIN1;
41
42 final class StringUTF16 {
43
44 public static byte[] newBytesFor(int len) {
45 if (len < 0) {
46 throw new NegativeArraySizeException();
47 }
48 if (len > MAX_LENGTH) {
49 throw new OutOfMemoryError("UTF16 String size is " + len +
50 ", should be less than " + MAX_LENGTH);
51 }
52 return new byte[len << 1];
53 }
54
55 @HotSpotIntrinsicCandidate
632 ? StringLatin1.indexOf(value, targ) :
633 (String.COMPACT_STRINGS && targLat1)
634 ? indexOfLatin1(value, targ)
635 : indexOf(value, targ);
636 if (i < 0) {
637 return null; // for string to return this;
638 }
639
640 // find and store indices of substrings to replace
641 int j, p = 0;
642 int[] pos = new int[16];
643 pos[0] = i;
644 i += targLen;
645 while ((j = ((String.COMPACT_STRINGS && valLat1)
646 ? StringLatin1.indexOf(value, valLen, targ, targLen, i) :
647 (String.COMPACT_STRINGS && targLat1)
648 ? indexOfLatin1(value, valLen, targ, targLen, i)
649 : indexOf(value, valLen, targ, targLen, i))) > 0)
650 {
651 if (++p == pos.length) {
652 int cap = p + (p >> 1);
653 // overflow-conscious code
654 if (cap - MAX_ARRAY_SIZE > 0) {
655 if (p == MAX_ARRAY_SIZE) {
656 throw new OutOfMemoryError();
657 }
658 cap = MAX_ARRAY_SIZE;
659 }
660 pos = Arrays.copyOf(pos, cap);
661 }
662 pos[p] = j;
663 i = j + targLen;
664 }
665
666 int resultLen;
667 try {
668 resultLen = Math.addExact(valLen,
669 Math.multiplyExact(++p, replLen - targLen));
670 } catch (ArithmeticException ignored) {
671 throw new OutOfMemoryError();
672 }
673 if (resultLen == 0) {
674 return "";
675 }
676
677 byte[] result = newBytesFor(resultLen);
678 int posFrom = 0, posTo = 0;
679 for (int q = 0; q < p; ++q) {
680 int nextPos = pos[q];
1536 }
1537 }
1538
1539 ////////////////////////////////////////////////////////////////
1540
1541 private static native boolean isBigEndian();
1542
1543 static final int HI_BYTE_SHIFT;
1544 static final int LO_BYTE_SHIFT;
1545 static {
1546 if (isBigEndian()) {
1547 HI_BYTE_SHIFT = 8;
1548 LO_BYTE_SHIFT = 0;
1549 } else {
1550 HI_BYTE_SHIFT = 0;
1551 LO_BYTE_SHIFT = 8;
1552 }
1553 }
1554
1555 static final int MAX_LENGTH = Integer.MAX_VALUE >> 1;
1556
1557
1558 /**
1559 * The maximum size of array to allocate (unless necessary).
1560 * Some VMs reserve some header words in an array.
1561 * Attempts to allocate larger arrays may result in
1562 * OutOfMemoryError: Requested array size exceeds VM limit
1563 */
1564 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
1565
1566 // Used by trusted callers. Assumes all necessary bounds checks have
1567 // been done by the caller.
1568
1569 /**
1570 * This is a variant of {@link Integer#getChars(int, int, byte[])}, but for
1571 * UTF-16 coder.
1572 *
1573 * @param i value to convert
1574 * @param index next index, after the least significant digit
1575 * @param buf target buffer, UTF16-coded.
1576 * @return index of the most significant digit or minus sign, if present
1577 */
1578 static int getChars(int i, int index, byte[] buf) {
1579 int q, r;
1580 int charPos = index;
1581
1582 boolean negative = (i < 0);
1583 if (!negative) {
1584 i = -i;
|
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang;
27
28 import java.util.Arrays;
29 import java.util.Locale;
30 import java.util.Spliterator;
31 import java.util.function.Consumer;
32 import java.util.function.IntConsumer;
33 import java.util.stream.Stream;
34 import java.util.stream.StreamSupport;
35 import jdk.internal.HotSpotIntrinsicCandidate;
36 import jdk.internal.util.ArraysSupport;
37 import jdk.internal.vm.annotation.ForceInline;
38 import jdk.internal.vm.annotation.DontInline;
39
40 import static java.lang.String.UTF16;
41 import static java.lang.String.LATIN1;
42
43 final class StringUTF16 {
44
45 public static byte[] newBytesFor(int len) {
46 if (len < 0) {
47 throw new NegativeArraySizeException();
48 }
49 if (len > MAX_LENGTH) {
50 throw new OutOfMemoryError("UTF16 String size is " + len +
51 ", should be less than " + MAX_LENGTH);
52 }
53 return new byte[len << 1];
54 }
55
56 @HotSpotIntrinsicCandidate
633 ? StringLatin1.indexOf(value, targ) :
634 (String.COMPACT_STRINGS && targLat1)
635 ? indexOfLatin1(value, targ)
636 : indexOf(value, targ);
637 if (i < 0) {
638 return null; // for string to return this;
639 }
640
641 // find and store indices of substrings to replace
642 int j, p = 0;
643 int[] pos = new int[16];
644 pos[0] = i;
645 i += targLen;
646 while ((j = ((String.COMPACT_STRINGS && valLat1)
647 ? StringLatin1.indexOf(value, valLen, targ, targLen, i) :
648 (String.COMPACT_STRINGS && targLat1)
649 ? indexOfLatin1(value, valLen, targ, targLen, i)
650 : indexOf(value, valLen, targ, targLen, i))) > 0)
651 {
652 if (++p == pos.length) {
653 pos = Arrays.copyOf(pos, ArraysSupport.calcLength(p, 1, p >> 1));
654 }
655 pos[p] = j;
656 i = j + targLen;
657 }
658
659 int resultLen;
660 try {
661 resultLen = Math.addExact(valLen,
662 Math.multiplyExact(++p, replLen - targLen));
663 } catch (ArithmeticException ignored) {
664 throw new OutOfMemoryError();
665 }
666 if (resultLen == 0) {
667 return "";
668 }
669
670 byte[] result = newBytesFor(resultLen);
671 int posFrom = 0, posTo = 0;
672 for (int q = 0; q < p; ++q) {
673 int nextPos = pos[q];
1529 }
1530 }
1531
1532 ////////////////////////////////////////////////////////////////
1533
1534 private static native boolean isBigEndian();
1535
1536 static final int HI_BYTE_SHIFT;
1537 static final int LO_BYTE_SHIFT;
1538 static {
1539 if (isBigEndian()) {
1540 HI_BYTE_SHIFT = 8;
1541 LO_BYTE_SHIFT = 0;
1542 } else {
1543 HI_BYTE_SHIFT = 0;
1544 LO_BYTE_SHIFT = 8;
1545 }
1546 }
1547
1548 static final int MAX_LENGTH = Integer.MAX_VALUE >> 1;
1549
1550 // Used by trusted callers. Assumes all necessary bounds checks have
1551 // been done by the caller.
1552
1553 /**
1554 * This is a variant of {@link Integer#getChars(int, int, byte[])}, but for
1555 * UTF-16 coder.
1556 *
1557 * @param i value to convert
1558 * @param index next index, after the least significant digit
1559 * @param buf target buffer, UTF16-coded.
1560 * @return index of the most significant digit or minus sign, if present
1561 */
1562 static int getChars(int i, int index, byte[] buf) {
1563 int q, r;
1564 int charPos = index;
1565
1566 boolean negative = (i < 0);
1567 if (!negative) {
1568 i = -i;
|