Review and correct exception messages.

This commit is contained in:
Ilya Gorbunov
2016-02-13 00:17:38 +03:00
parent 00e30e417f
commit 19fa2f51ea
19 changed files with 220 additions and 265 deletions
+108 -108
View File
@@ -802,7 +802,7 @@ public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? {
*/
public fun <T> Array<out T>.first(): T {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[0]
}
@@ -812,7 +812,7 @@ public fun <T> Array<out T>.first(): T {
*/
public fun ByteArray.first(): Byte {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[0]
}
@@ -822,7 +822,7 @@ public fun ByteArray.first(): Byte {
*/
public fun ShortArray.first(): Short {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[0]
}
@@ -832,7 +832,7 @@ public fun ShortArray.first(): Short {
*/
public fun IntArray.first(): Int {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[0]
}
@@ -842,7 +842,7 @@ public fun IntArray.first(): Int {
*/
public fun LongArray.first(): Long {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[0]
}
@@ -852,7 +852,7 @@ public fun LongArray.first(): Long {
*/
public fun FloatArray.first(): Float {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[0]
}
@@ -862,7 +862,7 @@ public fun FloatArray.first(): Float {
*/
public fun DoubleArray.first(): Double {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[0]
}
@@ -872,7 +872,7 @@ public fun DoubleArray.first(): Double {
*/
public fun BooleanArray.first(): Boolean {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[0]
}
@@ -882,7 +882,7 @@ public fun BooleanArray.first(): Boolean {
*/
public fun CharArray.first(): Char {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[0]
}
@@ -892,7 +892,7 @@ public fun CharArray.first(): Char {
*/
public inline fun <T> Array<out T>.first(predicate: (T) -> Boolean): T {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -901,7 +901,7 @@ public inline fun <T> Array<out T>.first(predicate: (T) -> Boolean): T {
*/
public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -910,7 +910,7 @@ public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte {
*/
public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -919,7 +919,7 @@ public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short {
*/
public inline fun IntArray.first(predicate: (Int) -> Boolean): Int {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -928,7 +928,7 @@ public inline fun IntArray.first(predicate: (Int) -> Boolean): Int {
*/
public inline fun LongArray.first(predicate: (Long) -> Boolean): Long {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -937,7 +937,7 @@ public inline fun LongArray.first(predicate: (Long) -> Boolean): Long {
*/
public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -946,7 +946,7 @@ public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float {
*/
public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -955,7 +955,7 @@ public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double {
*/
public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -964,7 +964,7 @@ public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean {
*/
public inline fun CharArray.first(predicate: (Char) -> Boolean): Char {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -1575,7 +1575,7 @@ public inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int {
*/
public fun <T> Array<out T>.last(): T {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[lastIndex]
}
@@ -1585,7 +1585,7 @@ public fun <T> Array<out T>.last(): T {
*/
public fun ByteArray.last(): Byte {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[lastIndex]
}
@@ -1595,7 +1595,7 @@ public fun ByteArray.last(): Byte {
*/
public fun ShortArray.last(): Short {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[lastIndex]
}
@@ -1605,7 +1605,7 @@ public fun ShortArray.last(): Short {
*/
public fun IntArray.last(): Int {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[lastIndex]
}
@@ -1615,7 +1615,7 @@ public fun IntArray.last(): Int {
*/
public fun LongArray.last(): Long {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[lastIndex]
}
@@ -1625,7 +1625,7 @@ public fun LongArray.last(): Long {
*/
public fun FloatArray.last(): Float {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[lastIndex]
}
@@ -1635,7 +1635,7 @@ public fun FloatArray.last(): Float {
*/
public fun DoubleArray.last(): Double {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[lastIndex]
}
@@ -1645,7 +1645,7 @@ public fun DoubleArray.last(): Double {
*/
public fun BooleanArray.last(): Boolean {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[lastIndex]
}
@@ -1655,7 +1655,7 @@ public fun BooleanArray.last(): Boolean {
*/
public fun CharArray.last(): Char {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Array is empty.")
return this[lastIndex]
}
@@ -1668,7 +1668,7 @@ public inline fun <T> Array<out T>.last(predicate: (T) -> Boolean): T {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -1680,7 +1680,7 @@ public inline fun ByteArray.last(predicate: (Byte) -> Boolean): Byte {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -1692,7 +1692,7 @@ public inline fun ShortArray.last(predicate: (Short) -> Boolean): Short {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -1704,7 +1704,7 @@ public inline fun IntArray.last(predicate: (Int) -> Boolean): Int {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -1716,7 +1716,7 @@ public inline fun LongArray.last(predicate: (Long) -> Boolean): Long {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -1728,7 +1728,7 @@ public inline fun FloatArray.last(predicate: (Float) -> Boolean): Float {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -1740,7 +1740,7 @@ public inline fun DoubleArray.last(predicate: (Double) -> Boolean): Double {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -1752,7 +1752,7 @@ public inline fun BooleanArray.last(predicate: (Boolean) -> Boolean): Boolean {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -1764,7 +1764,7 @@ public inline fun CharArray.last(predicate: (Char) -> Boolean): Char {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("Array contains no element matching the predicate.")
}
/**
@@ -2050,9 +2050,9 @@ public inline fun CharArray.lastOrNull(predicate: (Char) -> Boolean): Char? {
*/
public fun <T> Array<out T>.single(): T {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("Array has more than one element.")
}
}
@@ -2061,9 +2061,9 @@ public fun <T> Array<out T>.single(): T {
*/
public fun ByteArray.single(): Byte {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("Array has more than one element.")
}
}
@@ -2072,9 +2072,9 @@ public fun ByteArray.single(): Byte {
*/
public fun ShortArray.single(): Short {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("Array has more than one element.")
}
}
@@ -2083,9 +2083,9 @@ public fun ShortArray.single(): Short {
*/
public fun IntArray.single(): Int {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("Array has more than one element.")
}
}
@@ -2094,9 +2094,9 @@ public fun IntArray.single(): Int {
*/
public fun LongArray.single(): Long {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("Array has more than one element.")
}
}
@@ -2105,9 +2105,9 @@ public fun LongArray.single(): Long {
*/
public fun FloatArray.single(): Float {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("Array has more than one element.")
}
}
@@ -2116,9 +2116,9 @@ public fun FloatArray.single(): Float {
*/
public fun DoubleArray.single(): Double {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("Array has more than one element.")
}
}
@@ -2127,9 +2127,9 @@ public fun DoubleArray.single(): Double {
*/
public fun BooleanArray.single(): Boolean {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("Array has more than one element.")
}
}
@@ -2138,9 +2138,9 @@ public fun BooleanArray.single(): Boolean {
*/
public fun CharArray.single(): Char {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("Array has more than one element.")
}
}
@@ -2152,12 +2152,12 @@ public inline fun <T> Array<out T>.single(predicate: (T) -> Boolean): T {
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Array contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
return single as T
}
@@ -2169,12 +2169,12 @@ public inline fun ByteArray.single(predicate: (Byte) -> Boolean): Byte {
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Array contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
return single as Byte
}
@@ -2186,12 +2186,12 @@ public inline fun ShortArray.single(predicate: (Short) -> Boolean): Short {
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Array contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
return single as Short
}
@@ -2203,12 +2203,12 @@ public inline fun IntArray.single(predicate: (Int) -> Boolean): Int {
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Array contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
return single as Int
}
@@ -2220,12 +2220,12 @@ public inline fun LongArray.single(predicate: (Long) -> Boolean): Long {
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Array contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
return single as Long
}
@@ -2237,12 +2237,12 @@ public inline fun FloatArray.single(predicate: (Float) -> Boolean): Float {
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Array contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
return single as Float
}
@@ -2254,12 +2254,12 @@ public inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double {
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Array contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
return single as Double
}
@@ -2271,12 +2271,12 @@ public inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Array contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
return single as Boolean
}
@@ -2288,12 +2288,12 @@ public inline fun CharArray.single(predicate: (Char) -> Boolean): Char {
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Array contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
return single as Char
}
@@ -2517,7 +2517,7 @@ public inline fun CharArray.singleOrNull(predicate: (Char) -> Boolean): Char? {
* Returns a list containing all elements except first [n] elements.
*/
public fun <T> Array<out T>.drop(n: Int): List<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0)
return toList()
if (n >= size)
@@ -2533,7 +2533,7 @@ public fun <T> Array<out T>.drop(n: Int): List<T> {
* Returns a list containing all elements except first [n] elements.
*/
public fun ByteArray.drop(n: Int): List<Byte> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0)
return toList()
if (n >= size)
@@ -2549,7 +2549,7 @@ public fun ByteArray.drop(n: Int): List<Byte> {
* Returns a list containing all elements except first [n] elements.
*/
public fun ShortArray.drop(n: Int): List<Short> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0)
return toList()
if (n >= size)
@@ -2565,7 +2565,7 @@ public fun ShortArray.drop(n: Int): List<Short> {
* Returns a list containing all elements except first [n] elements.
*/
public fun IntArray.drop(n: Int): List<Int> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0)
return toList()
if (n >= size)
@@ -2581,7 +2581,7 @@ public fun IntArray.drop(n: Int): List<Int> {
* Returns a list containing all elements except first [n] elements.
*/
public fun LongArray.drop(n: Int): List<Long> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0)
return toList()
if (n >= size)
@@ -2597,7 +2597,7 @@ public fun LongArray.drop(n: Int): List<Long> {
* Returns a list containing all elements except first [n] elements.
*/
public fun FloatArray.drop(n: Int): List<Float> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0)
return toList()
if (n >= size)
@@ -2613,7 +2613,7 @@ public fun FloatArray.drop(n: Int): List<Float> {
* Returns a list containing all elements except first [n] elements.
*/
public fun DoubleArray.drop(n: Int): List<Double> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0)
return toList()
if (n >= size)
@@ -2629,7 +2629,7 @@ public fun DoubleArray.drop(n: Int): List<Double> {
* Returns a list containing all elements except first [n] elements.
*/
public fun BooleanArray.drop(n: Int): List<Boolean> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0)
return toList()
if (n >= size)
@@ -2645,7 +2645,7 @@ public fun BooleanArray.drop(n: Int): List<Boolean> {
* Returns a list containing all elements except first [n] elements.
*/
public fun CharArray.drop(n: Int): List<Char> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0)
return toList()
if (n >= size)
@@ -2661,7 +2661,7 @@ public fun CharArray.drop(n: Int): List<Char> {
* Returns a list containing all elements except last [n] elements.
*/
public fun <T> Array<out T>.dropLast(n: Int): List<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
@@ -2669,7 +2669,7 @@ public fun <T> Array<out T>.dropLast(n: Int): List<T> {
* Returns a list containing all elements except last [n] elements.
*/
public fun ByteArray.dropLast(n: Int): List<Byte> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
@@ -2677,7 +2677,7 @@ public fun ByteArray.dropLast(n: Int): List<Byte> {
* Returns a list containing all elements except last [n] elements.
*/
public fun ShortArray.dropLast(n: Int): List<Short> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
@@ -2685,7 +2685,7 @@ public fun ShortArray.dropLast(n: Int): List<Short> {
* Returns a list containing all elements except last [n] elements.
*/
public fun IntArray.dropLast(n: Int): List<Int> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
@@ -2693,7 +2693,7 @@ public fun IntArray.dropLast(n: Int): List<Int> {
* Returns a list containing all elements except last [n] elements.
*/
public fun LongArray.dropLast(n: Int): List<Long> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
@@ -2701,7 +2701,7 @@ public fun LongArray.dropLast(n: Int): List<Long> {
* Returns a list containing all elements except last [n] elements.
*/
public fun FloatArray.dropLast(n: Int): List<Float> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
@@ -2709,7 +2709,7 @@ public fun FloatArray.dropLast(n: Int): List<Float> {
* Returns a list containing all elements except last [n] elements.
*/
public fun DoubleArray.dropLast(n: Int): List<Double> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
@@ -2717,7 +2717,7 @@ public fun DoubleArray.dropLast(n: Int): List<Double> {
* Returns a list containing all elements except last [n] elements.
*/
public fun BooleanArray.dropLast(n: Int): List<Boolean> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
@@ -2725,7 +2725,7 @@ public fun BooleanArray.dropLast(n: Int): List<Boolean> {
* Returns a list containing all elements except last [n] elements.
*/
public fun CharArray.dropLast(n: Int): List<Char> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
@@ -3981,7 +3981,7 @@ public fun CharArray.take(n: Int): List<Char> {
* Returns a list containing last [n] elements.
*/
public fun <T> Array<out T>.takeLast(n: Int): List<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
@@ -3995,7 +3995,7 @@ public fun <T> Array<out T>.takeLast(n: Int): List<T> {
* Returns a list containing last [n] elements.
*/
public fun ByteArray.takeLast(n: Int): List<Byte> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
@@ -4009,7 +4009,7 @@ public fun ByteArray.takeLast(n: Int): List<Byte> {
* Returns a list containing last [n] elements.
*/
public fun ShortArray.takeLast(n: Int): List<Short> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
@@ -4023,7 +4023,7 @@ public fun ShortArray.takeLast(n: Int): List<Short> {
* Returns a list containing last [n] elements.
*/
public fun IntArray.takeLast(n: Int): List<Int> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
@@ -4037,7 +4037,7 @@ public fun IntArray.takeLast(n: Int): List<Int> {
* Returns a list containing last [n] elements.
*/
public fun LongArray.takeLast(n: Int): List<Long> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
@@ -4051,7 +4051,7 @@ public fun LongArray.takeLast(n: Int): List<Long> {
* Returns a list containing last [n] elements.
*/
public fun FloatArray.takeLast(n: Int): List<Float> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
@@ -4065,7 +4065,7 @@ public fun FloatArray.takeLast(n: Int): List<Float> {
* Returns a list containing last [n] elements.
*/
public fun DoubleArray.takeLast(n: Int): List<Double> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
@@ -4079,7 +4079,7 @@ public fun DoubleArray.takeLast(n: Int): List<Double> {
* Returns a list containing last [n] elements.
*/
public fun BooleanArray.takeLast(n: Int): List<Boolean> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
@@ -4093,7 +4093,7 @@ public fun BooleanArray.takeLast(n: Int): List<Boolean> {
* Returns a list containing last [n] elements.
*/
public fun CharArray.takeLast(n: Int): List<Char> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
@@ -10351,7 +10351,7 @@ public inline fun CharArray.reduceIndexed(operation: (Int, Char, Char) -> Char):
*/
public inline fun <S, T: S> Array<out T>.reduceRight(operation: (T, S) -> S): S {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
var accumulator: S = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
@@ -10364,7 +10364,7 @@ public inline fun <S, T: S> Array<out T>.reduceRight(operation: (T, S) -> S): S
*/
public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
@@ -10377,7 +10377,7 @@ public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte {
*/
public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Short {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
@@ -10390,7 +10390,7 @@ public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Sh
*/
public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int): Int {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
@@ -10403,7 +10403,7 @@ public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int): Int {
*/
public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long): Long {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
@@ -10416,7 +10416,7 @@ public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long): Long {
*/
public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float): Float {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
@@ -10429,7 +10429,7 @@ public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float): Fl
*/
public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double): Double {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
@@ -10442,7 +10442,7 @@ public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double)
*/
public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Boolean): Boolean {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
@@ -10455,7 +10455,7 @@ public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Bool
*/
public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char): Char {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
+18 -32
View File
@@ -161,12 +161,7 @@ public inline fun <T> List<T>.findLast(predicate: (T) -> Boolean): T? {
*/
public fun <T> Iterable<T>.first(): T {
when (this) {
is List -> {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
else
return this[0]
}
is List -> return this.first()
else -> {
val iterator = iterator()
if (!iterator.hasNext())
@@ -182,7 +177,7 @@ public fun <T> Iterable<T>.first(): T {
*/
public fun <T> List<T>.first(): T {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("List is empty.")
return this[0]
}
@@ -192,7 +187,7 @@ public fun <T> List<T>.first(): T {
*/
public inline fun <T> Iterable<T>.first(predicate: (T) -> Boolean): T {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Collection contains no element matching the predicate.")
}
/**
@@ -323,12 +318,7 @@ public inline fun <T> List<T>.indexOfLast(predicate: (T) -> Boolean): Int {
*/
public fun <T> Iterable<T>.last(): T {
when (this) {
is List -> {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
else
return this[this.lastIndex]
}
is List -> return this.last()
else -> {
val iterator = iterator()
if (!iterator.hasNext())
@@ -347,7 +337,7 @@ public fun <T> Iterable<T>.last(): T {
*/
public fun <T> List<T>.last(): T {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("List is empty.")
return this[lastIndex]
}
@@ -366,7 +356,7 @@ public inline fun <T> Iterable<T>.last(predicate: (T) -> Boolean): T {
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.")
return last as T
}
@@ -379,7 +369,7 @@ public inline fun <T> List<T>.last(predicate: (T) -> Boolean): T {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("List contains no element matching the predicate.")
}
/**
@@ -460,11 +450,7 @@ public inline fun <T> List<T>.lastOrNull(predicate: (T) -> Boolean): T? {
*/
public fun <T> Iterable<T>.single(): T {
when (this) {
is List -> return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
}
is List -> return this.single()
else -> {
val iterator = iterator()
if (!iterator.hasNext())
@@ -482,9 +468,9 @@ public fun <T> Iterable<T>.single(): T {
*/
public fun <T> List<T>.single(): T {
return when (size) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("List is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("List has more than one element.")
}
}
@@ -501,7 +487,7 @@ public inline fun <T> Iterable<T>.single(predicate: (T) -> Boolean): T {
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.")
return single as T
}
@@ -551,7 +537,7 @@ public inline fun <T> Iterable<T>.singleOrNull(predicate: (T) -> Boolean): T? {
* Returns a list containing all elements except first [n] elements.
*/
public fun <T> Iterable<T>.drop(n: Int): List<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return toList()
val list: ArrayList<T>
if (this is Collection<*>) {
@@ -580,7 +566,7 @@ public fun <T> Iterable<T>.drop(n: Int): List<T> {
* Returns a list containing all elements except last [n] elements.
*/
public fun <T> List<T>.dropLast(n: Int): List<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return take((size - n).coerceAtLeast(0))
}
@@ -703,7 +689,7 @@ public fun <T> List<T>.slice(indices: Iterable<Int>): List<T> {
* Returns a list containing first [n] elements.
*/
public fun <T> Iterable<T>.take(n: Int): List<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
if (this is Collection<T> && n >= size) return toList()
var count = 0
@@ -720,7 +706,7 @@ public fun <T> Iterable<T>.take(n: Int): List<T> {
* Returns a list containing last [n] elements.
*/
public fun <T> List<T>.takeLast(n: Int): List<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
if (n == 0) return emptyList()
val size = size
if (n >= size) return toList()
@@ -1509,7 +1495,7 @@ public inline fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean {
*/
public inline fun <S, T: S> Iterable<T>.reduce(operation: (S, T) -> S): S {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.")
var accumulator: S = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
@@ -1525,7 +1511,7 @@ public inline fun <S, T: S> Iterable<T>.reduce(operation: (S, T) -> S): S {
*/
public inline fun <S, T: S> Iterable<T>.reduceIndexed(operation: (Int, S, T) -> S): S {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.")
var index = 1
var accumulator: S = iterator.next()
while (iterator.hasNext()) {
@@ -1539,7 +1525,7 @@ public inline fun <S, T: S> Iterable<T>.reduceIndexed(operation: (Int, S, T) ->
*/
public inline fun <S, T: S> List<T>.reduceRight(operation: (T, S) -> S): S {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty list can't be reduced.")
var accumulator: S = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)
+8 -8
View File
@@ -92,7 +92,7 @@ public fun <T> Sequence<T>.first(): T {
*/
public inline fun <T> Sequence<T>.first(predicate: (T) -> Boolean): T {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Sequence contains no element matching the predicate.")
}
/**
@@ -180,7 +180,7 @@ public inline fun <T> Sequence<T>.last(predicate: (T) -> Boolean): T {
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.")
return last as T
}
@@ -245,12 +245,12 @@ public inline fun <T> Sequence<T>.single(predicate: (T) -> Boolean): T {
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Sequence contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.")
return single as T
}
@@ -288,7 +288,7 @@ public inline fun <T> Sequence<T>.singleOrNull(predicate: (T) -> Boolean): T? {
* Returns a sequence containing all elements except first [n] elements.
*/
public fun <T> Sequence<T>.drop(n: Int): Sequence<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return when {
n == 0 -> this
this is DropTakeSequence -> this.drop(n)
@@ -375,7 +375,7 @@ public inline fun <T, C : MutableCollection<in T>> Sequence<T>.filterTo(destinat
* Returns a sequence containing first [n] elements.
*/
public fun <T> Sequence<T>.take(n: Int): Sequence<T> {
require(n >= 0, { "Requested element count $n is less than zero." })
require(n >= 0) { "Requested element count $n is less than zero." }
return when {
n == 0 -> emptySequence()
this is DropTakeSequence -> this.take(n)
@@ -931,7 +931,7 @@ public inline fun <T> Sequence<T>.none(predicate: (T) -> Boolean): Boolean {
*/
public inline fun <S, T: S> Sequence<T>.reduce(operation: (S, T) -> S): S {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty sequence can't be reduced.")
var accumulator: S = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
@@ -947,7 +947,7 @@ public inline fun <S, T: S> Sequence<T>.reduce(operation: (S, T) -> S): S {
*/
public inline fun <S, T: S> Sequence<T>.reduceIndexed(operation: (Int, S, T) -> S): S {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty sequence can't be reduced.")
var index = 1
var accumulator: S = iterator.next()
while (iterator.hasNext()) {
+17 -17
View File
@@ -59,7 +59,7 @@ public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? {
*/
public fun CharSequence.first(): Char {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Char sequence is empty.")
return this[0]
}
@@ -69,7 +69,7 @@ public fun CharSequence.first(): Char {
*/
public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char {
for (element in this) if (predicate(element)) return element
throw NoSuchElementException("No element matching predicate was found.")
throw NoSuchElementException("Char sequence contains no character matching the predicate.")
}
/**
@@ -132,7 +132,7 @@ public inline fun CharSequence.indexOfLast(predicate: (Char) -> Boolean): Int {
*/
public fun CharSequence.last(): Char {
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
throw NoSuchElementException("Char sequence is empty.")
return this[lastIndex]
}
@@ -145,7 +145,7 @@ public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char {
val element = this[index]
if (predicate(element)) return element
}
throw NoSuchElementException("Collection doesn't contain any element matching the predicate.")
throw NoSuchElementException("Char sequence contains no character matching the predicate.")
}
/**
@@ -171,9 +171,9 @@ public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? {
*/
public fun CharSequence.single(): Char {
return when (length) {
0 -> throw NoSuchElementException("Collection is empty.")
0 -> throw NoSuchElementException("Char sequence is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
else -> throw IllegalArgumentException("Char sequence has more than one element.")
}
}
@@ -185,12 +185,12 @@ public inline fun CharSequence.single(predicate: (Char) -> Boolean): Char {
var found = false
for (element in this) {
if (predicate(element)) {
if (found) throw IllegalArgumentException("Collection contains more than one matching element.")
if (found) throw IllegalArgumentException("Char sequence contains more than one matching element.")
single = element
found = true
}
}
if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.")
if (!found) throw NoSuchElementException("Char sequence contains no character matching the predicate.")
return single as Char
}
@@ -222,7 +222,7 @@ public inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char?
* Returns a subsequence of this char sequence with the first [n] characters removed.
*/
public fun CharSequence.drop(n: Int): CharSequence {
require(n >= 0, { "Requested character count $n is less than zero." })
require(n >= 0) { "Requested character count $n is less than zero." }
return subSequence(n.coerceAtMost(length), length)
}
@@ -230,7 +230,7 @@ public fun CharSequence.drop(n: Int): CharSequence {
* Returns a string with the first [n] characters removed.
*/
public fun String.drop(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." })
require(n >= 0) { "Requested character count $n is less than zero." }
return substring(n.coerceAtMost(length))
}
@@ -238,7 +238,7 @@ public fun String.drop(n: Int): String {
* Returns a subsequence of this char sequence with the last [n] characters removed.
*/
public fun CharSequence.dropLast(n: Int): CharSequence {
require(n >= 0, { "Requested character count $n is less than zero." })
require(n >= 0) { "Requested character count $n is less than zero." }
return take((length - n).coerceAtLeast(0))
}
@@ -246,7 +246,7 @@ public fun CharSequence.dropLast(n: Int): CharSequence {
* Returns a string with the last [n] characters removed.
*/
public fun String.dropLast(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." })
require(n >= 0) { "Requested character count $n is less than zero." }
return take((length - n).coerceAtLeast(0))
}
@@ -408,7 +408,7 @@ public inline fun String.slice(indices: Iterable<Int>): String {
* Returns a subsequence of this char sequence containing the first [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter.
*/
public fun CharSequence.take(n: Int): CharSequence {
require(n >= 0, { "Requested character count $n is less than zero." })
require(n >= 0) { "Requested character count $n is less than zero." }
return subSequence(0, n.coerceAtMost(length))
}
@@ -416,7 +416,7 @@ public fun CharSequence.take(n: Int): CharSequence {
* Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter.
*/
public fun String.take(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." })
require(n >= 0) { "Requested character count $n is less than zero." }
return substring(0, n.coerceAtMost(length))
}
@@ -424,7 +424,7 @@ public fun String.take(n: Int): String {
* Returns a subsequence of this char sequence containing the last [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter.
*/
public fun CharSequence.takeLast(n: Int): CharSequence {
require(n >= 0, { "Requested character count $n is less than zero." })
require(n >= 0) { "Requested character count $n is less than zero." }
val length = length
return subSequence(length - n.coerceAtMost(length), length)
}
@@ -433,7 +433,7 @@ public fun CharSequence.takeLast(n: Int): CharSequence {
* Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter.
*/
public fun String.takeLast(n: Int): String {
require(n >= 0, { "Requested character count $n is less than zero." })
require(n >= 0) { "Requested character count $n is less than zero." }
val length = length
return substring(length - n.coerceAtMost(length))
}
@@ -1014,7 +1014,7 @@ public inline fun CharSequence.reduceIndexed(operation: (Int, Char, Char) -> Cha
*/
public inline fun CharSequence.reduceRight(operation: (Char, Char) -> Char): Char {
var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.")
if (index < 0) throw UnsupportedOperationException("Empty char sequence can't be reduced.")
var accumulator = get(index--)
while (index >= 0) {
accumulator = operation(get(index--), accumulator)