From af7b0bde141096f79b63a1055c82eb53977dd584 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Mon, 11 Dec 2023 07:23:35 -0800 Subject: [PATCH] initial commits --- sp/bytes.go | 23 ++++++ sp/bytes_test.go | 166 ++++++++++++++++++++++++++++++++++++++ sp/bytes_tolerant.go | 23 ++++++ sp/bytes_tolerant_test.go | 166 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 378 insertions(+) create mode 100644 sp/bytes.go create mode 100644 sp/bytes_test.go create mode 100644 sp/bytes_tolerant.go create mode 100644 sp/bytes_tolerant_test.go diff --git a/sp/bytes.go b/sp/bytes.go new file mode 100644 index 0000000..baf0332 --- /dev/null +++ b/sp/bytes.go @@ -0,0 +1,23 @@ +package sp + +func Bytes(p []byte) (result []byte, rest []byte, ok bool) { + if len(p) <= 0 { + return nil, nil, false + } + + { + p0 := p[0] + + if !ByteIs(p0) { + return nil, p, false + } + } + + for i,b := range p { + if !ByteIs(b) { + return p[:i], p[i:], true + } + } + + return p, nil, true +} diff --git a/sp/bytes_test.go b/sp/bytes_test.go new file mode 100644 index 0000000..a50d2f2 --- /dev/null +++ b/sp/bytes_test.go @@ -0,0 +1,166 @@ +package sp_test + +import ( + "testing" + + "bytes" + + "sourcecode.social/reiver/go-rfc2616/sp" +) + +func TestBytes(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("apple banana cherry"), + ExpectedResult: nil, + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: false, + }, + + { + Value: []byte(" apple banana cherry"), + ExpectedResult: []byte(" "), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + { + Value: []byte(" apple banana cherry"), + ExpectedResult: []byte(" "), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + { + Value: []byte(" apple banana cherry"), + ExpectedResult: []byte(" "), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + { + Value: []byte(" apple banana cherry"), + ExpectedResult: []byte(" "), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + + { + Value: []byte("\tapple banana cherry"), + ExpectedResult: nil, + ExpectedRest: []byte("\tapple banana cherry"), + ExpectedOK: false, + }, + { + Value: []byte("\t\tapple banana cherry"), + ExpectedResult: nil, + ExpectedRest: []byte("\t\tapple banana cherry"), + ExpectedOK: false, + }, + { + Value: []byte("\t\t\tapple banana cherry"), + ExpectedResult: nil, + ExpectedRest: []byte("\t\t\tapple banana cherry"), + ExpectedOK: false, + }, + { + Value: []byte("\t\t\t\tapple banana cherry"), + ExpectedResult: nil, + ExpectedRest: []byte("\t\t\t\tapple banana cherry"), + ExpectedOK: false, + }, + + + + { + Value: []byte("\t\t apple banana cherry"), + ExpectedResult: nil, + ExpectedRest: []byte("\t\t apple banana cherry"), + ExpectedOK: false, + }, + { + Value: []byte(" \t\t\t\t\tapple banana cherry"), + ExpectedResult: []byte(" "), + ExpectedRest: []byte("\t\t\t\t\tapple banana cherry"), + ExpectedOK: true, + }, + } + + for testNumber, test := range tests { + + actualResult, actualRest, actualOK := sp.Bytes(test.Value) + + { + expected := test.ExpectedOK + actual := actualOK + + if expected != actual { + t.Errorf("For test #%d, the actual ok-result is not what was 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 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 + } + } + + { + expected := test.ExpectedRest + actual := actualRest + + 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 + } + } + + } +} diff --git a/sp/bytes_tolerant.go b/sp/bytes_tolerant.go new file mode 100644 index 0000000..9fcc6cf --- /dev/null +++ b/sp/bytes_tolerant.go @@ -0,0 +1,23 @@ +package sp + +func BytesTolerant(p []byte) (result []byte, rest []byte, ok bool) { + if len(p) <= 0 { + return nil, nil, false + } + + { + p0 := p[0] + + if !ByteIsTolerant(p0) { + return nil, p, false + } + } + + for i,b := range p { + if !ByteIsTolerant(b) { + return p[:i], p[i:], true + } + } + + return p, nil, true +} diff --git a/sp/bytes_tolerant_test.go b/sp/bytes_tolerant_test.go new file mode 100644 index 0000000..d8121c1 --- /dev/null +++ b/sp/bytes_tolerant_test.go @@ -0,0 +1,166 @@ +package sp_test + +import ( + "testing" + + "bytes" + + "sourcecode.social/reiver/go-rfc2616/sp" +) + +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("apple banana cherry"), + ExpectedResult: nil, + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: false, + }, + + { + Value: []byte(" apple banana cherry"), + ExpectedResult: []byte(" "), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + { + Value: []byte(" apple banana cherry"), + ExpectedResult: []byte(" "), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + { + Value: []byte(" apple banana cherry"), + ExpectedResult: []byte(" "), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + { + Value: []byte(" apple banana cherry"), + ExpectedResult: []byte(" "), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + + { + Value: []byte("\tapple banana cherry"), + ExpectedResult: []byte("\t"), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + { + Value: []byte("\t\tapple banana cherry"), + ExpectedResult: []byte("\t\t"), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + { + Value: []byte("\t\t\tapple banana cherry"), + ExpectedResult: []byte("\t\t\t"), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + { + Value: []byte("\t\t\t\tapple banana cherry"), + ExpectedResult: []byte("\t\t\t\t"), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + + + + { + Value: []byte("\t\t apple banana cherry"), + ExpectedResult: []byte("\t\t "), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + { + Value: []byte(" \t\t\t\t\tapple banana cherry"), + ExpectedResult: []byte(" \t\t\t\t\t"), + ExpectedRest: []byte("apple banana cherry"), + ExpectedOK: true, + }, + } + + for testNumber, test := range tests { + + actualResult, actualRest, actualOK := sp.BytesTolerant(test.Value) + + { + expected := test.ExpectedOK + actual := actualOK + + if expected != actual { + t.Errorf("For test #%d, the actual ok-result is not what was 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 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 + } + } + + { + expected := test.ExpectedRest + actual := actualRest + + 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 + } + } + + } +}