From 9574040f854d1e498fd4b1ba54f8c8329e8d30fd Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sun, 26 Jun 2022 03:03:10 +0300 Subject: [PATCH] Add rangeUntil for primitives as an extension #KT-52932 --- .../stdlib/common/src/generated/_Ranges.kt | 204 ++++++++++++++++++ .../stdlib/common/src/generated/_URanges.kt | 48 +++++ .../kotlin-stdlib-gen/src/templates/Ranges.kt | 28 ++- 3 files changed, 279 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/common/src/generated/_Ranges.kt b/libraries/stdlib/common/src/generated/_Ranges.kt index 78e6c054bcc..69f8c4391c3 100644 --- a/libraries/stdlib/common/src/generated/_Ranges.kt +++ b/libraries/stdlib/common/src/generated/_Ranges.kt @@ -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. */ diff --git a/libraries/stdlib/common/src/generated/_URanges.kt b/libraries/stdlib/common/src/generated/_URanges.kt index b432e514a5d..26d1376eb85 100644 --- a/libraries/stdlib/common/src/generated/_URanges.kt +++ b/libraries/stdlib/common/src/generated/_URanges.kt @@ -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. */ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt index ca7e8afe2bd..9f80da5dbf2 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt @@ -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 }