(s string)
| 358 | } |
| 359 | |
| 360 | func parseFromHex(s string) (res BitArray, err error) { |
| 361 | words, lastBitsUsed := EncodingPartsForBitLen(uint(len(s)) * 4) |
| 362 | |
| 363 | // Parse the bits. |
| 364 | wordIdx := 0 |
| 365 | bitIdx := uint(0) |
| 366 | curWord := word(0) |
| 367 | for _, c := range s { |
| 368 | var bitVal word |
| 369 | if c >= '0' && c <= '9' { |
| 370 | bitVal = word(c - '0') |
| 371 | } else if c >= 'a' && c <= 'f' { |
| 372 | bitVal = word(c-'a') + 10 |
| 373 | } else if c >= 'A' && c <= 'F' { |
| 374 | bitVal = word(c-'A') + 10 |
| 375 | } else { |
| 376 | // Note: the prefix "could not parse" is important as it is used |
| 377 | // to detect parsing errors in tests. |
| 378 | err := fmt.Errorf(`could not parse string as bit array: "%c" is not a valid hexadecimal digit`, c) |
| 379 | return res, pgerror.WithCandidateCode(err, pgcode.InvalidTextRepresentation) |
| 380 | } |
| 381 | curWord |= bitVal << (60 - bitIdx) |
| 382 | bitIdx = (bitIdx + 4) % numBitsPerWord |
| 383 | if bitIdx == 0 { |
| 384 | words[wordIdx] = curWord |
| 385 | curWord = 0 |
| 386 | wordIdx++ |
| 387 | } |
| 388 | } |
| 389 | if bitIdx > 0 { |
| 390 | // Ensure the last word is stored. |
| 391 | words[wordIdx] = curWord |
| 392 | } |
| 393 | |
| 394 | return FromEncodingParts(words, lastBitsUsed) |
| 395 | } |
| 396 | |
| 397 | // Concat concatenates two bit arrays. |
| 398 | func Concat(lhs, rhs BitArray) BitArray { |
no test coverage detected
searching dependent graphs…