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() {
|
||||
|
||||
Reference in New Issue
Block a user