From bd20bf4807fe38b81b5cdf9159b2cd29d9990811 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 21 Mar 2023 18:33:46 -0400 Subject: [PATCH] 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 --- src/log/slog/text_handler.go | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) 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 - } - } -} -- 2.50.0