diff --git a/serialize.go b/serialize.go index 8bdfff4..5921167 100644 --- a/serialize.go +++ b/serialize.go @@ -5,6 +5,11 @@ import ( "encoding/base64" "encoding/binary" "io" + "strings" +) + +const ( + serializationprefix = "x" ) var ( @@ -19,25 +24,42 @@ var ( 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 { 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) { + { + if !strings.HasPrefix(value, serializationprefix) { + return badvalue, false + } + + value = value[len(serializationprefix):] + } + var p []byte { var err error diff --git a/serialize_test.go b/serialize_test.go index 305b97d..bcfe471 100644 --- a/serialize_test.go +++ b/serialize_test.go @@ -13,6 +13,7 @@ func TestSerialize(t *testing.T) { actual, successful := unserialize(serialized) if !successful { 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("VALUE: %064b", test.Value) continue @@ -21,6 +22,7 @@ func TestSerialize(t *testing.T) { if expected := test.Value; expected != actual { t.Errorf("For test #%d, ", testNumber) + t.Logf("SERIALIZED: %s", serialized) t.Logf("SUCCESSFUL: %t", successful) t.Logf("EXPECTED: %064b", expected) t.Logf("ACTUAL: %064b", actual)