-
Notifications
You must be signed in to change notification settings - Fork 196
/
writer.go
160 lines (135 loc) · 3.5 KB
/
writer.go
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
package fastjson
import (
"strconv"
"time"
"unicode/utf8"
)
// Writer is a JSON writer.
type Writer struct {
buf []byte
}
// Bytes returns the internal buffer. The result
// is invalidated when Reset is called.
func (w *Writer) Bytes() []byte {
return w.buf
}
// Size returns the current size of the buffer.
func (w *Writer) Size() int {
return len(w.buf)
}
// Rewind rewinds the buffer such that it has size bytes,
// dropping everything proceeding.
func (w *Writer) Rewind(size int) {
w.buf = w.buf[:size]
}
// Reset resets the internal []byte buffer to empty.
func (w *Writer) Reset() {
w.buf = w.buf[:0]
}
// RawByte appends c to the buffer.
func (w *Writer) RawByte(c byte) {
w.buf = append(w.buf, c)
}
// RawBytes appends data, unmodified, to the buffer.
func (w *Writer) RawBytes(data []byte) {
w.buf = append(w.buf, data...)
}
// RawString appends s to the buffer.
func (w *Writer) RawString(s string) {
w.buf = append(w.buf, s...)
}
// Uint64 appends n to the buffer.
func (w *Writer) Uint64(n uint64) {
w.buf = strconv.AppendUint(w.buf, uint64(n), 10)
}
// Int64 appends n to the buffer.
func (w *Writer) Int64(n int64) {
w.buf = strconv.AppendInt(w.buf, int64(n), 10)
}
// Float32 appends n to the buffer.
func (w *Writer) Float32(n float32) {
w.buf = strconv.AppendFloat(w.buf, float64(n), 'g', -1, 32)
}
// Float64 appends n to the buffer.
func (w *Writer) Float64(n float64) {
w.buf = strconv.AppendFloat(w.buf, float64(n), 'g', -1, 64)
}
// Bool appends v to the buffer.
func (w *Writer) Bool(v bool) {
w.buf = strconv.AppendBool(w.buf, v)
}
// Time appends t to the buffer, formatted according to layout.
func (w *Writer) Time(t time.Time, layout string) {
w.buf = t.AppendFormat(w.buf, layout)
}
const chars = "0123456789abcdef"
func isNotEscapedSingleChar(c byte, escapeHTML bool) bool {
// Note: might make sense to use a table if there are more chars to escape. With 4 chars
// it benchmarks the same.
if escapeHTML {
return c != '<' && c != '>' && c != '&' && c != '\\' && c != '"' && c >= 0x20 && c < utf8.RuneSelf
}
return c != '\\' && c != '"' && c >= 0x20 && c < utf8.RuneSelf
}
// String appends s, quoted and escaped, to the buffer.
func (w *Writer) String(s string) {
w.RawByte('"')
w.StringContents(s)
w.RawByte('"')
}
// StringContents is the same as String, but without the surrounding quotes.
func (w *Writer) StringContents(s string) {
// Portions of the string that contain no escapes are appended as
// byte slices.
p := 0 // last non-escape symbol
for i := 0; i < len(s); {
c := s[i]
if isNotEscapedSingleChar(c, true) {
// single-width character, no escaping is required
i++
continue
} else if c < utf8.RuneSelf {
// single-with character, need to escape
w.RawString(s[p:i])
switch c {
case '\t':
w.RawString(`\t`)
case '\r':
w.RawString(`\r`)
case '\n':
w.RawString(`\n`)
case '\\':
w.RawString(`\\`)
case '"':
w.RawString(`\"`)
default:
w.RawString(`\u00`)
w.RawByte(chars[c>>4])
w.RawByte(chars[c&0xf])
}
i++
p = i
continue
}
// broken utf
runeValue, runeWidth := utf8.DecodeRuneInString(s[i:])
if runeValue == utf8.RuneError && runeWidth == 1 {
w.RawString(s[p:i])
w.RawString(`\ufffd`)
i++
p = i
continue
}
// jsonp stuff - tab separator and line separator
if runeValue == '\u2028' || runeValue == '\u2029' {
w.RawString(s[p:i])
w.RawString(`\u202`)
w.RawByte(chars[runeValue&0xf])
i += runeWidth
p = i
continue
}
i += runeWidth
}
w.RawString(s[p:])
}