1
0
mirror of https://github.com/golang/go synced 2024-11-06 01:36:10 -07:00
go/cmd/stringer
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
..
testdata tools/cmd/stringer: fake import C. 2014-11-27 15:36:14 +11:00
endtoend_test.go tools/cmd/vet: check that cgo is enabled before testing it 2014-12-02 08:15:26 +09:00
golden_test.go stringer: avoid if's in the generated code 2014-12-22 20:58:05 +00:00
stringer.go stringer: avoid if's in the generated code 2014-12-22 20:58:05 +00:00
util_test.go go.tools/cmd/stringer: add tests 2014-09-04 14:16:59 -07:00