From d0c868ca5cdbe806cf0bd5fba25baab7d96f6fa9 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 31 Jul 2024 13:05:13 +0200 Subject: [PATCH] Add coder/decoder size to coder --- encoding/ascii.go | 7 +++++++ encoding/coder.go | 4 +++- encoding/gsm7.go | 8 ++++---- encoding/gsm7_test.go | 16 ++++++++-------- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/encoding/ascii.go b/encoding/ascii.go index 2df3096..552a7b2 100644 --- a/encoding/ascii.go +++ b/encoding/ascii.go @@ -13,3 +13,10 @@ func (c *ASCIICoder) Encode(s *string, buf *bytes.Buffer) error { func (c *ASCIICoder) Decode(buf *bytes.Buffer) (string, error) { 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() +} diff --git a/encoding/coder.go b/encoding/coder.go index 76f30a3..2b3d69b 100644 --- a/encoding/coder.go +++ b/encoding/coder.go @@ -5,4 +5,6 @@ import "bytes" type Coder interface { Encode(s *string, buf *bytes.Buffer) error Decode(buf *bytes.Buffer) (string, error) -} \ No newline at end of file + EncodesInto(s *string) int + DecodesInto(buf *bytes.Buffer) int +} diff --git a/encoding/gsm7.go b/encoding/gsm7.go index b2560c2..8beadcd 100644 --- a/encoding/gsm7.go +++ b/encoding/gsm7.go @@ -19,7 +19,7 @@ func (c *GSM7Coder) Encode(s *string, buf *bytes.Buffer) error { bitshift byte = 0 leap, shift bool ) - encodedSize := GSM7EncodesInto(s) + encodedSize := c.EncodesInto(s) cap := buf.Cap() if cap < encodedSize { buf.Grow(encodedSize - cap) @@ -77,7 +77,7 @@ func (c *GSM7Coder) Decode(buf *bytes.Buffer) (string, error) { bitshift byte = 0 leap bool ) - outLength := GSM7DecodesInto(buf) + outLength := c.DecodesInto(buf) lengthDiff := outLength - len(gsm7) gsm7 = append(gsm7, make([]byte, lengthDiff)...) start := len(gsm7) - 2 @@ -121,7 +121,7 @@ func InsertAt(data *[]byte, index int, value byte) { (*data)[index] = value } -func GSM7EncodesInto(s *string) int { +func (c GSM7Coder) EncodesInto(s *string) int { slen := len(*s) enclen := slen * 7 / 8 if slen%8 != 0 { @@ -129,7 +129,7 @@ func GSM7EncodesInto(s *string) int { } return enclen } -func GSM7DecodesInto(buf *bytes.Buffer) int { +func (c GSM7Coder) DecodesInto(buf *bytes.Buffer) int { blen := buf.Len() declen := blen * 8 / 7 return declen diff --git a/encoding/gsm7_test.go b/encoding/gsm7_test.go index 47f9053..fe9731a 100644 --- a/encoding/gsm7_test.go +++ b/encoding/gsm7_test.go @@ -70,7 +70,7 @@ func TestGSM7EncodeComplex8nASCIIString(t *testing.T) { func TestGSM7EncodeDoesNotAllocateMoreThanNecessary(t *testing.T) { coder := &GSM7Coder{} 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() expected := buf.Cap() @@ -271,7 +271,7 @@ func TestDeletesLastValue(t *testing.T) { func TestGSM7EncodesIntoSmallString(t *testing.T) { input := short8nString expected := 7 - actual := GSM7EncodesInto(&input) + actual := GSM7Coder{}.EncodesInto(&input) if actual != expected { t.Errorf("Expected %d, but got %d", expected, actual) } @@ -280,7 +280,7 @@ func TestGSM7EncodesIntoSmallString(t *testing.T) { func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) { input := longNot8nString expected := 33 - actual := GSM7EncodesInto(&input) + actual := GSM7Coder{}.EncodesInto(&input) if actual != expected { t.Errorf("Expected %d, but got %d", expected, actual) } @@ -289,7 +289,7 @@ func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) { func TestGSM7EncodesIntoLarger8nString(t *testing.T) { input := long8nString expected := 56 - actual := GSM7EncodesInto(&input) + actual := GSM7Coder{}.EncodesInto(&input) if actual != expected { t.Errorf("Expected %d, but got %d", expected, actual) } @@ -298,7 +298,7 @@ func TestGSM7EncodesIntoLarger8nString(t *testing.T) { func TestGSM7DecodesIntoSmallString(t *testing.T) { input := short8nStringEncodedBytes expected := 8 - actual := GSM7DecodesInto(bytes.NewBuffer(input)) + actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input)) if actual != expected { t.Errorf("Expected %d, but got %d", expected, actual) } @@ -307,7 +307,7 @@ func TestGSM7DecodesIntoSmallString(t *testing.T) { func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) { input := longNot8nStringEncodedBytes expected := 37 - actual := GSM7DecodesInto(bytes.NewBuffer(input)) + actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input)) if actual != expected { t.Errorf("Expected %d, but got %d", expected, actual) } @@ -316,7 +316,7 @@ func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) { func TestGSM7DecodesIntoLarger8nString(t *testing.T) { input := long8nStringEncodedBytes expected := 64 - actual := GSM7DecodesInto(bytes.NewBuffer(input)) + actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input)) if actual != expected { t.Errorf("Expected %d, but got %d", expected, actual) } @@ -362,7 +362,7 @@ func BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - buf := bytes.NewBuffer(make([]byte, GSM7EncodesInto(&input))) + buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input))) buf.Reset() coder.Encode(&input, buf) }