ToWidth resizes the bit array to the specified size. If the specified width is shorter, bits on the right are truncated away. If the specified width is larger, zero bits are added on the right.
(desiredLen uint)
| 115 | // If the specified width is shorter, bits on the right are truncated away. |
| 116 | // If the specified width is larger, zero bits are added on the right. |
| 117 | func (d BitArray) ToWidth(desiredLen uint) BitArray { |
| 118 | bitlen := d.BitLen() |
| 119 | if bitlen == desiredLen { |
| 120 | // Nothing to do; fast path. |
| 121 | return d |
| 122 | } |
| 123 | if desiredLen == 0 { |
| 124 | // Nothing to do; fast path. |
| 125 | return BitArray{} |
| 126 | } |
| 127 | if desiredLen < bitlen { |
| 128 | // Destructive, we have to copy. |
| 129 | words, lastBitsUsed := EncodingPartsForBitLen(desiredLen) |
| 130 | copy(words, d.words[:len(words)]) |
| 131 | words[len(words)-1] &= (^word(0) << (numBitsPerWord - lastBitsUsed)) |
| 132 | return mustFromEncodingParts(words, lastBitsUsed) |
| 133 | } |
| 134 | |
| 135 | // New length is larger. |
| 136 | numWords, lastBitsUsed := SizesForBitLen(desiredLen) |
| 137 | var words []word |
| 138 | if numWords <= uint(cap(d.words)) { |
| 139 | words = d.words[0:numWords] |
| 140 | } else { |
| 141 | words = make([]word, numWords) |
| 142 | copy(words, d.words) |
| 143 | } |
| 144 | return mustFromEncodingParts(words, lastBitsUsed) |
| 145 | } |
| 146 | |
| 147 | // Sizeof returns the size in bytes of the bit array and its components. |
| 148 | func (d BitArray) Sizeof() uintptr { |
no test coverage detected