BitVecFromStr
Description
Parse a bitvector from a null-terminated string using an explicit allocator.
Parameters
| Name | Direction | Description |
|---|---|---|
str |
in | Input string containing 0 and 1 characters. |
alloc |
in | Allocator to bind to the returned bitvector. |
Success
Returns parsed bitvector.
Failure
Returns an empty bitvector on invalid input or allocation failure.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Io.c:3167:
);
*bv = BitVecFromStr(StrBegin(&bin_str), BitVecAllocator(bv));
StrDeinit(&bin_str);- In
Io.Write.c:530:
bool success = true;
BitVec bv1 = BitVecFromStr("10110", alloc_base);
StrAppendFmt(&output, "{}", bv1);
success = success && (ZstrCompare(StrBegin(&output), "10110") == 0); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecFromStr\n");
// Convert from string
// Test with empty string
BitVec empty_bv = BitVecFromStr("", ALLOCATOR_OF(&alloc));
result = result && (BitVecLen(&empty_bv) == 0); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecFromStr edge cases\n");
bool result = true;
// Test empty string
BitVec bv1 = BitVecFromStr("", ALLOCATOR_OF(&alloc));
result = result && (BitVecLen(&bv1) == 0);
BitVecDeinit(&bv1);
// Test single character
BitVec bv2 = BitVecFromStr("1", ALLOCATOR_OF(&alloc));
result = result && (BitVecLen(&bv2) == 1);
result = result && (BitVecGet(&bv2, 0) == true); long_str[1000] = '\0';
BitVec bv3 = BitVecFromStr(long_str, ALLOCATOR_OF(&alloc));
result = result && (BitVecLen(&bv3) == 1000);
result = result && (BitVecGet(&bv3, 0) == true);
for (size i = 0; i < sizeof(patterns) / sizeof(patterns[0]); i++) {
BitVec bv = BitVecFromStr(patterns[i], ALLOCATOR_OF(&alloc));
Str str = BitVecToStr(&bv);
for (size i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
BitVec bv = BitVecFromStr(test_cases[i].pattern, ALLOCATOR_OF(&alloc));
// Test string conversion consistency
// Test cross-format validation
BitVec bv1 = BitVecFromStr("11010110", ALLOCATOR_OF(&alloc));
BitVec bv2 = BitVecFromInteger(0xD6, 8, ALLOCATOR_OF(&alloc)); // Assuming MSB-first: 11010110 = 0xD6
BitVec bv3 = BitVecFromBytes((u8[]) {0xD6}, 8, ALLOCATOR_OF(&alloc)); large_pattern[2000] = '\0';
BitVec large_from_str = BitVecFromStr(large_pattern, ALLOCATOR_OF(&alloc));
result = result && (BitVecLen(&large_from_str) == 2000);
// Test NULL string - should abort
BitVecFromStr((Zstr)NULL, ALLOCATOR_OF(&alloc));
DefaultAllocatorDeinit(&alloc);
Last updated on