An asked-for-in #go-nuts extension to quickly create a repeated

copy of a string or a byte array.
        strings.Repeat("-", 50)
	bytes.Repeat(b, 99)

R=rsc
https://golang.org/cl/155063
This commit is contained in:
David G. Andersen 2009-11-16 12:40:01 -08:00 committed by Russ Cox
parent 11c1aa9f6d
commit 37f71e8ad6
4 changed files with 79 additions and 0 deletions

View file

@ -188,6 +188,20 @@ func Map(mapping func(rune int) int, s string) string {
return string(b[0:nbytes]);
}
// Repeat returns a new string consisting of count copies of the string s.
func Repeat(s string, count int) string {
b := make([]byte, len(s)*count);
bp := 0;
for i := 0; i < count; i++ {
for j := 0; j < len(s); j++ {
b[bp] = s[j];
bp++;
}
}
return string(b);
}
// ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case.
func ToUpper(s string) string { return Map(unicode.ToUpper, s) }