From bd4635fd25596cdd56c1fb399c53b351d1a81f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 14 Nov 2017 11:28:52 +0000 Subject: [PATCH] cmd/stringer: use strconv instead of fmt In the generated code, we want to pull in as few dependencies as possible. fmt is heavier than strconv, and the latter can be used with a bit of extra code for the same results. More importantly, this will allow stringer to be used in some std packages that cannot import fmt, such as regexp/syntax. While strconv lies in L2 in deps_test.go, fmt lies in L4. This means that many other packages will also be able to use stringer where it could be useful, such as path/filepath, os/exec, or io/ioutil. Since some of these types may be 64-bit integers, use FormatInt instead of Itoa to avoid overflows with int on 32-bit. Also double-checked that the generated code is still formatted properly. Change-Id: Iffb3bd2df5c94407705689719240aca0c7474a89 Reviewed-on: https://go-review.googlesource.com/77473 Reviewed-by: Ian Lance Taylor --- cmd/stringer/golden_test.go | 14 +++++++------- cmd/stringer/stringer.go | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/stringer/golden_test.go b/cmd/stringer/golden_test.go index 166fc035..2c418d22 100644 --- a/cmd/stringer/golden_test.go +++ b/cmd/stringer/golden_test.go @@ -54,7 +54,7 @@ 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)-1) { - return fmt.Sprintf("Day(%d)", i) + return "Day(" + strconv.FormatInt(int64(i), 10) + ")" } return _Day_name[_Day_index[i]:_Day_index[i+1]] } @@ -80,7 +80,7 @@ var _Number_index = [...]uint8{0, 3, 6, 11} func (i Number) String() string { i -= 1 if i < 0 || i >= Number(len(_Number_index)-1) { - return fmt.Sprintf("Number(%d)", i+1) + return "Number(" + strconv.FormatInt(int64(i+1), 10) + ")" } return _Number_name[_Number_index[i]:_Number_index[i+1]] } @@ -124,7 +124,7 @@ func (i Gap) String() string { case i == 11: return _Gap_name_2 default: - return fmt.Sprintf("Gap(%d)", i) + return "Gap(" + strconv.FormatInt(int64(i), 10) + ")" } } ` @@ -148,7 +148,7 @@ 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)-1) { - return fmt.Sprintf("Num(%d)", i+-2) + return "Num(" + strconv.FormatInt(int64(i+-2), 10) + ")" } return _Num_name[_Num_index[i]:_Num_index[i+1]] } @@ -187,7 +187,7 @@ func (i Unum) String() string { i -= 253 return _Unum_name_1[_Unum_index_1[i]:_Unum_index_1[i+1]] default: - return fmt.Sprintf("Unum(%d)", i) + return "Unum(" + strconv.FormatInt(int64(i), 10) + ")" } } ` @@ -236,7 +236,7 @@ func (i Prime) String() string { if str, ok := _Prime_map[i]; ok { return str } - return fmt.Sprintf("Prime(%d)", i) + return "Prime(" + strconv.FormatInt(int64(i), 10) + ")" } ` @@ -259,7 +259,7 @@ var _Type_index = [...]uint8{0, 3, 9, 14, 18, 22, 28, 33} func (i Type) String() string { if i < 0 || i >= Type(len(_Type_index)-1) { - return fmt.Sprintf("Type(%d)", i) + return "Type(" + strconv.FormatInt(int64(i), 10) + ")" } return _Type_name[_Type_index[i]:_Type_index[i+1]] } diff --git a/cmd/stringer/stringer.go b/cmd/stringer/stringer.go index 2ae36e86..50dec936 100644 --- a/cmd/stringer/stringer.go +++ b/cmd/stringer/stringer.go @@ -128,7 +128,7 @@ func main() { g.Printf("\n") g.Printf("package %s", g.pkg.name) g.Printf("\n") - g.Printf("import \"fmt\"\n") // Used by all methods. + g.Printf("import \"strconv\"\n") // Used by all methods. // Run generate for each type. for _, typeName := range types { @@ -565,7 +565,7 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) { // [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)-1) { - return fmt.Sprintf("%[1]s(%%d)", i) + return "%[1]s(" + strconv.FormatInt(int64(i), 10) + ")" } return _%[1]s_name[_%[1]s_index[i]:_%[1]s_index[i+1]] } @@ -581,7 +581,7 @@ 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)-1) { - return fmt.Sprintf("%[1]s(%%d)", i + %[2]s) + return "%[1]s(" + strconv.FormatInt(int64(i + %[2]s), 10) + ")" } return _%[1]s_name[_%[1]s_index[i] : _%[1]s_index[i+1]] } @@ -608,7 +608,7 @@ func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) { typeName, i, typeName, i, typeName, i) } g.Printf("\tdefault:\n") - g.Printf("\t\treturn fmt.Sprintf(\"%s(%%d)\", i)\n", typeName) + g.Printf("\t\treturn \"%s(\" + strconv.FormatInt(int64(i), 10) + \")\"\n", typeName) g.Printf("\t}\n") g.Printf("}\n") } @@ -635,6 +635,6 @@ const stringMap = `func (i %[1]s) String() string { if str, ok := _%[1]s_map[i]; ok { return str } - return fmt.Sprintf("%[1]s(%%d)", i) + return "%[1]s(" + strconv.FormatInt(int64(i), 10) + ")" } `