Skip to content

Commit

Permalink
remove redundant comments
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotwutingfeng committed Jul 20, 2022
1 parent 936bfaa commit 3adac21
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
33 changes: 13 additions & 20 deletions fasttld.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,10 @@ func nestedDict(dic *trie, keys []string) {

for _, key := range keysExceptLast {
dicBk = dic
// If dic.matches[key] does not exist
if _, ok := dic.matches[key]; !ok {
// Set dic.matches[key] to &trie
dic.matches[key] = &trie{hasChildren: true, matches: make(map[string]*trie)}
}
dic = dic.matches[key] // point dic to it
dic = dic.matches[key]
if len(dic.matches) == 0 && !dic.hasChildren {
end = true
dic = dicBk
Expand All @@ -120,12 +118,10 @@ func trieConstruct(includePrivateSuffix bool, cacheFilePath string) (*trie, erro
}

var suffixList []string
if !includePrivateSuffix {
// public suffixes only
suffixList = suffixLists[0]
if includePrivateSuffix {
suffixList = suffixLists.AllSuffixes
} else {
// public suffixes AND private suffixes
suffixList = suffixLists[2]
suffixList = suffixLists.PublicSuffixes
}

for _, suffix := range suffixList {
Expand Down Expand Up @@ -267,8 +263,7 @@ func (f *FastTLD) Extract(e URLParams) (ExtractResult, error) {
}
}

if closingSquareBracketIdx > 0 {
// Is IPv6 address
if urlParts.HostType == IPv6 {
return urlParts, nil
}

Expand All @@ -282,7 +277,7 @@ func (f *FastTLD) Extract(e URLParams) (ExtractResult, error) {
return urlParts, err
}

// Define the root node
// Check for TLD Suffix
node := f.TldTrie

var (
Expand All @@ -292,7 +287,6 @@ func (f *FastTLD) Extract(e URLParams) (ExtractResult, error) {
)
sepIdx, suffixStartIdx, suffixEndIdx := len(netloc), len(netloc), len(netloc)

// Check for TLD Suffix
for !end {
var label string
previousSepIdx = sepIdx
Expand Down Expand Up @@ -385,7 +379,7 @@ func (f *FastTLD) Extract(e URLParams) (ExtractResult, error) {
urlParts.RegisteredDomain = netloc[0:suffixEndIdx]
}
} else {
// If only Suffix exists
// Only Suffix exists
urlParts.Suffix = netloc[0:suffixEndIdx]
}
} else {
Expand All @@ -407,17 +401,16 @@ func (f *FastTLD) Extract(e URLParams) (ExtractResult, error) {
return urlParts, nil
}

// New creates a new *FastTLD.
// New creates a new *FastTLD using data from a Public Suffix List file.
func New(n SuffixListParams) (*FastTLD, error) {
cacheFilePath, err := filepath.Abs(n.CacheFilePath)
invalidCacheFilePath := err != nil
cacheFilePathIsInvalid := err != nil

// If cacheFilePath is unreachable, use default Public Suffix List
if stat, err := os.Stat(strings.TrimSpace(cacheFilePath)); invalidCacheFilePath || err != nil || stat.IsDir() || stat.Size() == 0 {
// If cacheFilePath is unreachable, use default Public Suffix List file
if stat, err := os.Stat(strings.TrimSpace(cacheFilePath)); cacheFilePathIsInvalid || err != nil || stat.IsDir() || stat.Size() == 0 {
n.CacheFilePath = getCurrentFilePath() + string(os.PathSeparator) + defaultPSLFileName
// Update Public Suffix List if it doesn't exist or is older than pslMaxAgeHours
// Update Public Suffix List file if it doesn't exist or is older than pslMaxAgeHours
if fileinfo, err := os.Stat(n.CacheFilePath); err != nil || fileLastModifiedHours(fileinfo) > pslMaxAgeHours {
// Create local file at n.CacheFilePath
if file, err := os.Create(n.CacheFilePath); err == nil {
if err := update(file, publicSuffixListSources); err != nil {
log.Println(err)
Expand All @@ -427,7 +420,7 @@ func New(n SuffixListParams) (*FastTLD, error) {
}
}

// Construct *trie using list located at n.CacheFilePath
// Construct *trie using Public Suffix List file located at n.CacheFilePath
tldTrie, err := trieConstruct(n.IncludePrivateSuffix, n.CacheFilePath)

return &FastTLD{cacheFilePath: n.CacheFilePath, TldTrie: tldTrie}, err
Expand Down
14 changes: 9 additions & 5 deletions psl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ var publicSuffixListSources = []string{
"https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat",
}

type suffixes struct {
PublicSuffixes []string
PrivateSuffixes []string
AllSuffixes []string
}

// getPublicSuffixList retrieves Public Suffixes and Private Suffixes from Public Suffix list located at cacheFilePath.
//
// PublicSuffixes: ICANN domains. Example: com, net, org etc.
//
// PrivateSuffixes: PRIVATE domains. Example: blogspot.co.uk, appspot.com etc.
//
// AllSuffixes: Both ICANN and PRIVATE domains.
func getPublicSuffixList(cacheFilePath string) ([3]([]string), error) {
func getPublicSuffixList(cacheFilePath string) (suffixes, error) {
PublicSuffixes := []string{}
PrivateSuffixes := []string{}
AllSuffixes := []string{}

fd, err := os.Open(cacheFilePath)
if err != nil {
log.Println(err)
return [3]([]string){PublicSuffixes, PrivateSuffixes, AllSuffixes}, err
return suffixes{PublicSuffixes, PrivateSuffixes, AllSuffixes}, err
}
defer fd.Close()

Expand Down Expand Up @@ -77,9 +83,8 @@ func getPublicSuffixList(cacheFilePath string) ([3]([]string), error) {
// add non-punycode version if it is different from punycode version
AllSuffixes = append(AllSuffixes, line)
}

}
return [3]([]string){PublicSuffixes, PrivateSuffixes, AllSuffixes}, nil
return suffixes{PublicSuffixes, PrivateSuffixes, AllSuffixes}, nil
}

// downloadFile downloads file from url as byte slice
Expand Down Expand Up @@ -148,7 +153,6 @@ func (t *FastTLD) Update() error {
if t.cacheFilePath != getCurrentFilePath()+string(os.PathSeparator)+defaultPSLFileName {
return errors.New("function Update() only applies to default Public Suffix List, not custom Public Suffix List")
}
// Create local file at cacheFilePath
file, err := os.Create(t.cacheFilePath)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion psl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestGetPublicSuffixList(t *testing.T) {
if !test.hasError && err != nil {
t.Errorf("Expected no error. Got an error.")
}
if output := reflect.DeepEqual(suffixLists,
if output := reflect.DeepEqual([3][]string{suffixLists.PublicSuffixes, suffixLists.PrivateSuffixes, suffixLists.AllSuffixes},
test.expectedLists); !output {
t.Errorf("Output %q not equal to expected %q",
suffixLists, test.expectedLists)
Expand Down
5 changes: 2 additions & 3 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
const alphabets string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const numbers string = "0123456789"

// Obtained from IETF RFC 3490
// IETF RFC 3490
const labelSeparators string = "\u002e\u3002\uff0e\uff61"

const controlChars string = "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\t\n\v\f\r\u000e\u000f" +
Expand All @@ -34,10 +34,9 @@ var endOfHostWithPortDelimitersSet asciiSet = makeASCIISet(endOfHostWithPortDeli
var endOfHostDelimitersSet asciiSet = makeASCIISet(endOfHostDelimiters)
var invalidUserInfoCharsSet asciiSet = makeASCIISet(invalidUserInfoChars)

// For extracting URL scheme.
var schemeFirstCharSet asciiSet = makeASCIISet(alphabets)
var schemeRemainingCharSet asciiSet = makeASCIISet(alphabets + numbers + "+-.")
var slashes asciiSet = makeASCIISet("/\\")
var slashes asciiSet = makeASCIISet(`/\`)

// asciiSet is a 32-byte value, where each bit represents the presence of a
// given ASCII character in the set. The 128-bits of the lower 16 bytes,
Expand Down

0 comments on commit 3adac21

Please sign in to comment.