summaryrefslogtreecommitdiff
path: root/reencode.go
blob: f23c85a97a68b76365a73890b77c2d2b6333427a (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package lowmemjson

import (
	"bytes"
	"fmt"
	"io"
	"unicode/utf8"

	"git.lukeshu.com/go/lowmemjson/internal/fastio"
	"git.lukeshu.com/go/lowmemjson/internal/jsonparse"
	"git.lukeshu.com/go/lowmemjson/internal/jsonstring"
)

// A ReEncoderConfig controls how a ReEncoder should behave.
type ReEncoderConfig struct {
	// A JSON document is specified to be a single JSON element;
	// but it is often desirable to handle streams of multiple
	// JSON elements.
	AllowMultipleValues bool

	// Whether to minify the JSON.
	//
	// Trims all whitespace, except that it emits a newline
	// between two *number* top-level values (or puts a newline
	// after all top-level values if ForceTrailingNewlines).
	//
	// Trims superflous 0s from numbers.
	Compact bool

	// CompactIfUnder causes the *ReEncoder to behave as if
	// Compact=true for individual elements if doing so would
	// cause that element to be under this number of bytes.
	//
	// Has no affect if Compact is true or Indent is empty.
	//
	// his has O(2^min(CompactIfUnder, depth)) time overhead, so
	// set with caution.
	CompactIfUnder int

	// String to use to indent; ignored if Compact is true.
	//
	// Newlines are emitted *between* top-level values; a newline is
	// not emitted after the *last* top-level value (unless
	// ForceTrailingNewlines is on).
	Indent string

	// String to put before indents.
	Prefix string

	// Whether to emit a newline after each top-level value.  See
	// the comments on Compact and Indent for discussion of how
	// this is different than the usual behavior.
	ForceTrailingNewlines bool

	// Returns whether a given character in a string should be
	// backslash-escaped.  The bool argument is whether it was
	// \u-escaped in the input.  This does not affect characters
	// that must or must-not be escaped to be valid JSON.
	//
	// If not set, then EscapeDefault is used.
	BackslashEscape BackslashEscaper
}

// NewReEncoder returns a new ReEncoder instance.
//
// A ReEncoder tends to make many small writes; if Out.Write
// calls are syscalls, then you may want to wrap Out in a
// bufio.Writer.
func NewReEncoder(out io.Writer, cfg ReEncoderConfig) *ReEncoder {
	return &ReEncoder{
		cfg:   cfg,
		out:   fastio.NewAllWriter(out),
		specu: new(speculation),
	}
}

// A ReEncoder takes a stream of JSON elements (by way of implementing
// io.Writer, io.StringWriter, io.ByteWriter, and WriteRune), and
// re-encodes the JSON, writing it to the .Out member.
//
// This is useful for prettifying, minifying, sanitizing, and/or
// validating JSON.
//
// The memory use of a ReEncoder is O(CompactIfUnder+depth).
type ReEncoder struct {
	cfg ReEncoderConfig
	out fastio.AllWriter

	// state: .Write's and .WriteString's utf8-decoding buffer
	buf    [utf8.UTFMax]byte
	bufLen int

	// state: .WriteRune
	err      error
	par      jsonparse.Parser
	written  int
	inputPos int64

	// state: .handleRune
	lastNonSpace       jsonparse.RuneType
	lastNonSpaceNonEOF jsonparse.RuneType
	wasNumber          bool
	curIndent          int
	uhex               [4]byte // "\uABCD"-encoded characters in strings
	fracZeros          int64
	expZero            bool
	specu              *speculation

	// state: .pushWriteBarrier and .popWriteBarrier
	barriers []barrier
}

type barrier struct {
	inputPos  int64
	stackSize int
}

type speculation struct {
	speculating      bool
	endWhenStackSize int
	fmt              ReEncoder
	compact          bytes.Buffer
	buf              []inputTuple
}

func (specu *speculation) Reset() {
	specu.speculating = false
	specu.endWhenStackSize = 0
	specu.fmt = ReEncoder{}
	specu.compact.Reset()
	specu.buf = specu.buf[:0]
}

type inputTuple struct {
	c         rune
	t         jsonparse.RuneType
	stackSize int
}

// public API //////////////////////////////////////////////////////////////////

var (
	_ fastio.AllWriter = (*ReEncoder)(nil)
	_ io.Closer        = (*ReEncoder)(nil)
)

// Write implements io.Writer; it does what you'd expect.
//
// It is worth noting that Write returns the number of bytes consumed
// from p, not number of bytes written to the output stream.  This
// distinction that most io.Writer implementations don't need to make,
// but *ReEncoder does because it transforms the data written to it,
// and the number of bytes written may be wildly different than the
// number of bytes handled.
func (enc *ReEncoder) Write(p []byte) (int, error) {
	if len(p) == 0 {
		return 0, nil
	}
	var n int
	if enc.bufLen > 0 {
		copy(enc.buf[enc.bufLen:], p)
		c, size := utf8.DecodeRune(enc.buf[:])
		n += size - enc.bufLen
		enc.bufLen = 0
		if _, err := enc.WriteRune(c); err != nil {
			return 0, err
		}
	}
	for utf8.FullRune(p[n:]) {
		c, size := utf8.DecodeRune(p[n:])
		if _, err := enc.WriteRune(c); err != nil {
			return n, err
		}
		n += size
	}
	enc.bufLen = copy(enc.buf[:], p[n:])
	return len(p), nil
}

// WriteString implements io.StringWriter; it does what you'd expect,
// but see the notes on the Write method.
func (enc *ReEncoder) WriteString(p string) (int, error) {
	if len(p) == 0 {
		return 0, nil
	}
	var n int
	if enc.bufLen > 0 {
		copy(enc.buf[enc.bufLen:], p)
		c, size := utf8.DecodeRune(enc.buf[:])
		n += size - enc.bufLen
		enc.bufLen = 0
		if _, err := enc.WriteRune(c); err != nil {
			return 0, err
		}
	}
	for utf8.FullRuneInString(p[n:]) {
		c, size := utf8.DecodeRuneInString(p[n:])
		if _, err := enc.WriteRune(c); err != nil {
			return n, err
		}
		n += size
	}
	enc.bufLen = copy(enc.buf[:], p[n:])
	return len(p), nil
}

// WriteByte implements io.ByteWriter; it does what you'd expect.
func (enc *ReEncoder) WriteByte(b byte) error {
	return fastio.WriteByte(enc, b)
}

// Close implements io.Closer; it does what you'd expect, mostly.
//
// The *ReEncoder may continue to be written to with new JSON values
// if enc.AllowMultipleValues is set.
func (enc *ReEncoder) Close() error {
	if enc.bufLen > 0 {
		return &ReEncodeSyntaxError{
			Offset: enc.inputPos,
			Err:    fmt.Errorf("%w: unflushed unicode garbage: %q", io.ErrUnexpectedEOF, enc.buf[:enc.bufLen]),
		}
	}
	if _, err := enc.par.HandleEOF(); err != nil {
		enc.err = &ReEncodeSyntaxError{
			Err:    err,
			Offset: enc.inputPos,
		}
		return enc.err
	}
	if len(enc.barriers) == 0 {
		if err := enc.handleRune(0, jsonparse.RuneTypeError, enc.stackSize()); err != nil {
			enc.err = &ReEncodeWriteError{
				Err:    err,
				Offset: enc.inputPos,
			}
			return enc.err
		}
		if enc.cfg.AllowMultipleValues {
			enc.par.Reset()
		}
	}
	return nil
}

// WriteRune writes a single Unicode code point, returning the number
// of bytes written to the output stream and any error.
//
// Even when there is no error, the number of bytes written may be
// zero (for example, when the rune is whitespace and the ReEncoder is
// minifying the JSON), or it may be substantially longer than one
// code point's worth (for example, when `\uXXXX` escaping a character
// in a string, or when outputing extra whitespace when the ReEncoder
// is prettifying the JSON).
func (enc *ReEncoder) WriteRune(c rune) (n int, err error) {
	if enc.err != nil {
		return 0, enc.err
	}
	if enc.bufLen > 0 {
		enc.err = fmt.Errorf("lowmemjson.ReEncoder: cannot .WriteRune() when there is a partial rune that has been .Write()en: %q", enc.buf[:enc.bufLen])
		return 0, enc.err
	}

	enc.written = 0

rehandle:
	t, err := enc.par.HandleRune(c)
	if err != nil {
		enc.err = &ReEncodeSyntaxError{
			Err:    err,
			Offset: enc.inputPos,
		}
		return enc.written, enc.err
	}
	if err := enc.handleRune(c, t, enc.stackSize()); err != nil {
		enc.err = &ReEncodeWriteError{
			Err:    err,
			Offset: enc.inputPos,
		}
		return enc.written, enc.err
	}
	if t == jsonparse.RuneTypeEOF {
		if enc.cfg.AllowMultipleValues && len(enc.barriers) == 0 {
			enc.par.Reset()
			goto rehandle
		} else {
			enc.err = &ReEncodeSyntaxError{
				Err:    fmt.Errorf("invalid character %q after top-level value", c),
				Offset: enc.inputPos,
			}
			return enc.written, enc.err
		}
	}

	enc.inputPos += int64(utf8.RuneLen(c))
	return enc.written, enc.err
}

// semi-public API /////////////////////////////////////////////////////////////

func (enc *ReEncoder) pushWriteBarrier() {
	enc.barriers = append(enc.barriers, barrier{
		inputPos:  enc.inputPos,
		stackSize: enc.stackSize(),
	})
	enc.par.PushWriteBarrier()
	enc.inputPos = 0
}

func (enc *ReEncoder) popWriteBarrier() {
	enc.par.PopBarrier()
	enc.inputPos += enc.barriers[len(enc.barriers)-1].inputPos
	enc.barriers = enc.barriers[:len(enc.barriers)-1]
	enc.lastNonSpace = enc.lastNonSpaceNonEOF
}

// internal ////////////////////////////////////////////////////////////////////

func (enc *ReEncoder) stackSize() int {
	sz := enc.par.StackSize()
	for _, barrier := range enc.barriers {
		sz += barrier.stackSize
	}
	return sz
}

func (enc *ReEncoder) handleRune(c rune, t jsonparse.RuneType, stackSize int) error {
	if enc.cfg.CompactIfUnder == 0 || enc.cfg.Compact || enc.cfg.Indent == "" {
		return enc.handleRuneNoSpeculation(c, t)
	}

	// main
	if !enc.specu.speculating { // not speculating
		switch t {
		case jsonparse.RuneTypeObjectBeg, jsonparse.RuneTypeArrayBeg: // start speculating
			if err, _ := enc.handleRunePre(c, t); err != nil {
				return err
			}
			enc.specu.speculating = true
			enc.specu.endWhenStackSize = stackSize - 1
			enc.specu.fmt = ReEncoder{
				cfg: enc.cfg,
				out: &enc.specu.compact,
			}
			enc.specu.fmt.cfg.Compact = true
			enc.specu.buf = append(enc.specu.buf, inputTuple{
				c:         c,
				t:         t,
				stackSize: stackSize,
			})
			if err := enc.specu.fmt.handleRuneMain(c, t); err != nil {
				return err
			}
		default:
			if err := enc.handleRuneNoSpeculation(c, t); err != nil {
				return err
			}
		}
	} else { // speculating
		enc.specu.buf = append(enc.specu.buf, inputTuple{
			c:         c,
			t:         t,
			stackSize: stackSize,
		})
		if err := enc.specu.fmt.handleRune(c, t, stackSize); err != nil {
			return err
		}
		switch {
		case enc.specu.compact.Len() >= enc.cfg.CompactIfUnder: // stop speculating; use indent
			buf := append([]inputTuple(nil), enc.specu.buf...)
			enc.specu.Reset()
			if err := enc.handleRuneMain(buf[0].c, buf[0].t); err != nil {
				return err
			}
			for _, tuple := range buf[1:] {
				if err := enc.handleRune(tuple.c, tuple.t, tuple.stackSize); err != nil {
					return err
				}
			}
		case stackSize == enc.specu.endWhenStackSize: // stop speculating; use compact
			if _, err := enc.specu.compact.WriteTo(enc.out); err != nil {
				return err
			}
			enc.specu.Reset()
			enc.lastNonSpace = t
			enc.curIndent--
		}
	}

	return nil
}

func (enc *ReEncoder) handleRuneNoSpeculation(c rune, t jsonparse.RuneType) error {
	err, shouldHandle := enc.handleRunePre(c, t)
	if err != nil {
		return err
	}
	if !shouldHandle {
		return nil
	}
	return enc.handleRuneMain(c, t)
}

// handleRunePre handles buffered things that need to happen before
// the new rune itself is handled.
func (enc *ReEncoder) handleRunePre(c rune, t jsonparse.RuneType) (error, bool) {
	// emit newlines between top-level values
	if enc.lastNonSpace == jsonparse.RuneTypeEOF {
		switch {
		case enc.wasNumber && t.IsNumber():
			if err := enc.emitByte('\n'); err != nil {
				return err, false
			}
		case enc.cfg.Indent != "" && !enc.cfg.Compact:
			if err := enc.emitByte('\n'); err != nil {
				return err, false
			}
		}
	}

	// shorten numbers
	switch t { // trim trailing '0's from the fraction-part, but don't remove all digits
	case jsonparse.RuneTypeNumberFracDot:
		enc.fracZeros = 0
	case jsonparse.RuneTypeNumberFracDig:
		if c == '0' && enc.lastNonSpace == jsonparse.RuneTypeNumberFracDig {
			enc.fracZeros++
			return nil, false
		}
		fallthrough
	default:
		for enc.fracZeros > 0 {
			if err := enc.emitByte('0'); err != nil {
				return err, false
			}
			enc.fracZeros--
		}
	}
	switch t { // trim leading '0's from the exponent-part, but don't remove all digits
	case jsonparse.RuneTypeNumberExpE, jsonparse.RuneTypeNumberExpSign:
		enc.expZero = true
	case jsonparse.RuneTypeNumberExpDig:
		if c == '0' && enc.expZero {
			return nil, false
		}
		enc.expZero = false
	default:
		if enc.expZero {
			if err := enc.emitByte('0'); err != nil {
				return err, false
			}
			enc.expZero = false
		}
	}

	// whitespace
	switch {
	case enc.cfg.Compact:
		if t == jsonparse.RuneTypeSpace {
			return nil, false
		}
	case enc.cfg.Indent != "":
		switch t {
		case jsonparse.RuneTypeSpace:
			// let us manage whitespace, don't pass it through
			return nil, false
		case jsonparse.RuneTypeObjectEnd, jsonparse.RuneTypeArrayEnd:
			enc.curIndent--
			switch enc.lastNonSpace {
			case jsonparse.RuneTypeObjectBeg, jsonparse.RuneTypeArrayBeg:
				// collapse
			default:
				if err := enc.emitNlIndent(); err != nil {
					return err, false
				}
			}
		default:
			switch enc.lastNonSpace {
			case jsonparse.RuneTypeObjectBeg, jsonparse.RuneTypeObjectComma, jsonparse.RuneTypeArrayBeg, jsonparse.RuneTypeArrayComma:
				if err := enc.emitNlIndent(); err != nil {
					return err, false
				}
			case jsonparse.RuneTypeObjectColon:
				if err := enc.emitByte(' '); err != nil {
					return err, false
				}
			}
			switch t {
			case jsonparse.RuneTypeObjectBeg, jsonparse.RuneTypeArrayBeg:
				enc.curIndent++
			}
		}
	}

	return nil, true
}

// handleRuneMain handles the new rune itself, not buffered things.
func (enc *ReEncoder) handleRuneMain(c rune, t jsonparse.RuneType) error {
	escaper := enc.cfg.BackslashEscape
	if escaper == nil {
		escaper = EscapeDefault
	}
	var err error
	switch t {

	case jsonparse.RuneTypeStringChar:
		err = enc.emit(jsonstring.WriteStringChar(enc.out, c, escaper(c, BackslashEscapeNone)))
	case jsonparse.RuneTypeStringEsc, jsonparse.RuneTypeStringEscU:
		// do nothing
	case jsonparse.RuneTypeStringEsc1:
		switch c {
		case '"', '\\', '/':
			// self
		case 'b':
			c = '\b'
		case 'f':
			c = '\f'
		case 'n':
			c = '\n'
		case 'r':
			c = '\r'
		case 't':
			c = '\t'
		default:
			panic(fmt.Errorf("should not happen: rune %q is not a RuneTypeStringEsc1", c))
		}
		err = enc.emit(jsonstring.WriteStringChar(enc.out, c, escaper(c, BackslashEscapeShort)))
	case jsonparse.RuneTypeStringEscUA:
		enc.uhex[0], _ = jsonparse.HexToInt(c)
	case jsonparse.RuneTypeStringEscUB:
		enc.uhex[1], _ = jsonparse.HexToInt(c)
	case jsonparse.RuneTypeStringEscUC:
		enc.uhex[2], _ = jsonparse.HexToInt(c)
	case jsonparse.RuneTypeStringEscUD:
		enc.uhex[3], _ = jsonparse.HexToInt(c)
		c := 0 |
			rune(enc.uhex[0])<<12 |
			rune(enc.uhex[1])<<8 |
			rune(enc.uhex[2])<<4 |
			rune(enc.uhex[3])<<0
		err = enc.emit(jsonstring.WriteStringChar(enc.out, c, escaper(c, BackslashEscapeUnicode)))

	case jsonparse.RuneTypeError: // EOF explicitly stated by .Close()
		fallthrough
	case jsonparse.RuneTypeEOF: // EOF implied by the start of the next top-level value
		enc.wasNumber = enc.lastNonSpace.IsNumber()
		switch {
		case enc.cfg.ForceTrailingNewlines && len(enc.barriers) == 0:
			t = jsonparse.RuneTypeError // enc.lastNonSpace : an NL isn't needed (we already printed one)
			err = enc.emitByte('\n')
		default:
			t = jsonparse.RuneTypeEOF // enc.lastNonSpace : an NL *might* be needed
		}
	default:
		err = enc.emitByte(byte(c))
	}

	if t != jsonparse.RuneTypeSpace {
		enc.lastNonSpace = t
		if t != jsonparse.RuneTypeEOF {
			enc.lastNonSpaceNonEOF = t
		}
	}
	return err
}

func (enc *ReEncoder) emitByte(c byte) error {
	err := enc.out.WriteByte(c)
	if err == nil {
		enc.written++
	}
	return err
}

func (enc *ReEncoder) emit(n int, err error) error {
	enc.written += n
	return err
}

func (enc *ReEncoder) emitNlIndent() error {
	if err := enc.emitByte('\n'); err != nil {
		return err
	}
	if enc.cfg.Prefix != "" {
		if err := enc.emit(enc.out.WriteString(enc.cfg.Prefix)); err != nil {
			return err
		}
	}
	for i := 0; i < enc.curIndent; i++ {
		if err := enc.emit(enc.out.WriteString(enc.cfg.Indent)); err != nil {
			return err
		}
	}
	return nil
}