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

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