diff --git a/src/strings/strings.go b/src/strings/strings.go index 1dc4238522..d9c8936fc8 100644 --- a/src/strings/strings.go +++ b/src/strings/strings.go @@ -526,8 +526,11 @@ func Map(mapping func(rune) rune, s string) string { // It panics if count is negative or if // the result of (len(s) * count) overflows. func Repeat(s string, count int) string { - if count == 0 { + switch count { + case 0: return "" + case 1: + return s } // Since we cannot return an error on overflow, diff --git a/src/strings/strings_test.go b/src/strings/strings_test.go index 9e7fb85ddf..1cda6546d0 100644 --- a/src/strings/strings_test.go +++ b/src/strings/strings_test.go @@ -1807,7 +1807,7 @@ func BenchmarkSplitNMultiByteSeparator(b *testing.B) { func BenchmarkRepeat(b *testing.B) { s := "0123456789" for _, n := range []int{5, 10} { - for _, c := range []int{1, 2, 6} { + for _, c := range []int{0, 1, 2, 6} { b.Run(fmt.Sprintf("%dx%d", n, c), func(b *testing.B) { for i := 0; i < b.N; i++ { Repeat(s[:n], c)