From 6b0013630ae216ce9cd9572b084f79c3317d9764 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 http://codereview.appspot.com/155063 Committer: Russ Cox --- src/pkg/strings/strings.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/pkg/strings/strings.go') diff --git a/src/pkg/strings/strings.go b/src/pkg/strings/strings.go index 055d7d1e9..7ccfc5ca8 100644 --- a/src/pkg/strings/strings.go +++ b/src/pkg/strings/strings.go @@ -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) } -- cgit v1.2.3