| 197 | } |
| 198 | |
| 199 | func (enc *Encoder) writeLength(value uint64) error { |
| 200 | var buf []byte |
| 201 | if value <= maxUint6 { |
| 202 | // 00 + 6 bits of data |
| 203 | enc.buffer[0] = byte(value) |
| 204 | buf = enc.buffer[0:1] |
| 205 | } else if value <= maxUint14 { |
| 206 | enc.buffer[0] = byte(value>>8) | len14BitMask // high 6 bit and mask(0x40) |
| 207 | enc.buffer[1] = byte(value) // low 8 bit |
| 208 | buf = enc.buffer[0:2] |
| 209 | } else if value <= math.MaxUint32 { |
| 210 | buf = make([]byte, 5) |
| 211 | buf[0] = len32Bit |
| 212 | binary.BigEndian.PutUint32(buf[1:], uint32(value)) |
| 213 | } else { |
| 214 | buf = make([]byte, 9) |
| 215 | buf[0] = len64Bit |
| 216 | binary.BigEndian.PutUint64(buf[1:], value) |
| 217 | } |
| 218 | return enc.write(buf) |
| 219 | } |
| 220 | |
| 221 | func (enc *Encoder) writeSimpleString(s string) error { |
| 222 | err := enc.writeLength(uint64(len(s))) |