mirror of
https://github.com/golang/go
synced 2024-11-19 21:04:43 -07:00
bytes, strings: simplify Join
R=gri, rsc CC=golang-dev https://golang.org/cl/4300044
This commit is contained in:
parent
ac74f14b6b
commit
47f4ae1a78
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user