-
Notifications
You must be signed in to change notification settings - Fork 163
/
quotes.go
91 lines (83 loc) · 1.42 KB
/
quotes.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
package actionlint
import (
"sort"
"strconv"
"strings"
)
type quotesBuilder struct {
inner strings.Builder
buf []byte
comma bool
}
func (b *quotesBuilder) append(s string) {
if b.comma {
b.inner.WriteString(", ")
} else {
b.comma = true
}
b.buf = strconv.AppendQuote(b.buf[:0], s)
b.inner.Write(b.buf)
}
func (b *quotesBuilder) appendRune(r rune) {
if b.comma {
b.inner.WriteString(", ")
} else {
b.comma = true
}
b.buf = strconv.AppendQuoteRune(b.buf[:0], r)
b.inner.Write(b.buf)
}
func (b *quotesBuilder) build() string {
return b.inner.String()
}
func quotes(ss []string) string {
l := len(ss)
if l == 0 {
return ""
}
n, max := 0, 0
for _, s := range ss {
m := len(s) + 2 // 2 for delims
n += m
if m > max {
max = m
}
}
n += (l - 1) * 2 // comma
b := quotesBuilder{}
b.buf = make([]byte, 0, max)
b.inner.Grow(n)
for _, s := range ss {
b.append(s)
}
return b.build()
}
func sortedQuotes(ss []string) string {
sort.Strings(ss)
return quotes(ss)
}
func quotesAll(sss ...[]string) string {
n, max := 0, 0
for _, ss := range sss {
for _, s := range ss {
m := len(s) + 2 // 2 for delims
n += m
if m > max {
max = m
}
}
n += (len(ss) - 1) * 2 // comma
}
b := quotesBuilder{}
b.buf = make([]byte, 0, max)
n += (len(sss) - 1) * 2 // comma
if n > 0 {
b.inner.Grow(n)
}
for _, ss := range sss {
for _, s := range ss {
b.append(s)
}
}
return b.build()
}