1688 |
|
|
1689 |
/** |
/** |
1690 |
* The minimum Unicode 4.0 code point. This value is <code>0</code>. |
* The minimum Unicode 4.0 code point. This value is <code>0</code>. |
1691 |
|
* @since 1.5 |
1692 |
*/ |
*/ |
1693 |
public static final int MIN_CODE_POINT = 0; |
public static final int MIN_CODE_POINT = 0; |
1694 |
|
|
1696 |
* The maximum Unicode 4.0 code point, which is greater than the range |
* The maximum Unicode 4.0 code point, which is greater than the range |
1697 |
* of the char data type. |
* of the char data type. |
1698 |
* This value is <code>0x10FFFF</code>. |
* This value is <code>0x10FFFF</code>. |
1699 |
|
* @since 1.5 |
1700 |
*/ |
*/ |
1701 |
public static final int MAX_CODE_POINT = 0x10FFFF; |
public static final int MAX_CODE_POINT = 0x10FFFF; |
1702 |
|
|
1704 |
* The minimum Unicode high surrogate code unit, or |
* The minimum Unicode high surrogate code unit, or |
1705 |
* <emph>leading-surrogate</emph>, in the UTF-16 character encoding. |
* <emph>leading-surrogate</emph>, in the UTF-16 character encoding. |
1706 |
* This value is <code>'\uD800'</code>. |
* This value is <code>'\uD800'</code>. |
1707 |
|
* @since 1.5 |
1708 |
*/ |
*/ |
1709 |
public static final char MIN_HIGH_SURROGATE = '\uD800'; |
public static final char MIN_HIGH_SURROGATE = '\uD800'; |
1710 |
|
|
1712 |
* The maximum Unicode high surrogate code unit, or |
* The maximum Unicode high surrogate code unit, or |
1713 |
* <emph>leading-surrogate</emph>, in the UTF-16 character encoding. |
* <emph>leading-surrogate</emph>, in the UTF-16 character encoding. |
1714 |
* This value is <code>'\uDBFF'</code>. |
* This value is <code>'\uDBFF'</code>. |
1715 |
|
* @since 1.5 |
1716 |
*/ |
*/ |
1717 |
public static final char MAX_HIGH_SURROGATE = '\uDBFF'; |
public static final char MAX_HIGH_SURROGATE = '\uDBFF'; |
1718 |
|
|
1720 |
* The minimum Unicode low surrogate code unit, or |
* The minimum Unicode low surrogate code unit, or |
1721 |
* <emph>trailing-surrogate</emph>, in the UTF-16 character encoding. |
* <emph>trailing-surrogate</emph>, in the UTF-16 character encoding. |
1722 |
* This value is <code>'\uDC00'</code>. |
* This value is <code>'\uDC00'</code>. |
1723 |
|
* @since 1.5 |
1724 |
*/ |
*/ |
1725 |
public static final char MIN_LOW_SURROGATE = '\uDC00'; |
public static final char MIN_LOW_SURROGATE = '\uDC00'; |
1726 |
|
|
1728 |
* The maximum Unicode low surrogate code unit, or |
* The maximum Unicode low surrogate code unit, or |
1729 |
* <emph>trailing-surrogate</emph>, in the UTF-16 character encoding. |
* <emph>trailing-surrogate</emph>, in the UTF-16 character encoding. |
1730 |
* This value is <code>'\uDFFF'</code>. |
* This value is <code>'\uDFFF'</code>. |
1731 |
|
* @since 1.5 |
1732 |
*/ |
*/ |
1733 |
public static final char MAX_LOW_SURROGATE = '\uDFFF'; |
public static final char MAX_LOW_SURROGATE = '\uDFFF'; |
1734 |
|
|
1735 |
/** |
/** |
1736 |
* The minimum Unicode surrogate code unit in the UTF-16 character encoding. |
* The minimum Unicode surrogate code unit in the UTF-16 character encoding. |
1737 |
* This value is <code>'\uD800'</code>. |
* This value is <code>'\uD800'</code>. |
1738 |
|
* @since 1.5 |
1739 |
*/ |
*/ |
1740 |
public static final char MIN_SURROGATE = '\uD800'; |
public static final char MIN_SURROGATE = MIN_HIGH_SURROGATE; |
1741 |
|
|
1742 |
/** |
/** |
1743 |
* The maximum Unicode surrogate code unit in the UTF-16 character encoding. |
* The maximum Unicode surrogate code unit in the UTF-16 character encoding. |
1744 |
* This value is <code>'\uDFFF'</code>. |
* This value is <code>'\uDFFF'</code>. |
1745 |
|
* @since 1.5 |
1746 |
*/ |
*/ |
1747 |
public static final char MAX_SURROGATE = '\uDFFF'; |
public static final char MAX_SURROGATE = MAX_LOW_SURROGATE; |
1748 |
|
|
1749 |
/** |
/** |
1750 |
* The lowest possible supplementary Unicode code point (the first code |
* The lowest possible supplementary Unicode code point (the first code |
2980 |
* |
* |
2981 |
* @param val the value to wrap |
* @param val the value to wrap |
2982 |
* @return the <code>Character</code> |
* @return the <code>Character</code> |
2983 |
|
* @since 1.5 |
2984 |
*/ |
*/ |
2985 |
public static Character valueOf(char val) |
public static Character valueOf(char val) |
2986 |
{ |
{ |
3116 |
{ |
{ |
3117 |
return codePoint >= MIN_CODE_POINT && codePoint <= MAX_CODE_POINT; |
return codePoint >= MIN_CODE_POINT && codePoint <= MAX_CODE_POINT; |
3118 |
} |
} |
3119 |
|
|
3120 |
|
/** |
3121 |
|
* Return true if the given character is a high surrogate. |
3122 |
|
* @param ch the character |
3123 |
|
* @return true if the character is a high surrogate character |
3124 |
|
* |
3125 |
|
* @since 1.5 |
3126 |
|
*/ |
3127 |
|
public static boolean isHighSurrogate(char ch) |
3128 |
|
{ |
3129 |
|
return ch >= MIN_HIGH_SURROGATE && ch <= MAX_HIGH_SURROGATE; |
3130 |
|
} |
3131 |
|
|
3132 |
|
/** |
3133 |
|
* Return true if the given character is a low surrogate. |
3134 |
|
* @param ch the character |
3135 |
|
* @return true if the character is a low surrogate character |
3136 |
|
* |
3137 |
|
* @since 1.5 |
3138 |
|
*/ |
3139 |
|
public static boolean isLowSurrogate(char ch) |
3140 |
|
{ |
3141 |
|
return ch >= MIN_LOW_SURROGATE && ch <= MAX_LOW_SURROGATE; |
3142 |
|
} |
3143 |
|
|
3144 |
|
/** |
3145 |
|
* Return true if the given characters compose a surrogate pair. |
3146 |
|
* This is true if the first character is a high surrogate and the |
3147 |
|
* second character is a low surrogate. |
3148 |
|
* @param ch1 the first character |
3149 |
|
* @param ch2 the first character |
3150 |
|
* @return true if the characters compose a surrogate pair |
3151 |
|
* |
3152 |
|
* @since 1.5 |
3153 |
|
*/ |
3154 |
|
public static boolean isSurrogatePair(char ch1, char ch2) |
3155 |
|
{ |
3156 |
|
return isHighSurrogate(ch1) && isLowSurrogate(ch2); |
3157 |
|
} |
3158 |
|
|
3159 |
|
/** |
3160 |
|
* Given a valid surrogate pair, this returns the corresponding |
3161 |
|
* code point. |
3162 |
|
* @param high the high character of the pair |
3163 |
|
* @param low the low character of the pair |
3164 |
|
* @return the corresponding code point |
3165 |
|
* |
3166 |
|
* @since 1.5 |
3167 |
|
*/ |
3168 |
|
public static int toCodePoint(char high, char low) |
3169 |
|
{ |
3170 |
|
return ((high - MIN_HIGH_SURROGATE) << 10) + (low - MIN_LOW_SURROGATE); |
3171 |
|
} |
3172 |
|
|
3173 |
|
/** |
3174 |
|
* Get the code point at the specified index in the CharSequence. |
3175 |
|
* This is like CharSequence#charAt(int), but if the character is |
3176 |
|
* the start of a surrogate pair, and there is a following |
3177 |
|
* character, and this character completes the pair, then the |
3178 |
|
* corresponding supplementary code point is returned. Otherwise, |
3179 |
|
* the character at the index is returned. |
3180 |
|
* |
3181 |
|
* @param sequence the CharSequence |
3182 |
|
* @param index the index of the codepoint to get, starting at 0 |
3183 |
|
* @return the codepoint at the specified index |
3184 |
|
* @throws IndexOutOfBoundsException if index is negative or >= length() |
3185 |
|
* @since 1.5 |
3186 |
|
*/ |
3187 |
|
public static int codePointAt(CharSequence sequence, int index) |
3188 |
|
{ |
3189 |
|
int len = sequence.length(); |
3190 |
|
if (index < 0 || index >= len) |
3191 |
|
throw new IndexOutOfBoundsException(); |
3192 |
|
char high = sequence.charAt(index); |
3193 |
|
if (! isHighSurrogate(high) || ++index >= len) |
3194 |
|
return high; |
3195 |
|
char low = sequence.charAt(index); |
3196 |
|
if (! isLowSurrogate(low)) |
3197 |
|
return high; |
3198 |
|
return toCodePoint(high, low); |
3199 |
|
} |
3200 |
|
|
3201 |
|
/** |
3202 |
|
* Get the code point at the specified index in the CharSequence. |
3203 |
|
* If the character is the start of a surrogate pair, and there is a |
3204 |
|
* following character, and this character completes the pair, then |
3205 |
|
* the corresponding supplementary code point is returned. |
3206 |
|
* Otherwise, the character at the index is returned. |
3207 |
|
* |
3208 |
|
* @param chars the character array in which to look |
3209 |
|
* @param index the index of the codepoint to get, starting at 0 |
3210 |
|
* @return the codepoint at the specified index |
3211 |
|
* @throws IndexOutOfBoundsException if index is negative or >= length() |
3212 |
|
* @since 1.5 |
3213 |
|
*/ |
3214 |
|
public static int codePointAt(char[] chars, int index) |
3215 |
|
{ |
3216 |
|
return codePointAt(chars, index, chars.length); |
3217 |
|
} |
3218 |
|
|
3219 |
|
/** |
3220 |
|
* Get the code point at the specified index in the CharSequence. |
3221 |
|
* If the character is the start of a surrogate pair, and there is a |
3222 |
|
* following character within the specified range, and this |
3223 |
|
* character completes the pair, then the corresponding |
3224 |
|
* supplementary code point is returned. Otherwise, the character |
3225 |
|
* at the index is returned. |
3226 |
|
* |
3227 |
|
* @param chars the character array in which to look |
3228 |
|
* @param index the index of the codepoint to get, starting at 0 |
3229 |
|
* @param limit the limit past which characters should not be examined |
3230 |
|
* @return the codepoint at the specified index |
3231 |
|
* @throws IndexOutOfBoundsException if index is negative or >= |
3232 |
|
* limit, or if limit is negative or >= the length of the array |
3233 |
|
* @since 1.5 |
3234 |
|
*/ |
3235 |
|
public static int codePointAt(char[] chars, int index, int limit) |
3236 |
|
{ |
3237 |
|
if (index < 0 || index >= limit || limit < 0 || limit >= chars.length) |
3238 |
|
throw new IndexOutOfBoundsException(); |
3239 |
|
char high = chars[index]; |
3240 |
|
if (! isHighSurrogate(high) || ++index >= limit) |
3241 |
|
return high; |
3242 |
|
char low = chars[index]; |
3243 |
|
if (! isLowSurrogate(low)) |
3244 |
|
return high; |
3245 |
|
return toCodePoint(high, low); |
3246 |
|
} |
3247 |
|
|
3248 |
|
/** |
3249 |
|
* Get the code point before the specified index. This is like |
3250 |
|
* #codePointAt(char[], int), but checks the characters at |
3251 |
|
* <code>index-1</code> and <code>index-2</code> to see if they form |
3252 |
|
* a supplementary code point. If they do not, the character at |
3253 |
|
* <code>index-1</code> is returned. |
3254 |
|
* |
3255 |
|
* @param chars the character array |
3256 |
|
* @param index the index just past the codepoint to get, starting at 0 |
3257 |
|
* @return the codepoint at the specified index |
3258 |
|
* @throws IndexOutOfBoundsException if index is negative or >= length() |
3259 |
|
* @since 1.5 |
3260 |
|
*/ |
3261 |
|
public static int codePointBefore(char[] chars, int index) |
3262 |
|
{ |
3263 |
|
return codePointBefore(chars, index, 1); |
3264 |
|
} |
3265 |
|
|
3266 |
|
/** |
3267 |
|
* Get the code point before the specified index. This is like |
3268 |
|
* #codePointAt(char[], int), but checks the characters at |
3269 |
|
* <code>index-1</code> and <code>index-2</code> to see if they form |
3270 |
|
* a supplementary code point. If they do not, the character at |
3271 |
|
* <code>index-1</code> is returned. The start parameter is used to |
3272 |
|
* limit the range of the array which may be examined. |
3273 |
|
* |
3274 |
|
* @param chars the character array |
3275 |
|
* @param index the index just past the codepoint to get, starting at 0 |
3276 |
|
* @param start the index before which characters should not be examined |
3277 |
|
* @return the codepoint at the specified index |
3278 |
|
* @throws IndexOutOfBoundsException if index is > start or > |
3279 |
|
* the length of the array, or if limit is negative or >= the |
3280 |
|
* length of the array |
3281 |
|
* @since 1.5 |
3282 |
|
*/ |
3283 |
|
public static int codePointBefore(char[] chars, int index, int start) |
3284 |
|
{ |
3285 |
|
if (index < start || index > chars.length |
3286 |
|
|| start < 0 || start >= chars.length) |
3287 |
|
throw new IndexOutOfBoundsException(); |
3288 |
|
--index; |
3289 |
|
char low = chars[index]; |
3290 |
|
if (! isLowSurrogate(low) || --index < start) |
3291 |
|
return low; |
3292 |
|
char high = chars[index]; |
3293 |
|
if (! isHighSurrogate(high)) |
3294 |
|
return low; |
3295 |
|
return toCodePoint(high, low); |
3296 |
|
} |
3297 |
|
|
3298 |
|
/** |
3299 |
|
* Get the code point before the specified index. This is like |
3300 |
|
* #codePointAt(CharSequence, int), but checks the characters at |
3301 |
|
* <code>index-1</code> and <code>index-2</code> to see if they form |
3302 |
|
* a supplementary code point. If they do not, the character at |
3303 |
|
* <code>index-1</code> is returned. |
3304 |
|
* |
3305 |
|
* @param sequence the CharSequence |
3306 |
|
* @param index the index just past the codepoint to get, starting at 0 |
3307 |
|
* @return the codepoint at the specified index |
3308 |
|
* @throws IndexOutOfBoundsException if index is negative or >= length() |
3309 |
|
* @since 1.5 |
3310 |
|
*/ |
3311 |
|
public static int codePointBefore(CharSequence sequence, int index) |
3312 |
|
{ |
3313 |
|
int len = sequence.length(); |
3314 |
|
if (index < 1 || index > len) |
3315 |
|
throw new IndexOutOfBoundsException(); |
3316 |
|
--index; |
3317 |
|
char low = sequence.charAt(index); |
3318 |
|
if (! isLowSurrogate(low) || --index < 0) |
3319 |
|
return low; |
3320 |
|
char high = sequence.charAt(index); |
3321 |
|
if (! isHighSurrogate(high)) |
3322 |
|
return low; |
3323 |
|
return toCodePoint(high, low); |
3324 |
|
} |
3325 |
} // class Character |
} // class Character |