Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions pkg/locodedb/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"cmp"
"errors"
"slices"
"strings"
)

// ErrNotFound is returned when the record is not found in the location database.
Expand Down Expand Up @@ -52,10 +51,10 @@ func Get(locodeStr string) (Record, error) {
}

code := locodeStr[CountryCodeLen:]
n, _ := slices.BinarySearchFunc(cd.locodes, code, func(csv locodesCSV, s string) int {
return cmp.Compare(csv.code, s)
n, ok := slices.BinarySearchFunc(cd.locodes, code, func(csv locodesCSV, s string) int {
return cmp.Compare(codeFromCSV(&csv), s)
})
if n == len(cd.locodes) || strings.Compare(cd.locodes[n].code, code) != 0 {
if !ok {
return Record{}, ErrNotFound
}

Expand All @@ -69,14 +68,18 @@ func Get(locodeStr string) (Record, error) {
}, nil
}

func codeFromCSV(c *locodesCSV) string {
return locodeStrings[c.offset : c.offset+LocationCodeLen]
}

func locFromCSV(c *locodesCSV) string {
return locodeStrings[c.offset : c.offset+uint32(c.locationLen)]
return locodeStrings[c.offset+LocationCodeLen : c.offset+LocationCodeLen+uint32(c.locationLen)]
}

func divCodeFromCSV(c *locodesCSV) string {
return locodeStrings[c.offset+uint32(c.locationLen) : c.offset+uint32(c.locationLen)+uint32(c.subDivCodeLen)]
return locodeStrings[c.offset+LocationCodeLen+uint32(c.locationLen) : c.offset+LocationCodeLen+uint32(c.locationLen)+uint32(c.subDivCodeLen)]
}

func divNameFromCSV(c *locodesCSV) string {
return locodeStrings[c.offset+uint32(c.locationLen)+uint32(c.subDivCodeLen) : c.offset+uint32(c.locationLen)+uint32(c.subDivCodeLen)+uint32(c.subDivNameLen)]
return locodeStrings[c.offset+LocationCodeLen+uint32(c.locationLen)+uint32(c.subDivCodeLen) : c.offset+LocationCodeLen+uint32(c.locationLen)+uint32(c.subDivCodeLen)+uint32(c.subDivNameLen)]
}
3 changes: 1 addition & 2 deletions pkg/locodedb/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type countryData struct {
type locodesCSV struct {
point Point
offset uint32
code string
locationLen uint8
subDivCodeLen uint8
subDivNameLen uint8
Expand Down Expand Up @@ -106,6 +105,7 @@ func unpackLocodesData(data []byte, mc map[countryCode]countryData) (string, err
subDivNameLen = uint8(len(record[4]))
)

b.WriteString(record[0][CountryCodeLen:])
b.WriteString(record[1])
b.WriteString(record[3])
b.WriteString(record[4])
Expand All @@ -132,7 +132,6 @@ func unpackLocodesData(data []byte, mc map[countryCode]countryData) (string, err
}
rec.locodes = append(rec.locodes, locodesCSV{
point: Point{Latitude: float32(lat), Longitude: float32(lng)},
code: record[0][CountryCodeLen:],
offset: recOffset,
locationLen: locationLen,
subDivCodeLen: subDivCodeLen,
Expand Down
Loading