From: Joe Tsai Date: Thu, 16 Jun 2022 00:39:59 +0000 (-0700) Subject: strconv: rely on utf8.AppendRune X-Git-Tag: go1.22rc1~1253 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2fcfdb96860855be0c88e10e3fd5bb858420cfe2;p=gostls13.git strconv: rely on utf8.AppendRune This is both simpler and more performant. Quote 268ns ± 5% 258ns ± 4% -3.70% (p=0.014 n=10+10) QuoteRune 28.9ns ± 3% 28.4ns ± 4% ~ (p=0.113 n=9+10) AppendQuote 165ns ± 3% 165ns ± 3% ~ (p=0.661 n=9+10) AppendQuoteRune 8.05ns ± 5% 7.75ns ± 7% ~ (p=0.065 n=10+9) Change-Id: Ib0ee332e970d4986026c05e5e0e368f41eff7977 Reviewed-on: https://go-review.googlesource.com/c/go/+/412338 TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Run-TryBot: Joseph Tsai Reviewed-by: Robert Griesemer --- diff --git a/src/strconv/quote.go b/src/strconv/quote.go index 1b5bddfeae..7c38433679 100644 --- a/src/strconv/quote.go +++ b/src/strconv/quote.go @@ -66,7 +66,6 @@ func appendQuotedRuneWith(buf []byte, r rune, quote byte, ASCIIonly, graphicOnly } func appendEscapedRune(buf []byte, r rune, quote byte, ASCIIonly, graphicOnly bool) []byte { - var runeTmp [utf8.UTFMax]byte if r == rune(quote) || r == '\\' { // always backslashed buf = append(buf, '\\') buf = append(buf, byte(r)) @@ -78,9 +77,7 @@ func appendEscapedRune(buf []byte, r rune, quote byte, ASCIIonly, graphicOnly bo return buf } } else if IsPrint(r) || graphicOnly && isInGraphicList(r) { - n := utf8.EncodeRune(runeTmp[:], r) - buf = append(buf, runeTmp[:n]...) - return buf + return utf8.AppendRune(buf, r) } switch r { case '\a': @@ -471,9 +468,7 @@ func unquote(in string, unescape bool) (out, rem string, err error) { if r < utf8.RuneSelf || !multibyte { buf = append(buf, byte(r)) } else { - var arr [utf8.UTFMax]byte - n := utf8.EncodeRune(arr[:], r) - buf = append(buf, arr[:n]...) + buf = utf8.AppendRune(buf, r) } }