From cfefbc184b60b6b6c4650419262a0d5c83e2dd80 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 28 Dec 2018 04:41:40 +0300 Subject: [PATCH] Replace remaining usages of copyRangeTo to copyInto and drop copyRangeTo --- .../kotlin/kotlin/collections/ArrayList.kt | 8 +- .../kotlin/kotlin/collections/ArrayUtil.kt | 101 +----------------- .../kotlin/native/concurrent/MutableData.kt | 6 +- .../main/kotlin/kotlin/text/StringBuilder.kt | 12 +-- 4 files changed, 17 insertions(+), 110 deletions(-) diff --git a/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt b/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt index f6d0b72bcb3..90f3a4cc8f8 100644 --- a/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt +++ b/runtime/src/main/kotlin/kotlin/collections/ArrayList.kt @@ -200,7 +200,7 @@ actual class ArrayList private constructor( private fun insertAtInternal(i: Int, n: Int) { ensureExtraCapacity(n) - array.copyRangeTo(array, fromIndex = i, toIndex = offset + length, destinationIndex = i + n) + array.copyInto(array, startIndex = i, endIndex = offset + length, destinationOffset = i + n) length += n } @@ -238,7 +238,7 @@ actual class ArrayList private constructor( return old } else { val old = array[i] - array.copyRangeTo(array, fromIndex = i + 1, toIndex = offset + length, destinationIndex = i) + array.copyInto(array, startIndex = i + 1, endIndex = offset + length, destinationOffset = i) array.resetAt(offset + length - 1) length-- return old @@ -249,7 +249,7 @@ actual class ArrayList private constructor( if (backing != null) { backing.removeRangeInternal(rangeOffset, rangeLength) } else { - array.copyRangeTo(array, fromIndex = rangeOffset + rangeLength, toIndex = length, destinationIndex = rangeOffset) + array.copyInto(array, startIndex = rangeOffset + rangeLength, endIndex = length, destinationOffset = rangeOffset) array.resetRange(fromIndex = length - rangeLength, toIndex = length) } length -= rangeLength @@ -272,7 +272,7 @@ actual class ArrayList private constructor( } } val removed = rangeLength - j - array.copyRangeTo(array, fromIndex = rangeOffset + rangeLength, toIndex = length, destinationIndex = rangeOffset + j) + array.copyInto(array, startIndex = rangeOffset + rangeLength, endIndex = length, destinationOffset = rangeOffset + j) array.resetRange(fromIndex = length - removed, toIndex = length) length -= removed return removed diff --git a/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt b/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt index 0bdd6da3e81..8d1a174e220 100644 --- a/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt +++ b/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt @@ -99,8 +99,7 @@ internal fun IntArray.fill(fromIndex: Int, toIndex: Int, value: Int) { @SymbolName("Kotlin_Array_copyImpl") @PointsTo(0b000100, 0, 0b000001) // points to , points to . -internal external fun arrayCopy(array: Array, fromIndex: Int, - destination: Array, toIndex: Int, count: Int) +internal external fun arrayCopy(array: Array, fromIndex: Int, destination: Array, toIndex: Int, count: Int) @SymbolName("Kotlin_ByteArray_copyImpl") internal external fun arrayCopy(array: ByteArray, fromIndex: Int, destination: ByteArray, toIndex: Int, count: Int) @@ -117,107 +116,15 @@ internal external fun arrayCopy(array: IntArray, fromIndex: Int, destination: In @SymbolName("Kotlin_LongArray_copyImpl") internal external fun arrayCopy(array: LongArray, fromIndex: Int, destination: LongArray, toIndex: Int, count: Int) -// Note: [arrayCopy] for an unsigned array is bitwise identical to signed type, so -// signed array implementations from runtime are directly reused for unsigned ones. - -@ExperimentalUnsignedTypes -@SymbolName("Kotlin_ByteArray_copyImpl") -internal external fun arrayCopy(array: UByteArray, fromIndex: Int, destination: UByteArray, toIndex: Int, count: Int) - -@ExperimentalUnsignedTypes -@SymbolName("Kotlin_ShortArray_copyImpl") -internal external fun arrayCopy(array: UShortArray, fromIndex: Int, destination: UShortArray, toIndex: Int, count: Int) - -@ExperimentalUnsignedTypes -@SymbolName("Kotlin_IntArray_copyImpl") -internal external fun arrayCopy(array: UIntArray, fromIndex: Int, - destination: UIntArray, toIndex: Int, count: Int) - -@ExperimentalUnsignedTypes -@SymbolName("Kotlin_LongArray_copyImpl") -internal external fun arrayCopy(array: ULongArray, fromIndex: Int, - destination: ULongArray, toIndex: Int, count: Int) - - @SymbolName("Kotlin_FloatArray_copyImpl") -internal external fun arrayCopy(array: FloatArray, fromIndex: Int, - destination: FloatArray, toIndex: Int, count: Int) +internal external fun arrayCopy(array: FloatArray, fromIndex: Int, destination: FloatArray, toIndex: Int, count: Int) @SymbolName("Kotlin_DoubleArray_copyImpl") -internal external fun arrayCopy(array: DoubleArray, fromIndex: Int, - destination: DoubleArray, toIndex: Int, count: Int) +internal external fun arrayCopy(array: DoubleArray, fromIndex: Int, destination: DoubleArray, toIndex: Int, count: Int) @SymbolName("Kotlin_BooleanArray_copyImpl") -internal external fun arrayCopy(array: BooleanArray, fromIndex: Int, - destination: BooleanArray, toIndex: Int, count: Int) +internal external fun arrayCopy(array: BooleanArray, fromIndex: Int, destination: BooleanArray, toIndex: Int, count: Int) -/** - * Copies a range of array elements at a specified [fromIndex] (inclusive) to [toIndex] (exclusive) range of indices - * to another [destination] array starting at [destinationIndex]. - */ -@PublishedApi -internal fun Array.copyRangeTo( - destination: Array, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(@Suppress("UNCHECKED_CAST") (this as Array), fromIndex, - @Suppress("UNCHECKED_CAST") (destination as Array), - destinationIndex, toIndex - fromIndex) -} - -internal fun ByteArray.copyRangeTo(destination: ByteArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -internal fun ShortArray.copyRangeTo(destination: ShortArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -internal fun CharArray.copyRangeTo(destination: CharArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -internal fun IntArray.copyRangeTo(destination: IntArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -internal fun LongArray.copyRangeTo(destination: LongArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -@SinceKotlin("1.3") -@ExperimentalUnsignedTypes -internal fun UByteArray.copyRangeTo(destination: UByteArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -@SinceKotlin("1.3") -@ExperimentalUnsignedTypes -internal fun UShortArray.copyRangeTo(destination: UShortArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -@SinceKotlin("1.3") -@ExperimentalUnsignedTypes -internal fun UIntArray.copyRangeTo(destination: UIntArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -@SinceKotlin("1.3") -@ExperimentalUnsignedTypes -internal fun ULongArray.copyRangeTo(destination: ULongArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -internal fun FloatArray.copyRangeTo(destination: FloatArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -internal fun DoubleArray.copyRangeTo(destination: DoubleArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} - -internal fun BooleanArray.copyRangeTo(destination: BooleanArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { - arrayCopy(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) -} internal fun Collection.collectionToString(): String { val sb = StringBuilder(2 + size * 3) diff --git a/runtime/src/main/kotlin/kotlin/native/concurrent/MutableData.kt b/runtime/src/main/kotlin/kotlin/native/concurrent/MutableData.kt index 50815bc78f5..cc8f3ff88d6 100644 --- a/runtime/src/main/kotlin/kotlin/native/concurrent/MutableData.kt +++ b/runtime/src/main/kotlin/kotlin/native/concurrent/MutableData.kt @@ -34,7 +34,7 @@ public class MutableData constructor(capacity: Int = 16) { if (newSize > buffer.size) { val actualSize = maxOf(buffer.size * 3 / 2 + 1, newSize) val newBuffer = ByteArray(actualSize) - buffer.copyRangeTo(newBuffer, 0, size, 0) + buffer.copyInto(newBuffer, startIndex = 0, endIndex = size) newBuffer.share() buffer = newBuffer } @@ -73,7 +73,7 @@ public class MutableData constructor(capacity: Int = 16) { throw IndexOutOfBoundsException("$fromIndex is bigger than $toIndex") if (toIndex == toIndex) return val where = resizeDataLocked(this.size + (toIndex - fromIndex)) - data.copyRangeTo(buffer, fromIndex, toIndex, where) + data.copyInto(buffer, where, fromIndex, toIndex) } /** @@ -91,7 +91,7 @@ public class MutableData constructor(capacity: Int = 16) { * Copies range of mutable data to the byte array. */ public fun copyInto(output: ByteArray, destinationIndex: Int, startIndex: Int, endIndex: Int): Unit = locked(lock) { - buffer.copyRangeTo(output, startIndex, endIndex, destinationIndex) + buffer.copyInto(output, destinationIndex, startIndex, endIndex) } /** diff --git a/runtime/src/main/kotlin/kotlin/text/StringBuilder.kt b/runtime/src/main/kotlin/kotlin/text/StringBuilder.kt index 74a2fe7c304..5f7192319b7 100644 --- a/runtime/src/main/kotlin/kotlin/text/StringBuilder.kt +++ b/runtime/src/main/kotlin/kotlin/text/StringBuilder.kt @@ -168,7 +168,7 @@ actual class StringBuilder private constructor ( val extraLength = end - start ensureExtraCapacity(extraLength) - array.copyRangeTo(array, index, _length, index + extraLength) + array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + extraLength) var from = start var to = index while (from < end) { @@ -183,8 +183,8 @@ actual class StringBuilder private constructor ( checkInsertIndex(index) ensureExtraCapacity(chars.size) - array.copyRangeTo(array, index, _length, index + chars.size) - chars.copyRangeTo(array, 0, chars.size, index) + array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + chars.size) + chars.copyInto(array, destinationOffset = index) _length += chars.size return this @@ -193,7 +193,7 @@ actual class StringBuilder private constructor ( fun insert(index: Int, string: String): StringBuilder { checkInsertIndex(index) ensureExtraCapacity(string.length) - array.copyRangeTo(array, index, _length, index + string.length) + array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + string.length) _length += insertString(array, index, string) return this } @@ -235,7 +235,7 @@ actual class StringBuilder private constructor ( fun append(it: CharArray): StringBuilder { ensureExtraCapacity(it.size) - it.copyRangeTo(array, 0, it.size, _length) + it.copyInto(array, _length) _length += it.size return this } @@ -262,7 +262,7 @@ actual class StringBuilder private constructor ( fun deleteCharAt(index: Int) { checkIndex(index) - array.copyRangeTo(array, index + 1, _length, index) + array.copyInto(array, startIndex = index + 1, endIndex = _length, destinationOffset = index) --_length }