jaroslav@68: /* jaroslav@68: * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. jaroslav@68: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@68: * jaroslav@68: * This code is free software; you can redistribute it and/or modify it jaroslav@68: * under the terms of the GNU General Public License version 2 only, as jaroslav@68: * published by the Free Software Foundation. Oracle designates this jaroslav@68: * particular file as subject to the "Classpath" exception as provided jaroslav@68: * by Oracle in the LICENSE file that accompanied this code. jaroslav@68: * jaroslav@68: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@68: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@68: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@68: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@68: * accompanied this code). jaroslav@68: * jaroslav@68: * You should have received a copy of the GNU General Public License version jaroslav@68: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@68: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@68: * jaroslav@68: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@68: * or visit www.oracle.com if you need additional information or have any jaroslav@68: * questions. jaroslav@68: */ jaroslav@68: jaroslav@68: package java.lang; jaroslav@68: jaroslav@326: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@326: jaroslav@68: /** jaroslav@68: * The {@code Character} class wraps a value of the primitive jaroslav@68: * type {@code char} in an object. An object of type jaroslav@68: * {@code Character} contains a single field whose type is jaroslav@68: * {@code char}. jaroslav@68: *
jaroslav@68: * In addition, this class provides several methods for determining jaroslav@68: * a character's category (lowercase letter, digit, etc.) and for converting jaroslav@68: * characters from uppercase to lowercase and vice versa. jaroslav@68: *
jaroslav@68: * Character information is based on the Unicode Standard, version 6.0.0. jaroslav@68: *
jaroslav@68: * The methods and data of class {@code Character} are defined by jaroslav@68: * the information in the UnicodeData file that is part of the jaroslav@68: * Unicode Character Database maintained by the Unicode jaroslav@68: * Consortium. This file specifies various properties including name jaroslav@68: * and general category for every defined Unicode code point or jaroslav@68: * character range. jaroslav@68: *
jaroslav@68: * The file and its description are available from the Unicode Consortium at: jaroslav@68: *
The {@code char} data type (and therefore the value that a jaroslav@68: * {@code Character} object encapsulates) are based on the jaroslav@68: * original Unicode specification, which defined characters as jaroslav@68: * fixed-width 16-bit entities. The Unicode Standard has since been jaroslav@68: * changed to allow for characters whose representation requires more jaroslav@68: * than 16 bits. The range of legal code points is now jaroslav@68: * U+0000 to U+10FFFF, known as Unicode scalar value. jaroslav@68: * (Refer to the jaroslav@68: * definition of the U+n notation in the Unicode jaroslav@68: * Standard.) jaroslav@68: * jaroslav@68: *
The set of characters from U+0000 to U+FFFF is jaroslav@68: * sometimes referred to as the Basic Multilingual Plane (BMP). jaroslav@68: * Characters whose code points are greater jaroslav@68: * than U+FFFF are called supplementary characters. The Java jaroslav@68: * platform uses the UTF-16 representation in {@code char} arrays and jaroslav@68: * in the {@code String} and {@code StringBuffer} classes. In jaroslav@68: * this representation, supplementary characters are represented as a pair jaroslav@68: * of {@code char} values, the first from the high-surrogates jaroslav@68: * range, (\uD800-\uDBFF), the second from the jaroslav@68: * low-surrogates range (\uDC00-\uDFFF). jaroslav@68: * jaroslav@68: *
A {@code char} value, therefore, represents Basic jaroslav@68: * Multilingual Plane (BMP) code points, including the surrogate jaroslav@68: * code points, or code units of the UTF-16 encoding. An jaroslav@68: * {@code int} value represents all Unicode code points, jaroslav@68: * including supplementary code points. The lower (least significant) jaroslav@68: * 21 bits of {@code int} are used to represent Unicode code jaroslav@68: * points and the upper (most significant) 11 bits must be zero. jaroslav@68: * Unless otherwise specified, the behavior with respect to jaroslav@68: * supplementary characters and surrogate {@code char} values is jaroslav@68: * as follows: jaroslav@68: * jaroslav@68: *
In the Java SE API documentation, Unicode code point is
jaroslav@68: * used for character values in the range between U+0000 and U+10FFFF,
jaroslav@68: * and Unicode code unit is used for 16-bit
jaroslav@68: * {@code char} values that are code units of the UTF-16
jaroslav@68: * encoding. For more information on Unicode terminology, refer to the
jaroslav@68: * Unicode Glossary.
jaroslav@68: *
jaroslav@68: * @author Lee Boynton
jaroslav@68: * @author Guy Steele
jaroslav@68: * @author Akira Tanaka
jaroslav@68: * @author Martin Buchholz
jaroslav@68: * @author Ulf Zibis
jaroslav@68: * @since 1.0
jaroslav@68: */
jaroslav@68: public final
jaroslav@68: class Character implements java.io.Serializable, Comparable Such values do not represent characters by themselves,
jaroslav@68: * but are used in the representation of
jaroslav@68: * supplementary characters
jaroslav@68: * in the UTF-16 encoding.
jaroslav@68: *
jaroslav@68: * @param ch the {@code char} value to be tested.
jaroslav@68: * @return {@code true} if the {@code char} value is between
jaroslav@68: * {@link #MIN_HIGH_SURROGATE} and
jaroslav@68: * {@link #MAX_HIGH_SURROGATE} inclusive;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isLowSurrogate(char)
jaroslav@68: * @see Character.UnicodeBlock#of(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static boolean isHighSurrogate(char ch) {
jaroslav@68: // Help VM constant-fold; MAX_HIGH_SURROGATE + 1 == MIN_LOW_SURROGATE
jaroslav@68: return ch >= MIN_HIGH_SURROGATE && ch < (MAX_HIGH_SURROGATE + 1);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the given {@code char} value is a
jaroslav@68: *
jaroslav@68: * Unicode low-surrogate code unit
jaroslav@68: * (also known as trailing-surrogate code unit).
jaroslav@68: *
jaroslav@68: * Such values do not represent characters by themselves,
jaroslav@68: * but are used in the representation of
jaroslav@68: * supplementary characters
jaroslav@68: * in the UTF-16 encoding.
jaroslav@68: *
jaroslav@68: * @param ch the {@code char} value to be tested.
jaroslav@68: * @return {@code true} if the {@code char} value is between
jaroslav@68: * {@link #MIN_LOW_SURROGATE} and
jaroslav@68: * {@link #MAX_LOW_SURROGATE} inclusive;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isHighSurrogate(char)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static boolean isLowSurrogate(char ch) {
jaroslav@68: return ch >= MIN_LOW_SURROGATE && ch < (MAX_LOW_SURROGATE + 1);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the given {@code char} value is a Unicode
jaroslav@68: * surrogate code unit.
jaroslav@68: *
jaroslav@68: * Such values do not represent characters by themselves,
jaroslav@68: * but are used in the representation of
jaroslav@68: * supplementary characters
jaroslav@68: * in the UTF-16 encoding.
jaroslav@68: *
jaroslav@68: * A char value is a surrogate code unit if and only if it is either
jaroslav@68: * a {@linkplain #isLowSurrogate(char) low-surrogate code unit} or
jaroslav@68: * a {@linkplain #isHighSurrogate(char) high-surrogate code unit}.
jaroslav@68: *
jaroslav@68: * @param ch the {@code char} value to be tested.
jaroslav@68: * @return {@code true} if the {@code char} value is between
jaroslav@68: * {@link #MIN_SURROGATE} and
jaroslav@68: * {@link #MAX_SURROGATE} inclusive;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @since 1.7
jaroslav@68: */
jaroslav@68: public static boolean isSurrogate(char ch) {
jaroslav@68: return ch >= MIN_SURROGATE && ch < (MAX_SURROGATE + 1);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines whether the specified pair of {@code char}
jaroslav@68: * values is a valid
jaroslav@68: *
jaroslav@68: * Unicode surrogate pair.
jaroslav@68:
jaroslav@68: * This method is equivalent to the expression:
jaroslav@68: * This method doesn't validate the specified character to be a
jaroslav@68: * valid Unicode code point. The caller must validate the
jaroslav@68: * character value using {@link #isValidCodePoint(int) isValidCodePoint}
jaroslav@68: * if necessary.
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be tested.
jaroslav@68: * @return 2 if the character is a valid supplementary character; 1 otherwise.
jaroslav@68: * @see Character#isSupplementaryCodePoint(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int charCount(int codePoint) {
jaroslav@68: return codePoint >= MIN_SUPPLEMENTARY_CODE_POINT ? 2 : 1;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Converts the specified surrogate pair to its supplementary code
jaroslav@68: * point value. This method does not validate the specified
jaroslav@68: * surrogate pair. The caller must validate it using {@link
jaroslav@68: * #isSurrogatePair(char, char) isSurrogatePair} if necessary.
jaroslav@68: *
jaroslav@68: * @param high the high-surrogate code unit
jaroslav@68: * @param low the low-surrogate code unit
jaroslav@68: * @return the supplementary code point composed from the
jaroslav@68: * specified surrogate pair.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int toCodePoint(char high, char low) {
jaroslav@68: // Optimized form of:
jaroslav@68: // return ((high - MIN_HIGH_SURROGATE) << 10)
jaroslav@68: // + (low - MIN_LOW_SURROGATE)
jaroslav@68: // + MIN_SUPPLEMENTARY_CODE_POINT;
jaroslav@68: return ((high << 10) + low) + (MIN_SUPPLEMENTARY_CODE_POINT
jaroslav@68: - (MIN_HIGH_SURROGATE << 10)
jaroslav@68: - MIN_LOW_SURROGATE);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the code point at the given index of the
jaroslav@68: * {@code CharSequence}. If the {@code char} value at
jaroslav@68: * the given index in the {@code CharSequence} is in the
jaroslav@68: * high-surrogate range, the following index is less than the
jaroslav@68: * length of the {@code CharSequence}, and the
jaroslav@68: * {@code char} value at the following index is in the
jaroslav@68: * low-surrogate range, then the supplementary code point
jaroslav@68: * corresponding to this surrogate pair is returned. Otherwise,
jaroslav@68: * the {@code char} value at the given index is returned.
jaroslav@68: *
jaroslav@68: * @param seq a sequence of {@code char} values (Unicode code
jaroslav@68: * units)
jaroslav@68: * @param index the index to the {@code char} values (Unicode
jaroslav@68: * code units) in {@code seq} to be converted
jaroslav@68: * @return the Unicode code point at the given index
jaroslav@68: * @exception NullPointerException if {@code seq} is null.
jaroslav@68: * @exception IndexOutOfBoundsException if the value
jaroslav@68: * {@code index} is negative or not less than
jaroslav@68: * {@link CharSequence#length() seq.length()}.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int codePointAt(CharSequence seq, int index) {
jaroslav@68: char c1 = seq.charAt(index++);
jaroslav@68: if (isHighSurrogate(c1)) {
jaroslav@68: if (index < seq.length()) {
jaroslav@68: char c2 = seq.charAt(index);
jaroslav@68: if (isLowSurrogate(c2)) {
jaroslav@68: return toCodePoint(c1, c2);
jaroslav@68: }
jaroslav@68: }
jaroslav@68: }
jaroslav@68: return c1;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the code point at the given index of the
jaroslav@68: * {@code char} array. If the {@code char} value at
jaroslav@68: * the given index in the {@code char} array is in the
jaroslav@68: * high-surrogate range, the following index is less than the
jaroslav@68: * length of the {@code char} array, and the
jaroslav@68: * {@code char} value at the following index is in the
jaroslav@68: * low-surrogate range, then the supplementary code point
jaroslav@68: * corresponding to this surrogate pair is returned. Otherwise,
jaroslav@68: * the {@code char} value at the given index is returned.
jaroslav@68: *
jaroslav@68: * @param a the {@code char} array
jaroslav@68: * @param index the index to the {@code char} values (Unicode
jaroslav@68: * code units) in the {@code char} array to be converted
jaroslav@68: * @return the Unicode code point at the given index
jaroslav@68: * @exception NullPointerException if {@code a} is null.
jaroslav@68: * @exception IndexOutOfBoundsException if the value
jaroslav@68: * {@code index} is negative or not less than
jaroslav@68: * the length of the {@code char} array.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int codePointAt(char[] a, int index) {
jaroslav@68: return codePointAtImpl(a, index, a.length);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the code point at the given index of the
jaroslav@68: * {@code char} array, where only array elements with
jaroslav@68: * {@code index} less than {@code limit} can be used. If
jaroslav@68: * the {@code char} value at the given index in the
jaroslav@68: * {@code char} array is in the high-surrogate range, the
jaroslav@68: * following index is less than the {@code limit}, and the
jaroslav@68: * {@code char} value at the following index is in the
jaroslav@68: * low-surrogate range, then the supplementary code point
jaroslav@68: * corresponding to this surrogate pair is returned. Otherwise,
jaroslav@68: * the {@code char} value at the given index is returned.
jaroslav@68: *
jaroslav@68: * @param a the {@code char} array
jaroslav@68: * @param index the index to the {@code char} values (Unicode
jaroslav@68: * code units) in the {@code char} array to be converted
jaroslav@68: * @param limit the index after the last array element that
jaroslav@68: * can be used in the {@code char} array
jaroslav@68: * @return the Unicode code point at the given index
jaroslav@68: * @exception NullPointerException if {@code a} is null.
jaroslav@68: * @exception IndexOutOfBoundsException if the {@code index}
jaroslav@68: * argument is negative or not less than the {@code limit}
jaroslav@68: * argument, or if the {@code limit} argument is negative or
jaroslav@68: * greater than the length of the {@code char} array.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int codePointAt(char[] a, int index, int limit) {
jaroslav@68: if (index >= limit || limit < 0 || limit > a.length) {
jaroslav@68: throw new IndexOutOfBoundsException();
jaroslav@68: }
jaroslav@68: return codePointAtImpl(a, index, limit);
jaroslav@68: }
jaroslav@68:
jaroslav@68: // throws ArrayIndexOutofBoundsException if index out of bounds
jaroslav@68: static int codePointAtImpl(char[] a, int index, int limit) {
jaroslav@68: char c1 = a[index++];
jaroslav@68: if (isHighSurrogate(c1)) {
jaroslav@68: if (index < limit) {
jaroslav@68: char c2 = a[index];
jaroslav@68: if (isLowSurrogate(c2)) {
jaroslav@68: return toCodePoint(c1, c2);
jaroslav@68: }
jaroslav@68: }
jaroslav@68: }
jaroslav@68: return c1;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the code point preceding the given index of the
jaroslav@68: * {@code CharSequence}. If the {@code char} value at
jaroslav@68: * {@code (index - 1)} in the {@code CharSequence} is in
jaroslav@68: * the low-surrogate range, {@code (index - 2)} is not
jaroslav@68: * negative, and the {@code char} value at {@code (index - 2)}
jaroslav@68: * in the {@code CharSequence} is in the
jaroslav@68: * high-surrogate range, then the supplementary code point
jaroslav@68: * corresponding to this surrogate pair is returned. Otherwise,
jaroslav@68: * the {@code char} value at {@code (index - 1)} is
jaroslav@68: * returned.
jaroslav@68: *
jaroslav@68: * @param seq the {@code CharSequence} instance
jaroslav@68: * @param index the index following the code point that should be returned
jaroslav@68: * @return the Unicode code point value before the given index.
jaroslav@68: * @exception NullPointerException if {@code seq} is null.
jaroslav@68: * @exception IndexOutOfBoundsException if the {@code index}
jaroslav@68: * argument is less than 1 or greater than {@link
jaroslav@68: * CharSequence#length() seq.length()}.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int codePointBefore(CharSequence seq, int index) {
jaroslav@68: char c2 = seq.charAt(--index);
jaroslav@68: if (isLowSurrogate(c2)) {
jaroslav@68: if (index > 0) {
jaroslav@68: char c1 = seq.charAt(--index);
jaroslav@68: if (isHighSurrogate(c1)) {
jaroslav@68: return toCodePoint(c1, c2);
jaroslav@68: }
jaroslav@68: }
jaroslav@68: }
jaroslav@68: return c2;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the code point preceding the given index of the
jaroslav@68: * {@code char} array. If the {@code char} value at
jaroslav@68: * {@code (index - 1)} in the {@code char} array is in
jaroslav@68: * the low-surrogate range, {@code (index - 2)} is not
jaroslav@68: * negative, and the {@code char} value at {@code (index - 2)}
jaroslav@68: * in the {@code char} array is in the
jaroslav@68: * high-surrogate range, then the supplementary code point
jaroslav@68: * corresponding to this surrogate pair is returned. Otherwise,
jaroslav@68: * the {@code char} value at {@code (index - 1)} is
jaroslav@68: * returned.
jaroslav@68: *
jaroslav@68: * @param a the {@code char} array
jaroslav@68: * @param index the index following the code point that should be returned
jaroslav@68: * @return the Unicode code point value before the given index.
jaroslav@68: * @exception NullPointerException if {@code a} is null.
jaroslav@68: * @exception IndexOutOfBoundsException if the {@code index}
jaroslav@68: * argument is less than 1 or greater than the length of the
jaroslav@68: * {@code char} array
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int codePointBefore(char[] a, int index) {
jaroslav@68: return codePointBeforeImpl(a, index, 0);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the code point preceding the given index of the
jaroslav@68: * {@code char} array, where only array elements with
jaroslav@68: * {@code index} greater than or equal to {@code start}
jaroslav@68: * can be used. If the {@code char} value at {@code (index - 1)}
jaroslav@68: * in the {@code char} array is in the
jaroslav@68: * low-surrogate range, {@code (index - 2)} is not less than
jaroslav@68: * {@code start}, and the {@code char} value at
jaroslav@68: * {@code (index - 2)} in the {@code char} array is in
jaroslav@68: * the high-surrogate range, then the supplementary code point
jaroslav@68: * corresponding to this surrogate pair is returned. Otherwise,
jaroslav@68: * the {@code char} value at {@code (index - 1)} is
jaroslav@68: * returned.
jaroslav@68: *
jaroslav@68: * @param a the {@code char} array
jaroslav@68: * @param index the index following the code point that should be returned
jaroslav@68: * @param start the index of the first array element in the
jaroslav@68: * {@code char} array
jaroslav@68: * @return the Unicode code point value before the given index.
jaroslav@68: * @exception NullPointerException if {@code a} is null.
jaroslav@68: * @exception IndexOutOfBoundsException if the {@code index}
jaroslav@68: * argument is not greater than the {@code start} argument or
jaroslav@68: * is greater than the length of the {@code char} array, or
jaroslav@68: * if the {@code start} argument is negative or not less than
jaroslav@68: * the length of the {@code char} array.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int codePointBefore(char[] a, int index, int start) {
jaroslav@68: if (index <= start || start < 0 || start >= a.length) {
jaroslav@68: throw new IndexOutOfBoundsException();
jaroslav@68: }
jaroslav@68: return codePointBeforeImpl(a, index, start);
jaroslav@68: }
jaroslav@68:
jaroslav@68: // throws ArrayIndexOutofBoundsException if index-1 out of bounds
jaroslav@68: static int codePointBeforeImpl(char[] a, int index, int start) {
jaroslav@68: char c2 = a[--index];
jaroslav@68: if (isLowSurrogate(c2)) {
jaroslav@68: if (index > start) {
jaroslav@68: char c1 = a[--index];
jaroslav@68: if (isHighSurrogate(c1)) {
jaroslav@68: return toCodePoint(c1, c2);
jaroslav@68: }
jaroslav@68: }
jaroslav@68: }
jaroslav@68: return c2;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the leading surrogate (a
jaroslav@68: *
jaroslav@68: * high surrogate code unit) of the
jaroslav@68: *
jaroslav@68: * surrogate pair
jaroslav@68: * representing the specified supplementary character (Unicode
jaroslav@68: * code point) in the UTF-16 encoding. If the specified character
jaroslav@68: * is not a
jaroslav@68: * supplementary character,
jaroslav@68: * an unspecified {@code char} is returned.
jaroslav@68: *
jaroslav@68: * If
jaroslav@68: * {@link #isSupplementaryCodePoint isSupplementaryCodePoint(x)}
jaroslav@68: * is {@code true}, then
jaroslav@68: * {@link #isHighSurrogate isHighSurrogate}{@code (highSurrogate(x))} and
jaroslav@68: * {@link #toCodePoint toCodePoint}{@code (highSurrogate(x), }{@link #lowSurrogate lowSurrogate}{@code (x)) == x}
jaroslav@68: * are also always {@code true}.
jaroslav@68: *
jaroslav@68: * @param codePoint a supplementary character (Unicode code point)
jaroslav@68: * @return the leading surrogate code unit used to represent the
jaroslav@68: * character in the UTF-16 encoding
jaroslav@68: * @since 1.7
jaroslav@68: */
jaroslav@68: public static char highSurrogate(int codePoint) {
jaroslav@68: return (char) ((codePoint >>> 10)
jaroslav@68: + (MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT >>> 10)));
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the trailing surrogate (a
jaroslav@68: *
jaroslav@68: * low surrogate code unit) of the
jaroslav@68: *
jaroslav@68: * surrogate pair
jaroslav@68: * representing the specified supplementary character (Unicode
jaroslav@68: * code point) in the UTF-16 encoding. If the specified character
jaroslav@68: * is not a
jaroslav@68: * supplementary character,
jaroslav@68: * an unspecified {@code char} is returned.
jaroslav@68: *
jaroslav@68: * If
jaroslav@68: * {@link #isSupplementaryCodePoint isSupplementaryCodePoint(x)}
jaroslav@68: * is {@code true}, then
jaroslav@68: * {@link #isLowSurrogate isLowSurrogate}{@code (lowSurrogate(x))} and
jaroslav@68: * {@link #toCodePoint toCodePoint}{@code (}{@link #highSurrogate highSurrogate}{@code (x), lowSurrogate(x)) == x}
jaroslav@68: * are also always {@code true}.
jaroslav@68: *
jaroslav@68: * @param codePoint a supplementary character (Unicode code point)
jaroslav@68: * @return the trailing surrogate code unit used to represent the
jaroslav@68: * character in the UTF-16 encoding
jaroslav@68: * @since 1.7
jaroslav@68: */
jaroslav@68: public static char lowSurrogate(int codePoint) {
jaroslav@68: return (char) ((codePoint & 0x3ff) + MIN_LOW_SURROGATE);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Converts the specified character (Unicode code point) to its
jaroslav@68: * UTF-16 representation. If the specified code point is a BMP
jaroslav@68: * (Basic Multilingual Plane or Plane 0) value, the same value is
jaroslav@68: * stored in {@code dst[dstIndex]}, and 1 is returned. If the
jaroslav@68: * specified code point is a supplementary character, its
jaroslav@68: * surrogate values are stored in {@code dst[dstIndex]}
jaroslav@68: * (high-surrogate) and {@code dst[dstIndex+1]}
jaroslav@68: * (low-surrogate), and 2 is returned.
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be converted.
jaroslav@68: * @param dst an array of {@code char} in which the
jaroslav@68: * {@code codePoint}'s UTF-16 value is stored.
jaroslav@68: * @param dstIndex the start index into the {@code dst}
jaroslav@68: * array where the converted value is stored.
jaroslav@68: * @return 1 if the code point is a BMP code point, 2 if the
jaroslav@68: * code point is a supplementary code point.
jaroslav@68: * @exception IllegalArgumentException if the specified
jaroslav@68: * {@code codePoint} is not a valid Unicode code point.
jaroslav@68: * @exception NullPointerException if the specified {@code dst} is null.
jaroslav@68: * @exception IndexOutOfBoundsException if {@code dstIndex}
jaroslav@68: * is negative or not less than {@code dst.length}, or if
jaroslav@68: * {@code dst} at {@code dstIndex} doesn't have enough
jaroslav@68: * array element(s) to store the resulting {@code char}
jaroslav@68: * value(s). (If {@code dstIndex} is equal to
jaroslav@68: * {@code dst.length-1} and the specified
jaroslav@68: * {@code codePoint} is a supplementary character, the
jaroslav@68: * high-surrogate value is not stored in
jaroslav@68: * {@code dst[dstIndex]}.)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int toChars(int codePoint, char[] dst, int dstIndex) {
jaroslav@68: if (isBmpCodePoint(codePoint)) {
jaroslav@68: dst[dstIndex] = (char) codePoint;
jaroslav@68: return 1;
jaroslav@68: } else if (isValidCodePoint(codePoint)) {
jaroslav@68: toSurrogates(codePoint, dst, dstIndex);
jaroslav@68: return 2;
jaroslav@68: } else {
jaroslav@68: throw new IllegalArgumentException();
jaroslav@68: }
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Converts the specified character (Unicode code point) to its
jaroslav@68: * UTF-16 representation stored in a {@code char} array. If
jaroslav@68: * the specified code point is a BMP (Basic Multilingual Plane or
jaroslav@68: * Plane 0) value, the resulting {@code char} array has
jaroslav@68: * the same value as {@code codePoint}. If the specified code
jaroslav@68: * point is a supplementary code point, the resulting
jaroslav@68: * {@code char} array has the corresponding surrogate pair.
jaroslav@68: *
jaroslav@68: * @param codePoint a Unicode code point
jaroslav@68: * @return a {@code char} array having
jaroslav@68: * {@code codePoint}'s UTF-16 representation.
jaroslav@68: * @exception IllegalArgumentException if the specified
jaroslav@68: * {@code codePoint} is not a valid Unicode code point.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static char[] toChars(int codePoint) {
jaroslav@68: if (isBmpCodePoint(codePoint)) {
jaroslav@68: return new char[] { (char) codePoint };
jaroslav@68: } else if (isValidCodePoint(codePoint)) {
jaroslav@68: char[] result = new char[2];
jaroslav@68: toSurrogates(codePoint, result, 0);
jaroslav@68: return result;
jaroslav@68: } else {
jaroslav@68: throw new IllegalArgumentException();
jaroslav@68: }
jaroslav@68: }
jaroslav@68:
jaroslav@68: static void toSurrogates(int codePoint, char[] dst, int index) {
jaroslav@68: // We write elements "backwards" to guarantee all-or-nothing
jaroslav@68: dst[index+1] = lowSurrogate(codePoint);
jaroslav@68: dst[index] = highSurrogate(codePoint);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the number of Unicode code points in the text range of
jaroslav@68: * the specified char sequence. The text range begins at the
jaroslav@68: * specified {@code beginIndex} and extends to the
jaroslav@68: * {@code char} at index {@code endIndex - 1}. Thus the
jaroslav@68: * length (in {@code char}s) of the text range is
jaroslav@68: * {@code endIndex-beginIndex}. Unpaired surrogates within
jaroslav@68: * the text range count as one code point each.
jaroslav@68: *
jaroslav@68: * @param seq the char sequence
jaroslav@68: * @param beginIndex the index to the first {@code char} of
jaroslav@68: * the text range.
jaroslav@68: * @param endIndex the index after the last {@code char} of
jaroslav@68: * the text range.
jaroslav@68: * @return the number of Unicode code points in the specified text
jaroslav@68: * range
jaroslav@68: * @exception NullPointerException if {@code seq} is null.
jaroslav@68: * @exception IndexOutOfBoundsException if the
jaroslav@68: * {@code beginIndex} is negative, or {@code endIndex}
jaroslav@68: * is larger than the length of the given sequence, or
jaroslav@68: * {@code beginIndex} is larger than {@code endIndex}.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int codePointCount(CharSequence seq, int beginIndex, int endIndex) {
jaroslav@68: int length = seq.length();
jaroslav@68: if (beginIndex < 0 || endIndex > length || beginIndex > endIndex) {
jaroslav@68: throw new IndexOutOfBoundsException();
jaroslav@68: }
jaroslav@68: int n = endIndex - beginIndex;
jaroslav@68: for (int i = beginIndex; i < endIndex; ) {
jaroslav@68: if (isHighSurrogate(seq.charAt(i++)) && i < endIndex &&
jaroslav@68: isLowSurrogate(seq.charAt(i))) {
jaroslav@68: n--;
jaroslav@68: i++;
jaroslav@68: }
jaroslav@68: }
jaroslav@68: return n;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the number of Unicode code points in a subarray of the
jaroslav@68: * {@code char} array argument. The {@code offset}
jaroslav@68: * argument is the index of the first {@code char} of the
jaroslav@68: * subarray and the {@code count} argument specifies the
jaroslav@68: * length of the subarray in {@code char}s. Unpaired
jaroslav@68: * surrogates within the subarray count as one code point each.
jaroslav@68: *
jaroslav@68: * @param a the {@code char} array
jaroslav@68: * @param offset the index of the first {@code char} in the
jaroslav@68: * given {@code char} array
jaroslav@68: * @param count the length of the subarray in {@code char}s
jaroslav@68: * @return the number of Unicode code points in the specified subarray
jaroslav@68: * @exception NullPointerException if {@code a} is null.
jaroslav@68: * @exception IndexOutOfBoundsException if {@code offset} or
jaroslav@68: * {@code count} is negative, or if {@code offset +
jaroslav@68: * count} is larger than the length of the given array.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int codePointCount(char[] a, int offset, int count) {
jaroslav@68: if (count > a.length - offset || offset < 0 || count < 0) {
jaroslav@68: throw new IndexOutOfBoundsException();
jaroslav@68: }
jaroslav@68: return codePointCountImpl(a, offset, count);
jaroslav@68: }
jaroslav@68:
jaroslav@68: static int codePointCountImpl(char[] a, int offset, int count) {
jaroslav@68: int endIndex = offset + count;
jaroslav@68: int n = count;
jaroslav@68: for (int i = offset; i < endIndex; ) {
jaroslav@68: if (isHighSurrogate(a[i++]) && i < endIndex &&
jaroslav@68: isLowSurrogate(a[i])) {
jaroslav@68: n--;
jaroslav@68: i++;
jaroslav@68: }
jaroslav@68: }
jaroslav@68: return n;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the index within the given char sequence that is offset
jaroslav@68: * from the given {@code index} by {@code codePointOffset}
jaroslav@68: * code points. Unpaired surrogates within the text range given by
jaroslav@68: * {@code index} and {@code codePointOffset} count as
jaroslav@68: * one code point each.
jaroslav@68: *
jaroslav@68: * @param seq the char sequence
jaroslav@68: * @param index the index to be offset
jaroslav@68: * @param codePointOffset the offset in code points
jaroslav@68: * @return the index within the char sequence
jaroslav@68: * @exception NullPointerException if {@code seq} is null.
jaroslav@68: * @exception IndexOutOfBoundsException if {@code index}
jaroslav@68: * is negative or larger then the length of the char sequence,
jaroslav@68: * or if {@code codePointOffset} is positive and the
jaroslav@68: * subsequence starting with {@code index} has fewer than
jaroslav@68: * {@code codePointOffset} code points, or if
jaroslav@68: * {@code codePointOffset} is negative and the subsequence
jaroslav@68: * before {@code index} has fewer than the absolute value
jaroslav@68: * of {@code codePointOffset} code points.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int offsetByCodePoints(CharSequence seq, int index,
jaroslav@68: int codePointOffset) {
jaroslav@68: int length = seq.length();
jaroslav@68: if (index < 0 || index > length) {
jaroslav@68: throw new IndexOutOfBoundsException();
jaroslav@68: }
jaroslav@68:
jaroslav@68: int x = index;
jaroslav@68: if (codePointOffset >= 0) {
jaroslav@68: int i;
jaroslav@68: for (i = 0; x < length && i < codePointOffset; i++) {
jaroslav@68: if (isHighSurrogate(seq.charAt(x++)) && x < length &&
jaroslav@68: isLowSurrogate(seq.charAt(x))) {
jaroslav@68: x++;
jaroslav@68: }
jaroslav@68: }
jaroslav@68: if (i < codePointOffset) {
jaroslav@68: throw new IndexOutOfBoundsException();
jaroslav@68: }
jaroslav@68: } else {
jaroslav@68: int i;
jaroslav@68: for (i = codePointOffset; x > 0 && i < 0; i++) {
jaroslav@68: if (isLowSurrogate(seq.charAt(--x)) && x > 0 &&
jaroslav@68: isHighSurrogate(seq.charAt(x-1))) {
jaroslav@68: x--;
jaroslav@68: }
jaroslav@68: }
jaroslav@68: if (i < 0) {
jaroslav@68: throw new IndexOutOfBoundsException();
jaroslav@68: }
jaroslav@68: }
jaroslav@68: return x;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the index within the given {@code char} subarray
jaroslav@68: * that is offset from the given {@code index} by
jaroslav@68: * {@code codePointOffset} code points. The
jaroslav@68: * {@code start} and {@code count} arguments specify a
jaroslav@68: * subarray of the {@code char} array. Unpaired surrogates
jaroslav@68: * within the text range given by {@code index} and
jaroslav@68: * {@code codePointOffset} count as one code point each.
jaroslav@68: *
jaroslav@68: * @param a the {@code char} array
jaroslav@68: * @param start the index of the first {@code char} of the
jaroslav@68: * subarray
jaroslav@68: * @param count the length of the subarray in {@code char}s
jaroslav@68: * @param index the index to be offset
jaroslav@68: * @param codePointOffset the offset in code points
jaroslav@68: * @return the index within the subarray
jaroslav@68: * @exception NullPointerException if {@code a} is null.
jaroslav@68: * @exception IndexOutOfBoundsException
jaroslav@68: * if {@code start} or {@code count} is negative,
jaroslav@68: * or if {@code start + count} is larger than the length of
jaroslav@68: * the given array,
jaroslav@68: * or if {@code index} is less than {@code start} or
jaroslav@68: * larger then {@code start + count},
jaroslav@68: * or if {@code codePointOffset} is positive and the text range
jaroslav@68: * starting with {@code index} and ending with {@code start + count - 1}
jaroslav@68: * has fewer than {@code codePointOffset} code
jaroslav@68: * points,
jaroslav@68: * or if {@code codePointOffset} is negative and the text range
jaroslav@68: * starting with {@code start} and ending with {@code index - 1}
jaroslav@68: * has fewer than the absolute value of
jaroslav@68: * {@code codePointOffset} code points.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int offsetByCodePoints(char[] a, int start, int count,
jaroslav@68: int index, int codePointOffset) {
jaroslav@68: if (count > a.length-start || start < 0 || count < 0
jaroslav@68: || index < start || index > start+count) {
jaroslav@68: throw new IndexOutOfBoundsException();
jaroslav@68: }
jaroslav@68: return offsetByCodePointsImpl(a, start, count, index, codePointOffset);
jaroslav@68: }
jaroslav@68:
jaroslav@68: static int offsetByCodePointsImpl(char[]a, int start, int count,
jaroslav@68: int index, int codePointOffset) {
jaroslav@68: int x = index;
jaroslav@68: if (codePointOffset >= 0) {
jaroslav@68: int limit = start + count;
jaroslav@68: int i;
jaroslav@68: for (i = 0; x < limit && i < codePointOffset; i++) {
jaroslav@68: if (isHighSurrogate(a[x++]) && x < limit &&
jaroslav@68: isLowSurrogate(a[x])) {
jaroslav@68: x++;
jaroslav@68: }
jaroslav@68: }
jaroslav@68: if (i < codePointOffset) {
jaroslav@68: throw new IndexOutOfBoundsException();
jaroslav@68: }
jaroslav@68: } else {
jaroslav@68: int i;
jaroslav@68: for (i = codePointOffset; x > start && i < 0; i++) {
jaroslav@68: if (isLowSurrogate(a[--x]) && x > start &&
jaroslav@68: isHighSurrogate(a[x-1])) {
jaroslav@68: x--;
jaroslav@68: }
jaroslav@68: }
jaroslav@68: if (i < 0) {
jaroslav@68: throw new IndexOutOfBoundsException();
jaroslav@68: }
jaroslav@68: }
jaroslav@68: return x;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character is a lowercase character.
jaroslav@68: *
jaroslav@68: * A character is lowercase if its general category type, provided
jaroslav@68: * by {@code Character.getType(ch)}, is
jaroslav@68: * {@code LOWERCASE_LETTER}, or it has contributory property
jaroslav@68: * Other_Lowercase as defined by the Unicode Standard.
jaroslav@68: *
jaroslav@68: * The following are examples of lowercase characters:
jaroslav@68: * Many other Unicode characters are lowercase too.
jaroslav@68: *
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #isLowerCase(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be tested.
jaroslav@68: * @return {@code true} if the character is lowercase;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isLowerCase(char)
jaroslav@68: * @see Character#isTitleCase(char)
jaroslav@68: * @see Character#toLowerCase(char)
jaroslav@68: * @see Character#getType(char)
jaroslav@68: */
jaroslav@68: public static boolean isLowerCase(char ch) {
jaroslav@326: return ch == toLowerCase(ch);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character is an uppercase character.
jaroslav@68: *
jaroslav@68: * A character is uppercase if its general category type, provided by
jaroslav@68: * {@code Character.getType(ch)}, is {@code UPPERCASE_LETTER}.
jaroslav@68: * or it has contributory property Other_Uppercase as defined by the Unicode Standard.
jaroslav@68: *
jaroslav@68: * The following are examples of uppercase characters:
jaroslav@68: * Many other Unicode characters are uppercase too.
jaroslav@68: *
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #isUpperCase(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be tested.
jaroslav@68: * @return {@code true} if the character is uppercase;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isLowerCase(char)
jaroslav@68: * @see Character#isTitleCase(char)
jaroslav@68: * @see Character#toUpperCase(char)
jaroslav@68: * @see Character#getType(char)
jaroslav@68: * @since 1.0
jaroslav@68: */
jaroslav@68: public static boolean isUpperCase(char ch) {
jaroslav@326: return ch == toUpperCase(ch);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character is a titlecase character.
jaroslav@68: *
jaroslav@68: * A character is a titlecase character if its general
jaroslav@68: * category type, provided by {@code Character.getType(ch)},
jaroslav@68: * is {@code TITLECASE_LETTER}.
jaroslav@68: *
jaroslav@68: * Some characters look like pairs of Latin letters. For example, there
jaroslav@68: * is an uppercase letter that looks like "LJ" and has a corresponding
jaroslav@68: * lowercase letter that looks like "lj". A third form, which looks like "Lj",
jaroslav@68: * is the appropriate form to use when rendering a word in lowercase
jaroslav@68: * with initial capitals, as for a book title.
jaroslav@68: *
jaroslav@68: * These are some of the Unicode characters for which this method returns
jaroslav@68: * {@code true}:
jaroslav@68: * Many other Unicode characters are titlecase too.
jaroslav@68: *
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #isTitleCase(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be tested.
jaroslav@68: * @return {@code true} if the character is titlecase;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isLowerCase(char)
jaroslav@68: * @see Character#isUpperCase(char)
jaroslav@68: * @see Character#toTitleCase(char)
jaroslav@68: * @see Character#getType(char)
jaroslav@68: * @since 1.0.2
jaroslav@68: */
jaroslav@68: public static boolean isTitleCase(char ch) {
jaroslav@68: return isTitleCase((int)ch);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character (Unicode code point) is a titlecase character.
jaroslav@68: *
jaroslav@68: * A character is a titlecase character if its general
jaroslav@68: * category type, provided by {@link Character#getType(int) getType(codePoint)},
jaroslav@68: * is {@code TITLECASE_LETTER}.
jaroslav@68: *
jaroslav@68: * Some characters look like pairs of Latin letters. For example, there
jaroslav@68: * is an uppercase letter that looks like "LJ" and has a corresponding
jaroslav@68: * lowercase letter that looks like "lj". A third form, which looks like "Lj",
jaroslav@68: * is the appropriate form to use when rendering a word in lowercase
jaroslav@68: * with initial capitals, as for a book title.
jaroslav@68: *
jaroslav@68: * These are some of the Unicode characters for which this method returns
jaroslav@68: * {@code true}:
jaroslav@68: * Many other Unicode characters are titlecase too.
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be tested.
jaroslav@68: * @return {@code true} if the character is titlecase;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isLowerCase(int)
jaroslav@68: * @see Character#isUpperCase(int)
jaroslav@68: * @see Character#toTitleCase(int)
jaroslav@68: * @see Character#getType(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static boolean isTitleCase(int codePoint) {
jaroslav@68: return getType(codePoint) == Character.TITLECASE_LETTER;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character is a digit.
jaroslav@68: *
jaroslav@68: * A character is a digit if its general category type, provided
jaroslav@68: * by {@code Character.getType(ch)}, is
jaroslav@68: * {@code DECIMAL_DIGIT_NUMBER}.
jaroslav@68: *
jaroslav@68: * Some Unicode character ranges that contain digits:
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #isDigit(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be tested.
jaroslav@68: * @return {@code true} if the character is a digit;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#digit(char, int)
jaroslav@68: * @see Character#forDigit(int, int)
jaroslav@68: * @see Character#getType(char)
jaroslav@68: */
jaroslav@68: public static boolean isDigit(char ch) {
jaroslav@326: return String.valueOf(ch).matches("\\d");
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character (Unicode code point) is a digit.
jaroslav@68: *
jaroslav@68: * A character is a digit if its general category type, provided
jaroslav@68: * by {@link Character#getType(int) getType(codePoint)}, is
jaroslav@68: * {@code DECIMAL_DIGIT_NUMBER}.
jaroslav@68: *
jaroslav@68: * Some Unicode character ranges that contain digits:
jaroslav@68: *
jaroslav@68: * A character is defined if at least one of the following is true:
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #isDefined(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be tested
jaroslav@68: * @return {@code true} if the character has a defined meaning
jaroslav@68: * in Unicode; {@code false} otherwise.
jaroslav@68: * @see Character#isDigit(char)
jaroslav@68: * @see Character#isLetter(char)
jaroslav@68: * @see Character#isLetterOrDigit(char)
jaroslav@68: * @see Character#isLowerCase(char)
jaroslav@68: * @see Character#isTitleCase(char)
jaroslav@68: * @see Character#isUpperCase(char)
jaroslav@68: * @since 1.0.2
jaroslav@68: */
jaroslav@68: public static boolean isDefined(char ch) {
jaroslav@68: return isDefined((int)ch);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if a character (Unicode code point) is defined in Unicode.
jaroslav@68: *
jaroslav@68: * A character is defined if at least one of the following is true:
jaroslav@68: *
jaroslav@68: * A character is considered to be a letter if its general
jaroslav@68: * category type, provided by {@code Character.getType(ch)},
jaroslav@68: * is any of the following:
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #isLetter(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be tested.
jaroslav@68: * @return {@code true} if the character is a letter;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isDigit(char)
jaroslav@68: * @see Character#isJavaIdentifierStart(char)
jaroslav@68: * @see Character#isJavaLetter(char)
jaroslav@68: * @see Character#isJavaLetterOrDigit(char)
jaroslav@68: * @see Character#isLetterOrDigit(char)
jaroslav@68: * @see Character#isLowerCase(char)
jaroslav@68: * @see Character#isTitleCase(char)
jaroslav@68: * @see Character#isUnicodeIdentifierStart(char)
jaroslav@68: * @see Character#isUpperCase(char)
jaroslav@68: */
jaroslav@68: public static boolean isLetter(char ch) {
jaroslav@326: return String.valueOf(ch).matches("\\w") && !isDigit(ch);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character (Unicode code point) is a letter.
jaroslav@68: *
jaroslav@68: * A character is considered to be a letter if its general
jaroslav@68: * category type, provided by {@link Character#getType(int) getType(codePoint)},
jaroslav@68: * is any of the following:
jaroslav@68: *
jaroslav@68: * A character is considered to be a letter or digit if either
jaroslav@68: * {@code Character.isLetter(char ch)} or
jaroslav@68: * {@code Character.isDigit(char ch)} returns
jaroslav@68: * {@code true} for the character.
jaroslav@68: *
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #isLetterOrDigit(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be tested.
jaroslav@68: * @return {@code true} if the character is a letter or digit;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isDigit(char)
jaroslav@68: * @see Character#isJavaIdentifierPart(char)
jaroslav@68: * @see Character#isJavaLetter(char)
jaroslav@68: * @see Character#isJavaLetterOrDigit(char)
jaroslav@68: * @see Character#isLetter(char)
jaroslav@68: * @see Character#isUnicodeIdentifierPart(char)
jaroslav@68: * @since 1.0.2
jaroslav@68: */
jaroslav@68: public static boolean isLetterOrDigit(char ch) {
jaroslav@326: return String.valueOf(ch).matches("\\w");
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character (Unicode code point) is a letter or digit.
jaroslav@68: *
jaroslav@68: * A character is considered to be a letter or digit if either
jaroslav@68: * {@link #isLetter(int) isLetter(codePoint)} or
jaroslav@68: * {@link #isDigit(int) isDigit(codePoint)} returns
jaroslav@68: * {@code true} for the character.
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be tested.
jaroslav@68: * @return {@code true} if the character is a letter or digit;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isDigit(int)
jaroslav@68: * @see Character#isJavaIdentifierPart(int)
jaroslav@68: * @see Character#isLetter(int)
jaroslav@68: * @see Character#isUnicodeIdentifierPart(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static boolean isLetterOrDigit(int codePoint) {
jaroslav@326: return fromCodeChars(codePoint).matches("\\w");
jaroslav@68: }
jaroslav@85:
jaroslav@85: static int getType(int x) {
jaroslav@85: throw new UnsupportedOperationException();
jaroslav@68: }
jaroslav@563:
jaroslav@563: /**
jaroslav@563: * Determines if the specified character is
jaroslav@563: * permissible as the first character in a Java identifier.
jaroslav@563: *
jaroslav@563: * A character may start a Java identifier if and only if
jaroslav@563: * one of the following conditions is true:
jaroslav@563: * Note: This method cannot handle supplementary characters. To support
jaroslav@563: * all Unicode characters, including supplementary characters, use
jaroslav@563: * the {@link #isJavaIdentifierStart(int)} method.
jaroslav@563: *
jaroslav@563: * @param ch the character to be tested.
jaroslav@563: * @return {@code true} if the character may start a Java identifier;
jaroslav@563: * {@code false} otherwise.
jaroslav@563: * @see Character#isJavaIdentifierPart(char)
jaroslav@563: * @see Character#isLetter(char)
jaroslav@563: * @see Character#isUnicodeIdentifierStart(char)
jaroslav@563: * @see javax.lang.model.SourceVersion#isIdentifier(CharSequence)
jaroslav@563: * @since 1.1
jaroslav@563: */
jaroslav@563: public static boolean isJavaIdentifierStart(char ch) {
jaroslav@563: return isJavaIdentifierStart((int)ch);
jaroslav@563: }
jaroslav@563:
jaroslav@563: /**
jaroslav@563: * Determines if the character (Unicode code point) is
jaroslav@563: * permissible as the first character in a Java identifier.
jaroslav@563: *
jaroslav@563: * A character may start a Java identifier if and only if
jaroslav@563: * one of the following conditions is true:
jaroslav@563: *
jaroslav@563: * A character may be part of a Java identifier if any of the following
jaroslav@563: * are true:
jaroslav@563: * Note: This method cannot handle supplementary characters. To support
jaroslav@563: * all Unicode characters, including supplementary characters, use
jaroslav@563: * the {@link #isJavaIdentifierPart(int)} method.
jaroslav@563: *
jaroslav@563: * @param ch the character to be tested.
jaroslav@563: * @return {@code true} if the character may be part of a
jaroslav@563: * Java identifier; {@code false} otherwise.
jaroslav@563: * @see Character#isIdentifierIgnorable(char)
jaroslav@563: * @see Character#isJavaIdentifierStart(char)
jaroslav@563: * @see Character#isLetterOrDigit(char)
jaroslav@563: * @see Character#isUnicodeIdentifierPart(char)
jaroslav@563: * @see javax.lang.model.SourceVersion#isIdentifier(CharSequence)
jaroslav@563: * @since 1.1
jaroslav@563: */
jaroslav@563: public static boolean isJavaIdentifierPart(char ch) {
jaroslav@563: return isJavaIdentifierPart((int)ch);
jaroslav@563: }
jaroslav@563:
jaroslav@563: /**
jaroslav@563: * Determines if the character (Unicode code point) may be part of a Java
jaroslav@563: * identifier as other than the first character.
jaroslav@563: *
jaroslav@563: * A character may be part of a Java identifier if any of the following
jaroslav@563: * are true:
jaroslav@563: *
jaroslav@68: * Note that
jaroslav@68: * {@code Character.isLowerCase(Character.toLowerCase(ch))}
jaroslav@68: * does not always return {@code true} for some ranges of
jaroslav@68: * characters, particularly those that are symbols or ideographs.
jaroslav@68: *
jaroslav@68: * In general, {@link String#toLowerCase()} should be used to map
jaroslav@68: * characters to lowercase. {@code String} case mapping methods
jaroslav@68: * have several benefits over {@code Character} case mapping methods.
jaroslav@68: * {@code String} case mapping methods can perform locale-sensitive
jaroslav@68: * mappings, context-sensitive mappings, and 1:M character mappings, whereas
jaroslav@68: * the {@code Character} case mapping methods cannot.
jaroslav@68: *
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #toLowerCase(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be converted.
jaroslav@68: * @return the lowercase equivalent of the character, if any;
jaroslav@68: * otherwise, the character itself.
jaroslav@68: * @see Character#isLowerCase(char)
jaroslav@68: * @see String#toLowerCase()
jaroslav@68: */
jaroslav@68: public static char toLowerCase(char ch) {
jaroslav@326: return String.valueOf(ch).toLowerCase().charAt(0);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Converts the character argument to uppercase using case mapping
jaroslav@68: * information from the UnicodeData file.
jaroslav@68: *
jaroslav@68: * Note that
jaroslav@68: * {@code Character.isUpperCase(Character.toUpperCase(ch))}
jaroslav@68: * does not always return {@code true} for some ranges of
jaroslav@68: * characters, particularly those that are symbols or ideographs.
jaroslav@68: *
jaroslav@68: * In general, {@link String#toUpperCase()} should be used to map
jaroslav@68: * characters to uppercase. {@code String} case mapping methods
jaroslav@68: * have several benefits over {@code Character} case mapping methods.
jaroslav@68: * {@code String} case mapping methods can perform locale-sensitive
jaroslav@68: * mappings, context-sensitive mappings, and 1:M character mappings, whereas
jaroslav@68: * the {@code Character} case mapping methods cannot.
jaroslav@68: *
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #toUpperCase(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be converted.
jaroslav@68: * @return the uppercase equivalent of the character, if any;
jaroslav@68: * otherwise, the character itself.
jaroslav@68: * @see Character#isUpperCase(char)
jaroslav@68: * @see String#toUpperCase()
jaroslav@68: */
jaroslav@68: public static char toUpperCase(char ch) {
jaroslav@326: return String.valueOf(ch).toUpperCase().charAt(0);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the numeric value of the character {@code ch} in the
jaroslav@68: * specified radix.
jaroslav@68: *
jaroslav@68: * If the radix is not in the range {@code MIN_RADIX} ≤
jaroslav@68: * {@code radix} ≤ {@code MAX_RADIX} or if the
jaroslav@68: * value of {@code ch} is not a valid digit in the specified
jaroslav@68: * radix, {@code -1} is returned. A character is a valid digit
jaroslav@68: * if at least one of the following is true:
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #digit(int, int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be converted.
jaroslav@68: * @param radix the radix.
jaroslav@68: * @return the numeric value represented by the character in the
jaroslav@68: * specified radix.
jaroslav@68: * @see Character#forDigit(int, int)
jaroslav@68: * @see Character#isDigit(char)
jaroslav@68: */
jaroslav@68: public static int digit(char ch, int radix) {
jaroslav@68: return digit((int)ch, radix);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the numeric value of the specified character (Unicode
jaroslav@68: * code point) in the specified radix.
jaroslav@68: *
jaroslav@68: * If the radix is not in the range {@code MIN_RADIX} ≤
jaroslav@68: * {@code radix} ≤ {@code MAX_RADIX} or if the
jaroslav@68: * character is not a valid digit in the specified
jaroslav@68: * radix, {@code -1} is returned. A character is a valid digit
jaroslav@68: * if at least one of the following is true:
jaroslav@68: *
jaroslav@68: * The letters A-Z in their uppercase ({@code '\u005Cu0041'} through
jaroslav@68: * {@code '\u005Cu005A'}), lowercase
jaroslav@68: * ({@code '\u005Cu0061'} through {@code '\u005Cu007A'}), and
jaroslav@68: * full width variant ({@code '\u005CuFF21'} through
jaroslav@68: * {@code '\u005CuFF3A'} and {@code '\u005CuFF41'} through
jaroslav@68: * {@code '\u005CuFF5A'}) forms have numeric values from 10
jaroslav@68: * through 35. This is independent of the Unicode specification,
jaroslav@68: * which does not assign numeric values to these {@code char}
jaroslav@68: * values.
jaroslav@68: *
jaroslav@68: * If the character does not have a numeric value, then -1 is returned.
jaroslav@68: * If the character has a numeric value that cannot be represented as a
jaroslav@68: * nonnegative integer (for example, a fractional value), then -2
jaroslav@68: * is returned.
jaroslav@68: *
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #getNumericValue(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be converted.
jaroslav@68: * @return the numeric value of the character, as a nonnegative {@code int}
jaroslav@68: * value; -2 if the character has a numeric value that is not a
jaroslav@68: * nonnegative integer; -1 if the character has no numeric value.
jaroslav@68: * @see Character#forDigit(int, int)
jaroslav@68: * @see Character#isDigit(char)
jaroslav@68: * @since 1.1
jaroslav@68: */
jaroslav@68: public static int getNumericValue(char ch) {
jaroslav@68: return getNumericValue((int)ch);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the {@code int} value that the specified
jaroslav@68: * character (Unicode code point) represents. For example, the character
jaroslav@68: * {@code '\u005Cu216C'} (the Roman numeral fifty) will return
jaroslav@68: * an {@code int} with a value of 50.
jaroslav@68: *
jaroslav@68: * The letters A-Z in their uppercase ({@code '\u005Cu0041'} through
jaroslav@68: * {@code '\u005Cu005A'}), lowercase
jaroslav@68: * ({@code '\u005Cu0061'} through {@code '\u005Cu007A'}), and
jaroslav@68: * full width variant ({@code '\u005CuFF21'} through
jaroslav@68: * {@code '\u005CuFF3A'} and {@code '\u005CuFF41'} through
jaroslav@68: * {@code '\u005CuFF5A'}) forms have numeric values from 10
jaroslav@68: * through 35. This is independent of the Unicode specification,
jaroslav@68: * which does not assign numeric values to these {@code char}
jaroslav@68: * values.
jaroslav@68: *
jaroslav@68: * If the character does not have a numeric value, then -1 is returned.
jaroslav@68: * If the character has a numeric value that cannot be represented as a
jaroslav@68: * nonnegative integer (for example, a fractional value), then -2
jaroslav@68: * is returned.
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be converted.
jaroslav@68: * @return the numeric value of the character, as a nonnegative {@code int}
jaroslav@68: * value; -2 if the character has a numeric value that is not a
jaroslav@68: * nonnegative integer; -1 if the character has no numeric value.
jaroslav@68: * @see Character#forDigit(int, int)
jaroslav@68: * @see Character#isDigit(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static int getNumericValue(int codePoint) {
jaroslav@85: throw new UnsupportedOperationException();
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character is ISO-LATIN-1 white space.
jaroslav@68: * This method returns {@code true} for the following five
jaroslav@68: * characters only:
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #isWhitespace(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be tested.
jaroslav@68: * @return {@code true} if the character is a Java whitespace
jaroslav@68: * character; {@code false} otherwise.
jaroslav@68: * @see Character#isSpaceChar(char)
jaroslav@68: * @since 1.1
jaroslav@68: */
jaroslav@68: public static boolean isWhitespace(char ch) {
jaroslav@68: return isWhitespace((int)ch);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character (Unicode code point) is
jaroslav@68: * white space according to Java. A character is a Java
jaroslav@68: * whitespace character if and only if it satisfies one of the
jaroslav@68: * following criteria:
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be tested.
jaroslav@68: * @return {@code true} if the character is a Java whitespace
jaroslav@68: * character; {@code false} otherwise.
jaroslav@68: * @see Character#isSpaceChar(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static boolean isWhitespace(int codePoint) {
jaroslav@85: throw new UnsupportedOperationException();
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character is an ISO control
jaroslav@68: * character. A character is considered to be an ISO control
jaroslav@68: * character if its code is in the range {@code '\u005Cu0000'}
jaroslav@68: * through {@code '\u005Cu001F'} or in the range
jaroslav@68: * {@code '\u005Cu007F'} through {@code '\u005Cu009F'}.
jaroslav@68: *
jaroslav@68: * Note: This method cannot handle supplementary characters. To support
jaroslav@68: * all Unicode characters, including supplementary characters, use
jaroslav@68: * the {@link #isISOControl(int)} method.
jaroslav@68: *
jaroslav@68: * @param ch the character to be tested.
jaroslav@68: * @return {@code true} if the character is an ISO control character;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: *
jaroslav@68: * @see Character#isSpaceChar(char)
jaroslav@68: * @see Character#isWhitespace(char)
jaroslav@68: * @since 1.1
jaroslav@68: */
jaroslav@68: public static boolean isISOControl(char ch) {
jaroslav@68: return isISOControl((int)ch);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the referenced character (Unicode code point) is an ISO control
jaroslav@68: * character. A character is considered to be an ISO control
jaroslav@68: * character if its code is in the range {@code '\u005Cu0000'}
jaroslav@68: * through {@code '\u005Cu001F'} or in the range
jaroslav@68: * {@code '\u005Cu007F'} through {@code '\u005Cu009F'}.
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be tested.
jaroslav@68: * @return {@code true} if the character is an ISO control character;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isSpaceChar(int)
jaroslav@68: * @see Character#isWhitespace(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static boolean isISOControl(int codePoint) {
jaroslav@68: // Optimized form of:
jaroslav@68: // (codePoint >= 0x00 && codePoint <= 0x1F) ||
jaroslav@68: // (codePoint >= 0x7F && codePoint <= 0x9F);
jaroslav@68: return codePoint <= 0x9F &&
jaroslav@68: (codePoint >= 0x7F || (codePoint >>> 5 == 0));
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines the character representation for a specific digit in
jaroslav@68: * the specified radix. If the value of {@code radix} is not a
jaroslav@68: * valid radix, or the value of {@code digit} is not a valid
jaroslav@68: * digit in the specified radix, the null character
jaroslav@68: * ({@code '\u005Cu0000'}) is returned.
jaroslav@68: *
jaroslav@68: * The {@code radix} argument is valid if it is greater than or
jaroslav@68: * equal to {@code MIN_RADIX} and less than or equal to
jaroslav@68: * {@code MAX_RADIX}. The {@code digit} argument is valid if
jaroslav@68: * {@code 0 <= digit < radix}.
jaroslav@68: *
jaroslav@68: * If the digit is less than 10, then
jaroslav@68: * {@code '0' + digit} is returned. Otherwise, the value
jaroslav@68: * {@code 'a' + digit - 10} is returned.
jaroslav@68: *
jaroslav@68: * @param digit the number to convert to a character.
jaroslav@68: * @param radix the radix.
jaroslav@68: * @return the {@code char} representation of the specified digit
jaroslav@68: * in the specified radix.
jaroslav@68: * @see Character#MIN_RADIX
jaroslav@68: * @see Character#MAX_RADIX
jaroslav@68: * @see Character#digit(char, int)
jaroslav@68: */
jaroslav@68: public static char forDigit(int digit, int radix) {
jaroslav@68: if ((digit >= radix) || (digit < 0)) {
jaroslav@68: return '\0';
jaroslav@68: }
jaroslav@68: if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX)) {
jaroslav@68: return '\0';
jaroslav@68: }
jaroslav@68: if (digit < 10) {
jaroslav@68: return (char)('0' + digit);
jaroslav@68: }
jaroslav@68: return (char)('a' - 10 + digit);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Compares two {@code Character} objects numerically.
jaroslav@68: *
jaroslav@68: * @param anotherCharacter the {@code Character} to be compared.
jaroslav@68:
jaroslav@68: * @return the value {@code 0} if the argument {@code Character}
jaroslav@68: * is equal to this {@code Character}; a value less than
jaroslav@68: * {@code 0} if this {@code Character} is numerically less
jaroslav@68: * than the {@code Character} argument; and a value greater than
jaroslav@68: * {@code 0} if this {@code Character} is numerically greater
jaroslav@68: * than the {@code Character} argument (unsigned comparison).
jaroslav@68: * Note that this is strictly a numerical comparison; it is not
jaroslav@68: * locale-dependent.
jaroslav@68: * @since 1.2
jaroslav@68: */
jaroslav@68: public int compareTo(Character anotherCharacter) {
jaroslav@68: return compare(this.value, anotherCharacter.value);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Compares two {@code char} values numerically.
jaroslav@68: * The value returned is identical to what would be returned by:
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * @param high the high-surrogate code value to be tested
jaroslav@68: * @param low the low-surrogate code value to be tested
jaroslav@68: * @return {@code true} if the specified high and
jaroslav@68: * low-surrogate code values represent a valid surrogate pair;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static boolean isSurrogatePair(char high, char low) {
jaroslav@68: return isHighSurrogate(high) && isLowSurrogate(low);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines the number of {@code char} values needed to
jaroslav@68: * represent the specified character (Unicode code point). If the
jaroslav@68: * specified character is equal to or greater than 0x10000, then
jaroslav@68: * the method returns 2. Otherwise, the method returns 1.
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * isHighSurrogate(high) && isLowSurrogate(low)
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * 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
jaroslav@68: * '\u00DF' '\u00E0' '\u00E1' '\u00E2' '\u00E3' '\u00E4' '\u00E5' '\u00E6'
jaroslav@68: * '\u00E7' '\u00E8' '\u00E9' '\u00EA' '\u00EB' '\u00EC' '\u00ED' '\u00EE'
jaroslav@68: * '\u00EF' '\u00F0' '\u00F1' '\u00F2' '\u00F3' '\u00F4' '\u00F5' '\u00F6'
jaroslav@68: * '\u00F8' '\u00F9' '\u00FA' '\u00FB' '\u00FC' '\u00FD' '\u00FE' '\u00FF'
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * 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
jaroslav@68: * '\u00C0' '\u00C1' '\u00C2' '\u00C3' '\u00C4' '\u00C5' '\u00C6' '\u00C7'
jaroslav@68: * '\u00C8' '\u00C9' '\u00CA' '\u00CB' '\u00CC' '\u00CD' '\u00CE' '\u00CF'
jaroslav@68: * '\u00D0' '\u00D1' '\u00D2' '\u00D3' '\u00D4' '\u00D5' '\u00D6' '\u00D8'
jaroslav@68: * '\u00D9' '\u00DA' '\u00DB' '\u00DC' '\u00DD' '\u00DE'
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * Many other character ranges contain digits as well.
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * Many other character ranges contain digits as well.
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be tested.
jaroslav@68: * @return {@code true} if the character is a digit;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#forDigit(int, int)
jaroslav@68: * @see Character#getType(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static boolean isDigit(int codePoint) {
jaroslav@326: return fromCodeChars(codePoint).matches("\\d");
jaroslav@68: }
jaroslav@326:
jaroslav@326: @JavaScriptBody(args = "c", body = "return String.fromCharCode(c);")
jaroslav@326: private native static String fromCodeChars(int codePoint);
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if a character is defined in Unicode.
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be tested.
jaroslav@68: * @return {@code true} if the character has a defined meaning
jaroslav@68: * in Unicode; {@code false} otherwise.
jaroslav@68: * @see Character#isDigit(int)
jaroslav@68: * @see Character#isLetter(int)
jaroslav@68: * @see Character#isLetterOrDigit(int)
jaroslav@68: * @see Character#isLowerCase(int)
jaroslav@68: * @see Character#isTitleCase(int)
jaroslav@68: * @see Character#isUpperCase(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static boolean isDefined(int codePoint) {
jaroslav@68: return getType(codePoint) != Character.UNASSIGNED;
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character is a letter.
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * Not all letters have case. Many characters are
jaroslav@68: * letters but are neither uppercase nor lowercase nor titlecase.
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * Not all letters have case. Many characters are
jaroslav@68: * letters but are neither uppercase nor lowercase nor titlecase.
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be tested.
jaroslav@68: * @return {@code true} if the character is a letter;
jaroslav@68: * {@code false} otherwise.
jaroslav@68: * @see Character#isDigit(int)
jaroslav@68: * @see Character#isJavaIdentifierStart(int)
jaroslav@68: * @see Character#isLetterOrDigit(int)
jaroslav@68: * @see Character#isLowerCase(int)
jaroslav@68: * @see Character#isTitleCase(int)
jaroslav@68: * @see Character#isUnicodeIdentifierStart(int)
jaroslav@68: * @see Character#isUpperCase(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static boolean isLetter(int codePoint) {
jaroslav@326: return fromCodeChars(codePoint).matches("\\w") && !isDigit(codePoint);
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character is a letter or digit.
jaroslav@68: *
jaroslav@563: *
jaroslav@563: *
jaroslav@563: *
jaroslav@563: *
jaroslav@563: *
jaroslav@563: * @param codePoint the character (Unicode code point) to be tested.
jaroslav@563: * @return {@code true} if the character may start a Java identifier;
jaroslav@563: * {@code false} otherwise.
jaroslav@563: * @see Character#isJavaIdentifierPart(int)
jaroslav@563: * @see Character#isLetter(int)
jaroslav@563: * @see Character#isUnicodeIdentifierStart(int)
jaroslav@563: * @see javax.lang.model.SourceVersion#isIdentifier(CharSequence)
jaroslav@563: * @since 1.5
jaroslav@563: */
jaroslav@563: public static boolean isJavaIdentifierStart(int codePoint) {
jaroslav@563: return
jaroslav@563: ('A' <= codePoint && codePoint <= 'Z') ||
jaroslav@563: ('a' <= codePoint && codePoint <= 'z');
jaroslav@563: }
jaroslav@563:
jaroslav@563: /**
jaroslav@563: * Determines if the specified character may be part of a Java
jaroslav@563: * identifier as other than the first character.
jaroslav@563: *
jaroslav@563: *
jaroslav@563: *
jaroslav@563: *
jaroslav@563: *
jaroslav@563: *
jaroslav@563: * @param codePoint the character (Unicode code point) to be tested.
jaroslav@563: * @return {@code true} if the character may be part of a
jaroslav@563: * Java identifier; {@code false} otherwise.
jaroslav@563: * @see Character#isIdentifierIgnorable(int)
jaroslav@563: * @see Character#isJavaIdentifierStart(int)
jaroslav@563: * @see Character#isLetterOrDigit(int)
jaroslav@563: * @see Character#isUnicodeIdentifierPart(int)
jaroslav@563: * @see javax.lang.model.SourceVersion#isIdentifier(CharSequence)
jaroslav@563: * @since 1.5
jaroslav@563: */
jaroslav@563: public static boolean isJavaIdentifierPart(int codePoint) {
jaroslav@563: return isJavaIdentifierStart(codePoint) ||
jaroslav@590: ('0' <= codePoint && codePoint <= '9') || codePoint == '$';
jaroslav@563: }
jaroslav@563:
jaroslav@68: /**
jaroslav@68: * Converts the character argument to lowercase using case
jaroslav@68: * mapping information from the UnicodeData file.
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * @param codePoint the character (Unicode code point) to be converted.
jaroslav@68: * @param radix the radix.
jaroslav@68: * @return the numeric value represented by the character in the
jaroslav@68: * specified radix.
jaroslav@68: * @see Character#forDigit(int, int)
jaroslav@68: * @see Character#isDigit(int)
jaroslav@68: * @since 1.5
jaroslav@68: */
Martin@594: @JavaScriptBody(args = { "codePoint", "radix" }, body=
Martin@594: "var x = parseInt(String.fromCharCode(codePoint), radix);\n"
Martin@594: + "return isNaN(x) ? -1 : x;"
Martin@594: )
jaroslav@68: public static int digit(int codePoint, int radix) {
jaroslav@85: throw new UnsupportedOperationException();
jaroslav@68: }
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the {@code int} value that the specified Unicode
jaroslav@68: * character represents. For example, the character
jaroslav@68: * {@code '\u005Cu216C'} (the roman numeral fifty) will return
jaroslav@68: * an int with a value of 50.
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * @param ch the character to be tested.
jaroslav@68: * @return {@code true} if the character is ISO-LATIN-1 white
jaroslav@68: * space; {@code false} otherwise.
jaroslav@68: * @see Character#isSpaceChar(char)
jaroslav@68: * @see Character#isWhitespace(char)
jaroslav@68: * @deprecated Replaced by isWhitespace(char).
jaroslav@68: */
jaroslav@68: @Deprecated
jaroslav@68: public static boolean isSpace(char ch) {
jaroslav@68: return (ch <= 0x0020) &&
jaroslav@68: (((((1L << 0x0009) |
jaroslav@68: (1L << 0x000A) |
jaroslav@68: (1L << 0x000C) |
jaroslav@68: (1L << 0x000D) |
jaroslav@68: (1L << 0x0020)) >> ch) & 1L) != 0);
jaroslav@68: }
jaroslav@68:
jaroslav@68:
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Determines if the specified character is white space according to Java.
jaroslav@68: * A character is a Java whitespace character if and only if it satisfies
jaroslav@68: * one of the following criteria:
jaroslav@68: *
jaroslav@68: * {@code '\t'} {@code U+0009}
jaroslav@68: * {@code HORIZONTAL TABULATION}
jaroslav@68: * {@code '\n'} {@code U+000A}
jaroslav@68: * {@code NEW LINE}
jaroslav@68: * {@code '\f'} {@code U+000C}
jaroslav@68: * {@code FORM FEED}
jaroslav@68: * {@code '\r'} {@code U+000D}
jaroslav@68: * {@code CARRIAGE RETURN}
jaroslav@68: * {@code ' '} {@code U+0020}
jaroslav@68: * {@code SPACE}
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * Character.valueOf(x).compareTo(Character.valueOf(y))
jaroslav@68: *
jaroslav@68: *
jaroslav@68: * @param x the first {@code char} to compare
jaroslav@68: * @param y the second {@code char} to compare
jaroslav@68: * @return the value {@code 0} if {@code x == y};
jaroslav@68: * a value less than {@code 0} if {@code x < y}; and
jaroslav@68: * a value greater than {@code 0} if {@code x > y}
jaroslav@68: * @since 1.7
jaroslav@68: */
jaroslav@68: public static int compare(char x, char y) {
jaroslav@68: return x - y;
jaroslav@68: }
jaroslav@68:
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * The number of bits used to represent a char value in unsigned
jaroslav@68: * binary form, constant {@code 16}.
jaroslav@68: *
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static final int SIZE = 16;
jaroslav@68:
jaroslav@68: /**
jaroslav@68: * Returns the value obtained by reversing the order of the bytes in the
jaroslav@68: * specified char value.
jaroslav@68: *
jaroslav@68: * @return the value obtained by reversing (or, equivalently, swapping)
jaroslav@68: * the bytes in the specified char value.
jaroslav@68: * @since 1.5
jaroslav@68: */
jaroslav@68: public static char reverseBytes(char ch) {
jaroslav@68: return (char) (((ch & 0xFF00) >> 8) | (ch << 8));
jaroslav@68: }
jaroslav@68:
jaroslav@68: }