All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlinx.serialization.csv.decode.StringSource.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
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})"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy