From 4202e6d8182da0056c56dae1d4c986edfa3733cd Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Wed, 14 Jun 2017 14:55:17 +0700 Subject: [PATCH] stdlib: Remove redundant checks from String.lastIndexOf Kotlin/JVM allows a user to specify a start index greater than a string length in String.lastIndexOf method. This patch modifies the Kotlin/Native implementation according to this behaviour. It also removes other redundant checks. --- runtime/src/main/cpp/KString.cpp | 55 ++++++++++++++++---------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/runtime/src/main/cpp/KString.cpp b/runtime/src/main/cpp/KString.cpp index 30d3883f353..16b9da4209d 100644 --- a/runtime/src/main/cpp/KString.cpp +++ b/runtime/src/main/cpp/KString.cpp @@ -1020,9 +1020,12 @@ KInt Kotlin_String_indexOfChar(KString thiz, KChar ch, KInt fromIndex) { } KInt Kotlin_String_lastIndexOfChar(KString thiz, KChar ch, KInt fromIndex) { - if (fromIndex < 0 || fromIndex > thiz->count_ || thiz->count_ == 0) { + if (fromIndex < 0 || thiz->count_ == 0) { return -1; } + if (fromIndex >= thiz->count_) { + fromIndex = thiz->count_ - 1; + } KInt count = thiz->count_; KInt index = fromIndex; const KChar* thizRaw = CharArrayAddressOfElementAt(thiz, index); @@ -1065,35 +1068,33 @@ KInt Kotlin_String_indexOfString(KString thiz, KString other, KInt fromIndex) { } KInt Kotlin_String_lastIndexOfString(KString thiz, KString other, KInt fromIndex) { - if (fromIndex < 0 || fromIndex > thiz->count_ || thiz->count_ == 0 || - other->count_ > thiz->count_ - fromIndex) { - return -1; - } KInt count = thiz->count_; KInt otherCount = other->count_; - KInt start = fromIndex; - if (otherCount <= count && start >= 0) { - if (otherCount > 0) { - if (fromIndex > count - otherCount) - start = count - otherCount; - KChar firstChar = *CharArrayAddressOfElementAt(other, 0); - while (true) { - KInt candidate = Kotlin_String_lastIndexOfChar(thiz, firstChar, start); - if (candidate == -1) return -1; - KInt offsetThiz = candidate; - KInt offsetOther = 0; - while (++offsetOther < otherCount && - *CharArrayAddressOfElementAt(thiz, ++offsetThiz) == - *CharArrayAddressOfElementAt(other, offsetOther)) {} - if (offsetOther == otherCount) { - return candidate; - } - start = candidate - 1; - } - } - return start < count ? start : count; + + if (fromIndex < 0 || otherCount > count) { + return -1; + } + if (otherCount == 0) { + return fromIndex < count ? fromIndex : count; + } + + KInt start = fromIndex; + if (fromIndex > count - otherCount) + start = count - otherCount; + KChar firstChar = *CharArrayAddressOfElementAt(other, 0); + while (true) { + KInt candidate = Kotlin_String_lastIndexOfChar(thiz, firstChar, start); + if (candidate == -1) return -1; + KInt offsetThiz = candidate; + KInt offsetOther = 0; + while (++offsetOther < otherCount && + *CharArrayAddressOfElementAt(thiz, ++offsetThiz) == + *CharArrayAddressOfElementAt(other, offsetOther)) {} + if (offsetOther == otherCount) { + return candidate; + } + start = candidate - 1; } - return -1; } KInt Kotlin_String_hashCode(KString thiz) {