Repository navigation
fix: keep the address intact in the 0-optimized IP format - #898
Merged
Mzack9999 merged 2 commits intoOct 23, 2026
Merged
Conversation
The -if 2 format removed every "0." substring and any trailing zero octet, so it changed the address: 10.0.0.1 became 11 (0.0.0.11), 192.168.10.1 became 192.168.11 (192.168.0.11), and 127.0.1.0 became 127.1 (127.0.0.1). Only omit the zero octets that precede the last octet, which is the only case where the shortened form resolves to the same address.
Mzack9999
approved these changes
Oct 23, 2026
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.
Summary
The
-if 2(0-optimized dotted-decimal) format removes every0.substring and any trailing zero octet. That rewrites the address instead of shortening it:10.0.0.1110.0.0.1110.1192.168.10.1192.168.11192.168.0.11192.168.10.11.2.3.01.2.31.2.0.31.2.3.0127.0.1.0127.1127.0.0.1127.0.1.0127.0.0.1127.1127.0.0.1127.1(unchanged)In the shortened
a.b.canda.dforms, the last part fills all remaining bytes, so the only zero octets that can be dropped are the ones directly before the last octet (a.0.0.dtoa.d,a.b.0.dtoa.b.d). The fix builds the short form from the octets under that rule and returns the address unchanged when no octet can be dropped. IPv6 input keeps its current output.The README example for
127.0.1.0showed127.1; it now shows the address unchanged.Testing
TestIpEncodingsfor10.0.0.1,192.168.0.10,192.168.10.1,1.2.3.0, and127.0.1.0.go test ./...passes. The new cases fail without the change inip.go.Fixes #897
Summary by CodeRabbit
Enhancements
Documentation
Tests