]> Sergey Matveev's repositories - btrtrc.git/commitdiff
iplist: Double performance of parsing line
authorMatt Joiner <anacrolix@gmail.com>
Tue, 2 Dec 2014 20:55:06 +0000 (14:55 -0600)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 2 Dec 2014 20:55:06 +0000 (14:55 -0600)
iplist/iplist.go

index 5467c33055f177011307172e413526ebbf67c10d..9fe9871efe88e2e85bce725fc4cd78a0b2dd6e7e 100644 (file)
@@ -4,7 +4,6 @@ import (
        "bytes"
        "fmt"
        "net"
-       "regexp"
        "sort"
        "strings"
 )
@@ -50,24 +49,19 @@ func (me *IPList) Lookup(ip net.IP) (r *Range) {
        return
 }
 
-var p2pBlocklistLineRe = regexp.MustCompile(`(.*):([\d.]+)-([\d.]+)`)
-
 // Parse a line of the PeerGuardian Text Lists (P2P) Format. Returns !ok but
 // no error if a line doesn't contain a range but isn't erroneous, such as
 // comment and blank lines.
 func ParseBlocklistP2PLine(l string) (r Range, ok bool, err error) {
        l = strings.TrimSpace(l)
-       if l == "" || strings.HasPrefix(l, "#") {
-               return
-       }
-       sms := p2pBlocklistLineRe.FindStringSubmatch(l)
-       if sms == nil {
-               err = fmt.Errorf("error parsing %q", l)
+       if len(l) == 0 || strings.HasPrefix(l, "#") {
                return
        }
-       r.Description = sms[1]
-       r.First = net.ParseIP(sms[2])
-       r.Last = net.ParseIP(sms[3])
+       colon := strings.IndexByte(l, ':')
+       hyphen := strings.IndexByte(l[colon+1:], '-') + colon + 1
+       r.Description = l[:colon]
+       r.First = net.ParseIP(l[colon+1 : hyphen])
+       r.Last = net.ParseIP(l[hyphen+1:])
        if r.First == nil || r.Last == nil {
                return
        }