Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mileusna committed Sep 13, 2024
1 parent 5331aef commit af0d3cb
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions ua.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func Parse(userAgent string) UserAgent {
String: userAgent,
}

tokens := parse(userAgent)
tokens := parse([]byte(userAgent))
ua.URL = tokens.url

// OS lookup
Expand Down Expand Up @@ -389,7 +389,7 @@ func Parse(userAgent string) UserAgent {
// return bytes.NewBuffer(make([]byte, 0, 30))
// }}

func parse(userAgent string) properties {
func parse(userAgent []byte) properties {
clients := properties{
list: make([]property, 0, 8),
}
Expand Down Expand Up @@ -428,8 +428,7 @@ func parse(userAgent string) properties {
parOpen := false
braOpen := false

bua := []byte(userAgent)
for i, c := range bua {
for i, c := range userAgent {
switch {
case c == 41: // )
addToken()
Expand All @@ -456,7 +455,7 @@ func parse(userAgent string) properties {
if bytes.HasSuffix(buff.Bytes(), []byte("http")) || bytes.HasSuffix(buff.Bytes(), []byte("https")) {
// If we are part of a URL just write the character.
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.
buff.WriteByte(' ')
}
Expand All @@ -469,7 +468,7 @@ func parse(userAgent string) properties {
val.WriteByte(c)

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)
isURL = true
} else {
Expand Down

0 comments on commit af0d3cb

Please sign in to comment.