github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/streamer_int.go (about) 1 package jzon 2 3 const ( 4 nSmalls = 1000 5 ) 6 7 var ( 8 digits [nSmalls]uint32 9 ) 10 11 func init() { 12 for i := uint32(0); i < nSmalls; i++ { 13 v := ((i/100 + '0') << 16) + (((i/10)%10 + '0') << 8) + (i%10 + '0') 14 if i < 10 { 15 v |= 2 << 24 16 } else if i < 100 { 17 v |= 1 << 24 18 } 19 digits[i] = v 20 } 21 } 22 23 func appendLeadingDigit(data []byte, v uint32) []byte { 24 start := v >> 24 25 if start == 0 { 26 return append(data, byte(v>>16), byte(v>>8), byte(v)) 27 } else if start == 1 { 28 return append(data, byte(v>>8), byte(v)) 29 } else { // start == 2 30 return append(data, byte(v)) 31 } 32 } 33 34 func appendDigit(data []byte, v uint32) []byte { 35 return append(data, byte(v>>16), byte(v>>8), byte(v)) 36 } 37 38 func appendUint8(b []byte, v uint8) []byte { 39 return appendLeadingDigit(b, digits[v]) 40 } 41 42 func appendInt8(b []byte, v int8) []byte { 43 if v < 0 { 44 b = append(b, '-') 45 return appendUint8(b, uint8(-v)) 46 } 47 return appendUint8(b, uint8(v)) 48 } 49 50 func (s *Streamer) quotedInt8(v int8) *Streamer { 51 if s.Error != nil { 52 return s 53 } 54 s.onVal() 55 s.buffer = append(s.buffer, '"') 56 s.buffer = appendInt8(s.buffer, v) 57 s.buffer = append(s.buffer, '"') 58 return s 59 } 60 61 // Int8 writes an int8 value 62 func (s *Streamer) Int8(v int8) *Streamer { 63 if s.Error != nil { 64 return s 65 } 66 s.onVal() 67 s.buffer = appendInt8(s.buffer, v) 68 return s 69 } 70 71 func (s *Streamer) quotedUint8(v uint8) *Streamer { 72 if s.Error != nil { 73 return s 74 } 75 s.onVal() 76 s.buffer = append(s.buffer, '"') 77 s.buffer = appendUint8(s.buffer, v) 78 s.buffer = append(s.buffer, '"') 79 return s 80 } 81 82 // Uint8 writes an uint8 value 83 func (s *Streamer) Uint8(v uint8) *Streamer { 84 if s.Error != nil { 85 return s 86 } 87 s.onVal() 88 s.buffer = appendUint8(s.buffer, v) 89 return s 90 } 91 92 func appendUint16(b []byte, v uint16) []byte { 93 q1 := v / nSmalls 94 if q1 == 0 { 95 return appendLeadingDigit(b, digits[v]) 96 } 97 r1 := v - q1*nSmalls 98 b = appendLeadingDigit(b, digits[q1]) 99 return appendDigit(b, digits[r1]) 100 } 101 102 func appendInt16(b []byte, v int16) []byte { 103 if v < 0 { 104 b = append(b, '-') 105 return appendUint16(b, uint16(-v)) 106 } 107 return appendUint16(b, uint16(v)) 108 } 109 110 func (s *Streamer) quotedInt16(v int16) *Streamer { 111 if s.Error != nil { 112 return s 113 } 114 s.onVal() 115 s.buffer = append(s.buffer, '"') 116 s.buffer = appendInt16(s.buffer, v) 117 s.buffer = append(s.buffer, '"') 118 return s 119 } 120 121 // Int16 writes an int16 value 122 func (s *Streamer) Int16(v int16) *Streamer { 123 if s.Error != nil { 124 return s 125 } 126 s.onVal() 127 s.buffer = appendInt16(s.buffer, v) 128 return s 129 } 130 131 func (s *Streamer) quotedUint16(v uint16) *Streamer { 132 if s.Error != nil { 133 return s 134 } 135 s.onVal() 136 s.buffer = append(s.buffer, '"') 137 s.buffer = appendUint16(s.buffer, v) 138 s.buffer = append(s.buffer, '"') 139 return s 140 } 141 142 // Uint16 writes an uint16 value 143 func (s *Streamer) Uint16(v uint16) *Streamer { 144 if s.Error != nil { 145 return s 146 } 147 s.onVal() 148 s.buffer = appendUint16(s.buffer, v) 149 return s 150 } 151 152 func appendUint32(b []byte, v uint32) []byte { 153 q1 := v / nSmalls 154 if q1 == 0 { 155 return appendLeadingDigit(b, digits[v]) 156 } 157 r1 := v - q1*nSmalls 158 q2 := q1 / nSmalls 159 if q2 == 0 { 160 b = appendLeadingDigit(b, digits[q1]) 161 return appendDigit(b, digits[r1]) 162 } 163 r2 := q1 - q2*nSmalls 164 q3 := q2 / nSmalls 165 if q3 == 0 { 166 b = appendLeadingDigit(b, digits[q2]) 167 } else { 168 r3 := q2 - q3*nSmalls 169 // max 10 digit for int32/uint32 170 b = append(b, byte(q3+'0')) 171 b = appendDigit(b, digits[r3]) 172 } 173 b = appendDigit(b, digits[r2]) 174 return appendDigit(b, digits[r1]) 175 } 176 177 func appendInt32(b []byte, v int32) []byte { 178 if v < 0 { 179 b = append(b, '-') 180 return appendUint32(b, uint32(-v)) 181 } 182 return appendUint32(b, uint32(v)) 183 } 184 185 func (s *Streamer) quotedInt32(v int32) *Streamer { 186 if s.Error != nil { 187 return s 188 } 189 s.onVal() 190 s.buffer = append(s.buffer, '"') 191 s.buffer = appendInt32(s.buffer, v) 192 s.buffer = append(s.buffer, '"') 193 return s 194 } 195 196 // Int32 writes an int32 value 197 func (s *Streamer) Int32(v int32) *Streamer { 198 if s.Error != nil { 199 return s 200 } 201 s.onVal() 202 s.buffer = appendInt32(s.buffer, v) 203 return s 204 } 205 206 func (s *Streamer) quotedUint32(v uint32) *Streamer { 207 if s.Error != nil { 208 return s 209 } 210 s.onVal() 211 s.buffer = append(s.buffer, '"') 212 s.buffer = appendUint32(s.buffer, v) 213 s.buffer = append(s.buffer, '"') 214 return s 215 } 216 217 // Uint32 writes an uint32 value 218 func (s *Streamer) Uint32(v uint32) *Streamer { 219 if s.Error != nil { 220 return s 221 } 222 s.onVal() 223 s.buffer = appendUint32(s.buffer, v) 224 return s 225 } 226 227 func appendUint64(b []byte, v uint64) []byte { 228 q1 := v / nSmalls 229 if q1 == 0 { 230 return appendLeadingDigit(b, digits[v]) 231 } 232 r1 := v - q1*nSmalls 233 q2 := q1 / nSmalls 234 if q2 == 0 { 235 b = appendLeadingDigit(b, digits[q1]) 236 } else { 237 r2 := q1 - q2*nSmalls 238 q3 := q2 / nSmalls 239 if q3 == 0 { 240 b = appendLeadingDigit(b, digits[q2]) 241 } else { 242 r3 := q2 - q3*nSmalls 243 q4 := q3 / nSmalls 244 if q4 == 0 { 245 b = appendLeadingDigit(b, digits[q3]) 246 } else { 247 r4 := q3 - q4*nSmalls 248 q5 := q4 / nSmalls 249 if q5 == 0 { 250 b = appendLeadingDigit(b, digits[q4]) 251 } else { 252 r5 := q4 - q5*nSmalls 253 q6 := q5 / nSmalls 254 if q6 == 0 { 255 b = appendLeadingDigit(b, digits[q5]) 256 } else { 257 b = appendLeadingDigit(b, digits[q6]) 258 r6 := q5 - q6*nSmalls 259 b = appendDigit(b, digits[r6]) 260 } 261 b = appendDigit(b, digits[r5]) 262 } 263 b = appendDigit(b, digits[r4]) 264 } 265 b = appendDigit(b, digits[r3]) 266 } 267 b = appendDigit(b, digits[r2]) 268 } 269 b = appendDigit(b, digits[r1]) 270 return b 271 } 272 273 func appendInt64(b []byte, v int64) []byte { 274 if v < 0 { 275 b = append(b, '-') 276 return appendUint64(b, uint64(-v)) 277 } 278 return appendUint64(b, uint64(v)) 279 } 280 281 func (s *Streamer) quotedInt64(v int64) *Streamer { 282 if s.Error != nil { 283 return s 284 } 285 s.onVal() 286 s.buffer = append(s.buffer, '"') 287 s.buffer = appendInt64(s.buffer, v) 288 s.buffer = append(s.buffer, '"') 289 return s 290 } 291 292 // Int64 writes an int64 value 293 func (s *Streamer) Int64(v int64) *Streamer { 294 if s.Error != nil { 295 return s 296 } 297 s.onVal() 298 s.buffer = appendInt64(s.buffer, v) 299 return s 300 } 301 302 func (s *Streamer) quotedUint64(v uint64) *Streamer { 303 if s.Error != nil { 304 return s 305 } 306 s.onVal() 307 s.buffer = append(s.buffer, '"') 308 s.buffer = appendUint64(s.buffer, v) 309 s.buffer = append(s.buffer, '"') 310 return s 311 } 312 313 // Uint64 writes an uint64 value 314 func (s *Streamer) Uint64(v uint64) *Streamer { 315 if s.Error != nil { 316 return s 317 } 318 s.onVal() 319 s.buffer = appendUint64(s.buffer, v) 320 return s 321 } 322 323 // Int writes an int value 324 func (s *Streamer) Int(v int) *Streamer { 325 return s.Int64(int64(v)) 326 } 327 328 // Uint writes an uint value 329 func (s *Streamer) Uint(v uint) *Streamer { 330 return s.Uint64(uint64(v)) 331 }