]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cover: simplify and correct isValidIdentifier
authorIan Lance Taylor <iant@golang.org>
Tue, 11 Dec 2018 03:35:01 +0000 (19:35 -0800)
committerIan Lance Taylor <iant@golang.org>
Tue, 11 Dec 2018 15:41:17 +0000 (15:41 +0000)
Per comment on CL 120316.

Updates #25280

Change-Id: I7d078de4030bd10934468e04ff696a34749bd454
Reviewed-on: https://go-review.googlesource.com/c/153500
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/cmd/cover/cover.go

index 7f473a233c419b6437c19cc099f31e6ace2217e4..425bcbdd2660a64ce7341ea5a5b48491d5e71b70 100644 (file)
@@ -118,7 +118,7 @@ func parseFlags() error {
        }
 
        if *varVar != "" && !isValidIdentifier(*varVar) {
-               return fmt.Errorf("argument of -var is not a valid identifier: %v", *varVar)
+               return fmt.Errorf("-var: %q is not a valid identifier", *varVar)
        }
 
        if *mode != "" {
@@ -683,12 +683,17 @@ func (f *File) addVariables(w io.Writer) {
 }
 
 func isValidIdentifier(ident string) bool {
-       first := true
-       for _, c := range ident {
-               if !unicode.IsLetter(c) && c != '_' && (first || !unicode.IsDigit(c)) {
-                       return false // invalid identifier
+       if len(ident) == 0 {
+               return false
+       }
+       for i, c := range ident {
+               if i > 0 && unicode.IsDigit(c) {
+                       continue
+               }
+               if c == '_' || unicode.IsLetter(c) {
+                       continue
                }
-               first = false
+               return false
        }
        return true
 }