Added generator and generated for downTo() extension functions for numbers.
#KT-2519 in progress
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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<File>, 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<String>) {
|
||||
generateFile(File(outDir, "StandardFromJUtilCollectionsJVM.kt"), "package kotlin", File(srcDir, "JUtilCollectionsJVM.kt")) {
|
||||
it.replaceAll("java.util.Collection<T", "Iterable<T").replaceAll("(this.size)", "")
|
||||
}
|
||||
|
||||
generateDownTos(File(outDir, "DownTo.kt"), "package kotlin")
|
||||
}
|
||||
|
||||
// Pretty hacky way to code generate; ideally we'd be using the AST and just changing the function prototypes
|
||||
|
||||
Reference in New Issue
Block a user