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