Skip to content

Commit 87566fe

Browse files
committed
更新
1 parent 8432b74 commit 87566fe

5 files changed

Lines changed: 59 additions & 26 deletions

File tree

encoding/base45.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ import (
66

77
// Base45
88
func (this Encoding) Base45Decode() Encoding {
9-
data := string(this.data)
10-
decoded, err := base45.Decode(data)
9+
decoded, err := base45.StdEncoding.DecodeString(string(this.data))
1110

12-
this.data = []byte(decoded)
11+
this.data = decoded
1312
this.Error = err
1413

1514
return this
1615
}
1716

1817
// 编码 Base45
1918
func (this Encoding) Base45Encode() Encoding {
20-
data := base45.Encode(string(this.data))
19+
data := base45.StdEncoding.EncodeToString(this.data)
2120
this.data = []byte(data)
2221

2322
return this

encoding/basex.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
// Basex2
88
func (this Encoding) Basex2Decode() Encoding {
99
data := string(this.data)
10-
this.data, this.Error = basex.Base2Encoding.Decode(data)
10+
this.data, this.Error = basex.Base2Encoding.DecodeString(data)
1111

1212
return this
1313
}
1414

1515
// 编码 Base2
1616
func (this Encoding) Basex2Encode() Encoding {
17-
data := basex.Base2Encoding.Encode(this.data)
17+
data := basex.Base2Encoding.EncodeToString(this.data)
1818
this.data = []byte(data)
1919

2020
return this
@@ -25,14 +25,14 @@ func (this Encoding) Basex2Encode() Encoding {
2525
// Basex16
2626
func (this Encoding) Basex16Decode() Encoding {
2727
data := string(this.data)
28-
this.data, this.Error = basex.Base16Encoding.Decode(data)
28+
this.data, this.Error = basex.Base16Encoding.DecodeString(data)
2929

3030
return this
3131
}
3232

3333
// 编码 Base16
3434
func (this Encoding) Basex16Encode() Encoding {
35-
data := basex.Base16Encoding.Encode(this.data)
35+
data := basex.Base16Encoding.EncodeToString(this.data)
3636
this.data = []byte(data)
3737

3838
return this
@@ -43,14 +43,14 @@ func (this Encoding) Basex16Encode() Encoding {
4343
// Basex62
4444
func (this Encoding) Basex62Decode() Encoding {
4545
data := string(this.data)
46-
this.data, this.Error = basex.Base62Encoding.Decode(data)
46+
this.data, this.Error = basex.Base62Encoding.DecodeString(data)
4747

4848
return this
4949
}
5050

5151
// 编码 Basex62
5252
func (this Encoding) Basex62Encode() Encoding {
53-
data := basex.Base62Encoding.Encode(this.data)
53+
data := basex.Base62Encoding.EncodeToString(this.data)
5454
this.data = []byte(data)
5555

5656
return this
@@ -61,14 +61,14 @@ func (this Encoding) Basex62Encode() Encoding {
6161
// BasexDecodeWithEncoder
6262
func (this Encoding) BasexDecodeWithEncoder(encoder string) Encoding {
6363
data := string(this.data)
64-
this.data, this.Error = basex.NewEncoding(encoder).Decode(data)
64+
this.data, this.Error = basex.NewEncoding(encoder).DecodeString(data)
6565

6666
return this
6767
}
6868

6969
// BasexEncodeWithEncoder
7070
func (this Encoding) BasexEncodeWithEncoder(encoder string) Encoding {
71-
data := basex.NewEncoding(encoder).Encode(this.data)
71+
data := basex.NewEncoding(encoder).EncodeToString(this.data)
7272
this.data = []byte(data)
7373

7474
return this

encoding/morse.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ import (
66

77
// MorseITU
88
func (this Encoding) MorseITUDecode() Encoding {
9-
data := string(this.data)
10-
data, err := morse.DecodeITU(data)
9+
data, err := morse.ITUEncoding.DecodeString(string(this.data))
1110

12-
this.data = []byte(data)
11+
this.data = data
1312
this.Error = err
1413

1514
return this
1615
}
1716

1817
// 编码 MorseITU
1918
func (this Encoding) MorseITUEncode() Encoding {
20-
data := morse.EncodeITU(string(this.data))
19+
data := morse.ITUEncoding.EncodeToString(this.data)
2120
this.data = []byte(data)
2221

2322
return this

morse/morse.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package morse
22

33
import (
44
"fmt"
5+
"unsafe"
6+
"reflect"
57
"strings"
68
)
79

@@ -26,9 +28,9 @@ func NewEncoding(alphabet map[string]string, letterSeparator, wordSeparator stri
2628
}
2729

2830
// Encode encodes clear text in `s` using `alphabet` mapping
29-
func (enc *Encoding) Encode(s string) string {
31+
func (enc *Encoding) Encode(s []byte) []byte {
3032
res := ""
31-
for _, part := range s {
33+
for _, part := range string(s) {
3234
p := string(part)
3335
if p == " " {
3436
if enc.wordSeparator != "" {
@@ -39,13 +41,19 @@ func (enc *Encoding) Encode(s string) string {
3941
}
4042
}
4143

42-
return strings.TrimSpace(res)
44+
return []byte(strings.TrimSpace(res))
45+
}
46+
47+
// EncodeToString returns the base62 encoding of src.
48+
func (enc *Encoding) EncodeToString(src []byte) string {
49+
buf := enc.Encode(src)
50+
return string(buf)
4351
}
4452

4553
// Decode decodes morse code in `s` using `alphabet` mapping
46-
func (enc *Encoding) Decode(s string) (string, error) {
54+
func (enc *Encoding) Decode(s []byte) ([]byte, error) {
4755
res := ""
48-
for _, part := range strings.Split(s, enc.letterSeparator) {
56+
for _, part := range strings.Split(string(s), enc.letterSeparator) {
4957
found := false
5058
for key, val := range enc.alphabet {
5159
if val == part {
@@ -61,11 +69,18 @@ func (enc *Encoding) Decode(s string) (string, error) {
6169
}
6270

6371
if !found {
64-
return res, fmt.Errorf("go-encoding/morse: unknown character " + part)
72+
return []byte(res), fmt.Errorf("go-encoding/morse: unknown character " + part)
6573
}
6674
}
6775

68-
return res, nil
76+
return []byte(res), nil
77+
}
78+
79+
// DecodeString returns the bytes represented by the base62 string s.
80+
func (enc *Encoding) DecodeString(s string) ([]byte, error) {
81+
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
82+
bh := reflect.SliceHeader{Data: sh.Data, Len: sh.Len, Cap: sh.Len}
83+
return enc.Decode(*(*[]byte)(unsafe.Pointer(&bh)))
6984
}
7085

7186
// LooksLikeMorse returns true if string seems to be a morse encoded string

morse/morse_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,41 @@ package morse
22

33
import "testing"
44

5-
func TestLooksLikeMorse(t *testing.T) {
5+
func Test_LooksLikeMorse(t *testing.T) {
66
if !LooksLikeMorse("- .... . .-..") {
77
t.Error("fail 1")
88
}
9+
910
if LooksLikeMorse("one one one...") {
1011
t.Error("fail 2")
1112
}
13+
1214
if LooksLikeMorse("") {
1315
t.Error("fail 3")
1416
}
1517
}
1618

17-
func TestDecodeBrokenITU(t *testing.T) {
18-
_, err := ITUEncoding.Decode("-- ?.. ...")
19+
func Test_DecodeBrokenITU(t *testing.T) {
20+
_, err := ITUEncoding.DecodeString("-- ?.. ...")
1921
if err == nil {
2022
t.Error("expected error")
2123
}
2224
}
25+
26+
func Test_EncodeToString(t *testing.T) {
27+
in := []byte("test:date")
28+
29+
encoded := ITUEncoding.EncodeToString(in)
30+
if len(encoded) == 0 {
31+
t.Error("EncodeToString fail")
32+
}
33+
34+
decoded, err := ITUEncoding.DecodeString(encoded)
35+
if err != nil {
36+
t.Error("DecodeString fail")
37+
}
38+
39+
if string(decoded) != string(in) {
40+
t.Errorf("DecodeString, got %s, want %s", string(decoded), string(in))
41+
}
42+
}

0 commit comments

Comments
 (0)