mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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:
parent
11c1aa9f6d
commit
37f71e8ad6
4 changed files with 79 additions and 0 deletions
|
|
@ -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) }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue