From 5a6ea4ffd67348d8c3793bf619560d7f8094f311 Mon Sep 17 00:00:00 2001 From: zhangming12 Date: Mon, 16 Dec 2024 20:44:50 +0800 Subject: [PATCH] opt: encoder support Uint64ToString --- api.go | 4 ++-- encode_test.go | 20 +++++++++---------- encoder/encoder_native.go | 4 ++-- internal/encoder/alg/opts.go | 2 +- internal/encoder/encoder.go | 4 ++-- internal/encoder/vm/vm.go | 12 +---------- .../encoder/x86/assembler_regabi_amd64.go | 15 +------------- sonic.go | 4 ++-- 8 files changed, 21 insertions(+), 44 deletions(-) diff --git a/api.go b/api.go index abd9f63f0..26cdbaba7 100644 --- a/api.go +++ b/api.go @@ -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 ( diff --git a/encode_test.go b/encode_test.go index 1e226d5d4..3a64abd6a 100644 --- a/encode_test.go +++ b/encode_test.go @@ -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 { @@ -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}`, }, { @@ -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}`, }, { @@ -1266,7 +1266,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}`, }, { @@ -1274,13 +1274,13 @@ func TestInt64OrUInteger64BitToString(t *testing.T) { 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`, }, { @@ -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}}}`, }, @@ -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) }) diff --git a/encoder/encoder_native.go b/encoder/encoder_native.go index 9aa99d56d..9eb159e3c 100644 --- a/encoder/encoder_native.go +++ b/encoder/encoder_native.go @@ -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 ) diff --git a/internal/encoder/alg/opts.go b/internal/encoder/alg/opts.go index 3c962ba7e..8fc94f721 100644 --- a/internal/encoder/alg/opts.go +++ b/internal/encoder/alg/opts.go @@ -26,7 +26,7 @@ const ( BitNoValidateJSONMarshaler BitNoEncoderNewline BitEncodeNullForInfOrNan - BitInteger64BitToString + BitUint64ToString BitPointerValue = 63 ) diff --git a/internal/encoder/encoder.go b/internal/encoder/encoder.go index c7a5de188..c9b54f29f 100644 --- a/internal/encoder/encoder.go +++ b/internal/encoder/encoder.go @@ -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. diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go index 5e7c88d41..c767b6af7 100644 --- a/internal/encoder/vm/vm.go +++ b/internal/encoder/vm/vm.go @@ -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<