-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.go
68 lines (54 loc) · 1.15 KB
/
misc.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
package csvtool
import (
"strings"
)
func isWrappedWith(s, prefix, suffix string) bool {
return strings.HasPrefix(s, prefix) && strings.HasSuffix(s, suffix)
}
func isWrappedWithSQ(s string) bool {
return isWrappedWith(s, "'", "'")
}
func isWrappedWithDQ(s string) bool {
return isWrappedWith(s, "\"", "\"")
}
func tryWrap(s, prefix, suffix string) string {
if !isWrappedWith(s, prefix, suffix) {
return prefix + s + suffix
}
return s
}
func tryWrapWithSQ(s string) string {
return tryWrap(s, "'", "'")
}
func tryWrapWithDQ(s string) string {
return tryWrap(s, "\"", "\"")
}
////////////////////////////////////////////////
func tryStrip(s string, n int) string {
if n < 0 {
panic("[n] must >= 0")
}
if len(s) >= 2*n {
return s[n : len(s)-n]
}
return s
}
func tryStripSQ(s string) string {
if isWrappedWithSQ(s) {
return tryStrip(s, 1)
}
return s
}
func tryStripDQ(s string) string {
if isWrappedWithDQ(s) {
return tryStrip(s, 1)
}
return s
}
////////////////////////////////////////////////
func isBlank(s string) bool {
return len(strings.Trim(s, " \t\n")) == 0
}
func trimBlank(s string) string {
return strings.Trim(s, " \t\n")
}