Skip to content
StrInitFromStr

StrInitFromStr

Description

Deep-copy an existing Str by value. Length is taken from StrLen(str) and bytes are read via StrBegin(str), so embedded NULs in the source are preserved. The 1-arg form uses the enclosing MisraScope allocator; the 2-arg form takes an explicit allocator, which may differ from the source’s allocator.

Success

Returns a usable Str holding an independent copy of str’s contents. The returned handle does NOT share storage with str.

Failure

Returns an empty Str on allocator OOM. LOG_FATAL if the source handle is invalid.

Usage example (Cross-references)

Usage examples (Cross-references)
            }
            StrResize(&buffer, (size)len);
            *exe_path = StrInitFromStr(&buffer, alloc);
            got       = true;
        }
                data[len] = '\0';
                StrResize(&buffer, (size)len);
                *exe_path = StrInitFromStr(&buffer, alloc);
                got       = true;
            }
    // Test StrInitFromStr function
    bool test_str_init_from_str(void) {
        WriteFmt("Testing StrInitFromStr\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Str src = StrInitFromZstr("Hello, World!", &alloc);
        Str dst = StrInitFromStr(&src, &alloc);
    
        // Validate both strings
        Str src = StrInitFromCstr("Hello, World!", ZstrLen("Hello, World!"), &alloc);
    
        Str  dup    = StrInitFromStr(&src, &alloc);
        Str  dst    = StrInit(&alloc);
        bool copied = StrInitCopy(&dst, &src);
Last updated on