-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutil.go
More file actions
55 lines (49 loc) · 774 Bytes
/
util.go
File metadata and controls
55 lines (49 loc) · 774 Bytes
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
package natsrpc
import (
"strings"
"sync"
)
var bufPool = sync.Pool{
New: func() interface{} {
return &strings.Builder{}
},
}
// joinSubject 组合字符串成subject
func joinSubject(s ...string) string {
// 跳过前导空字符串
start := 0
for start < len(s) && s[start] == "" {
start++
}
s = s[start:]
switch len(s) {
case 0:
return ""
case 1:
return s[0]
case 2:
if s[1] == "" {
return s[0]
}
return s[0] + "." + s[1]
}
// 3个或更多元素使用 bufPool
bf := bufPool.Get().(*strings.Builder)
defer func() {
bf.Reset()
bufPool.Put(bf)
}()
first := true
for _, v := range s {
if v == "" {
continue
}
if first {
first = false
} else {
bf.WriteByte('.')
}
bf.WriteString(v)
}
return bf.String()
}