diff --git a/libraries/stdlib/src/generated/_Ranges.kt b/libraries/stdlib/src/generated/_Ranges.kt index 6389661f3b0..6c21ed33e06 100644 --- a/libraries/stdlib/src/generated/_Ranges.kt +++ b/libraries/stdlib/src/generated/_Ranges.kt @@ -12,9 +12,64 @@ import java.util.* import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) +@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion] +public fun Byte.`-downTo`(to: Byte): ByteProgression { + return ByteProgression.fromClosedRange(this, to, -1) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) +@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion] +public fun Short.`-downTo`(to: Byte): ShortProgression { + return ShortProgression.fromClosedRange(this, to.toShort(), -1) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) +@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion] +public fun Byte.`-downTo`(to: Short): ShortProgression { + return ShortProgression.fromClosedRange(this.toShort(), to, -1) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) +@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion] +public fun Short.`-downTo`(to: Short): ShortProgression { + return ShortProgression.fromClosedRange(this, to, -1) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) +@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion] +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 ByteRange(this, to_) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) +@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion] +public fun Short.`-until`(to: Byte): ShortRange { + return ShortRange(this, (to.toShort() - 1).toShort()) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) +@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion] +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 ShortRange(this.toShort(), to_) +} + +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) +@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion] +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 ShortRange(this, to_) +} + /** * Checks if the specified [item] belongs to this range. */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("intRangeContains") public operator fun Range.contains(item: Byte): Boolean { return start <= item && item <= end @@ -23,6 +78,15 @@ public operator fun Range.contains(item: Byte): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("intRangeContains") +public operator fun ClosedRange.contains(item: Byte): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("longRangeContains") public operator fun Range.contains(item: Byte): Boolean { return start <= item && item <= end @@ -31,6 +95,15 @@ public operator fun Range.contains(item: Byte): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("longRangeContains") +public operator fun ClosedRange.contains(item: Byte): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("shortRangeContains") public operator fun Range.contains(item: Byte): Boolean { return start <= item && item <= end @@ -39,6 +112,15 @@ public operator fun Range.contains(item: Byte): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("shortRangeContains") +public operator fun ClosedRange.contains(item: Byte): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("doubleRangeContains") public operator fun Range.contains(item: Byte): Boolean { return start <= item && item <= end @@ -47,6 +129,15 @@ public operator fun Range.contains(item: Byte): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("doubleRangeContains") +public operator fun ClosedRange.contains(item: Byte): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("floatRangeContains") public operator fun Range.contains(item: Byte): Boolean { return start <= item && item <= end @@ -55,6 +146,15 @@ public operator fun Range.contains(item: Byte): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("floatRangeContains") +public operator fun ClosedRange.contains(item: Byte): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("intRangeContains") public operator fun Range.contains(item: Double): Boolean { return start <= item && item <= end @@ -63,6 +163,15 @@ public operator fun Range.contains(item: Double): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("intRangeContains") +public operator fun ClosedRange.contains(item: Double): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("longRangeContains") public operator fun Range.contains(item: Double): Boolean { return start <= item && item <= end @@ -71,6 +180,15 @@ public operator fun Range.contains(item: Double): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("longRangeContains") +public operator fun ClosedRange.contains(item: Double): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("byteRangeContains") public operator fun Range.contains(item: Double): Boolean { return start <= item && item <= end @@ -79,6 +197,15 @@ public operator fun Range.contains(item: Double): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("byteRangeContains") +public operator fun ClosedRange.contains(item: Double): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("shortRangeContains") public operator fun Range.contains(item: Double): Boolean { return start <= item && item <= end @@ -87,6 +214,15 @@ public operator fun Range.contains(item: Double): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("shortRangeContains") +public operator fun ClosedRange.contains(item: Double): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("floatRangeContains") public operator fun Range.contains(item: Double): Boolean { return start <= item && item <= end @@ -95,6 +231,15 @@ public operator fun Range.contains(item: Double): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("floatRangeContains") +public operator fun ClosedRange.contains(item: Double): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("intRangeContains") public operator fun Range.contains(item: Float): Boolean { return start <= item && item <= end @@ -103,6 +248,15 @@ public operator fun Range.contains(item: Float): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("intRangeContains") +public operator fun ClosedRange.contains(item: Float): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("longRangeContains") public operator fun Range.contains(item: Float): Boolean { return start <= item && item <= end @@ -111,6 +265,15 @@ public operator fun Range.contains(item: Float): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("longRangeContains") +public operator fun ClosedRange.contains(item: Float): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("byteRangeContains") public operator fun Range.contains(item: Float): Boolean { return start <= item && item <= end @@ -119,6 +282,15 @@ public operator fun Range.contains(item: Float): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("byteRangeContains") +public operator fun ClosedRange.contains(item: Float): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("shortRangeContains") public operator fun Range.contains(item: Float): Boolean { return start <= item && item <= end @@ -127,6 +299,15 @@ public operator fun Range.contains(item: Float): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("shortRangeContains") +public operator fun ClosedRange.contains(item: Float): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("doubleRangeContains") public operator fun Range.contains(item: Float): Boolean { return start <= item && item <= end @@ -135,6 +316,15 @@ public operator fun Range.contains(item: Float): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("doubleRangeContains") +public operator fun ClosedRange.contains(item: Float): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("longRangeContains") public operator fun Range.contains(item: Int): Boolean { return start <= item && item <= end @@ -143,6 +333,15 @@ public operator fun Range.contains(item: Int): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("longRangeContains") +public operator fun ClosedRange.contains(item: Int): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("byteRangeContains") public operator fun Range.contains(item: Int): Boolean { return start <= item && item <= end @@ -151,6 +350,15 @@ public operator fun Range.contains(item: Int): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("byteRangeContains") +public operator fun ClosedRange.contains(item: Int): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("shortRangeContains") public operator fun Range.contains(item: Int): Boolean { return start <= item && item <= end @@ -159,6 +367,15 @@ public operator fun Range.contains(item: Int): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("shortRangeContains") +public operator fun ClosedRange.contains(item: Int): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("doubleRangeContains") public operator fun Range.contains(item: Int): Boolean { return start <= item && item <= end @@ -167,6 +384,15 @@ public operator fun Range.contains(item: Int): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("doubleRangeContains") +public operator fun ClosedRange.contains(item: Int): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("floatRangeContains") public operator fun Range.contains(item: Int): Boolean { return start <= item && item <= end @@ -175,6 +401,15 @@ public operator fun Range.contains(item: Int): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("floatRangeContains") +public operator fun ClosedRange.contains(item: Int): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("intRangeContains") public operator fun Range.contains(item: Long): Boolean { return start <= item && item <= end @@ -183,6 +418,15 @@ public operator fun Range.contains(item: Long): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("intRangeContains") +public operator fun ClosedRange.contains(item: Long): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("byteRangeContains") public operator fun Range.contains(item: Long): Boolean { return start <= item && item <= end @@ -191,6 +435,15 @@ public operator fun Range.contains(item: Long): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("byteRangeContains") +public operator fun ClosedRange.contains(item: Long): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("shortRangeContains") public operator fun Range.contains(item: Long): Boolean { return start <= item && item <= end @@ -199,6 +452,15 @@ public operator fun Range.contains(item: Long): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("shortRangeContains") +public operator fun ClosedRange.contains(item: Long): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("doubleRangeContains") public operator fun Range.contains(item: Long): Boolean { return start <= item && item <= end @@ -207,6 +469,15 @@ public operator fun Range.contains(item: Long): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("doubleRangeContains") +public operator fun ClosedRange.contains(item: Long): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("floatRangeContains") public operator fun Range.contains(item: Long): Boolean { return start <= item && item <= end @@ -215,6 +486,15 @@ public operator fun Range.contains(item: Long): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("floatRangeContains") +public operator fun ClosedRange.contains(item: Long): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("intRangeContains") public operator fun Range.contains(item: Short): Boolean { return start <= item && item <= end @@ -223,6 +503,15 @@ public operator fun Range.contains(item: Short): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("intRangeContains") +public operator fun ClosedRange.contains(item: Short): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("longRangeContains") public operator fun Range.contains(item: Short): Boolean { return start <= item && item <= end @@ -231,6 +520,15 @@ public operator fun Range.contains(item: Short): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("longRangeContains") +public operator fun ClosedRange.contains(item: Short): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("byteRangeContains") public operator fun Range.contains(item: Short): Boolean { return start <= item && item <= end @@ -239,6 +537,15 @@ public operator fun Range.contains(item: Short): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("byteRangeContains") +public operator fun ClosedRange.contains(item: Short): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("doubleRangeContains") public operator fun Range.contains(item: Short): Boolean { return start <= item && item <= end @@ -247,11 +554,28 @@ public operator fun Range.contains(item: Short): Boolean { /** * Checks if the specified [item] belongs to this range. */ +@kotlin.jvm.JvmName("doubleRangeContains") +public operator fun ClosedRange.contains(item: Short): Boolean { + return start <= item && item <= endInclusive +} + +/** + * Checks if the specified [item] belongs to this range. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") @kotlin.jvm.JvmName("floatRangeContains") public operator fun Range.contains(item: Short): Boolean { return start <= item && item <= end } +/** + * Checks if the specified [item] belongs to this range. + */ +@kotlin.jvm.JvmName("floatRangeContains") +public operator fun ClosedRange.contains(item: Short): Boolean { + return start <= item && item <= endInclusive +} + /** * Returns a progression from this value down to the specified [to] value with the increment -1. * The [to] value has to be less than this value. @@ -412,6 +736,7 @@ public fun LongProgression.reversed(): LongProgression { /** * Returns a progression that goes over this range in reverse direction. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun CharRange.reversed(): CharProgression { return CharProgression.fromClosedRange(last, first, -1) } @@ -419,6 +744,7 @@ public fun CharRange.reversed(): CharProgression { /** * Returns a progression that goes over this range in reverse direction. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun IntRange.reversed(): IntProgression { return IntProgression.fromClosedRange(last, first, -1) } @@ -426,6 +752,7 @@ public fun IntRange.reversed(): IntProgression { /** * Returns a progression that goes over this range in reverse direction. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun LongRange.reversed(): LongProgression { return LongProgression.fromClosedRange(last, first, -1L) } @@ -451,7 +778,7 @@ public fun ShortProgression.reversed(): ShortProgression { /** * Returns a progression that goes over this range in reverse direction. */ -@Deprecated("This range implementation has unclear semantics and will be removed soon.") +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) @Suppress("DEPRECATION_ERROR") public fun ByteRange.reversed(): ByteProgression { return ByteProgression.fromClosedRange(last, first, -1) @@ -460,7 +787,7 @@ public fun ByteRange.reversed(): ByteProgression { /** * Returns a progression that goes over this range in reverse direction. */ -@Deprecated("This range implementation has unclear semantics and will be removed soon.") +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) @Suppress("DEPRECATION_ERROR") public fun ShortRange.reversed(): ShortProgression { return ShortProgression.fromClosedRange(last, first, -1) @@ -493,6 +820,7 @@ public infix fun LongProgression.step(step: Long): LongProgression { /** * Returns a progression that goes over this range with given step. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public infix fun CharRange.step(step: Int): CharProgression { checkStepIsPositive(step > 0, step) return CharProgression.fromClosedRange(first, last, step) @@ -501,6 +829,7 @@ public infix fun CharRange.step(step: Int): CharProgression { /** * Returns a progression that goes over this range with given step. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public infix fun IntRange.step(step: Int): IntProgression { checkStepIsPositive(step > 0, step) return IntProgression.fromClosedRange(first, last, step) @@ -509,6 +838,7 @@ public infix fun IntRange.step(step: Int): IntProgression { /** * Returns a progression that goes over this range with given step. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public infix fun LongRange.step(step: Long): LongProgression { checkStepIsPositive(step > 0, step) return LongProgression.fromClosedRange(first, last, step) @@ -537,7 +867,7 @@ public infix fun ShortProgression.step(step: Int): ShortProgression { /** * Returns a progression that goes over this range with given step. */ -@Deprecated("This range implementation has unclear semantics and will be removed soon.") +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) @Suppress("DEPRECATION_ERROR") public infix fun ByteRange.step(step: Int): ByteProgression { checkStepIsPositive(step > 0, step) @@ -547,7 +877,7 @@ public infix fun ByteRange.step(step: Int): ByteProgression { /** * Returns a progression that goes over this range with given step. */ -@Deprecated("This range implementation has unclear semantics and will be removed soon.") +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) @Suppress("DEPRECATION_ERROR") public infix fun ShortRange.step(step: Int): ShortProgression { checkStepIsPositive(step > 0, step) @@ -932,42 +1262,34 @@ public fun Short.coerceIn(minimumValue: Short?, maximumValue: Short?): Short { * Ensures that this value lies in the specified [range]. * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. */ -public fun Byte.coerceIn(range: Range): Byte { +public fun > T.coerceIn(range: ClosedRange): T { if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this + return if (this < range.start) range.start else if (this > range.endInclusive) range.endInclusive else this } /** * Ensures that this value lies in the specified [range]. * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. */ -public fun Int.coerceIn(range: Range): Int { +public fun Int.coerceIn(range: ClosedRange): Int { if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this + return if (this < range.start) range.start else if (this > range.endInclusive) range.endInclusive else this } /** * Ensures that this value lies in the specified [range]. * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. */ -public fun Long.coerceIn(range: Range): Long { +public fun Long.coerceIn(range: ClosedRange): Long { if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this -} - -/** - * Ensures that this value lies in the specified [range]. - * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. - */ -public fun Short.coerceIn(range: Range): Short { - if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this + return if (this < range.start) range.start else if (this > range.endInclusive) range.endInclusive else this } /** * Ensures that this value lies in the specified [range]. * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun > T.coerceIn(range: Range): T { if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") return if (this < range.start) range.start else if (this > range.end) range.end else this @@ -977,6 +1299,47 @@ public fun > T.coerceIn(range: Range): T { * Ensures that this value lies in the specified [range]. * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") +public fun Byte.coerceIn(range: Range): Byte { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") +public fun Int.coerceIn(range: Range): Int { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") +public fun Long.coerceIn(range: Range): Long { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +@Deprecated("Range is deprecated. Use ClosedRange instead.") +public fun Short.coerceIn(range: Range): Short { + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this +} + +/** + * Ensures that this value lies in the specified [range]. + * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun Double.coerceIn(range: Range): Double { if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") return if (this < range.start) range.start else if (this > range.end) range.end else this @@ -986,6 +1349,7 @@ public fun Double.coerceIn(range: Range): Double { * Ensures that this value lies in the specified [range]. * @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. */ +@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN) public fun Float.coerceIn(range: Range): Float { if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.") return if (this < range.start) range.start else if (this > range.end) range.end else this diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Comparables.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Comparables.kt index be3b8d4ef28..86f8bad8d61 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Comparables.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Comparables.kt @@ -48,9 +48,12 @@ fun comparables(): List { templates add f("coerceIn(range: Range)") { sourceFile(SourceFile.Ranges) - only(Primitives) + only(Generic, Primitives) only(numericPrimitives.filter { it.isIntegral() }) + typeParam("T: Comparable") returns("SELF") + deprecate("Range is deprecated. Use ClosedRange instead.") + deprecate(Generic) { forBinaryCompatibility } doc { """ Ensures that this value lies in the specified [range]. @@ -68,9 +71,30 @@ fun comparables(): List { templates add f("coerceIn(range: Range)") { sourceFile(SourceFile.Ranges) - only(Primitives, Generic) + only(Primitives) only(numericPrimitives.filterNot { it.isIntegral() }) returns("SELF") + deprecate { forBinaryCompatibility } // force use generic overload instead + doc { + """ + Ensures that this value lies in the specified [range]. + + @return this value if it's in the [range], or range.start if this value is less than range.start, or range.end if this value is greater than range.end. + """ + } + body { + """ + if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.") + return if (this < range.start) range.start else if (this > range.end) range.end else this + """ + } + } + + templates add f("coerceIn(range: ClosedRange)") { + sourceFile(SourceFile.Ranges) + only(Primitives, Generic) + only(PrimitiveType.Int, PrimitiveType.Long) + returns("SELF") typeParam("T: Comparable") doc { """ @@ -82,7 +106,7 @@ fun comparables(): List { body { """ if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: ${'$'}range.") - return if (this < range.start) range.start else if (this > range.end) range.end else this + return if (this < range.start) range.start else if (this > range.endInclusive) range.endInclusive else this """ } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt index d5d2342647d..a63c874e0fd 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ranges.kt @@ -11,6 +11,7 @@ fun ranges(): List { fun rangeElementType(fromType: PrimitiveType, toType: PrimitiveType) = maxByCapacity(fromType, toType).let { if (it == PrimitiveType.Char) it else maxByCapacity(it, PrimitiveType.Int) } + fun Collection.permutations(): List> = flatMap { a -> map { b -> a to b } } templates add f("reversed()") { only(RangesOfPrimitives, ProgressionsOfPrimitives) @@ -18,6 +19,7 @@ fun ranges(): List { doc(ProgressionsOfPrimitives) { "Returns a progression that goes over the same range in the opposite direction with the same step." } doc(RangesOfPrimitives) { "Returns a progression that goes over this range in reverse direction." } returns("TProgression") + deprecate(RangesOfPrimitives) { forBinaryCompatibility } body(RangesOfPrimitives) { "return TProgression.fromClosedRange(last, first, -ONE)" } @@ -33,6 +35,7 @@ fun ranges(): List { doc(RangesOfPrimitives) { "Returns a progression that goes over this range in reverse direction." } returns("TProgression") deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING)) + deprecate(RangesOfPrimitives) { forBinaryCompatibility } annotations("""@Suppress("DEPRECATION_ERROR")""") body(RangesOfPrimitives) { "return TProgression.fromClosedRange(last, first, -ONE)" @@ -50,6 +53,7 @@ fun ranges(): List { doc(ProgressionsOfPrimitives) { "Returns a progression that goes over the same range with the given step." } doc(RangesOfPrimitives) { "Returns a progression that goes over this range with given step." } returns("TProgression") + deprecate(RangesOfPrimitives) { forBinaryCompatibility } body(RangesOfPrimitives) { """ checkStepIsPositive(step > 0, step) @@ -73,6 +77,7 @@ fun ranges(): List { doc(RangesOfPrimitives) { "Returns a progression that goes over this range with given step." } returns("TProgression") deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING)) + deprecate(RangesOfPrimitives) { forBinaryCompatibility } annotations("""@Suppress("DEPRECATION_ERROR")""") body(RangesOfPrimitives) { """ @@ -130,12 +135,34 @@ fun ranges(): List { body { "return $progressionType.fromClosedRange($fromExpr, $toExpr, $incrementExpr)" } } + fun downToDeprecated(fromType: PrimitiveType, toType: PrimitiveType) = f("`-downTo`(to: $toType)") { + deprecate { forBinaryCompatibility } + sourceFile(SourceFile.Ranges) + only(Primitives) + only(fromType) + val elementType = maxByCapacity(fromType, toType) + val progressionType = elementType.name + "Progression" + returns(progressionType) + annotations("""@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion]""") + val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()" + val toExpr = if (elementType == toType) "to" else "to.to$elementType()" + val incrementExpr = when (elementType) { + PrimitiveType.Long -> "-1L" + PrimitiveType.Float -> "-1.0F" + PrimitiveType.Double -> "-1.0" + else -> "-1" + } + + body { "return $progressionType.fromClosedRange($fromExpr, $toExpr, $incrementExpr)" } + } + val numericPrimitives = PrimitiveType.numericPrimitives - val numericPermutations = numericPrimitives.flatMap { fromType -> numericPrimitives.map { toType -> fromType to toType }} + val numericPermutations = numericPrimitives.permutations() val primitivePermutations = numericPermutations + (PrimitiveType.Char to PrimitiveType.Char) val integralPermutations = primitivePermutations.filter { it.first.isIntegral() && it.second.isIntegral() } templates addAll integralPermutations.map { downTo(it.first, it.second) } + templates addAll listOf(PrimitiveType.Byte, PrimitiveType.Short).permutations().map { downToDeprecated(it.first, it.second) } fun until(fromType: PrimitiveType, toType: PrimitiveType) = f("until(to: $toType)") { infix(true) @@ -180,18 +207,41 @@ fun ranges(): List { } } - templates addAll integralPermutations.map { until(it.first, it.second) } + fun untilDeprecated(fromType: PrimitiveType, toType: PrimitiveType) = f("`-until`(to: $toType)") { + deprecate { forBinaryCompatibility } - fun contains(rangeType: PrimitiveType, itemType: PrimitiveType) = f("contains(item: $itemType)") { + sourceFile(SourceFile.Ranges) + only(Primitives) + only(fromType) + val elementType = maxByCapacity(fromType, toType) + val progressionType = elementType.name + "Range" + returns(progressionType) + annotations("""@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion]""") + + 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 = "to" + body { + """ + val to_ = ($toExpr - 1).to$elementType() + if (to_ > to) throw IllegalArgumentException("The to argument value '${'$'}to' was too small.") + return $progressionType($fromExpr, to_) + """ + } + } else { + body { "return $progressionType($fromExpr, (to.to$elementType() - 1).to$elementType())" } + } + } + + templates addAll integralPermutations.map { until(it.first, it.second) } + templates addAll listOf(PrimitiveType.Byte, PrimitiveType.Short).permutations().map { untilDeprecated(it.first, it.second) } + + fun containsDeprecated(rangeType: PrimitiveType, itemType: PrimitiveType) = f("contains(item: $itemType)") { operator(true) -// if (!rangeType.isIntegral()) { -// deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING)) -// annotations("""@Suppress("DEPRECATION_ERROR")""") -// } -// else if (rangeType !in rangePrimitives) { -// deprecate(Deprecation("This range will be removed soon.", level = DeprecationLevel.WARNING)) -// } + deprecate("Range is deprecated. Use ClosedRange instead.") check(rangeType.isNumeric() == itemType.isNumeric()) { "Required rangeType and itemType both to be numeric or both not, got: $rangeType, $itemType" } only(Ranges) @@ -202,8 +252,23 @@ fun ranges(): List { body { "return start <= item && item <= end" } } + fun contains(rangeType: PrimitiveType, itemType: PrimitiveType) = f("contains(item: $itemType)") { + operator(true) + + check(rangeType.isNumeric() == itemType.isNumeric()) { "Required rangeType and itemType both to be numeric or both not, got: $rangeType, $itemType" } + only(Ranges) + onlyPrimitives(Ranges, rangeType) + customReceiver("ClosedRange") + platformName("${rangeType.name.decapitalize()}RangeContains") + returns("Boolean") + doc { "Checks if the specified [item] belongs to this range." } + body { "return start <= item && item <= endInclusive" } + } + + + templates addAll numericPermutations.filter { it.first != it.second } + .flatMap { listOf(containsDeprecated(it.first, it.second), contains(it.first, it.second)) } - templates addAll numericPermutations.filter { it.first != it.second }.map { contains(it.first, it.second) } return templates } \ No newline at end of file