Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | package jsoniter |
| 2 | |
| 3 | var digits []uint32 |
| 4 | |
| 5 | func init() { |
| 6 | digits = make([]uint32, 1000) |
| 7 | for i := uint32(0); i < 1000; i++ { |
| 8 | digits[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0' |
| 9 | if i < 10 { |
| 10 | digits[i] += 2 << 24 |
| 11 | } else if i < 100 { |
| 12 | digits[i] += 1 << 24 |
| 13 | } |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | func writeFirstBuf(space []byte, v uint32) []byte { |
| 18 | start := v >> 24 |
| 19 | if start == 0 { |
| 20 | space = append(space, byte(v>>16), byte(v>>8)) |
| 21 | } else if start == 1 { |
| 22 | space = append(space, byte(v>>8)) |
| 23 | } |
| 24 | space = append(space, byte(v)) |
| 25 | return space |
| 26 | } |
| 27 | |
| 28 | func writeBuf(buf []byte, v uint32) []byte { |
| 29 | return append(buf, byte(v>>16), byte(v>>8), byte(v)) |
| 30 | } |
| 31 | |
| 32 | // WriteUint8 write uint8 to stream |
| 33 | func (stream *Stream) WriteUint8(val uint8) { |
| 34 | stream.buf = writeFirstBuf(stream.buf, digits[val]) |
| 35 | } |
| 36 | |
| 37 | // WriteInt8 write int8 to stream |
| 38 | func (stream *Stream) WriteInt8(nval int8) { |
| 39 | var val uint8 |
| 40 | if nval < 0 { |
| 41 | val = uint8(-nval) |
| 42 | stream.buf = append(stream.buf, '-') |
| 43 | } else { |
| 44 | val = uint8(nval) |
| 45 | } |
| 46 | stream.buf = writeFirstBuf(stream.buf, digits[val]) |
| 47 | } |
| 48 | |
| 49 | // WriteUint16 write uint16 to stream |
| 50 | func (stream *Stream) WriteUint16(val uint16) { |
| 51 | q1 := val / 1000 |
| 52 | if q1 == 0 { |
| 53 | stream.buf = writeFirstBuf(stream.buf, digits[val]) |
| 54 | return |
| 55 | } |
| 56 | r1 := val - q1*1000 |
| 57 | stream.buf = writeFirstBuf(stream.buf, digits[q1]) |
| 58 | stream.buf = writeBuf(stream.buf, digits[r1]) |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | // WriteInt16 write int16 to stream |
| 63 | func (stream *Stream) WriteInt16(nval int16) { |
| 64 | var val uint16 |
| 65 | if nval < 0 { |
| 66 | val = uint16(-nval) |
| 67 | stream.buf = append(stream.buf, '-') |
| 68 | } else { |
| 69 | val = uint16(nval) |
| 70 | } |
| 71 | stream.WriteUint16(val) |
| 72 | } |
| 73 | |
| 74 | // WriteUint32 write uint32 to stream |
| 75 | func (stream *Stream) WriteUint32(val uint32) { |
| 76 | q1 := val / 1000 |
| 77 | if q1 == 0 { |
| 78 | stream.buf = writeFirstBuf(stream.buf, digits[val]) |
| 79 | return |
| 80 | } |
| 81 | r1 := val - q1*1000 |
| 82 | q2 := q1 / 1000 |
| 83 | if q2 == 0 { |
| 84 | stream.buf = writeFirstBuf(stream.buf, digits[q1]) |
| 85 | stream.buf = writeBuf(stream.buf, digits[r1]) |
| 86 | return |
| 87 | } |
| 88 | r2 := q1 - q2*1000 |
| 89 | q3 := q2 / 1000 |
| 90 | if q3 == 0 { |
| 91 | stream.buf = writeFirstBuf(stream.buf, digits[q2]) |
| 92 | } else { |
| 93 | r3 := q2 - q3*1000 |
| 94 | stream.buf = append(stream.buf, byte(q3+'0')) |
| 95 | stream.buf = writeBuf(stream.buf, digits[r3]) |
| 96 | } |
| 97 | stream.buf = writeBuf(stream.buf, digits[r2]) |
| 98 | stream.buf = writeBuf(stream.buf, digits[r1]) |
| 99 | } |
| 100 | |
| 101 | // WriteInt32 write int32 to stream |
| 102 | func (stream *Stream) WriteInt32(nval int32) { |
| 103 | var val uint32 |
| 104 | if nval < 0 { |
| 105 | val = uint32(-nval) |
| 106 | stream.buf = append(stream.buf, '-') |
| 107 | } else { |
| 108 | val = uint32(nval) |
| 109 | } |
| 110 | stream.WriteUint32(val) |
| 111 | } |
| 112 | |
| 113 | // WriteUint64 write uint64 to stream |
| 114 | func (stream *Stream) WriteUint64(val uint64) { |
| 115 | q1 := val / 1000 |
| 116 | if q1 == 0 { |
| 117 | stream.buf = writeFirstBuf(stream.buf, digits[val]) |
| 118 | return |
| 119 | } |
| 120 | r1 := val - q1*1000 |
| 121 | q2 := q1 / 1000 |
| 122 | if q2 == 0 { |
| 123 | stream.buf = writeFirstBuf(stream.buf, digits[q1]) |
| 124 | stream.buf = writeBuf(stream.buf, digits[r1]) |
| 125 | return |
| 126 | } |
| 127 | r2 := q1 - q2*1000 |
| 128 | q3 := q2 / 1000 |
| 129 | if q3 == 0 { |
| 130 | stream.buf = writeFirstBuf(stream.buf, digits[q2]) |
| 131 | stream.buf = writeBuf(stream.buf, digits[r2]) |
| 132 | stream.buf = writeBuf(stream.buf, digits[r1]) |
| 133 | return |
| 134 | } |
| 135 | r3 := q2 - q3*1000 |
| 136 | q4 := q3 / 1000 |
| 137 | if q4 == 0 { |
| 138 | stream.buf = writeFirstBuf(stream.buf, digits[q3]) |
| 139 | stream.buf = writeBuf(stream.buf, digits[r3]) |
| 140 | stream.buf = writeBuf(stream.buf, digits[r2]) |
| 141 | stream.buf = writeBuf(stream.buf, digits[r1]) |
| 142 | return |
| 143 | } |
| 144 | r4 := q3 - q4*1000 |
| 145 | q5 := q4 / 1000 |
| 146 | if q5 == 0 { |
| 147 | stream.buf = writeFirstBuf(stream.buf, digits[q4]) |
| 148 | stream.buf = writeBuf(stream.buf, digits[r4]) |
| 149 | stream.buf = writeBuf(stream.buf, digits[r3]) |
| 150 | stream.buf = writeBuf(stream.buf, digits[r2]) |
| 151 | stream.buf = writeBuf(stream.buf, digits[r1]) |
| 152 | return |
| 153 | } |
| 154 | r5 := q4 - q5*1000 |
| 155 | q6 := q5 / 1000 |
| 156 | if q6 == 0 { |
| 157 | stream.buf = writeFirstBuf(stream.buf, digits[q5]) |
| 158 | } else { |
| 159 | stream.buf = writeFirstBuf(stream.buf, digits[q6]) |
| 160 | r6 := q5 - q6*1000 |
| 161 | stream.buf = writeBuf(stream.buf, digits[r6]) |
| 162 | } |
| 163 | stream.buf = writeBuf(stream.buf, digits[r5]) |
| 164 | stream.buf = writeBuf(stream.buf, digits[r4]) |
| 165 | stream.buf = writeBuf(stream.buf, digits[r3]) |
| 166 | stream.buf = writeBuf(stream.buf, digits[r2]) |
| 167 | stream.buf = writeBuf(stream.buf, digits[r1]) |
| 168 | } |
| 169 | |
| 170 | // WriteInt64 write int64 to stream |
| 171 | func (stream *Stream) WriteInt64(nval int64) { |
| 172 | var val uint64 |
| 173 | if nval < 0 { |
| 174 | val = uint64(-nval) |
| 175 | stream.buf = append(stream.buf, '-') |
| 176 | } else { |
| 177 | val = uint64(nval) |
| 178 | } |
| 179 | stream.WriteUint64(val) |
| 180 | } |
| 181 | |
| 182 | // WriteInt write int to stream |
| 183 | func (stream *Stream) WriteInt(val int) { |
| 184 | stream.WriteInt64(int64(val)) |
| 185 | } |
| 186 | |
| 187 | // WriteUint write uint to stream |
| 188 | func (stream *Stream) WriteUint(val uint) { |
| 189 | stream.WriteUint64(uint64(val)) |
| 190 | } |