Locale-agnostic case conversions by default #KT-43023

This commit is contained in:
Abduqodiri Qurbonzoda
2020-10-02 06:46:42 +03:00
parent 80289e4a3f
commit 1314adb6f7
25 changed files with 805 additions and 142 deletions
+70 -2
View File
@@ -8,11 +8,79 @@ package kotlin.text
// actually \s is enough to match all whitespace, but \xA0 added because of different regexp behavior of Rhino used in Selenium tests
public actual fun Char.isWhitespace(): Boolean = toString().matches("[\\s\\xA0]")
/**
* Converts this character to lower case using Unicode mapping rules of the invariant locale.
*/
@OptIn(ExperimentalStdlibApi::class)
@kotlin.internal.InlineOnly
public actual inline fun Char.toLowerCase(): Char = js("String.fromCharCode")(toInt()).toLowerCase().charCodeAt(0).unsafeCast<Int>().toChar()
public actual inline fun Char.toLowerCase(): Char = lowercaseChar()
/**
* 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
@kotlin.internal.InlineOnly
public actual inline fun Char.toUpperCase(): Char = js("String.fromCharCode")(toInt()).toUpperCase().charCodeAt(0).unsafeCast<Int>().toChar()
public actual inline fun Char.lowercaseChar(): Char = toString().asDynamic().toLowerCase().charCodeAt(0).unsafeCast<Int>().toChar()
/**
* 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
@kotlin.internal.InlineOnly
public actual inline fun Char.lowercase(): String = toString().asDynamic().toLowerCase() as String
/**
* Converts this character to upper case using Unicode mapping rules of the invariant locale.
*/
@OptIn(ExperimentalStdlibApi::class)
@kotlin.internal.InlineOnly
public actual inline fun Char.toUpperCase(): Char = uppercaseChar()
/**
* 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 {
val uppercase = uppercase()
return if (uppercase.length > 1) this else uppercase[0]
}
/**
* 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
@kotlin.internal.InlineOnly
public actual inline fun Char.uppercase(): String = toString().asDynamic().toUpperCase() as String
/**
* Returns `true` if this character is a Unicode high-surrogate code unit (also known as leading-surrogate code unit).
@@ -17,8 +17,9 @@ public actual inline fun String.toBoolean(): Boolean = this.toBoolean()
/**
* Returns `true` if this string is not `null` and its content is equal to the word "true", ignoring case, and `false` otherwise.
*/
@OptIn(ExperimentalStdlibApi::class)
@SinceKotlin("1.4")
public actual fun String?.toBoolean(): Boolean = this != null && this.toLowerCase() == "true"
public actual fun String?.toBoolean(): Boolean = this != null && this.lowercase() == "true"
/**
* Parses the string as a signed [Byte] number and returns the result.
@@ -130,7 +131,8 @@ public actual inline fun Short.toString(radix: Int): String = this.toInt().toStr
@SinceKotlin("1.2")
public actual fun Int.toString(radix: Int): String = asDynamic().toString(checkRadix(radix))
private fun String.isNaN(): Boolean = when (this.toLowerCase()) {
@OptIn(ExperimentalStdlibApi::class)
private fun String.isNaN(): Boolean = when (this.lowercase()) {
"nan", "+nan", "-nan" -> true
else -> false
}
+31 -8
View File
@@ -168,20 +168,42 @@ public actual fun String.encodeToByteArray(
/**
* Returns a copy of this string converted to upper case using the rules of the default locale.
*
* @sample samples.text.Strings.toUpperCase
*/
@kotlin.internal.InlineOnly
public actual inline fun String.toUpperCase(): String = asDynamic().toUpperCase()
/**
* Returns a copy of this string converted to lower case using the rules of the default locale.
* Returns a copy of this string converted to upper case using Unicode mapping rules of the invariant locale.
*
* @sample samples.text.Strings.toLowerCase
* 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
@kotlin.internal.InlineOnly
public actual inline fun String.uppercase(): String = asDynamic().toUpperCase()
/**
* Returns a copy of this string converted to lower case using the rules of the default locale.
*/
@kotlin.internal.InlineOnly
public actual inline fun String.toLowerCase(): String = asDynamic().toLowerCase()
/**
* 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
@kotlin.internal.InlineOnly
public actual inline fun String.lowercase(): String = asDynamic().toLowerCase()
@kotlin.internal.InlineOnly
internal actual inline fun String.nativeIndexOf(str: String, fromIndex: Int): Int = asDynamic().indexOf(str, fromIndex)
@@ -212,6 +234,7 @@ public inline fun String.match(regex: String): Array<String>? = asDynamic().matc
@kotlin.internal.InlineOnly
internal inline fun String.nativeReplace(pattern: RegExp, replacement: String): String = asDynamic().replace(pattern, replacement)
@OptIn(ExperimentalStdlibApi::class)
@SinceKotlin("1.2")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun String.compareTo(other: String, ignoreCase: Boolean = false): Int {
@@ -226,11 +249,11 @@ public actual fun String.compareTo(other: String, ignoreCase: Boolean = false):
var s1 = this.substring(start, end)
var s2 = other.substring(start, end)
if (s1 != s2) {
s1 = s1.toUpperCase()
s2 = s2.toUpperCase()
s1 = s1.uppercase()
s2 = s2.uppercase()
if (s1 != s2) {
s1 = s1.toLowerCase()
s2 = s2.toLowerCase()
s1 = s1.lowercase()
s2 = s2.lowercase()
if (s1 != s2) {
return s1.compareTo(s2)
}
@@ -54,6 +54,7 @@ public fun String.matches(regex: String): Boolean {
public actual fun CharSequence.isBlank(): Boolean = length == 0 || (if (this is String) this else this.toString()).matches("^[\\s\\xA0]+$")
@OptIn(ExperimentalStdlibApi::class)
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun String?.equals(other: String?, ignoreCase: Boolean = false): Boolean =
if (this == null)
@@ -61,7 +62,7 @@ public actual fun String?.equals(other: String?, ignoreCase: Boolean = false): B
else if (!ignoreCase)
this == other
else
other != null && this.toLowerCase() == other.toLowerCase()
other != null && this.lowercase() == other.lowercase()
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")