-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
174 lines (142 loc) · 2.69 KB
/
helpers.go
File metadata and controls
174 lines (142 loc) · 2.69 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package irc
import (
"io"
"strings"
"unicode/utf8"
)
// Hand written string.Fields, much faster than string.FieldsFunc.
// Note that unlike strings.Fields, if the returned slice would be empty, it
// will be nil.
func stringFields(s string, sep byte) []string {
s = fastTrim(s, sep)
if s == "" {
return nil
}
count := strings.Count(s, string(sep))
if count == 0 {
return []string{s}
}
out := make([]string, 0, count+1)
for {
i := strings.IndexByte(s, sep)
if i == -1 {
break
}
f := s[:i]
s = s[i+1:]
if f != "" {
out = append(out, f)
}
}
if s != "" {
out = append(out, s)
}
if len(out) == 0 {
return nil
}
return out
}
// fastTrim is a faster implementation of trimming a string against a single
// byte.
func fastTrim(s string, b byte) string {
if s == "" {
return ""
}
br := rune(b)
findLeft := true
left := 0
prevB := false
right := 0
for i, r := range s {
if findLeft {
if r != br {
left = i
right = i
findLeft = false
}
continue
}
if r == br {
if prevB {
continue
}
prevB = true
right = i
} else {
prevB = false
right = i
}
}
if !prevB {
right++
}
return s[left:right]
}
var (
tagEscapeReplacer = strings.NewReplacer(";", `\:`, " ", `\s`, "\r", `\r`, "\n", `\n`, "\\", `\\`)
tagUnescapeReplacer = strings.NewReplacer(`\:`, ";", `\s`, " ", `\r`, "\r", `\n`, "\n", `\\`, "\\")
)
func tagUnescape(s string) string {
// Check ahead of time to see if the string contains something that
// needs to be unescaped. This is faster and much more allocation
// efficient than always running the replacer.
if containsUnescapeable(s) {
return tagUnescapeReplacer.Replace(s)
}
return s
}
type stringWriter interface {
io.Writer
WriteString(string) (int, error)
}
func tagEscapeWrite(w stringWriter, s string) (n int, err error) {
if containsEscapable(s) {
return tagEscapeReplacer.WriteString(w, s)
}
return w.WriteString(s)
}
var escapableSet = [256]bool{
';': true,
' ': true,
'\n': true,
'\r': true,
'\\': true,
}
func containsEscapable(s string) bool {
for _, r := range s {
if 0 <= r && r < utf8.RuneSelf && escapableSet[byte(r)] {
return true
}
}
return false
}
var escapableCounts = [256]int{
';': 1,
' ': 1,
'\n': 1,
'\r': 1,
'\\': 1,
}
func countEscapeable(s string) int {
count := 0
for _, r := range s {
if 0 <= r && r < utf8.RuneSelf {
count += escapableCounts[byte(r)]
}
}
return count
}
var unescapeableSet = [256]bool{
':': true,
's': true,
'n': true,
'r': true,
'\\': true,
}
func containsUnescapeable(s string) bool {
bi := strings.IndexByte(s, '\\') + 1
if bi != 0 && bi < len(s) {
return unescapeableSet[s[bi]]
}
return false
}