Files
go-utils/geoip/qqwry/utils.go
2023-10-19 15:27:32 +08:00

36 lines
887 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package qqwry
import (
"bytes"
"io"
"regexp"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
var regSpiltAddress = regexp.MustCompile(`.+?(省|市|自治区|自治州|盟|县|区|管委会|街道|镇|乡)`)
func gb18030Decode(src []byte) string {
in := bytes.NewReader(src)
out := transform.NewReader(in, simplifiedchinese.GB18030.NewDecoder())
d, _ := io.ReadAll(out)
return string(d)
}
// getMiddleOffset 取得begin和end之间的偏移量用于二分搜索
func getMiddleOffset(start uint32, end uint32) uint32 {
return start + (((end-start)/ipRecordLength)>>1)*ipRecordLength
}
func byte3ToUInt32(data []byte) uint32 {
i := uint32(data[0]) & 0xff
i |= (uint32(data[1]) << 8) & 0xff00
i |= (uint32(data[2]) << 16) & 0xff0000
return i
}
func SpiltAddress(addr string) []string {
return regSpiltAddress.FindAllString(addr, -1)
}