]> Cypherpunks repositories - gostls13.git/commitdiff
go/token: use array instead of map for token->string table
authorRobert Griesemer <gri@golang.org>
Mon, 28 Mar 2011 20:38:24 +0000 (13:38 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 28 Mar 2011 20:38:24 +0000 (13:38 -0700)
R=rsc
CC=golang-dev
https://golang.org/cl/4284070

src/pkg/go/token/token.go

index 2a2d3ecc4fb99afdc7839be118b436f9dd3eb8a8..a5f21df168e7db04d324e1649517ca4388bee4b8 100644 (file)
@@ -126,10 +126,7 @@ const (
 )
 
 
-// At the moment we have no array literal syntax that lets us describe
-// the index for each element - use a map for now to make sure they are
-// in sync.
-var tokens = map[Token]string{
+var tokens = [...]string{
        ILLEGAL: "ILLEGAL",
 
        EOF:     "EOF",
@@ -237,10 +234,14 @@ var tokens = map[Token]string{
 // constant name (e.g. for the token IDENT, the string is "IDENT").
 //
 func (tok Token) String() string {
-       if str, exists := tokens[tok]; exists {
-               return str
+       s := ""
+       if 0 <= tok && tok < Token(len(tokens)) {
+               s = tokens[tok]
        }
-       return "token(" + strconv.Itoa(int(tok)) + ")"
+       if s == "" {
+               s = "token(" + strconv.Itoa(int(tok)) + ")"
+       }
+       return s
 }