-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
106 lines (89 loc) · 2.38 KB
/
helpers.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ts3
// Escaping table:
// NAME CHAR ASCII REPLACE CHAR REPLACE ASCII
// Backslash \ 92 \\ 92 92
// Slash / 47 \/ 92 47
// Whitespace " " 32 \s 92 115
// Pipe | 124 \p 92 112
// Bell \a 7 \a 92 97
// Backspace \b 8 \b 92 98
// Formfeed \f 12 \f 92 102
// Newline \n 10 \n 92 110
// Car. Ret \r 13 \r 92 114
// Hor. Tab \t 9 \t 92 116
// Ver. Tab \v 11 \v 92 118
import (
"strconv"
"strings"
)
var (
quoteTable = map[rune][]rune{
92: []rune{92, 92}, // \ -> \\
'/': []rune{92, 47}, // / -> \/
' ': []rune{92, 115}, // " " -> \s
'|': []rune{92, 112}, // | -> \p
'\a': []rune{92, 97}, // All these translate to `/` + `letter`
'\b': []rune{92, 98}, //
'\f': []rune{92, 102}, //
'\n': []rune{92, 110}, //
'\r': []rune{92, 114}, //
'\t': []rune{92, 116}, //
'\v': []rune{92, 118}, //
}
unquoteTable = map[string]string{
`\\`: `\`, // \\ -> \
`\/`: `/`, // \/ -> /
`\s`: ` `, // \s -> " "
`\p`: `|`, // \p -> |
`\a`: "\a",
`\b`: "\b",
`\f`: "\f",
`\n`: "\n",
`\r`: "\r",
`\t`: "\t",
`\v`: "\v",
}
)
// Escapes special chars
func Quote(s string) string {
var res = make([]rune, 0)
for _, r := range []rune(s) {
if v, ok := quoteTable[r]; ok {
res = append(res, v...)
} else {
res = append(res, r)
}
}
return string(res)
}
// Unescapes special chars
func Unquote(s string) string {
for k, v := range unquoteTable {
s = strings.Replace(s, k, v, -1)
}
return s
}
// Keeps only printable ASCII runes, also cleans "\r"
func trimNet(s string) string {
var res []rune
s = strings.Trim(s, "\r")
// Just pretty ASCII runes
for _, r := range s {
switch {
case 32 >= r || r <= 127:
res = append(res, r)
}
}
return string(res)
}
func parseError(data string) ErrorMsg {
var err ErrorMsg
split := strings.Split(data, " ")
values := make([]string, 2)
for i, s := range split[1:] {
values[i] = strings.Split(s, "=")[1]
}
err.Id, _ = strconv.Atoi(values[0])
err.Msg = values[1]
return err
}