From e9dade20fc4ce18a25f835b74fe908404e10a385 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 23 Dec 2014 19:10:13 -0800 Subject: [PATCH] blob: add weird StringMinusOne method Change-Id: If1cb450e2131002eaa80e7f5ee2a449c745b99a6 --- pkg/blob/ref.go | 15 +++++++++++++++ pkg/blob/ref_test.go | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/pkg/blob/ref.go b/pkg/blob/ref.go index 46fa3d52e..d93380958 100644 --- a/pkg/blob/ref.go +++ b/pkg/blob/ref.go @@ -88,6 +88,21 @@ func (r Ref) String() string { return string(r.appendString(buf)) } +// StringMinusOne returns the first string that's before String. +func (r Ref) StringMinusOne() string { + if r.digest == nil { + return "" + } + // TODO: maybe memoize this. + dname := r.digest.digestName() + bs := r.digest.bytes() + buf := getBuf(len(dname) + 1 + len(bs)*2)[:0] + defer putBuf(buf) + buf = r.appendString(buf) + buf[len(buf)-1]-- // no need to deal with carrying underflow (no 0 bytes ever) + return string(buf) +} + func (r Ref) appendString(buf []byte) []byte { dname := r.digest.digestName() bs := r.digest.bytes() diff --git a/pkg/blob/ref_test.go b/pkg/blob/ref_test.go index 1d127ae76..7cae13f08 100644 --- a/pkg/blob/ref_test.go +++ b/pkg/blob/ref_test.go @@ -188,6 +188,14 @@ func TestSizedBlobRefString(t *testing.T) { } } +func TestRefStringMinusOne(t *testing.T) { + br := MustParse("abc-1234") + want := "abc-1233" + if got := br.StringMinusOne(); got != want { + t.Errorf("StringMinusOne = %q; want %q", got, want) + } +} + func TestMarshalBinary(t *testing.T) { br := MustParse("abc-00ff4869") data, _ := br.MarshalBinary()