mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-15 16:07:51 +03:00
Optimized Peek() function for args and request header cookies
This commit is contained in:
@@ -137,8 +137,7 @@ func (a *Args) SetBytesKV(key, value []byte) {
|
||||
//
|
||||
// Returned value is valid until the next Args call.
|
||||
func (a *Args) Peek(key string) []byte {
|
||||
a.bufKV.key = AppendBytesStr(a.bufKV.key[:0], key)
|
||||
return a.PeekBytes(a.bufKV.key)
|
||||
return peekArgStr(a.args, key)
|
||||
}
|
||||
|
||||
// PeekBytes returns query arg value for the given key.
|
||||
@@ -147,7 +146,7 @@ func (a *Args) Peek(key string) []byte {
|
||||
//
|
||||
// It is safe modifying key buffer after PeekBytes return.
|
||||
func (a *Args) PeekBytes(key []byte) []byte {
|
||||
return peekArg(a.args, key)
|
||||
return peekArgBytes(a.args, key)
|
||||
}
|
||||
|
||||
// Has returns true if the given key exists in Args.
|
||||
@@ -282,17 +281,27 @@ func releaseArg(h []argsKV) []argsKV {
|
||||
func hasArg(h []argsKV, k []byte) bool {
|
||||
for i, n := 0, len(h); i < n; i++ {
|
||||
kv := &h[i]
|
||||
if bytes.Equal(k, kv.key) {
|
||||
if bytes.Equal(kv.key, k) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func peekArg(h []argsKV, k []byte) []byte {
|
||||
func peekArgBytes(h []argsKV, k []byte) []byte {
|
||||
for i, n := 0, len(h); i < n; i++ {
|
||||
kv := &h[i]
|
||||
if bytes.Equal(k, kv.key) {
|
||||
if bytes.Equal(kv.key, k) {
|
||||
return kv.value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func peekArgStr(h []argsKV, k string) []byte {
|
||||
for i, n := 0, len(h); i < n; i++ {
|
||||
kv := &h[i]
|
||||
if EqualBytesStr(kv.key, k) {
|
||||
return kv.value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -549,7 +549,7 @@ func (h *ResponseHeader) peek(key []byte) []byte {
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return peekArg(h.h, key)
|
||||
return peekArgBytes(h.h, key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -567,26 +567,25 @@ func (h *RequestHeader) peek(key []byte) []byte {
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return peekArg(h.h, key)
|
||||
return peekArgBytes(h.h, key)
|
||||
}
|
||||
}
|
||||
|
||||
// PeekCookie returns cookie for the given key.
|
||||
func (h *RequestHeader) PeekCookie(key string) []byte {
|
||||
h.bufKV.key = AppendBytesStr(h.bufKV.key[:0], key)
|
||||
return h.PeekCookieBytes(h.bufKV.key)
|
||||
return peekArgStr(h.cookies, key)
|
||||
}
|
||||
|
||||
// PeekCookieBytes returns cookie for the given key.
|
||||
func (h *RequestHeader) PeekCookieBytes(key []byte) []byte {
|
||||
return peekArg(h.cookies, key)
|
||||
return peekArgBytes(h.cookies, key)
|
||||
}
|
||||
|
||||
// GetCookie fills cookie for the given cookie.Key.
|
||||
//
|
||||
// Returns false if cookie with the given cookie.Key is missing.
|
||||
func (h *ResponseHeader) GetCookie(cookie *Cookie) bool {
|
||||
v := peekArg(h.cookies, cookie.Key)
|
||||
v := peekArgBytes(h.cookies, cookie.Key)
|
||||
if v == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user