-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
62 lines (49 loc) · 991 Bytes
/
index.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
package inventory
import (
"bytes"
"strings"
"sync"
)
const (
curlyStart = '{'
curlyEnd = '}'
colon = ':'
)
var bufPool = sync.Pool{New: func() any {
return &bytes.Buffer{}
}}
func mkKey(kind, key, val string) (res string) {
buf := bufPool.Get().(*bytes.Buffer)
writeKey(buf, kind, key, val)
res = buf.String()
buf.Reset()
bufPool.Put(buf)
return
}
func parseKey(key string) (kind, k, v string, ok bool) {
endOfKind := strings.IndexByte(key, curlyStart)
if endOfKind < 0 {
return
}
kind = key[:endOfKind]
endOfKey := strings.IndexByte(key, colon)
if endOfKey < 0 {
return
}
k = key[endOfKind+1 : endOfKey]
endOfKeyVal := strings.IndexByte(key, curlyEnd)
if endOfKeyVal < 0 {
return
}
v = key[endOfKey+1 : endOfKeyVal]
ok = true
return
}
func writeKey(dst *bytes.Buffer, kind, key, val string) {
dst.WriteString(kind)
dst.WriteRune(curlyStart)
dst.WriteString(key)
dst.WriteRune(colon)
dst.WriteString(val)
dst.WriteRune(curlyEnd)
}