encoder/filename: Wrap scsu package

This commit is contained in:
Klaus Post 2021-02-11 10:41:29 +01:00 committed by Nick Craig-Wood
parent f14220ef1e
commit 5ca7f1fe87
5 changed files with 37 additions and 6 deletions

View File

@ -7,7 +7,6 @@ import (
"errors" "errors"
"sync" "sync"
"github.com/dop251/scsu"
"github.com/klauspost/compress/huff0" "github.com/klauspost/compress/huff0"
) )
@ -51,7 +50,7 @@ func DecodeBytes(table byte, data []byte) (string, error) {
case tableReserved: case tableReserved:
return "", ErrUnsupported return "", ErrUnsupported
case tableSCSUPlain: case tableSCSUPlain:
return scsu.Decode(data) return scsuDecode(data)
case tableRLE: case tableRLE:
if len(data) < 2 { if len(data) < 2 {
return "", ErrCorrupted return "", ErrCorrupted
@ -88,7 +87,7 @@ func DecodeBytes(table byte, data []byte) (string, error) {
return "", ErrCorrupted return "", ErrCorrupted
} }
if table == tableSCSU { if table == tableSCSU {
return scsu.Decode(name) return scsuDecode(name)
} }
return string(name), nil return string(name), nil
} }

View File

@ -1,6 +1,9 @@
package filename package filename
import "testing" import (
"runtime"
"testing"
)
func TestDecode(t *testing.T) { func TestDecode(t *testing.T) {
tests := []struct { tests := []struct {
@ -117,6 +120,10 @@ func TestDecode(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := Decode(tt.encoded) got, err := Decode(tt.encoded)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
if err != nil && err.Error() == scsuNotEnabled && runtime.Version() < "go1.13" {
t.Skip(err.Error())
return
}
if tt.encoded == "" && tt.want != "" { if tt.encoded == "" && tt.want != "" {
proposed := Encode(tt.want) proposed := Encode(tt.want)
table := decodeMap[proposed[0]] - 1 table := decodeMap[proposed[0]] - 1

View File

@ -4,7 +4,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/binary" "encoding/binary"
"github.com/dop251/scsu"
"github.com/klauspost/compress/huff0" "github.com/klauspost/compress/huff0"
) )
@ -38,7 +37,7 @@ func EncodeBytes(s string) (table byte, payload []byte) {
if i == tableSCSU { if i == tableSCSU {
var err error var err error
olen := len(org) olen := len(org)
org, err = scsu.EncodeStrict(s, make([]byte, 0, len(org))) org, err = scsuEncodeStrict(s, make([]byte, 0, len(org)))
if err != nil || olen <= len(org) { if err != nil || olen <= len(org) {
continue continue
} }

View File

@ -0,0 +1,16 @@
package filename
import "errors"
const scsuNotEnabled = "scsu encoding not enabled in this build due to old go version"
// Functions wrap scsu package, since it doesn't build on old Go versions.
// Remove once v1.13 is minimum supported version.
var scsuDecode = func(b []byte) (string, error) {
return "", errors.New(scsuNotEnabled)
}
var scsuEncodeStrict = func(src string, dst []byte) ([]byte, error) {
return nil, errors.New(scsuNotEnabled)
}

View File

@ -0,0 +1,10 @@
// +build go1.13
package filename
import "github.com/dop251/scsu"
func init() {
scsuDecode = scsu.Decode
scsuEncodeStrict = scsu.EncodeStrict
}