mirror of https://github.com/perkeep/perkeep.git
blobref: add AsUint64 accessor
Change-Id: I1326763beecdaed5461c29b65d1dab78e456af99
This commit is contained in:
parent
972f3adfb2
commit
1d089ec601
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue