Skip to content

refactor: simplify local scan/branch/copy idioms in host/percent/query#49

Merged
OmarAlJarrah merged 2 commits into
mainfrom
refactor/host-percent-query-cleanups
Jul 8, 2026
Merged

refactor: simplify local scan/branch/copy idioms in host/percent/query#49
OmarAlJarrah merged 2 commits into
mainfrom
refactor/host-percent-query-cleanups

Conversation

@OmarAlJarrah

Copy link
Copy Markdown
Member

What

Four small, independent, behavior-preserving simplifications.

  • Hand-rolled scan loops → indexOfFirst. firstForbiddenDomainIndex (host/HostParser) and firstForbiddenHostIndex (host/OpaqueHost) were ~12-line index/sentinel while loops returning "first offending index, or -1". CharSequence.indexOfFirst { } is exactly that. The manual loops mostly existed to host in-bounds check() postconditions that only asserted their own iteration mechanics — vacuous over vetted stdlib.
  • Ipv4.parsePart doubled radix branch → one when. Two parallel three-way if/else chains (one picking the radix, one picking the digit substring) on the same hex/octal/decimal decision collapse into a single when. Carrying the digit substring (not a prefix length) keeps the decimal path allocation-free.
  • PercentCodec duplicated run loop → one helper. The two run appenders held a byte-identical triplet-run materialization loop; extracted to a private tripletRunBytes. The differing run-scan loops stay separate (unifying them would need a lambda — not worth it).
  • QueryParameters builder redundant copy. build() did QueryParameters(pairs.toList()), but the constructor already snapshots via toList(). build() now passes the list directly, dropping one allocation per materialization; the constructor's copy remains the immutability boundary.

Safety

Pure refactor — output unchanged (IPv4 classification verified for 0x0a / 010 / 10 / 0; forbidden-index predicates and -1 sentinel preserved). Public API unchanged (apiCheck passes with no apiDump). Verified on the JVM gate + a Kotlin/Native macosArm64 compile. Net −27 lines.


Stacked on #48.

Four small behavior-preserving cleanups:

  - Replace two hand-rolled "first offending index" scan loops
    (firstForbiddenDomainIndex in host/HostParser, firstForbiddenHostIndex in
    host/OpaqueHost) with stdlib indexOfFirst. The manual loops existed mostly
    to host in-bounds check() postconditions that only asserted their own
    iteration mechanics; those are vacuous over a vetted stdlib call.
  - Collapse Ipv4.parsePart's two parallel hex/octal/decimal if/else chains
    (one selecting the radix, one selecting the digit substring) into a single
    `when`, carrying the digit substring so the common decimal path keeps using
    the part directly with no extra allocation.
  - Extract the byte-identical triplet-run materialization loop shared by
    PercentCodec's two run appenders into a private tripletRunBytes helper; the
    differing run-scan loops and surrounding logic stay put.
  - Drop a redundant defensive copy in the QueryParameters builder's build():
    the QueryParameters constructor already snapshots via toList(), so build()
    can hand it the builder's list directly.

Pure refactor: output unchanged, public API unchanged.
@OmarAlJarrah OmarAlJarrah force-pushed the refactor/host-percent-query-cleanups branch from a90250a to 71fe19b Compare July 8, 2026 11:01
@OmarAlJarrah OmarAlJarrah changed the base branch from refactor/share-uri-url-internals to main July 8, 2026 11:01
The prior single-when form returned a (digits, radix) Pair, so every part
allocated a Pair even though the common decimal case carries the input string
unchanged. Drive both the radix and the prefix length to strip from one when
into plain vals instead: the decimal path uses offset 0 and reuses part
verbatim, allocating neither a substring nor a Pair. Replaces the bare 2/1
prefix lengths with named HEX_PREFIX_LENGTH / OCTAL_PREFIX_LENGTH constants.
@OmarAlJarrah OmarAlJarrah merged commit c3cb611 into main Jul 8, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant