MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / parseFromBinary

Function parseFromBinary

pkg/util/bitarray/bitarray.go:328–358  ·  view source on GitHub ↗
(s string)

Source from the content-addressed store, hash-verified

326}
327
328func parseFromBinary(s string) (res BitArray, err error) {
329 words, lastBitsUsed := EncodingPartsForBitLen(uint(len(s)))
330
331 // Parse the bits.
332 wordIdx := 0
333 bitIdx := uint(0)
334 curWord := word(0)
335 for _, c := range s {
336 val := word(c - '0')
337 bitVal := val & 1
338 if bitVal != val {
339 // Note: the prefix "could not parse" is important as it is used
340 // to detect parsing errors in tests.
341 err := fmt.Errorf(`could not parse string as bit array: "%c" is not a valid binary digit`, c)
342 return res, pgerror.WithCandidateCode(err, pgcode.InvalidTextRepresentation)
343 }
344 curWord |= bitVal << (63 - bitIdx)
345 bitIdx = (bitIdx + 1) % numBitsPerWord
346 if bitIdx == 0 {
347 words[wordIdx] = curWord
348 curWord = 0
349 wordIdx++
350 }
351 }
352 if bitIdx > 0 {
353 // Ensure the last word is stored.
354 words[wordIdx] = curWord
355 }
356
357 return FromEncodingParts(words, lastBitsUsed)
358}
359
360func parseFromHex(s string) (res BitArray, err error) {
361 words, lastBitsUsed := EncodingPartsForBitLen(uint(len(s)) * 4)

Callers 1

ParseFunction · 0.85

Calls 4

WithCandidateCodeFunction · 0.92
EncodingPartsForBitLenFunction · 0.85
FromEncodingPartsFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…