mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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>
This commit is contained in:
parent
e7f2e5697a
commit
723a27994d
1 changed files with 3 additions and 12 deletions
|
|
@ -103,18 +103,9 @@ func (b *Builder) WriteByte(c byte) error {
|
||||||
// It returns the length of r and a nil error.
|
// It returns the length of r and a nil error.
|
||||||
func (b *Builder) WriteRune(r rune) (int, error) {
|
func (b *Builder) WriteRune(r rune) (int, error) {
|
||||||
b.copyCheck()
|
b.copyCheck()
|
||||||
// Compare as uint32 to correctly handle negative runes.
|
n := len(b.buf)
|
||||||
if uint32(r) < utf8.RuneSelf {
|
b.buf = utf8.AppendRune(b.buf, r)
|
||||||
b.buf = append(b.buf, byte(r))
|
return len(b.buf) - n, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteString appends the contents of s to b's buffer.
|
// WriteString appends the contents of s to b's buffer.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue