BitVecFromStr

Table of Contents

BitVecFromStr

Description

Create bitvector from string representation. String should contain only ‘1’ and ‘0’ characters.

Parameters

NameDirectionDescription
strinString containing bit pattern like “10110”

Usage example (from documentation)

  BitVec flags = BitVecFromString("10110");
  // ... use bitvector ...
  BitVecDeinit(&flags);

Usage example (Cross-references)

    
    // Convert to BitVec using the null-terminated string
    *bv = BitVecFromStr(bin_str.data);
    
    StrDeinit(&bin_str);
    }
    
    BitVec BitVecFromStr(const char *str) {
    if (!str) {
    LOG_FATAL("str is NULL");
    
    // Test 1: Basic binary formatting
    BitVec bv1 = BitVecFromStr("10110");
    StrWriteFmt(&output, "{}", bv1);
    success = success && (ZstrCompare(output.data, "10110") == 0);
    // Test BitVecFromStr function
    bool test_bitvec_from_string(void) {
    printf("Testing BitVecFromStr\n");
    
    // Convert from string
    // Convert from string
    const char* str = "1011";
    BitVec      bv  = BitVecFromStr(str);
    
    // Check result
    
    // Test with empty string
    BitVec empty_bv = BitVecFromStr("");
    result          = result && (empty_bv.length == 0);
    
    bool test_bitvec_from_string_edge_cases(void) {
    printf("Testing BitVecFromStr edge cases\n");
    
    bool result = true;
    
    // Test empty string
    BitVec bv1 = BitVecFromStr("");
    result     = result && (bv1.length == 0);
    BitVecDeinit(&bv1);
    
    // Test single character
    BitVec bv2 = BitVecFromStr("1");
    result     = result && (bv2.length == 1);
    result     = result && (BitVecGet(&bv2, 0) == true);
    long_str[1000] = '\0';
    
    BitVec bv3 = BitVecFromStr(long_str);
    result     = result && (bv3.length == 1000);
    result     = result && (BitVecGet(&bv3, 0) == true);
    
    for (size_t i = 0; i < sizeof(patterns) / sizeof(patterns[0]); i++) {
    BitVec bv  = BitVecFromStr(patterns[i]);
    Str    str = BitVecToStr(&bv);
    
    for (size_t i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); i++) {
    BitVec bv = BitVecFromStr(test_cases[i].pattern);
    
    // Test string conversion consistency
    
    // Test cross-format validation
    BitVec bv1 = BitVecFromStr("11010110");
    BitVec bv2 = BitVecFromInteger(0xD6, 8); // Assuming MSB-first: 11010110 = 0xD6
    BitVec bv3 = BitVecFromBytes((u8[]) {0xD6}, 8);
    large_pattern[2000] = '\0';
    
    BitVec large_from_str = BitVecFromStr(large_pattern);
    result                = result && (large_from_str.length == 2000);
    
    // Test NULL string - should abort
    BitVecFromStr(NULL);
    
    return false;

Share :

Related Posts

BitVecToStr

BitVecToStr Description Convert bitvector to string representation. Each bit becomes ‘1’ or ‘0’ character. Caller must free the returned string.

Read More

BitVecCompare

BitVecCompare Description Compare two bitvectors lexicographically. Comparison is done bit by bit from left to right.

Read More

BitVecEquals

BitVecEquals Description Test equality between two bitvectors. Two bitvectors are equal if they have the same length and all bits match.

Read More