Skip to content

Commit

Permalink
Restore Go 1.18 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
flwyd committed Jan 28, 2024
1 parent ca271b9 commit 40adb81
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion adif/cabrillo.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (o *CabrilloIO) Write(l *Logfile, out io.Writer) error {
}
headers := make(map[string]string)
for _, f := range l.Header.Fields() {
if h, ok := strings.CutPrefix(f.Name, "APP_CABRILLO_"); ok {
if h, ok := cutPrefix(f.Name, "APP_CABRILLO_"); ok {
headers[strings.Replace(h, "_", "-", -1)] = f.Value
}
}
Expand Down
24 changes: 23 additions & 1 deletion adif/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package adif

import (
"fmt"
"strings"
)

Expand Down Expand Up @@ -74,5 +75,26 @@ func fieldValues(l *Logfile, fname string) map[string]int {
}

func isAllDigits(s string) bool {
return !strings.ContainsFunc(s, func(r rune) bool { return r < '0' || r > '9' })
// TODO when upgrading to Go 1.20+, use
// return !strings.ContainsFunc(s, func(r rune) bool { return r < '0' || r > '9' })
for _, r := range s {
if r < '0' || r > '9' {
return false
}
}
return true
}

// cutPrefix is a polyfill for strings.CutPrefix which was introduced in Go 1.20.
func cutPrefix(s, prefix string) (after string, found bool) {
// TODO when upgrading to Go 1.20+ remove this polyfill
if strings.HasPrefix(s, prefix) {
var before string
before, after, found = strings.Cut(s, prefix)
if !found || before != "" {
panic(fmt.Sprintf("strings.Cut(%q, %q) got %q, %q, %v", s, prefix, before, after, found))
}
return
}
return s, false
}

0 comments on commit 40adb81

Please sign in to comment.