summaryrefslogtreecommitdiff
path: root/src/main/kotlin/Path.kt
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2021-08-13 00:54:39 +0200
committernea <romangraef@gmail.com>2021-08-13 00:54:39 +0200
commit06d9212e5901b8b73e17bb8938b0c4542a42770b (patch)
tree15ca09200aea736f4342d34d29a08f343e4d2c68 /src/main/kotlin/Path.kt
parent2ee4b7c2bef9f80ed12e58bc3cdbb965299920e1 (diff)
downloadwebos-06d9212e5901b8b73e17bb8938b0c4542a42770b.tar.gz
webos-06d9212e5901b8b73e17bb8938b0c4542a42770b.tar.bz2
webos-06d9212e5901b8b73e17bb8938b0c4542a42770b.zip
test basics
Diffstat (limited to 'src/main/kotlin/Path.kt')
-rw-r--r--src/main/kotlin/Path.kt75
1 files changed, 0 insertions, 75 deletions
diff --git a/src/main/kotlin/Path.kt b/src/main/kotlin/Path.kt
deleted file mode 100644
index 0e209cf..0000000
--- a/src/main/kotlin/Path.kt
+++ /dev/null
@@ -1,75 +0,0 @@
-import util.expandWith
-
-sealed interface Path {
- val parts: List<String>
- fun toAbsolutePath(relativeTo: Absolute): Absolute {
- return relativeTo.resolve(this)
- }
-
- fun resolve(path: Path): Path
-
- companion object {
- val root = Absolute(listOf())
-
- fun ofShell(string: String, userHome: Absolute): Path =
- ofShell(string.split("/"), userHome)
-
- fun ofShell(vararg parts: String, userHome: Absolute): Path =
- ofShell(parts.toList(), userHome)
-
- fun of(vararg parts: String): Path =
- of(parts.toList())
-
- fun of(string: String): Path =
- of(string.split("/"))
-
- fun ofShell(parts: List<String>, userHome: Absolute): Path {
- if (parts.firstOrNull() == "~")
- return userHome.resolve(Relative(parts.subList(1, parts.size).filter { it.isNotEmpty() }))
- return of(parts)
- }
-
- fun of(parts: List<String>): Path {
- if (parts.isEmpty())
- return root
- if (parts[0] == "") // Starts with a /
- return Absolute(parts.subList(1, parts.size).filter { it.isNotEmpty() })
- return Relative(parts.filter { it.isNotEmpty() })
- }
- }
-
- data class Relative internal constructor(override val parts: List<String>) : Path {
- override fun resolve(path: Path): Path {
- if (path is Absolute) return path
- return Relative(this.parts + path.parts)
- }
- }
-
- data class Absolute internal constructor(override val parts: List<String>) : Path {
- override fun resolve(path: Path): Absolute {
- if (path is Absolute) return path
- return Absolute(this.parts + path.parts)
- }
-
- fun relativize(path: Path): Relative = when (path) {
- is Relative -> path
- is Absolute -> {
- var commonPrefix = true
- val partList = mutableListOf<String>()
- var returns = 0
- for ((idx, part) in path.parts.withIndex()) {
- if (idx < this.parts.size) {
- if (this.parts[idx] == part && commonPrefix) {
- continue
- } else {
- commonPrefix = false
- returns++
- }
- }
- partList.add(part)
- }
- Relative(List(returns) { ".." } + partList)
- }
- }
- }
-}