Replace .. with until or indices where appropriate

Replace indices.reversed() with lastIndex..0 as it is not optimized in JS yet
This commit is contained in:
Ilya Gorbunov
2018-01-15 21:27:01 +03:00
parent 513f50785e
commit 40aa2280a5
14 changed files with 91 additions and 91 deletions
+26 -26
View File
@@ -3946,7 +3946,7 @@ public fun <T> Array<out T>.takeLast(n: Int): List<T> {
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<T>(n)
for (index in size - n .. size - 1)
for (index in size - n until size)
list.add(this[index])
return list
}
@@ -3961,7 +3961,7 @@ public fun ByteArray.takeLast(n: Int): List<Byte> {
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Byte>(n)
for (index in size - n .. size - 1)
for (index in size - n until size)
list.add(this[index])
return list
}
@@ -3976,7 +3976,7 @@ public fun ShortArray.takeLast(n: Int): List<Short> {
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Short>(n)
for (index in size - n .. size - 1)
for (index in size - n until size)
list.add(this[index])
return list
}
@@ -3991,7 +3991,7 @@ public fun IntArray.takeLast(n: Int): List<Int> {
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Int>(n)
for (index in size - n .. size - 1)
for (index in size - n until size)
list.add(this[index])
return list
}
@@ -4006,7 +4006,7 @@ public fun LongArray.takeLast(n: Int): List<Long> {
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Long>(n)
for (index in size - n .. size - 1)
for (index in size - n until size)
list.add(this[index])
return list
}
@@ -4021,7 +4021,7 @@ public fun FloatArray.takeLast(n: Int): List<Float> {
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Float>(n)
for (index in size - n .. size - 1)
for (index in size - n until size)
list.add(this[index])
return list
}
@@ -4036,7 +4036,7 @@ public fun DoubleArray.takeLast(n: Int): List<Double> {
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Double>(n)
for (index in size - n .. size - 1)
for (index in size - n until size)
list.add(this[index])
return list
}
@@ -4051,7 +4051,7 @@ public fun BooleanArray.takeLast(n: Int): List<Boolean> {
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Boolean>(n)
for (index in size - n .. size - 1)
for (index in size - n until size)
list.add(this[index])
return list
}
@@ -4066,7 +4066,7 @@ public fun CharArray.takeLast(n: Int): List<Char> {
if (n >= size) return toList()
if (n == 1) return listOf(this[size - 1])
val list = ArrayList<Char>(n)
for (index in size - n .. size - 1)
for (index in size - n until size)
list.add(this[index])
return list
}
@@ -11799,7 +11799,7 @@ public infix fun <R> CharArray.zip(other: Array<out R>): List<Pair<Char, R>> {
public inline fun <T, R, V> Array<out T>.zip(other: Array<out R>, transform: (a: T, b: R) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -11811,7 +11811,7 @@ public inline fun <T, R, V> Array<out T>.zip(other: Array<out R>, transform: (a:
public inline fun <R, V> ByteArray.zip(other: Array<out R>, transform: (a: Byte, b: R) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -11823,7 +11823,7 @@ public inline fun <R, V> ByteArray.zip(other: Array<out R>, transform: (a: Byte,
public inline fun <R, V> ShortArray.zip(other: Array<out R>, transform: (a: Short, b: R) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -11835,7 +11835,7 @@ public inline fun <R, V> ShortArray.zip(other: Array<out R>, transform: (a: Shor
public inline fun <R, V> IntArray.zip(other: Array<out R>, transform: (a: Int, b: R) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -11847,7 +11847,7 @@ public inline fun <R, V> IntArray.zip(other: Array<out R>, transform: (a: Int, b
public inline fun <R, V> LongArray.zip(other: Array<out R>, transform: (a: Long, b: R) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -11859,7 +11859,7 @@ public inline fun <R, V> LongArray.zip(other: Array<out R>, transform: (a: Long,
public inline fun <R, V> FloatArray.zip(other: Array<out R>, transform: (a: Float, b: R) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -11871,7 +11871,7 @@ public inline fun <R, V> FloatArray.zip(other: Array<out R>, transform: (a: Floa
public inline fun <R, V> DoubleArray.zip(other: Array<out R>, transform: (a: Double, b: R) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -11883,7 +11883,7 @@ public inline fun <R, V> DoubleArray.zip(other: Array<out R>, transform: (a: Dou
public inline fun <R, V> BooleanArray.zip(other: Array<out R>, transform: (a: Boolean, b: R) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -11895,7 +11895,7 @@ public inline fun <R, V> BooleanArray.zip(other: Array<out R>, transform: (a: Bo
public inline fun <R, V> CharArray.zip(other: Array<out R>, transform: (a: Char, b: R) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -12152,7 +12152,7 @@ public infix fun CharArray.zip(other: CharArray): List<Pair<Char, Char>> {
public inline fun <V> ByteArray.zip(other: ByteArray, transform: (a: Byte, b: Byte) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -12164,7 +12164,7 @@ public inline fun <V> ByteArray.zip(other: ByteArray, transform: (a: Byte, b: By
public inline fun <V> ShortArray.zip(other: ShortArray, transform: (a: Short, b: Short) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -12176,7 +12176,7 @@ public inline fun <V> ShortArray.zip(other: ShortArray, transform: (a: Short, b:
public inline fun <V> IntArray.zip(other: IntArray, transform: (a: Int, b: Int) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -12188,7 +12188,7 @@ public inline fun <V> IntArray.zip(other: IntArray, transform: (a: Int, b: Int)
public inline fun <V> LongArray.zip(other: LongArray, transform: (a: Long, b: Long) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -12200,7 +12200,7 @@ public inline fun <V> LongArray.zip(other: LongArray, transform: (a: Long, b: Lo
public inline fun <V> FloatArray.zip(other: FloatArray, transform: (a: Float, b: Float) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -12212,7 +12212,7 @@ public inline fun <V> FloatArray.zip(other: FloatArray, transform: (a: Float, b:
public inline fun <V> DoubleArray.zip(other: DoubleArray, transform: (a: Double, b: Double) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -12224,7 +12224,7 @@ public inline fun <V> DoubleArray.zip(other: DoubleArray, transform: (a: Double,
public inline fun <V> BooleanArray.zip(other: BooleanArray, transform: (a: Boolean, b: Boolean) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -12236,7 +12236,7 @@ public inline fun <V> BooleanArray.zip(other: BooleanArray, transform: (a: Boole
public inline fun <V> CharArray.zip(other: CharArray, transform: (a: Char, b: Char) -> V): List<V> {
val size = minOf(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
for (i in 0 until size) {
list.add(transform(this[i], other[i]))
}
return list
@@ -551,7 +551,7 @@ public fun <T> Iterable<T>.drop(n: Int): List<T> {
list = ArrayList<T>(resultSize)
if (this is List<T>) {
if (this is RandomAccess) {
for (index in n..size - 1)
for (index in n until size)
list.add(this[index])
} else {
for (item in listIterator(n))
@@ -742,7 +742,7 @@ public fun <T> List<T>.takeLast(n: Int): List<T> {
if (n == 1) return listOf(last())
val list = ArrayList<T>(n)
if (this is RandomAccess) {
for (index in size - n .. size - 1)
for (index in size - n until size)
list.add(this[index])
} else {
for (item in listIterator(size - n))
+7 -7
View File
@@ -253,7 +253,7 @@ public fun String.dropLast(n: Int): String {
* Returns a subsequence of this char sequence containing all characters except last characters that satisfy the given [predicate].
*/
public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): CharSequence {
for (index in this.indices.reversed())
for (index in lastIndex downTo 0)
if (!predicate(this[index]))
return subSequence(0, index + 1)
return ""
@@ -263,7 +263,7 @@ public inline fun CharSequence.dropLastWhile(predicate: (Char) -> Boolean): Char
* Returns a string containing all characters except last characters that satisfy the given [predicate].
*/
public inline fun String.dropLastWhile(predicate: (Char) -> Boolean): String {
for (index in this.indices.reversed())
for (index in lastIndex downTo 0)
if (!predicate(this[index]))
return substring(0, index + 1)
return ""
@@ -359,7 +359,7 @@ public inline fun <C : Appendable> CharSequence.filterNotTo(destination: C, pred
* Appends all characters matching the given [predicate] to the given [destination].
*/
public inline fun <C : Appendable> CharSequence.filterTo(destination: C, predicate: (Char) -> Boolean): C {
for (index in 0..length - 1) {
for (index in 0 until length) {
val element = get(index)
if (predicate(element)) destination.append(element)
}
@@ -465,7 +465,7 @@ public inline fun String.takeLastWhile(predicate: (Char) -> Boolean): String {
* Returns a subsequence of this char sequence containing the first characters that satisfy the given [predicate].
*/
public inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequence {
for (index in 0..length - 1)
for (index in 0 until length)
if (!predicate(get(index))) {
return subSequence(0, index)
}
@@ -476,7 +476,7 @@ public inline fun CharSequence.takeWhile(predicate: (Char) -> Boolean): CharSequ
* Returns a string containing the first characters that satisfy the given [predicate].
*/
public inline fun String.takeWhile(predicate: (Char) -> Boolean): String {
for (index in 0..length - 1)
for (index in 0 until length)
if (!predicate(get(index))) {
return substring(0, index)
}
@@ -1321,7 +1321,7 @@ public infix fun CharSequence.zip(other: CharSequence): List<Pair<Char, Char>> {
public inline fun <V> CharSequence.zip(other: CharSequence, transform: (a: Char, b: Char) -> V): List<V> {
val length = minOf(this.length, other.length)
val list = ArrayList<V>(length)
for (i in 0..length-1) {
for (i in 0 until length) {
list.add(transform(this[i], other[i]))
}
return list
@@ -1352,7 +1352,7 @@ public inline fun <R> CharSequence.zipWithNext(transform: (a: Char, b: Char) ->
val size = length - 1
if (size < 1) return emptyList()
val result = ArrayList<R>(size)
for (index in 0..size - 1) {
for (index in 0 until size) {
result.add(transform(this[index], this[index + 1]))
}
return result
@@ -203,7 +203,7 @@ private class RingBuffer<T>(val capacity: Int): AbstractList<T>(), RandomAccess
// TODO: replace with Array.fill from stdlib when available in common
private fun <T> Array<T>.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit {
for (idx in fromIndex .. toIndex-1) {
for (idx in fromIndex until toIndex) {
this[idx] = element
}
}
+2 -2
View File
@@ -703,7 +703,7 @@ internal fun CharSequence.regionMatchesImpl(thisOffset: Int, other: CharSequence
return false
}
for (index in 0..length-1) {
for (index in 0 until length) {
if (!this[thisOffset + index].equals(other[otherOffset + index], ignoreCase))
return false
}
@@ -1067,7 +1067,7 @@ private class DelimitedRangesSequence(private val input: CharSequence, private v
}
else {
val (index,length) = match
nextItem = currentStartIndex..index-1
nextItem = currentStartIndex until index
currentStartIndex = index + length
nextSearchIndex = currentStartIndex + if (length == 0) 1 else 0
}
@@ -281,5 +281,5 @@ private class MatcherMatchResult(private val matcher: Matcher, private val input
}
private fun java.util.regex.MatchResult.range(): IntRange = start()..end()-1
private fun java.util.regex.MatchResult.range(groupIndex: Int): IntRange = start(groupIndex)..end(groupIndex)-1
private fun java.util.regex.MatchResult.range(): IntRange = start() until end()
private fun java.util.regex.MatchResult.range(groupIndex: Int): IntRange = start(groupIndex) until end(groupIndex)
+1 -1
View File
@@ -128,7 +128,7 @@ public inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? {
public inline fun repeat(times: Int, action: (Int) -> Unit) {
contract { callsInPlace(action) }
for (index in 0..times - 1) {
for (index in 0 until times) {
action(index)
}
}