Skip to content

Commit

Permalink
- replaces usage of bool by void to save memory space in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Dec 13, 2022
1 parent eb434ef commit da2b3ba
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions request_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,39 @@ import "strings"

//RequestHeaders represents a collection of request headers
type RequestHeaders struct {
headers map[string]map[string]bool
headers map[string]map[string]struct{}
}

//NewRequestHeaders creates a new RequestHeaders
func NewRequestHeaders() *RequestHeaders {
return &RequestHeaders{
headers: make(map[string]map[string]bool),
headers: make(map[string]map[string]struct{}),
}
}

func (r *RequestHeaders) normalizeHeaderKey(key string) string {
return strings.ToLower(strings.Trim(key, " "))
}

type void struct{}

var voidInstance void

//Add adds a new header or append a new value to an existing header
func (r *RequestHeaders) Add(key string, value string, additionalValues ...string) {
if key == "" || value == "" {
normalizedKey := r.normalizeHeaderKey(key)
if normalizedKey == "" || value == "" {
return
}
if r.headers == nil {
r.headers = make(map[string]map[string]bool)
r.headers = make(map[string]map[string]struct{})
}
normalizedKey := r.normalizeHeaderKey(key)
if r.headers[normalizedKey] == nil {
r.headers[normalizedKey] = make(map[string]bool)
r.headers[normalizedKey] = make(map[string]struct{})
}
r.headers[normalizedKey][value] = true
r.headers[normalizedKey][value] = voidInstance
for _, v := range additionalValues {
r.headers[normalizedKey][v] = true
r.headers[normalizedKey][v] = voidInstance
}
}

Expand Down

0 comments on commit da2b3ba

Please sign in to comment.