diff --git a/gendelims.go b/gendelims.go new file mode 100644 index 0000000..9e0f1fc --- /dev/null +++ b/gendelims.go @@ -0,0 +1,13 @@ +package rfc3986 + +// IsGenDelim returns true if the value of 'r' matches 'sub-delims' as defined in IETF RFC-3986: +// +// gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" +func IsGenDelim(r rune) bool { + switch r { + case ':' , '/' , '?' , '#' , '[' , ']' , '@': + return true + default: + return false + } +} diff --git a/gendelims_test.go b/gendelims_test.go new file mode 100644 index 0000000..005e92e --- /dev/null +++ b/gendelims_test.go @@ -0,0 +1,51 @@ +package rfc3986_test + +import ( + "testing" + + "sourcecode.social/reiver/go-rfc3986" +) + +func TestIsGenDelim(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 ':' == r || + '/' == r || + '?' == r || + '#' == r || + '[' == r || + ']' == r || + '@' == r { + test.Expected = true + } + + tests = append(tests, test) + } + + for testNumber, test := range tests { + actual := rfc3986.IsGenDelim(test.Rune) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value for rfc3986.IsGenDelim() 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 + } + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5fbc7eb --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module sourcecode.social/reiver/go-rfc3986 + +go 1.20 diff --git a/reserved.go b/reserved.go new file mode 100644 index 0000000..4c5839f --- /dev/null +++ b/reserved.go @@ -0,0 +1,12 @@ +package rfc3986 + +// IsReserved returns true if value of 'r' matches 'reserved' as defined in IETF RFC-3986: +// +// reserved = gen-delims / sub-delims +// +// gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" +// +// sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" +func IsReserved(r rune) bool { + return IsGenDelim(r) || IsSubDelim(r) +} diff --git a/subdelims.go b/subdelims.go new file mode 100644 index 0000000..0ac18b4 --- /dev/null +++ b/subdelims.go @@ -0,0 +1,13 @@ +package rfc3986 + +// IsSubDelim returns true if the value of 'r' matches 'sub-delims' as defined in IETF RFC-3986: +// +// sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" +func IsSubDelim(r rune) bool { + switch r { + case '!' , '$' , '&' ,'\'' , '(' , ')' , '*' , '+' , ',' , ';' , '=': + return true + default: + return false + } +} diff --git a/subdelims_test.go b/subdelims_test.go new file mode 100644 index 0000000..701e221 --- /dev/null +++ b/subdelims_test.go @@ -0,0 +1,55 @@ +package rfc3986_test + +import ( + "testing" + + "sourcecode.social/reiver/go-rfc3986" +) + +func TestIsSubDelim(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 '!' == r || + '$' == r || + '&' == r || + '\'' == r || + '(' == r || + ')' == r || + '*' == r || + '+' == r || + ',' == r || + ';' == r || + '=' == r { + test.Expected = true + } + + tests = append(tests, test) + } + + for testNumber, test := range tests { + actual := rfc3986.IsSubDelim(test.Rune) + expected := test.Expected + + if expected != actual { + t.Errorf("For test #%d, the actual value for rfc3986.IsSubDelim() 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 + } + } +}