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.Objects;
31 import java.util.Spliterator;
32 import java.util.function.Consumer;
33 import java.util.function.IntConsumer;
34 import java.util.stream.IntStream;
35 import java.util.stream.Stream;
36 import java.util.stream.StreamSupport;
37 import jdk.internal.HotSpotIntrinsicCandidate;
38
39 import static java.lang.String.LATIN1;
40 import static java.lang.String.UTF16;
41 import static java.lang.String.checkOffset;
42
43 final class StringLatin1 {
44
45 /**
46 * The maximum size of array to allocate (unless necessary).
47 * Some VMs reserve some header words in an array.
48 * Attempts to allocate larger arrays may result in
49 * OutOfMemoryError: Requested array size exceeds VM limit
50 */
51 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
52
53 public static char charAt(byte[] value, int index) {
54 if (index < 0 || index >= value.length) {
55 throw new StringIndexOutOfBoundsException(index);
56 }
57 return (char)(value[index] & 0xff);
58 }
59
60 public static boolean canEncode(int cp) {
61 return cp >>> 8 == 0;
62 }
63
64 public static int length(byte[] value) {
65 return value.length;
66 }
67
68 public static int codePointAt(byte[] value, int index, int end) {
69 return value[index] & 0xff;
70 }
71
72 public static int codePointBefore(byte[] value, int index) {
336 }
337 }
338 return null; // for string to return this;
339 }
340
341 public static String replace(byte[] value, int valLen, byte[] targ,
342 int targLen, byte[] repl, int replLen)
343 {
344 assert targLen > 0;
345 int i, j, p = 0;
346 if (valLen == 0 || (i = indexOf(value, valLen, targ, targLen, 0)) < 0) {
347 return null; // for string to return this;
348 }
349
350 // find and store indices of substrings to replace
351 int[] pos = new int[16];
352 pos[0] = i;
353 i += targLen;
354 while ((j = indexOf(value, valLen, targ, targLen, i)) > 0) {
355 if (++p == pos.length) {
356 int cap = p + (p >> 1);
357 // overflow-conscious code
358 if (cap - MAX_ARRAY_SIZE > 0) {
359 if (p == MAX_ARRAY_SIZE) {
360 throw new OutOfMemoryError();
361 }
362 cap = MAX_ARRAY_SIZE;
363 }
364 pos = Arrays.copyOf(pos, cap);
365 }
366 pos[p] = j;
367 i = j + targLen;
368 }
369
370 int resultLen;
371 try {
372 resultLen = Math.addExact(valLen,
373 Math.multiplyExact(++p, replLen - targLen));
374 } catch (ArithmeticException ignored) {
375 throw new OutOfMemoryError();
376 }
377 if (resultLen == 0) {
378 return "";
379 }
380
381 byte[] result = StringConcatHelper.newArray(resultLen);
382 int posFrom = 0, posTo = 0;
383 for (int q = 0; q < p; ++q) {
384 int nextPos = pos[q];
|
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.Objects;
31 import java.util.Spliterator;
32 import java.util.function.Consumer;
33 import java.util.function.IntConsumer;
34 import java.util.stream.IntStream;
35 import java.util.stream.Stream;
36 import java.util.stream.StreamSupport;
37 import jdk.internal.HotSpotIntrinsicCandidate;
38 import jdk.internal.util.ArraysSupport;
39
40 import static java.lang.String.LATIN1;
41 import static java.lang.String.UTF16;
42 import static java.lang.String.checkOffset;
43
44 final class StringLatin1 {
45
46 public static char charAt(byte[] value, int index) {
47 if (index < 0 || index >= value.length) {
48 throw new StringIndexOutOfBoundsException(index);
49 }
50 return (char)(value[index] & 0xff);
51 }
52
53 public static boolean canEncode(int cp) {
54 return cp >>> 8 == 0;
55 }
56
57 public static int length(byte[] value) {
58 return value.length;
59 }
60
61 public static int codePointAt(byte[] value, int index, int end) {
62 return value[index] & 0xff;
63 }
64
65 public static int codePointBefore(byte[] value, int index) {
329 }
330 }
331 return null; // for string to return this;
332 }
333
334 public static String replace(byte[] value, int valLen, byte[] targ,
335 int targLen, byte[] repl, int replLen)
336 {
337 assert targLen > 0;
338 int i, j, p = 0;
339 if (valLen == 0 || (i = indexOf(value, valLen, targ, targLen, 0)) < 0) {
340 return null; // for string to return this;
341 }
342
343 // find and store indices of substrings to replace
344 int[] pos = new int[16];
345 pos[0] = i;
346 i += targLen;
347 while ((j = indexOf(value, valLen, targ, targLen, i)) > 0) {
348 if (++p == pos.length) {
349 pos = Arrays.copyOf(pos, ArraysSupport.newCapacity(pos.length,
350 1, p >> 1));
351 }
352 pos[p] = j;
353 i = j + targLen;
354 }
355
356 int resultLen;
357 try {
358 resultLen = Math.addExact(valLen,
359 Math.multiplyExact(++p, replLen - targLen));
360 } catch (ArithmeticException ignored) {
361 throw new OutOfMemoryError();
362 }
363 if (resultLen == 0) {
364 return "";
365 }
366
367 byte[] result = StringConcatHelper.newArray(resultLen);
368 int posFrom = 0, posTo = 0;
369 for (int q = 0; q < p; ++q) {
370 int nextPos = pos[q];
|