-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.go
More file actions
163 lines (153 loc) · 2.83 KB
/
buffer.go
File metadata and controls
163 lines (153 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package errors
import (
"unicode/utf8"
"unsafe"
)
const (
hex = "0123456789abcdef"
)
var (
// 标识 JSON string字符是否需要需要的转义
safeSet = func() (safeSet [utf8.RuneSelf]bool) {
for i := '\u0000'; i <= '\u007f'; i++ {
if i < ' ' || i == '"' || i == '\\' {
safeSet[i] = false
continue
}
safeSet[i] = true
}
return
}()
)
type writeBuffer struct {
buf []byte
}
func NewWriteBuffer(n int) (buf *writeBuffer) {
return &writeBuffer{
buf: make([]byte, 0, n),
}
}
func (buf *writeBuffer) Bytes() []byte { return buf.buf }
func (buf *writeBuffer) String() string {
return *(*string)(unsafe.Pointer(&buf.buf))
}
func (buf *writeBuffer) Grow(n int) {
if cap(buf.buf) == 0 {
buf.buf = make([]byte, 0, n)
return
}
if cap(buf.buf)-len(buf.buf) >= n {
return
}
bs := buf.buf
buf.buf = make([]byte, len(bs), n+len(bs))
copy(buf.buf, bs)
return
}
func (buf *writeBuffer) Write(p []byte) {
buf.buf = append(buf.buf, p...)
}
func (buf *writeBuffer) WriteString(s string) {
buf.buf = append(buf.buf, s...)
}
func (buf *writeBuffer) WriteByte(c byte) {
buf.buf = append(buf.buf, c)
}
// WriteEscape 抄 std json 库
func (buf *writeBuffer) WriteEscape(src string) {
start := 0
for i := 0; i < len(src); {
if c := src[i]; c < utf8.RuneSelf {
if safeSet[c] {
i++
continue
}
if start < i {
buf.WriteString(src[start:i])
}
buf.WriteByte('\\')
switch c {
case '\\', '"':
buf.WriteByte(c)
case '\n':
buf.WriteByte('n')
case '\r':
buf.WriteByte('r')
case '\t':
buf.WriteByte('t')
default:
buf.WriteString(`u00`)
buf.WriteByte(hex[c>>4])
buf.WriteByte(hex[c&0xF])
}
i++
start = i
continue
}
c, size := utf8.DecodeRuneInString(src[i:])
if c == utf8.RuneError && size == 1 {
buf.WriteString(`\ufffd`)
i += size
start = i
continue
}
if c == '\u2028' || c == '\u2029' {
if start < i {
buf.WriteString(src[start:i])
}
// to: \u202 hex[c&0xF]
buf.WriteString(`\u202`)
buf.WriteByte(hex[c&0xF])
i += size
start = i
continue
}
// 跳过无效字符
i += size
}
if start < len(src) {
buf.WriteString(src[start:])
}
return
}
func countEscape(str string) (l int, escape bool) {
start := 0
for i := 0; i < len(str); {
if buf := str[i]; buf < utf8.RuneSelf {
if safeSet[buf] {
i++
continue
}
escape = true
l += i - start
switch buf {
case '\\', '"', '\n', '\r', '\t':
l += 2
default:
l += 6
}
i++
start = i
continue
}
escape = true
c, size := utf8.DecodeRuneInString(str[i:])
if c == utf8.RuneError && size == 1 {
// to: \ufffd
i += size
start = i
l += 6
continue
}
if c == '\u2028' || c == '\u2029' {
// to: \u202 hex[c&0xF]
i += size
start = i
l += 6
continue
}
i += size
}
l += len(str) - start
return
}