From 2ce0bca34ef9fd784e468a9978e8062b6fc7eb10 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 6 Jul 2018 16:48:23 +0300 Subject: [PATCH] Make bound check helpers top-level so they are compiled as static on JVM Improve empty range exception text in nextInt/nextLong(range) --- libraries/stdlib/src/kotlin/random/Random.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/libraries/stdlib/src/kotlin/random/Random.kt b/libraries/stdlib/src/kotlin/random/Random.kt index 37dde95416b..340c48b4ad0 100644 --- a/libraries/stdlib/src/kotlin/random/Random.kt +++ b/libraries/stdlib/src/kotlin/random/Random.kt @@ -87,6 +87,7 @@ public abstract class Random { * @throws IllegalArgumentException if [range] is empty. */ public open fun nextInt(range: IntRange): Int = when { + range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range") range.last < Int.MAX_VALUE -> nextInt(range.first, range.last + 1) range.first > Int.MIN_VALUE -> nextInt(range.first - 1, range.last) + 1 else -> nextInt() @@ -165,7 +166,7 @@ public abstract class Random { * @throws IllegalArgumentException if [range] is empty. */ public open fun nextLong(range: LongRange): Long = when { -// range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range") + range.isEmpty() -> throw IllegalArgumentException("Cannot get random in empty range: $range") range.last < Long.MAX_VALUE -> nextLong(range.start, range.endInclusive + 1) range.start > Long.MIN_VALUE -> nextLong(range.start - 1, range.endInclusive) + 1 else -> nextLong() @@ -296,11 +297,6 @@ public abstract class Random { override fun nextBytes(array: ByteArray, fromIndex: Int, toIndex: Int): ByteArray = defaultRandom.nextBytes(array, fromIndex, toIndex) - internal fun checkRangeBounds(origin: Int, bound: Int) = require(bound > origin) { boundsErrorMessage(origin, bound) } - internal fun checkRangeBounds(origin: Long, bound: Long) = require(bound > origin) { boundsErrorMessage(origin, bound) } - internal fun checkRangeBounds(origin: Double, bound: Double) = require(bound > origin) { boundsErrorMessage(origin, bound) } - - private fun boundsErrorMessage(origin: Any, bound: Any) = "Random range is empty: [$origin, $bound)." } } @@ -331,4 +327,10 @@ internal expect fun doubleFromParts(hi26: Int, low27: Int): Double /** Takes upper [bitCount] bits (0..32) from this number. */ internal fun Int.takeUpperBits(bitCount: Int): Int = - this.ushr(32 - bitCount) and (-bitCount).shr(31) \ No newline at end of file + this.ushr(32 - bitCount) and (-bitCount).shr(31) + +internal fun checkRangeBounds(origin: Int, bound: Int) = require(bound > origin) { boundsErrorMessage(origin, bound) } +internal fun checkRangeBounds(origin: Long, bound: Long) = require(bound > origin) { boundsErrorMessage(origin, bound) } +internal fun checkRangeBounds(origin: Double, bound: Double) = require(bound > origin) { boundsErrorMessage(origin, bound) } + +private fun boundsErrorMessage(origin: Any, bound: Any) = "Random range is empty: [$origin, $bound)."