Skip to content

Commit 42bd096

Browse files
jbaeric
authored andcommitted
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 golang#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>
1 parent 8ef6f92 commit 42bd096

File tree

1 file changed

+3
-15
lines changed

1 file changed

+3
-15
lines changed

src/log/slog/text_handler.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ func needsQuoting(s string) bool {
137137
for i := 0; i < len(s); {
138138
b := s[i]
139139
if b < utf8.RuneSelf {
140-
if needsQuotingSet[b] {
140+
// Quote anything except a backslash that would need quoting in a
141+
// JSON string, as well as space and '='
142+
if b != '\\' && (b == ' ' || b == '=' || !safeSet[b]) {
141143
return true
142144
}
143145
i++
@@ -151,17 +153,3 @@ func needsQuoting(s string) bool {
151153
}
152154
return false
153155
}
154-
155-
var needsQuotingSet = [utf8.RuneSelf]bool{
156-
'"': true,
157-
'=': true,
158-
}
159-
160-
func init() {
161-
for i := 0; i < utf8.RuneSelf; i++ {
162-
r := rune(i)
163-
if unicode.IsSpace(r) || !unicode.IsPrint(r) {
164-
needsQuotingSet[i] = true
165-
}
166-
}
167-
}

0 commit comments

Comments
 (0)