Version bump.
[stompngo.git] / header_methods.go
blobcf1be08e42d1abe53072b2bc63545b592ff0563e
1 //
2 // Copyright © 2011-2019 Guy M. Allard
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 package stompngo
19 import (
20 "unicode/utf8"
24 Add appends a key and value pair as a header to a set of Headers.
26 func (h Headers) Add(k, v string) Headers {
27 r := append(h, k, v)
28 return r
32 AddHeaders appends one set of Headers to another.
34 func (h Headers) AddHeaders(o Headers) Headers {
35 r := append(h, o...)
36 return r
40 Compare compares one set of Headers with another.
42 func (h Headers) Compare(other Headers) bool {
43 if len(h) != len(other) {
44 return false
46 for i, v := range h {
47 if v != other[i] {
48 return false
51 return true
55 Contains returns true if a set of Headers contains a key.
57 func (h Headers) Contains(k string) (string, bool) {
58 for i := 0; i < len(h); i += 2 {
59 if h[i] == k {
60 return h[i+1], true
63 return "", false
67 ContainsKV returns true if a set of Headers contains a key and value pair.
69 func (h Headers) ContainsKV(k string, v string) bool {
70 for i := 0; i < len(h); i += 2 {
71 if h[i] == k && h[i+1] == v {
72 return true
75 return false
79 Value returns a header value for a specified key. If the key is not present
80 an empty string is returned.
82 func (h Headers) Value(k string) string {
83 for i := 0; i < len(h); i += 2 {
84 if h[i] == k {
85 return h[i+1]
88 return ""
92 Index returns the index of a keader key in Headers. Return -1 if the
93 key is not present.
95 func (h Headers) Index(k string) int {
96 r := -1
97 for i := 0; i < len(h); i += 2 {
98 if h[i] == k {
99 r = i
100 break
103 return r
107 Validate performs bacic validation of a set of Headers.
109 func (h Headers) Validate() error {
110 if len(h)%2 != 0 {
111 return EHDRLEN
113 return nil
117 ValidateUTF8 validates that header strings are UTF8.
119 func (h Headers) ValidateUTF8() (string, error) {
120 for i := range h {
121 if !utf8.ValidString(h[i]) {
122 return h[i], EHDRUTF8
125 return "", nil
129 Clone copies a set of Headers.
131 func (h Headers) Clone() Headers {
132 r := make(Headers, len(h))
133 copy(r, h)
134 return r
138 Delete removes a key and value pair from a set of Headers.
140 func (h Headers) Delete(k string) Headers {
141 r := h.Clone()
142 i := r.Index(k)
143 if i >= 0 {
144 r = append(r[:i], r[i+2:]...)
146 return r
150 Size returns the size of Headers on the wire, in bytes.
152 func (h Headers) Size(e bool) int64 {
153 l := 0
154 for i := 0; i < len(h); i += 2 {
155 if e {
156 l += len(encode(h[i])) + 1 + len(encode(h[i+1])) + 1
157 } else {
158 l += len(h[i]) + 1 + len(h[i+1]) + 1
161 return int64(l)
165 String makes Headers a Stringer.
167 func (h Headers) String() string {
168 ec := h.Validate()
169 if ec != nil {
170 return ec.Error()
172 b := make([]byte, 0, 1024)
173 for i := 0; i < len(h); i += 2 {
174 b = append(b, h[i]+":"+h[i+1]+"\n"...)
176 return string(b)
180 Bytes returns a byte slice of the headers
182 func (h Headers) Bytes() []byte {
183 b := make([]byte, 0, 1024)
184 for i := 0; i < len(h); i += 2 {
185 b = append(b, h[i]+":"+h[i+1]+"\n"...)
187 return b