From b61bef324d65bb6d1f0bcafbe701fd50358f3697 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 18 Nov 2015 21:39:10 +0300 Subject: [PATCH] Make Char.category and directionality properties instead of functions. Provide contains(Char) operator for CharCategory. --- .../stdlib/src/kotlin/text/CharCategoryJVM.kt | 5 +++++ libraries/stdlib/src/kotlin/text/CharJVM.kt | 17 +++++++++++++++-- libraries/stdlib/test/text/CharJVMTest.kt | 10 ++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/libraries/stdlib/src/kotlin/text/CharCategoryJVM.kt b/libraries/stdlib/src/kotlin/text/CharCategoryJVM.kt index 0aca0b1337d..09713795c58 100644 --- a/libraries/stdlib/src/kotlin/text/CharCategoryJVM.kt +++ b/libraries/stdlib/src/kotlin/text/CharCategoryJVM.kt @@ -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 } } diff --git a/libraries/stdlib/src/kotlin/text/CharJVM.kt b/libraries/stdlib/src/kotlin/text/CharJVM.kt index 4699e8d6a45..24d2b8e57d7 100644 --- a/libraries/stdlib/src/kotlin/text/CharJVM.kt +++ b/libraries/stdlib/src/kotlin/text/CharJVM.kt @@ -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+ ///** diff --git a/libraries/stdlib/test/text/CharJVMTest.kt b/libraries/stdlib/test/text/CharJVMTest.kt index af4bf883a63..375a32667c1 100644 --- a/libraries/stdlib/test/text/CharJVMTest.kt +++ b/libraries/stdlib/test/text/CharJVMTest.kt @@ -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) } } \ No newline at end of file