From 56c81c36cb3ce952ce1bd9ca8948831d038da868 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Mon, 12 Feb 2024 21:35:42 -0800 Subject: [PATCH] initial commits --- write.go | 38 +++ writebytes_test.go | 63 ++++ writerune_test.go | 687 ++++++++++++++++++++++++++++++++++++++++++++ writestring_test.go | 63 ++++ 4 files changed, 851 insertions(+) create mode 100644 write.go create mode 100644 writebytes_test.go create mode 100644 writerune_test.go create mode 100644 writestring_test.go diff --git a/write.go b/write.go new file mode 100644 index 0000000..5ff1837 --- /dev/null +++ b/write.go @@ -0,0 +1,38 @@ +package htmlescape + +import ( + "io" +) + +func WriteBytes(writer io.Writer, bytes []byte) (n int, err error) { + var buffer [bufferSize]byte + var p []byte = buffer[0:0] + + p, err = AppendBytes(p, bytes) + if nil != err { + return 0, err + } + + return writer.Write(p) +} + +func WriteRune(writer io.Writer, r rune) (n int, err error) { + var buffer [bufferSize]byte + var p []byte = buffer[0:0] + + p = AppendRune(p,r) + + return writer.Write(p) +} + +func WriteString(writer io.Writer, str string) (n int, err error) { + var buffer [bufferSize]byte + var p []byte = buffer[0:0] + + p, err = AppendString(p, str) + if nil != err { + return 0, err + } + + return writer.Write(p) +} diff --git a/writebytes_test.go b/writebytes_test.go new file mode 100644 index 0000000..19ec45b --- /dev/null +++ b/writebytes_test.go @@ -0,0 +1,63 @@ +package htmlescape_test + +import ( + "testing" + + "bytes" + + "sourcecode.social/reiver/go-htmlescape" +) + +func TestWriteBytes(t *testing.T) { + + tests := []struct{ + Slice []byte + Expected []byte + }{ + { + Slice: []byte("This is NOT bold!"), + Expected: []byte("This is NOT <b>bold</b>!"), + }, + + + + { + Slice: []byte(`"this is a quotation"`), + Expected: []byte(""this is a quotation""), + }, + + + + { + Slice: []byte("\t3 < 5\r\n3 > 2\x00"), + Expected: []byte(" 3 < 5 3 > 2�"), + }, + } + + for testNumber, test := range tests { + + var buffer bytes.Buffer + + _, err := htmlescape.WriteBytes(&buffer, test.Slice) + + if nil != err { + t.Errorf("For test #%d, did not expect to get an error but actually got one." , testNumber) + t.Logf("ERROR: (%T) %s", err, err) + t.Logf("SLICE: %q", test.Slice) + continue + } + + { + expected := test.Expected + actual := buffer.Bytes() + + if !bytes.Equal(expected, actual) { + t.Errorf("For test #%d, the actual resulting value of the append is not what was expected.", testNumber) + t.Logf("EXPECTED: %q", expected) + t.Logf("ACTUAL: %q", actual) + t.Logf("SLICE: %q", test.Slice) + continue + } + } + } +} diff --git a/writerune_test.go b/writerune_test.go new file mode 100644 index 0000000..b321e08 --- /dev/null +++ b/writerune_test.go @@ -0,0 +1,687 @@ +package htmlescape_test + +import ( + "testing" + + "bytes" + + "sourcecode.social/reiver/go-htmlescape" +) + +func TestWriteRune(t *testing.T) { + + tests := []struct{ + Rune rune + Expected []byte + }{ + { + Rune: 0x00, + Expected: []byte("�"), + }, + { + Rune: 0x01, + Expected: []byte(""), + }, + { + Rune: 0x02, + Expected: []byte(""), + }, + { + Rune: 0x03, + Expected: []byte(""), + }, + { + Rune: 0x04, + Expected: []byte(""), + }, + { + Rune: 0x05, + Expected: []byte(""), + }, + { + Rune: 0x06, + Expected: []byte(""), + }, + { + Rune: 0x07, + Expected: []byte(""), + }, + { + Rune: 0x08, + Expected: []byte(""), + }, + { + Rune: 0x09, + Expected: []byte(" "), + }, + { + Rune: 0x0A, + Expected: []byte(" "), + }, + { + Rune: 0x0B, + Expected: []byte(" "), + }, + { + Rune: 0x0C, + Expected: []byte(" "), + }, + { + Rune: 0x0D, + Expected: []byte(" "), + }, + { + Rune: 0x0E, + Expected: []byte(""), + }, + { + Rune: 0x0F, + Expected: []byte(""), + }, + { + Rune: 0x10, + Expected: []byte(""), + }, + { + Rune: 0x11, + Expected: []byte(""), + }, + { + Rune: 0x12, + Expected: []byte(""), + }, + { + Rune: 0x13, + Expected: []byte(""), + }, + { + Rune: 0x14, + Expected: []byte(""), + }, + { + Rune: 0x15, + Expected: []byte(""), + }, + { + Rune: 0x16, + Expected: []byte(""), + }, + { + Rune: 0x17, + Expected: []byte(""), + }, + { + Rune: 0x18, + Expected: []byte(""), + }, + { + Rune: 0x19, + Expected: []byte(""), + }, + { + Rune: 0x1A, + Expected: []byte(""), + }, + { + Rune: 0x1B, + Expected: []byte(""), + }, + { + Rune: 0x1C, + Expected: []byte(""), + }, + { + Rune: 0x1D, + Expected: []byte(""), + }, + { + Rune: 0x1E, + Expected: []byte(""), + }, + { + Rune: 0x1F, + Expected: []byte(""), + }, + { + Rune: 0x20, + Expected: []byte(" "), + }, + { + Rune: 0x21, + Expected: []byte("!"), + }, + { + Rune: 0x22, + Expected: []byte("""), + }, + { + Rune: 0x23, + Expected: []byte("#"), + }, + { + Rune: 0x24, + Expected: []byte("$"), + }, + { + Rune: 0x25, + Expected: []byte("%"), + }, + { + Rune: 0x26, + Expected: []byte("&"), + }, + { + Rune: 0x27, + Expected: []byte("'"), + }, + { + Rune: 0x28, + Expected: []byte("("), + }, + { + Rune: 0x29, + Expected: []byte(")"), + }, + { + Rune: 0x2A, + Expected: []byte("*"), + }, + { + Rune: 0x2B, + Expected: []byte("+"), + }, + { + Rune: 0x2C, + Expected: []byte(","), + }, + { + Rune: 0x2D, + Expected: []byte("-"), + }, + { + Rune: 0x2E, + Expected: []byte("."), + }, + { + Rune: 0x2F, + Expected: []byte("/"), + }, + { + Rune: 0x30, + Expected: []byte("0"), + }, + { + Rune: 0x31, + Expected: []byte("1"), + }, + { + Rune: 0x32, + Expected: []byte("2"), + }, + { + Rune: 0x33, + Expected: []byte("3"), + }, + { + Rune: 0x34, + Expected: []byte("4"), + }, + { + Rune: 0x35, + Expected: []byte("5"), + }, + { + Rune: 0x36, + Expected: []byte("6"), + }, + { + Rune: 0x37, + Expected: []byte("7"), + }, + { + Rune: 0x38, + Expected: []byte("8"), + }, + { + Rune: 0x39, + Expected: []byte("9"), + }, + { + Rune: 0x3A, + Expected: []byte(":"), + }, + { + Rune: 0x3B, + Expected: []byte(";"), + }, + { + Rune: 0x3C, + Expected: []byte("<"), + }, + { + Rune: 0x3D, + Expected: []byte("="), + }, + { + Rune: 0x3E, + Expected: []byte(">"), + }, + { + Rune: 0x3F, + Expected: []byte("?"), + }, + { + Rune: 0x40, + Expected: []byte("@"), + }, + { + Rune: 0x41, + Expected: []byte("A"), + }, + { + Rune: 0x42, + Expected: []byte("B"), + }, + { + Rune: 0x43, + Expected: []byte("C"), + }, + { + Rune: 0x44, + Expected: []byte("D"), + }, + { + Rune: 0x45, + Expected: []byte("E"), + }, + { + Rune: 0x46, + Expected: []byte("F"), + }, + { + Rune: 0x47, + Expected: []byte("G"), + }, + { + Rune: 0x48, + Expected: []byte("H"), + }, + { + Rune: 0x49, + Expected: []byte("I"), + }, + { + Rune: 0x4A, + Expected: []byte("J"), + }, + { + Rune: 0x4B, + Expected: []byte("K"), + }, + { + Rune: 0x4C, + Expected: []byte("L"), + }, + { + Rune: 0x4D, + Expected: []byte("M"), + }, + { + Rune: 0x4E, + Expected: []byte("N"), + }, + { + Rune: 0x4F, + Expected: []byte("O"), + }, + { + Rune: 0x50, + Expected: []byte("P"), + }, + { + Rune: 0x51, + Expected: []byte("Q"), + }, + { + Rune: 0x52, + Expected: []byte("R"), + }, + { + Rune: 0x53, + Expected: []byte("S"), + }, + { + Rune: 0x54, + Expected: []byte("T"), + }, + { + Rune: 0x55, + Expected: []byte("U"), + }, + { + Rune: 0x56, + Expected: []byte("V"), + }, + { + Rune: 0x57, + Expected: []byte("W"), + }, + { + Rune: 0x58, + Expected: []byte("X"), + }, + { + Rune: 0x59, + Expected: []byte("Y"), + }, + { + Rune: 0x5A, + Expected: []byte("Z"), + }, + { + Rune: 0x5B, + Expected: []byte("["), + }, + { + Rune: 0x5C, + Expected: []byte("\\"), + }, + { + Rune: 0x5D, + Expected: []byte("]"), + }, + { + Rune: 0x5E, + Expected: []byte("^"), + }, + { + Rune: 0x5F, + Expected: []byte("_"), + }, + { + Rune: 0x60, + Expected: []byte("`"), + }, + { + Rune: 0x61, + Expected: []byte("a"), + }, + { + Rune: 0x62, + Expected: []byte("b"), + }, + { + Rune: 0x63, + Expected: []byte("c"), + }, + { + Rune: 0x64, + Expected: []byte("d"), + }, + { + Rune: 0x65, + Expected: []byte("e"), + }, + { + Rune: 0x66, + Expected: []byte("f"), + }, + { + Rune: 0x67, + Expected: []byte("g"), + }, + { + Rune: 0x68, + Expected: []byte("h"), + }, + { + Rune: 0x69, + Expected: []byte("i"), + }, + { + Rune: 0x6A, + Expected: []byte("j"), + }, + { + Rune: 0x6B, + Expected: []byte("k"), + }, + { + Rune: 0x6C, + Expected: []byte("l"), + }, + { + Rune: 0x6D, + Expected: []byte("m"), + }, + { + Rune: 0x6E, + Expected: []byte("n"), + }, + { + Rune: 0x6F, + Expected: []byte("o"), + }, + { + Rune: 0x70, + Expected: []byte("p"), + }, + { + Rune: 0x71, + Expected: []byte("q"), + }, + { + Rune: 0x72, + Expected: []byte("r"), + }, + { + Rune: 0x73, + Expected: []byte("s"), + }, + { + Rune: 0x74, + Expected: []byte("t"), + }, + { + Rune: 0x75, + Expected: []byte("u"), + }, + { + Rune: 0x76, + Expected: []byte("v"), + }, + { + Rune: 0x77, + Expected: []byte("w"), + }, + { + Rune: 0x78, + Expected: []byte("x"), + }, + { + Rune: 0x79, + Expected: []byte("y"), + }, + { + Rune: 0x7A, + Expected: []byte("z"), + }, + { + Rune: 0x7B, + Expected: []byte("{"), + }, + { + Rune: 0x7C, + Expected: []byte("|"), + }, + { + Rune: 0x7D, + Expected: []byte("}"), + }, + { + Rune: 0x7E, + Expected: []byte("~"), + }, + { + Rune: 0x7F, + Expected: []byte(""), + }, + { + Rune: 0x80, + Expected: []byte("€"), + }, + { + Rune: 0x81, + Expected: []byte(""), + }, + { + Rune: 0x82, + Expected: []byte("‚"), + }, + { + Rune: 0x83, + Expected: []byte("ƒ"), + }, + { + Rune: 0x84, + Expected: []byte("„"), + }, + { + Rune: 0x85, + Expected: []byte("…"), + }, + { + Rune: 0x86, + Expected: []byte("†"), + }, + { + Rune: 0x87, + Expected: []byte("‡"), + }, + { + Rune: 0x88, + Expected: []byte("ˆ"), + }, + { + Rune: 0x89, + Expected: []byte("‰"), + }, + { + Rune: 0x8A, + Expected: []byte("Š"), + }, + { + Rune: 0x8B, + Expected: []byte("‹"), + }, + { + Rune: 0x8C, + Expected: []byte("Œ"), + }, + { + Rune: 0x8D, + Expected: []byte(""), + }, + { + Rune: 0x8E, + Expected: []byte("Ž"), + }, + { + Rune: 0x8F, + Expected: []byte(""), + }, + { + Rune: 0x90, + Expected: []byte(""), + }, + { + Rune: 0x91, + Expected: []byte("‘"), + }, + { + Rune: 0x92, + Expected: []byte("’"), + }, + { + Rune: 0x93, + Expected: []byte("“"), + }, + { + Rune: 0x94, + Expected: []byte("”"), + }, + { + Rune: 0x95, + Expected: []byte("•"), + }, + { + Rune: 0x96, + Expected: []byte("–"), + }, + { + Rune: 0x97, + Expected: []byte("—"), + }, + { + Rune: 0x98, + Expected: []byte("˜"), + }, + { + Rune: 0x99, + Expected: []byte("™"), + }, + { + Rune: 0x9A, + Expected: []byte("š"), + }, + { + Rune: 0x9B, + Expected: []byte("›"), + }, + { + Rune: 0x9C, + Expected: []byte("œ"), + }, + { + Rune: 0x9D, + Expected: []byte(""), + }, + { + Rune: 0x9E, + Expected: []byte("ž"), + }, + { + Rune: 0x9F, + Expected: []byte("Ÿ"), + }, + { + Rune: 0xA0, + Expected: []byte("\u00A0"), + }, + } + + for testNumber, test := range tests { + + var buffer bytes.Buffer + _, err := htmlescape.WriteRune(&buffer, test.Rune) + if nil != err { + t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber) + t.Logf("ERROR: (%T) %s", err, err) + t.Logf("RUNE: %q (%U)", test.Rune, test.Rune) + continue + } + + { + actual := buffer.Bytes() + expected := test.Expected + + if !bytes.Equal(expected, actual) { + t.Errorf("For test #%d, the actual resulting value of the append is not what was expected.", testNumber) + t.Logf("EXPECTED: %q", expected) + t.Logf("ACTUAL: %q", actual) + t.Logf("RUNE: %q (%U)", test.Rune, test.Rune) + continue + } + } + } +} diff --git a/writestring_test.go b/writestring_test.go new file mode 100644 index 0000000..7b1bf75 --- /dev/null +++ b/writestring_test.go @@ -0,0 +1,63 @@ +package htmlescape_test + +import ( + "testing" + + "bytes" + + "sourcecode.social/reiver/go-htmlescape" +) + +func TestWriteString(t *testing.T) { + + tests := []struct{ + String string + Expected []byte + }{ + { + String: "This is NOT bold!", + Expected: []byte("This is NOT <b>bold</b>!"), + }, + + + + { + String: `"this is a quotation"`, + Expected: []byte(""this is a quotation""), + }, + + + + { + String: "\t3 < 5\r\n3 > 2\x00", + Expected: []byte(" 3 < 5 3 > 2�"), + }, + } + + for testNumber, test := range tests { + + var buffer bytes.Buffer + + _, err := htmlescape.WriteString(&buffer, test.String) + + if nil != err { + t.Errorf("For test #%d, did not expect to get an error but actually got one." , testNumber) + t.Logf("ERROR: (%T) %s", err, err) + t.Logf("STRING: %q", test.String) + continue + } + + { + expected := test.Expected + actual := buffer.Bytes() + + if !bytes.Equal(expected, actual) { + t.Errorf("For test #%d, the actual resulting value of the append is not what was expected.", testNumber) + t.Logf("EXPECTED: %q", expected) + t.Logf("ACTUAL: %q", actual) + t.Logf("STRING: %q", test.String) + continue + } + } + } +}