-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
172 lines (152 loc) · 4.22 KB
/
string.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
package gotemplate
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/kennygrant/sanitize"
)
// slugify
func urlPath(title string) string {
reg, err := regexp.Compile("[^A-Za-z0-9]+")
if err != nil {
return ""
}
prettyurl := reg.ReplaceAllString(title, "-")
prettyurl = strings.ToLower(strings.Trim(prettyurl, "-"))
return prettyurl
}
func reReplaceAll(pattern, repl, text string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(text, repl)
}
func empty(a interface{}) interface{} {
k := reflect.ValueOf(a).Kind()
if k == reflect.Int || k == reflect.Int16 || k == reflect.Int32 ||
k == reflect.Int64 || k == reflect.Int8 || k == reflect.Bool ||
k == reflect.Float32 || k == reflect.Float64 || k == reflect.Uint ||
k == reflect.Uint16 || k == reflect.Uint32 || k == reflect.Uint64 ||
k == reflect.Uint8 || k == reflect.Func {
return a
}
v := reflect.ValueOf(a)
if a == nil ||
(k == reflect.Slice && v.Len() < 1) ||
(k == reflect.Struct && v.NumField() < 1) ||
(k == reflect.Map && v.Len() < 1) {
return ""
}
if k == reflect.Struct {
p := fmt.Sprintf("%#v", a)
if p == "[]interface {}{}" || p == "map[string]interface {}{}" {
return ""
}
}
if k == reflect.Slice {
for i := 0; i < v.Len(); i++ {
if empty(v.Index(i)) != "" {
return a
}
}
return ""
}
if k == reflect.Map {
for _, mk := range v.MapKeys() {
if empty(v.MapIndex(mk)) != "" {
return a
}
}
}
return a
}
func emptyStr(a interface{}) string {
k := reflect.ValueOf(a).Kind()
if k == reflect.Int || k == reflect.Int16 || k == reflect.Int32 ||
k == reflect.Int64 || k == reflect.Int8 || k == reflect.Bool ||
k == reflect.Float32 || k == reflect.Float64 || k == reflect.Uint ||
k == reflect.Uint16 || k == reflect.Uint32 || k == reflect.Uint64 ||
k == reflect.Uint8 || k == reflect.Func {
return fmt.Sprintf("%v", a)
}
if a == nil || reflect.ValueOf(a).Len() < 1 {
return ""
}
return fmt.Sprintf("%v", a)
}
func sanitise(str string) string {
return sanitize.Name(strings.Replace(str, "/", " ", -1))
}
func limit(data interface{}, length int) interface{} {
switch reflect.ValueOf(data).Kind() {
case reflect.String:
if len(data.(string)) > length {
return data.(string)[:length]
}
return data
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return fmt.Sprintf(fmt.Sprintf("%%-%dd", length), data)
case reflect.Float32, reflect.Float64:
return fmt.Sprintf(fmt.Sprintf("%%-%d.4f", length), data)
}
return data
}
func fixlen(length int, data interface{}) interface{} {
switch reflect.ValueOf(data).Kind() {
case reflect.String:
return fmt.Sprintf(fmt.Sprintf("%%-%d.%ds", length, length), data)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return fmt.Sprintf(fmt.Sprintf("%%-%d.%dd", length, length), data)
case reflect.Float32, reflect.Float64:
return fmt.Sprintf(fmt.Sprintf("%%-%d.4f", length), data)
}
return strings.Repeat(" ", length)
}
func fixlenright(length int, data interface{}) interface{} {
switch reflect.ValueOf(data).Kind() {
case reflect.String:
return fmt.Sprintf(fmt.Sprintf("%%%d.%ds", length, length), data)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return fmt.Sprintf(fmt.Sprintf("%%%d.%dd", length, length), data)
case reflect.Float32, reflect.Float64:
return fmt.Sprintf(fmt.Sprintf("%%%d.4f", length), data)
}
return strings.Repeat(" ", length)
}
func concat(ss ...string) string {
return strings.Join(ss, "")
}
func toint(s string) int {
i, _ := strconv.Atoi(s)
return i
}
func tofloat(s string) (float64, error) {
if s == "" {
s = "0.0"
}
return strconv.ParseFloat(s, 64)
}
func conditional(s1, s2 string) string {
if s1 != "" {
return s1
}
return s2
}
func notconditional(s1, s2 string) string {
if s1 == "" {
return s1
}
return s2
}
func contains(substr string, str string) bool {
return strings.Contains(str, substr)
}
func hasPrefix(substr string, str string) bool {
return strings.HasPrefix(str, substr)
}
func hasSuffix(substr string, str string) bool {
return strings.HasSuffix(str, substr)
}
func regReplaceAll(replaceRegex, input string) string {
return regexp.MustCompile(replaceRegex).ReplaceAllString(input, "")
}