summaryrefslogtreecommitdiff
path: root/reencode_nl.go
blob: b7a3cd921cde4cfb1c2f839526e1f5ea8858adc8 (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
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package lowmemjson

import (
	"git.lukeshu.com/go/lowmemjson/internal/jsonparse"
)

type reEncodeForceNL struct {
	out reEncoderModule

	// state
	skipNL bool
}

var _ reEncoderModule = (*reEncodeForceNL)(nil)

func (enc *reEncodeForceNL) PopWriteBarrier() {
	enc.out.PopWriteBarrier()
}

func (enc *reEncodeForceNL) HandleRune(c rune, t jsonparse.RuneType, escape BackslashEscapeMode, stackSize int) error {
	switch {
	case t == jsonparse.RuneTypeEOF:
		if err := enc.out.HandleRune('\n', jsonparse.RuneTypeSpace, 0, stackSize); err != nil {
			return err
		}
		enc.skipNL = true
		return enc.out.HandleRune(c, t, escape, stackSize)
	case c == '\n' && t == jsonparse.RuneTypeSpace && enc.skipNL:
		enc.skipNL = false
		return nil
	default:
		enc.skipNL = false
		return enc.out.HandleRune(c, t, escape, stackSize)
	}
}