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:
parent
c7ca05f2fb
commit
95a473a22d
15
main.go
15
main.go
@ -9,6 +9,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type lines []string
|
type lines []string
|
||||||
@ -16,7 +17,19 @@ type lines []string
|
|||||||
func (l lines) Len() int { return len(l) }
|
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) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
||||||
func (l lines) Less(i, j int) bool {
|
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
|
return bytes.Compare(a, b) < 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user