]> Cypherpunks repositories - gostls13.git/commitdiff
strings: move TrimPrefix and TrimSuffix to stringslite
authorapocelipes <seve3r@outlook.com>
Fri, 3 May 2024 08:36:03 +0000 (08:36 +0000)
committerGopher Robot <gobot@golang.org>
Fri, 3 May 2024 16:48:16 +0000 (16:48 +0000)
To help packages use these functions like "os" which using
the copied function "stringsTrimSuffix".

Change-Id: I223028ed264c7b7e95534b4883223af0988cda68
GitHub-Last-Rev: 2fd8fbf5286e5a4abdb03704d69f02e32d3f1a6b
GitHub-Pull-Request: golang/go#67151
Reviewed-on: https://go-review.googlesource.com/c/go/+/583075
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: qiu laidongfeng2 <2645477756@qq.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/internal/stringslite/strings.go
src/strings/strings.go

index ce8a913297bb47a1795363d6c80764297410948f..c0c6e2dce56e416889a7cb0926fda178f82827ec 100644 (file)
@@ -122,3 +122,17 @@ func CutSuffix(s, suffix string) (before string, found bool) {
        }
        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
+}
index d8cc09a24e165382fe85e887e8514218066ff86a..95180828f6b032c33b7cd5d1cd6c4f216dbba836 100644 (file)
@@ -1075,19 +1075,13 @@ func TrimSpace(s string) string {
 // TrimPrefix returns s without the provided leading prefix string.
 // If s doesn't start with prefix, s is returned unchanged.
 func TrimPrefix(s, prefix string) string {
-       if HasPrefix(s, prefix) {
-               return s[len(prefix):]
-       }
-       return s
+       return stringslite.TrimPrefix(s, prefix)
 }
 
 // TrimSuffix returns s without the provided trailing suffix string.
 // If s doesn't end with suffix, s is returned unchanged.
 func TrimSuffix(s, suffix string) string {
-       if HasSuffix(s, suffix) {
-               return s[:len(s)-len(suffix)]
-       }
-       return s
+       return stringslite.TrimSuffix(s, suffix)
 }
 
 // Replace returns a copy of the string s with the first n