Skip to content

Commit

Permalink
kbin: add Uuid support
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
twmb committed Jan 4, 2021
1 parent c87dac4 commit de22e10
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/kbin/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit de22e10

Please sign in to comment.