MCPcopy Create free account
hub / github.com/dolthub/doltgresql / ToWidth

Method ToWidth

postgres/parser/utils/bitarray.go:131–159  ·  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

129// If the specified width is shorter, bits on the right are truncated away.
130// If the specified width is larger, zero bits are added on the right.
131func (d BitArray) ToWidth(desiredLen uint) BitArray {
132 bitlen := d.BitLen()
133 if bitlen == desiredLen {
134 // Nothing to do; fast path.
135 return d
136 }
137 if desiredLen == 0 {
138 // Nothing to do; fast path.
139 return BitArray{}
140 }
141 if desiredLen < bitlen {
142 // Destructive, we have to copy.
143 words, lastBitsUsed := EncodingPartsForBitLen(desiredLen)
144 copy(words, d.words[:len(words)])
145 words[len(words)-1] &= (^word(0) << (numBitsPerWord - lastBitsUsed))
146 return mustFromEncodingParts(words, lastBitsUsed)
147 }
148
149 // New length is larger.
150 numWords, lastBitsUsed := SizesForBitLen(desiredLen)
151 var words []word
152 if numWords <= uint(cap(d.words)) {
153 words = d.words[0:numWords]
154 } else {
155 words = make([]word, numWords)
156 copy(words, d.words)
157 }
158 return mustFromEncodingParts(words, lastBitsUsed)
159}
160
161// Sizeof returns the size in bytes of the bit array and its components.
162func (d BitArray) Sizeof() uintptr {

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected