Fix case-insensitive character-wise comparison KT-45496
- Step 1: add failing tests - Step 2: fix common case insensitive Char.equals - Step 3: fix case insensitive String.equals in K/JS - Step 4: enable unicode case folding in K/JS Regexes and string replacement (KT-45928) - Step 5: fix case insensitive char comparison in K/N in String functions String.replace, equals, compareTo with ignoreCase
This commit is contained in:
@@ -63,13 +63,16 @@ public actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boole
|
||||
private external fun String.replace(oldChar: Char, newChar: Char): String
|
||||
|
||||
private fun String.replaceIgnoreCase(oldChar: Char, newChar: Char): String {
|
||||
val charArray = CharArray(length)
|
||||
val oldCharLower = oldChar.lowercaseChar()
|
||||
val charArray = this.toCharArray()
|
||||
val oldCharUpper = oldChar.uppercaseChar()
|
||||
val oldCharLower = oldCharUpper.lowercaseChar()
|
||||
|
||||
for (index in 0 until length) {
|
||||
val thisChar = this[index]
|
||||
val thisCharLower = thisChar.lowercaseChar()
|
||||
charArray[index] = if (thisCharLower == oldCharLower) newChar else thisChar
|
||||
if (thisChar != oldChar && thisChar.uppercaseChar().let { it != oldCharUpper && it.lowercaseChar() != oldCharLower }) {
|
||||
continue
|
||||
}
|
||||
charArray[index] = newChar
|
||||
}
|
||||
|
||||
return charArray.concatToString()
|
||||
@@ -186,9 +189,9 @@ private external fun String.unsafeRangeEquals(thisOffset: Int, other: String, ot
|
||||
// Bounds must be checked before calling this method
|
||||
private fun String.unsafeRangeEqualsIgnoreCase(thisOffset: Int, other: String, otherOffset: Int, length: Int): Boolean {
|
||||
for (index in 0 until length) {
|
||||
val thisCharLower = this[thisOffset + index].lowercaseChar()
|
||||
val otherCharLower = other[otherOffset + index].lowercaseChar()
|
||||
if (thisCharLower != otherCharLower) {
|
||||
val thisCharUpper = this[thisOffset + index].uppercaseChar()
|
||||
val otherCharUpper = other[otherOffset + index].uppercaseChar()
|
||||
if (thisCharUpper != otherCharUpper && thisCharUpper.lowercaseChar() != otherCharUpper.lowercaseChar()) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -429,8 +432,8 @@ internal fun compareToIgnoreCase(thiz: String, other: String): Int {
|
||||
val length = minOf(thiz.length, other.length)
|
||||
|
||||
for (index in 0 until length) {
|
||||
val thisLowerChar = thiz[index].lowercaseChar()
|
||||
val otherLowerChar = other[index].lowercaseChar()
|
||||
val thisLowerChar = thiz[index].uppercaseChar().lowercaseChar()
|
||||
val otherLowerChar = other[index].uppercaseChar().lowercaseChar()
|
||||
if (thisLowerChar != otherLowerChar) {
|
||||
return if (thisLowerChar < otherLowerChar) -1 else 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user