Check-in [8b68b756c8]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Change String to use character arrays instead of sequences (faster).
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:8b68b756c83caeb3e3ae966cc1a081ec8d73fd86
User & Date: stephanie.gawroriski 2019-05-24 14:09:03
Context
2019-05-24
14:28
Implement String.intern() using an internal LinkedList<WeakReference<String>> table. check-in: b3ceace762 user: stephanie.gawroriski tags: trunk
14:09
Change String to use character arrays instead of sequences (faster). check-in: 8b68b756c8 user: stephanie.gawroriski tags: trunk
13:38
In String.replace() if the input string does not contain the character to be replaced then it returns `this`. check-in: 74c9fa8b65 user: stephanie.gawroriski tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to runt/apis/cldc-compact/java/lang/String.java.

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
...
274
275
276
277
278
279
280
281


















282
283
284
285
286
287
288
...
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
...
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
...
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
...
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
...
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
...
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
...
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
...
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
...
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
...
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
...
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
...
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
...
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
....
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
....
1057
1058
1059
1060
1061
1062
1063






1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
....
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
....
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
....
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
....
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
....
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
....
1232
1233
1234
1235
1236
1237
1238

1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
....
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
....
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
....
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
	private static final short _QUICK_ISUPPER =
		0b0000_0000__0000_0010;
	
	/** String is already interned? */
	private static final short _QUICK_INTERN =
		0b0000_0000__0000_0100;
	
	/** The basic character sequence data. */
	@Deprecated
	private final BasicSequence _sequence;
	
	/** Quick determination flags for speedy operations. */
	private volatile short _quickflags;
	
	/** The hash code for this string, is cached. */
	private int _hashcode;
	
................................................................................
		
		// Copy characters
		char[] copy = new char[__l];
		for (int i = __o, o = 0; o < __l; i++, o++)
			copy[o] = __c[i];
		
		// Just use the copied buffer
		this._sequence = new CharArraySequence(copy);
	}
	
	/**
	 * Decodes the given bytes to a string using the specified encoding.
	 *
	 * @param __b The input bytes to decode.
	 * @param __o The offset into the array.
................................................................................
			// Store
			out[at++] = ch;
		}
		
		// Just allocate an exact buffer since the estimate could have been off
		if (at != cap)
			out = Arrays.copyOf(out, at);
		this._sequence = new CharArraySequence(out);


















	}
	
	/**
	 * Initializes the string using the given sequence for characters.
	 *
	 * @param __bs The sequence of characters to use.
	 * @throws NullPointerException On null arguments.
................................................................................
	 * @throws NullPointerException On null arguments.
	 * @since 2018/02/24
	 */
	@Deprecated
	private String(BasicSequence __bs, short __qf)
		throws NullPointerException
	{
		if (__bs == null)
			throw new NullPointerException("NARG");
		
		this._sequence = __bs;
		this._quickflags = __qf;
	}
	
	/**
	 * {@inheritDoc}
	 * @since 2018/09/20
	 */
	@Override
	public char charAt(int __i)
		throws IndexOutOfBoundsException
	{
		// Bounds checking is handled by the sequence
		return this._sequence.charAt(__i);


	}
	
	/**
	 * Compares the character values of this string and compares it to the
	 * character values of the other string.
	 *
	 * Smaller strings always precede longer strings.
................................................................................
	 *
	 * This is equivalent to the standard POSIX {@code strcmp()} with the "C"
	 * locale.
	 *
	 * Internally this does not handle the special variants of this class and
	 * is a general purpose method.
	 *
	 * @param __os The string to compare against.
	 * @return A negative value if this string precedes the other string, a
	 * positive value if this string procedes the other string, or zero if the
	 * strings are equal.
	 * @throws NullPointerException On null arguments.
	 * @since 2016/04/02
	 */
	public int compareTo(String __os)
		throws NullPointerException
	{
		// Check
		if (__os == null)
			throw new NullPointerException("NARG");
		








		// Get both string lengths
		int an = this.length();
		int bn = __os.length();
		
		// Max comparison length
		int max = Math.min(an, bn);
		
		// Compare both strings
		for (int i = 0; i < max; i++)
		{
			// Get character difference
			int diff = ((int)this.charAt(i)) - ((int)__os.charAt(i));
			
			// If there is a difference, then return it
			if (diff != 0)
				return diff;
		}
		
		// Remaining comparison is the length parameter, shorter strings are
................................................................................
	 * @since 2018/11/04
	 */
	public int compareToIgnoreCase(String __o)
		throws NullPointerException
	{
		if (__o == null)
			throw new NullPointerException("NARG");
		








		// Get both string lengths
		int an = this.length();
		int bn = __o.length();
		
		// Max comparison length
		int max = Math.min(an, bn);
		
		// Compare both strings
		for (int i = 0; i < max; i++)
		{
			// Get both characters and normalize case
			char ca = Character.toLowerCase(
					Character.toUpperCase(this.charAt(i))),
				cb = Character.toLowerCase(
					Character.toUpperCase(__o.charAt(i)));
			
			// Get character difference
			int diff = ca - cb;
			
			// If there is a difference, then return it
			if (diff != 0)
				return diff;
................................................................................
	 */
	public String concat(String __s)
		throws NullPointerException
	{
		// Check
		if (__s == null)
			throw new NullPointerException("NARG");
		
		// Short circuits to not do anything





		if (this.length() == 0)



			return __s;
		else if (__s.length() == 0)
			return this;
		
		// Just build a new string this way because it is much simpler than
		// having to mess with an internal representation of specially













		// encoded strings
		StringBuilder sb = new StringBuilder(this);
		sb.append(__s);
		return sb.toString();

	}
	
	/**
	 * Returns {@code true} if the string contains the given sequence.
	 *
	 * @param __b The sequence to find.
	 * @return If the string contains the given sequence or not.
................................................................................
	 */
	public boolean endsWith(String __s)
		throws NullPointerException
	{
		if (__s == null)
			throw new NullPointerException("NARG");
		
		// Empty string is always a match
		if (__s.equals(""))
			return true;
		
		// Work on sequences
		BasicSequence sa = this._sequence,
			sb = __s._sequence;
		




		// If our string is smaller than the other string then it will not
		// fit and as such, will not work
		int na = sa.length(),
			nb = sb.length();
		if (na < nb)
			return false;
		
		// Check all characters at the end of the string, we fail if there is
		// a mismatch
		for (int ia = na - nb, ib = 0; ia < na; ia++, ib++)
			if (sa.charAt(ia) != sb.charAt(ib))
				return false;
		
		// Is a match since nothing failed!
		return true;
	}
	
	/**
................................................................................
		// This at best removes all loops and just results in a simple integer
		// comparison being used
		int an = this.hashCode(),
			bn = o.hashCode();
		if (an != bn)
			return false;
		
		// Work on sequences now
		BasicSequence sa = this._sequence,
			sb = o._sequence;
		
		// If the sequence is the same then this represents the same string
		if (sa == sb)
			return true;
		
		// If the length differs, they are not equal
		an = sa.length();
		bn = sb.length();
		if (an != bn)
			return false;
		
		// Compare individual characters
		for (int i = 0; i < an; i++)
			if (sa.charAt(i) != sb.charAt(i))
				return false;
		
		// Would be a match!
		return true;
	}
	
	/**
................................................................................
	@ProgrammerTip("Locale is not considered.")
	public boolean equalsIgnoreCase(String __o)
	{
		// Always false
		if (__o == null)
			return false;
		
		// Directly use sequences, is faster
		BasicSequence sa = this._sequence,
			sb = __o._sequence;
		
		// Two strings of inequal length will never be the same
		int n = sa.length();
		if (n != sb.length())
			return false;
		
		// Check characters
		for (int i = 0; i < n; i++)
		{
			char a = sa.charAt(i),
				b = sb.charAt(i);
			
			// Is a different character?
			if (a != b &&
				Character.toUpperCase(a) != Character.toUpperCase(b) &&
				Character.toLowerCase(b) != Character.toLowerCase(b))
				return false;
		}
		
		// The same
		return true;
	}
	
................................................................................
		// cache
		int rv = this._hashcode;
		if (rv != 0)
			return rv;
		
		// Calculate the hashCode(), the JavaDoc gives the following formula:
		// == s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] .... yikes!
		BasicSequence sequence = this._sequence;
		for (int i = 0, n = sequence.length(); i < n; i++)
			rv = ((rv << 5) - rv) + sequence.charAt(i);
		
		// Cache hashcode for later
		this._hashcode = rv;
		return rv;
	}
	
	/**
................................................................................
	 * @param __i The index to start at, the values are capped within the
	 * string length.
	 * @return The character index or {@code -1} if it was not found.
	 * @since 2018/09/20
	 */
	public int indexOf(int __c, int __i)
	{
		BasicSequence sequence = this._sequence;
		
		// Cap index
		int n = sequence.length();
		if (__i < 0)
			__i = 0;
		
		for (int i = __i; i < n; i++)
			if (__c == sequence.charAt(i))
				return i;
		
		// Not found
		return -1;
	}
	
	/**
................................................................................
	 * Returns {@code true} if this string is empty.
	 *
	 * @return {@code true} if this string is empty.
	 * @since 2017/08/15
	 */
	public boolean isEmpty()
	{
		return this.length() == 0;
	}
	
	/**
	 * Returns the last occurance of the given character.
	 *
	 * @param __c The character to find.
	 * @return The last occurance of the character or {@code -1} if it was
................................................................................
	public int lastIndexOf(int __c, int __dx)
	{
		// Never going to find anything at all
		if (__dx < 0)
			return -1;
		
		// Cap index
		BasicSequence sequence = this._sequence;
		int n = sequence.length();
		if (__dx >= n)
			__dx = n - 1;
		
		for (; __dx >= 0; __dx--)
			if (__c == sequence.charAt(__dx))
				return __dx;
		
		// Not found
		return -1;
	}
	
	public int lastIndexOf(String __a)
................................................................................
	 * Returns the length of this string.
	 *
	 * @return The length of this string.
	 * @since 2018/09/19
	 */
	public int length()
	{
		return this._sequence.length();
	}
	
	public boolean regionMatches(int __a, String __b, int __c, int __d)
	{
		throw new todo.TODO();
	}
	
................................................................................
		// If a character is going to be replaced with itself then no
		// replacement has to actually be performed. Or if the original
		// character is not even in the string.
		if (__a == __b || this.indexOf(__a) < 0)
			return this;
		
		// Get source sequence
		BasicSequence bs = this._sequence;
		int n = bs.length();
		
		// Copy data into an array with translated characters
		char[] rv = new char[n];
		for (int i = 0; i < n; i++)
		{
			char c = bs.charAt(i);
			if (c == __a)
				c = __b;
			rv[i] = c;
		}
		
		// Build new string
		return new String(new CharArraySequence(rv));
	}
	
	/**
	 * Checks if this string starts with the other string at the given index.
	 *
	 * @param __s The string to check for a starting match.
	 * @param __sdx The starting index to start checking at.
................................................................................
		// {@squirreljme.error ZZ0y Starting index in string is out of
		// bounds. (The starting index)}
		if (__sdx < 0)
			throw new IndexOutOfBoundsException(
				String.format("ZZ0y %d", __sdx));
		
		// Need to work on both sequences
		BasicSequence sa = this._sequence,
			sb = __s._sequence;
		
		// If the second string is empty then it will always match
		int na = sa.length(),
			nb = sb.length();
		if (nb == 0)
			return true;
		
		// The second string cannot even fit from this index so do not bother
		// checking anything
		if (__sdx + nb > na)
			return false;
		
		// Find false match
		for (int ia = __sdx, ib = 0; ib < nb; ia++, ib++)
			if (sa.charAt(ia) != sb.charAt(ib))
				return false;
		
		// False not found, so it matches
		return true;
	}
	
	/**
................................................................................
	 * bounds.
	 * @since 2018/09/29
	 */
	public String substring(int __s, int __e)
		throws IndexOutOfBoundsException
	{
		// The entire string region requires no new string
		BasicSequence sequence = this._sequence;
		int n = sequence.length();
		if (__s == 0 && __e == n)
			return this;
		
		// A blank string with no characters
		if (__s == __e)
			return "";
		
................................................................................
		// {@squirreljme.error ZZ0z String substring is outside of bounds.
		// (The start index; The end index; The length)}
		if (__s < 0 || __s > __e || __e > n)
			throw new IndexOutOfBoundsException("ZZ0z " + __s + " " + __e +
				" " + n);
		
		// Derive sub-sequence






		return new String(sequence.subSequence(__s, __e));
	}
	
	/**
	 * Returns a character array which contains every character within this
	 * string.
	 *
	 * @return A character array containing the characters of this string.
	 * @since 2017/08/15
	 */
	public char[] toCharArray()
	{
		// The sequence may have a faster way to setup a char array
		return this._sequence.toCharArray();
	}
	
	/**
	 * Translates this string to lowercase using the current locale.
	 *
	 * Java ME specifies that only Latin-1 characters are supported
	 *
................................................................................
	public String toLowerCase()
	{
		// If this string is lowercased already do not mess with it
		if ((this._quickflags & String._QUICK_ISLOWER) != 0)
			return this;
		
		// Needed for case conversion
		BasicSequence sequence = this._sequence;
		Locale locale = DefaultLocale.defaultLocale();
		
		// Setup new character array for the conversion
		int n = this.length();
		char[] rv = new char[n];
		
		// Copy and convert characters
		boolean changed = false;
		for (int i = 0; i < n; i++)
		{
			char a = sequence.charAt(i),
				b = locale.toLowerCase(a);
			
			// Detect if the string actually changed
			if (!changed && a != b)
				changed = true;
			
			rv[i] = b;
................................................................................
		if (!changed)
		{
			this._quickflags |= String._QUICK_ISLOWER;
			return this;
		}
		
		// New string will be lowercase, so ignore this operation
		return new String(new CharArraySequence(rv), String._QUICK_ISLOWER);
	}
	
	/**
	 * Returns {@code this}.
	 *
	 * @return {@code this}.
	 * @since 2017/08/15
................................................................................
	public String toUpperCase()
	{
		// If this string is uppercased already do not mess with it
		if ((this._quickflags & String._QUICK_ISUPPER) != 0)
			return this;
		
		// Needed for case conversion
		BasicSequence sequence = this._sequence;
		Locale locale = DefaultLocale.defaultLocale();
		
		// Setup new character array for the conversion
		int n = this.length();
		char[] rv = new char[n];
		
		// Copy and convert characters
		boolean changed = false;
		for (int i = 0; i < n; i++)
		{
			char a = sequence.charAt(i),
				b = locale.toUpperCase(a);
			
			// Detect if the string actually changed
			if (!changed && a != b)
				changed = true;
			
			rv[i] = b;
................................................................................
		if (!changed)
		{
			this._quickflags |= String._QUICK_ISUPPER;
			return this;
		}
		
		// New string will be uppercase, so ignore this operation
		return new String(new CharArraySequence(rv), String._QUICK_ISUPPER);
	}
	
	/**
	 * This trims all of the low ASCII whitespace and control characters at
	 * the start and the end of this string and returns a new string with
	 * the trimmed whitespace.
	 *
................................................................................
	 * @return A string with the whitespace trimmed, if the string does not
	 * start or end in whitespace then {@code this} is returned.
	 * @since 2016/04/20
	 */
	public String trim()
	{
		// Empty strings do not need trimming
		BasicSequence sequence = this._sequence;
		int n = sequence.length();
		if (n <= 0)
			return this;
		
		// Find starting trim position
		int s;
		for (s = 0; s < n && sequence.charAt(s) <= _MIN_TRIM_CHAR; s++)
			;
		
		// Find ending trim position
		int e;
		for (e = n; e > s && sequence.charAt(e - 1) <= _MIN_TRIM_CHAR; e--)
			;
		
		// Return trimmed variant of it
		return substring(s, e);
	}
	
	/**
	 * Checks to see if this string matches the target sequence.
	 *
	 * @param __s The input sequence to check against.
	 * @return If they are the same or not.
................................................................................
		throws NullPointerException
	{
		// Check
		if (__s == null)
			throw new NullPointerException("NARG");
		
		// If the two have different lengths they will never be equal

		int al = this.length(),
			bl = __s.length();
		if (al != bl)
			return false;
		
		// Check each character
		for (int i = 0; i < al; i++)
			if (this.charAt(i) != __s.charAt(i))
				return false;
		
		// If reached, they are equal
		return true;
	}
	
	/**
................................................................................
			throw new NullPointerException("NARG");
		
		// Maximum size of sequences that can be encoded
		int msl;
		byte[] seq = new byte[(msl = __e.maximumSequenceLength())];
		
		// We operate directly on the sequence
		BasicSequence sequence = this._sequence;
		int n = sequence.length();
		
		// Write here
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream(
			(int)(n * __e.averageSequenceLength())))
		{
			// Encode every character!
			for (int i = 0; i < n; i++)
			{
				int sz = __e.encode(sequence.charAt(i), seq, 0, msl);
				
				// Should not occur
				if (sz < 0)
					throw new todo.OOPS();
				
				baos.write(seq, 0, sz);
			}
................................................................................
			throw new NullPointerException("NARG");
		
		// Normalize position
		if (__i < 0)
			__i = 0;
		
		// If the sequence is empty, then it will always be a match
		BasicSequence as = this._sequence;
		int an = as.length(),
			bn = __b.length();
		if (bn <= 0)
			return __i;
		
		// If the string is longer than ours, then it will never be a match
		if (bn > an - __i)
			return -1;
................................................................................
		// Do a long complicated loop matching, but we only need to check
		// for as long as the sequence can actually fit
__outer:
		for (int a = __i, lim = an - bn; a < lim; a++)
		{
			// Check sequence characters
			for (int x = a, b = 0; b < bn; x++, b++)
				if (as.charAt(x) != __b.charAt(b))
					continue __outer;
			
			// Since the inner loop continues to the outer, if this was reached
			// then we know the full sequence was matched
			return a;
		}
		







|
<
|







 







|







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
<
<
<
<










|
|
>
>







 







|






|



|


>
>
>
>
>
>
>
>

|
|








|







 








>
>
>
>
>
>
>
>

|
|








|
<
|
<







 







|
|
>
>
>
>
>
|
>
>
>

|


<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
>







 







|
|
|

|
|
|

>
>
>
>

|
<
<






|







 







|
|
|

<
<
<
<

|
|
<



|
|







 







|
|
|


|
|





|
|


|
<
<







 







|
|
|







 







|


|




|







 







|







 







|
|




|







 







|







 







|
|





|






|







 







|
|


|
|










|







 







|
|







 







>
>
>
>
>
>
|











|
|







 







|



|






|







 







|







 







|



|






|







 







|







 







|
|





|




|



|







 







>
|






|







 







|
|








|







 







|
|







 







|







65
66
67
68
69
70
71
72

73
74
75
76
77
78
79
80
...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
...
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
...
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
...
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
...
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
...
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
...
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
...
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
...
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
...
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
...
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
...
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
...
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
...
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
....
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
....
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
....
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
....
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
....
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
....
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
....
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
....
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
....
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
....
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
....
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
....
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
	private static final short _QUICK_ISUPPER =
		0b0000_0000__0000_0010;
	
	/** String is already interned? */
	private static final short _QUICK_INTERN =
		0b0000_0000__0000_0100;
	
	/** String character data. */

	private final char[] _chars;
	
	/** Quick determination flags for speedy operations. */
	private volatile short _quickflags;
	
	/** The hash code for this string, is cached. */
	private int _hashcode;
	
................................................................................
		
		// Copy characters
		char[] copy = new char[__l];
		for (int i = __o, o = 0; o < __l; i++, o++)
			copy[o] = __c[i];
		
		// Just use the copied buffer
		this._chars = copy;
	}
	
	/**
	 * Decodes the given bytes to a string using the specified encoding.
	 *
	 * @param __b The input bytes to decode.
	 * @param __o The offset into the array.
................................................................................
			// Store
			out[at++] = ch;
		}
		
		// Just allocate an exact buffer since the estimate could have been off
		if (at != cap)
			out = Arrays.copyOf(out, at);
		this._chars = out;
	}
	
	/**
	 * Initializes the string using the given character data.
	 *
	 * @param __c Character data.
	 * @param __qf The quick flags to use.
	 * @throws NullPointerException On null arguments.
	 * @since 2019/05/24
	 */
	String(char[] __c, short __qf)
		throws NullPointerException
	{
		if (__c == null)
			throw new NullPointerException("NARG");
		
		this._chars = __c;
		this._quickflags = __qf;
	}
	
	/**
	 * Initializes the string using the given sequence for characters.
	 *
	 * @param __bs The sequence of characters to use.
	 * @throws NullPointerException On null arguments.
................................................................................
	 * @throws NullPointerException On null arguments.
	 * @since 2018/02/24
	 */
	@Deprecated
	private String(BasicSequence __bs, short __qf)
		throws NullPointerException
	{
		this(__bs.toCharArray(), __qf);




	}
	
	/**
	 * {@inheritDoc}
	 * @since 2018/09/20
	 */
	@Override
	public char charAt(int __i)
		throws IndexOutOfBoundsException
	{
		char[] chars = this._chars;
		if (__i < 0 || __i >= chars.length)
			throw new IndexOutOfBoundsException("IOOB " + __i);
		return chars[__i];
	}
	
	/**
	 * Compares the character values of this string and compares it to the
	 * character values of the other string.
	 *
	 * Smaller strings always precede longer strings.
................................................................................
	 *
	 * This is equivalent to the standard POSIX {@code strcmp()} with the "C"
	 * locale.
	 *
	 * Internally this does not handle the special variants of this class and
	 * is a general purpose method.
	 *
	 * @param __o The string to compare against.
	 * @return A negative value if this string precedes the other string, a
	 * positive value if this string procedes the other string, or zero if the
	 * strings are equal.
	 * @throws NullPointerException On null arguments.
	 * @since 2016/04/02
	 */
	public int compareTo(String __o)
		throws NullPointerException
	{
		// Check
		if (__o == null)
			throw new NullPointerException("NARG");
		
		// Refers to the same exact string?
		if (this == __o)
			return 0;
		
		// Characters of both
		char[] ac = this._chars,
			bc = __o._chars;
		
		// Get both string lengths
		int an = ac.length;
		int bn = bc.length;
		
		// Max comparison length
		int max = Math.min(an, bn);
		
		// Compare both strings
		for (int i = 0; i < max; i++)
		{
			// Get character difference
			int diff = ((int)ac[i]) - ((int)bc[i]);
			
			// If there is a difference, then return it
			if (diff != 0)
				return diff;
		}
		
		// Remaining comparison is the length parameter, shorter strings are
................................................................................
	 * @since 2018/11/04
	 */
	public int compareToIgnoreCase(String __o)
		throws NullPointerException
	{
		if (__o == null)
			throw new NullPointerException("NARG");
		
		// Refers to the same exact string?
		if (this == __o)
			return 0;
		
		// Characters of both
		char[] ac = this._chars,
			bc = __o._chars;
		
		// Get both string lengths
		int an = ac.length;
		int bn = bc.length;
		
		// Max comparison length
		int max = Math.min(an, bn);
		
		// Compare both strings
		for (int i = 0; i < max; i++)
		{
			// Get both characters and normalize case
			char ca = Character.toLowerCase(Character.toUpperCase(ac[i])),

				cb = Character.toLowerCase(Character.toUpperCase(bc[i]));

			
			// Get character difference
			int diff = ca - cb;
			
			// If there is a difference, then return it
			if (diff != 0)
				return diff;
................................................................................
	 */
	public String concat(String __s)
		throws NullPointerException
	{
		// Check
		if (__s == null)
			throw new NullPointerException("NARG");
			
		// Get both character sources
		char[] ac = this._chars,
			bc = __s._chars;
		
		// Lengths
		int an = ac.length,
			bn = bc.length;
		
		// One of the strings has no length, which means it will be a no-op
		if (an == 0)
			return __s;
		else if (bn == 0)
			return this;
		


		// Setup result
		int nl = an + bn;
		char[] rv = new char[nl];
		
		// Copy first part
		int o = 0;
		for (int i = 0; i < an; i++)
			rv[o++] = ac[i];
		
		// Copy second
		for (int i = 0; i < bn; i++)
			rv[o++] = bc[i];
		
		// Build string



		return new String(rv, (short)0);
	}
	
	/**
	 * Returns {@code true} if the string contains the given sequence.
	 *
	 * @param __b The sequence to find.
	 * @return If the string contains the given sequence or not.
................................................................................
	 */
	public boolean endsWith(String __s)
		throws NullPointerException
	{
		if (__s == null)
			throw new NullPointerException("NARG");
		
		// Character data
		char[] ac = this._chars,
			bc = __s._chars;
		
		// Lengths
		int na = ac.length,
			nb = bc.length;
		
		// If the other string is empty, it is always a match
		if (nb == 0)
			return true;
		
		// If our string is smaller than the other string then it will not
		// fit and as such, will not match


		if (na < nb)
			return false;
		
		// Check all characters at the end of the string, we fail if there is
		// a mismatch
		for (int ia = na - nb, ib = 0; ia < na; ia++, ib++)
			if (ac[ia] != bc[ib])
				return false;
		
		// Is a match since nothing failed!
		return true;
	}
	
	/**
................................................................................
		// This at best removes all loops and just results in a simple integer
		// comparison being used
		int an = this.hashCode(),
			bn = o.hashCode();
		if (an != bn)
			return false;
		
		// Character data
		char[] ac = this._chars,
			bc = o._chars;
		




		// If the length differs, they are not equal
		int n = ac.length;
		if (n != bc.length)

			return false;
		
		// Compare individual characters
		for (int i = 0; i < n; i++)
			if (ac[i] != bc[i])
				return false;
		
		// Would be a match!
		return true;
	}
	
	/**
................................................................................
	@ProgrammerTip("Locale is not considered.")
	public boolean equalsIgnoreCase(String __o)
	{
		// Always false
		if (__o == null)
			return false;
		
		// Character data
		char[] ac = this._chars,
			bc = __o._chars;
		
		// Two strings of inequal length will never be the same
		int n = ac.length;
		if (n != bc.length)
			return false;
		
		// Check characters
		for (int i = 0; i < n; i++)
		{
			char a = Character.toLowerCase(Character.toUpperCase(ac[i])),
				b = Character.toLowerCase(Character.toUpperCase(bc[i]));
			
			// Is a different character?
			if (a != b)


				return false;
		}
		
		// The same
		return true;
	}
	
................................................................................
		// cache
		int rv = this._hashcode;
		if (rv != 0)
			return rv;
		
		// Calculate the hashCode(), the JavaDoc gives the following formula:
		// == s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] .... yikes!
		char[] ch = this._chars;
		for (int i = 0, n = ch.length; i < n; i++)
			rv = ((rv << 5) - rv) + ch[i];
		
		// Cache hashcode for later
		this._hashcode = rv;
		return rv;
	}
	
	/**
................................................................................
	 * @param __i The index to start at, the values are capped within the
	 * string length.
	 * @return The character index or {@code -1} if it was not found.
	 * @since 2018/09/20
	 */
	public int indexOf(int __c, int __i)
	{
		char[] ch = this._chars;
		
		// Cap index
		int n = ch.length;
		if (__i < 0)
			__i = 0;
		
		for (int i = __i; i < n; i++)
			if (__c == ch[i])
				return i;
		
		// Not found
		return -1;
	}
	
	/**
................................................................................
	 * Returns {@code true} if this string is empty.
	 *
	 * @return {@code true} if this string is empty.
	 * @since 2017/08/15
	 */
	public boolean isEmpty()
	{
		return this._chars.length == 0;
	}
	
	/**
	 * Returns the last occurance of the given character.
	 *
	 * @param __c The character to find.
	 * @return The last occurance of the character or {@code -1} if it was
................................................................................
	public int lastIndexOf(int __c, int __dx)
	{
		// Never going to find anything at all
		if (__dx < 0)
			return -1;
		
		// Cap index
		char[] ch = this._chars;
		int n = ch.length;
		if (__dx >= n)
			__dx = n - 1;
		
		for (; __dx >= 0; __dx--)
			if (__c == ch[__dx])
				return __dx;
		
		// Not found
		return -1;
	}
	
	public int lastIndexOf(String __a)
................................................................................
	 * Returns the length of this string.
	 *
	 * @return The length of this string.
	 * @since 2018/09/19
	 */
	public int length()
	{
		return this._chars.length;
	}
	
	public boolean regionMatches(int __a, String __b, int __c, int __d)
	{
		throw new todo.TODO();
	}
	
................................................................................
		// If a character is going to be replaced with itself then no
		// replacement has to actually be performed. Or if the original
		// character is not even in the string.
		if (__a == __b || this.indexOf(__a) < 0)
			return this;
		
		// Get source sequence
		char[] ch = this._chars;
		int n = ch.length;
		
		// Copy data into an array with translated characters
		char[] rv = new char[n];
		for (int i = 0; i < n; i++)
		{
			char c = ch[i];
			if (c == __a)
				c = __b;
			rv[i] = c;
		}
		
		// Build new string
		return new String(rv, (short)0);
	}
	
	/**
	 * Checks if this string starts with the other string at the given index.
	 *
	 * @param __s The string to check for a starting match.
	 * @param __sdx The starting index to start checking at.
................................................................................
		// {@squirreljme.error ZZ0y Starting index in string is out of
		// bounds. (The starting index)}
		if (__sdx < 0)
			throw new IndexOutOfBoundsException(
				String.format("ZZ0y %d", __sdx));
		
		// Need to work on both sequences
		char[] ca = this._chars,
			cb = __s._chars;
		
		// If the second string is empty then it will always match
		int na = ca.length,
			nb = cb.length;
		if (nb == 0)
			return true;
		
		// The second string cannot even fit from this index so do not bother
		// checking anything
		if (__sdx + nb > na)
			return false;
		
		// Find false match
		for (int ia = __sdx, ib = 0; ib < nb; ia++, ib++)
			if (ca[ia] != cb[ib])
				return false;
		
		// False not found, so it matches
		return true;
	}
	
	/**
................................................................................
	 * bounds.
	 * @since 2018/09/29
	 */
	public String substring(int __s, int __e)
		throws IndexOutOfBoundsException
	{
		// The entire string region requires no new string
		char[] ch = this._chars;
		int n = ch.length;
		if (__s == 0 && __e == n)
			return this;
		
		// A blank string with no characters
		if (__s == __e)
			return "";
		
................................................................................
		// {@squirreljme.error ZZ0z String substring is outside of bounds.
		// (The start index; The end index; The length)}
		if (__s < 0 || __s > __e || __e > n)
			throw new IndexOutOfBoundsException("ZZ0z " + __s + " " + __e +
				" " + n);
		
		// Derive sub-sequence
		int nl = __e - __s;
		char[] rv = new char[nl];
		for (int o = 0; o < nl; o++, __s++)
			rv[o] = ch[__s];
		
		// Build
		return new String(rv, (short)0);
	}
	
	/**
	 * Returns a character array which contains every character within this
	 * string.
	 *
	 * @return A character array containing the characters of this string.
	 * @since 2017/08/15
	 */
	public char[] toCharArray()
	{
		// Direct copy of the character array
		return this._chars.clone();
	}
	
	/**
	 * Translates this string to lowercase using the current locale.
	 *
	 * Java ME specifies that only Latin-1 characters are supported
	 *
................................................................................
	public String toLowerCase()
	{
		// If this string is lowercased already do not mess with it
		if ((this._quickflags & String._QUICK_ISLOWER) != 0)
			return this;
		
		// Needed for case conversion
		char[] ch = this._chars;
		Locale locale = DefaultLocale.defaultLocale();
		
		// Setup new character array for the conversion
		int n = ch.length;
		char[] rv = new char[n];
		
		// Copy and convert characters
		boolean changed = false;
		for (int i = 0; i < n; i++)
		{
			char a = ch[i],
				b = locale.toLowerCase(a);
			
			// Detect if the string actually changed
			if (!changed && a != b)
				changed = true;
			
			rv[i] = b;
................................................................................
		if (!changed)
		{
			this._quickflags |= String._QUICK_ISLOWER;
			return this;
		}
		
		// New string will be lowercase, so ignore this operation
		return new String(rv, String._QUICK_ISLOWER);
	}
	
	/**
	 * Returns {@code this}.
	 *
	 * @return {@code this}.
	 * @since 2017/08/15
................................................................................
	public String toUpperCase()
	{
		// If this string is uppercased already do not mess with it
		if ((this._quickflags & String._QUICK_ISUPPER) != 0)
			return this;
		
		// Needed for case conversion
		char[] ch = this._chars;
		Locale locale = DefaultLocale.defaultLocale();
		
		// Setup new character array for the conversion
		int n = ch.length;
		char[] rv = new char[n];
		
		// Copy and convert characters
		boolean changed = false;
		for (int i = 0; i < n; i++)
		{
			char a = ch[i],
				b = locale.toUpperCase(a);
			
			// Detect if the string actually changed
			if (!changed && a != b)
				changed = true;
			
			rv[i] = b;
................................................................................
		if (!changed)
		{
			this._quickflags |= String._QUICK_ISUPPER;
			return this;
		}
		
		// New string will be uppercase, so ignore this operation
		return new String(rv, String._QUICK_ISUPPER);
	}
	
	/**
	 * This trims all of the low ASCII whitespace and control characters at
	 * the start and the end of this string and returns a new string with
	 * the trimmed whitespace.
	 *
................................................................................
	 * @return A string with the whitespace trimmed, if the string does not
	 * start or end in whitespace then {@code this} is returned.
	 * @since 2016/04/20
	 */
	public String trim()
	{
		// Empty strings do not need trimming
		char[] ch = this._chars;
		int n = ch.length;
		if (n <= 0)
			return this;
		
		// Find starting trim position
		int s;
		for (s = 0; s < n && ch[s] <= _MIN_TRIM_CHAR; s++)
			;
		
		// Find ending trim position
		int e;
		for (e = n; e > s && ch[e - 1] <= _MIN_TRIM_CHAR; e--)
			;
		
		// Return trimmed variant of it
		return this.substring(s, e);
	}
	
	/**
	 * Checks to see if this string matches the target sequence.
	 *
	 * @param __s The input sequence to check against.
	 * @return If they are the same or not.
................................................................................
		throws NullPointerException
	{
		// Check
		if (__s == null)
			throw new NullPointerException("NARG");
		
		// If the two have different lengths they will never be equal
		char[] ca = this._chars;
		int al = ca.length,
			bl = __s.length();
		if (al != bl)
			return false;
		
		// Check each character
		for (int i = 0; i < al; i++)
			if (ca[i] != __s.charAt(i))
				return false;
		
		// If reached, they are equal
		return true;
	}
	
	/**
................................................................................
			throw new NullPointerException("NARG");
		
		// Maximum size of sequences that can be encoded
		int msl;
		byte[] seq = new byte[(msl = __e.maximumSequenceLength())];
		
		// We operate directly on the sequence
		char[] ch = this._chars;
		int n = ch.length;
		
		// Write here
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream(
			(int)(n * __e.averageSequenceLength())))
		{
			// Encode every character!
			for (int i = 0; i < n; i++)
			{
				int sz = __e.encode(ch[i], seq, 0, msl);
				
				// Should not occur
				if (sz < 0)
					throw new todo.OOPS();
				
				baos.write(seq, 0, sz);
			}
................................................................................
			throw new NullPointerException("NARG");
		
		// Normalize position
		if (__i < 0)
			__i = 0;
		
		// If the sequence is empty, then it will always be a match
		char[] ca = this._chars;
		int an = ca.length,
			bn = __b.length();
		if (bn <= 0)
			return __i;
		
		// If the string is longer than ours, then it will never be a match
		if (bn > an - __i)
			return -1;
................................................................................
		// Do a long complicated loop matching, but we only need to check
		// for as long as the sequence can actually fit
__outer:
		for (int a = __i, lim = an - bn; a < lim; a++)
		{
			// Check sequence characters
			for (int x = a, b = 0; b < bn; x++, b++)
				if (ca[x] != __b.charAt(b))
					continue __outer;
			
			// Since the inner loop continues to the outer, if this was reached
			// then we know the full sequence was matched
			return a;
		}