Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No heap allocations #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 44 additions & 29 deletions uasurfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
// strings.
package uasurfer

import "strings"
import (
"strings"
"sync"
"unsafe"
)

//go:generate stringer -type=DeviceType,BrowserName,OSName,Platform -output=const_string.go

Expand Down Expand Up @@ -213,7 +217,13 @@ func ParseUserAgent(ua string, dest *UserAgent) {
}

func parse(ua string, dest *UserAgent) {
ua = normalise(ua)
bp := bytesPool.Get().(*[]byte)
b := *bp

b = append(b[:0], ua...)
lowercaseBytes(b)
ua = b2s(b)

switch {
case len(ua) == 0:
dest.OS.Platform = PlatformUnknown
Expand All @@ -228,39 +238,44 @@ func parse(ua string, dest *UserAgent) {
dest.evalBrowserVersion(ua)
dest.evalDevice(ua)
}

*bp = b
bytesPool.Put(bp)
}

// normalise normalises the user supplied agent string so that
// we can more easily parse it.
func normalise(ua string) string {
if len(ua) <= 1024 {
var buf [1024]byte
ascii := copyLower(buf[:len(ua)], ua)
if !ascii {
// Fall back for non ascii characters
return strings.ToLower(ua)
}
return string(buf[:len(ua)])
}
// Fallback for unusually long strings
return strings.ToLower(ua)
// b2s converts a byte slice to a string without allocating.
// WARNING: changing the byte slice will change the string as well!
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

// copyLower copies a lowercase version of s to b. It assumes s contains only single byte characters
// and will panic if b is nil or is not long enough to contain all the bytes from s.
// It returns early with false if any characters were non ascii.
func copyLower(b []byte, s string) bool {
for j := 0; j < len(s); j++ {
c := s[j]
if c > 127 {
return false
}
const toLower = 'a' - 'A'

if 'A' <= c && c <= 'Z' {
c += 'a' - 'A'
var (
bytesPool = sync.Pool{
New: func() interface{} {
b := make([]byte, 0, 1024)
return &b
},
}

toLowerTable = func() [256]byte {
var a [256]byte
for i := 0; i < 256; i++ {
c := byte(i)
if c >= 'A' && c <= 'Z' {
c += toLower
}
a[i] = c
}
return a
}()
)

b[j] = c
// Lowercase all ascii characters in b.
func lowercaseBytes(b []byte) {
for i := 0; i < len(b); i++ {
p := &b[i]
*p = toLowerTable[*p]
}
return true
}