UnsafeWriteVarUint32 writes a VarUint32 without grow check. Caller must have called Reserve(8) beforehand (8 for bulk uint64 write).
(value uint32)
| 540 | // UnsafeWriteVarUint32 writes a VarUint32 without grow check. |
| 541 | // Caller must have called Reserve(8) beforehand (8 for bulk uint64 write). |
| 542 | func (b *ByteBuffer) UnsafeWriteVarUint32(value uint32) int8 { |
| 543 | if value>>7 == 0 { |
| 544 | b.data[b.writerIndex] = byte(value) |
| 545 | b.writerIndex++ |
| 546 | return 1 |
| 547 | } |
| 548 | if value>>14 == 0 { |
| 549 | // Bulk write 2 bytes |
| 550 | encoded := uint16((value&0x7F)|0x80) | uint16(value>>7)<<8 |
| 551 | if isLittleEndian { |
| 552 | *(*uint16)(unsafe.Pointer(&b.data[b.writerIndex])) = encoded |
| 553 | } else { |
| 554 | b.data[b.writerIndex] = byte(encoded) |
| 555 | b.data[b.writerIndex+1] = byte(encoded >> 8) |
| 556 | } |
| 557 | b.writerIndex += 2 |
| 558 | return 2 |
| 559 | } |
| 560 | if value>>21 == 0 { |
| 561 | // Bulk write 4 bytes (only first 3 are valid varint data) |
| 562 | encoded := uint32((value&0x7F)|0x80) | |
| 563 | uint32(((value>>7)&0x7F)|0x80)<<8 | |
| 564 | uint32(value>>14)<<16 |
| 565 | if isLittleEndian { |
| 566 | *(*uint32)(unsafe.Pointer(&b.data[b.writerIndex])) = encoded |
| 567 | } else { |
| 568 | b.data[b.writerIndex] = byte(encoded) |
| 569 | b.data[b.writerIndex+1] = byte(encoded >> 8) |
| 570 | b.data[b.writerIndex+2] = byte(encoded >> 16) |
| 571 | } |
| 572 | b.writerIndex += 3 |
| 573 | return 3 |
| 574 | } |
| 575 | if value>>28 == 0 { |
| 576 | // Bulk write 4 bytes |
| 577 | encoded := uint32((value&0x7F)|0x80) | |
| 578 | uint32(((value>>7)&0x7F)|0x80)<<8 | |
| 579 | uint32(((value>>14)&0x7F)|0x80)<<16 | |
| 580 | uint32(value>>21)<<24 |
| 581 | if isLittleEndian { |
| 582 | *(*uint32)(unsafe.Pointer(&b.data[b.writerIndex])) = encoded |
| 583 | } else { |
| 584 | binary.LittleEndian.PutUint32(b.data[b.writerIndex:], encoded) |
| 585 | } |
| 586 | b.writerIndex += 4 |
| 587 | return 4 |
| 588 | } |
| 589 | // Bulk write 8 bytes (only first 5 are valid varint data) |
| 590 | encoded := uint64((value&0x7F)|0x80) | |
| 591 | uint64(((value>>7)&0x7F)|0x80)<<8 | |
| 592 | uint64(((value>>14)&0x7F)|0x80)<<16 | |
| 593 | uint64(((value>>21)&0x7F)|0x80)<<24 | |
| 594 | uint64(value>>28)<<32 |
| 595 | if isLittleEndian { |
| 596 | *(*uint64)(unsafe.Pointer(&b.data[b.writerIndex])) = encoded |
| 597 | } else { |
| 598 | binary.LittleEndian.PutUint64(b.data[b.writerIndex:], encoded) |
| 599 | } |