Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
Precompile regular expressions
Browse files Browse the repository at this point in the history
Fixes #20

Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
  • Loading branch information
mssola committed Nov 7, 2015
1 parent a163d6a commit 783ec61
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
)

var botFromSiteRegexp = regexp.MustCompile("http://.+\\.\\w+")

// Get the name of the bot from the website that may be in the given comment. If
// there is no website in the comment, then an empty string is returned.
func getFromSite(comment []string) string {
Expand All @@ -23,8 +25,7 @@ func getFromSite(comment []string) string {
}

// Pick the site.
re := regexp.MustCompile("http://.+\\.\\w+")
results := re.FindStringSubmatch(comment[idx])
results := botFromSiteRegexp.FindStringSubmatch(comment[idx])
if len(results) == 1 {
// If it's a simple comment, just return the name of the site.
if idx == 0 {
Expand Down
5 changes: 3 additions & 2 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
)

var ie11Regexp = regexp.MustCompile("^rv:(.+)$")

// A struct containing all the information that we might be
// interested from the browser.
type Browser struct {
Expand Down Expand Up @@ -75,9 +77,8 @@ func (p *UserAgent) detectBrowser(sections []section) {
// This is the new user agent from Internet Explorer 11.
p.browser.Engine = "Trident"
p.browser.Name = "Internet Explorer"
reg, _ := regexp.Compile("^rv:(.+)$")
for _, c := range sections[0].comment {
version := reg.FindStringSubmatch(c)
version := ie11Regexp.FindStringSubmatch(c)
if len(version) > 0 {
p.browser.Version = version[1]
return
Expand Down

0 comments on commit 783ec61

Please sign in to comment.