diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index dfdb51caeb5..50809a1b32e 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -443,90 +443,63 @@ public operator fun CharArray.contains(element: Char): Boolean { * * @sample samples.collections.Collections.Elements.elementAt */ -@kotlin.internal.InlineOnly -public inline fun Array.elementAt(index: Int): T { - return get(index) -} +public expect fun Array.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. diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index b79ceaaf724..e3ef46f5fa8 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -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. diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 9f5dc876b94..b869a93f94e 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -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. diff --git a/libraries/stdlib/js/irRuntime/generated/_ArraysJs.kt b/libraries/stdlib/js/irRuntime/generated/_ArraysJs.kt index 3cc16a4f3cc..5cf88142882 100644 --- a/libraries/stdlib/js/irRuntime/generated/_ArraysJs.kt +++ b/libraries/stdlib/js/irRuntime/generated/_ArraysJs.kt @@ -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 Array.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. */ diff --git a/libraries/stdlib/js/irRuntime/generated/_StringsJs.kt b/libraries/stdlib/js/irRuntime/generated/_StringsJs.kt new file mode 100644 index 00000000000..71ff3153d1a --- /dev/null +++ b/libraries/stdlib/js/irRuntime/generated/_StringsJs.kt @@ -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}") } +} + diff --git a/libraries/stdlib/js/irRuntime/generated/_UArraysJs.kt b/libraries/stdlib/js/irRuntime/generated/_UArraysJs.kt index d5e1b99b498..ca9ddaaa72e 100644 --- a/libraries/stdlib/js/irRuntime/generated/_UArraysJs.kt +++ b/libraries/stdlib/js/irRuntime/generated/_UArraysJs.kt @@ -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. */ diff --git a/libraries/stdlib/js/src/generated/_ArraysJs.kt b/libraries/stdlib/js/src/generated/_ArraysJs.kt index 99b92ea31e0..614c4816d3b 100644 --- a/libraries/stdlib/js/src/generated/_ArraysJs.kt +++ b/libraries/stdlib/js/src/generated/_ArraysJs.kt @@ -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 Array.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. */ diff --git a/libraries/stdlib/js/src/generated/_StringsJs.kt b/libraries/stdlib/js/src/generated/_StringsJs.kt new file mode 100644 index 00000000000..71ff3153d1a --- /dev/null +++ b/libraries/stdlib/js/src/generated/_StringsJs.kt @@ -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}") } +} + diff --git a/libraries/stdlib/js/src/generated/_UArraysJs.kt b/libraries/stdlib/js/src/generated/_UArraysJs.kt index d5e1b99b498..ca9ddaaa72e 100644 --- a/libraries/stdlib/js/src/generated/_UArraysJs.kt +++ b/libraries/stdlib/js/src/generated/_UArraysJs.kt @@ -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. */ diff --git a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt index 4ef29d904bf..7270d9ea4ea 100644 --- a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt @@ -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 Array.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. */ diff --git a/libraries/stdlib/jvm/src/generated/_StringsJvm.kt b/libraries/stdlib/jvm/src/generated/_StringsJvm.kt index 8aad303c280..4468fc424c7 100644 --- a/libraries/stdlib/jvm/src/generated/_StringsJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_StringsJvm.kt @@ -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. */ diff --git a/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt index 619da7aae7a..87f62962386 100644 --- a/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_UArraysJvm.kt @@ -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. */ diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 918a0a48df4..631110c9bd1 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -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 { arrayOf().elementAt(0) } + assertFailsWith { longArrayOf(0, 1, 2).elementAt(-1) } + } } diff --git a/libraries/stdlib/test/collections/UnsignedArraysTest.kt b/libraries/stdlib/test/collections/UnsignedArraysTest.kt index e988dc0b2ad..b71431c3cae 100644 --- a/libraries/stdlib/test/collections/UnsignedArraysTest.kt +++ b/libraries/stdlib/test/collections/UnsignedArraysTest.kt @@ -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 { uintArrayOf().elementAt(0) } -// assertFailsWith { ulongArrayOf(0, 1, 2).elementAt(-1) } + assertFailsWith { uintArrayOf().elementAt(0) } + assertFailsWith { ulongArrayOf(0, 1, 2).elementAt(-1) } } @Test diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index ded4bc92967..37b614f406d 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -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 { "".elementAt(0) } + assertFailsWith { "a c".elementAt(-1) } + } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index 0db30d377ad..5f2563eda58 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -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)") {