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

@ -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 "":

View file

@ -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: "",
}, {

View file

@ -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)