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.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
39 final class StringUTF16 {
40
41 public static byte[] newBytesFor(int len) {
42 if (len < 0) {
43 throw new NegativeArraySizeException();
44 }
45 if (len > MAX_LENGTH) {
46 throw new OutOfMemoryError("UTF16 String size is " + len +
47 ", should be less than " + MAX_LENGTH);
48 }
49 return new byte[len << 1];
50 }
51
52 @HotSpotIntrinsicCandidate
53 public static void putChar(byte[] val, int index, int c) {
54 index <<= 1;
55 val[index++] = (byte)(c >> HI_BYTE_SHIFT);
56 val[index] = (byte)(c >> LO_BYTE_SHIFT);
57 }
139 }
140 return null;
141 }
142
143 // compressedCopy char[] -> byte[]
144 @HotSpotIntrinsicCandidate
145 private static int compress(char[] src, int srcOff, byte[] dst, int dstOff, int len) {
146 for (int i = 0; i < len; i++) {
147 int c = src[srcOff++];
148 if (c >>> 8 != 0) {
149 return 0;
150 }
151 dst[dstOff++] = (byte)c;
152 }
153 return len;
154 }
155
156 // compressedCopy byte[] -> byte[]
157 @HotSpotIntrinsicCandidate
158 public static int compress(byte[] src, int srcOff, byte[] dst, int dstOff, int len) {
159 for (int i = 0; i < len; i++) {
160 int c = getChar(src, srcOff++);
161 if (c >>> 8 != 0) {
162 return 0;
163 }
164 dst[dstOff++] = (byte)c;
165 }
166 return len;
167 }
168
169 public static byte[] toBytes(int[] val, int index, int len) {
170 final int end = index + len;
171 // Pass 1: Compute precise size of char[]
172 int n = len;
173 for (int i = index; i < end; i++) {
174 int cp = val[i];
175 if (Character.isBmpCodePoint(cp))
176 continue;
177 else if (Character.isValidCodePoint(cp))
178 n++;
183 for (int i = index, j = 0; i < end; i++, j++) {
184 int cp = val[i];
185 if (Character.isBmpCodePoint(cp)) {
186 putChar(buf, j, cp);
187 } else {
188 putChar(buf, j++, Character.highSurrogate(cp));
189 putChar(buf, j, Character.lowSurrogate(cp));
190 }
191 }
192 return buf;
193 }
194
195 public static byte[] toBytes(char c) {
196 byte[] result = new byte[2];
197 putChar(result, 0, c);
198 return result;
199 }
200
201 @HotSpotIntrinsicCandidate
202 public static void getChars(byte[] value, int srcBegin, int srcEnd, char dst[], int dstBegin) {
203 for (int i = srcBegin; i < srcEnd; i++) {
204 dst[dstBegin++] = getChar(value, i);
205 }
206 }
207
208 /* @see java.lang.String.getBytes(int, int, byte[], int) */
209 public static void getBytes(byte[] value, int srcBegin, int srcEnd, byte dst[], int dstBegin) {
210 srcBegin <<= 1;
211 srcEnd <<= 1;
212 for (int i = srcBegin + (1 >> LO_BYTE_SHIFT); i < srcEnd; i += 2) {
213 dst[dstBegin++] = value[i];
214 }
215 }
216
217 @HotSpotIntrinsicCandidate
218 public static boolean equals(byte[] value, byte[] other) {
219 if (value.length == other.length) {
220 int len = value.length >> 1;
221 for (int i = 0; i < len; i++) {
222 if (getChar(value, i) != getChar(other, i)) {
892 if (Character.isLowSurrogate(c2)) {
893 i++;
894 cp = Character.toCodePoint(c1, c2);
895 }
896 }
897 action.accept(cp);
898 return i;
899 }
900
901 @Override
902 public long estimateSize() { return (long)(fence - index); }
903
904 @Override
905 public int characteristics() {
906 return cs;
907 }
908 }
909
910 ////////////////////////////////////////////////////////////////
911
912 public static void getCharsSB(byte[] val, int srcBegin, int srcEnd, char dst[], int dstBegin) {
913 checkOffset(srcEnd, val.length >> 1);
914 getChars(val, srcBegin, srcEnd, dst, dstBegin);
915 }
916
917 public static void putCharSB(byte[] val, int index, int c) {
918 checkIndex(index, val.length >> 1);
919 putChar(val, index, c);
920 }
921
922 public static void putCharsSB(byte[] val, int index, char[] ca, int off, int end) {
923 checkOffset(index + end - off, val.length >> 1);
924 putChars(val, index, ca, off, end);
925 }
926
927 public static void putCharsSB(byte[] val, int index, CharSequence s, int off, int end) {
928 checkOffset(index + end - off, val.length >> 1);
929 for (int i = off; i < end; i++) {
930 putChar(val, index++, s.charAt(i));
931 }
932 }
933
934 public static int codePointAtSB(byte[] val, int index, int end) {
935 checkOffset(end, val.length >> 1);
936 return codePointAt(val, index, end);
937 }
938
939 public static int codePointBeforeSB(byte[] val, int index) {
940 checkOffset(index, val.length >> 1);
941 return codePointBefore(val, index);
942 }
943
944 public static int codePointCountSB(byte[] val, int beginIndex, int endIndex) {
945 checkOffset(endIndex, val.length >> 1);
946 return codePointCount(val, beginIndex, endIndex);
947 }
948
949 public static String newStringSB(byte[] val, int index, int len) {
950 checkOffset(index + len, val.length >> 1);
951 return newString(val, index, len);
952 }
953
954 ////////////////////////////////////////////////////////////////
955
956 private static native boolean isBigEndian();
957
958 static final int HI_BYTE_SHIFT;
959 static final int LO_BYTE_SHIFT;
960 static {
961 if (isBigEndian()) {
962 HI_BYTE_SHIFT = 8;
963 LO_BYTE_SHIFT = 0;
964 } else {
965 HI_BYTE_SHIFT = 0;
966 LO_BYTE_SHIFT = 8;
967 }
968 }
969
970 static final int MAX_LENGTH = Integer.MAX_VALUE >> 1;
971 }
|
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.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 }
140 }
141 return null;
142 }
143
144 // compressedCopy char[] -> byte[]
145 @HotSpotIntrinsicCandidate
146 private static int compress(char[] src, int srcOff, byte[] dst, int dstOff, int len) {
147 for (int i = 0; i < len; i++) {
148 int c = src[srcOff++];
149 if (c >>> 8 != 0) {
150 return 0;
151 }
152 dst[dstOff++] = (byte)c;
153 }
154 return len;
155 }
156
157 // compressedCopy byte[] -> byte[]
158 @HotSpotIntrinsicCandidate
159 public static int compress(byte[] src, int srcOff, byte[] dst, int dstOff, int len) {
160 // We need a range check here because 'getChar' has no checks
161 checkBoundsOffCount(srcOff, len, src.length);
162 for (int i = 0; i < len; i++) {
163 int c = getChar(src, srcOff++);
164 if (c >>> 8 != 0) {
165 return 0;
166 }
167 dst[dstOff++] = (byte)c;
168 }
169 return len;
170 }
171
172 public static byte[] toBytes(int[] val, int index, int len) {
173 final int end = index + len;
174 // Pass 1: Compute precise size of char[]
175 int n = len;
176 for (int i = index; i < end; i++) {
177 int cp = val[i];
178 if (Character.isBmpCodePoint(cp))
179 continue;
180 else if (Character.isValidCodePoint(cp))
181 n++;
186 for (int i = index, j = 0; i < end; i++, j++) {
187 int cp = val[i];
188 if (Character.isBmpCodePoint(cp)) {
189 putChar(buf, j, cp);
190 } else {
191 putChar(buf, j++, Character.highSurrogate(cp));
192 putChar(buf, j, Character.lowSurrogate(cp));
193 }
194 }
195 return buf;
196 }
197
198 public static byte[] toBytes(char c) {
199 byte[] result = new byte[2];
200 putChar(result, 0, c);
201 return result;
202 }
203
204 @HotSpotIntrinsicCandidate
205 public static void getChars(byte[] value, int srcBegin, int srcEnd, char dst[], int dstBegin) {
206 // We need a range check here because 'getChar' has no checks
207 checkBoundsOffCount(srcBegin, srcEnd - srcBegin, value.length);
208 for (int i = srcBegin; i < srcEnd; i++) {
209 dst[dstBegin++] = getChar(value, i);
210 }
211 }
212
213 /* @see java.lang.String.getBytes(int, int, byte[], int) */
214 public static void getBytes(byte[] value, int srcBegin, int srcEnd, byte dst[], int dstBegin) {
215 srcBegin <<= 1;
216 srcEnd <<= 1;
217 for (int i = srcBegin + (1 >> LO_BYTE_SHIFT); i < srcEnd; i += 2) {
218 dst[dstBegin++] = value[i];
219 }
220 }
221
222 @HotSpotIntrinsicCandidate
223 public static boolean equals(byte[] value, byte[] other) {
224 if (value.length == other.length) {
225 int len = value.length >> 1;
226 for (int i = 0; i < len; i++) {
227 if (getChar(value, i) != getChar(other, i)) {
897 if (Character.isLowSurrogate(c2)) {
898 i++;
899 cp = Character.toCodePoint(c1, c2);
900 }
901 }
902 action.accept(cp);
903 return i;
904 }
905
906 @Override
907 public long estimateSize() { return (long)(fence - index); }
908
909 @Override
910 public int characteristics() {
911 return cs;
912 }
913 }
914
915 ////////////////////////////////////////////////////////////////
916
917 public static void putCharSB(byte[] val, int index, int c) {
918 checkIndex(index, val.length >> 1);
919 putChar(val, index, c);
920 }
921
922 public static void putCharsSB(byte[] val, int index, char[] ca, int off, int end) {
923 checkOffset(index + end - off, val.length >> 1);
924 putChars(val, index, ca, off, end);
925 }
926
927 public static void putCharsSB(byte[] val, int index, CharSequence s, int off, int end) {
928 checkOffset(index + end - off, val.length >> 1);
929 for (int i = off; i < end; i++) {
930 putChar(val, index++, s.charAt(i));
931 }
932 }
933
934 public static int codePointAtSB(byte[] val, int index, int end) {
935 checkOffset(end, val.length >> 1);
936 return codePointAt(val, index, end);
937 }
938
939 public static int codePointBeforeSB(byte[] val, int index) {
940 checkOffset(index, val.length >> 1);
941 return codePointBefore(val, index);
942 }
943
944 public static int codePointCountSB(byte[] val, int beginIndex, int endIndex) {
945 checkOffset(endIndex, val.length >> 1);
946 return codePointCount(val, beginIndex, endIndex);
947 }
948
949 ////////////////////////////////////////////////////////////////
950
951 private static native boolean isBigEndian();
952
953 static final int HI_BYTE_SHIFT;
954 static final int LO_BYTE_SHIFT;
955 static {
956 if (isBigEndian()) {
957 HI_BYTE_SHIFT = 8;
958 LO_BYTE_SHIFT = 0;
959 } else {
960 HI_BYTE_SHIFT = 0;
961 LO_BYTE_SHIFT = 8;
962 }
963 }
964
965 static final int MAX_LENGTH = Integer.MAX_VALUE >> 1;
966 }
|