]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/go.opencensus.io/internal/tagencoding/tagencoding.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / go.opencensus.io / internal / tagencoding / tagencoding.go
1 // Copyright 2017, OpenCensus Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15
16 // Package tagencoding contains the tag encoding
17 // used interally by the stats collector.
18 package tagencoding // import "go.opencensus.io/internal/tagencoding"
19
20 type Values struct {
21 Buffer []byte
22 WriteIndex int
23 ReadIndex int
24 }
25
26 func (vb *Values) growIfRequired(expected int) {
27 if len(vb.Buffer)-vb.WriteIndex < expected {
28 tmp := make([]byte, 2*(len(vb.Buffer)+1)+expected)
29 copy(tmp, vb.Buffer)
30 vb.Buffer = tmp
31 }
32 }
33
34 func (vb *Values) WriteValue(v []byte) {
35 length := len(v) & 0xff
36 vb.growIfRequired(1 + length)
37
38 // writing length of v
39 vb.Buffer[vb.WriteIndex] = byte(length)
40 vb.WriteIndex++
41
42 if length == 0 {
43 // No value was encoded for this key
44 return
45 }
46
47 // writing v
48 copy(vb.Buffer[vb.WriteIndex:], v[:length])
49 vb.WriteIndex += length
50 }
51
52 // ReadValue is the helper method to read the values when decoding valuesBytes to a map[Key][]byte.
53 func (vb *Values) ReadValue() []byte {
54 // read length of v
55 length := int(vb.Buffer[vb.ReadIndex])
56 vb.ReadIndex++
57 if length == 0 {
58 // No value was encoded for this key
59 return nil
60 }
61
62 // read value of v
63 v := make([]byte, length)
64 endIdx := vb.ReadIndex + length
65 copy(v, vb.Buffer[vb.ReadIndex:endIdx])
66 vb.ReadIndex = endIdx
67 return v
68 }
69
70 func (vb *Values) Bytes() []byte {
71 return vb.Buffer[:vb.WriteIndex]
72 }