diff --git a/pkg/locodedb/calls.go b/pkg/locodedb/calls.go index 2a0436e..7c5c392 100644 --- a/pkg/locodedb/calls.go +++ b/pkg/locodedb/calls.go @@ -4,7 +4,6 @@ import ( "cmp" "errors" "slices" - "strings" ) // ErrNotFound is returned when the record is not found in the location database. @@ -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 } @@ -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)] } diff --git a/pkg/locodedb/utils.go b/pkg/locodedb/utils.go index 393a5f3..5d8fd5a 100644 --- a/pkg/locodedb/utils.go +++ b/pkg/locodedb/utils.go @@ -45,7 +45,6 @@ type countryData struct { type locodesCSV struct { point Point offset uint32 - code string locationLen uint8 subDivCodeLen uint8 subDivNameLen uint8 @@ -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]) @@ -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,