2020-04-25 13:55:05 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
2020-05-07 15:30:36 +03:00
|
|
|
"sync"
|
2020-04-25 13:55:05 +03:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sliceDecoder struct {
|
|
|
|
elemType *rtype
|
|
|
|
valueDecoder decoder
|
|
|
|
size uintptr
|
2020-05-07 15:30:36 +03:00
|
|
|
arrayPool sync.Pool
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSliceDecoder(dec decoder, elemType *rtype, size uintptr) *sliceDecoder {
|
|
|
|
return &sliceDecoder{
|
|
|
|
valueDecoder: dec,
|
|
|
|
elemType: elemType,
|
|
|
|
size: size,
|
2020-05-07 15:30:36 +03:00
|
|
|
arrayPool: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
cap := 2
|
|
|
|
return &reflect.SliceHeader{
|
|
|
|
Data: uintptr(newArray(elemType, cap)),
|
|
|
|
Len: 0,
|
|
|
|
Cap: cap,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 15:30:36 +03:00
|
|
|
func (d *sliceDecoder) newSlice() *reflect.SliceHeader {
|
|
|
|
slice := d.arrayPool.Get().(*reflect.SliceHeader)
|
|
|
|
slice.Len = 0
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *sliceDecoder) releaseSlice(p *reflect.SliceHeader) {
|
|
|
|
d.arrayPool.Put(p)
|
|
|
|
}
|
|
|
|
|
2020-04-25 13:55:05 +03:00
|
|
|
//go:linkname copySlice reflect.typedslicecopy
|
|
|
|
func copySlice(elemType *rtype, dst, src reflect.SliceHeader) int
|
|
|
|
|
|
|
|
//go:linkname newArray reflect.unsafe_NewArray
|
|
|
|
func newArray(*rtype, int) unsafe.Pointer
|
|
|
|
|
2020-05-06 20:37:29 +03:00
|
|
|
func (d *sliceDecoder) decode(buf []byte, cursor int, p uintptr) (int, error) {
|
|
|
|
buflen := len(buf)
|
2020-04-25 13:55:05 +03:00
|
|
|
for ; cursor < buflen; cursor++ {
|
|
|
|
switch buf[cursor] {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
|
|
|
continue
|
|
|
|
case '[':
|
|
|
|
idx := 0
|
2020-05-07 15:30:36 +03:00
|
|
|
slice := d.newSlice()
|
|
|
|
cap := slice.Cap
|
|
|
|
data := slice.Data
|
2020-04-25 13:55:05 +03:00
|
|
|
for {
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor++
|
2020-04-25 13:55:05 +03:00
|
|
|
if cap <= idx {
|
|
|
|
src := reflect.SliceHeader{Data: data, Len: idx, Cap: cap}
|
|
|
|
cap *= 2
|
|
|
|
data = uintptr(newArray(d.elemType, cap))
|
|
|
|
dst := reflect.SliceHeader{Data: data, Len: idx, Cap: cap}
|
|
|
|
copySlice(d.elemType, dst, src)
|
|
|
|
}
|
2020-05-06 20:37:29 +03:00
|
|
|
c, err := d.valueDecoder.decode(buf, cursor, data+uintptr(idx)*d.size)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor = c
|
|
|
|
cursor = skipWhiteSpace(buf, cursor)
|
2020-04-25 13:55:05 +03:00
|
|
|
switch buf[cursor] {
|
|
|
|
case ']':
|
2020-05-07 15:30:36 +03:00
|
|
|
slice.Cap = cap
|
|
|
|
slice.Len = idx + 1
|
|
|
|
slice.Data = data
|
|
|
|
dstCap := idx + 1
|
|
|
|
dst := reflect.SliceHeader{
|
|
|
|
Data: uintptr(newArray(d.elemType, dstCap)),
|
2020-04-25 13:55:05 +03:00
|
|
|
Len: idx + 1,
|
2020-05-07 15:30:36 +03:00
|
|
|
Cap: dstCap,
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
2020-05-07 15:30:36 +03:00
|
|
|
copySlice(d.elemType, dst, *slice)
|
|
|
|
*(*reflect.SliceHeader)(unsafe.Pointer(p)) = dst
|
|
|
|
d.releaseSlice(slice)
|
2020-05-06 20:37:29 +03:00
|
|
|
cursor++
|
|
|
|
return cursor, nil
|
2020-04-25 13:55:05 +03:00
|
|
|
case ',':
|
|
|
|
idx++
|
|
|
|
continue
|
|
|
|
default:
|
2020-05-07 15:30:36 +03:00
|
|
|
slice.Cap = cap
|
|
|
|
slice.Data = data
|
|
|
|
d.releaseSlice(slice)
|
2020-05-06 20:37:29 +03:00
|
|
|
return 0, errors.New("syntax error slice")
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 20:37:29 +03:00
|
|
|
return 0, errors.New("unexpected error slice")
|
2020-04-25 13:55:05 +03:00
|
|
|
}
|