github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/strutil/chop.go (about) 1 package strutil 2 3 // ChopLineEnding removes a line ending ("\r\n" or "\n") from the end of s. It 4 // returns s if it doesn't end with a line ending. 5 func ChopLineEnding(s string) string { 6 if len(s) >= 2 && s[len(s)-2:] == "\r\n" { // Windows line ending 7 return s[:len(s)-2] 8 } else if len(s) >= 1 && s[len(s)-1] == '\n' { // UNIX line ending 9 return s[:len(s)-1] 10 } 11 return s 12 } 13 14 // ChopTerminator removes a specific terminator byte from the end of s. It 15 // returns s if it doesn't end with the specified terminator. 16 func ChopTerminator(s string, terminator byte) string { 17 if len(s) >= 1 && s[len(s)-1] == terminator { 18 return s[:len(s)-1] 19 } 20 return s 21 }