diff --git a/libraries/stdlib/src/kotlin/collections/AbstractList.kt b/libraries/stdlib/src/kotlin/collections/AbstractList.kt index 66a00e8b59c..71ff76c4ae4 100644 --- a/libraries/stdlib/src/kotlin/collections/AbstractList.kt +++ b/libraries/stdlib/src/kotlin/collections/AbstractList.kt @@ -125,12 +125,12 @@ public abstract class AbstractList protected constructor() : AbstractColl } } - internal fun checkRangeIndexes(start: Int, end: Int, size: Int) { - if (start < 0 || end > size) { - throw IndexOutOfBoundsException("fromIndex: $start, toIndex: $end, size: $size") + internal fun checkRangeIndexes(fromIndex: Int, toIndex: Int, size: Int) { + if (fromIndex < 0 || toIndex > size) { + throw IndexOutOfBoundsException("fromIndex: $fromIndex, toIndex: $toIndex, size: $size") } - if (start > end) { - throw IllegalArgumentException("fromIndex: $start > toIndex: $end") + if (fromIndex > toIndex) { + throw IllegalArgumentException("fromIndex: $fromIndex > toIndex: $toIndex") } } diff --git a/libraries/stdlib/src/kotlin/collections/ReversedViews.kt b/libraries/stdlib/src/kotlin/collections/ReversedViews.kt index dfec641cb21..7aebfe5eb86 100644 --- a/libraries/stdlib/src/kotlin/collections/ReversedViews.kt +++ b/libraries/stdlib/src/kotlin/collections/ReversedViews.kt @@ -22,7 +22,6 @@ package kotlin.collections private open class ReversedListReadOnly(private val delegate: List) : AbstractList() { override val size: Int get() = delegate.size override fun get(index: Int): T = delegate[reverseElementIndex(index)] - } private class ReversedList(private val delegate: MutableList) : AbstractMutableList() { @@ -37,11 +36,11 @@ private class ReversedList(private val delegate: MutableList) : AbstractMu delegate.add(reversePositionIndex(index), element) } } -private fun List<*>.reverseElementIndex(index: Int) = // TODO: Use AbstractList.checkElementIndex: run { AbstractList.checkElementIndex(index, size); lastIndex - index } - if (index in 0..size - 1) size - index - 1 else throw IndexOutOfBoundsException("Index $index should be in range [${0..size - 1}].") +private fun List<*>.reverseElementIndex(index: Int) = + if (index in 0..lastIndex) lastIndex - index else throw IndexOutOfBoundsException("Element index $index must be in range [${0..lastIndex}].") private fun List<*>.reversePositionIndex(index: Int) = - if (index in 0..size) size - index else throw IndexOutOfBoundsException("Index $index should be in range [${0..size}].") + if (index in 0..size) size - index else throw IndexOutOfBoundsException("Position index $index must be in range [${0..size}].") /**