Commit f6a16317 authored by Ian Lance Taylor's avatar Ian Lance Taylor

cmd/cover: simplify and correct isValidIdentifier

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: default avatarRob Pike <r@golang.org>
parent 9b950356
...@@ -118,7 +118,7 @@ func parseFlags() error { ...@@ -118,7 +118,7 @@ func parseFlags() error {
} }
if *varVar != "" && !isValidIdentifier(*varVar) { 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 != "" { if *mode != "" {
...@@ -683,12 +683,17 @@ func (f *File) addVariables(w io.Writer) { ...@@ -683,12 +683,17 @@ func (f *File) addVariables(w io.Writer) {
} }
func isValidIdentifier(ident string) bool { func isValidIdentifier(ident string) bool {
first := true if len(ident) == 0 {
for _, c := range ident { return false
if !unicode.IsLetter(c) && c != '_' && (first || !unicode.IsDigit(c)) { }
return false // invalid identifier for i, c := range ident {
if i > 0 && unicode.IsDigit(c) {
continue
}
if c == '_' || unicode.IsLetter(c) {
continue
} }
first = false return false
} }
return true return true
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment