From bf6d657b20baf1b2c38e996a3499875e485e5b67 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Sat, 9 Dec 2023 09:30:21 -0800 Subject: [PATCH] initial commits --- sp/skip_tolerant.go | 17 +++++ sp/skip_tolerant_test.go | 154 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 sp/skip_tolerant.go create mode 100644 sp/skip_tolerant_test.go diff --git a/sp/skip_tolerant.go b/sp/skip_tolerant.go new file mode 100644 index 0000000..bef345f --- /dev/null +++ b/sp/skip_tolerant.go @@ -0,0 +1,17 @@ +package sp + +func SkipTolerant(p []byte) []byte { + length := len(p) + + if length <= 0 { + return nil + } + + for i,b := range p { + if !ByteIsTolerant(b) { + return p[i:] + } + } + + return nil +} diff --git a/sp/skip_tolerant_test.go b/sp/skip_tolerant_test.go new file mode 100644 index 0000000..78f5653 --- /dev/null +++ b/sp/skip_tolerant_test.go @@ -0,0 +1,154 @@ +package sp_test + +import ( + "testing" + + "bytes" + + "sourcecode.social/reiver/go-rfc2616/sp" +) + +func TestSkipTolerant(t *testing.T) { + + tests := []struct{ + Value []byte + Expected []byte + }{ + { + Value: nil, + Expected: nil, + }, + { + Value: []byte(nil), + Expected: nil, + }, + + + + { + Value: []byte{}, + Expected: nil, + }, + { + Value: []byte(""), + Expected: nil, + }, + + + + { + Value: []byte(" "), + Expected: nil, + }, + { + Value: []byte(" "), + Expected: nil, + }, + { + Value: []byte(" "), + Expected: nil, + }, + { + Value: []byte(" "), + Expected: nil, + }, + { + Value: []byte(" "), + Expected: nil, + }, + { + Value: []byte(" "), + Expected: nil, + }, + { + Value: []byte(" "), + Expected: nil, + }, + + { + Value: []byte(" "), + Expected: nil, + }, + + + + { + Value: []byte("\t"), + Expected: nil, + }, + { + Value: []byte("\t\t"), + Expected: nil, + }, + { + Value: []byte("\t\t\t"), + Expected: nil, + }, + { + Value: []byte("\t\t\t\t"), + Expected: nil, + }, + { + Value: []byte("\t\t\t\t\t"), + Expected: nil, + }, + { + Value: []byte("\t\t\t\t\t\t"), + Expected: nil, + }, + { + Value: []byte("\t\t\t\t\t\t\t"), + Expected: nil, + }, + + { + Value: []byte("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), + Expected: nil, + }, + + + + { + Value: []byte("\t \t \t \t\t"), + Expected: nil, + }, + + + + { + Value: []byte(" apple "), + Expected: []byte("apple "), + }, + { + Value: []byte("\tbanana"), + Expected: []byte("banana"), + }, + { + Value: []byte("\t\t cherry\r\n"), + Expected: []byte("cherry\r\n"), + }, + + + + { + Value: []byte(" \t\t 😈 :-)\r\n"), + Expected: []byte("😈 :-)\r\n"), + }, + } + + for testNumber, test := range tests { + + actual := sp.SkipTolerant(test.Value) + + expected := test.Expected + + if !bytes.Equal(expected, actual) { + t.Errorf("For test #%d, the actual result is not what was 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 + } + + } +}