github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/msgfmt/jsonfmt/encoder_int.go (about) 1 package jsonfmt 2 3 import ( 4 "unsafe" 5 "context" 6 ) 7 8 var digits []uint32 9 10 func init() { 11 digits = make([]uint32, 1000) 12 for i := uint32(0); i < 1000; i++ { 13 digits[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0' 14 if i < 10 { 15 digits[i] += 2 << 24 16 } else if i < 100 { 17 digits[i] += 1 << 24 18 } 19 } 20 } 21 22 type int8Encoder struct { 23 } 24 25 func (encoder *int8Encoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 26 return WriteInt8(space, *(*int8)(ptr)) 27 } 28 29 type uint8Encoder struct { 30 } 31 32 func (encoder *uint8Encoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 33 return WriteUint8(space, *(*uint8)(ptr)) 34 } 35 36 type int16Encoder struct { 37 } 38 39 func (encoder *int16Encoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 40 return WriteInt16(space, *(*int16)(ptr)) 41 } 42 43 type uint16Encoder struct { 44 } 45 46 func (encoder *uint16Encoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 47 return WriteUint16(space, *(*uint16)(ptr)) 48 } 49 50 type int32Encoder struct { 51 } 52 53 func (encoder *int32Encoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 54 return WriteInt32(space, *(*int32)(ptr)) 55 } 56 57 type uint32Encoder struct { 58 } 59 60 func (encoder *uint32Encoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 61 return WriteUint32(space, *(*uint32)(ptr)) 62 } 63 64 type int64Encoder struct { 65 } 66 67 func (encoder *int64Encoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 68 return WriteInt64(space, *(*int64)(ptr)) 69 } 70 71 type uint64Encoder struct { 72 } 73 74 func (encoder *uint64Encoder) Encode(ctx context.Context, space []byte, ptr unsafe.Pointer) []byte { 75 return WriteUint64(space, *(*uint64)(ptr)) 76 } 77 78 func WriteUint8(space []byte, val uint8) []byte { 79 return writeFirstBuf(space, digits[val]) 80 } 81 82 func WriteInt8(space []byte, nval int8) []byte { 83 var val uint8 84 if nval < 0 { 85 val = uint8(-nval) 86 space = append(space, '-') 87 } else { 88 val = uint8(nval) 89 } 90 return writeFirstBuf(space, digits[val]) 91 } 92 93 func WriteInt16(space []byte, nval int16) []byte { 94 var val uint16 95 if nval < 0 { 96 val = uint16(-nval) 97 space = append(space, '-') 98 } else { 99 val = uint16(nval) 100 } 101 return WriteUint16(space, val) 102 } 103 104 func WriteUint16(space []byte, val uint16) []byte { 105 q1 := val / 1000 106 if q1 == 0 { 107 return writeFirstBuf(space, digits[val]) 108 } 109 r1 := val - q1*1000 110 space = writeFirstBuf(space, digits[q1]) 111 return writeBuf(space, digits[r1]) 112 } 113 114 func WriteInt32(space []byte, nval int32) []byte { 115 var val uint32 116 if nval < 0 { 117 val = uint32(-nval) 118 space = append(space, '-') 119 } else { 120 val = uint32(nval) 121 } 122 return WriteUint32(space, val) 123 } 124 125 func WriteUint32(space []byte, val uint32) []byte { 126 q1 := val / 1000 127 if q1 == 0 { 128 return writeFirstBuf(space, digits[val]) 129 } 130 r1 := val - q1*1000 131 q2 := q1 / 1000 132 if q2 == 0 { 133 space = writeFirstBuf(space, digits[q1]) 134 return writeBuf(space, digits[r1]) 135 } 136 r2 := q1 - q2*1000 137 q3 := q2 / 1000 138 if q3 == 0 { 139 space = writeFirstBuf(space, digits[q2]) 140 } else { 141 r3 := q2 - q3*1000 142 space = append(space, byte(q3 + '0')) 143 return writeBuf(space, digits[r3]) 144 } 145 space = writeBuf(space, digits[r2]) 146 return writeBuf(space, digits[r1]) 147 } 148 149 func WriteInt64(space []byte, nval int64) []byte { 150 var val uint64 151 if nval < 0 { 152 val = uint64(-nval) 153 space = append(space, '-') 154 } else { 155 val = uint64(nval) 156 } 157 return WriteUint64(space, val) 158 } 159 160 func WriteUint64(space []byte, val uint64) []byte { 161 q1 := val / 1000 162 if q1 == 0 { 163 return writeFirstBuf(space, digits[val]) 164 } 165 r1 := val - q1*1000 166 q2 := q1 / 1000 167 if q2 == 0 { 168 space = writeFirstBuf(space, digits[q1]) 169 space = writeBuf(space, digits[r1]) 170 return space 171 } 172 r2 := q1 - q2*1000 173 q3 := q2 / 1000 174 if q3 == 0 { 175 space = writeFirstBuf(space, digits[q2]) 176 space = writeBuf(space, digits[r2]) 177 space = writeBuf(space, digits[r1]) 178 return space 179 } 180 r3 := q2 - q3*1000 181 q4 := q3 / 1000 182 if q4 == 0 { 183 space = writeFirstBuf(space, digits[q3]) 184 space = writeBuf(space, digits[r3]) 185 space = writeBuf(space, digits[r2]) 186 space = writeBuf(space, digits[r1]) 187 return space 188 } 189 r4 := q3 - q4*1000 190 q5 := q4 / 1000 191 if q5 == 0 { 192 space = writeFirstBuf(space, digits[q4]) 193 space = writeBuf(space, digits[r4]) 194 space = writeBuf(space, digits[r3]) 195 space = writeBuf(space, digits[r2]) 196 space = writeBuf(space, digits[r1]) 197 return space 198 } 199 r5 := q4 - q5*1000 200 q6 := q5 / 1000 201 if q6 == 0 { 202 space = writeFirstBuf(space, digits[q5]) 203 } else { 204 space = append(space, byte(q6 + '0')) 205 r6 := q5 - q6*1000 206 space = writeBuf(space, digits[r6]) 207 } 208 space = writeBuf(space, digits[r5]) 209 space = writeBuf(space, digits[r4]) 210 space = writeBuf(space, digits[r3]) 211 space = writeBuf(space, digits[r2]) 212 space = writeBuf(space, digits[r1]) 213 return space 214 } 215 216 217 func writeFirstBuf(space []byte, v uint32) []byte { 218 start := v >> 24 219 if start == 0 { 220 space = append(space, byte(v >> 16), byte(v >> 8)) 221 } else if start == 1 { 222 space = append(space, byte(v >> 8)) 223 } 224 space = append(space, byte(v)) 225 return space 226 } 227 228 func writeBuf(space []byte, v uint32) []byte { 229 return append(space, byte(v >> 16), byte(v >> 8), byte(v)) 230 }