From a4155e8fc4b4ec72f5ffbe1951f58ae741cb28b5 Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Tue, 6 Oct 2020 04:10:41 +0300 Subject: [PATCH] Locale-agnostic case conversions by default #KT-43023 (cherry picked from commit 1b565500b935239484580005a31ae2630c61b25d) --- .../src/main/kotlin/kotlin/text/Char.kt | 62 ++++++++++++++++++- .../src/main/kotlin/kotlin/text/Strings.kt | 25 +++++++- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/text/Char.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/text/Char.kt index 3298d7d48de..b8b11f70b2d 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/text/Char.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/text/Char.kt @@ -64,17 +64,75 @@ external public fun Char.isUpperCase(): Boolean external public fun Char.isLowerCase(): Boolean /** - * Converts this character to uppercase. + * Converts this character to upper case using Unicode mapping rules of the invariant locale. */ @SymbolName("Kotlin_Char_toUpperCase") external public actual fun Char.toUpperCase(): Char /** - * Converts this character to lowercase. + * Converts this character to upper case using Unicode mapping rules of the invariant locale. + * + * This function performs one-to-one character mapping. + * To support one-to-many character mapping use the [uppercase] function. + * If this character has no mapping equivalent, the character itself is returned. + * + * @sample samples.text.Chars.uppercase + */ +@SinceKotlin("1.4") +@ExperimentalStdlibApi +public actual fun Char.uppercaseChar(): Char = toUpperCase() + +/** + * Converts this character to upper case using Unicode mapping rules of the invariant locale. + * + * This function supports one-to-many character mapping, thus the length of the returned string can be greater than one. + * For example, `'\uFB00'.uppercase()` returns `"\u0046\u0046"`, + * where `'\uFB00'` is the LATIN SMALL LIGATURE FF character (`ff`). + * If this character has no upper case mapping, the result of `toString()` of this char is returned. + * + * @sample samples.text.Chars.uppercase + */ +@SinceKotlin("1.4") +@ExperimentalStdlibApi +public actual fun Char.uppercase(): String { + return uppercaseChar().toString() +} + +/** + * Converts this character to lower case using Unicode mapping rules of the invariant locale. */ @SymbolName("Kotlin_Char_toLowerCase") external public actual fun Char.toLowerCase(): Char +/** + * Converts this character to lower case using Unicode mapping rules of the invariant locale. + * + * This function performs one-to-one character mapping. + * To support one-to-many character mapping use the [lowercase] function. + * If this character has no mapping equivalent, the character itself is returned. + * + * @sample samples.text.Chars.lowercase + */ +@SinceKotlin("1.4") +@ExperimentalStdlibApi +public actual fun Char.lowercaseChar(): Char = toLowerCase() + +/** + * Converts this character to lower case using Unicode mapping rules of the invariant locale. + * + * This function supports one-to-many character mapping, thus the length of the returned string can be greater than one. + * For example, `'\u0130'.lowercase()` returns `"\u0069\u0307"`, + * where `'\u0130'` is the LATIN CAPITAL LETTER I WITH DOT ABOVE character (`İ`). + * If this character has no lower case mapping, the result of `toString()` of this char is returned. + * + * @sample samples.text.Chars.lowercase + */ +@SinceKotlin("1.4") +@ExperimentalStdlibApi +public actual fun Char.lowercase(): String { + return lowercaseChar().toString() +} + /** * Returns `true` if this character is a Unicode high-surrogate code unit (also known as leading-surrogate code unit). */ diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt index 744d5e35326..092521f7ca1 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt @@ -158,13 +158,36 @@ public external fun String.regionMatches( @SymbolName("Kotlin_String_toUpperCase") public actual external fun String.toUpperCase(): String +/** + * Returns a copy of this string converted to upper case using Unicode mapping rules of the invariant locale. + * + * This function supports one-to-many and many-to-one character mapping, + * thus the length of the returned string can be different from the length of the original string. + * + * @sample samples.text.Strings.uppercase + */ +@SinceKotlin("1.4") +@ExperimentalStdlibApi +public actual fun String.uppercase(): String = toUpperCase() + /** * Returns a copy of this string converted to lower case using the rules of the default locale. */ @SymbolName("Kotlin_String_toLowerCase") -@Suppress("NOTHING_TO_INLINE") public actual external fun String.toLowerCase(): String +/** + * Returns a copy of this string converted to lower case using Unicode mapping rules of the invariant locale. + * + * This function supports one-to-many and many-to-one character mapping, + * thus the length of the returned string can be different from the length of the original string. + * + * @sample samples.text.Strings.lowercase + */ +@SinceKotlin("1.4") +@ExperimentalStdlibApi +public actual fun String.lowercase(): String = toLowerCase() + /** * Returns a [CharArray] containing characters of this string. */