Use reverse indexed iteration in last and lastOrNull with predicate for CharSequences and Strings.
This commit is contained in:
@@ -265,16 +265,11 @@ public fun String.last(): Char {
|
|||||||
* @throws [NoSuchElementException] if no such character is found.
|
* @throws [NoSuchElementException] if no such character is found.
|
||||||
*/
|
*/
|
||||||
public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char {
|
public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char {
|
||||||
var last: Char? = null
|
for (index in this.indices.reversed()) {
|
||||||
var found = false
|
val element = this[index]
|
||||||
for (element in this) {
|
if (predicate(element)) return element
|
||||||
if (predicate(element)) {
|
|
||||||
last = element
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
|
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
|
||||||
return last as Char
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -283,16 +278,11 @@ public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char {
|
|||||||
*/
|
*/
|
||||||
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
|
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
|
||||||
public inline fun String.last(predicate: (Char) -> Boolean): Char {
|
public inline fun String.last(predicate: (Char) -> Boolean): Char {
|
||||||
var last: Char? = null
|
for (index in this.indices.reversed()) {
|
||||||
var found = false
|
val element = this[index]
|
||||||
for (element in this) {
|
if (predicate(element)) return element
|
||||||
if (predicate(element)) {
|
|
||||||
last = element
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
|
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
|
||||||
return last as Char
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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.
|
* 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? {
|
public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? {
|
||||||
var last: Char? = null
|
for (index in this.indices.reversed()) {
|
||||||
for (element in this) {
|
val element = this[index]
|
||||||
if (predicate(element)) {
|
if (predicate(element)) return element
|
||||||
last = 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)
|
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
|
||||||
public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? {
|
public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? {
|
||||||
var last: Char? = null
|
for (index in this.indices.reversed()) {
|
||||||
for (element in this) {
|
val element = this[index]
|
||||||
if (predicate(element)) {
|
if (predicate(element)) return element
|
||||||
last = element
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return last
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -643,7 +643,7 @@ fun elements(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
body(ArraysOfPrimitives, ArraysOfObjects, Lists) {
|
body(CharSequences, Strings, ArraysOfPrimitives, ArraysOfObjects, Lists) {
|
||||||
"""
|
"""
|
||||||
for (index in this.indices.reversed()) {
|
for (index in this.indices.reversed()) {
|
||||||
val element = this[index]
|
val element = this[index]
|
||||||
@@ -678,7 +678,7 @@ fun elements(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
body(ArraysOfPrimitives, ArraysOfObjects, Lists) {
|
body(CharSequences, Strings, ArraysOfPrimitives, ArraysOfObjects, Lists) {
|
||||||
"""
|
"""
|
||||||
for (index in this.indices.reversed()) {
|
for (index in this.indices.reversed()) {
|
||||||
val element = this[index]
|
val element = this[index]
|
||||||
|
|||||||
Reference in New Issue
Block a user