Skip to content
StrInitFromCstr

StrInitFromCstr

StrInitFromCstr

Description

Initializes a Str object from a C-style string (cstr) with a specified length (len). This macro creates a new Str object and copies up to len characters from cstr.

Parameters

Name Direction Description
cstr in Pointer to the null-terminated C-style string to initialize from.
len in The number of characters to copy from cstr.

Success

Returns a newly created Str object with its data field pointing to a newly allocated memory containing a copy of the first len characters of cstr. The length and capacity fields are set to len. copy_init and copy_deinit are set to NULL, and alignment is set to 1.

Failure

Returns a Str object with data set to NULL if memory allocation using ZstrDupN fails. In such a case, length and capacity will likely be uninitialized or zero. It’s crucial to check the data field for NULL after using this macro to handle potential memory allocation errors.

Usage example (Cross-references)

Usage examples (Cross-references)
    
        // Create Str from input data
        Str str  = StrInitFromCstr((const char *)(data + *offset), len);
        *offset += len;
                    if (cstr) {
                        StrDeinit(str);
                        *str = StrInitFromCstr(cstr, strlen(cstr));
                        free(cstr);
                    }
    
            // Create a temporary Str for parsing
            Str temp = StrInitFromCstr(start, i - start);
    
            // Try to parse as special value
    
        // Create a temporary Str for parsing
        Str temp = StrInitFromCstr(start, pos);
    
        // Validate the string is a proper floating point number
    
        // Create a temporary Str for parsing
        Str temp = StrInitFromCstr(start, pos);
    
        // Check for special prefixes with no digits
    
        // Create a temporary Str for parsing
        Str temp = StrInitFromCstr(start, pos);
    
        // Check for special prefixes with no digits
    
        // Create a temporary Str for parsing
        Str temp = StrInitFromCstr(start, pos);
    
        // Check for special prefixes with no digits
    
        // Create a temporary Str for parsing
        Str temp = StrInitFromCstr(start, pos);
    
        // Check for special prefixes with no digits
    
        // Create a temporary Str for parsing
        Str temp = StrInitFromCstr(start, pos);
    
        // Check for special prefixes with no digits
    
        // Create a temporary Str for parsing
        Str temp = StrInitFromCstr(start, pos);
    
        // Check for special prefixes with no digits
    
        // Create a temporary Str for parsing
        Str temp = StrInitFromCstr(start, pos);
    
        // Check for special prefixes with no digits
    
        // Create a temporary Str for parsing
        Str temp = StrInitFromCstr(start, pos);
    
        // Check for special prefixes with no digits
    
            // Parse hex string
            Str            hex_str = StrInitFromCstr(hex_start, i - hex_start);
            u64            value;
            StrParseConfig config = {.base = 16};
    
            // Parse octal string
            Str            oct_str = StrInitFromCstr(oct_start, i - oct_start);
            u64            value;
            StrParseConfig config = {.base = 8};
    
        // Create string from binary digits (already null-terminated by StrInitFromCstr)
        Str bin_str = StrInitFromCstr(bin_start, i - bin_start);
    
        // Convert to BitVec using the null-terminated string
        }
    
        Str temp   = StrInitFromCstr(start, i - start);
        Int parsed = IntFromStrRadix(temp.data, radix);
        }
    
        temp   = StrInitFromCstr(start, token_len);
        parsed = FloatFromStr(temp.data);
    
            // Create a temporary Str for parsing
            Str temp = StrInitFromCstr(start, i - start);
    
            // Try to parse as special value
    
        // Create a temporary Str for parsing
        Str temp = StrInitFromCstr(start, pos);
    
        // Validate the string is a proper floating point number
                const char *next = ZstrFindSubstring(prev, key);
                if (next) {
                    Str tmp = StrInitFromCstr(prev, next - prev);
                    VecPushBack(&sv, tmp); // exclude delimiter
                    prev = next + keylen;  // skip past delimiter
                } else {
                    if (ZstrCompareN(prev, key, end - prev)) {
                        Str tmp = StrInitFromCstr(prev, end - prev);
                        VecPushBack(&sv, tmp); // remaining part
                    }
    
        size new_len = end >= start ? (end - start + 1) : 0;
        return StrInitFromCstr(start, new_len);
    }
                    direntry.type = SYS_DIR_ENTRY_TYPE_UNKNOWN;
                }
                direntry.name = StrInitFromCstr(entry->d_name, NAMELEN(entry));
                VecPushBack(&dc, direntry);
            }
    // Test StrInitFromCstr function
    bool test_str_init_from_cstr(void) {
        WriteFmt("Testing StrInitFromCstr\n");
    
        const char *test_str = "Hello, World!";
        const char *test_str = "Hello, World!";
        size_t      len      = 5; // Just "Hello"
        Str         s        = StrInitFromCstr(test_str, len);
    
        // Validate the string
    
    #ifdef __cplusplus
    #    define StrInitFromCstr(cstr, len)                                                                                 \
            (Str {                                                                                                         \
                .data        = ZstrDupN((char *)(cstr), (len)),                                                            \
    ///           potential memory allocation errors.
    ///
    #define StrInitFromZstr(zstr) StrInitFromCstr((zstr), strlen(zstr))
    
    ///
    /// Initialize a Str object using another one
    ///
    #define StrInitFromStr(str) StrInitFromCstr((str)->data, (str)->length)
    #define StrDup(str)         StrInitFromStr(str)
Last updated on