aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/utilities/Uri.kt
blob: ef8549f7583b56571d4fea084c7648f3e97aad21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.jetbrains.dokka.utilities

import org.jetbrains.dokka.InternalDokkaApi
import java.net.URI

@InternalDokkaApi
@Deprecated("Deprecated for removal") // Unused in Dokka
fun URI.relativeTo(uri: URI): URI {
    // Normalize paths to remove . and .. segments
    val base = uri.normalize()
    val child = this.normalize()

    fun StringBuilder.appendRelativePath() {
        // Split paths into segments
        var bParts = base.path.split('/').dropLastWhile { it.isEmpty() }
        val cParts = child.path.split('/').dropLastWhile { it.isEmpty() }

        // Discard trailing segment of base path
        if (bParts.isNotEmpty() && !base.path.endsWith("/")) {
            bParts = bParts.dropLast(1)
        }

        // Compute common prefix
        val commonPartsSize = bParts.zip(cParts).takeWhile { (basePart, childPart) -> basePart == childPart }.count()
        bParts.drop(commonPartsSize).joinTo(this, separator = "") { "../" }
        cParts.drop(commonPartsSize).joinTo(this, separator = "/")
    }

    return URI.create(buildString {
        if (base.path != child.path) {
            appendRelativePath()
        }
        child.rawQuery?.let {
            append("?")
            append(it)
        }
        child.rawFragment?.let {
            append("#")
            append(it)
        }
    })
}