From 3916347130f07b509f8b5b01899ed740a14121cb Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Sat, 9 Dec 2023 10:40:10 -0800 Subject: [PATCH] initial commits --- eol/bytes_tolerant.go | 36 ++++++++++ eol/bytes_tolerant_test.go | 130 +++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 eol/bytes_tolerant.go create mode 100644 eol/bytes_tolerant_test.go diff --git a/eol/bytes_tolerant.go b/eol/bytes_tolerant.go new file mode 100644 index 0000000..6ba0cc3 --- /dev/null +++ b/eol/bytes_tolerant.go @@ -0,0 +1,36 @@ +package eol + +func BytesTolerant(p []byte) (result []byte, rest []byte, ok bool) { + + length := len(p) + + if length <= 0 { + return nil, p, false + } + + var p0 byte = p[0] + + switch p0 { + case '\n': + return p[:1], p[1:], true + case '\r': + // Nothing here. + default: + return nil, p, false + } + + // If we got here then p0 == '\r' + + if length < 2 { + return nil, p, false + } + + var p1 byte = p[1] + + switch p1 { + case '\n': + return p[:2], p[2:], true + default: + return nil, p, false + } +} diff --git a/eol/bytes_tolerant_test.go b/eol/bytes_tolerant_test.go new file mode 100644 index 0000000..d40e50d --- /dev/null +++ b/eol/bytes_tolerant_test.go @@ -0,0 +1,130 @@ +package eol_test + +import ( + "testing" + + "bytes" + + "sourcecode.social/reiver/go-rfc2616/eol" +) + +func TestBytesTolerant(t *testing.T) { + + tests := []struct{ + Value []byte + ExpectedResult []byte + ExpectedRest []byte + ExpectedOK bool + }{ + { + Value: nil, + ExpectedResult: nil, + ExpectedRest: nil, + ExpectedOK: false, + }, + { + Value: []byte(nil), + ExpectedResult: nil, + ExpectedRest: nil, + ExpectedOK: false, + }, + + + + { + Value: []byte{}, + ExpectedResult: nil, + ExpectedRest: nil, + ExpectedOK: false, + }, + { + Value: []byte(""), + ExpectedResult: nil, + ExpectedRest: nil, + ExpectedOK: false, + }, + + + + { + Value : []byte("\n"), + ExpectedResult: []byte("\n"), + ExpectedRest: nil, + ExpectedOK: true, + }, + { + Value : []byte("\r\n"), + ExpectedResult: []byte("\r\n"), + ExpectedRest: nil, + ExpectedOK: true, + }, + + + + { + Value : []byte("\nHost: example.com\n"), + ExpectedResult: []byte("\n"), + ExpectedRest: []byte("Host: example.com\n"), + ExpectedOK: true, + }, + { + Value : []byte("\r\nHost: example.com\r\n"), + ExpectedResult: []byte("\r\n"), + ExpectedRest: []byte("Host: example.com\r\n"), + ExpectedOK: true, + }, + + + + { + Value: []byte("\r"), + ExpectedResult: nil, + ExpectedRest: []byte("\r"), + ExpectedOK: false, + }, + } + + for testNumber, test := range tests { + + actualResult, actualRest, actualOK := eol.BytesTolerant(test.Value) + + { + expected := test.ExpectedOK + actual := actualOK + + if expected != actual { + t.Errorf("For test #%d, the actual ok-result is not what wad expected.", testNumber) + t.Logf("EXPECTED: %t", expected) + t.Logf("ACTUAL: %t", actual) + t.Logf("VALUE: %q (%#v)", test.Value, test.Value) + continue + } + } + + { + expected := test.ExpectedResult + actual := actualResult + + if !bytes.Equal(expected, actual) { + t.Errorf("For test #%d, the actual result is not what wad expected.", testNumber) + t.Logf("EXPECTED: %q (%#v)", expected, expected) + t.Logf("ACTUAL: %q (%#v)", actual, actual) + t.Logf("VALUE: %q (%#v)", test.Value, test.Value) + continue + } + } + + { + expected := test.ExpectedRest + actual := actualRest + + if !bytes.Equal(expected, actual) { + t.Errorf("For test #%d, the actual rest is not what wad expected.", testNumber) + t.Logf("EXPECTED: %q (%#v)", expected, expected) + t.Logf("ACTUAL: %q (%#v)", actual, actual) + t.Logf("VALUE: %q (%#v)", test.Value, test.Value) + continue + } + } + } +}