buildListpackWithBacklen builds a proper listpack with backlen values
(entries []listpackEntry)
| 562 | |
| 563 | // buildListpackWithBacklen builds a proper listpack with backlen values |
| 564 | func (enc *Encoder) buildListpackWithBacklen(entries []listpackEntry) []byte { |
| 565 | var listpackData []byte |
| 566 | var entrySizes []uint32 |
| 567 | |
| 568 | // First pass: encode entries and calculate sizes |
| 569 | for _, entry := range entries { |
| 570 | var encoded []byte |
| 571 | if entry.strVal != "" { |
| 572 | encoded = enc.encodeListPackString(entry.strVal) |
| 573 | } else { |
| 574 | encoded = enc.encodeListPackInt(entry.intVal) |
| 575 | } |
| 576 | listpackData = append(listpackData, encoded...) |
| 577 | entrySizes = append(entrySizes, uint32(len(encoded))) |
| 578 | } |
| 579 | |
| 580 | // Second pass: add backlen values |
| 581 | var finalListpack []byte |
| 582 | for i := len(entries) - 1; i >= 0; i-- { |
| 583 | // Add backlen |
| 584 | backlen := enc.encodeBacklen(entrySizes[i]) |
| 585 | finalListpack = append(backlen, finalListpack...) |
| 586 | // Add entry |
| 587 | entryStart := 0 |
| 588 | for j := 0; j < i; j++ { |
| 589 | entryStart += int(entrySizes[j]) |
| 590 | } |
| 591 | entryEnd := entryStart + int(entrySizes[i]) |
| 592 | finalListpack = append(listpackData[entryStart:entryEnd], finalListpack...) |
| 593 | } |
| 594 | |
| 595 | // Add header |
| 596 | totalBytes := len(finalListpack) + 6 // 6 bytes for header |
| 597 | header := make([]byte, 6) |
| 598 | binary.LittleEndian.PutUint32(header[0:4], uint32(totalBytes)) |
| 599 | binary.LittleEndian.PutUint16(header[4:6], uint16(len(entries))) |
| 600 | |
| 601 | return append(header, finalListpack...) |
| 602 | } |
| 603 | |
| 604 | // encodeBacklen encodes a backlen value |
| 605 | func (enc *Encoder) encodeBacklen(elementLen uint32) []byte { |
no test coverage detected