From f72f4169133572cf62f1e872c5657cdbc4d5de2c Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 30 Mar 2011 15:33:16 +0000 Subject: Update to current Go library. From-SVN: r171732 --- libgo/go/strings/strings.go | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'libgo/go/strings/strings.go') diff --git a/libgo/go/strings/strings.go b/libgo/go/strings/strings.go index 5f009e5..93c7c46 100644 --- a/libgo/go/strings/strings.go +++ b/libgo/go/strings/strings.go @@ -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) } @@ -312,9 +302,19 @@ func Map(mapping func(rune int) int, s string) string { // fine. It could also shrink but that falls out naturally. maxbytes := len(s) // length of b nbytes := 0 // number of bytes encoded in b - b := make([]byte, maxbytes) - for _, c := range s { + // The output buffer b is initialized on demand, the first + // time a character differs. + var b []byte + + for i, c := range s { rune := mapping(c) + if b == nil { + if rune == c { + continue + } + b = make([]byte, maxbytes) + nbytes = copy(b, s[:i]) + } if rune >= 0 { wid := 1 if rune >= utf8.RuneSelf { @@ -330,6 +330,9 @@ func Map(mapping func(rune int) int, s string) string { nbytes += utf8.EncodeRune(b[nbytes:maxbytes], rune) } } + if b == nil { + return s + } return string(b[0:nbytes]) } -- cgit v1.1