Add rangeUntil for primitives as an extension

#KT-52932
This commit is contained in:
Ilya Gorbunov
2022-06-26 03:03:10 +03:00
committed by Space
parent 71336df50a
commit 9574040f85
3 changed files with 279 additions and 1 deletions
@@ -762,6 +762,210 @@ public infix fun Short.downTo(to: Short): IntProgression {
return IntProgression.fromClosedRange(this.toInt(), to.toInt(), -1)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Int.rangeUntil(to: Byte): IntRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Long.rangeUntil(to: Byte): LongRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Byte.rangeUntil(to: Byte): IntRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Short.rangeUntil(to: Byte): IntRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Char.rangeUntil(to: Char): CharRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Int.rangeUntil(to: Int): IntRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Long.rangeUntil(to: Int): LongRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Byte.rangeUntil(to: Int): IntRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Short.rangeUntil(to: Int): IntRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Int.rangeUntil(to: Long): LongRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Long.rangeUntil(to: Long): LongRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Byte.rangeUntil(to: Long): LongRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Short.rangeUntil(to: Long): LongRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Int.rangeUntil(to: Short): IntRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Long.rangeUntil(to: Short): LongRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Byte.rangeUntil(to: Short): IntRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun Short.rangeUntil(to: Short): IntRange {
return until(to)
}
/**
* Returns a progression that goes over the same range in the opposite direction with the same step.
*/
@@ -325,6 +325,54 @@ public infix fun UShort.downTo(to: UShort): UIntProgression {
return UIntProgression.fromClosedRange(this.toUInt(), to.toUInt(), -1)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun UByte.rangeUntil(to: UByte): UIntRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun UInt.rangeUntil(to: UInt): UIntRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun ULong.rangeUntil(to: ULong): ULongRange {
return until(to)
}
/**
* Returns a range from this value up to but excluding the specified [to] value.
*
* If the [to] value is less than or equal to `this` value, then the returned range is empty.
*/
@SinceKotlin("1.7")
@ExperimentalStdlibApi
@kotlin.internal.InlineOnly
public inline operator fun UShort.rangeUntil(to: UShort): UIntRange {
return until(to)
}
/**
* Returns a progression that goes over the same range in the opposite direction with the same step.
*/
@@ -45,7 +45,9 @@ object RangeOps : TemplateGroupBase() {
sourceFile(SourceFile.Ranges)
if (primitive in PrimitiveType.unsignedPrimitives) {
sinceAtLeast("1.5")
wasExperimental("ExperimentalUnsignedTypes")
if (since == null || since!!.toDouble() <= 1.5) {
wasExperimental("ExperimentalUnsignedTypes")
}
sourceFile(SourceFile.URanges)
}
}
@@ -150,6 +152,30 @@ object RangeOps : TemplateGroupBase() {
}
}
val f_rangeUntil = fn("rangeUntil(to: Primitive)").byTwoPrimitives {
include(Primitives, integralCombinations + unsignedMappings)
} builderWith { (fromType, toType) ->
operator()
inlineOnly()
since("1.7")
annotation("@ExperimentalStdlibApi")
signature("rangeUntil(to: $toType)")
val elementType = rangeElementType(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 the [to] value is less than or equal to `this` value, then the returned range is empty.
"""
}
body { "return until(to)" }
}
val f_contains = fn("contains(value: Primitive)").byTwoPrimitives {
include(Ranges, numericCombinations)
filter { _, (rangeType, itemType) -> rangeType != itemType }