Compare commits

...

4 Commits

Author SHA1 Message Date
tidwall bbf40bb0e4 Fix backspace and form-feed for Go 1.22 2024-02-14 20:51:40 -07:00
Josh Baker 711c6fe9ec
Merge pull request #348 from V02460/json_string
Encode \b and \f in JSON string as themselves
2024-02-14 19:46:13 -07:00
Kai A. Hiller 0f87896dc3 Test \b and \f JSON encoding explicitly 2024-02-14 21:15:12 +01:00
Kai A. Hiller b622071bec Encode \b and \f in JSON string as themselves 2024-02-14 21:14:05 +01:00
2 changed files with 16 additions and 7 deletions

View File

@ -1926,6 +1926,10 @@ func AppendJSONString(dst []byte, s string) []byte {
if s[i] < ' ' { if s[i] < ' ' {
dst = append(dst, '\\') dst = append(dst, '\\')
switch s[i] { switch s[i] {
case '\b':
dst = append(dst, 'b')
case '\f':
dst = append(dst, 'f')
case '\n': case '\n':
dst = append(dst, 'n') dst = append(dst, 'n')
case '\r': case '\r':

View File

@ -2578,6 +2578,10 @@ func TestJSONString(t *testing.T) {
testJSONString(t, s) testJSONString(t, s)
testJSONString(t, "R\xfd\xfc\a!\x82eO\x16?_\x0f\x9ab\x1dr") testJSONString(t, "R\xfd\xfc\a!\x82eO\x16?_\x0f\x9ab\x1dr")
testJSONString(t, "_\xb9\v\xad\xb3|X!\xb6\xd9U&\xa4\x1a\x95\x04") testJSONString(t, "_\xb9\v\xad\xb3|X!\xb6\xd9U&\xa4\x1a\x95\x04")
data, _ := json.Marshal("\b\f")
if (string(data) == "\"\\b\\f\"") {
// Go version 1.22+ encodes "\b" and "\f" correctly.
testJSONString(t, "\b\f")
rng := rand.New(rand.NewSource(time.Now().UnixNano())) rng := rand.New(rand.NewSource(time.Now().UnixNano()))
start := time.Now() start := time.Now()
var buf [16]byte var buf [16]byte
@ -2587,6 +2591,7 @@ func TestJSONString(t *testing.T) {
} }
testJSONString(t, string(buf[:])) testJSONString(t, string(buf[:]))
} }
}
} }
func TestIndexAtSymbol(t *testing.T) { func TestIndexAtSymbol(t *testing.T) {