-
Notifications
You must be signed in to change notification settings - Fork 729
/
String.java
5310 lines (4583 loc) · 148 KB
/
String.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*[INCLUDE-IF Sidecar16]*/
package java.lang;
/*******************************************************************************
* Copyright (c) 1998, 2017 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
import java.io.Serializable;
import java.util.Locale;
import java.util.Comparator;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.Formatter;
import java.util.StringJoiner;
import java.util.Iterator;
/*[IF Sidecar19-SE]*/
import jdk.internal.misc.Unsafe;
import java.util.stream.IntStream;
/*[ELSE]*/
import sun.misc.Unsafe;
/*[ENDIF]*/
import java.nio.charset.Charset;
/**
* Strings are objects which represent immutable arrays of characters.
*
* @author OTI
* @version initial
*
* @see StringBuffer
*/
public final class String implements Serializable, Comparable<String>, CharSequence {
/*[IF]*/
/* The STRING_OPT_IN_HW is false by default and only set to true by the JIT for platforms
* that support vectorized string operations (currently, zSphinx+ hardware).
*/
/*[ENDIF]*/
// DO NOT CHANGE OR MOVE THIS LINE
// IT MUST BE THE FIRST THING IN THE INITIALIZATION
private static final boolean STRING_OPT_IN_HW = StrCheckHWAvailable();
private static final long serialVersionUID = -6849794470754667710L;
/**
* Determines whether String compression is enabled.
*/
static final boolean enableCompression = com.ibm.oti.vm.VM.J9_STRING_COMPRESSION_ENABLED;
/*[IF Sidecar19-SE]*/
/*[IF Java10]*/
private final byte coder;
/*[ENDIF]*/
static final byte LATIN1 = 0;
static final byte UTF16 = 1;
static final boolean COMPACT_STRINGS;
static {
COMPACT_STRINGS = enableCompression;
}
/*[IF Sidecar19-SE]*/
// returns UTF16 when COMPACT_STRINGS is false
byte coder() {
if (enableCompression && (null == compressionFlag || count >= 0)) {
return LATIN1;
} else {
return UTF16;
}
}
// no range checking, caller ensures bytes is in UTF16
// coder is one of LATIN1 or UTF16
void getBytes(byte[] bytes, int offset, byte coder) {
int currentLength = lengthInternal();
// Check if the String is compressed
if (enableCompression && (null == compressionFlag || count >= 0)) {
if (String.LATIN1 == coder) {
compressedArrayCopy(value, 0, bytes, offset, currentLength);
} else {
decompress(value, 0, bytes, offset, currentLength);
}
} else {
decompressedArrayCopy(value, 0, bytes, offset, currentLength);
}
}
/*[ENDIF]*/
static void checkIndex(int index, int length) {
if ((0 <= index) && (index < length)) {
return;
}
throw new StringIndexOutOfBoundsException("index="+index + " length="+length); //$NON-NLS-1$ //$NON-NLS-2$
}
static void checkOffset(int offset, int length) {
if ((0 <= offset) && (offset <= length)) {
return;
}
throw new StringIndexOutOfBoundsException("offset="+offset + " length="+length); //$NON-NLS-1$ //$NON-NLS-2$
}
/*[ENDIF]*/
/**
* CaseInsensitiveComparator compares Strings ignoring the case of the characters.
*/
private static final class CaseInsensitiveComparator implements Comparator<String>, Serializable {
static final long serialVersionUID = 8575799808933029326L;
/**
* Compare the two objects to determine the relative ordering.
*
* @param o1
* an Object to compare
* @param o2
* an Object to compare
* @return an int < 0 if object1 is less than object2, 0 if they are equal, and > 0 if object1 is greater
*
* @exception ClassCastException
* when objects are not the correct type
*/
public int compare(String o1, String o2) {
return o1.compareToIgnoreCase(o2);
}
};
/**
* A Comparator which compares Strings ignoring the case of the characters.
*/
public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();
/*[IF Sidecar19-SE]*/
// Used to represent the value of an empty String
private static final byte[] emptyValue = new byte[0];
// Used to extract the value of a single ASCII character String by the integral value of the respective character as
// an index into this table
private static final byte[][] compressedAsciiTable;
private static final byte[][] decompressedAsciiTable;
/*[ELSE]*/
// Used to represent the value of an empty String
private static final char[] emptyValue = new char[0];
// Used to extract the value of a single ASCII character String by the integral value of the respective character as
// an index into this table
private static final char[][] compressedAsciiTable;
private static final char[][] decompressedAsciiTable;
/*[ENDIF]*/
// Used to access compression related helper methods
private static final com.ibm.jit.JITHelpers helpers = com.ibm.jit.JITHelpers.getHelpers();
static class StringCompressionFlag implements Serializable {
private static final long serialVersionUID = 1346155847239551492L;
}
// Singleton used by all String instances to indicate a non-compressed string has been
// allocated. JIT attempts to fold away the null check involving this static if the
// StringCompressionFlag class has not been initialized and patches the code to bring back
// the null check if a non-compressed String is constructed.
private static StringCompressionFlag compressionFlag;
// Represents the bit in count field to test for whether this String backing array is not compressed
// under String compression mode. This bit is not used when String compression is disabled.
private static final int uncompressedBit = 0x80000000;
private static String[] stringArray;
private static final int stringArraySize = 10;
private static class UnsafeHelpers {
public final static long valueFieldOffset = getValueFieldOffset();
static long getValueFieldOffset() {
try {
return Unsafe.getUnsafe().objectFieldOffset(String.class.getDeclaredField("value")); //$NON-NLS-1$
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
}
/**
* This is a System property to enable sharing of the underlying value array in {@link #String.substring(int)} and
* {@link #String.substring(int, int)} if the offset is zero.
*/
static boolean enableSharingInSubstringWhenOffsetIsZero;
/*[IF Sidecar19-SE]*/
private final byte[] value;
/*[ELSE]*/
private final char[] value;
/*[ENDIF]*/
private final int count;
private int hashCode;
static {
stringArray = new String[stringArraySize];
/*[IF Sidecar19-SE]*/
compressedAsciiTable = new byte[256][];
/*[ELSE]*/
compressedAsciiTable = new char[256][];
/*[ENDIF]*/
for (int i = 0; i < compressedAsciiTable.length; ++i) {
/*[IF Sidecar19-SE]*/
byte[] asciiValue = new byte[1];
/*[ELSE]*/
char[] asciiValue = new char[1];
/*[ENDIF]*/
helpers.putByteInArrayByIndex(asciiValue, 0, (byte) i);
compressedAsciiTable[i] = asciiValue;
}
/*[IF Sidecar19-SE]*/
decompressedAsciiTable = new byte[256][];
/*[ELSE]*/
decompressedAsciiTable = new char[256][];
/*[ENDIF]*/
for (int i = 0; i < decompressedAsciiTable.length; ++i) {
/*[IF Sidecar19-SE]*/
byte[] asciiValue = new byte[2];
/*[ELSE]*/
char[] asciiValue = new char[1];
/*[ENDIF]*/
helpers.putCharInArrayByIndex(asciiValue, 0, (char) i);
decompressedAsciiTable[i] = asciiValue;
}
}
static void initCompressionFlag() {
if (compressionFlag == null) {
compressionFlag = new StringCompressionFlag();
}
}
static boolean compressible(char[] c, int start, int length) {
for (int i = start; i < length; ++i) {
if (c[i] > 255) {
return false;
}
}
return true;
}
static void compress(byte[] array1, int start1, byte[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putByteInArrayByIndex(array2, start2 + i, (byte) helpers.getCharFromArrayByIndex(array1, start1 + i));
}
}
static void compress(char[] array1, int start1, byte[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putByteInArrayByIndex(array2, start2 + i, (byte) helpers.getCharFromArrayByIndex(array1, start1 + i));
}
}
static void compress(byte[] array1, int start1, char[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putByteInArrayByIndex(array2, start2 + i, (byte) helpers.getCharFromArrayByIndex(array1, start1 + i));
}
}
static void compress(char[] array1, int start1, char[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putByteInArrayByIndex(array2, start2 + i, (byte) helpers.getCharFromArrayByIndex(array1, start1 + i));
}
}
static void decompress(byte[] array1, int start1, byte[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putCharInArrayByIndex(array2, start2 + i, helpers.byteToCharUnsigned(helpers.getByteFromArrayByIndex(array1, start1 + i)));
}
}
static void decompress(char[] array1, int start1, byte[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putCharInArrayByIndex(array2, start2 + i, helpers.byteToCharUnsigned(helpers.getByteFromArrayByIndex(array1, start1 + i)));
}
}
static void decompress(byte[] array1, int start1, char[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putCharInArrayByIndex(array2, start2 + i, helpers.byteToCharUnsigned(helpers.getByteFromArrayByIndex(array1, start1 + i)));
}
}
static void decompress(char[] array1, int start1, char[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putCharInArrayByIndex(array2, start2 + i, helpers.byteToCharUnsigned(helpers.getByteFromArrayByIndex(array1, start1 + i)));
}
}
static void compressedArrayCopy(byte[] array1, int start1, byte[] array2, int start2, int length) {
if (array1 == array2 && start1 < start2) {
for (int i = length - 1; i >= 0; --i) {
helpers.putByteInArrayByIndex(array2, start2 + i, helpers.getByteFromArrayByIndex(array1, start1 + i));
}
} else {
for (int i = 0; i < length; ++i) {
helpers.putByteInArrayByIndex(array2, start2 + i, helpers.getByteFromArrayByIndex(array1, start1 + i));
}
}
}
static void compressedArrayCopy(byte[] array1, int start1, char[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putByteInArrayByIndex(array2, start2 + i, helpers.getByteFromArrayByIndex(array1, start1 + i));
}
}
static void compressedArrayCopy(char[] array1, int start1, byte[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putByteInArrayByIndex(array2, start2 + i, helpers.getByteFromArrayByIndex(array1, start1 + i));
}
}
static void compressedArrayCopy(char[] array1, int start1, char[] array2, int start2, int length) {
if (array1 == array2 && start1 < start2) {
for (int i = length - 1; i >= 0; --i) {
helpers.putByteInArrayByIndex(array2, start2 + i, helpers.getByteFromArrayByIndex(array1, start1 + i));
}
} else {
for (int i = 0; i < length; ++i) {
helpers.putByteInArrayByIndex(array2, start2 + i, helpers.getByteFromArrayByIndex(array1, start1 + i));
}
}
}
/*[IF Sidecar19-SE]*/
static void decompressedArrayCopy(byte[] array1, int start1, byte[] array2, int start2, int length) {
if (array1 == array2 && start1 < start2) {
for (int i = length - 1; i >= 0; --i) {
helpers.putCharInArrayByIndex(array2, start2 + i, helpers.getCharFromArrayByIndex(array1, start1 + i));
}
} else {
for (int i = 0; i < length; ++i) {
helpers.putCharInArrayByIndex(array2, start2 + i, helpers.getCharFromArrayByIndex(array1, start1 + i));
}
}
}
static void decompressedArrayCopy(byte[] array1, int start1, char[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putCharInArrayByIndex(array2, start2 + i, helpers.getCharFromArrayByIndex(array1, start1 + i));
}
}
static void decompressedArrayCopy(char[] array1, int start1, byte[] array2, int start2, int length) {
for (int i = 0; i < length; ++i) {
helpers.putCharInArrayByIndex(array2, start2 + i, helpers.getCharFromArrayByIndex(array1, start1 + i));
}
}
/*[ENDIF]*/
static void decompressedArrayCopy(char[] array1, int start1, char[] array2, int start2, int length) {
if (array1 == array2 && start1 < start2) {
for (int i = length - 1; i >= 0; --i) {
helpers.putCharInArrayByIndex(array2, start2 + i, helpers.getCharFromArrayByIndex(array1, start1 + i));
}
} else {
for (int i = 0; i < length; ++i) {
helpers.putCharInArrayByIndex(array2, start2 + i, helpers.getCharFromArrayByIndex(array1, start1 + i));
}
}
}
boolean isCompressed() {
// Check if the String is compressed
if (enableCompression && (null == compressionFlag || count >= 0)) {
return true;
} else {
return false;
}
}
/*[IF Sidecar19-SE]*/
String(byte[] byteArray, byte coder) {
if (enableCompression) {
if (String.LATIN1 == coder) {
value = byteArray;
count = byteArray.length;
} else {
value = byteArray;
count = byteArray.length / 2 | uncompressedBit;
initCompressionFlag();
}
} else {
value = byteArray;
count = byteArray.length / 2;
}
/*[IF Java10]*/
this.coder = coder();
/*[ENDIF]*/
}
static void checkBoundsOffCount(int offset, int count, int length) {
if (offset >= 0 && count >= 0 && offset <= length - count) {
return;
}
throw newStringIndexOutOfBoundsException(offset, count, length);
}
static private StringIndexOutOfBoundsException newStringIndexOutOfBoundsException(int offset, int count, int length) {
return new StringIndexOutOfBoundsException("offset = " + offset + " count = " + count + " length = " + length); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
/*[ENDIF]*/
/**
* Answers an empty string.
*/
public String() {
value = emptyValue;
count = 0;
/*[IF Java10]*/
coder = coder();
/*[ENDIF]*/
}
/**
* Converts the byte array to a String using the default encoding as specified by the file.encoding system property. If the system property is not
* defined, the default encoding is ISO8859_1 (ISO-Latin-1). If 8859-1 is not available, an ASCII encoding is used.
*
* @param data
* the byte array to convert to a String
*
* @throws NullPointerException
* when data is null
*
* @see #getBytes()
* @see #getBytes(int, int, byte[], int)
* @see #getBytes(String)
* @see #valueOf(boolean)
* @see #valueOf(char)
* @see #valueOf(char[])
* @see #valueOf(char[], int, int)
* @see #valueOf(double)
* @see #valueOf(float)
* @see #valueOf(int)
* @see #valueOf(long)
* @see #valueOf(Object)
*
*/
public String(byte[] data) {
this(data, 0, data.length);
}
/**
* Converts the byte array to a String, setting the high byte of every character to the specified value.
*
* @param data
* the byte array to convert to a String
* @param high
* the high byte to use
*
* @throws NullPointerException
* when data is null
*
* @deprecated Use String(byte[]) or String(byte[], String) instead
*/
/*[IF Sidecar19-SE]*/
@Deprecated(forRemoval=false, since="1.1")
/*[ELSE]*/
@Deprecated
/*[ENDIF]*/
public String(byte[] data, int high) {
this(data, high, 0, data.length);
}
/**
* Converts the byte array to a String using the default encoding as specified by the file.encoding system property. If the system property is not
* defined, the default encoding is ISO8859_1 (ISO-Latin-1). If 8859-1 is not available, an ASCII encoding is used.
*
* @param data
* the byte array to convert to a String
* @param start
* the starting offset in the byte array
* @param length
* the number of bytes to convert
*
* @throws IndexOutOfBoundsException
* when <code>length < 0, start < 0</code> or <code>start + length > data.length</code>
* @throws NullPointerException
* when data is null
*
* @see #getBytes()
* @see #getBytes(int, int, byte[], int)
* @see #getBytes(String)
* @see #valueOf(boolean)
* @see #valueOf(char)
* @see #valueOf(char[])
* @see #valueOf(char[], int, int)
* @see #valueOf(double)
* @see #valueOf(float)
* @see #valueOf(int)
* @see #valueOf(long)
* @see #valueOf(Object)
*
*/
public String(byte[] data, int start, int length) {
data.getClass(); // Implicit null check
if (start >= 0 && 0 <= length && length <= data.length - start) {
/*[IF Sidecar19-SE]*/
StringCoding.Result scResult = StringCoding.decode(data, start, length);
if (enableCompression) {
if (String.LATIN1 == scResult.coder) {
value = scResult.value;
count = scResult.value.length;
} else {
value = scResult.value;
count = scResult.value.length / 2 | uncompressedBit;
initCompressionFlag();
}
} else {
value = scResult.value;
count = scResult.value.length / 2;
}
/*[ELSE]*/
char[] buffer = StringCoding.decode(data, start, length);
if (enableCompression) {
if (compressible(buffer, 0, buffer.length)) {
value = new char[(buffer.length + 1) / 2];
count = buffer.length;
compress(buffer, 0, value, 0, buffer.length);
} else {
value = buffer;
count = buffer.length | uncompressedBit;
initCompressionFlag();
}
} else {
value = buffer;
count = buffer.length;
}
/*[ENDIF]*/
} else {
throw new StringIndexOutOfBoundsException();
}
/*[IF Java10]*/
coder = coder();
/*[ENDIF]*/
}
/**
* Converts the byte array to a String, setting the high byte of every character to the specified value.
*
* @param data
* the byte array to convert to a String
* @param high
* the high byte to use
* @param start
* the starting offset in the byte array
* @param length
* the number of bytes to convert
*
* @throws IndexOutOfBoundsException
* when <code>length < 0, start < 0</code> or <code>start + length > data.length</code>
* @throws NullPointerException
* when data is null
*
* @deprecated Use String(byte[], int, int) instead
*/
/*[IF Sidecar19-SE]*/
@Deprecated(forRemoval=false, since="1.1")
/*[ELSE]*/
@Deprecated
/*[ENDIF]*/
public String(byte[] data, int high, int start, int length) {
data.getClass(); // Implicit null check
if (start >= 0 && 0 <= length && length <= data.length - start) {
if (enableCompression) {
if (high == 0) {
/*[IF Sidecar19-SE]*/
value = new byte[length];
/*[ELSE]*/
value = new char[(length + 1) / 2];
/*[ENDIF]*/
count = length;
compressedArrayCopy(data, start, value, 0, length);
} else {
/*[IF Sidecar19-SE]*/
value = new byte[length * 2];
/*[ELSE]*/
value = new char[length];
/*[ENDIF]*/
count = length | uncompressedBit;
high <<= 8;
for (int i = 0; i < length; ++i) {
/*[IF Sidecar19-SE]*/
helpers.putCharInArrayByIndex(value, i, (char) (high + (data[start++] & 0xff)));
/*[ELSE]*/
value[i] = (char) (high + (data[start++] & 0xff));
/*[ENDIF]*/
}
initCompressionFlag();
}
} else {
/*[IF Sidecar19-SE]*/
value = new byte[length * 2];
/*[ELSE]*/
value = new char[length];
/*[ENDIF]*/
count = length;
high <<= 8;
for (int i = 0; i < length; ++i) {
/*[IF Sidecar19-SE]*/
helpers.putCharInArrayByIndex(value, i, (char) (high + (data[start++] & 0xff)));
/*[ELSE]*/
value[i] = (char) (high + (data[start++] & 0xff));
/*[ENDIF]*/
}
}
} else {
throw new StringIndexOutOfBoundsException();
}
/*[IF Java10]*/
coder = coder();
/*[ENDIF]*/
}
/**
* Converts the byte array to a String using the specified encoding.
*
* @param data
* the byte array to convert to a String
* @param start
* the starting offset in the byte array
* @param length
* the number of bytes to convert
* @param encoding
* the encoding
*
* @throws IndexOutOfBoundsException
* when <code>length < 0, start < 0</code> or <code>start + length > data.length</code>
* @throws UnsupportedEncodingException
* when encoding is not supported
* @throws NullPointerException
* when data is null
*
* @see #getBytes()
* @see #getBytes(int, int, byte[], int)
* @see #getBytes(String)
* @see #valueOf(boolean)
* @see #valueOf(char)
* @see #valueOf(char[])
* @see #valueOf(char[], int, int)
* @see #valueOf(double)
* @see #valueOf(float)
* @see #valueOf(int)
* @see #valueOf(long)
* @see #valueOf(Object)
* @see UnsupportedEncodingException
*/
public String(byte[] data, int start, int length, final String encoding) throws UnsupportedEncodingException {
encoding.getClass(); // Implicit null check
data.getClass(); // Implicit null check
if (start >= 0 && 0 <= length && length <= data.length - start) {
/*[IF Sidecar19-SE]*/
StringCoding.Result scResult = StringCoding.decode(encoding, data, start, length);
if (enableCompression) {
if (String.LATIN1 == scResult.coder) {
value = scResult.value;
count = scResult.value.length;
} else {
value = scResult.value;
count = scResult.value.length / 2 | uncompressedBit;
initCompressionFlag();
}
} else {
value = scResult.value;
count = scResult.value.length / 2;
}
/*[ELSE]*/
char[] buffer = StringCoding.decode(encoding, data, start, length);
if (enableCompression) {
if (compressible(buffer, 0, buffer.length)) {
value = new char[(buffer.length + 1) / 2];
count = buffer.length;
compress(buffer, 0, value, 0, buffer.length);
} else {
value = buffer;
count = buffer.length | uncompressedBit;
initCompressionFlag();
}
} else {
value = buffer;
count = buffer.length;
}
/*[ENDIF]*/
} else {
throw new StringIndexOutOfBoundsException();
}
/*[IF Java10]*/
coder = coder();
/*[ENDIF]*/
}
/**
* Converts the byte array to a String using the specified encoding.
*
* @param data
* the byte array to convert to a String
* @param encoding
* the encoding
*
* @throws UnsupportedEncodingException
* when encoding is not supported
* @throws NullPointerException
* when data is null
*
* @see #getBytes()
* @see #getBytes(int, int, byte[], int)
* @see #getBytes(String)
* @see #valueOf(boolean)
* @see #valueOf(char)
* @see #valueOf(char[])
* @see #valueOf(char[], int, int)
* @see #valueOf(double)
* @see #valueOf(float)
* @see #valueOf(int)
* @see #valueOf(long)
* @see #valueOf(Object)
* @see UnsupportedEncodingException
*/
public String(byte[] data, String encoding) throws UnsupportedEncodingException {
this(data, 0, data.length, encoding);
}
/*[PR 92799] for JIT optimization */
private String(String s, char c) {
if (s == null) {
s = "null"; //$NON-NLS-1$
}
int slen = s.lengthInternal();
int concatlen = slen + 1;
if (enableCompression) {
// Check if the String is compressed
if ((null == compressionFlag || s.count >= 0) && c <= 255) {
/*[IF Sidecar19-SE]*/
value = new byte[concatlen];
/*[ELSE]*/
value = new char[(concatlen + 1) / 2];
/*[ENDIF]*/
count = concatlen;
compressedArrayCopy(s.value, 0, value, 0, slen);
helpers.putByteInArrayByIndex(value, slen, (byte) c);
} else {
/*[IF Sidecar19-SE]*/
value = new byte[concatlen * 2];
count = concatlen | uncompressedBit;
decompressedArrayCopy(s.value, 0, value, 0, slen);
helpers.putCharInArrayByIndex(value, slen, c);
/*[ELSE]*/
value = new char[concatlen];
count = (concatlen) | uncompressedBit;
System.arraycopy(s.value, 0, value, 0, slen);
value[slen] = c;
/*[ENDIF]*/
initCompressionFlag();
}
} else {
/*[IF Sidecar19-SE]*/
value = new byte[concatlen * 2];
count = concatlen;
decompressedArrayCopy(s.value, 0, value, 0, slen);
helpers.putCharInArrayByIndex(value, slen, c);
/*[ELSE]*/
value = new char[concatlen];
count = concatlen;
System.arraycopy(s.value, 0, value, 0, slen);
value[slen] = c;
/*[ENDIF]*/
}
/*[IF Java10]*/
coder = coder();
/*[ENDIF]*/
}
/**
* Initializes this String to contain the characters in the specified character array. Modifying the character array after creating the String has
* no effect on the String.
*
* @param data
* the array of characters
*
* @throws NullPointerException
* when data is null
*/
public String(char[] data) {
this(data, 0, data.length);
}
/**
* Initializes this String to use the specified character array. The character array should not be modified after the String is created.
*
* @param data
* a non-null array of characters
*/
String(char[] data, boolean ignore) {
if (enableCompression) {
if (compressible(data, 0, data.length)) {
/*[IF Sidecar19-SE]*/
value = new byte[data.length];
/*[ELSE]*/
value = new char[(data.length + 1) / 2];
/*[ENDIF]*/
count = data.length;
compress(data, 0, value, 0, data.length);
} else {
/*[IF Sidecar19-SE]*/
value = new byte[data.length * 2];
count = data.length | uncompressedBit;
decompressedArrayCopy(data, 0, value, 0, data.length);
/*[ELSE]*/
value = new char[data.length];
count = data.length | uncompressedBit;
System.arraycopy(data, 0, value, 0, data.length);
/*[ENDIF]*/
initCompressionFlag();
}
} else {
/*[IF Sidecar19-SE]*/
value = new byte[data.length * 2];
count = data.length;
decompressedArrayCopy(data, 0, value, 0, data.length);
/*[ELSE]*/
value = new char[data.length];
count = data.length;
System.arraycopy(data, 0, value, 0, data.length);
/*[ENDIF]*/
}
/*[IF Java10]*/
coder = coder();
/*[ENDIF]*/
}
/**
* Initializes this String to contain the specified characters in the character array. Modifying the character array after creating the String has
* no effect on the String.
*
* @param data
* the array of characters
* @param start
* the starting offset in the character array
* @param length
* the number of characters to use
*
* @throws IndexOutOfBoundsException
* when <code>length < 0, start < 0</code> or <code>start + length > data.length</code>
* @throws NullPointerException
* when data is null
*/
public String(char[] data, int start, int length) {
if (start >= 0 && 0 <= length && length <= data.length - start) {
if (enableCompression) {
if (compressible(data, start, length)) {
/*[IF Sidecar19-SE]*/
value = new byte[length];
/*[ELSE]*/
value = new char[(length + 1) / 2];
/*[ENDIF]*/
count = length;
compress(data, start, value, 0, length);
} else {
/*[IF Sidecar19-SE]*/
value = new byte[length * 2];
count = length | uncompressedBit;
decompressedArrayCopy(data, start, value, 0, length);
/*[ELSE]*/
value = new char[length];
count = length | uncompressedBit;
System.arraycopy(data, start, value, 0, length);
/*[ENDIF]*/
initCompressionFlag();
}
} else {
/*[IF Sidecar19-SE]*/
value = new byte[length * 2];
count = length;
decompressedArrayCopy(data, start, value, 0, length);
/*[ELSE]*/
value = new char[length];
count = length;
System.arraycopy(data, start, value, 0, length);
/*[ENDIF]*/
}
} else {
throw new StringIndexOutOfBoundsException();
}
/*[IF Java10]*/
coder = coder();
/*[ENDIF]*/
}
/*[IF Sidecar19-SE]*/
String(byte[] data, int start, int length, boolean compressed) {
/*[ELSE]*/
String(char[] data, int start, int length, boolean compressed) {
/*[ENDIF]*/
if (enableCompression) {
if (length == 0) {
value = emptyValue;
count = 0;
} else if (length == 1) {
if (compressed) {
char theChar = helpers.byteToCharUnsigned(helpers.getByteFromArrayByIndex(data, start));
value = compressedAsciiTable[theChar];
count = 1;
hashCode = theChar;
} else {
/*[IF]*/
// TODO : We should check if start == 0 and not allocate here as well. See the compression disabled path.
/*[ENDIF]*/
/*[IF Sidecar19-SE]*/
char theChar = helpers.getCharFromArrayByIndex(data, start);
/*[ELSE]*/
char theChar = data[start];