diff --git a/encoding/ascii.go b/encoding/ascii.go index 7f4b083..2df3096 100644 --- a/encoding/ascii.go +++ b/encoding/ascii.go @@ -4,8 +4,9 @@ import "bytes" type ASCIICoder struct{} -func (c *ASCIICoder) Encode(s string, buf *bytes.Buffer) error { - buf.WriteString(s) +func (c *ASCIICoder) Encode(s *string, buf *bytes.Buffer) error { + // These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine + buf.WriteString(*s) return nil } diff --git a/encoding/ascii_test.go b/encoding/ascii_test.go index bea47c9..631d29e 100644 --- a/encoding/ascii_test.go +++ b/encoding/ascii_test.go @@ -11,7 +11,7 @@ func TestASCIIEncodeSimpleASCIIString(t *testing.T) { input := "Hello, World!" expected := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33} - err := coder.Encode(input, &buf) + err := coder.Encode(&input, &buf) if err != nil { t.Errorf("Expected no error, but got %v", err) @@ -46,7 +46,7 @@ func TestASCIIEncodeEmptyString(t *testing.T) { input := "" expected := []byte{} - err := coder.Encode(input, &buf) + err := coder.Encode(&input, &buf) if err != nil { t.Errorf("Expected no error, but got %v", err) diff --git a/encoding/coder.go b/encoding/coder.go index 3a1ac50..76f30a3 100644 --- a/encoding/coder.go +++ b/encoding/coder.go @@ -3,6 +3,6 @@ package encoding import "bytes" type Coder interface { - Encode(s string, buf *bytes.Buffer) error + Encode(s *string, buf *bytes.Buffer) error Decode(buf *bytes.Buffer) (string, error) } \ No newline at end of file diff --git a/encoding/gsm7.go b/encoding/gsm7.go index 429443b..b2560c2 100644 --- a/encoding/gsm7.go +++ b/encoding/gsm7.go @@ -11,15 +11,15 @@ type GSM7Coder struct{} // Otherwise Encode will allocate memory as it sees fit // Which is fine but not optimal // Preallocate the buffer with the size of EncodesInto bytes -func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) error { +func (c *GSM7Coder) Encode(s *string, buf *bytes.Buffer) error { // utf8 := *(*[]byte)(unsafe.Pointer(&s)) - utf8 := []byte(s) + utf8 := []byte(*s) var ( offset int = 1 bitshift byte = 0 leap, shift bool ) - encodedSize := GSM7EncodesInto(&s) + encodedSize := GSM7EncodesInto(s) cap := buf.Cap() if cap < encodedSize { buf.Grow(encodedSize - cap) diff --git a/encoding/gsm7_test.go b/encoding/gsm7_test.go index e2818c2..47f9053 100644 --- a/encoding/gsm7_test.go +++ b/encoding/gsm7_test.go @@ -22,7 +22,7 @@ func TestGSM7EncodeSimpleASCIIString(t *testing.T) { input := short8nString expected := short8nStringEncodedBytes - err := coder.Encode(input, &buf) + err := coder.Encode(&input, &buf) if err != nil { t.Errorf("Expected no error, but got %v", err) @@ -39,7 +39,7 @@ func TestGSM7EncodeComplexASCIIString(t *testing.T) { input := longNot8nString expected := longNot8nStringEncodedBytes - err := coder.Encode(input, &buf) + err := coder.Encode(&input, &buf) if err != nil { t.Errorf("Expected no error, but got %v", err) @@ -56,7 +56,7 @@ func TestGSM7EncodeComplex8nASCIIString(t *testing.T) { input := "Ducks are fucking great, they quacks, O quackers, what the fuck." expected := long8nStringEncodedBytes - err := coder.Encode(input, &buf) + err := coder.Encode(&input, &buf) if err != nil { t.Errorf("Expected no error, but got %v", err) @@ -74,7 +74,7 @@ func TestGSM7EncodeDoesNotAllocateMoreThanNecessary(t *testing.T) { buf.Reset() expected := buf.Cap() - err := coder.Encode(input, buf) + err := coder.Encode(&input, buf) actual := buf.Cap() if err != nil { @@ -93,7 +93,7 @@ func TestGSM7EncodeDoesAllocateWhenNecessary(t *testing.T) { buf.Reset() original := buf.Cap() - err := coder.Encode(input, &buf) + err := coder.Encode(&input, &buf) modified := buf.Cap() if err != nil { @@ -111,7 +111,7 @@ func TestGSM7EncodeEmptyString(t *testing.T) { input := "" expected := []byte{} - err := coder.Encode(input, &buf) + err := coder.Encode(&input, &buf) if err != nil { t.Errorf("Expected no error, but got %v", err) @@ -330,7 +330,7 @@ func BenchmarkGSM7EncodeSimpleASCIIString(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - coder.Encode(input, &buf) + coder.Encode(&input, &buf) } } @@ -341,7 +341,7 @@ func BenchmarkGSM7EncodeComplexASCIIString(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - coder.Encode(input, &buf) + coder.Encode(&input, &buf) } } @@ -352,7 +352,7 @@ func BenchmarkGSM7EncodeComplex8nASCIIString(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - coder.Encode(input, &buf) + coder.Encode(&input, &buf) } } @@ -364,6 +364,6 @@ func BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc(b *testing.B) { for i := 0; i < b.N; i++ { buf := bytes.NewBuffer(make([]byte, GSM7EncodesInto(&input))) buf.Reset() - coder.Encode(input, buf) + coder.Encode(&input, buf) } }