forked from thatguystone/cog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trim.go
55 lines (43 loc) · 850 Bytes
/
trim.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
package stringc
import "strings"
// TrimLines removes empty lines from both the left and right of s.
func TrimLines(s string) string {
return TrimRightLines(TrimLeftLines(s))
}
// TrimLeftLines removes empty lines from the left of s.
func TrimLeftLines(s string) string {
for len(s) > 0 {
i := strings.Index(s, "\n")
if i == -1 {
break
}
l := strings.TrimSpace(s[:i+1])
if len(l) > 0 {
break
}
s = s[i+1:]
}
return trimEmpty(s)
}
// TrimRightLines removes empty lines from the right of s.
func TrimRightLines(s string) string {
for len(s) > 0 {
i := strings.LastIndex(s, "\n")
if i == -1 {
break
}
l := strings.TrimSpace(s[i:])
if len(l) > 0 {
break
}
s = s[:i]
}
return trimEmpty(s)
}
func trimEmpty(s string) string {
ss := strings.TrimSpace(s)
if len(ss) == 0 {
s = ss
}
return s
}