StrResize
Description
Resize the string to exactly len characters. Truncates when shrinking and allocates when growing; new characters (when growing) are zero-initialised. See VecResize for the full SUCCESS/FAILURE contract.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Io.c:623:
MemCopy(data, start, spec_len);
data[spec_len] = '\0';
StrResize(&spec_buf, (size)spec_len);
spec_ok = parse_format_spec(data, spec_len, &fmt_info);
}- In
Io.c:1140:
return;
}
StrResize(&buffer, (size)got);
}- In
ArgParse.c:503:
MemCopy(data, tok, n);
data[n] = '\0';
StrResize(&flagbuf, (size)n);
flag = data;
}- In
ArgParse.c:591:
data[1] = *p;
data[2] = '\0';
StrResize(&buf, 2);
ArgSpec *sp = find_short(self, (Zstr)data);- In
ArgParse.c:688:
data[1] = tok[1];
data[2] = '\0';
StrResize(&two, 2);
ArgSpec *first = find_short(self, (Zstr)data);- In
PdbCache.c:46:
return false;
StrResize(out_path, 0);
sys_append_dirname(out_path, pe_path);
if (StrLen(out_path) > 0) // (1) Build-ID
if (main->build_id && main->build_id_size > 0) {
StrResize(&path, 0);
StrPushBackMany(&path, "/usr/lib/debug/.build-id/");
append_build_id_path(&path, main->build_id, main->build_id_size);
// (2) {dir}/{name}
StrResize(&path, 0);
sys_append_dirname(&path, main_path);
StrPushBackR(&path, '/');
// (3) {dir}/.debug/{name}
StrResize(&path, 0);
sys_append_dirname(&path, main_path);
StrPushBackMany(&path, "/.debug/");
// (4) /usr/lib/debug{dir}/{name}
StrResize(&path, 0);
StrPushBackMany(&path, cand_prefix);
sys_append_dirname(&path, main_path);- In
Socket.c:401:
// split_host_port NUL-terminates; record the textual length so the
// Str is internally consistent before any read via StrBegin.
StrResize(&host, ZstrLen(host_data));
u16 port = 0;- In
Socket.c:472:
break;
}
StrResize(&host, ZstrLen(host_data));
port = FROM_BIG_ENDIAN2(sa->sin_port);
StrAppendFmt(&out, "{}:{}", (Zstr)host_data, (u32)port);- In
Socket.c:483:
break;
}
StrResize(&host, ZstrLen(host_data));
port = FROM_BIG_ENDIAN2(sa->sin6_port);
StrAppendFmt(&out, "[{}]:{}", (Zstr)host_data, (u32)port);- In
Proc.c:502:
ssize_t n = direct_sys3(MISRA_SYS_read, (long)(rfd), (long)(u64)(StrBegin(&tmpbuf)), (long)(1023));
if (n > 0) {
StrResize(&tmpbuf, (size)n);
StrMergeR(buf, &tmpbuf);
total_read += n;- In
Proc.c:549:
}
StrResize(&tmpbuf, bytes_read);
StrMergeR(buf, &tmpbuf);
total_read += bytes_read;- In
Proc.c:653:
break;
}
StrResize(&buffer, (size)len);
*exe_path = StrInitFromStr(&buffer, alloc);
got = true;- In
Proc.c:669:
if (len >= 0) {
data[len] = '\0';
StrResize(&buffer, (size)len);
*exe_path = StrInitFromStr(&buffer, alloc);
got = true;- In
ProcMaps.c:205:
return false;
}
StrResize(&out->raw, StrLen(&out->raw) + (u64)n);
if (n < (i64)CHUNK)
break; // EOF
- In
Dir.c:482:
MemCopy(data, path, n);
data[n] = 0;
StrResize(&buf, n);
// Skip leading slash so the loop doesn't try to mkdir("").
size i = (data[0] == '/') ? 1 : 0;- In
MachoCache.c:28:
if (base[0] == '\0')
return false;
StrResize(out, 0);
StrPushBackMany(out, binary_path);
StrPushBackMany(out, ".dSYM/Contents/Resources/DWARF/");- In
Dns.c:117:
if (n == 0)
break;
StrResize(&chunk, (size)n);
StrMergeR(out, &chunk);
}- In
Str.Memory.c:72:
// Test StrResize function
bool test_str_resize(void) {
WriteFmt("Testing StrResize\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Str.Memory.c:82:
// Resize to a smaller length
StrResize(&s, 3);
// Length should now be 3 and content should be "Hel"
- In
Str.Memory.c:88:
// Resize to a larger length
StrResize(&s, 8);
// Length should now be 8, and the first 3 characters should still be "Hel"
// But StrForeachInRangeIdx will continue until idx reaches original_length (12)
if (idx == 4) {
StrResize(&s, 3); // Shrink to only 3 characters
WriteFmt("String resized to length {}, idx={}...\n", StrLen(&s), idx);
} // but the string length is now smaller
if (idx == 10) {
StrResize(&s, 4); // Shrink to only 4 characters
WriteFmt("String resized to length {} during reverse iteration... idx = {}\n", StrLen(&s), idx);
} // This will make the current idx invalid after the body executes
if (idx == 4) {
StrResize(&s, 4); // Shrink to only 4 characters (valid indices: 0,1,2,3)
WriteFmt("String resized to length {}, current idx={} is now out of bounds...\n", StrLen(&s), idx);
} // When we reach idx=12, shrink the string significantly
if (idx == 12) {
StrResize(&s, 5); // Shrink to only 5 characters
WriteFmt("String resized to length {} during reverse ptr iteration... idx = {}\n", StrLen(&s), idx);
} // This will make subsequent iterations invalid
if (idx == 3) {
StrResize(&s, 2); // Shrink to only 2 characters
WriteFmt(
"String resized to length {}, but basic foreach iteration continues... idx = {}\n",
Last updated on