diff options
| author | David G. Andersen <dave.andersen@gmail.com> | 2009-11-16 12:40:01 -0800 |
|---|---|---|
| committer | David G. Andersen <dave.andersen@gmail.com> | 2009-11-16 12:40:01 -0800 |
| commit | 6b0013630ae216ce9cd9572b084f79c3317d9764 (patch) | |
| tree | 99340e66d789345d771393ed19b5d8d16c6d9f21 /src/pkg/strings/strings.go | |
| parent | 24dee3b34bec5a5da304b4f748e99686c35c1941 (diff) | |
| download | golang-6b0013630ae216ce9cd9572b084f79c3317d9764.tar.gz | |
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 <rsc@golang.org>
Diffstat (limited to 'src/pkg/strings/strings.go')
| -rw-r--r-- | src/pkg/strings/strings.go | 14 |
1 files changed, 14 insertions, 0 deletions
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) } |
