mirror of
https://github.com/golang/go
synced 2024-11-23 06:00:08 -07:00
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: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
9b95035654
commit
f6a163177f
@ -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
|
||||
}
|
||||
first = false
|
||||
if c == '_' || unicode.IsLetter(c) {
|
||||
continue
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user