Skip to content

StrResize

StrResize

Description

Resize string. If length is smaller than current capacity, string length is shrinked. If length is greater than current capacity, space is reserved and string is expanded.

Parameters

Name Direction Description
vec in,out Str to be resized.
len in New length of string.

Success

return

Failure

Does not return

Usage example (Cross-references)

Usage examples (Cross-references)
                if (*offset + 2 <= size) {
                    size_t new_size = extract_u16(data, offset, size) % 1000;
                    StrResize(str, new_size);
                }
                break;
            // 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", s.length, 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", s.length, 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", s.length, 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", s.length, 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", s.length, idx);
            }
    // Test StrResize function
    bool test_str_resize(void) {
        WriteFmt("Testing StrResize\n");
    
        Str s = StrInitFromZstr("Hello");
    
        // 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"
Last updated on