From 8d5141dfc77fc27a766433d32c18b5b183726224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Mileusni=C4=87?= Date: Fri, 14 Apr 2023 12:12:35 +0200 Subject: [PATCH] Small performance boost --- ua.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ua.go b/ua.go index 43b375d..6df3f0c 100644 --- a/ua.go +++ b/ua.go @@ -31,15 +31,6 @@ type UserAgent struct { Bot bool } -// TODO: turn to slice, check performance -var ignore = map[string]struct{}{ - "KHTML, like Gecko": {}, - "U": {}, - "compatible": {}, - "Mozilla": {}, - "WOW64": {}, -} - // Constants for browsers and operating systems for easier comparison const ( Windows = "Windows" @@ -372,7 +363,8 @@ func parse(userAgent string) properties { addToken := func() { if buff.Len() != 0 { s := strings.TrimSpace(buff.String()) - if _, ign := ignore[s]; !ign { + if !ignore(s) { + // if _, ign := ignore[s]; !ign { if isURL { s = strings.TrimPrefix(s, "+") } @@ -468,6 +460,16 @@ func checkVer(s string) (name, v string) { // return s[:i], s[i+1:] } +// ignore retursn true if token should be ignored +func ignore(s string) bool { + switch s { + case "KHTML, like Gecko", "U", "compatible", "Mozilla", "WOW64": + return true + default: + return false + } +} + type property struct { Key string Value string