1
mirror of https://github.com/rclone/rclone synced 2024-12-26 18:23:45 +01:00

encoder: add option to encode . and .. names

This commit is contained in:
Fabian Möller 2019-04-30 11:07:02 +02:00 committed by Nick Craig-Wood
parent 6c0a749a42
commit d5cd026547
2 changed files with 71 additions and 0 deletions

View File

@ -92,8 +92,22 @@ func (mask MultiEncoder) Encode(in string) string {
encodeRightSpace = uint(mask)&EncodeRightSpace != 0 encodeRightSpace = uint(mask)&EncodeRightSpace != 0
encodeRightPeriod = uint(mask)&EncodeRightPeriod != 0 encodeRightPeriod = uint(mask)&EncodeRightPeriod != 0
encodeInvalidUnicode = uint(mask)&EncodeInvalidUtf8 != 0 encodeInvalidUnicode = uint(mask)&EncodeInvalidUtf8 != 0
encodeDot = uint(mask)&EncodeDot != 0
) )
if encodeDot {
switch in {
case ".":
return ""
case "..":
return ""
case "":
return string(QuoteRune) + ""
case "":
return string(QuoteRune) + "" + string(QuoteRune) + ""
}
}
// handle prefix only replacements // handle prefix only replacements
prefix := "" prefix := ""
if encodeLeftSpace && len(in) > 0 { // Leading SPACE if encodeLeftSpace && len(in) > 0 { // Leading SPACE
@ -299,8 +313,22 @@ func (mask MultiEncoder) Decode(in string) string {
encodeRightSpace = uint(mask)&EncodeRightSpace != 0 encodeRightSpace = uint(mask)&EncodeRightSpace != 0
encodeRightPeriod = uint(mask)&EncodeRightPeriod != 0 encodeRightPeriod = uint(mask)&EncodeRightPeriod != 0
encodeInvalidUnicode = uint(mask)&EncodeInvalidUtf8 != 0 encodeInvalidUnicode = uint(mask)&EncodeInvalidUtf8 != 0
encodeDot = uint(mask)&EncodeDot != 0
) )
if encodeDot {
switch in {
case "":
return "."
case "":
return ".."
case string(QuoteRune) + "":
return ""
case string(QuoteRune) + "" + string(QuoteRune) + "":
return ""
}
}
// handle prefix only replacements // handle prefix only replacements
prefix := "" prefix := ""
if r, l1 := utf8.DecodeRuneInString(in); encodeLeftSpace && r == '␠' { // SYMBOL FOR SPACE if r, l1 := utf8.DecodeRuneInString(in); encodeLeftSpace && r == '␠' { // SYMBOL FOR SPACE

View File

@ -102,6 +102,49 @@ func TestEncodeInvalidUnicode(t *testing.T) {
}) })
} }
} }
func TestEncodeDot(t *testing.T) {
for i, tc := range []testCase{
{
mask: 0,
in: ".",
out: ".",
}, {
mask: EncodeDot,
in: ".",
out: "",
}, {
mask: 0,
in: "..",
out: "..",
}, {
mask: EncodeDot,
in: "..",
out: "",
}, {
mask: EncodeDot,
in: "...",
out: "...",
}, {
mask: EncodeDot,
in: ". .",
out: ". .",
},
} {
e := MultiEncoder(tc.mask)
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) {
got := e.Encode(tc.in)
if got != tc.out {
t.Errorf("Encode(%q) want %q got %q", tc.in, tc.out, got)
}
got2 := e.Decode(got)
if got2 != tc.in {
t.Errorf("Decode(%q) want %q got %q", got, tc.in, got2)
}
})
}
}
func TestDecodeHalf(t *testing.T) { func TestDecodeHalf(t *testing.T) {
for i, tc := range []testCase{ for i, tc := range []testCase{
{ {