1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // A protobuf is a simple protocol buffer encoder.
14 func (b
*protobuf
) varint(x
uint64) {
16 b
.data
= append(b
.data
, byte(x
)|
0x80)
19 b
.data
= append(b
.data
, byte(x
))
22 func (b
*protobuf
) length(tag
int, len int) {
23 b
.varint(uint64(tag
)<<3 |
2)
27 func (b
*protobuf
) uint64(tag
int, x
uint64) {
28 // append varint to b.data
29 b
.varint(uint64(tag
)<<3 |
0)
33 func (b
*protobuf
) uint64s(tag
int, x
[]uint64) {
35 // Use packed encoding
43 copy(b
.tmp
[:], b
.data
[n2
:n3
])
44 copy(b
.data
[n1
+(n3
-n2
):], b
.data
[n1
:n2
])
45 copy(b
.data
[n1
:], b
.tmp
[:n3
-n2
])
53 func (b
*protobuf
) uint64Opt(tag
int, x
uint64) {
60 func (b
*protobuf
) int64(tag
int, x
int64) {
65 func (b
*protobuf
) int64Opt(tag
int, x
int64) {
72 func (b
*protobuf
) int64s(tag
int, x
[]int64) {
74 // Use packed encoding
82 copy(b
.tmp
[:], b
.data
[n2
:n3
])
83 copy(b
.data
[n1
+(n3
-n2
):], b
.data
[n1
:n2
])
84 copy(b
.data
[n1
:], b
.tmp
[:n3
-n2
])
92 func (b
*protobuf
) string(tag
int, x
string) {
94 b
.data
= append(b
.data
, x
...)
97 func (b
*protobuf
) strings(tag
int, x
[]string) {
103 func (b
*protobuf
) stringOpt(tag
int, x
string) {
110 func (b
*protobuf
) bool(tag
int, x
bool) {
118 func (b
*protobuf
) boolOpt(tag
int, x
bool) {
127 func (b
*protobuf
) startMessage() msgOffset
{
129 return msgOffset(len(b
.data
))
132 func (b
*protobuf
) endMessage(tag
int, start msgOffset
) {
137 copy(b
.tmp
[:], b
.data
[n2
:n3
])
138 copy(b
.data
[n1
+(n3
-n2
):], b
.data
[n1
:n2
])
139 copy(b
.data
[n1
:], b
.tmp
[:n3
-n2
])