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)
- In
Proc.c:654:
}
StrResize(&buffer, (size)len);
*exe_path = StrInitFromStr(&buffer, alloc);
got = true;
}- In
Proc.c:670:
data[len] = '\0';
StrResize(&buffer, (size)len);
*exe_path = StrInitFromStr(&buffer, alloc);
got = true;
}- In
Str.Init.c:103:
// Test StrInitFromStr function
bool test_str_init_from_str(void) {
WriteFmt("Testing StrInitFromStr\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Str.Init.c:108:
Str src = StrInitFromZstr("Hello, World!", &alloc);
Str dst = StrInitFromStr(&src, &alloc);
// Validate both strings
- In
Str.Init.c:226:
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