Skip to content

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)
                    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);
                }
                    return;
                }
                StrResize(&buffer, (size)got);
            }
                MemCopy(data, tok, n);
                data[n] = '\0';
                StrResize(&flagbuf, (size)n);
                flag = data;
            }
                data[1]    = *p;
                data[2]    = '\0';
                StrResize(&buf, 2);
    
                ArgSpec *sp = find_short(self, (Zstr)data);
                            data[1]    = tok[1];
                            data[2]    = '\0';
                            StrResize(&two, 2);
    
                            ArgSpec *first = find_short(self, (Zstr)data);
            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);
            // 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;
                    break;
                }
                StrResize(&host, ZstrLen(host_data));
                port = FROM_BIG_ENDIAN2(sa->sin_port);
                StrAppendFmt(&out, "{}:{}", (Zstr)host_data, (u32)port);
                    break;
                }
                StrResize(&host, ZstrLen(host_data));
                port = FROM_BIG_ENDIAN2(sa->sin6_port);
                StrAppendFmt(&out, "[{}]:{}", (Zstr)host_data, (u32)port);
                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;
                }
    
                StrResize(&tmpbuf, bytes_read);
                StrMergeR(buf, &tmpbuf);
                total_read += bytes_read;
                break;
            }
            StrResize(&buffer, (size)len);
            *exe_path = StrInitFromStr(&buffer, alloc);
            got       = true;
            if (len >= 0) {
                data[len] = '\0';
                StrResize(&buffer, (size)len);
                *exe_path = StrInitFromStr(&buffer, alloc);
                got       = true;
                return false;
            }
            StrResize(&out->raw, StrLen(&out->raw) + (u64)n);
            if (n < (i64)CHUNK)
                break; // EOF
            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;
        if (base[0] == '\0')
            return false;
        StrResize(out, 0);
        StrPushBackMany(out, binary_path);
        StrPushBackMany(out, ".dSYM/Contents/Resources/DWARF/");
                if (n == 0)
                    break;
                StrResize(&chunk, (size)n);
                StrMergeR(out, &chunk);
            }
    // Test StrResize function
    bool test_str_resize(void) {
        WriteFmt("Testing StrResize\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Resize to a smaller length
        StrResize(&s, 3);
    
        // Length should now be 3 and content should be "Hel"
    
        // 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