little refactor
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 17s
Run Tests / Test (push) Failing after 15s

This commit is contained in:
2024-07-30 22:58:51 +02:00
parent 39c7876ed1
commit f5e263752e
2 changed files with 25 additions and 15 deletions

View File

@@ -3,7 +3,6 @@ package encoding
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"log"
) )
type GSM7Coder struct{} type GSM7Coder struct{}
@@ -63,7 +62,6 @@ func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) error {
offset = 1 offset = 1
} }
} }
log.Println(buf.Cap(), buf.Len())
return nil return nil
} }
@@ -74,7 +72,7 @@ func (c *GSM7Coder) Decode(buf *bytes.Buffer) (string, error) {
bitshift byte = 0 bitshift byte = 0
leap, shift bool leap, shift bool
) )
outLength := DecodesInto(buf) outLength := GSM7DecodesInto(buf)
lengthDiff := outLength - len(gsm7) lengthDiff := outLength - len(gsm7)
gsm7 = append(gsm7, make([]byte, lengthDiff)...) gsm7 = append(gsm7, make([]byte, lengthDiff)...)
@@ -144,7 +142,7 @@ func InsertAt(data *[]byte, index int, value byte) {
(*data)[index] = value (*data)[index] = value
} }
func EncodesInto(s *string) int { func GSM7EncodesInto(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 {
@@ -152,7 +150,7 @@ func EncodesInto(s *string) int {
} }
return enclen return enclen
} }
func DecodesInto(buf *bytes.Buffer) int { func GSM7DecodesInto(buf *bytes.Buffer) int {
blen := buf.Len() blen := buf.Len()
declen := blen * 8 / 7 declen := blen * 8 / 7
return declen return declen

View File

@@ -6,13 +6,13 @@ import (
) )
var ( var (
short8nString = "Sunshine" short8nString = "Sunshine"
longNot8nString = "Golden rays play, Chasing night away." longNot8nString = "Golden rays play, Chasing night away."
long8nString = "Ducks are fucking great, they quacks, O quackers, what the fuck." long8nString = "Ducks are fucking great, they quacks, O quackers, what the fuck."
short8nStringEncodedBytes = []byte{0b11010011, 0b10111010, 0b01111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011} short8nStringEncodedBytes = []byte{0b11010011, 0b10111010, 0b01111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011}
longNot8nStringEncodedBytes = []byte{0b11000111, 0b00110111, 0b10011011, 0b01011100, 0b01110110, 0b10000011, 0b11100100, 0b11100001, 0b11111100, 0b00011100, 0b00000100, 0b01100111, 0b10000111, 0b11110011, 0b00101100, 0b11010000, 0b00010000, 0b00011101, 0b10011110, 0b10100111, 0b11011101, 0b01100111, 0b10010000, 0b00111011, 0b01111101, 0b01000110, 0b11010011, 0b01000001, 0b11100001, 0b01111011, 0b00111000, 0b11101111, 0b00000010} longNot8nStringEncodedBytes = []byte{0b11000111, 0b00110111, 0b10011011, 0b01011100, 0b01110110, 0b10000011, 0b11100100, 0b11100001, 0b11111100, 0b00011100, 0b00000100, 0b01100111, 0b10000111, 0b11110011, 0b00101100, 0b11010000, 0b00010000, 0b00011101, 0b10011110, 0b10100111, 0b11011101, 0b01100111, 0b10010000, 0b00111011, 0b01111101, 0b01000110, 0b11010011, 0b01000001, 0b11100001, 0b01111011, 0b00111000, 0b11101111, 0b00000010}
long8nStringEncodedBytes = []byte{0b11000100, 0b11111010, 0b01111000, 0b00111101, 0b00000111, 0b10000101, 0b11100101, 0b01100101, 0b10010000, 0b10111001, 0b00111110, 0b01011110, 0b10100111, 0b11011101, 0b01100111, 0b11010000, 0b01011001, 0b01011110, 0b00001110, 0b11010011, 0b01011001, 0b00100000, 0b00111010, 0b10111010, 0b10011100, 0b00000111, 0b11000101, 0b11101011, 0b11100001, 0b11110001, 0b01111010, 0b11001110, 0b00000010, 0b00111101, 0b01000001, 0b11110001, 0b01111010, 0b01111000, 0b10111100, 0b00101110, 0b11001011, 0b11100111, 0b00101100, 0b11010000, 0b00011101, 0b00011101, 0b10100110, 0b10000011, 0b11101000, 0b11101000, 0b00110010, 0b11001000, 0b01011100, 0b00011111, 0b10101111, 0b01011101} long8nStringEncodedBytes = []byte{0b11000100, 0b11111010, 0b01111000, 0b00111101, 0b00000111, 0b10000101, 0b11100101, 0b01100101, 0b10010000, 0b10111001, 0b00111110, 0b01011110, 0b10100111, 0b11011101, 0b01100111, 0b11010000, 0b01011001, 0b01011110, 0b00001110, 0b11010011, 0b01011001, 0b00100000, 0b00111010, 0b10111010, 0b10011100, 0b00000111, 0b11000101, 0b11101011, 0b11100001, 0b11110001, 0b01111010, 0b11001110, 0b00000010, 0b00111101, 0b01000001, 0b11110001, 0b01111010, 0b01111000, 0b10111100, 0b00101110, 0b11001011, 0b11100111, 0b00101100, 0b11010000, 0b00011101, 0b00011101, 0b10100110, 0b10000011, 0b11101000, 0b11101000, 0b00110010, 0b11001000, 0b01011100, 0b00011111, 0b10101111, 0b01011101}
) )
// region encode // region encode
@@ -233,7 +233,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 := EncodesInto(&input) actual := GSM7EncodesInto(&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)
} }
@@ -242,7 +242,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 := EncodesInto(&input) actual := GSM7EncodesInto(&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)
} }
@@ -251,7 +251,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 := EncodesInto(&input) actual := GSM7EncodesInto(&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)
} }
@@ -260,7 +260,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 := DecodesInto(bytes.NewBuffer(input)) actual := GSM7DecodesInto(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)
} }
@@ -269,7 +269,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 := DecodesInto(bytes.NewBuffer(input)) actual := GSM7DecodesInto(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)
} }
@@ -278,7 +278,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 := DecodesInto(bytes.NewBuffer(input)) actual := GSM7DecodesInto(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)
} }
@@ -317,3 +317,15 @@ func BenchmarkGSM7EncodeComplex8nASCIIString(b *testing.B) {
coder.Encode(input, &buf) coder.Encode(input, &buf)
} }
} }
func BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc(b *testing.B) {
coder := &GSM7Coder{}
input := long8nString
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf := bytes.NewBuffer(make([]byte, GSM7EncodesInto(&input)))
buf.Reset()
coder.Encode(input, buf)
}
}