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
|
// 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 reEncodeCompactNum struct {
out reEncoderModule
// state
fracFirst bool
fracZeros int64
expZero bool
}
var _ reEncoderModule = (*reEncodeCompactNum)(nil)
func (enc *reEncodeCompactNum) PopWriteBarrier() {
enc.out.PopWriteBarrier()
}
func (enc *reEncodeCompactNum) HandleRune(c rune, t jsonparse.RuneType, escape BackslashEscapeMode, stackSize int) error {
// trim trailing '0's from the fraction-part, but don't remove all digits
switch t {
case jsonparse.RuneTypeNumberFracDot:
enc.fracFirst = true
enc.fracZeros = 0
case jsonparse.RuneTypeNumberFracDig:
if c == '0' && !enc.fracFirst {
enc.fracZeros++
return nil
} else {
for ; enc.fracZeros > 0; enc.fracZeros-- {
if err := enc.out.HandleRune('0', jsonparse.RuneTypeNumberFracDig, escape, stackSize); err != nil {
return err
}
}
}
enc.fracFirst = false
}
// trim leading '0's from the exponent-part, but don't remove all digits
switch t {
case jsonparse.RuneTypeNumberExpE, jsonparse.RuneTypeNumberExpSign:
enc.expZero = true
case jsonparse.RuneTypeNumberExpDig:
if c == '0' && enc.expZero {
return nil
}
enc.expZero = false
default:
if enc.expZero {
if err := enc.out.HandleRune('0', jsonparse.RuneTypeNumberFracDig, escape, stackSize); err != nil {
return err
}
enc.expZero = false
}
}
return enc.out.HandleRune(c, t, escape, stackSize)
}
|