mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2025-12-08 19:25:22 +00:00
make the region filtering an util func
This commit is contained in:
parent
cdd2a2fc85
commit
980c9cfdd4
@ -58,7 +58,6 @@ import (
|
||||
"log/slog"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -156,28 +155,6 @@ func (m *Maker) initDbHeader() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Maker) getFilteredRegion(region string) (string, error) {
|
||||
if len(m.fields) == 0 {
|
||||
return region, nil
|
||||
}
|
||||
|
||||
fs := strings.Split(region, "|")
|
||||
var sb []string
|
||||
for _, idx := range m.fields {
|
||||
if idx < 0 {
|
||||
return "", fmt.Errorf("negative filter index %d", idx)
|
||||
}
|
||||
|
||||
if idx >= len(fs) {
|
||||
return "", fmt.Errorf("field index %d exceeded the max length of %d", idx, len(fs))
|
||||
}
|
||||
|
||||
sb = append(sb, fs[idx])
|
||||
}
|
||||
|
||||
return strings.Join(sb, "|"), nil
|
||||
}
|
||||
|
||||
func (m *Maker) loadSegments() error {
|
||||
slog.Info("try to load the segments ... ")
|
||||
var last *Segment = nil
|
||||
@ -197,7 +174,7 @@ func (m *Maker) loadSegments() error {
|
||||
}
|
||||
|
||||
// apply the field filter
|
||||
region, err := m.getFilteredRegion(seg.Region)
|
||||
region, err := RegionFiltering(seg.Region, m.fields)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -199,3 +199,25 @@ func CheckSegments(segList []*Segment) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RegionFiltering(region string, fields []int) (string, error) {
|
||||
if len(fields) == 0 {
|
||||
return region, nil
|
||||
}
|
||||
|
||||
fs := strings.Split(region, "|")
|
||||
var sb []string
|
||||
for _, idx := range fields {
|
||||
if idx < 0 {
|
||||
return "", fmt.Errorf("negative filter index %d", idx)
|
||||
}
|
||||
|
||||
if idx >= len(fs) {
|
||||
return "", fmt.Errorf("field index %d exceeded the max length of %d", idx, len(fs))
|
||||
}
|
||||
|
||||
sb = append(sb, fs[idx])
|
||||
}
|
||||
|
||||
return strings.Join(sb, "|"), nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user