2017-01-07 15:25:23 +02:00
|
|
|
package jsoniter
|
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
var digits []uint32
|
2017-01-07 15:25:23 +02:00
|
|
|
|
|
|
|
func init() {
|
2017-07-09 08:48:34 +02:00
|
|
|
digits = make([]uint32, 1000)
|
2017-01-21 17:22:38 +02:00
|
|
|
for i := uint32(0); i < 1000; i++ {
|
2017-07-09 08:48:34 +02:00
|
|
|
digits[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0'
|
2017-01-21 17:22:38 +02:00
|
|
|
if i < 10 {
|
2017-07-09 08:48:34 +02:00
|
|
|
digits[i] += 2 << 24
|
2017-01-21 17:22:38 +02:00
|
|
|
} else if i < 100 {
|
2017-07-09 08:48:34 +02:00
|
|
|
digits[i] += 1 << 24
|
2017-01-21 17:22:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFirstBuf(buf []byte, v uint32, n int) int {
|
|
|
|
start := v >> 24
|
|
|
|
if start == 0 {
|
|
|
|
buf[n] = byte(v >> 16)
|
|
|
|
n++
|
|
|
|
buf[n] = byte(v >> 8)
|
|
|
|
n++
|
|
|
|
} else if start == 1 {
|
|
|
|
buf[n] = byte(v >> 8)
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
buf[n] = byte(v)
|
|
|
|
n++
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeBuf(buf []byte, v uint32, n int) {
|
|
|
|
buf[n] = byte(v >> 16)
|
2017-06-06 17:27:00 +02:00
|
|
|
buf[n+1] = byte(v >> 8)
|
|
|
|
buf[n+2] = byte(v)
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-01-21 17:22:38 +02:00
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
// WriteUint8 write uint8 to stream
|
2017-01-07 15:25:23 +02:00
|
|
|
func (stream *Stream) WriteUint8(val uint8) {
|
2017-06-06 03:44:56 +02:00
|
|
|
stream.ensure(3)
|
2017-07-09 08:48:34 +02:00
|
|
|
stream.n = writeFirstBuf(stream.buf, digits[val], stream.n)
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
// WriteInt8 write int8 to stream
|
2017-01-21 17:22:38 +02:00
|
|
|
func (stream *Stream) WriteInt8(nval int8) {
|
2017-06-06 03:44:56 +02:00
|
|
|
stream.ensure(4)
|
2017-01-21 17:22:38 +02:00
|
|
|
n := stream.n
|
|
|
|
var val uint8
|
2017-06-06 03:44:56 +02:00
|
|
|
if nval < 0 {
|
2017-01-21 17:22:38 +02:00
|
|
|
val = uint8(-nval)
|
|
|
|
stream.buf[n] = '-'
|
|
|
|
n++
|
2017-01-07 15:25:23 +02:00
|
|
|
} else {
|
2017-01-21 17:22:38 +02:00
|
|
|
val = uint8(nval)
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-07-09 08:48:34 +02:00
|
|
|
stream.n = writeFirstBuf(stream.buf, digits[val], n)
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
// WriteUint16 write uint16 to stream
|
2017-01-07 15:25:23 +02:00
|
|
|
func (stream *Stream) WriteUint16(val uint16) {
|
2017-06-06 03:44:56 +02:00
|
|
|
stream.ensure(5)
|
2017-01-21 17:22:38 +02:00
|
|
|
q1 := val / 1000
|
|
|
|
if q1 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
stream.n = writeFirstBuf(stream.buf, digits[val], stream.n)
|
2017-01-21 17:22:38 +02:00
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r1 := val - q1*1000
|
2017-07-09 08:48:34 +02:00
|
|
|
n := writeFirstBuf(stream.buf, digits[q1], stream.n)
|
|
|
|
writeBuf(stream.buf, digits[r1], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 3
|
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
// WriteInt16 write int16 to stream
|
2017-01-21 17:22:38 +02:00
|
|
|
func (stream *Stream) WriteInt16(nval int16) {
|
2017-06-06 03:44:56 +02:00
|
|
|
stream.ensure(6)
|
2017-01-21 17:22:38 +02:00
|
|
|
n := stream.n
|
|
|
|
var val uint16
|
2017-06-06 03:44:56 +02:00
|
|
|
if nval < 0 {
|
2017-01-21 17:22:38 +02:00
|
|
|
val = uint16(-nval)
|
|
|
|
stream.buf[n] = '-'
|
|
|
|
n++
|
2017-01-07 15:25:23 +02:00
|
|
|
} else {
|
2017-01-21 17:22:38 +02:00
|
|
|
val = uint16(nval)
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-01-21 17:22:38 +02:00
|
|
|
q1 := val / 1000
|
|
|
|
if q1 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
stream.n = writeFirstBuf(stream.buf, digits[val], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r1 := val - q1*1000
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q1], n)
|
|
|
|
writeBuf(stream.buf, digits[r1], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 3
|
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
// WriteUint32 write uint32 to stream
|
2017-01-07 15:25:23 +02:00
|
|
|
func (stream *Stream) WriteUint32(val uint32) {
|
2017-06-06 03:44:56 +02:00
|
|
|
stream.ensure(10)
|
2017-01-21 17:22:38 +02:00
|
|
|
n := stream.n
|
|
|
|
q1 := val / 1000
|
|
|
|
if q1 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
stream.n = writeFirstBuf(stream.buf, digits[val], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r1 := val - q1*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q2 := q1 / 1000
|
|
|
|
if q2 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n := writeFirstBuf(stream.buf, digits[q1], n)
|
|
|
|
writeBuf(stream.buf, digits[r1], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 3
|
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r2 := q1 - q2*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q3 := q2 / 1000
|
|
|
|
if q3 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q2], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
} else {
|
2017-06-06 17:27:00 +02:00
|
|
|
r3 := q2 - q3*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.buf[n] = byte(q3 + '0')
|
|
|
|
n++
|
2017-07-09 08:48:34 +02:00
|
|
|
writeBuf(stream.buf, digits[r3], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
n += 3
|
|
|
|
}
|
2017-07-09 08:48:34 +02:00
|
|
|
writeBuf(stream.buf, digits[r2], n)
|
|
|
|
writeBuf(stream.buf, digits[r1], n+3)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 6
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
// WriteInt32 write int32 to stream
|
2017-01-21 17:22:38 +02:00
|
|
|
func (stream *Stream) WriteInt32(nval int32) {
|
2017-06-06 03:44:56 +02:00
|
|
|
stream.ensure(11)
|
2017-01-21 17:22:38 +02:00
|
|
|
n := stream.n
|
|
|
|
var val uint32
|
2017-06-06 17:27:00 +02:00
|
|
|
if nval < 0 {
|
2017-01-21 17:22:38 +02:00
|
|
|
val = uint32(-nval)
|
|
|
|
stream.buf[n] = '-'
|
|
|
|
n++
|
2017-01-07 15:25:23 +02:00
|
|
|
} else {
|
2017-01-21 17:22:38 +02:00
|
|
|
val = uint32(nval)
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-01-21 17:22:38 +02:00
|
|
|
q1 := val / 1000
|
|
|
|
if q1 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
stream.n = writeFirstBuf(stream.buf, digits[val], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r1 := val - q1*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q2 := q1 / 1000
|
|
|
|
if q2 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n := writeFirstBuf(stream.buf, digits[q1], n)
|
|
|
|
writeBuf(stream.buf, digits[r1], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 3
|
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r2 := q1 - q2*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q3 := q2 / 1000
|
|
|
|
if q3 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q2], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
} else {
|
2017-06-06 17:27:00 +02:00
|
|
|
r3 := q2 - q3*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.buf[n] = byte(q3 + '0')
|
|
|
|
n++
|
2017-07-09 08:48:34 +02:00
|
|
|
writeBuf(stream.buf, digits[r3], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
n += 3
|
|
|
|
}
|
2017-07-09 08:48:34 +02:00
|
|
|
writeBuf(stream.buf, digits[r2], n)
|
|
|
|
writeBuf(stream.buf, digits[r1], n+3)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 6
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
// WriteUint64 write uint64 to stream
|
2017-01-07 15:25:23 +02:00
|
|
|
func (stream *Stream) WriteUint64(val uint64) {
|
2017-06-06 03:44:56 +02:00
|
|
|
stream.ensure(20)
|
2017-01-21 17:22:38 +02:00
|
|
|
n := stream.n
|
|
|
|
q1 := val / 1000
|
|
|
|
if q1 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
stream.n = writeFirstBuf(stream.buf, digits[val], n)
|
2017-01-07 15:25:23 +02:00
|
|
|
return
|
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r1 := val - q1*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q2 := q1 / 1000
|
|
|
|
if q2 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n := writeFirstBuf(stream.buf, digits[q1], n)
|
|
|
|
writeBuf(stream.buf, digits[r1], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 3
|
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r2 := q1 - q2*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q3 := q2 / 1000
|
|
|
|
if q3 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q2], n)
|
|
|
|
writeBuf(stream.buf, digits[r2], n)
|
|
|
|
writeBuf(stream.buf, digits[r1], n+3)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 6
|
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r3 := q2 - q3*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q4 := q3 / 1000
|
|
|
|
if q4 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q3], n)
|
|
|
|
writeBuf(stream.buf, digits[r3], n)
|
|
|
|
writeBuf(stream.buf, digits[r2], n+3)
|
|
|
|
writeBuf(stream.buf, digits[r1], n+6)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 9
|
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r4 := q3 - q4*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q5 := q4 / 1000
|
|
|
|
if q5 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q4], n)
|
|
|
|
writeBuf(stream.buf, digits[r4], n)
|
|
|
|
writeBuf(stream.buf, digits[r3], n+3)
|
|
|
|
writeBuf(stream.buf, digits[r2], n+6)
|
|
|
|
writeBuf(stream.buf, digits[r1], n+9)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 12
|
|
|
|
return
|
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r5 := q4 - q5*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q6 := q5 / 1000
|
|
|
|
if q6 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q5], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
} else {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q6], n)
|
2017-06-06 17:27:00 +02:00
|
|
|
r6 := q5 - q6*1000
|
2017-07-09 08:48:34 +02:00
|
|
|
writeBuf(stream.buf, digits[r6], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
n += 3
|
|
|
|
}
|
2017-07-09 08:48:34 +02:00
|
|
|
writeBuf(stream.buf, digits[r5], n)
|
|
|
|
writeBuf(stream.buf, digits[r4], n+3)
|
|
|
|
writeBuf(stream.buf, digits[r3], n+6)
|
|
|
|
writeBuf(stream.buf, digits[r2], n+9)
|
|
|
|
writeBuf(stream.buf, digits[r1], n+12)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 15
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
// WriteInt64 write int64 to stream
|
2017-01-21 17:22:38 +02:00
|
|
|
func (stream *Stream) WriteInt64(nval int64) {
|
2017-06-06 03:44:56 +02:00
|
|
|
stream.ensure(20)
|
2017-01-21 17:22:38 +02:00
|
|
|
n := stream.n
|
|
|
|
var val uint64
|
2017-06-06 17:27:00 +02:00
|
|
|
if nval < 0 {
|
2017-01-21 17:22:38 +02:00
|
|
|
val = uint64(-nval)
|
|
|
|
stream.buf[n] = '-'
|
|
|
|
n++
|
|
|
|
} else {
|
|
|
|
val = uint64(nval)
|
|
|
|
}
|
|
|
|
q1 := val / 1000
|
|
|
|
if q1 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
stream.n = writeFirstBuf(stream.buf, digits[val], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
return
|
2017-01-07 15:25:23 +02:00
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r1 := val - q1*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q2 := q1 / 1000
|
|
|
|
if q2 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n := writeFirstBuf(stream.buf, digits[q1], n)
|
|
|
|
writeBuf(stream.buf, digits[r1], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 3
|
|
|
|
return
|
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r2 := q1 - q2*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q3 := q2 / 1000
|
|
|
|
if q3 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q2], n)
|
|
|
|
writeBuf(stream.buf, digits[r2], n)
|
|
|
|
writeBuf(stream.buf, digits[r1], n+3)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 6
|
|
|
|
return
|
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r3 := q2 - q3*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q4 := q3 / 1000
|
|
|
|
if q4 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q3], n)
|
|
|
|
writeBuf(stream.buf, digits[r3], n)
|
|
|
|
writeBuf(stream.buf, digits[r2], n+3)
|
|
|
|
writeBuf(stream.buf, digits[r1], n+6)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 9
|
|
|
|
return
|
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r4 := q3 - q4*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q5 := q4 / 1000
|
|
|
|
if q5 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q4], n)
|
|
|
|
writeBuf(stream.buf, digits[r4], n)
|
|
|
|
writeBuf(stream.buf, digits[r3], n+3)
|
|
|
|
writeBuf(stream.buf, digits[r2], n+6)
|
|
|
|
writeBuf(stream.buf, digits[r1], n+9)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 12
|
|
|
|
return
|
|
|
|
}
|
2017-06-06 17:27:00 +02:00
|
|
|
r5 := q4 - q5*1000
|
2017-01-21 17:22:38 +02:00
|
|
|
q6 := q5 / 1000
|
|
|
|
if q6 == 0 {
|
2017-07-09 08:48:34 +02:00
|
|
|
n = writeFirstBuf(stream.buf, digits[q5], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
} else {
|
|
|
|
stream.buf[n] = byte(q6 + '0')
|
|
|
|
n++
|
2017-06-06 17:27:00 +02:00
|
|
|
r6 := q5 - q6*1000
|
2017-07-09 08:48:34 +02:00
|
|
|
writeBuf(stream.buf, digits[r6], n)
|
2017-01-21 17:22:38 +02:00
|
|
|
n += 3
|
|
|
|
}
|
2017-07-09 08:48:34 +02:00
|
|
|
writeBuf(stream.buf, digits[r5], n)
|
|
|
|
writeBuf(stream.buf, digits[r4], n+3)
|
|
|
|
writeBuf(stream.buf, digits[r3], n+6)
|
|
|
|
writeBuf(stream.buf, digits[r2], n+9)
|
|
|
|
writeBuf(stream.buf, digits[r1], n+12)
|
2017-01-21 17:22:38 +02:00
|
|
|
stream.n = n + 15
|
2017-01-07 16:08:45 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
// WriteInt write int to stream
|
2017-01-07 16:08:45 +02:00
|
|
|
func (stream *Stream) WriteInt(val int) {
|
|
|
|
stream.WriteInt64(int64(val))
|
|
|
|
}
|
|
|
|
|
2017-07-09 08:48:34 +02:00
|
|
|
// WriteUint write uint to stream
|
2017-01-07 16:08:45 +02:00
|
|
|
func (stream *Stream) WriteUint(val uint) {
|
|
|
|
stream.WriteUint64(uint64(val))
|
2017-06-06 17:27:00 +02:00
|
|
|
}
|