Skip to content

Commit a54af38

Browse files
json: encode \b and \f like the Go 1.22 stdlib does (#145)
* go.mod: upgrade to 1.23 (oldest supported by Go project) * json: encode `\b` and `\f` like the Go 1.22 stdlib does https://go-review.googlesource.com/c/go/+/521675
1 parent a8de6b8 commit a54af38

File tree

6 files changed

+28
-30
lines changed

6 files changed

+28
-30
lines changed

.github/workflows/benchmark.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup Go
1919
uses: actions/setup-go@v2
2020
with:
21-
go-version: "1.21"
21+
go-version: "1.24"
2222

2323
- name: Checkout
2424
uses: actions/checkout@v2
@@ -43,7 +43,7 @@ jobs:
4343
- name: Steup Go
4444
uses: actions/setup-go@v2
4545
with:
46-
go-version: "1.21"
46+
go-version: "1.24"
4747

4848
- name: Setup Benchstat
4949
run: go install golang.org/x/perf/cmd/benchstat@latest

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ jobs:
99
strategy:
1010
matrix:
1111
go:
12-
- "1.20"
13-
- "1.21"
12+
- "1.23"
13+
- "1.24"
1414

1515
runs-on: ubuntu-latest
1616

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/segmentio/encoding
22

3-
go 1.18
3+
go 1.23
44

55
require github.com/segmentio/asm v1.1.3
66

json/encode.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -158,30 +158,9 @@ func (e encoder) encodeString(b []byte, p unsafe.Pointer) ([]byte, error) {
158158
}
159159

160160
switch c {
161-
case '\\', '"':
161+
case '\\', '"', '\b', '\f', '\n', '\r', '\t':
162162
b = append(b, s[i:j]...)
163-
b = append(b, '\\', c)
164-
i = j + 1
165-
j = j + 1
166-
continue
167-
168-
case '\n':
169-
b = append(b, s[i:j]...)
170-
b = append(b, '\\', 'n')
171-
i = j + 1
172-
j = j + 1
173-
continue
174-
175-
case '\r':
176-
b = append(b, s[i:j]...)
177-
b = append(b, '\\', 'r')
178-
i = j + 1
179-
j = j + 1
180-
continue
181-
182-
case '\t':
183-
b = append(b, s[i:j]...)
184-
b = append(b, '\\', 't')
163+
b = append(b, '\\', escapeByteRepr(c))
185164
i = j + 1
186165
j = j + 1
187166
continue

json/golang_encode_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,11 @@ var encodeStringTests = []struct {
685685
{"\x05", `"\u0005"`},
686686
{"\x06", `"\u0006"`},
687687
{"\x07", `"\u0007"`},
688-
{"\x08", `"\u0008"`},
688+
{"\x08", `"\b"`},
689689
{"\x09", `"\t"`},
690690
{"\x0a", `"\n"`},
691691
{"\x0b", `"\u000b"`},
692-
{"\x0c", `"\u000c"`},
692+
{"\x0c", `"\f"`},
693693
{"\x0d", `"\r"`},
694694
{"\x0e", `"\u000e"`},
695695
{"\x0f", `"\u000f"`},

json/string.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ func escapeIndex(s string, escapeHTML bool) int {
4040
return -1
4141
}
4242

43+
func escapeByteRepr(b byte) byte {
44+
switch b {
45+
case '\\', '"':
46+
return b
47+
case '\b':
48+
return 'b'
49+
case '\f':
50+
return 'f'
51+
case '\n':
52+
return 'n'
53+
case '\r':
54+
return 'r'
55+
case '\t':
56+
return 't'
57+
}
58+
59+
return 0
60+
}
61+
4362
// below return a mask that can be used to determine if any of the bytes
4463
// in `n` are below `b`. If a byte's MSB is set in the mask then that byte was
4564
// below `b`. The result is only valid if `b`, and each byte in `n`, is below

0 commit comments

Comments
 (0)