1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:04:42 -07:00

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>
This commit is contained in:
Rob Pike 2014-12-23 07:20:51 +11:00
parent 4d81e11d78
commit 575b88be8f
2 changed files with 25 additions and 69 deletions

View File

@ -48,18 +48,13 @@ const (
const day_out = `
const _Day_name = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"
var _Day_index = [...]uint8{6, 13, 22, 30, 36, 44, 50}
var _Day_index = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50}
func (i Day) String() string {
if i < 0 || i >= Day(len(_Day_index)) {
if i < 0 || i+1 >= 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]
return _Day_name[_Day_index[i]:_Day_index[i+1]]
}
`
@ -78,19 +73,14 @@ const (
const offset_out = `
const _Number_name = "OneTwoThree"
var _Number_index = [...]uint8{3, 6, 11}
var _Number_index = [...]uint8{0, 3, 6, 11}
func (i Number) String() string {
i -= 1
if i < 0 || i >= Number(len(_Number_index)) {
if i < 0 || i+1 >= Number(len(_Number_index)) {
return fmt.Sprintf("Number(%d)", i+1)
}
hi := _Number_index[i]
lo := uint8(0)
if i > 0 {
lo = _Number_index[i-1]
}
return _Number_name[lo:hi]
return _Number_name[_Number_index[i]:_Number_index[i+1]]
}
`
@ -116,27 +106,19 @@ const (
)
var (
_Gap_index_0 = [...]uint8{3, 8}
_Gap_index_1 = [...]uint8{4, 7, 12, 17, 21}
_Gap_index_2 = [...]uint8{6}
_Gap_index_0 = [...]uint8{0, 3, 8}
_Gap_index_1 = [...]uint8{0, 4, 7, 12, 17, 21}
_Gap_index_2 = [...]uint8{0, 6}
)
func (i Gap) String() string {
switch {
case 2 <= i && i <= 3:
i -= 2
lo := uint8(0)
if i > 0 {
lo = _Gap_index_0[i-1]
}
return _Gap_name_0[lo:_Gap_index_0[i]]
return _Gap_name_0[_Gap_index_0[i]:_Gap_index_0[i+1]]
case 5 <= i && i <= 9:
i -= 5
lo := uint8(0)
if i > 0 {
lo = _Gap_index_1[i-1]
}
return _Gap_name_1[lo:_Gap_index_1[i]]
return _Gap_name_1[_Gap_index_1[i]:_Gap_index_1[i+1]]
case i == 11:
return _Gap_name_2
default:
@ -159,19 +141,14 @@ const (
const num_out = `
const _Num_name = "m_2m_1m0m1m2"
var _Num_index = [...]uint8{3, 6, 8, 10, 12}
var _Num_index = [...]uint8{0, 3, 6, 8, 10, 12}
func (i Num) String() string {
i -= -2
if i < 0 || i >= Num(len(_Num_index)) {
if i < 0 || i+1 >= Num(len(_Num_index)) {
return fmt.Sprintf("Num(%d)", i+-2)
}
hi := _Num_index[i]
lo := uint8(0)
if i > 0 {
lo = _Num_index[i-1]
}
return _Num_name[lo:hi]
return _Num_name[_Num_index[i]:_Num_index[i+1]]
}
`
@ -196,25 +173,17 @@ const (
)
var (
_Unum_index_0 = [...]uint8{2, 4, 6}
_Unum_index_1 = [...]uint8{3, 6}
_Unum_index_0 = [...]uint8{0, 2, 4, 6}
_Unum_index_1 = [...]uint8{0, 3, 6}
)
func (i Unum) String() string {
switch {
case 0 <= i && i <= 2:
lo := uint8(0)
if i > 0 {
lo = _Unum_index_0[i-1]
}
return _Unum_name_0[lo:_Unum_index_0[i]]
return _Unum_name_0[_Unum_index_0[i]:_Unum_index_0[i+1]]
case 253 <= i && i <= 254:
i -= 253
lo := uint8(0)
if i > 0 {
lo = _Unum_index_1[i-1]
}
return _Unum_name_1[lo:_Unum_index_1[i]]
return _Unum_name_1[_Unum_index_1[i]:_Unum_index_1[i+1]]
default:
return fmt.Sprintf("Unum(%d)", i)
}

View File

@ -519,7 +519,7 @@ func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix
nameConst := fmt.Sprintf("_%s_name%s = %q", typeName, suffix, b.String())
nameLen := b.Len()
b.Reset()
fmt.Fprintf(b, "_%s_index%s = [...]uint%d{", typeName, suffix, usize(nameLen))
fmt.Fprintf(b, "_%s_index%s = [...]uint%d{0, ", typeName, suffix, usize(nameLen))
for i, v := range indexes {
if i > 0 {
fmt.Fprintf(b, ", ")
@ -563,15 +563,10 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) {
// [2]: size of index element (8 for uint8 etc.)
// [3]: less than zero check (for signed types)
const stringOneRun = `func (i %[1]s) String() string {
if %[3]si >= %[1]s(len(_%[1]s_index)) {
if %[3]si+1 >= %[1]s(len(_%[1]s_index)) {
return fmt.Sprintf("%[1]s(%%d)", i)
}
hi := _%[1]s_index[i]
lo := uint%[2]d(0)
if i > 0 {
lo = _%[1]s_index[i-1]
}
return _%[1]s_name[lo:hi]
return _%[1]s_name[_%[1]s_index[i]:_%[1]s_index[i+1]]
}
`
@ -584,15 +579,10 @@ const stringOneRun = `func (i %[1]s) String() string {
*/
const stringOneRunWithOffset = `func (i %[1]s) String() string {
i -= %[2]s
if %[4]si >= %[1]s(len(_%[1]s_index)) {
if %[4]si+1 >= %[1]s(len(_%[1]s_index)) {
return fmt.Sprintf("%[1]s(%%d)", i + %[2]s)
}
hi := _%[1]s_index[i]
lo := uint%[3]d(0)
if i > 0 {
lo = _%[1]s_index[i-1]
}
return _%[1]s_name[lo : hi]
return _%[1]s_name[_%[1]s_index[i] : _%[1]s_index[i+1]]
}
`
@ -613,11 +603,8 @@ func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) {
if values[0].value != 0 {
g.Printf("\t\ti -= %s\n", &values[0])
}
g.Printf("\t\tlo := uint%d(0)\n", usize(len(values)))
g.Printf("\t\tif i > 0 {\n")
g.Printf("\t\t\tlo = _%s_index_%d[i-1]\n", typeName, i)
g.Printf("\t\t}\n")
g.Printf("\t\treturn _%s_name_%d[lo:_%s_index_%d[i]]\n", typeName, i, typeName, i)
g.Printf("\t\treturn _%s_name_%d[_%s_index_%d[i]:_%s_index_%d[i+1]]\n",
typeName, i, typeName, i, typeName, i)
}
g.Printf("\tdefault:\n")
g.Printf("\t\treturn fmt.Sprintf(\"%s(%%d)\", i)\n", typeName)