Check for index-out-of-bound in elementAt function (KT-30051)

This commit is contained in:
Abduqodiri Qurbonzoda
2019-03-07 22:10:29 +03:00
parent 584137121b
commit ba61695a45
16 changed files with 496 additions and 60 deletions
@@ -14,6 +14,87 @@ import kotlin.js.*
import primitiveArrayConcat
import withType
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public actual fun <T> Array<out T>.elementAt(index: Int): T {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public actual fun ByteArray.elementAt(index: Int): Byte {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public actual fun ShortArray.elementAt(index: Int): Short {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public actual fun IntArray.elementAt(index: Int): Int {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public actual fun LongArray.elementAt(index: Int): Long {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public actual fun FloatArray.elementAt(index: Int): Float {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public actual fun DoubleArray.elementAt(index: Int): Double {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public actual fun BooleanArray.elementAt(index: Int): Boolean {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public actual fun CharArray.elementAt(index: Int): Char {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns a [List] that wraps the original array.
*/
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.text
//
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.js.*
/**
* Returns a character at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this char sequence.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
public actual fun CharSequence.elementAt(index: Int): Char {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, length: $length}") }
}
@@ -12,6 +12,50 @@ package kotlin.collections
import kotlin.js.*
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public actual fun UIntArray.elementAt(index: Int): UInt {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public actual fun ULongArray.elementAt(index: Int): ULong {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public actual fun UByteArray.elementAt(index: Int): UByte {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
*
* @sample samples.collections.Collections.Elements.elementAt
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
public actual fun UShortArray.elementAt(index: Int): UShort {
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, size: $size}") }
}
/**
* Returns a [List] that wraps the original array.
*/