From ba5426b812cc6a5a2428c61e9f94db159dc1e706 Mon Sep 17 00:00:00 2001 From: Karl Gaissmaier Date: Wed, 28 Aug 2024 13:18:35 +0200 Subject: [PATCH] internal renaming --- conversion.go | 24 ++++++++++++------------ conversion_test.go | 13 ------------- uint128.go | 24 ++++++++++++------------ 3 files changed, 24 insertions(+), 37 deletions(-) diff --git a/conversion.go b/conversion.go index a09428d..23d2e0d 100644 --- a/conversion.go +++ b/conversion.go @@ -11,25 +11,25 @@ type addr struct { is4 bool } -func peek(a netip.Addr) (b addr) { +func peek(a netip.Addr) addr { + var b addr b.is4 = a.Is4() - raw := a.As16() - b.ip.hi = binary.BigEndian.Uint64(raw[:8]) - b.ip.lo = binary.BigEndian.Uint64(raw[8:]) - return + a16 := a.As16() + b.ip.hi = binary.BigEndian.Uint64(a16[:8]) + b.ip.lo = binary.BigEndian.Uint64(a16[8:]) + return b } func back(a addr) netip.Addr { - var a6 [16]byte - binary.BigEndian.PutUint64(a6[:8], a.ip.hi) - binary.BigEndian.PutUint64(a6[8:], a.ip.lo) + var a16 [16]byte + binary.BigEndian.PutUint64(a16[:8], a.ip.hi) + binary.BigEndian.PutUint64(a16[8:], a.ip.lo) if a.is4 { - // convert slice to array pointer - a4 := (*[4]byte)(a6[12:]) - return netip.AddrFrom4(*a4) + // slice it and convert it back to array on the fly + return netip.AddrFrom4(*(*[4]byte)(a16[12:])) } - return netip.AddrFrom16(a6) + return netip.AddrFrom16(a16) } diff --git a/conversion_test.go b/conversion_test.go index 34bc89e..f07d875 100644 --- a/conversion_test.go +++ b/conversion_test.go @@ -3,23 +3,10 @@ package extnetip import ( "net/netip" "testing" - "unsafe" ) var mustAddr = netip.MustParseAddr -// is it still safe to use unsafe to peek into the internal netip.Addr representation? -func TestSizeof(t *testing.T) { - s1 := unsafe.Sizeof(addr{}) - s2 := unsafe.Sizeof(netip.Addr{}) - - if s1 != s2 { - t.Fatalf( - "Address representations differ in size, (%v != %v), maybe internal representation for netip.Addr has changed.", - s1, s2) - } -} - func TestIdempotent(t *testing.T) { t.Parallel() v4 := mustAddr("0.0.0.0") diff --git a/uint128.go b/uint128.go index 96c66e8..92a4f8e 100644 --- a/uint128.go +++ b/uint128.go @@ -9,16 +9,16 @@ type uint128 struct { lo uint64 } -func (u uint128) and(m uint128) uint128 { - return uint128{u.hi & m.hi, u.lo & m.lo} +func (u uint128) and(v uint128) uint128 { + return uint128{u.hi & v.hi, u.lo & v.lo} } -func (u uint128) or(m uint128) uint128 { - return uint128{u.hi | m.hi, u.lo | m.lo} +func (u uint128) or(v uint128) uint128 { + return uint128{u.hi | v.hi, u.lo | v.lo} } -func (u uint128) xor(m uint128) uint128 { - return uint128{u.hi ^ m.hi, u.lo ^ m.lo} +func (u uint128) xor(v uint128) uint128 { + return uint128{u.hi ^ v.hi, u.lo ^ v.lo} } func (u uint128) not() uint128 { @@ -29,8 +29,8 @@ func mask6(n int) uint128 { return uint128{^(^uint64(0) >> n), ^uint64(0) << (128 - n)} } -func u64CommonPrefixLen(a, b uint64) int { - return bits.LeadingZeros64(a ^ b) +func u64CommonPrefixLen(u, v uint64) int { + return bits.LeadingZeros64(u ^ v) } func (u uint128) commonPrefixLen(v uint128) (n int) { @@ -41,16 +41,16 @@ func (u uint128) commonPrefixLen(v uint128) (n int) { } // prefixOK returns the common bits of two uint128 and true if they present exactly a prefix. -func (a uint128) prefixOK(b uint128) (bits int, ok bool) { - bits = a.commonPrefixLen(b) +func (u uint128) prefixOK(v uint128) (bits int, ok bool) { + bits = u.commonPrefixLen(v) if bits == 128 { return bits, true } mask := mask6(bits) // check if mask applied to first and last results in all zeros and all ones - allZero := a.xor(a.and(mask)) == uint128{} - allOnes := b.or(mask) == uint128{^uint64(0), ^uint64(0)} + allZero := u.xor(u.and(mask)) == uint128{} + allOnes := v.or(mask) == uint128{^uint64(0), ^uint64(0)} return bits, allZero && allOnes }