]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/base: optimize the readImportCfg function to make it clearer
authorcui fliter <imcusg@gmail.com>
Sat, 5 Aug 2023 15:52:52 +0000 (15:52 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 7 Aug 2023 00:22:01 +0000 (00:22 +0000)
Change-Id: If8fc77d5b04b2979707032d91112d4f33ef5e3da
GitHub-Last-Rev: d82d769c04569af8a0c99a0ba9a78db641b97368
GitHub-Pull-Request: golang/go#55913
Reviewed-on: https://go-review.googlesource.com/c/go/+/435536
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: shuang cui <imcusg@gmail.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/base/flag.go

index 753a60ae1e591b53620295a431c22f14dba1a1dd..7bd27c92a3db65526d108a91b96eb8463797a914 100644 (file)
@@ -470,26 +470,22 @@ func readImportCfg(file string) {
                        continue
                }
 
-               var verb, args string
-               if i := strings.Index(line, " "); i < 0 {
-                       verb = line
-               } else {
-                       verb, args = line[:i], strings.TrimSpace(line[i+1:])
-               }
-               var before, after string
-               if i := strings.Index(args, "="); i >= 0 {
-                       before, after = args[:i], args[i+1:]
+               verb, args, found := strings.Cut(line, " ")
+               if found {
+                       args = strings.TrimSpace(args)
                }
+               before, after, hasEq := strings.Cut(args, "=")
+
                switch verb {
                default:
                        log.Fatalf("%s:%d: unknown directive %q", file, lineNum, verb)
                case "importmap":
-                       if before == "" || after == "" {
+                       if !hasEq || before == "" || after == "" {
                                log.Fatalf(`%s:%d: invalid importmap: syntax is "importmap old=new"`, file, lineNum)
                        }
                        Flag.Cfg.ImportMap[before] = after
                case "packagefile":
-                       if before == "" || after == "" {
+                       if !hasEq || before == "" || after == "" {
                                log.Fatalf(`%s:%d: invalid packagefile: syntax is "packagefile path=filename"`, file, lineNum)
                        }
                        Flag.Cfg.PackageFile[before] = after