From 34e17e82f4784d3f57774466c78f5f96dc052abc Mon Sep 17 00:00:00 2001 From: Vasily Levchenko Date: Tue, 7 Mar 2017 15:01:48 +0300 Subject: [PATCH] STDLIB: bunch of ranges functions --- .../src/main/kotlin/kotlin/ranges/_Ranges.kt | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 runtime/src/main/kotlin/kotlin/ranges/_Ranges.kt diff --git a/runtime/src/main/kotlin/kotlin/ranges/_Ranges.kt b/runtime/src/main/kotlin/kotlin/ranges/_Ranges.kt new file mode 100644 index 00000000000..cdf5221c91b --- /dev/null +++ b/runtime/src/main/kotlin/kotlin/ranges/_Ranges.kt @@ -0,0 +1,145 @@ +package kotlin.ranges + +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public infix 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 infix 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. + */ +public infix fun Byte.until(to: Byte): IntRange { + return this.toInt() .. (to.toInt() - 1).toInt() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public infix fun Short.until(to: Byte): IntRange { + return this.toInt() .. (to.toInt() - 1).toInt() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * + * If the [to] value is less than or equal to ['\u0000'] the returned range is empty. + */ +public infix fun Char.until(to: Char): CharRange { + if (to <= '\u0000') return CharRange.EMPTY + return this .. (to - 1).toChar() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * + * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. + */ +public infix fun Int.until(to: Int): IntRange { + if (to <= Int.MIN_VALUE) return IntRange.EMPTY + return this .. (to - 1).toInt() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public infix 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. + * + * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. + */ +public infix fun Byte.until(to: Int): IntRange { + if (to <= Int.MIN_VALUE) return IntRange.EMPTY + return this.toInt() .. (to - 1).toInt() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * + * If the [to] value is less than or equal to [Int.MIN_VALUE] the returned range is empty. + */ +public infix fun Short.until(to: Int): IntRange { + if (to <= Int.MIN_VALUE) return IntRange.EMPTY + return this.toInt() .. (to - 1).toInt() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * + * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. + */ +public infix fun Int.until(to: Long): LongRange { + if (to <= Long.MIN_VALUE) return LongRange.EMPTY + return this.toLong() .. (to - 1).toLong() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * + * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. + */ +public infix fun Long.until(to: Long): LongRange { + if (to <= Long.MIN_VALUE) return LongRange.EMPTY + return this .. (to - 1).toLong() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * + * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. + */ +public infix fun Byte.until(to: Long): LongRange { + if (to <= Long.MIN_VALUE) return LongRange.EMPTY + return this.toLong() .. (to - 1).toLong() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + * + * If the [to] value is less than or equal to [Long.MIN_VALUE] the returned range is empty. + */ +public infix fun Short.until(to: Long): LongRange { + if (to <= Long.MIN_VALUE) return LongRange.EMPTY + return this.toLong() .. (to - 1).toLong() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public infix 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 infix 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. + */ +public infix fun Byte.until(to: Short): IntRange { + return this.toInt() .. (to.toInt() - 1).toInt() +} + +/** + * Returns a range from this value up to but excluding the specified [to] value. + */ +public infix fun Short.until(to: Short): IntRange { + return this.toInt() .. (to.toInt() - 1).toInt() +} +