From 0dd7e398c7c390fc46ed9d53e7de5ff101a6f6b1 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 28 Oct 2015 21:55:51 +0300 Subject: [PATCH] Use reverse indexed iteration in last and lastOrNull with predicate for CharSequences and Strings. --- libraries/stdlib/src/generated/_Strings.kt | 46 +++++++------------ .../src/templates/Elements.kt | 4 +- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 336ed95325f..211517c455e 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -265,16 +265,11 @@ public fun String.last(): Char { * @throws [NoSuchElementException] if no such character is found. */ public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char { - var last: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - last = element - found = true - } + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") - return last as Char + throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** @@ -283,16 +278,11 @@ public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char { */ @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public inline fun String.last(predicate: (Char) -> Boolean): Char { - var last: Char? = null - var found = false - for (element in this) { - if (predicate(element)) { - last = element - found = true - } + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") - return last as Char + throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") } /** @@ -314,13 +304,11 @@ public fun String.lastOrNull(): Char? { * Returns the last character matching the given [predicate], or `null` if no such character was found. */ public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? { - var last: Char? = null - for (element in this) { - if (predicate(element)) { - last = element - } + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element } - return last + return null } /** @@ -328,13 +316,11 @@ public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? { */ @Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? { - var last: Char? = null - for (element in this) { - if (predicate(element)) { - last = element - } + for (index in this.indices.reversed()) { + val element = this[index] + if (predicate(element)) return element } - return last + return null } /** diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index f05f5f1f8e0..2851e9bc68f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -643,7 +643,7 @@ fun elements(): List { """ } - body(ArraysOfPrimitives, ArraysOfObjects, Lists) { + body(CharSequences, Strings, ArraysOfPrimitives, ArraysOfObjects, Lists) { """ for (index in this.indices.reversed()) { val element = this[index] @@ -678,7 +678,7 @@ fun elements(): List { """ } - body(ArraysOfPrimitives, ArraysOfObjects, Lists) { + body(CharSequences, Strings, ArraysOfPrimitives, ArraysOfObjects, Lists) { """ for (index in this.indices.reversed()) { val element = this[index]