Provide until function to construct integer ranges with an end being excluded from range.
#KT-4665 Fixed
This commit is contained in:
@@ -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_
|
||||
}
|
||||
|
||||
|
||||
@@ -85,6 +85,14 @@ public class RangeIterationTest {
|
||||
listOf<Float>(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<Byte>(1, 2, 3, 4))
|
||||
doTest(1.toShort() until 5.toShort(), 1.toShort(), 4.toShort(), 1, listOf<Short>(1, 2, 3, 4))
|
||||
doTest(1L until 5L, 1L, 4L, 1L, listOf<Long>(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())
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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<GenericFunction> {
|
||||
|
||||
@@ -49,7 +49,7 @@ fun ranges(): List<GenericFunction> {
|
||||
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<GenericFunction> {
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user