urlvector

package module
v1.1.11 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 8 Imported by: 2

README

URL vector

URL parser based on Vector API with minimum memory consumption. This project is a part of policy to reducing memory consumption and amount of pointers.

Similar to JSON vector this package doesn't make a copy of the parsed URL. It just stores an array with indexes of each part of the url (schema, hostname, path, ...) and doesn't contain any pointer.

Usage

src := "http://x.com/x/y/z?arr[]=1&arr[]=2&arr[]=3&b=x&arr1[]=a&arr1[]=b&arr1[]=c"
vec := urlvector.Acquire()
defer urlvector.Release(vec)
_ = vec.ParseString(src)
fmt.Println("host", vec.HostString())
fmt.Println("path", vec.PathString())
fmt.Println("query:")
vec.Query().Get("arr[]").Each(func(_ int, node *vector.Node){
    fmt.Println("arr[] ->", node.String())
})
fmt.Println("b ->", vec.Query().Get("b"))
vec.Query().Get("arr1[]").Each(func(_ int, node *vector.Node){
    fmt.Println("arr1[] ->", node.String())
})

Output:

host x.com
path /x/y/z
query:
arr[] -> 1
arr[] -> 2
arr[] -> 3
b -> x
arr1[] -> a
arr1[] -> b
arr1[] -> c

Performance

See versus project for performance comparison between urlvector and net/url packages:

BenchmarkNetUrl_ParseUrl-8        	 1027659	      1139 ns/op	     192 B/op	       2 allocs/op
BenchmarkNetUrl_ParseQuery-8      	  153732	      7690 ns/op	    2919 B/op	      22 allocs/op
BenchmarkNetUrl_ModHost-8         	  704062	      1799 ns/op	     840 B/op	       7 allocs/op
BenchmarkNetUrl_ModQuery-8        	   86022	     14279 ns/op	    5649 B/op	      42 allocs/op
BenchmarkUrlvector_ParseUrl-8     	 1000000	      1107 ns/op	       0 B/op	       0 allocs/op
BenchmarkUrlvector_ParseQuery-8   	  337450	      3489 ns/op	       0 B/op	       0 allocs/op
BenchmarkUrlvector_ModHost-8      	  796296	      1317 ns/op	       0 B/op	       0 allocs/op
BenchmarkUrlvector_ModQuery-8     	  290557	      4110 ns/op	       0 B/op	       0 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PathEscape

func PathEscape(dst, p []byte) []byte

PathEscape escapes the string so it can be safely placed inside a URL query.

func PathUnescape

func PathUnescape(dst, p []byte) []byte

PathUnescape does the inverse transformation of PathEscape.

func QueryEscape

func QueryEscape(dst, p []byte) []byte

QueryEscape escapes the string so it can be safely placed inside a URL query.

func QueryUnescape

func QueryUnescape(dst, p []byte) []byte

QueryUnescape does the inverse transformation of QueryEscape.

func Release

func Release(vec *Vector)

Release puts vector back to default pool instance.

func ReleaseNC added in v1.1.7

func ReleaseNC(vec *Vector)

ReleaseNC puts vector back to pool with enforced no-clear flag.

Types

type Helper

type Helper struct{}

func (Helper) Beautify

func (h Helper) Beautify(_ io.Writer, _ *vector.Node) error

func (Helper) Indirect

func (h Helper) Indirect(p *vector.Byteptr) []byte

func (Helper) Marshal added in v1.1.6

func (h Helper) Marshal(w io.Writer, node *vector.Node) error

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool represents URL vectors pool.

func (*Pool) Get

func (p *Pool) Get() *Vector

Get old vector from the pool or create new one.

func (*Pool) Put

func (p *Pool) Put(vec *Vector)

Put vector back to the pool.

type Vector

type Vector struct {
	vector.Vector
}

Vector represents URL parser.

func Acquire

func Acquire() *Vector

Acquire returns vector from default pool instance.

func NewVector

func NewVector() *Vector

NewVector makes new parser.

func (*Vector) Auth

func (vec *Vector) Auth() *vector.Node

Auth returns auth node (contains both username and password substrings).

func (*Vector) AuthBytes

func (vec *Vector) AuthBytes() []byte

AuthBytes returns auth (username:password) as bytes.

func (*Vector) AuthString

func (vec *Vector) AuthString() string

AuthString returns auth (username:password as string.

func (*Vector) Bytes

func (vec *Vector) Bytes() []byte

Bytes reassembles the vector into a valid URL bytes array.

func (*Vector) BytesEscaped

func (vec *Vector) BytesEscaped() []byte

BytesEscaped returns escaped URL bytes.

In addition, escapes host and hash part.

func (*Vector) Hash

func (vec *Vector) Hash() *vector.Node

Hash returns hash node.

func (*Vector) HashBytes

func (vec *Vector) HashBytes() []byte

HashBytes returns hash as bytes.

func (*Vector) HashString

func (vec *Vector) HashString() string

HashString returns hash as string.

func (*Vector) Host

func (vec *Vector) Host() *vector.Node

Host returns host node (contains both hostname/IP and port substrings).

func (*Vector) HostBytes

func (vec *Vector) HostBytes() []byte

HostBytes returns host (hostname:port) as bytes.

func (*Vector) HostString

func (vec *Vector) HostString() string

HostString returns host (hostname:port) as string.

func (*Vector) Hostname

func (vec *Vector) Hostname() *vector.Node

Hostname returns hostname node (similar to Host(), but excludes port).

func (*Vector) HostnameBytes

func (vec *Vector) HostnameBytes() []byte

HostnameBytes returns hostname as bytes.

func (*Vector) HostnameString

func (vec *Vector) HostnameString() string

HostnameString returns hostname as string.

func (*Vector) Parse

func (vec *Vector) Parse(s []byte) error

Parse source bytes.

func (*Vector) ParseCopy

func (vec *Vector) ParseCopy(s []byte) error

ParseCopy copies source bytes and parse it.

func (*Vector) ParseCopyStr

func (vec *Vector) ParseCopyStr(s string) error

ParseCopyStr copies source string and parse it. DEPRECATED: use ParseCopyString instead.

func (*Vector) ParseCopyString added in v1.1.11

func (vec *Vector) ParseCopyString(s string) error

ParseCopyString copies source string and parse it.

func (*Vector) ParseStr

func (vec *Vector) ParseStr(s string) error

ParseStr parses source string. DEPRECATED: use ParseString instead.

func (*Vector) ParseString added in v1.1.11

func (vec *Vector) ParseString(s string) error

ParseString parses source string.

func (*Vector) Password

func (vec *Vector) Password() *vector.Node

Password returns password node.

func (*Vector) PasswordBytes

func (vec *Vector) PasswordBytes() []byte

PasswordBytes returns password as bytes.

func (*Vector) PasswordString

func (vec *Vector) PasswordString() string

PasswordString returns password as string.

func (*Vector) Path

func (vec *Vector) Path() *vector.Node

Path returns path node.

func (*Vector) PathBytes

func (vec *Vector) PathBytes() []byte

PathBytes returns password as bytes.

func (*Vector) PathEscape

func (vec *Vector) PathEscape(p []byte) []byte

PathEscape escapes the string so it can be safely placed inside a URL query.

Uses built-in buffer.

func (*Vector) PathString

func (vec *Vector) PathString() string

PathString returns password as string.

func (*Vector) PathUnescape

func (vec *Vector) PathUnescape(p []byte) []byte

PathUnescape does the inverse transformation of PathEscape.

func (*Vector) Port

func (vec *Vector) Port() int

Port returns port as integer.

func (*Vector) Query

func (vec *Vector) Query() *vector.Node

Query returns query node with origin query params order.

func (*Vector) QueryBytes

func (vec *Vector) QueryBytes() []byte

QueryBytes returns query as bytes.

func (*Vector) QueryEscape

func (vec *Vector) QueryEscape(p []byte) []byte

QueryEscape escapes the string so it can be safely placed inside a URL query.

Uses built-in buffer.

func (*Vector) QueryLen

func (vec *Vector) QueryLen() int

QueryLen returns length of the raw query without question mark symbol.

func (*Vector) QuerySort

func (vec *Vector) QuerySort() *Vector

QuerySort sorts query params an AB order.

func (*Vector) QueryString

func (vec *Vector) QueryString() string

QueryString returns query as string.

func (*Vector) QueryUnescape

func (vec *Vector) QueryUnescape(p []byte) []byte

QueryUnescape does the inverse transformation of QueryEscape.

func (*Vector) Scheme

func (vec *Vector) Scheme() *vector.Node

Scheme returns scheme node.

func (*Vector) SchemeBytes

func (vec *Vector) SchemeBytes() []byte

SchemeBytes returns scheme as bytes.

func (*Vector) SchemeString

func (vec *Vector) SchemeString() string

SchemeString returns scheme as string.

func (*Vector) SetAuthBytes

func (vec *Vector) SetAuthBytes(auth []byte) *Vector

SetAuthBytes replaces auth with bytes.

func (*Vector) SetAuthString

func (vec *Vector) SetAuthString(auth string) *Vector

SetAuthString replaces auth with string.

func (*Vector) SetHashBytes

func (vec *Vector) SetHashBytes(hash []byte) *Vector

SetHashBytes replaces hash with bytes.

func (*Vector) SetHashString

func (vec *Vector) SetHashString(hash string) *Vector

SetHashString replaces hash with string.

func (*Vector) SetHostBytes

func (vec *Vector) SetHostBytes(host []byte) *Vector

SetHostBytes replaces host with bytes.

func (*Vector) SetHostString

func (vec *Vector) SetHostString(host string) *Vector

SetHostString replaces host with string.

func (*Vector) SetHostnameBytes

func (vec *Vector) SetHostnameBytes(hostname []byte) *Vector

SetHostnameBytes replaces hostname with bytes.

func (*Vector) SetHostnameString

func (vec *Vector) SetHostnameString(hostname string) *Vector

SetHostnameString replaces hostname with string.

func (*Vector) SetPasswordBytes

func (vec *Vector) SetPasswordBytes(password []byte) *Vector

SetPasswordBytes replaces password with bytes.

func (*Vector) SetPasswordString

func (vec *Vector) SetPasswordString(password string) *Vector

SetPasswordString replaces password with string.

func (*Vector) SetPathBytes

func (vec *Vector) SetPathBytes(path []byte) *Vector

SetPathBytes replaces path with bytes.

func (*Vector) SetPathString

func (vec *Vector) SetPathString(path string) *Vector

SetPathString replaces path with string.

func (*Vector) SetPort

func (vec *Vector) SetPort(port int) *Vector

SetPort replaces port.

func (*Vector) SetQueryBytes

func (vec *Vector) SetQueryBytes(query []byte) *Vector

SetQueryBytes replaces query with bytes.

func (*Vector) SetQueryString

func (vec *Vector) SetQueryString(query string) *Vector

SetQueryString replaces query with string.

func (*Vector) SetSchemeBytes

func (vec *Vector) SetSchemeBytes(scheme []byte) *Vector

SetSchemeBytes replaces scheme with bytes.

func (*Vector) SetSchemeString

func (vec *Vector) SetSchemeString(scheme string) *Vector

SetSchemeString replaces scheme with string.

func (*Vector) SetUsernameBytes

func (vec *Vector) SetUsernameBytes(username []byte) *Vector

SetUsernameBytes replaces username with bytes.

func (*Vector) SetUsernameString

func (vec *Vector) SetUsernameString(username string) *Vector

SetUsernameString replaces username with string.

func (*Vector) Slashes

func (vec *Vector) Slashes() bool

Slashes indicates if URL is started with slashes.

func (*Vector) String

func (vec *Vector) String() string

String reassembles the vector into a valid URL string.

func (*Vector) StringEscaped

func (vec *Vector) StringEscaped() string

StringEscaped returns escaped URL string.

In addition, escapes host and hash part.

func (*Vector) Username

func (vec *Vector) Username() *vector.Node

Username returns username node.

func (*Vector) UsernameBytes

func (vec *Vector) UsernameBytes() []byte

UsernameBytes returns username as bytes.

func (*Vector) UsernameString

func (vec *Vector) UsernameString() string

UsernameString returns username as string.

Jump to

Keyboard shortcuts

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