Skip to content

StrInitCopy

Description

Deep-copy src into a freshly initialised dst, allocating through src’s inline allocator. Both ends carry independent backing storage afterwards.

Success

Returns true. *dst is a usable Str with the same contents and allocator as *src.

Failure

Returns false on allocator OOM. *dst is left zeroed.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    bool StrInitCopy(Str *dst, const Str *src) {
        if (!dst || !src) {
            LOG_FATAL("Invalid arguments");
    
        dst->type = src->type;
        StrInitCopy(&dst->name, &src->name);
    
        return dst;
        dst->value = StrInit((Allocator *)alloc);
    
        if (!StrInitCopy(&dst->key, &src->key)) {
            StrDeinit(&dst->key);
            return false;
            return false;
        }
        if (!StrInitCopy(&dst->value, &src->value)) {
            StrDeinit(&dst->key);
            StrDeinit(&dst->value);
    // Test StrInitCopy function
    bool test_str_init_copy(void) {
        WriteFmt("Testing StrInitCopy\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Copy src to dst
        bool success = StrInitCopy(&dst, &src);
    
        // Validate both strings
        Str  dup    = StrInitFromStr(&src, &alloc);
        Str  dst    = StrInit(&alloc);
        bool copied = StrInitCopy(&dst, &src);
    
        ValidateStr(&src);
Last updated on