From 22d5185575ebf2d43dbc83aa449f26f1645e146f Mon Sep 17 00:00:00 2001 From: Emil Guliyev Date: Tue, 14 Aug 2018 01:51:29 -0700 Subject: [PATCH] Optimize Decompose --- publicsuffix/publicsuffix.go | 79 +++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/publicsuffix/publicsuffix.go b/publicsuffix/publicsuffix.go index bcc47ebf..e4192950 100644 --- a/publicsuffix/publicsuffix.go +++ b/publicsuffix/publicsuffix.go @@ -11,7 +11,6 @@ import ( "io" "net/http/cookiejar" "os" - "regexp" "strings" "golang.org/x/net/idna" @@ -208,12 +207,12 @@ func (l *List) Find(name string, options *FindOptions) *Rule { } i := strings.IndexRune(name, '.') if i < 0 { - break + return options.DefaultRule } name = name[i+1:] } - return options.DefaultRule + return nil } // NewRule parses the rule content, creates and returns a Rule. @@ -291,36 +290,51 @@ func (r *Rule) Match(name string) bool { // Decompose takes a name as input and decomposes it into a tuple of , // according to the rule definition and type. -func (r *Rule) Decompose(name string) [2]string { - var parts []string - +func (r *Rule) Decompose(name string) (result [2]string) { + result = [2]string{"", ""} switch r.Type { + case NormalType: + name = strings.TrimSuffix(name, r.Value) + if len(name) == 0 { + return + } + result[0] = name[:len(name)-1] + result[1] = r.Value case WildcardType: - parts = append([]string{`.*?`}, r.parts()...) - default: - parts = r.parts() - } - - suffix := strings.Join(parts, `\.`) - re := regexp.MustCompile(fmt.Sprintf(`^(.+)\.(%s)$`, suffix)) - - matches := re.FindStringSubmatch(name) - if len(matches) < 3 { - return [2]string{"", ""} - } - - return [2]string{matches[1], matches[2]} -} - -func (r *Rule) parts() []string { - labels := Labels(r.Value) - if r.Type == ExceptionType { - return labels[1:] - } - if r.Type == WildcardType && r.Value == "" { - return []string{} + if r == DefaultRule { + i := strings.LastIndexByte(name, byte('.')) + if i < 0 { + return + } + result[0] = name[:i] + result[1] = name[i+1:] + return + } + name := strings.TrimSuffix(name, r.Value) + if len(name) == 0 { + return + } + name = name[:len(name)-1] + i := strings.LastIndexByte(name, byte('.')) + if i < 0 { + return + } + result[0] = name[:i] + result[1] = name[i+1:] + "." + r.Value + case ExceptionType: + i := strings.IndexRune(r.Value, '.') + if i < 0 { + return + } + suffix := r.Value[i+1:] + name = strings.TrimSuffix(name, suffix) + if len(name) == 0 { + return + } + result[0] = name[:len(name)-1] + result[1] = suffix } - return labels + return } // Labels decomposes given domain name into labels, @@ -414,7 +428,6 @@ func DomainFromListWithOptions(l *List, name string, options *FindOptions) (stri if err != nil { return "", err } - return dn.SLD + "." + dn.TLD, nil } @@ -453,10 +466,10 @@ func normalize(name string) (string, error) { ret := strings.ToLower(name) if ret == "" { - return "", fmt.Errorf("Name is blank") + return "", fmt.Errorf("name is blank") } if ret[0] == '.' { - return "", fmt.Errorf("Name %s starts with a dot", ret) + return "", fmt.Errorf("name %s starts with a dot", ret) } return ret, nil