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

Commit 783ec61

Browse files
committed
Precompile regular expressions
Fixes #20 Signed-off-by: Miquel Sabaté Solà <[email protected]>
1 parent a163d6a commit 783ec61

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

bot.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
)
1111

12+
var botFromSiteRegexp = regexp.MustCompile("http://.+\\.\\w+")
13+
1214
// Get the name of the bot from the website that may be in the given comment. If
1315
// there is no website in the comment, then an empty string is returned.
1416
func getFromSite(comment []string) string {
@@ -23,8 +25,7 @@ func getFromSite(comment []string) string {
2325
}
2426

2527
// Pick the site.
26-
re := regexp.MustCompile("http://.+\\.\\w+")
27-
results := re.FindStringSubmatch(comment[idx])
28+
results := botFromSiteRegexp.FindStringSubmatch(comment[idx])
2829
if len(results) == 1 {
2930
// If it's a simple comment, just return the name of the site.
3031
if idx == 0 {

browser.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
)
1111

12+
var ie11Regexp = regexp.MustCompile("^rv:(.+)$")
13+
1214
// A struct containing all the information that we might be
1315
// interested from the browser.
1416
type Browser struct {
@@ -75,9 +77,8 @@ func (p *UserAgent) detectBrowser(sections []section) {
7577
// This is the new user agent from Internet Explorer 11.
7678
p.browser.Engine = "Trident"
7779
p.browser.Name = "Internet Explorer"
78-
reg, _ := regexp.Compile("^rv:(.+)$")
7980
for _, c := range sections[0].comment {
80-
version := reg.FindStringSubmatch(c)
81+
version := ie11Regexp.FindStringSubmatch(c)
8182
if len(version) > 0 {
8283
p.browser.Version = version[1]
8384
return

0 commit comments

Comments
 (0)