Skip to content

Commit

Permalink
introduce AppendEncode and AppendDecode
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Mar 7, 2024
1 parent be1ca72 commit 29dc17e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
30 changes: 30 additions & 0 deletions base32.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ func (enc *Encoding) Encode(dst, src []byte) {
}
}

// AppendEncode appends the base32 encoded src to dst
// and returns the extended buffer.
func (enc *Encoding) AppendEncode(dst, src []byte) []byte {
n := enc.EncodedLen(len(src))
dst = grow(dst, n)
enc.Encode(dst[len(dst):][:n], src)
return dst[:len(dst)+n]
}

// EncodeToString returns the base32 encoding of src.
func (enc *Encoding) EncodeToString(src []byte) string {
buf := make([]byte, enc.EncodedLen(len(src)))
Expand Down Expand Up @@ -310,6 +319,16 @@ func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
return enc.decode(dst, src)
}

// AppendDecode appends the base32 decoded src to dst
// and returns the extended buffer.
// If the input is malformed, it returns the partially decoded src and an error.
func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error) {
n := enc.DecodedLen(len(src))
dst = grow(dst, n)
n, err := enc.Decode(dst[len(dst):][:n], src)
return dst[:len(dst)+n], err
}

// DecodeString returns the bytes represented by the base32 string s.
func (enc *Encoding) DecodeString(s string) ([]byte, error) {
buf := []byte(s)
Expand Down Expand Up @@ -406,3 +425,14 @@ func (d *decoder) Read(p []byte) (n int, err error) {
func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
return &decoder{enc: enc, r: r}
}

// grow increases the capacity of the byte slice.
func grow(buf []byte, n int) []byte {
if n < 0 {
panic("cannot be negative")
}
if n -= cap(buf) - len(buf); n > 0 {
buf = append(buf[:cap(buf)], make([]byte, n)...)[:len(buf)]
}
return buf
}
27 changes: 27 additions & 0 deletions base32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ func TestEncode(t *testing.T) {
}
}

func TestAppendEncode(t *testing.T) {
enc := NewEncoding()
for _, testCase := range testCasesEncode {
want := []byte("lead" + testCase.encoded)
got := enc.AppendEncode([]byte("lead"), []byte(testCase.plain))
if !bytes.Equal(got, want) {
t.Errorf("encoded %q, expected %q, actual %q\n",
testCase.plain, want, got)
}
}
}

func TestEncoder(t *testing.T) {
enc := NewEncoding()
for _, testCase := range testCasesEncode {
Expand Down Expand Up @@ -202,6 +214,21 @@ func TestDecode(t *testing.T) {
}
}

func TestAppendDecode(t *testing.T) {
enc := NewEncoding()
for _, testCase := range testCasesDecode {
want := []byte("lead" + testCase.plain)
got, err := enc.AppendDecode([]byte("lead"), []byte(testCase.encoded))
if err != nil {
t.Errorf("error while decoding %q: %v", testCase.encoded, err)
}
if !bytes.Equal(got, want) {
t.Errorf("decoded %q, expected %q, actual %q\n",
testCase.encoded, want, got)
}
}
}

var testCasesDecodeError = []struct {
input string
pos int64
Expand Down

0 comments on commit 29dc17e

Please sign in to comment.