]> Cypherpunks repositories - gostls13.git/commitdiff
bytes, strings: simplify Join
authorEvan Shaw <chickencha@gmail.com>
Tue, 29 Mar 2011 05:27:38 +0000 (01:27 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 29 Mar 2011 05:27:38 +0000 (01:27 -0400)
R=gri, rsc
CC=golang-dev
https://golang.org/cl/4300044

src/pkg/bytes/bytes.go
src/pkg/strings/strings.go

index bfe2ef39db500adb860c90f1ba2c4d607cd7bce7..c12a135738328e7853197caec22b5dd75a8e3ec3 100644 (file)
@@ -293,20 +293,10 @@ func Join(a [][]byte, sep []byte) []byte {
        }
 
        b := make([]byte, n)
-       bp := 0
-       for i := 0; i < len(a); i++ {
-               s := a[i]
-               for j := 0; j < len(s); j++ {
-                       b[bp] = s[j]
-                       bp++
-               }
-               if i+1 < len(a) {
-                       s = sep
-                       for j := 0; j < len(s); j++ {
-                               b[bp] = s[j]
-                               bp++
-                       }
-               }
+       bp := copy(b, a[0])
+       for _, s := range a[1:] {
+               bp += copy(b[bp:], sep)
+               bp += copy(b[bp:], s)
        }
        return b
 }
index 44dcf99b652a893a0207808d24c006860cddc356..93c7c46473882c6cf115d9d6c41a5488d7da2a83 100644 (file)
@@ -275,20 +275,10 @@ func Join(a []string, sep string) string {
        }
 
        b := make([]byte, n)
-       bp := 0
-       for i := 0; i < len(a); i++ {
-               s := a[i]
-               for j := 0; j < len(s); j++ {
-                       b[bp] = s[j]
-                       bp++
-               }
-               if i+1 < len(a) {
-                       s = sep
-                       for j := 0; j < len(s); j++ {
-                               b[bp] = s[j]
-                               bp++
-                       }
-               }
+       bp := copy(b, a[0])
+       for _, s := range a[1:] {
+               bp += copy(b[bp:], sep)
+               bp += copy(b[bp:], s)
        }
        return string(b)
 }