forked from TrueCloudLab/rclone
lib/encoder: move definitions here and remove uint casts
This commit is contained in:
parent
3c620d521d
commit
c555dc71c2
5 changed files with 172 additions and 200 deletions
|
@ -9,23 +9,13 @@ 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,8 +38,7 @@ 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 |
|
||||||
|
@ -60,8 +49,7 @@ const LocalWindows = encoder.MultiEncoder(
|
||||||
// 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
|
||||||
|
@ -69,8 +57,7 @@ const AmazonCloudDrive = encoder.MultiEncoder(
|
||||||
// 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)
|
||||||
|
|
||||||
|
@ -83,8 +70,7 @@ 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)
|
||||||
|
@ -93,8 +79,7 @@ const Box = encoder.MultiEncoder(
|
||||||
//
|
//
|
||||||
// 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,16 +87,14 @@ 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)
|
||||||
|
|
||||||
|
@ -120,24 +103,21 @@ const GoogleCloudStorage = encoder.MultiEncoder(
|
||||||
// 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)
|
||||||
|
@ -145,8 +125,7 @@ const Mailru = encoder.MultiEncoder(
|
||||||
// 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,8 +163,7 @@ 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 |
|
||||||
|
@ -218,8 +196,7 @@ 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 |
|
||||||
|
@ -231,8 +208,7 @@ const OpenDrive = encoder.MultiEncoder(
|
||||||
// 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)
|
||||||
|
@ -242,8 +218,7 @@ const PremiumizeMe = encoder.MultiEncoder(
|
||||||
// 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)
|
||||||
|
|
||||||
|
@ -252,8 +227,7 @@ const Pcloud = encoder.MultiEncoder(
|
||||||
// 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)
|
||||||
|
|
||||||
|
@ -270,8 +244,7 @@ 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 |
|
||||||
|
@ -289,8 +262,7 @@ 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,19 +277,16 @@ 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 |
|
||||||
|
@ -325,14 +294,12 @@ const AzureBlob = encoder.MultiEncoder(
|
||||||
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 |
|
||||||
|
@ -346,17 +313,16 @@ const Sharefile = encoder.MultiEncoder(
|
||||||
//
|
//
|
||||||
// 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":
|
||||||
|
|
|
@ -37,8 +37,8 @@ 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 // '
|
||||||
|
@ -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.
|
||||||
|
|
|
@ -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: "a‛B\\‛Eg",
|
out: "a‛B\\‛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,8 +255,7 @@ func TestDecodeHalf(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const oneDrive = MultiEncoder(
|
const oneDrive = (Standard |
|
||||||
uint(Standard) |
|
|
||||||
EncodeWin |
|
EncodeWin |
|
||||||
EncodeBackSlash |
|
EncodeBackSlash |
|
||||||
EncodeHashPercent |
|
EncodeHashPercent |
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Add table
Reference in a new issue