iplist: Double performance of parsing line

This commit is contained in:
Matt Joiner 2014-12-02 14:55:06 -06:00
parent 9d66a837eb
commit b1ac38a617
1 changed files with 6 additions and 12 deletions

View File

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