]> Cypherpunks.ru repositories - gostls13.git/commitdiff
slog: eliminate needsQuotingSet
authorJonathan Amsterdam <jba@google.com>
Tue, 21 Mar 2023 22:33:46 +0000 (18:33 -0400)
committerJonathan Amsterdam <jba@google.com>
Wed, 22 Mar 2023 11:15:33 +0000 (11:15 +0000)
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 <iant@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/log/slog/text_handler.go

index 0faa3670401ccd9761cabb67c2a2e47a4b0b9342..307d5c9d75c56cb3ddbfa5ac7b86575842c14220 100644 (file)
@@ -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
-               }
-       }
-}