44 "cmp"
55 "errors"
66 "slices"
7- "strings"
87)
98
109// ErrNotFound is returned when the record is not found in the location database.
@@ -52,10 +51,10 @@ func Get(locodeStr string) (Record, error) {
5251 }
5352
5453 code := locodeStr [CountryCodeLen :]
55- n , _ := slices .BinarySearchFunc (cd .locodes , code , func (csv locodesCSV , s string ) int {
56- return cmp .Compare (csv . code , s )
54+ n , ok := slices .BinarySearchFunc (cd .locodes , code , func (csv locodesCSV , s string ) int {
55+ return cmp .Compare (codeFromCSV ( & csv ) , s )
5756 })
58- if n == len ( cd . locodes ) || strings . Compare ( cd . locodes [ n ]. code , code ) != 0 {
57+ if ! ok {
5958 return Record {}, ErrNotFound
6059 }
6160
@@ -69,14 +68,18 @@ func Get(locodeStr string) (Record, error) {
6968 }, nil
7069}
7170
71+ func codeFromCSV (c * locodesCSV ) string {
72+ return locodeStrings [c .offset : c .offset + LocationCodeLen ]
73+ }
74+
7275func locFromCSV (c * locodesCSV ) string {
73- return locodeStrings [c .offset : c .offset + uint32 (c .locationLen )]
76+ return locodeStrings [c .offset + LocationCodeLen : c .offset + LocationCodeLen + uint32 (c .locationLen )]
7477}
7578
7679func divCodeFromCSV (c * locodesCSV ) string {
77- return locodeStrings [c .offset + uint32 (c .locationLen ) : c .offset + uint32 (c .locationLen )+ uint32 (c .subDivCodeLen )]
80+ return locodeStrings [c .offset + LocationCodeLen + uint32 (c .locationLen ) : c .offset + LocationCodeLen + uint32 (c .locationLen )+ uint32 (c .subDivCodeLen )]
7881}
7982
8083func divNameFromCSV (c * locodesCSV ) string {
81- return locodeStrings [c .offset + uint32 (c .locationLen )+ uint32 (c .subDivCodeLen ) : c .offset + uint32 (c .locationLen )+ uint32 (c .subDivCodeLen )+ uint32 (c .subDivNameLen )]
84+ return locodeStrings [c .offset + LocationCodeLen + uint32 (c .locationLen )+ uint32 (c .subDivCodeLen ) : c .offset + LocationCodeLen + uint32 (c .locationLen )+ uint32 (c .subDivCodeLen )+ uint32 (c .subDivNameLen )]
8285}
0 commit comments