JS stdlib: implement String.compareTo with ignoreCase

#KT-18067 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-08-03 10:25:22 +09:00
committed by Ilya Gorbunov
parent d194893012
commit f05a19aafb
4 changed files with 62 additions and 1 deletions
+31
View File
@@ -73,3 +73,34 @@ public inline val CharSequence.size: Int get() = length
@kotlin.internal.InlineOnly
internal inline fun String.nativeReplace(pattern: RegExp, replacement: String): String = asDynamic().replace(pattern, replacement)
public actual fun String.compareTo(other: String, ignoreCase: Boolean): Int {
if (ignoreCase) {
val n1 = this.length
val n2 = other.length
val min = minOf(n1, n2)
if (min == 0) return n1 - n2
var start = 0
while (true) {
val end = minOf(start + 16, min)
var s1 = this.substring(start, end)
var s2 = other.substring(start, end)
if (s1 != s2) {
s1 = s1.toUpperCase()
s2 = s2.toUpperCase()
if (s1 != s2) {
s1 = s1.toLowerCase()
s2 = s2.toLowerCase()
if (s1 != s2) {
return s1.compareTo(s2)
}
}
}
if (end == min) break
start = end
}
return n1 - n2
} else {
return compareTo(other)
}
}