splitWithNesting, Sequence<String>.flatten(), path fix on win

This commit is contained in:
alexander.nutz
2024-03-28 17:16:56 +01:00
parent c9f9a67697
commit 48f0e27f77
6 changed files with 49 additions and 3 deletions

View File

@@ -24,6 +24,6 @@ repositories {
}
dependencies {
implementation("me.alex_s168:blitz:0.1")
implementation("me.alex_s168:blitz:0.5")
}
```

View File

@@ -5,7 +5,7 @@ plugins {
}
group = "me.alex_s168"
version = "0.3"
version = "0.5"
repositories {
mavenCentral()

View File

@@ -0,0 +1,5 @@
package blitz
fun warn(msg: String) {
System.err.println(msg)
}

View File

@@ -7,4 +7,12 @@ fun ByteBatchSequence.stringify(batch: Int = 64): Sequence<String> {
iter.nextBytes(batch).decodeToString()
else null
}
}
fun Sequence<String>.flatten(): String {
val out = StringBuilder()
forEach {
out.append(it)
}
return out.toString()
}

View File

@@ -84,7 +84,7 @@ value class Path(
if (path.isEmpty()) return Path(emptyArray())
val kx = kotlinx.io.files.Path(path)
if (kx.isAbsolute)
return Path(path.substring(1).split(separator).toTypedArray())
return Path(path.split(separator, '/', '\\').filterNot { it.isEmpty() }.toTypedArray())
return of(SystemFileSystem.resolve(kx).toString())
}
}

View File

@@ -0,0 +1,33 @@
package blitz.str
fun String.splitWithNesting(
delim: Char,
nestUp: Char,
nestDown: Char,
dest: MutableList<String> = mutableListOf()
): MutableList<String> {
val last = StringBuilder()
val iter = iterator()
var nesting = 0
while (iter.hasNext()) {
val c = iter.next()
if (nesting == 0 && c == delim) {
dest.add(last.toString())
last.clear()
} else if (c == nestUp) {
nesting ++
last.append(c)
} else if (c == nestDown) {
if (nesting == 0)
throw Exception("Unmatched $nestDown")
nesting --
last.append(c)
} else {
last.append(c)
}
}
dest.add(last.toString())
if (nesting != 0)
throw Exception("Unmatched $nestUp")
return dest
}