Skip to content

Commit

Permalink
Extract korlibs-serialization-csv and korlibs-serialization-toml (#2180)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Mar 7, 2024
1 parent 41573e1 commit b73321c
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 16 deletions.
2 changes: 2 additions & 0 deletions korge-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dependencies {
commonMainApi(project(":korlibs-template"))
commonMainApi(project(":korge-foundation"))
commonMainApi(project(":korlibs-serialization-yaml"))
commonMainApi(project(":korlibs-serialization-toml"))
commonMainApi(project(":korlibs-serialization-csv"))
commonMainApi(libs.kotlinx.coroutines.core)
commonMainApi(libs.kotlinx.atomicfu)
//add("commonMainApi", libs.kotlinx.atomicfu)
Expand Down
9 changes: 5 additions & 4 deletions korge-core/src/korlibs/io/util/StrReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import korlibs.datastructure.*
import korlibs.datastructure.iterators.fastForEach
import korlibs.io.lang.*
import korlibs.io.stream.*
import korlibs.util.*
import kotlin.collections.*
import kotlin.math.max
import kotlin.math.min

/**
* @TODO: Make this an interface, but inline functions would need to be extension methods breaking source-compatibility
**/
abstract class BaseStrReader {
abstract class BaseStrReader : SimpleStrReader {
abstract val eof: Boolean
abstract val pos: Int
val hasMore: Boolean get() = !eof
override val hasMore: Boolean get() = !eof

abstract fun peekOffset(offset: Int = 0): Char
abstract fun peek(count: Int): String

open fun peekChar(): Char = peekOffset(0)
open fun readChar(): Char {
override fun peekChar(): Char = peekOffset(0)
override fun readChar(): Char {
val out = peekChar()
skip(1)
return out
Expand Down
1 change: 1 addition & 0 deletions korlibs-serialization-csv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
16 changes: 16 additions & 0 deletions korlibs-serialization-csv/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import korlibs.*

description = "Korlibs CSV Serialization Library"

project.extensions.extraProperties.properties.apply {
applyProjectProperties(
"https://raw.githubusercontent.com/korlibs/korge/main/korlibs-serialization-csv",
"Public Domain",
"https://raw.githubusercontent.com/korlibs/korge/main/korlibs-serialization-csv/LICENSE"
)
}


dependencies {
commonMainApi(project(":korlibs-util"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

package korlibs.io.serialization.csv

import korlibs.io.util.StrReader
import korlibs.util.*

class CSV(val lines: List<List<String>>, val names: List<String>? = null) : Collection<CSV.Record> {
val namesToIndex: Map<String, Int> = names?.withIndex()?.associate { it.value to it.index } ?: emptyMap()
Expand Down Expand Up @@ -47,13 +47,13 @@ class CSV(val lines: List<List<String>>, val names: List<String>? = null) : Coll
return values.joinToString("$separator") { serializeElement(it, separator) }
}

fun parseLine(line: String, separator: Char = DEFAULT_SEPARATOR): List<String> = parseLine(StrReader(line), separator)
fun parseLine(line: String, separator: Char = DEFAULT_SEPARATOR): List<String> = parseLine(SimpleStrReader(line), separator)

fun parseLine(line: StrReader, separator: Char = DEFAULT_SEPARATOR): List<String> {
fun parseLine(line: SimpleStrReader, separator: Char = DEFAULT_SEPARATOR): List<String> {
val out = arrayListOf<String>()
val str = StringBuilder()
while (line.hasMore) {
val c = line.read()
val c = line.readChar()
when (c) {
// Quoted string
'"' -> {
Expand Down Expand Up @@ -92,14 +92,14 @@ class CSV(val lines: List<List<String>>, val names: List<String>? = null) : Coll
return out
}

fun parse(s: StrReader, separator: Char = DEFAULT_SEPARATOR, headerNames: Boolean = true): CSV {
fun parse(s: SimpleStrReader, separator: Char = DEFAULT_SEPARATOR, headerNames: Boolean = true): CSV {
val lines = arrayListOf<List<String>>()
while (s.hasMore) {
lines.add(parseLine(s, separator))
}
return if (headerNames) CSV(lines.drop(1), lines[0]) else CSV(lines, null)
}

fun parse(str: String, separator: Char = DEFAULT_SEPARATOR, headerNames: Boolean = true): CSV = parse(StrReader(str), separator, headerNames)
fun parse(str: String, separator: Char = DEFAULT_SEPARATOR, headerNames: Boolean = true): CSV = parse(SimpleStrReader(str), separator, headerNames)
}
}
1 change: 1 addition & 0 deletions korlibs-serialization-toml/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
14 changes: 14 additions & 0 deletions korlibs-serialization-toml/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import korlibs.*

description = "Korlibs TOML Serialization Library"

project.extensions.extraProperties.properties.apply {
applyProjectProperties(
"https://raw.githubusercontent.com/korlibs/korge/main/korlibs-serialization-toml",
"Public Domain",
"https://raw.githubusercontent.com/korlibs/korge/main/korlibs-serialization-toml/LICENSE"
)
}

dependencies {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package korlibs.io.serialization.toml

//import org.intellij.lang.annotations.Language
import korlibs.math.geom.*
import kotlin.math.min

// @TODO: Dates
Expand Down Expand Up @@ -248,8 +247,3 @@ object TOML {
override fun toString(): String = "StrReader[$len](pos=$pos, peek='${str.substring(pos, min(len, pos + 10))}')"
}
}

fun test() {
Angle.between(Point(10, 10), Point(30, 30), Vector2D.UP_SCREEN)
Point.angle(Point(10, 10), Point(30, 30), Vector2D.UP_SCREEN)
}
17 changes: 17 additions & 0 deletions korlibs-util/src/korlibs/util/SimpleStrReader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package korlibs.util

interface SimpleStrReader {
val hasMore: Boolean
fun readChar(): Char
fun peekChar(): Char

companion object {
private class Impl(val str: String, var pos: Int) : SimpleStrReader {
override val hasMore: Boolean get() = pos < str.length

override fun readChar(): Char = peekChar().also { pos++ }
override fun peekChar(): Char = str.getOrElse(pos) { '\u0000' }
}
operator fun invoke(str: String, pos: Int = 0): SimpleStrReader = Impl(str, pos)
}
}
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ include(":korlibs-inject")
include(":korlibs-time")
include(":korlibs-crypto")
include(":korlibs-serialization-yaml")
include(":korlibs-serialization-toml")
include(":korlibs-serialization-csv")
include(":korlibs-template")
include(":korge-foundation")
include(":korge-core")
Expand Down

0 comments on commit b73321c

Please sign in to comment.