Skip to content

Commit

Permalink
internal renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Aug 28, 2024
1 parent 16b2bfe commit ba5426b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 37 deletions.
24 changes: 12 additions & 12 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 0 additions & 13 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
24 changes: 12 additions & 12 deletions uint128.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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
}

0 comments on commit ba5426b

Please sign in to comment.