kotlinx.serialization.csv.decode.StringSource.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-serialization-csv Show documentation
Show all versions of kotlinx-serialization-csv Show documentation
Library to easily use Kotlin Serialization to serialize to/from CSV.
package kotlinx.serialization.csv.decode
private const val EOF = -1
/**
* Character stream generated from a [String].
*/
internal class StringSource(private val content: String) : Source {
private var position = 0
private var marks = arrayListOf()
override fun canRead() = position != EOF
override fun read() =
peek().also {
when (position) {
in content.indices -> position++
else -> position = EOF
}
}
override fun peek() =
when (position) {
in content.indices -> content[position]
else -> null
}
override fun mark() {
marks.add(position)
}
override fun unmark() {
marks.removeAt(marks.size - 1)
}
override fun reset() {
position = marks.removeAt(marks.size - 1)
}
override fun toString(): String {
return "StringSource(position=$position, content.length=${content.length})"
}
}