Beispiel #1
0
// SetDecimal encodes the specified decimal value into the bytes field of
// the receiver using Gob encoding, sets the tag and clears the checksum.
func (v *Value) SetDecimal(dec *inf.Dec) error {
	decSize := encoding.UpperBoundNonsortingDecimalSize(dec)
	v.RawBytes = make([]byte, headerSize, headerSize+decSize)
	v.RawBytes = encoding.EncodeNonsortingDecimal(v.RawBytes, dec)
	v.setTag(ValueType_DECIMAL)
	return nil
}
Beispiel #2
0
// EncodeDecimalValue encodes an inf.Dec value, appends it to the supplied
// buffer, and returns the final buffer.
func EncodeDecimalValue(appendTo []byte, d *inf.Dec, delimited bool) []byte {
	if delimited {
		appendTo = append(appendTo, byte(ValueType_DELIMITED_DECIMAL))
		// To avoid the allocation, leave space for the varint, encode the decimal,
		// encode the varint, and shift the encoded decimal to the end of the
		// varint.
		varintPos := len(appendTo)
		// Manually append 10 (binary.MaxVarintLen64) 0s to avoid the allocation.
		appendTo = append(appendTo, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
		decOffset := len(appendTo)
		appendTo = encoding.EncodeNonsortingDecimal(appendTo, d)
		decLen := len(appendTo) - decOffset
		varintLen := binary.PutVarint(appendTo[varintPos:decOffset], int64(decLen))
		copy(appendTo[varintPos+varintLen:varintPos+varintLen+decLen], appendTo[decOffset:decOffset+decLen])
		return appendTo[:varintPos+varintLen+decLen]
	}
	appendTo = append(appendTo, byte(ValueType_DECIMAL))
	return encoding.EncodeNonsortingDecimal(appendTo, d)
}