MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / ToWidth

Method ToWidth

pkg/util/bitarray/bitarray.go:117–145  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
117func (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.
148func (d BitArray) Sizeof() uintptr {

Callers 1

PerformCastFunction · 0.80

Calls 4

BitLenMethod · 0.95
EncodingPartsForBitLenFunction · 0.85
mustFromEncodingPartsFunction · 0.85
SizesForBitLenFunction · 0.85

Tested by

no test coverage detected