From de22e10128a694dd470403b898a2d7b93ce947b1 Mon Sep 17 00:00:00 2001 From: Travis Bischel Date: Sun, 3 Jan 2021 20:36:11 -0700 Subject: [PATCH] kbin: add Uuid support In Java, a UUID is two longs, high and low. This imitates that by being [2]uint64. We can use uints even though Java does not have them; the important part is bitwise equality, not signedness. If Go ever adds u128 support, we will switch to that. --- pkg/kbin/primitives.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/kbin/primitives.go b/pkg/kbin/primitives.go index d3cc5bb3..38317a4a 100644 --- a/pkg/kbin/primitives.go +++ b/pkg/kbin/primitives.go @@ -52,6 +52,15 @@ func AppendFloat64(dst []byte, f float64) []byte { return appendUint64(dst, math.Float64bits(f)) } +// AppendUuid appends first the high (0) bytes and then the low (1) bytes to +// dst. +// +// If Go ever introduces u128, this will be replaced with that. +func AppendUuid(dst []byte, uuid [2]uint64) []byte { + dst = appendUint64(dst, uuid[0]) + return appendUint64(dst, uuid[1]) +} + func appendUint64(dst []byte, u uint64) []byte { return append(dst, byte(u>>56), byte(u>>48), byte(u>>40), byte(u>>32), byte(u>>24), byte(u>>16), byte(u>>8), byte(u)) @@ -373,6 +382,14 @@ func (b *Reader) Int64() int64 { return int64(b.readUint64()) } +// Uuid returns a uuid from the reader. +func (b *Reader) Uuid() [2]uint64 { + return [2]uint64{ + b.readUint64(), // hi + b.readUint64(), // lo + } +} + // Float64 returns a float64 from the reader. func (b *Reader) Float64() float64 { return math.Float64frombits(b.readUint64())