1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00

添加掩码格式

This commit is contained in:
2022-07-28 14:27:23 +08:00
parent 52fabedd66
commit 135b3a983b
4 changed files with 110 additions and 1 deletions

34
ip_range/mask_bits.go Normal file
View File

@ -0,0 +1,34 @@
package iprange
import "strings"
var maskBits = map[string]int{
"255": 8,
"254": 7,
"252": 6,
"248": 5,
"240": 4,
"224": 3,
"192": 2,
"128": 1,
"0": 0,
}
func MaskToBits(mask string) int {
bits := 0
secs := strings.Split(mask, ".")
if len(secs) != 4 {
panic("the mask is incorrect")
}
for _, s := range secs {
if v, ok := maskBits[s]; ok {
bits += v
} else {
panic("the mask is incorrect")
}
}
return bits
}