From 783ec61292aee3fc2f442ce740aa491e4849b794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= Date: Sat, 7 Nov 2015 20:03:14 +0100 Subject: [PATCH] Precompile regular expressions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #20 Signed-off-by: Miquel Sabaté Solà --- bot.go | 5 +++-- browser.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/bot.go b/bot.go index cc993d8..df0b6cc 100644 --- a/bot.go +++ b/bot.go @@ -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 { @@ -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 { diff --git a/browser.go b/browser.go index c5612db..a45c8ab 100644 --- a/browser.go +++ b/browser.go @@ -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 { @@ -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