diff --git a/encoding/gsm7.go b/encoding/gsm7.go index 50894cb..914dca7 100644 --- a/encoding/gsm7.go +++ b/encoding/gsm7.go @@ -15,6 +15,10 @@ func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) error { bitshift byte = 0 leap, shift bool ) + tbw := len(utf8) * 7 / 8 + if buf.Available() < tbw { + buf.Grow(tbw) + } for index, septet := range utf8 { if septet > 0b01111111 { diff --git a/encoding/gsm7_test.go b/encoding/gsm7_test.go index 2781079..298a26d 100644 --- a/encoding/gsm7_test.go +++ b/encoding/gsm7_test.go @@ -147,12 +147,12 @@ func TestGSM7DecodeSimpleASCIIString(t *testing.T) { // region insertat func TestInsertAtBeginning(t *testing.T) { - data := []byte{2, 3, 4, 0} - InsertAt(&data, 0, 1) - expected := []byte{1, 2, 3, 4} - if !bytes.Equal(data, expected) { - t.Errorf("expected %v, got %v", expected, data) - } + data := []byte{2, 3, 4, 0} + InsertAt(&data, 0, 1) + expected := []byte{1, 2, 3, 4} + if !bytes.Equal(data, expected) { + t.Errorf("expected %v, got %v", expected, data) + } } func TestInsertInMiddle(t *testing.T) { @@ -174,13 +174,13 @@ func TestInsertAtEnd(t *testing.T) { } func TestIndexOutOfBounds(t *testing.T) { - data := []byte{2, 3, 4} - defer func() { - if r := recover(); r == nil { - t.Errorf("The code did not panic") - } - }() - InsertAt(&data, 4, 5) + data := []byte{2, 3, 4} + defer func() { + if r := recover(); r == nil { + t.Errorf("The code did not panic") + } + }() + InsertAt(&data, 4, 5) } func TestNegativeIndex(t *testing.T) { @@ -194,12 +194,12 @@ func TestNegativeIndex(t *testing.T) { } func TestMaintainsOrderAfterInsertion(t *testing.T) { - data := []byte{2, 3, 4, 0} - InsertAt(&data, 1, 1) - expected := []byte{2, 1, 3, 4} - if !bytes.Equal(data, expected) { - t.Errorf("expected %v, got %v", expected, data) - } + data := []byte{2, 3, 4, 0} + InsertAt(&data, 1, 1) + expected := []byte{2, 1, 3, 4} + if !bytes.Equal(data, expected) { + t.Errorf("expected %v, got %v", expected, data) + } } func TestMaintainsLength(t *testing.T) { @@ -251,4 +251,4 @@ func BenchmarkGSM7EncodeComplex8nASCIIString(b *testing.B) { for i := 0; i < b.N; i++ { coder.Encode(input, &buf) } -} \ No newline at end of file +}