CollectionsKt.windowed throws IllegalArgumentException

This commit is contained in:
Abduqodiri Qurbonzoda
2019-09-16 20:34:13 +03:00
parent a7b984bcbf
commit d66f07a84e
6 changed files with 51 additions and 8 deletions
@@ -2147,7 +2147,8 @@ public fun <T> Iterable<T>.windowed(size: Int, step: Int = 1, partialWindows: Bo
checkWindowSizeStep(size, step)
if (this is RandomAccess && this is List) {
val thisSize = this.size
val result = ArrayList<List<T>>((thisSize + step - 1) / step)
val resultCapacity = thisSize / step + if (thisSize % step == 0) 0 else 1
val result = ArrayList<List<T>>(resultCapacity)
var index = 0
while (index < thisSize) {
val windowSize = size.coerceAtMost(thisSize - index)
@@ -1374,7 +1374,8 @@ public fun CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boole
public fun <R> CharSequence.windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R): List<R> {
checkWindowSizeStep(size, step)
val thisSize = this.length
val result = ArrayList<R>((thisSize + step - 1) / step)
val resultCapacity = thisSize / step + if (thisSize % step == 0) 0 else 1
val result = ArrayList<R>(resultCapacity)
var index = 0
while (index < thisSize) {
val end = index + size