Add coder/decoder size to coder

This commit is contained in:
2024-07-31 13:05:13 +02:00
parent 7001f2c51a
commit d0c868ca5c
4 changed files with 22 additions and 13 deletions

View File

@@ -13,3 +13,10 @@ func (c *ASCIICoder) Encode(s *string, buf *bytes.Buffer) error {
func (c *ASCIICoder) Decode(buf *bytes.Buffer) (string, error) { func (c *ASCIICoder) Decode(buf *bytes.Buffer) (string, error) {
return buf.String(), nil return buf.String(), nil
} }
func (c ASCIICoder) EncodesInto(s *string) int {
return len(*s)
}
func (c ASCIICoder) DecodesInto(buf *bytes.Buffer) int {
return buf.Len()
}

View File

@@ -5,4 +5,6 @@ import "bytes"
type Coder interface { type Coder interface {
Encode(s *string, buf *bytes.Buffer) error Encode(s *string, buf *bytes.Buffer) error
Decode(buf *bytes.Buffer) (string, error) Decode(buf *bytes.Buffer) (string, error)
EncodesInto(s *string) int
DecodesInto(buf *bytes.Buffer) int
} }

View File

@@ -19,7 +19,7 @@ func (c *GSM7Coder) Encode(s *string, buf *bytes.Buffer) error {
bitshift byte = 0 bitshift byte = 0
leap, shift bool leap, shift bool
) )
encodedSize := GSM7EncodesInto(s) encodedSize := c.EncodesInto(s)
cap := buf.Cap() cap := buf.Cap()
if cap < encodedSize { if cap < encodedSize {
buf.Grow(encodedSize - cap) buf.Grow(encodedSize - cap)
@@ -77,7 +77,7 @@ func (c *GSM7Coder) Decode(buf *bytes.Buffer) (string, error) {
bitshift byte = 0 bitshift byte = 0
leap bool leap bool
) )
outLength := GSM7DecodesInto(buf) outLength := c.DecodesInto(buf)
lengthDiff := outLength - len(gsm7) lengthDiff := outLength - len(gsm7)
gsm7 = append(gsm7, make([]byte, lengthDiff)...) gsm7 = append(gsm7, make([]byte, lengthDiff)...)
start := len(gsm7) - 2 start := len(gsm7) - 2
@@ -121,7 +121,7 @@ func InsertAt(data *[]byte, index int, value byte) {
(*data)[index] = value (*data)[index] = value
} }
func GSM7EncodesInto(s *string) int { func (c GSM7Coder) EncodesInto(s *string) int {
slen := len(*s) slen := len(*s)
enclen := slen * 7 / 8 enclen := slen * 7 / 8
if slen%8 != 0 { if slen%8 != 0 {
@@ -129,7 +129,7 @@ func GSM7EncodesInto(s *string) int {
} }
return enclen return enclen
} }
func GSM7DecodesInto(buf *bytes.Buffer) int { func (c GSM7Coder) DecodesInto(buf *bytes.Buffer) int {
blen := buf.Len() blen := buf.Len()
declen := blen * 8 / 7 declen := blen * 8 / 7
return declen return declen

View File

@@ -70,7 +70,7 @@ func TestGSM7EncodeComplex8nASCIIString(t *testing.T) {
func TestGSM7EncodeDoesNotAllocateMoreThanNecessary(t *testing.T) { func TestGSM7EncodeDoesNotAllocateMoreThanNecessary(t *testing.T) {
coder := &GSM7Coder{} coder := &GSM7Coder{}
input := "Ducks are fucking great, they quacks, O quackers, what the fuck." input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
buf := bytes.NewBuffer(make([]byte, GSM7EncodesInto(&input))) buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input)))
buf.Reset() buf.Reset()
expected := buf.Cap() expected := buf.Cap()
@@ -271,7 +271,7 @@ func TestDeletesLastValue(t *testing.T) {
func TestGSM7EncodesIntoSmallString(t *testing.T) { func TestGSM7EncodesIntoSmallString(t *testing.T) {
input := short8nString input := short8nString
expected := 7 expected := 7
actual := GSM7EncodesInto(&input) actual := GSM7Coder{}.EncodesInto(&input)
if actual != expected { if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual) t.Errorf("Expected %d, but got %d", expected, actual)
} }
@@ -280,7 +280,7 @@ func TestGSM7EncodesIntoSmallString(t *testing.T) {
func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) { func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) {
input := longNot8nString input := longNot8nString
expected := 33 expected := 33
actual := GSM7EncodesInto(&input) actual := GSM7Coder{}.EncodesInto(&input)
if actual != expected { if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual) t.Errorf("Expected %d, but got %d", expected, actual)
} }
@@ -289,7 +289,7 @@ func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) {
func TestGSM7EncodesIntoLarger8nString(t *testing.T) { func TestGSM7EncodesIntoLarger8nString(t *testing.T) {
input := long8nString input := long8nString
expected := 56 expected := 56
actual := GSM7EncodesInto(&input) actual := GSM7Coder{}.EncodesInto(&input)
if actual != expected { if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual) t.Errorf("Expected %d, but got %d", expected, actual)
} }
@@ -298,7 +298,7 @@ func TestGSM7EncodesIntoLarger8nString(t *testing.T) {
func TestGSM7DecodesIntoSmallString(t *testing.T) { func TestGSM7DecodesIntoSmallString(t *testing.T) {
input := short8nStringEncodedBytes input := short8nStringEncodedBytes
expected := 8 expected := 8
actual := GSM7DecodesInto(bytes.NewBuffer(input)) actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
if actual != expected { if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual) t.Errorf("Expected %d, but got %d", expected, actual)
} }
@@ -307,7 +307,7 @@ func TestGSM7DecodesIntoSmallString(t *testing.T) {
func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) { func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) {
input := longNot8nStringEncodedBytes input := longNot8nStringEncodedBytes
expected := 37 expected := 37
actual := GSM7DecodesInto(bytes.NewBuffer(input)) actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
if actual != expected { if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual) t.Errorf("Expected %d, but got %d", expected, actual)
} }
@@ -316,7 +316,7 @@ func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) {
func TestGSM7DecodesIntoLarger8nString(t *testing.T) { func TestGSM7DecodesIntoLarger8nString(t *testing.T) {
input := long8nStringEncodedBytes input := long8nStringEncodedBytes
expected := 64 expected := 64
actual := GSM7DecodesInto(bytes.NewBuffer(input)) actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
if actual != expected { if actual != expected {
t.Errorf("Expected %d, but got %d", expected, actual) t.Errorf("Expected %d, but got %d", expected, actual)
} }
@@ -362,7 +362,7 @@ func BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc(b *testing.B) {
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
buf := bytes.NewBuffer(make([]byte, GSM7EncodesInto(&input))) buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input)))
buf.Reset() buf.Reset()
coder.Encode(&input, buf) coder.Encode(&input, buf)
} }