rewrite Cookie with KVBuffer

This commit is contained in:
Patricio Whittingslow
2026-07-29 13:18:17 -03:00
parent feecdb309e
commit af0f1b8cc6
4 changed files with 46 additions and 72 deletions
+15 -1
View File
@@ -49,6 +49,18 @@ func (mb *KVBuffer) Get(key string) []byte {
return mb.musttoken(mb.kvs[v].value)
}
// ForEach iterates over the cookie's key-value pairs until cb returns false.
func (c *KVBuffer) ForEach(cb func(key, value []byte) bool) {
nc := len(c.kvs)
for i := range nc {
if !c.kvs[i].isValid() {
continue
} else if !cb(c.At(i)) {
break
}
}
}
func (mb *KVBuffer) Present(key string) bool {
return mb.getIdx(key) >= 0
}
@@ -69,10 +81,12 @@ func (mb *KVBuffer) setInternal(key, value []byte) bool {
}
func (mb *KVBuffer) Len() int { return len(mb.kvs) }
func (mb *KVBuffer) Pair(i int) (key, value []byte) {
func (mb *KVBuffer) At(i int) (key, value []byte) {
kv := mb.kvs[i]
return mb.musttoken(kv.key), mb.musttoken(kv.value)
}
func (mb *KVBuffer) AtKey(i int) (key []byte) { return mb.musttoken(mb.kvs[i].key) }
func (mb *KVBuffer) AtValue(i int) (key []byte) { return mb.musttoken(mb.kvs[i].value) }
func (mb *KVBuffer) getIdx(key string) int {
for i, kv := range mb.kvs {