Make Char.category and directionality properties instead of functions.

Provide contains(Char) operator for CharCategory.
This commit is contained in:
Ilya Gorbunov
2015-11-18 21:39:10 +03:00
parent 775755dfac
commit b61bef324d
3 changed files with 26 additions and 6 deletions
@@ -154,6 +154,11 @@ public enum class CharCategory(public val value: Int, public val code: String) {
*/
FINAL_QUOTE_PUNCTUATION(Character.FINAL_QUOTE_PUNCTUATION.toInt(), "Pf");
/**
* Returns `true` if [char] character belongs to this category.
*/
public operator fun contains(char: Char): Boolean = Character.getType(char) == this.value
public companion object {
private val categoryMap by lazy { CharCategory.values().toMap { it.value } }
+15 -2
View File
@@ -102,12 +102,25 @@ public fun Char.toTitleCase(): Char = Character.toTitleCase(this)
/**
* Returns a value indicating a character's general category.
*/
public fun Char.category(): CharCategory = CharCategory.valueOf(Character.getType(this))
@Deprecated("Use 'category' property instead.", ReplaceWith("category"))
public fun Char.category(): CharCategory = category
/**
* 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.
*/
public fun Char.directionality(): CharDirectionality = CharDirectionality.valueOf(Character.getDirectionality(this).toInt())
@Deprecated("Use 'directionality' property instead.", ReplaceWith("directionality"))
public fun Char.directionality(): CharDirectionality = directionality
/**
* Returns the Unicode directionality property for the given character.
*/
public val Char.directionality: CharDirectionality get() = CharDirectionality.valueOf(Character.getDirectionality(this).toInt())
// TODO Provide name for JVM7+
///**
+6 -4
View File
@@ -6,10 +6,12 @@ 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())
assertEquals(CharCategory.DECIMAL_DIGIT_NUMBER, '7'.category)
assertEquals(CharCategory.CURRENCY_SYMBOL, '$'.category)
assertEquals(CharCategory.LOWERCASE_LETTER, 'a'.category)
assertEquals(CharCategory.UPPERCASE_LETTER, 'Õ'.category)
assertTrue(',' in CharCategory.OTHER_PUNCTUATION)
}
}