Standalone utility class providing UTF16 character conversions and indexing conversions.
Code that uses strings alone rarely need modification.
By design, UTF-16 does not allow overlap, so searching for strings is a safe
operation. Similarly, concatenation is always safe. Substringing is safe if
the start and end are both on UTF-32 boundaries. In normal code, the values
for start and end are on those boundaries, since they arose from operations
like searching. If not, the nearest UTF-32 boundaries can be determined
using bounds().
Examples:
The following examples illustrate use of some of these methods.
// iteration forwards: Original
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
doSomethingWith(ch);
}
// iteration forwards: Changes for UTF-32
int ch;
for (int i = 0; i < s.length(); i += UTF16.getCharCount(ch)) {
ch = UTF16.charAt(s, i);
doSomethingWith(ch);
}
// iteration backwards: Original
for (int i = s.length() - 1; i >= 0; --i) {
char ch = s.charAt(i);
doSomethingWith(ch);
}
// iteration backwards: Changes for UTF-32
int ch;
for (int i = s.length() - 1; i > 0; i -= UTF16.getCharCount(ch)) {
ch = UTF16.charAt(s, i);
doSomethingWith(ch);
}
Notes:
Lead and Trail in the API, which gives a better
sense of their ordering in a string. offset16 and
offset32 are used to distinguish offsets to UTF-16
boundaries vs offsets to UTF-32 boundaries. int char32 is
used to contain UTF-32 characters, as opposed to char16,
which is a UTF-16 code unit.
bounds(string, offset16) != TRAIL.
UCharacter.isLegal() can be used to check
for validity if desired.
| Modifier and Type | Field and Description |
|---|---|
| public static final int | CODEPOINT_MAX_VALUE
The highest Unicode code point value (scalar value) according to the Unicode Standard. |
| public static final int | CODEPOINT_MIN_VALUE
The lowest Unicode code point value. |
| private static final int | LEAD_SURROGATE_BITMASK
Lead surrogate bitmask |
| private static final int | LEAD_SURROGATE_BITS
Lead surrogate bits |
| public static final int | LEAD_SURROGATE_MAX_VALUE
Lead surrogate maximum value |
| public static final int | LEAD_SURROGATE_MIN_VALUE
Lead surrogate minimum value |
| private static final int | LEAD_SURROGATE_OFFSET_
Value that all lead surrogate starts with |
| private static final int | LEAD_SURROGATE_SHIFT_
Shift value for lead surrogate to form a supplementary character. |
| public static final int | SUPPLEMENTARY_MIN_VALUE
The minimum value for Supplementary code points |
| private static final int | SURROGATE_BITMASK
Surrogate bitmask |
| private static final int | SURROGATE_BITS
Surrogate bits |
| public static final int | SURROGATE_MIN_VALUE
Surrogate minimum value |
| private static final int | TRAIL_SURROGATE_BITMASK
Trail surrogate bitmask |
| private static final int | TRAIL_SURROGATE_BITS
Trail surrogate bits |
| private static final int | TRAIL_SURROGATE_MASK_
Mask to retrieve the significant value from a trail surrogate. |
| public static final int | TRAIL_SURROGATE_MAX_VALUE
Trail surrogate maximum value |
| public static final int | TRAIL_SURROGATE_MIN_VALUE
Trail surrogate minimum value |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static int | |
| private static int | |
| public static StringBuffer | Returns: the updated StringBufferthe buffer to append to target, int value to append. char32)Append a single UTF-32 value to the end of a StringBuffer. |
| public static int | |
| public static int | Returns: UTF-32 value for the UTF-32 value that contains the char at offset16. The boundaries of that codepoint are the same as inbounds32().array of UTF-16 chars source, int UTF-16 offset to the start of the character. offset16)Extract a single UTF-32 value from a string. |
| public static int | Returns: UTF-32 value for the UTF-32 value that contains the char at offset16. The boundaries of that codepoint are the same as inbounds32().Array of UTF-16 chars source, int Offset to substring in the source array for analyzing start, int Offset to substring in the source array for analyzing limit, int UTF-16 offset relative to start offset16)Extract a single UTF-32 value from a substring. |
| public static int | Returns: 2 if is in supplementary space, otherwise 1.the input codepoint. char32)Determines how many chars this char32 requires. |
| public static char | Returns: lead surrogate if the getCharCount(ch) is 2;and 0 otherwise (note: 0 is not a valid lead surrogate). the input character. char32)Returns the lead surrogate. |
| public static char | Returns: the trail surrogate if the getCharCount(ch) is 2;otherwise the character itself the input character. char32)Returns the trail surrogate. |
| public static boolean | Returns: true If the input code point is a lead surrogateThe input character.
(In ICU 2.1-69 the type of this parameter was codePoint)char.)Determines whether the code point is a lead surrogate. |
| public static boolean | Returns: true If the input code point is a surrogate.The input character.
(In ICU 2.1-69 the type of this parameter was codePoint)char.)Determines whether the code point is a surrogate. |
| public static boolean | Returns: true If the input code point is a trail surrogate.The input character.
(In ICU 2.1-69 the type of this parameter was codePoint)char.)Determines whether the code point is a trail surrogate. |
| public static int | Returns: new shifted offset16 relative to startchar array source, int position of the subarray to be performed on start, int position of the subarray to be performed on limit, int UTF16 position to shift relative to start offset16, int number of codepoints to shift shift32)Shifts offset16 by the argument number of codepoints within a subarray. |
| private static String | Returns: string representation of the code pointcode point ch)Converts argument code point and returns a String object representing the code point's value in UTF16 format. |
| public static String | Returns: string value of char32 in UTF16 formatthe input character. char32)Convenience method corresponding to String.valueOf(char). |
| CODEPOINT_MAX_VALUE | back to summary |
|---|---|
| public static final int CODEPOINT_MAX_VALUE The highest Unicode code point value (scalar value) according to the Unicode Standard.
| |
| CODEPOINT_MIN_VALUE | back to summary |
|---|---|
| public static final int CODEPOINT_MIN_VALUE The lowest Unicode code point value.
| |
| LEAD_SURROGATE_BITMASK | back to summary |
|---|---|
| private static final int LEAD_SURROGATE_BITMASK Lead surrogate bitmask | |
| LEAD_SURROGATE_BITS | back to summary |
|---|---|
| private static final int LEAD_SURROGATE_BITS Lead surrogate bits | |
| LEAD_SURROGATE_MAX_VALUE | back to summary |
|---|---|
| public static final int LEAD_SURROGATE_MAX_VALUE Lead surrogate maximum value
| |
| LEAD_SURROGATE_MIN_VALUE | back to summary |
|---|---|
| public static final int LEAD_SURROGATE_MIN_VALUE Lead surrogate minimum value
| |
| LEAD_SURROGATE_OFFSET_ | back to summary |
|---|---|
| private static final int LEAD_SURROGATE_OFFSET_ Value that all lead surrogate starts with | |
| LEAD_SURROGATE_SHIFT_ | back to summary |
|---|---|
| private static final int LEAD_SURROGATE_SHIFT_ Shift value for lead surrogate to form a supplementary character. | |
| SUPPLEMENTARY_MIN_VALUE | back to summary |
|---|---|
| public static final int SUPPLEMENTARY_MIN_VALUE The minimum value for Supplementary code points
| |
| SURROGATE_BITMASK | back to summary |
|---|---|
| private static final int SURROGATE_BITMASK Surrogate bitmask | |
| SURROGATE_BITS | back to summary |
|---|---|
| private static final int SURROGATE_BITS Surrogate bits | |
| SURROGATE_MIN_VALUE | back to summary |
|---|---|
| public static final int SURROGATE_MIN_VALUE Surrogate minimum value
| |
| TRAIL_SURROGATE_BITMASK | back to summary |
|---|---|
| private static final int TRAIL_SURROGATE_BITMASK Trail surrogate bitmask | |
| TRAIL_SURROGATE_BITS | back to summary |
|---|---|
| private static final int TRAIL_SURROGATE_BITS Trail surrogate bits | |
| TRAIL_SURROGATE_MASK_ | back to summary |
|---|---|
| private static final int TRAIL_SURROGATE_MASK_ Mask to retrieve the significant value from a trail surrogate. | |
| TRAIL_SURROGATE_MAX_VALUE | back to summary |
|---|---|
| public static final int TRAIL_SURROGATE_MAX_VALUE Trail surrogate maximum value
| |
| TRAIL_SURROGATE_MIN_VALUE | back to summary |
|---|---|
| public static final int TRAIL_SURROGATE_MIN_VALUE Trail surrogate minimum value
| |
| UTF16 | back to summary |
|---|---|
| private UTF16() Prevent instance from being created. | |
| _charAt | back to summary |
|---|---|
| private static int _charAt(String source, int offset16, char single) | |
| _charAt | back to summary |
|---|---|
| private static int _charAt(CharSequence source, int offset16, char single) | |
| append | back to summary |
|---|---|
| public static StringBuffer append(StringBuffer target, int char32) Append a single UTF-32 value to the end of a StringBuffer.
If a validity check is required, use
| |
| charAt | back to summary |
|---|---|
| public static int charAt(String source, int offset16) Extract a single UTF-32 value from a string.
Used when iterating forwards or backwards (with
| |
| charAt | back to summary |
|---|---|
| public static int charAt(CharSequence source, int offset16) Extract a single UTF-32 value from a string.
Used when iterating forwards or backwards (with
| |
| charAt | back to summary |
|---|---|
| public static int charAt(char[] source, int start, int limit, int offset16) Extract a single UTF-32 value from a substring. Used when iterating forwards or backwards
(with
| |
| getCharCount | back to summary |
|---|---|
| public static int getCharCount(int char32) Determines how many chars this char32 requires.
If a validity check is required, use
| |
| getLeadSurrogate | back to summary |
|---|---|
| public static char getLeadSurrogate(int char32) Returns the lead surrogate.
If a validity check is required, use
| |
| getTrailSurrogate | back to summary |
|---|---|
| public static char getTrailSurrogate(int char32) Returns the trail surrogate.
If a validity check is required, use
| |
| isLeadSurrogate | back to summary |
|---|---|
| public static boolean isLeadSurrogate(int codePoint) Determines whether the code point is a lead surrogate.
| |
| isSurrogate | back to summary |
|---|---|
| public static boolean isSurrogate(int codePoint) Determines whether the code point is a surrogate.
| |
| isTrailSurrogate | back to summary |
|---|---|
| public static boolean isTrailSurrogate(int codePoint) Determines whether the code point is a trail surrogate.
| |
| moveCodePointOffset | back to summary |
|---|---|
| public static int moveCodePointOffset(char[] source, int start, int limit, int offset16, int shift32) Shifts offset16 by the argument number of codepoints within a subarray.
| |
| toString | back to summary |
|---|---|
| private static String toString(int ch) Converts argument code point and returns a String object representing the code point's value in UTF16 format. This method does not check for the validity of the codepoint, the results are not guaranteed if a invalid codepoint is passed as argument. The result is a string whose length is 1 for non-supplementary code points, 2 otherwise.
| |
| valueOf | back to summary |
|---|---|
| public static String valueOf(int char32) Convenience method corresponding to String.valueOf(char). Returns a one
or two char string containing the UTF-32 value in UTF16 format. If a
validity check is required, use
| |