-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
260 lines (220 loc) · 4.33 KB
/
util.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2014, Hǎiliàng Wáng. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cwrap
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"reflect"
"regexp"
"strconv"
"strings"
"unsafe"
)
var (
MachineSize = int(unsafe.Sizeof(uintptr(0)))
)
func p(v ...interface{}) {
fmt.Println(v...)
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
func gofmt(file string) error {
return newCmd("go", "fmt", file).exec()
}
func fp(w io.Writer, v ...interface{}) {
fmt.Fprint(w, v...)
fmt.Fprintln(w)
}
func fpn(w io.Writer, v ...interface{}) {
fmt.Fprint(w, v...)
}
func trimPrefix(s, prefix string) string {
return strings.TrimPrefix(s, prefix)
}
func join(a []string, sep string) string {
return strings.Join(a, sep)
}
func joins(a ...string) string {
return strings.Join(a, "")
}
func sprint(v ...interface{}) string {
return fmt.Sprint(v...)
}
func snakeToCamel(s string) string {
ss := strings.Split(s, "_")
for i := range ss {
s := ss[i]
if strings.ToUpper(s) == s {
s = strings.ToLower(s)
}
ss[i] = strings.Title(s)
}
return join(ss, "")
}
func upperName(s string, re *regexp.Regexp) string {
if re != nil {
m := re.FindStringSubmatch(s)
if len(m) > 1 && len(m[1]) > 2 {
s = m[1]
}
}
if len(s) > 3 {
s = trimSuffix(s, "_t")
}
return snakeToCamel(s)
}
func hasPrefix(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}
func snakeToLowerCamel(s string) string {
if len(s) <= 1 {
return s
}
s = snakeToCamel(s)
return strings.ToLower(s[:1]) + s[1:]
}
func spaceToSnake(s string) string {
return strings.Replace(s, " ", "_", -1)
}
func typeSizeAssert(ctype, gotype string) string {
return fmt.Sprintf(
` if s1, s2 := unsafe.Sizeof(%[1]s), unsafe.Sizeof(%[2]s); s1 != s2 {
panic(fmt.Errorf("Size of %[1]s is %%d, expected %%d as Go's %[2]s.", s1, s2))
}
`, ctype, gotype)
}
type SSet struct {
m map[string]struct{}
}
func NewSSet() SSet {
return SSet{make(map[string]struct{})}
}
func (m *SSet) Len() int {
return len(m.m)
}
func (m *SSet) IsNil() bool {
return m.m == nil
}
func (m *SSet) Add(ss ...string) {
for _, s := range ss {
m.m[s] = struct{}{}
}
}
func (m *SSet) Del(s string) {
delete(m.m, s)
}
func (m *SSet) Has(s string) bool {
if m.m == nil {
return false
}
_, has := m.m[s]
return has
}
func contains(s, substr string) bool {
return strings.Contains(s, substr)
}
func replace(s, old, new string) string {
return strings.Replace(s, old, new, -1)
}
func trimPreSuffix(s, fix string) string {
return strings.TrimSuffix(strings.TrimPrefix(s, fix), fix)
}
func trimSuffix(s, suffix string) string {
return strings.TrimSuffix(s, suffix)
}
func remove(s, substr string) string {
return strings.Replace(s, substr, "", -1)
}
func atoi(s string) int {
if s == "" {
return 0
}
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}
func fileExists(file string) bool {
f, err := os.Open(file)
if err != nil {
return false
}
f.Close()
return true
}
type cmd struct {
*exec.Cmd
}
func newCmd(name string, arg ...string) cmd {
return cmd{exec.Command(name, arg...)}
}
func (c cmd) read(visit func(r io.Reader) error) error {
stdout, err := c.StdoutPipe()
if err != nil {
return err
}
defer stdout.Close()
stderr, err := c.StderrPipe()
if err != nil {
return err
}
defer stderr.Close()
if err := c.Start(); err != nil {
return err
}
go io.Copy(os.Stderr, stderr)
if err := visit(stdout); err != nil {
return err
}
if err := c.Wait(); err != nil {
return err
}
return nil
}
func (c cmd) exec() error {
stdout, err := c.StdoutPipe()
if err != nil {
return err
}
defer stdout.Close()
stderr, err := c.StderrPipe()
if err != nil {
return err
}
defer stderr.Close()
if err := c.Start(); err != nil {
return err
}
go io.Copy(os.Stderr, stderr)
go io.Copy(os.Stdout, stdout)
if err := c.Wait(); err != nil {
return err
}
return nil
}
func writeToString(write func(w io.Writer)) string {
var buf bytes.Buffer
write(&buf)
return buf.String()
}
func pt(v interface{}) {
p(reflect.TypeOf(v))
}
func hex(i int, length int) string {
if i < 0 {
return strconv.FormatInt(int64(i), 10)
}
s := strings.ToUpper(strconv.FormatInt(int64(i), 16))
if len(s)+2 < length {
s = strings.Repeat("0", length-len(s)-2) + s
}
return "0x" + s
}