From 71f3e3049aeef225112a80fbf59da618de6337ff Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 10 Jul 2015 22:01:55 +0300 Subject: [PATCH] Provide until function to construct integer ranges with an end being excluded from range. #KT-4665 Fixed --- libraries/stdlib/src/generated/_Ranges.kt | 152 ++++++++++++++++++ .../test/language/RangeIterationTest.kt | 8 + libraries/stdlib/test/language/RangeTest.kt | 32 ++++ .../kotlin-stdlib-gen/src/templates/Engine.kt | 4 +- .../kotlin-stdlib-gen/src/templates/Ranges.kt | 42 ++++- 5 files changed, 236 insertions(+), 2 deletions(-) diff --git a/libraries/stdlib/src/generated/_Ranges.kt b/libraries/stdlib/src/generated/_Ranges.kt index b22c6567da4..7a3c238f8e8 100644 --- a/libraries/stdlib/src/generated/_Ranges.kt +++ b/libraries/stdlib/src/generated/_Ranges.kt @@ -518,3 +518,155 @@ public fun ShortRange.step(step: Int): ShortProgression { return ShortProgression(start, end, step) } +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public fun Int.until(to: Byte): IntRange { + return this .. (to.toInt() - 1).toInt() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public fun Long.until(to: Byte): LongRange { + return this .. (to.toLong() - 1).toLong() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Byte.MIN_VALUE]. + */ +public fun Byte.until(to: Byte): ByteRange { + val to_ = (to - 1).toByte() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this .. to_ +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public fun Short.until(to: Byte): ShortRange { + return this .. (to.toShort() - 1).toShort() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Char.MIN_VALUE]. + */ +public fun Char.until(to: Char): CharRange { + val to_ = (to.toInt() - 1).toChar() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this .. to_ +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Int.MIN_VALUE]. + */ +public fun Int.until(to: Int): IntRange { + val to_ = (to.toLong() - 1).toInt() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this .. to_ +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public fun Long.until(to: Int): LongRange { + return this .. (to.toLong() - 1).toLong() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Int.MIN_VALUE]. + */ +public fun Byte.until(to: Int): IntRange { + val to_ = (to.toLong() - 1).toInt() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this.toInt() .. to_ +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Int.MIN_VALUE]. + */ +public fun Short.until(to: Int): IntRange { + val to_ = (to.toLong() - 1).toInt() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this.toInt() .. to_ +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Long.MIN_VALUE]. + */ +public fun Int.until(to: Long): LongRange { + val to_ = (to - 1).toLong() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this.toLong() .. to_ +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Long.MIN_VALUE]. + */ +public fun Long.until(to: Long): LongRange { + val to_ = (to - 1).toLong() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this .. to_ +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Long.MIN_VALUE]. + */ +public fun Byte.until(to: Long): LongRange { + val to_ = (to - 1).toLong() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this.toLong() .. to_ +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Long.MIN_VALUE]. + */ +public fun Short.until(to: Long): LongRange { + val to_ = (to - 1).toLong() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this.toLong() .. to_ +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public fun Int.until(to: Short): IntRange { + return this .. (to.toInt() - 1).toInt() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public fun Long.until(to: Short): LongRange { + return this .. (to.toLong() - 1).toLong() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Short.MIN_VALUE]. + */ +public fun Byte.until(to: Short): ShortRange { + val to_ = (to - 1).toShort() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this.toShort() .. to_ +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * The [to] value must be greater than [Short.MIN_VALUE]. + */ +public fun Short.until(to: Short): ShortRange { + val to_ = (to - 1).toShort() + if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.") + return this .. to_ +} + diff --git a/libraries/stdlib/test/language/RangeIterationTest.kt b/libraries/stdlib/test/language/RangeIterationTest.kt index 065eabc9a51..b2516f6eeb2 100644 --- a/libraries/stdlib/test/language/RangeIterationTest.kt +++ b/libraries/stdlib/test/language/RangeIterationTest.kt @@ -85,6 +85,14 @@ public class RangeIterationTest { listOf(3.0.toFloat(), 4.0.toFloat(), 5.0.toFloat(), 6.0.toFloat(), 7.0.toFloat(), 8.0.toFloat(), 9.0.toFloat())) } + test fun openRange() { + doTest(1 until 5, 1, 4, 1, listOf(1, 2, 3, 4)) + doTest(1.toByte() until 5.toByte(), 1.toByte(), 4.toByte(), 1, listOf(1, 2, 3, 4)) + doTest(1.toShort() until 5.toShort(), 1.toShort(), 4.toShort(), 1, listOf(1, 2, 3, 4)) + doTest(1L until 5L, 1L, 4L, 1L, listOf(1, 2, 3, 4)) + doTest('a' until 'd', 'a', 'c', 1, listOf('a', 'b', 'c')) + } + test fun emptyDownto() { doTest(5 downTo 10, 5, 10, -1, listOf()) diff --git a/libraries/stdlib/test/language/RangeTest.kt b/libraries/stdlib/test/language/RangeTest.kt index 20e7672b80c..3e758863498 100644 --- a/libraries/stdlib/test/language/RangeTest.kt +++ b/libraries/stdlib/test/language/RangeTest.kt @@ -20,6 +20,12 @@ public class RangeTest { assertFalse(9000 in range) assertFalse(range.isEmpty()) + + val openRange = 1 until 10 + assertTrue(9 in openRange) + assertFalse(10 in openRange) + + assertTrue(fails { 1 until Int.MIN_VALUE } is IllegalArgumentException) } test fun byteRange() { @@ -38,6 +44,13 @@ public class RangeTest { assertFalse(111.toByte() in range) assertFalse(range.isEmpty()) + + val openRange = 1.toByte() until 10.toByte() + assertTrue(9.toByte() in openRange) + assertFalse(10.toByte() in openRange) + + assertTrue(fails { 0.toByte() until Byte.MIN_VALUE } is IllegalArgumentException) + } test fun shortRange() { @@ -56,6 +69,12 @@ public class RangeTest { assertFalse(239.toShort() in range) assertFalse(range.isEmpty()) + + val openRange = 1.toShort() until 10.toShort() + assertTrue(9.toShort() in openRange) + assertFalse(10.toShort() in openRange) + + assertTrue(fails { 0.toShort() until Short.MIN_VALUE } is IllegalArgumentException) } test fun longRange() { @@ -74,6 +93,13 @@ public class RangeTest { assertFalse(10000000L in range) assertFalse(range.isEmpty()) + + val openRange = 1L until 10L + assertTrue(9L in openRange) + assertFalse(10L in openRange) + + assertTrue(fails { 0L until Long.MIN_VALUE } is IllegalArgumentException) + } test fun charRange() { @@ -92,6 +118,12 @@ public class RangeTest { assertFalse('\u1000' in range) assertFalse(range.isEmpty()) + + val openRange = 'A' until 'Z' + assertTrue('Y' in openRange) + assertFalse('Z' in openRange) + + assertTrue(fails { 'A' until '\u0000' } is IllegalArgumentException) } test fun doubleRange() { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt index b912ce3b30b..f9de918d15f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt @@ -45,15 +45,17 @@ enum class PrimitiveType { companion object { val defaultPrimitives = PrimitiveType.values().toSet() val numericPrimitives = setOf(Int, Long, Byte, Short, Double, Float) + val integralPrimitives = setOf(Int, Long, Byte, Short, Char) val descendingByDomainCapacity = listOf(Double, Float, Long, Int, Short, Char, Byte) - fun getMaxCapacityType(fromType: PrimitiveType, toType: PrimitiveType): PrimitiveType = descendingByDomainCapacity.first { it == fromType || it == toType } + fun maxByCapacity(fromType: PrimitiveType, toType: PrimitiveType): PrimitiveType = descendingByDomainCapacity.first { it == fromType || it == toType } val primitivePermutations = numericPrimitives.flatMap { fromType -> numericPrimitives map { toType -> fromType to toType } } + (Char to Char) } } +fun PrimitiveType.isIntegral(): Boolean = this in PrimitiveType.integralPrimitives class GenericFunction(val signature: String, val keyword: String = "fun") : Comparable { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt index 66737c60aa5..bfa8144f95f 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt @@ -49,7 +49,7 @@ fun ranges(): List { fun downTo(fromType: PrimitiveType, toType: PrimitiveType) = f("downTo(to: $toType)") { only(Primitives) only(fromType) - val elementType = PrimitiveType.getMaxCapacityType(fromType, toType) + val elementType = PrimitiveType.maxByCapacity(fromType, toType) val progressionType = elementType.name + "Progression" returns(progressionType) @@ -68,10 +68,50 @@ fun ranges(): List { PrimitiveType.Double -> "-1.0" else -> "-1" } + body { "return $progressionType($fromExpr, $toExpr, $incrementExpr)" } } templates addAll PrimitiveType.primitivePermutations.map { downTo(it.first, it.second) } + fun until(fromType: PrimitiveType, toType: PrimitiveType) = f("until(to: $toType)") { + only(Primitives) + only(fromType) + val elementType = PrimitiveType.maxByCapacity(fromType, toType) + val progressionType = elementType.name + "Range" + returns(progressionType) + + doc { + """ + Returns a range from this value up to but excluding the specified [to] value. + ${ if (elementType == toType) "The [to] value must be greater than [$elementType.MIN_VALUE]." else "" } + """ + } + + val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()" + + if (elementType == toType) { + // hack to work around incorrect char overflow behavior in JVM and int overflow behavior in JS + val toExpr = when (toType) { + PrimitiveType.Char -> "to.toInt()" + PrimitiveType.Int -> "to.toLong()" + else -> "to" + } + body { + """ + val to_ = ($toExpr - 1).to$elementType() + if (to_ > to) throw IllegalArgumentException("The to argument value '${'$'}to' was too small.") + return $fromExpr .. to_ + """ + } + } else { + body { "return $fromExpr .. (to.to$elementType() - 1).to$elementType()" } + } + } + + templates addAll PrimitiveType.primitivePermutations + .filter { it.first.isIntegral() && it.second.isIntegral() } + .map { until(it.first, it.second) } + return templates } \ No newline at end of file