blob: add weird StringMinusOne method

Change-Id: If1cb450e2131002eaa80e7f5ee2a449c745b99a6
This commit is contained in:
Brad Fitzpatrick 2014-12-23 19:10:13 -08:00
parent 1cbae79412
commit e9dade20fc
2 changed files with 23 additions and 0 deletions

View File

@ -88,6 +88,21 @@ func (r Ref) String() string {
return string(r.appendString(buf)) return string(r.appendString(buf))
} }
// StringMinusOne returns the first string that's before String.
func (r Ref) StringMinusOne() string {
if r.digest == nil {
return "<invalid-blob.Ref>"
}
// 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 { func (r Ref) appendString(buf []byte) []byte {
dname := r.digest.digestName() dname := r.digest.digestName()
bs := r.digest.bytes() bs := r.digest.bytes()

View File

@ -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) { func TestMarshalBinary(t *testing.T) {
br := MustParse("abc-00ff4869") br := MustParse("abc-00ff4869")
data, _ := br.MarshalBinary() data, _ := br.MarshalBinary()