-
Notifications
You must be signed in to change notification settings - Fork 69
/
morse.go
58 lines (49 loc) · 1.07 KB
/
morse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package dongle
import (
"fmt"
"github.com/dromara/dongle/morse"
)
type MorseError struct {
}
func NewMorseError() MorseError {
return MorseError{}
}
func (e MorseError) SrcError() error {
return fmt.Errorf("morse: invalid src, the src can't contain spaces")
}
func (e MorseError) DecodeError() error {
return fmt.Errorf("morse: failed to decode src")
}
var morseError = NewMorseError()
// ByMorse encodes by morse.
func (e Encoder) ByMorse(separator ...string) Encoder {
if len(e.src) == 0 || e.Error != nil {
return e
}
if len(separator) == 0 {
separator = []string{"/"}
}
dst, err := morse.Encode(e.src, separator[0])
if err != nil {
e.Error = morseError.SrcError()
return e
}
e.dst = string2bytes(dst)
return e
}
// ByMorse decodes by morse.
func (d Decoder) ByMorse(separator ...string) Decoder {
if len(d.src) == 0 || d.Error != nil {
return d
}
if len(separator) == 0 {
separator = []string{"/"}
}
dst, err := morse.Decode(d.src, separator[0])
if err != nil {
d.Error = morseError.DecodeError()
return d
}
d.dst = string2bytes(dst)
return d
}