From: Jonathan Amsterdam Date: Tue, 21 Mar 2023 22:33:46 +0000 (-0400) Subject: slog: eliminate needsQuotingSet X-Git-Tag: go1.21rc1~1191 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=bd20bf4807fe38b81b5cdf9159b2cd29d9990811;p=gostls13.git slog: eliminate needsQuotingSet Delete the set of bytes that need quoting in TextHandler, because it is almost identical to the set for JSON. Use JSONHandler's safeSet with a few exceptions. Updates #56345. Change-Id: Iff6d309c067affef2e5ecfcebd6e1bb8f00f95b9 Reviewed-on: https://go-review.googlesource.com/c/go/+/478198 Reviewed-by: Ian Lance Taylor Run-TryBot: Jonathan Amsterdam TryBot-Result: Gopher Robot --- diff --git a/src/log/slog/text_handler.go b/src/log/slog/text_handler.go index 0faa367040..307d5c9d75 100644 --- a/src/log/slog/text_handler.go +++ b/src/log/slog/text_handler.go @@ -137,7 +137,9 @@ func needsQuoting(s string) bool { for i := 0; i < len(s); { b := s[i] if b < utf8.RuneSelf { - if needsQuotingSet[b] { + // Quote anything except a backslash that would need quoting in a + // JSON string, as well as space and '=' + if b != '\\' && (b == ' ' || b == '=' || !safeSet[b]) { return true } i++ @@ -151,17 +153,3 @@ func needsQuoting(s string) bool { } return false } - -var needsQuotingSet = [utf8.RuneSelf]bool{ - '"': true, - '=': true, -} - -func init() { - for i := 0; i < utf8.RuneSelf; i++ { - r := rune(i) - if unicode.IsSpace(r) || !unicode.IsPrint(r) { - needsQuotingSet[i] = true - } - } -}