Skip to content

Commit

Permalink
去除对 \ 和 " 的转义
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Jan 9, 2024
1 parent 783825f commit fd0929e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v1.5.9-alpha

> 此版本发布于 2024-01-09
* 去除对 \ 和 " 的转义

### v1.5.8-alpha

> 此版本发布于 2023-12-27
Expand Down
10 changes: 4 additions & 6 deletions handler/escape.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ import (
)

// needEscapedByte returns if value need to escape.
// The main character should be escaped is ascii less than \u0020 and \ and ".
// The main character should be escaped is ascii less than \u0020.
func needEscapedByte(value byte) bool {
return value < 32 || value == '"' || value == '\\'
return value < 32
}

// appendEscapedByte appends escaped value to dst.
// The main character should be escaped is ascii less than \u0020 and \ and ".
// The main character should be escaped is ascii less than \u0020.
func appendEscapedByte(dst []byte, value byte) []byte {
switch value {
case '"', '\\':
return append(dst, '\\', value)
case '\b':
return append(dst, '\\', 'b')
case '\f':
Expand All @@ -57,7 +55,7 @@ func appendEscapedByte(dst []byte, value byte) []byte {
}

// appendEscapedString appends escaped value to dst.
// The main character should be escaped is ascii less than \u0020 and \ and ".
// The main character should be escaped is ascii less than \u0020.
func appendEscapedString(dst []byte, value string) []byte {
start := 0
escaped := false
Expand Down
6 changes: 3 additions & 3 deletions handler/escape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import "testing"
// go test -v -cover -count=1 -test.cpu=1 -run=^TestAppendEscapedByte$
func TestAppendEscapedByte(t *testing.T) {
testcases := []byte{'a', '0', '\n', '\t', '\\', '\b', '\f', '\r', '"'}
want := "a0\\n\\t\\\\\\b\\f\\r\\\""
want := `a0\n\t\\b\f\r"`

buffer := make([]byte, 0, 16)
for _, b := range testcases {
Expand All @@ -33,8 +33,8 @@ func TestAppendEscapedByte(t *testing.T) {

// go test -v -cover -count=1 -test.cpu=1 -run=^TestAppendEscapedString$
func TestAppendEscapedString(t *testing.T) {
testcases := []string{"a0国\n\t\\\b\f\r\""}
want := "a0国\\n\\t\\\\\\b\\f\\r\\\""
testcases := []string{"a0国\n\t\\b\f\r\""}
want := `a0国\n\t\b\f\r"`

buffer := make([]byte, 0, 16)
for _, str := range testcases {
Expand Down

0 comments on commit fd0929e

Please sign in to comment.