Skip to content

Commit 108089f

Browse files
authored
fix: clarify operator grouping and resolve an ambiguous path-string overload (#80)
PR: #80
1 parent f125f87 commit 108089f

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

kuri/src/commonMain/kotlin/org/dexpace/kuri/host/Ipv4Rfc3986.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ internal object Ipv4Rfc3986 {
4343
/** Packs exactly four octets (high to low) into a single 32-bit [Int]. */
4444
private fun packOctets(octets: List<Int>): Int {
4545
require(octets.size == Ipv4.IPV4_OCTET_COUNT) { "expected four octets, got ${octets.size}" }
46-
return octets.fold(0) { acc, octet -> (acc shl Ipv4.BITS_PER_OCTET) or octet }
46+
return octets.fold(0) { acc, octet ->
47+
val shifted = acc shl Ipv4.BITS_PER_OCTET
48+
shifted or octet
49+
}
4750
}
4851
}

kuri/src/commonMain/kotlin/org/dexpace/kuri/idna/Punycode.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ internal object Punycode {
210210
terminated = true
211211
break
212212
}
213-
output.append(digitToCodePoint(t + (q - t) % (BASE - t)))
213+
val remainder = (q - t) % (BASE - t)
214+
output.append(digitToCodePoint(t + remainder))
214215
q = (q - t) / (BASE - t)
215216
}
216217
check(terminated) { "generalized integer did not terminate" }
@@ -297,7 +298,9 @@ internal object Punycode {
297298
scaled /= (BASE - TMIN)
298299
k += BASE
299300
}
300-
return k + ((BASE - TMIN + 1) * scaled) / (scaled + SKEW)
301+
val numerator = (BASE - TMIN + 1) * scaled
302+
val term = numerator / (scaled + SKEW)
303+
return k + term
301304
}
302305

303306
/** Clamps the digit threshold `t` for weight [k] under [bias] to `TMIN..TMAX`. */

kuri/src/commonMain/kotlin/org/dexpace/kuri/parser/ComponentPath.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private const val URI_PATH_SEPARATOR: String = "/"
9494
* selects the absolute (`/a/b`) versus rootless (`a/b`) form, so a relative reference (`a/b`) and a
9595
* scheme-rootless path (`mailto:a@b`) each round-trip unchanged.
9696
*/
97-
internal fun ComponentPath.Segments.toUriPathString(): String =
97+
internal fun ComponentPath.Segments.toSegmentsPathString(): String =
9898
when {
9999
segments.isEmpty() -> ""
100100
rooted -> URI_PATH_SEPARATOR + segments.joinToString(URI_PATH_SEPARATOR)
@@ -105,13 +105,14 @@ internal fun ComponentPath.Segments.toUriPathString(): String =
105105
* Encodes any [ComponentPath] back to its RFC 3986 §5.3 string form: an [Opaque][ComponentPath.Opaque] path
106106
* verbatim (no `/` guard, never dot-collapsed), else the [Segments][ComponentPath.Segments] join.
107107
*
108-
* The smart-cast `Segments` receiver dispatches to the more specific [toUriPathString] overload, so
109-
* this is a dispatch, not a recursion.
108+
* Named distinctly from [toSegmentsPathString] (rather than overloading the same name) so dispatch
109+
* never depends on the caller's static type: a `ComponentPath`-typed receiver always reaches this
110+
* function, which then forwards the smart-cast `Segments` case explicitly.
110111
*/
111112
internal fun ComponentPath.toUriPathString(): String =
112113
when (this) {
113114
is ComponentPath.Opaque -> path
114-
is ComponentPath.Segments -> toUriPathString()
115+
is ComponentPath.Segments -> toSegmentsPathString()
115116
}
116117

117118
/**
@@ -352,10 +353,10 @@ internal data class BuilderPath(
352353
*/
353354
fun effectivePath(hasHost: Boolean): String =
354355
when (rooting) {
355-
PathRooting.ROOTED -> ComponentPath.Segments(segments, rooted = true).toUriPathString()
356-
PathRooting.ROOTLESS -> ComponentPath.Segments(segments, rooted = false).toUriPathString()
356+
PathRooting.ROOTED -> ComponentPath.Segments(segments, rooted = true).toSegmentsPathString()
357+
PathRooting.ROOTLESS -> ComponentPath.Segments(segments, rooted = false).toSegmentsPathString()
357358
PathRooting.DEFERRED -> {
358-
val rootless = ComponentPath.Segments(segments, rooted = false).toUriPathString()
359+
val rootless = ComponentPath.Segments(segments, rooted = false).toSegmentsPathString()
359360
if (hasHost) URI_PATH_SEPARATOR + rootless else rootless
360361
}
361362
}

0 commit comments

Comments
 (0)