refactor: simplify local scan/branch/copy idioms in host/percent/query#49
Merged
Merged
Conversation
1b431d8 to
fc6fd17
Compare
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.
a90250a to
71fe19b
Compare
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Four small, independent, behavior-preserving simplifications.
indexOfFirst.firstForbiddenDomainIndex(host/HostParser) andfirstForbiddenHostIndex(host/OpaqueHost) were ~12-line index/sentinelwhileloops returning "first offending index, or -1".CharSequence.indexOfFirst { }is exactly that. The manual loops mostly existed to host in-boundscheck()postconditions that only asserted their own iteration mechanics — vacuous over vetted stdlib.Ipv4.parsePartdoubled radix branch → onewhen. Two parallel three-wayif/elsechains (one picking the radix, one picking the digit substring) on the same hex/octal/decimal decision collapse into a singlewhen. Carrying the digit substring (not a prefix length) keeps the decimal path allocation-free.PercentCodecduplicated run loop → one helper. The two run appenders held a byte-identical triplet-run materialization loop; extracted to a privatetripletRunBytes. The differing run-scan loops stay separate (unifying them would need a lambda — not worth it).QueryParametersbuilder redundant copy.build()didQueryParameters(pairs.toList()), but the constructor already snapshots viatoList().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-1sentinel preserved). Public API unchanged (apiCheckpasses with noapiDump). Verified on the JVM gate + a Kotlin/NativemacosArm64compile. Net −27 lines.Stacked on #48.