Skip to content

Commit

Permalink
slog: eliminate needsQuotingSet
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jba authored and eric committed Sep 7, 2023
1 parent 8ef6f92 commit 42bd096
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions src/log/slog/text_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand All @@ -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
}
}
}

0 comments on commit 42bd096

Please sign in to comment.