• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download

<lambda>null1 package org.jetbrains.dokka
2 
3 import java.net.URI
4 
5 
6 fun URI.relativeTo(uri: URI): URI {
7     // Normalize paths to remove . and .. segments
8     val base = uri.normalize()
9     val child = this.normalize()
10 
11     fun StringBuilder.appendRelativePath() {
12         // Split paths into segments
13         var bParts = base.path.split('/').dropLastWhile { it.isEmpty() }
14         val cParts = child.path.split('/').dropLastWhile { it.isEmpty() }
15 
16         // Discard trailing segment of base path
17         if (bParts.isNotEmpty() && !base.path.endsWith("/")) {
18             bParts = bParts.dropLast(1)
19         }
20 
21         // Compute common prefix
22         val commonPartsSize = bParts.zip(cParts).takeWhile { (basePart, childPart) -> basePart == childPart }.count()
23         bParts.drop(commonPartsSize).joinTo(this, separator = "") { "../" }
24         cParts.drop(commonPartsSize).joinTo(this, separator = "/")
25     }
26 
27     return URI.create(buildString {
28         if (base.path != child.path) {
29             appendRelativePath()
30         }
31         child.rawQuery?.let {
32             append("?")
33             append(it)
34         }
35         child.rawFragment?.let {
36             append("#")
37             append(it)
38         }
39     })
40 }