[K/N] Use AbstractList.checkIndex() functions instead of custom check functions
This commit is contained in:
committed by
Space Team
parent
89d1cfe05b
commit
3917ec94ab
@@ -358,19 +358,10 @@ public actual fun CharArray.concatToString(): String = unsafeStringFromCharArray
|
|||||||
@SinceKotlin("1.3")
|
@SinceKotlin("1.3")
|
||||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||||
public actual fun CharArray.concatToString(startIndex: Int = 0, endIndex: Int = this.size): String {
|
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)
|
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")
|
@GCUnsafeCall("Kotlin_String_unsafeStringFromCharArray")
|
||||||
internal external fun unsafeStringFromCharArray(array: CharArray, start: Int, size: Int) : String
|
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")
|
@SinceKotlin("1.3")
|
||||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||||
public actual fun String.toCharArray(startIndex: Int = 0, endIndex: Int = this.length): CharArray {
|
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)
|
return toCharArray(this, startIndex, endIndex - startIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -412,7 +403,7 @@ public actual fun ByteArray.decodeToString(): String = unsafeStringFromUtf8(0, s
|
|||||||
@SinceKotlin("1.3")
|
@SinceKotlin("1.3")
|
||||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||||
public actual fun ByteArray.decodeToString(startIndex: Int = 0, endIndex: Int = this.size, throwOnInvalidSequence: Boolean = false): String {
|
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)
|
return if (throwOnInvalidSequence)
|
||||||
unsafeStringFromUtf8OrThrow(startIndex, endIndex - startIndex)
|
unsafeStringFromUtf8OrThrow(startIndex, endIndex - startIndex)
|
||||||
else
|
else
|
||||||
@@ -441,7 +432,7 @@ public actual fun String.encodeToByteArray(): ByteArray = unsafeStringToUtf8(0,
|
|||||||
@SinceKotlin("1.3")
|
@SinceKotlin("1.3")
|
||||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||||
public actual fun String.encodeToByteArray(startIndex: Int = 0, endIndex: Int = this.length, throwOnInvalidSequence: Boolean = false): ByteArray {
|
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)
|
return if (throwOnInvalidSequence)
|
||||||
unsafeStringToUtf8OrThrow(startIndex, endIndex - startIndex)
|
unsafeStringToUtf8OrThrow(startIndex, endIndex - startIndex)
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -40,13 +40,13 @@ actual class ArrayList<E> private constructor(
|
|||||||
override actual fun isEmpty(): Boolean = length == 0
|
override actual fun isEmpty(): Boolean = length == 0
|
||||||
|
|
||||||
override actual fun get(index: Int): E {
|
override actual fun get(index: Int): E {
|
||||||
checkElementIndex(index)
|
AbstractList.checkElementIndex(index, length)
|
||||||
return backingArray[offset + index]
|
return backingArray[offset + index]
|
||||||
}
|
}
|
||||||
|
|
||||||
override actual operator fun set(index: Int, element: E): E {
|
override actual operator fun set(index: Int, element: E): E {
|
||||||
checkIsMutable()
|
checkIsMutable()
|
||||||
checkElementIndex(index)
|
AbstractList.checkElementIndex(index, length)
|
||||||
val old = backingArray[offset + index]
|
val old = backingArray[offset + index]
|
||||||
backingArray[offset + index] = element
|
backingArray[offset + index] = element
|
||||||
return old
|
return old
|
||||||
@@ -74,7 +74,7 @@ actual class ArrayList<E> private constructor(
|
|||||||
override actual fun listIterator(): MutableListIterator<E> = Itr(this, 0)
|
override actual fun listIterator(): MutableListIterator<E> = Itr(this, 0)
|
||||||
|
|
||||||
override actual fun listIterator(index: Int): MutableListIterator<E> {
|
override actual fun listIterator(index: Int): MutableListIterator<E> {
|
||||||
checkPositionIndex(index)
|
AbstractList.checkPositionIndex(index, length)
|
||||||
return Itr(this, index)
|
return Itr(this, index)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ actual class ArrayList<E> private constructor(
|
|||||||
|
|
||||||
override actual fun add(index: Int, element: E) {
|
override actual fun add(index: Int, element: E) {
|
||||||
checkIsMutable()
|
checkIsMutable()
|
||||||
checkPositionIndex(index)
|
AbstractList.checkPositionIndex(index, length)
|
||||||
addAtInternal(offset + index, element)
|
addAtInternal(offset + index, element)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ actual class ArrayList<E> private constructor(
|
|||||||
|
|
||||||
override actual fun addAll(index: Int, elements: Collection<E>): Boolean {
|
override actual fun addAll(index: Int, elements: Collection<E>): Boolean {
|
||||||
checkIsMutable()
|
checkIsMutable()
|
||||||
checkPositionIndex(index)
|
AbstractList.checkPositionIndex(index, length)
|
||||||
val n = elements.size
|
val n = elements.size
|
||||||
addAllInternal(offset + index, elements, n)
|
addAllInternal(offset + index, elements, n)
|
||||||
return n > 0
|
return n > 0
|
||||||
@@ -112,7 +112,7 @@ actual class ArrayList<E> private constructor(
|
|||||||
|
|
||||||
override actual fun removeAt(index: Int): E {
|
override actual fun removeAt(index: Int): E {
|
||||||
checkIsMutable()
|
checkIsMutable()
|
||||||
checkElementIndex(index)
|
AbstractList.checkElementIndex(index, length)
|
||||||
return removeAtInternal(offset + index)
|
return removeAtInternal(offset + index)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ actual class ArrayList<E> private constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override actual fun subList(fromIndex: Int, toIndex: Int): MutableList<E> {
|
override actual fun subList(fromIndex: Int, toIndex: Int): MutableList<E> {
|
||||||
checkRangeIndexes(fromIndex, toIndex)
|
AbstractList.checkRangeIndexes(fromIndex, toIndex, length)
|
||||||
return ArrayList(backingArray, offset + fromIndex, toIndex - fromIndex, isReadOnly, this, root ?: this)
|
return ArrayList(backingArray, offset + fromIndex, toIndex - fromIndex, isReadOnly, this, root ?: this)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,27 +185,6 @@ actual class ArrayList<E> private constructor(
|
|||||||
|
|
||||||
// ---------------------------- private ----------------------------
|
// ---------------------------- 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() {
|
private fun checkIsMutable() {
|
||||||
if (isReadOnly || root != null && root.isReadOnly) throw UnsupportedOperationException()
|
if (isReadOnly || root != null && root.isReadOnly) throw UnsupportedOperationException()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ actual class StringBuilder private constructor (
|
|||||||
get() = _length
|
get() = _length
|
||||||
|
|
||||||
actual override fun get(index: Int): Char {
|
actual override fun get(index: Int): Char {
|
||||||
checkIndex(index)
|
AbstractList.checkElementIndex(index, _length)
|
||||||
return array[index]
|
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.
|
* @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 {
|
actual fun insert(index: Int, value: Char): StringBuilder {
|
||||||
checkInsertIndex(index)
|
AbstractList.checkPositionIndex(index, _length)
|
||||||
ensureExtraCapacity(1)
|
ensureExtraCapacity(1)
|
||||||
val newLastIndex = lastIndex + 1
|
val newLastIndex = lastIndex + 1
|
||||||
for (i in newLastIndex downTo index + 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.
|
* @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 {
|
actual fun insert(index: Int, value: CharArray): StringBuilder {
|
||||||
checkInsertIndex(index)
|
AbstractList.checkPositionIndex(index, _length)
|
||||||
ensureExtraCapacity(value.size)
|
ensureExtraCapacity(value.size)
|
||||||
|
|
||||||
array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + 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 {
|
actual fun insert(index: Int, value: String?): StringBuilder {
|
||||||
val toInsert = value ?: "null"
|
val toInsert = value ?: "null"
|
||||||
checkInsertIndex(index)
|
AbstractList.checkPositionIndex(index, _length)
|
||||||
ensureExtraCapacity(toInsert.length)
|
ensureExtraCapacity(toInsert.length)
|
||||||
array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + toInsert.length)
|
array.copyInto(array, startIndex = index, endIndex = _length, destinationOffset = index + toInsert.length)
|
||||||
_length += insertString(array, index, toInsert)
|
_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`.
|
* @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 {
|
actual fun substring(startIndex: Int, endIndex: Int): String {
|
||||||
checkBoundsIndexes(startIndex, endIndex, _length)
|
AbstractList.checkBoundsIndexes(startIndex, endIndex, _length)
|
||||||
return unsafeStringFromCharArray(array, startIndex, endIndex - startIndex)
|
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.
|
* @throws IndexOutOfBoundsException if [index] is out of bounds of this string builder.
|
||||||
*/
|
*/
|
||||||
operator fun set(index: Int, value: Char) {
|
operator fun set(index: Int, value: Char) {
|
||||||
checkIndex(index)
|
AbstractList.checkElementIndex(index, _length)
|
||||||
array[index] = value
|
array[index] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,7 +452,7 @@ actual class StringBuilder private constructor (
|
|||||||
@SinceKotlin("1.4")
|
@SinceKotlin("1.4")
|
||||||
@WasExperimental(ExperimentalStdlibApi::class)
|
@WasExperimental(ExperimentalStdlibApi::class)
|
||||||
fun deleteAt(index: Int): StringBuilder {
|
fun deleteAt(index: Int): StringBuilder {
|
||||||
checkIndex(index)
|
AbstractList.checkElementIndex(index, _length)
|
||||||
array.copyInto(array, startIndex = index + 1, endIndex = _length, destinationOffset = index)
|
array.copyInto(array, startIndex = index + 1, endIndex = _length, destinationOffset = index)
|
||||||
--_length
|
--_length
|
||||||
return this
|
return this
|
||||||
@@ -492,8 +492,8 @@ actual class StringBuilder private constructor (
|
|||||||
@SinceKotlin("1.4")
|
@SinceKotlin("1.4")
|
||||||
@WasExperimental(ExperimentalStdlibApi::class)
|
@WasExperimental(ExperimentalStdlibApi::class)
|
||||||
fun toCharArray(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = this.length) {
|
fun toCharArray(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = this.length) {
|
||||||
checkBoundsIndexes(startIndex, endIndex, _length)
|
AbstractList.checkBoundsIndexes(startIndex, endIndex, _length)
|
||||||
checkBoundsIndexes(destinationOffset, destinationOffset + endIndex - startIndex, destination.size)
|
AbstractList.checkBoundsIndexes(destinationOffset, destinationOffset + endIndex - startIndex, destination.size)
|
||||||
|
|
||||||
array.copyInto(destination, destinationOffset, startIndex, endIndex)
|
array.copyInto(destination, destinationOffset, startIndex, endIndex)
|
||||||
}
|
}
|
||||||
@@ -512,7 +512,7 @@ actual class StringBuilder private constructor (
|
|||||||
@SinceKotlin("1.4")
|
@SinceKotlin("1.4")
|
||||||
@WasExperimental(ExperimentalStdlibApi::class)
|
@WasExperimental(ExperimentalStdlibApi::class)
|
||||||
fun appendRange(value: CharArray, startIndex: Int, endIndex: Int): StringBuilder {
|
fun appendRange(value: CharArray, startIndex: Int, endIndex: Int): StringBuilder {
|
||||||
checkBoundsIndexes(startIndex, endIndex, value.size)
|
AbstractList.checkBoundsIndexes(startIndex, endIndex, value.size)
|
||||||
val extraLength = endIndex - startIndex
|
val extraLength = endIndex - startIndex
|
||||||
ensureExtraCapacity(extraLength)
|
ensureExtraCapacity(extraLength)
|
||||||
value.copyInto(array, _length, startIndex, endIndex)
|
value.copyInto(array, _length, startIndex, endIndex)
|
||||||
@@ -532,7 +532,7 @@ actual class StringBuilder private constructor (
|
|||||||
@SinceKotlin("1.4")
|
@SinceKotlin("1.4")
|
||||||
@WasExperimental(ExperimentalStdlibApi::class)
|
@WasExperimental(ExperimentalStdlibApi::class)
|
||||||
fun appendRange(value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder {
|
fun appendRange(value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder {
|
||||||
checkBoundsIndexes(startIndex, endIndex, value.length)
|
AbstractList.checkBoundsIndexes(startIndex, endIndex, value.length)
|
||||||
val extraLength = endIndex - startIndex
|
val extraLength = endIndex - startIndex
|
||||||
ensureExtraCapacity(extraLength)
|
ensureExtraCapacity(extraLength)
|
||||||
(value as? String)?.let {
|
(value as? String)?.let {
|
||||||
@@ -561,8 +561,8 @@ actual class StringBuilder private constructor (
|
|||||||
@SinceKotlin("1.4")
|
@SinceKotlin("1.4")
|
||||||
@WasExperimental(ExperimentalStdlibApi::class)
|
@WasExperimental(ExperimentalStdlibApi::class)
|
||||||
fun insertRange(index: Int, value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder {
|
fun insertRange(index: Int, value: CharSequence, startIndex: Int, endIndex: Int): StringBuilder {
|
||||||
checkBoundsIndexes(startIndex, endIndex, value.length)
|
AbstractList.checkBoundsIndexes(startIndex, endIndex, value.length)
|
||||||
checkInsertIndex(index)
|
AbstractList.checkPositionIndex(index, _length)
|
||||||
val extraLength = endIndex - startIndex
|
val extraLength = endIndex - startIndex
|
||||||
ensureExtraCapacity(extraLength)
|
ensureExtraCapacity(extraLength)
|
||||||
|
|
||||||
@@ -593,8 +593,8 @@ actual class StringBuilder private constructor (
|
|||||||
@SinceKotlin("1.4")
|
@SinceKotlin("1.4")
|
||||||
@WasExperimental(ExperimentalStdlibApi::class)
|
@WasExperimental(ExperimentalStdlibApi::class)
|
||||||
fun insertRange(index: Int, value: CharArray, startIndex: Int, endIndex: Int): StringBuilder {
|
fun insertRange(index: Int, value: CharArray, startIndex: Int, endIndex: Int): StringBuilder {
|
||||||
checkInsertIndex(index)
|
AbstractList.checkPositionIndex(index, _length)
|
||||||
checkBoundsIndexes(startIndex, endIndex, value.size)
|
AbstractList.checkBoundsIndexes(startIndex, endIndex, value.size)
|
||||||
|
|
||||||
val extraLength = endIndex - startIndex
|
val extraLength = endIndex - startIndex
|
||||||
ensureExtraCapacity(extraLength)
|
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) {
|
private fun checkReplaceRange(startIndex: Int, endIndex: Int, length: Int) {
|
||||||
if (startIndex < 0 || startIndex > length) {
|
if (startIndex < 0 || startIndex > length) {
|
||||||
throw IndexOutOfBoundsException("startIndex: $startIndex, length: $length")
|
throw IndexOutOfBoundsException("startIndex: $startIndex, length: $length")
|
||||||
|
|||||||
@@ -24,12 +24,3 @@ internal fun insertInt(array: CharArray, start: Int, value: Int): Int {
|
|||||||
insertString(array, start, valueString, 0, length)
|
insertString(array, start, valueString, 0, length)
|
||||||
return 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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user