-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchop.go
57 lines (47 loc) · 1.1 KB
/
chop.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
package stringutil
import (
"strings"
)
// Chop returns a new String with the last character removed.
// If the string ends with \r\n, both characters are removed.
// Applying chop to an empty string returns an empty string.
func Chop(s string) string {
if s == "" {
return s
}
return chopChars(s)
}
func chopChars(s string) string {
if strHasWhitespaceSuffix(s) {
return chopWhitespace(s)
}
return chopPrintableChar(s)
}
func chopWhitespace(s string) string {
return strings.TrimSuffix(s, whitespaceSuffix(s))
}
func strHasWhitespaceSuffix(s string) bool {
return strings.HasSuffix(s, "\r") ||
strings.HasSuffix(s, "\n")
}
func chopPrintableChar(s string) string {
suffix := printableSuffix(s)
return strings.TrimSuffix(s, suffix)
}
func whitespaceSuffix(s string) string {
if strings.HasSuffix(s, "\r\n") {
return "\r\n"
}
return simpleWhitespaceSuffix(s)
}
func printableSuffix(s string) string {
return suffix(s)
}
func suffix(s string) string {
i := strings.LastIndex(s, "")
return s[i-1:]
}
func simpleWhitespaceSuffix(s string) string {
i := strings.LastIndex(s, "") - 1
return s[i:]
}