]> Cypherpunks repositories - gostls13.git/commitdiff
An asked-for-in #go-nuts extension to quickly create a repeated
authorDavid G. Andersen <dave.andersen@gmail.com>
Mon, 16 Nov 2009 20:40:01 +0000 (12:40 -0800)
committerRuss Cox <rsc@golang.org>
Mon, 16 Nov 2009 20:40:01 +0000 (12:40 -0800)
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
src/pkg/bytes/bytes_test.go
src/pkg/strings/strings.go
src/pkg/strings/strings_test.go

index f6cae73537259588a1b234c016e064b459a85b85..0c585bd80f8725f038f5da40b850f50334715def 100644 (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) }
 
index dddaf5064e6eb597b1f1595c15cbde720262bbec..3e737cb3767d219d56f2b4b2473361cceff7f7be 100644 (file)
@@ -361,3 +361,30 @@ func TestAddByte(t *testing.T) {
                }
        }
 }
+
+type RepeatTest struct {
+       in, out string;
+       count   int;
+}
+
+var RepeatTests = []RepeatTest{
+       RepeatTest{"", "", 0},
+       RepeatTest{"", "", 1},
+       RepeatTest{"", "", 2},
+       RepeatTest{"-", "", 0},
+       RepeatTest{"-", "-", 1},
+       RepeatTest{"-", "----------", 10},
+       RepeatTest{"abc ", "abc abc abc ", 3},
+}
+
+func TestRepeat(t *testing.T) {
+       for _, tt := range RepeatTests {
+               tin := strings.Bytes(tt.in);
+               tout := strings.Bytes(tt.out);
+               a := Repeat(tin, tt.count);
+               if !Equal(a, tout) {
+                       t.Errorf("Repeat(%q, %d) = %q; want %q", tin, tt.count, a, tout);
+                       continue;
+               }
+       }
+}
index 055d7d1e99cf13858db3fae02441127a42143686..7ccfc5ca840a5584989e250c4fcb3abaca6affe4 100644 (file)
@@ -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) }
 
index 732da42421e831dc0cdcea6703c2096b078e6be0..0073f0d0ea233ddb5d9f7c5f8e867200b151ecaa 100644 (file)
@@ -336,3 +336,28 @@ func TestCaseConsistency(t *testing.T) {
                }
        */
 }
+
+type RepeatTest struct {
+       in, out string;
+       count   int;
+}
+
+var RepeatTests = []RepeatTest{
+       RepeatTest{"", "", 0},
+       RepeatTest{"", "", 1},
+       RepeatTest{"", "", 2},
+       RepeatTest{"-", "", 0},
+       RepeatTest{"-", "-", 1},
+       RepeatTest{"-", "----------", 10},
+       RepeatTest{"abc ", "abc abc abc ", 3},
+}
+
+func TestRepeat(t *testing.T) {
+       for _, tt := range RepeatTests {
+               a := Repeat(tt.in, tt.count);
+               if !equal("Repeat(s)", a, tt.out, t) {
+                       t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out);
+                       continue;
+               }
+       }
+}