1
0
mirror of https://github.com/rclone/rclone.git synced 2026-01-07 19:13:19 +00:00

lib/encoder: move definitions here and remove uint casts

This commit is contained in:
Nick Craig-Wood
2020-01-14 21:22:02 +00:00
parent 3c620d521d
commit c555dc71c2
5 changed files with 172 additions and 200 deletions

View File

@@ -37,32 +37,32 @@ const (
// Possible flags for the MultiEncoder
const (
EncodeZero uint = 0 // NUL(0x00)
EncodeSlash uint = 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
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
// Synthetic
EncodeWin = EncodeColon | EncodeQuestion | EncodeDoubleQuote | EncodeAsterisk | EncodeLtGt | EncodePipe // :?"*<>|
@@ -70,8 +70,8 @@ const (
)
// Has returns true if flag is contained in mask
func (mask MultiEncoder) Has(flag uint) bool {
return uint(mask)&flag != 0
func (mask MultiEncoder) Has(flag MultiEncoder) bool {
return mask&flag != 0
}
// Encoder can transform names to and from the original and translated version.

View File

@@ -19,7 +19,7 @@ var (
func TestEncodeString(t *testing.T) {
for _, test := range []struct {
mask uint
mask MultiEncoder
want string
}{
{0, "None"},
@@ -31,7 +31,7 @@ func TestEncodeString(t *testing.T) {
{EncodeSlash | EncodeDollar | EncodeColon, "Slash,Dollar,Colon"},
{EncodeSlash | (1 << 31), "Slash,0x80000000"},
} {
got := MultiEncoder(test.mask).String()
got := test.mask.String()
assert.Equal(t, test.want, got)
}
@@ -40,7 +40,7 @@ func TestEncodeString(t *testing.T) {
func TestEncodeSet(t *testing.T) {
for _, test := range []struct {
in string
want uint
want MultiEncoder
wantErr bool
}{
{"", 0, true},
@@ -58,20 +58,20 @@ func TestEncodeSet(t *testing.T) {
var got MultiEncoder
err := got.Set(test.in)
assert.Equal(t, test.wantErr, err != nil, err)
assert.Equal(t, MultiEncoder(test.want), got, test.in)
assert.Equal(t, test.want, got, test.in)
}
}
type testCase struct {
mask uint
mask MultiEncoder
in string
out string
}
func TestEncodeSingleMask(t *testing.T) {
for i, tc := range testCasesSingle {
e := MultiEncoder(tc.mask)
e := tc.mask
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) {
got := e.Encode(tc.in)
if got != tc.out {
@@ -87,7 +87,7 @@ func TestEncodeSingleMask(t *testing.T) {
func TestEncodeSingleMaskEdge(t *testing.T) {
for i, tc := range testCasesSingleEdge {
e := MultiEncoder(tc.mask)
e := tc.mask
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) {
got := e.Encode(tc.in)
if got != tc.out {
@@ -103,7 +103,7 @@ func TestEncodeSingleMaskEdge(t *testing.T) {
func TestEncodeDoubleMaskEdge(t *testing.T) {
for i, tc := range testCasesDoubleEdge {
e := MultiEncoder(tc.mask)
e := tc.mask
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) {
got := e.Encode(tc.in)
if got != tc.out {
@@ -161,7 +161,7 @@ func TestEncodeInvalidUnicode(t *testing.T) {
out: "a\xBF\xFEb",
},
} {
e := MultiEncoder(tc.mask)
e := tc.mask
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) {
got := e.Encode(tc.in)
if got != tc.out {
@@ -203,7 +203,7 @@ func TestEncodeDot(t *testing.T) {
out: ". .",
},
} {
e := MultiEncoder(tc.mask)
e := tc.mask
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) {
got := e.Encode(tc.in)
if got != tc.out {
@@ -245,7 +245,7 @@ func TestDecodeHalf(t *testing.T) {
out: "aB\\Eg",
},
} {
e := MultiEncoder(tc.mask)
e := tc.mask
t.Run(strconv.FormatInt(int64(i), 10), func(t *testing.T) {
got := e.Decode(tc.in)
if got != tc.out {
@@ -255,16 +255,15 @@ func TestDecodeHalf(t *testing.T) {
}
}
const oneDrive = MultiEncoder(
uint(Standard) |
EncodeWin |
EncodeBackSlash |
EncodeHashPercent |
EncodeDel |
EncodeCtl |
EncodeLeftTilde |
EncodeRightSpace |
EncodeRightPeriod)
const oneDrive = (Standard |
EncodeWin |
EncodeBackSlash |
EncodeHashPercent |
EncodeDel |
EncodeCtl |
EncodeLeftTilde |
EncodeRightSpace |
EncodeRightPeriod)
var benchTests = []struct {
in string

View File

@@ -20,7 +20,7 @@ const (
)
type mapping struct {
mask uint
mask encoder.MultiEncoder
src, dst []rune
}
type stringPair struct {
@@ -36,7 +36,7 @@ package encoder
`
var maskBits = []struct {
mask uint
mask encoder.MultiEncoder
name string
}{
{encoder.EncodeZero, "EncodeZero"},
@@ -68,7 +68,7 @@ var maskBits = []struct {
}
type edge struct {
mask uint
mask encoder.MultiEncoder
name string
edge int
orig []rune
@@ -429,7 +429,7 @@ func fatalW(_ int, err error) func(...interface{}) {
return func(s ...interface{}) {}
}
func invalidMask(mask uint) bool {
func invalidMask(mask encoder.MultiEncoder) bool {
return mask&(encoder.EncodeCtl|encoder.EncodeCrLf) != 0 && mask&(encoder.EncodeLeftCrLfHtVt|encoder.EncodeRightCrLfHtVt) != 0
}
@@ -445,7 +445,7 @@ func runeRange(l, h rune) []rune {
return out
}
func getMapping(mask uint) mapping {
func getMapping(mask encoder.MultiEncoder) mapping {
for _, m := range allMappings {
if m.mask == mask {
return m

View File

@@ -7,9 +7,16 @@ package encoder
// List of replaced characters:
// (0x00) -> '␀' // SYMBOL FOR NULL
// / (slash) -> '' // FULLWIDTH SOLIDUS
const Standard = MultiEncoder(
EncodeZero |
EncodeSlash |
EncodeCtl |
EncodeDel |
EncodeDot)
const Standard = (EncodeZero |
EncodeSlash |
EncodeCtl |
EncodeDel |
EncodeDot)
// Base only encodes the zero byte and slash
const Base = (EncodeZero |
EncodeSlash |
EncodeDot)
// Display is the internal encoding for logging and output
const Display = Standard