StrInitFromCstr
Description
Initialise a Str by value from a byte range [cstr, cstr + len). The 2-arg form uses the enclosing MisraScope allocator; the 3-arg form takes an explicit allocator (typed handle or raw Allocator *).
Success
Returns a usable Str holding the copied bytes.
Failure
Returns an empty Str on allocator OOM. The empty return is indistinguishable from len == 0; use StrTryInitFromCstr when OOM must be detected. LOG_FATAL if cstr is NULL.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Io.c:2546:
}
Str temp = StrInitFromCstr(start, (size)(StrIterIndex(&si) - StrIterIndex(&saved)), &scratch);
// Special-value tokens (`inf`, `nan`, ...) carry through the
- In
Io.c:2605:
size pos = StrIterIndex(&si) - StrIterIndex(&saved);
Str temp = StrInitFromCstr(start, pos, &scratch);
if (!is_valid_numeric_string(&temp, true)) {- In
Io.c:2666:
size pos = StrIterIndex(&si) - StrIterIndex(&saved);
Str temp = StrInitFromCstr(start, pos, &scratch);
// A bare base prefix ("0x", "0b", "0o") with no following digits is
- In
Io.c:2776:
Zstr start = StrIterDataAt(&si, StrIterIndex(&saved)); \
size pos = StrIterIndex(&si) - StrIterIndex(&saved); \
Str temp = StrInitFromCstr(start, pos, &scratch); \
\
if (StrLen(&temp) == 2 && StrBegin(&temp)[0] == '0' && \
- In
Io.c:3086:
}
Str hex_str = StrInitFromCstr(
StrIterDataAt(&si, StrIterIndex(&hex_saved)),
StrIterIndex(&si) - StrIterIndex(&hex_saved),- In
Io.c:3127:
}
Str oct_str = StrInitFromCstr(
StrIterDataAt(&si, StrIterIndex(&oct_saved)),
StrIterIndex(&si) - StrIterIndex(&oct_saved),- In
Io.c:3161:
}
Str bin_str = StrInitFromCstr(
StrIterDataAt(&si, StrIterIndex(&bin_saved)),
StrIterIndex(&si) - StrIterIndex(&bin_saved),- In
Io.c:3244:
}
Str temp = StrInitFromCstr(start, StrIterIndex(&si) - StrIterIndex(&saved), IntAllocator(value));
Int parsed = IntInit(IntAllocator(value));
bool ok = IntTryFromStrRadix(&parsed, StrBegin(&temp), radix);- In
Io.c:3309:
StrDeinit(&temp);
temp = StrInitFromCstr(start, token_len, FloatAllocator(value));
if (!FloatTryFromStr(&parsed, StrBegin(&temp))) {
StrDeinit(&temp);- In
Io.c:3365:
}
Str temp = StrInitFromCstr(start, (size)(StrIterIndex(&si) - StrIterIndex(&saved)), &scratch);
// Special-value tokens (`inf`, `nan`, ...) carry through the
- In
Io.c:3423:
size pos = StrIterIndex(&si) - StrIterIndex(&saved);
Str temp = StrInitFromCstr(start, pos, &scratch);
if (!is_valid_numeric_string(&temp, true)) {- In
Str.c:301:
Zstr next = ZstrFindSubstringN(prev, key, keylen);
if (next) {
Str tmp = StrInitFromCstr(prev, next - prev, s->allocator);
VecPushBack(&sv, tmp);
prev = next + keylen;- In
Str.c:306:
} else {
if (ZstrCompareN(prev, key, end - prev)) {
Str tmp = StrInitFromCstr(prev, end - prev, s->allocator);
VecPushBack(&sv, tmp);
}- In
Str.c:413:
// result bound to the same allocator.
if (s->length == 0) {
return StrInitFromCstr("", 0, s->allocator);
}- In
Str.c:433:
size new_len = end >= start ? (end - start + 1) : 0;
return StrInitFromCstr(start, new_len, s->allocator);
}- In
Proc.c:691:
Zstr exe = _dyld_get_image_name(0);
if (exe) {
*exe_path = StrInitFromCstr(exe, ZstrLen(exe), alloc);
return exe_path;
}- In
Dir.c:114:
}
direntry.name = StrInitFromCstr(findFileData.cFileName, ZstrLen(findFileData.cFileName), alloc);
VecPushBack(&dc, direntry);
} while (FindNextFile(hFind, &findFileData) != 0);- In
Dir.c:266:
DirEntry direntry = {0};
direntry.type = dirent_type_to_misra(de->d_type);
direntry.name = StrInitFromCstr(nm, name_len, alloc);
VecPushBack(&dc, direntry);
}- In
Dns.c:217:
HostsEntry e = {0};
e.name = StrInitFromCstr((Zstr)StrIterDataAt(&si, nm_start), nm_len, alloc);
ascii_lower((u8 *)StrBegin(&e.name), StrLen(&e.name));
if (got_v4) {- In
KvConfig.c:349:
}
lookup = StrInitFromCstr(key, ZstrLen(key), MapAllocator(cfg));
value = kvconfig_get_ptr_str(cfg, &lookup);
StrDeinit(&lookup);- In
KvConfig.c:362:
}
return StrInitFromCstr(StrBegin(value), StrLen(value), MapAllocator(cfg));
}- In
KvConfig.c:372:
}
return StrInitFromCstr(StrBegin(value), StrLen(value), MapAllocator(cfg));
}- In
VecStr.c:28:
// empty fresh Str.
Str copy = (StrLen(src) == 0) ? StrInit((Allocator *)alloc) :
StrInitFromCstr(StrBegin(src), StrLen(src), (Allocator *)alloc);
*(Str *)dst_ptr = copy;
return true;
static GraphNodeId city_add_intersection(CityGraph *graph, CityIndex *index, const Str *name, DefaultAllocator *alloc) {
GraphNodeId id = GraphAddNodeR(graph, StrInitFromCstr(StrBegin(name), StrLen(name), alloc));
Str key_copy = StrInitFromCstr(StrBegin(name), StrLen(name), alloc); GraphNodeId id = GraphAddNodeR(graph, StrInitFromCstr(StrBegin(name), StrLen(name), alloc));
Str key_copy = StrInitFromCstr(StrBegin(name), StrLen(name), alloc);
MapInsertR(index, key_copy, id);
return id;- In
Str.Init.c:44:
// Test StrInitFromCstr function
bool test_str_init_from_cstr(void) {
WriteFmt("Testing StrInitFromCstr\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Str.Init.c:50:
Zstr test_str = "Hello, World!";
size len = 5; // Just "Hello"
Str s = StrInitFromCstr(test_str, len, &alloc);
// Validate the string
- In
Str.Init.c:224:
DefaultAllocator alloc = DefaultAllocatorInit();
Str src = StrInitFromCstr("Hello, World!", ZstrLen("Hello, World!"), &alloc);
Str dup = StrInitFromStr(&src, &alloc);
Last updated on