internal

package
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Unknown Match = iota

	// There is no match token for Android Browser, but the absence of any browser token paired with Android is a good indicator of this browser.
	BrowserAndroid
	BrowserChrome
	BrowserEdge
	BrowserFirefox
	BrowserIE
	BrowserOpera
	BrowserOperaMini
	BrowserSafari
	BrowserVivaldi
	BrowserSamsung
	BrowserSilk
	BrowserFalkon
	BrowserNintendo
	BrowserYandex

	OSAndroid
	OSChromeOS
	OSIOS
	OSLinux
	OSFreeBSD
	OSOpenBSD
	OSMacOS
	OSWindows

	DeviceDesktop
	DeviceMobile
	DeviceTablet
	DeviceTV
	DeviceBot

	TokenVersion
	// We need a separate type for mobile devices since some user agents use "Mobile/"
	// appended with a device ID. We need to handle these separately to strip those IDs
	// out.
	TokenMobileDevice

	MatchUnknown MatchType = iota
	MatchBrowser
	MatchOS
	MatchDevice
	MatchVersion
)

Variables

View Source
var MatchPrecedenceMap = map[Match]uint8{

	BrowserSafari:    1,
	BrowserAndroid:   2,
	BrowserChrome:    3,
	BrowserFirefox:   4,
	BrowserIE:        5,
	BrowserOpera:     6,
	BrowserOperaMini: 7,
	BrowserEdge:      8,
	BrowserVivaldi:   9,
	BrowserSamsung:   10,
	BrowserSilk:      11,
	BrowserFalkon:    12,
	BrowserNintendo:  13,
	BrowserYandex:    14,

	OSLinux:    1,
	OSAndroid:  2,
	OSIOS:      3,
	OSFreeBSD:  4,
	OSOpenBSD:  5,
	OSChromeOS: 5,
	OSMacOS:    6,
	OSWindows:  7,

	DeviceDesktop:     1,
	DeviceMobile:      2,
	TokenMobileDevice: 3,
	DeviceTablet:      4,
	DeviceTV:          5,
	DeviceBot:         6,
}

MatchPrecedenceMap is a map of user agent types to their importance in determining what is the actual browser/device/OS being used.

For example, Chrome user agents also contain the string "Safari" at the end of the user agent. This means that if we only check for "Safari" first, we will incorrectly determine the browser to be Safari instead of Chrome.

By setting a precedence, we can determine which match is more important and use that as the final result.

Functions

func IsDigit

func IsDigit(r rune) bool

IsDigit reports whether the rune is a decimal digit.

This is an optimised version of unicode.IsDigit without the check for r <= unicode.MaxLatin1.

func IsLetter

func IsLetter(r rune) bool

IsLetter reports whether the rune is a letter in ASCII.

This is an optimised version of unicode.IsLetter without the check for r <= unicode.MaxLatin1.

func RemoveAndroidIdentifiers

func RemoveAndroidIdentifiers(ua string) string

RemoveAndroidIdentifiers removes the device identifiers from the user agent string. This specifically removes any strings that follow the Android tokens.

func RemoveMobileIdentifiers

func RemoveMobileIdentifiers(ua string) string

RemoveMobileIdentifiers removes the device identifiers from the user agent string. This specifically removes any strings that follow the Mobile tokens. For example, "Mobile/14F89" should be "Mobile".

func RemoveVersions

func RemoveVersions(ua string) string

RemoveVersions removes the version numbers from the user agent string.

func ReplaceIndexes

func ReplaceIndexes(ua string, indexes []int) string

ReplaceIndexes replaces the runes at the given indexes with empty strings.

Types

type Match added in v1.0.3

type Match uint8

Match is an enum for the browser, os or device name.

func (Match) GetMatchBrowser added in v1.1.0

func (m Match) GetMatchBrowser() agents.Browser

GetMatchBrowser returns the browser name of a match.

func (Match) GetMatchDevice added in v1.1.0

func (m Match) GetMatchDevice() agents.Device

GetMatchDevice returns the device name of a match.

func (Match) GetMatchName added in v1.1.0

func (m Match) GetMatchName() string

GetMatchName returns the name of a match. This is used for debugging in tests.

func (Match) GetMatchOS added in v1.1.0

func (m Match) GetMatchOS() agents.OS

GetMatchOS returns the OS name of a match.

func (Match) GetMatchType added in v1.1.0

func (m Match) GetMatchType() MatchType

GetMatchType returns the match type of a match result using the MatchPrecedenceMap.

type MatchResults

type MatchResults struct {
	Match     Match
	MatchType MatchType

	// Precedence value for each result type to determine which result should be overwritten.
	// Higher values overwrite lower values.
	Precedence uint8

	EndIndex int
}

MatchResults contains the information from MatchTokenIndexes.

func MatchTokenIndexes

func MatchTokenIndexes(ua string) []MatchResults

MatchTokenIndexes finds the start and end indexes of necessary tokens that match a known browser, device, or OS. This is used to determine when to insert a result value into the trie.

type MatchType added in v1.0.3

type MatchType uint8

MatchType is an enum for the match type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL