diff options
author | Joe Tsai <joetsai@digital-static.net> | 2022-06-15 17:50:04 -0700 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2022-08-23 20:24:43 +0000 |
commit | 723a27994ddc926a2078bcc23ccbc33857577b4c (patch) | |
tree | 21fd5b086f5f8589e80c010ad48588e453922fa3 /src/strings | |
parent | e7f2e5697ac8b9b6ebfb3e0d059a8c318b4709eb (diff) | |
download | go-723a27994ddc926a2078bcc23ccbc33857577b4c.tar.gz go-723a27994ddc926a2078bcc23ccbc33857577b4c.zip |
strings: rely on utf8.AppendRune
This is both simpler and more performant.
Change-Id: I66ef8e49c059a722932392ee3ecfb951d9b8e121
Reviewed-on: https://go-review.googlesource.com/c/go/+/412339
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/strings')
-rw-r--r-- | src/strings/builder.go | 15 |
1 files changed, 3 insertions, 12 deletions
diff --git a/src/strings/builder.go b/src/strings/builder.go index 3caddabd4e..096e9c765e 100644 --- a/src/strings/builder.go +++ b/src/strings/builder.go @@ -103,18 +103,9 @@ func (b *Builder) WriteByte(c byte) error { // It returns the length of r and a nil error. func (b *Builder) WriteRune(r rune) (int, error) { b.copyCheck() - // Compare as uint32 to correctly handle negative runes. - if uint32(r) < utf8.RuneSelf { - b.buf = append(b.buf, byte(r)) - return 1, nil - } - l := len(b.buf) - if cap(b.buf)-l < utf8.UTFMax { - b.grow(utf8.UTFMax) - } - n := utf8.EncodeRune(b.buf[l:l+utf8.UTFMax], r) - b.buf = b.buf[:l+n] - return n, nil + n := len(b.buf) + b.buf = utf8.AppendRune(b.buf, r) + return len(b.buf) - n, nil } // WriteString appends the contents of s to b's buffer. |