CharCategory and CharDirectionality enums for JVM.
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
package kotlin
|
||||
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
/**
|
||||
* Represents the character general category in the Unicode specification.
|
||||
*/
|
||||
public enum class CharCategory(public val value: Int) {
|
||||
/**
|
||||
* General category "Cn" in the Unicode specification.
|
||||
*/
|
||||
UNASSIGNED: CharCategory(Character.UNASSIGNED.toInt())
|
||||
|
||||
/**
|
||||
* General category "Lu" in the Unicode specification.
|
||||
*/
|
||||
UPPERCASE_LETTER: CharCategory(Character.UPPERCASE_LETTER.toInt())
|
||||
|
||||
/**
|
||||
* General category "Ll" in the Unicode specification.
|
||||
*/
|
||||
LOWERCASE_LETTER: CharCategory(Character.LOWERCASE_LETTER.toInt())
|
||||
|
||||
/**
|
||||
* General category "Lt" in the Unicode specification.
|
||||
*/
|
||||
TITLECASE_LETTER: CharCategory(Character.TITLECASE_LETTER.toInt())
|
||||
|
||||
/**
|
||||
* General category "Lm" in the Unicode specification.
|
||||
*/
|
||||
MODIFIER_LETTER: CharCategory(Character.MODIFIER_LETTER.toInt())
|
||||
|
||||
/**
|
||||
* General category "Lo" in the Unicode specification.
|
||||
*/
|
||||
OTHER_LETTER: CharCategory(Character.OTHER_LETTER.toInt())
|
||||
|
||||
/**
|
||||
* General category "Mn" in the Unicode specification.
|
||||
*/
|
||||
NON_SPACING_MARK: CharCategory(Character.NON_SPACING_MARK.toInt())
|
||||
|
||||
/**
|
||||
* General category "Me" in the Unicode specification.
|
||||
*/
|
||||
ENCLOSING_MARK: CharCategory(Character.ENCLOSING_MARK.toInt())
|
||||
|
||||
/**
|
||||
* General category "Mc" in the Unicode specification.
|
||||
*/
|
||||
COMBINING_SPACING_MARK: CharCategory(Character.COMBINING_SPACING_MARK.toInt())
|
||||
|
||||
/**
|
||||
* General category "Nd" in the Unicode specification.
|
||||
*/
|
||||
DECIMAL_DIGIT_NUMBER: CharCategory(Character.DECIMAL_DIGIT_NUMBER.toInt())
|
||||
|
||||
/**
|
||||
* General category "Nl" in the Unicode specification.
|
||||
*/
|
||||
LETTER_NUMBER: CharCategory(Character.LETTER_NUMBER.toInt())
|
||||
|
||||
/**
|
||||
* General category "No" in the Unicode specification.
|
||||
*/
|
||||
OTHER_NUMBER: CharCategory(Character.OTHER_NUMBER.toInt())
|
||||
|
||||
/**
|
||||
* General category "Zs" in the Unicode specification.
|
||||
*/
|
||||
SPACE_SEPARATOR: CharCategory(Character.SPACE_SEPARATOR.toInt())
|
||||
|
||||
/**
|
||||
* General category "Zl" in the Unicode specification.
|
||||
*/
|
||||
LINE_SEPARATOR: CharCategory(Character.LINE_SEPARATOR.toInt())
|
||||
|
||||
/**
|
||||
* General category "Zp" in the Unicode specification.
|
||||
*/
|
||||
PARAGRAPH_SEPARATOR: CharCategory(Character.PARAGRAPH_SEPARATOR.toInt())
|
||||
|
||||
/**
|
||||
* General category "Cc" in the Unicode specification.
|
||||
*/
|
||||
CONTROL: CharCategory(Character.CONTROL.toInt())
|
||||
|
||||
/**
|
||||
* General category "Cf" in the Unicode specification.
|
||||
*/
|
||||
FORMAT: CharCategory(Character.FORMAT.toInt())
|
||||
|
||||
/**
|
||||
* General category "Co" in the Unicode specification.
|
||||
*/
|
||||
PRIVATE_USE: CharCategory(Character.PRIVATE_USE.toInt())
|
||||
|
||||
/**
|
||||
* General category "Cs" in the Unicode specification.
|
||||
*/
|
||||
SURROGATE: CharCategory(Character.SURROGATE.toInt())
|
||||
|
||||
/**
|
||||
* General category "Pd" in the Unicode specification.
|
||||
*/
|
||||
DASH_PUNCTUATION: CharCategory(Character.DASH_PUNCTUATION.toInt())
|
||||
|
||||
/**
|
||||
* General category "Ps" in the Unicode specification.
|
||||
*/
|
||||
START_PUNCTUATION: CharCategory(Character.START_PUNCTUATION.toInt())
|
||||
|
||||
/**
|
||||
* General category "Pe" in the Unicode specification.
|
||||
*/
|
||||
END_PUNCTUATION: CharCategory(Character.END_PUNCTUATION.toInt())
|
||||
|
||||
/**
|
||||
* General category "Pc" in the Unicode specification.
|
||||
*/
|
||||
CONNECTOR_PUNCTUATION: CharCategory(Character.CONNECTOR_PUNCTUATION.toInt())
|
||||
|
||||
/**
|
||||
* General category "Po" in the Unicode specification.
|
||||
*/
|
||||
OTHER_PUNCTUATION: CharCategory(Character.OTHER_PUNCTUATION.toInt())
|
||||
|
||||
/**
|
||||
* General category "Sm" in the Unicode specification.
|
||||
*/
|
||||
MATH_SYMBOL: CharCategory(Character.MATH_SYMBOL.toInt())
|
||||
|
||||
/**
|
||||
* General category "Sc" in the Unicode specification.
|
||||
*/
|
||||
CURRENCY_SYMBOL: CharCategory(Character.CURRENCY_SYMBOL.toInt())
|
||||
|
||||
/**
|
||||
* General category "Sk" in the Unicode specification.
|
||||
*/
|
||||
MODIFIER_SYMBOL: CharCategory(Character.MODIFIER_SYMBOL.toInt())
|
||||
|
||||
/**
|
||||
* General category "So" in the Unicode specification.
|
||||
*/
|
||||
OTHER_SYMBOL: CharCategory(Character.OTHER_SYMBOL.toInt())
|
||||
|
||||
/**
|
||||
* General category "Pi" in the Unicode specification.
|
||||
*/
|
||||
INITIAL_QUOTE_PUNCTUATION: CharCategory(Character.INITIAL_QUOTE_PUNCTUATION.toInt())
|
||||
|
||||
/**
|
||||
* General category "Pf" in the Unicode specification.
|
||||
*/
|
||||
FINAL_QUOTE_PUNCTUATION: CharCategory(Character.FINAL_QUOTE_PUNCTUATION.toInt())
|
||||
|
||||
|
||||
public companion object {
|
||||
private val categoryMap by Delegates.lazy { CharCategory.values().toMap { it.value } }
|
||||
|
||||
public fun valueOf(category: Int): CharCategory = categoryMap[category] ?: throw IllegalArgumentException("Category #$category is not defined.")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package kotlin
|
||||
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
/**
|
||||
* Represents the Unicode directionality of a character.
|
||||
* Character directionality is used to calculate the
|
||||
* visual ordering of text.
|
||||
*/
|
||||
public enum class CharDirectionality(public val value: Int) {
|
||||
|
||||
/**
|
||||
* Undefined bidirectional character type. Undefined `char`
|
||||
* values have undefined directionality in the Unicode specification.
|
||||
*/
|
||||
UNDEFINED: CharDirectionality(Character.DIRECTIONALITY_UNDEFINED.toInt())
|
||||
|
||||
/**
|
||||
* Strong bidirectional character type "L" in the Unicode specification.
|
||||
*/
|
||||
LEFT_TO_RIGHT: CharDirectionality(Character.DIRECTIONALITY_LEFT_TO_RIGHT.toInt())
|
||||
|
||||
/**
|
||||
* Strong bidirectional character type "R" in the Unicode specification.
|
||||
*/
|
||||
RIGHT_TO_LEFT: CharDirectionality(Character.DIRECTIONALITY_RIGHT_TO_LEFT.toInt())
|
||||
|
||||
/**
|
||||
* Strong bidirectional character type "AL" in the Unicode specification.
|
||||
*/
|
||||
RIGHT_TO_LEFT_ARABIC: CharDirectionality(Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC.toInt())
|
||||
|
||||
/**
|
||||
* Weak bidirectional character type "EN" in the Unicode specification.
|
||||
*/
|
||||
EUROPEAN_NUMBER: CharDirectionality(Character.DIRECTIONALITY_EUROPEAN_NUMBER.toInt())
|
||||
|
||||
/**
|
||||
* Weak bidirectional character type "ES" in the Unicode specification.
|
||||
*/
|
||||
EUROPEAN_NUMBER_SEPARATOR: CharDirectionality(Character.DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR.toInt())
|
||||
|
||||
/**
|
||||
* Weak bidirectional character type "ET" in the Unicode specification.
|
||||
*/
|
||||
EUROPEAN_NUMBER_TERMINATOR: CharDirectionality(Character.DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR.toInt())
|
||||
|
||||
/**
|
||||
* Weak bidirectional character type "AN" in the Unicode specification.
|
||||
*/
|
||||
ARABIC_NUMBER: CharDirectionality(Character.DIRECTIONALITY_ARABIC_NUMBER.toInt())
|
||||
|
||||
/**
|
||||
* Weak bidirectional character type "CS" in the Unicode specification.
|
||||
*/
|
||||
COMMON_NUMBER_SEPARATOR: CharDirectionality(Character.DIRECTIONALITY_COMMON_NUMBER_SEPARATOR.toInt())
|
||||
|
||||
/**
|
||||
* Weak bidirectional character type "NSM" in the Unicode specification.
|
||||
*/
|
||||
NONSPACING_MARK: CharDirectionality(Character.DIRECTIONALITY_NONSPACING_MARK.toInt())
|
||||
|
||||
/**
|
||||
* Weak bidirectional character type "BN" in the Unicode specification.
|
||||
*/
|
||||
BOUNDARY_NEUTRAL: CharDirectionality(Character.DIRECTIONALITY_BOUNDARY_NEUTRAL.toInt())
|
||||
|
||||
/**
|
||||
* Neutral bidirectional character type "B" in the Unicode specification.
|
||||
*/
|
||||
PARAGRAPH_SEPARATOR: CharDirectionality(Character.DIRECTIONALITY_PARAGRAPH_SEPARATOR.toInt())
|
||||
|
||||
/**
|
||||
* Neutral bidirectional character type "S" in the Unicode specification.
|
||||
*/
|
||||
SEGMENT_SEPARATOR: CharDirectionality(Character.DIRECTIONALITY_SEGMENT_SEPARATOR.toInt())
|
||||
|
||||
/**
|
||||
* Neutral bidirectional character type "WS" in the Unicode specification.
|
||||
*/
|
||||
WHITESPACE: CharDirectionality(Character.DIRECTIONALITY_WHITESPACE.toInt())
|
||||
|
||||
/**
|
||||
* Neutral bidirectional character type "ON" in the Unicode specification.
|
||||
*/
|
||||
OTHER_NEUTRALS: CharDirectionality(Character.DIRECTIONALITY_OTHER_NEUTRALS.toInt())
|
||||
|
||||
/**
|
||||
* Strong bidirectional character type "LRE" in the Unicode specification.
|
||||
*/
|
||||
LEFT_TO_RIGHT_EMBEDDING: CharDirectionality(Character.DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING.toInt())
|
||||
|
||||
/**
|
||||
* Strong bidirectional character type "LRO" in the Unicode specification.
|
||||
*/
|
||||
LEFT_TO_RIGHT_OVERRIDE: CharDirectionality(Character.DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE.toInt())
|
||||
|
||||
/**
|
||||
* Strong bidirectional character type "RLE" in the Unicode specification.
|
||||
*/
|
||||
RIGHT_TO_LEFT_EMBEDDING: CharDirectionality(Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING.toInt())
|
||||
|
||||
/**
|
||||
* Strong bidirectional character type "RLO" in the Unicode specification.
|
||||
*/
|
||||
RIGHT_TO_LEFT_OVERRIDE: CharDirectionality(Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE.toInt())
|
||||
|
||||
/**
|
||||
* Weak bidirectional character type "PDF" in the Unicode specification.
|
||||
*/
|
||||
POP_DIRECTIONAL_FORMAT: CharDirectionality(Character.DIRECTIONALITY_POP_DIRECTIONAL_FORMAT.toInt())
|
||||
|
||||
|
||||
public companion object {
|
||||
private val directionalityMap by Delegates.lazy { CharDirectionality.values().toMap { it.value } }
|
||||
|
||||
public fun valueOf(directionality: Int): CharDirectionality = directionalityMap[directionality] ?: throw IllegalArgumentException("Directionality #$directionality is not defined.")
|
||||
}
|
||||
}
|
||||
@@ -79,3 +79,19 @@ public fun Char.toUpperCase(): Char = Character.toUpperCase(this)
|
||||
* Converts this character to lowercase.
|
||||
*/
|
||||
public fun Char.toLowerCase(): Char = Character.toLowerCase(this)
|
||||
|
||||
/**
|
||||
* Returns a value indicating a character's general category.
|
||||
*/
|
||||
public fun Char.category(): CharCategory = CharCategory.valueOf(Character.getType(this))
|
||||
|
||||
/**
|
||||
* Returns the Unicode directionality property for the given character.
|
||||
*/
|
||||
public fun Char.directionality(): CharDirectionality = CharDirectionality.valueOf(Character.getDirectionality(this).toInt())
|
||||
|
||||
// TODO Provide name for JVM7+
|
||||
///**
|
||||
// * Returns the Unicode name of this character, or null if the code point of this character is unassigned.
|
||||
// */
|
||||
//public fun Char.name(): String? = Character.getName(this.toInt())
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package test.text
|
||||
|
||||
import kotlin.test.*
|
||||
import org.junit.Test as test
|
||||
|
||||
class CharJVMTest {
|
||||
|
||||
test fun getCategory() {
|
||||
assertEquals(CharCategory.DECIMAL_DIGIT_NUMBER, '7'.category())
|
||||
assertEquals(CharCategory.CURRENCY_SYMBOL, '$'.category())
|
||||
assertEquals(CharCategory.LOWERCASE_LETTER, 'a'.category())
|
||||
assertEquals(CharCategory.UPPERCASE_LETTER, 'Õ'.category())
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user