Fix overflow in empty progressions' last element

#KT-24204 Fixed
This commit is contained in:
Ilya Gorbunov
2018-07-09 12:42:58 +03:00
parent be8cb94105
commit 35beb9698e
@@ -42,16 +42,10 @@ private fun differenceModulo(a: Long, b: Long, c: Long): Long {
* @suppress * @suppress
*/ */
@PublishedApi @PublishedApi
internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int { internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int = when {
if (step > 0) { step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step)
return end - differenceModulo(end, start, step) step < 0 -> if (start <= end) end else end + differenceModulo(start, end, -step)
} else -> throw kotlin.IllegalArgumentException("Step is zero.")
else if (step < 0) {
return end + differenceModulo(start, end, -step)
}
else {
throw kotlin.IllegalArgumentException("Step is zero.")
}
} }
/** /**
@@ -71,14 +65,8 @@ internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int {
* @suppress * @suppress
*/ */
@PublishedApi @PublishedApi
internal fun getProgressionLastElement(start: Long, end: Long, step: Long): Long { internal fun getProgressionLastElement(start: Long, end: Long, step: Long): Long = when {
if (step > 0) { step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step)
return end - differenceModulo(end, start, step) step < 0 -> if (start <= end) end else end + differenceModulo(start, end, -step)
} else -> throw kotlin.IllegalArgumentException("Step is zero.")
else if (step < 0) {
return end + differenceModulo(start, end, -step)
}
else {
throw kotlin.IllegalArgumentException("Step is zero.")
}
} }