JoinLenPrefix() appends the items together separated by a single byte to represent the length of the segment
(toAppend ...[]byte)
| 773 | |
| 774 | // JoinLenPrefix() appends the items together separated by a single byte to represent the length of the segment |
| 775 | func JoinLenPrefix(toAppend ...[]byte) []byte { |
| 776 | // calculate total length first |
| 777 | totalLen := 0 |
| 778 | for _, item := range toAppend { |
| 779 | // if the item isn't empty, calculate the size |
| 780 | if item != nil { |
| 781 | // 1 byte for length + item length |
| 782 | totalLen += 1 + len(item) |
| 783 | } |
| 784 | } |
| 785 | // make the proper size buffer |
| 786 | res := make([]byte, 0, totalLen) |
| 787 | // iterate through each 'segment' and append it |
| 788 | for _, item := range toAppend { |
| 789 | // if item is empty, skip |
| 790 | if item == nil { |
| 791 | continue |
| 792 | } |
| 793 | // length |
| 794 | res = append(res, byte(len(item))) |
| 795 | // item |
| 796 | res = append(res, item...) |
| 797 | } |
| 798 | // return the result |
| 799 | return res |
| 800 | } |
| 801 | |
| 802 | // DecodeLengthPrefixed() decodes a key that is delimited by the length of the segment in a single byte |
| 803 | func DecodeLengthPrefixed(key []byte) (segments [][]byte) { |
no outgoing calls