-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathCharacter.java
More file actions
1035 lines (957 loc) · 36 KB
/
Character.java
File metadata and controls
1035 lines (957 loc) · 36 KB
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
/*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores
* CA 94065 USA or visit www.oracle.com if you need additional information or
* have any questions.
*/
package java.lang;
/**
* The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.
* In addition, this class provides several methods for determining the type of a character and converting characters from uppercase to lowercase and vice versa.
* Character information is based on the Unicode Standard, version 3.0. However, in order to reduce footprint, by default the character property and case conversion operations in CLDC are available only for the ISO Latin-1 range of characters. Other Unicode character blocks can be supported as necessary.
* Since: JDK1.0, CLDC 1.0
*/
public final class Character{
/**
* The maximum radix available for conversion to and from Strings.
* See Also:Integer.toString(int, int), Integer.valueOf(java.lang.String), Constant Field Values
*/
public static final int MAX_RADIX=36;
/**
* The constant value of this field is the largest value of type char.
* Since: JDK1.0.2 See Also:Constant Field Values
*/
public static final char MAX_VALUE=(char)65535;
/**
* The minimum radix available for conversion to and from Strings.
* See Also:Integer.toString(int, int), Integer.valueOf(java.lang.String), Constant Field Values
*/
public static final int MIN_RADIX=2;
/**
* The constant value of this field is the smallest value of type char.
* Since: JDK1.0.2 See Also:Constant Field Values
*/
public static final char MIN_VALUE=(char)0;
/**
* Constructs a Character object and initializes it so that it represents the primitive value argument.
* value - value for the new Character object.
*/
public Character(char value){
//TODO codavaj!!
}
/**
* Returns the value of this Character object.
*/
public char charValue(){
return ' '; //TODO codavaj!!
}
/**
* Returns the numeric value of the character ch in the specified radix.
*/
public static int digit(char ch, int radix){
return 0; //TODO codavaj!!
}
/**
* Compares this object against the specified object. The result is true if and only if the argument is not null and is a Character object that represents the same char value as this object.
*/
public boolean equals(java.lang.Object obj){
return false; //TODO codavaj!!
}
/**
* Returns a hash code for this Character.
*/
public int hashCode(){
return 0; //TODO codavaj!!
}
/**
* Determines if the specified character is alphabetic.
*/
static boolean isLetterCompat(char ch){
return isLowerCase(ch) || isUpperCase(ch);
}
/**
* Determines if the specified character is numeric.
*/
static boolean isDigitCompat(char ch){
return isDigit(ch);
}
/**
* Determines if the specified character is alphabetic or numeric.
*/
static boolean isLetterOrDigitCompat(char ch){
return isLetterCompat(ch) || isDigitCompat(ch);
}
/**
* Determines if the specified character is a digit.
*/
public static boolean isDigit(char ch){
return false; //TODO codavaj!!
}
/**
* Determines if the specified character is a lowercase character.
* Note that by default CLDC only supports the ISO Latin-1 range of characters.
* Of the ISO Latin-1 characters (character codes 0x0000 through 0x00FF), the following are lowercase:
* a b c d e f g h i j k l m n o p q r s t u v w x y z u00DF u00E0 u00E1 u00E2 u00E3 u00E4 u00E5 u00E6 u00E7 u00E8 u00E9 u00EA u00EB u00EC u00ED u00EE u00EF u00F0 u00F1 u00F2 u00F3 u00F4 u00F5 u00F6 u00F8 u00F9 u00FA u00FB u00FC u00FD u00FE u00FF
*/
public static boolean isLowerCase(char ch){
return false; //TODO codavaj!!
}
/**
* Determines if the specified character is an uppercase character.
* Note that by default CLDC only supports the ISO Latin-1 range of characters.
* Of the ISO Latin-1 characters (character codes 0x0000 through 0x00FF), the following are uppercase:
* A B C D E F G H I J K L M N O P Q R S T U V W X Y Z u00C0 u00C1 u00C2 u00C3 u00C4 u00C5 u00C6 u00C7 u00C8 u00C9 u00CA u00CB u00CC u00CD u00CE u00CF u00D0 u00D1 u00D2 u00D3 u00D4 u00D5 u00D6 u00D8 u00D9 u00DA u00DB u00DC u00DD u00DE
*/
public static boolean isUpperCase(char ch){
return false; //TODO codavaj!!
}
/**
* The given character is mapped to its lowercase equivalent; if the character has no lowercase equivalent, the character itself is returned.
* Note that by default CLDC only supports the ISO Latin-1 range of characters.
*/
public static char toLowerCase(char ch){
return ' '; //TODO codavaj!!
}
/**
* Returns a String object representing this character's value. Converts this Character object to a string. The result is a string whose length is 1. The string's sole component is the primitive char value represented by this object.
*/
public java.lang.String toString(){
return null; //TODO codavaj!!
}
/**
* Converts the character argument to uppercase; if the character has no uppercase equivalent, the character itself is returned.
* Note that by default CLDC only supports the ISO Latin-1 range of characters.
*/
public static char toUpperCase(char ch){
return ' '; //TODO codavaj!!
}
/**
* <p>
* Minimum value of a high surrogate or leading surrogate unit in UTF-16
* encoding - <code>'\uD800'</code>.
* </p>
*
* @since 1.5
*/
public static final char MIN_HIGH_SURROGATE = '\uD800';
/**
* <p>
* Maximum value of a high surrogate or leading surrogate unit in UTF-16
* encoding - <code>'\uDBFF'</code>.
* </p>
*
* @since 1.5
*/
public static final char MAX_HIGH_SURROGATE = '\uDBFF';
/**
* <p>
* Minimum value of a low surrogate or trailing surrogate unit in UTF-16
* encoding - <code>'\uDC00'</code>.
* </p>
*
* @since 1.5
*/
public static final char MIN_LOW_SURROGATE = '\uDC00';
/**
* Maximum value of a low surrogate or trailing surrogate unit in UTF-16
* encoding - <code>'\uDFFF'</code>.
* </p>
*
* @since 1.5
*/
public static final char MAX_LOW_SURROGATE = '\uDFFF';
/**
* <p>
* Minimum value of a surrogate unit in UTF-16 encoding - <code>'\uD800'</code>.
* </p>
*
* @since 1.5
*/
public static final char MIN_SURROGATE = '\uD800';
/**
* <p>
* Maximum value of a surrogate unit in UTF-16 encoding - <code>'\uDFFF'</code>.
* </p>
*
* @since 1.5
*/
public static final char MAX_SURROGATE = '\uDFFF';
/**
* <p>
* Minimum value of a supplementary code point - <code>U+010000</code>.
* </p>
*
* @since 1.5
*/
public static final int MIN_SUPPLEMENTARY_CODE_POINT = 0x10000;
/**
* <p>
* Minimum code point value - <code>U+0000</code>.
* </p>
*
* @since 1.5
*/
public static final int MIN_CODE_POINT = 0x000000;
/**
* <p>
* Maximum code point value - <code>U+10FFFF</code>.
* </p>
*
* @since 1.5
*/
public static final int MAX_CODE_POINT = 0x10FFFF;
/**
* <p>
* Constant for the number of bits to represent a <code>char</code> in
* two's compliment form.
* </p>
*
* @since 1.5
*/
public static final int SIZE = 16;
/**
* <p>
* A test for determining if the <code>codePoint</code> is a valid Unicode
* code point.
* </p>
*
* @param codePoint The code point to test.
* @return A boolean value.
* @since 1.5
*/
public static boolean isValidCodePoint(int codePoint) {
return (MIN_CODE_POINT <= codePoint && MAX_CODE_POINT >= codePoint);
}
/**
* <p>
* A test for determining if the <code>codePoint</code> is within the
* supplementary code point range.
* </p>
*
* @param codePoint The code point to test.
* @return A boolean value.
* @since 1.5
*/
public static boolean isSupplementaryCodePoint(int codePoint) {
return (MIN_SUPPLEMENTARY_CODE_POINT <= codePoint && MAX_CODE_POINT >= codePoint);
}
/**
* <p>
* A test for determining if the <code>char</code> is a high
* surrogate/leading surrogate unit that's used for representing
* supplementary characters in UTF-16 encoding.
* </p>
*
* @param ch The <code>char</code> unit to test.
* @return A boolean value.
* @since 1.5
* @see #isLowSurrogate(char)
*/
public static boolean isHighSurrogate(char ch) {
return (MIN_HIGH_SURROGATE <= ch && MAX_HIGH_SURROGATE >= ch);
}
/**
* <p>
* A test for determining if the <code>char</code> is a high
* surrogate/leading surrogate unit that's used for representing
* supplementary characters in UTF-16 encoding.
* </p>
*
* @param ch The <code>char</code> unit to test.
* @return A boolean value.
* @since 1.5
* @see #isHighSurrogate(char)
*/
public static boolean isLowSurrogate(char ch) {
return (MIN_LOW_SURROGATE <= ch && MAX_LOW_SURROGATE >= ch);
}
/**
* <p>
* A test for determining if the <code>char</code> pair is a valid
* surrogate pair.
* </p>
*
* @param high The high surrogate unit to test.
* @param low The low surrogate unit to test.
* @return A boolean value.
* @since 1.5
* @see #isHighSurrogate(char)
* @see #isLowSurrogate(char)
*/
public static boolean isSurrogatePair(char high, char low) {
return (isHighSurrogate(high) && isLowSurrogate(low));
}
/**
* <p>
* Calculates the number of <code>char</code> values required to represent
* the Unicode code point. This method only tests if the
* <code>codePoint</code> is greater than or equal to <code>0x10000</code>,
* in which case <code>2</code> is returned, otherwise <code>1</code>.
* To test if the code point is valid, use the
* {@link #isValidCodePoint(int)} method.
* </p>
*
* @param codePoint The code point to test.
* @return An <code>int</code> value of 2 or 1.
* @since 1.5
* @see #isValidCodePoint(int)
* @see #isSupplementaryCodePoint(int)
*/
public static int charCount(int codePoint) {
return (codePoint >= 0x10000 ? 2 : 1);
}
/**
* <p>
* Converts a surrogate pair into a Unicode code point. This method assume
* that the pair are valid surrogates. If the pair are NOT valid surrogates,
* then the result is indeterminate. The
* {@link #isSurrogatePair(char, char)} method should be used prior to this
* method to validate the pair.
* </p>
*
* @param high The high surrogate unit.
* @param low The low surrogate unit.
* @return The decoded code point.
* @since 1.5
* @see #isSurrogatePair(char, char)
*/
public static int toCodePoint(char high, char low) {
// See RFC 2781, Section 2.2
// http://www.faqs.org/rfcs/rfc2781.html
int h = (high & 0x3FF) << 10;
int l = low & 0x3FF;
return (h | l) + 0x10000;
}
/**
* <p>
* Returns the code point at the index in the <code>CharSequence</code>.
* If <code>char</code> unit at the index is a high-surrogate unit, the
* next index is less than the length of the sequence and the
* <code>char</code> unit at the next index is a low surrogate unit, then
* the code point represented by the pair is returned; otherwise the
* <code>char</code> unit at the index is returned.
* </p>
*
* @param seq The sequence of <code>char</code> units.
* @param index The index into the <code>seq</code> to retrieve and
* convert.
* @return The Unicode code point.
* @throws NullPointerException if <code>seq</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if the <code>index</code> is negative
* or greater than or equal to <code>seq.length()</code>.
* @since 1.5
*/
public static int codePointAt(CharSequence seq, int index) {
if (seq == null) {
throw new NullPointerException();
}
int len = seq.length();
if (index < 0 || index >= len) {
throw new IndexOutOfBoundsException();
}
char high = seq.charAt(index++);
if (index >= len) {
return high;
}
char low = seq.charAt(index);
if (isSurrogatePair(high, low)) {
return toCodePoint(high, low);
}
return high;
}
/**
* <p>
* Returns the code point at the index in the <code>char[]</code>. If
* <code>char</code> unit at the index is a high-surrogate unit, the next
* index is less than the length of the sequence and the <code>char</code>
* unit at the next index is a low surrogate unit, then the code point
* represented by the pair is returned; otherwise the <code>char</code>
* unit at the index is returned.
* </p>
*
* @param seq The sequence of <code>char</code> units.
* @param index The index into the <code>seq</code> to retrieve and
* convert.
* @return The Unicode code point.
* @throws NullPointerException if <code>seq</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if the <code>index</code> is negative
* or greater than or equal to <code>seq.length()</code>.
* @since 1.5
*/
public static int codePointAt(char[] seq, int index) {
if (seq == null) {
throw new NullPointerException();
}
int len = seq.length;
if (index < 0 || index >= len) {
throw new IndexOutOfBoundsException();
}
char high = seq[index++];
if (index >= len) {
return high;
}
char low = seq[index];
if (isSurrogatePair(high, low)) {
return toCodePoint(high, low);
}
return high;
}
/**
* <p>
* Returns the code point at the index in the <code>char[]</code> that's
* within the limit. If <code>char</code> unit at the index is a
* high-surrogate unit, the next index is less than the <code>limit</code>
* and the <code>char</code> unit at the next index is a low surrogate
* unit, then the code point represented by the pair is returned; otherwise
* the <code>char</code> unit at the index is returned.
* </p>
*
* @param seq The sequence of <code>char</code> units.
* @param index The index into the <code>seq</code> to retrieve and
* convert.
* @param limit The exclusive index into the <code>seq</code> that marks
* the end of the units that can be used.
* @return The Unicode code point.
* @throws NullPointerException if <code>seq</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if the <code>index</code> is
* negative, greater than or equal to <code>limit</code>,
* <code>limit</code> is negative or <code>limit</code> is
* greater than the length of <code>seq</code>.
* @since 1.5
*/
public static int codePointAt(char[] seq, int index, int limit) {
if (index < 0 || index >= limit || limit < 0 || limit > seq.length) {
throw new IndexOutOfBoundsException();
}
char high = seq[index++];
if (index >= limit) {
return high;
}
char low = seq[index];
if (isSurrogatePair(high, low)) {
return toCodePoint(high, low);
}
return high;
}
/**
* <p>
* Returns the Unicode code point that proceeds the <code>index</code> in
* the <code>CharSequence</code>. If the <code>char</code> unit at
* <code>index - 1</code> is within the low surrogate range, the value
* <code>index - 2</code> isn't negative and the <code>char</code> unit
* at <code>index - 2</code> is within the high surrogate range, then the
* supplementary code point made up of the surrogate pair is returned;
* otherwise, the <code>char</code> value at <code>index - 1</code> is
* returned.
* </p>
*
* @param seq The <code>CharSequence</code> to search.
* @param index The index into the <code>seq</code>.
* @return A Unicode code point.
* @throws NullPointerException if <code>seq</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if <code>index</code> is less than 1
* or greater than <code>seq.length()</code>.
* @since 1.5
*/
public static int codePointBefore(CharSequence seq, int index) {
if (seq == null) {
throw new NullPointerException();
}
int len = seq.length();
if (index < 1 || index > len) {
throw new IndexOutOfBoundsException();
}
char low = seq.charAt(--index);
if (--index < 0) {
return low;
}
char high = seq.charAt(index);
if (isSurrogatePair(high, low)) {
return toCodePoint(high, low);
}
return low;
}
/**
* <p>
* Returns the Unicode code point that proceeds the <code>index</code> in
* the <code>char[]</code>. If the <code>char</code> unit at
* <code>index - 1</code> is within the low surrogate range, the value
* <code>index - 2</code> isn't negative and the <code>char</code> unit
* at <code>index - 2</code> is within the high surrogate range, then the
* supplementary code point made up of the surrogate pair is returned;
* otherwise, the <code>char</code> value at <code>index - 1</code> is
* returned.
* </p>
*
* @param seq The <code>char[]</code> to search.
* @param index The index into the <code>seq</code>.
* @return A Unicode code point.
* @throws NullPointerException if <code>seq</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if <code>index</code> is less than 1
* or greater than <code>seq.length</code>.
* @since 1.5
*/
public static int codePointBefore(char[] seq, int index) {
if (seq == null) {
throw new NullPointerException();
}
int len = seq.length;
if (index < 1 || index > len) {
throw new IndexOutOfBoundsException();
}
char low = seq[--index];
if (--index < 0) {
return low;
}
char high = seq[index];
if (isSurrogatePair(high, low)) {
return toCodePoint(high, low);
}
return low;
}
/**
* <p>
* Returns the Unicode code point that proceeds the <code>index</code> in
* the <code>char[]</code> and isn't less than <code>start</code>. If
* the <code>char</code> unit at <code>index - 1</code> is within the
* low surrogate range, the value <code>index - 2</code> isn't less than
* <code>start</code> and the <code>char</code> unit at
* <code>index - 2</code> is within the high surrogate range, then the
* supplementary code point made up of the surrogate pair is returned;
* otherwise, the <code>char</code> value at <code>index - 1</code> is
* returned.
* </p>
*
* @param seq The <code>char[]</code> to search.
* @param index The index into the <code>seq</code>.
* @return A Unicode code point.
* @throws NullPointerException if <code>seq</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if <code>index</code> is less than or
* equal to <code>start</code>, <code>index</code> is greater
* than <code>seq.length</code>, <code>start</code> is not
* negative and <code>start</code> is greater than
* <code>seq.length</code>.
* @since 1.5
*/
public static int codePointBefore(char[] seq, int index, int start) {
if (seq == null) {
throw new NullPointerException();
}
int len = seq.length;
if (index <= start || index > len || start < 0 || start >= len) {
throw new IndexOutOfBoundsException();
}
char low = seq[--index];
if (--index < start) {
return low;
}
char high = seq[index];
if (isSurrogatePair(high, low)) {
return toCodePoint(high, low);
}
return low;
}
/**
* <p>
* Converts the Unicode code point, <code>codePoint</code>, into a UTF-16
* encoded sequence and copies the value(s) into the
* <code>char[]</code> <code>dst</code>, starting at the index
* <code>dstIndex</code>.
* </p>
*
* @param codePoint The Unicode code point to encode.
* @param dst The <code>char[]</code> to copy the encoded value into.
* @param dstIndex The index to start copying into <code>dst</code>.
* @return The number of <code>char</code> value units copied into
* <code>dst</code>.
* @throws IllegalArgumentException if <code>codePoint</code> is not a
* valid Unicode code point.
* @throws NullPointerException if <code>dst</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if <code>dstIndex</code> is negative,
* greater than or equal to <code>dst.length</code> or equals
* <code>dst.length - 1</code> when <code>codePoint</code> is a
* {@link #isSupplementaryCodePoint(int) supplementary code point}.
* @since 1.5
*/
public static int toChars(int codePoint, char[] dst, int dstIndex) {
if (!isValidCodePoint(codePoint)) {
throw new IllegalArgumentException();
}
if (dst == null) {
throw new NullPointerException();
}
if (dstIndex < 0 || dstIndex >= dst.length) {
throw new IndexOutOfBoundsException();
}
if (isSupplementaryCodePoint(codePoint)) {
if (dstIndex == dst.length - 1) {
throw new IndexOutOfBoundsException();
}
// See RFC 2781, Section 2.1
// http://www.faqs.org/rfcs/rfc2781.html
int cpPrime = codePoint - 0x10000;
int high = 0xD800 | ((cpPrime >> 10) & 0x3FF);
int low = 0xDC00 | (cpPrime & 0x3FF);
dst[dstIndex] = (char) high;
dst[dstIndex + 1] = (char) low;
return 2;
}
dst[dstIndex] = (char) codePoint;
return 1;
}
/**
* <p>
* Converts the Unicode code point, <code>codePoint</code>, into a UTF-16
* encoded sequence that is returned as a <code>char[]</code>.
* </p>
*
* @param codePoint The Unicode code point to encode.
* @return The UTF-16 encoded <code>char</code> sequence; if code point is
* a {@link #isSupplementaryCodePoint(int) supplementary code point},
* then a 2 <code>char</code> array is returned, otherwise a 1
* <code>char</code> array is returned.
* @throws IllegalArgumentException if <code>codePoint</code> is not a
* valid Unicode code point.
* @since 1.5
*/
public static char[] toChars(int codePoint) {
if (!isValidCodePoint(codePoint)) {
throw new IllegalArgumentException();
}
if (isSupplementaryCodePoint(codePoint)) {
int cpPrime = codePoint - 0x10000;
int high = 0xD800 | ((cpPrime >> 10) & 0x3FF);
int low = 0xDC00 | (cpPrime & 0x3FF);
return new char[] { (char) high, (char) low };
}
return new char[] { (char) codePoint };
}
/**
* <p>
* Counts the number of Unicode code points in the subsequence of the
* <code>CharSequence</code>, as delineated by the
* <code>beginIndex</code> and <code>endIndex</code>. Any surrogate
* values with missing pair values will be counted as 1 code point.
* </p>
*
* @param seq The <code>CharSequence</code> to look through.
* @param beginIndex The inclusive index to begin counting at.
* @param endIndex The exclusive index to stop counting at.
* @return The number of Unicode code points.
* @throws NullPointerException if <code>seq</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if <code>beginIndex</code> is
* negative, greater than <code>seq.length()</code> or greater
* than <code>endIndex</code>.
* @since 1.5
*/
public static int codePointCount(CharSequence seq, int beginIndex,
int endIndex) {
if (seq == null) {
throw new NullPointerException();
}
int len = seq.length();
if (beginIndex < 0 || endIndex > len || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
int result = 0;
for (int i = beginIndex; i < endIndex; i++) {
char c = seq.charAt(i);
if (isHighSurrogate(c)) {
if (++i < endIndex) {
c = seq.charAt(i);
if (!isLowSurrogate(c)) {
result++;
}
}
}
result++;
}
return result;
}
/**
* <p>
* Counts the number of Unicode code points in the subsequence of the
* <code>char[]</code>, as delineated by the <code>offset</code> and
* <code>count</code>. Any surrogate values with missing pair values will
* be counted as 1 code point.
* </p>
*
* @param seq The <code>char[]</code> to look through.
* @param offset The inclusive index to begin counting at.
* @param count The number of <code>char</code> values to look through in
* <code>seq</code>.
* @return The number of Unicode code points.
* @throws NullPointerException if <code>seq</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if <code>offset</code> or
* <code>count</code> is negative or if <code>endIndex</code> is
* greater than <code>seq.length</code>.
* @since 1.5
*/
public static int codePointCount(char[] seq, int offset, int count) {
if (seq == null) {
throw new NullPointerException();
}
int len = seq.length;
int endIndex = offset + count;
if (offset < 0 || count < 0 || endIndex > len) {
throw new IndexOutOfBoundsException();
}
int result = 0;
for (int i = offset; i < endIndex; i++) {
char c = seq[i];
if (isHighSurrogate(c)) {
if (++i < endIndex) {
c = seq[i];
if (!isLowSurrogate(c)) {
result++;
}
}
}
result++;
}
return result;
}
/**
* <p>
* Determines the index into the <code>CharSequence</code> that is offset
* (measured in code points and specified by <code>codePointOffset</code>),
* from the <code>index</code> argument.
* </p>
*
* @param seq The <code>CharSequence</code> to find the index within.
* @param index The index to begin from, within the
* <code>CharSequence</code>.
* @param codePointOffset The number of code points to look back or
* forwards; may be a negative or positive value.
* @return The calculated index that is <code>codePointOffset</code> code
* points from <code>index</code>.
* @throws NullPointerException if <code>seq</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if <code>index</code> is negative,
* greater than <code>seq.length()</code>, there aren't enough
* values in <code>seq</code> after <code>index</code> or before
* <code>index</code> if <code>codePointOffset</code> is
* negative.
* @since 1.5
*/
public static int offsetByCodePoints(CharSequence seq, int index,
int codePointOffset) {
if (seq == null) {
throw new NullPointerException();
}
int len = seq.length();
if (index < 0 || index > len) {
throw new IndexOutOfBoundsException();
}
if (codePointOffset == 0) {
return index;
}
if (codePointOffset > 0) {
int codePoints = codePointOffset;
int i = index;
while (codePoints > 0) {
codePoints--;
if (i >= len) {
throw new IndexOutOfBoundsException();
}
if (isHighSurrogate(seq.charAt(i))) {
int next = i + 1;
if (next < len && isLowSurrogate(seq.charAt(next))) {
i++;
}
}
i++;
}
return i;
}
int codePoints = -codePointOffset;
int i = index;
while (codePoints > 0) {
codePoints--;
i--;
if (i < 0) {
throw new IndexOutOfBoundsException();
}
if (isLowSurrogate(seq.charAt(i))) {
int prev = i - 1;
if (prev >= 0 && isHighSurrogate(seq.charAt(prev))) {
i--;
}
}
}
return i;
}
/**
* <p>
* Determines the index into the <code>char[]</code> that is offset
* (measured in code points and specified by <code>codePointOffset</code>),
* from the <code>index</code> argument and is within the subsequence as
* delineated by <code>start</code> and <code>count</code>.
* </p>
*
* @param seq The <code>char[]</code> to find the index within.
*
* @param index The index to begin from, within the <code>char[]</code>.
* @param codePointOffset The number of code points to look back or
* forwards; may be a negative or positive value.
* @param start The inclusive index that marks the beginning of the
* subsequence.
* @param count The number of <code>char</code> values to include within
* the subsequence.
* @return The calculated index that is <code>codePointOffset</code> code
* points from <code>index</code>.
* @throws NullPointerException if <code>seq</code> is <code>null</code>.
* @throws IndexOutOfBoundsException if <code>start</code> or
* <code>count</code> is negative, <code>start + count</code>
* greater than <code>seq.length</code>, <code>index</code> is
* less than <code>start</code>, <code>index</code> is greater
* than <code>start + count</code> or there aren't enough values
* in <code>seq</code> after <code>index</code> or before
* <code>index</code> if <code>codePointOffset</code> is
* negative.
* @since 1.5
*/
public static int offsetByCodePoints(char[] seq, int start, int count,
int index, int codePointOffset) {
if (seq == null) {
throw new NullPointerException();
}
int end = start + count;
if (start < 0 || count < 0 || end > seq.length || index < start
|| index > end) {
throw new IndexOutOfBoundsException();
}
if (codePointOffset == 0) {
return index;
}
if (codePointOffset > 0) {
int codePoints = codePointOffset;
int i = index;
while (codePoints > 0) {
codePoints--;
if (i >= end) {
throw new IndexOutOfBoundsException();
}
if (isHighSurrogate(seq[i])) {
int next = i + 1;
if (next < end && isLowSurrogate(seq[next])) {
i++;
}
}
i++;
}
return i;
}
int codePoints = -codePointOffset;
int i = index;
while (codePoints > 0) {
codePoints--;
i--;
if (i < start) {
throw new IndexOutOfBoundsException();
}
if (isLowSurrogate(seq[i])) {
int prev = i - 1;
if (prev >= start && isHighSurrogate(seq[prev])) {
i--;
}
}
}
return i;
}
/**
* Reverse the order of the first and second bytes in character
* @param c
* the character
* @return the character with reordered bytes.
*/
public static char reverseBytes(char c) {
return (char)((c<<8) | (c>>8));
}
/**
* Returns the object instance of i
* @param i the primitive
* @return object instance
*/
public static Character valueOf(char i) {
return null;
}
/**
* See {@link #isWhitespace(int)}.
*/
public static boolean isWhitespace(char c) {
return isWhitespace((int) c);
}
public static boolean isSpace(char ch) {
switch (ch) {
case '\t':
case '\n':
case '\f':
case '\r':
case ' ':
return true;
default:
return false;
}
}
public static boolean isSpaceChar(char ch) {
return false;
}
/**
* Returns true if the given code point is a Unicode whitespace character.
* The exact set of characters considered as whitespace varies with Unicode version.
* Note that non-breaking spaces are not considered whitespace.
* Note also that line separators are considered whitespace; see {@link #isSpaceChar}
* for an alternative.
*/
public static boolean isWhitespace(int codePoint) {
// We don't just call into icu4c because of the JNI overhead. Ideally we'd fix that.
// Any ASCII whitespace character?