all: use strings.CutPrefix

Updates #42537

Change-Id: Ice23d7d36bbede27551cbc086119694f6a3b5e4a
GitHub-Last-Rev: 0d65208313
GitHub-Pull-Request: golang/go#55347
Reviewed-on: https://go-review.googlesource.com/c/go/+/432895
Reviewed-by: Benny Siegert <bsiegert@gmail.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
cui fliter 2022-09-27 01:31:31 +00:00 committed by Benny Siegert
parent 17f83e3473
commit 361f5eba9f
2 changed files with 7 additions and 5 deletions

View file

@ -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.

View file

@ -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()