initial commits

master
Charles Iliya Krempeaux 2021-11-07 16:42:42 -08:00
parent 0e9afc8511
commit 43b4f22894
2 changed files with 29 additions and 5 deletions

View File

@ -5,6 +5,11 @@ import (
"encoding/base64" "encoding/base64"
"encoding/binary" "encoding/binary"
"io" "io"
"strings"
)
const (
serializationprefix = "x"
) )
var ( var (
@ -19,25 +24,42 @@ var (
func serialize(value uint64) string { func serialize(value uint64) string {
var storage bytes.Buffer var binstorage bytes.Buffer
{ {
err := binary.Write(&storage, binary.BigEndian, value) err := binary.Write(&binstorage, binary.BigEndian, value)
if nil != err { if nil != err {
return "" return ""
} }
} }
var encoded string var encoded strings.Builder
{ {
encoded = base64encoding.EncodeToString(storage.Bytes()) encoded.WriteString(serializationprefix)
}
{
var wc io.WriteCloser = base64.NewEncoder(base64encoding, &encoded)
if nil == wc {
return ""
}
wc.Write(binstorage.Bytes())
wc.Close()
} }
return encoded return encoded.String()
} }
func unserialize(value string) (uint64, bool) { func unserialize(value string) (uint64, bool) {
{
if !strings.HasPrefix(value, serializationprefix) {
return badvalue, false
}
value = value[len(serializationprefix):]
}
var p []byte var p []byte
{ {
var err error var err error

View File

@ -13,6 +13,7 @@ func TestSerialize(t *testing.T) {
actual, successful := unserialize(serialized) actual, successful := unserialize(serialized)
if !successful { if !successful {
t.Errorf("For test #%d, expected unserialization of serialized data to be successful but wasn't." , testNumber) t.Errorf("For test #%d, expected unserialization of serialized data to be successful but wasn't." , testNumber)
t.Logf("SERIALIZED: %s", serialized)
t.Logf("SUCCESSFUL: %t", successful) t.Logf("SUCCESSFUL: %t", successful)
t.Logf("VALUE: %064b", test.Value) t.Logf("VALUE: %064b", test.Value)
continue continue
@ -21,6 +22,7 @@ func TestSerialize(t *testing.T) {
if expected := test.Value; expected != actual { if expected := test.Value; expected != actual {
t.Errorf("For test #%d, ", testNumber) t.Errorf("For test #%d, ", testNumber)
t.Logf("SERIALIZED: %s", serialized)
t.Logf("SUCCESSFUL: %t", successful) t.Logf("SUCCESSFUL: %t", successful)
t.Logf("EXPECTED: %064b", expected) t.Logf("EXPECTED: %064b", expected)
t.Logf("ACTUAL: %064b", actual) t.Logf("ACTUAL: %064b", actual)