Skip to content

Commit

Permalink
Remove duplicated backslash in string escaping (#458)
Browse files Browse the repository at this point in the history
* Remove duplicated backslash in string escaping

* Deliver benchmark test exit status
  • Loading branch information
cozitive authored Feb 9, 2023
1 parent 7635b73 commit de20c70
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 33 deletions.
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

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

0 comments on commit de20c70

Please sign in to comment.