-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
194 lines (161 loc) · 4.69 KB
/
parse.go
File metadata and controls
194 lines (161 loc) · 4.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package namespace
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
"github.com/yehan2002/errors"
)
const maxLength = 200
const defaultNamespace = "minecraft"
const validChars = "abcdefghijklmnopqrstuvwxyz1234567890-_"
const upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var translate [unicode.MaxASCII + 1]rune = func() (table [unicode.MaxASCII + 1]rune) {
for _, char := range validChars {
table[char] = char
}
for _, char := range upperChars {
table[char] = unicode.ToLower(char)
}
for i := range table {
if table[i] == 0 {
table[i] = utf8.RuneError
}
}
return
}()
// validBytes is a array of bytes in which validBytes[r] == r for all valid
// bytes in a namespace/namespaced key (except . and /).
var validBytes [256]byte = func() (table [256]byte) {
for _, char := range []byte(validChars) {
table[char] = char
}
table[0] = 255
return
}()
// parseNSK parses the given string and converts it into a namespaced key.
// If noSeparator if set, this parses the string as a key without a namespace.
// If nsOnly is also set, the string is parsed as a namespace without a key.
// If strict is set, this makes no attempt to correct the nsk and returns at the first
// error it encounters. If strict is false, this never returns an error unless the string is longer
// than [maxLength] or ends with a trailing ':' character.
func parseNSK(v string, strict, noSeparator, nsOnly bool) (ns, key string, err error) {
var sep int
var invalid bool
if l := len(v); l == 0 {
// return default namespace for empty string if we only need a namespace
if nsOnly {
return defaultNamespace, "", nil
}
return "", "", ErrEmpty
} else if l > maxLength {
return "", "", ErrTooLong
} else if v[l-1] == ':' && !noSeparator {
return "", "", ErrTrailingSep
}
// there can be no separator if nsOnly is set
if !noSeparator && nsOnly {
noSeparator = true
}
// fast path. iterates over each character in the string without modifying it.
var i int
var charByte byte
for i, charByte = range []byte(v) {
switch charByte {
case validBytes[charByte]:
// char is a valid character.
continue
case ':':
// first ':' separates the namespace and the key
if sep == 0 && !noSeparator {
sep = i
continue
}
case '/', '.':
// the `/` and `.` characters are only allowed in the key.
if sep != 0 || (noSeparator && !nsOnly) {
continue
}
if !noSeparator && sep == 0 {
// check if the key contains a separator.
// we do this to insure bare keys that have `/` or `.` are parsed correctly.
noSeparator = strings.IndexByte(v[i:], ':') == -1
// the string has no separator, we are parsing a key so `/` or `.` is allowed
if noSeparator {
continue
}
}
}
invalid = true
break
}
var char rune
if invalid {
if strict {
return "", "", getCharError(v, i, noSeparator)
}
// create a new string builder and copy the string we already validated
var b strings.Builder
b.Grow(len(v))
b.WriteString(v[:i])
if !noSeparator && sep == 0 {
// check if the key contains a separator.
// we do this to insure bare keys that have `/` are parsed correctly.
noSeparator = strings.IndexByte(v[i:], ':') == -1
}
start := i
for i, char = range v[start:] {
replaced := translate[char&0x7f]
// handle replacing the invalid characters
if char > unicode.MaxASCII || replaced == utf8.RuneError {
replaced = '_'
switch char {
case ':':
// if this is the first ':' we encountered
// it is valid and separates the namespace and key
if sep == 0 && !noSeparator {
replaced = ':'
sep = i + start
}
case '/', '.':
// `/` is allowed in keys
if sep != 0 || noSeparator && !nsOnly {
replaced = char
}
}
}
b.WriteRune(replaced)
}
v = b.String()
}
if sep != 0 {
if len(v)-sep > 1 {
return v[:sep], v[sep+1:], nil
}
// this only happens when a string that ends with `:` is given.
// we already checked this at the start, so this should be unreachable.
return "", "", ErrTrailingSep
}
if nsOnly {
return v, "", nil
}
return defaultNamespace, v, nil
}
// getCharError returns [ErrInvalidChar] wrapped with more information about the error that
// occurred.
func getCharError(v string, i int, inNamespace bool) error {
invalidChar, _ := utf8.DecodeRuneInString(v[i:])
var msg string
if invalidChar == '/' || invalidChar == '.' {
msg = fmt.Sprintf(`%q is not allowed in a key`, invalidChar)
} else if invalidChar == ':' {
if inNamespace {
msg = `":" is not allowed in a namespace`
} else {
msg = `found multiple ":" characters`
}
} else {
msg = fmt.Sprintf("invalid character %q (%c)", invalidChar, invalidChar)
}
return errors.CauseStr(ErrInvalidChar, msg)
}