forked from gSpera/morse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
69 lines (53 loc) · 1.77 KB
/
utils.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
59
60
61
62
63
64
65
66
67
68
69
package mltmorse
import (
"io"
)
// mergeMap merges multiple Map objects map[rune]interface{}
func MergeEncMap(mps ...EncodingMap) EncodingMap {
ans := make(EncodingMap, 0)
for _, m := range mps {
for k, v := range m {
ans[k] = v
}
}
return ans
}
// ToText converts a morse string to his textual representation, it is an alias to DefaultConverter.ToText
func ToText(morse string) string { return DefaultConverter.ToText(morse) }
// ToMorse converts a text to his morse rrpresentation, it is an alias to DefaultConverter.ToMorse
func ToMorse(text string) string { return DefaultConverter.ToMorse(text) }
// ToMorseWriter translates all the text written to the returned io.Writer in morse code and writes it in the input io.Writer
func ToMorseWriter(output io.Writer) io.Writer { return DefaultConverter.ToMorseWriter(output) }
// ToTextWriter translates all the text written to the returned io.Writer from morse code and writes it in the input io.Writer
func ToTextWriter(output io.Writer) io.Writer { return DefaultConverter.ToTextWriter(output) }
type translateToMorse struct {
conv Converter
buffer []byte
input io.Reader
output io.Writer
}
type translateToText struct {
conv Converter
buffer []byte
input io.Reader
output io.Writer
}
// Text -> Morse
func (t translateToMorse) Write(data []byte) (int, error) {
morse := t.conv.ToMorse(string(data))
_, err := t.output.Write([]byte(morse))
return len(data), err
}
// Morse -> Text
func (t translateToText) Write(data []byte) (int, error) {
morse := t.conv.ToText(string(data))
_, err := t.output.Write([]byte(morse))
return len(data), err
}
func reverseEncodingMap(encoding EncodingMap) map[string]rune {
ret := make(map[string]rune, len(encoding))
for k, v := range encoding {
ret[v] = k
}
return ret
}