aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-09-07 23:22:23 +0700
committerShulhan <ms@kilabit.info>2022-09-07 23:22:23 +0700
commitc6b0f2520f27bedd36cee06c0d62dffd95f90381 (patch)
treeca8bdd4c2868a7efbfc06b7fbc27855b039d450f /lib
parentdd485ce62d0554bddb6f8253ee4700ece6c4ea00 (diff)
downloadpakakeh.go-c6b0f2520f27bedd36cee06c0d62dffd95f90381.tar.xz
lib/text: replace json.Escape with strconv.Quote
We want to prevent import cycle in the future, when the test package use the text/diff for display difference between string.
Diffstat (limited to 'lib')
-rw-r--r--lib/text/chunk.go8
-rw-r--r--lib/text/chunk_test.go38
-rw-r--r--lib/text/line.go8
-rw-r--r--lib/text/line_test.go38
4 files changed, 82 insertions, 10 deletions
diff --git a/lib/text/chunk.go b/lib/text/chunk.go
index 49ab395d..ddf2edc4 100644
--- a/lib/text/chunk.go
+++ b/lib/text/chunk.go
@@ -8,8 +8,6 @@ import (
"bytes"
"fmt"
"strconv"
-
- "github.com/shuLhan/share/lib/json"
)
// Chunk represent subset of line, contain starting position and slice of
@@ -38,9 +36,9 @@ func (chunk Chunk) MarshalJSON() ([]byte, error) {
bb.WriteString(`{"StartAt":`)
bb.WriteString(strconv.Itoa(chunk.StartAt))
- bb.WriteString(`,"V":"`)
- bb.Write(json.Escape(chunk.V))
- bb.WriteString(`"}`)
+ bb.WriteString(`,"V":`)
+ bb.WriteString(strconv.Quote(string(chunk.V)))
+ bb.WriteString(`}`)
return bb.Bytes(), nil
}
diff --git a/lib/text/chunk_test.go b/lib/text/chunk_test.go
new file mode 100644
index 00000000..36f1cf8b
--- /dev/null
+++ b/lib/text/chunk_test.go
@@ -0,0 +1,38 @@
+package text
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestChunk_MarshalJSON(t *testing.T) {
+ type testCase struct {
+ chunk Chunk
+ exp string
+ }
+
+ var cases = []testCase{{
+ chunk: Chunk{
+ StartAt: 1,
+ V: []byte("<script>a\"\\ \b\f\n\r\tz"),
+ },
+ exp: `{"StartAt":1,"V":"<script>a\"\\ \b\f\n\r\tz"}`,
+ }}
+
+ var (
+ c testCase
+ got []byte
+ err error
+ )
+
+ for _, c = range cases {
+ got, err = c.chunk.MarshalJSON()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !reflect.DeepEqual(c.exp, string(got)) {
+ t.Fatalf(`want %s, got %s`, c.exp, got)
+ }
+ }
+}
diff --git a/lib/text/line.go b/lib/text/line.go
index cb551141..254fc0dc 100644
--- a/lib/text/line.go
+++ b/lib/text/line.go
@@ -8,8 +8,6 @@ import (
"bytes"
"fmt"
"strconv"
-
- "github.com/shuLhan/share/lib/json"
)
// Line represent line number and slice of bytes as string.
@@ -23,9 +21,9 @@ func (line Line) MarshalJSON() ([]byte, error) {
bb.WriteString(`{"N":`)
bb.WriteString(strconv.Itoa(line.N))
- bb.WriteString(`,"V":"`)
- bb.Write(json.Escape(line.V))
- bb.WriteString(`"}`)
+ bb.WriteString(`,"V":`)
+ bb.WriteString(strconv.Quote(string(line.V)))
+ bb.WriteString(`}`)
return bb.Bytes(), nil
}
diff --git a/lib/text/line_test.go b/lib/text/line_test.go
new file mode 100644
index 00000000..e4395592
--- /dev/null
+++ b/lib/text/line_test.go
@@ -0,0 +1,38 @@
+package text
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestLine_MarshalJSON(t *testing.T) {
+ type testCase struct {
+ line Line
+ exp string
+ }
+
+ var cases = []testCase{{
+ line: Line{
+ N: 1,
+ V: []byte("<script>a\"\\ \b\f\n\r\tz"),
+ },
+ exp: `{"N":1,"V":"<script>a\"\\ \b\f\n\r\tz"}`,
+ }}
+
+ var (
+ c testCase
+ got []byte
+ err error
+ )
+
+ for _, c = range cases {
+ got, err = c.line.MarshalJSON()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !reflect.DeepEqual(c.exp, string(got)) {
+ t.Fatalf(`want %s, got %s`, c.exp, got)
+ }
+ }
+}