1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 23:47:07 -05:00

Leverage math bitx

This commit is contained in:
Darien Raymond 2017-08-29 11:31:16 +02:00
parent 97b03ac87f
commit c33d7f0e21

View File

@ -1,13 +1,10 @@
package net package net
import ( import (
"math/bits"
"net" "net"
) )
var (
onesCount = make(map[byte]byte)
)
type IPNet struct { type IPNet struct {
cache map[uint32]byte cache map[uint32]byte
} }
@ -30,7 +27,7 @@ func ipToUint32(ip net.IP) uint32 {
func ipMaskToByte(mask net.IPMask) byte { func ipMaskToByte(mask net.IPMask) byte {
value := byte(0) value := byte(0)
for _, b := range []byte(mask) { for _, b := range []byte(mask) {
value += onesCount[b] value += byte(bits.OnesCount8(b))
} }
return value return value
} }
@ -83,11 +80,3 @@ func (n *IPNet) Contains(ip net.IP) bool {
func (n *IPNet) IsEmpty() bool { func (n *IPNet) IsEmpty() bool {
return len(n.cache) == 0 return len(n.cache) == 0
} }
func init() {
value := byte(0)
for mask := byte(1); mask <= 8; mask++ {
value += 1 << byte(8-mask)
onesCount[value] = mask
}
}