Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicated backslash in string escaping #458

Merged
merged 5 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ test: ## runs integration tests that require local applications such as MongoDB
go test -tags integration -race ./...

bench: ## runs benchmark tests
go test -tags bench -benchmem -bench=. ./test/bench -memprofile=mem.prof -cpuprofile=cpu.prof | tee output.txt
mkfifo pipe
tee output.txt < pipe &
go test -tags bench -benchmem -bench=. ./test/bench -memprofile=mem.prof -cpuprofile=cpu.prof > pipe
Copy link
Contributor Author

@cozitive cozitive Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exit status of benchmark test has been ignored, because the exit status of a pipeline is the exit status of the last command (tee) in the pipeline.
To reveal the original exit status, piping command is separated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed description.


docker: ## builds docker images with the current version and latest tag
docker buildx build --push --platform linux/amd64,linux/arm64,linux/386 -t yorkieteam/yorkie:$(YORKIE_VERSION) -t yorkieteam/yorkie:latest .
Expand Down
2 changes: 1 addition & 1 deletion pkg/document/crdt/rht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestMarshal(t *testing.T) {
value1 := "world\"\f\b"
key2 := "hi"
value2 := `test\r`
expected := `{"hello\\\\\\t":"world\\"\\f\\b","hi":"test\\r"}`
expected := `{"hello\\\\\\t":"world\"\f\b","hi":"test\\r"}`

rht := NewRHT()
rht.Set(key1, value1, nil)
Expand Down
7 changes: 0 additions & 7 deletions pkg/document/crdt/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,24 @@ func EscapeString(s string) string {
buf.WriteByte('\\')
buf.WriteByte('\\')
case '"':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('"')
case '\n':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('n')
case '\f':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('f')
case '\b':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('b')
case '\r':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('r')
case '\t':
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('t')
default:
buf.WriteByte('\\')
buf.WriteByte('\\')
buf.WriteByte('u')
buf.WriteByte('0')
Expand Down
26 changes: 6 additions & 20 deletions pkg/document/crdt/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,16 @@ func TestEscapeString(t *testing.T) {
assert.Equal(t, expected, actual)
})

t.Run("escape string with doublequote", func(t *testing.T) {
str := `hello world"`
expected := `hello world\\"`
t.Run("escape string with backslash, doublequote and control characters", func(t *testing.T) {
str := "hello world\"\\\n\f\b\r\t"
expected := `hello world\"\\\n\f\b\r\t`
actual := EscapeString(str)
assert.Equal(t, expected, actual)
})

t.Run("escape string with raw control character", func(t *testing.T) {
str := "hello world\n"
expected := `hello world\\n`
actual := EscapeString(str)
assert.Equal(t, expected, actual)
})

t.Run("escape string with escaped control character", func(t *testing.T) {
str := `hello world\n`
expected := `hello world\\n`
actual := EscapeString(str)
assert.Equal(t, expected, actual)
})

t.Run("escape string with raw unicode character", func(t *testing.T) {
str := "hello world\u1234"
expected := "hello world\u1234"
t.Run("escape string with unicode characters", func(t *testing.T) {
str := "hello world\u1234\u6645"
expected := "hello world\u1234\u6645"
actual := EscapeString(str)
assert.Equal(t, expected, actual)
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func TestDocument(t *testing.T) {
assert.Equal(
t,
`[0:0:00:0 {} ""][1:2:00:0 {"b":"1"} "Hel"][1:2:00:3 {"b":"1","i":"1"} "lo"]`+
`[5:1:00:0 {"list":"true"} "\\n"][4:1:00:0 {} " Yorkie"]{1:2:00:5 {} " world"}`,
`[5:1:00:0 {"list":"true"} "\n"][4:1:00:0 {} " Yorkie"]{1:2:00:5 {} " world"}`,
text.StructureAsString(),
)
return nil
Expand All @@ -345,7 +345,7 @@ func TestDocument(t *testing.T) {
assert.Equal(
t,
`{"k1":[{"attrs":{"b":"1"},"val":"Hel"},{"attrs":{"b":"1","i":"1"},"val":"lo"},`+
`{"attrs":{"list":"true"},"val":"\\n"},{"val":" Yorkie"}]}`,
`{"attrs":{"list":"true"},"val":"\n"},{"val":" Yorkie"}]}`,
doc.Marshal(),
)
})
Expand Down
4 changes: 2 additions & 2 deletions test/bench/document_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ func BenchmarkDocument(b *testing.B) {
text.Edit(5, 5, "\n", map[string]string{"list": "true"})
assert.Equal(
b,
`[0:0:00:0 {} ""][1:2:00:0 {"b":"1"} "Hel"][1:2:00:3 {"b":"1","i":"1"} "lo"][5:1:00:0 {"list":"true"} "\\n"][4:1:00:0 {} " Yorkie"]{1:2:00:5 {} " world"}`,
`[0:0:00:0 {} ""][1:2:00:0 {"b":"1"} "Hel"][1:2:00:3 {"b":"1","i":"1"} "lo"][5:1:00:0 {"list":"true"} "\n"][4:1:00:0 {} " Yorkie"]{1:2:00:5 {} " world"}`,
text.StructureAsString(),
)
return nil
})
assert.NoError(b, err)
assert.Equal(
b,
`{"k1":[{"attrs":{"b":"1"},"val":"Hel"},{"attrs":{"b":"1","i":"1"},"val":"lo"},{"attrs":{"list":"true"},"val":"\\n"},{"val":" Yorkie"}]}`,
`{"k1":[{"attrs":{"b":"1"},"val":"Hel"},{"attrs":{"b":"1","i":"1"},"val":"lo"},{"attrs":{"list":"true"},"val":"\n"},{"val":" Yorkie"}]}`,
doc.Marshal(),
)
}
Expand Down