mirror of https://github.com/goccy/go-json.git
Refactor encoder
This commit is contained in:
parent
0707c2a188
commit
6af83d9bdd
|
@ -403,11 +403,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
b = appendStructHead(ctx, b)
|
b = appendStructHead(ctx, b)
|
||||||
mapCtx := encoder.NewMapContext(mlen)
|
unorderedMap := (ctx.Option.Flag & encoder.UnorderedMapOption) != 0
|
||||||
|
mapCtx := encoder.NewMapContext(mlen, unorderedMap)
|
||||||
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
||||||
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
||||||
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
||||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
if unorderedMap {
|
||||||
b = appendMapKeyIndent(ctx, code.Next, b)
|
b = appendMapKeyIndent(ctx, code.Next, b)
|
||||||
} else {
|
} else {
|
||||||
mapCtx.Start = len(b)
|
mapCtx.Start = len(b)
|
||||||
|
|
|
@ -44,13 +44,6 @@ var first = [256]uint8{
|
||||||
s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF
|
s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF
|
||||||
}
|
}
|
||||||
|
|
||||||
// acceptRange gives the range of valid values for the second byte in a UTF-8
|
|
||||||
// sequence.
|
|
||||||
type acceptRange struct {
|
|
||||||
lo uint8 // lowest value for second byte.
|
|
||||||
hi uint8 // highest value for second byte.
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
lineSep = byte(168) //'\u2028'
|
lineSep = byte(168) //'\u2028'
|
||||||
paragraphSep = byte(169) //'\u2029'
|
paragraphSep = byte(169) //'\u2029'
|
||||||
|
@ -80,26 +73,32 @@ func decodeRuneInString(s string) (decodeRuneState, int) {
|
||||||
return validUTF8State, 1
|
return validUTF8State, 1
|
||||||
}
|
}
|
||||||
sz := int(x & 7)
|
sz := int(x & 7)
|
||||||
var accept acceptRange
|
|
||||||
switch x >> 4 {
|
|
||||||
case 0:
|
|
||||||
accept = acceptRange{locb, hicb}
|
|
||||||
case 1:
|
|
||||||
accept = acceptRange{0xA0, hicb}
|
|
||||||
case 2:
|
|
||||||
accept = acceptRange{locb, 0x9F}
|
|
||||||
case 3:
|
|
||||||
accept = acceptRange{0x90, hicb}
|
|
||||||
case 4:
|
|
||||||
accept = acceptRange{locb, 0x8F}
|
|
||||||
}
|
|
||||||
if n < sz {
|
if n < sz {
|
||||||
return runeErrorState, 1
|
return runeErrorState, 1
|
||||||
}
|
}
|
||||||
s1 := s[1]
|
s1 := s[1]
|
||||||
if s1 < accept.lo || accept.hi < s1 {
|
switch x >> 4 {
|
||||||
|
case 0:
|
||||||
|
if s1 < locb || hicb < s1 {
|
||||||
return runeErrorState, 1
|
return runeErrorState, 1
|
||||||
}
|
}
|
||||||
|
case 1:
|
||||||
|
if s1 < 0xA0 || hicb < s1 {
|
||||||
|
return runeErrorState, 1
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
if s1 < locb || 0x9F < s1 {
|
||||||
|
return runeErrorState, 1
|
||||||
|
}
|
||||||
|
case 3:
|
||||||
|
if s1 < 0x90 || hicb < s1 {
|
||||||
|
return runeErrorState, 1
|
||||||
|
}
|
||||||
|
case 4:
|
||||||
|
if s1 < locb || 0x8F < s1 {
|
||||||
|
return runeErrorState, 1
|
||||||
|
}
|
||||||
|
}
|
||||||
if sz <= 2 {
|
if sz <= 2 {
|
||||||
return validUTF8State, 2
|
return validUTF8State, 2
|
||||||
}
|
}
|
||||||
|
|
|
@ -275,13 +275,15 @@ var mapContextPool = sync.Pool{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMapContext(mapLen int) *MapContext {
|
func NewMapContext(mapLen int, unorderedMap bool) *MapContext {
|
||||||
ctx := mapContextPool.Get().(*MapContext)
|
ctx := mapContextPool.Get().(*MapContext)
|
||||||
|
if !unorderedMap {
|
||||||
if len(ctx.Slice.Items) < mapLen {
|
if len(ctx.Slice.Items) < mapLen {
|
||||||
ctx.Slice.Items = make([]MapItem, mapLen)
|
ctx.Slice.Items = make([]MapItem, mapLen)
|
||||||
} else {
|
} else {
|
||||||
ctx.Slice.Items = ctx.Slice.Items[:mapLen]
|
ctx.Slice.Items = ctx.Slice.Items[:mapLen]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
ctx.Buf = ctx.Buf[:0]
|
ctx.Buf = ctx.Buf[:0]
|
||||||
ctx.Iter = mapIter{}
|
ctx.Iter = mapIter{}
|
||||||
ctx.Idx = 0
|
ctx.Idx = 0
|
||||||
|
|
|
@ -403,11 +403,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
b = appendStructHead(ctx, b)
|
b = appendStructHead(ctx, b)
|
||||||
mapCtx := encoder.NewMapContext(mlen)
|
unorderedMap := (ctx.Option.Flag & encoder.UnorderedMapOption) != 0
|
||||||
|
mapCtx := encoder.NewMapContext(mlen, unorderedMap)
|
||||||
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
||||||
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
||||||
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
||||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
if unorderedMap {
|
||||||
b = appendMapKeyIndent(ctx, code.Next, b)
|
b = appendMapKeyIndent(ctx, code.Next, b)
|
||||||
} else {
|
} else {
|
||||||
mapCtx.Start = len(b)
|
mapCtx.Start = len(b)
|
||||||
|
|
|
@ -403,11 +403,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
b = appendStructHead(ctx, b)
|
b = appendStructHead(ctx, b)
|
||||||
mapCtx := encoder.NewMapContext(mlen)
|
unorderedMap := (ctx.Option.Flag & encoder.UnorderedMapOption) != 0
|
||||||
|
mapCtx := encoder.NewMapContext(mlen, unorderedMap)
|
||||||
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
||||||
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
||||||
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
||||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
if unorderedMap {
|
||||||
b = appendMapKeyIndent(ctx, code.Next, b)
|
b = appendMapKeyIndent(ctx, code.Next, b)
|
||||||
} else {
|
} else {
|
||||||
mapCtx.Start = len(b)
|
mapCtx.Start = len(b)
|
||||||
|
|
|
@ -403,11 +403,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
b = appendStructHead(ctx, b)
|
b = appendStructHead(ctx, b)
|
||||||
mapCtx := encoder.NewMapContext(mlen)
|
unorderedMap := (ctx.Option.Flag & encoder.UnorderedMapOption) != 0
|
||||||
|
mapCtx := encoder.NewMapContext(mlen, unorderedMap)
|
||||||
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
||||||
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
||||||
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
||||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
if unorderedMap {
|
||||||
b = appendMapKeyIndent(ctx, code.Next, b)
|
b = appendMapKeyIndent(ctx, code.Next, b)
|
||||||
} else {
|
} else {
|
||||||
mapCtx.Start = len(b)
|
mapCtx.Start = len(b)
|
||||||
|
|
|
@ -403,11 +403,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
b = appendStructHead(ctx, b)
|
b = appendStructHead(ctx, b)
|
||||||
mapCtx := encoder.NewMapContext(mlen)
|
unorderedMap := (ctx.Option.Flag & encoder.UnorderedMapOption) != 0
|
||||||
|
mapCtx := encoder.NewMapContext(mlen, unorderedMap)
|
||||||
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
mapiterinit(code.Type, uptr, &mapCtx.Iter)
|
||||||
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
store(ctxptr, code.Idx, uintptr(unsafe.Pointer(mapCtx)))
|
||||||
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
ctx.KeepRefs = append(ctx.KeepRefs, unsafe.Pointer(mapCtx))
|
||||||
if (ctx.Option.Flag & encoder.UnorderedMapOption) != 0 {
|
if unorderedMap {
|
||||||
b = appendMapKeyIndent(ctx, code.Next, b)
|
b = appendMapKeyIndent(ctx, code.Next, b)
|
||||||
} else {
|
} else {
|
||||||
mapCtx.Start = len(b)
|
mapCtx.Start = len(b)
|
||||||
|
|
Loading…
Reference in New Issue