[K/N] String.indexOf matches byte sequences not on the char boundary #KT-56637
If the found index is odd, retry search from index + 1. Merge-request: KT-MR-10364 Merged-by: Abduqodiri Qurbonzoda <abduqodiri.qurbonzoda@jetbrains.com>
This commit is contained in:
committed by
Space Team
parent
b11447ea6b
commit
7acaf6e473
@@ -398,14 +398,20 @@ KInt Kotlin_String_indexOfString(KString thiz, KString other, KInt fromIndex) {
|
||||
if (other->count_ == 0) {
|
||||
return fromIndex;
|
||||
}
|
||||
const KChar* thizRaw = CharArrayAddressOfElementAt(thiz, fromIndex);
|
||||
const KChar* thizRaw = CharArrayAddressOfElementAt(thiz, 0);
|
||||
const KChar* otherRaw = CharArrayAddressOfElementAt(other, 0);
|
||||
void* result = konan::memmem(thizRaw, (thiz->count_ - fromIndex) * sizeof(KChar),
|
||||
otherRaw, other->count_ * sizeof(KChar));
|
||||
if (result == nullptr) return -1;
|
||||
|
||||
return (reinterpret_cast<intptr_t>(result) - reinterpret_cast<intptr_t>(
|
||||
CharArrayAddressOfElementAt(thiz, 0))) / sizeof(KChar);
|
||||
const auto otherSize = other->count_ * sizeof(KChar);
|
||||
while (true) {
|
||||
void* result = konan::memmem(thizRaw + fromIndex, (thiz->count_ - fromIndex) * sizeof(KChar),
|
||||
otherRaw, otherSize);
|
||||
if (result == nullptr) return -1;
|
||||
auto byteIndex = reinterpret_cast<intptr_t>(result) - reinterpret_cast<intptr_t>(thizRaw);
|
||||
if (byteIndex % sizeof(KChar) == 0) {
|
||||
return byteIndex / sizeof(KChar);
|
||||
} else {
|
||||
fromIndex = byteIndex / sizeof(KChar) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
KInt Kotlin_String_lastIndexOfString(KString thiz, KString other, KInt fromIndex) {
|
||||
|
||||
@@ -1856,4 +1856,17 @@ ${" "}
|
||||
assertTrue(null.contentEquals(null, ignoreCase = true))
|
||||
assertTrue(null.contentEquals(null, ignoreCase = false))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun indexOfRespectsCharBoundary() {
|
||||
withOneCharSequenceArg("\u003a\u3b3c\u003d") { input ->
|
||||
assertEquals(-1, input.indexOf("\u3c00"))
|
||||
assertEquals(-1, input.indexOf("\u3d3b"))
|
||||
assertEquals(-1, input.indexOf("\u3c00\u3d3b"))
|
||||
}
|
||||
|
||||
// KT-56637
|
||||
assertEquals("買っ", "買っ".replace("掌", "X"))
|
||||
assertEquals("買", "買".replace("掌", "X"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user