Merge branch 'master' of github.com:JetBrains/kotlin

This commit is contained in:
James Strachan
2012-04-26 06:24:02 +01:00
229 changed files with 4414 additions and 2040 deletions
+27
View File
@@ -124,3 +124,30 @@ public inline fun <T> Array<T>.isEmpty() : Boolean = this.size == 0
/** Returns the array if its not null or else returns an empty array */
public inline fun <T> Array<T>?.orEmpty() : Array<T> = if (this != null) this else array<T>()
public inline val BooleanArray.lastIndex : Int
get() = this.size - 1
public inline val ByteArray.lastIndex : Int
get() = this.size - 1
public inline val ShortArray.lastIndex : Int
get() = this.size - 1
public inline val IntArray.lastIndex : Int
get() = this.size - 1
public inline val LongArray.lastIndex : Int
get() = this.size - 1
public inline val FloatArray.lastIndex : Int
get() = this.size - 1
public inline val DoubleArray.lastIndex : Int
get() = this.size - 1
public inline val CharArray.lastIndex : Int
get() = this.size - 1
public inline val <in T: Any?> Array<T>.lastIndex : Int
get() = this.size - 1
+7 -1
View File
@@ -60,6 +60,8 @@ public inline fun String.format(format : String, vararg args : Any?) : String =
public inline fun String.split(regex : String) : Array<String> = (this as java.lang.String).split(regex) as Array<String>
public inline fun String.split(ch : Char) : Array<String> = (this as java.lang.String).split(java.util.regex.Pattern.quote(ch.toString())) as Array<String>
public inline fun String.substring(beginIndex : Int) : String = (this as java.lang.String).substring(beginIndex).sure()
public inline fun String.substring(beginIndex : Int, endIndex : Int) : String = (this as java.lang.String).substring(beginIndex, endIndex).sure()
@@ -68,10 +70,14 @@ public inline fun String.startsWith(prefix: String) : Boolean = (this as java.la
public inline fun String.startsWith(prefix: String, toffset: Int) : Boolean = (this as java.lang.String).startsWith(prefix, toffset)
public inline fun String.startsWith(ch: Char) : Boolean = (this as java.lang.String).startsWith(ch.toString())
public inline fun String.contains(seq: CharSequence) : Boolean = (this as java.lang.String).contains(seq)
public inline fun String.endsWith(suffix: String) : Boolean = (this as java.lang.String).endsWith(suffix)
public inline fun String.endsWith(ch: Char) : Boolean = (this as java.lang.String).endsWith(ch.toString())
inline val String.size : Int
get() = length()
@@ -226,4 +232,4 @@ public inline fun String.count(predicate: (Char) -> Boolean): Int {
}
}
return answer
}
}
+55
View File
@@ -3,6 +3,8 @@ package kotlin.io
import java.io.*
import java.nio.charset.*
import java.util.NoSuchElementException
import java.util.List
import java.util.ArrayList
import java.net.URL
@@ -147,3 +149,56 @@ public fun File.copyTo(file: File, bufferSize: Int = defaultBufferSize): Long {
}
}
}
/**
* Reads file by byte blocks and calls closure for each block read. Block size depends on implementation but never less than 512.
* This functions passes byte array and amount of bytes in this buffer to the closure function.
*
* You can use this function for huge files
*/
fun File.forEachBlock(closure : (ByteArray, Int) -> Unit) : Unit {
val arr = ByteArray(4096)
val fis = FileInputStream(this)
try {
do {
val size = fis.read(arr)
if (size == -1) {
break
} else if (size > 0) {
closure(arr, size)
}
} while(true)
} finally {
fis.close()
}
}
/**
* Reads file line by line. Default charset is UTF-8.
*
* You may use this function on huge files
*/
fun File.forEachLine (charset : String = "UTF-8", closure : (line : String) -> Unit) : Unit {
val reader = BufferedReader(InputStreamReader(FileInputStream(this), charset))
try {
reader.forEachLine(closure)
} finally {
reader.close()
}
}
/**
* Reads file content as strings list. By default uses UTF-8 charset.
*
* Do not use this function for huge files.
*/
fun File.readLines(charset : String = "UTF-8") : List<String> {
val rs = ArrayList<String>()
this.forEachLine(charset) { (line : String) : Unit ->
rs.add(line);
}
return rs
}
+18
View File
@@ -39,4 +39,22 @@ class ArraysTest() : TestCase() {
assertFalse(iter.hasNext, "Invalid length (hasNext)")
}
fun testEmptyArrayLastIndex() {
val arr1 = IntArray(0)
assertEquals(-1, arr1.lastIndex)
val arr2 = Array<String>(0, {"$it"})
assertEquals(-1, arr2.lastIndex)
}
fun testArrayLastIndex() {
val arr1 = intArray(0, 1, 2, 3, 4)
assertEquals(4, arr1.lastIndex)
assertEquals(4, arr1[arr1.lastIndex])
val arr2 = Array<String>(5, {"$it"})
assertEquals(4, arr2.lastIndex)
assertEquals("4", arr2[arr2.lastIndex])
}
}
+26
View File
@@ -52,4 +52,30 @@ class StringTest {
assertEquals(3, whitespaceCount)
}
test fun testSplitByChar() {
val s = "ab\n[|^$&\\]^cd"
var list = s.split('b');
assertEquals(2, list.size)
assertEquals("a", list[0])
assertEquals("\n[|^$&\\]^cd", list[1])
list = s.split('^')
assertEquals(3, list.size)
assertEquals("cd", list[2])
list = s.split('.')
assertEquals(1, list.size)
assertEquals(s, list[0])
}
test fun testStartsWithChar() {
assertTrue("abcd".startsWith('a'))
assertFalse("abcd".startsWith('b'))
assertFalse("".startsWith('a'))
}
test fun testEndsWithChar() {
assertTrue("abcd".endsWith('d'))
assertFalse("abcd".endsWith('b'))
assertFalse("".endsWith('a'))
}
}