forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
45 lines (41 loc) · 804 Bytes
/
strings.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
package strings
import (
"sort"
"strings"
)
// Indent - indent each line of the string with the given indent string
func Indent(width int, indent, s string) string {
if width == 0 {
return s
}
if width > 1 {
indent = strings.Repeat(indent, width)
}
var res []byte
bol := true
for i := 0; i < len(s); i++ {
c := s[i]
if bol && c != '\n' {
res = append(res, indent...)
}
res = append(res, c)
bol = c == '\n'
}
return string(res)
}
// Trunc - truncate a string to the given length
func Trunc(length int, s string) string {
if length < 0 {
return s
}
if len(s) <= length {
return s
}
return s[0:length]
}
// Sort - return an alphanumerically-sorted list of strings
func Sort(list []string) []string {
sorted := sort.StringSlice(list)
sorted.Sort()
return sorted
}