Add support for CASE_INSENSITIVE_ORDER in JS and common

#KT-18067
This commit is contained in:
Ilya Gorbunov
2018-08-12 04:08:05 +03:00
parent 6866f5d19a
commit ed53983e1e
5 changed files with 25 additions and 7 deletions
@@ -187,6 +187,16 @@ expect fun CharSequence.regionMatches(
): Boolean ): 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. * Returns `true` if the contents of this string is equal to the word "true", ignoring case, and `false` otherwise.
*/ */
+7
View File
@@ -106,3 +106,10 @@ public actual fun String.compareTo(other: String, ignoreCase: Boolean = false):
return compareTo(other) 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, * Note that this Comparator does not take locale into account,
* and will result in an unsatisfactory ordering for certain locales. * 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 get() = java.lang.String.CASE_INSENSITIVE_ORDER
@@ -61,11 +61,6 @@ class StringJVMTest {
assertArrayNotSameButEquals(charArrayOf('\u0000', '\u0000', 'e', 'l'), buffer) 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() { @Test fun charsets() {
assertEquals("UTF-32", Charsets.UTF_32.name()) assertEquals("UTF-32", Charsets.UTF_32.name())
+6
View File
@@ -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() { @Test fun replace() {
val input = "abbAb" val input = "abbAb"
assertEquals("abb${'$'}b", input.replace('A', '$')) assertEquals("abb${'$'}b", input.replace('A', '$'))