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.IntConsumer;
32 import jdk.internal.HotSpotIntrinsicCandidate;
33
34 import static java.lang.String.UTF16;
35 import static java.lang.String.LATIN1;
36 import static java.lang.String.checkIndex;
37 import static java.lang.String.checkOffset;
38 import static java.lang.String.checkBoundsOffCount;
39
40 final class StringUTF16 {
41
42 public static byte[] newBytesFor(int len) {
43 if (len < 0) {
44 throw new NegativeArraySizeException();
45 }
46 if (len > MAX_LENGTH) {
47 throw new OutOfMemoryError("UTF16 String size is " + len +
48 ", should be less than " + MAX_LENGTH);
49 }
50 return new byte[len << 1];
51 }
52
53 @HotSpotIntrinsicCandidate
54 public static void putChar(byte[] val, int index, int c) {
55 index <<= 1;
56 val[index++] = (byte)(c >> HI_BYTE_SHIFT);
57 val[index] = (byte)(c >> LO_BYTE_SHIFT);
58 }
396 }
397 return -1;
398 }
399
400 /**
401 * Handles (rare) calls of indexOf with a supplementary character.
402 */
403 private static int indexOfSupplementary(byte[] value, int ch, int fromIndex, int max) {
404 if (Character.isValidCodePoint(ch)) {
405 final char hi = Character.highSurrogate(ch);
406 final char lo = Character.lowSurrogate(ch);
407 for (int i = fromIndex; i < max - 1; i++) {
408 if (getChar(value, i) == hi && getChar(value, i + 1 ) == lo) {
409 return i;
410 }
411 }
412 }
413 return -1;
414 }
415
416 public static int lastIndexOf(byte[] src, int srcCount,
417 byte[] tgt, int tgtCount, int fromIndex) {
418 int min = tgtCount - 1;
419 int i = min + fromIndex;
420 int strLastIndex = tgtCount - 1;
421 char strLastChar = getChar(tgt, strLastIndex);
422
423 startSearchForLastChar:
424 while (true) {
425 while (i >= min && getChar(src, i) != strLastChar) {
426 i--;
427 }
428 if (i < min) {
429 return -1;
430 }
431 int j = i - 1;
432 int start = j - strLastIndex;
433 int k = strLastIndex - 1;
434 while (j > start) {
435 if (getChar(src, j--) != getChar(tgt, k--)) {
915 return i;
916 }
917
918 @Override
919 public long estimateSize() { return (long)(fence - index); }
920
921 @Override
922 public int characteristics() {
923 return cs;
924 }
925 }
926
927 ////////////////////////////////////////////////////////////////
928
929 public static void putCharSB(byte[] val, int index, int c) {
930 checkIndex(index, val.length >> 1);
931 putChar(val, index, c);
932 }
933
934 public static void putCharsSB(byte[] val, int index, char[] ca, int off, int end) {
935 checkOffset(index + end - off, val.length >> 1);
936 putChars(val, index, ca, off, end);
937 }
938
939 public static void putCharsSB(byte[] val, int index, CharSequence s, int off, int end) {
940 checkOffset(index + end - off, val.length >> 1);
941 for (int i = off; i < end; i++) {
942 putChar(val, index++, s.charAt(i));
943 }
944 }
945
946 public static int codePointAtSB(byte[] val, int index, int end) {
947 checkOffset(end, val.length >> 1);
948 return codePointAt(val, index, end);
949 }
950
951 public static int codePointBeforeSB(byte[] val, int index) {
952 checkOffset(index, val.length >> 1);
953 return codePointBefore(val, index);
954 }
955
956 public static int codePointCountSB(byte[] val, int beginIndex, int endIndex) {
957 checkOffset(endIndex, val.length >> 1);
958 return codePointCount(val, beginIndex, endIndex);
959 }
960
961 ////////////////////////////////////////////////////////////////
962
963 private static native boolean isBigEndian();
964
965 static final int HI_BYTE_SHIFT;
966 static final int LO_BYTE_SHIFT;
967 static {
968 if (isBigEndian()) {
969 HI_BYTE_SHIFT = 8;
970 LO_BYTE_SHIFT = 0;
971 } else {
972 HI_BYTE_SHIFT = 0;
973 LO_BYTE_SHIFT = 8;
974 }
975 }
976
977 static final int MAX_LENGTH = Integer.MAX_VALUE >> 1;
|
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.IntConsumer;
32 import jdk.internal.HotSpotIntrinsicCandidate;
33
34 import static java.lang.String.UTF16;
35 import static java.lang.String.LATIN1;
36 import static java.lang.String.checkIndex;
37 import static java.lang.String.checkOffset;
38 import static java.lang.String.checkBoundsOffCount;
39 import static java.lang.String.checkBoundsBeginEnd;
40
41 final class StringUTF16 {
42
43 public static byte[] newBytesFor(int len) {
44 if (len < 0) {
45 throw new NegativeArraySizeException();
46 }
47 if (len > MAX_LENGTH) {
48 throw new OutOfMemoryError("UTF16 String size is " + len +
49 ", should be less than " + MAX_LENGTH);
50 }
51 return new byte[len << 1];
52 }
53
54 @HotSpotIntrinsicCandidate
55 public static void putChar(byte[] val, int index, int c) {
56 index <<= 1;
57 val[index++] = (byte)(c >> HI_BYTE_SHIFT);
58 val[index] = (byte)(c >> LO_BYTE_SHIFT);
59 }
397 }
398 return -1;
399 }
400
401 /**
402 * Handles (rare) calls of indexOf with a supplementary character.
403 */
404 private static int indexOfSupplementary(byte[] value, int ch, int fromIndex, int max) {
405 if (Character.isValidCodePoint(ch)) {
406 final char hi = Character.highSurrogate(ch);
407 final char lo = Character.lowSurrogate(ch);
408 for (int i = fromIndex; i < max - 1; i++) {
409 if (getChar(value, i) == hi && getChar(value, i + 1 ) == lo) {
410 return i;
411 }
412 }
413 }
414 return -1;
415 }
416
417 // srcCoder == UTF16 && tgtCoder == UTF16
418 public static int lastIndexOf(byte[] src, int srcCount,
419 byte[] tgt, int tgtCount, int fromIndex) {
420 int min = tgtCount - 1;
421 int i = min + fromIndex;
422 int strLastIndex = tgtCount - 1;
423 char strLastChar = getChar(tgt, strLastIndex);
424
425 startSearchForLastChar:
426 while (true) {
427 while (i >= min && getChar(src, i) != strLastChar) {
428 i--;
429 }
430 if (i < min) {
431 return -1;
432 }
433 int j = i - 1;
434 int start = j - strLastIndex;
435 int k = strLastIndex - 1;
436 while (j > start) {
437 if (getChar(src, j--) != getChar(tgt, k--)) {
917 return i;
918 }
919
920 @Override
921 public long estimateSize() { return (long)(fence - index); }
922
923 @Override
924 public int characteristics() {
925 return cs;
926 }
927 }
928
929 ////////////////////////////////////////////////////////////////
930
931 public static void putCharSB(byte[] val, int index, int c) {
932 checkIndex(index, val.length >> 1);
933 putChar(val, index, c);
934 }
935
936 public static void putCharsSB(byte[] val, int index, char[] ca, int off, int end) {
937 checkBoundsOffCount(index, index + end - off, val.length >> 1);
938 putChars(val, index, ca, off, end);
939 }
940
941 public static void putCharsSB(byte[] val, int index, CharSequence s, int off, int end) {
942 checkBoundsOffCount(index, end - off, val.length >> 1);
943 for (int i = off; i < end; i++) {
944 putChar(val, index++, s.charAt(i));
945 }
946 }
947
948 public static int codePointAtSB(byte[] val, int index, int end) {
949 checkOffset(end, val.length >> 1);
950 return codePointAt(val, index, end);
951 }
952
953 public static int codePointBeforeSB(byte[] val, int index) {
954 checkOffset(index, val.length >> 1);
955 return codePointBefore(val, index);
956 }
957
958 public static int codePointCountSB(byte[] val, int beginIndex, int endIndex) {
959 checkBoundsBeginEnd(beginIndex, endIndex, val.length >> 1);
960 return codePointCount(val, beginIndex, endIndex);
961 }
962
963 ////////////////////////////////////////////////////////////////
964
965 private static native boolean isBigEndian();
966
967 static final int HI_BYTE_SHIFT;
968 static final int LO_BYTE_SHIFT;
969 static {
970 if (isBigEndian()) {
971 HI_BYTE_SHIFT = 8;
972 LO_BYTE_SHIFT = 0;
973 } else {
974 HI_BYTE_SHIFT = 0;
975 LO_BYTE_SHIFT = 8;
976 }
977 }
978
979 static final int MAX_LENGTH = Integer.MAX_VALUE >> 1;
|