return b.String()
}
+// According to static analysis, spaces, dashes, zeros, equals, and tabs
+// are the most commonly repeated string literal,
+// often used for display on fixed-width terminal windows.
+// Pre-declare constants for these for O(1) repetition in the common-case.
+const (
+ repeatedSpaces = "" +
+ " " +
+ " "
+ repeatedDashes = "" +
+ "----------------------------------------------------------------" +
+ "----------------------------------------------------------------"
+ repeatedZeroes = "" +
+ "0000000000000000000000000000000000000000000000000000000000000000"
+ repeatedEquals = "" +
+ "================================================================" +
+ "================================================================"
+ repeatedTabs = "" +
+ "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" +
+ "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
+)
+
// Repeat returns a new string consisting of count copies of the string s.
//
// It panics if count is negative or if the result of (len(s) * count)
return ""
}
+ // Optimize for commonly repeated strings of relatively short length.
+ switch s[0] {
+ case ' ', '-', '0', '=', '\t':
+ switch {
+ case n <= len(repeatedSpaces) && HasPrefix(repeatedSpaces, s):
+ return repeatedSpaces[:n]
+ case n <= len(repeatedDashes) && HasPrefix(repeatedDashes, s):
+ return repeatedDashes[:n]
+ case n <= len(repeatedZeroes) && HasPrefix(repeatedZeroes, s):
+ return repeatedZeroes[:n]
+ case n <= len(repeatedEquals) && HasPrefix(repeatedEquals, s):
+ return repeatedEquals[:n]
+ case n <= len(repeatedTabs) && HasPrefix(repeatedTabs, s):
+ return repeatedTabs[:n]
+ }
+ }
+
// Past a certain chunk size it is counterproductive to use
// larger chunks as the source of the write, as when the source
// is too large we are basically just thrashing the CPU D-cache.
}
var longString = "a" + string(make([]byte, 1<<16)) + "z"
+var longSpaces = func() string {
+ b := make([]byte, 200)
+ for i := range b {
+ b[i] = ' '
+ }
+ return string(b)
+}()
var RepeatTests = []struct {
in, out string
{"-", "-", 1},
{"-", "----------", 10},
{"abc ", "abc abc abc ", 3},
+ {" ", " ", 1},
+ {"--", "----", 2},
+ {"===", "======", 2},
+ {"000", "000000000", 3},
+ {"\t\t\t\t", "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t", 4},
+ {" ", longSpaces, len(longSpaces)},
// Tests for results over the chunkLimit
{string(rune(0)), string(make([]byte, 1<<16)), 1 << 16},
{longString, longString + longString, 2},
}
}
+func BenchmarkRepeatSpaces(b *testing.B) {
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ Repeat(" ", 2)
+ }
+}
+
func BenchmarkIndexAnyASCII(b *testing.B) {
x := Repeat("#", 2048) // Never matches set
cs := "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"