// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pprof

// A protobuf is a simple protocol buffer encoder.
type protobuf struct {
	data []byte
	tmp  [16]byte
	nest int
}

func ( *protobuf) ( uint64) {
	for  >= 128 {
		.data = append(.data, byte()|0x80)
		 >>= 7
	}
	.data = append(.data, byte())
}

func ( *protobuf) ( int,  int) {
	.varint(uint64()<<3 | 2)
	.varint(uint64())
}

func ( *protobuf) ( int,  uint64) {
	// append varint to b.data
	.varint(uint64()<<3 | 0)
	.varint()
}

func ( *protobuf) ( int,  []uint64) {
	if len() > 2 {
		// Use packed encoding
		 := len(.data)
		for ,  := range  {
			.varint()
		}
		 := len(.data)
		.length(, -)
		 := len(.data)
		copy(.tmp[:], .data[:])
		copy(.data[+(-):], .data[:])
		copy(.data[:], .tmp[:-])
		return
	}
	for ,  := range  {
		.uint64(, )
	}
}

func ( *protobuf) ( int,  uint64) {
	if  == 0 {
		return
	}
	.uint64(, )
}

func ( *protobuf) ( int,  int64) {
	 := uint64()
	.uint64(, )
}

func ( *protobuf) ( int,  int64) {
	if  == 0 {
		return
	}
	.int64(, )
}

func ( *protobuf) ( int,  []int64) {
	if len() > 2 {
		// Use packed encoding
		 := len(.data)
		for ,  := range  {
			.varint(uint64())
		}
		 := len(.data)
		.length(, -)
		 := len(.data)
		copy(.tmp[:], .data[:])
		copy(.data[+(-):], .data[:])
		copy(.data[:], .tmp[:-])
		return
	}
	for ,  := range  {
		.int64(, )
	}
}

func ( *protobuf) ( int,  string) {
	.length(, len())
	.data = append(.data, ...)
}

func ( *protobuf) ( int,  []string) {
	for ,  := range  {
		.string(, )
	}
}

func ( *protobuf) ( int,  string) {
	if  == "" {
		return
	}
	.string(, )
}

func ( *protobuf) ( int,  bool) {
	if  {
		.uint64(, 1)
	} else {
		.uint64(, 0)
	}
}

func ( *protobuf) ( int,  bool) {
	if ! {
		return
	}
	.bool(, )
}

type msgOffset int

func ( *protobuf) () msgOffset {
	.nest++
	return msgOffset(len(.data))
}

func ( *protobuf) ( int,  msgOffset) {
	 := int()
	 := len(.data)
	.length(, -)
	 := len(.data)
	copy(.tmp[:], .data[:])
	copy(.data[+(-):], .data[:])
	copy(.data[:], .tmp[:-])
	.nest--
}