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
@@ -443,90 +443,63 @@ public operator fun CharArray.contains(element: Char): Boolean {
*
* @sample samples.collections.Collections.Elements.elementAt
*/
@kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.elementAt(index: Int): T {
return get(index)
}
public expect fun <T> Array<out T>.elementAt(index: Int): T
/**
* 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
*/
@kotlin.internal.InlineOnly
public inline fun ByteArray.elementAt(index: Int): Byte {
return get(index)
}
public expect fun ByteArray.elementAt(index: Int): Byte
/**
* 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
*/
@kotlin.internal.InlineOnly
public inline fun ShortArray.elementAt(index: Int): Short {
return get(index)
}
public expect fun ShortArray.elementAt(index: Int): Short
/**
* 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
*/
@kotlin.internal.InlineOnly
public inline fun IntArray.elementAt(index: Int): Int {
return get(index)
}
public expect fun IntArray.elementAt(index: Int): Int
/**
* 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
*/
@kotlin.internal.InlineOnly
public inline fun LongArray.elementAt(index: Int): Long {
return get(index)
}
public expect fun LongArray.elementAt(index: Int): Long
/**
* 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
*/
@kotlin.internal.InlineOnly
public inline fun FloatArray.elementAt(index: Int): Float {
return get(index)
}
public expect fun FloatArray.elementAt(index: Int): Float
/**
* 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
*/
@kotlin.internal.InlineOnly
public inline fun DoubleArray.elementAt(index: Int): Double {
return get(index)
}
public expect fun DoubleArray.elementAt(index: Int): Double
/**
* 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
*/
@kotlin.internal.InlineOnly
public inline fun BooleanArray.elementAt(index: Int): Boolean {
return get(index)
}
public expect fun BooleanArray.elementAt(index: Int): Boolean
/**
* 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
*/
@kotlin.internal.InlineOnly
public inline fun CharArray.elementAt(index: Int): Char {
return get(index)
}
public expect fun CharArray.elementAt(index: Int): Char
/**
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
@@ -20,10 +20,7 @@ import kotlin.random.*
*
* @sample samples.collections.Collections.Elements.elementAt
*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.elementAt(index: Int): Char {
return get(index)
}
public expect fun CharSequence.elementAt(index: Int): Char
/**
* Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence.
@@ -223,10 +223,7 @@ public inline operator fun UShortArray.component5(): UShort {
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.elementAt(index: Int): UInt {
return get(index)
}
public expect fun UIntArray.elementAt(index: Int): UInt
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
@@ -235,10 +232,7 @@ public inline fun UIntArray.elementAt(index: Int): UInt {
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.elementAt(index: Int): ULong {
return get(index)
}
public expect fun ULongArray.elementAt(index: Int): ULong
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
@@ -247,10 +241,7 @@ public inline fun ULongArray.elementAt(index: Int): ULong {
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.elementAt(index: Int): UByte {
return get(index)
}
public expect fun UByteArray.elementAt(index: Int): UByte
/**
* Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array.
@@ -259,10 +250,7 @@ public inline fun UByteArray.elementAt(index: Int): UByte {
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.elementAt(index: Int): UShort {
return get(index)
}
public expect fun UShortArray.elementAt(index: Int): UShort
/**
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
@@ -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.
*/
@@ -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.
*/
@@ -14,6 +14,96 @@ package kotlin.collections
//
/**
* 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
*/
@kotlin.internal.InlineOnly
public actual inline fun <T> Array<out T>.elementAt(index: Int): T {
return get(index)
}
/**
* 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
*/
@kotlin.internal.InlineOnly
public actual inline fun ByteArray.elementAt(index: Int): Byte {
return get(index)
}
/**
* 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
*/
@kotlin.internal.InlineOnly
public actual inline fun ShortArray.elementAt(index: Int): Short {
return get(index)
}
/**
* 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
*/
@kotlin.internal.InlineOnly
public actual inline fun IntArray.elementAt(index: Int): Int {
return get(index)
}
/**
* 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
*/
@kotlin.internal.InlineOnly
public actual inline fun LongArray.elementAt(index: Int): Long {
return get(index)
}
/**
* 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
*/
@kotlin.internal.InlineOnly
public actual inline fun FloatArray.elementAt(index: Int): Float {
return get(index)
}
/**
* 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
*/
@kotlin.internal.InlineOnly
public actual inline fun DoubleArray.elementAt(index: Int): Double {
return get(index)
}
/**
* 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
*/
@kotlin.internal.InlineOnly
public actual inline fun BooleanArray.elementAt(index: Int): Boolean {
return get(index)
}
/**
* 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
*/
@kotlin.internal.InlineOnly
public actual inline fun CharArray.elementAt(index: Int): Char {
return get(index)
}
/**
* Returns a list containing all elements that are instances of specified class.
*/
@@ -14,6 +14,16 @@ package kotlin.text
//
/**
* 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
*/
@kotlin.internal.InlineOnly
public actual inline fun CharSequence.elementAt(index: Int): Char {
return get(index)
}
/**
* Returns a [SortedSet][java.util.SortedSet] of all characters.
*/
@@ -15,6 +15,54 @@ package kotlin.collections
//
/**
* 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
@kotlin.internal.InlineOnly
public actual inline fun UIntArray.elementAt(index: Int): UInt {
return get(index)
}
/**
* 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
@kotlin.internal.InlineOnly
public actual inline fun ULongArray.elementAt(index: Int): ULong {
return get(index)
}
/**
* 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
@kotlin.internal.InlineOnly
public actual inline fun UByteArray.elementAt(index: Int): UByte {
return get(index)
}
/**
* 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
@kotlin.internal.InlineOnly
public actual inline fun UShortArray.elementAt(index: Int): UShort {
return get(index)
}
/**
* Returns a [List] that wraps the original array.
*/
@@ -1490,6 +1490,16 @@ class ArraysTest {
array.sortWith(comparator)
array.iterator().assertSorted { a, b -> comparator.compare(a, b) <= 0 }
}
@Test
fun elementAt() {
expect(0) { byteArrayOf(0, 1, 2).elementAt(0) }
expect(1) { shortArrayOf(0, 1, 2).elementAt(1) }
expect(2) { intArrayOf(0, 1, 2).elementAt(2) }
assertFailsWith<IndexOutOfBoundsException> { arrayOf<String>().elementAt(0) }
assertFailsWith<IndexOutOfBoundsException> { longArrayOf(0, 1, 2).elementAt(-1) }
}
}
@@ -625,10 +625,8 @@ class UnsignedArraysTest {
expect(1u) { ushortArrayOf(0, 1, 2).elementAt(1) }
expect(2u) { uintArrayOf(0, 1, 2).elementAt(2) }
// TODO: Uncomment these tests after KT-30051 gets fixed.
// Currently JS does not throw exception on incorrect index.
// assertFailsWith<IndexOutOfBoundsException> { uintArrayOf().elementAt(0) }
// assertFailsWith<IndexOutOfBoundsException> { ulongArrayOf(0, 1, 2).elementAt(-1) }
assertFailsWith<IndexOutOfBoundsException> { uintArrayOf().elementAt(0) }
assertFailsWith<IndexOutOfBoundsException> { ulongArrayOf(0, 1, 2).elementAt(-1) }
}
@Test
+10
View File
@@ -1460,4 +1460,14 @@ ${" "}
assertEquals(" ABC\n \n 123", "ABC\n \n123".prependIndent(" "))
assertEquals(" ", "".prependIndent(" "))
}
@Test
fun elementAt() {
expect('a') { "a c".elementAt(0) }
expect(' ') { "a c".elementAt(1) }
expect('c') { "a c".elementAt(2) }
assertFailsWith<IndexOutOfBoundsException> { "".elementAt(0) }
assertFailsWith<IndexOutOfBoundsException> { "a c".elementAt(-1) }
}
}
@@ -284,6 +284,22 @@ object Elements : TemplateGroupBase() {
inlineOnly()
body { "return get(index)" }
}
specialFor(CharSequences, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
on(Platform.Common) {
inline(Inline.No)
}
on(Platform.JS) {
inline(Inline.No)
val size = if (f == CharSequences) "length" else "size"
body {
"""
return elementAtOrElse(index) { throw IndexOutOfBoundsException("index: $index, $size: $$size}") }
"""
}
}
}
}
val f_elementAtOrElse = fn("elementAtOrElse(index: Int, defaultValue: (Int) -> T)") {