Skip to content

Commit

Permalink
Refactor encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Jan 3, 2022
1 parent 0707c2a commit 6af83d9
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 35 deletions.
5 changes: 3 additions & 2 deletions internal/cmd/generator/vm.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,12 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]b
break
}
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)
store(ctxptr, code.Idx, uintptr(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)
} else {
mapCtx.Start = len(b)
Expand Down
39 changes: 19 additions & 20 deletions internal/encoder/decode_rune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

// 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 (
lineSep = byte(168) //'\u2028'
paragraphSep = byte(169) //'\u2029'
Expand Down Expand Up @@ -80,25 +73,31 @@ func decodeRuneInString(s string) (decodeRuneState, int) {
return validUTF8State, 1
}
sz := int(x & 7)
var accept acceptRange
if n < sz {
return runeErrorState, 1
}
s1 := s[1]
switch x >> 4 {
case 0:
accept = acceptRange{locb, hicb}
if s1 < locb || hicb < s1 {
return runeErrorState, 1
}
case 1:
accept = acceptRange{0xA0, hicb}
if s1 < 0xA0 || hicb < s1 {
return runeErrorState, 1
}
case 2:
accept = acceptRange{locb, 0x9F}
if s1 < locb || 0x9F < s1 {
return runeErrorState, 1
}
case 3:
accept = acceptRange{0x90, hicb}
if s1 < 0x90 || hicb < s1 {
return runeErrorState, 1
}
case 4:
accept = acceptRange{locb, 0x8F}
}
if n < sz {
return runeErrorState, 1
}
s1 := s[1]
if s1 < accept.lo || accept.hi < s1 {
return runeErrorState, 1
if s1 < locb || 0x8F < s1 {
return runeErrorState, 1
}
}
if sz <= 2 {
return validUTF8State, 2
Expand Down
12 changes: 7 additions & 5 deletions internal/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,14 @@ var mapContextPool = sync.Pool{
},
}

func NewMapContext(mapLen int) *MapContext {
func NewMapContext(mapLen int, unorderedMap bool) *MapContext {
ctx := mapContextPool.Get().(*MapContext)
if len(ctx.Slice.Items) < mapLen {
ctx.Slice.Items = make([]MapItem, mapLen)
} else {
ctx.Slice.Items = ctx.Slice.Items[:mapLen]
if !unorderedMap {
if len(ctx.Slice.Items) < mapLen {
ctx.Slice.Items = make([]MapItem, mapLen)
} else {
ctx.Slice.Items = ctx.Slice.Items[:mapLen]
}
}
ctx.Buf = ctx.Buf[:0]
ctx.Iter = mapIter{}
Expand Down
5 changes: 3 additions & 2 deletions internal/encoder/vm/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions internal/encoder/vm_color/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions internal/encoder/vm_color_indent/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions internal/encoder/vm_indent/vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6af83d9

Please sign in to comment.