From 662fdca034eb3789f92cbaf087a77bb45ffca846 Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Tue, 7 Aug 2012 21:59:54 +0400 Subject: [PATCH] Added generator and generated for downTo() extension functions for numbers. #KT-2519 in progress --- libraries/stdlib/src/generated/DownTo.kt | 203 ++++++++++++++++++ .../jetbrains/kotlin/tools/GenerateDownTos.kt | 59 +++++ .../kotlin/tools/GenerateStandardLib.kt | 12 +- 3 files changed, 270 insertions(+), 4 deletions(-) create mode 100644 libraries/stdlib/src/generated/DownTo.kt create mode 100644 libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateDownTos.kt diff --git a/libraries/stdlib/src/generated/DownTo.kt b/libraries/stdlib/src/generated/DownTo.kt new file mode 100644 index 00000000000..068cde743bd --- /dev/null +++ b/libraries/stdlib/src/generated/DownTo.kt @@ -0,0 +1,203 @@ +package kotlin + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + + +public inline fun Byte.downTo(to: Byte): ByteRange { + return if (this >= to) ByteRange(this, to - this - 1) else ByteRange(0, 0) +} + +public inline fun Byte.downTo(to: Char): CharRange { + return if (this >= to) CharRange(this.toChar(), to - this - 1) else CharRange(0.toChar(), 0) +} + +public inline fun Byte.downTo(to: Short): ShortRange { + return if (this >= to) ShortRange(this.toShort(), to - this - 1) else ShortRange(0, 0) +} + +public inline fun Byte.downTo(to: Int): IntRange { + return if (this >= to) IntRange(this.toInt(), to - this - 1) else IntRange(0, 0) +} + +public inline fun Byte.downTo(to: Long): LongRange { + return if (this >= to) LongRange(this.toLong(), to - this - 1) else LongRange(0, 0) +} + +public inline fun Byte.downTo(to: Float): FloatRange { + return FloatRange(this.toFloat(), to - this) +} + +public inline fun Byte.downTo(to: Double): DoubleRange { + return DoubleRange(this.toDouble(), to - this) +} + +public inline fun Char.downTo(to: Byte): CharRange { + return if (this >= to) CharRange(this, to - this - 1) else CharRange(0.toChar(), 0) +} + +public inline fun Char.downTo(to: Char): CharRange { + return if (this >= to) CharRange(this, to - this - 1) else CharRange(0.toChar(), 0) +} + +public inline fun Char.downTo(to: Short): ShortRange { + return if (this >= to) ShortRange(this.toShort(), to - this - 1) else ShortRange(0, 0) +} + +public inline fun Char.downTo(to: Int): IntRange { + return if (this >= to) IntRange(this.toInt(), to - this - 1) else IntRange(0, 0) +} + +public inline fun Char.downTo(to: Long): LongRange { + return if (this >= to) LongRange(this.toLong(), to - this - 1) else LongRange(0, 0) +} + +public inline fun Char.downTo(to: Float): FloatRange { + return FloatRange(this.toFloat(), to - this) +} + +public inline fun Char.downTo(to: Double): DoubleRange { + return DoubleRange(this.toDouble(), to - this) +} + +public inline fun Short.downTo(to: Byte): ShortRange { + return if (this >= to) ShortRange(this, to - this - 1) else ShortRange(0, 0) +} + +public inline fun Short.downTo(to: Char): ShortRange { + return if (this >= to) ShortRange(this, to - this - 1) else ShortRange(0, 0) +} + +public inline fun Short.downTo(to: Short): ShortRange { + return if (this >= to) ShortRange(this, to - this - 1) else ShortRange(0, 0) +} + +public inline fun Short.downTo(to: Int): IntRange { + return if (this >= to) IntRange(this.toInt(), to - this - 1) else IntRange(0, 0) +} + +public inline fun Short.downTo(to: Long): LongRange { + return if (this >= to) LongRange(this.toLong(), to - this - 1) else LongRange(0, 0) +} + +public inline fun Short.downTo(to: Float): FloatRange { + return FloatRange(this.toFloat(), to - this) +} + +public inline fun Short.downTo(to: Double): DoubleRange { + return DoubleRange(this.toDouble(), to - this) +} + +public inline fun Int.downTo(to: Byte): IntRange { + return if (this >= to) IntRange(this, to - this - 1) else IntRange(0, 0) +} + +public inline fun Int.downTo(to: Char): IntRange { + return if (this >= to) IntRange(this, to - this - 1) else IntRange(0, 0) +} + +public inline fun Int.downTo(to: Short): IntRange { + return if (this >= to) IntRange(this, to - this - 1) else IntRange(0, 0) +} + +public inline fun Int.downTo(to: Int): IntRange { + return if (this >= to) IntRange(this, to - this - 1) else IntRange(0, 0) +} + +public inline fun Int.downTo(to: Long): LongRange { + return if (this >= to) LongRange(this.toLong(), to - this - 1) else LongRange(0, 0) +} + +public inline fun Int.downTo(to: Float): FloatRange { + return FloatRange(this.toFloat(), to - this) +} + +public inline fun Int.downTo(to: Double): DoubleRange { + return DoubleRange(this.toDouble(), to - this) +} + +public inline fun Long.downTo(to: Byte): LongRange { + return if (this >= to) LongRange(this, to - this - 1) else LongRange(0, 0) +} + +public inline fun Long.downTo(to: Char): LongRange { + return if (this >= to) LongRange(this, to - this - 1) else LongRange(0, 0) +} + +public inline fun Long.downTo(to: Short): LongRange { + return if (this >= to) LongRange(this, to - this - 1) else LongRange(0, 0) +} + +public inline fun Long.downTo(to: Int): LongRange { + return if (this >= to) LongRange(this, to - this - 1) else LongRange(0, 0) +} + +public inline fun Long.downTo(to: Long): LongRange { + return if (this >= to) LongRange(this, to - this - 1) else LongRange(0, 0) +} + +public inline fun Long.downTo(to: Float): FloatRange { + return FloatRange(this.toFloat(), to - this) +} + +public inline fun Long.downTo(to: Double): DoubleRange { + return DoubleRange(this.toDouble(), to - this) +} + +public inline fun Float.downTo(to: Byte): FloatRange { + return FloatRange(this, to - this) +} + +public inline fun Float.downTo(to: Char): FloatRange { + return FloatRange(this, to - this) +} + +public inline fun Float.downTo(to: Short): FloatRange { + return FloatRange(this, to - this) +} + +public inline fun Float.downTo(to: Int): FloatRange { + return FloatRange(this, to - this) +} + +public inline fun Float.downTo(to: Long): FloatRange { + return FloatRange(this, to - this) +} + +public inline fun Float.downTo(to: Float): FloatRange { + return FloatRange(this, to - this) +} + +public inline fun Float.downTo(to: Double): DoubleRange { + return DoubleRange(this.toDouble(), to - this) +} + +public inline fun Double.downTo(to: Byte): DoubleRange { + return DoubleRange(this, to - this) +} + +public inline fun Double.downTo(to: Char): DoubleRange { + return DoubleRange(this, to - this) +} + +public inline fun Double.downTo(to: Short): DoubleRange { + return DoubleRange(this, to - this) +} + +public inline fun Double.downTo(to: Int): DoubleRange { + return DoubleRange(this, to - this) +} + +public inline fun Double.downTo(to: Long): DoubleRange { + return DoubleRange(this, to - this) +} + +public inline fun Double.downTo(to: Float): DoubleRange { + return DoubleRange(this, to - this) +} + +public inline fun Double.downTo(to: Double): DoubleRange { + return DoubleRange(this, to - this) +} diff --git a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateDownTos.kt b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateDownTos.kt new file mode 100644 index 00000000000..e719de5f4e8 --- /dev/null +++ b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateDownTos.kt @@ -0,0 +1,59 @@ +package org.jetbrains.kotlin.tools + +import java.io.File +import java.io.FileWriter +import java.io.PrintWriter + +private fun generateDownTos(outputFile: File, header: String) { + + private fun getMaxType(fromType: String, toType: String): String { + return when { + fromType == "Double" || toType == "Double" -> "Double" + fromType == "Float" || toType == "Float" -> "Float" + fromType == "Long" || toType == "Long" -> "Long" + fromType == "Int" || toType == "Int" -> "Int" + fromType == "Short" || toType == "Short" -> "Short" + fromType == "Char" || toType == "Char" -> "Char" + else -> "Byte" + } + } + + private fun generateDownTo(writer: PrintWriter, fromType: String, toType: String) { + val elementType = getMaxType(fromType, toType) + val rangeType = elementType + "Range" + val fromExpr = "this${ if (elementType == fromType) "" else ".to$elementType()" }" + if (elementType == "Float" || elementType == "Double") { + writer.println(""" +public inline fun $fromType.downTo(to: $toType): $rangeType { + return $rangeType($fromExpr, to - this) +}""") + } else { + // TODO use empty range constant, which is not available yet (KT-2583) + writer.println(""" +public inline fun $fromType.downTo(to: $toType): $rangeType { + return if (this >= to) $rangeType($fromExpr, to - this - 1) else $rangeType(${if (elementType == "Char") "0.toChar()" else "0"}, 0) +}""") + } + } + + println("Writing $outputFile") + + outputFile.getParentFile()?.mkdirs() + val writer = PrintWriter(FileWriter(outputFile)) + try { + writer.println(header) + + writer.println(""" +$COMMON_AUTOGENERATED_WARNING +""") + + val types = array("Byte", "Char", "Short", "Int", "Long", "Float", "Double") + for (fromType in types) { + for (toType in types) { + generateDownTo(writer, fromType, toType) + } + } + } finally { + writer.close() + } +} \ No newline at end of file diff --git a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt index e24f50ffff9..6361afe4956 100644 --- a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt +++ b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt @@ -3,6 +3,11 @@ package org.jetbrains.kotlin.tools import java.io.* import java.util.List +private val COMMON_AUTOGENERATED_WARNING: String = """// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +//""" + fun generateFile(outFile: File, header: String, inputFile: File, f: (String)-> String) { generateFile(outFile, header, arrayList(inputFile), f) } @@ -19,10 +24,7 @@ fun generateFile(outFile: File, header: String, inputFiles: List, f: (Stri for (file in inputFiles) { writer.println(""" -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// +$COMMON_AUTOGENERATED_WARNING // Generated from input file: $file // """) @@ -158,6 +160,8 @@ fun main(args: Array) { generateFile(File(outDir, "StandardFromJUtilCollectionsJVM.kt"), "package kotlin", File(srcDir, "JUtilCollectionsJVM.kt")) { it.replaceAll("java.util.Collection