|
35 | 35 |
|
36 | 36 | import jdk.internal.misc.Unsafe; |
37 | 37 | import jdk.internal.util.ArraysSupport; |
| 38 | +import jdk.internal.util.DecimalDigits; |
38 | 39 | import jdk.internal.vm.annotation.ForceInline; |
39 | 40 | import jdk.internal.vm.annotation.IntrinsicCandidate; |
40 | 41 |
|
@@ -1512,6 +1513,20 @@ public static int codePointCountSB(byte[] val, int beginIndex, int endIndex) { |
1512 | 1513 | return codePointCount(val, beginIndex, endIndex, true /* checked */); |
1513 | 1514 | } |
1514 | 1515 |
|
| 1516 | + public static int getChars(int i, int begin, int end, byte[] value) { |
| 1517 | + checkBoundsBeginEnd(begin, end, value); |
| 1518 | + int pos = getChars(i, end, value); |
| 1519 | + assert begin == pos; |
| 1520 | + return pos; |
| 1521 | + } |
| 1522 | + |
| 1523 | + public static int getChars(long l, int begin, int end, byte[] value) { |
| 1524 | + checkBoundsBeginEnd(begin, end, value); |
| 1525 | + int pos = getChars(l, end, value); |
| 1526 | + assert begin == pos; |
| 1527 | + return pos; |
| 1528 | + } |
| 1529 | + |
1515 | 1530 | public static boolean contentEquals(byte[] v1, byte[] v2, int len) { |
1516 | 1531 | checkBoundsOffCount(0, len, v2); |
1517 | 1532 | for (int i = 0; i < len; i++) { |
@@ -1647,6 +1662,109 @@ public static int lastIndexOfLatin1(byte[] src, int srcCount, |
1647 | 1662 |
|
1648 | 1663 | static final int MAX_LENGTH = Integer.MAX_VALUE >> 1; |
1649 | 1664 |
|
| 1665 | + // Used by trusted callers. Assumes all necessary bounds checks have |
| 1666 | + // been done by the caller. |
| 1667 | + |
| 1668 | + /** |
| 1669 | + * This is a variant of {@link StringLatin1#getChars(int, int, byte[])}, but for |
| 1670 | + * UTF-16 coder. |
| 1671 | + * |
| 1672 | + * @param i value to convert |
| 1673 | + * @param index next index, after the least significant digit |
| 1674 | + * @param buf target buffer, UTF16-coded. |
| 1675 | + * @return index of the most significant digit or minus sign, if present |
| 1676 | + */ |
| 1677 | + static int getChars(int i, int index, byte[] buf) { |
| 1678 | + // Used by trusted callers. Assumes all necessary bounds checks have been done by the caller. |
| 1679 | + int q, r; |
| 1680 | + int charPos = index; |
| 1681 | + |
| 1682 | + boolean negative = (i < 0); |
| 1683 | + if (!negative) { |
| 1684 | + i = -i; |
| 1685 | + } |
| 1686 | + |
| 1687 | + // Get 2 digits/iteration using ints |
| 1688 | + while (i <= -100) { |
| 1689 | + q = i / 100; |
| 1690 | + r = (q * 100) - i; |
| 1691 | + i = q; |
| 1692 | + charPos -= 2; |
| 1693 | + putPair(buf, charPos, r); |
| 1694 | + } |
| 1695 | + |
| 1696 | + // We know there are at most two digits left at this point. |
| 1697 | + if (i < -9) { |
| 1698 | + charPos -= 2; |
| 1699 | + putPair(buf, charPos, -i); |
| 1700 | + } else { |
| 1701 | + putChar(buf, --charPos, '0' - i); |
| 1702 | + } |
| 1703 | + |
| 1704 | + if (negative) { |
| 1705 | + putChar(buf, --charPos, '-'); |
| 1706 | + } |
| 1707 | + return charPos; |
| 1708 | + } |
| 1709 | + |
| 1710 | + /** |
| 1711 | + * This is a variant of {@link StringLatin1#getChars(long, int, byte[])}, but for |
| 1712 | + * UTF-16 coder. |
| 1713 | + * |
| 1714 | + * @param i value to convert |
| 1715 | + * @param index next index, after the least significant digit |
| 1716 | + * @param buf target buffer, UTF16-coded. |
| 1717 | + * @return index of the most significant digit or minus sign, if present |
| 1718 | + */ |
| 1719 | + static int getChars(long i, int index, byte[] buf) { |
| 1720 | + // Used by trusted callers. Assumes all necessary bounds checks have been done by the caller. |
| 1721 | + long q; |
| 1722 | + int charPos = index; |
| 1723 | + |
| 1724 | + boolean negative = (i < 0); |
| 1725 | + if (!negative) { |
| 1726 | + i = -i; |
| 1727 | + } |
| 1728 | + |
| 1729 | + // Get 2 digits/iteration using longs until quotient fits into an int |
| 1730 | + while (i <= Integer.MIN_VALUE) { |
| 1731 | + q = i / 100; |
| 1732 | + charPos -= 2; |
| 1733 | + putPair(buf, charPos, (int)((q * 100) - i)); |
| 1734 | + i = q; |
| 1735 | + } |
| 1736 | + |
| 1737 | + // Get 2 digits/iteration using ints |
| 1738 | + int q2; |
| 1739 | + int i2 = (int)i; |
| 1740 | + while (i2 <= -100) { |
| 1741 | + q2 = i2 / 100; |
| 1742 | + charPos -= 2; |
| 1743 | + putPair(buf, charPos, (q2 * 100) - i2); |
| 1744 | + i2 = q2; |
| 1745 | + } |
| 1746 | + |
| 1747 | + // We know there are at most two digits left at this point. |
| 1748 | + if (i2 < -9) { |
| 1749 | + charPos -= 2; |
| 1750 | + putPair(buf, charPos, -i2); |
| 1751 | + } else { |
| 1752 | + putChar(buf, --charPos, '0' - i2); |
| 1753 | + } |
| 1754 | + |
| 1755 | + if (negative) { |
| 1756 | + putChar(buf, --charPos, '-'); |
| 1757 | + } |
| 1758 | + return charPos; |
| 1759 | + } |
| 1760 | + |
| 1761 | + private static void putPair(byte[] buf, int charPos, int v) { |
| 1762 | + int packed = (int) DecimalDigits.digitPair(v); |
| 1763 | + putChar(buf, charPos, packed & 0xFF); |
| 1764 | + putChar(buf, charPos + 1, packed >> 8); |
| 1765 | + } |
| 1766 | + // End of trusted methods. |
| 1767 | + |
1650 | 1768 | public static void checkIndex(int off, byte[] val) { |
1651 | 1769 | String.checkIndex(off, length(val)); |
1652 | 1770 | } |
|
0 commit comments