diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-16 22:56:37 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-16 22:56:37 -0700 |
commit | a0113140d447e59ce02d131499861aeafb02d328 (patch) | |
tree | 3a61b0c070a5db186e2c49fe70dff6f40431124e /reencode_compactnum.go | |
parent | 6f8e7db1ac5ddd21b8e3fcc39a1e30fde9b62c3a (diff) | |
parent | d19e2c6884c2d409fcc828c870f1839ee84f38cb (diff) |
Merge branch 'lukeshu/reencode-refactor'
Diffstat (limited to 'reencode_compactnum.go')
-rw-r--r-- | reencode_compactnum.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/reencode_compactnum.go b/reencode_compactnum.go new file mode 100644 index 0000000..5da2c54 --- /dev/null +++ b/reencode_compactnum.go @@ -0,0 +1,67 @@ +// 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 + } + fallthrough + default: + for enc.fracZeros > 0 { + if err := enc.out.HandleRune('0', jsonparse.RuneTypeNumberFracDig, escape, stackSize); err != nil { + return err + } + enc.fracZeros-- + } + 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) +} |