find space-delimited ip address anywhere in line

I'd prefer this were more robust and perhaps used a regexp.
This commit is contained in:
Stephen McQuay 2018-04-03 09:42:10 -07:00
parent c7ca05f2fb
commit 95a473a22d
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5

15
main.go
View File

@ -9,6 +9,7 @@ import (
"net"
"os"
"sort"
"strings"
)
type lines []string
@ -16,7 +17,19 @@ type lines []string
func (l lines) Len() int { return len(l) }
func (l lines) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l lines) Less(i, j int) bool {
a, b := net.ParseIP(l[i]), net.ParseIP(l[j])
var a, b net.IP
for _, s := range strings.Fields(l[i]) {
a = net.ParseIP(s)
if a != nil {
break
}
}
for _, s := range strings.Fields(l[j]) {
b = net.ParseIP(s)
if b != nil {
break
}
}
return bytes.Compare(a, b) < 0
}