From 3917ec94ab5877e353ac45e856f27c1c03867a2e Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Wed, 31 May 2023 00:57:21 +0300 Subject: [PATCH] [K/N] Use AbstractList.checkIndex() functions instead of custom check functions --- .../src/main/kotlin/kotlin/text/Strings.kt | 17 ++------ .../src/kotlin/collections/ArrayList.kt | 35 ++++------------ .../src/kotlin/text/StringBuilder.kt | 42 +++++++------------ .../wasm/src/kotlin/text/StringBuilderWasm.kt | 9 ---- 4 files changed, 26 insertions(+), 77 deletions(-) diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt index 42a119d875a..189fa008285 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt @@ -358,19 +358,10 @@ public actual fun CharArray.concatToString(): String = unsafeStringFromCharArray @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun CharArray.concatToString(startIndex: Int = 0, endIndex: Int = this.size): String { - checkBoundsIndexes(startIndex, endIndex, size) + AbstractList.checkBoundsIndexes(startIndex, endIndex, size) return unsafeStringFromCharArray(this, startIndex, endIndex - startIndex) } -internal fun checkBoundsIndexes(startIndex: Int, endIndex: Int, size: Int) { - if (startIndex < 0 || endIndex > size) { - throw IndexOutOfBoundsException("startIndex: $startIndex, endIndex: $endIndex, size: $size") - } - if (startIndex > endIndex) { - throw IllegalArgumentException("startIndex: $startIndex > endIndex: $endIndex") - } -} - @GCUnsafeCall("Kotlin_String_unsafeStringFromCharArray") internal external fun unsafeStringFromCharArray(array: CharArray, start: Int, size: Int) : String @@ -386,7 +377,7 @@ internal external fun unsafeStringFromCharArray(array: CharArray, start: Int, si @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun String.toCharArray(startIndex: Int = 0, endIndex: Int = this.length): CharArray { - checkBoundsIndexes(startIndex, endIndex, length) + AbstractList.checkBoundsIndexes(startIndex, endIndex, length) return toCharArray(this, startIndex, endIndex - startIndex) } @@ -412,7 +403,7 @@ public actual fun ByteArray.decodeToString(): String = unsafeStringFromUtf8(0, s @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun ByteArray.decodeToString(startIndex: Int = 0, endIndex: Int = this.size, throwOnInvalidSequence: Boolean = false): String { - checkBoundsIndexes(startIndex, endIndex, size) + AbstractList.checkBoundsIndexes(startIndex, endIndex, size) return if (throwOnInvalidSequence) unsafeStringFromUtf8OrThrow(startIndex, endIndex - startIndex) else @@ -441,7 +432,7 @@ public actual fun String.encodeToByteArray(): ByteArray = unsafeStringToUtf8(0, @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun String.encodeToByteArray(startIndex: Int = 0, endIndex: Int = this.length, throwOnInvalidSequence: Boolean = false): ByteArray { - checkBoundsIndexes(startIndex, endIndex, length) + AbstractList.checkBoundsIndexes(startIndex, endIndex, length) return if (throwOnInvalidSequence) unsafeStringToUtf8OrThrow(startIndex, endIndex - startIndex) else diff --git a/libraries/stdlib/native-wasm/src/kotlin/collections/ArrayList.kt b/libraries/stdlib/native-wasm/src/kotlin/collections/ArrayList.kt index fe544a7f5f7..1baf4d6b78c 100644 --- a/libraries/stdlib/native-wasm/src/kotlin/collections/ArrayList.kt +++ b/libraries/stdlib/native-wasm/src/kotlin/collections/ArrayList.kt @@ -40,13 +40,13 @@ actual class ArrayList private constructor( override actual fun isEmpty(): Boolean = length == 0 override actual fun get(index: Int): E { - checkElementIndex(index) + AbstractList.checkElementIndex(index, length) return backingArray[offset + index] } override actual operator fun set(index: Int, element: E): E { checkIsMutable() - checkElementIndex(index) + AbstractList.checkElementIndex(index, length) val old = backingArray[offset + index] backingArray[offset + index] = element return old @@ -74,7 +74,7 @@ actual class ArrayList private constructor( override actual fun listIterator(): MutableListIterator = Itr(this, 0) override actual fun listIterator(index: Int): MutableListIterator { - checkPositionIndex(index) + AbstractList.checkPositionIndex(index, length) return Itr(this, index) } @@ -86,7 +86,7 @@ actual class ArrayList private constructor( override actual fun add(index: Int, element: E) { checkIsMutable() - checkPositionIndex(index) + AbstractList.checkPositionIndex(index, length) addAtInternal(offset + index, element) } @@ -99,7 +99,7 @@ actual class ArrayList private constructor( override actual fun addAll(index: Int, elements: Collection): Boolean { checkIsMutable() - checkPositionIndex(index) + AbstractList.checkPositionIndex(index, length) val n = elements.size addAllInternal(offset + index, elements, n) return n > 0 @@ -112,7 +112,7 @@ actual class ArrayList private constructor( override actual fun removeAt(index: Int): E { checkIsMutable() - checkElementIndex(index) + AbstractList.checkElementIndex(index, length) return removeAtInternal(offset + index) } @@ -134,7 +134,7 @@ actual class ArrayList private constructor( } override actual fun subList(fromIndex: Int, toIndex: Int): MutableList { - checkRangeIndexes(fromIndex, toIndex) + AbstractList.checkRangeIndexes(fromIndex, toIndex, length) return ArrayList(backingArray, offset + fromIndex, toIndex - fromIndex, isReadOnly, this, root ?: this) } @@ -185,27 +185,6 @@ actual class ArrayList private constructor( // ---------------------------- private ---------------------------- - private fun checkElementIndex(index: Int) { - if (index < 0 || index >= length) { - throw IndexOutOfBoundsException("index: $index, size: $length") - } - } - - private fun checkPositionIndex(index: Int) { - if (index < 0 || index > length) { - throw IndexOutOfBoundsException("index: $index, size: $length") - } - } - - private fun checkRangeIndexes(fromIndex: Int, toIndex: Int) { - if (fromIndex < 0 || toIndex > length) { - throw IndexOutOfBoundsException("fromIndex: $fromIndex, toIndex: $toIndex, size: $length") - } - if (fromIndex > toIndex) { - throw IllegalArgumentException("fromIndex: $fromIndex > toIndex: $toIndex") - } - } - private fun checkIsMutable() { if (isReadOnly || root != null && root.isReadOnly) throw UnsupportedOperationException() } diff --git a/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt b/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt index a29ef7d5560..4d9e22710de 100644 --- a/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt +++ b/libraries/stdlib/native-wasm/src/kotlin/text/StringBuilder.kt @@ -36,7 +36,7 @@ actual class StringBuilder private constructor ( get() = _length actual override fun get(index: Int): Char { - checkIndex(index) + AbstractList.checkElementIndex(index, _length) return array[index] } @@ -276,7 +276,7 @@ actual class StringBuilder private constructor ( * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. */ actual fun insert(index: Int, value: Char): StringBuilder { - checkInsertIndex(index) + AbstractList.checkPositionIndex(index, _length) ensureExtraCapacity(1) val newLastIndex = lastIndex + 1 for (i in newLastIndex downTo index + 1) { @@ -295,7 +295,7 @@ actual class StringBuilder private constructor ( * @throws IndexOutOfBoundsException if [index] is less than zero or greater than the length of this string builder. */ actual fun insert(index: Int, value: CharArray): StringBuilder { - checkInsertIndex(index) + AbstractList.checkPositionIndex(index, _length) ensureExtraCapacity(value.size) array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + value.size) @@ -340,7 +340,7 @@ actual class StringBuilder private constructor ( */ actual fun insert(index: Int, value: String?): StringBuilder { val toInsert = value ?: "null" - checkInsertIndex(index) + AbstractList.checkPositionIndex(index, _length) ensureExtraCapacity(toInsert.length) array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + toInsert.length) _length += insertString(array, index, toInsert) @@ -376,7 +376,7 @@ actual class StringBuilder private constructor ( * @throws IndexOutOfBoundsException or [IllegalArgumentException] when [startIndex] or [endIndex] is out of range of this string builder indices or when `startIndex > endIndex`. */ actual fun substring(startIndex: Int, endIndex: Int): String { - checkBoundsIndexes(startIndex, endIndex, _length) + AbstractList.checkBoundsIndexes(startIndex, endIndex, _length) return unsafeStringFromCharArray(array, startIndex, endIndex - startIndex) } @@ -411,7 +411,7 @@ actual class StringBuilder private constructor ( * @throws IndexOutOfBoundsException if [index] is out of bounds of this string builder. */ operator fun set(index: Int, value: Char) { - checkIndex(index) + AbstractList.checkElementIndex(index, _length) array[index] = value } @@ -452,7 +452,7 @@ actual class StringBuilder private constructor ( @SinceKotlin("1.4") @WasExperimental(ExperimentalStdlibApi::class) fun deleteAt(index: Int): StringBuilder { - checkIndex(index) + AbstractList.checkElementIndex(index, _length) array.copyInto(array, startIndex = index + 1, endIndex = _length, destinationOffset = index) --_length return this @@ -492,8 +492,8 @@ actual class StringBuilder private constructor ( @SinceKotlin("1.4") @WasExperimental(ExperimentalStdlibApi::class) fun toCharArray(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = this.length) { - checkBoundsIndexes(startIndex, endIndex, _length) - checkBoundsIndexes(destinationOffset, destinationOffset + endIndex - startIndex, destination.size) + AbstractList.checkBoundsIndexes(startIndex, endIndex, _length) + AbstractList.checkBoundsIndexes(destinationOffset, destinationOffset + endIndex - startIndex, destination.size) array.copyInto(destination, destinationOffset, startIndex, endIndex) } @@ -512,7 +512,7 @@ actual class StringBuilder private constructor ( @SinceKotlin("1.4") @WasExperimental(ExperimentalStdlibApi::class) fun appendRange(value: CharArray, startIndex: Int, endIndex: Int): StringBuilder { - checkBoundsIndexes(startIndex, endIndex, value.size) + AbstractList.checkBoundsIndexes(startIndex, endIndex, value.size) val extraLength = endIndex - startIndex ensureExtraCapacity(extraLength) value.copyInto(array, _length, startIndex, endIndex) @@ -532,7 +532,7 @@ actual class StringBuilder private constructor ( @SinceKotlin("1.4") @WasExperimental(ExperimentalStdlibApi::class) fun appendRange(value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder { - checkBoundsIndexes(startIndex, endIndex, value.length) + AbstractList.checkBoundsIndexes(startIndex, endIndex, value.length) val extraLength = endIndex - startIndex ensureExtraCapacity(extraLength) (value as? String)?.let { @@ -561,8 +561,8 @@ actual class StringBuilder private constructor ( @SinceKotlin("1.4") @WasExperimental(ExperimentalStdlibApi::class) fun insertRange(index: Int, value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder { - checkBoundsIndexes(startIndex, endIndex, value.length) - checkInsertIndex(index) + AbstractList.checkBoundsIndexes(startIndex, endIndex, value.length) + AbstractList.checkPositionIndex(index, _length) val extraLength = endIndex - startIndex ensureExtraCapacity(extraLength) @@ -593,8 +593,8 @@ actual class StringBuilder private constructor ( @SinceKotlin("1.4") @WasExperimental(ExperimentalStdlibApi::class) fun insertRange(index: Int, value: CharArray, startIndex: Int, endIndex: Int): StringBuilder { - checkInsertIndex(index) - checkBoundsIndexes(startIndex, endIndex, value.size) + AbstractList.checkPositionIndex(index, _length) + AbstractList.checkBoundsIndexes(startIndex, endIndex, value.size) val extraLength = endIndex - startIndex ensureExtraCapacity(extraLength) @@ -619,18 +619,6 @@ actual class StringBuilder private constructor ( } } - private fun checkIndex(index: Int) { - if (index < 0 || index >= _length) throw IndexOutOfBoundsException() - } - - private fun checkInsertIndex(index: Int) { - if (index < 0 || index > _length) throw IndexOutOfBoundsException() - } - - private fun checkInsertIndexFrom(index: Int, fromIndex: Int) { - if (index < fromIndex || index > _length) throw IndexOutOfBoundsException() - } - private fun checkReplaceRange(startIndex: Int, endIndex: Int, length: Int) { if (startIndex < 0 || startIndex > length) { throw IndexOutOfBoundsException("startIndex: $startIndex, length: $length") diff --git a/libraries/stdlib/wasm/src/kotlin/text/StringBuilderWasm.kt b/libraries/stdlib/wasm/src/kotlin/text/StringBuilderWasm.kt index e9afcaa2a69..a958c16a7ac 100644 --- a/libraries/stdlib/wasm/src/kotlin/text/StringBuilderWasm.kt +++ b/libraries/stdlib/wasm/src/kotlin/text/StringBuilderWasm.kt @@ -24,12 +24,3 @@ internal fun insertInt(array: CharArray, start: Int, value: Int): Int { insertString(array, start, valueString, 0, length) return length } - -internal fun checkBoundsIndexes(startIndex: Int, endIndex: Int, size: Int) { - if (startIndex < 0 || endIndex > size) { - throw IndexOutOfBoundsException("startIndex: $startIndex, endIndex: $endIndex, size: $size") - } - if (startIndex > endIndex) { - throw IllegalArgumentException("startIndex: $startIndex > endIndex: $endIndex") - } -}