diff --git a/encoding/ascii.go b/encoding/ascii.go new file mode 100644 index 0000000..2557e43 --- /dev/null +++ b/encoding/ascii.go @@ -0,0 +1,13 @@ +package encoding + +import "bytes" + +type ASCIICoder struct{} + +func (c *ASCIICoder) Encode(s string, buf *bytes.Buffer) { + buf.WriteString(s) +} + +func (c *ASCIICoder) Decode(buf *bytes.Buffer) string { + return buf.String() +} diff --git a/encoding/ascii_test.go b/encoding/ascii_test.go new file mode 100644 index 0000000..d28f93b --- /dev/null +++ b/encoding/ascii_test.go @@ -0,0 +1,58 @@ +package encoding + +import ( + "bytes" + "testing" +) + +func TestASCIIEncodeSimpleASCIIString(t *testing.T) { + coder := &ASCIICoder{} + var buf bytes.Buffer + input := "Hello, World!" + + expected := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33} + coder.Encode(input, &buf) + + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("Expected %v, but got %v", expected, buf.Bytes()) + } +} + +func TestASCIIDecodeSimpleASCIIString(t *testing.T) { + coder := &ASCIICoder{} + var buf bytes.Buffer + input := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33} + + expected := "Hello, World!" + buf.Write(input) + output := coder.Decode(&buf) + + if output != expected { + t.Errorf("Expected %v, but got %v", expected, output) + } +} + +func TestASCIIEncodeEmptyString(t *testing.T) { + coder := &ASCIICoder{} + var buf bytes.Buffer + input := "" + + expected := []byte{} + coder.Encode(input, &buf) + + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("Expected %v, but got %v", expected, buf.Bytes()) + } +} + +func TestASCIIDecodeEmptyString(t *testing.T) { + coder := &ASCIICoder{} + buf := bytes.NewBuffer([]byte{}) + + expected := "" + output := coder.Decode(buf) + + if output != expected { + t.Errorf("Expected %v, but got %v", expected, output) + } +} diff --git a/encoding/charset.json b/encoding/charset.json new file mode 100644 index 0000000..60c72aa --- /dev/null +++ b/encoding/charset.json @@ -0,0 +1,5 @@ +{ + "ucs2": { + "link": "http://www.columbia.edu/kermit/ucs2.html" + } +} \ No newline at end of file diff --git a/encoding/coder.go b/encoding/coder.go new file mode 100644 index 0000000..7f2c39b --- /dev/null +++ b/encoding/coder.go @@ -0,0 +1,8 @@ +package encoding + +import "bytes" + +type Coder interface { + Encode(s string, buf *bytes.Buffer) + Decode(buf *bytes.Buffer) string +} \ No newline at end of file diff --git a/encoding/ucs2.go b/encoding/ucs2.go new file mode 100644 index 0000000..9ec252f --- /dev/null +++ b/encoding/ucs2.go @@ -0,0 +1,13 @@ +package encoding + +import "bytes" + +type UCS2Coder struct{} + +func (c *UCS2Coder) Encode(s string, buf *bytes.Buffer) { + panic("UCS2 not implemented yet") +} + +func (c *UCS2Coder) Decode(buf *bytes.Buffer) string { + panic("UCS2 not implemented yet") +}