From 37f71e8ad644c0e91bb4449882f60b95c7d4644a Mon Sep 17 00:00:00 2001 From: "David G. Andersen" Date: Mon, 16 Nov 2009 12:40:01 -0800 Subject: 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 --- src/pkg/bytes/bytes.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/pkg/bytes/bytes.go') diff --git a/src/pkg/bytes/bytes.go b/src/pkg/bytes/bytes.go index f6cae73537..0c585bd80f 100644 --- a/src/pkg/bytes/bytes.go +++ b/src/pkg/bytes/bytes.go @@ -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) } -- cgit v1.3-5-g9baa