blobref: add AsUint64 accessor

Change-Id: I1326763beecdaed5461c29b65d1dab78e456af99
This commit is contained in:
Brad Fitzpatrick 2013-07-21 05:15:02 +12:00
parent 972f3adfb2
commit 1d089ec601
2 changed files with 24 additions and 0 deletions

View File

@ -48,6 +48,22 @@ type BlobRef struct {
strValue string // "<hashname>-<digest>"
}
// AsUint64 returns the first 64-bits of the blobref as an integer.
func (br *BlobRef) AsUint64() uint64 {
var ret uint64
for i := 0; i < 16; i++ {
b := br.digest[i]
switch {
case b >= '0' && b <= '9':
b = b - '0'
default:
b = b - 'a' + 10
}
ret = ret<<4 | uint64(b)
}
return ret
}
func (br *BlobRef) GobEncode() ([]byte, error) {
return []byte(br.String()), nil
}

View File

@ -157,3 +157,11 @@ func TestSizedBlobRefString(t *testing.T) {
t.Errorf("fmt %%s &SizedBlobRef = %q, want %q", g, e)
}
}
func TestAsUint64(t *testing.T) {
br := MustParse("sha1-b123456789abcdef0123456789abcdef01234567")
got := br.AsUint64()
if want := uint64(0xb123456789abcdef); got != want {
t.Errorf("got %x; want %x", got, want)
}
}