util.go 接口优化

This commit is contained in:
linyufeng 2022-07-06 20:07:44 +08:00
parent 9e32937ca1
commit a4d04e3b79
1 changed files with 4 additions and 10 deletions

View File

@ -5,7 +5,6 @@
package xdb package xdb
import ( import (
"encoding/binary"
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
@ -19,7 +18,7 @@ func CheckIP(ip string) (uint32, error) {
return 0, fmt.Errorf("invalid ip address `%s`", ip) return 0, fmt.Errorf("invalid ip address `%s`", ip)
} }
var buff = make([]byte, 4) var val uint32
for i, s := range ps { for i, s := range ps {
d, err := strconv.Atoi(s) d, err := strconv.Atoi(s)
if err != nil { if err != nil {
@ -30,21 +29,16 @@ func CheckIP(ip string) (uint32, error) {
return 0, fmt.Errorf("the %dth part `%s` should be an integer bettween 0 and 255", i, s) return 0, fmt.Errorf("the %dth part `%s` should be an integer bettween 0 and 255", i, s)
} }
buff[i] = byte(d) val |= uint32(d) << ((3 - i) * 8)
} }
// convert the ip to integer // convert the ip to integer
return binary.BigEndian.Uint32(buff), nil return val, nil
} }
func Long2IP(ip uint32) string { func Long2IP(ip uint32) string {
var buff = make([]string, 4) return fmt.Sprintf("%d.%d.%d.%d", (ip>>24)&0xFF, (ip>>16)&0xFF, (ip>>8)&0xFF, (ip>>0)&0xFF)
buff[0] = fmt.Sprintf("%d", (ip>>24)&0xFF)
buff[1] = fmt.Sprintf("%d", (ip>>16)&0xFF)
buff[2] = fmt.Sprintf("%d", (ip>>8)&0xFF)
buff[3] = fmt.Sprintf("%d", (ip>>0)&0xFF)
return strings.Join(buff, ".")
} }
func MidIP(sip uint32, eip uint32) uint32 { func MidIP(sip uint32, eip uint32) uint32 {