mirror of https://github.com/golang/go.git
strings: move TrimPrefix and TrimSuffix to stringslite
To help packages use these funcions like "os" use stringsTrimSuffix.
This commit is contained in:
parent
a3eb55cea9
commit
2fd8fbf528
|
|
@ -122,3 +122,17 @@ func CutSuffix(s, suffix string) (before string, found bool) {
|
||||||
}
|
}
|
||||||
return s[:len(s)-len(suffix)], true
|
return s[:len(s)-len(suffix)], true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TrimPrefix(s, prefix string) string {
|
||||||
|
if HasPrefix(s, prefix) {
|
||||||
|
return s[len(prefix):]
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func TrimSuffix(s, suffix string) string {
|
||||||
|
if HasSuffix(s, suffix) {
|
||||||
|
return s[:len(s)-len(suffix)]
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1075,19 +1075,13 @@ func TrimSpace(s string) string {
|
||||||
// TrimPrefix returns s without the provided leading prefix string.
|
// TrimPrefix returns s without the provided leading prefix string.
|
||||||
// If s doesn't start with prefix, s is returned unchanged.
|
// If s doesn't start with prefix, s is returned unchanged.
|
||||||
func TrimPrefix(s, prefix string) string {
|
func TrimPrefix(s, prefix string) string {
|
||||||
if HasPrefix(s, prefix) {
|
return stringslite.TrimPrefix(s, prefix)
|
||||||
return s[len(prefix):]
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TrimSuffix returns s without the provided trailing suffix string.
|
// TrimSuffix returns s without the provided trailing suffix string.
|
||||||
// If s doesn't end with suffix, s is returned unchanged.
|
// If s doesn't end with suffix, s is returned unchanged.
|
||||||
func TrimSuffix(s, suffix string) string {
|
func TrimSuffix(s, suffix string) string {
|
||||||
if HasSuffix(s, suffix) {
|
return stringslite.TrimSuffix(s, suffix)
|
||||||
return s[:len(s)-len(suffix)]
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace returns a copy of the string s with the first n
|
// Replace returns a copy of the string s with the first n
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue