little refactor
This commit is contained in:
@@ -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
|
||||||
|
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user