stdlib: Add first() extension for CharSequence

This commit is contained in:
Ilya Matveev
2017-04-14 12:33:23 +07:00
committed by ilmat192
parent 64f1cea134
commit 69a49f987c
@@ -1280,4 +1280,23 @@ public inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char?
}
if (!found) return null
return single
}
/**
* Returns first character.
* @throws [NoSuchElementException] if the char sequence is empty.
*/
public fun CharSequence.first(): Char {
if (isEmpty())
throw NoSuchElementException("Char sequence is empty.")
return this[0]
}
/**
* Returns the first character matching the given [predicate].
* @throws [NoSuchElementException] if no such character is found.
*/
public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("Char sequence contains no character matching the predicate.")
}