|
1 |
| -// Copyright (C) 2019-2023 Algorand, Inc. |
| 1 | +// Copyright (C) 2018-2023 Algorand, Inc. |
2 | 2 | // This file is part of go-algorand
|
3 | 3 | //
|
4 | 4 | // go-algorand is free software: you can redistribute it and/or modify
|
@@ -72,7 +72,7 @@ func MakeNibbles(data []byte, oddLength bool) Nibbles {
|
72 | 72 | // [] -> [], false
|
73 | 73 | func Pack(nyb Nibbles) ([]byte, bool) {
|
74 | 74 | length := len(nyb)
|
75 |
| - data := make([]byte, length/2+length%2) |
| 75 | + data := make([]byte, length/2+length%2, length/2+length%2+1) |
76 | 76 | for i := 0; i < length; i++ {
|
77 | 77 | if i%2 == 0 {
|
78 | 78 | data[i/2] = nyb[i] << 4
|
@@ -131,19 +131,13 @@ func SharedPrefix(nyb1 Nibbles, nyb2 Nibbles) Nibbles {
|
131 | 131 | // [0x1, 0x2, 0x3, 0x4] -> [0x12, 0x34, 0x03]
|
132 | 132 | // [] -> [0x03]
|
133 | 133 | func Serialize(nyb Nibbles) (data []byte) {
|
134 |
| - p, h := Pack(nyb) |
135 |
| - length := len(p) |
136 |
| - output := make([]byte, length+1) |
137 |
| - copy(output, p) |
138 |
| - if h { |
| 134 | + if p, h := Pack(nyb); h { |
139 | 135 | // 0x01 is the odd length indicator
|
140 |
| - output[length] = oddIndicator |
| 136 | + return append(p, oddIndicator) |
141 | 137 | } else {
|
142 | 138 | // 0x03 is the even length indicator
|
143 |
| - output[length] = evenIndicator |
| 139 | + return append(p, evenIndicator) |
144 | 140 | }
|
145 |
| - |
146 |
| - return output |
147 | 141 | }
|
148 | 142 |
|
149 | 143 | // Deserialize returns a nibble array from the byte array.
|
|
0 commit comments