From 361f5eba9f9e9902226e7edac76646253b7025e4 Mon Sep 17 00:00:00 2001 From: cui fliter Date: Tue, 27 Sep 2022 01:31:31 +0000 Subject: [PATCH] all: use strings.CutPrefix Updates #42537 Change-Id: Ice23d7d36bbede27551cbc086119694f6a3b5e4a GitHub-Last-Rev: 0d65208313ea318725159186fad045fc6400fb25 GitHub-Pull-Request: golang/go#55347 Reviewed-on: https://go-review.googlesource.com/c/go/+/432895 Reviewed-by: Benny Siegert Auto-Submit: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gopher Robot --- src/go/build/build.go | 5 +++-- src/go/printer/comment.go | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/go/build/build.go b/src/go/build/build.go index 36d9165125..bf779da992 100644 --- a/src/go/build/build.go +++ b/src/go/build/build.go @@ -179,10 +179,11 @@ func hasSubdir(root, dir string) (rel string, ok bool) { root += sep } dir = filepath.Clean(dir) - if !strings.HasPrefix(dir, root) { + after, found := strings.CutPrefix(dir, root) + if !found { return "", false } - return filepath.ToSlash(dir[len(root):]), true + return filepath.ToSlash(after), true } // readDir calls ctxt.ReadDir (if not nil) or else os.ReadDir. diff --git a/src/go/printer/comment.go b/src/go/printer/comment.go index 76dd31efc7..9012714939 100644 --- a/src/go/printer/comment.go +++ b/src/go/printer/comment.go @@ -36,15 +36,16 @@ func formatDocComment(list []*ast.Comment) []*ast.Comment { kind = "//" var b strings.Builder for _, c := range list { - if !strings.HasPrefix(c.Text, "//") { + after, found := strings.CutPrefix(c.Text, "//") + if !found { return list } // Accumulate //go:build etc lines separately. - if isDirective(c.Text[2:]) { + if isDirective(after) { directives = append(directives, c) continue } - b.WriteString(strings.TrimPrefix(c.Text[2:], " ")) + b.WriteString(strings.TrimPrefix(after, " ")) b.WriteString("\n") } text = b.String() -- 2.50.0