lib/encoder: add EncodeRaw

This commit is contained in:
URenko 2024-04-22 13:21:13 +00:00 committed by Nick Craig-Wood
parent 846c1aeed0
commit 1b9217bc78
3 changed files with 46 additions and 36 deletions

View File

@ -36,7 +36,8 @@ const (
// Possible flags for the MultiEncoder // Possible flags for the MultiEncoder
const ( const (
EncodeZero MultiEncoder = 0 // NUL(0x00) EncodeZero MultiEncoder = 0 // NUL(0x00)
EncodeSlash MultiEncoder = 1 << iota // / EncodeRaw MultiEncoder = 1 << (iota - 1)
EncodeSlash // /
EncodeLtGt // <> EncodeLtGt // <>
EncodeDoubleQuote // " EncodeDoubleQuote // "
EncodeSingleQuote // ' EncodeSingleQuote // '
@ -117,6 +118,7 @@ func alias(name string, mask MultiEncoder) {
} }
func init() { func init() {
alias("Raw", EncodeRaw)
alias("None", EncodeZero) alias("None", EncodeZero)
alias("Slash", EncodeSlash) alias("Slash", EncodeSlash)
alias("LtGt", EncodeLtGt) alias("LtGt", EncodeLtGt)
@ -214,6 +216,10 @@ func (mask *MultiEncoder) Scan(s fmt.ScanState, ch rune) error {
// Encode takes a raw name and substitutes any reserved characters and // Encode takes a raw name and substitutes any reserved characters and
// patterns in it // patterns in it
func (mask MultiEncoder) Encode(in string) string { func (mask MultiEncoder) Encode(in string) string {
if mask == EncodeRaw {
return in
}
if in == "" { if in == "" {
return "" return ""
} }
@ -671,6 +677,10 @@ func (mask MultiEncoder) Encode(in string) string {
// Decode takes a name and undoes any substitutions made by Encode // Decode takes a name and undoes any substitutions made by Encode
func (mask MultiEncoder) Decode(in string) string { func (mask MultiEncoder) Decode(in string) string {
if mask == EncodeRaw {
return in
}
if mask.Has(EncodeDot) { if mask.Has(EncodeDot) {
switch in { switch in {
case "": case "":

View File

@ -22,7 +22,7 @@ func TestEncodeString(t *testing.T) {
mask MultiEncoder mask MultiEncoder
want string want string
}{ }{
{0, "None"}, {EncodeRaw, "Raw"},
{EncodeZero, "None"}, {EncodeZero, "None"},
{EncodeDoubleQuote, "DoubleQuote"}, {EncodeDoubleQuote, "DoubleQuote"},
{EncodeDot, "Dot"}, {EncodeDot, "Dot"},
@ -44,7 +44,7 @@ func TestEncodeSet(t *testing.T) {
wantErr bool wantErr bool
}{ }{
{"", 0, true}, {"", 0, true},
{"None", 0, false}, {"Raw", EncodeRaw, false},
{"None", EncodeZero, false}, {"None", EncodeZero, false},
{"DoubleQuote", EncodeDoubleQuote, false}, {"DoubleQuote", EncodeDoubleQuote, false},
{"Dot", EncodeDot, false}, {"Dot", EncodeDot, false},
@ -178,7 +178,7 @@ func TestEncodeInvalidUnicode(t *testing.T) {
func TestEncodeDot(t *testing.T) { func TestEncodeDot(t *testing.T) {
for i, tc := range []testCase{ for i, tc := range []testCase{
{ {
mask: 0, mask: EncodeZero,
in: ".", in: ".",
out: ".", out: ".",
}, { }, {
@ -186,7 +186,7 @@ func TestEncodeDot(t *testing.T) {
in: ".", in: ".",
out: "", out: "",
}, { }, {
mask: 0, mask: EncodeZero,
in: "..", in: "..",
out: "..", out: "..",
}, { }, {
@ -224,7 +224,7 @@ func TestDecodeHalf(t *testing.T) {
in: "", in: "",
out: "", out: "",
}, { }, {
mask: 0, mask: EncodeZero,
in: "", in: "",
out: "", out: "",
}, { }, {

View File

@ -230,7 +230,7 @@ func main() {
} }
in, out := buildTestString( in, out := buildTestString(
[]mapping{getMapping(m.mask)}, // pick []mapping{getMapping(m.mask)}, // pick
[]mapping{getMapping(0)}, // quote []mapping{getMapping(encoder.EncodeZero)}, // quote
printables, fullwidthPrintables, encodables, encoded, greek) // fill printables, fullwidthPrintables, encodables, encoded, greek) // fill
fatalW(fmt.Fprintf(fd, `{ // %d fatalW(fmt.Fprintf(fd, `{ // %d
mask: %s, mask: %s,
@ -262,7 +262,7 @@ var testCasesSingleEdge = []testCase{
for idx, orig := range e.orig { for idx, orig := range e.orig {
replace := e.replace[idx] replace := e.replace[idx]
pairs := buildEdgeTestString( pairs := buildEdgeTestString(
[]edge{e}, []mapping{getMapping(0), getMapping(m.mask)}, // quote []edge{e}, []mapping{getMapping(encoder.EncodeZero), getMapping(m.mask)}, // quote
[][]rune{printables, fullwidthPrintables, encodables, encoded, greek}, // fill [][]rune{printables, fullwidthPrintables, encodables, encoded, greek}, // fill
func(rIn, rOut []rune, quoteOut []bool, testMappings []mapping) (out []stringPair) { func(rIn, rOut []rune, quoteOut []bool, testMappings []mapping) (out []stringPair) {
testL := len(rIn) testL := len(rIn)
@ -386,7 +386,7 @@ var testCasesDoubleEdge = []testCase{
orig, replace := e1.orig[0], e1.replace[0] orig, replace := e1.orig[0], e1.replace[0]
edges := []edge{e1, e2} edges := []edge{e1, e2}
pairs := buildEdgeTestString( pairs := buildEdgeTestString(
edges, []mapping{getMapping(0), getMapping(m.mask)}, // quote edges, []mapping{getMapping(encoder.EncodeZero), getMapping(m.mask)}, // quote
[][]rune{printables, fullwidthPrintables, encodables, encoded, greek}, // fill [][]rune{printables, fullwidthPrintables, encodables, encoded, greek}, // fill
func(rIn, rOut []rune, quoteOut []bool, testMappings []mapping) (out []stringPair) { func(rIn, rOut []rune, quoteOut []bool, testMappings []mapping) (out []stringPair) {
testL := len(rIn) testL := len(rIn)