A lil optimize

This commit is contained in:
2024-07-30 22:15:36 +02:00
parent 9fbcdd86cc
commit db61da9fc2
2 changed files with 24 additions and 20 deletions

View File

@@ -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 {

View File

@@ -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) {