Skip to content

Commit

Permalink
opt: encoder support Uint64ToString
Browse files Browse the repository at this point in the history
  • Loading branch information
period331 committed Dec 16, 2024
1 parent 554dba4 commit 5a6ea4f
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 44 deletions.
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ type Config struct {
// Encode Infinity or Nan float into `null`, instead of returning an error.
EncodeNullForInfOrNan bool

// Int64 or Uint64 into strings on Marshal
Integer64BitToString bool
// Uint64 into strings on Marshal
Uint64ToString bool
}

var (
Expand Down
20 changes: 10 additions & 10 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ func TestMarshalInfOrNan(t *testing.T) {
}
}

func TestInt64OrUInteger64BitToString(t *testing.T) {
func TestUint64ToString(t *testing.T) {
int64ptr := int64(432556670863027541)
uint64ptr := uint64(12372850276778298372)
cases := []struct {
Expand All @@ -1242,7 +1242,7 @@ func TestInt64OrUInteger64BitToString(t *testing.T) {
"int64": int64(34),
"uint64": uint64(56),
},
exceptTrue: `{"int":12,"int64":"34","uint64":"56"}`,
exceptTrue: `{"int":12,"int64":34,"uint64":"56"}`,
exceptFalse: `{"int":12,"int64":34,"uint64":56}`,
},
{
Expand All @@ -1252,7 +1252,7 @@ func TestInt64OrUInteger64BitToString(t *testing.T) {
int64(34): int64(34),
int64(56): uint64(56),
},
exceptTrue: `{"12":12,"34":"34","56":"56"}`,
exceptTrue: `{"12":12,"34":34,"56":"56"}`,
exceptFalse: `{"12":12,"34":34,"56":56}`,
},
{
Expand All @@ -1266,21 +1266,21 @@ func TestInt64OrUInteger64BitToString(t *testing.T) {
Int64: int64(34),
Uint64: uint64(56),
},
exceptTrue: `{"int":12,"int64":"34","uint64":"56"}`,
exceptTrue: `{"int":12,"int64":34,"uint64":"56"}`,
exceptFalse: `{"int":12,"int64":34,"uint64":56}`,
},
{
name: "normal_slice",
val: []any{
int(12), int64(34), uint64(56),
},
exceptTrue: `[12,"34","56"]`,
exceptTrue: `[12,34,"56"]`,
exceptFalse: `[12,34,56]`,
},
{
name: "single_int64",
val: int64(34),
exceptTrue: `"34"`,
exceptTrue: `34`,
exceptFalse: `34`,
},
{
Expand All @@ -1304,8 +1304,8 @@ func TestInt64OrUInteger64BitToString(t *testing.T) {
Int64: int64(123),
Uint64: uint64(456),
}}},
exceptTrue: `{"Map":{"val":{"Int64Ptr":"432556670863027541",` +
`"Uint64Ptr":"12372850276778298372","Int64":"123","Uint64":"456"}}}`,
exceptTrue: `{"Map":{"val":{"Int64Ptr":432556670863027541,` +
`"Uint64Ptr":"12372850276778298372","Int64":123,"Uint64":"456"}}}`,
exceptFalse: `{"Map":{"val":{"Int64Ptr":432556670863027541,` +
`"Uint64Ptr":12372850276778298372,"Int64":123,"Uint64":456}}}`,
},
Expand All @@ -1321,11 +1321,11 @@ func TestInt64OrUInteger64BitToString(t *testing.T) {

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
b, e := Config{Integer64BitToString: true}.Froze().Marshal(c.val)
b, e := Config{Uint64ToString: true}.Froze().Marshal(c.val)
assert.Nil(t, e)
check(t, c.exceptTrue, b)

b, e = Config{Integer64BitToString: false}.Froze().Marshal(c.val)
b, e = Config{Uint64ToString: false}.Froze().Marshal(c.val)
assert.Nil(t, e)
check(t, c.exceptFalse, b)
})
Expand Down
4 changes: 2 additions & 2 deletions encoder/encoder_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ const (
// Encode Infinity or Nan float into `null`, instead of returning an error.
EncodeNullForInfOrNan Options = encoder.EncodeNullForInfOrNan

// Int64 or Uint64 into strings on Marshal
Integer64BitToString Options = encoder.Integer64BitToString
// Uint64 into strings on Marshal
Uint64ToString Options = encoder.Uint64ToString
)


Expand Down
2 changes: 1 addition & 1 deletion internal/encoder/alg/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
BitNoValidateJSONMarshaler
BitNoEncoderNewline
BitEncodeNullForInfOrNan
BitInteger64BitToString
BitUint64ToString

BitPointerValue = 63
)
4 changes: 2 additions & 2 deletions internal/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ const (
// Encode Infinity or Nan float into `null`, instead of returning an error.
EncodeNullForInfOrNan Options = 1 << alg.BitEncodeNullForInfOrNan

// Int64 or Uint64 into strings on Marshal
Integer64BitToString Options = 1 << alg.BitInteger64BitToString
// Uint64 into strings on Marshal
Uint64ToString Options = 1 << alg.BitUint64ToString
)

// Encoder represents a specific set of encoder configurations.
Expand Down
12 changes: 1 addition & 11 deletions internal/encoder/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,8 @@ func Execute(b *[]byte, p unsafe.Pointer, s *vars.Stack, flags uint64, prog *ir.
v := *(*int32)(p)
buf = alg.I64toa(buf, int64(v))
case ir.OP_i64:
quote := false
if ins.CompatOp() == ir.OP_i64 &&
!ins.IsMapKey() &&
flags&(1<<alg.BitInteger64BitToString) != 0 {
buf = append(buf, '"')
quote = true
}
v := *(*int64)(p)
buf = alg.I64toa(buf, int64(v))
if quote {
buf = append(buf, '"')
}
case ir.OP_u8:
v := *(*uint8)(p)
buf = alg.U64toa(buf, uint64(v))
Expand All @@ -165,7 +155,7 @@ func Execute(b *[]byte, p unsafe.Pointer, s *vars.Stack, flags uint64, prog *ir.
quote := false
if ins.CompatOp() == ir.OP_u64 &&
!ins.IsMapKey() &&
flags&(1<<alg.BitInteger64BitToString) != 0 {
flags&(1<<alg.BitUint64ToString) != 0 {
buf = append(buf, '"')
quote = true
}
Expand Down
15 changes: 1 addition & 14 deletions internal/encoder/x86/assembler_regabi_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,20 +845,7 @@ func (self *Assembler) _asm_OP_i32(_ *ir.Instr) {
}

func (self *Assembler) _asm_OP_i64(i *ir.Instr) {
if i.CompatOp() == ir.OP_i || i.IsMapKey() {
self.store_int(21, _F_i64toa, "MOVQ")
return
}
self.Emit("BTQ", jit.Imm(int64(alg.BitInteger64BitToString)), _ARG_fv)
self.Sjmp("JC", "_i64_to_string{n}")
self.store_int(21, _F_i64toa, "MOVQ")
self.Sjmp("JMP", "_i64_to_string_end{n}")
self.Link("_i64_to_string{n}")

self.add_char('"')
self.store_int(21, _F_i64toa, "MOVQ")
self.add_char('"')
self.Link("_i64_to_string_end{n}")
}

func (self *Assembler) _asm_OP_u8(_ *ir.Instr) {
Expand All @@ -878,7 +865,7 @@ func (self *Assembler) _asm_OP_u64(i *ir.Instr) {
self.store_int(20, _F_u64toa, "MOVQ")
return
}
self.Emit("BTQ", jit.Imm(int64(alg.BitInteger64BitToString)), _ARG_fv)
self.Emit("BTQ", jit.Imm(int64(alg.BitUint64ToString)), _ARG_fv)
self.Sjmp("JC", "_ui64_to_string{n}")
self.store_int(20, _F_u64toa, "MOVQ")
self.Sjmp("JMP", "_ui64_to_string_end{n}")
Expand Down
4 changes: 2 additions & 2 deletions sonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func (cfg Config) Froze() API {
if cfg.EncodeNullForInfOrNan {
api.encoderOpts |= encoder.EncodeNullForInfOrNan
}
if cfg.Integer64BitToString {
api.encoderOpts |= encoder.Integer64BitToString
if cfg.Uint64ToString {
api.encoderOpts |= encoder.Uint64ToString
}

// configure decoder options:
Expand Down

0 comments on commit 5a6ea4f

Please sign in to comment.