Merge branch 'master' of github.com:JetBrains/kotlin
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user