From a421524de59a968112486a0d64917bef6ce80a81 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Fri, 6 Oct 2023 09:28:51 -0700 Subject: [PATCH] initial commits --- unreserved.go | 25 ++++++++++++++++++++++ unreserved_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 unreserved.go create mode 100644 unreserved_test.go diff --git a/unreserved.go b/unreserved.go new file mode 100644 index 0000000..98f9be6 --- /dev/null +++ b/unreserved.go @@ -0,0 +1,25 @@ +package rfc3986 + +import ( + "sourcecode.social/reiver/go-rfc2234" +) + +// IsUnreserved returns true if the value of 'r' matches 'unreserved' as defined in IETF RFC-3986: +// +// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" +func IsUnreserved(r rune) bool { + if rfc2234.IsAlpha(r) { + return true + } + + if rfc2234.IsDigit(r) { + return true + } + + switch r { + case '-' , '.' , '_' , '~': + return true + } + + return false +} diff --git a/unreserved_test.go b/unreserved_test.go new file mode 100644 index 0000000..5e7fae8 --- /dev/null +++ b/unreserved_test.go @@ -0,0 +1,52 @@ +package rfc3986_test + +import ( + "testing" + + "sourcecode.social/reiver/go-rfc2234" + + "sourcecode.social/reiver/go-rfc3986" +) + +func TestIsUnreserved(t *testing.T) { + + tests := []struct{ + Rune rune + Expected bool + }{ + } + + for r:=rune(0); r < rune(8192); r++ { + test := struct{ + Rune rune + Expected bool + }{ + Rune: r, + Expected: false, + } + + if rfc2234.IsAlpha(r) || + rfc2234.IsDigit(r) || + '-' == r || + '.' == r || + '_' == r || + '~' == r { + test.Expected = true + } + + tests = append(tests, test) + } + + for testNumber, test := range tests { + actual := rfc3986.IsUnreserved(test.Rune) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value for rfc3986.Unreserved() is not what was expected.", testNumber) + t.Logf("EXPECTED: %t", expected) + t.Logf("ACTUAL: %t", actual) + t.Logf("RUNE: (%U) %q", test.Rune, string(test.Rune)) + continue + } + } +}