io.slink.tsv.tsv.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of slink-zero Show documentation
Show all versions of slink-zero Show documentation
Zero dependenices library to boost your Kotlin development
The newest version!
package io.slink.tsv
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.nio.charset.Charset
fun InputStream.readTsv(
commentChar: Char = '#',
charset: Charset = Charsets.UTF_8,
processRow: (List) -> Unit,
) {
BufferedReader(InputStreamReader(this, charset)).useLines {
it.forEach { line ->
line.toTsvRow(commentChar)?.let(processRow)
}
}
}
fun InputStream.readTsv(commentChar: Char = '#', charset: Charset = Charsets.UTF_8): List> {
val rows = mutableListOf>()
readTsv(commentChar, charset) { rows.add(it) }
return rows
}
fun String.readTsv(commentChar: Char = '#', processRow: (List) -> Unit) {
split("\n").forEach { line ->
line.toTsvRow(commentChar)?.let(processRow)
}
}
fun String.readTsv(commentChar: Char = '#'): List> {
val rows = mutableListOf>()
readTsv(commentChar) { rows.add(it) }
return rows
}
fun InputStream.readTsvWithHeader(
commentChar: Char = '#',
charset: Charset = Charsets.UTF_8,
processRow: (Map) -> Unit,
) {
var headers: List? = null
BufferedReader(InputStreamReader(this, charset)).useLines {
it.forEach { line ->
if (headers == null) {
headers = line.toTsvRow(commentChar)
} else {
line.toTsvRow(headers!!, commentChar)?.let(processRow)
}
}
}
}
fun InputStream.readTsvWithHeader(commentChar: Char = '#', charset: Charset = Charsets.UTF_8): List