summaryrefslogtreecommitdiff
path: root/src/jsMain/kotlin/io/Path.kt
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2021-08-16 15:05:45 +0200
committernea <romangraef@gmail.com>2021-08-16 15:05:45 +0200
commit3e1becfa688f2cba280c475caf39aec3ce14a095 (patch)
treee2d7d18c451c8e89ec0ed4ca9d056997394237b9 /src/jsMain/kotlin/io/Path.kt
parent97278b1e4ecd4db5224db83e167f6c7a68d4abb7 (diff)
downloadwebos-3e1becfa688f2cba280c475caf39aec3ce14a095.tar.gz
webos-3e1becfa688f2cba280c475caf39aec3ce14a095.tar.bz2
webos-3e1becfa688f2cba280c475caf39aec3ce14a095.zip
more test
Diffstat (limited to 'src/jsMain/kotlin/io/Path.kt')
-rw-r--r--src/jsMain/kotlin/io/Path.kt23
1 files changed, 9 insertions, 14 deletions
diff --git a/src/jsMain/kotlin/io/Path.kt b/src/jsMain/kotlin/io/Path.kt
index 20e4a90..241c559 100644
--- a/src/jsMain/kotlin/io/Path.kt
+++ b/src/jsMain/kotlin/io/Path.kt
@@ -60,21 +60,16 @@ sealed class Path {
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)
+ var idx = 0
+ while (idx < path.parts.size && idx < parts.size && path.parts[idx] == parts[idx]) {
+ idx++
}
- Relative(List(returns) { ".." } + partList)
+ val returns = if (idx < parts.size) {
+ parts.size - idx
+ } else {
+ 0
+ }
+ Relative(List(returns) { ".." } + path.parts.subList(idx, path.parts.size))
}
}
}