1
0
mirror of https://github.com/golang/go synced 2024-11-18 20:34:39 -07:00
go/cmd
Rob Pike 575b88be8f stringer: avoid if's in the generated code
Suggestion by dsymonds: Save code not data.
Add an extra element to the index array and an if can be eliminated.

Old generated code:
const _Day_name = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"

var _Day_index = [...]uint8{6, 13, 22, 30, 36, 44, 50}

func (i Day) String() string {
	if i < 0 || i >= Day(len(_Day_index)) {
		return fmt.Sprintf("Day(%d)", i)
	}
	hi := _Day_index[i]
	lo := uint8(0)
	if i > 0 {
		lo = _Day_index[i-1]
	}
	return _Day_name[lo:hi]
}

New generated code:
const _Day_name = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"

var _Day_index = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50}

func (i Day) String() string {
	if i < 0 || i+1 >= Day(len(_Day_index)) {
		return fmt.Sprintf("Day(%d)", i)
	}
	return _Day_name[_Day_index[i]:_Day_index[i+1]]
}

Change-Id: I6f46a4892d5813a12ec1ad01738c6a21c7e45172
Reviewed-on: https://go-review.googlesource.com/1990
Reviewed-by: David Symonds <dsymonds@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
2014-12-22 20:58:05 +00:00
..
benchcmp tools: add import comments. 2014-12-09 22:42:16 +00:00
callgraph tools: add import comments. 2014-12-09 22:42:16 +00:00
cover tools: add import comments. 2014-12-09 22:42:16 +00:00
digraph tools: add import comments. 2014-12-09 22:42:16 +00:00
eg cmd/eg, godoc/redirect: update remaining code.google.com links 2014-12-18 07:48:40 +00:00
godex tools: add import comments. 2014-12-09 22:42:16 +00:00
godoc cmd/godoc: add missing space to flag description 2014-12-19 19:42:26 +00:00
goimports all: update links from code.google.com to new homes 2014-12-12 03:10:55 +00:00
gorename tools: add import comments. 2014-12-09 22:42:16 +00:00
gotype tools: add import comments. 2014-12-09 22:42:16 +00:00
html2article tools: add import comments. 2014-12-09 22:42:16 +00:00
oracle tools: add import comments. 2014-12-09 22:42:16 +00:00
present cmd/present: skip the "golang.org" directory 2014-12-14 23:37:49 +00:00
ssadump tools: add import comments. 2014-12-09 22:42:16 +00:00
stringer stringer: avoid if's in the generated code 2014-12-22 20:58:05 +00:00
tipgodoc cmd/tipgodoc: add appenginevm build tag 2014-12-18 22:13:11 +00:00
vet tools: add import comments. 2014-12-09 22:42:16 +00:00