Skip to content

Commit

Permalink
feat: support copy string
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Jan 30, 2024
1 parent 4823eec commit 3135e27
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 118 deletions.
2 changes: 0 additions & 2 deletions dev/decoder/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

type context struct {
internal.Context
options Options
}

func newCtx(s string, opt Options) (context, error) {
Expand All @@ -17,6 +16,5 @@ func newCtx(s string, opt Options) (context, error) {

return context{
Context: ctx,
options: opt,
}, nil
}
4 changes: 2 additions & 2 deletions dev/decoder/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func (d *efaceDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *conte
var ret interface{}
var err error

if ctx.options&OptionUseNumber == 0 && ctx.options&OptionUseInt64 == 0 {
if Options(ctx.Options)&OptionUseNumber == 0 && Options(ctx.Options)&OptionUseInt64 == 0 {
ret, err = node.AsEface(&ctx.Context)
} else if ctx.options&OptionUseNumber != 0 {
} else if Options(ctx.Options)&OptionUseNumber != 0 {
ret, err = node.AsEfaceUseNumber(&ctx.Context)
} else {
ret, err = node.AsEfaceUseInt64(&ctx.Context)
Expand Down
20 changes: 10 additions & 10 deletions dev/decoder/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func (d *mapEfaceDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *co
return nil
}

if ctx.options&OptionUseNumber == 0 && ctx.options&OptionUseInt64 == 0 {
if Options(ctx.Options)&OptionUseNumber == 0 && Options(ctx.Options)&OptionUseInt64 == 0 {
return node.AsMapEface(&ctx.Context, vp)

}
if ctx.options&OptionUseNumber != 0 {
if Options(ctx.Options)&OptionUseNumber != 0 {
return node.AsMapEfaceUseNumber(&ctx.Context, vp)
}
return node.AsMapEfaceUseInt64(&ctx.Context, vp)
Expand Down Expand Up @@ -160,7 +160,7 @@ func (d *mapI32KeyFastDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ct
next := obj.Children()
for i := 0; i < obj.Len(); i++ {
keyn := internal.NewNode(next)
k, err := keyn.AsKeyI64(&ctx.Context)
k, err := keyn.ParseI64(&ctx.Context)
if k > math.MaxInt32 || k < math.MinInt32 {
return error_value(keyn.AsRaw(&ctx.Context), d.mapType.Key.Pack())
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func (d *mapI32KeyStdDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx
next := obj.Children()
for i := 0; i < obj.Len(); i++ {
keyn := internal.NewNode(next)
k, err := keyn.AsKeyI64(&ctx.Context)
k, err := keyn.ParseI64(&ctx.Context)
if k > math.MaxInt32 || k < math.MinInt32 {
return error_value(keyn.AsRaw(&ctx.Context), d.mapType.Key.Pack())
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func (d *mapI64KeyFastDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ct
next := obj.Children()
for i := 0; i < obj.Len(); i++ {
keyn := internal.NewNode(next)
key, err := keyn.AsKeyI64(&ctx.Context)
key, err := keyn.ParseI64(&ctx.Context)
ku64 := *(*uint64)(unsafe.Pointer(&key))
if err != nil {
return err
Expand Down Expand Up @@ -302,7 +302,7 @@ func (d *mapI64KeyStdDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx
next := obj.Children()
for i := 0; i < obj.Len(); i++ {
keyn := internal.NewNode(next)
key, err := keyn.AsKeyI64(&ctx.Context)
key, err := keyn.ParseI64(&ctx.Context)
if err != nil {
return err
}
Expand Down Expand Up @@ -347,7 +347,7 @@ func (d *mapU32KeyFastDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ct
next := obj.Children()
for i := 0; i < obj.Len(); i++ {
keyn := internal.NewNode(next)
k, err := keyn.AsKeyU64(&ctx.Context)
k, err := keyn.ParseU64(&ctx.Context)
if k > math.MaxUint32 {
return error_value(keyn.AsRaw(&ctx.Context), d.mapType.Key.Pack())
}
Expand Down Expand Up @@ -394,7 +394,7 @@ func (d *mapU32KeyStdDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx
next := obj.Children()
for i := 0; i < obj.Len(); i++ {
keyn := internal.NewNode(next)
k, err := keyn.AsKeyU64(&ctx.Context)
k, err := keyn.ParseU64(&ctx.Context)
if k > math.MaxUint32 {
return error_value(keyn.AsRaw(&ctx.Context), d.mapType.Key.Pack())
}
Expand Down Expand Up @@ -442,7 +442,7 @@ func (d *mapU64KeyFastDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ct

next := obj.Children()
for i := 0; i < obj.Len(); i++ {
key, err := internal.NewNode(next).AsKeyU64(&ctx.Context)
key, err := internal.NewNode(next).ParseU64(&ctx.Context)
if err != nil {
return err
}
Expand Down Expand Up @@ -485,7 +485,7 @@ func (d *mapU64KeyStdDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx
next := obj.Children()
for i := 0; i < obj.Len(); i++ {
keyn := internal.NewNode(next)
key, err := keyn.AsKeyU64(&ctx.Context)
key, err := keyn.ParseU64(&ctx.Context)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions dev/decoder/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func (d *sliceEfaceDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *
return nil
}

if ctx.options&OptionUseNumber == 0 && ctx.options&OptionUseInt64 == 0 {
if Options(ctx.Options)&OptionUseNumber == 0 && Options(ctx.Options)&OptionUseInt64 == 0 {
return node.AsSliceEface(&ctx.Context, vp)
}

if ctx.options&OptionUseNumber != 0 {
if Options(ctx.Options)&OptionUseNumber != 0 {
return node.AsSliceEfaceUseNumber(&ctx.Context, vp)
}

Expand Down
92 changes: 19 additions & 73 deletions dev/decoder/stringopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ func (d *boolStringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *
return nil
}

s, err := node.AsStr(&ctx.Context)
if err != nil {
return err
}

b, err := internal.ParseBool(s)
b, err := node.ParseBool(&ctx.Context)
if err != nil {
return err
}
Expand All @@ -36,18 +31,13 @@ func (d *i8StringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *co
return nil
}

s, err := node.AsStr(&ctx.Context)
if err != nil {
return err
}

ret, err := internal.ParseI64(s)
ret, err := node.ParseI64(&ctx.Context)
if err != nil {
return err
}

if ret > math.MaxInt8 || ret < math.MinInt8 {
return error_value(s, int8Type)
return error_value(node.AsRaw(&ctx.Context), int8Type)
}

*(*int8)(vp) = int8(ret)
Expand All @@ -61,18 +51,13 @@ func (d *i16StringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *c
return nil
}

s, err := node.AsStr(&ctx.Context)
if err != nil {
return err
}

ret, err := internal.ParseI64(s)
ret, err := node.ParseI64(&ctx.Context)
if err != nil {
return err
}

if ret > math.MaxInt16 || ret < math.MinInt16 {
return error_value(s, int16Type)
return error_value(node.AsRaw(&ctx.Context), int16Type)
}

*(*int16)(vp) = int16(ret)
Expand All @@ -86,18 +71,13 @@ func (d *i32StringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *c
return nil
}

s, err := node.AsStr(&ctx.Context)
if err != nil {
return err
}

ret, err := internal.ParseI64(s)
ret, err := node.ParseI64(&ctx.Context)
if err != nil {
return err
}

if ret > math.MaxInt32 || ret < math.MinInt32 {
return error_value(s, int32Type)
return error_value(node.AsRaw(&ctx.Context), int32Type)
}

*(*int32)(vp) = int32(ret)
Expand All @@ -111,12 +91,7 @@ func (d *i64StringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *c
return nil
}

s, err := node.AsStr(&ctx.Context)
if err != nil {
return err
}

ret, err := internal.ParseI64(s)
ret, err := node.ParseI64(&ctx.Context)
if err != nil {
return err
}
Expand All @@ -132,18 +107,13 @@ func (d *u8StringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *co
return nil
}

s, err := node.AsStr(&ctx.Context)
if err != nil {
return err
}

ret, err := internal.ParseU64(s)
ret, err := node.ParseU64(&ctx.Context)
if err != nil {
return err
}

if ret > math.MaxUint8 {
return error_value(s, uint8Type)
return error_value(node.AsRaw(&ctx.Context), uint8Type)
}

*(*uint8)(vp) = uint8(ret)
Expand All @@ -157,18 +127,13 @@ func (d *u16StringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *c
return nil
}

s, err := node.AsStr(&ctx.Context)
if err != nil {
return err
}

ret, err := internal.ParseU64(s)
ret, err := node.ParseU64(&ctx.Context)
if err != nil {
return err
}

if ret > math.MaxUint16 {
return error_value(s, uint16Type)
return error_value(node.AsRaw(&ctx.Context), uint16Type)
}

*(*uint16)(vp) = uint16(ret)
Expand All @@ -182,18 +147,13 @@ func (d *u32StringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *c
return nil
}

s, err := node.AsStr(&ctx.Context)
if err != nil {
return err
}

ret, err := internal.ParseU64(s)
ret, err := node.ParseU64(&ctx.Context)
if err != nil {
return err
}

if ret > math.MaxUint32 {
return error_value(s, uint32Type)
return error_value(node.AsRaw(&ctx.Context), uint32Type)
}

*(*uint32)(vp) = uint32(ret)
Expand All @@ -207,15 +167,11 @@ func (d *u64StringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *c
return nil
}

s, err := node.AsStr(&ctx.Context)
ret, err := node.ParseU64(&ctx.Context)
if err != nil {
return err
}

ret, err := internal.ParseU64(s)
if err != nil {
return err
}
*(*uint64)(vp) = uint64(ret)
return nil
}
Expand All @@ -227,18 +183,13 @@ func (d *f32StringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *c
return nil
}

s, err := node.AsStr(&ctx.Context)
if err != nil {
return err
}

ret, err := internal.ParseF64(s)
ret, err := node.ParseF64(&ctx.Context)
if err != nil {
return err
}

if ret > math.MaxFloat32 || ret < -math.MaxFloat32 {
return error_value(s, float32Type)
return error_value(node.AsRaw(&ctx.Context), float32Type)
}

*(*float32)(vp) = float32(ret)
Expand All @@ -252,12 +203,7 @@ func (d *f64StringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *c
return nil
}

s, err := node.AsStr(&ctx.Context)
if err != nil {
return err
}

ret, err := internal.ParseF64(s)
ret, err := node.ParseF64(&ctx.Context)
if err != nil {
return err
}
Expand All @@ -274,7 +220,7 @@ func (d *strStringDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *c
return nil
}

s, err := node.AsStr(&ctx.Context)
s, err := node.AsStrRef(&ctx.Context)
/* deal with empty string */
if err != nil || s == "" {
return err
Expand Down
4 changes: 2 additions & 2 deletions dev/decoder/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (d *structDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *cont

next := obj.Children()
for i := 0; i < obj.Len(); i++ {
key, err := internal.NewNode(next).AsStr(&ctx.Context)
key, err := internal.NewNode(next).AsStrRef(&ctx.Context)
val := internal.NewNode(internal.PtrOffset(next, 1))
next = val.Next()
if err != nil {
Expand All @@ -47,7 +47,7 @@ func (d *structDecoder) FromDom(vp unsafe.Pointer, node internal.Node, ctx *cont
idx = d.fieldMap.GetCaseInsensitive(key)
}
if idx == -1 {
if ctx.options&OptionDisableUnknown != 0 {
if Options(ctx.Options)&OptionDisableUnknown != 0 {
return error_field(key)
}
continue
Expand Down
18 changes: 0 additions & 18 deletions dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@ import (
"github.com/bytedance/sonic/dev/decoder"
)

// func UnmarshalString_Bak(json string, val interface{}) error {
// ctx, err := internal.NewContext(json)
// if err != nil {
// return err
// }

// node := ctx.Dom.Root()
// ret, err := node.AsIface(&ctx)
// if err != nil {
// return err
// }

// rv := reflect.ValueOf(val)
// rv.Elem().Set(reflect.ValueOf(ret))
// ctx.Delete()
// return nil
// }

func UnmarshalString(json string, val interface{}) error {
dec := decoder.NewDecoder(json)
err := dec.Decode(val)
Expand Down
7 changes: 7 additions & 0 deletions dev/internal/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ const (
ConLenMask = uint64(math.MaxUint32)
ConLenBits = 32
)

const (
_F_use_int64 = 0
_F_disable_urc = 2
_F_disable_unknown = 3
_F_copy_string = 4
)
Loading

0 comments on commit 3135e27

Please sign in to comment.