diff --git a/xxhash.go b/xxhash.go index e29a8c3..a72ee48 100644 --- a/xxhash.go +++ b/xxhash.go @@ -65,6 +65,13 @@ func (d *Digest) ResetWithSeed(seed uint64) { d.n = 0 } +// Copy returns a copy of the Digest. +func (d *Digest) Copy() *Digest { + digest := *d + copy(digest.mem[:], d.mem[:]) + return &digest +} + // Size always returns 8 bytes. func (d *Digest) Size() int { return 8 } diff --git a/xxhash_test.go b/xxhash_test.go index 8e2f456..9b499bf 100644 --- a/xxhash_test.go +++ b/xxhash_test.go @@ -190,6 +190,23 @@ func TestAllocs(t *testing.T) { }) } +func TestCopy(t *testing.T) { + want := Sum64String("abcdef") + + d0 := New() + d0.WriteString("abc") + d1 := d0.Copy() + d0.WriteString("def") + d1.WriteString("def") + + if got := d0.Sum64(); got != want { + t.Fatalf("hashing with original Digest, got 0x%x; want 0x%x", got, want) + } + if got := d1.Sum64(); got != want { + t.Fatalf("hashing with copied Digest, got 0x%x; want 0x%x", got, want) + } +} + func testAllocs(t *testing.T, fn func()) { t.Helper() if allocs := int(testing.AllocsPerRun(10, fn)); allocs > 0 {