-
Notifications
You must be signed in to change notification settings - Fork 183
/
strutil.go
164 lines (139 loc) · 3.53 KB
/
strutil.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
// Package strutil provide some string,char,byte util functions
package strutil
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"text/template"
)
// Position for padding string
const (
PosLeft uint8 = iota
PosRight
)
/*************************************************************
* String padding operation
*************************************************************/
// Padding a string.
func Padding(s, pad string, length int, pos uint8) string {
diff := len(s) - length
if diff >= 0 { // do not need padding.
return s
}
if pad == "" || pad == " " {
mark := ""
if pos == PosRight { // to right
mark = "-"
}
// padding left: "%7s", padding right: "%-7s"
tpl := fmt.Sprintf("%s%d", mark, length)
return fmt.Sprintf(`%`+tpl+`s`, s)
}
if pos == PosRight { // to right
return s + Repeat(pad, -diff)
}
return Repeat(pad, -diff) + s
}
// PadLeft a string.
func PadLeft(s, pad string, length int) string {
return Padding(s, pad, length, PosLeft)
}
// PadRight a string.
func PadRight(s, pad string, length int) string {
return Padding(s, pad, length, PosRight)
}
/*************************************************************
* String repeat operation
*************************************************************/
// Repeat a string
func Repeat(s string, times int) string {
if times <= 0 {
return ""
}
if times == 1 {
return s
}
ss := make([]string, 0, times)
for i := 0; i < times; i++ {
ss = append(ss, s)
}
return strings.Join(ss, "")
}
// RepeatRune repeat a rune char.
func RepeatRune(char rune, times int) (chars []rune) {
for i := 0; i < times; i++ {
chars = append(chars, char)
}
return
}
// RepeatBytes repeat a byte char.
func RepeatBytes(char byte, times int) (chars []byte) {
for i := 0; i < times; i++ {
chars = append(chars, char)
}
return
}
// Replaces replace multi strings
//
// pairs: {old1: new1, old2: new2, ...}
//
// Can also use:
// strings.NewReplacer("old1", "new1", "old2", "new2").Replace(str)
func Replaces(str string, pairs map[string]string) string {
ss := make([]string, len(pairs)*2)
for old, newVal := range pairs {
ss = append(ss, old, newVal)
}
return strings.NewReplacer(ss...).Replace(str)
}
// PrettyJSON get pretty Json string
// Deprecated: please use fmtutil.PrettyJSON() or jsonutil.Pretty() instead it
func PrettyJSON(v interface{}) (string, error) {
out, err := json.MarshalIndent(v, "", " ")
return string(out), err
}
// RenderTemplate render text template
func RenderTemplate(input string, data interface{}, fns template.FuncMap, isFile ...bool) string {
return RenderText(input, data, fns, isFile...)
}
// RenderText render text template
func RenderText(input string, data interface{}, fns template.FuncMap, isFile ...bool) string {
t := template.New("simple-text")
t.Funcs(template.FuncMap{
// don't escape content
"raw": func(s string) string {
return s
},
"trim": func(s string) string {
return strings.TrimSpace(s)
},
// join strings
"join": func(ss []string, sep string) string {
return strings.Join(ss, sep)
},
// lower first char
"lcFirst": func(s string) string {
return LowerFirst(s)
},
// upper first char
"upFirst": func(s string) string {
return UpperFirst(s)
},
})
// custom add template functions
if len(fns) > 0 {
t.Funcs(fns)
}
if len(isFile) > 0 && isFile[0] {
template.Must(t.ParseFiles(input))
} else {
template.Must(t.Parse(input))
}
// use buffer receive rendered content
buf := new(bytes.Buffer)
if err := t.Execute(buf, data); err != nil {
panic(err)
}
return buf.String()
}