Remove unnecessary copy of the decoder array

This commit is contained in:
Kovid Goyal
2023-09-04 22:28:18 +05:30
parent 1f4386fd08
commit d1d888ce19
2 changed files with 5 additions and 5 deletions

View File

@@ -26,7 +26,7 @@ var encode = [85]byte{
0x60, 0x7B, 0x7C, 0x7D, 0x7E,
}
var decoder_array = sync.OnceValue(func() [256]byte {
var decoder_array = sync.OnceValue(func() *[256]byte {
var decode = [256]byte{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
@@ -48,7 +48,7 @@ var decoder_array = sync.OnceValue(func() [256]byte {
for i := 0; i < len(encode); i++ {
decode[encode[i]] = byte(i)
}
return decode
return &decode
})
@@ -169,14 +169,14 @@ func Decode(dst, src []byte) (int, error) {
decode := decoder_array()
for len(src) > 0 {
if len(src) < 5 {
w, err := decodeChunk(&decode, dst, src)
w, err := decodeChunk(decode, dst, src)
if err > 0 {
return t, CorruptInputError(f + err)
}
return t + w, nil
}
_, err := decodeChunk(&decode, dst[:4], src[:5])
_, err := decodeChunk(decode, dst[:4], src[:5])
if err > 0 {
return t, CorruptInputError(f + err)
} else {

View File

@@ -23,7 +23,7 @@ func TestBase85(t *testing.T) {
t.Fatalf("Encoded len for %#v wrong: %d != %d", x, el, EncodedLen(len(a)))
}
encoded := dst[:el]
dl, bad_idx := decodeChunk(&decode, dst2[:], encoded)
dl, bad_idx := decodeChunk(decode, dst2[:], encoded)
if bad_idx != 0 {
t.Fatalf("Decode for %#v returned bad data at: %d (%#v)", x, bad_idx, encoded)
}