From 488cf35aaf85d8b99d4e16f941039c6615fff57f Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Thu, 8 Mar 2012 11:02:07 -0500 Subject: [PATCH 1/3] Added extension methods to convert String to all primitives. Also for String to ByteArray, and ByteArray to String using a character encoding and then reused in the IO extension methods to simplify. --- libraries/stdlib/src/Arrays.kt | 11 +++++ libraries/stdlib/src/JavaIo.kt | 75 +++++++++++++++++++--------------- libraries/stdlib/src/String.kt | 16 ++++++++ 3 files changed, 68 insertions(+), 34 deletions(-) diff --git a/libraries/stdlib/src/Arrays.kt b/libraries/stdlib/src/Arrays.kt index 435e65ae488..f83857db82b 100644 --- a/libraries/stdlib/src/Arrays.kt +++ b/libraries/stdlib/src/Arrays.kt @@ -3,6 +3,7 @@ package kotlin import java.io.ByteArrayInputStream import java.util.Arrays +import java.nio.charset.Charset // Array "constructor" inline fun array(vararg t : T) : Array = t @@ -101,6 +102,16 @@ inline val ByteArray.inputStream : ByteArrayInputStream inline fun ByteArray.inputStream(offset: Int, length: Int) = ByteArrayInputStream(this, offset, length) +inline fun ByteArray.toString(encoding: String?):String { + if(encoding==null) { + return String(this, encoding) + } else { + return String(this) + } +} + +inline fun ByteArray.toString(encoding: Charset) = String(this, encoding) + /** Returns true if the array is not empty */ inline fun Array.notEmpty() : Boolean = !this.isEmpty() diff --git a/libraries/stdlib/src/JavaIo.kt b/libraries/stdlib/src/JavaIo.kt index bcce2e368f8..108083112f0 100644 --- a/libraries/stdlib/src/JavaIo.kt +++ b/libraries/stdlib/src/JavaIo.kt @@ -155,11 +155,11 @@ get() = InputStreamReader(this) inline val InputStream.bufferedReader : BufferedReader get() = BufferedReader(reader) -inline fun InputStream.reader(charset: Charset) : InputStreamReader = InputStreamReader(this, charset) +inline fun InputStream.reader(encoding: Charset) : InputStreamReader = InputStreamReader(this, encoding) -inline fun InputStream.reader(charsetName: String) = InputStreamReader(this, charsetName) +inline fun InputStream.reader(encoding: String) = InputStreamReader(this, encoding) -inline fun InputStream.reader(charsetDecoder: CharsetDecoder) = InputStreamReader(this, charsetDecoder) +inline fun InputStream.reader(encoding: CharsetDecoder) = InputStreamReader(this, encoding) inline val InputStream.buffered : BufferedInputStream get() = if(this is BufferedInputStream) this else BufferedInputStream(this) @@ -289,22 +289,13 @@ fun File.relativePath(descendant: File): String { } } -/** - * Reads the entire content of the file as a String - * - * This method is not recommended on huge files. - */ -fun File.readText(encoding: String = defaultCharset): String { - return readBytes().toString(encoding) -} - /** * Reads the entire content of the file as bytes * * This method is not recommended on huge files. */ fun File.readBytes(): ByteArray { - return FileInputStream(this).use{ it.readBytes() } + return FileInputStream(this).use{ it.readBytes(this.length().toInt()) } } /** @@ -313,13 +304,36 @@ fun File.readBytes(): ByteArray { fun File.writeBytes(data: ByteArray): Unit { return FileOutputStream(this).use{ it.write(data) } } +/** + * Reads the entire content of the file as a String using the optional + * character encoding. The default platform encoding is used if the character + * encoding is not specified or null. + * + * This method is not recommended on huge files. + */ +fun File.readText(encoding:String? = null) = readBytes().toString(encoding) /** - * Writes the text as the contents of the file + * Reads the entire content of the file as a String using the + * character encoding. + * + * This method is not recommended on huge files. */ -fun File.writeText(text: String): Unit { - return FileWriter(this).use{ it.write(text) } -} +fun File.readText(encoding:Charset) = readBytes().toString(encoding) + +/** + * Writes the text as the contents of the file using the optional + * character encoding. The default platform encoding is used if the character + * encoding is not specified or null. + */ +fun File.writeText(text: String, encoding:String?=null): Unit { writeBytes(text.toByteArray(encoding)) } + +/** + * Writes the text as the contents of the file using the optional + * character encoding. The default platform encoding is used if the character + * encoding is not specified or null. + */ +fun File.writeText(text: String, encoding:Charset): Unit { writeBytes(text.toByteArray(encoding)) } /** * Copies this file to the given output file, returning the number of bytes copied @@ -340,8 +354,8 @@ fun File.copyTo(file: File, bufferSize: Int = defaultBufferSize): Long { * * **Note** it is the callers responsibility to close this resource */ -fun InputStream.readBytes(): ByteArray { - val buffer = ByteArrayOutputStream() +fun InputStream.readBytes(estimatedSize: Int = defaultBufferSize): ByteArray { + val buffer = ByteArrayOutputStream(estimatedSize) this.copyTo(buffer) return buffer.toByteArray().sure() } @@ -353,8 +367,8 @@ fun InputStream.readBytes(): ByteArray { */ fun Reader.readText(): String { val buffer = StringWriter() - this.copyTo(buffer) - return buffer.toString() ?: "" + copyTo(buffer) + return buffer.toString().sure() } /** @@ -374,7 +388,6 @@ fun InputStream.copyTo(out: OutputStream, bufferSize: Int = defaultBufferSize): return bytesCopied } - /** * Copies this reader to the given output writer, returning the number of bytes copied. * @@ -392,30 +405,24 @@ fun Reader.copyTo(out: Writer, bufferSize: Int = defaultBufferSize): Long { return charsCopied } - /** * Reads the entire content of the URL as a String with an optional character set name * * This method is not recommended on huge files. */ -fun URL.readText(encoding: String = defaultCharset): String { - val bytes = readBytes() - return bytes.toString(encoding) -} +fun URL.readText(encoding: String? = null): String = readBytes().toString(encoding) /** - * Converts the bytes to a [[String]] using the given encoding or uses the [[defaultCharset]] (UTF-8) + * Reads the entire content of the URL as a String with the specified character encoding. + * + * This method is not recommended on huge files. */ -fun ByteArray.toString(encoding: String = defaultCharset): String { - return if (encoding != null) String(this, encoding) else String(this) -} +fun URL.readText(encoding: Charset): String = readBytes().toString(encoding) /** * Reads the entire content of the URL as bytes * * This method is not recommended on huge files. */ -fun URL.readBytes(): ByteArray { - return this.openStream().sure().use{ it.readBytes() } -} +fun URL.readBytes(): ByteArray = this.openStream().sure().use{ it.readBytes() } diff --git a/libraries/stdlib/src/String.kt b/libraries/stdlib/src/String.kt index 9c5b9922e2d..8605201a610 100644 --- a/libraries/stdlib/src/String.kt +++ b/libraries/stdlib/src/String.kt @@ -97,6 +97,22 @@ inline fun String(stringBuilder : java.lang.StringBuilder) = java.lang.String(st /** Returns true if the string is not null and not empty */ inline fun String?.notEmpty() : Boolean = this != null && this.length() > 0 +inline fun String.toByteArray(encoding: String?=null):ByteArray { + if(encoding==null) { + return (this as java.lang.String).getBytes().sure() + } else { + return (this as java.lang.String).getBytes(encoding).sure() + } +} +inline fun String.toByteArray(encoding: java.nio.charset.Charset):ByteArray = (this as java.lang.String).getBytes(encoding).sure() + +inline fun String.toBoolean() = java.lang.Boolean.parseBoolean(this).sure() +inline fun String.toShort() = java.lang.Short.parseShort(this).sure() +inline fun String.toInt() = java.lang.Integer.parseInt(this).sure() +inline fun String.toLong() = java.lang.Long.parseLong(this).sure() +inline fun String.toFloat() = java.lang.Float.parseFloat(this).sure() +inline fun String.toDouble() = java.lang.Double.parseDouble(this).sure() + /** Iterator for characters of given CharSequence */ From 75dcb84c2eafb311bcfbd249880bb086525eda7b Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Thu, 8 Mar 2012 11:15:22 -0500 Subject: [PATCH 2/3] Fixes an NPE when compiling from the IDE. --- idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java b/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java index 056ea2b87d4..4b26526c40e 100644 --- a/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java +++ b/idea/src/org/jetbrains/jet/plugin/compiler/JetCompiler.java @@ -257,7 +257,7 @@ public class JetCompiler implements TranslatingCompiler { context.addMessage(INFORMATION, "Using kotlinHome=" + kotlinHome, "", -1, -1); context.addMessage(INFORMATION, "Invoking in-process compiler " + compilerClassName + " with arguments " + Arrays.asList(arguments), "", -1, -1); - Object rc = exec.invoke(null, out, arguments); + Object rc = exec.invoke(kompiler.newInstance(), out, arguments); if (rc instanceof Integer) { return ((Integer) rc).intValue(); } From b8dda416f3013d3d2fe4bd1527973b0d0443ce42 Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Wed, 7 Mar 2012 23:58:56 -0500 Subject: [PATCH 3/3] Remove functions which could conflict with the kotlin.assert function. --- examples/example-vfs/src/Utils.kt | 11 ----------- libraries/testlib/test/Test.kt | 13 +------------ libraries/testlib/test/TestDslExample.kt | 2 +- 3 files changed, 2 insertions(+), 24 deletions(-) diff --git a/examples/example-vfs/src/Utils.kt b/examples/example-vfs/src/Utils.kt index 6b8d98efb65..8709d9e17c1 100644 --- a/examples/example-vfs/src/Utils.kt +++ b/examples/example-vfs/src/Utils.kt @@ -16,17 +16,6 @@ private fun locked(lock : Lock, body : () -> T) : T { } } -/** - * Checks boolean value which should be true. If it is false, throws Assertion Error - * - * @param value value to be checked - */ -public fun assert(value : Boolean) { - if (!value) { - throw AssertionError() - } -} - /** * Returns list containing all elements which first contains and second does not. */ diff --git a/libraries/testlib/test/Test.kt b/libraries/testlib/test/Test.kt index f6eade6af77..14913d41086 100644 --- a/libraries/testlib/test/Test.kt +++ b/libraries/testlib/test/Test.kt @@ -61,23 +61,12 @@ private fun testSuite(builder: TestBuilder, description: TestBuilder.( fun testSuite(name: String, description: TestBuilder.() -> Unit) : TestSuite? = testSuite(TestBuilder(name), description) -fun assert(message: String, block: ()-> Boolean) { - val actual = block() - Assert.assertTrue(message, actual) -} - -fun assert(block: ()-> Boolean) = assert(block.toString(), block) - fun assertNot(message: String, block: ()-> Boolean) { - assert(message){ !block() } + Assert.assertTrue(message, block()) } fun assertNot(block: ()-> Boolean) = assertNot(block.toString(), block) -fun assert(actual: Boolean, message: String) { - println("Answer: ${actual} for ${message}") -} - fun assertTrue(actual: Boolean, message: String = "") { return assertEquals(true, actual, message) } diff --git a/libraries/testlib/test/TestDslExample.kt b/libraries/testlib/test/TestDslExample.kt index 4bbdf7bdf7b..536df38528e 100644 --- a/libraries/testlib/test/TestDslExample.kt +++ b/libraries/testlib/test/TestDslExample.kt @@ -25,7 +25,7 @@ public val suite : TestSuite? = testSuite("test group") { } "test 2" - { - assert(state == 31, "message") + assertTrue(state == 31, "message") println("test '${getName()}': state: $state") }