Commonize CharCategory and related functions #KT-39177 #KT-43216 #KT-39906 #KT-30652
This commit is contained in:
+46
-42
@@ -1,173 +1,177 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.text
|
||||
|
||||
/**
|
||||
* Represents the character general category in the Unicode specification.
|
||||
*/
|
||||
public enum class CharCategory(public val value: Int, public val code: String) {
|
||||
public actual enum class CharCategory(public val value: Int, public actual val code: String) {
|
||||
/**
|
||||
* General category "Cn" in the Unicode specification.
|
||||
*/
|
||||
UNASSIGNED(Character.UNASSIGNED.toInt(), "Cn"),
|
||||
UNASSIGNED(0, "Cn"),
|
||||
|
||||
/**
|
||||
* General category "Lu" in the Unicode specification.
|
||||
*/
|
||||
UPPERCASE_LETTER(Character.UPPERCASE_LETTER.toInt(), "Lu"),
|
||||
UPPERCASE_LETTER(1, "Lu"),
|
||||
|
||||
/**
|
||||
* General category "Ll" in the Unicode specification.
|
||||
*/
|
||||
LOWERCASE_LETTER(Character.LOWERCASE_LETTER.toInt(), "Ll"),
|
||||
LOWERCASE_LETTER(2, "Ll"),
|
||||
|
||||
/**
|
||||
* General category "Lt" in the Unicode specification.
|
||||
*/
|
||||
TITLECASE_LETTER(Character.TITLECASE_LETTER.toInt(), "Lt"),
|
||||
TITLECASE_LETTER(3, "Lt"),
|
||||
|
||||
/**
|
||||
* General category "Lm" in the Unicode specification.
|
||||
*/
|
||||
MODIFIER_LETTER(Character.MODIFIER_LETTER.toInt(), "Lm"),
|
||||
MODIFIER_LETTER(4, "Lm"),
|
||||
|
||||
/**
|
||||
* General category "Lo" in the Unicode specification.
|
||||
*/
|
||||
OTHER_LETTER(Character.OTHER_LETTER.toInt(), "Lo"),
|
||||
OTHER_LETTER(5, "Lo"),
|
||||
|
||||
/**
|
||||
* General category "Mn" in the Unicode specification.
|
||||
*/
|
||||
NON_SPACING_MARK(Character.NON_SPACING_MARK.toInt(), "Mn"),
|
||||
NON_SPACING_MARK(6, "Mn"),
|
||||
|
||||
/**
|
||||
* General category "Me" in the Unicode specification.
|
||||
*/
|
||||
ENCLOSING_MARK(Character.ENCLOSING_MARK.toInt(), "Me"),
|
||||
ENCLOSING_MARK(7, "Me"),
|
||||
|
||||
/**
|
||||
* General category "Mc" in the Unicode specification.
|
||||
*/
|
||||
COMBINING_SPACING_MARK(Character.COMBINING_SPACING_MARK.toInt(), "Mc"),
|
||||
COMBINING_SPACING_MARK(8, "Mc"),
|
||||
|
||||
/**
|
||||
* General category "Nd" in the Unicode specification.
|
||||
*/
|
||||
DECIMAL_DIGIT_NUMBER(Character.DECIMAL_DIGIT_NUMBER.toInt(), "Nd"),
|
||||
DECIMAL_DIGIT_NUMBER(9, "Nd"),
|
||||
|
||||
/**
|
||||
* General category "Nl" in the Unicode specification.
|
||||
*/
|
||||
LETTER_NUMBER(Character.LETTER_NUMBER.toInt(), "Nl"),
|
||||
LETTER_NUMBER(10, "Nl"),
|
||||
|
||||
/**
|
||||
* General category "No" in the Unicode specification.
|
||||
*/
|
||||
OTHER_NUMBER(Character.OTHER_NUMBER.toInt(), "No"),
|
||||
OTHER_NUMBER(11, "No"),
|
||||
|
||||
/**
|
||||
* General category "Zs" in the Unicode specification.
|
||||
*/
|
||||
SPACE_SEPARATOR(Character.SPACE_SEPARATOR.toInt(), "Zs"),
|
||||
SPACE_SEPARATOR(12, "Zs"),
|
||||
|
||||
/**
|
||||
* General category "Zl" in the Unicode specification.
|
||||
*/
|
||||
LINE_SEPARATOR(Character.LINE_SEPARATOR.toInt(), "Zl"),
|
||||
LINE_SEPARATOR(13, "Zl"),
|
||||
|
||||
/**
|
||||
* General category "Zp" in the Unicode specification.
|
||||
*/
|
||||
PARAGRAPH_SEPARATOR(Character.PARAGRAPH_SEPARATOR.toInt(), "Zp"),
|
||||
PARAGRAPH_SEPARATOR(14, "Zp"),
|
||||
|
||||
/**
|
||||
* General category "Cc" in the Unicode specification.
|
||||
*/
|
||||
CONTROL(Character.CONTROL.toInt(), "Cc"),
|
||||
CONTROL(15, "Cc"),
|
||||
|
||||
/**
|
||||
* General category "Cf" in the Unicode specification.
|
||||
*/
|
||||
FORMAT(Character.FORMAT.toInt(), "Cf"),
|
||||
FORMAT(16, "Cf"),
|
||||
|
||||
/**
|
||||
* General category "Co" in the Unicode specification.
|
||||
*/
|
||||
PRIVATE_USE(Character.PRIVATE_USE.toInt(), "Co"),
|
||||
PRIVATE_USE(18, "Co"),
|
||||
|
||||
/**
|
||||
* General category "Cs" in the Unicode specification.
|
||||
*/
|
||||
SURROGATE(Character.SURROGATE.toInt(), "Cs"),
|
||||
SURROGATE(19, "Cs"),
|
||||
|
||||
/**
|
||||
* General category "Pd" in the Unicode specification.
|
||||
*/
|
||||
DASH_PUNCTUATION(Character.DASH_PUNCTUATION.toInt(), "Pd"),
|
||||
DASH_PUNCTUATION(20, "Pd"),
|
||||
|
||||
/**
|
||||
* General category "Ps" in the Unicode specification.
|
||||
*/
|
||||
START_PUNCTUATION(Character.START_PUNCTUATION.toInt(), "Ps"),
|
||||
START_PUNCTUATION(21, "Ps"),
|
||||
|
||||
/**
|
||||
* General category "Pe" in the Unicode specification.
|
||||
*/
|
||||
END_PUNCTUATION(Character.END_PUNCTUATION.toInt(), "Pe"),
|
||||
END_PUNCTUATION(22, "Pe"),
|
||||
|
||||
/**
|
||||
* General category "Pc" in the Unicode specification.
|
||||
*/
|
||||
CONNECTOR_PUNCTUATION(Character.CONNECTOR_PUNCTUATION.toInt(), "Pc"),
|
||||
CONNECTOR_PUNCTUATION(23, "Pc"),
|
||||
|
||||
/**
|
||||
* General category "Po" in the Unicode specification.
|
||||
*/
|
||||
OTHER_PUNCTUATION(Character.OTHER_PUNCTUATION.toInt(), "Po"),
|
||||
OTHER_PUNCTUATION(24, "Po"),
|
||||
|
||||
/**
|
||||
* General category "Sm" in the Unicode specification.
|
||||
*/
|
||||
MATH_SYMBOL(Character.MATH_SYMBOL.toInt(), "Sm"),
|
||||
MATH_SYMBOL(25, "Sm"),
|
||||
|
||||
/**
|
||||
* General category "Sc" in the Unicode specification.
|
||||
*/
|
||||
CURRENCY_SYMBOL(Character.CURRENCY_SYMBOL.toInt(), "Sc"),
|
||||
CURRENCY_SYMBOL(26, "Sc"),
|
||||
|
||||
/**
|
||||
* General category "Sk" in the Unicode specification.
|
||||
*/
|
||||
MODIFIER_SYMBOL(Character.MODIFIER_SYMBOL.toInt(), "Sk"),
|
||||
MODIFIER_SYMBOL(27, "Sk"),
|
||||
|
||||
/**
|
||||
* General category "So" in the Unicode specification.
|
||||
*/
|
||||
OTHER_SYMBOL(Character.OTHER_SYMBOL.toInt(), "So"),
|
||||
OTHER_SYMBOL(28, "So"),
|
||||
|
||||
/**
|
||||
* General category "Pi" in the Unicode specification.
|
||||
*/
|
||||
INITIAL_QUOTE_PUNCTUATION(Character.INITIAL_QUOTE_PUNCTUATION.toInt(), "Pi"),
|
||||
INITIAL_QUOTE_PUNCTUATION(29, "Pi"),
|
||||
|
||||
/**
|
||||
* General category "Pf" in the Unicode specification.
|
||||
*/
|
||||
FINAL_QUOTE_PUNCTUATION(Character.FINAL_QUOTE_PUNCTUATION.toInt(), "Pf");
|
||||
FINAL_QUOTE_PUNCTUATION(30, "Pf");
|
||||
|
||||
/**
|
||||
* Returns `true` if [char] character belongs to this category.
|
||||
*/
|
||||
public operator fun contains(char: Char): Boolean = Character.getType(char) == this.value
|
||||
public actual operator fun contains(char: Char): Boolean = Character.getType(char) == this.value
|
||||
|
||||
|
||||
public companion object {
|
||||
private val categoryMap by lazy { CharCategory.values().associateBy { it.value } }
|
||||
|
||||
public fun valueOf(category: Int): CharCategory = categoryMap[category] ?: throw IllegalArgumentException("Category #$category is not defined.")
|
||||
companion object {
|
||||
/**
|
||||
* Returns the [CharCategory] corresponding to the specified [category] that represents a Java general category constant.
|
||||
*
|
||||
* @throws IllegalArgumentException if the [category] does not represent a Java general category constant.
|
||||
*/
|
||||
public fun valueOf(category: Int): CharCategory =
|
||||
when (category) {
|
||||
in 0..16 -> values()[category]
|
||||
in 18..30 -> values()[category - 1]
|
||||
else -> throw IllegalArgumentException("Category #$category is not defined.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,32 +10,51 @@ package kotlin.text
|
||||
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* Returns the Unicode general category of this character.
|
||||
*/
|
||||
public actual val Char.category: CharCategory
|
||||
get() = CharCategory.valueOf(Character.getType(this))
|
||||
|
||||
/**
|
||||
* Returns `true` if this character (Unicode code point) is defined in Unicode.
|
||||
*
|
||||
* A character is considered to be defined in Unicode if its [category] is not [CharCategory.UNASSIGNED].
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun Char.isDefined(): Boolean = Character.isDefined(this)
|
||||
public actual inline fun Char.isDefined(): Boolean = Character.isDefined(this)
|
||||
|
||||
/**
|
||||
* Returns `true` if this character is a letter.
|
||||
*
|
||||
* A character is considered to be a letter if its [category] is [CharCategory.UPPERCASE_LETTER],
|
||||
* [CharCategory.LOWERCASE_LETTER], [CharCategory.TITLECASE_LETTER], [CharCategory.MODIFIER_LETTER], or [CharCategory.OTHER_LETTER].
|
||||
*
|
||||
* @sample samples.text.Chars.isLetter
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun Char.isLetter(): Boolean = Character.isLetter(this)
|
||||
public actual inline fun Char.isLetter(): Boolean = Character.isLetter(this)
|
||||
|
||||
/**
|
||||
* Returns `true` if this character is a letter or digit.
|
||||
*
|
||||
* @see isLetter
|
||||
* @see isDigit
|
||||
*
|
||||
* @sample samples.text.Chars.isLetterOrDigit
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun Char.isLetterOrDigit(): Boolean = Character.isLetterOrDigit(this)
|
||||
public actual inline fun Char.isLetterOrDigit(): Boolean = Character.isLetterOrDigit(this)
|
||||
|
||||
/**
|
||||
* Returns `true` if this character (Unicode code point) is a digit.
|
||||
* Returns `true` if this character is a digit.
|
||||
*
|
||||
* A character is considered to be a digit if its [category] is [CharCategory.DECIMAL_DIGIT_NUMBER].
|
||||
*
|
||||
* @sample samples.text.Chars.isDigit
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun Char.isDigit(): Boolean = Character.isDigit(this)
|
||||
public actual inline fun Char.isDigit(): Boolean = Character.isDigit(this)
|
||||
|
||||
|
||||
/**
|
||||
@@ -47,10 +66,13 @@ public inline fun Char.isIdentifierIgnorable(): Boolean = Character.isIdentifier
|
||||
|
||||
/**
|
||||
* Returns `true` if this character is an ISO control character.
|
||||
*
|
||||
* A character is considered to be an ISO control character if its [category] is [CharCategory.CONTROL].
|
||||
*
|
||||
* @sample samples.text.Chars.isISOControl
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun Char.isISOControl(): Boolean = Character.isISOControl(this)
|
||||
public actual inline fun Char.isISOControl(): Boolean = Character.isISOControl(this)
|
||||
|
||||
/**
|
||||
* Returns `true` if this character (Unicode code point) may be part of a Java identifier as other than the first character.
|
||||
@@ -69,6 +91,7 @@ public inline fun Char.isJavaIdentifierStart(): Boolean = Character.isJavaIdenti
|
||||
/**
|
||||
* Determines whether a character is whitespace according to the Unicode standard.
|
||||
* Returns `true` if the character is whitespace.
|
||||
*
|
||||
* @sample samples.text.Chars.isWhitespace
|
||||
*/
|
||||
public actual fun Char.isWhitespace(): Boolean = Character.isWhitespace(this) || Character.isSpaceChar(this)
|
||||
@@ -78,14 +101,14 @@ public actual fun Char.isWhitespace(): Boolean = Character.isWhitespace(this) ||
|
||||
* @sample samples.text.Chars.isUpperCase
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun Char.isUpperCase(): Boolean = Character.isUpperCase(this)
|
||||
public actual inline fun Char.isUpperCase(): Boolean = Character.isUpperCase(this)
|
||||
|
||||
/**
|
||||
* Returns `true` if this character is lower case.
|
||||
* @sample samples.text.Chars.isLowerCase
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun Char.isLowerCase(): Boolean = Character.isLowerCase(this)
|
||||
public actual inline fun Char.isLowerCase(): Boolean = Character.isLowerCase(this)
|
||||
|
||||
/**
|
||||
* Converts this character to lower case using Unicode mapping rules of the invariant locale.
|
||||
@@ -192,7 +215,7 @@ public fun Char.lowercase(locale: Locale): String = toString().lowercase(locale)
|
||||
* @sample samples.text.Chars.isTitleCase
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun Char.isTitleCase(): Boolean = Character.isTitleCase(this)
|
||||
public actual inline fun Char.isTitleCase(): Boolean = Character.isTitleCase(this)
|
||||
|
||||
/**
|
||||
* Converts this character to title case using Unicode mapping rules of the invariant locale.
|
||||
@@ -260,11 +283,6 @@ public fun Char.titlecase(locale: Locale): String {
|
||||
return titlecaseChar().toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a value indicating a character's general category.
|
||||
*/
|
||||
public val Char.category: CharCategory get() = CharCategory.valueOf(Character.getType(this))
|
||||
|
||||
/**
|
||||
* Returns the Unicode directionality property for the given character.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user