-
Notifications
You must be signed in to change notification settings - Fork 1
/
chomp.go
70 lines (57 loc) · 1.36 KB
/
chomp.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
package stringutil
import (
"strings"
)
// Chomp Returns a new String
// with the given record separator(sep) removed
// from the end of string(s) if present.
// Chomp also removes carriage return characters
// (that is it will remove \n, \r, and \r\n).
// If sep is an empty string,
// it will remove all trailing newlines from the string.
func Chomp(s string, sep string) string {
if sep == "" {
return trimWhitespace(s)
}
return strings.TrimSuffix(s, sep)
}
func trimWhitespace(s string) string {
if isWhiteSpace(lastCharOf(s)) {
return trimCarriageReturnEtc(s)
}
return s
}
func trimCarriageReturnEtc(s string) string {
if !strings.HasSuffix(s, "\r\n") {
return trimReturnEtc(s)
}
return trimAllCarriageReturn(s)
}
func trimReturnEtc(s string) string {
if lastCharOf(s) == "\n" {
return trimNewLine(s)
}
return strings.TrimSuffix(s, "\r")
}
func trimNewLine(s string) string {
return strings.TrimSuffix(s, "\n")
}
func lastCharOf(s string) string {
i := len(s) - 1
return s[i:]
}
func isWhiteSpace(s string) bool {
return strings.ContainsAny(s, "\n\r")
}
func trimAllCarriageReturn(s string) string {
if s = chopReturn(s); hasSuffix(s, "\r\n") {
s = trimAllCarriageReturn(s)
}
return s
}
func chopReturn(s string) string {
return strings.TrimSuffix(s, "\r\n")
}
func hasSuffix(s string, suffix string) bool {
return strings.HasSuffix(s, suffix)
}