Introduce OpenEndRange and make primitive ranges implement it

#KT-52932
This commit is contained in:
Ilya Gorbunov
2022-06-13 14:48:07 +03:00
committed by Space
parent d54b5f8e85
commit cd9b36b4c3
19 changed files with 257 additions and 16 deletions
@@ -16,9 +16,18 @@ import kotlin.internal.*
*/
@SinceKotlin("1.5")
@WasExperimental(ExperimentalUnsignedTypes::class)
public class UIntRange(start: UInt, endInclusive: UInt) : UIntProgression(start, endInclusive, 1), ClosedRange<UInt> {
@OptIn(ExperimentalStdlibApi::class)
public class UIntRange(start: UInt, endInclusive: UInt) : UIntProgression(start, endInclusive, 1), ClosedRange<UInt>, OpenEndRange<UInt> {
override val start: UInt get() = first
override val endInclusive: UInt get() = last
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@Deprecated("Can throw an exception when it's impossible to represent the value with UInt type, for example, when the range includes MAX_VALUE. It's recommended to use 'endInclusive' property that doesn't throw.")
override val endExclusive: UInt get() {
if (last == UInt.MAX_VALUE) error("Cannot return the exclusive upper bound of a range that includes MAX_VALUE.")
return last + 1u
}
override fun contains(value: UInt): Boolean = first <= value && value <= last
@@ -16,9 +16,18 @@ import kotlin.internal.*
*/
@SinceKotlin("1.5")
@WasExperimental(ExperimentalUnsignedTypes::class)
public class ULongRange(start: ULong, endInclusive: ULong) : ULongProgression(start, endInclusive, 1), ClosedRange<ULong> {
@OptIn(ExperimentalStdlibApi::class)
public class ULongRange(start: ULong, endInclusive: ULong) : ULongProgression(start, endInclusive, 1), ClosedRange<ULong>, OpenEndRange<ULong> {
override val start: ULong get() = first
override val endInclusive: ULong get() = last
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@Deprecated("Can throw an exception when it's impossible to represent the value with ULong type, for example, when the range includes MAX_VALUE. It's recommended to use 'endInclusive' property that doesn't throw.")
override val endExclusive: ULong get() {
if (last == ULong.MAX_VALUE) error("Cannot return the exclusive upper bound of a range that includes MAX_VALUE.")
return last + 1u
}
override fun contains(value: ULong): Boolean = first <= value && value <= last