character.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 2,178 行 · 第 1/4 页

JAVA
2,178
字号
	/**
	 * Lo = Letter, Other (Informative).
	 *
	 * @since 1.1
	 */
	public static final byte OTHER_LETTER = 5;

	/**
	 * Pc = Punctuation, Connector (Informative).
	 *
	 * @since 1.1
	 */
	public static final byte CONNECTOR_PUNCTUATION = 23;

	/**
	 * Pd = Punctuation, Dash (Informative).
	 *
	 * @since 1.1
	 */
	public static final byte DASH_PUNCTUATION = 20;

	/**
	 * Ps = Punctuation, Open (Informative).
	 *
	 * @since 1.1
	 */
	public static final byte START_PUNCTUATION = 21;

	/**
	 * Pe = Punctuation, Close (Informative).
	 *
	 * @since 1.1
	 */
	public static final byte END_PUNCTUATION = 22;

	/**
	 * Pi = Punctuation, Initial Quote (Informative).
	 *
	 * @since 1.4
	 */
	public static final byte INITIAL_QUOTE_PUNCTUATION = 29;

	/**
	 * Pf = Punctuation, Final Quote (Informative).
	 *
	 * @since 1.4
	 */
	public static final byte FINAL_QUOTE_PUNCTUATION = 30;

	/**
	 * Po = Punctuation, Other (Informative).
	 *
	 * @since 1.1
	 */
	public static final byte OTHER_PUNCTUATION = 24;

	/**
	 * Sm = Symbol, Math (Informative).
	 *
	 * @since 1.1
	 */
	public static final byte MATH_SYMBOL = 25;

	/**
	 * Sc = Symbol, Currency (Informative).
	 *
	 * @since 1.1
	 */
	public static final byte CURRENCY_SYMBOL = 26;

	/**
	 * Sk = Symbol, Modifier (Informative).
	 *
	 * @since 1.1
	 */
	public static final byte MODIFIER_SYMBOL = 27;

	/**
	 * So = Symbol, Other (Informative).
	 *
	 * @since 1.1
	 */
	public static final byte OTHER_SYMBOL = 28;

	/**
	 * Undefined bidirectional character type. Undefined char values have
	 * undefined directionality in the Unicode specification.
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_UNDEFINED = -1;

	/**
	 * Strong bidirectional character type "L".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_LEFT_TO_RIGHT = 0;

	/**
	 * Strong bidirectional character type "R".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_RIGHT_TO_LEFT = 1;

	/**
	 * Strong bidirectional character type "AL".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC = 2;

	/**
	 * Weak bidirectional character type "EN".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_EUROPEAN_NUMBER = 3;

	/**
	 * Weak bidirectional character type "ES".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR = 4;

	/**
	 * Weak bidirectional character type "ET".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR = 5;

	/**
	 * Weak bidirectional character type "AN".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_ARABIC_NUMBER = 6;

	/**
	 * Weak bidirectional character type "CS".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_COMMON_NUMBER_SEPARATOR = 7;

	/**
	 * Weak bidirectional character type "NSM".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_NONSPACING_MARK = 8;

	/**
	 * Weak bidirectional character type "BN".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_BOUNDARY_NEUTRAL = 9;

	/**
	 * Neutral bidirectional character type "B".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_PARAGRAPH_SEPARATOR = 10;

	/**
	 * Neutral bidirectional character type "S".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_SEGMENT_SEPARATOR = 11;

	/**
	 * Strong bidirectional character type "WS".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_WHITESPACE = 12;

	/**
	 * Neutral bidirectional character type "ON".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_OTHER_NEUTRALS = 13;

	/**
	 * Strong bidirectional character type "LRE".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING = 14;

	/**
	 * Strong bidirectional character type "LRO".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE = 15;

	/**
	 * Strong bidirectional character type "RLE".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING = 16;

	/**
	 * Strong bidirectional character type "RLO".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE = 17;

	/**
	 * Weak bidirectional character type "PDF".
	 *
	 * @since 1.4
	 */
	public static final byte DIRECTIONALITY_POP_DIRECTIONAL_FORMAT = 18;

	/**
	 * Stores unicode block offset lookup table. Exploit package visibility of
	 * String.value to avoid copying the array.
	 * @see #readChar(char)
	 * @see CharData#BLOCKS
	 */
	private static final char[] blocks =
		String.zeroBasedStringValue(CharData.BLOCKS);

	/**
	 * Stores unicode attribute offset lookup table. Exploit package visibility
	 * of String.value to avoid copying the array.
	 * @see CharData#DATA
	 */
	private static final char[] data =
		String.zeroBasedStringValue(CharData.DATA);

	/**
	 * Stores unicode numeric value attribute table. Exploit package visibility
	 * of String.value to avoid copying the array.
	 * @see CharData#NUM_VALUE
	 */
	private static final char[] numValue =
		String.zeroBasedStringValue(CharData.NUM_VALUE);

	/**
	 * Stores unicode uppercase attribute table. Exploit package visibility
	 * of String.value to avoid copying the array.
	 * @see CharData#UPPER
	 */
	private static final char[] upper =
		String.zeroBasedStringValue(CharData.UPPER);

	/**
	 * Stores unicode lowercase attribute table. Exploit package visibility
	 * of String.value to avoid copying the array.
	 * @see CharData#LOWER
	 */
	private static final char[] lower =
		String.zeroBasedStringValue(CharData.LOWER);

	/**
	 * Stores unicode direction attribute table. Exploit package visibility
	 * of String.value to avoid copying the array.
	 * @see CharData#DIRECTION
	 */
	// Package visible for use by String.
	static final char[] direction =
		String.zeroBasedStringValue(CharData.DIRECTION);

	/**
	 * Stores unicode titlecase table. Exploit package visibility of
	 * String.value to avoid copying the array.
	 * @see CharData#TITLE
	 */
	private static final char[] title =
		String.zeroBasedStringValue(CharData.TITLE);

	/**
	 * Mask for grabbing the type out of the contents of data.
	 * @see CharData#DATA
	 */
	private static final int TYPE_MASK = 0x1F;

	/**
	 * Mask for grabbing the non-breaking space flag out of the contents of
	 * data.
	 * @see CharData#DATA
	 */
	private static final int NO_BREAK_MASK = 0x20;

	/**
	 * Mask for grabbing the mirrored directionality flag out of the contents
	 * of data.
	 * @see CharData#DATA
	 */
	private static final int MIRROR_MASK = 0x40;

	/**
	 * Grabs an attribute offset from the Unicode attribute database. The lower
	 * 5 bits are the character type, the next 2 bits are flags, and the top
	 * 9 bits are the offset into the attribute tables.
	 *
	 * @param ch the character to look up
	 * @return the character's attribute offset and type
	 * @see #TYPE_MASK
	 * @see #NO_BREAK_MASK
	 * @see #MIRROR_MASK
	 * @see CharData#DATA
	 * @see CharData#SHIFT
	 */
	// Package visible for use in String.
	static char readChar(char ch) {
		// Perform 16-bit addition to find the correct entry in data.
		return data[(char) (blocks[ch >> CharData.SHIFT] + ch)];
	}

	/**
	 * Wraps up a character.
	 *
	 * @param value the character to wrap
	 */
	public Character(char value) {
		this.value = value;
	}

	/**
	 * Returns the character which has been wrapped by this class.
	 *
	 * @return the character wrapped
	 */
	public char charValue() {
		return value;
	}

	/**
	 * Returns the numerical value (unsigned) of the wrapped character.
	 * Range of returned values: 0x0000-0xFFFF.
	 *
	 * @return the value of the wrapped character
	 */
	public int hashCode() {
		return value;
	}

	/**
	 * Determines if an object is equal to this object. This is only true for
	 * another Character object wrapping the same value.
	 *
	 * @param o object to compare
	 * @return true if o is a Character with the same value
	 */
	public boolean equals(Object o) {
		return o instanceof Character && value == ((Character) o).value;
	}

	/**
	 * Converts the wrapped character into a String.
	 *
	 * @return a String containing one character -- the wrapped character
	 *         of this instance
	 */
	public String toString() {
		// Package constructor avoids an array copy.
		return new String(new char[] { value }, 0, 1, true);
	}

	/**
	 * Returns a String of length 1 representing the specified character.
	 *
	 * @param ch the character to convert
	 * @return a String containing the character
	 * @since 1.4
	 */
	public static String toString(char ch) {
		// Package constructor avoids an array copy.
		return new String(new char[] { ch }, 0, 1, true);
	}

	/**
	 * Determines if a character is a Unicode lowercase letter. For example,
	 * <code>'a'</code> is lowercase.
	 * <br>
	 * lowercase = [Ll]
	 *
	 * @param ch character to test
	 * @return true if ch is a Unicode lowercase letter, else false
	 * @see #isUpperCase(char)
	 * @see #isTitleCase(char)
	 * @see #toLowerCase(char)
	 * @see #getType(char)
	 */
	public static boolean isLowerCase(char ch) {
		return getType(ch) == LOWERCASE_LETTER;
	}

	/**
	 * Determines if a character is a Unicode uppercase letter. For example,
	 * <code>'A'</code> is uppercase.
	 * <br>
	 * uppercase = [Lu]
	 *
	 * @param ch character to test
	 * @return true if ch is a Unicode uppercase letter, else false
	 * @see #isLowerCase(char)
	 * @see #isTitleCase(char)
	 * @see #toUpperCase(char)
	 * @see #getType(char)
	 */
	public static boolean isUpperCase(char ch) {
		return getType(ch) == UPPERCASE_LETTER;
	}

	/**
	 * Determines if a character is a Unicode titlecase letter. For example,
	 * the character "Lj" (Latin capital L with small letter j) is titlecase.
	 * <br>
	 * titlecase = [Lt]
	 *
	 * @param ch character to test
	 * @return true if ch is a Unicode titlecase letter, else false
	 * @see #isLowerCase(char)
	 * @see #isUpperCase(char)
	 * @see #toTitleCase(char)
	 * @see #getType(char)
	 */
	public static boolean isTitleCase(char ch) {
		return getType(ch) == TITLECASE_LETTER;
	}

	/**
	 * Determines if a character is a Unicode decimal digit. For example,
	 * <code>'0'</code> is a digit.
	 * <br>
	 * Unicode decimal digit = [Nd]
	 *
	 * @param ch character to test
	 * @return true if ch is a Unicode decimal digit, else false
	 * @see #digit(char, int)
	 * @see #forDigit(int, int)
	 * @see #getType(char)
	 */
	public static boolean isDigit(char ch) {
		return getType(ch) == DECIMAL_DIGIT_NUMBER;
	}

	/**
	 * Determines if a character is part of the Unicode Standard. This is an
	 * evolving standard, but covers every character in the data file.
	 * <br>
	 * defined = not [Cn]
	 *
	 * @param ch character to test
	 * @return true if ch is a Unicode character, else false
	 * @see #isDigit(char)
	 * @see #isLetter(char)
	 * @see #isLetterOrDigit(char)
	 * @see #isLowerCase(char)
	 * @see #isTitleCase(char)
	 * @see #isUpperCase(char)
	 */
	public static boolean isDefined(char ch) {
		return getType(ch) != UNASSIGNED;
	}

	/**
	 * Determines if a character is a Unicode letter. Not all letters have case,
	 * so this may return true when isLowerCase and isUpperCase return false.
	 * <br>
	 * letter = [Lu]|[Ll]|[Lt]|[Lm]|[Lo]
	 *
	 * @param ch character to test
	 * @return true if ch is a Unicode letter, else false
	 * @see #isDigit(char)
	 * @see #isJavaIdentifierStart(char)
	 * @see #isJavaLetter(char)
	 * @see #isJavaLetterOrDigit(char)
	 * @see #isLetterOrDigit(char)
	 * @see #isLowerCase(char)
	 * @see #isTitleCase(char)
	 * @see #isUnicodeIdentifierStart(char)
	 * @see #isUpperCase(char)
	 */
	public static boolean isLetter(char ch) {
		return (
			(1 << getType(ch))
				& ((1 << UPPERCASE_LETTER)
					| (1 << LOWERCASE_LETTER)
					| (1 << TITLECASE_LETTER)
					| (1 << MODIFIER_LETTER)
					| (1 << OTHER_LETTER)))
			!= 0;
	}

	/**
	 * Determines if a character is a Unicode letter or a Unicode digit. This
	 * is the combination of isLetter and isDigit.
	 * <br>
	 * letter or digit = [Lu]|[Ll]|[Lt]|[Lm]|[Lo]|[Nd]
	 *
	 * @param ch character to test
	 * @return true if ch is a Unicode letter or a Unicode digit, else false
	 * @see #isDigit(char)
	 * @see #isJavaIdentifierPart(char)
	 * @see #isJavaLetter(char)
	 * @see #isJavaLetterOrDigit(char)
	 * @see #isLetter(char)
	 * @see #isUnicodeIdentifierPart(char)
	 */
	public static boolean isLetterOrDigit(char ch) {
		return (
			(1 << getType(ch))
				& ((1 << UPPERCASE_LETTER)
					| (1 << LOWERCASE_LETTER)
					| (1 << TITLECASE_LETTER)
					| (1 << MODIFIER_LETTER)
					| (1 << OTHER_LETTER)
					| (1 << DECIMAL_DIGIT_NUMBER)))
			!= 0;
	}

	/**
	 * Determines if a character can start a Java identifier. This is the
	 * combination of isLetter, any character where getType returns
	 * LETTER_NUMBER, currency symbols (like '$'), and connecting punctuation
	 * (like '_').
	 *
	 * @param ch character to test
	 * @return true if ch can start a Java identifier, else false
	 * @deprecated Replaced by {@link #isJavaIdentifierStart(char)}
	 * @see #isJavaLetterOrDigit(char)
	 * @see #isJavaIdentifierStart(char)
	 * @see #isJavaIdentifierPart(char)
	 * @see #isLetter(char)
	 * @see #isLetterOrDigit(char)
	 * @see #isUnicodeIdentifierStart(char)
	 */
	public static boolean isJavaLetter(char ch) {
		return isJavaIdentifierStart(ch);
	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?