2
0

Refactoring

This commit is contained in:
Miloš Mileusnić
2024-09-13 02:37:02 +02:00
parent 5331aef4b5
commit af0d3cbc18
+5 -6
View File
@@ -80,7 +80,7 @@ func Parse(userAgent string) UserAgent {
String: userAgent, String: userAgent,
} }
tokens := parse(userAgent) tokens := parse([]byte(userAgent))
ua.URL = tokens.url ua.URL = tokens.url
// OS lookup // OS lookup
@@ -389,7 +389,7 @@ func Parse(userAgent string) UserAgent {
// return bytes.NewBuffer(make([]byte, 0, 30)) // return bytes.NewBuffer(make([]byte, 0, 30))
// }} // }}
func parse(userAgent string) properties { func parse(userAgent []byte) properties {
clients := properties{ clients := properties{
list: make([]property, 0, 8), list: make([]property, 0, 8),
} }
@@ -428,8 +428,7 @@ func parse(userAgent string) properties {
parOpen := false parOpen := false
braOpen := false braOpen := false
bua := []byte(userAgent) for i, c := range userAgent {
for i, c := range bua {
switch { switch {
case c == 41: // ) case c == 41: // )
addToken() addToken()
@@ -456,7 +455,7 @@ func parse(userAgent string) properties {
if bytes.HasSuffix(buff.Bytes(), []byte("http")) || bytes.HasSuffix(buff.Bytes(), []byte("https")) { if bytes.HasSuffix(buff.Bytes(), []byte("http")) || bytes.HasSuffix(buff.Bytes(), []byte("https")) {
// If we are part of a URL just write the character. // If we are part of a URL just write the character.
buff.WriteByte(c) buff.WriteByte(c)
} else if i != len(bua)-1 && bua[i+1] != ' ' { } else if i != len(userAgent)-1 && userAgent[i+1] != ' ' {
// If the following character is not a space, change to a space. // If the following character is not a space, change to a space.
buff.WriteByte(' ') buff.WriteByte(' ')
} }
@@ -469,7 +468,7 @@ func parse(userAgent string) properties {
val.WriteByte(c) val.WriteByte(c)
case c == 47 && !isURL: // / case c == 47 && !isURL: // /
if i != len(bua)-1 && bua[i+1] == 47 && (bytes.HasSuffix(buff.Bytes(), []byte("http:")) || bytes.HasSuffix(buff.Bytes(), []byte("https:"))) { if i != len(userAgent)-1 && userAgent[i+1] == 47 && (bytes.HasSuffix(buff.Bytes(), []byte("http:")) || bytes.HasSuffix(buff.Bytes(), []byte("https:"))) {
buff.WriteByte(c) buff.WriteByte(c)
isURL = true isURL = true
} else { } else {