Parse parses a bit array from the specified string.
(s string)
| 335 | |
| 336 | // Parse parses a bit array from the specified string. |
| 337 | func Parse(s string) (res BitArray, err error) { |
| 338 | if len(s) == 0 { |
| 339 | return res, nil |
| 340 | } |
| 341 | |
| 342 | words, lastBitsUsed := EncodingPartsForBitLen(uint(len(s))) |
| 343 | |
| 344 | // Parse the bits. |
| 345 | wordIdx := 0 |
| 346 | bitIdx := uint(0) |
| 347 | curWord := word(0) |
| 348 | for _, c := range s { |
| 349 | val := word(c - '0') |
| 350 | bitVal := val & 1 |
| 351 | if bitVal != val { |
| 352 | // Note: the prefix "could not parse" is important as it is used |
| 353 | // to detect parsing errors in tests. |
| 354 | err := fmt.Errorf(`could not parse string as bit array: "%c" is not a valid binary digit`, c) |
| 355 | return res, pgerror.WithCandidateCode(err, pgcode.InvalidTextRepresentation) |
| 356 | } |
| 357 | curWord |= bitVal << (63 - bitIdx) |
| 358 | bitIdx = (bitIdx + 1) % numBitsPerWord |
| 359 | if bitIdx == 0 { |
| 360 | words[wordIdx] = curWord |
| 361 | curWord = 0 |
| 362 | wordIdx++ |
| 363 | } |
| 364 | } |
| 365 | if bitIdx > 0 { |
| 366 | // Ensure the last word is stored. |
| 367 | words[wordIdx] = curWord |
| 368 | } |
| 369 | |
| 370 | return FromEncodingParts(words, lastBitsUsed) |
| 371 | } |
| 372 | |
| 373 | // Concat concatenates two bit arrays. |
| 374 | func Concat(lhs, rhs BitArray) BitArray { |
no test coverage detected