Skip to content

StrReserve

Description

Reserve enough capacity to fit at least n characters without further allocation. Does not change the string length. See VecReserve for the full SUCCESS/FAILURE contract.

Usage example (Cross-references)

Usage examples (Cross-references)
    
            if (file_len > 0) {
                StrReserve(&buffer, (u64)file_len);
                i64 got = FileRead(file, StrBegin(&buffer), (u64)file_len);
                if (got < 0) {
        }
    
        if (!StrReserve(out, len)) {
            return false;
        }
        }
    
        if (!StrReserve(out, bv->length + 1)) {
            return false;
        }
        while (true) {
            u64 grown_to = StrLen(raw) + CHUNK + 1;
            if (!StrReserve(raw, grown_to))
                return false;
            i64 n = FileRead(f, StrEnd(raw), CHUNK);
        out->entries = VecInitT(out->entries, alloc);
        if (len) {
            if (!StrReserve(&out->raw, len + 1)) {
                ProcMapsDeinit(out);
                return false;
    
        Str a = StrInitFromCstr("ab", 2, &alloc);
        StrReserve(&a, 8);
        // Plant bytes beyond `a`'s logical length (still inside capacity 8) that
        // exceed the corresponding bytes of `b`. Only the mutant ever reads them.
    
        // Reserve more space than needed
        StrReserve(&s, 100);
    
        // Add some data
    // Test StrReserve function
    bool test_str_reserve(void) {
        WriteFmt("Testing StrReserve\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Reserve more space
        StrReserve(&s, 100);
    
        // Capacity should now be at least 100
    
        // Reserve less space (should be a no-op)
        StrReserve(&s, 50);
    
        // Capacity should still be at least 100
Last updated on