diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 10cd0c70d33..3a0614e585e 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -5572,6 +5572,177 @@ public expect fun BooleanArray.contentToString(): String @SinceKotlin("1.1") public expect fun CharArray.contentToString(): String +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +public expect fun Array.copyInto(destination: Array, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): Array + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +public expect fun ByteArray.copyInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): ByteArray + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +public expect fun ShortArray.copyInto(destination: ShortArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): ShortArray + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +public expect fun IntArray.copyInto(destination: IntArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): IntArray + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +public expect fun LongArray.copyInto(destination: LongArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): LongArray + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +public expect fun FloatArray.copyInto(destination: FloatArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): FloatArray + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +public expect fun DoubleArray.copyInto(destination: DoubleArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): DoubleArray + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +public expect fun BooleanArray.copyInto(destination: BooleanArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): BooleanArray + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +public expect fun CharArray.copyInto(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): CharArray + /** * Returns new array which is a copy of the original array. * diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 44b9403da98..3afc5cfa879 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -225,6 +225,98 @@ public fun UShortArray.contentToString(): String { return joinToString(", ", "[", "]") } +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.copyInto(destination: UIntArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): UIntArray { + return UIntArray(storage.copyInto(destination.storage, destinationOffset, startIndex, endIndex)) +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.copyInto(destination: ULongArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): ULongArray { + return ULongArray(storage.copyInto(destination.storage, destinationOffset, startIndex, endIndex)) +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.copyInto(destination: UByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): UByteArray { + return UByteArray(storage.copyInto(destination.storage, destinationOffset, startIndex, endIndex)) +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.copyInto(destination: UShortArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): UShortArray { + return UShortArray(storage.copyInto(destination.storage, destinationOffset, startIndex, endIndex)) +} + /** * Returns new array which is a copy of the original array. * diff --git a/libraries/stdlib/js/src/generated/_ArraysJs.kt b/libraries/stdlib/js/src/generated/_ArraysJs.kt index 2d257eb36e0..7edc01d48c6 100644 --- a/libraries/stdlib/js/src/generated/_ArraysJs.kt +++ b/libraries/stdlib/js/src/generated/_ArraysJs.kt @@ -407,6 +407,222 @@ public actual fun CharArray.contentToString(): String { definedExternally } +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@kotlin.internal.InlineOnly +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual inline fun Array.copyInto(destination: Array, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): Array { + arrayCopy(this, destination, destinationOffset, startIndex, endIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@kotlin.internal.InlineOnly +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual inline fun ByteArray.copyInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ByteArray { + arrayCopy(this.unsafeCast>(), destination.unsafeCast>(), destinationOffset, startIndex, endIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@kotlin.internal.InlineOnly +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual inline fun ShortArray.copyInto(destination: ShortArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ShortArray { + arrayCopy(this.unsafeCast>(), destination.unsafeCast>(), destinationOffset, startIndex, endIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@kotlin.internal.InlineOnly +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual inline fun IntArray.copyInto(destination: IntArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): IntArray { + arrayCopy(this.unsafeCast>(), destination.unsafeCast>(), destinationOffset, startIndex, endIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@kotlin.internal.InlineOnly +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual inline fun LongArray.copyInto(destination: LongArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): LongArray { + arrayCopy(this.unsafeCast>(), destination.unsafeCast>(), destinationOffset, startIndex, endIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@kotlin.internal.InlineOnly +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual inline fun FloatArray.copyInto(destination: FloatArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): FloatArray { + arrayCopy(this.unsafeCast>(), destination.unsafeCast>(), destinationOffset, startIndex, endIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@kotlin.internal.InlineOnly +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual inline fun DoubleArray.copyInto(destination: DoubleArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): DoubleArray { + arrayCopy(this.unsafeCast>(), destination.unsafeCast>(), destinationOffset, startIndex, endIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@kotlin.internal.InlineOnly +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual inline fun BooleanArray.copyInto(destination: BooleanArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): BooleanArray { + arrayCopy(this.unsafeCast>(), destination.unsafeCast>(), destinationOffset, startIndex, endIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@kotlin.internal.InlineOnly +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual inline fun CharArray.copyInto(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): CharArray { + arrayCopy(this.unsafeCast>(), destination.unsafeCast>(), destinationOffset, startIndex, endIndex) + return destination +} + /** * Returns new array which is a copy of the original array. * diff --git a/libraries/stdlib/js/src/kotlin/collections.kt b/libraries/stdlib/js/src/kotlin/collections.kt index 68cc558bf40..fa9c69c59cd 100644 --- a/libraries/stdlib/js/src/kotlin/collections.kt +++ b/libraries/stdlib/js/src/kotlin/collections.kt @@ -126,6 +126,27 @@ internal actual fun arrayOfNulls(reference: Array, size: Int): Array { return arrayOfNulls(size).unsafeCast>() } +@SinceKotlin("1.3") +@PublishedApi +@JsName("arrayCopy") +internal fun arrayCopy(source: Array, destination: Array, destinationOffset: Int, startIndex: Int, endIndex: Int) { + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) source.size else endIndex // TODO: Remove when default value from expect is fixed + AbstractList.checkRangeIndexes(startIndex, endIndex, source.size) + val rangeSize = endIndex - startIndex + AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size) + + if (source !== destination || destinationOffset <= startIndex) { + for (index in 0 until rangeSize) { + destination[destinationOffset + index] = source[startIndex + index] + } + } else { + for (index in rangeSize - 1 downTo 0) { + destination[destinationOffset + index] = source[startIndex + index] + } + } +} + // no singleton map implementation in js, return map as is @Suppress("NOTHING_TO_INLINE") internal actual inline fun Map.toSingletonMapOrSelf(): Map = this diff --git a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt index 50d0ab423f4..ec2f042bf30 100644 --- a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt @@ -597,6 +597,231 @@ public actual inline fun CharArray.contentToString(): String { return java.util.Arrays.toString(this) } +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun Array.copyInto(destination: Array, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): Array { + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed + System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun ByteArray.copyInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ByteArray { + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed + System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun ShortArray.copyInto(destination: ShortArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ShortArray { + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed + System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun IntArray.copyInto(destination: IntArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): IntArray { + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed + System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun LongArray.copyInto(destination: LongArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): LongArray { + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed + System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun FloatArray.copyInto(destination: FloatArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): FloatArray { + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed + System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun DoubleArray.copyInto(destination: DoubleArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): DoubleArray { + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed + System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun BooleanArray.copyInto(destination: BooleanArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): BooleanArray { + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed + System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) + return destination +} + +/** + * Copies this array or its subrange into the [destination] array and returns that array. + * + * It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + * + * @param destination the array to copy to. + * @param destinationOffset the position in the [destination] array to copy to, 0 by default. + * @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + * @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + * + * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + * @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + * or when that index is out of the [destination] array indices range. + * + * @return the [destination] array. + */ +@SinceKotlin("1.3") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun CharArray.copyInto(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): CharArray { + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed + System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) + return destination +} + /** * Returns new array which is a copy of the original array. * diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 775481c98c1..aaf6484521a 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -5,6 +5,8 @@ package test.collections +import test.assertStaticTypeIs +import test.assertTypeEquals import test.collections.behaviors.* import test.comparisons.STRING_CASE_INSENSITIVE_ORDER import kotlin.test.* @@ -789,6 +791,98 @@ class ArraysTest { } + @Test fun copyRangeInto() { + fun doTest( + copyInto: T.(T, Int, Int, Int) -> T, + assertTEquals: (T, T, String) -> Unit, + toStringT: T.() -> String, + dest: T, newValues: T, + result1: T, result2: T, result3: T + ) { + newValues.copyInto(dest, 0, 1, 3) + assertTypeEquals(result1, dest) + assertTEquals(result1, dest, "Copying from newValues: ${result1.toStringT()}, ${dest.toStringT()}") + + dest.copyInto(dest, 0, 1, 3) + assertTEquals(result2, dest, "Overlapping backward copy: ${result2.toStringT()}, ${dest.toStringT()}") + + dest.copyInto(dest, 1, 0, 2) + assertTEquals(result3, dest, "Overlapping forward copy: ${result2.toStringT()}, ${dest.toStringT()}") + + for ((start, end) in listOf(-1 to 0, 0 to 4, 4 to 4, 1 to 0)) { + val bounds = "start: $start, end: $end" + val ex = assertFails(bounds) { newValues.copyInto(dest, 0, start, end) } + assertTrue(ex is IllegalArgumentException || ex is IndexOutOfBoundsException, "Unexpected exception type: $ex") + } + for (destIndex in listOf(-1, 2, 4)) { + assertFailsWith("index: $destIndex") { newValues.copyInto(dest, destIndex, 0, 2) } + } + } + + doTest( + Array::copyInto, { e, a, msg -> assertArrayNotSameButEquals(e, a, msg) }, Array<*>::contentToString, + arrayOf("a", "b", "c"), arrayOf("e", "f", "g"), + arrayOf("f", "g", "c"), arrayOf("g", "c", "c"), arrayOf("g", "g", "c") + ) + + doTest( + IntArray::copyInto, ::assertArrayNotSameButEquals, IntArray::contentToString, + intArrayOf(1, 2, 3), intArrayOf(4, 5, 6), + intArrayOf(5, 6, 3), intArrayOf(6, 3, 3), intArrayOf(6, 6, 3) + ) + + doTest( + LongArray::copyInto, ::assertArrayNotSameButEquals, LongArray::contentToString, + longArrayOf(1, 2, 3), longArrayOf(4, 5, 6), + longArrayOf(5, 6, 3), longArrayOf(6, 3, 3), longArrayOf(6, 6, 3) + ) + + doTest( + ByteArray::copyInto, ::assertArrayNotSameButEquals, ByteArray::contentToString, + byteArrayOf(1, 2, 3), byteArrayOf(4, 5, 6), + byteArrayOf(5, 6, 3), byteArrayOf(6, 3, 3), byteArrayOf(6, 6, 3) + ) + + doTest( + CharArray::copyInto, ::assertArrayNotSameButEquals, CharArray::contentToString, + charArrayOf('a', 'b', 'c'), charArrayOf('e', 'f', 'g'), + charArrayOf('f', 'g', 'c'), charArrayOf('g', 'c', 'c'), charArrayOf('g', 'g', 'c') + ) + + doTest( + UIntArray::copyInto, { e, a, msg -> assertTrue(e contentEquals a, msg) }, UIntArray::contentToString, + uintArrayOf(1, 2, 3), uintArrayOf(4, 5, 6), + uintArrayOf(5, 6, 3), uintArrayOf(6, 3, 3), uintArrayOf(6, 6, 3) + ) + + doTest( + ULongArray::copyInto, { e, a, msg -> assertTrue(e contentEquals a, msg) }, ULongArray::contentToString, + ulongArrayOf(1, 2, 3), ulongArrayOf(4, 5, 6), + ulongArrayOf(5, 6, 3), ulongArrayOf(6, 3, 3), ulongArrayOf(6, 6, 3) + ) + + doTest( + UByteArray::copyInto, { e, a, msg -> assertTrue(e contentEquals a, msg) }, UByteArray::contentToString, + ubyteArrayOf(1, 2, 3), ubyteArrayOf(4, 5, 6), + ubyteArrayOf(5, 6, 3), ubyteArrayOf(6, 3, 3), ubyteArrayOf(6, 6, 3) + ) + } + + @Test fun copyRangeIntoVarianceTest() { + val sourceArr: Array = arrayOf(1, 2, 3) + val targetAnyArr: Array = arrayOfNulls(3) + val targetNumberArr: Array = Array(3) { 0.0 } + val targetArrProjection: Array = targetNumberArr + + val c1 = sourceArr.copyInto(targetAnyArr) + assertStaticTypeIs>(c1) + + val c2 = sourceArr.copyInto(targetNumberArr) + assertStaticTypeIs>(c2) + + val c3 = sourceArr.copyInto(targetArrProjection) + assertStaticTypeIs>(c3) + } @Test fun reduceIndexed() { expect(-1) { intArrayOf(1, 2, 3).reduceIndexed { index, a, b -> index + a - b } } diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index 22595e88599..cadbaf2901b 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -998,6 +998,24 @@ public final class kotlin/collections/ArraysKt { public static final fun contains ([Ljava/lang/Object;Ljava/lang/Object;)Z public static final fun contains ([SS)Z public static final fun contains ([ZZ)Z + public static final fun copyInto ([B[BIII)[B + public static final fun copyInto ([C[CIII)[C + public static final fun copyInto ([D[DIII)[D + public static final fun copyInto ([F[FIII)[F + public static final fun copyInto ([I[IIII)[I + public static final fun copyInto ([J[JIII)[J + public static final fun copyInto ([Ljava/lang/Object;[Ljava/lang/Object;III)[Ljava/lang/Object; + public static final fun copyInto ([S[SIII)[S + public static final fun copyInto ([Z[ZIII)[Z + public static synthetic fun copyInto$default ([B[BIIIILjava/lang/Object;)[B + public static synthetic fun copyInto$default ([C[CIIIILjava/lang/Object;)[C + public static synthetic fun copyInto$default ([D[DIIIILjava/lang/Object;)[D + public static synthetic fun copyInto$default ([F[FIIIILjava/lang/Object;)[F + public static synthetic fun copyInto$default ([I[IIIIILjava/lang/Object;)[I + public static synthetic fun copyInto$default ([J[JIIIILjava/lang/Object;)[J + public static synthetic fun copyInto$default ([Ljava/lang/Object;[Ljava/lang/Object;IIIILjava/lang/Object;)[Ljava/lang/Object; + public static synthetic fun copyInto$default ([S[SIIIILjava/lang/Object;)[S + public static synthetic fun copyInto$default ([Z[ZIIIILjava/lang/Object;)[Z public static final fun copyOfRange ([BII)[B public static final fun copyOfRange ([CII)[C public static final fun copyOfRange ([DII)[D diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 9c224ca35a3..d03517e088c 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -465,6 +465,69 @@ object ArrayOps : TemplateGroupBase() { } } + // TODO: Remove -1 from common signature + val f_copyInto = fn("copyInto(destination: SELF, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1)") { + include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) + } builder { + since("1.3") + returns("SELF") + + doc { + """ + Copies this array or its subrange into the [destination] array and returns that array. + + It's allowed to pass the same array in the [destination] and even specify the subrange so that it overlaps with the destination range. + + @param destination the array to copy to. + @param destinationOffset the position in the [destination] array to copy to, 0 by default. + @param startIndex the beginning (inclusive) of the subrange to copy, 0 by default. + @param endIndex the end (exclusive) of the subrange to copy, size of this array by default. + + @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this array indices or when `startIndex > endIndex`. + @throws IndexOutOfBoundsException when the subrange doesn't fit into the [destination] array starting at the specified [destinationIndex], + or when that index is out of the [destination] array indices range. + + @return the [destination] array. + """ + } + + specialFor(ArraysOfUnsigned) { + inlineOnly() + body { + "return SELF(storage.copyInto(destination.storage, destinationOffset, startIndex, endIndex))" + } + } + specialFor(ArraysOfPrimitives, InvariantArraysOfObjects) { + specialFor(InvariantArraysOfObjects) { + receiver("Array") + } + on(Platform.JVM) { + suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") + signature("copyInto(destination: SELF, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size)") + body { + """ + @Suppress("NAME_SHADOWING") + val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed + System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) + return destination + """ + } + } + on(Platform.JS) { + suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") + signature("copyInto(destination: SELF, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size)") + inlineOnly() + body { + val cast = ".unsafeCast>()".takeIf { family == ArraysOfPrimitives } ?: "" + """ + arrayCopy(this$cast, destination$cast, destinationOffset, startIndex, endIndex) + return destination + """ + } + } + } + } + val f_copyOfRangeJvmImpl = fn("copyOfRangeImpl(fromIndex: Int, toIndex: Int)") { include(InvariantArraysOfObjects, ArraysOfPrimitives) platforms(Platform.JVM)