From 1b9217bc78318f3db386908f08fe83b22ae8578a Mon Sep 17 00:00:00 2001 From: URenko <18209292+URenko@users.noreply.github.com> Date: Mon, 22 Apr 2024 13:21:13 +0000 Subject: [PATCH] lib/encoder: add EncodeRaw --- lib/encoder/encoder.go | 66 ++++++++++++++++++-------------- lib/encoder/encoder_test.go | 10 ++--- lib/encoder/internal/gen/main.go | 6 +-- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/lib/encoder/encoder.go b/lib/encoder/encoder.go index a14d7c848..264757cd8 100644 --- a/lib/encoder/encoder.go +++ b/lib/encoder/encoder.go @@ -35,34 +35,35 @@ const ( // Possible flags for the MultiEncoder const ( - EncodeZero MultiEncoder = 0 // NUL(0x00) - EncodeSlash MultiEncoder = 1 << iota // / - EncodeLtGt // <> - EncodeDoubleQuote // " - EncodeSingleQuote // ' - EncodeBackQuote // ` - EncodeDollar // $ - EncodeColon // : - EncodeQuestion // ? - EncodeAsterisk // * - EncodePipe // | - EncodeHash // # - EncodePercent // % - EncodeBackSlash // \ - EncodeCrLf // CR(0x0D), LF(0x0A) - EncodeDel // DEL(0x7F) - EncodeCtl // CTRL(0x01-0x1F) - EncodeLeftSpace // Leading SPACE - EncodeLeftPeriod // Leading . - EncodeLeftTilde // Leading ~ - EncodeLeftCrLfHtVt // Leading CR LF HT VT - EncodeRightSpace // Trailing SPACE - EncodeRightPeriod // Trailing . - EncodeRightCrLfHtVt // Trailing CR LF HT VT - EncodeInvalidUtf8 // Invalid UTF-8 bytes - EncodeDot // . and .. names - EncodeSquareBracket // [] - EncodeSemicolon // ; + EncodeZero MultiEncoder = 0 // NUL(0x00) + EncodeRaw MultiEncoder = 1 << (iota - 1) + EncodeSlash // / + EncodeLtGt // <> + EncodeDoubleQuote // " + EncodeSingleQuote // ' + EncodeBackQuote // ` + EncodeDollar // $ + EncodeColon // : + EncodeQuestion // ? + EncodeAsterisk // * + EncodePipe // | + EncodeHash // # + EncodePercent // % + EncodeBackSlash // \ + EncodeCrLf // CR(0x0D), LF(0x0A) + EncodeDel // DEL(0x7F) + EncodeCtl // CTRL(0x01-0x1F) + EncodeLeftSpace // Leading SPACE + EncodeLeftPeriod // Leading . + EncodeLeftTilde // Leading ~ + EncodeLeftCrLfHtVt // Leading CR LF HT VT + EncodeRightSpace // Trailing SPACE + EncodeRightPeriod // Trailing . + EncodeRightCrLfHtVt // Trailing CR LF HT VT + EncodeInvalidUtf8 // Invalid UTF-8 bytes + EncodeDot // . and .. names + EncodeSquareBracket // [] + EncodeSemicolon // ; // Synthetic EncodeWin = EncodeColon | EncodeQuestion | EncodeDoubleQuote | EncodeAsterisk | EncodeLtGt | EncodePipe // :?"*<>| @@ -117,6 +118,7 @@ func alias(name string, mask MultiEncoder) { } func init() { + alias("Raw", EncodeRaw) alias("None", EncodeZero) alias("Slash", EncodeSlash) 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 // patterns in it func (mask MultiEncoder) Encode(in string) string { + if mask == EncodeRaw { + return in + } + if in == "" { return "" } @@ -671,6 +677,10 @@ func (mask MultiEncoder) Encode(in string) string { // Decode takes a name and undoes any substitutions made by Encode func (mask MultiEncoder) Decode(in string) string { + if mask == EncodeRaw { + return in + } + if mask.Has(EncodeDot) { switch in { case ".": diff --git a/lib/encoder/encoder_test.go b/lib/encoder/encoder_test.go index 61f4b73ef..b36339824 100644 --- a/lib/encoder/encoder_test.go +++ b/lib/encoder/encoder_test.go @@ -22,7 +22,7 @@ func TestEncodeString(t *testing.T) { mask MultiEncoder want string }{ - {0, "None"}, + {EncodeRaw, "Raw"}, {EncodeZero, "None"}, {EncodeDoubleQuote, "DoubleQuote"}, {EncodeDot, "Dot"}, @@ -44,7 +44,7 @@ func TestEncodeSet(t *testing.T) { wantErr bool }{ {"", 0, true}, - {"None", 0, false}, + {"Raw", EncodeRaw, false}, {"None", EncodeZero, false}, {"DoubleQuote", EncodeDoubleQuote, false}, {"Dot", EncodeDot, false}, @@ -178,7 +178,7 @@ func TestEncodeInvalidUnicode(t *testing.T) { func TestEncodeDot(t *testing.T) { for i, tc := range []testCase{ { - mask: 0, + mask: EncodeZero, in: ".", out: ".", }, { @@ -186,7 +186,7 @@ func TestEncodeDot(t *testing.T) { in: ".", out: ".", }, { - mask: 0, + mask: EncodeZero, in: "..", out: "..", }, { @@ -224,7 +224,7 @@ func TestDecodeHalf(t *testing.T) { in: "‛", out: "‛", }, { - mask: 0, + mask: EncodeZero, in: "‛‛", out: "‛", }, { diff --git a/lib/encoder/internal/gen/main.go b/lib/encoder/internal/gen/main.go index 97bcb32ba..23e3777e2 100644 --- a/lib/encoder/internal/gen/main.go +++ b/lib/encoder/internal/gen/main.go @@ -230,7 +230,7 @@ func main() { } in, out := buildTestString( []mapping{getMapping(m.mask)}, // pick - []mapping{getMapping(0)}, // quote + []mapping{getMapping(encoder.EncodeZero)}, // quote printables, fullwidthPrintables, encodables, encoded, greek) // fill fatalW(fmt.Fprintf(fd, `{ // %d mask: %s, @@ -262,7 +262,7 @@ var testCasesSingleEdge = []testCase{ for idx, orig := range e.orig { replace := e.replace[idx] 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 func(rIn, rOut []rune, quoteOut []bool, testMappings []mapping) (out []stringPair) { testL := len(rIn) @@ -386,7 +386,7 @@ var testCasesDoubleEdge = []testCase{ orig, replace := e1.orig[0], e1.replace[0] edges := []edge{e1, e2} 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 func(rIn, rOut []rune, quoteOut []bool, testMappings []mapping) (out []stringPair) { testL := len(rIn)