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

@ -9,24 +9,14 @@ import (
"github.com/rclone/rclone/lib/encoder" "github.com/rclone/rclone/lib/encoder"
) )
// Base only encodes the zero byte and slash
const Base = encoder.MultiEncoder(
encoder.EncodeZero |
encoder.EncodeSlash |
encoder.EncodeDot)
// Display is the internal encoding for logging and output
const Display = encoder.Standard
// LocalUnix is the encoding used by the local backend for non windows platforms // LocalUnix is the encoding used by the local backend for non windows platforms
const LocalUnix = Base const LocalUnix = encoder.Base
// LocalMacOS is the encoding used by the local backend for macOS // LocalMacOS is the encoding used by the local backend for macOS
// //
// macOS can't store invalid UTF-8, it converts them into %XX encoding // macOS can't store invalid UTF-8, it converts them into %XX encoding
const LocalMacOS = encoder.MultiEncoder( const LocalMacOS = (encoder.Base |
uint(Base) | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// LocalWindows is the encoding used by the local backend for windows platforms // LocalWindows is the encoding used by the local backend for windows platforms
// //
@ -48,31 +38,28 @@ const LocalMacOS = encoder.MultiEncoder(
// Also encode invalid UTF-8 bytes as Go can't convert them to UTF-16. // Also encode invalid UTF-8 bytes as Go can't convert them to UTF-16.
// //
// https://docs.microsoft.com/de-de/windows/desktop/FileIO/naming-a-file#naming-conventions // https://docs.microsoft.com/de-de/windows/desktop/FileIO/naming-a-file#naming-conventions
const LocalWindows = encoder.MultiEncoder( const LocalWindows = (encoder.Base |
uint(Base) | encoder.EncodeWin |
encoder.EncodeWin | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeCtl |
encoder.EncodeCtl | encoder.EncodeRightSpace |
encoder.EncodeRightSpace | encoder.EncodeRightPeriod |
encoder.EncodeRightPeriod | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// AmazonCloudDrive is the encoding used by the amazonclouddrive backend // AmazonCloudDrive is the encoding used by the amazonclouddrive backend
// //
// Encode invalid UTF-8 bytes as json doesn't handle them properly. // Encode invalid UTF-8 bytes as json doesn't handle them properly.
const AmazonCloudDrive = encoder.MultiEncoder( const AmazonCloudDrive = (encoder.Base |
uint(Base) | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// B2 is the encoding used by the b2 backend // B2 is the encoding used by the b2 backend
// //
// See: https://www.backblaze.com/b2/docs/files.html // See: https://www.backblaze.com/b2/docs/files.html
// Encode invalid UTF-8 bytes as json doesn't handle them properly. // Encode invalid UTF-8 bytes as json doesn't handle them properly.
// FIXME: allow /, but not leading, trailing or double // FIXME: allow /, but not leading, trailing or double
const B2 = encoder.MultiEncoder( const B2 = (encoder.Display |
uint(Display) | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// Box is the encoding used by the box backend // Box is the encoding used by the box backend
// //
@ -83,18 +70,16 @@ const B2 = encoder.MultiEncoder(
// //
// Testing revealed names with leading spaces work fine. // Testing revealed names with leading spaces work fine.
// Also encode invalid UTF-8 bytes as json doesn't handle them properly. // Also encode invalid UTF-8 bytes as json doesn't handle them properly.
const Box = encoder.MultiEncoder( const Box = (encoder.Display |
uint(Display) | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeRightSpace |
encoder.EncodeRightSpace | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// Drive is the encoding used by the drive backend // Drive is the encoding used by the drive backend
// //
// Encode invalid UTF-8 bytes as json doesn't handle them properly. // Encode invalid UTF-8 bytes as json doesn't handle them properly.
// Don't encode / as it's a valid name character in drive. // Don't encode / as it's a valid name character in drive.
const Drive = encoder.MultiEncoder( const Drive = (encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// Dropbox is the encoding used by the dropbox backend // Dropbox is the encoding used by the dropbox backend
// //
@ -102,52 +87,46 @@ const Drive = encoder.MultiEncoder(
// as invalid characters. // as invalid characters.
// Testing revealed names with trailing spaces and the DEL character don't work. // Testing revealed names with trailing spaces and the DEL character don't work.
// Also encode invalid UTF-8 bytes as json doesn't handle them properly. // Also encode invalid UTF-8 bytes as json doesn't handle them properly.
const Dropbox = encoder.MultiEncoder( const Dropbox = (encoder.Base |
uint(Base) | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeDel |
encoder.EncodeDel | encoder.EncodeRightSpace |
encoder.EncodeRightSpace | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// GoogleCloudStorage is the encoding used by the googlecloudstorage backend // GoogleCloudStorage is the encoding used by the googlecloudstorage backend
const GoogleCloudStorage = encoder.MultiEncoder( const GoogleCloudStorage = (encoder.Base |
uint(Base) | encoder.EncodeCrLf |
encoder.EncodeCrLf | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// JottaCloud is the encoding used by the jottacloud backend // JottaCloud is the encoding used by the jottacloud backend
// //
// Encode invalid UTF-8 bytes as xml doesn't handle them properly. // Encode invalid UTF-8 bytes as xml doesn't handle them properly.
// //
// Also: '*', '/', ':', '<', '>', '?', '\"', '\x00', '|' // Also: '*', '/', ':', '<', '>', '?', '\"', '\x00', '|'
const JottaCloud = encoder.MultiEncoder( const JottaCloud = (encoder.Display |
uint(Display) | encoder.EncodeWin | // :?"*<>|
encoder.EncodeWin | // :?"*<>| encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// Koofr is the encoding used by the koofr backend // Koofr is the encoding used by the koofr backend
// //
// Encode invalid UTF-8 bytes as json doesn't handle them properly. // Encode invalid UTF-8 bytes as json doesn't handle them properly.
const Koofr = encoder.MultiEncoder( const Koofr = (encoder.Display |
uint(Display) | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// Mailru is the encoding used by the mailru backend // Mailru is the encoding used by the mailru backend
// //
// Encode invalid UTF-8 bytes as json doesn't handle them properly. // Encode invalid UTF-8 bytes as json doesn't handle them properly.
const Mailru = encoder.MultiEncoder( const Mailru = (encoder.Display |
uint(Display) | encoder.EncodeWin | // :?"*<>|
encoder.EncodeWin | // :?"*<>| encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// Mega is the encoding used by the mega backend // Mega is the encoding used by the mega backend
// //
// Encode invalid UTF-8 bytes as json doesn't handle them properly. // Encode invalid UTF-8 bytes as json doesn't handle them properly.
const Mega = encoder.MultiEncoder( const Mega = (encoder.Base |
uint(Base) | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// OneDrive is the encoding used by the onedrive backend // OneDrive is the encoding used by the onedrive backend
// //
@ -184,16 +163,15 @@ const Mega = encoder.MultiEncoder(
// the same rules as the Windows naming conventions. // the same rules as the Windows naming conventions.
// //
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/addressing-driveitems?view=odsp-graph-online#path-encoding // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/addressing-driveitems?view=odsp-graph-online#path-encoding
const OneDrive = encoder.MultiEncoder( const OneDrive = (encoder.Display |
uint(Display) | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeHashPercent |
encoder.EncodeHashPercent | encoder.EncodeLeftSpace |
encoder.EncodeLeftSpace | encoder.EncodeLeftTilde |
encoder.EncodeLeftTilde | encoder.EncodeRightPeriod |
encoder.EncodeRightPeriod | encoder.EncodeRightSpace |
encoder.EncodeRightSpace | encoder.EncodeWin |
encoder.EncodeWin | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// OpenDrive is the encoding used by the opendrive backend // OpenDrive is the encoding used by the opendrive backend
// //
@ -218,44 +196,40 @@ const OneDrive = encoder.MultiEncoder(
// Also encode invalid UTF-8 bytes as json doesn't handle them properly. // Also encode invalid UTF-8 bytes as json doesn't handle them properly.
// //
// https://www.opendrive.com/wp-content/uploads/guides/OpenDrive_API_guide.pdf // https://www.opendrive.com/wp-content/uploads/guides/OpenDrive_API_guide.pdf
const OpenDrive = encoder.MultiEncoder( const OpenDrive = (encoder.Base |
uint(Base) | encoder.EncodeWin |
encoder.EncodeWin | encoder.EncodeLeftCrLfHtVt |
encoder.EncodeLeftCrLfHtVt | encoder.EncodeRightCrLfHtVt |
encoder.EncodeRightCrLfHtVt | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeLeftSpace |
encoder.EncodeLeftSpace | encoder.EncodeRightSpace |
encoder.EncodeRightSpace | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// PremiumizeMe is the encoding used by the premiumizeme backend // PremiumizeMe is the encoding used by the premiumizeme backend
// //
// Encode invalid UTF-8 bytes as json doesn't handle them properly. // Encode invalid UTF-8 bytes as json doesn't handle them properly.
const PremiumizeMe = encoder.MultiEncoder( const PremiumizeMe = (encoder.Display |
uint(Display) | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeDoubleQuote |
encoder.EncodeDoubleQuote | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// Pcloud is the encoding used by the pcloud backend // Pcloud is the encoding used by the pcloud backend
// //
// Encode invalid UTF-8 bytes as json doesn't handle them properly. // Encode invalid UTF-8 bytes as json doesn't handle them properly.
// //
// TODO: Investigate Unicode simplification ( gets converted to \ server-side) // TODO: Investigate Unicode simplification ( gets converted to \ server-side)
const Pcloud = encoder.MultiEncoder( const Pcloud = (encoder.Display |
uint(Display) | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// Putio is the encoding used by the putio backend // Putio is the encoding used by the putio backend
// //
// Note that \ is renamed to - // Note that \ is renamed to -
// //
// Encode invalid UTF-8 bytes as json doesn't handle them properly. // Encode invalid UTF-8 bytes as json doesn't handle them properly.
const Putio = encoder.MultiEncoder( const Putio = (encoder.Display |
uint(Display) | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// Fichier is the encoding used by the fichier backend // Fichier is the encoding used by the fichier backend
// //
@ -270,17 +244,16 @@ const Putio = encoder.MultiEncoder(
// '`': '', // FULLWIDTH GRAVE ACCENT // '`': '', // FULLWIDTH GRAVE ACCENT
// //
// Leading space and trailing space // Leading space and trailing space
const Fichier = encoder.MultiEncoder( const Fichier = (encoder.Display |
uint(Display) | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeSingleQuote |
encoder.EncodeSingleQuote | encoder.EncodeBackQuote |
encoder.EncodeBackQuote | encoder.EncodeDoubleQuote |
encoder.EncodeDoubleQuote | encoder.EncodeLtGt |
encoder.EncodeLtGt | encoder.EncodeDollar |
encoder.EncodeDollar | encoder.EncodeLeftSpace |
encoder.EncodeLeftSpace | encoder.EncodeRightSpace |
encoder.EncodeRightSpace | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// FTP is the encoding used by the ftp backend // FTP is the encoding used by the ftp backend
// //
@ -289,9 +262,8 @@ const Fichier = encoder.MultiEncoder(
// //
// proftpd can't handle '*' in file names // proftpd can't handle '*' in file names
// pureftpd can't handle '[', ']' or '*' // pureftpd can't handle '[', ']' or '*'
const FTP = encoder.MultiEncoder( const FTP = (encoder.Display |
uint(Display) | encoder.EncodeRightSpace)
encoder.EncodeRightSpace)
// S3 is the encoding used by the s3 backend // S3 is the encoding used by the s3 backend
// //
@ -305,58 +277,52 @@ const FTP = encoder.MultiEncoder(
// - doubled / encoding // - doubled / encoding
// - trailing / encoding // - trailing / encoding
// so that AWS keys are always valid file names // so that AWS keys are always valid file names
const S3 = encoder.MultiEncoder( const S3 = (encoder.EncodeInvalidUtf8 |
encoder.EncodeInvalidUtf8 | encoder.EncodeSlash |
encoder.EncodeSlash | encoder.EncodeDot)
encoder.EncodeDot)
// Swift is the encoding used by the swift backend // Swift is the encoding used by the swift backend
const Swift = encoder.MultiEncoder( const Swift = (encoder.EncodeInvalidUtf8 |
encoder.EncodeInvalidUtf8 | encoder.EncodeSlash)
encoder.EncodeSlash)
// AzureBlob is the encoding used by the azureblob backend // AzureBlob is the encoding used by the azureblob backend
const AzureBlob = encoder.MultiEncoder( const AzureBlob = (encoder.EncodeInvalidUtf8 |
encoder.EncodeInvalidUtf8 | encoder.EncodeSlash |
encoder.EncodeSlash | encoder.EncodeCtl |
encoder.EncodeCtl | encoder.EncodeDel |
encoder.EncodeDel | encoder.EncodeBackSlash |
encoder.EncodeBackSlash | encoder.EncodeRightPeriod)
encoder.EncodeRightPeriod)
// QingStor is the encoding used by the qingstor backend // QingStor is the encoding used by the qingstor backend
const QingStor = encoder.MultiEncoder( const QingStor = (encoder.EncodeInvalidUtf8 |
encoder.EncodeInvalidUtf8 | encoder.EncodeCtl |
encoder.EncodeCtl | encoder.EncodeSlash)
encoder.EncodeSlash)
// Sharefile is the encoding used by the sharefile backend // Sharefile is the encoding used by the sharefile backend
const Sharefile = encoder.MultiEncoder( const Sharefile = (encoder.Base |
uint(Base) | encoder.EncodeWin | // :?"*<>|
encoder.EncodeWin | // :?"*<>| encoder.EncodeBackSlash | // \
encoder.EncodeBackSlash | // \ encoder.EncodeCtl |
encoder.EncodeCtl | encoder.EncodeRightSpace |
encoder.EncodeRightSpace | encoder.EncodeRightPeriod |
encoder.EncodeRightPeriod | encoder.EncodeLeftSpace |
encoder.EncodeLeftSpace | encoder.EncodeLeftPeriod |
encoder.EncodeLeftPeriod | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// Yandex is the encoding used by the yandex backend // Yandex is the encoding used by the yandex backend
// //
// Of the control characters \t \n \r are allowed // Of the control characters \t \n \r are allowed
// it doesn't seem worth making an exception for this // it doesn't seem worth making an exception for this
const Yandex = encoder.MultiEncoder( const Yandex = (encoder.Display |
uint(Display) | encoder.EncodeInvalidUtf8)
encoder.EncodeInvalidUtf8)
// ByName returns the encoder for a give backend name or nil // ByName returns the encoder for a give backend name or nil
func ByName(name string) encoder.Encoder { func ByName(name string) encoder.Encoder {
switch strings.ToLower(name) { switch strings.ToLower(name) {
case "base": case "base":
return Base return encoder.Base
case "display": case "display":
return Display return encoder.Display
case "amazonclouddrive": case "amazonclouddrive":
return AmazonCloudDrive return AmazonCloudDrive
case "azureblob": case "azureblob":

View File

@ -37,32 +37,32 @@ const (
// Possible flags for the MultiEncoder // Possible flags for the MultiEncoder
const ( const (
EncodeZero uint = 0 // NUL(0x00) EncodeZero MultiEncoder = 0 // NUL(0x00)
EncodeSlash uint = 1 << iota // / EncodeSlash MultiEncoder = 1 << iota // /
EncodeLtGt // <> EncodeLtGt // <>
EncodeDoubleQuote // " EncodeDoubleQuote // "
EncodeSingleQuote // ' EncodeSingleQuote // '
EncodeBackQuote // ` EncodeBackQuote // `
EncodeDollar // $ EncodeDollar // $
EncodeColon // : EncodeColon // :
EncodeQuestion // ? EncodeQuestion // ?
EncodeAsterisk // * EncodeAsterisk // *
EncodePipe // | EncodePipe // |
EncodeHash // # EncodeHash // #
EncodePercent // % EncodePercent // %
EncodeBackSlash // \ EncodeBackSlash // \
EncodeCrLf // CR(0x0D), LF(0x0A) EncodeCrLf // CR(0x0D), LF(0x0A)
EncodeDel // DEL(0x7F) EncodeDel // DEL(0x7F)
EncodeCtl // CTRL(0x01-0x1F) EncodeCtl // CTRL(0x01-0x1F)
EncodeLeftSpace // Leading SPACE EncodeLeftSpace // Leading SPACE
EncodeLeftPeriod // Leading . EncodeLeftPeriod // Leading .
EncodeLeftTilde // Leading ~ EncodeLeftTilde // Leading ~
EncodeLeftCrLfHtVt // Leading CR LF HT VT EncodeLeftCrLfHtVt // Leading CR LF HT VT
EncodeRightSpace // Trailing SPACE EncodeRightSpace // Trailing SPACE
EncodeRightPeriod // Trailing . EncodeRightPeriod // Trailing .
EncodeRightCrLfHtVt // Trailing CR LF HT VT EncodeRightCrLfHtVt // Trailing CR LF HT VT
EncodeInvalidUtf8 // Invalid UTF-8 bytes EncodeInvalidUtf8 // Invalid UTF-8 bytes
EncodeDot // . and .. names EncodeDot // . and .. names
// Synthetic // Synthetic
EncodeWin = EncodeColon | EncodeQuestion | EncodeDoubleQuote | EncodeAsterisk | EncodeLtGt | EncodePipe // :?"*<>| EncodeWin = EncodeColon | EncodeQuestion | EncodeDoubleQuote | EncodeAsterisk | EncodeLtGt | EncodePipe // :?"*<>|
@ -70,8 +70,8 @@ const (
) )
// Has returns true if flag is contained in mask // Has returns true if flag is contained in mask
func (mask MultiEncoder) Has(flag uint) bool { func (mask MultiEncoder) Has(flag MultiEncoder) bool {
return uint(mask)&flag != 0 return mask&flag != 0
} }
// Encoder can transform names to and from the original and translated version. // Encoder can transform names to and from the original and translated version.

View File

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

View File

@ -20,7 +20,7 @@ const (
) )
type mapping struct { type mapping struct {
mask uint mask encoder.MultiEncoder
src, dst []rune src, dst []rune
} }
type stringPair struct { type stringPair struct {
@ -36,7 +36,7 @@ package encoder
` `
var maskBits = []struct { var maskBits = []struct {
mask uint mask encoder.MultiEncoder
name string name string
}{ }{
{encoder.EncodeZero, "EncodeZero"}, {encoder.EncodeZero, "EncodeZero"},
@ -68,7 +68,7 @@ var maskBits = []struct {
} }
type edge struct { type edge struct {
mask uint mask encoder.MultiEncoder
name string name string
edge int edge int
orig []rune orig []rune
@ -429,7 +429,7 @@ func fatalW(_ int, err error) func(...interface{}) {
return func(s ...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 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 return out
} }
func getMapping(mask uint) mapping { func getMapping(mask encoder.MultiEncoder) mapping {
for _, m := range allMappings { for _, m := range allMappings {
if m.mask == mask { if m.mask == mask {
return m return m

View File

@ -7,9 +7,16 @@ package encoder
// List of replaced characters: // List of replaced characters:
// (0x00) -> '␀' // SYMBOL FOR NULL // (0x00) -> '␀' // SYMBOL FOR NULL
// / (slash) -> '' // FULLWIDTH SOLIDUS // / (slash) -> '' // FULLWIDTH SOLIDUS
const Standard = MultiEncoder( const Standard = (EncodeZero |
EncodeZero | EncodeSlash |
EncodeSlash | EncodeCtl |
EncodeCtl | EncodeDel |
EncodeDel | EncodeDot)
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