aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/goccy/go-json/internal/encoder/compact.go
blob: 0eb9545d8974b78f00e8db07be9f2cbb45a67ffb (plain) (blame)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package encoder

import (
	"bytes"
	"fmt"
	"strconv"
	"unsafe"

	"github.com/goccy/go-json/internal/errors"
)

var (
	isWhiteSpace = [256]bool{
		' ':  true,
		'\n': true,
		'\t': true,
		'\r': true,
	}
	isHTMLEscapeChar = [256]bool{
		'<': true,
		'>': true,
		'&': true,
	}
	nul = byte('\000')
)

func Compact(buf *bytes.Buffer, src []byte, escape bool) error {
	if len(src) == 0 {
		return errors.ErrUnexpectedEndOfJSON("", 0)
	}
	buf.Grow(len(src))
	dst := buf.Bytes()

	ctx := TakeRuntimeContext()
	ctxBuf := ctx.Buf[:0]
	ctxBuf = append(append(ctxBuf, src...), nul)
	ctx.Buf = ctxBuf

	if err := compactAndWrite(buf, dst, ctxBuf, escape); err != nil {
		ReleaseRuntimeContext(ctx)
		return err
	}
	ReleaseRuntimeContext(ctx)
	return nil
}

func compactAndWrite(buf *bytes.Buffer, dst []byte, src []byte, escape bool) error {
	dst, err := compact(dst, src, escape)
	if err != nil {
		return err
	}
	if _, err := buf.Write(dst); err != nil {
		return err
	}
	return nil
}

func compact(dst, src []byte, escape bool) ([]byte, error) {
	buf, cursor, err := compactValue(dst, src, 0, escape)
	if err != nil {
		return nil, err
	}
	if err := validateEndBuf(src, cursor); err != nil {
		return nil, err
	}
	return buf, nil
}

func validateEndBuf(src []byte, cursor int64) error {
	for {
		switch src[cursor] {
		case ' ', '\t', '\n', '\r':
			cursor++
			continue
		case nul:
			return nil
		}
		return errors.ErrSyntax(
			fmt.Sprintf("invalid character '%c' after top-level value", src[cursor]),
			cursor+1,
		)
	}
}

func skipWhiteSpace(buf []byte, cursor int64) int64 {
LOOP:
	if isWhiteSpace[buf[cursor]] {
		cursor++
		goto LOOP
	}
	return cursor
}

func compactValue(dst, src []byte, cursor int64, escape bool) ([]byte, int64, error) {
	for {
		switch src[cursor] {
		case ' ', '\t', '\n', '\r':
			cursor++
			continue
		case '{':
			return compactObject(dst, src, cursor, escape)
		case '}':
			return nil, 0, errors.ErrSyntax("unexpected character '}'", cursor)
		case '[':
			return compactArray(dst, src, cursor, escape)
		case ']':
			return nil, 0, errors.ErrSyntax("unexpected character ']'", cursor)
		case '"':
			return compactString(dst, src, cursor, escape)
		case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			return compactNumber(dst, src, cursor)
		case 't':
			return compactTrue(dst, src, cursor)
		case 'f':
			return compactFalse(dst, src, cursor)
		case 'n':
			return compactNull(dst, src, cursor)
		default:
			return nil, 0, errors.ErrSyntax(fmt.Sprintf("unexpected character '%c'", src[cursor]), cursor)
		}
	}
}

func compactObject(dst, src []byte, cursor int64, escape bool) ([]byte, int64, error) {
	if src[cursor] == '{' {
		dst = append(dst, '{')
	} else {
		return nil, 0, errors.ErrExpected("expected { character for object value", cursor)
	}
	cursor = skipWhiteSpace(src, cursor+1)
	if src[cursor] == '}' {
		dst = append(dst, '}')
		return dst, cursor + 1, nil
	}
	var err error
	for {
		cursor = skipWhiteSpace(src, cursor)
		dst, cursor, err = compactString(dst, src, cursor, escape)
		if err != nil {
			return nil, 0, err
		}
		cursor = skipWhiteSpace(src, cursor)
		if src[cursor] != ':' {
			return nil, 0, errors.ErrExpected("colon after object key", cursor)
		}
		dst = append(dst, ':')
		dst, cursor, err = compactValue(dst, src, cursor+1, escape)
		if err != nil {
			return nil, 0, err
		}
		cursor = skipWhiteSpace(src, cursor)
		switch src[cursor] {
		case '}':
			dst = append(dst, '}')
			cursor++
			return dst, cursor, nil
		case ',':
			dst = append(dst, ',')
		default:
			return nil, 0, errors.ErrExpected("comma after object value", cursor)
		}
		cursor++
	}
}

func compactArray(dst, src []byte, cursor int64, escape bool) ([]byte, int64, error) {
	if src[cursor] == '[' {
		dst = append(dst, '[')
	} else {
		return nil, 0, errors.ErrExpected("expected [ character for array value", cursor)
	}
	cursor = skipWhiteSpace(src, cursor+1)
	if src[cursor] == ']' {
		dst = append(dst, ']')
		return dst, cursor + 1, nil
	}
	var err error
	for {
		dst, cursor, err = compactValue(dst, src, cursor, escape)
		if err != nil {
			return nil, 0, err
		}
		cursor = skipWhiteSpace(src, cursor)
		switch src[cursor] {
		case ']':
			dst = append(dst, ']')
			cursor++
			return dst, cursor, nil
		case ',':
			dst = append(dst, ',')
		default:
			return nil, 0, errors.ErrExpected("comma after array value", cursor)
		}
		cursor++
	}
}

func compactString(dst, src []byte, cursor int64, escape bool) ([]byte, int64, error) {
	if src[cursor] != '"' {
		return nil, 0, errors.ErrInvalidCharacter(src[cursor], "string", cursor)
	}
	start := cursor
	for {
		cursor++
		c := src[cursor]
		if escape {
			if isHTMLEscapeChar[c] {
				dst = append(dst, src[start:cursor]...)
				dst = append(dst, `\u00`...)
				dst = append(dst, hex[c>>4], hex[c&0xF])
				start = cursor + 1
			} else if c == 0xE2 && cursor+2 < int64(len(src)) && src[cursor+1] == 0x80 && src[cursor+2]&^1 == 0xA8 {
				dst = append(dst, src[start:cursor]...)
				dst = append(dst, `\u202`...)
				dst = append(dst, hex[src[cursor+2]&0xF])
				cursor += 2
				start = cursor + 3
			}
		}
		switch c {
		case '\\':
			cursor++
			if src[cursor] == nul {
				return nil, 0, errors.ErrUnexpectedEndOfJSON("string", int64(len(src)))
			}
		case '"':
			cursor++
			return append(dst, src[start:cursor]...), cursor, nil
		case nul:
			return nil, 0, errors.ErrUnexpectedEndOfJSON("string", int64(len(src)))
		}
	}
}

func compactNumber(dst, src []byte, cursor int64) ([]byte, int64, error) {
	start := cursor
	for {
		cursor++
		if floatTable[src[cursor]] {
			continue
		}
		break
	}
	num := src[start:cursor]
	if _, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&num)), 64); err != nil {
		return nil, 0, err
	}
	dst = append(dst, num...)
	return dst, cursor, nil
}

func compactTrue(dst, src []byte, cursor int64) ([]byte, int64, error) {
	if cursor+3 >= int64(len(src)) {
		return nil, 0, errors.ErrUnexpectedEndOfJSON("true", cursor)
	}
	if !bytes.Equal(src[cursor:cursor+4], []byte(`true`)) {
		return nil, 0, errors.ErrInvalidCharacter(src[cursor], "true", cursor)
	}
	dst = append(dst, "true"...)
	cursor += 4
	return dst, cursor, nil
}

func compactFalse(dst, src []byte, cursor int64) ([]byte, int64, error) {
	if cursor+4 >= int64(len(src)) {
		return nil, 0, errors.ErrUnexpectedEndOfJSON("false", cursor)
	}
	if !bytes.Equal(src[cursor:cursor+5], []byte(`false`)) {
		return nil, 0, errors.ErrInvalidCharacter(src[cursor], "false", cursor)
	}
	dst = append(dst, "false"...)
	cursor += 5
	return dst, cursor, nil
}

func compactNull(dst, src []byte, cursor int64) ([]byte, int64, error) {
	if cursor+3 >= int64(len(src)) {
		return nil, 0, errors.ErrUnexpectedEndOfJSON("null", cursor)
	}
	if !bytes.Equal(src[cursor:cursor+4], []byte(`null`)) {
		return nil, 0, errors.ErrInvalidCharacter(src[cursor], "null", cursor)
	}
	dst = append(dst, "null"...)
	cursor += 4
	return dst, cursor, nil
}