Merge pull request #15 from chirino/master
stdlib enhancements around String conversions.
This commit is contained in:
@@ -16,17 +16,6 @@ private fun locked<T>(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.
|
* Returns list containing all elements which first contains and second does not.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -257,7 +257,7 @@ public class JetCompiler implements TranslatingCompiler {
|
|||||||
context.addMessage(INFORMATION, "Using kotlinHome=" + kotlinHome, "", -1, -1);
|
context.addMessage(INFORMATION, "Using kotlinHome=" + kotlinHome, "", -1, -1);
|
||||||
context.addMessage(INFORMATION, "Invoking in-process compiler " + compilerClassName + " with arguments " + Arrays.asList(arguments), "", -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) {
|
if (rc instanceof Integer) {
|
||||||
return ((Integer) rc).intValue();
|
return ((Integer) rc).intValue();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package kotlin
|
|||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
|
|
||||||
import java.util.Arrays
|
import java.util.Arrays
|
||||||
|
import java.nio.charset.Charset
|
||||||
|
|
||||||
// Array "constructor"
|
// Array "constructor"
|
||||||
inline fun <T> array(vararg t : T) : Array<T> = t
|
inline fun <T> array(vararg t : T) : Array<T> = 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.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 */
|
/** Returns true if the array is not empty */
|
||||||
inline fun <T> Array<T>.notEmpty() : Boolean = !this.isEmpty()
|
inline fun <T> Array<T>.notEmpty() : Boolean = !this.isEmpty()
|
||||||
|
|
||||||
|
|||||||
@@ -155,11 +155,11 @@ get() = InputStreamReader(this)
|
|||||||
inline val InputStream.bufferedReader : BufferedReader
|
inline val InputStream.bufferedReader : BufferedReader
|
||||||
get() = BufferedReader(reader)
|
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
|
inline val InputStream.buffered : BufferedInputStream
|
||||||
get() = if(this is BufferedInputStream) this else BufferedInputStream(this)
|
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
|
* Reads the entire content of the file as bytes
|
||||||
*
|
*
|
||||||
* This method is not recommended on huge files.
|
* This method is not recommended on huge files.
|
||||||
*/
|
*/
|
||||||
fun File.readBytes(): ByteArray {
|
fun File.readBytes(): ByteArray {
|
||||||
return FileInputStream(this).use<FileInputStream,ByteArray>{ it.readBytes() }
|
return FileInputStream(this).use<FileInputStream,ByteArray>{ it.readBytes(this.length().toInt()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -313,13 +304,36 @@ fun File.readBytes(): ByteArray {
|
|||||||
fun File.writeBytes(data: ByteArray): Unit {
|
fun File.writeBytes(data: ByteArray): Unit {
|
||||||
return FileOutputStream(this).use<FileOutputStream,Unit>{ it.write(data) }
|
return FileOutputStream(this).use<FileOutputStream,Unit>{ 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 {
|
fun File.readText(encoding:Charset) = readBytes().toString(encoding)
|
||||||
return FileWriter(this).use<FileWriter,Unit>{ it.write(text) }
|
|
||||||
}
|
/**
|
||||||
|
* 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
|
* 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
|
* **Note** it is the callers responsibility to close this resource
|
||||||
*/
|
*/
|
||||||
fun InputStream.readBytes(): ByteArray {
|
fun InputStream.readBytes(estimatedSize: Int = defaultBufferSize): ByteArray {
|
||||||
val buffer = ByteArrayOutputStream()
|
val buffer = ByteArrayOutputStream(estimatedSize)
|
||||||
this.copyTo(buffer)
|
this.copyTo(buffer)
|
||||||
return buffer.toByteArray().sure()
|
return buffer.toByteArray().sure()
|
||||||
}
|
}
|
||||||
@@ -353,8 +367,8 @@ fun InputStream.readBytes(): ByteArray {
|
|||||||
*/
|
*/
|
||||||
fun Reader.readText(): String {
|
fun Reader.readText(): String {
|
||||||
val buffer = StringWriter()
|
val buffer = StringWriter()
|
||||||
this.copyTo(buffer)
|
copyTo(buffer)
|
||||||
return buffer.toString() ?: ""
|
return buffer.toString().sure()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -374,7 +388,6 @@ fun InputStream.copyTo(out: OutputStream, bufferSize: Int = defaultBufferSize):
|
|||||||
return bytesCopied
|
return bytesCopied
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies this reader to the given output writer, returning the number of bytes copied.
|
* 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
|
return charsCopied
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the entire content of the URL as a String with an optional character set name
|
* Reads the entire content of the URL as a String with an optional character set name
|
||||||
*
|
*
|
||||||
* This method is not recommended on huge files.
|
* This method is not recommended on huge files.
|
||||||
*/
|
*/
|
||||||
fun URL.readText(encoding: String = defaultCharset): String {
|
fun URL.readText(encoding: String? = null): String = readBytes().toString(encoding)
|
||||||
val bytes = readBytes()
|
|
||||||
return bytes.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 {
|
fun URL.readText(encoding: Charset): String = readBytes().toString(encoding)
|
||||||
return if (encoding != null) String(this, encoding) else String(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the entire content of the URL as bytes
|
* Reads the entire content of the URL as bytes
|
||||||
*
|
*
|
||||||
* This method is not recommended on huge files.
|
* This method is not recommended on huge files.
|
||||||
*/
|
*/
|
||||||
fun URL.readBytes(): ByteArray {
|
fun URL.readBytes(): ByteArray = this.openStream().sure().use<InputStream,ByteArray>{ it.readBytes() }
|
||||||
return this.openStream().sure().use<InputStream,ByteArray>{ it.readBytes() }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -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 */
|
/** Returns true if the string is not null and not empty */
|
||||||
inline fun String?.notEmpty() : Boolean = this != null && this.length() > 0
|
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
|
Iterator for characters of given CharSequence
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -61,23 +61,12 @@ private fun <T> testSuite(builder: TestBuilder<T>, description: TestBuilder<T>.(
|
|||||||
fun <T> testSuite(name: String, description: TestBuilder<T>.() -> Unit) : TestSuite? =
|
fun <T> testSuite(name: String, description: TestBuilder<T>.() -> Unit) : TestSuite? =
|
||||||
testSuite(TestBuilder<T>(name), description)
|
testSuite(TestBuilder<T>(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) {
|
fun assertNot(message: String, block: ()-> Boolean) {
|
||||||
assert(message){ !block() }
|
Assert.assertTrue(message, block())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun assertNot(block: ()-> Boolean) = assertNot(block.toString(), 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 = "") {
|
fun assertTrue(actual: Boolean, message: String = "") {
|
||||||
return assertEquals(true, actual, message)
|
return assertEquals(true, actual, message)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public val suite : TestSuite? = testSuite<Int>("test group") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
"test 2" - {
|
"test 2" - {
|
||||||
assert(state == 31, "message")
|
assertTrue(state == 31, "message")
|
||||||
println("test '${getName()}': state: $state")
|
println("test '${getName()}': state: $state")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user