Add support for CASE_INSENSITIVE_ORDER in JS and common
#KT-18067
This commit is contained in:
@@ -187,6 +187,16 @@ expect fun CharSequence.regionMatches(
|
||||
): Boolean
|
||||
|
||||
|
||||
/**
|
||||
* A Comparator that orders strings ignoring character case.
|
||||
*
|
||||
* Note that this Comparator does not take locale into account,
|
||||
* and will result in an unsatisfactory ordering for certain locales.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public expect val String.Companion.CASE_INSENSITIVE_ORDER: Comparator<String>
|
||||
|
||||
|
||||
/**
|
||||
* Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise.
|
||||
*/
|
||||
|
||||
@@ -105,4 +105,11 @@ public actual fun String.compareTo(other: String, ignoreCase: Boolean = false):
|
||||
} else {
|
||||
return compareTo(other)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private val STRING_CASE_INSENSITIVE_ORDER = Comparator<String> { a, b -> a.compareTo(b, ignoreCase = true) }
|
||||
|
||||
@SinceKotlin("1.2")
|
||||
public actual val String.Companion.CASE_INSENSITIVE_ORDER: Comparator<String>
|
||||
get() = STRING_CASE_INSENSITIVE_ORDER
|
||||
|
||||
@@ -466,5 +466,5 @@ public actual fun CharSequence.repeat(n: Int): String {
|
||||
* Note that this Comparator does not take locale into account,
|
||||
* and will result in an unsatisfactory ordering for certain locales.
|
||||
*/
|
||||
public val String.Companion.CASE_INSENSITIVE_ORDER: Comparator<String>
|
||||
public actual val String.Companion.CASE_INSENSITIVE_ORDER: Comparator<String>
|
||||
get() = java.lang.String.CASE_INSENSITIVE_ORDER
|
||||
|
||||
@@ -61,11 +61,6 @@ class StringJVMTest {
|
||||
assertArrayNotSameButEquals(charArrayOf('\u0000', '\u0000', 'e', 'l'), buffer)
|
||||
}
|
||||
|
||||
@Test fun orderIgnoringCase() {
|
||||
val list = listOf("Beast", "Ast", "asterisk")
|
||||
assertEquals(listOf("Ast", "Beast", "asterisk"), list.sorted())
|
||||
assertEquals(listOf("Ast", "asterisk", "Beast"), list.sortedWith(String.CASE_INSENSITIVE_ORDER))
|
||||
}
|
||||
|
||||
@Test fun charsets() {
|
||||
assertEquals("UTF-32", Charsets.UTF_32.name())
|
||||
|
||||
@@ -804,6 +804,12 @@ class StringTest {
|
||||
}
|
||||
|
||||
|
||||
@Test fun orderIgnoringCase() {
|
||||
val list = listOf("Beast", "Ast", "asterisk", "[]")
|
||||
assertEquals(listOf("Ast", "Beast", "[]", "asterisk"), list.sorted())
|
||||
assertEquals(listOf("[]", "Ast", "asterisk", "Beast"), list.sortedWith(String.CASE_INSENSITIVE_ORDER))
|
||||
}
|
||||
|
||||
@Test fun replace() {
|
||||
val input = "abbAb"
|
||||
assertEquals("abb${'$'}b", input.replace('A', '$'))
|
||||
|
||||
Reference in New Issue
Block a user