Skip to content

Commit

Permalink
Remove unnecessary export. Clarify comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
krashanoff committed Jul 3, 2020
1 parent 9b59100 commit 8a940bd
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,14 @@ type HostnameTrie struct {
terminal bool // end of a valid match
}

// describes a valid hostname pattern to block by. Global var to avoid
// compilation penalty each call to ValidHostname.
var validHostnamePattern *regexp.Regexp = regexp.MustCompile("^\\*?(\\pL|[0-9\\.])*")

// ValidHostname returns whether the provided hostname pattern
// has an optional wildcard at the start, and is composed entirely
// of letters, numbers, or '.'s.
func ValidHostname(s string) error {
if len(validHostnamePattern.FindString(s)) != len(s) {
// Regex description of hostname pattern to enforce blocks by. Global var
// to avoid compilation penalty at runtime.
// Matches against strings composed entirely of letters, numbers, or '.'s
// with an optional wildcard at the start.
var legalHostnamePattern *regexp.Regexp = regexp.MustCompile("^\\*?(\\pL|[0-9\\.])*")

func legalHostname(s string) error {
if len(legalHostnamePattern.FindString(s)) != len(s) {
return fmt.Errorf("invalid hostname pattern %s", s)
}
return nil
Expand Down Expand Up @@ -238,13 +237,14 @@ func (t *HostnameTrie) UnmarshalText(b []byte) error {
return nil
}

// Insert a string into the given HostnameTrie.
// Insert a hostname pattern into the given HostnameTrie. Returns an error
// if hostname pattern is illegal.
func (t *HostnameTrie) Insert(s string) error {
if len(s) == 0 {
return nil
}

if err := ValidHostname(s); err != nil {
if err := legalHostname(s); err != nil {
return err
}

Expand Down Expand Up @@ -272,6 +272,7 @@ func (t *HostnameTrie) Contains(s string) (bool, string) {
return false, ""
}

// recursively traverse HostnameTrie children searching for a match.
func (t *HostnameTrie) childContains(s string, match string) (bool, string) {
if len(s) == 0 {
return false, ""
Expand Down

0 comments on commit 8a940bd

Please sign in to comment.